/[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 2901 by schoenebeck, Mon May 2 16:10:56 2016 UTC revision 3736 by schoenebeck, Sat Feb 1 19:39:06 2020 UTC
# Line 1  Line 1 
1  /*  /*
2      Copyright (c) 2014-2016 Christian Schoenebeck      Copyright (c) 2014-2020 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_*()
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    
19  static const std::string _keywords[] = {  static const std::string _keywords[] = {
20      "on", "end", "declare", "while", "if", "or", "and", "not", "else", "case",      "on", "end", "declare", "while", "if", "or", "and", "not", "else", "case",
21      "select", "to", "const", "polyphonic", "mod"      "select", "to", "const", "polyphonic", "mod", "synchronized"
22  };  };
23  static int _keywordsSz = sizeof(_keywords) / sizeof(std::string);  static int _keywordsSz = sizeof(_keywords) / sizeof(std::string);
24    
# Line 41  static Glib::RefPtr<Gdk::Pixbuf> createI Line 47  static Glib::RefPtr<Gdk::Pixbuf> createI
47      int w = 0;      int w = 0;
48      int h = 0; // ignored      int h = 0; // ignored
49      Gtk::IconSize::lookup(Gtk::ICON_SIZE_SMALL_TOOLBAR, w, h);      Gtk::IconSize::lookup(Gtk::ICON_SIZE_SMALL_TOOLBAR, w, h);
50        if (!theme->has_icon(name))
51            return Glib::RefPtr<Gdk::Pixbuf>();
52      Glib::RefPtr<Gdk::Pixbuf> pixbuf = theme->load_icon(name, w, Gtk::ICON_LOOKUP_GENERIC_FALLBACK);      Glib::RefPtr<Gdk::Pixbuf> pixbuf = theme->load_icon(name, w, Gtk::ICON_LOOKUP_GENERIC_FALLBACK);
53      if (pixbuf->get_height() != targetH) {      if (pixbuf->get_height() != targetH) {
54          pixbuf = pixbuf->scale_simple(targetH, targetH, Gdk::INTERP_BILINEAR);          pixbuf = pixbuf->scale_simple(targetH, targetH, Gdk::INTERP_BILINEAR);
# Line 48  static Glib::RefPtr<Gdk::Pixbuf> createI Line 56  static Glib::RefPtr<Gdk::Pixbuf> createI
56      return pixbuf;      return pixbuf;
57  }  }
58    
59    static Glib::RefPtr<Gdk::Pixbuf> createIcon(std::vector<std::string> alternativeNames, const Glib::RefPtr<Gdk::Screen>& screen) {
60        for (int i = 0; i < alternativeNames.size(); ++i) {
61            Glib::RefPtr<Gdk::Pixbuf> buf = createIcon(alternativeNames[i], screen);
62            if (buf) return buf;
63        }
64        return Glib::RefPtr<Gdk::Pixbuf>();
65    }
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),
71        m_cancelButton(Gtk::Stock::CANCEL)
72    #else
73      m_applyButton(_("_Apply"), true),      m_applyButton(_("_Apply"), true),
74      m_cancelButton(_("_Cancel"), true)      m_cancelButton(_("_Cancel"), true)
75    #endif
76  {  {
77      m_script = NULL;      m_script = NULL;
78  #if USE_LS_SCRIPTVM  #if USE_LS_SCRIPTVM
79      m_vm = NULL;      m_vm = NULL;
80  #endif  #endif
81    
82      m_errorIcon = createIcon("dialog-error", get_screen());      if (!Settings::singleton()->autoRestoreWindowDimension) {
83      m_warningIcon = createIcon("dialog-warning-symbolic", get_screen());          set_default_size(800, 700);
84      m_successIcon = createIcon("emblem-default", get_screen());          set_position(Gtk::WIN_POS_MOUSE);
85        }
86    
87        // depending on GTK version and installed themes, there may be different
88        // icons, and different names for them, so for each type of icon we use,
89        // we provide a list of possible icon names, the first one found to be
90        // installed on the local system from the list will be used and loaded for
91        // the respective purpose (so order matters in those lists)
92        //
93        // (see https://developer.gnome.org/gtkmm/stable/namespaceGtk_1_1Stock.html for
94        // available icon names)
95        std::vector<std::string> errorIconNames;
96        errorIconNames.push_back("dialog-error");
97        errorIconNames.push_back("media-record");
98        errorIconNames.push_back("process-stop");
99    
100        std::vector<std::string> warningIconNames;
101        warningIconNames.push_back("dialog-warning-symbolic");
102        warningIconNames.push_back("dialog-warning");
103    
104        std::vector<std::string> successIconNames;
105        successIconNames.push_back("emblem-default");
106        successIconNames.push_back("tools-check-spelling");
107    
108        m_errorIcon = createIcon(errorIconNames, get_screen());
109        m_warningIcon = createIcon(warningIconNames, get_screen());
110        m_successIcon = createIcon(successIconNames, get_screen());
111    
112      add(m_vbox);      add(m_vbox);
113    
# Line 92  ScriptEditor::ScriptEditor() : Line 139  ScriptEditor::ScriptEditor() :
139      m_stringTag->property_foreground() = "#c40c0c"; // red      m_stringTag->property_foreground() = "#c40c0c"; // red
140      m_tagTable->add(m_stringTag);      m_tagTable->add(m_stringTag);
141    
142        m_patchTag = Gtk::TextBuffer::Tag::create();
143        m_patchTag->property_foreground() = "#FF4FF3"; // pink
144        m_patchTag->property_weight() = PANGO_WEIGHT_BOLD;
145        m_tagTable->add(m_patchTag);
146    
147      m_commentTag = Gtk::TextBuffer::Tag::create();      m_commentTag = Gtk::TextBuffer::Tag::create();
148      m_commentTag->property_foreground() = "#9c9c9c"; // gray      m_commentTag->property_foreground() = "#9c9c9c"; // gray
149      m_tagTable->add(m_commentTag);      m_tagTable->add(m_commentTag);
150    
151        #define PREPROC_TOKEN_COLOR  "#2f8a33" // green
152    
153      m_preprocTag = Gtk::TextBuffer::Tag::create();      m_preprocTag = Gtk::TextBuffer::Tag::create();
154      m_preprocTag->property_foreground() = "#2f8a33"; // green      m_preprocTag->property_foreground() = PREPROC_TOKEN_COLOR;
155      m_tagTable->add(m_preprocTag);      m_tagTable->add(m_preprocTag);
156    
157        m_preprocCommentTag = Gtk::TextBuffer::Tag::create();
158        m_preprocCommentTag->property_strikethrough() = true;
159        m_preprocCommentTag->property_background() = "#e5e5e5";
160        m_tagTable->add(m_preprocCommentTag);
161    
162      m_errorTag = Gtk::TextBuffer::Tag::create();      m_errorTag = Gtk::TextBuffer::Tag::create();
163      m_errorTag->property_background() = "#ff9393"; // red      m_errorTag->property_background() = "#ff9393"; // red
164      m_tagTable->add(m_errorTag);      m_tagTable->add(m_errorTag);
# Line 108  ScriptEditor::ScriptEditor() : Line 167  ScriptEditor::ScriptEditor() :
167      m_warningTag->property_background() = "#fffd7c"; // yellow      m_warningTag->property_background() = "#fffd7c"; // yellow
168      m_tagTable->add(m_warningTag);      m_tagTable->add(m_warningTag);
169    
170        m_lineNrTag = Gtk::TextBuffer::Tag::create();
171        m_lineNrTag->property_foreground() = "#CCCCCC";
172        m_tagTable->add(m_lineNrTag);
173    
174        m_metricTag = Gtk::TextBuffer::Tag::create();
175        m_metricTag->property_foreground() = "#000000"; // black
176        m_tagTable->add(m_metricTag);
177    
178        m_stdUnitTag = Gtk::TextBuffer::Tag::create();
179        m_stdUnitTag->property_foreground() = "#50BC00"; // greenish
180        m_tagTable->add(m_stdUnitTag);
181    
182      // create menu      // create menu
183    #if USE_GTKMM_BUILDER
184        m_actionGroup = Gio::SimpleActionGroup::create();
185        m_actionGroup->add_action(
186            "Apply", sigc::mem_fun(*this, &ScriptEditor::onButtonApply)
187        );
188        m_actionGroup->add_action(
189            "Close", sigc::mem_fun(*this, &ScriptEditor::onButtonCancel)
190        );
191        m_actionGroup->add_action(
192            "ChangeFont", sigc::mem_fun(*this, &ScriptEditor::onMenuChangeFontSize)
193        );
194        insert_action_group("ScriptEditor", m_actionGroup);
195    
196        m_uiManager = Gtk::Builder::create();
197        Glib::ustring ui_info =
198            "<interface>"
199            "  <menubar id='MenuBar'>"
200            "    <menu id='MenuScript'>"
201            "      <section>"
202            "        <item id='Apply'>"
203            "          <attribute name='label' translatable='yes'>_Apply</attribute>"
204            "          <attribute name='action'>ScriptEditor.Apply</attribute>"
205            "          <attribute name='accel'>&lt;Primary&gt;s</attribute>"
206            "        </item>"
207            "      </section>"
208            "      <section>"
209            "        <item id='Close'>"
210            "          <attribute name='label' translatable='yes'>_Close</attribute>"
211            "          <attribute name='action'>ScriptEditor.Close</attribute>"
212            "          <attribute name='accel'>&lt;Primary&gt;q</attribute>"
213            "        </item>"
214            "      </section>"
215            "    </menu>"
216            "    <menu id='MenuEditor'>"
217            "      <section>"
218            "        <item id='ChangeFont'>"
219            "          <attribute name='label' translatable='yes'>_Font Size ...</attribute>"
220            "          <attribute name='action'>ScriptEditor.ChangeFont</attribute>"
221            "        </item>"
222            "      </section>"
223            "    </menu>"
224            "  </menubar>"
225            "</interface>";
226        m_uiManager->add_from_string(ui_info);
227        /*{
228            auto object = uiManager->get_object("MenuBar");
229            auto gmenu = Glib::RefPtr<Gio::Menu>::cast_dynamic(object);
230            set_menubar(gmenu);
231        }*/
232    #else
233      m_actionGroup = Gtk::ActionGroup::create();      m_actionGroup = Gtk::ActionGroup::create();
234      m_actionGroup->add(Gtk::Action::create("MenuScript", _("_Script")));      m_actionGroup->add(Gtk::Action::create("MenuScript", _("_Script")));
235      m_actionGroup->add(Gtk::Action::create("Apply", _("_Apply")),      m_actionGroup->add(Gtk::Action::create("Apply", _("_Apply")),
236                         Gtk::AccelKey("<control>s"),                         Gtk::AccelKey("<control>s"),
237                         sigc::mem_fun(*this, &ScriptEditor::onButtonApply));                         sigc::mem_fun(*this, &ScriptEditor::onButtonApply));
238      m_actionGroup->add(Gtk::Action::create("Close", _("_Close")),      m_actionGroup->add(Gtk::Action::create("Close", _("_Close")),
239                         Gtk::AccelKey("<control>x"),                         Gtk::AccelKey("<control>q"),
240                         sigc::mem_fun(*this, &ScriptEditor::onButtonCancel));                         sigc::mem_fun(*this, &ScriptEditor::onButtonCancel));
241        m_actionGroup->add(Gtk::Action::create("MenuEditor", _("_Editor")));
242        m_actionGroup->add(Gtk::Action::create("ChangeFont", _("_Font Size ...")),
243                           sigc::mem_fun(*this, &ScriptEditor::onMenuChangeFontSize));
244      m_uiManager = Gtk::UIManager::create();      m_uiManager = Gtk::UIManager::create();
245      m_uiManager->insert_action_group(m_actionGroup);      m_uiManager->insert_action_group(m_actionGroup);
246      add_accel_group(m_uiManager->get_accel_group());      add_accel_group(m_uiManager->get_accel_group());
# Line 128  ScriptEditor::ScriptEditor() : Line 252  ScriptEditor::ScriptEditor() :
252          "      <separator/>"          "      <separator/>"
253          "      <menuitem action='Close'/>"          "      <menuitem action='Close'/>"
254          "    </menu>"          "    </menu>"
255            "    <menu action='MenuEditor'>"
256            "      <menuitem action='ChangeFont'/>"
257            "    </menu>"
258          "  </menubar>"          "  </menubar>"
259          "</ui>"          "</ui>"
260      );      );
261    #endif
262    
263      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);      m_lineNrBuffer = Gtk::TextBuffer::create(m_tagTable);
264      m_textView.set_buffer(m_textBuffer);      m_lineNrView.set_size_request(22,14);
265        m_lineNrView.set_buffer(m_lineNrBuffer);
266        m_lineNrView.set_left_margin(3);
267        m_lineNrView.set_right_margin(3);
268        m_lineNrView.property_editable() = false;
269        m_lineNrView.property_sensitive() = false;
270        m_lineNrTextViewSpacer.set_size_request(5,14);
271        m_lineNrTextViewSpacer.property_editable() = false;
272        m_lineNrTextViewSpacer.property_sensitive() = false;
273      {      {
274          Pango::FontDescription fdesc;  #if 1 //(GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
275          fdesc.set_family("monospace");          Gdk::Color color;
 #if defined(__APPLE__)  
         fdesc.set_size(12 * PANGO_SCALE);  
276  #else  #else
277          fdesc.set_size(10 * PANGO_SCALE);          Gdk::RGBA color;
278  #endif  #endif
279  #if GTKMM_MAJOR_VERSION < 3          color.set("#F5F5F5");
280          m_textView.modify_font(fdesc);          GtkWidget* widget = (GtkWidget*) m_lineNrView.gobj();
281    #if GTK_MAJOR_VERSION < 3 || (GTK_MAJOR_VERSION == 3 && GTK_MINOR_VERSION <= 24)
282            gtk_widget_modify_base(widget, GTK_STATE_NORMAL, color.gobj());
283            gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, color.gobj());
284    #endif
285        }
286        {
287    #if 1 //(GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
288            Gdk::Color color;
289  #else  #else
290          m_textView.override_font(fdesc);          Gdk::RGBA color;
291    #endif
292            color.set("#EEEEEE");
293            GtkWidget* widget = (GtkWidget*) m_lineNrTextViewSpacer.gobj();
294    #if GTK_MAJOR_VERSION < 3 || (GTK_MAJOR_VERSION == 3 && GTK_MINOR_VERSION <= 24)
295            gtk_widget_modify_base(widget, GTK_STATE_NORMAL, color.gobj());
296            gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, color.gobj());
297  #endif  #endif
298      }      }
299      m_scrolledWindow.add(m_textView);      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);
300        m_textView.set_buffer(m_textBuffer);
301        m_textView.set_left_margin(5);
302        setFontSize(currentFontSize(), false);
303        m_textViewHBox.pack_start(m_lineNrView, Gtk::PACK_SHRINK);
304        m_textViewHBox.pack_start(m_lineNrTextViewSpacer, Gtk::PACK_SHRINK);
305        m_textViewHBox.add(m_textView);
306        m_scrolledWindow.add(m_textViewHBox);
307      m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);      m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
308    
309    #if USE_GTKMM_BUILDER
310        Gtk::Widget* menuBar = new Gtk::MenuBar(
311            Glib::RefPtr<Gio::Menu>::cast_dynamic(
312                m_uiManager->get_object("MenuBar")
313            )
314        );
315    #else
316      Gtk::Widget* menuBar = m_uiManager->get_widget("/MenuBar");      Gtk::Widget* menuBar = m_uiManager->get_widget("/MenuBar");
317    #endif
318    
319      m_vbox.pack_start(*menuBar, Gtk::PACK_SHRINK);      m_vbox.pack_start(*menuBar, Gtk::PACK_SHRINK);
320      m_vbox.pack_start(m_scrolledWindow);      m_vbox.pack_start(m_scrolledWindow);
321    
# Line 162  ScriptEditor::ScriptEditor() : Line 326  ScriptEditor::ScriptEditor() :
326      m_applyButton.set_sensitive(false);      m_applyButton.set_sensitive(false);
327      m_applyButton.grab_focus();      m_applyButton.grab_focus();
328    
329  #if GTKMM_MAJOR_VERSION >= 3  #if GTKMM_MAJOR_VERSION < 3
330        m_statusHBox.set_spacing(6);
331    #elif GTKMM_MAJOR_VERSION == 3 && GTKMM_MINOR_VERSION < 12
332      m_statusImage.set_margin_left(6);      m_statusImage.set_margin_left(6);
333      m_statusImage.set_margin_right(6);      m_statusImage.set_margin_right(6);
334  #else  #else
335      m_statusHBox.set_spacing(6);      m_statusImage.set_margin_start(6);
336        m_statusImage.set_margin_end(6);
337  #endif  #endif
338    
339      m_statusHBox.pack_start(m_statusImage, Gtk::PACK_SHRINK);      m_statusHBox.pack_start(m_statusImage, Gtk::PACK_SHRINK);
340      m_statusHBox.pack_start(m_statusLabel);      m_statusHBox.pack_start(m_statusLabel);
341    #if HAS_GTKMM_SHOW_ALL_CHILDREN
342      m_statusHBox.show_all_children();      m_statusHBox.show_all_children();
343    #endif
344    
345      m_footerHBox.pack_start(m_statusHBox);      m_footerHBox.pack_start(m_statusHBox);
346      m_footerHBox.pack_start(m_buttonBox, Gtk::PACK_SHRINK);      m_footerHBox.pack_start(m_buttonBox, Gtk::PACK_SHRINK);
# Line 203  ScriptEditor::ScriptEditor() : Line 372  ScriptEditor::ScriptEditor() :
372      );      );
373    
374      signal_delete_event().connect(      signal_delete_event().connect(
375    #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
376          sigc::mem_fun(*this, &ScriptEditor::onWindowDelete)          sigc::mem_fun(*this, &ScriptEditor::onWindowDelete)
377    #else
378            sigc::mem_fun(*this, &ScriptEditor::onWindowDeleteP)
379    #endif
380      );      );
381    
382    #if HAS_GTKMM_SHOW_ALL_CHILDREN
383      show_all_children();      show_all_children();
384    #endif
385    
386    #if !USE_LS_SCRIPTVM
387        // make user aware about gigedit had been compiled without liblinuxsampler support
388        m_statusLabel.set_markup(_("Limited editor features (since Gigedit was compiled without liblinuxsampler support)!"));
389        m_statusImage.set(m_warningIcon);
390    #endif
391  }  }
392    
393  ScriptEditor::~ScriptEditor() {  ScriptEditor::~ScriptEditor() {
# Line 216  ScriptEditor::~ScriptEditor() { Line 397  ScriptEditor::~ScriptEditor() {
397  #endif  #endif
398  }  }
399    
400    int ScriptEditor::currentFontSize() const {
401    #if defined(__APPLE__)
402        const int defaultFontSize = 11;
403    #else
404        const int defaultFontSize = 10;
405    #endif
406        const int settingFontSize = Settings::singleton()->scriptEditorFontSize;
407        const int fontSize = (settingFontSize > 0) ? settingFontSize : defaultFontSize;
408        return fontSize;
409    }
410    
411    void ScriptEditor::setFontSize(int sizePt, bool save) {
412        //printf("setFontSize(%d,%d)\n", size, save);
413    
414        // make sure the real size on the screen for the editor's font is consistent
415        // on all screens (which otherwise may vary between models and DPI settings)
416        const double referenceDPI = 96;
417        double dpi = Gdk::Screen::get_default()->get_resolution();
418        double sizePx = sizePt * dpi / referenceDPI;
419    
420    #if GTKMM_MAJOR_VERSION < 3 || (GTKMM_MAJOR_VERSION == 3 && GTKMM_MINOR_VERSION < 20)
421        Pango::FontDescription fdesc;
422        fdesc.set_family("monospace");
423    # if defined(__APPLE__)
424        // fixes poor readability of default monospace font on Macs
425        if (macIsMinMac10_6())
426            fdesc.set_family("Menlo");
427    # endif
428        fdesc.set_size(sizePx * PANGO_SCALE);
429    # if GTKMM_MAJOR_VERSION < 3
430        m_lineNrView.modify_font(fdesc);
431        m_textView.modify_font(fdesc);
432    # else
433        m_lineNrView.override_font(fdesc);
434        m_textView.override_font(fdesc);
435    # endif
436    #else
437        Glib::ustring family = "monospace";
438    # if defined(__APPLE__)
439        // fixes poor readability of default monospace font on Macs
440        if (macIsMinMac10_6())
441            family = "Menlo";
442    # endif
443        if (!m_css) {
444            m_css = Gtk::CssProvider::create();
445            m_lineNrView.get_style_context()->add_provider(m_css, GTK_STYLE_PROVIDER_PRIORITY_FALLBACK);
446            m_textView.get_style_context()->add_provider(m_css, GTK_STYLE_PROVIDER_PRIORITY_FALLBACK);
447        }
448        m_css->load_from_data(
449            "* {"
450            "  font: " + ToString(sizePt) + "pt " + family + ";"
451            "}"
452        );
453    #endif
454        if (save) Settings::singleton()->scriptEditorFontSize = sizePt;
455    }
456    
457  void ScriptEditor::setScript(gig::Script* script) {  void ScriptEditor::setScript(gig::Script* script) {
458      m_script = script;      m_script = script;
459      if (!script) {      if (!script) {
# Line 229  void ScriptEditor::setScript(gig::Script Line 467  void ScriptEditor::setScript(gig::Script
467      //printf("text : '%s'\n", txt.c_str());      //printf("text : '%s'\n", txt.c_str());
468      m_textBuffer->set_text(txt);      m_textBuffer->set_text(txt);
469      m_textBuffer->set_modified(false);      m_textBuffer->set_modified(false);
470    
471        // on Gtk 3 the respective text change callback would not be called, so force this update here
472        if (txt.empty())
473            updateLineNumbers();
474    }
475    
476    void ScriptEditor::updateLineNumbers() {
477        int n = m_textBuffer->get_line_count();
478        int old = m_lineNrBuffer->get_line_count();
479        if (n == old && old > 1) return;
480        if (n < 1) n = 1;
481        const int digits = log10(n) + 1;
482        const int bufSz = digits + 2;
483        char* buf = new char[bufSz];
484        std::string sFmt1 =   "%" + ToString(digits) + "d";
485        std::string sFmt2 = "\n%" + ToString(digits) + "d";
486        Glib::ustring s;
487        for (int i = 0; i < n; ++i) {
488            snprintf(buf, bufSz, i ? sFmt2.c_str() : sFmt1.c_str(), i+1);
489            s += buf;
490        }
491        m_lineNrBuffer->remove_all_tags(m_lineNrBuffer->begin(), m_lineNrBuffer->end());
492        m_lineNrBuffer->set_text(s);
493        m_lineNrBuffer->apply_tag(m_lineNrTag, m_lineNrBuffer->begin(), m_lineNrBuffer->end());
494        if (buf) delete[] buf;
495  }  }
496    
497  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 281  void ScriptEditor::onTextInserted(const Line 544  void ScriptEditor::onTextInserted(const
544      ;      ;
545            
546  #endif // USE_LS_SCRIPTVM  #endif // USE_LS_SCRIPTVM
547        updateLineNumbers();
548  }  }
549    
550  #if USE_LS_SCRIPTVM  #if USE_LS_SCRIPTVM
# Line 290  LinuxSampler::ScriptVM* ScriptEditor::Ge Line 554  LinuxSampler::ScriptVM* ScriptEditor::Ge
554      return m_vm;      return m_vm;
555  }  }
556    
557  static void getIteratorsForIssue(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::ParserIssue& issue, Gtk::TextBuffer::iterator& start, Gtk::TextBuffer::iterator& end) {  template<class T>
558      start = txtbuf->get_iter_at_line_index(issue.firstLine - 1, issue.firstColumn - 1);  static void getIteratorsForIssue(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const T& issue, Gtk::TextBuffer::iterator& start, Gtk::TextBuffer::iterator& end) {
559        Gtk::TextBuffer::iterator itLine =
560            txtbuf->get_iter_at_line_index(issue.firstLine - 1, 0);
561        const int charsInLine = itLine.get_bytes_in_line();
562        start = txtbuf->get_iter_at_line_index(
563            issue.firstLine - 1,
564            // check we are not getting past the end of the line here, otherwise Gtk crashes
565            issue.firstColumn - 1 < charsInLine ? issue.firstColumn - 1 : charsInLine - 1
566        );
567      end = start;      end = start;
568      end.forward_lines(issue.lastLine - issue.firstLine);      end.forward_lines(issue.lastLine - issue.firstLine);
569      end.forward_chars(      end.forward_chars(
# Line 302  static void getIteratorsForIssue(Glib::R Line 574  static void getIteratorsForIssue(Glib::R
574  }  }
575    
576  static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::VMSourceToken& token, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {  static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::VMSourceToken& token, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
577      Gtk::TextBuffer::iterator itStart =      Gtk::TextBuffer::iterator itLine =
578          txtbuf->get_iter_at_line_index(token.firstLine(), token.firstColumn());          txtbuf->get_iter_at_line_index(token.firstLine(), 0);
579        const int charsInLine = itLine.get_bytes_in_line();
580        Gtk::TextBuffer::iterator itStart = txtbuf->get_iter_at_line_index(
581            token.firstLine(),
582            // check we are not getting past the end of the line here, otherwise Gtk crashes
583            token.firstColumn() < charsInLine ? token.firstColumn() : charsInLine - 1
584        );
585      Gtk::TextBuffer::iterator itEnd = itStart;      Gtk::TextBuffer::iterator itEnd = itStart;
586      const int length = token.text().length();      const int length = token.text().length();
587      itEnd.forward_chars(length);      itEnd.forward_chars(length);
# Line 316  static void applyCodeTag(Glib::RefPtr<Gt Line 594  static void applyCodeTag(Glib::RefPtr<Gt
594      txtbuf->apply_tag(tag, itStart, itEnd);      txtbuf->apply_tag(tag, itStart, itEnd);
595  }  }
596    
597    static void applyPreprocessorComment(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::CodeBlock& block, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
598        Gtk::TextBuffer::iterator itStart, itEnd;
599        getIteratorsForIssue(txtbuf, block, itStart, itEnd);
600        txtbuf->apply_tag(tag, itStart, itEnd);
601    }
602    
603  void ScriptEditor::updateSyntaxHighlightingByVM() {  void ScriptEditor::updateSyntaxHighlightingByVM() {
604      GetScriptVM();      GetScriptVM();
605      const std::string s = m_textBuffer->get_text();      const std::string s = m_textBuffer->get_text();
606        if (s.empty()) return;
607      std::vector<LinuxSampler::VMSourceToken> tokens = m_vm->syntaxHighlighting(s);      std::vector<LinuxSampler::VMSourceToken> tokens = m_vm->syntaxHighlighting(s);
608    
609      for (int i = 0; i < tokens.size(); ++i) {      for (int i = 0; i < tokens.size(); ++i) {
610          const LinuxSampler::VMSourceToken& token = tokens[i];          const LinuxSampler::VMSourceToken& token = tokens[i];
611    
612          if (token.isKeyword()) {          if (token.isKeyword()) {
613              applyCodeTag(m_textBuffer, token, m_keywordTag);              if (token.text() == "patch")
614                    applyCodeTag(m_textBuffer, token, m_patchTag);
615                else
616                    applyCodeTag(m_textBuffer, token, m_keywordTag);
617          } else if (token.isVariableName()) {          } else if (token.isVariableName()) {
618              applyCodeTag(m_textBuffer, token, m_variableTag);              applyCodeTag(m_textBuffer, token, m_variableTag);
619          } else if (token.isIdentifier()) {          } else if (token.isIdentifier()) {
# Line 342  void ScriptEditor::updateSyntaxHighlight Line 630  void ScriptEditor::updateSyntaxHighlight
630              applyCodeTag(m_textBuffer, token, m_commentTag);              applyCodeTag(m_textBuffer, token, m_commentTag);
631          } else if (token.isPreprocessor()) {          } else if (token.isPreprocessor()) {
632              applyCodeTag(m_textBuffer, token, m_preprocTag);              applyCodeTag(m_textBuffer, token, m_preprocTag);
633            } else if (token.isMetricPrefix()) {
634                applyCodeTag(m_textBuffer, token, m_metricTag);
635            } else if (token.isStdUnit()) {
636                applyCodeTag(m_textBuffer, token, m_stdUnitTag);
637          } else if (token.isNewLine()) {          } else if (token.isNewLine()) {
638          }          }
639      }      }
# Line 354  void ScriptEditor::updateParserIssuesByV Line 646  void ScriptEditor::updateParserIssuesByV
646      m_issues = parserContext->issues();      m_issues = parserContext->issues();
647      m_errors = parserContext->errors();      m_errors = parserContext->errors();
648      m_warnings = parserContext->warnings();      m_warnings = parserContext->warnings();
649        m_preprocComments = parserContext->preprocessorComments();
650    
651      for (int i = 0; i < m_issues.size(); ++i) {      if (!s.empty()) {
652          const LinuxSampler::ParserIssue& issue = m_issues[i];          for (int i = 0; i < m_issues.size(); ++i) {
653                const LinuxSampler::ParserIssue& issue = m_issues[i];
654          if (issue.isErr()) {  
655              applyCodeTag(m_textBuffer, issue, m_errorTag);              if (issue.isErr()) {
656          } else if (issue.isWrn()) {                  applyCodeTag(m_textBuffer, issue, m_errorTag);
657              applyCodeTag(m_textBuffer, issue, m_warningTag);              } else if (issue.isWrn()) {
658                    applyCodeTag(m_textBuffer, issue, m_warningTag);
659                }
660          }          }
661      }      }
662    
663        for (int i = 0; i < m_preprocComments.size(); ++i) {
664            applyPreprocessorComment(m_textBuffer, m_preprocComments[i],
665                                     m_preprocCommentTag);
666        }
667    
668      delete parserContext;      delete parserContext;
669  }  }
670    
# Line 398  void ScriptEditor::updateIssueTooltip(Gd Line 698  void ScriptEditor::updateIssueTooltip(Gd
698          }          }
699      }      }
700    
701        for (int i = 0; i < m_preprocComments.size(); ++i) {
702            const LinuxSampler::CodeBlock& block = m_preprocComments[i];
703            const int firstLine   = block.firstLine - 1;
704            const int firstColumn = block.firstColumn - 1;
705            const int lastLine    = block.lastLine - 1;
706            const int lastColumn  = block.lastColumn - 1;
707            if (firstLine  <= line && line <= lastLine &&
708                (firstLine != line || firstColumn <= column) &&
709                (lastLine  != line || lastColumn  >= column))
710            {
711                m_textView.set_tooltip_markup(
712                    "Code block filtered out by preceding <span foreground=\"" PREPROC_TOKEN_COLOR "\">preprocessor</span> statement."
713                );
714                return;
715            }
716        }
717    
718      m_textView.set_tooltip_markup("");      m_textView.set_tooltip_markup("");
719  }  }
720    
# Line 460  void ScriptEditor::onTextErased(const Gt Line 777  void ScriptEditor::onTextErased(const Gt
777    
778      m_textBuffer->remove_all_tags(itStart2, itEnd2);      m_textBuffer->remove_all_tags(itStart2, itEnd2);
779  #endif // USE_LS_SCRIPTVM  #endif // USE_LS_SCRIPTVM
780        updateLineNumbers();
781  }  }
782    
783  bool ScriptEditor::on_motion_notify_event(GdkEventMotion* e) {  bool ScriptEditor::on_motion_notify_event(GdkEventMotion* e) {
# Line 467  bool ScriptEditor::on_motion_notify_even Line 785  bool ScriptEditor::on_motion_notify_even
785      //TODO: event throttling would be a good idea here      //TODO: event throttling would be a good idea here
786      updateIssueTooltip(e);      updateIssueTooltip(e);
787  #endif  #endif
788    #if GTKMM_MAJOR_VERSION < 3 || (GTKMM_MAJOR_VERSION == 3 && GTKMM_MINOR_VERSION <= 24)
789      return ManagedWindow::on_motion_notify_event(e);      return ManagedWindow::on_motion_notify_event(e);
790    #else
791        Gdk::EventMotion em = Glib::wrap(e, true);
792        return ManagedWindow::on_motion_notify_event(em);
793    #endif
794  }  }
795    
796  bool ScriptEditor::onWindowDelete(GdkEventAny* e) {  void ScriptEditor::onMenuChangeFontSize() {
797        //TODO: for GTKMM >= 3.2 class Gtk::FontChooser could be used instead
798        Gtk::Dialog dialog(_("Font Size"), true /*modal*/);
799        HBox hbox;
800        hbox.set_spacing(6);
801    
802        Gtk::Label label(_("Editor's Font Size:"), Gtk::ALIGN_START);
803        hbox.pack_start(label, Gtk::PACK_SHRINK);
804    
805        Gtk::SpinButton spinButton;
806        spinButton.set_range(4, 80);
807        spinButton.set_increments(1, 10);
808        spinButton.set_value(currentFontSize());
809        hbox.pack_start(spinButton);
810    
811    #if USE_GTKMM_BOX
812        dialog.get_content_area()->pack_start(hbox);
813    #else
814        dialog.get_vbox()->pack_start(hbox);
815    #endif
816        dialog.add_button(_("_OK"), 0);
817        dialog.add_button(_("_Cancel"), 1);
818    
819    #if HAS_GTKMM_SHOW_ALL_CHILDREN
820        dialog.show_all_children();
821    #endif
822    
823        if (!dialog.run()) { // OK selected ...
824            const int newFontSize = spinButton.get_value_as_int();
825            if (newFontSize >= 4)
826                setFontSize(newFontSize, true);
827        }
828    }
829    
830    #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
831    bool ScriptEditor::onWindowDelete(Gdk::Event& e) {
832        return onWindowDeleteP(NULL);
833    }
834    #endif
835    
836    bool ScriptEditor::onWindowDeleteP(GdkEventAny* /*e*/) {
837      //printf("onWindowDelete\n");      //printf("onWindowDelete\n");
838    
839      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 519  void ScriptEditor::onModifiedChanged() { Line 882  void ScriptEditor::onModifiedChanged() {
882  }  }
883    
884  void ScriptEditor::onButtonCancel() {  void ScriptEditor::onButtonCancel() {
885      bool dropEvent = onWindowDelete(NULL);      bool dropEvent = onWindowDeleteP(NULL);
886      if (dropEvent) return;      if (dropEvent) return;
887      hide();      hide();
888  }  }
889    
890  void ScriptEditor::onButtonApply() {  void ScriptEditor::onButtonApply() {
891        signal_script_to_be_changed.emit(m_script);
892      m_script->SetScriptAsText(m_textBuffer->get_text());      m_script->SetScriptAsText(m_textBuffer->get_text());
893        signal_script_changed.emit(m_script);
894      m_textBuffer->set_modified(false);      m_textBuffer->set_modified(false);
895  }  }
896    

Legend:
Removed from v.2901  
changed lines
  Added in v.3736

  ViewVC Help
Powered by ViewVC