Code

Merged in Native File Dialogs for Windows Branch
[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-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)
137     Glib::RefPtr<Gtk::TextBuffer> buffer = view.get_buffer();
138     buffer->erase(buffer->begin(), buffer->end());
142 /**
143  * Also a public method.  Remove all text from the dialog
144  */
145 void ScriptDialogImpl::clear()
147     textViewClear(scriptText);
148     textViewClear(outputText);
149     textViewClear(errorText);
152 /**
153  * Execute the script in the dialog
154  */
155 void
156 ScriptDialogImpl::execute(Inkscape::Extension::Script::InkscapeScript::ScriptLanguage
157 lang)
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         }
172 /**
173  * Execute the script in the dialog
174  */
175 void ScriptDialogImpl::executePython()
177     execute(Inkscape::Extension::Script::InkscapeScript::PYTHON);
180 /**
181  * Execute the script in the dialog
182  */
183 void ScriptDialogImpl::executePerl()
185     execute(Inkscape::Extension::Script::InkscapeScript::PERL);
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     ScriptDialog()
198     Gtk::Box *contents = _getContents();
200     //## Add a menu for clear()
201     menuBar.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_File"), fileMenu) );
202     fileMenu.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_Clear"),
203            sigc::mem_fun(*this, &ScriptDialogImpl::clear) ) );
204 #ifdef WITH_PYTHON
205     fileMenu.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_Execute Python"),
206            sigc::mem_fun(*this, &ScriptDialogImpl::executePython) ) );
207 #endif
208 #ifdef WITH_PERL
209     fileMenu.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_Execute Perl"),
210            sigc::mem_fun(*this, &ScriptDialogImpl::executePerl) ) );
211 #endif
212     contents->pack_start(menuBar, Gtk::PACK_SHRINK);
214     //### Set up the script field
215     scriptText.set_editable(true);
216     scriptText.get_buffer()->set_text(defaultPythonCodeStr);
217     scriptTextScroll.add(scriptText);
218     scriptTextScroll.set_policy(Gtk::POLICY_ALWAYS, Gtk::POLICY_ALWAYS);
219     scriptTextFrame.set_label(_("Script"));
220     scriptTextFrame.set_shadow_type(Gtk::SHADOW_NONE);
221     scriptTextFrame.add(scriptTextScroll);
222     contents->pack_start(scriptTextFrame);
224     //### Set up the output field
225     outputText.set_editable(true);
226     outputText.get_buffer()->set_text("");
227     outputTextScroll.add(outputText);
228     outputTextScroll.set_policy(Gtk::POLICY_ALWAYS, Gtk::POLICY_ALWAYS);
229     outputTextFrame.set_label(_("Output"));
230     outputTextFrame.set_shadow_type(Gtk::SHADOW_NONE);
231     outputTextFrame.add(outputTextScroll);
232     contents->pack_start(outputTextFrame);
234     //### Set up the error field
235     errorText.set_editable(true);
236     errorText.get_buffer()->set_text("");
237     errorTextScroll.add(errorText);
238     errorTextScroll.set_policy(Gtk::POLICY_ALWAYS, Gtk::POLICY_ALWAYS);
239     errorTextFrame.set_label(_("Errors"));
240     errorTextFrame.set_shadow_type(Gtk::SHADOW_NONE);
241     errorTextFrame.add(errorTextScroll);
242     contents->pack_start(errorTextFrame);
244     // sick of this thing shrinking too much
245     set_size_request(350, 400);
246     show_all_children();
250 /**
251  * Factory method.  Use this to create a new ScriptDialog
252  */
253 ScriptDialog &ScriptDialog::getInstance()
255     ScriptDialog *dialog = new ScriptDialogImpl();
256     return *dialog;
264 } //namespace Dialogs
265 } //namespace UI
266 } //namespace Inkscape
268 //#########################################################################
269 //## E N D    O F    F I L E
270 //#########################################################################