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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2900 - (show annotations) (download)
Mon May 2 14:47:34 2016 UTC (7 years, 10 months ago) by schoenebeck
File size: 17035 byte(s)
- Fixed compile error with GTK 2.

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

  ViewVC Help
Powered by ViewVC