Code

Rework dialog management. Use singleton behavior for dialogs when
[inkscape.git] / src / ui / dialog / livepatheffect-editor.cpp
index 5f765b4204233f390c96f2d091cfd598a8d4332e..b1dfda573861272d7e91e45cd4e368687b27e493 100644 (file)
-/**\r
- * \brief LivePathEffect dialog\r
- *\r
- * Authors:\r
- *   Johan Engelen <j.b.c.engelen@utwente.nl>\r
- *\r
- * Copyright (C) 2007 Author\r
- *\r
- * Released under GNU GPL.  Read the file 'COPYING' for more information.\r
- */\r
-\r
-#ifdef HAVE_CONFIG_H\r
-# include <config.h>\r
-#endif\r
-\r
-#include <glibmm/i18n.h>\r
-#include "livepatheffect-editor.h"\r
-#include "verbs.h"\r
-#include "selection.h"\r
-#include "sp-shape.h"\r
-#include "sp-path.h"\r
-#include "live_effects/effect.h"\r
-#include "live_effects/lpeobject.h"\r
-#include "gtkmm/widget.h"\r
-#include <vector>\r
-#include "inkscape.h"\r
-#include "desktop-handles.h"\r
-#include "desktop.h"\r
-#include "document-private.h"\r
-#include "xml/node.h"\r
-#include "xml/document.h"\r
-\r
-namespace Inkscape {\r
-class Application;\r
-\r
-namespace UI {\r
-namespace Dialog {\r
-\r
-\r
-/*####################\r
- * Callback functions\r
- */\r
-static void lpeeditor_selection_changed (Inkscape::Selection * selection, gpointer data)\r
-{\r
-    LivePathEffectEditor *lpeeditor = static_cast<LivePathEffectEditor *>(data);\r
-    lpeeditor->onSelectionChanged(selection);\r
-}\r
-\r
-static void lpeeditor_selection_modified (Inkscape::Selection *selection, guint flags, gpointer data)\r
-{\r
-    lpeeditor_selection_changed (selection, data);\r
-}\r
-\r
-\r
-static void lpeeditor_desktop_change(Inkscape::Application*, SPDesktop* desktop, void *data)\r
-{\r
-    if (!desktop) {\r
-        return;\r
-    }\r
-    LivePathEffectEditor* editor = reinterpret_cast<LivePathEffectEditor*>(data);\r
-    editor->setDesktop(desktop);\r
-}\r
-\r
-\r
-\r
-/*#######################\r
- * LivePathEffectEditor\r
- */\r
-LivePathEffectEditor::LivePathEffectEditor() \r
-    : Dialog ("dialogs.livepatheffect", SP_VERB_DIALOG_LIVE_PATH_EFFECT),\r
-      combo_effecttype(Inkscape::LivePathEffect::LPETypeConverter),\r
-      button_apply(_("_Apply"), _("Apply chosen effect to selection")),\r
-      button_remove(_("_Remove"), _("Remove effect from selection")),\r
-      effectwidget(NULL),\r
-      explain_label("", Gtk::ALIGN_CENTER),\r
-      effectapplication_frame(_("Apply new effect")),\r
-      effectcontrol_frame(_("Current effect")),\r
-      current_desktop(NULL)\r
-{\r
-    // Top level vbox\r
-    Gtk::VBox *vbox = get_vbox();\r
-    vbox->set_spacing(4);\r
-\r
-    effectapplication_vbox.set_spacing(4);\r
-    effectcontrol_vbox.set_spacing(4);\r
-\r
-    effectapplication_vbox.pack_start(combo_effecttype, true, true);\r
-    effectapplication_vbox.pack_start(button_apply, true, true);\r
-    effectapplication_vbox.pack_start(button_remove, true, true);\r
-    effectapplication_frame.add(effectapplication_vbox);\r
-\r
-    effectcontrol_vbox.pack_start(explain_label, true, true);\r
-    effectcontrol_frame.add(effectcontrol_vbox);\r
-\r
-    vbox->pack_start(effectapplication_frame, true, true);\r
-    vbox->pack_start(effectcontrol_frame, true, true);\r
-\r
-    // connect callback functions to buttons\r
-    button_apply.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onApply));\r
-    button_remove.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onRemove));\r
-\r
-    // connect callback functions to changes in selected desktop.\r
-    g_signal_connect( G_OBJECT(INKSCAPE), "activate_desktop",\r
-                       G_CALLBACK(lpeeditor_desktop_change), this);\r
-\r
-    g_signal_connect( G_OBJECT(INKSCAPE), "deactivate_desktop",\r
-                       G_CALLBACK(lpeeditor_desktop_change), this);\r
-\r
-    setDesktop(SP_ACTIVE_DESKTOP);\r
-    show_all_children();\r
-}\r
-\r
-LivePathEffectEditor::~LivePathEffectEditor() \r
-{\r
-    if (effectwidget) {\r
-        effectcontrol_vbox.remove(*effectwidget);\r
-        effectwidget = NULL;\r
-    }\r
-\r
-    if (current_desktop) {\r
-        selection_changed_connection.disconnect();\r
-        selection_modified_connection.disconnect();\r
-    }\r
-}\r
-\r
-void\r
-LivePathEffectEditor::showParams(LivePathEffect::Effect* effect)\r
-{\r
-    if (effectwidget) {\r
-        effectcontrol_vbox.remove(*effectwidget);\r
-        effectwidget = NULL;\r
-    }\r
-\r
-    explain_label.set_markup("<b>" + effect->getName() + "</b>");\r
-    effectwidget = effect->getWidget();\r
-    if (effectwidget) {\r
-        effectcontrol_vbox.pack_start(*effectwidget, true, true);\r
-    }\r
-\r
-    effectcontrol_vbox.show_all_children();\r
-    // fixme: do resizing of dialog \r
-}\r
-\r
-void\r
-LivePathEffectEditor::showText(Glib::ustring const &str)\r
-{\r
-    if (effectwidget) {\r
-        effectcontrol_vbox.remove(*effectwidget);\r
-        effectwidget = NULL;\r
-    }\r
-\r
-    explain_label.set_label(str);\r
-\r
-    // fixme: do resizing of dialog ?\r
-}\r
-\r
-void\r
-LivePathEffectEditor::set_sensitize_all(bool sensitive)\r
-{\r
-    combo_effecttype.set_sensitive(sensitive);\r
-    button_apply.set_sensitive(sensitive);\r
-    button_remove.set_sensitive(sensitive);\r
-}\r
-\r
-void\r
-LivePathEffectEditor::onSelectionChanged(Inkscape::Selection *sel)\r
-{\r
-    if ( sel && !sel->isEmpty() ) {\r
-        SPItem *item = sel->singleItem();\r
-        if ( item ) {\r
-            if ( SP_IS_SHAPE(item) ) {\r
-                SPShape *shape = SP_SHAPE(item);\r
-                LivePathEffectObject *lpeobj = sp_shape_get_livepatheffectobject(shape);\r
-                set_sensitize_all(true);\r
-                if (lpeobj) {\r
-                    if (lpeobj->lpe) {\r
-                        showParams(lpeobj->lpe);\r
-                    } else {\r
-                        showText(_("Unknown effect is applied"));\r
-                    }\r
-                } else {\r
-                    showText(_("No effect applied"));\r
-                    button_remove.set_sensitive(false);\r
-                }\r
-            } else {\r
-                showText(_("Item is not a shape"));\r
-                set_sensitize_all(false);\r
-            }\r
-        } else {\r
-            showText(_("Only one item can be selected"));\r
-            set_sensitize_all(false);\r
-        }\r
-    } else {\r
-        showText(_("Empty selection"));\r
-        set_sensitize_all(false);\r
-    }\r
-}\r
-\r
-void \r
-LivePathEffectEditor::setDesktop(SPDesktop *desktop)\r
-{\r
-\r
-    if ( desktop == current_desktop ) {\r
-        return;\r
-    }\r
-\r
-    if (current_desktop) {\r
-        selection_changed_connection.disconnect();\r
-        selection_modified_connection.disconnect();\r
-    }\r
-\r
-    current_desktop = desktop;\r
-    if (desktop) {\r
-        Inkscape::Selection *selection = sp_desktop_selection(desktop);\r
-        selection_changed_connection = selection->connectChanged(\r
-            sigc::bind (sigc::ptr_fun(&lpeeditor_selection_changed), this ) );\r
-        selection_modified_connection = selection->connectModified(\r
-            sigc::bind (sigc::ptr_fun(&lpeeditor_selection_modified), this ) );\r
-\r
-        onSelectionChanged(selection);\r
-    } else {\r
-        onSelectionChanged(NULL);\r
-    }\r
-}\r
-\r
-\r
-\r
-\r
-/*########################################################################\r
-# BUTTON CLICK HANDLERS    (callbacks)\r
-########################################################################*/\r
-\r
-void\r
-LivePathEffectEditor::onApply()\r
-{\r
-    Inkscape::Selection *sel = _getSelection();\r
-    if ( sel && !sel->isEmpty() ) {\r
-        SPItem *item = sel->singleItem();\r
-        if ( item && SP_IS_SHAPE(item) ) {\r
-            SPDocument *doc = current_desktop->doc();\r
-\r
-            const Util::EnumData<LivePathEffect::EffectType>* data = combo_effecttype.get_active_data();\r
-            if (!data) return;\r
-\r
-            Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);\r
-            Inkscape::XML::Node *repr = xml_doc->createElement("inkscape:path-effect");\r
-            repr->setAttribute("effect", data->key.c_str() );\r
-\r
-            SP_OBJECT_REPR(SP_DOCUMENT_DEFS(doc))->addChild(repr, NULL); // adds to <defs> and assigns the 'id' attribute\r
-            const gchar * repr_id = repr->attribute("id");\r
-            Inkscape::GC::release(repr);\r
-\r
-            gchar *href = g_strdup_printf("#%s", repr_id);\r
-            sp_shape_set_path_effect(SP_SHAPE(item), href);\r
-            g_free(href);\r
-\r
-            // make sure there is an original-d for paths!!!\r
-            if ( SP_IS_PATH(item) ) {\r
-                Inkscape::XML::Node *pathrepr = SP_OBJECT_REPR(item);\r
-                if ( ! pathrepr->attribute("inkscape:original-d") ) {\r
-                    pathrepr->setAttribute("inkscape:original-d", pathrepr->attribute("d"));\r
-                }\r
-            }\r
-\r
-            sp_document_done(doc, SP_VERB_DIALOG_LIVE_PATH_EFFECT, \r
-                             _("Create and apply live effect"));\r
-        }\r
-    }\r
-}\r
-\r
-void\r
-LivePathEffectEditor::onRemove()\r
-{\r
-    Inkscape::Selection *sel = _getSelection();\r
-    if ( sel && !sel->isEmpty() ) {\r
-        SPItem *item = sel->singleItem();\r
-        if ( item && SP_IS_SHAPE(item) ) {\r
-            sp_shape_remove_path_effect(SP_SHAPE(item));\r
-            sp_document_done ( sp_desktop_document (current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT, \r
-                               _("Remove live path effect") );\r
-        }\r
-    }\r
-}\r
-\r
-\r
-\r
-} // namespace Dialog\r
-} // namespace UI\r
-} // namespace Inkscape\r
-\r
+/**
+ * \brief LivePathEffect dialog
+ *
+ * Authors:
+ *   Johan Engelen <j.b.c.engelen@utwente.nl>
+ *
+ * Copyright (C) 2007 Author
+ *
+ * Released under GNU GPL.  Read the file 'COPYING' for more information.
+ */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <glibmm/i18n.h>
+#include "livepatheffect-editor.h"
+#include "verbs.h"
+#include "selection.h"
+#include "sp-shape.h"
+#include "sp-path.h"
+#include "live_effects/effect.h"
+#include "live_effects/lpeobject.h"
+#include "gtkmm/widget.h"
+#include <vector>
+#include "inkscape.h"
+#include "desktop-handles.h"
+#include "desktop.h"
+#include "document-private.h"
+#include "xml/node.h"
+#include "xml/document.h"
+
+namespace Inkscape {
+class Application;
+
+namespace UI {
+namespace Dialog {
+
+
+/*####################
+ * Callback functions
+ */
+static void lpeeditor_selection_changed (Inkscape::Selection * selection, gpointer data)
+{
+    LivePathEffectEditor *lpeeditor = static_cast<LivePathEffectEditor *>(data);
+    lpeeditor->onSelectionChanged(selection);
+}
+
+static void lpeeditor_selection_modified( Inkscape::Selection *selection, guint /*flags*/, gpointer data )
+{
+    lpeeditor_selection_changed (selection, data);
+}
+
+
+/*#######################
+ * LivePathEffectEditor
+ */
+
+LivePathEffectEditor::LivePathEffectEditor() 
+    : UI::Widget::Panel("", "dialogs.livepatheffect", SP_VERB_DIALOG_LIVE_PATH_EFFECT),
+      combo_effecttype(Inkscape::LivePathEffect::LPETypeConverter),
+      button_apply(_("_Apply"), _("Apply chosen effect to selection")),
+      button_remove(_("_Remove"), _("Remove effect from selection")),
+      effectwidget(NULL),
+      explain_label("", Gtk::ALIGN_CENTER),
+      effectapplication_frame(_("Apply new effect")),
+      effectcontrol_frame(_("Current effect")),
+      current_desktop(NULL)
+{
+    Gtk::Box *contents = _getContents();
+    contents->set_spacing(4);
+
+    effectapplication_hbox.set_spacing(4);
+    effectcontrol_vbox.set_spacing(4);
+
+    effectapplication_hbox.pack_start(combo_effecttype, true, true);
+    effectapplication_hbox.pack_start(button_apply, true, true);
+    effectapplication_frame.add(effectapplication_hbox);
+
+    effectcontrol_vbox.pack_start(explain_label, true, true);
+    effectcontrol_vbox.pack_end(button_remove, true, true);
+    effectcontrol_frame.add(effectcontrol_vbox);
+
+    contents->pack_start(effectapplication_frame, true, true);
+    contents->pack_start(effectcontrol_frame, true, true);
+
+    // connect callback functions to buttons
+    button_apply.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onApply));
+    button_remove.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onRemove));
+
+    show_all_children();
+
+    button_remove.hide();
+}
+
+LivePathEffectEditor::~LivePathEffectEditor() 
+{
+    if (effectwidget) {
+        effectcontrol_vbox.remove(*effectwidget);
+        effectwidget = NULL;
+    }
+
+    if (current_desktop) {
+        selection_changed_connection.disconnect();
+        selection_modified_connection.disconnect();
+    }
+}
+
+void
+LivePathEffectEditor::showParams(LivePathEffect::Effect* effect)
+{
+    if (effectwidget) {
+        effectcontrol_vbox.remove(*effectwidget);
+        effectwidget = NULL;
+    }
+
+    explain_label.set_markup("<b>" + effect->getName() + "</b>");
+    effectwidget = effect->getWidget();
+    if (effectwidget) {
+        effectcontrol_vbox.pack_start(*effectwidget, true, true);
+    }
+    button_remove.show();
+
+    effectcontrol_vbox.show_all_children();
+    // fixme: do resizing of dialog 
+}
+
+void
+LivePathEffectEditor::showText(Glib::ustring const &str)
+{
+    if (effectwidget) {
+        effectcontrol_vbox.remove(*effectwidget);
+        effectwidget = NULL;
+    }
+
+    explain_label.set_label(str);
+    button_remove.hide();
+
+    // fixme: do resizing of dialog ?
+}
+
+void
+LivePathEffectEditor::set_sensitize_all(bool sensitive)
+{
+    combo_effecttype.set_sensitive(sensitive);
+    button_apply.set_sensitive(sensitive);
+    button_remove.set_sensitive(sensitive);
+}
+
+void
+LivePathEffectEditor::onSelectionChanged(Inkscape::Selection *sel)
+{
+    if ( sel && !sel->isEmpty() ) {
+        SPItem *item = sel->singleItem();
+        if ( item ) {
+            if ( SP_IS_SHAPE(item) ) {
+                SPShape *shape = SP_SHAPE(item);
+                LivePathEffectObject *lpeobj = sp_shape_get_livepatheffectobject(shape);
+                set_sensitize_all(true);
+                if (lpeobj) {
+                    if (lpeobj->lpe) {
+                        showParams(lpeobj->lpe);
+                    } else {
+                        showText(_("Unknown effect is applied"));
+                    }
+                } else {
+                    showText(_("No effect applied"));
+                    button_remove.set_sensitive(false);
+                }
+            } else {
+                showText(_("Item is not a shape or path"));
+                set_sensitize_all(false);
+            }
+        } else {
+            showText(_("Only one item can be selected"));
+            set_sensitize_all(false);
+        }
+    } else {
+        showText(_("Empty selection"));
+        set_sensitize_all(false);
+    }
+}
+
+void 
+LivePathEffectEditor::setDesktop(SPDesktop *desktop)
+{
+    Panel::setDesktop(desktop);
+
+    if ( desktop == current_desktop ) {
+        return;
+    }
+
+    if (current_desktop) {
+        selection_changed_connection.disconnect();
+        selection_modified_connection.disconnect();
+    }
+
+    current_desktop = desktop;
+    if (desktop) {
+        Inkscape::Selection *selection = sp_desktop_selection(desktop);
+        selection_changed_connection = selection->connectChanged(
+            sigc::bind (sigc::ptr_fun(&lpeeditor_selection_changed), this ) );
+        selection_modified_connection = selection->connectModified(
+            sigc::bind (sigc::ptr_fun(&lpeeditor_selection_modified), this ) );
+
+        onSelectionChanged(selection);
+    } else {
+        onSelectionChanged(NULL);
+    }
+}
+
+
+
+
+/*########################################################################
+# BUTTON CLICK HANDLERS    (callbacks)
+########################################################################*/
+
+// TODO:  factor out the effect applying code which can be called from anywhere. (selection-chemistry.cpp also needs it)
+
+void
+LivePathEffectEditor::onApply()
+{
+    Inkscape::Selection *sel = _getSelection();
+    if ( sel && !sel->isEmpty() ) {
+        SPItem *item = sel->singleItem();
+        if ( item && SP_IS_SHAPE(item) ) {
+            SPDocument *doc = current_desktop->doc();
+
+            const Util::EnumData<LivePathEffect::EffectType>* data = combo_effecttype.get_active_data();
+            if (!data) return;
+
+            Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
+            Inkscape::XML::Node *repr = xml_doc->createElement("inkscape:path-effect");
+            repr->setAttribute("effect", data->key.c_str() );
+
+            SP_OBJECT_REPR(SP_DOCUMENT_DEFS(doc))->addChild(repr, NULL); // adds to <defs> and assigns the 'id' attribute
+            const gchar * repr_id = repr->attribute("id");
+            Inkscape::GC::release(repr);
+
+            gchar *href = g_strdup_printf("#%s", repr_id);
+            sp_shape_set_path_effect(SP_SHAPE(item), href);
+            g_free(href);
+
+            // make sure there is an original-d for paths!!!
+            if ( SP_IS_PATH(item) ) {
+                Inkscape::XML::Node *pathrepr = SP_OBJECT_REPR(item);
+                if ( ! pathrepr->attribute("inkscape:original-d") ) {
+                    pathrepr->setAttribute("inkscape:original-d", pathrepr->attribute("d"));
+                }
+            }
+
+            LivePathEffectObject *lpeobj = sp_shape_get_livepatheffectobject(SP_SHAPE(item));
+            if (lpeobj && lpeobj->lpe) {
+                lpeobj->lpe->resetDefaults(item);
+            }
+
+            sp_document_done(doc, SP_VERB_DIALOG_LIVE_PATH_EFFECT, 
+                             _("Create and apply path effect"));
+        }
+    }
+}
+
+void
+LivePathEffectEditor::onRemove()
+{
+    Inkscape::Selection *sel = _getSelection();
+    if ( sel && !sel->isEmpty() ) {
+        SPItem *item = sel->singleItem();
+        if ( item && SP_IS_SHAPE(item) ) {
+            sp_shape_remove_path_effect(SP_SHAPE(item));
+            sp_document_done ( sp_desktop_document (current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT, 
+                               _("Remove path effect") );
+        }
+    }
+}
+
+
+
+} // namespace Dialog
+} // namespace UI
+} // namespace Inkscape
+