/[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 2928 by schoenebeck, Fri Jul 1 14:09:25 2016 UTC revision 3314 by schoenebeck, Tue Jul 18 22:00:56 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 7  Line 7 
7    
8  #include "scripteditor.h"  #include "scripteditor.h"
9  #include "global.h"  #include "global.h"
10    #include <gtk/gtkwidget.h> // for gtk_widget_modify_*()
11    #if defined(__APPLE__)
12    # include "MacHelper.h"
13    #endif
14    #include <math.h> // for log10()
15    
16  #if !USE_LS_SCRIPTVM  #if !USE_LS_SCRIPTVM
17    
18  static const std::string _keywords[] = {  static const std::string _keywords[] = {
19      "on", "end", "declare", "while", "if", "or", "and", "not", "else", "case",      "on", "end", "declare", "while", "if", "or", "and", "not", "else", "case",
20      "select", "to", "const", "polyphonic", "mod"      "select", "to", "const", "polyphonic", "mod", "synchronized"
21  };  };
22  static int _keywordsSz = sizeof(_keywords) / sizeof(std::string);  static int _keywordsSz = sizeof(_keywords) / sizeof(std::string);
23    
# Line 60  static Glib::RefPtr<Gdk::Pixbuf> createI Line 65  static Glib::RefPtr<Gdk::Pixbuf> createI
65    
66  ScriptEditor::ScriptEditor() :  ScriptEditor::ScriptEditor() :
67      m_statusLabel("",  Gtk::ALIGN_START),      m_statusLabel("",  Gtk::ALIGN_START),
68      m_applyButton(_("_Apply"), true),      m_applyButton(Gtk::Stock::APPLY),
69      m_cancelButton(_("_Cancel"), true)      m_cancelButton(Gtk::Stock::CANCEL)
70  {  {
71      m_script = NULL;      m_script = NULL;
72  #if USE_LS_SCRIPTVM  #if USE_LS_SCRIPTVM
73      m_vm = NULL;      m_vm = NULL;
74  #endif  #endif
75    
76        if (!Settings::singleton()->autoRestoreWindowDimension) {
77            set_default_size(800, 700);
78            set_position(Gtk::WIN_POS_MOUSE);
79        }
80    
81      // depending on GTK version and installed themes, there may be different      // depending on GTK version and installed themes, there may be different
82      // icons, and different names for them, so for each type of icon we use,      // icons, and different names for them, so for each type of icon we use,
83      // we provide a list of possible icon names, the first one found to be      // we provide a list of possible icon names, the first one found to be
# Line 127  ScriptEditor::ScriptEditor() : Line 137  ScriptEditor::ScriptEditor() :
137      m_commentTag->property_foreground() = "#9c9c9c"; // gray      m_commentTag->property_foreground() = "#9c9c9c"; // gray
138      m_tagTable->add(m_commentTag);      m_tagTable->add(m_commentTag);
139    
140        #define PREPROC_TOKEN_COLOR  "#2f8a33" // green
141    
142      m_preprocTag = Gtk::TextBuffer::Tag::create();      m_preprocTag = Gtk::TextBuffer::Tag::create();
143      m_preprocTag->property_foreground() = "#2f8a33"; // green      m_preprocTag->property_foreground() = PREPROC_TOKEN_COLOR;
144      m_tagTable->add(m_preprocTag);      m_tagTable->add(m_preprocTag);
145    
146        m_preprocCommentTag = Gtk::TextBuffer::Tag::create();
147        m_preprocCommentTag->property_strikethrough() = true;
148        m_preprocCommentTag->property_background() = "#e5e5e5";
149        m_tagTable->add(m_preprocCommentTag);
150    
151      m_errorTag = Gtk::TextBuffer::Tag::create();      m_errorTag = Gtk::TextBuffer::Tag::create();
152      m_errorTag->property_background() = "#ff9393"; // red      m_errorTag->property_background() = "#ff9393"; // red
153      m_tagTable->add(m_errorTag);      m_tagTable->add(m_errorTag);
# Line 139  ScriptEditor::ScriptEditor() : Line 156  ScriptEditor::ScriptEditor() :
156      m_warningTag->property_background() = "#fffd7c"; // yellow      m_warningTag->property_background() = "#fffd7c"; // yellow
157      m_tagTable->add(m_warningTag);      m_tagTable->add(m_warningTag);
158    
159        m_lineNrTag = Gtk::TextBuffer::Tag::create();
160        m_lineNrTag->property_foreground() = "#CCCCCC";
161        m_tagTable->add(m_lineNrTag);
162    
163      // create menu      // create menu
164      m_actionGroup = Gtk::ActionGroup::create();      m_actionGroup = Gtk::ActionGroup::create();
165      m_actionGroup->add(Gtk::Action::create("MenuScript", _("_Script")));      m_actionGroup->add(Gtk::Action::create("MenuScript", _("_Script")));
# Line 148  ScriptEditor::ScriptEditor() : Line 169  ScriptEditor::ScriptEditor() :
169      m_actionGroup->add(Gtk::Action::create("Close", _("_Close")),      m_actionGroup->add(Gtk::Action::create("Close", _("_Close")),
170                         Gtk::AccelKey("<control>q"),                         Gtk::AccelKey("<control>q"),
171                         sigc::mem_fun(*this, &ScriptEditor::onButtonCancel));                         sigc::mem_fun(*this, &ScriptEditor::onButtonCancel));
172        m_actionGroup->add(Gtk::Action::create("MenuEditor", _("_Editor")));
173        m_actionGroup->add(Gtk::Action::create("ChangeFont", _("_Font Size ...")),
174                           sigc::mem_fun(*this, &ScriptEditor::onMenuChangeFontSize));
175      m_uiManager = Gtk::UIManager::create();      m_uiManager = Gtk::UIManager::create();
176      m_uiManager->insert_action_group(m_actionGroup);      m_uiManager->insert_action_group(m_actionGroup);
177      add_accel_group(m_uiManager->get_accel_group());      add_accel_group(m_uiManager->get_accel_group());
# Line 159  ScriptEditor::ScriptEditor() : Line 183  ScriptEditor::ScriptEditor() :
183          "      <separator/>"          "      <separator/>"
184          "      <menuitem action='Close'/>"          "      <menuitem action='Close'/>"
185          "    </menu>"          "    </menu>"
186            "    <menu action='MenuEditor'>"
187            "      <menuitem action='ChangeFont'/>"
188            "    </menu>"
189          "  </menubar>"          "  </menubar>"
190          "</ui>"          "</ui>"
191      );      );
192    
193      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);      m_lineNrBuffer = Gtk::TextBuffer::create(m_tagTable);
194      m_textView.set_buffer(m_textBuffer);      m_lineNrView.set_buffer(m_lineNrBuffer);
195        m_lineNrView.set_left_margin(3);
196        m_lineNrView.set_right_margin(3);
197        m_lineNrTextViewSpacer.set_size_request(5);
198      {      {
199          Pango::FontDescription fdesc;          Gdk::Color color;
200          fdesc.set_family("monospace");          color.set("#F5F5F5");
201  #if defined(__APPLE__)          GtkWidget* widget = (GtkWidget*) m_lineNrView.gobj();
202          fdesc.set_size(12 * PANGO_SCALE);          gtk_widget_modify_base(widget, GTK_STATE_NORMAL, color.gobj());
203  #else          gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, color.gobj());
204          fdesc.set_size(10 * PANGO_SCALE);      }
205  #endif      {
206  #if GTKMM_MAJOR_VERSION < 3          Gdk::Color color;
207          m_textView.modify_font(fdesc);          color.set("#EEEEEE");
208  #else          GtkWidget* widget = (GtkWidget*) m_lineNrTextViewSpacer.gobj();
209          m_textView.override_font(fdesc);          gtk_widget_modify_base(widget, GTK_STATE_NORMAL, color.gobj());
210  #endif          gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, color.gobj());
211      }      }
212      m_scrolledWindow.add(m_textView);      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);
213        m_textView.set_buffer(m_textBuffer);
214        m_textView.set_left_margin(5);
215        setFontSize(currentFontSize(), false);
216        m_textViewHBox.pack_start(m_lineNrView, Gtk::PACK_SHRINK);
217        m_textViewHBox.pack_start(m_lineNrTextViewSpacer, Gtk::PACK_SHRINK);
218        m_textViewHBox.add(m_textView);
219        m_scrolledWindow.add(m_textViewHBox);
220      m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);      m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
221    
222      Gtk::Widget* menuBar = m_uiManager->get_widget("/MenuBar");      Gtk::Widget* menuBar = m_uiManager->get_widget("/MenuBar");
# Line 247  ScriptEditor::~ScriptEditor() { Line 284  ScriptEditor::~ScriptEditor() {
284  #endif  #endif
285  }  }
286    
287    int ScriptEditor::currentFontSize() const {
288    #if defined(__APPLE__)
289        const int defaultFontSize = 11;
290    #else
291        const int defaultFontSize = 10;
292    #endif
293        const int settingFontSize = Settings::singleton()->scriptEditorFontSize;
294        const int fontSize = (settingFontSize > 0) ? settingFontSize : defaultFontSize;
295        return fontSize;
296    }
297    
298    void ScriptEditor::setFontSize(int size, bool save) {
299        //printf("setFontSize(%d,%d)\n", size, save);
300        Pango::FontDescription fdesc;
301        fdesc.set_family("monospace");
302    #if defined(__APPLE__)
303        // fixes poor readability of default monospace font on Macs
304        if (macIsMinMac10_6())
305            fdesc.set_family("Menlo");
306    #endif
307        fdesc.set_size(size * PANGO_SCALE);
308        /*Glib::RefPtr<Pango::Context> context = m_textView.get_pango_context();
309        Cairo::FontOptions options;
310        options.set_antialias(Cairo::ANTIALIAS_NONE);
311        context->set_cairo_font_options(options);*/
312    #if GTKMM_MAJOR_VERSION < 3
313        m_lineNrView.modify_font(fdesc);
314        m_textView.modify_font(fdesc);
315    #else
316        m_lineNrView.override_font(fdesc);
317        m_textView.override_font(fdesc);
318    #endif
319        if (save) Settings::singleton()->scriptEditorFontSize = size;
320    }
321    
322  void ScriptEditor::setScript(gig::Script* script) {  void ScriptEditor::setScript(gig::Script* script) {
323      m_script = script;      m_script = script;
324      if (!script) {      if (!script) {
# Line 262  void ScriptEditor::setScript(gig::Script Line 334  void ScriptEditor::setScript(gig::Script
334      m_textBuffer->set_modified(false);      m_textBuffer->set_modified(false);
335  }  }
336    
337    void ScriptEditor::updateLineNumbers() {
338        const int n = m_textBuffer->get_line_count();
339        const int old = m_lineNrBuffer->get_line_count();
340        if (n == old) return;
341        const int digits = log10(n) + 1;
342        const int bufSz = digits + 2;
343        char* buf = new char[bufSz];
344        std::string sFmt1 =   "%" + ToString(digits) + "d";
345        std::string sFmt2 = "\n%" + ToString(digits) + "d";
346        Glib::ustring s;
347        for (int i = 0; i < n; ++i) {
348            snprintf(buf, bufSz, i ? sFmt2.c_str() : sFmt1.c_str(), i+1);
349            s += buf;
350        }
351        m_lineNrBuffer->remove_all_tags(m_lineNrBuffer->begin(), m_lineNrBuffer->end());
352        m_lineNrBuffer->set_text(s);
353        m_lineNrBuffer->apply_tag(m_lineNrTag, m_lineNrBuffer->begin(), m_lineNrBuffer->end());
354        if (buf) delete[] buf;
355    }
356    
357  void ScriptEditor::onTextInserted(const Gtk::TextBuffer::iterator& itEnd, const Glib::ustring& txt, int length) {  void ScriptEditor::onTextInserted(const Gtk::TextBuffer::iterator& itEnd, const Glib::ustring& txt, int length) {
358      //printf("onTextInserted()\n");      //printf("onTextInserted()\n");
359  #if USE_LS_SCRIPTVM  #if USE_LS_SCRIPTVM
# Line 312  void ScriptEditor::onTextInserted(const Line 404  void ScriptEditor::onTextInserted(const
404      ;      ;
405            
406  #endif // USE_LS_SCRIPTVM  #endif // USE_LS_SCRIPTVM
407        updateLineNumbers();
408  }  }
409    
410  #if USE_LS_SCRIPTVM  #if USE_LS_SCRIPTVM
# Line 321  LinuxSampler::ScriptVM* ScriptEditor::Ge Line 414  LinuxSampler::ScriptVM* ScriptEditor::Ge
414      return m_vm;      return m_vm;
415  }  }
416    
417  static void getIteratorsForIssue(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::ParserIssue& issue, Gtk::TextBuffer::iterator& start, Gtk::TextBuffer::iterator& end) {  template<class T>
418      start = txtbuf->get_iter_at_line_index(issue.firstLine - 1, issue.firstColumn - 1);  static void getIteratorsForIssue(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const T& issue, Gtk::TextBuffer::iterator& start, Gtk::TextBuffer::iterator& end) {
419        Gtk::TextBuffer::iterator itLine =
420            txtbuf->get_iter_at_line_index(issue.firstLine - 1, 0);
421        const int charsInLine = itLine.get_bytes_in_line();
422        start = txtbuf->get_iter_at_line_index(
423            issue.firstLine - 1,
424            // check we are not getting past the end of the line here, otherwise Gtk crashes
425            issue.firstColumn - 1 < charsInLine ? issue.firstColumn - 1 : charsInLine - 1
426        );
427      end = start;      end = start;
428      end.forward_lines(issue.lastLine - issue.firstLine);      end.forward_lines(issue.lastLine - issue.firstLine);
429      end.forward_chars(      end.forward_chars(
# Line 333  static void getIteratorsForIssue(Glib::R Line 434  static void getIteratorsForIssue(Glib::R
434  }  }
435    
436  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) {
437      Gtk::TextBuffer::iterator itStart =      Gtk::TextBuffer::iterator itLine =
438          txtbuf->get_iter_at_line_index(token.firstLine(), token.firstColumn());          txtbuf->get_iter_at_line_index(token.firstLine(), 0);
439        const int charsInLine = itLine.get_bytes_in_line();
440        Gtk::TextBuffer::iterator itStart = txtbuf->get_iter_at_line_index(
441            token.firstLine(),
442            // check we are not getting past the end of the line here, otherwise Gtk crashes
443            token.firstColumn() < charsInLine ? token.firstColumn() : charsInLine - 1
444        );
445      Gtk::TextBuffer::iterator itEnd = itStart;      Gtk::TextBuffer::iterator itEnd = itStart;
446      const int length = token.text().length();      const int length = token.text().length();
447      itEnd.forward_chars(length);      itEnd.forward_chars(length);
# Line 347  static void applyCodeTag(Glib::RefPtr<Gt Line 454  static void applyCodeTag(Glib::RefPtr<Gt
454      txtbuf->apply_tag(tag, itStart, itEnd);      txtbuf->apply_tag(tag, itStart, itEnd);
455  }  }
456    
457    static void applyPreprocessorComment(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::CodeBlock& block, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
458        Gtk::TextBuffer::iterator itStart, itEnd;
459        getIteratorsForIssue(txtbuf, block, itStart, itEnd);
460        txtbuf->apply_tag(tag, itStart, itEnd);
461    }
462    
463  void ScriptEditor::updateSyntaxHighlightingByVM() {  void ScriptEditor::updateSyntaxHighlightingByVM() {
464      GetScriptVM();      GetScriptVM();
465      const std::string s = m_textBuffer->get_text();      const std::string s = m_textBuffer->get_text();
466        if (s.empty()) return;
467      std::vector<LinuxSampler::VMSourceToken> tokens = m_vm->syntaxHighlighting(s);      std::vector<LinuxSampler::VMSourceToken> tokens = m_vm->syntaxHighlighting(s);
468    
469      for (int i = 0; i < tokens.size(); ++i) {      for (int i = 0; i < tokens.size(); ++i) {
# Line 385  void ScriptEditor::updateParserIssuesByV Line 499  void ScriptEditor::updateParserIssuesByV
499      m_issues = parserContext->issues();      m_issues = parserContext->issues();
500      m_errors = parserContext->errors();      m_errors = parserContext->errors();
501      m_warnings = parserContext->warnings();      m_warnings = parserContext->warnings();
502        m_preprocComments = parserContext->preprocessorComments();
503    
504      for (int i = 0; i < m_issues.size(); ++i) {      if (!s.empty()) {
505          const LinuxSampler::ParserIssue& issue = m_issues[i];          for (int i = 0; i < m_issues.size(); ++i) {
506                const LinuxSampler::ParserIssue& issue = m_issues[i];
507          if (issue.isErr()) {  
508              applyCodeTag(m_textBuffer, issue, m_errorTag);              if (issue.isErr()) {
509          } else if (issue.isWrn()) {                  applyCodeTag(m_textBuffer, issue, m_errorTag);
510              applyCodeTag(m_textBuffer, issue, m_warningTag);              } else if (issue.isWrn()) {
511                    applyCodeTag(m_textBuffer, issue, m_warningTag);
512                }
513          }          }
514      }      }
515    
516        for (int i = 0; i < m_preprocComments.size(); ++i) {
517            applyPreprocessorComment(m_textBuffer, m_preprocComments[i],
518                                     m_preprocCommentTag);
519        }
520    
521      delete parserContext;      delete parserContext;
522  }  }
523    
# Line 429  void ScriptEditor::updateIssueTooltip(Gd Line 551  void ScriptEditor::updateIssueTooltip(Gd
551          }          }
552      }      }
553    
554        for (int i = 0; i < m_preprocComments.size(); ++i) {
555            const LinuxSampler::CodeBlock& block = m_preprocComments[i];
556            const int firstLine   = block.firstLine - 1;
557            const int firstColumn = block.firstColumn - 1;
558            const int lastLine    = block.lastLine - 1;
559            const int lastColumn  = block.lastColumn - 1;
560            if (firstLine  <= line && line <= lastLine &&
561                (firstLine != line || firstColumn <= column) &&
562                (lastLine  != line || lastColumn  >= column))
563            {
564                m_textView.set_tooltip_markup(
565                    "Code block filtered out by preceding <span foreground=\"" PREPROC_TOKEN_COLOR "\">preprocessor</span> statement."
566                );
567                return;
568            }
569        }
570    
571      m_textView.set_tooltip_markup("");      m_textView.set_tooltip_markup("");
572  }  }
573    
# Line 491  void ScriptEditor::onTextErased(const Gt Line 630  void ScriptEditor::onTextErased(const Gt
630    
631      m_textBuffer->remove_all_tags(itStart2, itEnd2);      m_textBuffer->remove_all_tags(itStart2, itEnd2);
632  #endif // USE_LS_SCRIPTVM  #endif // USE_LS_SCRIPTVM
633        updateLineNumbers();
634  }  }
635    
636  bool ScriptEditor::on_motion_notify_event(GdkEventMotion* e) {  bool ScriptEditor::on_motion_notify_event(GdkEventMotion* e) {
# Line 501  bool ScriptEditor::on_motion_notify_even Line 641  bool ScriptEditor::on_motion_notify_even
641      return ManagedWindow::on_motion_notify_event(e);      return ManagedWindow::on_motion_notify_event(e);
642  }  }
643    
644    void ScriptEditor::onMenuChangeFontSize() {
645        //TODO: for GTKMM >= 3.2 class Gtk::FontChooser could be used instead
646        Gtk::Dialog dialog(_("Font Size"), true /*modal*/);
647        Gtk::HBox hbox;
648        hbox.set_spacing(6);
649    
650        Gtk::Label label(_("Editor's Font Size:"), Gtk::ALIGN_START);
651        hbox.pack_start(label, Gtk::PACK_SHRINK);
652    
653        Gtk::SpinButton spinButton;
654        spinButton.set_range(4, 80);
655        spinButton.set_increments(1, 10);
656        spinButton.set_value(currentFontSize());
657        hbox.pack_start(spinButton);
658    
659        dialog.get_vbox()->pack_start(hbox);
660        dialog.add_button(_("_OK"), 0);
661        dialog.add_button(_("_Cancel"), 1);
662    
663        dialog.show_all_children();
664    
665        if (!dialog.run()) { // OK selected ...
666            const int newFontSize = spinButton.get_value_as_int();
667            if (newFontSize >= 4)
668                setFontSize(newFontSize, true);
669        }
670    }
671    
672  bool ScriptEditor::onWindowDelete(GdkEventAny* e) {  bool ScriptEditor::onWindowDelete(GdkEventAny* e) {
673      //printf("onWindowDelete\n");      //printf("onWindowDelete\n");
674    

Legend:
Removed from v.2928  
changed lines
  Added in v.3314

  ViewVC Help
Powered by ViewVC