lpeobject.cpp revision e72ec96ad2ab870dc0af88bc559c86b32c63dd9c
1029N/A#define INKSCAPE_LIVEPATHEFFECT_OBJECT_CPP
1029N/A
1407N/A/*
1029N/A * Copyright (C) Johan Engelen 2007-2008 <j.b.c.engelen@utwente.nl>
246N/A *
246N/A * Released under GNU GPL, read the file 'COPYING' for more information
246N/A */
281N/A
281N/A#include "live_effects/lpeobject.h"
246N/A
131N/A#include "live_effects/effect.h"
131N/A
131N/A#include "xml/repr.h"
131N/A#include "xml/node-event-vector.h"
1086N/A#include "sp-object.h"
131N/A#include "attributes.h"
725N/A#include "document.h"
131N/A#include "document-private.h"
758N/A
131N/A#include <glibmm/i18n.h>
131N/A
131N/A//#define LIVEPATHEFFECT_VERBOSE
131N/A
500N/Astatic void livepatheffect_on_repr_attr_changed (Inkscape::XML::Node * repr, const gchar *key, const gchar *oldval, const gchar *newval, bool is_interactive, void * data);
131N/A
131N/Astatic SPObjectClass *livepatheffect_parent_class;
131N/A/**
131N/A * Registers the LivePathEffect class with Gdk and returns its type number.
131N/A */
131N/AGType
131N/ALivePathEffectObject::livepatheffect_get_type ()
144N/A{
131N/A static GType livepatheffect_type = 0;
1029N/A
131N/A if (!livepatheffect_type) {
246N/A GTypeInfo livepatheffect_info = {
929N/A sizeof (LivePathEffectObjectClass),
1097N/A NULL, NULL,
1456N/A (GClassInitFunc) LivePathEffectObject::livepatheffect_class_init,
1425N/A NULL, NULL,
1318N/A sizeof (LivePathEffectObject),
1318N/A 16,
1318N/A (GInstanceInitFunc) LivePathEffectObject::livepatheffect_init,
1318N/A NULL,
131N/A };
131N/A livepatheffect_type = g_type_register_static (SP_TYPE_OBJECT, "LivePathEffectObject", &livepatheffect_info, (GTypeFlags)0);
131N/A }
131N/A return livepatheffect_type;
131N/A}
246N/A
929N/Astatic Inkscape::XML::NodeEventVector const livepatheffect_repr_events = {
1097N/A NULL, /* child_added */
131N/A NULL, /* child_removed */
340N/A livepatheffect_on_repr_attr_changed,
1318N/A NULL, /* content_changed */
1318N/A NULL /* order_changed */
340N/A};
131N/A
131N/A
131N/A/**
1029N/A * Callback to initialize livepatheffect vtable.
1029N/A */
920N/Avoid
920N/ALivePathEffectObject::livepatheffect_class_init(LivePathEffectObjectClass *klass)
131N/A{
131N/A SPObjectClass *sp_object_class = (SPObjectClass *) klass;
131N/A
214N/A livepatheffect_parent_class = (SPObjectClass *) g_type_class_ref(SP_TYPE_OBJECT);
214N/A
131N/A sp_object_class->build = livepatheffect_build;
131N/A sp_object_class->release = livepatheffect_release;
131N/A
131N/A sp_object_class->set = livepatheffect_set;
131N/A sp_object_class->write = livepatheffect_write;
131N/A}
131N/A
131N/A/**
131N/A * Callback to initialize livepatheffect object.
131N/A */
131N/Avoid
927N/ALivePathEffectObject::livepatheffect_init(LivePathEffectObject *lpeobj)
131N/A{
131N/A#ifdef LIVEPATHEFFECT_VERBOSE
131N/A g_message("Init livepatheffectobject");
131N/A#endif
131N/A lpeobj->effecttype = Inkscape::LivePathEffect::INVALID_LPE;
131N/A lpeobj->lpe = NULL;
131N/A
131N/A lpeobj->effecttype_set = false;
1318N/A}
131N/A
131N/A/**
131N/A * Virtual build: set livepatheffect attributes from its associated XML node.
131N/A */
131N/Avoid
131N/ALivePathEffectObject::livepatheffect_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
131N/A{
1380N/A#ifdef LIVEPATHEFFECT_VERBOSE
131N/A g_message("Build livepatheffect");
131N/A#endif
895N/A g_assert(object != NULL);
1456N/A g_assert(SP_IS_OBJECT(object));
1456N/A
131N/A if (((SPObjectClass *) livepatheffect_parent_class)->build)
131N/A (* ((SPObjectClass *) livepatheffect_parent_class)->build)(object, document, repr);
131N/A
131N/A sp_object_read_attr(object, "effect");
if (repr) {
repr->addListener (&livepatheffect_repr_events, object);
}
/* Register ourselves, is this necessary? */
// sp_document_add_resource(document, "path-effect", object);
}
/**
* Virtual release of livepatheffect members before destruction.
*/
void
LivePathEffectObject::livepatheffect_release(SPObject *object)
{
#ifdef LIVEPATHEFFECT_VERBOSE
g_print("Releasing livepatheffect");
#endif
LivePathEffectObject *lpeobj = LIVEPATHEFFECT(object);
SP_OBJECT_REPR(object)->removeListenerByData(object);
/*
if (SP_OBJECT_DOCUMENT(object)) {
// Unregister ourselves
sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "livepatheffect", SP_OBJECT(object));
}
if (gradient->ref) {
gradient->modified_connection.disconnect();
gradient->ref->detach();
delete gradient->ref;
gradient->ref = NULL;
}
gradient->modified_connection.~connection();
*/
if (lpeobj->lpe) {
delete lpeobj->lpe;
lpeobj->lpe = NULL;
}
lpeobj->effecttype = Inkscape::LivePathEffect::INVALID_LPE;
if (((SPObjectClass *) livepatheffect_parent_class)->release)
((SPObjectClass *) livepatheffect_parent_class)->release(object);
}
/**
* Virtual set: set attribute to value.
*/
void
LivePathEffectObject::livepatheffect_set(SPObject *object, unsigned key, gchar const *value)
{
LivePathEffectObject *lpeobj = LIVEPATHEFFECT(object);
#ifdef LIVEPATHEFFECT_VERBOSE
g_print("Set livepatheffect");
#endif
switch (key) {
case SP_PROP_PATH_EFFECT:
if (lpeobj->lpe) {
delete lpeobj->lpe;
lpeobj->lpe = NULL;
}
if ( value && Inkscape::LivePathEffect::LPETypeConverter.is_valid_key(value) ) {
lpeobj->effecttype = Inkscape::LivePathEffect::LPETypeConverter.get_id_from_key(value);
lpeobj->lpe = Inkscape::LivePathEffect::Effect::New(lpeobj->effecttype, lpeobj);
lpeobj->effecttype_set = true;
} else {
lpeobj->effecttype = Inkscape::LivePathEffect::INVALID_LPE;
lpeobj->effecttype_set = false;
}
object->requestModified(SP_OBJECT_MODIFIED_FLAG);
break;
}
if (((SPObjectClass *) livepatheffect_parent_class)->set) {
((SPObjectClass *) livepatheffect_parent_class)->set(object, key, value);
}
}
/**
* Virtual write: write object attributes to repr.
*/
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");
#endif
LivePathEffectObject *lpeobj = LIVEPATHEFFECT(object);
if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
repr = xml_doc->createElement("inkscape:path-effect");
}
if ((flags & SP_OBJECT_WRITE_ALL) || lpeobj->lpe) {
repr->setAttribute("effect", Inkscape::LivePathEffect::LPETypeConverter.get_key(lpeobj->effecttype).c_str());
lpeobj->lpe->writeParamsToSVG();
}
if (((SPObjectClass *) livepatheffect_parent_class)->write)
(* ((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*/,
void * data )
{
#ifdef LIVEPATHEFFECT_VERBOSE
g_print("livepatheffect_on_repr_attr_changed");
#endif
if (!data)
return;
LivePathEffectObject *lpeobj = (LivePathEffectObject*) data;
if (!lpeobj->get_lpe())
return;
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)
* Check out sp_lpe_item_fork_path_effects_if_necessary !
*/
LivePathEffectObject *
LivePathEffectObject::fork_private_if_necessary(unsigned int nr_of_allowed_users)
{
if (SP_OBJECT_HREFCOUNT(this) > nr_of_allowed_users) {
SPDocument *doc = SP_OBJECT_DOCUMENT(this);
Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
Inkscape::XML::Node *dup_repr = SP_OBJECT_REPR (this)->duplicate(xml_doc);
SP_OBJECT_REPR (SP_DOCUMENT_DEFS (doc))->addChild(dup_repr, NULL);
LivePathEffectObject *lpeobj_new = LIVEPATHEFFECT( doc->getObjectByRepr(dup_repr) );
Inkscape::GC::release(dup_repr);
return lpeobj_new;
}
return this;
}
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :