/[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 2903 - (show annotations) (download)
Tue May 3 14:08:34 2016 UTC (7 years, 10 months ago) by schoenebeck
File size: 18208 byte(s)
* Script Editor: altered keyboard shortcut Ctrl-X to Ctrl-Q (to avoid
  masking the common cut text shortcut).
* Script Editor: if editor is used in live-mode, inform the sampler that
  it needs to automatically reload the script after the script has been
  altered and applied with the script editor.
* Bumped version (1.0.0.svn13).

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

  ViewVC Help
Powered by ViewVC