/[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 2898 - (hide annotations) (download)
Sun May 1 21:17:42 2016 UTC (7 years, 10 months ago) by schoenebeck
File size: 14148 byte(s)
* Script Editor: Show a question dialog to the user when the editor is
  to be closed and the script changes have not been applied yet.
* Bumped version (1.0.0.svn10).

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 2604 ScriptEditor::ScriptEditor() :
39 persson 2845 m_applyButton(_("_Apply"), true),
40     m_cancelButton(_("_Cancel"), true)
41 schoenebeck 2604 {
42     m_script = NULL;
43 schoenebeck 2896 #if USE_LS_SCRIPTVM
44 schoenebeck 2886 m_vm = NULL;
45 schoenebeck 2896 #endif
46 schoenebeck 2604
47     add(m_vbox);
48    
49     m_tagTable = Gtk::TextBuffer::TagTable::create();
50 schoenebeck 2886
51 schoenebeck 2604 m_keywordTag = Gtk::TextBuffer::Tag::create();
52 schoenebeck 2887 m_keywordTag->property_foreground() = "#000000"; // black
53 schoenebeck 2604 m_keywordTag->property_weight() = PANGO_WEIGHT_BOLD;
54     m_tagTable->add(m_keywordTag);
55 schoenebeck 2886
56 schoenebeck 2604 m_eventTag = Gtk::TextBuffer::Tag::create();
57 schoenebeck 2887 m_eventTag->property_foreground() = "#07c0cf"; // cyan 1
58 schoenebeck 2610 m_eventTag->property_weight() = PANGO_WEIGHT_BOLD;
59 schoenebeck 2604 m_tagTable->add(m_eventTag);
60 schoenebeck 2886
61     m_variableTag = Gtk::TextBuffer::Tag::create();
62 schoenebeck 2887 m_variableTag->property_foreground() = "#790cc4"; // magenta
63 schoenebeck 2886 m_tagTable->add(m_variableTag);
64    
65     m_functionTag = Gtk::TextBuffer::Tag::create();
66 schoenebeck 2887 m_functionTag->property_foreground() = "#1ba1dd"; // cyan 2
67 schoenebeck 2886 m_tagTable->add(m_functionTag);
68    
69     m_numberTag = Gtk::TextBuffer::Tag::create();
70 schoenebeck 2887 m_numberTag->property_foreground() = "#c4950c"; // yellow
71 schoenebeck 2886 m_tagTable->add(m_numberTag);
72    
73     m_stringTag = Gtk::TextBuffer::Tag::create();
74 schoenebeck 2887 m_stringTag->property_foreground() = "#c40c0c"; // red
75 schoenebeck 2886 m_tagTable->add(m_stringTag);
76    
77     m_commentTag = Gtk::TextBuffer::Tag::create();
78 schoenebeck 2887 m_commentTag->property_foreground() = "#9c9c9c"; // gray
79 schoenebeck 2886 m_tagTable->add(m_commentTag);
80    
81     m_preprocTag = Gtk::TextBuffer::Tag::create();
82 schoenebeck 2887 m_preprocTag->property_foreground() = "#2f8a33"; // green
83 schoenebeck 2886 m_tagTable->add(m_preprocTag);
84    
85 schoenebeck 2890 m_errorTag = Gtk::TextBuffer::Tag::create();
86     m_errorTag->property_background() = "#ff9393"; // red
87     m_tagTable->add(m_errorTag);
88    
89     m_warningTag = Gtk::TextBuffer::Tag::create();
90     m_warningTag->property_background() = "#fffd7c"; // yellow
91     m_tagTable->add(m_warningTag);
92    
93 schoenebeck 2604 m_textBuffer = Gtk::TextBuffer::create(m_tagTable);
94     m_textView.set_buffer(m_textBuffer);
95     {
96     Pango::FontDescription fdesc;
97     fdesc.set_family("monospace");
98 schoenebeck 2664 #if defined(__APPLE__)
99     fdesc.set_size(12 * PANGO_SCALE);
100     #else
101 schoenebeck 2604 fdesc.set_size(10 * PANGO_SCALE);
102 schoenebeck 2664 #endif
103 schoenebeck 2604 #if GTKMM_MAJOR_VERSION < 3
104     m_textView.modify_font(fdesc);
105     #else
106     m_textView.override_font(fdesc);
107     #endif
108     }
109     m_scrolledWindow.add(m_textView);
110     m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
111     m_vbox.pack_start(m_scrolledWindow);
112    
113     m_buttonBox.set_layout(Gtk::BUTTONBOX_END);
114     m_buttonBox.pack_start(m_applyButton);
115     m_buttonBox.pack_start(m_cancelButton);
116     m_applyButton.set_can_default();
117     m_applyButton.set_sensitive(false);
118     m_applyButton.grab_focus();
119     m_vbox.pack_start(m_buttonBox, Gtk::PACK_SHRINK);
120    
121     m_applyButton.signal_clicked().connect(
122     sigc::mem_fun(*this, &ScriptEditor::onButtonApply)
123     );
124    
125     m_cancelButton.signal_clicked().connect(
126     sigc::mem_fun(*this, &ScriptEditor::onButtonCancel)
127     );
128    
129     m_textBuffer->signal_insert().connect(
130     sigc::mem_fun(*this, &ScriptEditor::onTextInserted)
131     );
132    
133     m_textBuffer->signal_erase().connect(
134     sigc::mem_fun(*this, &ScriptEditor::onTextErased)
135     );
136    
137     m_textBuffer->signal_modified_changed().connect(
138     sigc::mem_fun(*this, &ScriptEditor::onModifiedChanged)
139     );
140    
141     signal_hide().connect(
142     sigc::mem_fun(*this, &ScriptEditor::onWindowHide)
143     );
144    
145 schoenebeck 2898 signal_delete_event().connect(
146     sigc::mem_fun(*this, &ScriptEditor::onWindowDelete)
147     );
148    
149 schoenebeck 2604 show_all_children();
150    
151     resize(460,300);
152     }
153    
154     ScriptEditor::~ScriptEditor() {
155     printf("ScriptEditor destruct\n");
156 schoenebeck 2896 #if USE_LS_SCRIPTVM
157 schoenebeck 2886 if (m_vm) delete m_vm;
158 schoenebeck 2896 #endif
159 schoenebeck 2604 }
160    
161     void ScriptEditor::setScript(gig::Script* script) {
162     m_script = script;
163     if (!script) {
164     set_title(_("No Script"));
165     return;
166     }
167    
168     set_title(std::string(_("Instrument Script")) + " - \"" + script->Name + "\"");
169    
170     std::string txt = script->GetScriptAsText();
171     //printf("text : '%s'\n", txt.c_str());
172     m_textBuffer->set_text(txt);
173     m_textBuffer->set_modified(false);
174     }
175    
176     void ScriptEditor::onTextInserted(const Gtk::TextBuffer::iterator& itEnd, const Glib::ustring& txt, int length) {
177 schoenebeck 2897 //printf("onTextInserted()\n");
178 schoenebeck 2886 #if USE_LS_SCRIPTVM
179 schoenebeck 2890 m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
180 schoenebeck 2886 updateSyntaxHighlightingByVM();
181 schoenebeck 2890 updateParserIssuesByVM();
182 schoenebeck 2886 #else
183 schoenebeck 2610 //printf("inserted %d\n", length);
184 schoenebeck 2604 Gtk::TextBuffer::iterator itStart = itEnd;
185     itStart.backward_chars(length);
186    
187     Gtk::TextBuffer::iterator it = itStart;
188     it.backward_word_start();
189    
190     bool eofReached = false;
191     while (it <= itEnd) {
192     Gtk::TextBuffer::iterator itWordStart = it;
193     if (!it.forward_word_end()) {
194     eofReached = true;
195     it = itEnd;
196     }
197    
198     Glib::ustring s = m_textBuffer->get_text(itWordStart, it, false);
199 schoenebeck 2610 //printf("{%s}\n", s.c_str());
200 schoenebeck 2604 if (isKeyword(s))
201     m_textBuffer->apply_tag(m_keywordTag, itWordStart, it);
202     else if (isEvent(s)) {
203     // check if previous word is "on"
204     Gtk::TextBuffer::iterator itPreviousWordStart = itWordStart;
205     if (itPreviousWordStart.backward_word_start()) {
206     Gtk::TextBuffer::iterator itPreviousWordEnd = itPreviousWordStart;
207     itPreviousWordEnd.forward_word_end();
208     if (m_textBuffer->get_text(itPreviousWordStart, itPreviousWordEnd, false) == "on") {
209     m_textBuffer->apply_tag(m_eventTag, itWordStart, it);
210     }
211     }
212     }
213    
214     if (eofReached) break;
215    
216     while (!it.inside_word())
217     if (!it.forward_char())
218     goto EOF_REACHED;
219     }
220    
221     EOF_REACHED:
222     ;
223 schoenebeck 2886
224     #endif // USE_LS_SCRIPTVM
225 schoenebeck 2604 }
226    
227 schoenebeck 2886 #if USE_LS_SCRIPTVM
228    
229 schoenebeck 2890 LinuxSampler::ScriptVM* ScriptEditor::GetScriptVM() {
230     if (!m_vm) m_vm = LinuxSampler::ScriptVMFactory::Create("gig");
231     return m_vm;
232     }
233    
234 schoenebeck 2897 static void getIteratorsForIssue(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::ParserIssue& issue, Gtk::TextBuffer::iterator& start, Gtk::TextBuffer::iterator& end) {
235     start = txtbuf->get_iter_at_line_index(issue.firstLine - 1, issue.firstColumn - 1);
236     end = start;
237     end.forward_lines(issue.lastLine - issue.firstLine);
238     end.forward_chars(
239     (issue.lastLine != issue.firstLine)
240     ? issue.lastColumn - 1
241     : issue.lastColumn - issue.firstColumn + 1
242     );
243     }
244    
245 schoenebeck 2886 static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::VMSourceToken& token, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
246     Gtk::TextBuffer::iterator itStart =
247     txtbuf->get_iter_at_line_index(token.firstLine(), token.firstColumn());
248     Gtk::TextBuffer::iterator itEnd = itStart;
249     const int length = token.text().length();
250     itEnd.forward_chars(length);
251     txtbuf->apply_tag(tag, itStart, itEnd);
252     }
253    
254 schoenebeck 2890 static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::ParserIssue& issue, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
255 schoenebeck 2897 Gtk::TextBuffer::iterator itStart, itEnd;
256     getIteratorsForIssue(txtbuf, issue, itStart, itEnd);
257 schoenebeck 2890 txtbuf->apply_tag(tag, itStart, itEnd);
258     }
259    
260 schoenebeck 2886 void ScriptEditor::updateSyntaxHighlightingByVM() {
261 schoenebeck 2890 GetScriptVM();
262 schoenebeck 2886 const std::string s = m_textBuffer->get_text();
263     std::vector<LinuxSampler::VMSourceToken> tokens = m_vm->syntaxHighlighting(s);
264    
265     for (int i = 0; i < tokens.size(); ++i) {
266     const LinuxSampler::VMSourceToken& token = tokens[i];
267    
268     if (token.isKeyword()) {
269     applyCodeTag(m_textBuffer, token, m_keywordTag);
270     } else if (token.isVariableName()) {
271     applyCodeTag(m_textBuffer, token, m_variableTag);
272     } else if (token.isIdentifier()) {
273     if (token.isEventHandlerName()) {
274     applyCodeTag(m_textBuffer, token, m_eventTag);
275     } else { // a function ...
276     applyCodeTag(m_textBuffer, token, m_functionTag);
277     }
278     } else if (token.isNumberLiteral()) {
279     applyCodeTag(m_textBuffer, token, m_numberTag);
280     } else if (token.isStringLiteral()) {
281     applyCodeTag(m_textBuffer, token, m_stringTag);
282     } else if (token.isComment()) {
283     applyCodeTag(m_textBuffer, token, m_commentTag);
284     } else if (token.isPreprocessor()) {
285     applyCodeTag(m_textBuffer, token, m_preprocTag);
286     } else if (token.isNewLine()) {
287     }
288     }
289     }
290    
291 schoenebeck 2890 void ScriptEditor::updateParserIssuesByVM() {
292     GetScriptVM();
293     const std::string s = m_textBuffer->get_text();
294     LinuxSampler::VMParserContext* parserContext = m_vm->loadScript(s);
295 schoenebeck 2896 m_issues = parserContext->issues();
296 schoenebeck 2890
297 schoenebeck 2896 for (int i = 0; i < m_issues.size(); ++i) {
298     const LinuxSampler::ParserIssue& issue = m_issues[i];
299 schoenebeck 2890
300     if (issue.isErr()) {
301     applyCodeTag(m_textBuffer, issue, m_errorTag);
302     } else if (issue.isWrn()) {
303     applyCodeTag(m_textBuffer, issue, m_warningTag);
304     }
305     }
306    
307 schoenebeck 2897 delete parserContext;
308     }
309 schoenebeck 2896
310 schoenebeck 2897 void ScriptEditor::updateIssueTooltip(GdkEventMotion* e) {
311     int x, y;
312     m_textView.window_to_buffer_coords(Gtk::TEXT_WINDOW_TEXT, int(e->x), int(e->y), x, y);
313 schoenebeck 2896
314 schoenebeck 2897 Gtk::TextBuffer::iterator it;
315     m_textView.get_iter_at_location(it, x, y);
316    
317     const int line = it.get_line();
318     const int column = it.get_line_offset();
319    
320     //printf("mouse at l%d c%d\n", line, column);
321    
322     for (int i = 0; i < m_issues.size(); ++i) {
323     const LinuxSampler::ParserIssue& issue = m_issues[i];
324     const int firstLine = issue.firstLine - 1;
325     const int firstColumn = issue.firstColumn - 1;
326     const int lastLine = issue.lastLine - 1;
327     const int lastColumn = issue.lastColumn - 1;
328     if (firstLine <= line && line <= lastLine &&
329     (firstLine != line || firstColumn <= column) &&
330     (lastLine != line || lastColumn >= column))
331     {
332     m_textView.set_tooltip_markup(
333     (issue.isErr() ? "<span foreground=\"#ff9393\">ERROR:</span> " : "<span foreground=\"#c4950c\">Warning:</span> ") +
334     issue.txt
335     );
336     return;
337 schoenebeck 2896 }
338     }
339    
340 schoenebeck 2897 m_textView.set_tooltip_markup("");
341 schoenebeck 2890 }
342    
343 schoenebeck 2886 #endif // USE_LS_SCRIPTVM
344    
345 schoenebeck 2604 void ScriptEditor::onTextErased(const Gtk::TextBuffer::iterator& itStart, const Gtk::TextBuffer::iterator& itEnd) {
346 schoenebeck 2610 //printf("erased\n");
347 schoenebeck 2886 #if USE_LS_SCRIPTVM
348 schoenebeck 2890 m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
349 schoenebeck 2886 updateSyntaxHighlightingByVM();
350 schoenebeck 2890 updateParserIssuesByVM();
351 schoenebeck 2886 #else
352 schoenebeck 2604 Gtk::TextBuffer::iterator itStart2 = itStart;
353     if (itStart2.inside_word() || itStart2.ends_word())
354     itStart2.backward_word_start();
355    
356     Gtk::TextBuffer::iterator itEnd2 = itEnd;
357     if (itEnd2.inside_word()) itEnd2.forward_word_end();
358    
359     m_textBuffer->remove_all_tags(itStart2, itEnd2);
360 schoenebeck 2886 #endif // USE_LS_SCRIPTVM
361 schoenebeck 2604 }
362    
363 schoenebeck 2897 bool ScriptEditor::on_motion_notify_event(GdkEventMotion* e) {
364     #if USE_LS_SCRIPTVM
365     //TODO: event throttling would be a good idea here
366     updateIssueTooltip(e);
367     #endif
368     return ManagedWindow::on_motion_notify_event(e);
369     }
370    
371 schoenebeck 2898 bool ScriptEditor::onWindowDelete(GdkEventAny* e) {
372     //printf("onWindowDelete\n");
373    
374     if (!isModified()) return false; // propagate event further (which will close this window)
375    
376     gchar* msg = g_strdup_printf(_("Apply changes to instrument script \"%s\" before closing?"),
377     m_script->Name.c_str());
378     Gtk::MessageDialog dialog(*this, msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
379     g_free(msg);
380     dialog.set_secondary_text(_("If you close without applying, your changes will be lost."));
381     dialog.add_button(_("Close _Without Applying"), Gtk::RESPONSE_NO);
382     dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL);
383     dialog.add_button(_("_Apply"), Gtk::RESPONSE_YES);
384     dialog.set_default_response(Gtk::RESPONSE_YES);
385     int response = dialog.run();
386     dialog.hide();
387    
388     // user decided to close script editor without saving
389     if (response == Gtk::RESPONSE_NO)
390     return false; // propagate event further (which will close this window)
391    
392     // user cancelled dialog, thus don't close script editor
393     if (response == Gtk::RESPONSE_CANCEL) {
394     show();
395     return true; // drop event (prevents closing this window)
396     }
397    
398     // user wants to apply the changes, afterwards close window
399     if (response == Gtk::RESPONSE_YES) {
400     onButtonApply();
401     return false; // propagate event further (which will close this window)
402     }
403    
404     // should never ever make it to this point actually
405     return false;
406     }
407    
408     bool ScriptEditor::isModified() const {
409     return m_textBuffer->get_modified();
410     }
411    
412 schoenebeck 2604 void ScriptEditor::onModifiedChanged() {
413 schoenebeck 2898 m_applyButton.set_sensitive(isModified());
414 schoenebeck 2604 }
415    
416     void ScriptEditor::onButtonCancel() {
417 schoenebeck 2898 bool dropEvent = onWindowDelete(NULL);
418     if (dropEvent) return;
419 schoenebeck 2604 hide();
420     }
421    
422     void ScriptEditor::onButtonApply() {
423     m_script->SetScriptAsText(m_textBuffer->get_text());
424     m_textBuffer->set_modified(false);
425     }
426    
427     void ScriptEditor::onWindowHide() {
428     delete this; // this is the end, my friend
429     }

  ViewVC Help
Powered by ViewVC