/[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 3286 - (show annotations) (download)
Thu Jun 22 10:54:10 2017 UTC (6 years, 9 months ago) by schoenebeck
File size: 23717 byte(s)
* Script Editor: strike through code blocks filtered out by the
  preprocessor.
* Visual refinements of hatched patterns.
* Bumped version (1.0.0.svn53).

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

  ViewVC Help
Powered by ViewVC