Code

SPDocument->Document
[inkscape.git] / src / live_effects / lpeobject.cpp
index 90a49e2ff7a75da3c6391281e406c2d71ac1adb9..c7ca5e4aab448e79a3dc54e3daf4db81af270801 100644 (file)
@@ -1,33 +1,26 @@
 #define INKSCAPE_LIVEPATHEFFECT_OBJECT_CPP
 
 /*
- * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>
+ * Copyright (C) Johan Engelen 2007-2008 <j.b.c.engelen@utwente.nl>
  *
  * Released under GNU GPL, read the file 'COPYING' for more information
  */
 
+#include "live_effects/lpeobject.h"
+
+#include "live_effects/effect.h"
+
 #include "xml/repr.h"
 #include "xml/node-event-vector.h"
 #include "sp-object.h"
 #include "attributes.h"
-
 #include "document.h"
-#include <glibmm/i18n.h>
+#include "document-private.h"
 
-#include "live_effects/lpeobject.h"
-#include "live_effects/effect.h"
+#include <glibmm/i18n.h>
 
 //#define LIVEPATHEFFECT_VERBOSE
 
-static void livepatheffect_class_init(LivePathEffectObjectClass *klass);
-static void livepatheffect_init(LivePathEffectObject *stop);
-
-static void livepatheffect_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
-static void livepatheffect_release(SPObject *object);
-
-static void livepatheffect_set(SPObject *object, unsigned key, gchar const *value);
-static Inkscape::XML::Node *livepatheffect_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
-
 static void livepatheffect_on_repr_attr_changed (Inkscape::XML::Node * repr, const gchar *key, const gchar *oldval, const gchar *newval, bool is_interactive, void * data);
 
 static SPObjectClass *livepatheffect_parent_class;
@@ -35,7 +28,7 @@ static SPObjectClass *livepatheffect_parent_class;
  * Registers the LivePathEffect class with Gdk and returns its type number.
  */
 GType
-livepatheffect_get_type ()
+LivePathEffectObject::livepatheffect_get_type ()
 {
     static GType livepatheffect_type = 0;
 
@@ -43,11 +36,11 @@ livepatheffect_get_type ()
         GTypeInfo livepatheffect_info = {
             sizeof (LivePathEffectObjectClass),
             NULL, NULL,
-            (GClassInitFunc) livepatheffect_class_init,
+            (GClassInitFunc) LivePathEffectObject::livepatheffect_class_init,
             NULL, NULL,
             sizeof (LivePathEffectObject),
             16,
-            (GInstanceInitFunc) livepatheffect_init,
+            (GInstanceInitFunc) LivePathEffectObject::livepatheffect_init,
             NULL,
         };
         livepatheffect_type = g_type_register_static (SP_TYPE_OBJECT, "LivePathEffectObject", &livepatheffect_info, (GTypeFlags)0);
@@ -67,8 +60,8 @@ static Inkscape::XML::NodeEventVector const livepatheffect_repr_events = {
 /**
  * Callback to initialize livepatheffect vtable.
  */
-static void 
-livepatheffect_class_init(LivePathEffectObjectClass *klass)
+void
+LivePathEffectObject::livepatheffect_class_init(LivePathEffectObjectClass *klass)
 {
     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
 
@@ -84,8 +77,8 @@ livepatheffect_class_init(LivePathEffectObjectClass *klass)
 /**
  * Callback to initialize livepatheffect object.
  */
-static void
-livepatheffect_init(LivePathEffectObject *lpeobj)
+void
+LivePathEffectObject::livepatheffect_init(LivePathEffectObject *lpeobj)
 {
 #ifdef LIVEPATHEFFECT_VERBOSE
     g_message("Init livepatheffectobject");
@@ -99,8 +92,8 @@ livepatheffect_init(LivePathEffectObject *lpeobj)
 /**
  * Virtual build: set livepatheffect attributes from its associated XML node.
  */
-static void 
-livepatheffect_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
+void
+LivePathEffectObject::livepatheffect_build(SPObject *object, Document *document, Inkscape::XML::Node *repr)
 {
 #ifdef LIVEPATHEFFECT_VERBOSE
     g_message("Build livepatheffect");
@@ -124,8 +117,8 @@ livepatheffect_build(SPObject *object, SPDocument *document, Inkscape::XML::Node
 /**
  * Virtual release of livepatheffect members before destruction.
  */
-static void
-livepatheffect_release(SPObject *object)
+void
+LivePathEffectObject::livepatheffect_release(SPObject *object)
 {
 #ifdef LIVEPATHEFFECT_VERBOSE
     g_print("Releasing livepatheffect");
@@ -166,8 +159,8 @@ livepatheffect_release(SPObject *object)
 /**
  * Virtual set: set attribute to value.
  */
-static void
-livepatheffect_set(SPObject *object, unsigned key, gchar const *value)
+void
+LivePathEffectObject::livepatheffect_set(SPObject *object, unsigned key, gchar const *value)
 {
     LivePathEffectObject *lpeobj = LIVEPATHEFFECT(object);
 #ifdef LIVEPATHEFFECT_VERBOSE
@@ -180,11 +173,9 @@ livepatheffect_set(SPObject *object, unsigned key, gchar const *value)
                 lpeobj->lpe = NULL;
             }
 
-            if (value) {
+            if ( value && Inkscape::LivePathEffect::LPETypeConverter.is_valid_key(value) ) {
                 lpeobj->effecttype = Inkscape::LivePathEffect::LPETypeConverter.get_id_from_key(value);
-                if (lpeobj->effecttype != Inkscape::LivePathEffect::INVALID_LPE) {
-                    lpeobj->lpe = Inkscape::LivePathEffect::Effect::New(lpeobj->effecttype, lpeobj);
-                }
+                lpeobj->lpe = Inkscape::LivePathEffect::Effect::New(lpeobj->effecttype, lpeobj);
                 lpeobj->effecttype_set = true;
             } else {
                 lpeobj->effecttype = Inkscape::LivePathEffect::INVALID_LPE;
@@ -202,8 +193,8 @@ livepatheffect_set(SPObject *object, unsigned key, gchar const *value)
 /**
  * Virtual write: write object attributes to repr.
  */
-static Inkscape::XML::Node *
-livepatheffect_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
+Inkscape::XML::Node *
+LivePathEffectObject::livepatheffect_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
 {
 #ifdef LIVEPATHEFFECT_VERBOSE
     g_print("Write livepatheffect");
@@ -212,27 +203,27 @@ livepatheffect_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
     LivePathEffectObject *lpeobj = LIVEPATHEFFECT(object);
 
     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
-        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
         repr = xml_doc->createElement("inkscape:path-effect");
     }
 
-    if ((flags & SP_OBJECT_WRITE_ALL) || lpeobj->effecttype_set)
+    if ((flags & SP_OBJECT_WRITE_ALL) || lpeobj->lpe) {
         repr->setAttribute("effect", Inkscape::LivePathEffect::LPETypeConverter.get_key(lpeobj->effecttype).c_str());
 
-//    lpeobj->lpe->write(repr); something like this.
+        lpeobj->lpe->writeParamsToSVG();
+    }
 
     if (((SPObjectClass *) livepatheffect_parent_class)->write)
-        (* ((SPObjectClass *) livepatheffect_parent_class)->write)(object, repr, flags);
+        (* ((SPObjectClass *) livepatheffect_parent_class)->write)(object, xml_doc, repr, flags);
 
     return repr;
 }
 
-static void 
-livepatheffect_on_repr_attr_changed ( Inkscape::XML::Node * repr, 
-                                      const gchar *key, 
-                                      const gchar *oldval, 
-                                      const gchar *newval, 
-                                      bool is_interactive, 
+static void
+livepatheffect_on_repr_attr_changed ( Inkscape::XML::Node * /*repr*/,
+                                      const gchar *key,
+                                      const gchar */*oldval*/,
+                                      const gchar *newval,
+                                      bool /*is_interactive*/,
                                       void * data )
 {
 #ifdef LIVEPATHEFFECT_VERBOSE
@@ -243,14 +234,33 @@ livepatheffect_on_repr_attr_changed ( Inkscape::XML::Node * repr,
         return;
 
     LivePathEffectObject *lpeobj = (LivePathEffectObject*) data;
-    if (!lpeobj->lpe) 
+    if (!lpeobj->get_lpe())
         return;
 
-    lpeobj->lpe->setParameter(key, newval);
+    lpeobj->get_lpe()->setParameter(key, newval);
 
     lpeobj->requestModified(SP_OBJECT_MODIFIED_FLAG);
 }
 
+/**
+ * If this has other users, create a new private duplicate and return it
+ * returns 'this' when no forking was necessary (and therefore no duplicate was made)
+ */
+LivePathEffectObject *
+LivePathEffectObject::fork_private_if_necessary(unsigned int nr_of_allowed_users)
+{
+    if (SP_OBJECT_HREFCOUNT(this) > nr_of_allowed_users) {
+        Document *doc = SP_OBJECT_DOCUMENT(this);
+        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
+
+        Inkscape::XML::Node *repr = SP_OBJECT_REPR (this)->duplicate(xml_doc);
+        SP_OBJECT_REPR (SP_DOCUMENT_DEFS (doc))->addChild(repr, NULL);
+        LivePathEffectObject *lpeobj_new = (LivePathEffectObject *) doc->getObjectByRepr(repr);
+        Inkscape::GC::release(repr);
+        return lpeobj_new;
+    }
+    return this;
+}
 
 /*
   Local Variables: