/[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 3314 - (show annotations) (download)
Tue Jul 18 22:00:56 2017 UTC (6 years, 9 months ago) by schoenebeck
File size: 26253 byte(s)
* Script Editor: Right aligned line numbers.
* Script Editor: Fixed font readability issue on Mac.
* Bumped version (1.0.0.svn59).

1 /*
2 Copyright (c) 2014-2017 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 #include <gtk/gtkwidget.h> // for gtk_widget_modify_*()
11 #if defined(__APPLE__)
12 # include "MacHelper.h"
13 #endif
14 #include <math.h> // for log10()
15
16 #if !USE_LS_SCRIPTVM
17
18 static const std::string _keywords[] = {
19 "on", "end", "declare", "while", "if", "or", "and", "not", "else", "case",
20 "select", "to", "const", "polyphonic", "mod", "synchronized"
21 };
22 static int _keywordsSz = sizeof(_keywords) / sizeof(std::string);
23
24 static const std::string _eventNames[] = {
25 "init", "note", "release", "controller"
26 };
27 static int _eventNamesSz = sizeof(_eventNames) / sizeof(std::string);
28
29 static bool isKeyword(const Glib::ustring& s) {
30 for (int i = 0; i < _keywordsSz; ++i)
31 if (_keywords[i] == s) return true;
32 return false;
33 }
34
35 static bool isEvent(const Glib::ustring& s) {
36 for (int i = 0; i < _eventNamesSz; ++i)
37 if (_eventNames[i] == s) return true;
38 return false;
39 }
40
41 #endif // !USE_LS_SCRIPTVM
42
43 static Glib::RefPtr<Gdk::Pixbuf> createIcon(std::string name, const Glib::RefPtr<Gdk::Screen>& screen) {
44 const int targetH = 16;
45 Glib::RefPtr<Gtk::IconTheme> theme = Gtk::IconTheme::get_for_screen(screen);
46 int w = 0;
47 int h = 0; // ignored
48 Gtk::IconSize::lookup(Gtk::ICON_SIZE_SMALL_TOOLBAR, w, h);
49 if (!theme->has_icon(name))
50 return Glib::RefPtr<Gdk::Pixbuf>();
51 Glib::RefPtr<Gdk::Pixbuf> pixbuf = theme->load_icon(name, w, Gtk::ICON_LOOKUP_GENERIC_FALLBACK);
52 if (pixbuf->get_height() != targetH) {
53 pixbuf = pixbuf->scale_simple(targetH, targetH, Gdk::INTERP_BILINEAR);
54 }
55 return pixbuf;
56 }
57
58 static Glib::RefPtr<Gdk::Pixbuf> createIcon(std::vector<std::string> alternativeNames, const Glib::RefPtr<Gdk::Screen>& screen) {
59 for (int i = 0; i < alternativeNames.size(); ++i) {
60 Glib::RefPtr<Gdk::Pixbuf> buf = createIcon(alternativeNames[i], screen);
61 if (buf) return buf;
62 }
63 return Glib::RefPtr<Gdk::Pixbuf>();
64 }
65
66 ScriptEditor::ScriptEditor() :
67 m_statusLabel("", Gtk::ALIGN_START),
68 m_applyButton(Gtk::Stock::APPLY),
69 m_cancelButton(Gtk::Stock::CANCEL)
70 {
71 m_script = NULL;
72 #if USE_LS_SCRIPTVM
73 m_vm = NULL;
74 #endif
75
76 if (!Settings::singleton()->autoRestoreWindowDimension) {
77 set_default_size(800, 700);
78 set_position(Gtk::WIN_POS_MOUSE);
79 }
80
81 // depending on GTK version and installed themes, there may be different
82 // icons, and different names for them, so for each type of icon we use,
83 // we provide a list of possible icon names, the first one found to be
84 // installed on the local system from the list will be used and loaded for
85 // the respective purpose (so order matters in those lists)
86 //
87 // (see https://developer.gnome.org/gtkmm/stable/namespaceGtk_1_1Stock.html for
88 // available icon names)
89 std::vector<std::string> errorIconNames;
90 errorIconNames.push_back("dialog-error");
91 errorIconNames.push_back("media-record");
92 errorIconNames.push_back("process-stop");
93
94 std::vector<std::string> warningIconNames;
95 warningIconNames.push_back("dialog-warning-symbolic");
96 warningIconNames.push_back("dialog-warning");
97
98 std::vector<std::string> successIconNames;
99 successIconNames.push_back("emblem-default");
100 successIconNames.push_back("tools-check-spelling");
101
102 m_errorIcon = createIcon(errorIconNames, get_screen());
103 m_warningIcon = createIcon(warningIconNames, get_screen());
104 m_successIcon = createIcon(successIconNames, get_screen());
105
106 add(m_vbox);
107
108 m_tagTable = Gtk::TextBuffer::TagTable::create();
109
110 m_keywordTag = Gtk::TextBuffer::Tag::create();
111 m_keywordTag->property_foreground() = "#000000"; // black
112 m_keywordTag->property_weight() = PANGO_WEIGHT_BOLD;
113 m_tagTable->add(m_keywordTag);
114
115 m_eventTag = Gtk::TextBuffer::Tag::create();
116 m_eventTag->property_foreground() = "#07c0cf"; // cyan 1
117 m_eventTag->property_weight() = PANGO_WEIGHT_BOLD;
118 m_tagTable->add(m_eventTag);
119
120 m_variableTag = Gtk::TextBuffer::Tag::create();
121 m_variableTag->property_foreground() = "#790cc4"; // magenta
122 m_tagTable->add(m_variableTag);
123
124 m_functionTag = Gtk::TextBuffer::Tag::create();
125 m_functionTag->property_foreground() = "#1ba1dd"; // cyan 2
126 m_tagTable->add(m_functionTag);
127
128 m_numberTag = Gtk::TextBuffer::Tag::create();
129 m_numberTag->property_foreground() = "#c4950c"; // yellow
130 m_tagTable->add(m_numberTag);
131
132 m_stringTag = Gtk::TextBuffer::Tag::create();
133 m_stringTag->property_foreground() = "#c40c0c"; // red
134 m_tagTable->add(m_stringTag);
135
136 m_commentTag = Gtk::TextBuffer::Tag::create();
137 m_commentTag->property_foreground() = "#9c9c9c"; // gray
138 m_tagTable->add(m_commentTag);
139
140 #define PREPROC_TOKEN_COLOR "#2f8a33" // green
141
142 m_preprocTag = Gtk::TextBuffer::Tag::create();
143 m_preprocTag->property_foreground() = PREPROC_TOKEN_COLOR;
144 m_tagTable->add(m_preprocTag);
145
146 m_preprocCommentTag = Gtk::TextBuffer::Tag::create();
147 m_preprocCommentTag->property_strikethrough() = true;
148 m_preprocCommentTag->property_background() = "#e5e5e5";
149 m_tagTable->add(m_preprocCommentTag);
150
151 m_errorTag = Gtk::TextBuffer::Tag::create();
152 m_errorTag->property_background() = "#ff9393"; // red
153 m_tagTable->add(m_errorTag);
154
155 m_warningTag = Gtk::TextBuffer::Tag::create();
156 m_warningTag->property_background() = "#fffd7c"; // yellow
157 m_tagTable->add(m_warningTag);
158
159 m_lineNrTag = Gtk::TextBuffer::Tag::create();
160 m_lineNrTag->property_foreground() = "#CCCCCC";
161 m_tagTable->add(m_lineNrTag);
162
163 // create menu
164 m_actionGroup = Gtk::ActionGroup::create();
165 m_actionGroup->add(Gtk::Action::create("MenuScript", _("_Script")));
166 m_actionGroup->add(Gtk::Action::create("Apply", _("_Apply")),
167 Gtk::AccelKey("<control>s"),
168 sigc::mem_fun(*this, &ScriptEditor::onButtonApply));
169 m_actionGroup->add(Gtk::Action::create("Close", _("_Close")),
170 Gtk::AccelKey("<control>q"),
171 sigc::mem_fun(*this, &ScriptEditor::onButtonCancel));
172 m_actionGroup->add(Gtk::Action::create("MenuEditor", _("_Editor")));
173 m_actionGroup->add(Gtk::Action::create("ChangeFont", _("_Font Size ...")),
174 sigc::mem_fun(*this, &ScriptEditor::onMenuChangeFontSize));
175 m_uiManager = Gtk::UIManager::create();
176 m_uiManager->insert_action_group(m_actionGroup);
177 add_accel_group(m_uiManager->get_accel_group());
178 m_uiManager->add_ui_from_string(
179 "<ui>"
180 " <menubar name='MenuBar'>"
181 " <menu action='MenuScript'>"
182 " <menuitem action='Apply'/>"
183 " <separator/>"
184 " <menuitem action='Close'/>"
185 " </menu>"
186 " <menu action='MenuEditor'>"
187 " <menuitem action='ChangeFont'/>"
188 " </menu>"
189 " </menubar>"
190 "</ui>"
191 );
192
193 m_lineNrBuffer = Gtk::TextBuffer::create(m_tagTable);
194 m_lineNrView.set_buffer(m_lineNrBuffer);
195 m_lineNrView.set_left_margin(3);
196 m_lineNrView.set_right_margin(3);
197 m_lineNrTextViewSpacer.set_size_request(5);
198 {
199 Gdk::Color color;
200 color.set("#F5F5F5");
201 GtkWidget* widget = (GtkWidget*) m_lineNrView.gobj();
202 gtk_widget_modify_base(widget, GTK_STATE_NORMAL, color.gobj());
203 gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, color.gobj());
204 }
205 {
206 Gdk::Color color;
207 color.set("#EEEEEE");
208 GtkWidget* widget = (GtkWidget*) m_lineNrTextViewSpacer.gobj();
209 gtk_widget_modify_base(widget, GTK_STATE_NORMAL, color.gobj());
210 gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, color.gobj());
211 }
212 m_textBuffer = Gtk::TextBuffer::create(m_tagTable);
213 m_textView.set_buffer(m_textBuffer);
214 m_textView.set_left_margin(5);
215 setFontSize(currentFontSize(), false);
216 m_textViewHBox.pack_start(m_lineNrView, Gtk::PACK_SHRINK);
217 m_textViewHBox.pack_start(m_lineNrTextViewSpacer, Gtk::PACK_SHRINK);
218 m_textViewHBox.add(m_textView);
219 m_scrolledWindow.add(m_textViewHBox);
220 m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
221
222 Gtk::Widget* menuBar = m_uiManager->get_widget("/MenuBar");
223 m_vbox.pack_start(*menuBar, Gtk::PACK_SHRINK);
224 m_vbox.pack_start(m_scrolledWindow);
225
226 m_buttonBox.set_layout(Gtk::BUTTONBOX_END);
227 m_buttonBox.pack_start(m_applyButton);
228 m_buttonBox.pack_start(m_cancelButton);
229 m_applyButton.set_can_default();
230 m_applyButton.set_sensitive(false);
231 m_applyButton.grab_focus();
232
233 #if GTKMM_MAJOR_VERSION >= 3
234 m_statusImage.set_margin_left(6);
235 m_statusImage.set_margin_right(6);
236 #else
237 m_statusHBox.set_spacing(6);
238 #endif
239
240 m_statusHBox.pack_start(m_statusImage, Gtk::PACK_SHRINK);
241 m_statusHBox.pack_start(m_statusLabel);
242 m_statusHBox.show_all_children();
243
244 m_footerHBox.pack_start(m_statusHBox);
245 m_footerHBox.pack_start(m_buttonBox, Gtk::PACK_SHRINK);
246
247 m_vbox.pack_start(m_footerHBox, Gtk::PACK_SHRINK);
248
249 m_applyButton.signal_clicked().connect(
250 sigc::mem_fun(*this, &ScriptEditor::onButtonApply)
251 );
252
253 m_cancelButton.signal_clicked().connect(
254 sigc::mem_fun(*this, &ScriptEditor::onButtonCancel)
255 );
256
257 m_textBuffer->signal_insert().connect(
258 sigc::mem_fun(*this, &ScriptEditor::onTextInserted)
259 );
260
261 m_textBuffer->signal_erase().connect(
262 sigc::mem_fun(*this, &ScriptEditor::onTextErased)
263 );
264
265 m_textBuffer->signal_modified_changed().connect(
266 sigc::mem_fun(*this, &ScriptEditor::onModifiedChanged)
267 );
268
269 signal_hide().connect(
270 sigc::mem_fun(*this, &ScriptEditor::onWindowHide)
271 );
272
273 signal_delete_event().connect(
274 sigc::mem_fun(*this, &ScriptEditor::onWindowDelete)
275 );
276
277 show_all_children();
278 }
279
280 ScriptEditor::~ScriptEditor() {
281 printf("ScriptEditor destruct\n");
282 #if USE_LS_SCRIPTVM
283 if (m_vm) delete m_vm;
284 #endif
285 }
286
287 int ScriptEditor::currentFontSize() const {
288 #if defined(__APPLE__)
289 const int defaultFontSize = 11;
290 #else
291 const int defaultFontSize = 10;
292 #endif
293 const int settingFontSize = Settings::singleton()->scriptEditorFontSize;
294 const int fontSize = (settingFontSize > 0) ? settingFontSize : defaultFontSize;
295 return fontSize;
296 }
297
298 void ScriptEditor::setFontSize(int size, bool save) {
299 //printf("setFontSize(%d,%d)\n", size, save);
300 Pango::FontDescription fdesc;
301 fdesc.set_family("monospace");
302 #if defined(__APPLE__)
303 // fixes poor readability of default monospace font on Macs
304 if (macIsMinMac10_6())
305 fdesc.set_family("Menlo");
306 #endif
307 fdesc.set_size(size * PANGO_SCALE);
308 /*Glib::RefPtr<Pango::Context> context = m_textView.get_pango_context();
309 Cairo::FontOptions options;
310 options.set_antialias(Cairo::ANTIALIAS_NONE);
311 context->set_cairo_font_options(options);*/
312 #if GTKMM_MAJOR_VERSION < 3
313 m_lineNrView.modify_font(fdesc);
314 m_textView.modify_font(fdesc);
315 #else
316 m_lineNrView.override_font(fdesc);
317 m_textView.override_font(fdesc);
318 #endif
319 if (save) Settings::singleton()->scriptEditorFontSize = size;
320 }
321
322 void ScriptEditor::setScript(gig::Script* script) {
323 m_script = script;
324 if (!script) {
325 set_title(_("No Script"));
326 return;
327 }
328
329 set_title(std::string(_("Instrument Script")) + " - \"" + script->Name + "\"");
330
331 std::string txt = script->GetScriptAsText();
332 //printf("text : '%s'\n", txt.c_str());
333 m_textBuffer->set_text(txt);
334 m_textBuffer->set_modified(false);
335 }
336
337 void ScriptEditor::updateLineNumbers() {
338 const int n = m_textBuffer->get_line_count();
339 const int old = m_lineNrBuffer->get_line_count();
340 if (n == old) return;
341 const int digits = log10(n) + 1;
342 const int bufSz = digits + 2;
343 char* buf = new char[bufSz];
344 std::string sFmt1 = "%" + ToString(digits) + "d";
345 std::string sFmt2 = "\n%" + ToString(digits) + "d";
346 Glib::ustring s;
347 for (int i = 0; i < n; ++i) {
348 snprintf(buf, bufSz, i ? sFmt2.c_str() : sFmt1.c_str(), i+1);
349 s += buf;
350 }
351 m_lineNrBuffer->remove_all_tags(m_lineNrBuffer->begin(), m_lineNrBuffer->end());
352 m_lineNrBuffer->set_text(s);
353 m_lineNrBuffer->apply_tag(m_lineNrTag, m_lineNrBuffer->begin(), m_lineNrBuffer->end());
354 if (buf) delete[] buf;
355 }
356
357 void ScriptEditor::onTextInserted(const Gtk::TextBuffer::iterator& itEnd, const Glib::ustring& txt, int length) {
358 //printf("onTextInserted()\n");
359 #if USE_LS_SCRIPTVM
360 m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
361 updateSyntaxHighlightingByVM();
362 updateParserIssuesByVM();
363 updateStatusBar();
364 #else
365 //printf("inserted %d\n", length);
366 Gtk::TextBuffer::iterator itStart = itEnd;
367 itStart.backward_chars(length);
368
369 Gtk::TextBuffer::iterator it = itStart;
370 it.backward_word_start();
371
372 bool eofReached = false;
373 while (it <= itEnd) {
374 Gtk::TextBuffer::iterator itWordStart = it;
375 if (!it.forward_word_end()) {
376 eofReached = true;
377 it = itEnd;
378 }
379
380 Glib::ustring s = m_textBuffer->get_text(itWordStart, it, false);
381 //printf("{%s}\n", s.c_str());
382 if (isKeyword(s))
383 m_textBuffer->apply_tag(m_keywordTag, itWordStart, it);
384 else if (isEvent(s)) {
385 // check if previous word is "on"
386 Gtk::TextBuffer::iterator itPreviousWordStart = itWordStart;
387 if (itPreviousWordStart.backward_word_start()) {
388 Gtk::TextBuffer::iterator itPreviousWordEnd = itPreviousWordStart;
389 itPreviousWordEnd.forward_word_end();
390 if (m_textBuffer->get_text(itPreviousWordStart, itPreviousWordEnd, false) == "on") {
391 m_textBuffer->apply_tag(m_eventTag, itWordStart, it);
392 }
393 }
394 }
395
396 if (eofReached) break;
397
398 while (!it.inside_word())
399 if (!it.forward_char())
400 goto EOF_REACHED;
401 }
402
403 EOF_REACHED:
404 ;
405
406 #endif // USE_LS_SCRIPTVM
407 updateLineNumbers();
408 }
409
410 #if USE_LS_SCRIPTVM
411
412 LinuxSampler::ScriptVM* ScriptEditor::GetScriptVM() {
413 if (!m_vm) m_vm = LinuxSampler::ScriptVMFactory::Create("gig");
414 return m_vm;
415 }
416
417 template<class T>
418 static void getIteratorsForIssue(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const T& issue, Gtk::TextBuffer::iterator& start, Gtk::TextBuffer::iterator& end) {
419 Gtk::TextBuffer::iterator itLine =
420 txtbuf->get_iter_at_line_index(issue.firstLine - 1, 0);
421 const int charsInLine = itLine.get_bytes_in_line();
422 start = txtbuf->get_iter_at_line_index(
423 issue.firstLine - 1,
424 // check we are not getting past the end of the line here, otherwise Gtk crashes
425 issue.firstColumn - 1 < charsInLine ? issue.firstColumn - 1 : charsInLine - 1
426 );
427 end = start;
428 end.forward_lines(issue.lastLine - issue.firstLine);
429 end.forward_chars(
430 (issue.lastLine != issue.firstLine)
431 ? issue.lastColumn - 1
432 : issue.lastColumn - issue.firstColumn + 1
433 );
434 }
435
436 static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::VMSourceToken& token, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
437 Gtk::TextBuffer::iterator itLine =
438 txtbuf->get_iter_at_line_index(token.firstLine(), 0);
439 const int charsInLine = itLine.get_bytes_in_line();
440 Gtk::TextBuffer::iterator itStart = txtbuf->get_iter_at_line_index(
441 token.firstLine(),
442 // check we are not getting past the end of the line here, otherwise Gtk crashes
443 token.firstColumn() < charsInLine ? token.firstColumn() : charsInLine - 1
444 );
445 Gtk::TextBuffer::iterator itEnd = itStart;
446 const int length = token.text().length();
447 itEnd.forward_chars(length);
448 txtbuf->apply_tag(tag, itStart, itEnd);
449 }
450
451 static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::ParserIssue& issue, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
452 Gtk::TextBuffer::iterator itStart, itEnd;
453 getIteratorsForIssue(txtbuf, issue, itStart, itEnd);
454 txtbuf->apply_tag(tag, itStart, itEnd);
455 }
456
457 static void applyPreprocessorComment(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::CodeBlock& block, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
458 Gtk::TextBuffer::iterator itStart, itEnd;
459 getIteratorsForIssue(txtbuf, block, itStart, itEnd);
460 txtbuf->apply_tag(tag, itStart, itEnd);
461 }
462
463 void ScriptEditor::updateSyntaxHighlightingByVM() {
464 GetScriptVM();
465 const std::string s = m_textBuffer->get_text();
466 if (s.empty()) return;
467 std::vector<LinuxSampler::VMSourceToken> tokens = m_vm->syntaxHighlighting(s);
468
469 for (int i = 0; i < tokens.size(); ++i) {
470 const LinuxSampler::VMSourceToken& token = tokens[i];
471
472 if (token.isKeyword()) {
473 applyCodeTag(m_textBuffer, token, m_keywordTag);
474 } else if (token.isVariableName()) {
475 applyCodeTag(m_textBuffer, token, m_variableTag);
476 } else if (token.isIdentifier()) {
477 if (token.isEventHandlerName()) {
478 applyCodeTag(m_textBuffer, token, m_eventTag);
479 } else { // a function ...
480 applyCodeTag(m_textBuffer, token, m_functionTag);
481 }
482 } else if (token.isNumberLiteral()) {
483 applyCodeTag(m_textBuffer, token, m_numberTag);
484 } else if (token.isStringLiteral()) {
485 applyCodeTag(m_textBuffer, token, m_stringTag);
486 } else if (token.isComment()) {
487 applyCodeTag(m_textBuffer, token, m_commentTag);
488 } else if (token.isPreprocessor()) {
489 applyCodeTag(m_textBuffer, token, m_preprocTag);
490 } else if (token.isNewLine()) {
491 }
492 }
493 }
494
495 void ScriptEditor::updateParserIssuesByVM() {
496 GetScriptVM();
497 const std::string s = m_textBuffer->get_text();
498 LinuxSampler::VMParserContext* parserContext = m_vm->loadScript(s);
499 m_issues = parserContext->issues();
500 m_errors = parserContext->errors();
501 m_warnings = parserContext->warnings();
502 m_preprocComments = parserContext->preprocessorComments();
503
504 if (!s.empty()) {
505 for (int i = 0; i < m_issues.size(); ++i) {
506 const LinuxSampler::ParserIssue& issue = m_issues[i];
507
508 if (issue.isErr()) {
509 applyCodeTag(m_textBuffer, issue, m_errorTag);
510 } else if (issue.isWrn()) {
511 applyCodeTag(m_textBuffer, issue, m_warningTag);
512 }
513 }
514 }
515
516 for (int i = 0; i < m_preprocComments.size(); ++i) {
517 applyPreprocessorComment(m_textBuffer, m_preprocComments[i],
518 m_preprocCommentTag);
519 }
520
521 delete parserContext;
522 }
523
524 void ScriptEditor::updateIssueTooltip(GdkEventMotion* e) {
525 int x, y;
526 m_textView.window_to_buffer_coords(Gtk::TEXT_WINDOW_TEXT, int(e->x), int(e->y), x, y);
527
528 Gtk::TextBuffer::iterator it;
529 m_textView.get_iter_at_location(it, x, y);
530
531 const int line = it.get_line();
532 const int column = it.get_line_offset();
533
534 //printf("mouse at l%d c%d\n", line, column);
535
536 for (int i = 0; i < m_issues.size(); ++i) {
537 const LinuxSampler::ParserIssue& issue = m_issues[i];
538 const int firstLine = issue.firstLine - 1;
539 const int firstColumn = issue.firstColumn - 1;
540 const int lastLine = issue.lastLine - 1;
541 const int lastColumn = issue.lastColumn - 1;
542 if (firstLine <= line && line <= lastLine &&
543 (firstLine != line || firstColumn <= column) &&
544 (lastLine != line || lastColumn >= column))
545 {
546 m_textView.set_tooltip_markup(
547 (issue.isErr() ? "<span foreground=\"#ff9393\">ERROR:</span> " : "<span foreground=\"#c4950c\">Warning:</span> ") +
548 issue.txt
549 );
550 return;
551 }
552 }
553
554 for (int i = 0; i < m_preprocComments.size(); ++i) {
555 const LinuxSampler::CodeBlock& block = m_preprocComments[i];
556 const int firstLine = block.firstLine - 1;
557 const int firstColumn = block.firstColumn - 1;
558 const int lastLine = block.lastLine - 1;
559 const int lastColumn = block.lastColumn - 1;
560 if (firstLine <= line && line <= lastLine &&
561 (firstLine != line || firstColumn <= column) &&
562 (lastLine != line || lastColumn >= column))
563 {
564 m_textView.set_tooltip_markup(
565 "Code block filtered out by preceding <span foreground=\"" PREPROC_TOKEN_COLOR "\">preprocessor</span> statement."
566 );
567 return;
568 }
569 }
570
571 m_textView.set_tooltip_markup("");
572 }
573
574 static std::string warningsCountTxt(const std::vector<LinuxSampler::ParserIssue> warnings) {
575 std::string txt = "<span foreground=\"#c4950c\">" + ToString(warnings.size());
576 txt += (warnings.size() == 1) ? " Warning" : " Warnings";
577 txt += "</span>";
578 return txt;
579 }
580
581 static std::string errorsCountTxt(const std::vector<LinuxSampler::ParserIssue> errors) {
582 std::string txt = "<span foreground=\"#c40c0c\">" + ToString(errors.size());
583 txt += (errors.size() == 1) ? " Error" : " Errors";
584 txt += "</span>";
585 return txt;
586 }
587
588 void ScriptEditor::updateStatusBar() {
589 // update status text
590 std::string txt;
591 if (m_issues.empty()) {
592 txt = "No issues with this script.";
593 } else {
594 const char* txtWontLoad = ". Sampler won't load instruments using this script!";
595 txt = "There ";
596 txt += (m_errors.size() <= 1 && m_warnings.size() <= 1) ? "is " : "are ";
597 if (m_errors.empty()) {
598 txt += warningsCountTxt(m_warnings) + ". Script will load, but might not behave as expected!";
599 } else if (m_warnings.empty()) {
600 txt += errorsCountTxt(m_errors) + txtWontLoad;
601 } else {
602 txt += errorsCountTxt(m_errors) + " and " +
603 warningsCountTxt(m_warnings) + txtWontLoad;
604 }
605 }
606 m_statusLabel.set_markup(txt);
607
608 // update status icon
609 m_statusImage.set(
610 m_issues.empty() ? m_successIcon : !m_errors.empty() ? m_errorIcon : m_warningIcon
611 );
612 }
613
614 #endif // USE_LS_SCRIPTVM
615
616 void ScriptEditor::onTextErased(const Gtk::TextBuffer::iterator& itStart, const Gtk::TextBuffer::iterator& itEnd) {
617 //printf("erased\n");
618 #if USE_LS_SCRIPTVM
619 m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
620 updateSyntaxHighlightingByVM();
621 updateParserIssuesByVM();
622 updateStatusBar();
623 #else
624 Gtk::TextBuffer::iterator itStart2 = itStart;
625 if (itStart2.inside_word() || itStart2.ends_word())
626 itStart2.backward_word_start();
627
628 Gtk::TextBuffer::iterator itEnd2 = itEnd;
629 if (itEnd2.inside_word()) itEnd2.forward_word_end();
630
631 m_textBuffer->remove_all_tags(itStart2, itEnd2);
632 #endif // USE_LS_SCRIPTVM
633 updateLineNumbers();
634 }
635
636 bool ScriptEditor::on_motion_notify_event(GdkEventMotion* e) {
637 #if USE_LS_SCRIPTVM
638 //TODO: event throttling would be a good idea here
639 updateIssueTooltip(e);
640 #endif
641 return ManagedWindow::on_motion_notify_event(e);
642 }
643
644 void ScriptEditor::onMenuChangeFontSize() {
645 //TODO: for GTKMM >= 3.2 class Gtk::FontChooser could be used instead
646 Gtk::Dialog dialog(_("Font Size"), true /*modal*/);
647 Gtk::HBox hbox;
648 hbox.set_spacing(6);
649
650 Gtk::Label label(_("Editor's Font Size:"), Gtk::ALIGN_START);
651 hbox.pack_start(label, Gtk::PACK_SHRINK);
652
653 Gtk::SpinButton spinButton;
654 spinButton.set_range(4, 80);
655 spinButton.set_increments(1, 10);
656 spinButton.set_value(currentFontSize());
657 hbox.pack_start(spinButton);
658
659 dialog.get_vbox()->pack_start(hbox);
660 dialog.add_button(_("_OK"), 0);
661 dialog.add_button(_("_Cancel"), 1);
662
663 dialog.show_all_children();
664
665 if (!dialog.run()) { // OK selected ...
666 const int newFontSize = spinButton.get_value_as_int();
667 if (newFontSize >= 4)
668 setFontSize(newFontSize, true);
669 }
670 }
671
672 bool ScriptEditor::onWindowDelete(GdkEventAny* e) {
673 //printf("onWindowDelete\n");
674
675 if (!isModified()) return false; // propagate event further (which will close this window)
676
677 gchar* msg = g_strdup_printf(_("Apply changes to instrument script \"%s\" before closing?"),
678 m_script->Name.c_str());
679 Gtk::MessageDialog dialog(*this, msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
680 g_free(msg);
681 dialog.set_secondary_text(_("If you close without applying, your changes will be lost."));
682 dialog.add_button(_("Close _Without Applying"), Gtk::RESPONSE_NO);
683 dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL);
684 dialog.add_button(_("_Apply"), Gtk::RESPONSE_YES);
685 dialog.set_default_response(Gtk::RESPONSE_YES);
686 int response = dialog.run();
687 dialog.hide();
688
689 // user decided to close script editor without saving
690 if (response == Gtk::RESPONSE_NO)
691 return false; // propagate event further (which will close this window)
692
693 // user cancelled dialog, thus don't close script editor
694 if (response == Gtk::RESPONSE_CANCEL) {
695 show();
696 return true; // drop event (prevents closing this window)
697 }
698
699 // user wants to apply the changes, afterwards close window
700 if (response == Gtk::RESPONSE_YES) {
701 onButtonApply();
702 return false; // propagate event further (which will close this window)
703 }
704
705 // should never ever make it to this point actually
706 return false;
707 }
708
709 bool ScriptEditor::isModified() const {
710 return m_textBuffer->get_modified();
711 }
712
713 void ScriptEditor::onModifiedChanged() {
714 m_applyButton.set_sensitive(isModified());
715 #if USE_LS_SCRIPTVM
716 updateStatusBar();
717 #endif
718 }
719
720 void ScriptEditor::onButtonCancel() {
721 bool dropEvent = onWindowDelete(NULL);
722 if (dropEvent) return;
723 hide();
724 }
725
726 void ScriptEditor::onButtonApply() {
727 signal_script_to_be_changed.emit(m_script);
728 m_script->SetScriptAsText(m_textBuffer->get_text());
729 signal_script_changed.emit(m_script);
730 m_textBuffer->set_modified(false);
731 }
732
733 void ScriptEditor::onWindowHide() {
734 delete this; // this is the end, my friend
735 }

  ViewVC Help
Powered by ViewVC