--- gigedit/trunk/src/gigedit/scripteditor.cpp 2016/05/01 14:51:55 2896 +++ gigedit/trunk/src/gigedit/scripteditor.cpp 2017/07/18 22:00:56 3314 @@ -1,5 +1,5 @@ /* - Copyright (c) 2014-2016 Christian Schoenebeck + Copyright (c) 2014-2017 Christian Schoenebeck This file is part of "gigedit" and released under the terms of the GNU General Public License version 2. @@ -7,12 +7,17 @@ #include "scripteditor.h" #include "global.h" +#include // for gtk_widget_modify_*() +#if defined(__APPLE__) +# include "MacHelper.h" +#endif +#include // for log10() #if !USE_LS_SCRIPTVM static const std::string _keywords[] = { "on", "end", "declare", "while", "if", "or", "and", "not", "else", "case", - "select", "to", "const", "polyphonic", "mod" + "select", "to", "const", "polyphonic", "mod", "synchronized" }; static int _keywordsSz = sizeof(_keywords) / sizeof(std::string); @@ -35,15 +40,68 @@ #endif // !USE_LS_SCRIPTVM +static Glib::RefPtr createIcon(std::string name, const Glib::RefPtr& screen) { + const int targetH = 16; + Glib::RefPtr theme = Gtk::IconTheme::get_for_screen(screen); + int w = 0; + int h = 0; // ignored + Gtk::IconSize::lookup(Gtk::ICON_SIZE_SMALL_TOOLBAR, w, h); + if (!theme->has_icon(name)) + return Glib::RefPtr(); + Glib::RefPtr pixbuf = theme->load_icon(name, w, Gtk::ICON_LOOKUP_GENERIC_FALLBACK); + if (pixbuf->get_height() != targetH) { + pixbuf = pixbuf->scale_simple(targetH, targetH, Gdk::INTERP_BILINEAR); + } + return pixbuf; +} + +static Glib::RefPtr createIcon(std::vector alternativeNames, const Glib::RefPtr& screen) { + for (int i = 0; i < alternativeNames.size(); ++i) { + Glib::RefPtr buf = createIcon(alternativeNames[i], screen); + if (buf) return buf; + } + return Glib::RefPtr(); +} + ScriptEditor::ScriptEditor() : - m_applyButton(_("_Apply"), true), - m_cancelButton(_("_Cancel"), true) + m_statusLabel("", Gtk::ALIGN_START), + m_applyButton(Gtk::Stock::APPLY), + m_cancelButton(Gtk::Stock::CANCEL) { m_script = NULL; #if USE_LS_SCRIPTVM m_vm = NULL; #endif - m_ignoreEraseEvents = false; + + if (!Settings::singleton()->autoRestoreWindowDimension) { + set_default_size(800, 700); + set_position(Gtk::WIN_POS_MOUSE); + } + + // depending on GTK version and installed themes, there may be different + // icons, and different names for them, so for each type of icon we use, + // we provide a list of possible icon names, the first one found to be + // installed on the local system from the list will be used and loaded for + // the respective purpose (so order matters in those lists) + // + // (see https://developer.gnome.org/gtkmm/stable/namespaceGtk_1_1Stock.html for + // available icon names) + std::vector errorIconNames; + errorIconNames.push_back("dialog-error"); + errorIconNames.push_back("media-record"); + errorIconNames.push_back("process-stop"); + + std::vector warningIconNames; + warningIconNames.push_back("dialog-warning-symbolic"); + warningIconNames.push_back("dialog-warning"); + + std::vector successIconNames; + successIconNames.push_back("emblem-default"); + successIconNames.push_back("tools-check-spelling"); + + m_errorIcon = createIcon(errorIconNames, get_screen()); + m_warningIcon = createIcon(warningIconNames, get_screen()); + m_successIcon = createIcon(successIconNames, get_screen()); add(m_vbox); @@ -79,10 +137,17 @@ m_commentTag->property_foreground() = "#9c9c9c"; // gray m_tagTable->add(m_commentTag); + #define PREPROC_TOKEN_COLOR "#2f8a33" // green + m_preprocTag = Gtk::TextBuffer::Tag::create(); - m_preprocTag->property_foreground() = "#2f8a33"; // green + m_preprocTag->property_foreground() = PREPROC_TOKEN_COLOR; m_tagTable->add(m_preprocTag); + m_preprocCommentTag = Gtk::TextBuffer::Tag::create(); + m_preprocCommentTag->property_strikethrough() = true; + m_preprocCommentTag->property_background() = "#e5e5e5"; + m_tagTable->add(m_preprocCommentTag); + m_errorTag = Gtk::TextBuffer::Tag::create(); m_errorTag->property_background() = "#ff9393"; // red m_tagTable->add(m_errorTag); @@ -91,28 +156,71 @@ m_warningTag->property_background() = "#fffd7c"; // yellow m_tagTable->add(m_warningTag); - m_readOnlyTag = Gtk::TextBuffer::Tag::create(); - m_readOnlyTag->property_editable() = false; - m_tagTable->add(m_readOnlyTag); + m_lineNrTag = Gtk::TextBuffer::Tag::create(); + m_lineNrTag->property_foreground() = "#CCCCCC"; + m_tagTable->add(m_lineNrTag); + + // create menu + m_actionGroup = Gtk::ActionGroup::create(); + m_actionGroup->add(Gtk::Action::create("MenuScript", _("_Script"))); + m_actionGroup->add(Gtk::Action::create("Apply", _("_Apply")), + Gtk::AccelKey("s"), + sigc::mem_fun(*this, &ScriptEditor::onButtonApply)); + m_actionGroup->add(Gtk::Action::create("Close", _("_Close")), + Gtk::AccelKey("q"), + sigc::mem_fun(*this, &ScriptEditor::onButtonCancel)); + m_actionGroup->add(Gtk::Action::create("MenuEditor", _("_Editor"))); + m_actionGroup->add(Gtk::Action::create("ChangeFont", _("_Font Size ...")), + sigc::mem_fun(*this, &ScriptEditor::onMenuChangeFontSize)); + m_uiManager = Gtk::UIManager::create(); + m_uiManager->insert_action_group(m_actionGroup); + add_accel_group(m_uiManager->get_accel_group()); + m_uiManager->add_ui_from_string( + "" + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + "" + ); - m_textBuffer = Gtk::TextBuffer::create(m_tagTable); - m_textView.set_buffer(m_textBuffer); + m_lineNrBuffer = Gtk::TextBuffer::create(m_tagTable); + m_lineNrView.set_buffer(m_lineNrBuffer); + m_lineNrView.set_left_margin(3); + m_lineNrView.set_right_margin(3); + m_lineNrTextViewSpacer.set_size_request(5); { - Pango::FontDescription fdesc; - fdesc.set_family("monospace"); -#if defined(__APPLE__) - fdesc.set_size(12 * PANGO_SCALE); -#else - fdesc.set_size(10 * PANGO_SCALE); -#endif -#if GTKMM_MAJOR_VERSION < 3 - m_textView.modify_font(fdesc); -#else - m_textView.override_font(fdesc); -#endif + Gdk::Color color; + color.set("#F5F5F5"); + GtkWidget* widget = (GtkWidget*) m_lineNrView.gobj(); + gtk_widget_modify_base(widget, GTK_STATE_NORMAL, color.gobj()); + gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, color.gobj()); + } + { + Gdk::Color color; + color.set("#EEEEEE"); + GtkWidget* widget = (GtkWidget*) m_lineNrTextViewSpacer.gobj(); + gtk_widget_modify_base(widget, GTK_STATE_NORMAL, color.gobj()); + gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, color.gobj()); } - m_scrolledWindow.add(m_textView); + m_textBuffer = Gtk::TextBuffer::create(m_tagTable); + m_textView.set_buffer(m_textBuffer); + m_textView.set_left_margin(5); + setFontSize(currentFontSize(), false); + m_textViewHBox.pack_start(m_lineNrView, Gtk::PACK_SHRINK); + m_textViewHBox.pack_start(m_lineNrTextViewSpacer, Gtk::PACK_SHRINK); + m_textViewHBox.add(m_textView); + m_scrolledWindow.add(m_textViewHBox); m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); + + Gtk::Widget* menuBar = m_uiManager->get_widget("/MenuBar"); + m_vbox.pack_start(*menuBar, Gtk::PACK_SHRINK); m_vbox.pack_start(m_scrolledWindow); m_buttonBox.set_layout(Gtk::BUTTONBOX_END); @@ -121,7 +229,22 @@ m_applyButton.set_can_default(); m_applyButton.set_sensitive(false); m_applyButton.grab_focus(); - m_vbox.pack_start(m_buttonBox, Gtk::PACK_SHRINK); + +#if GTKMM_MAJOR_VERSION >= 3 + m_statusImage.set_margin_left(6); + m_statusImage.set_margin_right(6); +#else + m_statusHBox.set_spacing(6); +#endif + + m_statusHBox.pack_start(m_statusImage, Gtk::PACK_SHRINK); + m_statusHBox.pack_start(m_statusLabel); + m_statusHBox.show_all_children(); + + m_footerHBox.pack_start(m_statusHBox); + m_footerHBox.pack_start(m_buttonBox, Gtk::PACK_SHRINK); + + m_vbox.pack_start(m_footerHBox, Gtk::PACK_SHRINK); m_applyButton.signal_clicked().connect( sigc::mem_fun(*this, &ScriptEditor::onButtonApply) @@ -147,9 +270,11 @@ sigc::mem_fun(*this, &ScriptEditor::onWindowHide) ); - show_all_children(); + signal_delete_event().connect( + sigc::mem_fun(*this, &ScriptEditor::onWindowDelete) + ); - resize(460,300); + show_all_children(); } ScriptEditor::~ScriptEditor() { @@ -159,6 +284,41 @@ #endif } +int ScriptEditor::currentFontSize() const { +#if defined(__APPLE__) + const int defaultFontSize = 11; +#else + const int defaultFontSize = 10; +#endif + const int settingFontSize = Settings::singleton()->scriptEditorFontSize; + const int fontSize = (settingFontSize > 0) ? settingFontSize : defaultFontSize; + return fontSize; +} + +void ScriptEditor::setFontSize(int size, bool save) { + //printf("setFontSize(%d,%d)\n", size, save); + Pango::FontDescription fdesc; + fdesc.set_family("monospace"); +#if defined(__APPLE__) + // fixes poor readability of default monospace font on Macs + if (macIsMinMac10_6()) + fdesc.set_family("Menlo"); +#endif + fdesc.set_size(size * PANGO_SCALE); + /*Glib::RefPtr context = m_textView.get_pango_context(); + Cairo::FontOptions options; + options.set_antialias(Cairo::ANTIALIAS_NONE); + context->set_cairo_font_options(options);*/ +#if GTKMM_MAJOR_VERSION < 3 + m_lineNrView.modify_font(fdesc); + m_textView.modify_font(fdesc); +#else + m_lineNrView.override_font(fdesc); + m_textView.override_font(fdesc); +#endif + if (save) Settings::singleton()->scriptEditorFontSize = size; +} + void ScriptEditor::setScript(gig::Script* script) { m_script = script; if (!script) { @@ -174,14 +334,33 @@ m_textBuffer->set_modified(false); } +void ScriptEditor::updateLineNumbers() { + const int n = m_textBuffer->get_line_count(); + const int old = m_lineNrBuffer->get_line_count(); + if (n == old) return; + const int digits = log10(n) + 1; + const int bufSz = digits + 2; + char* buf = new char[bufSz]; + std::string sFmt1 = "%" + ToString(digits) + "d"; + std::string sFmt2 = "\n%" + ToString(digits) + "d"; + Glib::ustring s; + for (int i = 0; i < n; ++i) { + snprintf(buf, bufSz, i ? sFmt2.c_str() : sFmt1.c_str(), i+1); + s += buf; + } + m_lineNrBuffer->remove_all_tags(m_lineNrBuffer->begin(), m_lineNrBuffer->end()); + m_lineNrBuffer->set_text(s); + m_lineNrBuffer->apply_tag(m_lineNrTag, m_lineNrBuffer->begin(), m_lineNrBuffer->end()); + if (buf) delete[] buf; +} + void ScriptEditor::onTextInserted(const Gtk::TextBuffer::iterator& itEnd, const Glib::ustring& txt, int length) { - printf("onTextInserted()\n"); - fflush(stdout); + //printf("onTextInserted()\n"); #if USE_LS_SCRIPTVM - removeIssueAnchors(); m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end()); updateSyntaxHighlightingByVM(); updateParserIssuesByVM(); + updateStatusBar(); #else //printf("inserted %d\n", length); Gtk::TextBuffer::iterator itStart = itEnd; @@ -225,6 +404,7 @@ ; #endif // USE_LS_SCRIPTVM + updateLineNumbers(); } #if USE_LS_SCRIPTVM @@ -234,9 +414,34 @@ return m_vm; } +template +static void getIteratorsForIssue(Glib::RefPtr& txtbuf, const T& issue, Gtk::TextBuffer::iterator& start, Gtk::TextBuffer::iterator& end) { + Gtk::TextBuffer::iterator itLine = + txtbuf->get_iter_at_line_index(issue.firstLine - 1, 0); + const int charsInLine = itLine.get_bytes_in_line(); + start = txtbuf->get_iter_at_line_index( + issue.firstLine - 1, + // check we are not getting past the end of the line here, otherwise Gtk crashes + issue.firstColumn - 1 < charsInLine ? issue.firstColumn - 1 : charsInLine - 1 + ); + end = start; + end.forward_lines(issue.lastLine - issue.firstLine); + end.forward_chars( + (issue.lastLine != issue.firstLine) + ? issue.lastColumn - 1 + : issue.lastColumn - issue.firstColumn + 1 + ); +} + static void applyCodeTag(Glib::RefPtr& txtbuf, const LinuxSampler::VMSourceToken& token, Glib::RefPtr& tag) { - Gtk::TextBuffer::iterator itStart = - txtbuf->get_iter_at_line_index(token.firstLine(), token.firstColumn()); + Gtk::TextBuffer::iterator itLine = + txtbuf->get_iter_at_line_index(token.firstLine(), 0); + const int charsInLine = itLine.get_bytes_in_line(); + Gtk::TextBuffer::iterator itStart = txtbuf->get_iter_at_line_index( + token.firstLine(), + // check we are not getting past the end of the line here, otherwise Gtk crashes + token.firstColumn() < charsInLine ? token.firstColumn() : charsInLine - 1 + ); Gtk::TextBuffer::iterator itEnd = itStart; const int length = token.text().length(); itEnd.forward_chars(length); @@ -244,37 +449,21 @@ } static void applyCodeTag(Glib::RefPtr& txtbuf, const LinuxSampler::ParserIssue& issue, Glib::RefPtr& tag) { - Gtk::TextBuffer::iterator itStart = - txtbuf->get_iter_at_line_index(issue.firstLine - 1, issue.firstColumn - 1); - Gtk::TextBuffer::iterator itEnd = itStart; - itEnd.forward_lines(issue.lastLine - issue.firstLine); - itEnd.forward_chars( - (issue.lastLine != issue.firstLine) - ? issue.lastColumn - 1 - : issue.lastColumn - issue.firstColumn + 1 - ); + Gtk::TextBuffer::iterator itStart, itEnd; + getIteratorsForIssue(txtbuf, issue, itStart, itEnd); txtbuf->apply_tag(tag, itStart, itEnd); } -void ScriptEditor::removeIssueAnchors() { - m_ignoreEraseEvents = true; // avoid endless recursion - - for (int i = 0; i < m_issues.size(); ++i) { - const LinuxSampler::ParserIssue& issue = m_issues[i]; - printf("erase anchor at l%d c%d\n", issue.firstLine - 1, issue.firstColumn - 1); - fflush(stdout); - Gtk::TextBuffer::iterator iter = m_textBuffer->get_iter_at_line_index(issue.firstLine - 1, issue.firstColumn - 1); - Gtk::TextBuffer::iterator iterEnd = iter; - iterEnd.forward_chars(1); - m_textBuffer->erase(iter, iterEnd); - } - - m_ignoreEraseEvents = false; // back to normal +static void applyPreprocessorComment(Glib::RefPtr& txtbuf, const LinuxSampler::CodeBlock& block, Glib::RefPtr& tag) { + Gtk::TextBuffer::iterator itStart, itEnd; + getIteratorsForIssue(txtbuf, block, itStart, itEnd); + txtbuf->apply_tag(tag, itStart, itEnd); } void ScriptEditor::updateSyntaxHighlightingByVM() { GetScriptVM(); const std::string s = m_textBuffer->get_text(); + if (s.empty()) return; std::vector tokens = m_vm->syntaxHighlighting(s); for (int i = 0; i < tokens.size(); ++i) { @@ -303,71 +492,134 @@ } } -static Glib::RefPtr createIcon(std::string name, const Glib::RefPtr& screen) { - const int targetH = 9; - Glib::RefPtr theme = Gtk::IconTheme::get_for_screen(screen); - int w = 0; - int h = 0; // ignored - Gtk::IconSize::lookup(Gtk::ICON_SIZE_SMALL_TOOLBAR, w, h); - Glib::RefPtr pixbuf = theme->load_icon(name, w, Gtk::ICON_LOOKUP_GENERIC_FALLBACK); - if (pixbuf->get_height() != targetH) { - pixbuf = pixbuf->scale_simple(targetH, targetH, Gdk::INTERP_BILINEAR); - } - return pixbuf; -} - void ScriptEditor::updateParserIssuesByVM() { GetScriptVM(); const std::string s = m_textBuffer->get_text(); LinuxSampler::VMParserContext* parserContext = m_vm->loadScript(s); m_issues = parserContext->issues(); + m_errors = parserContext->errors(); + m_warnings = parserContext->warnings(); + m_preprocComments = parserContext->preprocessorComments(); + + if (!s.empty()) { + for (int i = 0; i < m_issues.size(); ++i) { + const LinuxSampler::ParserIssue& issue = m_issues[i]; + + if (issue.isErr()) { + applyCodeTag(m_textBuffer, issue, m_errorTag); + } else if (issue.isWrn()) { + applyCodeTag(m_textBuffer, issue, m_warningTag); + } + } + } + + for (int i = 0; i < m_preprocComments.size(); ++i) { + applyPreprocessorComment(m_textBuffer, m_preprocComments[i], + m_preprocCommentTag); + } + + delete parserContext; +} + +void ScriptEditor::updateIssueTooltip(GdkEventMotion* e) { + int x, y; + m_textView.window_to_buffer_coords(Gtk::TEXT_WINDOW_TEXT, int(e->x), int(e->y), x, y); + + Gtk::TextBuffer::iterator it; + m_textView.get_iter_at_location(it, x, y); + + const int line = it.get_line(); + const int column = it.get_line_offset(); + + //printf("mouse at l%d c%d\n", line, column); for (int i = 0; i < m_issues.size(); ++i) { const LinuxSampler::ParserIssue& issue = m_issues[i]; - - if (issue.isErr()) { - applyCodeTag(m_textBuffer, issue, m_errorTag); - } else if (issue.isWrn()) { - applyCodeTag(m_textBuffer, issue, m_warningTag); + const int firstLine = issue.firstLine - 1; + const int firstColumn = issue.firstColumn - 1; + const int lastLine = issue.lastLine - 1; + const int lastColumn = issue.lastColumn - 1; + if (firstLine <= line && line <= lastLine && + (firstLine != line || firstColumn <= column) && + (lastLine != line || lastColumn >= column)) + { + m_textView.set_tooltip_markup( + (issue.isErr() ? "ERROR: " : "Warning: ") + + issue.txt + ); + return; } } - for (int i = m_issues.size() - 1; i >= 0; --i) { - const LinuxSampler::ParserIssue& issue = m_issues[i]; + for (int i = 0; i < m_preprocComments.size(); ++i) { + const LinuxSampler::CodeBlock& block = m_preprocComments[i]; + const int firstLine = block.firstLine - 1; + const int firstColumn = block.firstColumn - 1; + const int lastLine = block.lastLine - 1; + const int lastColumn = block.lastColumn - 1; + if (firstLine <= line && line <= lastLine && + (firstLine != line || firstColumn <= column) && + (lastLine != line || lastColumn >= column)) + { + m_textView.set_tooltip_markup( + "Code block filtered out by preceding preprocessor statement." + ); + return; + } + } - if (issue.isErr() || issue.isWrn()) { - Glib::RefPtr pixbuf = createIcon(issue.isErr() ? "dialog-error" : "dialog-warning-symbolic", get_screen()); - Gtk::Image* image = Gtk::manage(new Gtk::Image(pixbuf)); - image->show(); - Gtk::TextBuffer::iterator iter = - m_textBuffer->get_iter_at_line_index(issue.firstLine - 1, issue.firstColumn - 1); - Glib::RefPtr anchor = m_textBuffer->create_child_anchor(iter); - m_textView.add_child_at_anchor(*image, anchor); - - iter = - m_textBuffer->get_iter_at_line_index(issue.firstLine - 1, issue.firstColumn - 1); - Gtk::TextBuffer::iterator itEnd = iter; - itEnd.forward_char(); + m_textView.set_tooltip_markup(""); +} - // prevent that the user can erase the icon with backspace key - m_textBuffer->apply_tag(m_readOnlyTag, iter, itEnd); +static std::string warningsCountTxt(const std::vector warnings) { + std::string txt = "" + ToString(warnings.size()); + txt += (warnings.size() == 1) ? " Warning" : " Warnings"; + txt += ""; + return txt; +} + +static std::string errorsCountTxt(const std::vector errors) { + std::string txt = "" + ToString(errors.size()); + txt += (errors.size() == 1) ? " Error" : " Errors"; + txt += ""; + return txt; +} + +void ScriptEditor::updateStatusBar() { + // update status text + std::string txt; + if (m_issues.empty()) { + txt = "No issues with this script."; + } else { + const char* txtWontLoad = ". Sampler won't load instruments using this script!"; + txt = "There "; + txt += (m_errors.size() <= 1 && m_warnings.size() <= 1) ? "is " : "are "; + if (m_errors.empty()) { + txt += warningsCountTxt(m_warnings) + ". Script will load, but might not behave as expected!"; + } else if (m_warnings.empty()) { + txt += errorsCountTxt(m_errors) + txtWontLoad; + } else { + txt += errorsCountTxt(m_errors) + " and " + + warningsCountTxt(m_warnings) + txtWontLoad; } } + m_statusLabel.set_markup(txt); - delete parserContext; + // update status icon + m_statusImage.set( + m_issues.empty() ? m_successIcon : !m_errors.empty() ? m_errorIcon : m_warningIcon + ); } #endif // USE_LS_SCRIPTVM void ScriptEditor::onTextErased(const Gtk::TextBuffer::iterator& itStart, const Gtk::TextBuffer::iterator& itEnd) { //printf("erased\n"); - if (m_ignoreEraseEvents) return; - #if USE_LS_SCRIPTVM - removeIssueAnchors(); m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end()); updateSyntaxHighlightingByVM(); updateParserIssuesByVM(); + updateStatusBar(); #else Gtk::TextBuffer::iterator itStart2 = itStart; if (itStart2.inside_word() || itStart2.ends_word()) @@ -378,18 +630,103 @@ m_textBuffer->remove_all_tags(itStart2, itEnd2); #endif // USE_LS_SCRIPTVM + updateLineNumbers(); +} + +bool ScriptEditor::on_motion_notify_event(GdkEventMotion* e) { +#if USE_LS_SCRIPTVM + //TODO: event throttling would be a good idea here + updateIssueTooltip(e); +#endif + return ManagedWindow::on_motion_notify_event(e); +} + +void ScriptEditor::onMenuChangeFontSize() { + //TODO: for GTKMM >= 3.2 class Gtk::FontChooser could be used instead + Gtk::Dialog dialog(_("Font Size"), true /*modal*/); + Gtk::HBox hbox; + hbox.set_spacing(6); + + Gtk::Label label(_("Editor's Font Size:"), Gtk::ALIGN_START); + hbox.pack_start(label, Gtk::PACK_SHRINK); + + Gtk::SpinButton spinButton; + spinButton.set_range(4, 80); + spinButton.set_increments(1, 10); + spinButton.set_value(currentFontSize()); + hbox.pack_start(spinButton); + + dialog.get_vbox()->pack_start(hbox); + dialog.add_button(_("_OK"), 0); + dialog.add_button(_("_Cancel"), 1); + + dialog.show_all_children(); + + if (!dialog.run()) { // OK selected ... + const int newFontSize = spinButton.get_value_as_int(); + if (newFontSize >= 4) + setFontSize(newFontSize, true); + } +} + +bool ScriptEditor::onWindowDelete(GdkEventAny* e) { + //printf("onWindowDelete\n"); + + if (!isModified()) return false; // propagate event further (which will close this window) + + gchar* msg = g_strdup_printf(_("Apply changes to instrument script \"%s\" before closing?"), + m_script->Name.c_str()); + Gtk::MessageDialog dialog(*this, msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE); + g_free(msg); + dialog.set_secondary_text(_("If you close without applying, your changes will be lost.")); + dialog.add_button(_("Close _Without Applying"), Gtk::RESPONSE_NO); + dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL); + dialog.add_button(_("_Apply"), Gtk::RESPONSE_YES); + dialog.set_default_response(Gtk::RESPONSE_YES); + int response = dialog.run(); + dialog.hide(); + + // user decided to close script editor without saving + if (response == Gtk::RESPONSE_NO) + return false; // propagate event further (which will close this window) + + // user cancelled dialog, thus don't close script editor + if (response == Gtk::RESPONSE_CANCEL) { + show(); + return true; // drop event (prevents closing this window) + } + + // user wants to apply the changes, afterwards close window + if (response == Gtk::RESPONSE_YES) { + onButtonApply(); + return false; // propagate event further (which will close this window) + } + + // should never ever make it to this point actually + return false; +} + +bool ScriptEditor::isModified() const { + return m_textBuffer->get_modified(); } void ScriptEditor::onModifiedChanged() { - m_applyButton.set_sensitive( m_textBuffer->get_modified() ); + m_applyButton.set_sensitive(isModified()); +#if USE_LS_SCRIPTVM + updateStatusBar(); +#endif } void ScriptEditor::onButtonCancel() { + bool dropEvent = onWindowDelete(NULL); + if (dropEvent) return; hide(); } void ScriptEditor::onButtonApply() { + signal_script_to_be_changed.emit(m_script); m_script->SetScriptAsText(m_textBuffer->get_text()); + signal_script_changed.emit(m_script); m_textBuffer->set_modified(false); }