/[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 2964 by schoenebeck, Sun Jul 17 22:02:36 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        if (!theme->has_icon(name))
45            return Glib::RefPtr<Gdk::Pixbuf>();
46        Glib::RefPtr<Gdk::Pixbuf> pixbuf = theme->load_icon(name, w, Gtk::ICON_LOOKUP_GENERIC_FALLBACK);
47        if (pixbuf->get_height() != targetH) {
48            pixbuf = pixbuf->scale_simple(targetH, targetH, Gdk::INTERP_BILINEAR);
49        }
50        return pixbuf;
51    }
52    
53    static Glib::RefPtr<Gdk::Pixbuf> createIcon(std::vector<std::string> alternativeNames, const Glib::RefPtr<Gdk::Screen>& screen) {
54        for (int i = 0; i < alternativeNames.size(); ++i) {
55            Glib::RefPtr<Gdk::Pixbuf> buf = createIcon(alternativeNames[i], screen);
56            if (buf) return buf;
57        }
58        return Glib::RefPtr<Gdk::Pixbuf>();
59    }
60    
61  ScriptEditor::ScriptEditor() :  ScriptEditor::ScriptEditor() :
62        m_statusLabel("",  Gtk::ALIGN_START),
63      m_applyButton(_("_Apply"), true),      m_applyButton(_("_Apply"), true),
64      m_cancelButton(_("_Cancel"), true)      m_cancelButton(_("_Cancel"), true)
65  {  {
# Line 43  ScriptEditor::ScriptEditor() : Line 67  ScriptEditor::ScriptEditor() :
67  #if USE_LS_SCRIPTVM  #if USE_LS_SCRIPTVM
68      m_vm = NULL;      m_vm = NULL;
69  #endif  #endif
70      m_ignoreEraseEvents = false;  
71        // depending on GTK version and installed themes, there may be different
72        // icons, and different names for them, so for each type of icon we use,
73        // we provide a list of possible icon names, the first one found to be
74        // installed on the local system from the list will be used and loaded for
75        // the respective purpose (so order matters in those lists)
76        //
77        // (see https://developer.gnome.org/gtkmm/stable/namespaceGtk_1_1Stock.html for
78        // available icon names)
79        std::vector<std::string> errorIconNames;
80        errorIconNames.push_back("dialog-error");
81        errorIconNames.push_back("media-record");
82        errorIconNames.push_back("process-stop");
83    
84        std::vector<std::string> warningIconNames;
85        warningIconNames.push_back("dialog-warning-symbolic");
86        warningIconNames.push_back("dialog-warning");
87    
88        std::vector<std::string> successIconNames;
89        successIconNames.push_back("emblem-default");
90        successIconNames.push_back("tools-check-spelling");
91    
92        m_errorIcon = createIcon(errorIconNames, get_screen());
93        m_warningIcon = createIcon(warningIconNames, get_screen());
94        m_successIcon = createIcon(successIconNames, get_screen());
95    
96      add(m_vbox);      add(m_vbox);
97    
# Line 91  ScriptEditor::ScriptEditor() : Line 139  ScriptEditor::ScriptEditor() :
139      m_warningTag->property_background() = "#fffd7c"; // yellow      m_warningTag->property_background() = "#fffd7c"; // yellow
140      m_tagTable->add(m_warningTag);      m_tagTable->add(m_warningTag);
141    
142      m_readOnlyTag = Gtk::TextBuffer::Tag::create();      // create menu
143      m_readOnlyTag->property_editable() = false;      m_actionGroup = Gtk::ActionGroup::create();
144      m_tagTable->add(m_readOnlyTag);      m_actionGroup->add(Gtk::Action::create("MenuScript", _("_Script")));
145        m_actionGroup->add(Gtk::Action::create("Apply", _("_Apply")),
146                           Gtk::AccelKey("<control>s"),
147                           sigc::mem_fun(*this, &ScriptEditor::onButtonApply));
148        m_actionGroup->add(Gtk::Action::create("Close", _("_Close")),
149                           Gtk::AccelKey("<control>q"),
150                           sigc::mem_fun(*this, &ScriptEditor::onButtonCancel));
151        m_actionGroup->add(Gtk::Action::create("MenuEditor", _("_Editor")));
152        m_actionGroup->add(Gtk::Action::create("ChangeFont", _("_Font Size ...")),
153                           sigc::mem_fun(*this, &ScriptEditor::onMenuChangeFontSize));
154        m_uiManager = Gtk::UIManager::create();
155        m_uiManager->insert_action_group(m_actionGroup);
156        add_accel_group(m_uiManager->get_accel_group());
157        m_uiManager->add_ui_from_string(
158            "<ui>"
159            "  <menubar name='MenuBar'>"
160            "    <menu action='MenuScript'>"
161            "      <menuitem action='Apply'/>"
162            "      <separator/>"
163            "      <menuitem action='Close'/>"
164            "    </menu>"
165            "    <menu action='MenuEditor'>"
166            "      <menuitem action='ChangeFont'/>"
167            "    </menu>"
168            "  </menubar>"
169            "</ui>"
170        );
171    
172      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);
173      m_textView.set_buffer(m_textBuffer);      m_textView.set_buffer(m_textBuffer);
174      {      setFontSize(currentFontSize(), false);
         Pango::FontDescription fdesc;  
         fdesc.set_family("monospace");  
 #if defined(__APPLE__)  
         fdesc.set_size(12 * PANGO_SCALE);  
 #else  
         fdesc.set_size(10 * PANGO_SCALE);  
 #endif  
 #if GTKMM_MAJOR_VERSION < 3  
         m_textView.modify_font(fdesc);  
 #else  
         m_textView.override_font(fdesc);  
 #endif  
     }  
175      m_scrolledWindow.add(m_textView);      m_scrolledWindow.add(m_textView);
176      m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);      m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
177    
178        Gtk::Widget* menuBar = m_uiManager->get_widget("/MenuBar");
179        m_vbox.pack_start(*menuBar, Gtk::PACK_SHRINK);
180      m_vbox.pack_start(m_scrolledWindow);      m_vbox.pack_start(m_scrolledWindow);
181    
182      m_buttonBox.set_layout(Gtk::BUTTONBOX_END);      m_buttonBox.set_layout(Gtk::BUTTONBOX_END);
# Line 121  ScriptEditor::ScriptEditor() : Line 185  ScriptEditor::ScriptEditor() :
185      m_applyButton.set_can_default();      m_applyButton.set_can_default();
186      m_applyButton.set_sensitive(false);      m_applyButton.set_sensitive(false);
187      m_applyButton.grab_focus();      m_applyButton.grab_focus();
188      m_vbox.pack_start(m_buttonBox, Gtk::PACK_SHRINK);  
189    #if GTKMM_MAJOR_VERSION >= 3
190        m_statusImage.set_margin_left(6);
191        m_statusImage.set_margin_right(6);
192    #else
193        m_statusHBox.set_spacing(6);
194    #endif
195    
196        m_statusHBox.pack_start(m_statusImage, Gtk::PACK_SHRINK);
197        m_statusHBox.pack_start(m_statusLabel);
198        m_statusHBox.show_all_children();
199    
200        m_footerHBox.pack_start(m_statusHBox);
201        m_footerHBox.pack_start(m_buttonBox, Gtk::PACK_SHRINK);
202    
203        m_vbox.pack_start(m_footerHBox, Gtk::PACK_SHRINK);
204    
205      m_applyButton.signal_clicked().connect(      m_applyButton.signal_clicked().connect(
206          sigc::mem_fun(*this, &ScriptEditor::onButtonApply)          sigc::mem_fun(*this, &ScriptEditor::onButtonApply)
# Line 147  ScriptEditor::ScriptEditor() : Line 226  ScriptEditor::ScriptEditor() :
226          sigc::mem_fun(*this, &ScriptEditor::onWindowHide)          sigc::mem_fun(*this, &ScriptEditor::onWindowHide)
227      );      );
228    
229      show_all_children();      signal_delete_event().connect(
230            sigc::mem_fun(*this, &ScriptEditor::onWindowDelete)
231        );
232    
233      resize(460,300);      show_all_children();
234  }  }
235    
236  ScriptEditor::~ScriptEditor() {  ScriptEditor::~ScriptEditor() {
# Line 159  ScriptEditor::~ScriptEditor() { Line 240  ScriptEditor::~ScriptEditor() {
240  #endif  #endif
241  }  }
242    
243    int ScriptEditor::currentFontSize() const {
244    #if defined(__APPLE__)
245        const int defaultFontSize = 13;
246    #else
247        const int defaultFontSize = 10;
248    #endif
249        const int settingFontSize = Settings::singleton()->scriptEditorFontSize;
250        const int fontSize = (settingFontSize > 0) ? settingFontSize : defaultFontSize;
251        return fontSize;
252    }
253    
254    void ScriptEditor::setFontSize(int size, bool save) {
255        //printf("setFontSize(%d,%d)\n", size, save);
256        Pango::FontDescription fdesc;
257        fdesc.set_family("monospace");
258        fdesc.set_size(size * PANGO_SCALE);
259    #if GTKMM_MAJOR_VERSION < 3
260        m_textView.modify_font(fdesc);
261    #else
262        m_textView.override_font(fdesc);
263    #endif
264        if (save) Settings::singleton()->scriptEditorFontSize = size;
265    }
266    
267  void ScriptEditor::setScript(gig::Script* script) {  void ScriptEditor::setScript(gig::Script* script) {
268      m_script = script;      m_script = script;
269      if (!script) {      if (!script) {
# Line 175  void ScriptEditor::setScript(gig::Script Line 280  void ScriptEditor::setScript(gig::Script
280  }  }
281    
282  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) {
283      printf("onTextInserted()\n");      //printf("onTextInserted()\n");
     fflush(stdout);  
284  #if USE_LS_SCRIPTVM  #if USE_LS_SCRIPTVM
     removeIssueAnchors();  
285      m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());      m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
286      updateSyntaxHighlightingByVM();      updateSyntaxHighlightingByVM();
287      updateParserIssuesByVM();      updateParserIssuesByVM();
288        updateStatusBar();
289  #else  #else
290      //printf("inserted %d\n", length);      //printf("inserted %d\n", length);
291      Gtk::TextBuffer::iterator itStart = itEnd;      Gtk::TextBuffer::iterator itStart = itEnd;
# Line 234  LinuxSampler::ScriptVM* ScriptEditor::Ge Line 338  LinuxSampler::ScriptVM* ScriptEditor::Ge
338      return m_vm;      return m_vm;
339  }  }
340    
341    static void getIteratorsForIssue(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::ParserIssue& issue, Gtk::TextBuffer::iterator& start, Gtk::TextBuffer::iterator& end) {
342        start = txtbuf->get_iter_at_line_index(issue.firstLine - 1, issue.firstColumn - 1);
343        end = start;
344        end.forward_lines(issue.lastLine - issue.firstLine);
345        end.forward_chars(
346            (issue.lastLine != issue.firstLine)
347                ? issue.lastColumn - 1
348                : issue.lastColumn - issue.firstColumn + 1
349        );
350    }
351    
352  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) {
353      Gtk::TextBuffer::iterator itStart =      Gtk::TextBuffer::iterator itStart =
354          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 359  static void applyCodeTag(Glib::RefPtr<Gt
359  }  }
360    
361  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) {
362      Gtk::TextBuffer::iterator itStart =      Gtk::TextBuffer::iterator itStart, itEnd;
363          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  
     );  
364      txtbuf->apply_tag(tag, itStart, itEnd);      txtbuf->apply_tag(tag, itStart, itEnd);
365  }  }
366    
 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  
 }  
   
367  void ScriptEditor::updateSyntaxHighlightingByVM() {  void ScriptEditor::updateSyntaxHighlightingByVM() {
368      GetScriptVM();      GetScriptVM();
369      const std::string s = m_textBuffer->get_text();      const std::string s = m_textBuffer->get_text();
# Line 303  void ScriptEditor::updateSyntaxHighlight Line 395  void ScriptEditor::updateSyntaxHighlight
395      }      }
396  }  }
397    
 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;  
 }  
   
398  void ScriptEditor::updateParserIssuesByVM() {  void ScriptEditor::updateParserIssuesByVM() {
399      GetScriptVM();      GetScriptVM();
400      const std::string s = m_textBuffer->get_text();      const std::string s = m_textBuffer->get_text();
401      LinuxSampler::VMParserContext* parserContext = m_vm->loadScript(s);      LinuxSampler::VMParserContext* parserContext = m_vm->loadScript(s);
402      m_issues = parserContext->issues();      m_issues = parserContext->issues();
403        m_errors = parserContext->errors();
404        m_warnings = parserContext->warnings();
405    
406      for (int i = 0; i < m_issues.size(); ++i) {      for (int i = 0; i < m_issues.size(); ++i) {
407          const LinuxSampler::ParserIssue& issue = m_issues[i];          const LinuxSampler::ParserIssue& issue = m_issues[i];
# Line 332  void ScriptEditor::updateParserIssuesByV Line 413  void ScriptEditor::updateParserIssuesByV
413          }          }
414      }      }
415    
416      for (int i = m_issues.size() - 1; i >= 0; --i) {      delete parserContext;
417    }
418    
419    void ScriptEditor::updateIssueTooltip(GdkEventMotion* e) {
420        int x, y;
421        m_textView.window_to_buffer_coords(Gtk::TEXT_WINDOW_TEXT, int(e->x), int(e->y), x, y);
422    
423        Gtk::TextBuffer::iterator it;
424        m_textView.get_iter_at_location(it, x, y);
425        
426        const int line = it.get_line();
427        const int column = it.get_line_offset();
428    
429        //printf("mouse at l%d c%d\n", line, column);
430    
431        for (int i = 0; i < m_issues.size(); ++i) {
432          const LinuxSampler::ParserIssue& issue = m_issues[i];          const LinuxSampler::ParserIssue& issue = m_issues[i];
433            const int firstLine   = issue.firstLine - 1;
434            const int firstColumn = issue.firstColumn - 1;
435            const int lastLine    = issue.lastLine - 1;
436            const int lastColumn  = issue.lastColumn - 1;
437            if (firstLine <= line && line <= lastLine &&
438                (firstLine != line || firstColumn <= column) &&
439                (lastLine  != line || lastColumn  >= column))
440            {
441                m_textView.set_tooltip_markup(
442                    (issue.isErr() ? "<span foreground=\"#ff9393\">ERROR:</span> " : "<span foreground=\"#c4950c\">Warning:</span> ") +
443                    issue.txt
444                );
445                return;
446            }
447        }
448    
449          if (issue.isErr() || issue.isWrn()) {      m_textView.set_tooltip_markup("");
450              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();  
451    
452              // prevent that the user can erase the icon with backspace key  static std::string warningsCountTxt(const std::vector<LinuxSampler::ParserIssue> warnings) {
453              m_textBuffer->apply_tag(m_readOnlyTag, iter, itEnd);      std::string txt = "<span foreground=\"#c4950c\">" + ToString(warnings.size());
454        txt += (warnings.size() == 1) ? " Warning" : " Warnings";
455        txt += "</span>";
456        return txt;
457    }
458    
459    static std::string errorsCountTxt(const std::vector<LinuxSampler::ParserIssue> errors) {
460        std::string txt = "<span foreground=\"#c40c0c\">" + ToString(errors.size());
461        txt += (errors.size() == 1) ? " Error" : " Errors";
462        txt += "</span>";
463        return txt;
464    }
465    
466    void ScriptEditor::updateStatusBar() {
467        // update status text
468        std::string txt;
469        if (m_issues.empty()) {
470            txt = "No issues with this script.";
471        } else {
472            const char* txtWontLoad = ". Sampler won't load instruments using this script!";
473            txt = "There ";
474            txt += (m_errors.size() <= 1 && m_warnings.size() <= 1) ? "is " : "are ";
475            if (m_errors.empty()) {
476                txt += warningsCountTxt(m_warnings) + ". Script will load, but might not behave as expected!";
477            } else if (m_warnings.empty()) {
478                txt += errorsCountTxt(m_errors) + txtWontLoad;
479            } else {
480                txt += errorsCountTxt(m_errors) + " and " +
481                       warningsCountTxt(m_warnings) + txtWontLoad;
482          }          }
483      }      }
484        m_statusLabel.set_markup(txt);
485    
486      delete parserContext;      // update status icon
487        m_statusImage.set(
488            m_issues.empty() ? m_successIcon : !m_errors.empty() ? m_errorIcon : m_warningIcon
489        );
490  }  }
491    
492  #endif // USE_LS_SCRIPTVM  #endif // USE_LS_SCRIPTVM
493    
494  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) {
495      //printf("erased\n");      //printf("erased\n");
     if (m_ignoreEraseEvents) return;  
   
496  #if USE_LS_SCRIPTVM  #if USE_LS_SCRIPTVM
     removeIssueAnchors();  
497      m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());      m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
498      updateSyntaxHighlightingByVM();      updateSyntaxHighlightingByVM();
499      updateParserIssuesByVM();      updateParserIssuesByVM();
500        updateStatusBar();
501  #else  #else
502      Gtk::TextBuffer::iterator itStart2 = itStart;      Gtk::TextBuffer::iterator itStart2 = itStart;
503      if (itStart2.inside_word() || itStart2.ends_word())      if (itStart2.inside_word() || itStart2.ends_word())
# Line 380  void ScriptEditor::onTextErased(const Gt Line 510  void ScriptEditor::onTextErased(const Gt
510  #endif // USE_LS_SCRIPTVM  #endif // USE_LS_SCRIPTVM
511  }  }
512    
513    bool ScriptEditor::on_motion_notify_event(GdkEventMotion* e) {
514    #if USE_LS_SCRIPTVM
515        //TODO: event throttling would be a good idea here
516        updateIssueTooltip(e);
517    #endif
518        return ManagedWindow::on_motion_notify_event(e);
519    }
520    
521    void ScriptEditor::onMenuChangeFontSize() {
522        //TODO: for GTKMM >= 3.2 class Gtk::FontChooser could be used instead
523        Gtk::Dialog dialog(_("Font Size"), true /*modal*/);
524        Gtk::HBox hbox;
525        hbox.set_spacing(6);
526    
527        Gtk::Label label(_("Editor's Font Size:"), Gtk::ALIGN_START);
528        hbox.pack_start(label, Gtk::PACK_SHRINK);
529    
530        Gtk::SpinButton spinButton;
531        spinButton.set_range(4, 80);
532        spinButton.set_increments(1, 10);
533        spinButton.set_value(currentFontSize());
534        hbox.pack_start(spinButton);
535    
536        dialog.get_vbox()->pack_start(hbox);
537        dialog.add_button(_("_OK"), 0);
538        dialog.add_button(_("_Cancel"), 1);
539    
540        dialog.show_all_children();
541    
542        if (!dialog.run()) { // OK selected ...
543            const int newFontSize = spinButton.get_value_as_int();
544            if (newFontSize >= 4)
545                setFontSize(newFontSize, true);
546        }
547    }
548    
549    bool ScriptEditor::onWindowDelete(GdkEventAny* e) {
550        //printf("onWindowDelete\n");
551    
552        if (!isModified()) return false; // propagate event further (which will close this window)
553    
554        gchar* msg = g_strdup_printf(_("Apply changes to instrument script \"%s\" before closing?"),
555                                     m_script->Name.c_str());
556        Gtk::MessageDialog dialog(*this, msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
557        g_free(msg);
558        dialog.set_secondary_text(_("If you close without applying, your changes will be lost."));
559        dialog.add_button(_("Close _Without Applying"), Gtk::RESPONSE_NO);
560        dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL);
561        dialog.add_button(_("_Apply"), Gtk::RESPONSE_YES);
562        dialog.set_default_response(Gtk::RESPONSE_YES);
563        int response = dialog.run();
564        dialog.hide();
565    
566        // user decided to close script editor without saving
567        if (response == Gtk::RESPONSE_NO)
568            return false; // propagate event further (which will close this window)
569    
570        // user cancelled dialog, thus don't close script editor
571        if (response == Gtk::RESPONSE_CANCEL) {
572            show();
573            return true; // drop event (prevents closing this window)
574        }
575    
576        // user wants to apply the changes, afterwards close window
577        if (response == Gtk::RESPONSE_YES) {
578            onButtonApply();
579            return false; // propagate event further (which will close this window)
580        }
581    
582        // should never ever make it to this point actually
583        return false;
584    }
585    
586    bool ScriptEditor::isModified() const {
587        return m_textBuffer->get_modified();
588    }
589    
590  void ScriptEditor::onModifiedChanged() {  void ScriptEditor::onModifiedChanged() {
591      m_applyButton.set_sensitive( m_textBuffer->get_modified() );      m_applyButton.set_sensitive(isModified());
592    #if USE_LS_SCRIPTVM
593        updateStatusBar();
594    #endif
595  }  }
596    
597  void ScriptEditor::onButtonCancel() {  void ScriptEditor::onButtonCancel() {
598        bool dropEvent = onWindowDelete(NULL);
599        if (dropEvent) return;
600      hide();      hide();
601  }  }
602    
603  void ScriptEditor::onButtonApply() {  void ScriptEditor::onButtonApply() {
604        signal_script_to_be_changed.emit(m_script);
605      m_script->SetScriptAsText(m_textBuffer->get_text());      m_script->SetScriptAsText(m_textBuffer->get_text());
606        signal_script_changed.emit(m_script);
607      m_textBuffer->set_modified(false);      m_textBuffer->set_modified(false);
608  }  }
609    

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

  ViewVC Help
Powered by ViewVC