/[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 2887 by schoenebeck, Fri Apr 22 17:10:51 2016 UTC revision 2897 by schoenebeck, Sun May 1 20:20:06 2016 UTC
# Line 7  Line 7 
7    
8  #include "scripteditor.h"  #include "scripteditor.h"
9  #include "global.h"  #include "global.h"
 #if USE_LS_SCRIPTVM  
 # include <linuxsampler/scriptvm/ScriptVM.h>  
 #endif  
10    
11  #if !USE_LS_SCRIPTVM  #if !USE_LS_SCRIPTVM
12    
# Line 43  ScriptEditor::ScriptEditor() : Line 40  ScriptEditor::ScriptEditor() :
40      m_cancelButton(_("_Cancel"), true)      m_cancelButton(_("_Cancel"), true)
41  {  {
42      m_script = NULL;      m_script = NULL;
43    #if USE_LS_SCRIPTVM
44      m_vm = NULL;      m_vm = NULL;
45    #endif
46    
47      add(m_vbox);      add(m_vbox);
48    
# Line 83  ScriptEditor::ScriptEditor() : Line 82  ScriptEditor::ScriptEditor() :
82      m_preprocTag->property_foreground() = "#2f8a33"; // green      m_preprocTag->property_foreground() = "#2f8a33"; // green
83      m_tagTable->add(m_preprocTag);      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      {      {
# Line 142  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;      if (m_vm) delete m_vm;
154    #endif
155  }  }
156    
157  void ScriptEditor::setScript(gig::Script* script) {  void ScriptEditor::setScript(gig::Script* script) {
# Line 161  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  #if USE_LS_SCRIPTVM
175        m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
176      updateSyntaxHighlightingByVM();      updateSyntaxHighlightingByVM();
177        updateParserIssuesByVM();
178  #else  #else
179      //printf("inserted %d\n", length);      //printf("inserted %d\n", length);
180      Gtk::TextBuffer::iterator itStart = itEnd;      Gtk::TextBuffer::iterator itStart = itEnd;
# Line 210  void ScriptEditor::onTextInserted(const Line 222  void ScriptEditor::onTextInserted(const
222    
223  #if USE_LS_SCRIPTVM  #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) {  static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::VMSourceToken& token, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
242      Gtk::TextBuffer::iterator itStart =      Gtk::TextBuffer::iterator itStart =
243          txtbuf->get_iter_at_line_index(token.firstLine(), token.firstColumn());          txtbuf->get_iter_at_line_index(token.firstLine(), token.firstColumn());
# Line 219  static void applyCodeTag(Glib::RefPtr<Gt Line 247  static void applyCodeTag(Glib::RefPtr<Gt
247      txtbuf->apply_tag(tag, itStart, itEnd);      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() {  void ScriptEditor::updateSyntaxHighlightingByVM() {
257      if (!m_vm) m_vm = new LinuxSampler::ScriptVM();      GetScriptVM();
258      const std::string s = m_textBuffer->get_text();      const std::string s = m_textBuffer->get_text();
259      std::vector<LinuxSampler::VMSourceToken> tokens = m_vm->syntaxHighlighting(s);      std::vector<LinuxSampler::VMSourceToken> tokens = m_vm->syntaxHighlighting(s);
260    
     m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());  
   
261      for (int i = 0; i < tokens.size(); ++i) {      for (int i = 0; i < tokens.size(); ++i) {
262          const LinuxSampler::VMSourceToken& token = tokens[i];          const LinuxSampler::VMSourceToken& token = tokens[i];
263    
# Line 252  void ScriptEditor::updateSyntaxHighlight Line 284  void ScriptEditor::updateSyntaxHighlight
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  #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  #if USE_LS_SCRIPTVM
344        m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
345      updateSyntaxHighlightingByVM();      updateSyntaxHighlightingByVM();
346        updateParserIssuesByVM();
347  #else  #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())
# Line 270  void ScriptEditor::onTextErased(const Gt Line 356  void ScriptEditor::onTextErased(const Gt
356  #endif // USE_LS_SCRIPTVM  #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() {
368      m_applyButton.set_sensitive( m_textBuffer->get_modified() );      m_applyButton.set_sensitive( m_textBuffer->get_modified() );
369  }  }

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

  ViewVC Help
Powered by ViewVC