/[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 2898 by schoenebeck, Sun May 1 21:17:42 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 94  ScriptEditor::ScriptEditor() : Line 142  ScriptEditor::ScriptEditor() :
142          sigc::mem_fun(*this, &ScriptEditor::onWindowHide)          sigc::mem_fun(*this, &ScriptEditor::onWindowHide)
143      );      );
144    
145        signal_delete_event().connect(
146            sigc::mem_fun(*this, &ScriptEditor::onWindowDelete)
147        );
148    
149      show_all_children();      show_all_children();
150    
151      resize(460,300);      resize(460,300);
# Line 101  ScriptEditor::ScriptEditor() : Line 153  ScriptEditor::ScriptEditor() :
153    
154  ScriptEditor::~ScriptEditor() {  ScriptEditor::~ScriptEditor() {
155      printf("ScriptEditor destruct\n");      printf("ScriptEditor destruct\n");
156    #if USE_LS_SCRIPTVM
157        if (m_vm) delete m_vm;
158    #endif
159  }  }
160    
161  void ScriptEditor::setScript(gig::Script* script) {  void ScriptEditor::setScript(gig::Script* script) {
# Line 119  void ScriptEditor::setScript(gig::Script Line 174  void ScriptEditor::setScript(gig::Script
174  }  }
175    
176  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) {
177        //printf("onTextInserted()\n");
178    #if USE_LS_SCRIPTVM
179        m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
180        updateSyntaxHighlightingByVM();
181        updateParserIssuesByVM();
182    #else
183      //printf("inserted %d\n", length);      //printf("inserted %d\n", length);
184      Gtk::TextBuffer::iterator itStart = itEnd;      Gtk::TextBuffer::iterator itStart = itEnd;
185      itStart.backward_chars(length);      itStart.backward_chars(length);
# Line 159  void ScriptEditor::onTextInserted(const Line 220  void ScriptEditor::onTextInserted(const
220            
221      EOF_REACHED:      EOF_REACHED:
222      ;      ;
223        
224    #endif // USE_LS_SCRIPTVM
225    }
226    
227    #if USE_LS_SCRIPTVM
228    
229    LinuxSampler::ScriptVM* ScriptEditor::GetScriptVM() {
230        if (!m_vm) m_vm = LinuxSampler::ScriptVMFactory::Create("gig");
231        return m_vm;
232    }
233    
234    static void getIteratorsForIssue(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::ParserIssue& issue, Gtk::TextBuffer::iterator& start, Gtk::TextBuffer::iterator& end) {
235        start = txtbuf->get_iter_at_line_index(issue.firstLine - 1, issue.firstColumn - 1);
236        end = start;
237        end.forward_lines(issue.lastLine - issue.firstLine);
238        end.forward_chars(
239            (issue.lastLine != issue.firstLine)
240                ? issue.lastColumn - 1
241                : issue.lastColumn - issue.firstColumn + 1
242        );
243    }
244    
245    static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::VMSourceToken& token, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
246        Gtk::TextBuffer::iterator itStart =
247            txtbuf->get_iter_at_line_index(token.firstLine(), token.firstColumn());
248        Gtk::TextBuffer::iterator itEnd = itStart;
249        const int length = token.text().length();
250        itEnd.forward_chars(length);
251        txtbuf->apply_tag(tag, itStart, itEnd);
252    }
253    
254    static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::ParserIssue& issue, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
255        Gtk::TextBuffer::iterator itStart, itEnd;
256        getIteratorsForIssue(txtbuf, issue, itStart, itEnd);
257        txtbuf->apply_tag(tag, itStart, itEnd);
258    }
259    
260    void ScriptEditor::updateSyntaxHighlightingByVM() {
261        GetScriptVM();
262        const std::string s = m_textBuffer->get_text();
263        std::vector<LinuxSampler::VMSourceToken> tokens = m_vm->syntaxHighlighting(s);
264    
265        for (int i = 0; i < tokens.size(); ++i) {
266            const LinuxSampler::VMSourceToken& token = tokens[i];
267    
268            if (token.isKeyword()) {
269                applyCodeTag(m_textBuffer, token, m_keywordTag);
270            } else if (token.isVariableName()) {
271                applyCodeTag(m_textBuffer, token, m_variableTag);
272            } else if (token.isIdentifier()) {
273                if (token.isEventHandlerName()) {
274                    applyCodeTag(m_textBuffer, token, m_eventTag);
275                } else { // a function ...
276                    applyCodeTag(m_textBuffer, token, m_functionTag);
277                }
278            } else if (token.isNumberLiteral()) {
279                applyCodeTag(m_textBuffer, token, m_numberTag);
280            } else if (token.isStringLiteral()) {
281                applyCodeTag(m_textBuffer, token, m_stringTag);
282            } else if (token.isComment()) {
283                applyCodeTag(m_textBuffer, token, m_commentTag);
284            } else if (token.isPreprocessor()) {
285                applyCodeTag(m_textBuffer, token, m_preprocTag);
286            } else if (token.isNewLine()) {
287            }
288        }
289  }  }
290    
291    void ScriptEditor::updateParserIssuesByVM() {
292        GetScriptVM();
293        const std::string s = m_textBuffer->get_text();
294        LinuxSampler::VMParserContext* parserContext = m_vm->loadScript(s);
295        m_issues = parserContext->issues();
296    
297        for (int i = 0; i < m_issues.size(); ++i) {
298            const LinuxSampler::ParserIssue& issue = m_issues[i];
299    
300            if (issue.isErr()) {
301                applyCodeTag(m_textBuffer, issue, m_errorTag);
302            } else if (issue.isWrn()) {
303                applyCodeTag(m_textBuffer, issue, m_warningTag);
304            }
305        }
306    
307        delete parserContext;
308    }
309    
310    void ScriptEditor::updateIssueTooltip(GdkEventMotion* e) {
311        int x, y;
312        m_textView.window_to_buffer_coords(Gtk::TEXT_WINDOW_TEXT, int(e->x), int(e->y), x, y);
313    
314        Gtk::TextBuffer::iterator it;
315        m_textView.get_iter_at_location(it, x, y);
316        
317        const int line = it.get_line();
318        const int column = it.get_line_offset();
319    
320        //printf("mouse at l%d c%d\n", line, column);
321    
322        for (int i = 0; i < m_issues.size(); ++i) {
323            const LinuxSampler::ParserIssue& issue = m_issues[i];
324            const int firstLine   = issue.firstLine - 1;
325            const int firstColumn = issue.firstColumn - 1;
326            const int lastLine    = issue.lastLine - 1;
327            const int lastColumn  = issue.lastColumn - 1;
328            if (firstLine <= line && line <= lastLine &&
329                (firstLine != line || firstColumn <= column) &&
330                (lastLine  != line || lastColumn  >= column))
331            {
332                m_textView.set_tooltip_markup(
333                    (issue.isErr() ? "<span foreground=\"#ff9393\">ERROR:</span> " : "<span foreground=\"#c4950c\">Warning:</span> ") +
334                    issue.txt
335                );
336                return;
337            }
338        }
339    
340        m_textView.set_tooltip_markup("");
341    }
342    
343    #endif // USE_LS_SCRIPTVM
344    
345  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) {
346      //printf("erased\n");      //printf("erased\n");
347    #if USE_LS_SCRIPTVM
348        m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
349        updateSyntaxHighlightingByVM();
350        updateParserIssuesByVM();
351    #else
352      Gtk::TextBuffer::iterator itStart2 = itStart;      Gtk::TextBuffer::iterator itStart2 = itStart;
353      if (itStart2.inside_word() || itStart2.ends_word())      if (itStart2.inside_word() || itStart2.ends_word())
354          itStart2.backward_word_start();          itStart2.backward_word_start();
# Line 171  void ScriptEditor::onTextErased(const Gt Line 357  void ScriptEditor::onTextErased(const Gt
357      if (itEnd2.inside_word()) itEnd2.forward_word_end();      if (itEnd2.inside_word()) itEnd2.forward_word_end();
358    
359      m_textBuffer->remove_all_tags(itStart2, itEnd2);      m_textBuffer->remove_all_tags(itStart2, itEnd2);
360    #endif // USE_LS_SCRIPTVM
361    }
362    
363    bool ScriptEditor::on_motion_notify_event(GdkEventMotion* e) {
364    #if USE_LS_SCRIPTVM
365        //TODO: event throttling would be a good idea here
366        updateIssueTooltip(e);
367    #endif
368        return ManagedWindow::on_motion_notify_event(e);
369    }
370    
371    bool ScriptEditor::onWindowDelete(GdkEventAny* e) {
372        //printf("onWindowDelete\n");
373    
374        if (!isModified()) return false; // propagate event further (which will close this window)
375    
376        gchar* msg = g_strdup_printf(_("Apply changes to instrument script \"%s\" before closing?"),
377                                     m_script->Name.c_str());
378        Gtk::MessageDialog dialog(*this, msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
379        g_free(msg);
380        dialog.set_secondary_text(_("If you close without applying, your changes will be lost."));
381        dialog.add_button(_("Close _Without Applying"), Gtk::RESPONSE_NO);
382        dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL);
383        dialog.add_button(_("_Apply"), Gtk::RESPONSE_YES);
384        dialog.set_default_response(Gtk::RESPONSE_YES);
385        int response = dialog.run();
386        dialog.hide();
387    
388        // user decided to close script editor without saving
389        if (response == Gtk::RESPONSE_NO)
390            return false; // propagate event further (which will close this window)
391    
392        // user cancelled dialog, thus don't close script editor
393        if (response == Gtk::RESPONSE_CANCEL) {
394            show();
395            return true; // drop event (prevents closing this window)
396        }
397    
398        // user wants to apply the changes, afterwards close window
399        if (response == Gtk::RESPONSE_YES) {
400            onButtonApply();
401            return false; // propagate event further (which will close this window)
402        }
403    
404        // should never ever make it to this point actually
405        return false;
406    }
407    
408    bool ScriptEditor::isModified() const {
409        return m_textBuffer->get_modified();
410  }  }
411    
412  void ScriptEditor::onModifiedChanged() {  void ScriptEditor::onModifiedChanged() {
413      m_applyButton.set_sensitive( m_textBuffer->get_modified() );      m_applyButton.set_sensitive(isModified());
414  }  }
415    
416  void ScriptEditor::onButtonCancel() {  void ScriptEditor::onButtonCancel() {
417        bool dropEvent = onWindowDelete(NULL);
418        if (dropEvent) return;
419      hide();      hide();
420  }  }
421    

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

  ViewVC Help
Powered by ViewVC