Code

handle request for not-compiled-in internal scripting; execute didn't check return...
[inkscape.git] / src / ui / dialog / scriptdialog.cpp
1 /*
2  *   Other dudes from The Inkscape Organization
3  *
4  * Copyright (C) 2004, 2005 Authors
5  *
6  * Released under GNU GPL, read the file 'COPYING' for more information
7  */
8 #ifdef HAVE_CONFIG_H
9 # include <config.h>
10 #endif
12 #include <glibmm/i18n.h>
13 #include <gtkmm/menubar.h>
14 #include <gtkmm/frame.h>
15 #include <gtkmm/scrolledwindow.h>
16 #include <gtkmm/textview.h>
18 #include "scriptdialog.h"
19 #include <extension/script/InkscapeScript.h>
22 namespace Inkscape {
23 namespace UI {
24 namespace Dialog {
27 //#########################################################################
28 //## I M P L E M E N T A T I O N
29 //#########################################################################
31 /**
32  * A script editor/executor
33  */
34 class ScriptDialogImpl : public ScriptDialog
35 {
37     public:
40     /**
41      * Constructor
42      */
43     ScriptDialogImpl();
45     /**
46      * Destructor
47      */
48     ~ScriptDialogImpl()
49         {}
52     /**
53      * Clear the text
54      */
55     void clear();
57     /**
58      * Execute the script
59      */
60     void execute(Inkscape::Extension::Script::InkscapeScript::ScriptLanguage lang);
62     /**
63      * Execute a Python script
64      */
65     void executePython();
67     /**
68      * Execute a Perl script
69      */
70     void executePerl();
74     private:
77     Gtk::MenuBar menuBar;
79     Gtk::Menu   fileMenu;
81     //## Script text
82     Gtk::Frame          scriptTextFrame;
83     Gtk::ScrolledWindow scriptTextScroll;
84     Gtk::TextView       scriptText;
86     //## Output text
87     Gtk::Frame          outputTextFrame;
88     Gtk::ScrolledWindow outputTextScroll;
89     Gtk::TextView       outputText;
91     //## Error text
92     Gtk::Frame          errorTextFrame;
93     Gtk::ScrolledWindow errorTextScroll;
94     Gtk::TextView       errorText;
98 };
100 static char *defaultPythonCodeStr =
101     "# This is a sample Python script.\n"
102     "# To run it, select 'Execute Python' from the File menu above.\n"
103     "desktop = inkscape.getDesktop()\n"
104     "dialogmanager = inkscape.getDialogManager()\n"
105     "document = desktop.getDocument()\n"
106     "document.hello()\n"
107     "dialogmanager.showAbout()\n"
108     "";
112 //#########################################################################
113 //## E V E N T S
114 //#########################################################################
116 static void textViewClear(Gtk::TextView &view)
118     Glib::RefPtr<Gtk::TextBuffer> buffer = view.get_buffer();
119     buffer->erase(buffer->begin(), buffer->end());
123 /**
124  * Also a public method.  Remove all text from the dialog
125  */
126 void ScriptDialogImpl::clear()
128     textViewClear(scriptText);
129     textViewClear(outputText);
130     textViewClear(errorText);
133 /**
134  * Execute the script in the dialog
135  */
136 void
137 ScriptDialogImpl::execute(Inkscape::Extension::Script::InkscapeScript::ScriptLanguage
138 lang)
140     Glib::ustring script = scriptText.get_buffer()->get_text(true);
141     Glib::ustring output;
142     Glib::ustring error;
143     Inkscape::Extension::Script::InkscapeScript engine;
144     bool ok = engine.interpretScript(script, output, error, lang);
145     if (!ok) return;
146     outputText.get_buffer()->set_text(output);
147     errorText.get_buffer()->set_text(error);
150 /**
151  * Execute the script in the dialog
152  */
153 void ScriptDialogImpl::executePython()
155     execute(Inkscape::Extension::Script::InkscapeScript::PYTHON);
158 /**
159  * Execute the script in the dialog
160  */
161 void ScriptDialogImpl::executePerl()
163     execute(Inkscape::Extension::Script::InkscapeScript::PERL);
167 //#########################################################################
168 //## C O N S T R U C T O R    /    D E S T R U C T O R
169 //#########################################################################
170 /**
171  * Constructor
172  */
173 ScriptDialogImpl::ScriptDialogImpl()
175     Gtk::VBox *mainVBox = get_vbox();
177     //## Add a menu for clear()
178     menuBar.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_File"), fileMenu) );
179     fileMenu.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_Clear"),
180            sigc::mem_fun(*this, &ScriptDialogImpl::clear) ) );
181     fileMenu.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_Execute Python"),
182            sigc::mem_fun(*this, &ScriptDialogImpl::executePython) ) );
183     fileMenu.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_Execute Perl"),
184            sigc::mem_fun(*this, &ScriptDialogImpl::executePerl) ) );
185     mainVBox->pack_start(menuBar, Gtk::PACK_SHRINK);
187     //### Set up the script field
188     scriptText.set_editable(true);
189     scriptText.get_buffer()->set_text(defaultPythonCodeStr);
190     scriptTextScroll.add(scriptText);
191     scriptTextScroll.set_policy(Gtk::POLICY_ALWAYS, Gtk::POLICY_ALWAYS);
192     scriptTextFrame.set_label(_("Script"));
193     scriptTextFrame.set_shadow_type(Gtk::SHADOW_NONE);
194     scriptTextFrame.add(scriptTextScroll);
195     mainVBox->pack_start(scriptTextFrame);
197     //### Set up the output field
198     outputText.set_editable(true);
199     outputText.get_buffer()->set_text("");
200     outputTextScroll.add(outputText);
201     outputTextScroll.set_policy(Gtk::POLICY_ALWAYS, Gtk::POLICY_ALWAYS);
202     outputTextFrame.set_label(_("Output"));
203     outputTextFrame.set_shadow_type(Gtk::SHADOW_NONE);
204     outputTextFrame.add(outputTextScroll);
205     mainVBox->pack_start(outputTextFrame);
207     //### Set up the error field
208     errorText.set_editable(true);
209     errorText.get_buffer()->set_text("");
210     errorTextScroll.add(errorText);
211     errorTextScroll.set_policy(Gtk::POLICY_ALWAYS, Gtk::POLICY_ALWAYS);
212     errorTextFrame.set_label(_("Errors"));
213     errorTextFrame.set_shadow_type(Gtk::SHADOW_NONE);
214     errorTextFrame.add(errorTextScroll);
215     mainVBox->pack_start(errorTextFrame);
217     show_all_children();
221 /**
222  * Factory method.  Use this to create a new ScriptDialog
223  */
224 ScriptDialog *ScriptDialog::create()
226     ScriptDialog *dialog = new ScriptDialogImpl();
227     return dialog;
235 } //namespace Dialogs
236 } //namespace UI
237 } //namespace Inkscape
239 //#########################################################################
240 //## E N D    O F    F I L E
241 //#########################################################################