/[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 2896 by schoenebeck, Sun May 1 14:51:55 2016 UTC revision 2900 by schoenebeck, Mon May 2 14:47:34 2016 UTC
# Line 35  static bool isEvent(const Glib::ustring& Line 35  static bool isEvent(const Glib::ustring&
35    
36  #endif // !USE_LS_SCRIPTVM  #endif // !USE_LS_SCRIPTVM
37    
38    static Glib::RefPtr<Gdk::Pixbuf> createIcon(std::string name, const Glib::RefPtr<Gdk::Screen>& screen) {
39        const int targetH = 16;
40        Glib::RefPtr<Gtk::IconTheme> theme = Gtk::IconTheme::get_for_screen(screen);
41        int w = 0;
42        int h = 0; // ignored
43        Gtk::IconSize::lookup(Gtk::ICON_SIZE_SMALL_TOOLBAR, w, h);
44        Glib::RefPtr<Gdk::Pixbuf> pixbuf = theme->load_icon(name, w, Gtk::ICON_LOOKUP_GENERIC_FALLBACK);
45        if (pixbuf->get_height() != targetH) {
46            pixbuf = pixbuf->scale_simple(targetH, targetH, Gdk::INTERP_BILINEAR);
47        }
48        return pixbuf;
49    }
50    
51  ScriptEditor::ScriptEditor() :  ScriptEditor::ScriptEditor() :
52        m_statusLabel("",  Gtk::ALIGN_START),
53      m_applyButton(_("_Apply"), true),      m_applyButton(_("_Apply"), true),
54      m_cancelButton(_("_Cancel"), true)      m_cancelButton(_("_Cancel"), true)
55  {  {
# Line 43  ScriptEditor::ScriptEditor() : Line 57  ScriptEditor::ScriptEditor() :
57  #if USE_LS_SCRIPTVM  #if USE_LS_SCRIPTVM
58      m_vm = NULL;      m_vm = NULL;
59  #endif  #endif
60      m_ignoreEraseEvents = false;  
61        m_errorIcon = createIcon("dialog-error", get_screen());
62        m_warningIcon = createIcon("dialog-warning-symbolic", get_screen());
63        m_successIcon = createIcon("emblem-default", get_screen());
64    
65      add(m_vbox);      add(m_vbox);
66    
# Line 91  ScriptEditor::ScriptEditor() : Line 108  ScriptEditor::ScriptEditor() :
108      m_warningTag->property_background() = "#fffd7c"; // yellow      m_warningTag->property_background() = "#fffd7c"; // yellow
109      m_tagTable->add(m_warningTag);      m_tagTable->add(m_warningTag);
110    
     m_readOnlyTag = Gtk::TextBuffer::Tag::create();  
     m_readOnlyTag->property_editable() = false;  
     m_tagTable->add(m_readOnlyTag);  
   
111      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);
112      m_textView.set_buffer(m_textBuffer);      m_textView.set_buffer(m_textBuffer);
113      {      {
# Line 121  ScriptEditor::ScriptEditor() : Line 134  ScriptEditor::ScriptEditor() :
134      m_applyButton.set_can_default();      m_applyButton.set_can_default();
135      m_applyButton.set_sensitive(false);      m_applyButton.set_sensitive(false);
136      m_applyButton.grab_focus();      m_applyButton.grab_focus();
137      m_vbox.pack_start(m_buttonBox, Gtk::PACK_SHRINK);      
138    #if GTKMM_MAJOR_VERSION >= 3
139        m_statusImage.set_margin_left(6);
140        m_statusImage.set_margin_right(6);
141    #else
142        m_statusHBox.set_spacing(6);
143    #endif
144    
145        m_statusHBox.pack_start(m_statusImage, Gtk::PACK_SHRINK);
146        m_statusHBox.pack_start(m_statusLabel);
147        m_statusHBox.show_all_children();
148    
149        m_footerHBox.pack_start(m_statusHBox);
150        m_footerHBox.pack_start(m_buttonBox, Gtk::PACK_SHRINK);
151    
152        m_vbox.pack_start(m_footerHBox, Gtk::PACK_SHRINK);
153    
154      m_applyButton.signal_clicked().connect(      m_applyButton.signal_clicked().connect(
155          sigc::mem_fun(*this, &ScriptEditor::onButtonApply)          sigc::mem_fun(*this, &ScriptEditor::onButtonApply)
# Line 147  ScriptEditor::ScriptEditor() : Line 175  ScriptEditor::ScriptEditor() :
175          sigc::mem_fun(*this, &ScriptEditor::onWindowHide)          sigc::mem_fun(*this, &ScriptEditor::onWindowHide)
176      );      );
177    
178        signal_delete_event().connect(
179            sigc::mem_fun(*this, &ScriptEditor::onWindowDelete)
180        );
181    
182      show_all_children();      show_all_children();
183    
184      resize(460,300);      resize(460,300);
# Line 175  void ScriptEditor::setScript(gig::Script Line 207  void ScriptEditor::setScript(gig::Script
207  }  }
208    
209  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) {
210      printf("onTextInserted()\n");      //printf("onTextInserted()\n");
     fflush(stdout);  
211  #if USE_LS_SCRIPTVM  #if USE_LS_SCRIPTVM
     removeIssueAnchors();  
212      m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());      m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
213      updateSyntaxHighlightingByVM();      updateSyntaxHighlightingByVM();
214      updateParserIssuesByVM();      updateParserIssuesByVM();
215        updateStatusBar();
216  #else  #else
217      //printf("inserted %d\n", length);      //printf("inserted %d\n", length);
218      Gtk::TextBuffer::iterator itStart = itEnd;      Gtk::TextBuffer::iterator itStart = itEnd;
# Line 234  LinuxSampler::ScriptVM* ScriptEditor::Ge Line 265  LinuxSampler::ScriptVM* ScriptEditor::Ge
265      return m_vm;      return m_vm;
266  }  }
267    
268    static void getIteratorsForIssue(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::ParserIssue& issue, Gtk::TextBuffer::iterator& start, Gtk::TextBuffer::iterator& end) {
269        start = txtbuf->get_iter_at_line_index(issue.firstLine - 1, issue.firstColumn - 1);
270        end = start;
271        end.forward_lines(issue.lastLine - issue.firstLine);
272        end.forward_chars(
273            (issue.lastLine != issue.firstLine)
274                ? issue.lastColumn - 1
275                : issue.lastColumn - issue.firstColumn + 1
276        );
277    }
278    
279  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) {
280      Gtk::TextBuffer::iterator itStart =      Gtk::TextBuffer::iterator itStart =
281          txtbuf->get_iter_at_line_index(token.firstLine(), token.firstColumn());          txtbuf->get_iter_at_line_index(token.firstLine(), token.firstColumn());
# Line 244  static void applyCodeTag(Glib::RefPtr<Gt Line 286  static void applyCodeTag(Glib::RefPtr<Gt
286  }  }
287    
288  static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::ParserIssue& issue, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {  static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::ParserIssue& issue, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
289      Gtk::TextBuffer::iterator itStart =      Gtk::TextBuffer::iterator itStart, itEnd;
290          txtbuf->get_iter_at_line_index(issue.firstLine - 1, issue.firstColumn - 1);      getIteratorsForIssue(txtbuf, issue, itStart, itEnd);
     Gtk::TextBuffer::iterator itEnd = itStart;  
     itEnd.forward_lines(issue.lastLine - issue.firstLine);  
     itEnd.forward_chars(  
         (issue.lastLine != issue.firstLine)  
             ? issue.lastColumn - 1  
             : issue.lastColumn - issue.firstColumn + 1  
     );  
291      txtbuf->apply_tag(tag, itStart, itEnd);      txtbuf->apply_tag(tag, itStart, itEnd);
292  }  }
293    
 void ScriptEditor::removeIssueAnchors() {  
     m_ignoreEraseEvents = true; // avoid endless recursion  
       
     for (int i = 0; i < m_issues.size(); ++i) {  
         const LinuxSampler::ParserIssue& issue = m_issues[i];  
         printf("erase anchor at l%d c%d\n", issue.firstLine - 1, issue.firstColumn - 1);  
         fflush(stdout);  
         Gtk::TextBuffer::iterator iter = m_textBuffer->get_iter_at_line_index(issue.firstLine - 1, issue.firstColumn - 1);  
         Gtk::TextBuffer::iterator iterEnd = iter;  
         iterEnd.forward_chars(1);  
         m_textBuffer->erase(iter, iterEnd);  
     }  
       
     m_ignoreEraseEvents = false; // back to normal  
 }  
   
294  void ScriptEditor::updateSyntaxHighlightingByVM() {  void ScriptEditor::updateSyntaxHighlightingByVM() {
295      GetScriptVM();      GetScriptVM();
296      const std::string s = m_textBuffer->get_text();      const std::string s = m_textBuffer->get_text();
# Line 303  void ScriptEditor::updateSyntaxHighlight Line 322  void ScriptEditor::updateSyntaxHighlight
322      }      }
323  }  }
324    
 static Glib::RefPtr<Gdk::Pixbuf> createIcon(std::string name, const Glib::RefPtr<Gdk::Screen>& screen) {  
     const int targetH = 9;  
     Glib::RefPtr<Gtk::IconTheme> theme = Gtk::IconTheme::get_for_screen(screen);  
     int w = 0;  
     int h = 0; // ignored  
     Gtk::IconSize::lookup(Gtk::ICON_SIZE_SMALL_TOOLBAR, w, h);  
     Glib::RefPtr<Gdk::Pixbuf> pixbuf = theme->load_icon(name, w, Gtk::ICON_LOOKUP_GENERIC_FALLBACK);  
     if (pixbuf->get_height() != targetH) {  
         pixbuf = pixbuf->scale_simple(targetH, targetH, Gdk::INTERP_BILINEAR);  
     }  
     return pixbuf;  
 }  
   
325  void ScriptEditor::updateParserIssuesByVM() {  void ScriptEditor::updateParserIssuesByVM() {
326      GetScriptVM();      GetScriptVM();
327      const std::string s = m_textBuffer->get_text();      const std::string s = m_textBuffer->get_text();
328      LinuxSampler::VMParserContext* parserContext = m_vm->loadScript(s);      LinuxSampler::VMParserContext* parserContext = m_vm->loadScript(s);
329      m_issues = parserContext->issues();      m_issues = parserContext->issues();
330        m_errors = parserContext->errors();
331        m_warnings = parserContext->warnings();
332    
333      for (int i = 0; i < m_issues.size(); ++i) {      for (int i = 0; i < m_issues.size(); ++i) {
334          const LinuxSampler::ParserIssue& issue = m_issues[i];          const LinuxSampler::ParserIssue& issue = m_issues[i];
# Line 332  void ScriptEditor::updateParserIssuesByV Line 340  void ScriptEditor::updateParserIssuesByV
340          }          }
341      }      }
342    
343      for (int i = m_issues.size() - 1; i >= 0; --i) {      delete parserContext;
344    }
345    
346    void ScriptEditor::updateIssueTooltip(GdkEventMotion* e) {
347        int x, y;
348        m_textView.window_to_buffer_coords(Gtk::TEXT_WINDOW_TEXT, int(e->x), int(e->y), x, y);
349    
350        Gtk::TextBuffer::iterator it;
351        m_textView.get_iter_at_location(it, x, y);
352        
353        const int line = it.get_line();
354        const int column = it.get_line_offset();
355    
356        //printf("mouse at l%d c%d\n", line, column);
357    
358        for (int i = 0; i < m_issues.size(); ++i) {
359          const LinuxSampler::ParserIssue& issue = m_issues[i];          const LinuxSampler::ParserIssue& issue = m_issues[i];
360            const int firstLine   = issue.firstLine - 1;
361            const int firstColumn = issue.firstColumn - 1;
362            const int lastLine    = issue.lastLine - 1;
363            const int lastColumn  = issue.lastColumn - 1;
364            if (firstLine <= line && line <= lastLine &&
365                (firstLine != line || firstColumn <= column) &&
366                (lastLine  != line || lastColumn  >= column))
367            {
368                m_textView.set_tooltip_markup(
369                    (issue.isErr() ? "<span foreground=\"#ff9393\">ERROR:</span> " : "<span foreground=\"#c4950c\">Warning:</span> ") +
370                    issue.txt
371                );
372                return;
373            }
374        }
375    
376          if (issue.isErr() || issue.isWrn()) {      m_textView.set_tooltip_markup("");
377              Glib::RefPtr<Gdk::Pixbuf> pixbuf = createIcon(issue.isErr() ? "dialog-error" : "dialog-warning-symbolic", get_screen());  }
             Gtk::Image* image = Gtk::manage(new Gtk::Image(pixbuf));  
             image->show();  
             Gtk::TextBuffer::iterator iter =  
                 m_textBuffer->get_iter_at_line_index(issue.firstLine - 1, issue.firstColumn - 1);  
             Glib::RefPtr<Gtk::TextChildAnchor> anchor = m_textBuffer->create_child_anchor(iter);  
             m_textView.add_child_at_anchor(*image, anchor);  
               
             iter =  
                 m_textBuffer->get_iter_at_line_index(issue.firstLine - 1, issue.firstColumn - 1);  
             Gtk::TextBuffer::iterator itEnd = iter;  
             itEnd.forward_char();  
378    
379              // prevent that the user can erase the icon with backspace key  static std::string warningsCountTxt(const std::vector<LinuxSampler::ParserIssue> warnings) {
380              m_textBuffer->apply_tag(m_readOnlyTag, iter, itEnd);      std::string txt = "<span foreground=\"#c4950c\">" + ToString(warnings.size());
381        txt += (warnings.size() == 1) ? " Warning" : " Warnings";
382        txt += "</span>";
383        return txt;
384    }
385    
386    static std::string errorsCountTxt(const std::vector<LinuxSampler::ParserIssue> errors) {
387        std::string txt = "<span foreground=\"#c40c0c\">" + ToString(errors.size());
388        txt += (errors.size() == 1) ? " Error" : " Errors";
389        txt += "</span>";
390        return txt;
391    }
392    
393    void ScriptEditor::updateStatusBar() {
394        // update status text
395        std::string txt;
396        if (m_issues.empty()) {
397            txt = "No issues with this script.";
398        } else {
399            const char* txtWontLoad = ". Sampler won't load instruments using this script!";
400            txt = "There ";
401            txt += (m_errors.size() <= 1 && m_warnings.size() <= 1) ? "is " : "are ";
402            if (m_errors.empty()) {
403                txt += warningsCountTxt(m_warnings) + ". Script will load, but might not behave as expected!";
404            } else if (m_warnings.empty()) {
405                txt += errorsCountTxt(m_errors) + txtWontLoad;
406            } else {
407                txt += errorsCountTxt(m_errors) + " and " +
408                       warningsCountTxt(m_warnings) + txtWontLoad;
409          }          }
410      }      }
411        m_statusLabel.set_markup(txt);
412    
413      delete parserContext;      // update status icon
414        m_statusImage.set(
415            m_issues.empty() ? m_successIcon : !m_errors.empty() ? m_errorIcon : m_warningIcon
416        );
417  }  }
418    
419  #endif // USE_LS_SCRIPTVM  #endif // USE_LS_SCRIPTVM
420    
421  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) {
422      //printf("erased\n");      //printf("erased\n");
     if (m_ignoreEraseEvents) return;  
   
423  #if USE_LS_SCRIPTVM  #if USE_LS_SCRIPTVM
     removeIssueAnchors();  
424      m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());      m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
425      updateSyntaxHighlightingByVM();      updateSyntaxHighlightingByVM();
426      updateParserIssuesByVM();      updateParserIssuesByVM();
427        updateStatusBar();
428  #else  #else
429      Gtk::TextBuffer::iterator itStart2 = itStart;      Gtk::TextBuffer::iterator itStart2 = itStart;
430      if (itStart2.inside_word() || itStart2.ends_word())      if (itStart2.inside_word() || itStart2.ends_word())
# Line 380  void ScriptEditor::onTextErased(const Gt Line 437  void ScriptEditor::onTextErased(const Gt
437  #endif // USE_LS_SCRIPTVM  #endif // USE_LS_SCRIPTVM
438  }  }
439    
440    bool ScriptEditor::on_motion_notify_event(GdkEventMotion* e) {
441    #if USE_LS_SCRIPTVM
442        //TODO: event throttling would be a good idea here
443        updateIssueTooltip(e);
444    #endif
445        return ManagedWindow::on_motion_notify_event(e);
446    }
447    
448    bool ScriptEditor::onWindowDelete(GdkEventAny* e) {
449        //printf("onWindowDelete\n");
450    
451        if (!isModified()) return false; // propagate event further (which will close this window)
452    
453        gchar* msg = g_strdup_printf(_("Apply changes to instrument script \"%s\" before closing?"),
454                                     m_script->Name.c_str());
455        Gtk::MessageDialog dialog(*this, msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
456        g_free(msg);
457        dialog.set_secondary_text(_("If you close without applying, your changes will be lost."));
458        dialog.add_button(_("Close _Without Applying"), Gtk::RESPONSE_NO);
459        dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL);
460        dialog.add_button(_("_Apply"), Gtk::RESPONSE_YES);
461        dialog.set_default_response(Gtk::RESPONSE_YES);
462        int response = dialog.run();
463        dialog.hide();
464    
465        // user decided to close script editor without saving
466        if (response == Gtk::RESPONSE_NO)
467            return false; // propagate event further (which will close this window)
468    
469        // user cancelled dialog, thus don't close script editor
470        if (response == Gtk::RESPONSE_CANCEL) {
471            show();
472            return true; // drop event (prevents closing this window)
473        }
474    
475        // user wants to apply the changes, afterwards close window
476        if (response == Gtk::RESPONSE_YES) {
477            onButtonApply();
478            return false; // propagate event further (which will close this window)
479        }
480    
481        // should never ever make it to this point actually
482        return false;
483    }
484    
485    bool ScriptEditor::isModified() const {
486        return m_textBuffer->get_modified();
487    }
488    
489  void ScriptEditor::onModifiedChanged() {  void ScriptEditor::onModifiedChanged() {
490      m_applyButton.set_sensitive( m_textBuffer->get_modified() );      m_applyButton.set_sensitive(isModified());
491    #if USE_LS_SCRIPTVM
492        updateStatusBar();
493    #endif
494  }  }
495    
496  void ScriptEditor::onButtonCancel() {  void ScriptEditor::onButtonCancel() {
497        bool dropEvent = onWindowDelete(NULL);
498        if (dropEvent) return;
499      hide();      hide();
500  }  }
501    

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

  ViewVC Help
Powered by ViewVC