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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3364 - (hide annotations) (download)
Tue Nov 14 18:07:25 2017 UTC (6 years, 5 months ago) by schoenebeck
File size: 30357 byte(s)
* Added experimental support for upcoming GTK(MM)4
  (for now up to GTKMM 3.91.2 while still preserving backward compatibility
  down to GTKMM 2).
* Re-merged r2845 to compile now with and without Gtk "Stock ID" API
  (see also r3158).

1 schoenebeck 2604 /*
2 schoenebeck 3225 Copyright (c) 2014-2017 Christian Schoenebeck
3 schoenebeck 2604
4     This file is part of "gigedit" and released under the terms of the
5     GNU General Public License version 2.
6     */
7    
8     #include "scripteditor.h"
9     #include "global.h"
10 schoenebeck 3364 #include "compat.h"
11 schoenebeck 3310 #include <gtk/gtkwidget.h> // for gtk_widget_modify_*()
12 schoenebeck 3314 #if defined(__APPLE__)
13     # include "MacHelper.h"
14     #endif
15     #include <math.h> // for log10()
16 schoenebeck 2604
17 schoenebeck 2886 #if !USE_LS_SCRIPTVM
18    
19 schoenebeck 2604 static const std::string _keywords[] = {
20     "on", "end", "declare", "while", "if", "or", "and", "not", "else", "case",
21 schoenebeck 3261 "select", "to", "const", "polyphonic", "mod", "synchronized"
22 schoenebeck 2604 };
23     static int _keywordsSz = sizeof(_keywords) / sizeof(std::string);
24    
25     static const std::string _eventNames[] = {
26     "init", "note", "release", "controller"
27     };
28     static int _eventNamesSz = sizeof(_eventNames) / sizeof(std::string);
29    
30     static bool isKeyword(const Glib::ustring& s) {
31     for (int i = 0; i < _keywordsSz; ++i)
32     if (_keywords[i] == s) return true;
33     return false;
34     }
35    
36     static bool isEvent(const Glib::ustring& s) {
37     for (int i = 0; i < _eventNamesSz; ++i)
38     if (_eventNames[i] == s) return true;
39     return false;
40     }
41    
42 schoenebeck 2886 #endif // !USE_LS_SCRIPTVM
43    
44 schoenebeck 2899 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 schoenebeck 2928 if (!theme->has_icon(name))
51     return Glib::RefPtr<Gdk::Pixbuf>();
52 schoenebeck 2899 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 schoenebeck 2928 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 schoenebeck 2604 ScriptEditor::ScriptEditor() :
68 schoenebeck 2899 m_statusLabel("", Gtk::ALIGN_START),
69 schoenebeck 3364 #if HAS_GTKMM_STOCK
70 schoenebeck 3158 m_applyButton(Gtk::Stock::APPLY),
71     m_cancelButton(Gtk::Stock::CANCEL)
72 schoenebeck 3364 #else
73     m_applyButton(_("_Apply"), true),
74     m_cancelButton(_("_Cancel"), true)
75     #endif
76 schoenebeck 2604 {
77     m_script = NULL;
78 schoenebeck 2896 #if USE_LS_SCRIPTVM
79 schoenebeck 2886 m_vm = NULL;
80 schoenebeck 2896 #endif
81 schoenebeck 2604
82 schoenebeck 3225 if (!Settings::singleton()->autoRestoreWindowDimension) {
83     set_default_size(800, 700);
84     set_position(Gtk::WIN_POS_MOUSE);
85     }
86    
87 schoenebeck 2928 // 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 schoenebeck 2899
100 schoenebeck 2928 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 schoenebeck 2604 add(m_vbox);
113    
114     m_tagTable = Gtk::TextBuffer::TagTable::create();
115 schoenebeck 2886
116 schoenebeck 2604 m_keywordTag = Gtk::TextBuffer::Tag::create();
117 schoenebeck 2887 m_keywordTag->property_foreground() = "#000000"; // black
118 schoenebeck 2604 m_keywordTag->property_weight() = PANGO_WEIGHT_BOLD;
119     m_tagTable->add(m_keywordTag);
120 schoenebeck 2886
121 schoenebeck 2604 m_eventTag = Gtk::TextBuffer::Tag::create();
122 schoenebeck 2887 m_eventTag->property_foreground() = "#07c0cf"; // cyan 1
123 schoenebeck 2610 m_eventTag->property_weight() = PANGO_WEIGHT_BOLD;
124 schoenebeck 2604 m_tagTable->add(m_eventTag);
125 schoenebeck 2886
126     m_variableTag = Gtk::TextBuffer::Tag::create();
127 schoenebeck 2887 m_variableTag->property_foreground() = "#790cc4"; // magenta
128 schoenebeck 2886 m_tagTable->add(m_variableTag);
129    
130     m_functionTag = Gtk::TextBuffer::Tag::create();
131 schoenebeck 2887 m_functionTag->property_foreground() = "#1ba1dd"; // cyan 2
132 schoenebeck 2886 m_tagTable->add(m_functionTag);
133    
134     m_numberTag = Gtk::TextBuffer::Tag::create();
135 schoenebeck 2887 m_numberTag->property_foreground() = "#c4950c"; // yellow
136 schoenebeck 2886 m_tagTable->add(m_numberTag);
137    
138     m_stringTag = Gtk::TextBuffer::Tag::create();
139 schoenebeck 2887 m_stringTag->property_foreground() = "#c40c0c"; // red
140 schoenebeck 2886 m_tagTable->add(m_stringTag);
141    
142     m_commentTag = Gtk::TextBuffer::Tag::create();
143 schoenebeck 2887 m_commentTag->property_foreground() = "#9c9c9c"; // gray
144 schoenebeck 2886 m_tagTable->add(m_commentTag);
145    
146 schoenebeck 3286 #define PREPROC_TOKEN_COLOR "#2f8a33" // green
147    
148 schoenebeck 2886 m_preprocTag = Gtk::TextBuffer::Tag::create();
149 schoenebeck 3286 m_preprocTag->property_foreground() = PREPROC_TOKEN_COLOR;
150 schoenebeck 2886 m_tagTable->add(m_preprocTag);
151    
152 schoenebeck 3286 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 schoenebeck 2890 m_errorTag = Gtk::TextBuffer::Tag::create();
158     m_errorTag->property_background() = "#ff9393"; // red
159     m_tagTable->add(m_errorTag);
160    
161     m_warningTag = Gtk::TextBuffer::Tag::create();
162     m_warningTag->property_background() = "#fffd7c"; // yellow
163     m_tagTable->add(m_warningTag);
164    
165 schoenebeck 3310 m_lineNrTag = Gtk::TextBuffer::Tag::create();
166     m_lineNrTag->property_foreground() = "#CCCCCC";
167     m_tagTable->add(m_lineNrTag);
168    
169 schoenebeck 2901 // create menu
170 schoenebeck 3364 #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 schoenebeck 2901 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 schoenebeck 2903 Gtk::AccelKey("<control>q"),
227 schoenebeck 2901 sigc::mem_fun(*this, &ScriptEditor::onButtonCancel));
228 schoenebeck 2956 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 schoenebeck 2901 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 schoenebeck 2956 " <menu action='MenuEditor'>"
243     " <menuitem action='ChangeFont'/>"
244     " </menu>"
245 schoenebeck 2901 " </menubar>"
246     "</ui>"
247     );
248 schoenebeck 3364 #endif
249 schoenebeck 2901
250 schoenebeck 3310 m_lineNrBuffer = Gtk::TextBuffer::create(m_tagTable);
251     m_lineNrView.set_buffer(m_lineNrBuffer);
252     m_lineNrView.set_left_margin(3);
253     m_lineNrView.set_right_margin(3);
254     m_lineNrTextViewSpacer.set_size_request(5);
255     {
256 schoenebeck 3364 #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
257 schoenebeck 3310 Gdk::Color color;
258 schoenebeck 3364 #else
259     Gdk::RGBA color;
260     #endif
261 schoenebeck 3310 color.set("#F5F5F5");
262     GtkWidget* widget = (GtkWidget*) m_lineNrView.gobj();
263 schoenebeck 3364 #if GTK_MAJOR_VERSION < 3
264 schoenebeck 3310 gtk_widget_modify_base(widget, GTK_STATE_NORMAL, color.gobj());
265     gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, color.gobj());
266 schoenebeck 3364 #endif
267 schoenebeck 3310 }
268     {
269 schoenebeck 3364 #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
270 schoenebeck 3310 Gdk::Color color;
271 schoenebeck 3364 #else
272     Gdk::RGBA color;
273     #endif
274 schoenebeck 3310 color.set("#EEEEEE");
275     GtkWidget* widget = (GtkWidget*) m_lineNrTextViewSpacer.gobj();
276 schoenebeck 3364 #if GTK_MAJOR_VERSION < 3
277 schoenebeck 3310 gtk_widget_modify_base(widget, GTK_STATE_NORMAL, color.gobj());
278     gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, color.gobj());
279 schoenebeck 3364 #endif
280 schoenebeck 3310 }
281 schoenebeck 2604 m_textBuffer = Gtk::TextBuffer::create(m_tagTable);
282     m_textView.set_buffer(m_textBuffer);
283 schoenebeck 3310 m_textView.set_left_margin(5);
284 schoenebeck 2956 setFontSize(currentFontSize(), false);
285 schoenebeck 3310 m_textViewHBox.pack_start(m_lineNrView, Gtk::PACK_SHRINK);
286     m_textViewHBox.pack_start(m_lineNrTextViewSpacer, Gtk::PACK_SHRINK);
287     m_textViewHBox.add(m_textView);
288     m_scrolledWindow.add(m_textViewHBox);
289 schoenebeck 2604 m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
290 schoenebeck 2901
291 schoenebeck 3364 #if USE_GTKMM_BUILDER
292     Gtk::Widget* menuBar = new Gtk::MenuBar(
293     Glib::RefPtr<Gio::Menu>::cast_dynamic(
294     m_uiManager->get_object("MenuBar")
295     )
296     );
297     #else
298 schoenebeck 2901 Gtk::Widget* menuBar = m_uiManager->get_widget("/MenuBar");
299 schoenebeck 3364 #endif
300    
301 schoenebeck 2901 m_vbox.pack_start(*menuBar, Gtk::PACK_SHRINK);
302 schoenebeck 2604 m_vbox.pack_start(m_scrolledWindow);
303    
304     m_buttonBox.set_layout(Gtk::BUTTONBOX_END);
305     m_buttonBox.pack_start(m_applyButton);
306     m_buttonBox.pack_start(m_cancelButton);
307     m_applyButton.set_can_default();
308     m_applyButton.set_sensitive(false);
309     m_applyButton.grab_focus();
310 schoenebeck 2901
311 schoenebeck 3364 #if GTKMM_MAJOR_VERSION < 3
312     m_statusHBox.set_spacing(6);
313     #elif GTKMM_MAJOR_VERSION == 3 && GTKMM_MINOR_VERSION < 12
314 schoenebeck 2899 m_statusImage.set_margin_left(6);
315     m_statusImage.set_margin_right(6);
316 schoenebeck 2900 #else
317 schoenebeck 3364 m_statusImage.set_margin_start(6);
318     m_statusImage.set_margin_end(6);
319 schoenebeck 2900 #endif
320 schoenebeck 2604
321 schoenebeck 2899 m_statusHBox.pack_start(m_statusImage, Gtk::PACK_SHRINK);
322     m_statusHBox.pack_start(m_statusLabel);
323 schoenebeck 3364 #if HAS_GTKMM_SHOW_ALL_CHILDREN
324 schoenebeck 2899 m_statusHBox.show_all_children();
325 schoenebeck 3364 #endif
326 schoenebeck 2899
327     m_footerHBox.pack_start(m_statusHBox);
328     m_footerHBox.pack_start(m_buttonBox, Gtk::PACK_SHRINK);
329    
330     m_vbox.pack_start(m_footerHBox, Gtk::PACK_SHRINK);
331    
332 schoenebeck 2604 m_applyButton.signal_clicked().connect(
333     sigc::mem_fun(*this, &ScriptEditor::onButtonApply)
334     );
335    
336     m_cancelButton.signal_clicked().connect(
337     sigc::mem_fun(*this, &ScriptEditor::onButtonCancel)
338     );
339    
340     m_textBuffer->signal_insert().connect(
341     sigc::mem_fun(*this, &ScriptEditor::onTextInserted)
342     );
343    
344     m_textBuffer->signal_erase().connect(
345     sigc::mem_fun(*this, &ScriptEditor::onTextErased)
346     );
347    
348     m_textBuffer->signal_modified_changed().connect(
349     sigc::mem_fun(*this, &ScriptEditor::onModifiedChanged)
350     );
351    
352     signal_hide().connect(
353     sigc::mem_fun(*this, &ScriptEditor::onWindowHide)
354     );
355    
356 schoenebeck 2898 signal_delete_event().connect(
357 schoenebeck 3364 #if GTKMM_MAJOR_VERSION > 3 || (GTKMM_MAJOR_VERSION == 3 && (GTKMM_MINOR_VERSION > 91 || (GTKMM_MINOR_VERSION == 91 && GTKMM_MICRO_VERSION >= 2))) // GTKMM >= 3.91.2
358 schoenebeck 2898 sigc::mem_fun(*this, &ScriptEditor::onWindowDelete)
359 schoenebeck 3364 #else
360     sigc::mem_fun(*this, &ScriptEditor::onWindowDeleteP)
361     #endif
362 schoenebeck 2898 );
363    
364 schoenebeck 3364 #if HAS_GTKMM_SHOW_ALL_CHILDREN
365 schoenebeck 2604 show_all_children();
366 schoenebeck 3364 #endif
367 schoenebeck 2604 }
368    
369     ScriptEditor::~ScriptEditor() {
370     printf("ScriptEditor destruct\n");
371 schoenebeck 2896 #if USE_LS_SCRIPTVM
372 schoenebeck 2886 if (m_vm) delete m_vm;
373 schoenebeck 2896 #endif
374 schoenebeck 2604 }
375    
376 schoenebeck 2956 int ScriptEditor::currentFontSize() const {
377     #if defined(__APPLE__)
378 schoenebeck 3314 const int defaultFontSize = 11;
379 schoenebeck 2956 #else
380     const int defaultFontSize = 10;
381     #endif
382     const int settingFontSize = Settings::singleton()->scriptEditorFontSize;
383     const int fontSize = (settingFontSize > 0) ? settingFontSize : defaultFontSize;
384     return fontSize;
385     }
386    
387     void ScriptEditor::setFontSize(int size, bool save) {
388     //printf("setFontSize(%d,%d)\n", size, save);
389 schoenebeck 3364 #if GTKMM_MAJOR_VERSION < 3 || (GTKMM_MAJOR_VERSION == 3 && GTKMM_MINOR_VERSION < 20)
390 schoenebeck 2956 Pango::FontDescription fdesc;
391     fdesc.set_family("monospace");
392 schoenebeck 3364 # if defined(__APPLE__)
393 schoenebeck 3314 // fixes poor readability of default monospace font on Macs
394     if (macIsMinMac10_6())
395     fdesc.set_family("Menlo");
396 schoenebeck 3364 # endif
397 schoenebeck 2956 fdesc.set_size(size * PANGO_SCALE);
398 schoenebeck 3364 # if GTKMM_MAJOR_VERSION < 3
399 schoenebeck 3310 m_lineNrView.modify_font(fdesc);
400 schoenebeck 2956 m_textView.modify_font(fdesc);
401 schoenebeck 3364 # else
402 schoenebeck 3310 m_lineNrView.override_font(fdesc);
403 schoenebeck 2956 m_textView.override_font(fdesc);
404 schoenebeck 3364 # endif
405     #else
406     Glib::ustring family = "monospace";
407     # if defined(__APPLE__)
408     // fixes poor readability of default monospace font on Macs
409     if (macIsMinMac10_6())
410     family = "Menlo";
411     # endif
412     if (!m_css) {
413     m_css = Gtk::CssProvider::create();
414     m_lineNrView.get_style_context()->add_provider(m_css, GTK_STYLE_PROVIDER_PRIORITY_FALLBACK);
415     m_textView.get_style_context()->add_provider(m_css, GTK_STYLE_PROVIDER_PRIORITY_FALLBACK);
416     }
417     m_css->load_from_data(
418     "* {"
419     " font: " + ToString(size) + " " + family + ";"
420     "}"
421     );
422 schoenebeck 2956 #endif
423     if (save) Settings::singleton()->scriptEditorFontSize = size;
424     }
425    
426 schoenebeck 2604 void ScriptEditor::setScript(gig::Script* script) {
427     m_script = script;
428     if (!script) {
429     set_title(_("No Script"));
430     return;
431     }
432    
433     set_title(std::string(_("Instrument Script")) + " - \"" + script->Name + "\"");
434    
435     std::string txt = script->GetScriptAsText();
436     //printf("text : '%s'\n", txt.c_str());
437     m_textBuffer->set_text(txt);
438     m_textBuffer->set_modified(false);
439     }
440    
441 schoenebeck 3310 void ScriptEditor::updateLineNumbers() {
442     const int n = m_textBuffer->get_line_count();
443     const int old = m_lineNrBuffer->get_line_count();
444     if (n == old) return;
445 schoenebeck 3314 const int digits = log10(n) + 1;
446     const int bufSz = digits + 2;
447     char* buf = new char[bufSz];
448     std::string sFmt1 = "%" + ToString(digits) + "d";
449     std::string sFmt2 = "\n%" + ToString(digits) + "d";
450 schoenebeck 3310 Glib::ustring s;
451     for (int i = 0; i < n; ++i) {
452 schoenebeck 3314 snprintf(buf, bufSz, i ? sFmt2.c_str() : sFmt1.c_str(), i+1);
453     s += buf;
454 schoenebeck 3310 }
455     m_lineNrBuffer->remove_all_tags(m_lineNrBuffer->begin(), m_lineNrBuffer->end());
456     m_lineNrBuffer->set_text(s);
457     m_lineNrBuffer->apply_tag(m_lineNrTag, m_lineNrBuffer->begin(), m_lineNrBuffer->end());
458 schoenebeck 3314 if (buf) delete[] buf;
459 schoenebeck 3310 }
460    
461 schoenebeck 2604 void ScriptEditor::onTextInserted(const Gtk::TextBuffer::iterator& itEnd, const Glib::ustring& txt, int length) {
462 schoenebeck 2897 //printf("onTextInserted()\n");
463 schoenebeck 2886 #if USE_LS_SCRIPTVM
464 schoenebeck 2890 m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
465 schoenebeck 2886 updateSyntaxHighlightingByVM();
466 schoenebeck 2890 updateParserIssuesByVM();
467 schoenebeck 2899 updateStatusBar();
468 schoenebeck 2886 #else
469 schoenebeck 2610 //printf("inserted %d\n", length);
470 schoenebeck 2604 Gtk::TextBuffer::iterator itStart = itEnd;
471     itStart.backward_chars(length);
472    
473     Gtk::TextBuffer::iterator it = itStart;
474     it.backward_word_start();
475    
476     bool eofReached = false;
477     while (it <= itEnd) {
478     Gtk::TextBuffer::iterator itWordStart = it;
479     if (!it.forward_word_end()) {
480     eofReached = true;
481     it = itEnd;
482     }
483    
484     Glib::ustring s = m_textBuffer->get_text(itWordStart, it, false);
485 schoenebeck 2610 //printf("{%s}\n", s.c_str());
486 schoenebeck 2604 if (isKeyword(s))
487     m_textBuffer->apply_tag(m_keywordTag, itWordStart, it);
488     else if (isEvent(s)) {
489     // check if previous word is "on"
490     Gtk::TextBuffer::iterator itPreviousWordStart = itWordStart;
491     if (itPreviousWordStart.backward_word_start()) {
492     Gtk::TextBuffer::iterator itPreviousWordEnd = itPreviousWordStart;
493     itPreviousWordEnd.forward_word_end();
494     if (m_textBuffer->get_text(itPreviousWordStart, itPreviousWordEnd, false) == "on") {
495     m_textBuffer->apply_tag(m_eventTag, itWordStart, it);
496     }
497     }
498     }
499    
500     if (eofReached) break;
501    
502     while (!it.inside_word())
503     if (!it.forward_char())
504     goto EOF_REACHED;
505     }
506    
507     EOF_REACHED:
508     ;
509 schoenebeck 2886
510     #endif // USE_LS_SCRIPTVM
511 schoenebeck 3310 updateLineNumbers();
512 schoenebeck 2604 }
513    
514 schoenebeck 2886 #if USE_LS_SCRIPTVM
515    
516 schoenebeck 2890 LinuxSampler::ScriptVM* ScriptEditor::GetScriptVM() {
517     if (!m_vm) m_vm = LinuxSampler::ScriptVMFactory::Create("gig");
518     return m_vm;
519     }
520    
521 schoenebeck 3286 template<class T>
522     static void getIteratorsForIssue(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const T& issue, Gtk::TextBuffer::iterator& start, Gtk::TextBuffer::iterator& end) {
523 schoenebeck 3206 Gtk::TextBuffer::iterator itLine =
524     txtbuf->get_iter_at_line_index(issue.firstLine - 1, 0);
525     const int charsInLine = itLine.get_bytes_in_line();
526     start = txtbuf->get_iter_at_line_index(
527     issue.firstLine - 1,
528     // check we are not getting past the end of the line here, otherwise Gtk crashes
529     issue.firstColumn - 1 < charsInLine ? issue.firstColumn - 1 : charsInLine - 1
530     );
531 schoenebeck 2897 end = start;
532     end.forward_lines(issue.lastLine - issue.firstLine);
533     end.forward_chars(
534     (issue.lastLine != issue.firstLine)
535     ? issue.lastColumn - 1
536     : issue.lastColumn - issue.firstColumn + 1
537     );
538     }
539    
540 schoenebeck 2886 static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::VMSourceToken& token, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
541 schoenebeck 3206 Gtk::TextBuffer::iterator itLine =
542     txtbuf->get_iter_at_line_index(token.firstLine(), 0);
543     const int charsInLine = itLine.get_bytes_in_line();
544     Gtk::TextBuffer::iterator itStart = txtbuf->get_iter_at_line_index(
545     token.firstLine(),
546     // check we are not getting past the end of the line here, otherwise Gtk crashes
547     token.firstColumn() < charsInLine ? token.firstColumn() : charsInLine - 1
548     );
549 schoenebeck 2886 Gtk::TextBuffer::iterator itEnd = itStart;
550     const int length = token.text().length();
551     itEnd.forward_chars(length);
552     txtbuf->apply_tag(tag, itStart, itEnd);
553     }
554    
555 schoenebeck 2890 static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::ParserIssue& issue, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
556 schoenebeck 2897 Gtk::TextBuffer::iterator itStart, itEnd;
557     getIteratorsForIssue(txtbuf, issue, itStart, itEnd);
558 schoenebeck 2890 txtbuf->apply_tag(tag, itStart, itEnd);
559     }
560    
561 schoenebeck 3286 static void applyPreprocessorComment(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::CodeBlock& block, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
562     Gtk::TextBuffer::iterator itStart, itEnd;
563     getIteratorsForIssue(txtbuf, block, itStart, itEnd);
564     txtbuf->apply_tag(tag, itStart, itEnd);
565     }
566    
567 schoenebeck 2886 void ScriptEditor::updateSyntaxHighlightingByVM() {
568 schoenebeck 2890 GetScriptVM();
569 schoenebeck 2886 const std::string s = m_textBuffer->get_text();
570 schoenebeck 2975 if (s.empty()) return;
571 schoenebeck 2886 std::vector<LinuxSampler::VMSourceToken> tokens = m_vm->syntaxHighlighting(s);
572    
573     for (int i = 0; i < tokens.size(); ++i) {
574     const LinuxSampler::VMSourceToken& token = tokens[i];
575    
576     if (token.isKeyword()) {
577     applyCodeTag(m_textBuffer, token, m_keywordTag);
578     } else if (token.isVariableName()) {
579     applyCodeTag(m_textBuffer, token, m_variableTag);
580     } else if (token.isIdentifier()) {
581     if (token.isEventHandlerName()) {
582     applyCodeTag(m_textBuffer, token, m_eventTag);
583     } else { // a function ...
584     applyCodeTag(m_textBuffer, token, m_functionTag);
585     }
586     } else if (token.isNumberLiteral()) {
587     applyCodeTag(m_textBuffer, token, m_numberTag);
588     } else if (token.isStringLiteral()) {
589     applyCodeTag(m_textBuffer, token, m_stringTag);
590     } else if (token.isComment()) {
591     applyCodeTag(m_textBuffer, token, m_commentTag);
592     } else if (token.isPreprocessor()) {
593     applyCodeTag(m_textBuffer, token, m_preprocTag);
594     } else if (token.isNewLine()) {
595     }
596     }
597     }
598    
599 schoenebeck 2890 void ScriptEditor::updateParserIssuesByVM() {
600     GetScriptVM();
601     const std::string s = m_textBuffer->get_text();
602     LinuxSampler::VMParserContext* parserContext = m_vm->loadScript(s);
603 schoenebeck 2896 m_issues = parserContext->issues();
604 schoenebeck 2899 m_errors = parserContext->errors();
605     m_warnings = parserContext->warnings();
606 schoenebeck 3286 m_preprocComments = parserContext->preprocessorComments();
607 schoenebeck 2890
608 schoenebeck 2975 if (!s.empty()) {
609     for (int i = 0; i < m_issues.size(); ++i) {
610     const LinuxSampler::ParserIssue& issue = m_issues[i];
611 schoenebeck 2890
612 schoenebeck 2975 if (issue.isErr()) {
613     applyCodeTag(m_textBuffer, issue, m_errorTag);
614     } else if (issue.isWrn()) {
615     applyCodeTag(m_textBuffer, issue, m_warningTag);
616     }
617 schoenebeck 2890 }
618     }
619    
620 schoenebeck 3286 for (int i = 0; i < m_preprocComments.size(); ++i) {
621     applyPreprocessorComment(m_textBuffer, m_preprocComments[i],
622     m_preprocCommentTag);
623     }
624    
625 schoenebeck 2897 delete parserContext;
626     }
627 schoenebeck 2896
628 schoenebeck 2897 void ScriptEditor::updateIssueTooltip(GdkEventMotion* e) {
629     int x, y;
630     m_textView.window_to_buffer_coords(Gtk::TEXT_WINDOW_TEXT, int(e->x), int(e->y), x, y);
631 schoenebeck 2896
632 schoenebeck 2897 Gtk::TextBuffer::iterator it;
633     m_textView.get_iter_at_location(it, x, y);
634    
635     const int line = it.get_line();
636     const int column = it.get_line_offset();
637    
638     //printf("mouse at l%d c%d\n", line, column);
639    
640     for (int i = 0; i < m_issues.size(); ++i) {
641     const LinuxSampler::ParserIssue& issue = m_issues[i];
642     const int firstLine = issue.firstLine - 1;
643     const int firstColumn = issue.firstColumn - 1;
644     const int lastLine = issue.lastLine - 1;
645     const int lastColumn = issue.lastColumn - 1;
646     if (firstLine <= line && line <= lastLine &&
647     (firstLine != line || firstColumn <= column) &&
648     (lastLine != line || lastColumn >= column))
649     {
650     m_textView.set_tooltip_markup(
651     (issue.isErr() ? "<span foreground=\"#ff9393\">ERROR:</span> " : "<span foreground=\"#c4950c\">Warning:</span> ") +
652     issue.txt
653     );
654     return;
655 schoenebeck 2896 }
656     }
657    
658 schoenebeck 3286 for (int i = 0; i < m_preprocComments.size(); ++i) {
659     const LinuxSampler::CodeBlock& block = m_preprocComments[i];
660     const int firstLine = block.firstLine - 1;
661     const int firstColumn = block.firstColumn - 1;
662     const int lastLine = block.lastLine - 1;
663     const int lastColumn = block.lastColumn - 1;
664     if (firstLine <= line && line <= lastLine &&
665     (firstLine != line || firstColumn <= column) &&
666     (lastLine != line || lastColumn >= column))
667     {
668     m_textView.set_tooltip_markup(
669     "Code block filtered out by preceding <span foreground=\"" PREPROC_TOKEN_COLOR "\">preprocessor</span> statement."
670     );
671     return;
672     }
673     }
674    
675 schoenebeck 2897 m_textView.set_tooltip_markup("");
676 schoenebeck 2890 }
677    
678 schoenebeck 2899 static std::string warningsCountTxt(const std::vector<LinuxSampler::ParserIssue> warnings) {
679     std::string txt = "<span foreground=\"#c4950c\">" + ToString(warnings.size());
680     txt += (warnings.size() == 1) ? " Warning" : " Warnings";
681     txt += "</span>";
682     return txt;
683     }
684    
685     static std::string errorsCountTxt(const std::vector<LinuxSampler::ParserIssue> errors) {
686     std::string txt = "<span foreground=\"#c40c0c\">" + ToString(errors.size());
687     txt += (errors.size() == 1) ? " Error" : " Errors";
688     txt += "</span>";
689     return txt;
690     }
691    
692     void ScriptEditor::updateStatusBar() {
693     // update status text
694     std::string txt;
695     if (m_issues.empty()) {
696     txt = "No issues with this script.";
697     } else {
698     const char* txtWontLoad = ". Sampler won't load instruments using this script!";
699     txt = "There ";
700     txt += (m_errors.size() <= 1 && m_warnings.size() <= 1) ? "is " : "are ";
701     if (m_errors.empty()) {
702     txt += warningsCountTxt(m_warnings) + ". Script will load, but might not behave as expected!";
703     } else if (m_warnings.empty()) {
704     txt += errorsCountTxt(m_errors) + txtWontLoad;
705     } else {
706     txt += errorsCountTxt(m_errors) + " and " +
707     warningsCountTxt(m_warnings) + txtWontLoad;
708     }
709     }
710     m_statusLabel.set_markup(txt);
711    
712     // update status icon
713     m_statusImage.set(
714     m_issues.empty() ? m_successIcon : !m_errors.empty() ? m_errorIcon : m_warningIcon
715     );
716     }
717    
718 schoenebeck 2886 #endif // USE_LS_SCRIPTVM
719    
720 schoenebeck 2604 void ScriptEditor::onTextErased(const Gtk::TextBuffer::iterator& itStart, const Gtk::TextBuffer::iterator& itEnd) {
721 schoenebeck 2610 //printf("erased\n");
722 schoenebeck 2886 #if USE_LS_SCRIPTVM
723 schoenebeck 2890 m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
724 schoenebeck 2886 updateSyntaxHighlightingByVM();
725 schoenebeck 2890 updateParserIssuesByVM();
726 schoenebeck 2899 updateStatusBar();
727 schoenebeck 2886 #else
728 schoenebeck 2604 Gtk::TextBuffer::iterator itStart2 = itStart;
729     if (itStart2.inside_word() || itStart2.ends_word())
730     itStart2.backward_word_start();
731    
732     Gtk::TextBuffer::iterator itEnd2 = itEnd;
733     if (itEnd2.inside_word()) itEnd2.forward_word_end();
734    
735     m_textBuffer->remove_all_tags(itStart2, itEnd2);
736 schoenebeck 2886 #endif // USE_LS_SCRIPTVM
737 schoenebeck 3310 updateLineNumbers();
738 schoenebeck 2604 }
739    
740 schoenebeck 2897 bool ScriptEditor::on_motion_notify_event(GdkEventMotion* e) {
741     #if USE_LS_SCRIPTVM
742     //TODO: event throttling would be a good idea here
743     updateIssueTooltip(e);
744     #endif
745 schoenebeck 3364 #if GTKMM_MAJOR_VERSION < 3 || (GTKMM_MAJOR_VERSION == 3 && GTKMM_MINOR_VERSION <= 22)
746 schoenebeck 2897 return ManagedWindow::on_motion_notify_event(e);
747 schoenebeck 3364 #else
748     Gdk::EventMotion em = Glib::wrap(e, true);
749     return ManagedWindow::on_motion_notify_event(em);
750     #endif
751 schoenebeck 2897 }
752    
753 schoenebeck 2956 void ScriptEditor::onMenuChangeFontSize() {
754     //TODO: for GTKMM >= 3.2 class Gtk::FontChooser could be used instead
755     Gtk::Dialog dialog(_("Font Size"), true /*modal*/);
756 schoenebeck 3364 HBox hbox;
757 schoenebeck 2956 hbox.set_spacing(6);
758    
759     Gtk::Label label(_("Editor's Font Size:"), Gtk::ALIGN_START);
760     hbox.pack_start(label, Gtk::PACK_SHRINK);
761    
762     Gtk::SpinButton spinButton;
763     spinButton.set_range(4, 80);
764     spinButton.set_increments(1, 10);
765     spinButton.set_value(currentFontSize());
766     hbox.pack_start(spinButton);
767    
768 schoenebeck 3364 #if USE_GTKMM_BOX
769     dialog.get_content_area()->pack_start(hbox);
770     #else
771 schoenebeck 2956 dialog.get_vbox()->pack_start(hbox);
772 schoenebeck 3364 #endif
773 schoenebeck 2956 dialog.add_button(_("_OK"), 0);
774     dialog.add_button(_("_Cancel"), 1);
775    
776 schoenebeck 3364 #if HAS_GTKMM_SHOW_ALL_CHILDREN
777 schoenebeck 2956 dialog.show_all_children();
778 schoenebeck 3364 #endif
779 schoenebeck 2956
780     if (!dialog.run()) { // OK selected ...
781     const int newFontSize = spinButton.get_value_as_int();
782     if (newFontSize >= 4)
783     setFontSize(newFontSize, true);
784     }
785     }
786    
787 schoenebeck 3364 #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
788     bool ScriptEditor::onWindowDelete(Gdk::Event& e) {
789     return onWindowDeleteP(NULL);
790     }
791     #endif
792    
793     bool ScriptEditor::onWindowDeleteP(GdkEventAny* /*e*/) {
794 schoenebeck 2898 //printf("onWindowDelete\n");
795    
796     if (!isModified()) return false; // propagate event further (which will close this window)
797    
798     gchar* msg = g_strdup_printf(_("Apply changes to instrument script \"%s\" before closing?"),
799     m_script->Name.c_str());
800     Gtk::MessageDialog dialog(*this, msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
801     g_free(msg);
802     dialog.set_secondary_text(_("If you close without applying, your changes will be lost."));
803     dialog.add_button(_("Close _Without Applying"), Gtk::RESPONSE_NO);
804     dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL);
805     dialog.add_button(_("_Apply"), Gtk::RESPONSE_YES);
806     dialog.set_default_response(Gtk::RESPONSE_YES);
807     int response = dialog.run();
808     dialog.hide();
809    
810     // user decided to close script editor without saving
811     if (response == Gtk::RESPONSE_NO)
812     return false; // propagate event further (which will close this window)
813    
814     // user cancelled dialog, thus don't close script editor
815     if (response == Gtk::RESPONSE_CANCEL) {
816     show();
817     return true; // drop event (prevents closing this window)
818     }
819    
820     // user wants to apply the changes, afterwards close window
821     if (response == Gtk::RESPONSE_YES) {
822     onButtonApply();
823     return false; // propagate event further (which will close this window)
824     }
825    
826     // should never ever make it to this point actually
827     return false;
828     }
829    
830     bool ScriptEditor::isModified() const {
831     return m_textBuffer->get_modified();
832     }
833    
834 schoenebeck 2604 void ScriptEditor::onModifiedChanged() {
835 schoenebeck 2898 m_applyButton.set_sensitive(isModified());
836 schoenebeck 2899 #if USE_LS_SCRIPTVM
837     updateStatusBar();
838     #endif
839 schoenebeck 2604 }
840    
841     void ScriptEditor::onButtonCancel() {
842 schoenebeck 3364 bool dropEvent = onWindowDeleteP(NULL);
843 schoenebeck 2898 if (dropEvent) return;
844 schoenebeck 2604 hide();
845     }
846    
847     void ScriptEditor::onButtonApply() {
848 schoenebeck 2903 signal_script_to_be_changed.emit(m_script);
849 schoenebeck 2604 m_script->SetScriptAsText(m_textBuffer->get_text());
850 schoenebeck 2903 signal_script_changed.emit(m_script);
851 schoenebeck 2604 m_textBuffer->set_modified(false);
852     }
853    
854     void ScriptEditor::onWindowHide() {
855     delete this; // this is the end, my friend
856     }

  ViewVC Help
Powered by ViewVC