--- gigedit/trunk/src/gigedit/scripteditor.cpp 2016/05/01 20:20:06 2897 +++ gigedit/trunk/src/gigedit/scripteditor.cpp 2016/07/16 15:31:47 2956 @@ -35,7 +35,31 @@ #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_statusLabel("", Gtk::ALIGN_START), m_applyButton(_("_Apply"), true), m_cancelButton(_("_Cancel"), true) { @@ -44,6 +68,31 @@ m_vm = NULL; #endif + // 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); m_tagTable = Gtk::TextBuffer::TagTable::create(); @@ -90,24 +139,44 @@ m_warningTag->property_background() = "#fffd7c"; // yellow m_tagTable->add(m_warningTag); + // 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); - { - 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 - } + setFontSize(currentFontSize(), false); m_scrolledWindow.add(m_textView); 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); @@ -116,7 +185,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) @@ -142,9 +226,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() { @@ -154,6 +240,30 @@ #endif } +int ScriptEditor::currentFontSize() const { +#if defined(__APPLE__) + const int defaultFontSize = 14; +#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"); + fdesc.set_size(size * PANGO_SCALE); +#if GTKMM_MAJOR_VERSION < 3 + m_textView.modify_font(fdesc); +#else + m_textView.override_font(fdesc); +#endif + if (save) Settings::singleton()->scriptEditorFontSize = size; +} + void ScriptEditor::setScript(gig::Script* script) { m_script = script; if (!script) { @@ -175,6 +285,7 @@ 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; @@ -289,6 +400,8 @@ 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(); for (int i = 0; i < m_issues.size(); ++i) { const LinuxSampler::ParserIssue& issue = m_issues[i]; @@ -336,6 +449,46 @@ m_textView.set_tooltip_markup(""); } +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); + + // 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) { @@ -344,6 +497,7 @@ 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()) @@ -364,16 +518,92 @@ 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); }