1 /**
2 * Dialog for executing and monitoring script execution
3 *
4 * Author:
5 * Bob Jamison
6 *
7 * Copyright (C) 2004-2008 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>
26 namespace Inkscape
27 {
28 namespace UI
29 {
30 namespace Dialog
31 {
35 //#########################################################################
36 //## I M P L E M E N T A T I O N
37 //#########################################################################
39 /**
40 * A script editor/executor
41 */
42 class ScriptDialogImpl : public ScriptDialog
43 {
45 public:
48 /**
49 * Constructor
50 */
51 ScriptDialogImpl();
53 /**
54 * Destructor
55 */
56 ~ScriptDialogImpl()
57 {}
60 /**
61 * Clear the text
62 */
63 void clear();
65 /**
66 * Execute the script
67 */
68 void execute(Inkscape::Extension::Script::InkscapeScript::ScriptLanguage lang);
70 /**
71 * Execute a Javascript script
72 */
73 void executeJavascript();
75 /**
76 * Execute a Python script
77 */
78 void executePython();
80 /**
81 * Execute a Ruby script
82 */
83 void executeRuby();
87 private:
90 Gtk::MenuBar menuBar;
92 Gtk::Menu fileMenu;
94 //## Script text
95 Gtk::Frame scriptTextFrame;
96 Gtk::ScrolledWindow scriptTextScroll;
97 Gtk::TextView scriptText;
99 //## Output text
100 Gtk::Frame outputTextFrame;
101 Gtk::ScrolledWindow outputTextScroll;
102 Gtk::TextView outputText;
104 //## Error text
105 Gtk::Frame errorTextFrame;
106 Gtk::ScrolledWindow errorTextScroll;
107 Gtk::TextView errorText;
111 };
113 static const char *defaultCodeStr =
114 "/**\n"
115 " * This is some example Javascript.\n"
116 " * Try 'Execute Javascript'\n"
117 " */\n"
118 "function sayHello() {\n"
119 " println('Hello, world!');\n"
120 "}\n"
121 "\n"
122 "sayHello();\n"
123 "\n";
128 //#########################################################################
129 //## E V E N T S
130 //#########################################################################
132 static void textViewClear(Gtk::TextView &view)
133 {
134 Glib::RefPtr<Gtk::TextBuffer> buffer = view.get_buffer();
135 buffer->erase(buffer->begin(), buffer->end());
136 }
139 /**
140 * Also a public method. Remove all text from the dialog
141 */
142 void ScriptDialogImpl::clear()
143 {
144 textViewClear(scriptText);
145 textViewClear(outputText);
146 textViewClear(errorText);
147 }
149 /**
150 * Execute the script in the dialog
151 */
152 void
153 ScriptDialogImpl::execute(Inkscape::Extension::Script::InkscapeScript::ScriptLanguage
154 lang)
155 {
156 Glib::ustring script = scriptText.get_buffer()->get_text(true);
157 Glib::ustring output;
158 Glib::ustring error;
159 Inkscape::Extension::Script::InkscapeScript engine;
160 bool ok = engine.interpretScript(script, output, error, lang);
161 outputText.get_buffer()->set_text(output);
162 errorText.get_buffer()->set_text(error);
163 if (!ok)
164 {
165 //do we want something here?
166 }
167 }
169 /**
170 * Execute the script in the dialog
171 */
172 void ScriptDialogImpl::executeJavascript()
173 {
174 execute(Inkscape::Extension::Script::InkscapeScript::JAVASCRIPT);
175 }
177 /**
178 * Execute the script in the dialog
179 */
180 void ScriptDialogImpl::executePython()
181 {
182 execute(Inkscape::Extension::Script::InkscapeScript::PYTHON);
183 }
185 /**
186 * Execute the script in the dialog
187 */
188 void ScriptDialogImpl::executeRuby()
189 {
190 execute(Inkscape::Extension::Script::InkscapeScript::RUBY);
191 }
194 //#########################################################################
195 //## C O N S T R U C T O R / D E S T R U C T O R
196 //#########################################################################
197 /**
198 * Constructor
199 */
200 ScriptDialogImpl::ScriptDialogImpl() :
201 ScriptDialog()
202 {
203 Gtk::Box *contents = _getContents();
205 //## Add a menu for clear()
206 menuBar.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_File"), fileMenu) );
207 fileMenu.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_Clear"),
208 sigc::mem_fun(*this, &ScriptDialogImpl::clear) ) );
209 fileMenu.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_Execute Javascript"),
210 sigc::mem_fun(*this, &ScriptDialogImpl::executeJavascript) ) );
211 fileMenu.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_Execute Python"),
212 sigc::mem_fun(*this, &ScriptDialogImpl::executePython) ) );
213 fileMenu.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_Execute Ruby"),
214 sigc::mem_fun(*this, &ScriptDialogImpl::executeRuby) ) );
215 contents->pack_start(menuBar, Gtk::PACK_SHRINK);
217 //### Set up the script field
218 scriptText.set_editable(true);
219 scriptText.get_buffer()->set_text(defaultCodeStr);
220 scriptTextScroll.add(scriptText);
221 scriptTextScroll.set_policy(Gtk::POLICY_ALWAYS, Gtk::POLICY_ALWAYS);
222 scriptTextFrame.set_label(_("Script"));
223 scriptTextFrame.set_shadow_type(Gtk::SHADOW_NONE);
224 scriptTextFrame.add(scriptTextScroll);
225 contents->pack_start(scriptTextFrame);
227 //### Set up the output field
228 outputText.set_editable(true);
229 outputText.get_buffer()->set_text("");
230 outputTextScroll.add(outputText);
231 outputTextScroll.set_policy(Gtk::POLICY_ALWAYS, Gtk::POLICY_ALWAYS);
232 outputTextFrame.set_label(_("Output"));
233 outputTextFrame.set_shadow_type(Gtk::SHADOW_NONE);
234 outputTextFrame.add(outputTextScroll);
235 contents->pack_start(outputTextFrame);
237 //### Set up the error field
238 errorText.set_editable(true);
239 errorText.get_buffer()->set_text("");
240 errorTextScroll.add(errorText);
241 errorTextScroll.set_policy(Gtk::POLICY_ALWAYS, Gtk::POLICY_ALWAYS);
242 errorTextFrame.set_label(_("Errors"));
243 errorTextFrame.set_shadow_type(Gtk::SHADOW_NONE);
244 errorTextFrame.add(errorTextScroll);
245 contents->pack_start(errorTextFrame);
247 // sick of this thing shrinking too much
248 set_size_request(350, 400);
249 show_all_children();
251 }
253 /**
254 * Factory method. Use this to create a new ScriptDialog
255 */
256 ScriptDialog &ScriptDialog::getInstance()
257 {
258 ScriptDialog *dialog = new ScriptDialogImpl();
259 return *dialog;
260 }
267 } //namespace Dialogs
268 } //namespace UI
269 } //namespace Inkscape
271 //#########################################################################
272 //## E N D O F F I L E
273 //#########################################################################