Code

moving trunk for module inkscape
[inkscape.git] / src / ui / dialog / scriptdialog.cpp
1 /*
2  * This dialog is for launching scripts whose main purpose if
3  * the scripting of Inkscape itself.
4  *
5  * Authors:
6  *   Bob Jamison
7  *   Other dudes from The Inkscape Organization
8  *
9  * Copyright (C) 2004, 2005 Authors
10  *
11  * Released under GNU GPL, read the file 'COPYING' for more information
12  */
13 #ifdef HAVE_CONFIG_H
14 # include <config.h>
15 #endif
17 #include <glibmm/i18n.h>
18 #include <gtkmm/menubar.h>
19 #include <gtkmm/frame.h>
20 #include <gtkmm/scrolledwindow.h>
21 #include <gtkmm/textview.h>
23 #include "scriptdialog.h"
24 #include <extension/script/InkscapeScript.h>
27 namespace Inkscape {
28 namespace UI {
29 namespace Dialog {
32 //#########################################################################
33 //## I M P L E M E N T A T I O N
34 //#########################################################################
36 /**
37  * A script editor/executor
38  */
39 class ScriptDialogImpl : public ScriptDialog
40 {
42     public:
45     /**
46      * Constructor
47      */
48     ScriptDialogImpl();
50     /**
51      * Destructor
52      */
53     ~ScriptDialogImpl()
54         {}
57     /**
58      * Clear the text
59      */
60     void clear();
62     /**
63      * Execute the script
64      */
65     void execute(Inkscape::Extension::Script::InkscapeScript::ScriptLanguage lang);
67     /**
68      * Execute a Python script
69      */
70     void executePython();
72     /**
73      * Execute a Perl script
74      */
75     void executePerl();
79     private:
82     Gtk::MenuBar menuBar;
84     Gtk::Menu   fileMenu;
86     //## Script text
87     Gtk::Frame          scriptTextFrame;
88     Gtk::ScrolledWindow scriptTextScroll;
89     Gtk::TextView       scriptText;
91     //## Output text
92     Gtk::Frame          outputTextFrame;
93     Gtk::ScrolledWindow outputTextScroll;
94     Gtk::TextView       outputText;
96     //## Error text
97     Gtk::Frame          errorTextFrame;
98     Gtk::ScrolledWindow errorTextScroll;
99     Gtk::TextView       errorText;
103 };
105 static char *defaultPythonCodeStr =
106     "# This is a sample Python script.\n"
107     "# To run it, select 'Execute Python' from the File menu above.\n"
108     "desktop = inkscape.getDesktop()\n"
109     "dialogmanager = inkscape.getDialogManager()\n"
110     "document = desktop.getDocument()\n"
111     "document.hello()\n"
112     "dialogmanager.showAbout()\n"
113     "";
117 //#########################################################################
118 //## E V E N T S
119 //#########################################################################
121 static void textViewClear(Gtk::TextView &view)
123     Glib::RefPtr<Gtk::TextBuffer> buffer = view.get_buffer();
124     buffer->erase(buffer->begin(), buffer->end());
128 /**
129  * Also a public method.  Remove all text from the dialog
130  */
131 void ScriptDialogImpl::clear()
133     textViewClear(scriptText);
134     textViewClear(outputText);
135     textViewClear(errorText);
138 /**
139  * Execute the script in the dialog
140  */
141 void
142 ScriptDialogImpl::execute(Inkscape::Extension::Script::InkscapeScript::ScriptLanguage
143 lang)
145     Glib::ustring script = scriptText.get_buffer()->get_text(true);
146     Glib::ustring output;
147     Glib::ustring error;
148     Inkscape::Extension::Script::InkscapeScript engine;
149     engine.interpretScript(script, output, error, lang);
150     outputText.get_buffer()->set_text(output);
151     errorText.get_buffer()->set_text(error);
154 /**
155  * Execute the script in the dialog
156  */
157 void ScriptDialogImpl::executePython()
159     execute(Inkscape::Extension::Script::InkscapeScript::PYTHON);
162 /**
163  * Execute the script in the dialog
164  */
165 void ScriptDialogImpl::executePerl()
167     execute(Inkscape::Extension::Script::InkscapeScript::PERL);
171 //#########################################################################
172 //## C O N S T R U C T O R    /    D E S T R U C T O R
173 //#########################################################################
174 /**
175  * Constructor
176  */
177 ScriptDialogImpl::ScriptDialogImpl()
179     Gtk::VBox *mainVBox = get_vbox();
181     //## Add a menu for clear()
182     menuBar.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_File"), fileMenu) );
183     fileMenu.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_Clear"),
184            sigc::mem_fun(*this, &ScriptDialogImpl::clear) ) );
185     fileMenu.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_Execute Python"),
186            sigc::mem_fun(*this, &ScriptDialogImpl::executePython) ) );
187     fileMenu.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_Execute Perl"),
188            sigc::mem_fun(*this, &ScriptDialogImpl::executePerl) ) );
189     mainVBox->pack_start(menuBar, Gtk::PACK_SHRINK);
191     //### Set up the script field
192     scriptText.set_editable(true);
193     scriptText.get_buffer()->set_text(defaultPythonCodeStr);
194     scriptTextScroll.add(scriptText);
195     scriptTextScroll.set_policy(Gtk::POLICY_ALWAYS, Gtk::POLICY_ALWAYS);
196     scriptTextFrame.set_label(_("Script"));
197     scriptTextFrame.set_shadow_type(Gtk::SHADOW_NONE);
198     scriptTextFrame.add(scriptTextScroll);
199     mainVBox->pack_start(scriptTextFrame);
201     //### Set up the output field
202     outputText.set_editable(true);
203     outputText.get_buffer()->set_text("");
204     outputTextScroll.add(outputText);
205     outputTextScroll.set_policy(Gtk::POLICY_ALWAYS, Gtk::POLICY_ALWAYS);
206     outputTextFrame.set_label(_("Output"));
207     outputTextFrame.set_shadow_type(Gtk::SHADOW_NONE);
208     outputTextFrame.add(outputTextScroll);
209     mainVBox->pack_start(outputTextFrame);
211     //### Set up the error field
212     errorText.set_editable(true);
213     errorText.get_buffer()->set_text("");
214     errorTextScroll.add(errorText);
215     errorTextScroll.set_policy(Gtk::POLICY_ALWAYS, Gtk::POLICY_ALWAYS);
216     errorTextFrame.set_label(_("Errors"));
217     errorTextFrame.set_shadow_type(Gtk::SHADOW_NONE);
218     errorTextFrame.add(errorTextScroll);
219     mainVBox->pack_start(errorTextFrame);
221     show_all_children();
225 /**
226  * Factory method.  Use this to create a new ScriptDialog
227  */
228 ScriptDialog *ScriptDialog::create()
230     ScriptDialog *dialog = new ScriptDialogImpl();
231     return dialog;
239 } //namespace Dialogs
240 } //namespace UI
241 } //namespace Inkscape
243 //#########################################################################
244 //## E N D    O F    F I L E
245 //#########################################################################