/[svn]/gigedit/trunk/src/gigedit/scripteditor.cpp
ViewVC logotype

Diff of /gigedit/trunk/src/gigedit/scripteditor.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3310 by schoenebeck, Sat Jul 15 12:02:21 2017 UTC revision 3368 by schoenebeck, Thu Nov 16 19:18:42 2017 UTC
# Line 7  Line 7 
7    
8  #include "scripteditor.h"  #include "scripteditor.h"
9  #include "global.h"  #include "global.h"
10    #include "compat.h"
11  #include <gtk/gtkwidget.h> // for gtk_widget_modify_*()  #include <gtk/gtkwidget.h> // for gtk_widget_modify_*()
12    #if defined(__APPLE__)
13    # include "MacHelper.h"
14    #endif
15    #include <math.h> // for log10()
16    
17  #if !USE_LS_SCRIPTVM  #if !USE_LS_SCRIPTVM
18    
# Line 61  static Glib::RefPtr<Gdk::Pixbuf> createI Line 66  static Glib::RefPtr<Gdk::Pixbuf> createI
66    
67  ScriptEditor::ScriptEditor() :  ScriptEditor::ScriptEditor() :
68      m_statusLabel("",  Gtk::ALIGN_START),      m_statusLabel("",  Gtk::ALIGN_START),
69    #if HAS_GTKMM_STOCK
70      m_applyButton(Gtk::Stock::APPLY),      m_applyButton(Gtk::Stock::APPLY),
71      m_cancelButton(Gtk::Stock::CANCEL)      m_cancelButton(Gtk::Stock::CANCEL)
72    #else
73        m_applyButton(_("_Apply"), true),
74        m_cancelButton(_("_Cancel"), true)
75    #endif
76  {  {
77      m_script = NULL;      m_script = NULL;
78  #if USE_LS_SCRIPTVM  #if USE_LS_SCRIPTVM
# Line 157  ScriptEditor::ScriptEditor() : Line 167  ScriptEditor::ScriptEditor() :
167      m_tagTable->add(m_lineNrTag);      m_tagTable->add(m_lineNrTag);
168    
169      // create menu      // create menu
170    #if USE_GTKMM_BUILDER
171        m_actionGroup = Gio::SimpleActionGroup::create();
172        m_actionGroup->add_action(
173            "Apply", sigc::mem_fun(*this, &ScriptEditor::onButtonApply)
174        );
175        m_actionGroup->add_action(
176            "Close", sigc::mem_fun(*this, &ScriptEditor::onButtonCancel)
177        );
178        m_actionGroup->add_action(
179            "ChangeFont", sigc::mem_fun(*this, &ScriptEditor::onMenuChangeFontSize)
180        );
181        insert_action_group("ScriptEditor", m_actionGroup);
182    
183        m_uiManager = Gtk::Builder::create();
184        Glib::ustring ui_info =
185            "<interface>"
186            "  <menubar id='MenuBar'>"
187            "    <menu id='MenuScript'>"
188            "      <section>"
189            "        <item id='Apply'>"
190            "          <attribute name='label' translatable='yes'>_Apply</attribute>"
191            "          <attribute name='action'>ScriptEditor.Apply</attribute>"
192            "          <attribute name='accel'>&lt;Primary&gt;s</attribute>"
193            "        </item>"
194            "      </section>"
195            "      <section>"
196            "        <item id='Close'>"
197            "          <attribute name='label' translatable='yes'>_Close</attribute>"
198            "          <attribute name='action'>ScriptEditor.Close</attribute>"
199            "          <attribute name='accel'>&lt;Primary&gt;q</attribute>"
200            "        </item>"
201            "      </section>"
202            "    </menu>"
203            "    <menu id='MenuEditor'>"
204            "      <section>"
205            "        <item id='ChangeFont'>"
206            "          <attribute name='label' translatable='yes'>_Font Size ...</attribute>"
207            "          <attribute name='action'>ScriptEditor.ChangeFont</attribute>"
208            "        </item>"
209            "      </section>"
210            "    </menu>"
211            "  </menubar>"
212            "</interface>";
213        m_uiManager->add_from_string(ui_info);
214        /*{
215            auto object = uiManager->get_object("MenuBar");
216            auto gmenu = Glib::RefPtr<Gio::Menu>::cast_dynamic(object);
217            set_menubar(gmenu);
218        }*/
219    #else
220      m_actionGroup = Gtk::ActionGroup::create();      m_actionGroup = Gtk::ActionGroup::create();
221      m_actionGroup->add(Gtk::Action::create("MenuScript", _("_Script")));      m_actionGroup->add(Gtk::Action::create("MenuScript", _("_Script")));
222      m_actionGroup->add(Gtk::Action::create("Apply", _("_Apply")),      m_actionGroup->add(Gtk::Action::create("Apply", _("_Apply")),
# Line 185  ScriptEditor::ScriptEditor() : Line 245  ScriptEditor::ScriptEditor() :
245          "  </menubar>"          "  </menubar>"
246          "</ui>"          "</ui>"
247      );      );
248    #endif
249    
250      m_lineNrBuffer = Gtk::TextBuffer::create(m_tagTable);      m_lineNrBuffer = Gtk::TextBuffer::create(m_tagTable);
251      m_lineNrView.set_buffer(m_lineNrBuffer);      m_lineNrView.set_buffer(m_lineNrBuffer);
# Line 192  ScriptEditor::ScriptEditor() : Line 253  ScriptEditor::ScriptEditor() :
253      m_lineNrView.set_right_margin(3);      m_lineNrView.set_right_margin(3);
254      m_lineNrTextViewSpacer.set_size_request(5);      m_lineNrTextViewSpacer.set_size_request(5);
255      {      {
256    #if 1 //(GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
257          Gdk::Color color;          Gdk::Color color;
258    #else
259            Gdk::RGBA color;
260    #endif
261          color.set("#F5F5F5");          color.set("#F5F5F5");
262          GtkWidget* widget = (GtkWidget*) m_lineNrView.gobj();          GtkWidget* widget = (GtkWidget*) m_lineNrView.gobj();
263    #if GTK_MAJOR_VERSION < 3 || (GTK_MAJOR_VERSION == 3 && GTK_MINOR_VERSION <= 22)
264          gtk_widget_modify_base(widget, GTK_STATE_NORMAL, color.gobj());          gtk_widget_modify_base(widget, GTK_STATE_NORMAL, color.gobj());
265          gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, color.gobj());          gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, color.gobj());
266    #endif
267      }      }
268      {      {
269    #if 1 //(GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
270          Gdk::Color color;          Gdk::Color color;
271    #else
272            Gdk::RGBA color;
273    #endif
274          color.set("#EEEEEE");          color.set("#EEEEEE");
275          GtkWidget* widget = (GtkWidget*) m_lineNrTextViewSpacer.gobj();          GtkWidget* widget = (GtkWidget*) m_lineNrTextViewSpacer.gobj();
276    #if GTK_MAJOR_VERSION < 3 || (GTK_MAJOR_VERSION == 3 && GTK_MINOR_VERSION <= 22)
277          gtk_widget_modify_base(widget, GTK_STATE_NORMAL, color.gobj());          gtk_widget_modify_base(widget, GTK_STATE_NORMAL, color.gobj());
278          gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, color.gobj());          gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, color.gobj());
279    #endif
280      }      }
281      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);
282      m_textView.set_buffer(m_textBuffer);      m_textView.set_buffer(m_textBuffer);
# Line 215  ScriptEditor::ScriptEditor() : Line 288  ScriptEditor::ScriptEditor() :
288      m_scrolledWindow.add(m_textViewHBox);      m_scrolledWindow.add(m_textViewHBox);
289      m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);      m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
290    
291    #if USE_GTKMM_BUILDER
292        Gtk::Widget* menuBar = new Gtk::MenuBar(
293            Glib::RefPtr<Gio::Menu>::cast_dynamic(
294                m_uiManager->get_object("MenuBar")
295            )
296        );
297    #else
298      Gtk::Widget* menuBar = m_uiManager->get_widget("/MenuBar");      Gtk::Widget* menuBar = m_uiManager->get_widget("/MenuBar");
299    #endif
300    
301      m_vbox.pack_start(*menuBar, Gtk::PACK_SHRINK);      m_vbox.pack_start(*menuBar, Gtk::PACK_SHRINK);
302      m_vbox.pack_start(m_scrolledWindow);      m_vbox.pack_start(m_scrolledWindow);
303    
# Line 226  ScriptEditor::ScriptEditor() : Line 308  ScriptEditor::ScriptEditor() :
308      m_applyButton.set_sensitive(false);      m_applyButton.set_sensitive(false);
309      m_applyButton.grab_focus();      m_applyButton.grab_focus();
310    
311  #if GTKMM_MAJOR_VERSION >= 3  #if GTKMM_MAJOR_VERSION < 3
312        m_statusHBox.set_spacing(6);
313    #elif GTKMM_MAJOR_VERSION == 3 && GTKMM_MINOR_VERSION < 12
314      m_statusImage.set_margin_left(6);      m_statusImage.set_margin_left(6);
315      m_statusImage.set_margin_right(6);      m_statusImage.set_margin_right(6);
316  #else  #else
317      m_statusHBox.set_spacing(6);      m_statusImage.set_margin_start(6);
318        m_statusImage.set_margin_end(6);
319  #endif  #endif
320    
321      m_statusHBox.pack_start(m_statusImage, Gtk::PACK_SHRINK);      m_statusHBox.pack_start(m_statusImage, Gtk::PACK_SHRINK);
322      m_statusHBox.pack_start(m_statusLabel);      m_statusHBox.pack_start(m_statusLabel);
323    #if HAS_GTKMM_SHOW_ALL_CHILDREN
324      m_statusHBox.show_all_children();      m_statusHBox.show_all_children();
325    #endif
326    
327      m_footerHBox.pack_start(m_statusHBox);      m_footerHBox.pack_start(m_statusHBox);
328      m_footerHBox.pack_start(m_buttonBox, Gtk::PACK_SHRINK);      m_footerHBox.pack_start(m_buttonBox, Gtk::PACK_SHRINK);
# Line 267  ScriptEditor::ScriptEditor() : Line 354  ScriptEditor::ScriptEditor() :
354      );      );
355    
356      signal_delete_event().connect(      signal_delete_event().connect(
357    #if GTKMM_MAJOR_VERSION > 3 || (GTKMM_MAJOR_VERSION == 3 && (GTKMM_MINOR_VERSION > 91 || (GTKMM_MINOR_VERSION == 91 && GTKMM_MICRO_VERSION >= 2))) // GTKMM >= 3.91.2
358          sigc::mem_fun(*this, &ScriptEditor::onWindowDelete)          sigc::mem_fun(*this, &ScriptEditor::onWindowDelete)
359    #else
360            sigc::mem_fun(*this, &ScriptEditor::onWindowDeleteP)
361    #endif
362      );      );
363    
364    #if HAS_GTKMM_SHOW_ALL_CHILDREN
365      show_all_children();      show_all_children();
366    #endif
367  }  }
368    
369  ScriptEditor::~ScriptEditor() {  ScriptEditor::~ScriptEditor() {
# Line 282  ScriptEditor::~ScriptEditor() { Line 375  ScriptEditor::~ScriptEditor() {
375    
376  int ScriptEditor::currentFontSize() const {  int ScriptEditor::currentFontSize() const {
377  #if defined(__APPLE__)  #if defined(__APPLE__)
378      const int defaultFontSize = 13;      const int defaultFontSize = 11;
379  #else  #else
380      const int defaultFontSize = 10;      const int defaultFontSize = 10;
381  #endif  #endif
# Line 291  int ScriptEditor::currentFontSize() cons Line 384  int ScriptEditor::currentFontSize() cons
384      return fontSize;      return fontSize;
385  }  }
386    
387  void ScriptEditor::setFontSize(int size, bool save) {  void ScriptEditor::setFontSize(int sizePt, bool save) {
388      //printf("setFontSize(%d,%d)\n", size, save);      //printf("setFontSize(%d,%d)\n", size, save);
389    
390        // make sure the real size on the screen for the editor's font is consistent
391        // on all screens (which otherwise may vary between models and DPI settings)
392        const double referenceDPI = 96;
393        double dpi = Gdk::Screen::get_default()->get_resolution();
394        double sizePx = sizePt * dpi / referenceDPI;
395    
396    #if GTKMM_MAJOR_VERSION < 3 || (GTKMM_MAJOR_VERSION == 3 && GTKMM_MINOR_VERSION < 20)
397      Pango::FontDescription fdesc;      Pango::FontDescription fdesc;
398      fdesc.set_family("monospace");      fdesc.set_family("monospace");
399      fdesc.set_size(size * PANGO_SCALE);  # if defined(__APPLE__)
400  #if GTKMM_MAJOR_VERSION < 3      // fixes poor readability of default monospace font on Macs
401        if (macIsMinMac10_6())
402            fdesc.set_family("Menlo");
403    # endif
404        fdesc.set_size(sizePx * PANGO_SCALE);
405    # if GTKMM_MAJOR_VERSION < 3
406      m_lineNrView.modify_font(fdesc);      m_lineNrView.modify_font(fdesc);
407      m_textView.modify_font(fdesc);      m_textView.modify_font(fdesc);
408  #else  # else
409      m_lineNrView.override_font(fdesc);      m_lineNrView.override_font(fdesc);
410      m_textView.override_font(fdesc);      m_textView.override_font(fdesc);
411    # endif
412    #else
413        Glib::ustring family = "monospace";
414    # if defined(__APPLE__)
415        // fixes poor readability of default monospace font on Macs
416        if (macIsMinMac10_6())
417            family = "Menlo";
418    # endif
419        if (!m_css) {
420            m_css = Gtk::CssProvider::create();
421            m_lineNrView.get_style_context()->add_provider(m_css, GTK_STYLE_PROVIDER_PRIORITY_FALLBACK);
422            m_textView.get_style_context()->add_provider(m_css, GTK_STYLE_PROVIDER_PRIORITY_FALLBACK);
423        }
424        m_css->load_from_data(
425            "* {"
426            "  font: " + ToString(sizePt) + "pt " + family + ";"
427            "}"
428        );
429  #endif  #endif
430      if (save) Settings::singleton()->scriptEditorFontSize = size;      if (save) Settings::singleton()->scriptEditorFontSize = sizePt;
431  }  }
432    
433  void ScriptEditor::setScript(gig::Script* script) {  void ScriptEditor::setScript(gig::Script* script) {
# Line 325  void ScriptEditor::updateLineNumbers() { Line 449  void ScriptEditor::updateLineNumbers() {
449      const int n = m_textBuffer->get_line_count();      const int n = m_textBuffer->get_line_count();
450      const int old = m_lineNrBuffer->get_line_count();      const int old = m_lineNrBuffer->get_line_count();
451      if (n == old) return;      if (n == old) return;
452        const int digits = log10(n) + 1;
453        const int bufSz = digits + 2;
454        char* buf = new char[bufSz];
455        std::string sFmt1 =   "%" + ToString(digits) + "d";
456        std::string sFmt2 = "\n%" + ToString(digits) + "d";
457      Glib::ustring s;      Glib::ustring s;
458      for (int i = 0; i < n; ++i) {      for (int i = 0; i < n; ++i) {
459          if (i) s += "\n";          snprintf(buf, bufSz, i ? sFmt2.c_str() : sFmt1.c_str(), i+1);
460          s += ToString(i+1);          s += buf;
461      }      }
462      m_lineNrBuffer->remove_all_tags(m_lineNrBuffer->begin(), m_lineNrBuffer->end());      m_lineNrBuffer->remove_all_tags(m_lineNrBuffer->begin(), m_lineNrBuffer->end());
463      m_lineNrBuffer->set_text(s);      m_lineNrBuffer->set_text(s);
464      m_lineNrBuffer->apply_tag(m_lineNrTag, m_lineNrBuffer->begin(), m_lineNrBuffer->end());      m_lineNrBuffer->apply_tag(m_lineNrTag, m_lineNrBuffer->begin(), m_lineNrBuffer->end());
465        if (buf) delete[] buf;
466  }  }
467    
468  void ScriptEditor::onTextInserted(const Gtk::TextBuffer::iterator& itEnd, const Glib::ustring& txt, int length) {  void ScriptEditor::onTextInserted(const Gtk::TextBuffer::iterator& itEnd, const Glib::ustring& txt, int length) {
# Line 619  bool ScriptEditor::on_motion_notify_even Line 749  bool ScriptEditor::on_motion_notify_even
749      //TODO: event throttling would be a good idea here      //TODO: event throttling would be a good idea here
750      updateIssueTooltip(e);      updateIssueTooltip(e);
751  #endif  #endif
752    #if GTKMM_MAJOR_VERSION < 3 || (GTKMM_MAJOR_VERSION == 3 && GTKMM_MINOR_VERSION <= 22)
753      return ManagedWindow::on_motion_notify_event(e);      return ManagedWindow::on_motion_notify_event(e);
754    #else
755        Gdk::EventMotion em = Glib::wrap(e, true);
756        return ManagedWindow::on_motion_notify_event(em);
757    #endif
758  }  }
759    
760  void ScriptEditor::onMenuChangeFontSize() {  void ScriptEditor::onMenuChangeFontSize() {
761      //TODO: for GTKMM >= 3.2 class Gtk::FontChooser could be used instead      //TODO: for GTKMM >= 3.2 class Gtk::FontChooser could be used instead
762      Gtk::Dialog dialog(_("Font Size"), true /*modal*/);      Gtk::Dialog dialog(_("Font Size"), true /*modal*/);
763      Gtk::HBox hbox;      HBox hbox;
764      hbox.set_spacing(6);      hbox.set_spacing(6);
765    
766      Gtk::Label label(_("Editor's Font Size:"), Gtk::ALIGN_START);      Gtk::Label label(_("Editor's Font Size:"), Gtk::ALIGN_START);
# Line 637  void ScriptEditor::onMenuChangeFontSize( Line 772  void ScriptEditor::onMenuChangeFontSize(
772      spinButton.set_value(currentFontSize());      spinButton.set_value(currentFontSize());
773      hbox.pack_start(spinButton);      hbox.pack_start(spinButton);
774    
775    #if USE_GTKMM_BOX
776        dialog.get_content_area()->pack_start(hbox);
777    #else
778      dialog.get_vbox()->pack_start(hbox);      dialog.get_vbox()->pack_start(hbox);
779    #endif
780      dialog.add_button(_("_OK"), 0);      dialog.add_button(_("_OK"), 0);
781      dialog.add_button(_("_Cancel"), 1);      dialog.add_button(_("_Cancel"), 1);
782    
783    #if HAS_GTKMM_SHOW_ALL_CHILDREN
784      dialog.show_all_children();      dialog.show_all_children();
785    #endif
786    
787      if (!dialog.run()) { // OK selected ...      if (!dialog.run()) { // OK selected ...
788          const int newFontSize = spinButton.get_value_as_int();          const int newFontSize = spinButton.get_value_as_int();
# Line 650  void ScriptEditor::onMenuChangeFontSize( Line 791  void ScriptEditor::onMenuChangeFontSize(
791      }      }
792  }  }
793    
794  bool ScriptEditor::onWindowDelete(GdkEventAny* e) {  #if GTKMM_MAJOR_VERSION > 3 || (GTKMM_MAJOR_VERSION == 3 && (GTKMM_MINOR_VERSION > 91 || (GTKMM_MINOR_VERSION == 91 && GTKMM_MICRO_VERSION >= 2))) // GTKMM >= 3.91.2
795    bool ScriptEditor::onWindowDelete(Gdk::Event& e) {
796        return onWindowDeleteP(NULL);
797    }
798    #endif
799    
800    bool ScriptEditor::onWindowDeleteP(GdkEventAny* /*e*/) {
801      //printf("onWindowDelete\n");      //printf("onWindowDelete\n");
802    
803      if (!isModified()) return false; // propagate event further (which will close this window)      if (!isModified()) return false; // propagate event further (which will close this window)
# Line 699  void ScriptEditor::onModifiedChanged() { Line 846  void ScriptEditor::onModifiedChanged() {
846  }  }
847    
848  void ScriptEditor::onButtonCancel() {  void ScriptEditor::onButtonCancel() {
849      bool dropEvent = onWindowDelete(NULL);      bool dropEvent = onWindowDeleteP(NULL);
850      if (dropEvent) return;      if (dropEvent) return;
851      hide();      hide();
852  }  }

Legend:
Removed from v.3310  
changed lines
  Added in v.3368

  ViewVC Help
Powered by ViewVC