/[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 2896 - (hide annotations) (download)
Sun May 1 14:51:55 2016 UTC (7 years, 11 months ago) by schoenebeck
File size: 13341 byte(s)
* EXPERIMENTAL: Show error/warning icon at the respective script
  locations of instrument script editor.
* Bumped version (1.0.0.svn8).

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     m_ignoreEraseEvents = false;
47 schoenebeck 2604
48     add(m_vbox);
49    
50     m_tagTable = Gtk::TextBuffer::TagTable::create();
51 schoenebeck 2886
52 schoenebeck 2604 m_keywordTag = Gtk::TextBuffer::Tag::create();
53 schoenebeck 2887 m_keywordTag->property_foreground() = "#000000"; // black
54 schoenebeck 2604 m_keywordTag->property_weight() = PANGO_WEIGHT_BOLD;
55     m_tagTable->add(m_keywordTag);
56 schoenebeck 2886
57 schoenebeck 2604 m_eventTag = Gtk::TextBuffer::Tag::create();
58 schoenebeck 2887 m_eventTag->property_foreground() = "#07c0cf"; // cyan 1
59 schoenebeck 2610 m_eventTag->property_weight() = PANGO_WEIGHT_BOLD;
60 schoenebeck 2604 m_tagTable->add(m_eventTag);
61 schoenebeck 2886
62     m_variableTag = Gtk::TextBuffer::Tag::create();
63 schoenebeck 2887 m_variableTag->property_foreground() = "#790cc4"; // magenta
64 schoenebeck 2886 m_tagTable->add(m_variableTag);
65    
66     m_functionTag = Gtk::TextBuffer::Tag::create();
67 schoenebeck 2887 m_functionTag->property_foreground() = "#1ba1dd"; // cyan 2
68 schoenebeck 2886 m_tagTable->add(m_functionTag);
69    
70     m_numberTag = Gtk::TextBuffer::Tag::create();
71 schoenebeck 2887 m_numberTag->property_foreground() = "#c4950c"; // yellow
72 schoenebeck 2886 m_tagTable->add(m_numberTag);
73    
74     m_stringTag = Gtk::TextBuffer::Tag::create();
75 schoenebeck 2887 m_stringTag->property_foreground() = "#c40c0c"; // red
76 schoenebeck 2886 m_tagTable->add(m_stringTag);
77    
78     m_commentTag = Gtk::TextBuffer::Tag::create();
79 schoenebeck 2887 m_commentTag->property_foreground() = "#9c9c9c"; // gray
80 schoenebeck 2886 m_tagTable->add(m_commentTag);
81    
82     m_preprocTag = Gtk::TextBuffer::Tag::create();
83 schoenebeck 2887 m_preprocTag->property_foreground() = "#2f8a33"; // green
84 schoenebeck 2886 m_tagTable->add(m_preprocTag);
85    
86 schoenebeck 2890 m_errorTag = Gtk::TextBuffer::Tag::create();
87     m_errorTag->property_background() = "#ff9393"; // red
88     m_tagTable->add(m_errorTag);
89    
90     m_warningTag = Gtk::TextBuffer::Tag::create();
91     m_warningTag->property_background() = "#fffd7c"; // yellow
92     m_tagTable->add(m_warningTag);
93    
94 schoenebeck 2896 m_readOnlyTag = Gtk::TextBuffer::Tag::create();
95     m_readOnlyTag->property_editable() = false;
96     m_tagTable->add(m_readOnlyTag);
97    
98 schoenebeck 2604 m_textBuffer = Gtk::TextBuffer::create(m_tagTable);
99     m_textView.set_buffer(m_textBuffer);
100     {
101     Pango::FontDescription fdesc;
102     fdesc.set_family("monospace");
103 schoenebeck 2664 #if defined(__APPLE__)
104     fdesc.set_size(12 * PANGO_SCALE);
105     #else
106 schoenebeck 2604 fdesc.set_size(10 * PANGO_SCALE);
107 schoenebeck 2664 #endif
108 schoenebeck 2604 #if GTKMM_MAJOR_VERSION < 3
109     m_textView.modify_font(fdesc);
110     #else
111     m_textView.override_font(fdesc);
112     #endif
113     }
114     m_scrolledWindow.add(m_textView);
115     m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
116     m_vbox.pack_start(m_scrolledWindow);
117    
118     m_buttonBox.set_layout(Gtk::BUTTONBOX_END);
119     m_buttonBox.pack_start(m_applyButton);
120     m_buttonBox.pack_start(m_cancelButton);
121     m_applyButton.set_can_default();
122     m_applyButton.set_sensitive(false);
123     m_applyButton.grab_focus();
124     m_vbox.pack_start(m_buttonBox, Gtk::PACK_SHRINK);
125    
126     m_applyButton.signal_clicked().connect(
127     sigc::mem_fun(*this, &ScriptEditor::onButtonApply)
128     );
129    
130     m_cancelButton.signal_clicked().connect(
131     sigc::mem_fun(*this, &ScriptEditor::onButtonCancel)
132     );
133    
134     m_textBuffer->signal_insert().connect(
135     sigc::mem_fun(*this, &ScriptEditor::onTextInserted)
136     );
137    
138     m_textBuffer->signal_erase().connect(
139     sigc::mem_fun(*this, &ScriptEditor::onTextErased)
140     );
141    
142     m_textBuffer->signal_modified_changed().connect(
143     sigc::mem_fun(*this, &ScriptEditor::onModifiedChanged)
144     );
145    
146     signal_hide().connect(
147     sigc::mem_fun(*this, &ScriptEditor::onWindowHide)
148     );
149    
150     show_all_children();
151    
152     resize(460,300);
153     }
154    
155     ScriptEditor::~ScriptEditor() {
156     printf("ScriptEditor destruct\n");
157 schoenebeck 2896 #if USE_LS_SCRIPTVM
158 schoenebeck 2886 if (m_vm) delete m_vm;
159 schoenebeck 2896 #endif
160 schoenebeck 2604 }
161    
162     void ScriptEditor::setScript(gig::Script* script) {
163     m_script = script;
164     if (!script) {
165     set_title(_("No Script"));
166     return;
167     }
168    
169     set_title(std::string(_("Instrument Script")) + " - \"" + script->Name + "\"");
170    
171     std::string txt = script->GetScriptAsText();
172     //printf("text : '%s'\n", txt.c_str());
173     m_textBuffer->set_text(txt);
174     m_textBuffer->set_modified(false);
175     }
176    
177     void ScriptEditor::onTextInserted(const Gtk::TextBuffer::iterator& itEnd, const Glib::ustring& txt, int length) {
178 schoenebeck 2896 printf("onTextInserted()\n");
179     fflush(stdout);
180 schoenebeck 2886 #if USE_LS_SCRIPTVM
181 schoenebeck 2896 removeIssueAnchors();
182 schoenebeck 2890 m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
183 schoenebeck 2886 updateSyntaxHighlightingByVM();
184 schoenebeck 2890 updateParserIssuesByVM();
185 schoenebeck 2886 #else
186 schoenebeck 2610 //printf("inserted %d\n", length);
187 schoenebeck 2604 Gtk::TextBuffer::iterator itStart = itEnd;
188     itStart.backward_chars(length);
189    
190     Gtk::TextBuffer::iterator it = itStart;
191     it.backward_word_start();
192    
193     bool eofReached = false;
194     while (it <= itEnd) {
195     Gtk::TextBuffer::iterator itWordStart = it;
196     if (!it.forward_word_end()) {
197     eofReached = true;
198     it = itEnd;
199     }
200    
201     Glib::ustring s = m_textBuffer->get_text(itWordStart, it, false);
202 schoenebeck 2610 //printf("{%s}\n", s.c_str());
203 schoenebeck 2604 if (isKeyword(s))
204     m_textBuffer->apply_tag(m_keywordTag, itWordStart, it);
205     else if (isEvent(s)) {
206     // check if previous word is "on"
207     Gtk::TextBuffer::iterator itPreviousWordStart = itWordStart;
208     if (itPreviousWordStart.backward_word_start()) {
209     Gtk::TextBuffer::iterator itPreviousWordEnd = itPreviousWordStart;
210     itPreviousWordEnd.forward_word_end();
211     if (m_textBuffer->get_text(itPreviousWordStart, itPreviousWordEnd, false) == "on") {
212     m_textBuffer->apply_tag(m_eventTag, itWordStart, it);
213     }
214     }
215     }
216    
217     if (eofReached) break;
218    
219     while (!it.inside_word())
220     if (!it.forward_char())
221     goto EOF_REACHED;
222     }
223    
224     EOF_REACHED:
225     ;
226 schoenebeck 2886
227     #endif // USE_LS_SCRIPTVM
228 schoenebeck 2604 }
229    
230 schoenebeck 2886 #if USE_LS_SCRIPTVM
231    
232 schoenebeck 2890 LinuxSampler::ScriptVM* ScriptEditor::GetScriptVM() {
233     if (!m_vm) m_vm = LinuxSampler::ScriptVMFactory::Create("gig");
234     return m_vm;
235     }
236    
237 schoenebeck 2886 static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::VMSourceToken& token, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
238     Gtk::TextBuffer::iterator itStart =
239     txtbuf->get_iter_at_line_index(token.firstLine(), token.firstColumn());
240     Gtk::TextBuffer::iterator itEnd = itStart;
241     const int length = token.text().length();
242     itEnd.forward_chars(length);
243     txtbuf->apply_tag(tag, itStart, itEnd);
244     }
245    
246 schoenebeck 2890 static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::ParserIssue& issue, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
247     Gtk::TextBuffer::iterator itStart =
248     txtbuf->get_iter_at_line_index(issue.firstLine - 1, issue.firstColumn - 1);
249     Gtk::TextBuffer::iterator itEnd = itStart;
250     itEnd.forward_lines(issue.lastLine - issue.firstLine);
251     itEnd.forward_chars(
252     (issue.lastLine != issue.firstLine)
253     ? issue.lastColumn - 1
254     : issue.lastColumn - issue.firstColumn + 1
255     );
256     txtbuf->apply_tag(tag, itStart, itEnd);
257     }
258    
259 schoenebeck 2896 void ScriptEditor::removeIssueAnchors() {
260     m_ignoreEraseEvents = true; // avoid endless recursion
261    
262     for (int i = 0; i < m_issues.size(); ++i) {
263     const LinuxSampler::ParserIssue& issue = m_issues[i];
264     printf("erase anchor at l%d c%d\n", issue.firstLine - 1, issue.firstColumn - 1);
265     fflush(stdout);
266     Gtk::TextBuffer::iterator iter = m_textBuffer->get_iter_at_line_index(issue.firstLine - 1, issue.firstColumn - 1);
267     Gtk::TextBuffer::iterator iterEnd = iter;
268     iterEnd.forward_chars(1);
269     m_textBuffer->erase(iter, iterEnd);
270     }
271    
272     m_ignoreEraseEvents = false; // back to normal
273     }
274 schoenebeck 2890
275 schoenebeck 2886 void ScriptEditor::updateSyntaxHighlightingByVM() {
276 schoenebeck 2890 GetScriptVM();
277 schoenebeck 2886 const std::string s = m_textBuffer->get_text();
278     std::vector<LinuxSampler::VMSourceToken> tokens = m_vm->syntaxHighlighting(s);
279    
280     for (int i = 0; i < tokens.size(); ++i) {
281     const LinuxSampler::VMSourceToken& token = tokens[i];
282    
283     if (token.isKeyword()) {
284     applyCodeTag(m_textBuffer, token, m_keywordTag);
285     } else if (token.isVariableName()) {
286     applyCodeTag(m_textBuffer, token, m_variableTag);
287     } else if (token.isIdentifier()) {
288     if (token.isEventHandlerName()) {
289     applyCodeTag(m_textBuffer, token, m_eventTag);
290     } else { // a function ...
291     applyCodeTag(m_textBuffer, token, m_functionTag);
292     }
293     } else if (token.isNumberLiteral()) {
294     applyCodeTag(m_textBuffer, token, m_numberTag);
295     } else if (token.isStringLiteral()) {
296     applyCodeTag(m_textBuffer, token, m_stringTag);
297     } else if (token.isComment()) {
298     applyCodeTag(m_textBuffer, token, m_commentTag);
299     } else if (token.isPreprocessor()) {
300     applyCodeTag(m_textBuffer, token, m_preprocTag);
301     } else if (token.isNewLine()) {
302     }
303     }
304     }
305    
306 schoenebeck 2896 static Glib::RefPtr<Gdk::Pixbuf> createIcon(std::string name, const Glib::RefPtr<Gdk::Screen>& screen) {
307     const int targetH = 9;
308     Glib::RefPtr<Gtk::IconTheme> theme = Gtk::IconTheme::get_for_screen(screen);
309     int w = 0;
310     int h = 0; // ignored
311     Gtk::IconSize::lookup(Gtk::ICON_SIZE_SMALL_TOOLBAR, w, h);
312     Glib::RefPtr<Gdk::Pixbuf> pixbuf = theme->load_icon(name, w, Gtk::ICON_LOOKUP_GENERIC_FALLBACK);
313     if (pixbuf->get_height() != targetH) {
314     pixbuf = pixbuf->scale_simple(targetH, targetH, Gdk::INTERP_BILINEAR);
315     }
316     return pixbuf;
317     }
318    
319 schoenebeck 2890 void ScriptEditor::updateParserIssuesByVM() {
320     GetScriptVM();
321     const std::string s = m_textBuffer->get_text();
322     LinuxSampler::VMParserContext* parserContext = m_vm->loadScript(s);
323 schoenebeck 2896 m_issues = parserContext->issues();
324 schoenebeck 2890
325 schoenebeck 2896 for (int i = 0; i < m_issues.size(); ++i) {
326     const LinuxSampler::ParserIssue& issue = m_issues[i];
327 schoenebeck 2890
328     if (issue.isErr()) {
329     applyCodeTag(m_textBuffer, issue, m_errorTag);
330     } else if (issue.isWrn()) {
331     applyCodeTag(m_textBuffer, issue, m_warningTag);
332     }
333     }
334    
335 schoenebeck 2896 for (int i = m_issues.size() - 1; i >= 0; --i) {
336     const LinuxSampler::ParserIssue& issue = m_issues[i];
337    
338     if (issue.isErr() || issue.isWrn()) {
339     Glib::RefPtr<Gdk::Pixbuf> pixbuf = createIcon(issue.isErr() ? "dialog-error" : "dialog-warning-symbolic", get_screen());
340     Gtk::Image* image = Gtk::manage(new Gtk::Image(pixbuf));
341     image->show();
342     Gtk::TextBuffer::iterator iter =
343     m_textBuffer->get_iter_at_line_index(issue.firstLine - 1, issue.firstColumn - 1);
344     Glib::RefPtr<Gtk::TextChildAnchor> anchor = m_textBuffer->create_child_anchor(iter);
345     m_textView.add_child_at_anchor(*image, anchor);
346    
347     iter =
348     m_textBuffer->get_iter_at_line_index(issue.firstLine - 1, issue.firstColumn - 1);
349     Gtk::TextBuffer::iterator itEnd = iter;
350     itEnd.forward_char();
351    
352     // prevent that the user can erase the icon with backspace key
353     m_textBuffer->apply_tag(m_readOnlyTag, iter, itEnd);
354     }
355     }
356    
357 schoenebeck 2890 delete parserContext;
358     }
359    
360 schoenebeck 2886 #endif // USE_LS_SCRIPTVM
361    
362 schoenebeck 2604 void ScriptEditor::onTextErased(const Gtk::TextBuffer::iterator& itStart, const Gtk::TextBuffer::iterator& itEnd) {
363 schoenebeck 2610 //printf("erased\n");
364 schoenebeck 2896 if (m_ignoreEraseEvents) return;
365    
366 schoenebeck 2886 #if USE_LS_SCRIPTVM
367 schoenebeck 2896 removeIssueAnchors();
368 schoenebeck 2890 m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
369 schoenebeck 2886 updateSyntaxHighlightingByVM();
370 schoenebeck 2890 updateParserIssuesByVM();
371 schoenebeck 2886 #else
372 schoenebeck 2604 Gtk::TextBuffer::iterator itStart2 = itStart;
373     if (itStart2.inside_word() || itStart2.ends_word())
374     itStart2.backward_word_start();
375    
376     Gtk::TextBuffer::iterator itEnd2 = itEnd;
377     if (itEnd2.inside_word()) itEnd2.forward_word_end();
378    
379     m_textBuffer->remove_all_tags(itStart2, itEnd2);
380 schoenebeck 2886 #endif // USE_LS_SCRIPTVM
381 schoenebeck 2604 }
382    
383     void ScriptEditor::onModifiedChanged() {
384     m_applyButton.set_sensitive( m_textBuffer->get_modified() );
385     }
386    
387     void ScriptEditor::onButtonCancel() {
388     hide();
389     }
390    
391     void ScriptEditor::onButtonApply() {
392     m_script->SetScriptAsText(m_textBuffer->get_text());
393     m_textBuffer->set_modified(false);
394     }
395    
396     void ScriptEditor::onWindowHide() {
397     delete this; // this is the end, my friend
398     }

  ViewVC Help
Powered by ViewVC