--- gigedit/trunk/src/gigedit/scripteditor.cpp 2016/05/02 14:47:34 2900 +++ gigedit/trunk/src/gigedit/scripteditor.cpp 2017/05/26 22:10:16 3225 @@ -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. @@ -41,6 +41,8 @@ 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); @@ -48,19 +50,53 @@ 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) + m_applyButton(Gtk::Stock::APPLY), + m_cancelButton(Gtk::Stock::CANCEL) { m_script = NULL; #if USE_LS_SCRIPTVM m_vm = NULL; #endif - m_errorIcon = createIcon("dialog-error", get_screen()); - m_warningIcon = createIcon("dialog-warning-symbolic", get_screen()); - m_successIcon = createIcon("emblem-default", get_screen()); + 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); @@ -108,24 +144,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); @@ -134,7 +190,7 @@ m_applyButton.set_can_default(); m_applyButton.set_sensitive(false); m_applyButton.grab_focus(); - + #if GTKMM_MAJOR_VERSION >= 3 m_statusImage.set_margin_left(6); m_statusImage.set_margin_right(6); @@ -180,8 +236,6 @@ ); show_all_children(); - - resize(460,300); } ScriptEditor::~ScriptEditor() { @@ -191,6 +245,30 @@ #endif } +int ScriptEditor::currentFontSize() const { +#if defined(__APPLE__) + const int defaultFontSize = 13; +#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) { @@ -266,7 +344,14 @@ } static void getIteratorsForIssue(Glib::RefPtr& txtbuf, const LinuxSampler::ParserIssue& issue, Gtk::TextBuffer::iterator& start, Gtk::TextBuffer::iterator& end) { - start = txtbuf->get_iter_at_line_index(issue.firstLine - 1, issue.firstColumn - 1); + 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( @@ -277,8 +362,14 @@ } 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); @@ -294,6 +385,7 @@ 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) { @@ -330,13 +422,15 @@ m_errors = parserContext->errors(); m_warnings = parserContext->warnings(); - 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); + 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); + } } } @@ -445,6 +539,34 @@ 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"); @@ -500,7 +622,9 @@ } 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); }