/[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 2901 by schoenebeck, Mon May 2 16:10:56 2016 UTC revision 3225 by schoenebeck, Fri May 26 22:10:16 2017 UTC
# Line 1  Line 1 
1  /*  /*
2      Copyright (c) 2014-2016 Christian Schoenebeck      Copyright (c) 2014-2017 Christian Schoenebeck
3            
4      This file is part of "gigedit" and released under the terms of the      This file is part of "gigedit" and released under the terms of the
5      GNU General Public License version 2.      GNU General Public License version 2.
# 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());      if (!Settings::singleton()->autoRestoreWindowDimension) {
72      m_warningIcon = createIcon("dialog-warning-symbolic", get_screen());          set_default_size(800, 700);
73      m_successIcon = createIcon("emblem-default", get_screen());          set_position(Gtk::WIN_POS_MOUSE);
74        }
75    
76        // depending on GTK version and installed themes, there may be different
77        // icons, and different names for them, so for each type of icon we use,
78        // we provide a list of possible icon names, the first one found to be
79        // installed on the local system from the list will be used and loaded for
80        // the respective purpose (so order matters in those lists)
81        //
82        // (see https://developer.gnome.org/gtkmm/stable/namespaceGtk_1_1Stock.html for
83        // available icon names)
84        std::vector<std::string> errorIconNames;
85        errorIconNames.push_back("dialog-error");
86        errorIconNames.push_back("media-record");
87        errorIconNames.push_back("process-stop");
88    
89        std::vector<std::string> warningIconNames;
90        warningIconNames.push_back("dialog-warning-symbolic");
91        warningIconNames.push_back("dialog-warning");
92    
93        std::vector<std::string> successIconNames;
94        successIconNames.push_back("emblem-default");
95        successIconNames.push_back("tools-check-spelling");
96    
97        m_errorIcon = createIcon(errorIconNames, get_screen());
98        m_warningIcon = createIcon(warningIconNames, get_screen());
99        m_successIcon = createIcon(successIconNames, get_screen());
100    
101      add(m_vbox);      add(m_vbox);
102    
# Line 115  ScriptEditor::ScriptEditor() : Line 151  ScriptEditor::ScriptEditor() :
151                         Gtk::AccelKey("<control>s"),                         Gtk::AccelKey("<control>s"),
152                         sigc::mem_fun(*this, &ScriptEditor::onButtonApply));                         sigc::mem_fun(*this, &ScriptEditor::onButtonApply));
153      m_actionGroup->add(Gtk::Action::create("Close", _("_Close")),      m_actionGroup->add(Gtk::Action::create("Close", _("_Close")),
154                         Gtk::AccelKey("<control>x"),                         Gtk::AccelKey("<control>q"),
155                         sigc::mem_fun(*this, &ScriptEditor::onButtonCancel));                         sigc::mem_fun(*this, &ScriptEditor::onButtonCancel));
156        m_actionGroup->add(Gtk::Action::create("MenuEditor", _("_Editor")));
157        m_actionGroup->add(Gtk::Action::create("ChangeFont", _("_Font Size ...")),
158                           sigc::mem_fun(*this, &ScriptEditor::onMenuChangeFontSize));
159      m_uiManager = Gtk::UIManager::create();      m_uiManager = Gtk::UIManager::create();
160      m_uiManager->insert_action_group(m_actionGroup);      m_uiManager->insert_action_group(m_actionGroup);
161      add_accel_group(m_uiManager->get_accel_group());      add_accel_group(m_uiManager->get_accel_group());
# Line 128  ScriptEditor::ScriptEditor() : Line 167  ScriptEditor::ScriptEditor() :
167          "      <separator/>"          "      <separator/>"
168          "      <menuitem action='Close'/>"          "      <menuitem action='Close'/>"
169          "    </menu>"          "    </menu>"
170            "    <menu action='MenuEditor'>"
171            "      <menuitem action='ChangeFont'/>"
172            "    </menu>"
173          "  </menubar>"          "  </menubar>"
174          "</ui>"          "</ui>"
175      );      );
176    
177      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);
178      m_textView.set_buffer(m_textBuffer);      m_textView.set_buffer(m_textBuffer);
179      {      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  
     }  
180      m_scrolledWindow.add(m_textView);      m_scrolledWindow.add(m_textView);
181      m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);      m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
182    
# Line 216  ScriptEditor::~ScriptEditor() { Line 245  ScriptEditor::~ScriptEditor() {
245  #endif  #endif
246  }  }
247    
248    int ScriptEditor::currentFontSize() const {
249    #if defined(__APPLE__)
250        const int defaultFontSize = 13;
251    #else
252        const int defaultFontSize = 10;
253    #endif
254        const int settingFontSize = Settings::singleton()->scriptEditorFontSize;
255        const int fontSize = (settingFontSize > 0) ? settingFontSize : defaultFontSize;
256        return fontSize;
257    }
258    
259    void ScriptEditor::setFontSize(int size, bool save) {
260        //printf("setFontSize(%d,%d)\n", size, save);
261        Pango::FontDescription fdesc;
262        fdesc.set_family("monospace");
263        fdesc.set_size(size * PANGO_SCALE);
264    #if GTKMM_MAJOR_VERSION < 3
265        m_textView.modify_font(fdesc);
266    #else
267        m_textView.override_font(fdesc);
268    #endif
269        if (save) Settings::singleton()->scriptEditorFontSize = size;
270    }
271    
272  void ScriptEditor::setScript(gig::Script* script) {  void ScriptEditor::setScript(gig::Script* script) {
273      m_script = script;      m_script = script;
274      if (!script) {      if (!script) {
# Line 291  LinuxSampler::ScriptVM* ScriptEditor::Ge Line 344  LinuxSampler::ScriptVM* ScriptEditor::Ge
344  }  }
345    
346  static void getIteratorsForIssue(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::ParserIssue& issue, Gtk::TextBuffer::iterator& start, Gtk::TextBuffer::iterator& end) {  static void getIteratorsForIssue(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::ParserIssue& issue, Gtk::TextBuffer::iterator& start, Gtk::TextBuffer::iterator& end) {
347      start = txtbuf->get_iter_at_line_index(issue.firstLine - 1, issue.firstColumn - 1);      Gtk::TextBuffer::iterator itLine =
348            txtbuf->get_iter_at_line_index(issue.firstLine - 1, 0);
349        const int charsInLine = itLine.get_bytes_in_line();
350        start = txtbuf->get_iter_at_line_index(
351            issue.firstLine - 1,
352            // check we are not getting past the end of the line here, otherwise Gtk crashes
353            issue.firstColumn - 1 < charsInLine ? issue.firstColumn - 1 : charsInLine - 1
354        );
355      end = start;      end = start;
356      end.forward_lines(issue.lastLine - issue.firstLine);      end.forward_lines(issue.lastLine - issue.firstLine);
357      end.forward_chars(      end.forward_chars(
# Line 302  static void getIteratorsForIssue(Glib::R Line 362  static void getIteratorsForIssue(Glib::R
362  }  }
363    
364  static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::VMSourceToken& token, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {  static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::VMSourceToken& token, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
365      Gtk::TextBuffer::iterator itStart =      Gtk::TextBuffer::iterator itLine =
366          txtbuf->get_iter_at_line_index(token.firstLine(), token.firstColumn());          txtbuf->get_iter_at_line_index(token.firstLine(), 0);
367        const int charsInLine = itLine.get_bytes_in_line();
368        Gtk::TextBuffer::iterator itStart = txtbuf->get_iter_at_line_index(
369            token.firstLine(),
370            // check we are not getting past the end of the line here, otherwise Gtk crashes
371            token.firstColumn() < charsInLine ? token.firstColumn() : charsInLine - 1
372        );
373      Gtk::TextBuffer::iterator itEnd = itStart;      Gtk::TextBuffer::iterator itEnd = itStart;
374      const int length = token.text().length();      const int length = token.text().length();
375      itEnd.forward_chars(length);      itEnd.forward_chars(length);
# Line 319  static void applyCodeTag(Glib::RefPtr<Gt Line 385  static void applyCodeTag(Glib::RefPtr<Gt
385  void ScriptEditor::updateSyntaxHighlightingByVM() {  void ScriptEditor::updateSyntaxHighlightingByVM() {
386      GetScriptVM();      GetScriptVM();
387      const std::string s = m_textBuffer->get_text();      const std::string s = m_textBuffer->get_text();
388        if (s.empty()) return;
389      std::vector<LinuxSampler::VMSourceToken> tokens = m_vm->syntaxHighlighting(s);      std::vector<LinuxSampler::VMSourceToken> tokens = m_vm->syntaxHighlighting(s);
390    
391      for (int i = 0; i < tokens.size(); ++i) {      for (int i = 0; i < tokens.size(); ++i) {
# Line 355  void ScriptEditor::updateParserIssuesByV Line 422  void ScriptEditor::updateParserIssuesByV
422      m_errors = parserContext->errors();      m_errors = parserContext->errors();
423      m_warnings = parserContext->warnings();      m_warnings = parserContext->warnings();
424    
425      for (int i = 0; i < m_issues.size(); ++i) {      if (!s.empty()) {
426          const LinuxSampler::ParserIssue& issue = m_issues[i];          for (int i = 0; i < m_issues.size(); ++i) {
427                const LinuxSampler::ParserIssue& issue = m_issues[i];
428          if (issue.isErr()) {  
429              applyCodeTag(m_textBuffer, issue, m_errorTag);              if (issue.isErr()) {
430          } else if (issue.isWrn()) {                  applyCodeTag(m_textBuffer, issue, m_errorTag);
431              applyCodeTag(m_textBuffer, issue, m_warningTag);              } else if (issue.isWrn()) {
432                    applyCodeTag(m_textBuffer, issue, m_warningTag);
433                }
434          }          }
435      }      }
436    
# Line 470  bool ScriptEditor::on_motion_notify_even Line 539  bool ScriptEditor::on_motion_notify_even
539      return ManagedWindow::on_motion_notify_event(e);      return ManagedWindow::on_motion_notify_event(e);
540  }  }
541    
542    void ScriptEditor::onMenuChangeFontSize() {
543        //TODO: for GTKMM >= 3.2 class Gtk::FontChooser could be used instead
544        Gtk::Dialog dialog(_("Font Size"), true /*modal*/);
545        Gtk::HBox hbox;
546        hbox.set_spacing(6);
547    
548        Gtk::Label label(_("Editor's Font Size:"), Gtk::ALIGN_START);
549        hbox.pack_start(label, Gtk::PACK_SHRINK);
550    
551        Gtk::SpinButton spinButton;
552        spinButton.set_range(4, 80);
553        spinButton.set_increments(1, 10);
554        spinButton.set_value(currentFontSize());
555        hbox.pack_start(spinButton);
556    
557        dialog.get_vbox()->pack_start(hbox);
558        dialog.add_button(_("_OK"), 0);
559        dialog.add_button(_("_Cancel"), 1);
560    
561        dialog.show_all_children();
562    
563        if (!dialog.run()) { // OK selected ...
564            const int newFontSize = spinButton.get_value_as_int();
565            if (newFontSize >= 4)
566                setFontSize(newFontSize, true);
567        }
568    }
569    
570  bool ScriptEditor::onWindowDelete(GdkEventAny* e) {  bool ScriptEditor::onWindowDelete(GdkEventAny* e) {
571      //printf("onWindowDelete\n");      //printf("onWindowDelete\n");
572    
# Line 525  void ScriptEditor::onButtonCancel() { Line 622  void ScriptEditor::onButtonCancel() {
622  }  }
623    
624  void ScriptEditor::onButtonApply() {  void ScriptEditor::onButtonApply() {
625        signal_script_to_be_changed.emit(m_script);
626      m_script->SetScriptAsText(m_textBuffer->get_text());      m_script->SetScriptAsText(m_textBuffer->get_text());
627        signal_script_changed.emit(m_script);
628      m_textBuffer->set_modified(false);      m_textBuffer->set_modified(false);
629  }  }
630    

Legend:
Removed from v.2901  
changed lines
  Added in v.3225

  ViewVC Help
Powered by ViewVC