/[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 2897 - (hide annotations) (download)
Sun May 1 20:20:06 2016 UTC (7 years, 11 months ago) by schoenebeck
File size: 12413 byte(s)
* Script Editor: Reverted previous commit regarding error/warning
  icons, since Gtk seems not to be appropriate for handling icons
  correctly that way.
* Script Editor: Show precise parser error/warning messages as tooltip
  when the mouse points over the respective location in the script's
  source code.
* Bumped version (1.0.0.svn9).

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     show_all_children();
146    
147     resize(460,300);
148     }
149    
150     ScriptEditor::~ScriptEditor() {
151     printf("ScriptEditor destruct\n");
152 schoenebeck 2896 #if USE_LS_SCRIPTVM
153 schoenebeck 2886 if (m_vm) delete m_vm;
154 schoenebeck 2896 #endif
155 schoenebeck 2604 }
156    
157     void ScriptEditor::setScript(gig::Script* script) {
158     m_script = script;
159     if (!script) {
160     set_title(_("No Script"));
161     return;
162     }
163    
164     set_title(std::string(_("Instrument Script")) + " - \"" + script->Name + "\"");
165    
166     std::string txt = script->GetScriptAsText();
167     //printf("text : '%s'\n", txt.c_str());
168     m_textBuffer->set_text(txt);
169     m_textBuffer->set_modified(false);
170     }
171    
172     void ScriptEditor::onTextInserted(const Gtk::TextBuffer::iterator& itEnd, const Glib::ustring& txt, int length) {
173 schoenebeck 2897 //printf("onTextInserted()\n");
174 schoenebeck 2886 #if USE_LS_SCRIPTVM
175 schoenebeck 2890 m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
176 schoenebeck 2886 updateSyntaxHighlightingByVM();
177 schoenebeck 2890 updateParserIssuesByVM();
178 schoenebeck 2886 #else
179 schoenebeck 2610 //printf("inserted %d\n", length);
180 schoenebeck 2604 Gtk::TextBuffer::iterator itStart = itEnd;
181     itStart.backward_chars(length);
182    
183     Gtk::TextBuffer::iterator it = itStart;
184     it.backward_word_start();
185    
186     bool eofReached = false;
187     while (it <= itEnd) {
188     Gtk::TextBuffer::iterator itWordStart = it;
189     if (!it.forward_word_end()) {
190     eofReached = true;
191     it = itEnd;
192     }
193    
194     Glib::ustring s = m_textBuffer->get_text(itWordStart, it, false);
195 schoenebeck 2610 //printf("{%s}\n", s.c_str());
196 schoenebeck 2604 if (isKeyword(s))
197     m_textBuffer->apply_tag(m_keywordTag, itWordStart, it);
198     else if (isEvent(s)) {
199     // check if previous word is "on"
200     Gtk::TextBuffer::iterator itPreviousWordStart = itWordStart;
201     if (itPreviousWordStart.backward_word_start()) {
202     Gtk::TextBuffer::iterator itPreviousWordEnd = itPreviousWordStart;
203     itPreviousWordEnd.forward_word_end();
204     if (m_textBuffer->get_text(itPreviousWordStart, itPreviousWordEnd, false) == "on") {
205     m_textBuffer->apply_tag(m_eventTag, itWordStart, it);
206     }
207     }
208     }
209    
210     if (eofReached) break;
211    
212     while (!it.inside_word())
213     if (!it.forward_char())
214     goto EOF_REACHED;
215     }
216    
217     EOF_REACHED:
218     ;
219 schoenebeck 2886
220     #endif // USE_LS_SCRIPTVM
221 schoenebeck 2604 }
222    
223 schoenebeck 2886 #if USE_LS_SCRIPTVM
224    
225 schoenebeck 2890 LinuxSampler::ScriptVM* ScriptEditor::GetScriptVM() {
226     if (!m_vm) m_vm = LinuxSampler::ScriptVMFactory::Create("gig");
227     return m_vm;
228     }
229    
230 schoenebeck 2897 static void getIteratorsForIssue(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::ParserIssue& issue, Gtk::TextBuffer::iterator& start, Gtk::TextBuffer::iterator& end) {
231     start = txtbuf->get_iter_at_line_index(issue.firstLine - 1, issue.firstColumn - 1);
232     end = start;
233     end.forward_lines(issue.lastLine - issue.firstLine);
234     end.forward_chars(
235     (issue.lastLine != issue.firstLine)
236     ? issue.lastColumn - 1
237     : issue.lastColumn - issue.firstColumn + 1
238     );
239     }
240    
241 schoenebeck 2886 static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::VMSourceToken& token, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
242     Gtk::TextBuffer::iterator itStart =
243     txtbuf->get_iter_at_line_index(token.firstLine(), token.firstColumn());
244     Gtk::TextBuffer::iterator itEnd = itStart;
245     const int length = token.text().length();
246     itEnd.forward_chars(length);
247     txtbuf->apply_tag(tag, itStart, itEnd);
248     }
249    
250 schoenebeck 2890 static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::ParserIssue& issue, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
251 schoenebeck 2897 Gtk::TextBuffer::iterator itStart, itEnd;
252     getIteratorsForIssue(txtbuf, issue, itStart, itEnd);
253 schoenebeck 2890 txtbuf->apply_tag(tag, itStart, itEnd);
254     }
255    
256 schoenebeck 2886 void ScriptEditor::updateSyntaxHighlightingByVM() {
257 schoenebeck 2890 GetScriptVM();
258 schoenebeck 2886 const std::string s = m_textBuffer->get_text();
259     std::vector<LinuxSampler::VMSourceToken> tokens = m_vm->syntaxHighlighting(s);
260    
261     for (int i = 0; i < tokens.size(); ++i) {
262     const LinuxSampler::VMSourceToken& token = tokens[i];
263    
264     if (token.isKeyword()) {
265     applyCodeTag(m_textBuffer, token, m_keywordTag);
266     } else if (token.isVariableName()) {
267     applyCodeTag(m_textBuffer, token, m_variableTag);
268     } else if (token.isIdentifier()) {
269     if (token.isEventHandlerName()) {
270     applyCodeTag(m_textBuffer, token, m_eventTag);
271     } else { // a function ...
272     applyCodeTag(m_textBuffer, token, m_functionTag);
273     }
274     } else if (token.isNumberLiteral()) {
275     applyCodeTag(m_textBuffer, token, m_numberTag);
276     } else if (token.isStringLiteral()) {
277     applyCodeTag(m_textBuffer, token, m_stringTag);
278     } else if (token.isComment()) {
279     applyCodeTag(m_textBuffer, token, m_commentTag);
280     } else if (token.isPreprocessor()) {
281     applyCodeTag(m_textBuffer, token, m_preprocTag);
282     } else if (token.isNewLine()) {
283     }
284     }
285     }
286    
287 schoenebeck 2890 void ScriptEditor::updateParserIssuesByVM() {
288     GetScriptVM();
289     const std::string s = m_textBuffer->get_text();
290     LinuxSampler::VMParserContext* parserContext = m_vm->loadScript(s);
291 schoenebeck 2896 m_issues = parserContext->issues();
292 schoenebeck 2890
293 schoenebeck 2896 for (int i = 0; i < m_issues.size(); ++i) {
294     const LinuxSampler::ParserIssue& issue = m_issues[i];
295 schoenebeck 2890
296     if (issue.isErr()) {
297     applyCodeTag(m_textBuffer, issue, m_errorTag);
298     } else if (issue.isWrn()) {
299     applyCodeTag(m_textBuffer, issue, m_warningTag);
300     }
301     }
302    
303 schoenebeck 2897 delete parserContext;
304     }
305 schoenebeck 2896
306 schoenebeck 2897 void ScriptEditor::updateIssueTooltip(GdkEventMotion* e) {
307     int x, y;
308     m_textView.window_to_buffer_coords(Gtk::TEXT_WINDOW_TEXT, int(e->x), int(e->y), x, y);
309 schoenebeck 2896
310 schoenebeck 2897 Gtk::TextBuffer::iterator it;
311     m_textView.get_iter_at_location(it, x, y);
312    
313     const int line = it.get_line();
314     const int column = it.get_line_offset();
315    
316     //printf("mouse at l%d c%d\n", line, column);
317    
318     for (int i = 0; i < m_issues.size(); ++i) {
319     const LinuxSampler::ParserIssue& issue = m_issues[i];
320     const int firstLine = issue.firstLine - 1;
321     const int firstColumn = issue.firstColumn - 1;
322     const int lastLine = issue.lastLine - 1;
323     const int lastColumn = issue.lastColumn - 1;
324     if (firstLine <= line && line <= lastLine &&
325     (firstLine != line || firstColumn <= column) &&
326     (lastLine != line || lastColumn >= column))
327     {
328     m_textView.set_tooltip_markup(
329     (issue.isErr() ? "<span foreground=\"#ff9393\">ERROR:</span> " : "<span foreground=\"#c4950c\">Warning:</span> ") +
330     issue.txt
331     );
332     return;
333 schoenebeck 2896 }
334     }
335    
336 schoenebeck 2897 m_textView.set_tooltip_markup("");
337 schoenebeck 2890 }
338    
339 schoenebeck 2886 #endif // USE_LS_SCRIPTVM
340    
341 schoenebeck 2604 void ScriptEditor::onTextErased(const Gtk::TextBuffer::iterator& itStart, const Gtk::TextBuffer::iterator& itEnd) {
342 schoenebeck 2610 //printf("erased\n");
343 schoenebeck 2886 #if USE_LS_SCRIPTVM
344 schoenebeck 2890 m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
345 schoenebeck 2886 updateSyntaxHighlightingByVM();
346 schoenebeck 2890 updateParserIssuesByVM();
347 schoenebeck 2886 #else
348 schoenebeck 2604 Gtk::TextBuffer::iterator itStart2 = itStart;
349     if (itStart2.inside_word() || itStart2.ends_word())
350     itStart2.backward_word_start();
351    
352     Gtk::TextBuffer::iterator itEnd2 = itEnd;
353     if (itEnd2.inside_word()) itEnd2.forward_word_end();
354    
355     m_textBuffer->remove_all_tags(itStart2, itEnd2);
356 schoenebeck 2886 #endif // USE_LS_SCRIPTVM
357 schoenebeck 2604 }
358    
359 schoenebeck 2897 bool ScriptEditor::on_motion_notify_event(GdkEventMotion* e) {
360     #if USE_LS_SCRIPTVM
361     //TODO: event throttling would be a good idea here
362     updateIssueTooltip(e);
363     #endif
364     return ManagedWindow::on_motion_notify_event(e);
365     }
366    
367 schoenebeck 2604 void ScriptEditor::onModifiedChanged() {
368     m_applyButton.set_sensitive( m_textBuffer->get_modified() );
369     }
370    
371     void ScriptEditor::onButtonCancel() {
372     hide();
373     }
374    
375     void ScriptEditor::onButtonApply() {
376     m_script->SetScriptAsText(m_textBuffer->get_text());
377     m_textBuffer->set_modified(false);
378     }
379    
380     void ScriptEditor::onWindowHide() {
381     delete this; // this is the end, my friend
382     }

  ViewVC Help
Powered by ViewVC