Code

Merging from trunk
[inkscape.git] / src / ui / dialog / scriptdialog.cpp
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     "importPackage(javax.swing);\n"
119     "function sayHello() {\n"
120     "  JOptionPane.showMessageDialog(null, 'Hello, world!',\n"
121         "     'Welcome to Inkscape', JOptionPane.WARNING_MESSAGE);\n"
122     "}\n"
123     "\n"
124     "sayHello();\n"
125     "\n";
130 //#########################################################################
131 //## E V E N T S
132 //#########################################################################
134 static void textViewClear(Gtk::TextView &view)
136     Glib::RefPtr<Gtk::TextBuffer> buffer = view.get_buffer();
137     buffer->erase(buffer->begin(), buffer->end());
141 /**
142  * Also a public method.  Remove all text from the dialog
143  */
144 void ScriptDialogImpl::clear()
146     textViewClear(scriptText);
147     textViewClear(outputText);
148     textViewClear(errorText);
151 /**
152  * Execute the script in the dialog
153  */
154 void
155 ScriptDialogImpl::execute(Inkscape::Extension::Script::InkscapeScript::ScriptLanguage
156 lang)
158     Glib::ustring script = scriptText.get_buffer()->get_text(true);
159     Glib::ustring output;
160     Glib::ustring error;
161     Inkscape::Extension::Script::InkscapeScript engine;
162     bool ok = engine.interpretScript(script, output, error, lang);
163     outputText.get_buffer()->set_text(output);
164     errorText.get_buffer()->set_text(error);
165     if (!ok)
166         {
167         //do we want something here?
168         }
171 /**
172  * Execute the script in the dialog
173  */
174 void ScriptDialogImpl::executeJavascript()
176     execute(Inkscape::Extension::Script::InkscapeScript::JAVASCRIPT);
179 /**
180  * Execute the script in the dialog
181  */
182 void ScriptDialogImpl::executePython()
184     execute(Inkscape::Extension::Script::InkscapeScript::PYTHON);
187 /**
188  * Execute the script in the dialog
189  */
190 void ScriptDialogImpl::executeRuby()
192     execute(Inkscape::Extension::Script::InkscapeScript::RUBY);
196 //#########################################################################
197 //## C O N S T R U C T O R    /    D E S T R U C T O R
198 //#########################################################################
199 /**
200  * Constructor
201  */
202 ScriptDialogImpl::ScriptDialogImpl() :
203     ScriptDialog()
205     Gtk::Box *contents = _getContents();
207     //## Add a menu for clear()
208     menuBar.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_File"), fileMenu) );
209     fileMenu.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_Clear"),
210            sigc::mem_fun(*this, &ScriptDialogImpl::clear) ) );
211     fileMenu.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_Execute Javascript"),
212            sigc::mem_fun(*this, &ScriptDialogImpl::executeJavascript) ) );
213     fileMenu.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_Execute Python"),
214            sigc::mem_fun(*this, &ScriptDialogImpl::executePython) ) );
215     fileMenu.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_Execute Ruby"),
216            sigc::mem_fun(*this, &ScriptDialogImpl::executeRuby) ) );
217     contents->pack_start(menuBar, Gtk::PACK_SHRINK);
219     //### Set up the script field
220     scriptText.set_editable(true);
221     scriptText.get_buffer()->set_text(defaultCodeStr);
222     scriptTextScroll.add(scriptText);
223     scriptTextScroll.set_policy(Gtk::POLICY_ALWAYS, Gtk::POLICY_ALWAYS);
224     scriptTextFrame.set_label(_("Script"));
225     scriptTextFrame.set_shadow_type(Gtk::SHADOW_NONE);
226     scriptTextFrame.add(scriptTextScroll);
227     contents->pack_start(scriptTextFrame);
229     //### Set up the output field
230     outputText.set_editable(true);
231     outputText.get_buffer()->set_text("");
232     outputTextScroll.add(outputText);
233     outputTextScroll.set_policy(Gtk::POLICY_ALWAYS, Gtk::POLICY_ALWAYS);
234     outputTextFrame.set_label(_("Output"));
235     outputTextFrame.set_shadow_type(Gtk::SHADOW_NONE);
236     outputTextFrame.add(outputTextScroll);
237     contents->pack_start(outputTextFrame);
239     //### Set up the error field
240     errorText.set_editable(true);
241     errorText.get_buffer()->set_text("");
242     errorTextScroll.add(errorText);
243     errorTextScroll.set_policy(Gtk::POLICY_ALWAYS, Gtk::POLICY_ALWAYS);
244     errorTextFrame.set_label(_("Errors"));
245     errorTextFrame.set_shadow_type(Gtk::SHADOW_NONE);
246     errorTextFrame.add(errorTextScroll);
247     contents->pack_start(errorTextFrame);
249     // sick of this thing shrinking too much
250     set_size_request(350, 400);
251     show_all_children();
255 /**
256  * Factory method.  Use this to create a new ScriptDialog
257  */
258 ScriptDialog &ScriptDialog::getInstance()
260     ScriptDialog *dialog = new ScriptDialogImpl();
261     return *dialog;
269 } //namespace Dialogs
270 } //namespace UI
271 } //namespace Inkscape
273 //#########################################################################
274 //## E N D    O F    F I L E
275 //#########################################################################