/[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 2845 by persson, Sun Sep 20 10:18:22 2015 UTC revision 2886 by schoenebeck, Fri Apr 22 15:55:08 2016 UTC
# Line 1  Line 1 
1  /*  /*
2      Copyright (c) 2014 Christian Schoenebeck      Copyright (c) 2014-2016 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    #if USE_LS_SCRIPTVM
11    # include <linuxsampler/scriptvm/ScriptVM.h>
12    #endif
13    
14    #if !USE_LS_SCRIPTVM
15    
16  static const std::string _keywords[] = {  static const std::string _keywords[] = {
17      "on", "end", "declare", "while", "if", "or", "and", "not", "else", "case",      "on", "end", "declare", "while", "if", "or", "and", "not", "else", "case",
# Line 31  static bool isEvent(const Glib::ustring& Line 36  static bool isEvent(const Glib::ustring&
36      return false;      return false;
37  }  }
38    
39    #endif // !USE_LS_SCRIPTVM
40    
41  ScriptEditor::ScriptEditor() :  ScriptEditor::ScriptEditor() :
42      m_applyButton(_("_Apply"), true),      m_applyButton(_("_Apply"), true),
43      m_cancelButton(_("_Cancel"), true)      m_cancelButton(_("_Cancel"), true)
44  {  {
45      m_script = NULL;      m_script = NULL;
46        m_vm = NULL;
47    
48      add(m_vbox);      add(m_vbox);
49    
50      m_tagTable = Gtk::TextBuffer::TagTable::create();      m_tagTable = Gtk::TextBuffer::TagTable::create();
51    
52      m_keywordTag = Gtk::TextBuffer::Tag::create();      m_keywordTag = Gtk::TextBuffer::Tag::create();
53      m_keywordTag->property_weight() = PANGO_WEIGHT_BOLD;      m_keywordTag->property_weight() = PANGO_WEIGHT_BOLD;
54      m_tagTable->add(m_keywordTag);      m_tagTable->add(m_keywordTag);
55    
56      m_eventTag = Gtk::TextBuffer::Tag::create();      m_eventTag = Gtk::TextBuffer::Tag::create();
57      m_eventTag->property_foreground() = "blue";      m_eventTag->property_foreground() = "blue";
58      m_eventTag->property_weight() = PANGO_WEIGHT_BOLD;      m_eventTag->property_weight() = PANGO_WEIGHT_BOLD;
59      m_tagTable->add(m_eventTag);      m_tagTable->add(m_eventTag);
60        
61        m_variableTag = Gtk::TextBuffer::Tag::create();
62        m_variableTag->property_foreground() = "magenta";
63        m_tagTable->add(m_variableTag);
64        
65        m_functionTag = Gtk::TextBuffer::Tag::create();
66        m_functionTag->property_foreground() = "cyan";
67        m_tagTable->add(m_functionTag);
68        
69        m_numberTag = Gtk::TextBuffer::Tag::create();
70        m_numberTag->property_foreground() = "yellow";
71        m_tagTable->add(m_numberTag);
72    
73        m_stringTag = Gtk::TextBuffer::Tag::create();
74        m_stringTag->property_foreground() = "red";
75        m_tagTable->add(m_stringTag);
76    
77        m_commentTag = Gtk::TextBuffer::Tag::create();
78        m_commentTag->property_foreground() = "gray";
79        m_tagTable->add(m_commentTag);
80    
81        m_preprocTag = Gtk::TextBuffer::Tag::create();
82        m_preprocTag->property_foreground() = "green";
83        m_tagTable->add(m_preprocTag);
84    
85      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);
86      m_textView.set_buffer(m_textBuffer);      m_textView.set_buffer(m_textBuffer);
87      {      {
# Line 106  ScriptEditor::ScriptEditor() : Line 141  ScriptEditor::ScriptEditor() :
141    
142  ScriptEditor::~ScriptEditor() {  ScriptEditor::~ScriptEditor() {
143      printf("ScriptEditor destruct\n");      printf("ScriptEditor destruct\n");
144        if (m_vm) delete m_vm;
145  }  }
146    
147  void ScriptEditor::setScript(gig::Script* script) {  void ScriptEditor::setScript(gig::Script* script) {
# Line 124  void ScriptEditor::setScript(gig::Script Line 160  void ScriptEditor::setScript(gig::Script
160  }  }
161    
162  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) {
163    #if USE_LS_SCRIPTVM
164        updateSyntaxHighlightingByVM();
165    #else
166      //printf("inserted %d\n", length);      //printf("inserted %d\n", length);
167      Gtk::TextBuffer::iterator itStart = itEnd;      Gtk::TextBuffer::iterator itStart = itEnd;
168      itStart.backward_chars(length);      itStart.backward_chars(length);
# Line 164  void ScriptEditor::onTextInserted(const Line 203  void ScriptEditor::onTextInserted(const
203            
204      EOF_REACHED:      EOF_REACHED:
205      ;      ;
206        
207    #endif // USE_LS_SCRIPTVM
208  }  }
209    
210    #if USE_LS_SCRIPTVM
211    
212    static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::VMSourceToken& token, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
213        Gtk::TextBuffer::iterator itStart =
214            txtbuf->get_iter_at_line_index(token.firstLine(), token.firstColumn());
215        Gtk::TextBuffer::iterator itEnd = itStart;
216        const int length = token.text().length();
217        itEnd.forward_chars(length);
218        txtbuf->apply_tag(tag, itStart, itEnd);
219    }
220    
221    void ScriptEditor::updateSyntaxHighlightingByVM() {
222        if (!m_vm) m_vm = new LinuxSampler::ScriptVM();
223        const std::string s = m_textBuffer->get_text();
224        std::vector<LinuxSampler::VMSourceToken> tokens = m_vm->syntaxHighlighting(s);
225    
226        m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
227    
228        for (int i = 0; i < tokens.size(); ++i) {
229            const LinuxSampler::VMSourceToken& token = tokens[i];
230    
231            if (token.isKeyword()) {
232                applyCodeTag(m_textBuffer, token, m_keywordTag);
233            } else if (token.isVariableName()) {
234                applyCodeTag(m_textBuffer, token, m_variableTag);
235            } else if (token.isIdentifier()) {
236                if (token.isEventHandlerName()) {
237                    applyCodeTag(m_textBuffer, token, m_eventTag);
238                } else { // a function ...
239                    applyCodeTag(m_textBuffer, token, m_functionTag);
240                }
241            } else if (token.isNumberLiteral()) {
242                applyCodeTag(m_textBuffer, token, m_numberTag);
243            } else if (token.isStringLiteral()) {
244                applyCodeTag(m_textBuffer, token, m_stringTag);
245            } else if (token.isComment()) {
246                applyCodeTag(m_textBuffer, token, m_commentTag);
247            } else if (token.isPreprocessor()) {
248                applyCodeTag(m_textBuffer, token, m_preprocTag);
249            } else if (token.isNewLine()) {
250            }
251        }
252    }
253    
254    #endif // USE_LS_SCRIPTVM
255    
256  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) {
257      //printf("erased\n");      //printf("erased\n");
258    #if USE_LS_SCRIPTVM
259        updateSyntaxHighlightingByVM();
260    #else
261      Gtk::TextBuffer::iterator itStart2 = itStart;      Gtk::TextBuffer::iterator itStart2 = itStart;
262      if (itStart2.inside_word() || itStart2.ends_word())      if (itStart2.inside_word() || itStart2.ends_word())
263          itStart2.backward_word_start();          itStart2.backward_word_start();
# Line 176  void ScriptEditor::onTextErased(const Gt Line 266  void ScriptEditor::onTextErased(const Gt
266      if (itEnd2.inside_word()) itEnd2.forward_word_end();      if (itEnd2.inside_word()) itEnd2.forward_word_end();
267    
268      m_textBuffer->remove_all_tags(itStart2, itEnd2);      m_textBuffer->remove_all_tags(itStart2, itEnd2);
269    #endif // USE_LS_SCRIPTVM
270  }  }
271    
272  void ScriptEditor::onModifiedChanged() {  void ScriptEditor::onModifiedChanged() {

Legend:
Removed from v.2845  
changed lines
  Added in v.2886

  ViewVC Help
Powered by ViewVC