/[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 3621 by schoenebeck, Tue Oct 1 16:27:13 2019 UTC
# Line 1  Line 1 
1  /*  /*
2      Copyright (c) 2014-2016 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_*()
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 96  ScriptEditor::ScriptEditor() : Line 143  ScriptEditor::ScriptEditor() :
143      m_commentTag->property_foreground() = "#9c9c9c"; // gray      m_commentTag->property_foreground() = "#9c9c9c"; // gray
144      m_tagTable->add(m_commentTag);      m_tagTable->add(m_commentTag);
145    
146        #define PREPROC_TOKEN_COLOR  "#2f8a33" // green
147    
148      m_preprocTag = Gtk::TextBuffer::Tag::create();      m_preprocTag = Gtk::TextBuffer::Tag::create();
149      m_preprocTag->property_foreground() = "#2f8a33"; // green      m_preprocTag->property_foreground() = PREPROC_TOKEN_COLOR;
150      m_tagTable->add(m_preprocTag);      m_tagTable->add(m_preprocTag);
151    
152        m_preprocCommentTag = Gtk::TextBuffer::Tag::create();
153        m_preprocCommentTag->property_strikethrough() = true;
154        m_preprocCommentTag->property_background() = "#e5e5e5";
155        m_tagTable->add(m_preprocCommentTag);
156    
157      m_errorTag = Gtk::TextBuffer::Tag::create();      m_errorTag = Gtk::TextBuffer::Tag::create();
158      m_errorTag->property_background() = "#ff9393"; // red      m_errorTag->property_background() = "#ff9393"; // red
159      m_tagTable->add(m_errorTag);      m_tagTable->add(m_errorTag);
# Line 108  ScriptEditor::ScriptEditor() : Line 162  ScriptEditor::ScriptEditor() :
162      m_warningTag->property_background() = "#fffd7c"; // yellow      m_warningTag->property_background() = "#fffd7c"; // yellow
163      m_tagTable->add(m_warningTag);      m_tagTable->add(m_warningTag);
164    
165        m_lineNrTag = Gtk::TextBuffer::Tag::create();
166        m_lineNrTag->property_foreground() = "#CCCCCC";
167        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")),
231                         Gtk::AccelKey("<control>s"),                         Gtk::AccelKey("<control>s"),
232                         sigc::mem_fun(*this, &ScriptEditor::onButtonApply));                         sigc::mem_fun(*this, &ScriptEditor::onButtonApply));
233      m_actionGroup->add(Gtk::Action::create("Close", _("_Close")),      m_actionGroup->add(Gtk::Action::create("Close", _("_Close")),
234                         Gtk::AccelKey("<control>x"),                         Gtk::AccelKey("<control>q"),
235                         sigc::mem_fun(*this, &ScriptEditor::onButtonCancel));                         sigc::mem_fun(*this, &ScriptEditor::onButtonCancel));
236        m_actionGroup->add(Gtk::Action::create("MenuEditor", _("_Editor")));
237        m_actionGroup->add(Gtk::Action::create("ChangeFont", _("_Font Size ...")),
238                           sigc::mem_fun(*this, &ScriptEditor::onMenuChangeFontSize));
239      m_uiManager = Gtk::UIManager::create();      m_uiManager = Gtk::UIManager::create();
240      m_uiManager->insert_action_group(m_actionGroup);      m_uiManager->insert_action_group(m_actionGroup);
241      add_accel_group(m_uiManager->get_accel_group());      add_accel_group(m_uiManager->get_accel_group());
# Line 128  ScriptEditor::ScriptEditor() : Line 247  ScriptEditor::ScriptEditor() :
247          "      <separator/>"          "      <separator/>"
248          "      <menuitem action='Close'/>"          "      <menuitem action='Close'/>"
249          "    </menu>"          "    </menu>"
250            "    <menu action='MenuEditor'>"
251            "      <menuitem action='ChangeFont'/>"
252            "    </menu>"
253          "  </menubar>"          "  </menubar>"
254          "</ui>"          "</ui>"
255      );      );
256    #endif
257    
258      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);      m_lineNrBuffer = Gtk::TextBuffer::create(m_tagTable);
259      m_textView.set_buffer(m_textBuffer);      m_lineNrView.set_size_request(22,14);
260        m_lineNrView.set_buffer(m_lineNrBuffer);
261        m_lineNrView.set_left_margin(3);
262        m_lineNrView.set_right_margin(3);
263        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          Pango::FontDescription fdesc;  #if 1 //(GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
270          fdesc.set_family("monospace");          Gdk::Color color;
 #if defined(__APPLE__)  
         fdesc.set_size(12 * PANGO_SCALE);  
271  #else  #else
272          fdesc.set_size(10 * PANGO_SCALE);          Gdk::RGBA color;
273  #endif  #endif
274  #if GTKMM_MAJOR_VERSION < 3          color.set("#F5F5F5");
275          m_textView.modify_font(fdesc);          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());
278            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;
284  #else  #else
285          m_textView.override_font(fdesc);          Gdk::RGBA color;
286    #endif
287            color.set("#EEEEEE");
288            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());
291            gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, color.gobj());
292  #endif  #endif
293      }      }
294      m_scrolledWindow.add(m_textView);      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);
295        m_textView.set_buffer(m_textBuffer);
296        m_textView.set_left_margin(5);
297        setFontSize(currentFontSize(), false);
298        m_textViewHBox.pack_start(m_lineNrView, Gtk::PACK_SHRINK);
299        m_textViewHBox.pack_start(m_lineNrTextViewSpacer, Gtk::PACK_SHRINK);
300        m_textViewHBox.add(m_textView);
301        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 162  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 203  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    #if !USE_LS_SCRIPTVM
382        // make user aware about gigedit had been compiled without liblinuxsampler support
383        m_statusLabel.set_markup(_("Limited editor features (since Gigedit was compiled without liblinuxsampler support)!"));
384        m_statusImage.set(m_warningIcon);
385    #endif
386  }  }
387    
388  ScriptEditor::~ScriptEditor() {  ScriptEditor::~ScriptEditor() {
# Line 216  ScriptEditor::~ScriptEditor() { Line 392  ScriptEditor::~ScriptEditor() {
392  #endif  #endif
393  }  }
394    
395    int ScriptEditor::currentFontSize() const {
396    #if defined(__APPLE__)
397        const int defaultFontSize = 11;
398    #else
399        const int defaultFontSize = 10;
400    #endif
401        const int settingFontSize = Settings::singleton()->scriptEditorFontSize;
402        const int fontSize = (settingFontSize > 0) ? settingFontSize : defaultFontSize;
403        return fontSize;
404    }
405    
406    void ScriptEditor::setFontSize(int sizePt, bool save) {
407        //printf("setFontSize(%d,%d)\n", size, save);
408    
409        // make sure the real size on the screen for the editor's font is consistent
410        // on all screens (which otherwise may vary between models and DPI settings)
411        const double referenceDPI = 96;
412        double dpi = Gdk::Screen::get_default()->get_resolution();
413        double sizePx = sizePt * dpi / referenceDPI;
414    
415    #if GTKMM_MAJOR_VERSION < 3 || (GTKMM_MAJOR_VERSION == 3 && GTKMM_MINOR_VERSION < 20)
416        Pango::FontDescription fdesc;
417        fdesc.set_family("monospace");
418    # if defined(__APPLE__)
419        // fixes poor readability of default monospace font on Macs
420        if (macIsMinMac10_6())
421            fdesc.set_family("Menlo");
422    # endif
423        fdesc.set_size(sizePx * PANGO_SCALE);
424    # if GTKMM_MAJOR_VERSION < 3
425        m_lineNrView.modify_font(fdesc);
426        m_textView.modify_font(fdesc);
427    # else
428        m_lineNrView.override_font(fdesc);
429        m_textView.override_font(fdesc);
430    # endif
431    #else
432        Glib::ustring family = "monospace";
433    # if defined(__APPLE__)
434        // fixes poor readability of default monospace font on Macs
435        if (macIsMinMac10_6())
436            family = "Menlo";
437    # endif
438        if (!m_css) {
439            m_css = Gtk::CssProvider::create();
440            m_lineNrView.get_style_context()->add_provider(m_css, GTK_STYLE_PROVIDER_PRIORITY_FALLBACK);
441            m_textView.get_style_context()->add_provider(m_css, GTK_STYLE_PROVIDER_PRIORITY_FALLBACK);
442        }
443        m_css->load_from_data(
444            "* {"
445            "  font: " + ToString(sizePt) + "pt " + family + ";"
446            "}"
447        );
448    #endif
449        if (save) Settings::singleton()->scriptEditorFontSize = sizePt;
450    }
451    
452  void ScriptEditor::setScript(gig::Script* script) {  void ScriptEditor::setScript(gig::Script* script) {
453      m_script = script;      m_script = script;
454      if (!script) {      if (!script) {
# Line 229  void ScriptEditor::setScript(gig::Script Line 462  void ScriptEditor::setScript(gig::Script
462      //printf("text : '%s'\n", txt.c_str());      //printf("text : '%s'\n", txt.c_str());
463      m_textBuffer->set_text(txt);      m_textBuffer->set_text(txt);
464      m_textBuffer->set_modified(false);      m_textBuffer->set_modified(false);
465    
466        // on Gtk 3 the respective text change callback would not be called, so force this update here
467        if (txt.empty())
468            updateLineNumbers();
469    }
470    
471    void ScriptEditor::updateLineNumbers() {
472        int n = m_textBuffer->get_line_count();
473        int old = m_lineNrBuffer->get_line_count();
474        if (n == old && old > 1) return;
475        if (n < 1) n = 1;
476        const int digits = log10(n) + 1;
477        const int bufSz = digits + 2;
478        char* buf = new char[bufSz];
479        std::string sFmt1 =   "%" + ToString(digits) + "d";
480        std::string sFmt2 = "\n%" + ToString(digits) + "d";
481        Glib::ustring s;
482        for (int i = 0; i < n; ++i) {
483            snprintf(buf, bufSz, i ? sFmt2.c_str() : sFmt1.c_str(), i+1);
484            s += buf;
485        }
486        m_lineNrBuffer->remove_all_tags(m_lineNrBuffer->begin(), m_lineNrBuffer->end());
487        m_lineNrBuffer->set_text(s);
488        m_lineNrBuffer->apply_tag(m_lineNrTag, m_lineNrBuffer->begin(), m_lineNrBuffer->end());
489        if (buf) delete[] buf;
490  }  }
491    
492  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 539  void ScriptEditor::onTextInserted(const
539      ;      ;
540            
541  #endif // USE_LS_SCRIPTVM  #endif // USE_LS_SCRIPTVM
542        updateLineNumbers();
543  }  }
544    
545  #if USE_LS_SCRIPTVM  #if USE_LS_SCRIPTVM
# Line 290  LinuxSampler::ScriptVM* ScriptEditor::Ge Line 549  LinuxSampler::ScriptVM* ScriptEditor::Ge
549      return m_vm;      return m_vm;
550  }  }
551    
552  static void getIteratorsForIssue(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::ParserIssue& issue, Gtk::TextBuffer::iterator& start, Gtk::TextBuffer::iterator& end) {  template<class T>
553      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) {
554        Gtk::TextBuffer::iterator itLine =
555            txtbuf->get_iter_at_line_index(issue.firstLine - 1, 0);
556        const int charsInLine = itLine.get_bytes_in_line();
557        start = txtbuf->get_iter_at_line_index(
558            issue.firstLine - 1,
559            // check we are not getting past the end of the line here, otherwise Gtk crashes
560            issue.firstColumn - 1 < charsInLine ? issue.firstColumn - 1 : charsInLine - 1
561        );
562      end = start;      end = start;
563      end.forward_lines(issue.lastLine - issue.firstLine);      end.forward_lines(issue.lastLine - issue.firstLine);
564      end.forward_chars(      end.forward_chars(
# Line 302  static void getIteratorsForIssue(Glib::R Line 569  static void getIteratorsForIssue(Glib::R
569  }  }
570    
571  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) {
572      Gtk::TextBuffer::iterator itStart =      Gtk::TextBuffer::iterator itLine =
573          txtbuf->get_iter_at_line_index(token.firstLine(), token.firstColumn());          txtbuf->get_iter_at_line_index(token.firstLine(), 0);
574        const int charsInLine = itLine.get_bytes_in_line();
575        Gtk::TextBuffer::iterator itStart = txtbuf->get_iter_at_line_index(
576            token.firstLine(),
577            // check we are not getting past the end of the line here, otherwise Gtk crashes
578            token.firstColumn() < charsInLine ? token.firstColumn() : charsInLine - 1
579        );
580      Gtk::TextBuffer::iterator itEnd = itStart;      Gtk::TextBuffer::iterator itEnd = itStart;
581      const int length = token.text().length();      const int length = token.text().length();
582      itEnd.forward_chars(length);      itEnd.forward_chars(length);
# Line 316  static void applyCodeTag(Glib::RefPtr<Gt Line 589  static void applyCodeTag(Glib::RefPtr<Gt
589      txtbuf->apply_tag(tag, itStart, itEnd);      txtbuf->apply_tag(tag, itStart, itEnd);
590  }  }
591    
592    static void applyPreprocessorComment(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::CodeBlock& block, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
593        Gtk::TextBuffer::iterator itStart, itEnd;
594        getIteratorsForIssue(txtbuf, block, itStart, itEnd);
595        txtbuf->apply_tag(tag, itStart, itEnd);
596    }
597    
598  void ScriptEditor::updateSyntaxHighlightingByVM() {  void ScriptEditor::updateSyntaxHighlightingByVM() {
599      GetScriptVM();      GetScriptVM();
600      const std::string s = m_textBuffer->get_text();      const std::string s = m_textBuffer->get_text();
601        if (s.empty()) return;
602      std::vector<LinuxSampler::VMSourceToken> tokens = m_vm->syntaxHighlighting(s);      std::vector<LinuxSampler::VMSourceToken> tokens = m_vm->syntaxHighlighting(s);
603    
604      for (int i = 0; i < tokens.size(); ++i) {      for (int i = 0; i < tokens.size(); ++i) {
# Line 342  void ScriptEditor::updateSyntaxHighlight Line 622  void ScriptEditor::updateSyntaxHighlight
622              applyCodeTag(m_textBuffer, token, m_commentTag);              applyCodeTag(m_textBuffer, token, m_commentTag);
623          } else if (token.isPreprocessor()) {          } else if (token.isPreprocessor()) {
624              applyCodeTag(m_textBuffer, token, m_preprocTag);              applyCodeTag(m_textBuffer, token, m_preprocTag);
625            } else if (token.isMetricPrefix()) {
626                applyCodeTag(m_textBuffer, token, m_metricTag);
627            } else if (token.isStdUnit()) {
628                applyCodeTag(m_textBuffer, token, m_stdUnitTag);
629          } else if (token.isNewLine()) {          } else if (token.isNewLine()) {
630          }          }
631      }      }
# Line 354  void ScriptEditor::updateParserIssuesByV Line 638  void ScriptEditor::updateParserIssuesByV
638      m_issues = parserContext->issues();      m_issues = parserContext->issues();
639      m_errors = parserContext->errors();      m_errors = parserContext->errors();
640      m_warnings = parserContext->warnings();      m_warnings = parserContext->warnings();
641        m_preprocComments = parserContext->preprocessorComments();
642    
643      for (int i = 0; i < m_issues.size(); ++i) {      if (!s.empty()) {
644          const LinuxSampler::ParserIssue& issue = m_issues[i];          for (int i = 0; i < m_issues.size(); ++i) {
645                const LinuxSampler::ParserIssue& issue = m_issues[i];
646          if (issue.isErr()) {  
647              applyCodeTag(m_textBuffer, issue, m_errorTag);              if (issue.isErr()) {
648          } else if (issue.isWrn()) {                  applyCodeTag(m_textBuffer, issue, m_errorTag);
649              applyCodeTag(m_textBuffer, issue, m_warningTag);              } else if (issue.isWrn()) {
650                    applyCodeTag(m_textBuffer, issue, m_warningTag);
651                }
652          }          }
653      }      }
654    
655        for (int i = 0; i < m_preprocComments.size(); ++i) {
656            applyPreprocessorComment(m_textBuffer, m_preprocComments[i],
657                                     m_preprocCommentTag);
658        }
659    
660      delete parserContext;      delete parserContext;
661  }  }
662    
# Line 398  void ScriptEditor::updateIssueTooltip(Gd Line 690  void ScriptEditor::updateIssueTooltip(Gd
690          }          }
691      }      }
692    
693        for (int i = 0; i < m_preprocComments.size(); ++i) {
694            const LinuxSampler::CodeBlock& block = m_preprocComments[i];
695            const int firstLine   = block.firstLine - 1;
696            const int firstColumn = block.firstColumn - 1;
697            const int lastLine    = block.lastLine - 1;
698            const int lastColumn  = block.lastColumn - 1;
699            if (firstLine  <= line && line <= lastLine &&
700                (firstLine != line || firstColumn <= column) &&
701                (lastLine  != line || lastColumn  >= column))
702            {
703                m_textView.set_tooltip_markup(
704                    "Code block filtered out by preceding <span foreground=\"" PREPROC_TOKEN_COLOR "\">preprocessor</span> statement."
705                );
706                return;
707            }
708        }
709    
710      m_textView.set_tooltip_markup("");      m_textView.set_tooltip_markup("");
711  }  }
712    
# Line 460  void ScriptEditor::onTextErased(const Gt Line 769  void ScriptEditor::onTextErased(const Gt
769    
770      m_textBuffer->remove_all_tags(itStart2, itEnd2);      m_textBuffer->remove_all_tags(itStart2, itEnd2);
771  #endif // USE_LS_SCRIPTVM  #endif // USE_LS_SCRIPTVM
772        updateLineNumbers();
773  }  }
774    
775  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 777  bool ScriptEditor::on_motion_notify_even
777      //TODO: event throttling would be a good idea here      //TODO: event throttling would be a good idea here
778      updateIssueTooltip(e);      updateIssueTooltip(e);
779  #endif  #endif
780    #if GTKMM_MAJOR_VERSION < 3 || (GTKMM_MAJOR_VERSION == 3 && GTKMM_MINOR_VERSION <= 24)
781      return ManagedWindow::on_motion_notify_event(e);      return ManagedWindow::on_motion_notify_event(e);
782    #else
783        Gdk::EventMotion em = Glib::wrap(e, true);
784        return ManagedWindow::on_motion_notify_event(em);
785    #endif
786  }  }
787    
788  bool ScriptEditor::onWindowDelete(GdkEventAny* e) {  void ScriptEditor::onMenuChangeFontSize() {
789        //TODO: for GTKMM >= 3.2 class Gtk::FontChooser could be used instead
790        Gtk::Dialog dialog(_("Font Size"), true /*modal*/);
791        HBox hbox;
792        hbox.set_spacing(6);
793    
794        Gtk::Label label(_("Editor's Font Size:"), Gtk::ALIGN_START);
795        hbox.pack_start(label, Gtk::PACK_SHRINK);
796    
797        Gtk::SpinButton spinButton;
798        spinButton.set_range(4, 80);
799        spinButton.set_increments(1, 10);
800        spinButton.set_value(currentFontSize());
801        hbox.pack_start(spinButton);
802    
803    #if USE_GTKMM_BOX
804        dialog.get_content_area()->pack_start(hbox);
805    #else
806        dialog.get_vbox()->pack_start(hbox);
807    #endif
808        dialog.add_button(_("_OK"), 0);
809        dialog.add_button(_("_Cancel"), 1);
810    
811    #if HAS_GTKMM_SHOW_ALL_CHILDREN
812        dialog.show_all_children();
813    #endif
814    
815        if (!dialog.run()) { // OK selected ...
816            const int newFontSize = spinButton.get_value_as_int();
817            if (newFontSize >= 4)
818                setFontSize(newFontSize, true);
819        }
820    }
821    
822    #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
823    bool ScriptEditor::onWindowDelete(Gdk::Event& e) {
824        return onWindowDeleteP(NULL);
825    }
826    #endif
827    
828    bool ScriptEditor::onWindowDeleteP(GdkEventAny* /*e*/) {
829      //printf("onWindowDelete\n");      //printf("onWindowDelete\n");
830    
831      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 874  void ScriptEditor::onModifiedChanged() {
874  }  }
875    
876  void ScriptEditor::onButtonCancel() {  void ScriptEditor::onButtonCancel() {
877      bool dropEvent = onWindowDelete(NULL);      bool dropEvent = onWindowDeleteP(NULL);
878      if (dropEvent) return;      if (dropEvent) return;
879      hide();      hide();
880  }  }
881    
882  void ScriptEditor::onButtonApply() {  void ScriptEditor::onButtonApply() {
883        signal_script_to_be_changed.emit(m_script);
884      m_script->SetScriptAsText(m_textBuffer->get_text());      m_script->SetScriptAsText(m_textBuffer->get_text());
885        signal_script_changed.emit(m_script);
886      m_textBuffer->set_modified(false);      m_textBuffer->set_modified(false);
887  }  }
888    

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

  ViewVC Help
Powered by ViewVC