/[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 2896 by schoenebeck, Sun May 1 14:51:55 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        m_ignoreEraseEvents = false;
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_foreground() = "#000000"; // black
54      m_keywordTag->property_weight() = PANGO_WEIGHT_BOLD;      m_keywordTag->property_weight() = PANGO_WEIGHT_BOLD;
55      m_tagTable->add(m_keywordTag);      m_tagTable->add(m_keywordTag);
56    
57      m_eventTag = Gtk::TextBuffer::Tag::create();      m_eventTag = Gtk::TextBuffer::Tag::create();
58      m_eventTag->property_foreground() = "blue";      m_eventTag->property_foreground() = "#07c0cf"; // cyan 1
59      m_eventTag->property_weight() = PANGO_WEIGHT_BOLD;      m_eventTag->property_weight() = PANGO_WEIGHT_BOLD;
60      m_tagTable->add(m_eventTag);      m_tagTable->add(m_eventTag);
61        
62        m_variableTag = Gtk::TextBuffer::Tag::create();
63        m_variableTag->property_foreground() = "#790cc4"; // magenta
64        m_tagTable->add(m_variableTag);
65        
66        m_functionTag = Gtk::TextBuffer::Tag::create();
67        m_functionTag->property_foreground() = "#1ba1dd"; // cyan 2
68        m_tagTable->add(m_functionTag);
69        
70        m_numberTag = Gtk::TextBuffer::Tag::create();
71        m_numberTag->property_foreground() = "#c4950c"; // yellow
72        m_tagTable->add(m_numberTag);
73    
74        m_stringTag = Gtk::TextBuffer::Tag::create();
75        m_stringTag->property_foreground() = "#c40c0c"; // red
76        m_tagTable->add(m_stringTag);
77    
78        m_commentTag = Gtk::TextBuffer::Tag::create();
79        m_commentTag->property_foreground() = "#9c9c9c"; // gray
80        m_tagTable->add(m_commentTag);
81    
82        m_preprocTag = Gtk::TextBuffer::Tag::create();
83        m_preprocTag->property_foreground() = "#2f8a33"; // green
84        m_tagTable->add(m_preprocTag);
85    
86        m_errorTag = Gtk::TextBuffer::Tag::create();
87        m_errorTag->property_background() = "#ff9393"; // red
88        m_tagTable->add(m_errorTag);
89    
90        m_warningTag = Gtk::TextBuffer::Tag::create();
91        m_warningTag->property_background() = "#fffd7c"; // yellow
92        m_tagTable->add(m_warningTag);
93    
94        m_readOnlyTag = Gtk::TextBuffer::Tag::create();
95        m_readOnlyTag->property_editable() = false;
96        m_tagTable->add(m_readOnlyTag);
97    
98      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);
99      m_textView.set_buffer(m_textBuffer);      m_textView.set_buffer(m_textBuffer);
100      {      {
101          Pango::FontDescription fdesc;          Pango::FontDescription fdesc;
102          fdesc.set_family("monospace");          fdesc.set_family("monospace");
103    #if defined(__APPLE__)
104            fdesc.set_size(12 * PANGO_SCALE);
105    #else
106          fdesc.set_size(10 * PANGO_SCALE);          fdesc.set_size(10 * PANGO_SCALE);
107    #endif
108  #if GTKMM_MAJOR_VERSION < 3  #if GTKMM_MAJOR_VERSION < 3
109          m_textView.modify_font(fdesc);          m_textView.modify_font(fdesc);
110  #else  #else
# Line 101  ScriptEditor::ScriptEditor() : Line 154  ScriptEditor::ScriptEditor() :
154    
155  ScriptEditor::~ScriptEditor() {  ScriptEditor::~ScriptEditor() {
156      printf("ScriptEditor destruct\n");      printf("ScriptEditor destruct\n");
157    #if USE_LS_SCRIPTVM
158        if (m_vm) delete m_vm;
159    #endif
160  }  }
161    
162  void ScriptEditor::setScript(gig::Script* script) {  void ScriptEditor::setScript(gig::Script* script) {
# Line 119  void ScriptEditor::setScript(gig::Script Line 175  void ScriptEditor::setScript(gig::Script
175  }  }
176    
177  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) {
178        printf("onTextInserted()\n");
179        fflush(stdout);
180    #if USE_LS_SCRIPTVM
181        removeIssueAnchors();
182        m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
183        updateSyntaxHighlightingByVM();
184        updateParserIssuesByVM();
185    #else
186      //printf("inserted %d\n", length);      //printf("inserted %d\n", length);
187      Gtk::TextBuffer::iterator itStart = itEnd;      Gtk::TextBuffer::iterator itStart = itEnd;
188      itStart.backward_chars(length);      itStart.backward_chars(length);
# Line 159  void ScriptEditor::onTextInserted(const Line 223  void ScriptEditor::onTextInserted(const
223            
224      EOF_REACHED:      EOF_REACHED:
225      ;      ;
226        
227    #endif // USE_LS_SCRIPTVM
228    }
229    
230    #if USE_LS_SCRIPTVM
231    
232    LinuxSampler::ScriptVM* ScriptEditor::GetScriptVM() {
233        if (!m_vm) m_vm = LinuxSampler::ScriptVMFactory::Create("gig");
234        return m_vm;
235    }
236    
237    static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::VMSourceToken& token, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
238        Gtk::TextBuffer::iterator itStart =
239            txtbuf->get_iter_at_line_index(token.firstLine(), token.firstColumn());
240        Gtk::TextBuffer::iterator itEnd = itStart;
241        const int length = token.text().length();
242        itEnd.forward_chars(length);
243        txtbuf->apply_tag(tag, itStart, itEnd);
244    }
245    
246    static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::ParserIssue& issue, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
247        Gtk::TextBuffer::iterator itStart =
248            txtbuf->get_iter_at_line_index(issue.firstLine - 1, issue.firstColumn - 1);
249        Gtk::TextBuffer::iterator itEnd = itStart;
250        itEnd.forward_lines(issue.lastLine - issue.firstLine);
251        itEnd.forward_chars(
252            (issue.lastLine != issue.firstLine)
253                ? issue.lastColumn - 1
254                : issue.lastColumn - issue.firstColumn + 1
255        );
256        txtbuf->apply_tag(tag, itStart, itEnd);
257    }
258    
259    void ScriptEditor::removeIssueAnchors() {
260        m_ignoreEraseEvents = true; // avoid endless recursion
261        
262        for (int i = 0; i < m_issues.size(); ++i) {
263            const LinuxSampler::ParserIssue& issue = m_issues[i];
264            printf("erase anchor at l%d c%d\n", issue.firstLine - 1, issue.firstColumn - 1);
265            fflush(stdout);
266            Gtk::TextBuffer::iterator iter = m_textBuffer->get_iter_at_line_index(issue.firstLine - 1, issue.firstColumn - 1);
267            Gtk::TextBuffer::iterator iterEnd = iter;
268            iterEnd.forward_chars(1);
269            m_textBuffer->erase(iter, iterEnd);
270        }
271        
272        m_ignoreEraseEvents = false; // back to normal
273    }
274    
275    void ScriptEditor::updateSyntaxHighlightingByVM() {
276        GetScriptVM();
277        const std::string s = m_textBuffer->get_text();
278        std::vector<LinuxSampler::VMSourceToken> tokens = m_vm->syntaxHighlighting(s);
279    
280        for (int i = 0; i < tokens.size(); ++i) {
281            const LinuxSampler::VMSourceToken& token = tokens[i];
282    
283            if (token.isKeyword()) {
284                applyCodeTag(m_textBuffer, token, m_keywordTag);
285            } else if (token.isVariableName()) {
286                applyCodeTag(m_textBuffer, token, m_variableTag);
287            } else if (token.isIdentifier()) {
288                if (token.isEventHandlerName()) {
289                    applyCodeTag(m_textBuffer, token, m_eventTag);
290                } else { // a function ...
291                    applyCodeTag(m_textBuffer, token, m_functionTag);
292                }
293            } else if (token.isNumberLiteral()) {
294                applyCodeTag(m_textBuffer, token, m_numberTag);
295            } else if (token.isStringLiteral()) {
296                applyCodeTag(m_textBuffer, token, m_stringTag);
297            } else if (token.isComment()) {
298                applyCodeTag(m_textBuffer, token, m_commentTag);
299            } else if (token.isPreprocessor()) {
300                applyCodeTag(m_textBuffer, token, m_preprocTag);
301            } else if (token.isNewLine()) {
302            }
303        }
304    }
305    
306    static Glib::RefPtr<Gdk::Pixbuf> createIcon(std::string name, const Glib::RefPtr<Gdk::Screen>& screen) {
307        const int targetH = 9;
308        Glib::RefPtr<Gtk::IconTheme> theme = Gtk::IconTheme::get_for_screen(screen);
309        int w = 0;
310        int h = 0; // ignored
311        Gtk::IconSize::lookup(Gtk::ICON_SIZE_SMALL_TOOLBAR, w, h);
312        Glib::RefPtr<Gdk::Pixbuf> pixbuf = theme->load_icon(name, w, Gtk::ICON_LOOKUP_GENERIC_FALLBACK);
313        if (pixbuf->get_height() != targetH) {
314            pixbuf = pixbuf->scale_simple(targetH, targetH, Gdk::INTERP_BILINEAR);
315        }
316        return pixbuf;
317    }
318    
319    void ScriptEditor::updateParserIssuesByVM() {
320        GetScriptVM();
321        const std::string s = m_textBuffer->get_text();
322        LinuxSampler::VMParserContext* parserContext = m_vm->loadScript(s);
323        m_issues = parserContext->issues();
324    
325        for (int i = 0; i < m_issues.size(); ++i) {
326            const LinuxSampler::ParserIssue& issue = m_issues[i];
327    
328            if (issue.isErr()) {
329                applyCodeTag(m_textBuffer, issue, m_errorTag);
330            } else if (issue.isWrn()) {
331                applyCodeTag(m_textBuffer, issue, m_warningTag);
332            }
333        }
334    
335        for (int i = m_issues.size() - 1; i >= 0; --i) {
336            const LinuxSampler::ParserIssue& issue = m_issues[i];
337    
338            if (issue.isErr() || issue.isWrn()) {
339                Glib::RefPtr<Gdk::Pixbuf> pixbuf = createIcon(issue.isErr() ? "dialog-error" : "dialog-warning-symbolic", get_screen());
340                Gtk::Image* image = Gtk::manage(new Gtk::Image(pixbuf));
341                image->show();
342                Gtk::TextBuffer::iterator iter =
343                    m_textBuffer->get_iter_at_line_index(issue.firstLine - 1, issue.firstColumn - 1);
344                Glib::RefPtr<Gtk::TextChildAnchor> anchor = m_textBuffer->create_child_anchor(iter);
345                m_textView.add_child_at_anchor(*image, anchor);
346                
347                iter =
348                    m_textBuffer->get_iter_at_line_index(issue.firstLine - 1, issue.firstColumn - 1);
349                Gtk::TextBuffer::iterator itEnd = iter;
350                itEnd.forward_char();
351    
352                // prevent that the user can erase the icon with backspace key
353                m_textBuffer->apply_tag(m_readOnlyTag, iter, itEnd);
354            }
355        }
356    
357        delete parserContext;
358  }  }
359    
360    #endif // USE_LS_SCRIPTVM
361    
362  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) {
363      //printf("erased\n");      //printf("erased\n");
364        if (m_ignoreEraseEvents) return;
365    
366    #if USE_LS_SCRIPTVM
367        removeIssueAnchors();
368        m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
369        updateSyntaxHighlightingByVM();
370        updateParserIssuesByVM();
371    #else
372      Gtk::TextBuffer::iterator itStart2 = itStart;      Gtk::TextBuffer::iterator itStart2 = itStart;
373      if (itStart2.inside_word() || itStart2.ends_word())      if (itStart2.inside_word() || itStart2.ends_word())
374          itStart2.backward_word_start();          itStart2.backward_word_start();
# Line 171  void ScriptEditor::onTextErased(const Gt Line 377  void ScriptEditor::onTextErased(const Gt
377      if (itEnd2.inside_word()) itEnd2.forward_word_end();      if (itEnd2.inside_word()) itEnd2.forward_word_end();
378    
379      m_textBuffer->remove_all_tags(itStart2, itEnd2);      m_textBuffer->remove_all_tags(itStart2, itEnd2);
380    #endif // USE_LS_SCRIPTVM
381  }  }
382    
383  void ScriptEditor::onModifiedChanged() {  void ScriptEditor::onModifiedChanged() {

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

  ViewVC Help
Powered by ViewVC