Code

Got initial JS execution working
[inkscape.git] / src / ui / dialog / scriptdialog.cpp
index 4836774641918a77e6d52e8c6478f14cad683826..ff075c1868da82da7d4992a4297423780f6667c5 100644 (file)
@@ -1,7 +1,10 @@
-/*
- *   Other dudes from The Inkscape Organization
+/**
+ *  Dialog for executing and monitoring script execution
+ *  
+ * Author:  
+ *   Bob Jamison
  *
- * Copyright (C) 2004, 2005 Authors
+ * Copyright (C) 2004-2008 Authors
  *
  * Released under GNU GPL, read the file 'COPYING' for more information
  */
 #include <extension/script/InkscapeScript.h>
 
 
-namespace Inkscape {
-namespace UI {
-namespace Dialog {
+
+namespace Inkscape
+{
+namespace UI
+{
+namespace Dialog
+{
+
 
 
 //#########################################################################
@@ -59,15 +67,20 @@ class ScriptDialogImpl : public ScriptDialog
      */
     void execute(Inkscape::Extension::Script::InkscapeScript::ScriptLanguage lang);
 
+    /**
+     * Execute a Javascript script
+     */
+    void executeJavascript();
+
     /**
      * Execute a Python script
      */
     void executePython();
 
     /**
-     * Execute a Perl script
+     * Execute a Ruby script
      */
-    void executePerl();
+    void executeRuby();
 
 
 
@@ -97,15 +110,18 @@ class ScriptDialogImpl : public ScriptDialog
 
 };
 
-static char *defaultPythonCodeStr =
-    "# This is a sample Python script.\n"
-    "# To run it, select 'Execute Python' from the File menu above.\n"
-    "desktop = inkscape.getDesktop()\n"
-    "dialogmanager = inkscape.getDialogManager()\n"
-    "document = desktop.getDocument()\n"
-    "document.hello()\n"
-    "dialogmanager.showAbout()\n"
-    "";
+static const char *defaultCodeStr =
+    "/**\n"
+    " * This is some example Javascript.\n"
+    " * Try 'Execute Javascript'\n"
+    " */\n"
+    "function sayHello() {\n"
+    "  println('Hello, world!');\n"
+    "}\n"
+    "\n"
+    "sayHello();\n"
+    "\n";
+
 
 
 
@@ -142,9 +158,20 @@ lang)
     Glib::ustring error;
     Inkscape::Extension::Script::InkscapeScript engine;
     bool ok = engine.interpretScript(script, output, error, lang);
-    if (!ok) return;
     outputText.get_buffer()->set_text(output);
     errorText.get_buffer()->set_text(error);
+    if (!ok)
+        {
+        //do we want something here?
+        }
+}
+
+/**
+ * Execute the script in the dialog
+ */
+void ScriptDialogImpl::executeJavascript()
+{
+    execute(Inkscape::Extension::Script::InkscapeScript::JAVASCRIPT);
 }
 
 /**
@@ -158,9 +185,9 @@ void ScriptDialogImpl::executePython()
 /**
  * Execute the script in the dialog
  */
-void ScriptDialogImpl::executePerl()
+void ScriptDialogImpl::executeRuby()
 {
-    execute(Inkscape::Extension::Script::InkscapeScript::PERL);
+    execute(Inkscape::Extension::Script::InkscapeScript::RUBY);
 }
 
 
@@ -170,29 +197,32 @@ void ScriptDialogImpl::executePerl()
 /**
  * Constructor
  */
-ScriptDialogImpl::ScriptDialogImpl()
+ScriptDialogImpl::ScriptDialogImpl() :
+    ScriptDialog()
 {
-    Gtk::VBox *mainVBox = get_vbox();
+    Gtk::Box *contents = _getContents();
 
     //## Add a menu for clear()
     menuBar.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_File"), fileMenu) );
     fileMenu.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_Clear"),
            sigc::mem_fun(*this, &ScriptDialogImpl::clear) ) );
+    fileMenu.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_Execute Javascript"),
+           sigc::mem_fun(*this, &ScriptDialogImpl::executeJavascript) ) );
     fileMenu.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_Execute Python"),
            sigc::mem_fun(*this, &ScriptDialogImpl::executePython) ) );
-    fileMenu.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_Execute Perl"),
-           sigc::mem_fun(*this, &ScriptDialogImpl::executePerl) ) );
-    mainVBox->pack_start(menuBar, Gtk::PACK_SHRINK);
+    fileMenu.items().push_back( Gtk::Menu_Helpers::MenuElem(_("_Execute Ruby"),
+           sigc::mem_fun(*this, &ScriptDialogImpl::executeRuby) ) );
+    contents->pack_start(menuBar, Gtk::PACK_SHRINK);
 
     //### Set up the script field
     scriptText.set_editable(true);
-    scriptText.get_buffer()->set_text(defaultPythonCodeStr);
+    scriptText.get_buffer()->set_text(defaultCodeStr);
     scriptTextScroll.add(scriptText);
     scriptTextScroll.set_policy(Gtk::POLICY_ALWAYS, Gtk::POLICY_ALWAYS);
     scriptTextFrame.set_label(_("Script"));
     scriptTextFrame.set_shadow_type(Gtk::SHADOW_NONE);
     scriptTextFrame.add(scriptTextScroll);
-    mainVBox->pack_start(scriptTextFrame);
+    contents->pack_start(scriptTextFrame);
 
     //### Set up the output field
     outputText.set_editable(true);
@@ -202,7 +232,7 @@ ScriptDialogImpl::ScriptDialogImpl()
     outputTextFrame.set_label(_("Output"));
     outputTextFrame.set_shadow_type(Gtk::SHADOW_NONE);
     outputTextFrame.add(outputTextScroll);
-    mainVBox->pack_start(outputTextFrame);
+    contents->pack_start(outputTextFrame);
 
     //### Set up the error field
     errorText.set_editable(true);
@@ -212,8 +242,10 @@ ScriptDialogImpl::ScriptDialogImpl()
     errorTextFrame.set_label(_("Errors"));
     errorTextFrame.set_shadow_type(Gtk::SHADOW_NONE);
     errorTextFrame.add(errorTextScroll);
-    mainVBox->pack_start(errorTextFrame);
+    contents->pack_start(errorTextFrame);
 
+    // sick of this thing shrinking too much
+    set_size_request(350, 400);
     show_all_children();
 
 }
@@ -221,10 +253,10 @@ ScriptDialogImpl::ScriptDialogImpl()
 /**
  * Factory method.  Use this to create a new ScriptDialog
  */
-ScriptDialog *ScriptDialog::create()
+ScriptDialog &ScriptDialog::getInstance()
 {
     ScriptDialog *dialog = new ScriptDialogImpl();
-    return dialog;
+    return *dialog;
 }