/[svn]/gigedit/trunk/src/gigedit/scripteditor.cpp
ViewVC logotype

Diff of /gigedit/trunk/src/gigedit/scripteditor.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2897 by schoenebeck, Sun May 1 20:20:06 2016 UTC revision 2939 by schoenebeck, Mon Jul 11 17:58:33 2016 UTC
# Line 35  static bool isEvent(const Glib::ustring& Line 35  static bool isEvent(const Glib::ustring&
35    
36  #endif // !USE_LS_SCRIPTVM  #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() :  ScriptEditor::ScriptEditor() :
62        m_statusLabel("",  Gtk::ALIGN_START),
63      m_applyButton(_("_Apply"), true),      m_applyButton(_("_Apply"), true),
64      m_cancelButton(_("_Cancel"), true)      m_cancelButton(_("_Cancel"), true)
65  {  {
# Line 44  ScriptEditor::ScriptEditor() : Line 68  ScriptEditor::ScriptEditor() :
68      m_vm = NULL;      m_vm = NULL;
69  #endif  #endif
70    
71        // depending on GTK version and installed themes, there may be different
72        // icons, and different names for them, so for each type of icon we use,
73        // we provide a list of possible icon names, the first one found to be
74        // installed on the local system from the list will be used and loaded for
75        // the respective purpose (so order matters in those lists)
76        //
77        // (see https://developer.gnome.org/gtkmm/stable/namespaceGtk_1_1Stock.html for
78        // available icon names)
79        std::vector<std::string> errorIconNames;
80        errorIconNames.push_back("dialog-error");
81        errorIconNames.push_back("media-record");
82        errorIconNames.push_back("process-stop");
83    
84        std::vector<std::string> warningIconNames;
85        warningIconNames.push_back("dialog-warning-symbolic");
86        warningIconNames.push_back("dialog-warning");
87    
88        std::vector<std::string> successIconNames;
89        successIconNames.push_back("emblem-default");
90        successIconNames.push_back("tools-check-spelling");
91    
92        m_errorIcon = createIcon(errorIconNames, get_screen());
93        m_warningIcon = createIcon(warningIconNames, get_screen());
94        m_successIcon = createIcon(successIconNames, get_screen());
95    
96      add(m_vbox);      add(m_vbox);
97    
98      m_tagTable = Gtk::TextBuffer::TagTable::create();      m_tagTable = Gtk::TextBuffer::TagTable::create();
# Line 90  ScriptEditor::ScriptEditor() : Line 139  ScriptEditor::ScriptEditor() :
139      m_warningTag->property_background() = "#fffd7c"; // yellow      m_warningTag->property_background() = "#fffd7c"; // yellow
140      m_tagTable->add(m_warningTag);      m_tagTable->add(m_warningTag);
141    
142        // create menu
143        m_actionGroup = Gtk::ActionGroup::create();
144        m_actionGroup->add(Gtk::Action::create("MenuScript", _("_Script")));
145        m_actionGroup->add(Gtk::Action::create("Apply", _("_Apply")),
146                           Gtk::AccelKey("<control>s"),
147                           sigc::mem_fun(*this, &ScriptEditor::onButtonApply));
148        m_actionGroup->add(Gtk::Action::create("Close", _("_Close")),
149                           Gtk::AccelKey("<control>q"),
150                           sigc::mem_fun(*this, &ScriptEditor::onButtonCancel));
151        m_uiManager = Gtk::UIManager::create();
152        m_uiManager->insert_action_group(m_actionGroup);
153        add_accel_group(m_uiManager->get_accel_group());
154        m_uiManager->add_ui_from_string(
155            "<ui>"
156            "  <menubar name='MenuBar'>"
157            "    <menu action='MenuScript'>"
158            "      <menuitem action='Apply'/>"
159            "      <separator/>"
160            "      <menuitem action='Close'/>"
161            "    </menu>"
162            "  </menubar>"
163            "</ui>"
164        );
165    
166      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);
167      m_textView.set_buffer(m_textBuffer);      m_textView.set_buffer(m_textBuffer);
168      {      {
169          Pango::FontDescription fdesc;          Pango::FontDescription fdesc;
170          fdesc.set_family("monospace");          fdesc.set_family("monospace");
171  #if defined(__APPLE__)  #if defined(__APPLE__)
172          fdesc.set_size(12 * PANGO_SCALE);          fdesc.set_size(14 * PANGO_SCALE);
173  #else  #else
174          fdesc.set_size(10 * PANGO_SCALE);          fdesc.set_size(10 * PANGO_SCALE);
175  #endif  #endif
# Line 108  ScriptEditor::ScriptEditor() : Line 181  ScriptEditor::ScriptEditor() :
181      }      }
182      m_scrolledWindow.add(m_textView);      m_scrolledWindow.add(m_textView);
183      m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);      m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
184    
185        Gtk::Widget* menuBar = m_uiManager->get_widget("/MenuBar");
186        m_vbox.pack_start(*menuBar, Gtk::PACK_SHRINK);
187      m_vbox.pack_start(m_scrolledWindow);      m_vbox.pack_start(m_scrolledWindow);
188    
189      m_buttonBox.set_layout(Gtk::BUTTONBOX_END);      m_buttonBox.set_layout(Gtk::BUTTONBOX_END);
# Line 116  ScriptEditor::ScriptEditor() : Line 192  ScriptEditor::ScriptEditor() :
192      m_applyButton.set_can_default();      m_applyButton.set_can_default();
193      m_applyButton.set_sensitive(false);      m_applyButton.set_sensitive(false);
194      m_applyButton.grab_focus();      m_applyButton.grab_focus();
195      m_vbox.pack_start(m_buttonBox, Gtk::PACK_SHRINK);  
196    #if GTKMM_MAJOR_VERSION >= 3
197        m_statusImage.set_margin_left(6);
198        m_statusImage.set_margin_right(6);
199    #else
200        m_statusHBox.set_spacing(6);
201    #endif
202    
203        m_statusHBox.pack_start(m_statusImage, Gtk::PACK_SHRINK);
204        m_statusHBox.pack_start(m_statusLabel);
205        m_statusHBox.show_all_children();
206    
207        m_footerHBox.pack_start(m_statusHBox);
208        m_footerHBox.pack_start(m_buttonBox, Gtk::PACK_SHRINK);
209    
210        m_vbox.pack_start(m_footerHBox, Gtk::PACK_SHRINK);
211    
212      m_applyButton.signal_clicked().connect(      m_applyButton.signal_clicked().connect(
213          sigc::mem_fun(*this, &ScriptEditor::onButtonApply)          sigc::mem_fun(*this, &ScriptEditor::onButtonApply)
# Line 142  ScriptEditor::ScriptEditor() : Line 233  ScriptEditor::ScriptEditor() :
233          sigc::mem_fun(*this, &ScriptEditor::onWindowHide)          sigc::mem_fun(*this, &ScriptEditor::onWindowHide)
234      );      );
235    
236      show_all_children();      signal_delete_event().connect(
237            sigc::mem_fun(*this, &ScriptEditor::onWindowDelete)
238        );
239    
240      resize(460,300);      show_all_children();
241  }  }
242    
243  ScriptEditor::~ScriptEditor() {  ScriptEditor::~ScriptEditor() {
# Line 175  void ScriptEditor::onTextInserted(const Line 268  void ScriptEditor::onTextInserted(const
268      m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());      m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
269      updateSyntaxHighlightingByVM();      updateSyntaxHighlightingByVM();
270      updateParserIssuesByVM();      updateParserIssuesByVM();
271        updateStatusBar();
272  #else  #else
273      //printf("inserted %d\n", length);      //printf("inserted %d\n", length);
274      Gtk::TextBuffer::iterator itStart = itEnd;      Gtk::TextBuffer::iterator itStart = itEnd;
# Line 289  void ScriptEditor::updateParserIssuesByV Line 383  void ScriptEditor::updateParserIssuesByV
383      const std::string s = m_textBuffer->get_text();      const std::string s = m_textBuffer->get_text();
384      LinuxSampler::VMParserContext* parserContext = m_vm->loadScript(s);      LinuxSampler::VMParserContext* parserContext = m_vm->loadScript(s);
385      m_issues = parserContext->issues();      m_issues = parserContext->issues();
386        m_errors = parserContext->errors();
387        m_warnings = parserContext->warnings();
388    
389      for (int i = 0; i < m_issues.size(); ++i) {      for (int i = 0; i < m_issues.size(); ++i) {
390          const LinuxSampler::ParserIssue& issue = m_issues[i];          const LinuxSampler::ParserIssue& issue = m_issues[i];
# Line 336  void ScriptEditor::updateIssueTooltip(Gd Line 432  void ScriptEditor::updateIssueTooltip(Gd
432      m_textView.set_tooltip_markup("");      m_textView.set_tooltip_markup("");
433  }  }
434    
435    static std::string warningsCountTxt(const std::vector<LinuxSampler::ParserIssue> warnings) {
436        std::string txt = "<span foreground=\"#c4950c\">" + ToString(warnings.size());
437        txt += (warnings.size() == 1) ? " Warning" : " Warnings";
438        txt += "</span>";
439        return txt;
440    }
441    
442    static std::string errorsCountTxt(const std::vector<LinuxSampler::ParserIssue> errors) {
443        std::string txt = "<span foreground=\"#c40c0c\">" + ToString(errors.size());
444        txt += (errors.size() == 1) ? " Error" : " Errors";
445        txt += "</span>";
446        return txt;
447    }
448    
449    void ScriptEditor::updateStatusBar() {
450        // update status text
451        std::string txt;
452        if (m_issues.empty()) {
453            txt = "No issues with this script.";
454        } else {
455            const char* txtWontLoad = ". Sampler won't load instruments using this script!";
456            txt = "There ";
457            txt += (m_errors.size() <= 1 && m_warnings.size() <= 1) ? "is " : "are ";
458            if (m_errors.empty()) {
459                txt += warningsCountTxt(m_warnings) + ". Script will load, but might not behave as expected!";
460            } else if (m_warnings.empty()) {
461                txt += errorsCountTxt(m_errors) + txtWontLoad;
462            } else {
463                txt += errorsCountTxt(m_errors) + " and " +
464                       warningsCountTxt(m_warnings) + txtWontLoad;
465            }
466        }
467        m_statusLabel.set_markup(txt);
468    
469        // update status icon
470        m_statusImage.set(
471            m_issues.empty() ? m_successIcon : !m_errors.empty() ? m_errorIcon : m_warningIcon
472        );
473    }
474    
475  #endif // USE_LS_SCRIPTVM  #endif // USE_LS_SCRIPTVM
476    
477  void ScriptEditor::onTextErased(const Gtk::TextBuffer::iterator& itStart, const Gtk::TextBuffer::iterator& itEnd) {  void ScriptEditor::onTextErased(const Gtk::TextBuffer::iterator& itStart, const Gtk::TextBuffer::iterator& itEnd) {
# Line 344  void ScriptEditor::onTextErased(const Gt Line 480  void ScriptEditor::onTextErased(const Gt
480      m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());      m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
481      updateSyntaxHighlightingByVM();      updateSyntaxHighlightingByVM();
482      updateParserIssuesByVM();      updateParserIssuesByVM();
483        updateStatusBar();
484  #else  #else
485      Gtk::TextBuffer::iterator itStart2 = itStart;      Gtk::TextBuffer::iterator itStart2 = itStart;
486      if (itStart2.inside_word() || itStart2.ends_word())      if (itStart2.inside_word() || itStart2.ends_word())
# Line 364  bool ScriptEditor::on_motion_notify_even Line 501  bool ScriptEditor::on_motion_notify_even
501      return ManagedWindow::on_motion_notify_event(e);      return ManagedWindow::on_motion_notify_event(e);
502  }  }
503    
504    bool ScriptEditor::onWindowDelete(GdkEventAny* e) {
505        //printf("onWindowDelete\n");
506    
507        if (!isModified()) return false; // propagate event further (which will close this window)
508    
509        gchar* msg = g_strdup_printf(_("Apply changes to instrument script \"%s\" before closing?"),
510                                     m_script->Name.c_str());
511        Gtk::MessageDialog dialog(*this, msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
512        g_free(msg);
513        dialog.set_secondary_text(_("If you close without applying, your changes will be lost."));
514        dialog.add_button(_("Close _Without Applying"), Gtk::RESPONSE_NO);
515        dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL);
516        dialog.add_button(_("_Apply"), Gtk::RESPONSE_YES);
517        dialog.set_default_response(Gtk::RESPONSE_YES);
518        int response = dialog.run();
519        dialog.hide();
520    
521        // user decided to close script editor without saving
522        if (response == Gtk::RESPONSE_NO)
523            return false; // propagate event further (which will close this window)
524    
525        // user cancelled dialog, thus don't close script editor
526        if (response == Gtk::RESPONSE_CANCEL) {
527            show();
528            return true; // drop event (prevents closing this window)
529        }
530    
531        // user wants to apply the changes, afterwards close window
532        if (response == Gtk::RESPONSE_YES) {
533            onButtonApply();
534            return false; // propagate event further (which will close this window)
535        }
536    
537        // should never ever make it to this point actually
538        return false;
539    }
540    
541    bool ScriptEditor::isModified() const {
542        return m_textBuffer->get_modified();
543    }
544    
545  void ScriptEditor::onModifiedChanged() {  void ScriptEditor::onModifiedChanged() {
546      m_applyButton.set_sensitive( m_textBuffer->get_modified() );      m_applyButton.set_sensitive(isModified());
547    #if USE_LS_SCRIPTVM
548        updateStatusBar();
549    #endif
550  }  }
551    
552  void ScriptEditor::onButtonCancel() {  void ScriptEditor::onButtonCancel() {
553        bool dropEvent = onWindowDelete(NULL);
554        if (dropEvent) return;
555      hide();      hide();
556  }  }
557    
558  void ScriptEditor::onButtonApply() {  void ScriptEditor::onButtonApply() {
559        signal_script_to_be_changed.emit(m_script);
560      m_script->SetScriptAsText(m_textBuffer->get_text());      m_script->SetScriptAsText(m_textBuffer->get_text());
561        signal_script_changed.emit(m_script);
562      m_textBuffer->set_modified(false);      m_textBuffer->set_modified(false);
563  }  }
564    

Legend:
Removed from v.2897  
changed lines
  Added in v.2939

  ViewVC Help
Powered by ViewVC