1 /**
2 * Dialog for executing and monitoring script execution
3 *
4 * Author:
5 * Bob Jamison
6 *
7 * Copyright (C) 2004-2007 Authors
8 *
9 * Released under GNU GPL, read the file 'COPYING' for more information
10 */
11 #ifdef HAVE_CONFIG_H
12 # include <config.h>
13 #endif
15 #include <glibmm/i18n.h>
16 #include <gtkmm/menubar.h>
17 #include <gtkmm/frame.h>
18 #include <gtkmm/scrolledwindow.h>
19 #include <gtkmm/textview.h>
21 #include "scriptdialog.h"
22 #include <extension/script/InkscapeScript.h>
25 namespace Inkscape {
26 namespace UI {
27 namespace Dialog {
30 //#########################################################################
31 //## I M P L E M E N T A T I O N
32 //#########################################################################
34 /**
35 * A script editor/executor
36 */
37 class ScriptDialogImpl : public ScriptDialog
38 {
40 public:
43 /**
44 * Constructor
45 */
46 ScriptDialogImpl();
48 /**
49 * Destructor
50 */
51 ~ScriptDialogImpl()
52 {}
55 /**
56 * Clear the text
57 */
58 void clear();
60 /**
61 * Execute the script
62 */
63 void execute(Inkscape::Extension::Script::InkscapeScript::ScriptLanguage lang);
65 /**
66 * Execute a Python script
67 */
68 void executePython();
70 /**
71 * Execute a Perl script
72 */
73 void executePerl();
77 private:
80 Gtk::MenuBar menuBar;
82 Gtk::Menu fileMenu;
84 //## Script text
85 Gtk::Frame scriptTextFrame;
86 Gtk::ScrolledWindow scriptTextScroll;
87 Gtk::TextView scriptText;
89 //## Output text
90 Gtk::Frame outputTextFrame;
91 Gtk::ScrolledWindow outputTextScroll;
92 Gtk::TextView outputText;
94 //## Error text
95 Gtk::Frame errorTextFrame;
96 Gtk::ScrolledWindow errorTextScroll;
97 Gtk::TextView errorText;
101 };
103 static char *defaultPythonCodeStr =
104 #if defined(WITH_PYTHON)
105 "# This is a sample Python script.\n"
106 "# To run it, select 'Execute Python' from the File menu above.\n"
107 "desktop = inkscape.activeDesktop\n"
108 "dialogmanager = desktop.dialogManager\n"
109 "document = inkscape.activeDocument\n"
110 "inkscape.hello()\n"
111 "dialogmanager.showAbout()\n"
112 #elif defined(WITH_PERL)
113 "# This is a sample Perl script.\n"
114 "# To run it, select 'Execute Perl' from the File menu above.\n"
115 "my $desktop = $inkscape->getDesktop();\n"
116 "my $dialogmanager = $inkscape->getDialogManager();\n"
117 "my $document = $desktop->getDocument();\n"
118 "$document->hello();\n"
119 "$dialogmanager->showAbout();\n"
120 #else
121 "# This is where you could type a script.\n"
122 "# However, no scripting languages have been compiled\n"
123 "# into Inkscape, so this window has no functionality.\n"
124 "# When compiling Inkscape, run \"configure\" with\n"
125 "# \"--with-python\" and/or \"--with-perl\".\n"
126 #endif
127 "";
131 //#########################################################################
132 //## E V E N T S
133 //#########################################################################
135 static void textViewClear(Gtk::TextView &view)
136 {
137 Glib::RefPtr<Gtk::TextBuffer> buffer = view.get_buffer();
138 buffer->erase(buffer->begin(), buffer->end());
139 }
142 /**
143 * Also a public method. Remove all text from the dialog
144 */
145 void ScriptDialogImpl::clear()
146 {
147 textViewClear(scriptText);
148 textViewClear(outputText);
149 textViewClear(errorText);
150 }
152 /**
153 * Execute the script in the dialog
154 */
155 void
156 ScriptDialogImpl::execute(Inkscape::Extension::Script::InkscapeScript::ScriptLanguage
157 lang)
158 {
159 Glib::ustring script = scriptText.get_buffer()->get_text(true);
160 Glib::ustring output;
161 Glib::ustring error;
162 Inkscape::Extension::Script::InkscapeScript engine;
163 bool ok = engine.interpretScript(script, output, error, lang);
164 outputText.get_buffer()->set_text(output);
165 errorText.get_buffer()->set_text(error);
166 if (!ok)
167 {
168 //do we want something here?
169 }
170 }
172 /**
173 * Execute the script in the dialog
174 */
175 void ScriptDialogImpl::executePython()
176 {
177 execute(Inkscape::Extension::Script::InkscapeScript::PYTHON);
178 }
180 /**
181 * Execute the script in the dialog
182 */
183 void ScriptDialogImpl::executePerl()
184 {
185 execute(Inkscape::Extension::Script::InkscapeScript::PERL);
186 }
189 //#########################################################################
190 //## C O N S T R U C T O R / D E S T R U C T O R
191 //#########################################################################
192 /**
193 * Constructor
194 */
195 ScriptDialogImpl::ScriptDialogImpl()
196 {
197 Gtk::VBox *mainVBox = get_vbox();
199 //## Add a menu for clear()
200 menuBar.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_File"), fileMenu) );
201 fileMenu.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_Clear"),
202 sigc::mem_fun(*this, &ScriptDialogImpl::clear) ) );
203 #ifdef WITH_PYTHON
204 fileMenu.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_Execute Python"),
205 sigc::mem_fun(*this, &ScriptDialogImpl::executePython) ) );
206 #endif
207 #ifdef WITH_PERL
208 fileMenu.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_Execute Perl"),
209 sigc::mem_fun(*this, &ScriptDialogImpl::executePerl) ) );
210 #endif
211 mainVBox->pack_start(menuBar, Gtk::PACK_SHRINK);
213 //### Set up the script field
214 scriptText.set_editable(true);
215 scriptText.get_buffer()->set_text(defaultPythonCodeStr);
216 scriptTextScroll.add(scriptText);
217 scriptTextScroll.set_policy(Gtk::POLICY_ALWAYS, Gtk::POLICY_ALWAYS);
218 scriptTextFrame.set_label(_("Script"));
219 scriptTextFrame.set_shadow_type(Gtk::SHADOW_NONE);
220 scriptTextFrame.add(scriptTextScroll);
221 mainVBox->pack_start(scriptTextFrame);
223 //### Set up the output field
224 outputText.set_editable(true);
225 outputText.get_buffer()->set_text("");
226 outputTextScroll.add(outputText);
227 outputTextScroll.set_policy(Gtk::POLICY_ALWAYS, Gtk::POLICY_ALWAYS);
228 outputTextFrame.set_label(_("Output"));
229 outputTextFrame.set_shadow_type(Gtk::SHADOW_NONE);
230 outputTextFrame.add(outputTextScroll);
231 mainVBox->pack_start(outputTextFrame);
233 //### Set up the error field
234 errorText.set_editable(true);
235 errorText.get_buffer()->set_text("");
236 errorTextScroll.add(errorText);
237 errorTextScroll.set_policy(Gtk::POLICY_ALWAYS, Gtk::POLICY_ALWAYS);
238 errorTextFrame.set_label(_("Errors"));
239 errorTextFrame.set_shadow_type(Gtk::SHADOW_NONE);
240 errorTextFrame.add(errorTextScroll);
241 mainVBox->pack_start(errorTextFrame);
243 // sick of this thing shrinking too much
244 set_size_request(350, 400);
245 show_all_children();
247 }
249 /**
250 * Factory method. Use this to create a new ScriptDialog
251 */
252 ScriptDialog *ScriptDialog::create()
253 {
254 ScriptDialog *dialog = new ScriptDialogImpl();
255 return dialog;
256 }
263 } //namespace Dialogs
264 } //namespace UI
265 } //namespace Inkscape
267 //#########################################################################
268 //## E N D O F F I L E
269 //#########################################################################