X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fsp-object.cpp;h=6526435de98101f25ae6fc984d79a4876648b52b;hb=fb7b063b3b4603551cac8bb4e8caafb2b7654db8;hp=5f6bf779fabc2d90c011ce32469ee5ddf736a517;hpb=ec988e4e64be7b5c5de708799bb93e818a15f8d4;p=inkscape.git diff --git a/src/sp-object.cpp b/src/sp-object.cpp index 5f6bf779f..6526435de 100644 --- a/src/sp-object.cpp +++ b/src/sp-object.cpp @@ -44,6 +44,10 @@ #include "xml/repr.h" #include "xml/node-fns.h" #include "debug/event-tracker.h" +#include "debug/simple-event.h" +#include "debug/demangle.h" +#include "util/share.h" +#include "util/format.h" #include "algorithms/longest-common-suffix.h" using std::memcpy; @@ -95,8 +99,6 @@ static gchar *sp_object_get_unique_id(SPObject *object, gchar const *defid); guint update_in_progress = 0; // guard against update-during-update -enum {RELEASE, MODIFIED, LAST_SIGNAL}; - Inkscape::XML::NodeEventVector object_event_vector = { sp_object_repr_child_added, sp_object_repr_child_removed, @@ -106,7 +108,6 @@ Inkscape::XML::NodeEventVector object_event_vector = { }; static GObjectClass *parent_class; -static guint object_signals[LAST_SIGNAL] = {0}; /** * Registers the SPObject class with Gdk and returns its type number. @@ -143,21 +144,6 @@ sp_object_class_init(SPObjectClass *klass) parent_class = (GObjectClass *) g_type_class_ref(G_TYPE_OBJECT); - object_signals[RELEASE] = g_signal_new("release", - G_TYPE_FROM_CLASS(klass), - (GSignalFlags)(G_SIGNAL_RUN_CLEANUP | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS), - G_STRUCT_OFFSET(SPObjectClass, release), - NULL, NULL, - sp_marshal_VOID__VOID, - G_TYPE_NONE, 0); - object_signals[MODIFIED] = g_signal_new("modified", - G_TYPE_FROM_CLASS(klass), - G_SIGNAL_RUN_FIRST, - G_STRUCT_OFFSET(SPObjectClass, modified), - NULL, NULL, - sp_marshal_NONE__UINT, - G_TYPE_NONE, 1, G_TYPE_UINT); - object_class->finalize = sp_object_finalize; klass->child_added = sp_object_child_added; @@ -187,14 +173,21 @@ sp_object_init(SPObject *object) object->parent = object->next = NULL; object->repr = NULL; object->id = NULL; - object->style = NULL; object->_collection_policy = SPObject::COLLECT_WITH_PARENT; + new (&object->_release_signal) sigc::signal(); + new (&object->_modified_signal) sigc::signal(); new (&object->_delete_signal) sigc::signal(); new (&object->_position_changed_signal) sigc::signal(); object->_successor = NULL; + // FIXME: now we create style for all objects, but per SVG, only the following can have style attribute: + // vg, g, defs, desc, title, symbol, use, image, switch, path, rect, circle, ellipse, line, polyline, + // polygon, text, tspan, tref, textPath, altGlyph, glyphRef, marker, linearGradient, radialGradient, + // stop, pattern, clipPath, mask, filter, feImage, a, font, glyph, missing-glyph, foreignObject + object->style = sp_style_new_from_object(object); + object->_label = NULL; object->_default_label = NULL; } @@ -221,60 +214,42 @@ sp_object_finalize(GObject *object) (* ((GObjectClass *) (parent_class))->finalize)(object); } + spobject->_release_signal.~signal(); + spobject->_modified_signal.~signal(); spobject->_delete_signal.~signal(); spobject->_position_changed_signal.~signal(); } namespace { -Inkscape::Util::shared_ptr stringify(SPObject *obj) { - char *temp=g_strdup_printf("%p", obj); - Inkscape::Util::shared_ptr result=Inkscape::Util::share_string(temp); - g_free(temp); - return result; -} +namespace Debug = Inkscape::Debug; +namespace Util = Inkscape::Util; -Inkscape::Util::shared_ptr stringify(unsigned n) { - char *temp=g_strdup_printf("%u", n); - Inkscape::Util::shared_ptr result=Inkscape::Util::share_string(temp); - g_free(temp); - return result; -} +typedef Debug::SimpleEvent BaseRefCountEvent; -class RefEvent : public Inkscape::Debug::Event { +class RefCountEvent : public BaseRefCountEvent { public: - enum Type { REF, UNREF }; + RefCountEvent(SPObject *object, int bias, Util::ptr_shared name) + : BaseRefCountEvent(name) + { + _addProperty("object", Util::format("%p", object)); + _addProperty("class", Debug::demangle(g_type_name(G_TYPE_FROM_INSTANCE(object)))); + _addProperty("new-refcount", Util::format("%d", G_OBJECT(object)->ref_count + bias)); + } +}; - RefEvent(SPObject *object, Type type) - : _object(stringify(object)), _refcount(G_OBJECT(object)->ref_count), - _type(type) +class RefEvent : public RefCountEvent { +public: + RefEvent(SPObject *object) + : RefCountEvent(object, 1, Util::share_static_string("sp-object-ref")) {} +}; - static Category category() { return REFCOUNT; } - - Inkscape::Util::shared_ptr name() const { - if ( _type == REF) { - return Inkscape::Util::share_static_string("sp-object-ref"); - } else { - return Inkscape::Util::share_static_string("sp-object-unref"); - } - } - unsigned propertyCount() const { return 2; } - PropertyPair property(unsigned index) const { - switch (index) { - case 0: - return PropertyPair("object", _object); - case 1: - return PropertyPair("refcount", stringify( _type == REF ? _refcount + 1 : _refcount - 1 )); - default: - return PropertyPair(); - } - } - -private: - Inkscape::Util::shared_ptr _object; - unsigned _refcount; - Type _type; +class UnrefEvent : public RefCountEvent { +public: + UnrefEvent(SPObject *object) + : RefCountEvent(object, -1, Util::share_static_string("sp-object-unref")) + {} }; } @@ -293,11 +268,8 @@ sp_object_ref(SPObject *object, SPObject *owner) g_return_val_if_fail(SP_IS_OBJECT(object), NULL); g_return_val_if_fail(!owner || SP_IS_OBJECT(owner), NULL); - Inkscape::Debug::EventTracker<> tracker; - tracker.set(object, RefEvent::REF); - + Inkscape::Debug::EventTracker tracker(object); g_object_ref(G_OBJECT(object)); - return object; } @@ -316,11 +288,8 @@ sp_object_unref(SPObject *object, SPObject *owner) g_return_val_if_fail(SP_IS_OBJECT(object), NULL); g_return_val_if_fail(!owner || SP_IS_OBJECT(owner), NULL); - Inkscape::Debug::EventTracker<> tracker; - tracker.set(object, RefEvent::UNREF); - + Inkscape::Debug::EventTracker tracker(object); g_object_unref(G_OBJECT(object)); - return NULL; } @@ -480,6 +449,22 @@ SPObject::appendChildRepr(Inkscape::XML::Node *repr) { } } +/** + * Retrieves the children as a GSList object, optionally ref'ing the children + * in the process, if add_ref is specified. + */ +GSList *SPObject::childList(bool add_ref, Action) { + GSList *l = NULL; + for (SPObject *child = sp_object_first_child(this) ; child != NULL; child = SP_OBJECT_NEXT(child) ) { + if (add_ref) + g_object_ref (G_OBJECT (child)); + + l = g_slist_prepend (l, child); + } + return l; + +} + /** Gets the label property for the object or a default if no label * is defined. */ @@ -659,7 +644,7 @@ sp_object_detach(SPObject *parent, SPObject *object) { g_return_if_fail(SP_IS_OBJECT(object)); g_return_if_fail(object->parent == parent); - sp_object_invoke_release(object); + object->releaseReferences(); SPObject *prev=NULL; for ( SPObject *child = parent->children ; child && child != object ; @@ -757,8 +742,9 @@ sp_object_remove_child(SPObject *object, Inkscape::XML::Node *child) { debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object)); SPObject *ochild = sp_object_get_child_by_repr(object, child); - g_return_if_fail(ochild != NULL); - sp_object_detach(object, ochild); + g_return_if_fail (ochild != NULL || !strcmp("comment", child->name())); // comments have no objects + if (ochild) + sp_object_detach(object, ochild); } /** @@ -835,20 +821,26 @@ sp_object_invoke_build(SPObject *object, SPDocument *document, Inkscape::XML::No object->document->bindObjectToRepr(object->repr, object); if (Inkscape::XML::id_permitted(object->repr)) { - /* If we are not cloned, force unique id */ + /* If we are not cloned, and not seeking, force unique id */ gchar const *id = object->repr->attribute("id"); - gchar *realid = sp_object_get_unique_id(object, id); - g_assert(realid != NULL); - - object->document->bindObjectToId(realid, object); - object->id = realid; - - /* Redefine ID, if required */ - if ((id == NULL) || (strcmp(id, realid) != 0)) { - gboolean undo_sensitive=sp_document_get_undo_sensitive(document); - sp_document_set_undo_sensitive(document, FALSE); - object->repr->setAttribute("id", realid); - sp_document_set_undo_sensitive(document, undo_sensitive); + if (!document->isSeeking()) { + gchar *realid = sp_object_get_unique_id(object, id); + g_assert(realid != NULL); + + object->document->bindObjectToId(realid, object); + object->id = realid; + + /* Redefine ID, if required */ + if ((id == NULL) || (strcmp(id, realid) != 0)) { + object->repr->setAttribute("id", realid); + } + } else if (id) { + // bind if id, but no conflict -- otherwise, we can expect + // a subsequent setting of the id attribute + if (!object->document->getObjectById(id)) { + object->document->bindObjectToId(id, object); + object->id = g_strdup(id); + } } } } else { @@ -865,45 +857,44 @@ sp_object_invoke_build(SPObject *object, SPDocument *document, Inkscape::XML::No sp_repr_add_listener(repr, &object_event_vector, object); } -void -sp_object_invoke_release(SPObject *object) -{ - g_assert(object != NULL); - g_assert(SP_IS_OBJECT(object)); - - g_assert(object->document); - g_assert(object->repr); +void SPObject::releaseReferences() { + g_assert(this->document); + g_assert(this->repr); - sp_repr_remove_listener_by_data(object->repr, object); + sp_repr_remove_listener_by_data(this->repr, this); - g_signal_emit(G_OBJECT(object), object_signals[RELEASE], 0); + this->_release_signal.emit(this); + SPObjectClass *klass=(SPObjectClass *)G_OBJECT_GET_CLASS(this); + if (klass->release) { + klass->release(this); + } /* all hrefs should be released by the "release" handlers */ - g_assert(object->hrefcount == 0); + g_assert(this->hrefcount == 0); - if (!SP_OBJECT_IS_CLONED(object)) { - if (object->id) { - object->document->bindObjectToId(object->id, NULL); + if (!SP_OBJECT_IS_CLONED(this)) { + if (this->id) { + this->document->bindObjectToId(this->id, NULL); } - g_free(object->id); - object->id = NULL; + g_free(this->id); + this->id = NULL; - g_free(object->_default_label); - object->_default_label = NULL; + g_free(this->_default_label); + this->_default_label = NULL; - object->document->bindObjectToRepr(object->repr, NULL); + this->document->bindObjectToRepr(this->repr, NULL); } else { - g_assert(!object->id); + g_assert(!this->id); } - if (object->style) { - object->style = sp_style_unref(object->style); + if (this->style) { + this->style = sp_style_unref(this->style); } - Inkscape::GC::release(object->repr); + Inkscape::GC::release(this->repr); - object->document = NULL; - object->repr = NULL; + this->document = NULL; + this->repr = NULL; } /** @@ -960,16 +951,23 @@ sp_object_private_set(SPObject *object, unsigned int key, gchar const *value) SPDocument *document=object->document; SPObject *conflict=NULL; - if (value) { - conflict = document->getObjectById((char const *)value); + gchar const *new_id = value; + + if (new_id) { + conflict = document->getObjectById((char const *)new_id); } + if ( conflict && conflict != object ) { - sp_object_ref(conflict, NULL); - // give the conflicting object a new ID - gchar *new_conflict_id = sp_object_get_unique_id(conflict, NULL); - SP_OBJECT_REPR(conflict)->setAttribute("id", new_conflict_id); - g_free(new_conflict_id); - sp_object_unref(conflict, NULL); + if (!document->isSeeking()) { + sp_object_ref(conflict, NULL); + // give the conflicting object a new ID + gchar *new_conflict_id = sp_object_get_unique_id(conflict, NULL); + SP_OBJECT_REPR(conflict)->setAttribute("id", new_conflict_id); + g_free(new_conflict_id); + sp_object_unref(conflict, NULL); + } else { + new_id = NULL; + } } if (object->id) { @@ -977,8 +975,8 @@ sp_object_private_set(SPObject *object, unsigned int key, gchar const *value) g_free(object->id); } - if (value) { - object->id = g_strdup((char const*)value); + if (new_id) { + object->id = g_strdup((char const*)new_id); document->bindObjectToId(object->id, object); } else { object->id = NULL; @@ -1019,6 +1017,10 @@ sp_object_private_set(SPObject *object, unsigned int key, gchar const *value) } object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG); break; + case SP_ATTR_STYLE: + sp_style_read_from_object(object->style, object); + object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG); + break; default: break; } @@ -1111,7 +1113,7 @@ static Inkscape::XML::Node * sp_object_private_write(SPObject *object, Inkscape::XML::Node *repr, guint flags) { if (!repr && (flags & SP_OBJECT_WRITE_BUILD)) { - repr = SP_OBJECT_REPR(object)->duplicate(); + repr = SP_OBJECT_REPR(object)->duplicate(NULL); // FIXME if (!( flags & SP_OBJECT_WRITE_EXT )) { repr->setAttribute("inkscape:collect", NULL); } @@ -1131,6 +1133,38 @@ sp_object_private_write(SPObject *object, Inkscape::XML::Node *repr, guint flags } else { repr->setAttribute("inkscape:collect", NULL); } + + SPStyle const *const obj_style = SP_OBJECT_STYLE(object); + if (obj_style) { + gchar *s = sp_style_write_string(obj_style, SP_STYLE_FLAG_IFSET); + repr->setAttribute("style", ( *s ? s : NULL )); + g_free(s); + } else { + /** \todo I'm not sure what to do in this case. Bug #1165868 + * suggests that it can arise, but the submitter doesn't know + * how to do so reliably. The main two options are either + * leave repr's style attribute unchanged, or explicitly clear it. + * Must also consider what to do with property attributes for + * the element; see below. + */ + char const *style_str = repr->attribute("style"); + if (!style_str) { + style_str = "NULL"; + } + g_warning("Item's style is NULL; repr style attribute is %s", style_str); + } + + /** \note We treat object->style as authoritative. Its effects have + * been written to the style attribute above; any properties that are + * unset we take to be deliberately unset (e.g. so that clones can + * override the property). + * + * Note that the below has an undesirable consequence of changing the + * appearance on renderers that lack CSS support (e.g. SVG tiny); + * possibly we should write property attributes instead of a style + * attribute. + */ + sp_style_unset_property_attrs (object); } return repr; @@ -1155,6 +1189,10 @@ SPObject::updateRepr(unsigned int flags) { } } +/** Used both to create reprs in the original document, and to create + * reprs in another document (e.g. a temporary document used when + * saving as "Plain SVG" + */ Inkscape::XML::Node * SPObject::updateRepr(Inkscape::XML::Node *repr, unsigned int flags) { if (SP_OBJECT_IS_CLONED(this)) { @@ -1170,9 +1208,10 @@ SPObject::updateRepr(Inkscape::XML::Node *repr, unsigned int flags) { g_warning("Class %s does not implement ::write", G_OBJECT_TYPE_NAME(this)); if (!repr) { if (flags & SP_OBJECT_WRITE_BUILD) { - repr = SP_OBJECT_REPR(this)->duplicate(); + /// \todo FIXME: Plumb an appropriate XML::Document into this + repr = SP_OBJECT_REPR(this)->duplicate(NULL); } - /// \todo fixme: else probably error (Lauris) */ + /// \todo FIXME: else probably error (Lauris) */ } else { repr->mergeFrom(SP_OBJECT_REPR(this), "id"); } @@ -1190,30 +1229,38 @@ SPObject::updateRepr(Inkscape::XML::Node *repr, unsigned int flags) { void SPObject::requestDisplayUpdate(unsigned int flags) { + g_return_if_fail( this->document != NULL ); + if (update_in_progress) { g_print("WARNING: Requested update while update in progress, counter = %d\n", update_in_progress); } + /* requestModified must be used only to set one of SP_OBJECT_MODIFIED_FLAG or + * SP_OBJECT_CHILD_MODIFIED_FLAG */ g_return_if_fail(!(flags & SP_OBJECT_PARENT_MODIFIED_FLAG)); g_return_if_fail((flags & SP_OBJECT_MODIFIED_FLAG) || (flags & SP_OBJECT_CHILD_MODIFIED_FLAG)); g_return_if_fail(!((flags & SP_OBJECT_MODIFIED_FLAG) && (flags & SP_OBJECT_CHILD_MODIFIED_FLAG))); - /* Check for propagate before we set any flags */ - /* Propagate means, that this is not passed through by modification request cascade yet */ - unsigned int propagate = (!(this->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))); + bool already_propagated = (!(this->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))); - /* Just set this flags safe even if some have been set before */ this->uflags |= flags; - if (propagate) { - if (this->parent) { - this->parent->requestDisplayUpdate(SP_OBJECT_CHILD_MODIFIED_FLAG); + /* If requestModified has already been called on this object or one of its children, then we + * don't need to set CHILD_MODIFIED on our ancestors because it's already been done. + */ + if (already_propagated) { + SPObject *parent = SP_OBJECT_PARENT(this); + if (parent) { + parent->requestDisplayUpdate(SP_OBJECT_CHILD_MODIFIED_FLAG); } else { - sp_document_request_modified(this->document); + sp_document_request_modified(SP_OBJECT_DOCUMENT(this)); } } } +/** + * Update views + */ void SPObject::updateDisplay(SPCtx *ctx, unsigned int flags) { @@ -1250,29 +1297,30 @@ SPObject::updateDisplay(SPCtx *ctx, unsigned int flags) update_in_progress --; } +/** + * Request modified always bubbles *up* the tree, as opposed to + * request display update, which trickles down and relies on the + * flags set during this pass... + */ void SPObject::requestModified(unsigned int flags) { g_return_if_fail( this->document != NULL ); - /* PARENT_MODIFIED is computed later on and is not intended to be - * "manually" queued */ + /* requestModified must be used only to set one of SP_OBJECT_MODIFIED_FLAG or + * SP_OBJECT_CHILD_MODIFIED_FLAG */ g_return_if_fail(!(flags & SP_OBJECT_PARENT_MODIFIED_FLAG)); - - /* we should be setting either MODIFIED or CHILD_MODIFIED... */ g_return_if_fail((flags & SP_OBJECT_MODIFIED_FLAG) || (flags & SP_OBJECT_CHILD_MODIFIED_FLAG)); - - /* ...but not both */ g_return_if_fail(!((flags & SP_OBJECT_MODIFIED_FLAG) && (flags & SP_OBJECT_CHILD_MODIFIED_FLAG))); - unsigned int old_mflags=this->mflags; + bool already_propagated = (!(this->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))); + this->mflags |= flags; - /* If we already had MODIFIED or CHILD_MODIFIED queued, we will - * have already queued CHILD_MODIFIED with our ancestors and - * need not disturb them again. + /* If requestModified has already been called on this object or one of its children, then we + * don't need to set CHILD_MODIFIED on our ancestors because it's already been done. */ - if (!( old_mflags & ( SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG ) )) { + if (already_propagated) { SPObject *parent=SP_OBJECT_PARENT(this); if (parent) { parent->requestModified(SP_OBJECT_CHILD_MODIFIED_FLAG); @@ -1282,6 +1330,12 @@ SPObject::requestModified(unsigned int flags) } } +/** + * Emits the MODIFIED signal with the object's flags. + * The object's mflags are the original set aside during the update pass for + * later delivery here. Once emitModified() is called, those flags don't + * need to be stored any longer. + */ void SPObject::emitModified(unsigned int flags) { @@ -1299,7 +1353,11 @@ SPObject::emitModified(unsigned int flags) this->mflags = 0; g_object_ref(G_OBJECT(this)); - g_signal_emit(G_OBJECT(this), object_signals[MODIFIED], 0, flags); + SPObjectClass *klass=(SPObjectClass *)G_OBJECT_GET_CLASS(this); + if (klass->modified) { + klass->modified(this, flags); + } + _modified_signal.emit(this, flags); g_object_unref(G_OBJECT(this)); }