/[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 2897 by schoenebeck, Sun May 1 20:20:06 2016 UTC revision 3450 by schoenebeck, Wed Jan 2 16:39:20 2019 UTC
# Line 1  Line 1 
1  /*  /*
2      Copyright (c) 2014-2016 Christian Schoenebeck      Copyright (c) 2014-2017 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 35  static bool isEvent(const Glib::ustring& Line 41  static bool isEvent(const Glib::ustring&
41    
42  #endif // !USE_LS_SCRIPTVM  #endif // !USE_LS_SCRIPTVM
43    
44    static Glib::RefPtr<Gdk::Pixbuf> createIcon(std::string name, const Glib::RefPtr<Gdk::Screen>& screen) {
45        const int targetH = 16;
46        Glib::RefPtr<Gtk::IconTheme> theme = Gtk::IconTheme::get_for_screen(screen);
47        int w = 0;
48        int h = 0; // ignored
49        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);
53        if (pixbuf->get_height() != targetH) {
54            pixbuf = pixbuf->scale_simple(targetH, targetH, Gdk::INTERP_BILINEAR);
55        }
56        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),
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        if (!Settings::singleton()->autoRestoreWindowDimension) {
83            set_default_size(800, 700);
84            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    
114      m_tagTable = Gtk::TextBuffer::TagTable::create();      m_tagTable = Gtk::TextBuffer::TagTable::create();
# Line 78  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 90  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_textBuffer = Gtk::TextBuffer::create(m_tagTable);      m_lineNrTag = Gtk::TextBuffer::Tag::create();
166      m_textView.set_buffer(m_textBuffer);      m_lineNrTag->property_foreground() = "#CCCCCC";
167        m_tagTable->add(m_lineNrTag);
168    
169        // 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();
221        m_actionGroup->add(Gtk::Action::create("MenuScript", _("_Script")));
222        m_actionGroup->add(Gtk::Action::create("Apply", _("_Apply")),
223                           Gtk::AccelKey("<control>s"),
224                           sigc::mem_fun(*this, &ScriptEditor::onButtonApply));
225        m_actionGroup->add(Gtk::Action::create("Close", _("_Close")),
226                           Gtk::AccelKey("<control>q"),
227                           sigc::mem_fun(*this, &ScriptEditor::onButtonCancel));
228        m_actionGroup->add(Gtk::Action::create("MenuEditor", _("_Editor")));
229        m_actionGroup->add(Gtk::Action::create("ChangeFont", _("_Font Size ...")),
230                           sigc::mem_fun(*this, &ScriptEditor::onMenuChangeFontSize));
231        m_uiManager = Gtk::UIManager::create();
232        m_uiManager->insert_action_group(m_actionGroup);
233        add_accel_group(m_uiManager->get_accel_group());
234        m_uiManager->add_ui_from_string(
235            "<ui>"
236            "  <menubar name='MenuBar'>"
237            "    <menu action='MenuScript'>"
238            "      <menuitem action='Apply'/>"
239            "      <separator/>"
240            "      <menuitem action='Close'/>"
241            "    </menu>"
242            "    <menu action='MenuEditor'>"
243            "      <menuitem action='ChangeFont'/>"
244            "    </menu>"
245            "  </menubar>"
246            "</ui>"
247        );
248    #endif
249    
250        m_lineNrBuffer = Gtk::TextBuffer::create(m_tagTable);
251        m_lineNrView.set_size_request(22,14);
252        m_lineNrView.set_buffer(m_lineNrBuffer);
253        m_lineNrView.set_left_margin(3);
254        m_lineNrView.set_right_margin(3);
255        m_lineNrView.property_editable() = false;
256        m_lineNrView.property_sensitive() = false;
257        m_lineNrTextViewSpacer.set_size_request(5,14);
258        m_lineNrTextViewSpacer.property_editable() = false;
259        m_lineNrTextViewSpacer.property_sensitive() = false;
260      {      {
261          Pango::FontDescription fdesc;  #if 1 //(GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
262          fdesc.set_family("monospace");          Gdk::Color color;
 #if defined(__APPLE__)  
         fdesc.set_size(12 * PANGO_SCALE);  
263  #else  #else
264          fdesc.set_size(10 * PANGO_SCALE);          Gdk::RGBA color;
265  #endif  #endif
266  #if GTKMM_MAJOR_VERSION < 3          color.set("#F5F5F5");
267          m_textView.modify_font(fdesc);          GtkWidget* widget = (GtkWidget*) m_lineNrView.gobj();
268    #if GTK_MAJOR_VERSION < 3 || (GTK_MAJOR_VERSION == 3 && GTK_MINOR_VERSION <= 24)
269            gtk_widget_modify_base(widget, GTK_STATE_NORMAL, color.gobj());
270            gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, color.gobj());
271    #endif
272        }
273        {
274    #if 1 //(GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
275            Gdk::Color color;
276  #else  #else
277          m_textView.override_font(fdesc);          Gdk::RGBA color;
278    #endif
279            color.set("#EEEEEE");
280            GtkWidget* widget = (GtkWidget*) m_lineNrTextViewSpacer.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  #endif
285      }      }
286      m_scrolledWindow.add(m_textView);      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);
287        m_textView.set_buffer(m_textBuffer);
288        m_textView.set_left_margin(5);
289        setFontSize(currentFontSize(), false);
290        m_textViewHBox.pack_start(m_lineNrView, Gtk::PACK_SHRINK);
291        m_textViewHBox.pack_start(m_lineNrTextViewSpacer, Gtk::PACK_SHRINK);
292        m_textViewHBox.add(m_textView);
293        m_scrolledWindow.add(m_textViewHBox);
294      m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);      m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
295    
296    #if USE_GTKMM_BUILDER
297        Gtk::Widget* menuBar = new Gtk::MenuBar(
298            Glib::RefPtr<Gio::Menu>::cast_dynamic(
299                m_uiManager->get_object("MenuBar")
300            )
301        );
302    #else
303        Gtk::Widget* menuBar = m_uiManager->get_widget("/MenuBar");
304    #endif
305    
306        m_vbox.pack_start(*menuBar, Gtk::PACK_SHRINK);
307      m_vbox.pack_start(m_scrolledWindow);      m_vbox.pack_start(m_scrolledWindow);
308    
309      m_buttonBox.set_layout(Gtk::BUTTONBOX_END);      m_buttonBox.set_layout(Gtk::BUTTONBOX_END);
# Line 116  ScriptEditor::ScriptEditor() : Line 312  ScriptEditor::ScriptEditor() :
312      m_applyButton.set_can_default();      m_applyButton.set_can_default();
313      m_applyButton.set_sensitive(false);      m_applyButton.set_sensitive(false);
314      m_applyButton.grab_focus();      m_applyButton.grab_focus();
315      m_vbox.pack_start(m_buttonBox, Gtk::PACK_SHRINK);  
316    #if GTKMM_MAJOR_VERSION < 3
317        m_statusHBox.set_spacing(6);
318    #elif GTKMM_MAJOR_VERSION == 3 && GTKMM_MINOR_VERSION < 12
319        m_statusImage.set_margin_left(6);
320        m_statusImage.set_margin_right(6);
321    #else
322        m_statusImage.set_margin_start(6);
323        m_statusImage.set_margin_end(6);
324    #endif
325    
326        m_statusHBox.pack_start(m_statusImage, Gtk::PACK_SHRINK);
327        m_statusHBox.pack_start(m_statusLabel);
328    #if HAS_GTKMM_SHOW_ALL_CHILDREN
329        m_statusHBox.show_all_children();
330    #endif
331    
332        m_footerHBox.pack_start(m_statusHBox);
333        m_footerHBox.pack_start(m_buttonBox, Gtk::PACK_SHRINK);
334    
335        m_vbox.pack_start(m_footerHBox, Gtk::PACK_SHRINK);
336    
337      m_applyButton.signal_clicked().connect(      m_applyButton.signal_clicked().connect(
338          sigc::mem_fun(*this, &ScriptEditor::onButtonApply)          sigc::mem_fun(*this, &ScriptEditor::onButtonApply)
# Line 142  ScriptEditor::ScriptEditor() : Line 358  ScriptEditor::ScriptEditor() :
358          sigc::mem_fun(*this, &ScriptEditor::onWindowHide)          sigc::mem_fun(*this, &ScriptEditor::onWindowHide)
359      );      );
360    
361      show_all_children();      signal_delete_event().connect(
362    #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
363            sigc::mem_fun(*this, &ScriptEditor::onWindowDelete)
364    #else
365            sigc::mem_fun(*this, &ScriptEditor::onWindowDeleteP)
366    #endif
367        );
368    
369      resize(460,300);  #if HAS_GTKMM_SHOW_ALL_CHILDREN
370        show_all_children();
371    #endif
372  }  }
373    
374  ScriptEditor::~ScriptEditor() {  ScriptEditor::~ScriptEditor() {
# Line 154  ScriptEditor::~ScriptEditor() { Line 378  ScriptEditor::~ScriptEditor() {
378  #endif  #endif
379  }  }
380    
381    int ScriptEditor::currentFontSize() const {
382    #if defined(__APPLE__)
383        const int defaultFontSize = 11;
384    #else
385        const int defaultFontSize = 10;
386    #endif
387        const int settingFontSize = Settings::singleton()->scriptEditorFontSize;
388        const int fontSize = (settingFontSize > 0) ? settingFontSize : defaultFontSize;
389        return fontSize;
390    }
391    
392    void ScriptEditor::setFontSize(int sizePt, bool save) {
393        //printf("setFontSize(%d,%d)\n", size, save);
394    
395        // make sure the real size on the screen for the editor's font is consistent
396        // on all screens (which otherwise may vary between models and DPI settings)
397        const double referenceDPI = 96;
398        double dpi = Gdk::Screen::get_default()->get_resolution();
399        double sizePx = sizePt * dpi / referenceDPI;
400    
401    #if GTKMM_MAJOR_VERSION < 3 || (GTKMM_MAJOR_VERSION == 3 && GTKMM_MINOR_VERSION < 20)
402        Pango::FontDescription fdesc;
403        fdesc.set_family("monospace");
404    # if defined(__APPLE__)
405        // fixes poor readability of default monospace font on Macs
406        if (macIsMinMac10_6())
407            fdesc.set_family("Menlo");
408    # endif
409        fdesc.set_size(sizePx * PANGO_SCALE);
410    # if GTKMM_MAJOR_VERSION < 3
411        m_lineNrView.modify_font(fdesc);
412        m_textView.modify_font(fdesc);
413    # else
414        m_lineNrView.override_font(fdesc);
415        m_textView.override_font(fdesc);
416    # endif
417    #else
418        Glib::ustring family = "monospace";
419    # if defined(__APPLE__)
420        // fixes poor readability of default monospace font on Macs
421        if (macIsMinMac10_6())
422            family = "Menlo";
423    # endif
424        if (!m_css) {
425            m_css = Gtk::CssProvider::create();
426            m_lineNrView.get_style_context()->add_provider(m_css, GTK_STYLE_PROVIDER_PRIORITY_FALLBACK);
427            m_textView.get_style_context()->add_provider(m_css, GTK_STYLE_PROVIDER_PRIORITY_FALLBACK);
428        }
429        m_css->load_from_data(
430            "* {"
431            "  font: " + ToString(sizePt) + "pt " + family + ";"
432            "}"
433        );
434    #endif
435        if (save) Settings::singleton()->scriptEditorFontSize = sizePt;
436    }
437    
438  void ScriptEditor::setScript(gig::Script* script) {  void ScriptEditor::setScript(gig::Script* script) {
439      m_script = script;      m_script = script;
440      if (!script) {      if (!script) {
# Line 167  void ScriptEditor::setScript(gig::Script Line 448  void ScriptEditor::setScript(gig::Script
448      //printf("text : '%s'\n", txt.c_str());      //printf("text : '%s'\n", txt.c_str());
449      m_textBuffer->set_text(txt);      m_textBuffer->set_text(txt);
450      m_textBuffer->set_modified(false);      m_textBuffer->set_modified(false);
451    
452        // on Gtk 3 the respective text change callback would not be called, so force this update here
453        if (txt.empty())
454            updateLineNumbers();
455    }
456    
457    void ScriptEditor::updateLineNumbers() {
458        int n = m_textBuffer->get_line_count();
459        int old = m_lineNrBuffer->get_line_count();
460        if (n == old && old > 1) return;
461        if (n < 1) n = 1;
462        const int digits = log10(n) + 1;
463        const int bufSz = digits + 2;
464        char* buf = new char[bufSz];
465        std::string sFmt1 =   "%" + ToString(digits) + "d";
466        std::string sFmt2 = "\n%" + ToString(digits) + "d";
467        Glib::ustring s;
468        for (int i = 0; i < n; ++i) {
469            snprintf(buf, bufSz, i ? sFmt2.c_str() : sFmt1.c_str(), i+1);
470            s += buf;
471        }
472        m_lineNrBuffer->remove_all_tags(m_lineNrBuffer->begin(), m_lineNrBuffer->end());
473        m_lineNrBuffer->set_text(s);
474        m_lineNrBuffer->apply_tag(m_lineNrTag, m_lineNrBuffer->begin(), m_lineNrBuffer->end());
475        if (buf) delete[] buf;
476  }  }
477    
478  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 175  void ScriptEditor::onTextInserted(const Line 481  void ScriptEditor::onTextInserted(const
481      m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());      m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
482      updateSyntaxHighlightingByVM();      updateSyntaxHighlightingByVM();
483      updateParserIssuesByVM();      updateParserIssuesByVM();
484        updateStatusBar();
485  #else  #else
486      //printf("inserted %d\n", length);      //printf("inserted %d\n", length);
487      Gtk::TextBuffer::iterator itStart = itEnd;      Gtk::TextBuffer::iterator itStart = itEnd;
# Line 218  void ScriptEditor::onTextInserted(const Line 525  void ScriptEditor::onTextInserted(const
525      ;      ;
526            
527  #endif // USE_LS_SCRIPTVM  #endif // USE_LS_SCRIPTVM
528        updateLineNumbers();
529  }  }
530    
531  #if USE_LS_SCRIPTVM  #if USE_LS_SCRIPTVM
# Line 227  LinuxSampler::ScriptVM* ScriptEditor::Ge Line 535  LinuxSampler::ScriptVM* ScriptEditor::Ge
535      return m_vm;      return m_vm;
536  }  }
537    
538  static void getIteratorsForIssue(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::ParserIssue& issue, Gtk::TextBuffer::iterator& start, Gtk::TextBuffer::iterator& end) {  template<class T>
539      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) {
540        Gtk::TextBuffer::iterator itLine =
541            txtbuf->get_iter_at_line_index(issue.firstLine - 1, 0);
542        const int charsInLine = itLine.get_bytes_in_line();
543        start = txtbuf->get_iter_at_line_index(
544            issue.firstLine - 1,
545            // check we are not getting past the end of the line here, otherwise Gtk crashes
546            issue.firstColumn - 1 < charsInLine ? issue.firstColumn - 1 : charsInLine - 1
547        );
548      end = start;      end = start;
549      end.forward_lines(issue.lastLine - issue.firstLine);      end.forward_lines(issue.lastLine - issue.firstLine);
550      end.forward_chars(      end.forward_chars(
# Line 239  static void getIteratorsForIssue(Glib::R Line 555  static void getIteratorsForIssue(Glib::R
555  }  }
556    
557  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) {
558      Gtk::TextBuffer::iterator itStart =      Gtk::TextBuffer::iterator itLine =
559          txtbuf->get_iter_at_line_index(token.firstLine(), token.firstColumn());          txtbuf->get_iter_at_line_index(token.firstLine(), 0);
560        const int charsInLine = itLine.get_bytes_in_line();
561        Gtk::TextBuffer::iterator itStart = txtbuf->get_iter_at_line_index(
562            token.firstLine(),
563            // check we are not getting past the end of the line here, otherwise Gtk crashes
564            token.firstColumn() < charsInLine ? token.firstColumn() : charsInLine - 1
565        );
566      Gtk::TextBuffer::iterator itEnd = itStart;      Gtk::TextBuffer::iterator itEnd = itStart;
567      const int length = token.text().length();      const int length = token.text().length();
568      itEnd.forward_chars(length);      itEnd.forward_chars(length);
# Line 253  static void applyCodeTag(Glib::RefPtr<Gt Line 575  static void applyCodeTag(Glib::RefPtr<Gt
575      txtbuf->apply_tag(tag, itStart, itEnd);      txtbuf->apply_tag(tag, itStart, itEnd);
576  }  }
577    
578    static void applyPreprocessorComment(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::CodeBlock& block, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
579        Gtk::TextBuffer::iterator itStart, itEnd;
580        getIteratorsForIssue(txtbuf, block, itStart, itEnd);
581        txtbuf->apply_tag(tag, itStart, itEnd);
582    }
583    
584  void ScriptEditor::updateSyntaxHighlightingByVM() {  void ScriptEditor::updateSyntaxHighlightingByVM() {
585      GetScriptVM();      GetScriptVM();
586      const std::string s = m_textBuffer->get_text();      const std::string s = m_textBuffer->get_text();
587        if (s.empty()) return;
588      std::vector<LinuxSampler::VMSourceToken> tokens = m_vm->syntaxHighlighting(s);      std::vector<LinuxSampler::VMSourceToken> tokens = m_vm->syntaxHighlighting(s);
589    
590      for (int i = 0; i < tokens.size(); ++i) {      for (int i = 0; i < tokens.size(); ++i) {
# Line 289  void ScriptEditor::updateParserIssuesByV Line 618  void ScriptEditor::updateParserIssuesByV
618      const std::string s = m_textBuffer->get_text();      const std::string s = m_textBuffer->get_text();
619      LinuxSampler::VMParserContext* parserContext = m_vm->loadScript(s);      LinuxSampler::VMParserContext* parserContext = m_vm->loadScript(s);
620      m_issues = parserContext->issues();      m_issues = parserContext->issues();
621        m_errors = parserContext->errors();
622      for (int i = 0; i < m_issues.size(); ++i) {      m_warnings = parserContext->warnings();
623          const LinuxSampler::ParserIssue& issue = m_issues[i];      m_preprocComments = parserContext->preprocessorComments();
624    
625          if (issue.isErr()) {      if (!s.empty()) {
626              applyCodeTag(m_textBuffer, issue, m_errorTag);          for (int i = 0; i < m_issues.size(); ++i) {
627          } else if (issue.isWrn()) {              const LinuxSampler::ParserIssue& issue = m_issues[i];
628              applyCodeTag(m_textBuffer, issue, m_warningTag);  
629                if (issue.isErr()) {
630                    applyCodeTag(m_textBuffer, issue, m_errorTag);
631                } else if (issue.isWrn()) {
632                    applyCodeTag(m_textBuffer, issue, m_warningTag);
633                }
634          }          }
635      }      }
636    
637        for (int i = 0; i < m_preprocComments.size(); ++i) {
638            applyPreprocessorComment(m_textBuffer, m_preprocComments[i],
639                                     m_preprocCommentTag);
640        }
641    
642      delete parserContext;      delete parserContext;
643  }  }
644    
# Line 333  void ScriptEditor::updateIssueTooltip(Gd Line 672  void ScriptEditor::updateIssueTooltip(Gd
672          }          }
673      }      }
674    
675        for (int i = 0; i < m_preprocComments.size(); ++i) {
676            const LinuxSampler::CodeBlock& block = m_preprocComments[i];
677            const int firstLine   = block.firstLine - 1;
678            const int firstColumn = block.firstColumn - 1;
679            const int lastLine    = block.lastLine - 1;
680            const int lastColumn  = block.lastColumn - 1;
681            if (firstLine  <= line && line <= lastLine &&
682                (firstLine != line || firstColumn <= column) &&
683                (lastLine  != line || lastColumn  >= column))
684            {
685                m_textView.set_tooltip_markup(
686                    "Code block filtered out by preceding <span foreground=\"" PREPROC_TOKEN_COLOR "\">preprocessor</span> statement."
687                );
688                return;
689            }
690        }
691    
692      m_textView.set_tooltip_markup("");      m_textView.set_tooltip_markup("");
693  }  }
694    
695    static std::string warningsCountTxt(const std::vector<LinuxSampler::ParserIssue> warnings) {
696        std::string txt = "<span foreground=\"#c4950c\">" + ToString(warnings.size());
697        txt += (warnings.size() == 1) ? " Warning" : " Warnings";
698        txt += "</span>";
699        return txt;
700    }
701    
702    static std::string errorsCountTxt(const std::vector<LinuxSampler::ParserIssue> errors) {
703        std::string txt = "<span foreground=\"#c40c0c\">" + ToString(errors.size());
704        txt += (errors.size() == 1) ? " Error" : " Errors";
705        txt += "</span>";
706        return txt;
707    }
708    
709    void ScriptEditor::updateStatusBar() {
710        // update status text
711        std::string txt;
712        if (m_issues.empty()) {
713            txt = "No issues with this script.";
714        } else {
715            const char* txtWontLoad = ". Sampler won't load instruments using this script!";
716            txt = "There ";
717            txt += (m_errors.size() <= 1 && m_warnings.size() <= 1) ? "is " : "are ";
718            if (m_errors.empty()) {
719                txt += warningsCountTxt(m_warnings) + ". Script will load, but might not behave as expected!";
720            } else if (m_warnings.empty()) {
721                txt += errorsCountTxt(m_errors) + txtWontLoad;
722            } else {
723                txt += errorsCountTxt(m_errors) + " and " +
724                       warningsCountTxt(m_warnings) + txtWontLoad;
725            }
726        }
727        m_statusLabel.set_markup(txt);
728    
729        // update status icon
730        m_statusImage.set(
731            m_issues.empty() ? m_successIcon : !m_errors.empty() ? m_errorIcon : m_warningIcon
732        );
733    }
734    
735  #endif // USE_LS_SCRIPTVM  #endif // USE_LS_SCRIPTVM
736    
737  void ScriptEditor::onTextErased(const Gtk::TextBuffer::iterator& itStart, const Gtk::TextBuffer::iterator& itEnd) {  void ScriptEditor::onTextErased(const Gtk::TextBuffer::iterator& itStart, const Gtk::TextBuffer::iterator& itEnd) {
# Line 344  void ScriptEditor::onTextErased(const Gt Line 740  void ScriptEditor::onTextErased(const Gt
740      m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());      m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
741      updateSyntaxHighlightingByVM();      updateSyntaxHighlightingByVM();
742      updateParserIssuesByVM();      updateParserIssuesByVM();
743        updateStatusBar();
744  #else  #else
745      Gtk::TextBuffer::iterator itStart2 = itStart;      Gtk::TextBuffer::iterator itStart2 = itStart;
746      if (itStart2.inside_word() || itStart2.ends_word())      if (itStart2.inside_word() || itStart2.ends_word())
# Line 354  void ScriptEditor::onTextErased(const Gt Line 751  void ScriptEditor::onTextErased(const Gt
751    
752      m_textBuffer->remove_all_tags(itStart2, itEnd2);      m_textBuffer->remove_all_tags(itStart2, itEnd2);
753  #endif // USE_LS_SCRIPTVM  #endif // USE_LS_SCRIPTVM
754        updateLineNumbers();
755  }  }
756    
757  bool ScriptEditor::on_motion_notify_event(GdkEventMotion* e) {  bool ScriptEditor::on_motion_notify_event(GdkEventMotion* e) {
# Line 361  bool ScriptEditor::on_motion_notify_even Line 759  bool ScriptEditor::on_motion_notify_even
759      //TODO: event throttling would be a good idea here      //TODO: event throttling would be a good idea here
760      updateIssueTooltip(e);      updateIssueTooltip(e);
761  #endif  #endif
762    #if GTKMM_MAJOR_VERSION < 3 || (GTKMM_MAJOR_VERSION == 3 && GTKMM_MINOR_VERSION <= 24)
763      return ManagedWindow::on_motion_notify_event(e);      return ManagedWindow::on_motion_notify_event(e);
764    #else
765        Gdk::EventMotion em = Glib::wrap(e, true);
766        return ManagedWindow::on_motion_notify_event(em);
767    #endif
768    }
769    
770    void ScriptEditor::onMenuChangeFontSize() {
771        //TODO: for GTKMM >= 3.2 class Gtk::FontChooser could be used instead
772        Gtk::Dialog dialog(_("Font Size"), true /*modal*/);
773        HBox hbox;
774        hbox.set_spacing(6);
775    
776        Gtk::Label label(_("Editor's Font Size:"), Gtk::ALIGN_START);
777        hbox.pack_start(label, Gtk::PACK_SHRINK);
778    
779        Gtk::SpinButton spinButton;
780        spinButton.set_range(4, 80);
781        spinButton.set_increments(1, 10);
782        spinButton.set_value(currentFontSize());
783        hbox.pack_start(spinButton);
784    
785    #if USE_GTKMM_BOX
786        dialog.get_content_area()->pack_start(hbox);
787    #else
788        dialog.get_vbox()->pack_start(hbox);
789    #endif
790        dialog.add_button(_("_OK"), 0);
791        dialog.add_button(_("_Cancel"), 1);
792    
793    #if HAS_GTKMM_SHOW_ALL_CHILDREN
794        dialog.show_all_children();
795    #endif
796    
797        if (!dialog.run()) { // OK selected ...
798            const int newFontSize = spinButton.get_value_as_int();
799            if (newFontSize >= 4)
800                setFontSize(newFontSize, true);
801        }
802    }
803    
804    #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
805    bool ScriptEditor::onWindowDelete(Gdk::Event& e) {
806        return onWindowDeleteP(NULL);
807    }
808    #endif
809    
810    bool ScriptEditor::onWindowDeleteP(GdkEventAny* /*e*/) {
811        //printf("onWindowDelete\n");
812    
813        if (!isModified()) return false; // propagate event further (which will close this window)
814    
815        gchar* msg = g_strdup_printf(_("Apply changes to instrument script \"%s\" before closing?"),
816                                     m_script->Name.c_str());
817        Gtk::MessageDialog dialog(*this, msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
818        g_free(msg);
819        dialog.set_secondary_text(_("If you close without applying, your changes will be lost."));
820        dialog.add_button(_("Close _Without Applying"), Gtk::RESPONSE_NO);
821        dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL);
822        dialog.add_button(_("_Apply"), Gtk::RESPONSE_YES);
823        dialog.set_default_response(Gtk::RESPONSE_YES);
824        int response = dialog.run();
825        dialog.hide();
826    
827        // user decided to close script editor without saving
828        if (response == Gtk::RESPONSE_NO)
829            return false; // propagate event further (which will close this window)
830    
831        // user cancelled dialog, thus don't close script editor
832        if (response == Gtk::RESPONSE_CANCEL) {
833            show();
834            return true; // drop event (prevents closing this window)
835        }
836    
837        // user wants to apply the changes, afterwards close window
838        if (response == Gtk::RESPONSE_YES) {
839            onButtonApply();
840            return false; // propagate event further (which will close this window)
841        }
842    
843        // should never ever make it to this point actually
844        return false;
845    }
846    
847    bool ScriptEditor::isModified() const {
848        return m_textBuffer->get_modified();
849  }  }
850    
851  void ScriptEditor::onModifiedChanged() {  void ScriptEditor::onModifiedChanged() {
852      m_applyButton.set_sensitive( m_textBuffer->get_modified() );      m_applyButton.set_sensitive(isModified());
853    #if USE_LS_SCRIPTVM
854        updateStatusBar();
855    #endif
856  }  }
857    
858  void ScriptEditor::onButtonCancel() {  void ScriptEditor::onButtonCancel() {
859        bool dropEvent = onWindowDeleteP(NULL);
860        if (dropEvent) return;
861      hide();      hide();
862  }  }
863    
864  void ScriptEditor::onButtonApply() {  void ScriptEditor::onButtonApply() {
865        signal_script_to_be_changed.emit(m_script);
866      m_script->SetScriptAsText(m_textBuffer->get_text());      m_script->SetScriptAsText(m_textBuffer->get_text());
867        signal_script_changed.emit(m_script);
868      m_textBuffer->set_modified(false);      m_textBuffer->set_modified(false);
869  }  }
870    

Legend:
Removed from v.2897  
changed lines
  Added in v.3450

  ViewVC Help
Powered by ViewVC