/[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 2900 by schoenebeck, Mon May 2 14:47:34 2016 UTC revision 3158 by schoenebeck, Mon May 8 18:05:35 2017 UTC
# Line 41  static Glib::RefPtr<Gdk::Pixbuf> createI Line 41  static Glib::RefPtr<Gdk::Pixbuf> createI
41      int w = 0;      int w = 0;
42      int h = 0; // ignored      int h = 0; // ignored
43      Gtk::IconSize::lookup(Gtk::ICON_SIZE_SMALL_TOOLBAR, w, h);      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);      Glib::RefPtr<Gdk::Pixbuf> pixbuf = theme->load_icon(name, w, Gtk::ICON_LOOKUP_GENERIC_FALLBACK);
47      if (pixbuf->get_height() != targetH) {      if (pixbuf->get_height() != targetH) {
48          pixbuf = pixbuf->scale_simple(targetH, targetH, Gdk::INTERP_BILINEAR);          pixbuf = pixbuf->scale_simple(targetH, targetH, Gdk::INTERP_BILINEAR);
# Line 48  static Glib::RefPtr<Gdk::Pixbuf> createI Line 50  static Glib::RefPtr<Gdk::Pixbuf> createI
50      return pixbuf;      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),      m_statusLabel("",  Gtk::ALIGN_START),
63      m_applyButton(_("_Apply"), true),      m_applyButton(Gtk::Stock::APPLY),
64      m_cancelButton(_("_Cancel"), true)      m_cancelButton(Gtk::Stock::CANCEL)
65  {  {
66      m_script = NULL;      m_script = NULL;
67  #if USE_LS_SCRIPTVM  #if USE_LS_SCRIPTVM
68      m_vm = NULL;      m_vm = NULL;
69  #endif  #endif
70    
71      m_errorIcon = createIcon("dialog-error", get_screen());      // depending on GTK version and installed themes, there may be different
72      m_warningIcon = createIcon("dialog-warning-symbolic", get_screen());      // icons, and different names for them, so for each type of icon we use,
73      m_successIcon = createIcon("emblem-default", get_screen());      // 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    
# Line 108  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_actionGroup->add(Gtk::Action::create("MenuEditor", _("_Editor")));
152        m_actionGroup->add(Gtk::Action::create("ChangeFont", _("_Font Size ...")),
153                           sigc::mem_fun(*this, &ScriptEditor::onMenuChangeFontSize));
154        m_uiManager = Gtk::UIManager::create();
155        m_uiManager->insert_action_group(m_actionGroup);
156        add_accel_group(m_uiManager->get_accel_group());
157        m_uiManager->add_ui_from_string(
158            "<ui>"
159            "  <menubar name='MenuBar'>"
160            "    <menu action='MenuScript'>"
161            "      <menuitem action='Apply'/>"
162            "      <separator/>"
163            "      <menuitem action='Close'/>"
164            "    </menu>"
165            "    <menu action='MenuEditor'>"
166            "      <menuitem action='ChangeFont'/>"
167            "    </menu>"
168            "  </menubar>"
169            "</ui>"
170        );
171    
172      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);
173      m_textView.set_buffer(m_textBuffer);      m_textView.set_buffer(m_textBuffer);
174      {      setFontSize(currentFontSize(), false);
         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  
     }  
175      m_scrolledWindow.add(m_textView);      m_scrolledWindow.add(m_textView);
176      m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);      m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
177    
178        Gtk::Widget* menuBar = m_uiManager->get_widget("/MenuBar");
179        m_vbox.pack_start(*menuBar, Gtk::PACK_SHRINK);
180      m_vbox.pack_start(m_scrolledWindow);      m_vbox.pack_start(m_scrolledWindow);
181    
182      m_buttonBox.set_layout(Gtk::BUTTONBOX_END);      m_buttonBox.set_layout(Gtk::BUTTONBOX_END);
# Line 134  ScriptEditor::ScriptEditor() : Line 185  ScriptEditor::ScriptEditor() :
185      m_applyButton.set_can_default();      m_applyButton.set_can_default();
186      m_applyButton.set_sensitive(false);      m_applyButton.set_sensitive(false);
187      m_applyButton.grab_focus();      m_applyButton.grab_focus();
188        
189  #if GTKMM_MAJOR_VERSION >= 3  #if GTKMM_MAJOR_VERSION >= 3
190      m_statusImage.set_margin_left(6);      m_statusImage.set_margin_left(6);
191      m_statusImage.set_margin_right(6);      m_statusImage.set_margin_right(6);
# Line 180  ScriptEditor::ScriptEditor() : Line 231  ScriptEditor::ScriptEditor() :
231      );      );
232    
233      show_all_children();      show_all_children();
   
     resize(460,300);  
234  }  }
235    
236  ScriptEditor::~ScriptEditor() {  ScriptEditor::~ScriptEditor() {
# Line 191  ScriptEditor::~ScriptEditor() { Line 240  ScriptEditor::~ScriptEditor() {
240  #endif  #endif
241  }  }
242    
243    int ScriptEditor::currentFontSize() const {
244    #if defined(__APPLE__)
245        const int defaultFontSize = 13;
246    #else
247        const int defaultFontSize = 10;
248    #endif
249        const int settingFontSize = Settings::singleton()->scriptEditorFontSize;
250        const int fontSize = (settingFontSize > 0) ? settingFontSize : defaultFontSize;
251        return fontSize;
252    }
253    
254    void ScriptEditor::setFontSize(int size, bool save) {
255        //printf("setFontSize(%d,%d)\n", size, save);
256        Pango::FontDescription fdesc;
257        fdesc.set_family("monospace");
258        fdesc.set_size(size * PANGO_SCALE);
259    #if GTKMM_MAJOR_VERSION < 3
260        m_textView.modify_font(fdesc);
261    #else
262        m_textView.override_font(fdesc);
263    #endif
264        if (save) Settings::singleton()->scriptEditorFontSize = size;
265    }
266    
267  void ScriptEditor::setScript(gig::Script* script) {  void ScriptEditor::setScript(gig::Script* script) {
268      m_script = script;      m_script = script;
269      if (!script) {      if (!script) {
# Line 294  static void applyCodeTag(Glib::RefPtr<Gt Line 367  static void applyCodeTag(Glib::RefPtr<Gt
367  void ScriptEditor::updateSyntaxHighlightingByVM() {  void ScriptEditor::updateSyntaxHighlightingByVM() {
368      GetScriptVM();      GetScriptVM();
369      const std::string s = m_textBuffer->get_text();      const std::string s = m_textBuffer->get_text();
370        if (s.empty()) return;
371      std::vector<LinuxSampler::VMSourceToken> tokens = m_vm->syntaxHighlighting(s);      std::vector<LinuxSampler::VMSourceToken> tokens = m_vm->syntaxHighlighting(s);
372    
373      for (int i = 0; i < tokens.size(); ++i) {      for (int i = 0; i < tokens.size(); ++i) {
# Line 330  void ScriptEditor::updateParserIssuesByV Line 404  void ScriptEditor::updateParserIssuesByV
404      m_errors = parserContext->errors();      m_errors = parserContext->errors();
405      m_warnings = parserContext->warnings();      m_warnings = parserContext->warnings();
406    
407      for (int i = 0; i < m_issues.size(); ++i) {      if (!s.empty()) {
408          const LinuxSampler::ParserIssue& issue = m_issues[i];          for (int i = 0; i < m_issues.size(); ++i) {
409                const LinuxSampler::ParserIssue& issue = m_issues[i];
410          if (issue.isErr()) {  
411              applyCodeTag(m_textBuffer, issue, m_errorTag);              if (issue.isErr()) {
412          } else if (issue.isWrn()) {                  applyCodeTag(m_textBuffer, issue, m_errorTag);
413              applyCodeTag(m_textBuffer, issue, m_warningTag);              } else if (issue.isWrn()) {
414                    applyCodeTag(m_textBuffer, issue, m_warningTag);
415                }
416          }          }
417      }      }
418    
# Line 445  bool ScriptEditor::on_motion_notify_even Line 521  bool ScriptEditor::on_motion_notify_even
521      return ManagedWindow::on_motion_notify_event(e);      return ManagedWindow::on_motion_notify_event(e);
522  }  }
523    
524    void ScriptEditor::onMenuChangeFontSize() {
525        //TODO: for GTKMM >= 3.2 class Gtk::FontChooser could be used instead
526        Gtk::Dialog dialog(_("Font Size"), true /*modal*/);
527        Gtk::HBox hbox;
528        hbox.set_spacing(6);
529    
530        Gtk::Label label(_("Editor's Font Size:"), Gtk::ALIGN_START);
531        hbox.pack_start(label, Gtk::PACK_SHRINK);
532    
533        Gtk::SpinButton spinButton;
534        spinButton.set_range(4, 80);
535        spinButton.set_increments(1, 10);
536        spinButton.set_value(currentFontSize());
537        hbox.pack_start(spinButton);
538    
539        dialog.get_vbox()->pack_start(hbox);
540        dialog.add_button(_("_OK"), 0);
541        dialog.add_button(_("_Cancel"), 1);
542    
543        dialog.show_all_children();
544    
545        if (!dialog.run()) { // OK selected ...
546            const int newFontSize = spinButton.get_value_as_int();
547            if (newFontSize >= 4)
548                setFontSize(newFontSize, true);
549        }
550    }
551    
552  bool ScriptEditor::onWindowDelete(GdkEventAny* e) {  bool ScriptEditor::onWindowDelete(GdkEventAny* e) {
553      //printf("onWindowDelete\n");      //printf("onWindowDelete\n");
554    
# Line 500  void ScriptEditor::onButtonCancel() { Line 604  void ScriptEditor::onButtonCancel() {
604  }  }
605    
606  void ScriptEditor::onButtonApply() {  void ScriptEditor::onButtonApply() {
607        signal_script_to_be_changed.emit(m_script);
608      m_script->SetScriptAsText(m_textBuffer->get_text());      m_script->SetScriptAsText(m_textBuffer->get_text());
609        signal_script_changed.emit(m_script);
610      m_textBuffer->set_modified(false);      m_textBuffer->set_modified(false);
611  }  }
612    

Legend:
Removed from v.2900  
changed lines
  Added in v.3158

  ViewVC Help
Powered by ViewVC