Code

a8c0286d68f3cfcaa41182306c75be5c58a8dfdf
[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 #if defined(WITH_PYTHON)
102     "# This is a sample Python script.\n"
103     "# To run it, select 'Execute Python' from the File menu above.\n"
104     "desktop = inkscape.getDesktop()\n"
105     "dialogmanager = inkscape.getDialogManager()\n"
106     "document = desktop.getDocument()\n"
107     "document.hello()\n"
108     "dialogmanager.showAbout()\n"
109 #elif defined(WITH_PERL)
110     "# This is a sample Perl script.\n"
111     "# To run it, select 'Execute Perl' from the File menu above.\n"
112     "my $desktop = $inkscape->getDesktop();\n"
113     "my $dialogmanager = $inkscape->getDialogManager();\n"
114     "my $document = $desktop->getDocument();\n"
115     "$document->hello();\n"
116     "$dialogmanager->showAbout();\n"
117 #else
118     "# This is where you could type a script.\n"
119     "# However, no scripting languages have been compiled\n"
120     "# into Inkscape, so this window has no functionality.\n"
121     "# When compiling Inkscape, run \"configure\" with\n"
122     "# \"--with-python\" and/or \"--with-perl\".\n"
123 #endif
124     "";
128 //#########################################################################
129 //## E V E N T S
130 //#########################################################################
132 static void textViewClear(Gtk::TextView &view)
134     Glib::RefPtr<Gtk::TextBuffer> buffer = view.get_buffer();
135     buffer->erase(buffer->begin(), buffer->end());
139 /**
140  * Also a public method.  Remove all text from the dialog
141  */
142 void ScriptDialogImpl::clear()
144     textViewClear(scriptText);
145     textViewClear(outputText);
146     textViewClear(errorText);
149 /**
150  * Execute the script in the dialog
151  */
152 void
153 ScriptDialogImpl::execute(Inkscape::Extension::Script::InkscapeScript::ScriptLanguage
154 lang)
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     if (!ok) return;
162     outputText.get_buffer()->set_text(output);
163     errorText.get_buffer()->set_text(error);
166 /**
167  * Execute the script in the dialog
168  */
169 void ScriptDialogImpl::executePython()
171     execute(Inkscape::Extension::Script::InkscapeScript::PYTHON);
174 /**
175  * Execute the script in the dialog
176  */
177 void ScriptDialogImpl::executePerl()
179     execute(Inkscape::Extension::Script::InkscapeScript::PERL);
183 //#########################################################################
184 //## C O N S T R U C T O R    /    D E S T R U C T O R
185 //#########################################################################
186 /**
187  * Constructor
188  */
189 ScriptDialogImpl::ScriptDialogImpl()
191     Gtk::VBox *mainVBox = get_vbox();
193     //## Add a menu for clear()
194     menuBar.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_File"), fileMenu) );
195     fileMenu.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_Clear"),
196            sigc::mem_fun(*this, &ScriptDialogImpl::clear) ) );
197 #ifdef WITH_PYTHON
198     fileMenu.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_Execute Python"),
199            sigc::mem_fun(*this, &ScriptDialogImpl::executePython) ) );
200 #endif
201 #ifdef WITH_PERL
202     fileMenu.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_Execute Perl"),
203            sigc::mem_fun(*this, &ScriptDialogImpl::executePerl) ) );
204 #endif
205     mainVBox->pack_start(menuBar, Gtk::PACK_SHRINK);
207     //### Set up the script field
208     scriptText.set_editable(true);
209     scriptText.get_buffer()->set_text(defaultPythonCodeStr);
210     scriptTextScroll.add(scriptText);
211     scriptTextScroll.set_policy(Gtk::POLICY_ALWAYS, Gtk::POLICY_ALWAYS);
212     scriptTextFrame.set_label(_("Script"));
213     scriptTextFrame.set_shadow_type(Gtk::SHADOW_NONE);
214     scriptTextFrame.add(scriptTextScroll);
215     mainVBox->pack_start(scriptTextFrame);
217     //### Set up the output field
218     outputText.set_editable(true);
219     outputText.get_buffer()->set_text("");
220     outputTextScroll.add(outputText);
221     outputTextScroll.set_policy(Gtk::POLICY_ALWAYS, Gtk::POLICY_ALWAYS);
222     outputTextFrame.set_label(_("Output"));
223     outputTextFrame.set_shadow_type(Gtk::SHADOW_NONE);
224     outputTextFrame.add(outputTextScroll);
225     mainVBox->pack_start(outputTextFrame);
227     //### Set up the error field
228     errorText.set_editable(true);
229     errorText.get_buffer()->set_text("");
230     errorTextScroll.add(errorText);
231     errorTextScroll.set_policy(Gtk::POLICY_ALWAYS, Gtk::POLICY_ALWAYS);
232     errorTextFrame.set_label(_("Errors"));
233     errorTextFrame.set_shadow_type(Gtk::SHADOW_NONE);
234     errorTextFrame.add(errorTextScroll);
235     mainVBox->pack_start(errorTextFrame);
237     // sick of this thing shrinking too much
238     set_size_request(350, 400);
239     show_all_children();
243 /**
244  * Factory method.  Use this to create a new ScriptDialog
245  */
246 ScriptDialog *ScriptDialog::create()
248     ScriptDialog *dialog = new ScriptDialogImpl();
249     return dialog;
257 } //namespace Dialogs
258 } //namespace UI
259 } //namespace Inkscape
261 //#########################################################################
262 //## E N D    O F    F I L E
263 //#########################################################################