/[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 3314 by schoenebeck, Tue Jul 18 22:00:56 2017 UTC revision 3566 by schoenebeck, Sat Aug 24 13:42:17 2019 UTC
# Line 1  Line 1 
1  /*  /*
2      Copyright (c) 2014-2017 Christian Schoenebeck      Copyright (c) 2014-2019 Christian Schoenebeck
3            
4      This file is part of "gigedit" and released under the terms of the      This file is part of "gigedit" and released under the terms of the
5      GNU General Public License version 2.      GNU General Public License version 2.
# 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__)  #if defined(__APPLE__)
13  # include "MacHelper.h"  # include "MacHelper.h"
# Line 65  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 160  ScriptEditor::ScriptEditor() : Line 166  ScriptEditor::ScriptEditor() :
166      m_lineNrTag->property_foreground() = "#CCCCCC";      m_lineNrTag->property_foreground() = "#CCCCCC";
167      m_tagTable->add(m_lineNrTag);      m_tagTable->add(m_lineNrTag);
168    
169        m_metricTag = Gtk::TextBuffer::Tag::create();
170        m_metricTag->property_foreground() = "#000000"; // black
171        m_tagTable->add(m_metricTag);
172    
173        m_stdUnitTag = Gtk::TextBuffer::Tag::create();
174        m_stdUnitTag->property_foreground() = "#50BC00"; // greenish
175        m_tagTable->add(m_stdUnitTag);
176    
177      // create menu      // create menu
178    #if USE_GTKMM_BUILDER
179        m_actionGroup = Gio::SimpleActionGroup::create();
180        m_actionGroup->add_action(
181            "Apply", sigc::mem_fun(*this, &ScriptEditor::onButtonApply)
182        );
183        m_actionGroup->add_action(
184            "Close", sigc::mem_fun(*this, &ScriptEditor::onButtonCancel)
185        );
186        m_actionGroup->add_action(
187            "ChangeFont", sigc::mem_fun(*this, &ScriptEditor::onMenuChangeFontSize)
188        );
189        insert_action_group("ScriptEditor", m_actionGroup);
190    
191        m_uiManager = Gtk::Builder::create();
192        Glib::ustring ui_info =
193            "<interface>"
194            "  <menubar id='MenuBar'>"
195            "    <menu id='MenuScript'>"
196            "      <section>"
197            "        <item id='Apply'>"
198            "          <attribute name='label' translatable='yes'>_Apply</attribute>"
199            "          <attribute name='action'>ScriptEditor.Apply</attribute>"
200            "          <attribute name='accel'>&lt;Primary&gt;s</attribute>"
201            "        </item>"
202            "      </section>"
203            "      <section>"
204            "        <item id='Close'>"
205            "          <attribute name='label' translatable='yes'>_Close</attribute>"
206            "          <attribute name='action'>ScriptEditor.Close</attribute>"
207            "          <attribute name='accel'>&lt;Primary&gt;q</attribute>"
208            "        </item>"
209            "      </section>"
210            "    </menu>"
211            "    <menu id='MenuEditor'>"
212            "      <section>"
213            "        <item id='ChangeFont'>"
214            "          <attribute name='label' translatable='yes'>_Font Size ...</attribute>"
215            "          <attribute name='action'>ScriptEditor.ChangeFont</attribute>"
216            "        </item>"
217            "      </section>"
218            "    </menu>"
219            "  </menubar>"
220            "</interface>";
221        m_uiManager->add_from_string(ui_info);
222        /*{
223            auto object = uiManager->get_object("MenuBar");
224            auto gmenu = Glib::RefPtr<Gio::Menu>::cast_dynamic(object);
225            set_menubar(gmenu);
226        }*/
227    #else
228      m_actionGroup = Gtk::ActionGroup::create();      m_actionGroup = Gtk::ActionGroup::create();
229      m_actionGroup->add(Gtk::Action::create("MenuScript", _("_Script")));      m_actionGroup->add(Gtk::Action::create("MenuScript", _("_Script")));
230      m_actionGroup->add(Gtk::Action::create("Apply", _("_Apply")),      m_actionGroup->add(Gtk::Action::create("Apply", _("_Apply")),
# Line 189  ScriptEditor::ScriptEditor() : Line 253  ScriptEditor::ScriptEditor() :
253          "  </menubar>"          "  </menubar>"
254          "</ui>"          "</ui>"
255      );      );
256    #endif
257    
258      m_lineNrBuffer = Gtk::TextBuffer::create(m_tagTable);      m_lineNrBuffer = Gtk::TextBuffer::create(m_tagTable);
259        m_lineNrView.set_size_request(22,14);
260      m_lineNrView.set_buffer(m_lineNrBuffer);      m_lineNrView.set_buffer(m_lineNrBuffer);
261      m_lineNrView.set_left_margin(3);      m_lineNrView.set_left_margin(3);
262      m_lineNrView.set_right_margin(3);      m_lineNrView.set_right_margin(3);
263      m_lineNrTextViewSpacer.set_size_request(5);      m_lineNrView.property_editable() = false;
264        m_lineNrView.property_sensitive() = false;
265        m_lineNrTextViewSpacer.set_size_request(5,14);
266        m_lineNrTextViewSpacer.property_editable() = false;
267        m_lineNrTextViewSpacer.property_sensitive() = false;
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("#F5F5F5");          color.set("#F5F5F5");
275          GtkWidget* widget = (GtkWidget*) m_lineNrView.gobj();          GtkWidget* widget = (GtkWidget*) m_lineNrView.gobj();
276    #if GTK_MAJOR_VERSION < 3 || (GTK_MAJOR_VERSION == 3 && GTK_MINOR_VERSION <= 24)
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      {      {
282    #if 1 //(GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
283          Gdk::Color color;          Gdk::Color color;
284    #else
285            Gdk::RGBA color;
286    #endif
287          color.set("#EEEEEE");          color.set("#EEEEEE");
288          GtkWidget* widget = (GtkWidget*) m_lineNrTextViewSpacer.gobj();          GtkWidget* widget = (GtkWidget*) m_lineNrTextViewSpacer.gobj();
289    #if GTK_MAJOR_VERSION < 3 || (GTK_MAJOR_VERSION == 3 && GTK_MINOR_VERSION <= 24)
290          gtk_widget_modify_base(widget, GTK_STATE_NORMAL, color.gobj());          gtk_widget_modify_base(widget, GTK_STATE_NORMAL, color.gobj());
291          gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, color.gobj());          gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, color.gobj());
292    #endif
293      }      }
294      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);
295      m_textView.set_buffer(m_textBuffer);      m_textView.set_buffer(m_textBuffer);
# Line 219  ScriptEditor::ScriptEditor() : Line 301  ScriptEditor::ScriptEditor() :
301      m_scrolledWindow.add(m_textViewHBox);      m_scrolledWindow.add(m_textViewHBox);
302      m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);      m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
303    
304    #if USE_GTKMM_BUILDER
305        Gtk::Widget* menuBar = new Gtk::MenuBar(
306            Glib::RefPtr<Gio::Menu>::cast_dynamic(
307                m_uiManager->get_object("MenuBar")
308            )
309        );
310    #else
311      Gtk::Widget* menuBar = m_uiManager->get_widget("/MenuBar");      Gtk::Widget* menuBar = m_uiManager->get_widget("/MenuBar");
312    #endif
313    
314      m_vbox.pack_start(*menuBar, Gtk::PACK_SHRINK);      m_vbox.pack_start(*menuBar, Gtk::PACK_SHRINK);
315      m_vbox.pack_start(m_scrolledWindow);      m_vbox.pack_start(m_scrolledWindow);
316    
# Line 230  ScriptEditor::ScriptEditor() : Line 321  ScriptEditor::ScriptEditor() :
321      m_applyButton.set_sensitive(false);      m_applyButton.set_sensitive(false);
322      m_applyButton.grab_focus();      m_applyButton.grab_focus();
323    
324  #if GTKMM_MAJOR_VERSION >= 3  #if GTKMM_MAJOR_VERSION < 3
325        m_statusHBox.set_spacing(6);
326    #elif GTKMM_MAJOR_VERSION == 3 && GTKMM_MINOR_VERSION < 12
327      m_statusImage.set_margin_left(6);      m_statusImage.set_margin_left(6);
328      m_statusImage.set_margin_right(6);      m_statusImage.set_margin_right(6);
329  #else  #else
330      m_statusHBox.set_spacing(6);      m_statusImage.set_margin_start(6);
331        m_statusImage.set_margin_end(6);
332  #endif  #endif
333    
334      m_statusHBox.pack_start(m_statusImage, Gtk::PACK_SHRINK);      m_statusHBox.pack_start(m_statusImage, Gtk::PACK_SHRINK);
335      m_statusHBox.pack_start(m_statusLabel);      m_statusHBox.pack_start(m_statusLabel);
336    #if HAS_GTKMM_SHOW_ALL_CHILDREN
337      m_statusHBox.show_all_children();      m_statusHBox.show_all_children();
338    #endif
339    
340      m_footerHBox.pack_start(m_statusHBox);      m_footerHBox.pack_start(m_statusHBox);
341      m_footerHBox.pack_start(m_buttonBox, Gtk::PACK_SHRINK);      m_footerHBox.pack_start(m_buttonBox, Gtk::PACK_SHRINK);
# Line 271  ScriptEditor::ScriptEditor() : Line 367  ScriptEditor::ScriptEditor() :
367      );      );
368    
369      signal_delete_event().connect(      signal_delete_event().connect(
370    #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
371          sigc::mem_fun(*this, &ScriptEditor::onWindowDelete)          sigc::mem_fun(*this, &ScriptEditor::onWindowDelete)
372    #else
373            sigc::mem_fun(*this, &ScriptEditor::onWindowDeleteP)
374    #endif
375      );      );
376    
377    #if HAS_GTKMM_SHOW_ALL_CHILDREN
378      show_all_children();      show_all_children();
379    #endif
380  }  }
381    
382  ScriptEditor::~ScriptEditor() {  ScriptEditor::~ScriptEditor() {
# Line 295  int ScriptEditor::currentFontSize() cons Line 397  int ScriptEditor::currentFontSize() cons
397      return fontSize;      return fontSize;
398  }  }
399    
400  void ScriptEditor::setFontSize(int size, bool save) {  void ScriptEditor::setFontSize(int sizePt, bool save) {
401      //printf("setFontSize(%d,%d)\n", size, save);      //printf("setFontSize(%d,%d)\n", size, save);
402    
403        // make sure the real size on the screen for the editor's font is consistent
404        // on all screens (which otherwise may vary between models and DPI settings)
405        const double referenceDPI = 96;
406        double dpi = Gdk::Screen::get_default()->get_resolution();
407        double sizePx = sizePt * dpi / referenceDPI;
408    
409    #if GTKMM_MAJOR_VERSION < 3 || (GTKMM_MAJOR_VERSION == 3 && GTKMM_MINOR_VERSION < 20)
410      Pango::FontDescription fdesc;      Pango::FontDescription fdesc;
411      fdesc.set_family("monospace");      fdesc.set_family("monospace");
412  #if defined(__APPLE__)  # if defined(__APPLE__)
413      // fixes poor readability of default monospace font on Macs      // fixes poor readability of default monospace font on Macs
414      if (macIsMinMac10_6())      if (macIsMinMac10_6())
415          fdesc.set_family("Menlo");          fdesc.set_family("Menlo");
416  #endif  # endif
417      fdesc.set_size(size * PANGO_SCALE);      fdesc.set_size(sizePx * PANGO_SCALE);
418      /*Glib::RefPtr<Pango::Context> context = m_textView.get_pango_context();  # if GTKMM_MAJOR_VERSION < 3
     Cairo::FontOptions options;  
     options.set_antialias(Cairo::ANTIALIAS_NONE);  
     context->set_cairo_font_options(options);*/  
 #if GTKMM_MAJOR_VERSION < 3  
419      m_lineNrView.modify_font(fdesc);      m_lineNrView.modify_font(fdesc);
420      m_textView.modify_font(fdesc);      m_textView.modify_font(fdesc);
421  #else  # else
422      m_lineNrView.override_font(fdesc);      m_lineNrView.override_font(fdesc);
423      m_textView.override_font(fdesc);      m_textView.override_font(fdesc);
424    # endif
425    #else
426        Glib::ustring family = "monospace";
427    # if defined(__APPLE__)
428        // fixes poor readability of default monospace font on Macs
429        if (macIsMinMac10_6())
430            family = "Menlo";
431    # endif
432        if (!m_css) {
433            m_css = Gtk::CssProvider::create();
434            m_lineNrView.get_style_context()->add_provider(m_css, GTK_STYLE_PROVIDER_PRIORITY_FALLBACK);
435            m_textView.get_style_context()->add_provider(m_css, GTK_STYLE_PROVIDER_PRIORITY_FALLBACK);
436        }
437        m_css->load_from_data(
438            "* {"
439            "  font: " + ToString(sizePt) + "pt " + family + ";"
440            "}"
441        );
442  #endif  #endif
443      if (save) Settings::singleton()->scriptEditorFontSize = size;      if (save) Settings::singleton()->scriptEditorFontSize = sizePt;
444  }  }
445    
446  void ScriptEditor::setScript(gig::Script* script) {  void ScriptEditor::setScript(gig::Script* script) {
# Line 332  void ScriptEditor::setScript(gig::Script Line 456  void ScriptEditor::setScript(gig::Script
456      //printf("text : '%s'\n", txt.c_str());      //printf("text : '%s'\n", txt.c_str());
457      m_textBuffer->set_text(txt);      m_textBuffer->set_text(txt);
458      m_textBuffer->set_modified(false);      m_textBuffer->set_modified(false);
459    
460        // on Gtk 3 the respective text change callback would not be called, so force this update here
461        if (txt.empty())
462            updateLineNumbers();
463  }  }
464    
465  void ScriptEditor::updateLineNumbers() {  void ScriptEditor::updateLineNumbers() {
466      const int n = m_textBuffer->get_line_count();      int n = m_textBuffer->get_line_count();
467      const int old = m_lineNrBuffer->get_line_count();      int old = m_lineNrBuffer->get_line_count();
468      if (n == old) return;      if (n == old && old > 1) return;
469        if (n < 1) n = 1;
470      const int digits = log10(n) + 1;      const int digits = log10(n) + 1;
471      const int bufSz = digits + 2;      const int bufSz = digits + 2;
472      char* buf = new char[bufSz];      char* buf = new char[bufSz];
# Line 487  void ScriptEditor::updateSyntaxHighlight Line 616  void ScriptEditor::updateSyntaxHighlight
616              applyCodeTag(m_textBuffer, token, m_commentTag);              applyCodeTag(m_textBuffer, token, m_commentTag);
617          } else if (token.isPreprocessor()) {          } else if (token.isPreprocessor()) {
618              applyCodeTag(m_textBuffer, token, m_preprocTag);              applyCodeTag(m_textBuffer, token, m_preprocTag);
619            } else if (token.isMetricPrefix()) {
620                applyCodeTag(m_textBuffer, token, m_metricTag);
621            } else if (token.isStdUnit()) {
622                applyCodeTag(m_textBuffer, token, m_stdUnitTag);
623          } else if (token.isNewLine()) {          } else if (token.isNewLine()) {
624          }          }
625      }      }
# Line 638  bool ScriptEditor::on_motion_notify_even Line 771  bool ScriptEditor::on_motion_notify_even
771      //TODO: event throttling would be a good idea here      //TODO: event throttling would be a good idea here
772      updateIssueTooltip(e);      updateIssueTooltip(e);
773  #endif  #endif
774    #if GTKMM_MAJOR_VERSION < 3 || (GTKMM_MAJOR_VERSION == 3 && GTKMM_MINOR_VERSION <= 24)
775      return ManagedWindow::on_motion_notify_event(e);      return ManagedWindow::on_motion_notify_event(e);
776    #else
777        Gdk::EventMotion em = Glib::wrap(e, true);
778        return ManagedWindow::on_motion_notify_event(em);
779    #endif
780  }  }
781    
782  void ScriptEditor::onMenuChangeFontSize() {  void ScriptEditor::onMenuChangeFontSize() {
783      //TODO: for GTKMM >= 3.2 class Gtk::FontChooser could be used instead      //TODO: for GTKMM >= 3.2 class Gtk::FontChooser could be used instead
784      Gtk::Dialog dialog(_("Font Size"), true /*modal*/);      Gtk::Dialog dialog(_("Font Size"), true /*modal*/);
785      Gtk::HBox hbox;      HBox hbox;
786      hbox.set_spacing(6);      hbox.set_spacing(6);
787    
788      Gtk::Label label(_("Editor's Font Size:"), Gtk::ALIGN_START);      Gtk::Label label(_("Editor's Font Size:"), Gtk::ALIGN_START);
# Line 656  void ScriptEditor::onMenuChangeFontSize( Line 794  void ScriptEditor::onMenuChangeFontSize(
794      spinButton.set_value(currentFontSize());      spinButton.set_value(currentFontSize());
795      hbox.pack_start(spinButton);      hbox.pack_start(spinButton);
796    
797    #if USE_GTKMM_BOX
798        dialog.get_content_area()->pack_start(hbox);
799    #else
800      dialog.get_vbox()->pack_start(hbox);      dialog.get_vbox()->pack_start(hbox);
801    #endif
802      dialog.add_button(_("_OK"), 0);      dialog.add_button(_("_OK"), 0);
803      dialog.add_button(_("_Cancel"), 1);      dialog.add_button(_("_Cancel"), 1);
804    
805    #if HAS_GTKMM_SHOW_ALL_CHILDREN
806      dialog.show_all_children();      dialog.show_all_children();
807    #endif
808    
809      if (!dialog.run()) { // OK selected ...      if (!dialog.run()) { // OK selected ...
810          const int newFontSize = spinButton.get_value_as_int();          const int newFontSize = spinButton.get_value_as_int();
# Line 669  void ScriptEditor::onMenuChangeFontSize( Line 813  void ScriptEditor::onMenuChangeFontSize(
813      }      }
814  }  }
815    
816  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
817    bool ScriptEditor::onWindowDelete(Gdk::Event& e) {
818        return onWindowDeleteP(NULL);
819    }
820    #endif
821    
822    bool ScriptEditor::onWindowDeleteP(GdkEventAny* /*e*/) {
823      //printf("onWindowDelete\n");      //printf("onWindowDelete\n");
824    
825      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 718  void ScriptEditor::onModifiedChanged() { Line 868  void ScriptEditor::onModifiedChanged() {
868  }  }
869    
870  void ScriptEditor::onButtonCancel() {  void ScriptEditor::onButtonCancel() {
871      bool dropEvent = onWindowDelete(NULL);      bool dropEvent = onWindowDeleteP(NULL);
872      if (dropEvent) return;      if (dropEvent) return;
873      hide();      hide();
874  }  }

Legend:
Removed from v.3314  
changed lines
  Added in v.3566

  ViewVC Help
Powered by ViewVC