/[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 2899 - (hide annotations) (download)
Mon May 2 14:36:40 2016 UTC (7 years, 10 months ago) by schoenebeck
File size: 16960 byte(s)
* Script Editor: Added status bar at the bottom of the script editor
  window showing the amount (if any) of errors and warnings.
* Bumped version (1.0.0.svn11).

1 schoenebeck 2604 /*
2 schoenebeck 2886 Copyright (c) 2014-2016 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    
11 schoenebeck 2886 #if !USE_LS_SCRIPTVM
12    
13 schoenebeck 2604 static const std::string _keywords[] = {
14     "on", "end", "declare", "while", "if", "or", "and", "not", "else", "case",
15     "select", "to", "const", "polyphonic", "mod"
16     };
17     static int _keywordsSz = sizeof(_keywords) / sizeof(std::string);
18    
19     static const std::string _eventNames[] = {
20     "init", "note", "release", "controller"
21     };
22     static int _eventNamesSz = sizeof(_eventNames) / sizeof(std::string);
23    
24     static bool isKeyword(const Glib::ustring& s) {
25     for (int i = 0; i < _keywordsSz; ++i)
26     if (_keywords[i] == s) return true;
27     return false;
28     }
29    
30     static bool isEvent(const Glib::ustring& s) {
31     for (int i = 0; i < _eventNamesSz; ++i)
32     if (_eventNames[i] == s) return true;
33     return false;
34     }
35    
36 schoenebeck 2886 #endif // !USE_LS_SCRIPTVM
37    
38 schoenebeck 2899 static Glib::RefPtr<Gdk::Pixbuf> createIcon(std::string name, const Glib::RefPtr<Gdk::Screen>& screen) {
39     const int targetH = 16;
40     Glib::RefPtr<Gtk::IconTheme> theme = Gtk::IconTheme::get_for_screen(screen);
41     int w = 0;
42     int h = 0; // ignored
43     Gtk::IconSize::lookup(Gtk::ICON_SIZE_SMALL_TOOLBAR, w, h);
44     Glib::RefPtr<Gdk::Pixbuf> pixbuf = theme->load_icon(name, w, Gtk::ICON_LOOKUP_GENERIC_FALLBACK);
45     if (pixbuf->get_height() != targetH) {
46     pixbuf = pixbuf->scale_simple(targetH, targetH, Gdk::INTERP_BILINEAR);
47     }
48     return pixbuf;
49     }
50    
51 schoenebeck 2604 ScriptEditor::ScriptEditor() :
52 schoenebeck 2899 m_statusLabel("", Gtk::ALIGN_START),
53 persson 2845 m_applyButton(_("_Apply"), true),
54     m_cancelButton(_("_Cancel"), true)
55 schoenebeck 2604 {
56     m_script = NULL;
57 schoenebeck 2896 #if USE_LS_SCRIPTVM
58 schoenebeck 2886 m_vm = NULL;
59 schoenebeck 2896 #endif
60 schoenebeck 2604
61 schoenebeck 2899 m_errorIcon = createIcon("dialog-error", get_screen());
62     m_warningIcon = createIcon("dialog-warning-symbolic", get_screen());
63     m_successIcon = createIcon("emblem-default", get_screen());
64    
65 schoenebeck 2604 add(m_vbox);
66    
67     m_tagTable = Gtk::TextBuffer::TagTable::create();
68 schoenebeck 2886
69 schoenebeck 2604 m_keywordTag = Gtk::TextBuffer::Tag::create();
70 schoenebeck 2887 m_keywordTag->property_foreground() = "#000000"; // black
71 schoenebeck 2604 m_keywordTag->property_weight() = PANGO_WEIGHT_BOLD;
72     m_tagTable->add(m_keywordTag);
73 schoenebeck 2886
74 schoenebeck 2604 m_eventTag = Gtk::TextBuffer::Tag::create();
75 schoenebeck 2887 m_eventTag->property_foreground() = "#07c0cf"; // cyan 1
76 schoenebeck 2610 m_eventTag->property_weight() = PANGO_WEIGHT_BOLD;
77 schoenebeck 2604 m_tagTable->add(m_eventTag);
78 schoenebeck 2886
79     m_variableTag = Gtk::TextBuffer::Tag::create();
80 schoenebeck 2887 m_variableTag->property_foreground() = "#790cc4"; // magenta
81 schoenebeck 2886 m_tagTable->add(m_variableTag);
82    
83     m_functionTag = Gtk::TextBuffer::Tag::create();
84 schoenebeck 2887 m_functionTag->property_foreground() = "#1ba1dd"; // cyan 2
85 schoenebeck 2886 m_tagTable->add(m_functionTag);
86    
87     m_numberTag = Gtk::TextBuffer::Tag::create();
88 schoenebeck 2887 m_numberTag->property_foreground() = "#c4950c"; // yellow
89 schoenebeck 2886 m_tagTable->add(m_numberTag);
90    
91     m_stringTag = Gtk::TextBuffer::Tag::create();
92 schoenebeck 2887 m_stringTag->property_foreground() = "#c40c0c"; // red
93 schoenebeck 2886 m_tagTable->add(m_stringTag);
94    
95     m_commentTag = Gtk::TextBuffer::Tag::create();
96 schoenebeck 2887 m_commentTag->property_foreground() = "#9c9c9c"; // gray
97 schoenebeck 2886 m_tagTable->add(m_commentTag);
98    
99     m_preprocTag = Gtk::TextBuffer::Tag::create();
100 schoenebeck 2887 m_preprocTag->property_foreground() = "#2f8a33"; // green
101 schoenebeck 2886 m_tagTable->add(m_preprocTag);
102    
103 schoenebeck 2890 m_errorTag = Gtk::TextBuffer::Tag::create();
104     m_errorTag->property_background() = "#ff9393"; // red
105     m_tagTable->add(m_errorTag);
106    
107     m_warningTag = Gtk::TextBuffer::Tag::create();
108     m_warningTag->property_background() = "#fffd7c"; // yellow
109     m_tagTable->add(m_warningTag);
110    
111 schoenebeck 2604 m_textBuffer = Gtk::TextBuffer::create(m_tagTable);
112     m_textView.set_buffer(m_textBuffer);
113     {
114     Pango::FontDescription fdesc;
115     fdesc.set_family("monospace");
116 schoenebeck 2664 #if defined(__APPLE__)
117     fdesc.set_size(12 * PANGO_SCALE);
118     #else
119 schoenebeck 2604 fdesc.set_size(10 * PANGO_SCALE);
120 schoenebeck 2664 #endif
121 schoenebeck 2604 #if GTKMM_MAJOR_VERSION < 3
122     m_textView.modify_font(fdesc);
123     #else
124     m_textView.override_font(fdesc);
125     #endif
126     }
127     m_scrolledWindow.add(m_textView);
128     m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
129     m_vbox.pack_start(m_scrolledWindow);
130    
131     m_buttonBox.set_layout(Gtk::BUTTONBOX_END);
132     m_buttonBox.pack_start(m_applyButton);
133     m_buttonBox.pack_start(m_cancelButton);
134     m_applyButton.set_can_default();
135     m_applyButton.set_sensitive(false);
136     m_applyButton.grab_focus();
137 schoenebeck 2899
138     m_statusImage.set_margin_left(6);
139     m_statusImage.set_margin_right(6);
140 schoenebeck 2604
141 schoenebeck 2899 m_statusHBox.pack_start(m_statusImage, Gtk::PACK_SHRINK);
142     m_statusHBox.pack_start(m_statusLabel);
143     m_statusHBox.show_all_children();
144    
145     m_footerHBox.pack_start(m_statusHBox);
146     m_footerHBox.pack_start(m_buttonBox, Gtk::PACK_SHRINK);
147    
148     m_vbox.pack_start(m_footerHBox, Gtk::PACK_SHRINK);
149    
150 schoenebeck 2604 m_applyButton.signal_clicked().connect(
151     sigc::mem_fun(*this, &ScriptEditor::onButtonApply)
152     );
153    
154     m_cancelButton.signal_clicked().connect(
155     sigc::mem_fun(*this, &ScriptEditor::onButtonCancel)
156     );
157    
158     m_textBuffer->signal_insert().connect(
159     sigc::mem_fun(*this, &ScriptEditor::onTextInserted)
160     );
161    
162     m_textBuffer->signal_erase().connect(
163     sigc::mem_fun(*this, &ScriptEditor::onTextErased)
164     );
165    
166     m_textBuffer->signal_modified_changed().connect(
167     sigc::mem_fun(*this, &ScriptEditor::onModifiedChanged)
168     );
169    
170     signal_hide().connect(
171     sigc::mem_fun(*this, &ScriptEditor::onWindowHide)
172     );
173    
174 schoenebeck 2898 signal_delete_event().connect(
175     sigc::mem_fun(*this, &ScriptEditor::onWindowDelete)
176     );
177    
178 schoenebeck 2604 show_all_children();
179    
180     resize(460,300);
181     }
182    
183     ScriptEditor::~ScriptEditor() {
184     printf("ScriptEditor destruct\n");
185 schoenebeck 2896 #if USE_LS_SCRIPTVM
186 schoenebeck 2886 if (m_vm) delete m_vm;
187 schoenebeck 2896 #endif
188 schoenebeck 2604 }
189    
190     void ScriptEditor::setScript(gig::Script* script) {
191     m_script = script;
192     if (!script) {
193     set_title(_("No Script"));
194     return;
195     }
196    
197     set_title(std::string(_("Instrument Script")) + " - \"" + script->Name + "\"");
198    
199     std::string txt = script->GetScriptAsText();
200     //printf("text : '%s'\n", txt.c_str());
201     m_textBuffer->set_text(txt);
202     m_textBuffer->set_modified(false);
203     }
204    
205     void ScriptEditor::onTextInserted(const Gtk::TextBuffer::iterator& itEnd, const Glib::ustring& txt, int length) {
206 schoenebeck 2897 //printf("onTextInserted()\n");
207 schoenebeck 2886 #if USE_LS_SCRIPTVM
208 schoenebeck 2890 m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
209 schoenebeck 2886 updateSyntaxHighlightingByVM();
210 schoenebeck 2890 updateParserIssuesByVM();
211 schoenebeck 2899 updateStatusBar();
212 schoenebeck 2886 #else
213 schoenebeck 2610 //printf("inserted %d\n", length);
214 schoenebeck 2604 Gtk::TextBuffer::iterator itStart = itEnd;
215     itStart.backward_chars(length);
216    
217     Gtk::TextBuffer::iterator it = itStart;
218     it.backward_word_start();
219    
220     bool eofReached = false;
221     while (it <= itEnd) {
222     Gtk::TextBuffer::iterator itWordStart = it;
223     if (!it.forward_word_end()) {
224     eofReached = true;
225     it = itEnd;
226     }
227    
228     Glib::ustring s = m_textBuffer->get_text(itWordStart, it, false);
229 schoenebeck 2610 //printf("{%s}\n", s.c_str());
230 schoenebeck 2604 if (isKeyword(s))
231     m_textBuffer->apply_tag(m_keywordTag, itWordStart, it);
232     else if (isEvent(s)) {
233     // check if previous word is "on"
234     Gtk::TextBuffer::iterator itPreviousWordStart = itWordStart;
235     if (itPreviousWordStart.backward_word_start()) {
236     Gtk::TextBuffer::iterator itPreviousWordEnd = itPreviousWordStart;
237     itPreviousWordEnd.forward_word_end();
238     if (m_textBuffer->get_text(itPreviousWordStart, itPreviousWordEnd, false) == "on") {
239     m_textBuffer->apply_tag(m_eventTag, itWordStart, it);
240     }
241     }
242     }
243    
244     if (eofReached) break;
245    
246     while (!it.inside_word())
247     if (!it.forward_char())
248     goto EOF_REACHED;
249     }
250    
251     EOF_REACHED:
252     ;
253 schoenebeck 2886
254     #endif // USE_LS_SCRIPTVM
255 schoenebeck 2604 }
256    
257 schoenebeck 2886 #if USE_LS_SCRIPTVM
258    
259 schoenebeck 2890 LinuxSampler::ScriptVM* ScriptEditor::GetScriptVM() {
260     if (!m_vm) m_vm = LinuxSampler::ScriptVMFactory::Create("gig");
261     return m_vm;
262     }
263    
264 schoenebeck 2897 static void getIteratorsForIssue(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::ParserIssue& issue, Gtk::TextBuffer::iterator& start, Gtk::TextBuffer::iterator& end) {
265     start = txtbuf->get_iter_at_line_index(issue.firstLine - 1, issue.firstColumn - 1);
266     end = start;
267     end.forward_lines(issue.lastLine - issue.firstLine);
268     end.forward_chars(
269     (issue.lastLine != issue.firstLine)
270     ? issue.lastColumn - 1
271     : issue.lastColumn - issue.firstColumn + 1
272     );
273     }
274    
275 schoenebeck 2886 static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::VMSourceToken& token, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
276     Gtk::TextBuffer::iterator itStart =
277     txtbuf->get_iter_at_line_index(token.firstLine(), token.firstColumn());
278     Gtk::TextBuffer::iterator itEnd = itStart;
279     const int length = token.text().length();
280     itEnd.forward_chars(length);
281     txtbuf->apply_tag(tag, itStart, itEnd);
282     }
283    
284 schoenebeck 2890 static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::ParserIssue& issue, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
285 schoenebeck 2897 Gtk::TextBuffer::iterator itStart, itEnd;
286     getIteratorsForIssue(txtbuf, issue, itStart, itEnd);
287 schoenebeck 2890 txtbuf->apply_tag(tag, itStart, itEnd);
288     }
289    
290 schoenebeck 2886 void ScriptEditor::updateSyntaxHighlightingByVM() {
291 schoenebeck 2890 GetScriptVM();
292 schoenebeck 2886 const std::string s = m_textBuffer->get_text();
293     std::vector<LinuxSampler::VMSourceToken> tokens = m_vm->syntaxHighlighting(s);
294    
295     for (int i = 0; i < tokens.size(); ++i) {
296     const LinuxSampler::VMSourceToken& token = tokens[i];
297    
298     if (token.isKeyword()) {
299     applyCodeTag(m_textBuffer, token, m_keywordTag);
300     } else if (token.isVariableName()) {
301     applyCodeTag(m_textBuffer, token, m_variableTag);
302     } else if (token.isIdentifier()) {
303     if (token.isEventHandlerName()) {
304     applyCodeTag(m_textBuffer, token, m_eventTag);
305     } else { // a function ...
306     applyCodeTag(m_textBuffer, token, m_functionTag);
307     }
308     } else if (token.isNumberLiteral()) {
309     applyCodeTag(m_textBuffer, token, m_numberTag);
310     } else if (token.isStringLiteral()) {
311     applyCodeTag(m_textBuffer, token, m_stringTag);
312     } else if (token.isComment()) {
313     applyCodeTag(m_textBuffer, token, m_commentTag);
314     } else if (token.isPreprocessor()) {
315     applyCodeTag(m_textBuffer, token, m_preprocTag);
316     } else if (token.isNewLine()) {
317     }
318     }
319     }
320    
321 schoenebeck 2890 void ScriptEditor::updateParserIssuesByVM() {
322     GetScriptVM();
323     const std::string s = m_textBuffer->get_text();
324     LinuxSampler::VMParserContext* parserContext = m_vm->loadScript(s);
325 schoenebeck 2896 m_issues = parserContext->issues();
326 schoenebeck 2899 m_errors = parserContext->errors();
327     m_warnings = parserContext->warnings();
328 schoenebeck 2890
329 schoenebeck 2896 for (int i = 0; i < m_issues.size(); ++i) {
330     const LinuxSampler::ParserIssue& issue = m_issues[i];
331 schoenebeck 2890
332     if (issue.isErr()) {
333     applyCodeTag(m_textBuffer, issue, m_errorTag);
334     } else if (issue.isWrn()) {
335     applyCodeTag(m_textBuffer, issue, m_warningTag);
336     }
337     }
338    
339 schoenebeck 2897 delete parserContext;
340     }
341 schoenebeck 2896
342 schoenebeck 2897 void ScriptEditor::updateIssueTooltip(GdkEventMotion* e) {
343     int x, y;
344     m_textView.window_to_buffer_coords(Gtk::TEXT_WINDOW_TEXT, int(e->x), int(e->y), x, y);
345 schoenebeck 2896
346 schoenebeck 2897 Gtk::TextBuffer::iterator it;
347     m_textView.get_iter_at_location(it, x, y);
348    
349     const int line = it.get_line();
350     const int column = it.get_line_offset();
351    
352     //printf("mouse at l%d c%d\n", line, column);
353    
354     for (int i = 0; i < m_issues.size(); ++i) {
355     const LinuxSampler::ParserIssue& issue = m_issues[i];
356     const int firstLine = issue.firstLine - 1;
357     const int firstColumn = issue.firstColumn - 1;
358     const int lastLine = issue.lastLine - 1;
359     const int lastColumn = issue.lastColumn - 1;
360     if (firstLine <= line && line <= lastLine &&
361     (firstLine != line || firstColumn <= column) &&
362     (lastLine != line || lastColumn >= column))
363     {
364     m_textView.set_tooltip_markup(
365     (issue.isErr() ? "<span foreground=\"#ff9393\">ERROR:</span> " : "<span foreground=\"#c4950c\">Warning:</span> ") +
366     issue.txt
367     );
368     return;
369 schoenebeck 2896 }
370     }
371    
372 schoenebeck 2897 m_textView.set_tooltip_markup("");
373 schoenebeck 2890 }
374    
375 schoenebeck 2899 static std::string warningsCountTxt(const std::vector<LinuxSampler::ParserIssue> warnings) {
376     std::string txt = "<span foreground=\"#c4950c\">" + ToString(warnings.size());
377     txt += (warnings.size() == 1) ? " Warning" : " Warnings";
378     txt += "</span>";
379     return txt;
380     }
381    
382     static std::string errorsCountTxt(const std::vector<LinuxSampler::ParserIssue> errors) {
383     std::string txt = "<span foreground=\"#c40c0c\">" + ToString(errors.size());
384     txt += (errors.size() == 1) ? " Error" : " Errors";
385     txt += "</span>";
386     return txt;
387     }
388    
389     void ScriptEditor::updateStatusBar() {
390     // update status text
391     std::string txt;
392     if (m_issues.empty()) {
393     txt = "No issues with this script.";
394     } else {
395     const char* txtWontLoad = ". Sampler won't load instruments using this script!";
396     txt = "There ";
397     txt += (m_errors.size() <= 1 && m_warnings.size() <= 1) ? "is " : "are ";
398     if (m_errors.empty()) {
399     txt += warningsCountTxt(m_warnings) + ". Script will load, but might not behave as expected!";
400     } else if (m_warnings.empty()) {
401     txt += errorsCountTxt(m_errors) + txtWontLoad;
402     } else {
403     txt += errorsCountTxt(m_errors) + " and " +
404     warningsCountTxt(m_warnings) + txtWontLoad;
405     }
406     }
407     m_statusLabel.set_markup(txt);
408    
409     // update status icon
410     m_statusImage.set(
411     m_issues.empty() ? m_successIcon : !m_errors.empty() ? m_errorIcon : m_warningIcon
412     );
413     }
414    
415 schoenebeck 2886 #endif // USE_LS_SCRIPTVM
416    
417 schoenebeck 2604 void ScriptEditor::onTextErased(const Gtk::TextBuffer::iterator& itStart, const Gtk::TextBuffer::iterator& itEnd) {
418 schoenebeck 2610 //printf("erased\n");
419 schoenebeck 2886 #if USE_LS_SCRIPTVM
420 schoenebeck 2890 m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
421 schoenebeck 2886 updateSyntaxHighlightingByVM();
422 schoenebeck 2890 updateParserIssuesByVM();
423 schoenebeck 2899 updateStatusBar();
424 schoenebeck 2886 #else
425 schoenebeck 2604 Gtk::TextBuffer::iterator itStart2 = itStart;
426     if (itStart2.inside_word() || itStart2.ends_word())
427     itStart2.backward_word_start();
428    
429     Gtk::TextBuffer::iterator itEnd2 = itEnd;
430     if (itEnd2.inside_word()) itEnd2.forward_word_end();
431    
432     m_textBuffer->remove_all_tags(itStart2, itEnd2);
433 schoenebeck 2886 #endif // USE_LS_SCRIPTVM
434 schoenebeck 2604 }
435    
436 schoenebeck 2897 bool ScriptEditor::on_motion_notify_event(GdkEventMotion* e) {
437     #if USE_LS_SCRIPTVM
438     //TODO: event throttling would be a good idea here
439     updateIssueTooltip(e);
440     #endif
441     return ManagedWindow::on_motion_notify_event(e);
442     }
443    
444 schoenebeck 2898 bool ScriptEditor::onWindowDelete(GdkEventAny* e) {
445     //printf("onWindowDelete\n");
446    
447     if (!isModified()) return false; // propagate event further (which will close this window)
448    
449     gchar* msg = g_strdup_printf(_("Apply changes to instrument script \"%s\" before closing?"),
450     m_script->Name.c_str());
451     Gtk::MessageDialog dialog(*this, msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
452     g_free(msg);
453     dialog.set_secondary_text(_("If you close without applying, your changes will be lost."));
454     dialog.add_button(_("Close _Without Applying"), Gtk::RESPONSE_NO);
455     dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL);
456     dialog.add_button(_("_Apply"), Gtk::RESPONSE_YES);
457     dialog.set_default_response(Gtk::RESPONSE_YES);
458     int response = dialog.run();
459     dialog.hide();
460    
461     // user decided to close script editor without saving
462     if (response == Gtk::RESPONSE_NO)
463     return false; // propagate event further (which will close this window)
464    
465     // user cancelled dialog, thus don't close script editor
466     if (response == Gtk::RESPONSE_CANCEL) {
467     show();
468     return true; // drop event (prevents closing this window)
469     }
470    
471     // user wants to apply the changes, afterwards close window
472     if (response == Gtk::RESPONSE_YES) {
473     onButtonApply();
474     return false; // propagate event further (which will close this window)
475     }
476    
477     // should never ever make it to this point actually
478     return false;
479     }
480    
481     bool ScriptEditor::isModified() const {
482     return m_textBuffer->get_modified();
483     }
484    
485 schoenebeck 2604 void ScriptEditor::onModifiedChanged() {
486 schoenebeck 2898 m_applyButton.set_sensitive(isModified());
487 schoenebeck 2899 #if USE_LS_SCRIPTVM
488     updateStatusBar();
489     #endif
490 schoenebeck 2604 }
491    
492     void ScriptEditor::onButtonCancel() {
493 schoenebeck 2898 bool dropEvent = onWindowDelete(NULL);
494     if (dropEvent) return;
495 schoenebeck 2604 hide();
496     }
497    
498     void ScriptEditor::onButtonApply() {
499     m_script->SetScriptAsText(m_textBuffer->get_text());
500     m_textBuffer->set_modified(false);
501     }
502    
503     void ScriptEditor::onWindowHide() {
504     delete this; // this is the end, my friend
505     }

  ViewVC Help
Powered by ViewVC