/[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 2610 by schoenebeck, Sun Jun 8 19:09:26 2014 UTC revision 2897 by schoenebeck, Sun May 1 20:20:06 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 8  Line 8 
8  #include "scripteditor.h"  #include "scripteditor.h"
9  #include "global.h"  #include "global.h"
10    
11    #if !USE_LS_SCRIPTVM
12    
13  static const std::string _keywords[] = {  static const std::string _keywords[] = {
14      "on", "end", "declare", "while", "if", "or", "and", "not", "else", "case",      "on", "end", "declare", "while", "if", "or", "and", "not", "else", "case",
15      "select", "to", "const", "polyphonic", "mod"      "select", "to", "const", "polyphonic", "mod"
# Line 31  static bool isEvent(const Glib::ustring& Line 33  static bool isEvent(const Glib::ustring&
33      return false;      return false;
34  }  }
35    
36    #endif // !USE_LS_SCRIPTVM
37    
38  ScriptEditor::ScriptEditor() :  ScriptEditor::ScriptEditor() :
39      m_applyButton(Gtk::Stock::APPLY), m_cancelButton(Gtk::Stock::CANCEL)      m_applyButton(_("_Apply"), true),
40        m_cancelButton(_("_Cancel"), true)
41  {  {
42      m_script = NULL;      m_script = NULL;
43    #if USE_LS_SCRIPTVM
44        m_vm = NULL;
45    #endif
46    
47      add(m_vbox);      add(m_vbox);
48    
49      m_tagTable = Gtk::TextBuffer::TagTable::create();      m_tagTable = Gtk::TextBuffer::TagTable::create();
50    
51      m_keywordTag = Gtk::TextBuffer::Tag::create();      m_keywordTag = Gtk::TextBuffer::Tag::create();
52        m_keywordTag->property_foreground() = "#000000"; // black
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() = "#07c0cf"; // cyan 1
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() = "#790cc4"; // magenta
63        m_tagTable->add(m_variableTag);
64        
65        m_functionTag = Gtk::TextBuffer::Tag::create();
66        m_functionTag->property_foreground() = "#1ba1dd"; // cyan 2
67        m_tagTable->add(m_functionTag);
68        
69        m_numberTag = Gtk::TextBuffer::Tag::create();
70        m_numberTag->property_foreground() = "#c4950c"; // yellow
71        m_tagTable->add(m_numberTag);
72    
73        m_stringTag = Gtk::TextBuffer::Tag::create();
74        m_stringTag->property_foreground() = "#c40c0c"; // red
75        m_tagTable->add(m_stringTag);
76    
77        m_commentTag = Gtk::TextBuffer::Tag::create();
78        m_commentTag->property_foreground() = "#9c9c9c"; // gray
79        m_tagTable->add(m_commentTag);
80    
81        m_preprocTag = Gtk::TextBuffer::Tag::create();
82        m_preprocTag->property_foreground() = "#2f8a33"; // green
83        m_tagTable->add(m_preprocTag);
84    
85        m_errorTag = Gtk::TextBuffer::Tag::create();
86        m_errorTag->property_background() = "#ff9393"; // red
87        m_tagTable->add(m_errorTag);
88    
89        m_warningTag = Gtk::TextBuffer::Tag::create();
90        m_warningTag->property_background() = "#fffd7c"; // yellow
91        m_tagTable->add(m_warningTag);
92    
93      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);
94      m_textView.set_buffer(m_textBuffer);      m_textView.set_buffer(m_textBuffer);
95      {      {
96          Pango::FontDescription fdesc;          Pango::FontDescription fdesc;
97          fdesc.set_family("monospace");          fdesc.set_family("monospace");
98    #if defined(__APPLE__)
99            fdesc.set_size(12 * PANGO_SCALE);
100    #else
101          fdesc.set_size(10 * PANGO_SCALE);          fdesc.set_size(10 * PANGO_SCALE);
102    #endif
103  #if GTKMM_MAJOR_VERSION < 3  #if GTKMM_MAJOR_VERSION < 3
104          m_textView.modify_font(fdesc);          m_textView.modify_font(fdesc);
105  #else  #else
# Line 101  ScriptEditor::ScriptEditor() : Line 149  ScriptEditor::ScriptEditor() :
149    
150  ScriptEditor::~ScriptEditor() {  ScriptEditor::~ScriptEditor() {
151      printf("ScriptEditor destruct\n");      printf("ScriptEditor destruct\n");
152    #if USE_LS_SCRIPTVM
153        if (m_vm) delete m_vm;
154    #endif
155  }  }
156    
157  void ScriptEditor::setScript(gig::Script* script) {  void ScriptEditor::setScript(gig::Script* script) {
# Line 119  void ScriptEditor::setScript(gig::Script Line 170  void ScriptEditor::setScript(gig::Script
170  }  }
171    
172  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) {
173        //printf("onTextInserted()\n");
174    #if USE_LS_SCRIPTVM
175        m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
176        updateSyntaxHighlightingByVM();
177        updateParserIssuesByVM();
178    #else
179      //printf("inserted %d\n", length);      //printf("inserted %d\n", length);
180      Gtk::TextBuffer::iterator itStart = itEnd;      Gtk::TextBuffer::iterator itStart = itEnd;
181      itStart.backward_chars(length);      itStart.backward_chars(length);
# Line 159  void ScriptEditor::onTextInserted(const Line 216  void ScriptEditor::onTextInserted(const
216            
217      EOF_REACHED:      EOF_REACHED:
218      ;      ;
219        
220    #endif // USE_LS_SCRIPTVM
221    }
222    
223    #if USE_LS_SCRIPTVM
224    
225    LinuxSampler::ScriptVM* ScriptEditor::GetScriptVM() {
226        if (!m_vm) m_vm = LinuxSampler::ScriptVMFactory::Create("gig");
227        return m_vm;
228    }
229    
230    static void getIteratorsForIssue(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::ParserIssue& issue, Gtk::TextBuffer::iterator& start, Gtk::TextBuffer::iterator& end) {
231        start = txtbuf->get_iter_at_line_index(issue.firstLine - 1, issue.firstColumn - 1);
232        end = start;
233        end.forward_lines(issue.lastLine - issue.firstLine);
234        end.forward_chars(
235            (issue.lastLine != issue.firstLine)
236                ? issue.lastColumn - 1
237                : issue.lastColumn - issue.firstColumn + 1
238        );
239    }
240    
241    static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::VMSourceToken& token, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
242        Gtk::TextBuffer::iterator itStart =
243            txtbuf->get_iter_at_line_index(token.firstLine(), token.firstColumn());
244        Gtk::TextBuffer::iterator itEnd = itStart;
245        const int length = token.text().length();
246        itEnd.forward_chars(length);
247        txtbuf->apply_tag(tag, itStart, itEnd);
248    }
249    
250    static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::ParserIssue& issue, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
251        Gtk::TextBuffer::iterator itStart, itEnd;
252        getIteratorsForIssue(txtbuf, issue, itStart, itEnd);
253        txtbuf->apply_tag(tag, itStart, itEnd);
254  }  }
255    
256    void ScriptEditor::updateSyntaxHighlightingByVM() {
257        GetScriptVM();
258        const std::string s = m_textBuffer->get_text();
259        std::vector<LinuxSampler::VMSourceToken> tokens = m_vm->syntaxHighlighting(s);
260    
261        for (int i = 0; i < tokens.size(); ++i) {
262            const LinuxSampler::VMSourceToken& token = tokens[i];
263    
264            if (token.isKeyword()) {
265                applyCodeTag(m_textBuffer, token, m_keywordTag);
266            } else if (token.isVariableName()) {
267                applyCodeTag(m_textBuffer, token, m_variableTag);
268            } else if (token.isIdentifier()) {
269                if (token.isEventHandlerName()) {
270                    applyCodeTag(m_textBuffer, token, m_eventTag);
271                } else { // a function ...
272                    applyCodeTag(m_textBuffer, token, m_functionTag);
273                }
274            } else if (token.isNumberLiteral()) {
275                applyCodeTag(m_textBuffer, token, m_numberTag);
276            } else if (token.isStringLiteral()) {
277                applyCodeTag(m_textBuffer, token, m_stringTag);
278            } else if (token.isComment()) {
279                applyCodeTag(m_textBuffer, token, m_commentTag);
280            } else if (token.isPreprocessor()) {
281                applyCodeTag(m_textBuffer, token, m_preprocTag);
282            } else if (token.isNewLine()) {
283            }
284        }
285    }
286    
287    void ScriptEditor::updateParserIssuesByVM() {
288        GetScriptVM();
289        const std::string s = m_textBuffer->get_text();
290        LinuxSampler::VMParserContext* parserContext = m_vm->loadScript(s);
291        m_issues = parserContext->issues();
292    
293        for (int i = 0; i < m_issues.size(); ++i) {
294            const LinuxSampler::ParserIssue& issue = m_issues[i];
295    
296            if (issue.isErr()) {
297                applyCodeTag(m_textBuffer, issue, m_errorTag);
298            } else if (issue.isWrn()) {
299                applyCodeTag(m_textBuffer, issue, m_warningTag);
300            }
301        }
302    
303        delete parserContext;
304    }
305    
306    void ScriptEditor::updateIssueTooltip(GdkEventMotion* e) {
307        int x, y;
308        m_textView.window_to_buffer_coords(Gtk::TEXT_WINDOW_TEXT, int(e->x), int(e->y), x, y);
309    
310        Gtk::TextBuffer::iterator it;
311        m_textView.get_iter_at_location(it, x, y);
312        
313        const int line = it.get_line();
314        const int column = it.get_line_offset();
315    
316        //printf("mouse at l%d c%d\n", line, column);
317    
318        for (int i = 0; i < m_issues.size(); ++i) {
319            const LinuxSampler::ParserIssue& issue = m_issues[i];
320            const int firstLine   = issue.firstLine - 1;
321            const int firstColumn = issue.firstColumn - 1;
322            const int lastLine    = issue.lastLine - 1;
323            const int lastColumn  = issue.lastColumn - 1;
324            if (firstLine <= line && line <= lastLine &&
325                (firstLine != line || firstColumn <= column) &&
326                (lastLine  != line || lastColumn  >= column))
327            {
328                m_textView.set_tooltip_markup(
329                    (issue.isErr() ? "<span foreground=\"#ff9393\">ERROR:</span> " : "<span foreground=\"#c4950c\">Warning:</span> ") +
330                    issue.txt
331                );
332                return;
333            }
334        }
335    
336        m_textView.set_tooltip_markup("");
337    }
338    
339    #endif // USE_LS_SCRIPTVM
340    
341  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) {
342      //printf("erased\n");      //printf("erased\n");
343    #if USE_LS_SCRIPTVM
344        m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
345        updateSyntaxHighlightingByVM();
346        updateParserIssuesByVM();
347    #else
348      Gtk::TextBuffer::iterator itStart2 = itStart;      Gtk::TextBuffer::iterator itStart2 = itStart;
349      if (itStart2.inside_word() || itStart2.ends_word())      if (itStart2.inside_word() || itStart2.ends_word())
350          itStart2.backward_word_start();          itStart2.backward_word_start();
# Line 171  void ScriptEditor::onTextErased(const Gt Line 353  void ScriptEditor::onTextErased(const Gt
353      if (itEnd2.inside_word()) itEnd2.forward_word_end();      if (itEnd2.inside_word()) itEnd2.forward_word_end();
354    
355      m_textBuffer->remove_all_tags(itStart2, itEnd2);      m_textBuffer->remove_all_tags(itStart2, itEnd2);
356    #endif // USE_LS_SCRIPTVM
357    }
358    
359    bool ScriptEditor::on_motion_notify_event(GdkEventMotion* e) {
360    #if USE_LS_SCRIPTVM
361        //TODO: event throttling would be a good idea here
362        updateIssueTooltip(e);
363    #endif
364        return ManagedWindow::on_motion_notify_event(e);
365  }  }
366    
367  void ScriptEditor::onModifiedChanged() {  void ScriptEditor::onModifiedChanged() {

Legend:
Removed from v.2610  
changed lines
  Added in v.2897

  ViewVC Help
Powered by ViewVC