/[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 3158 by schoenebeck, Mon May 8 18:05:35 2017 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 68  ScriptEditor::ScriptEditor() : Line 73  ScriptEditor::ScriptEditor() :
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 169  ScriptEditor::ScriptEditor() : Line 190  ScriptEditor::ScriptEditor() :
190          "</ui>"          "</ui>"
191      );      );
192    
193        m_lineNrBuffer = Gtk::TextBuffer::create(m_tagTable);
194        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            Gdk::Color color;
200            color.set("#F5F5F5");
201            GtkWidget* widget = (GtkWidget*) m_lineNrView.gobj();
202            gtk_widget_modify_base(widget, GTK_STATE_NORMAL, color.gobj());
203            gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, color.gobj());
204        }
205        {
206            Gdk::Color color;
207            color.set("#EEEEEE");
208            GtkWidget* widget = (GtkWidget*) m_lineNrTextViewSpacer.gobj();
209            gtk_widget_modify_base(widget, GTK_STATE_NORMAL, color.gobj());
210            gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, color.gobj());
211        }
212      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);
213      m_textView.set_buffer(m_textBuffer);      m_textView.set_buffer(m_textBuffer);
214        m_textView.set_left_margin(5);
215      setFontSize(currentFontSize(), false);      setFontSize(currentFontSize(), false);
216      m_scrolledWindow.add(m_textView);      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 242  ScriptEditor::~ScriptEditor() { Line 286  ScriptEditor::~ScriptEditor() {
286    
287  int ScriptEditor::currentFontSize() const {  int ScriptEditor::currentFontSize() const {
288  #if defined(__APPLE__)  #if defined(__APPLE__)
289      const int defaultFontSize = 13;      const int defaultFontSize = 11;
290  #else  #else
291      const int defaultFontSize = 10;      const int defaultFontSize = 10;
292  #endif  #endif
# Line 255  void ScriptEditor::setFontSize(int size, Line 299  void ScriptEditor::setFontSize(int size,
299      //printf("setFontSize(%d,%d)\n", size, save);      //printf("setFontSize(%d,%d)\n", size, save);
300      Pango::FontDescription fdesc;      Pango::FontDescription fdesc;
301      fdesc.set_family("monospace");      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);      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  #if GTKMM_MAJOR_VERSION < 3
313        m_lineNrView.modify_font(fdesc);
314      m_textView.modify_font(fdesc);      m_textView.modify_font(fdesc);
315  #else  #else
316        m_lineNrView.override_font(fdesc);
317      m_textView.override_font(fdesc);      m_textView.override_font(fdesc);
318  #endif  #endif
319      if (save) Settings::singleton()->scriptEditorFontSize = size;      if (save) Settings::singleton()->scriptEditorFontSize = size;
# Line 279  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 329  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 338  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 350  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 364  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();
# Line 403  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      if (!s.empty()) {      if (!s.empty()) {
505          for (int i = 0; i < m_issues.size(); ++i) {          for (int i = 0; i < m_issues.size(); ++i) {
# Line 416  void ScriptEditor::updateParserIssuesByV Line 513  void ScriptEditor::updateParserIssuesByV
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 449  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 511  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) {

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

  ViewVC Help
Powered by ViewVC