diff --git a/src/sp-object.cpp b/src/sp-object.cpp
index f76e70a51cbc9460fc7645c917d809d9001b4369..cd90ab7ab494b5063ba58b5a2a8dc073c2ace785 100644 (file)
--- a/src/sp-object.cpp
+++ b/src/sp-object.cpp
* Authors:
* Lauris Kaplinski <lauris@kaplinski.com>
* bulia byak <buliabyak@users.sf.net>
* Authors:
* Lauris Kaplinski <lauris@kaplinski.com>
* bulia byak <buliabyak@users.sf.net>
+ * Stephen Silver <sasilver@users.sourceforge.net>
*
*
- * Copyright (C) 1999-2005 authors
+ * Copyright (C) 1999-2008 authors
* Copyright (C) 2001-2002 Ximian, Inc.
*
* Released under GNU GPL, read the file 'COPYING' for more information
* Copyright (C) 2001-2002 Ximian, Inc.
*
* Released under GNU GPL, read the file 'COPYING' for more information
* dictionary and so on. Source: doc/architecture.txt
*/
* dictionary and so on. Source: doc/architecture.txt
*/
+#include <cstring>
+#include <string>
#include "helper/sp-marshal.h"
#include "xml/node-event-vector.h"
#include "helper/sp-marshal.h"
#include "xml/node-event-vector.h"
static void sp_object_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
static void sp_object_private_set(SPObject *object, unsigned int key, gchar const *value);
static void sp_object_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
static void sp_object_private_set(SPObject *object, unsigned int key, gchar const *value);
-static Inkscape::XML::Node *sp_object_private_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
+static Inkscape::XML::Node *sp_object_private_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
/* Real handlers of repr signals */
/* Real handlers of repr signals */
* \pre object points to real object
*/
SPObject *
* \pre object points to real object
*/
SPObject *
-sp_object_href(SPObject *object, gpointer owner)
+sp_object_href(SPObject *object, gpointer /*owner*/)
{
g_return_val_if_fail(object != NULL, NULL);
g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
{
g_return_val_if_fail(object != NULL, NULL);
g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
* \pre object points to real object and hrefcount>0
*/
SPObject *
* \pre object points to real object and hrefcount>0
*/
SPObject *
-sp_object_hunref(SPObject *object, gpointer owner)
+sp_object_hunref(SPObject *object, gpointer /*owner*/)
{
g_return_val_if_fail(object != NULL, NULL);
g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
{
g_return_val_if_fail(object != NULL, NULL);
g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
* Invoked whenever the given mutation event happens in the XML tree.
* \param old_ref Ignored
*/
* Invoked whenever the given mutation event happens in the XML tree.
* \param old_ref Ignored
*/
-static void sp_object_order_changed(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref,
+static void sp_object_order_changed(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node */*old_ref*/,
Inkscape::XML::Node *new_ref)
{
SPObject *ochild = sp_object_get_child_by_repr(object, child);
Inkscape::XML::Node *new_ref)
{
SPObject *ochild = sp_object_get_child_by_repr(object, child);
* Callback for child_added node event.
*/
static void
* Callback for child_added node event.
*/
static void
-sp_object_repr_child_added(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data)
+sp_object_repr_child_added(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data)
{
SPObject *object = SP_OBJECT(data);
{
SPObject *object = SP_OBJECT(data);
@@ -912,7 +915,7 @@ sp_object_repr_child_added(Inkscape::XML::Node *repr, Inkscape::XML::Node *child
* Callback for remove_child node event.
*/
static void
* Callback for remove_child node event.
*/
static void
-sp_object_repr_child_removed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data)
+sp_object_repr_child_removed(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node *child, Inkscape::XML::Node */*ref*/, gpointer data)
{
SPObject *object = SP_OBJECT(data);
{
SPObject *object = SP_OBJECT(data);
@@ -927,7 +930,7 @@ sp_object_repr_child_removed(Inkscape::XML::Node *repr, Inkscape::XML::Node *chi
* \todo fixme:
*/
static void
* \todo fixme:
*/
static void
-sp_object_repr_order_changed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *old, Inkscape::XML::Node *newer, gpointer data)
+sp_object_repr_order_changed(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node *child, Inkscape::XML::Node *old, Inkscape::XML::Node *newer, gpointer data)
{
SPObject *object = SP_OBJECT(data);
{
SPObject *object = SP_OBJECT(data);
* Callback for attr_changed node event.
*/
static void
* Callback for attr_changed node event.
*/
static void
-sp_object_repr_attr_changed(Inkscape::XML::Node *repr, gchar const *key, gchar const *oldval, gchar const *newval, bool is_interactive, gpointer data)
+sp_object_repr_attr_changed(Inkscape::XML::Node */*repr*/, gchar const *key, gchar const */*oldval*/, gchar const */*newval*/, bool is_interactive, gpointer data)
{
SPObject *object = SP_OBJECT(data);
{
SPObject *object = SP_OBJECT(data);
@@ -1073,7 +1076,7 @@ sp_object_repr_attr_changed(Inkscape::XML::Node *repr, gchar const *key, gchar c
// manual changes to extension attributes require the normal
// attributes, which depend on them, to be updated immediately
if (is_interactive) {
// manual changes to extension attributes require the normal
// attributes, which depend on them, to be updated immediately
if (is_interactive) {
- object->updateRepr(repr, 0);
+ object->updateRepr(0);
}
}
}
}
@@ -1081,7 +1084,7 @@ sp_object_repr_attr_changed(Inkscape::XML::Node *repr, gchar const *key, gchar c
* Callback for content_changed node event.
*/
static void
* Callback for content_changed node event.
*/
static void
-sp_object_repr_content_changed(Inkscape::XML::Node *repr, gchar const *oldcontent, gchar const *newcontent, gpointer data)
+sp_object_repr_content_changed(Inkscape::XML::Node */*repr*/, gchar const */*oldcontent*/, gchar const */*newcontent*/, gpointer data)
{
SPObject *object = SP_OBJECT(data);
{
SPObject *object = SP_OBJECT(data);
* Callback for write event.
*/
static Inkscape::XML::Node *
* Callback for write event.
*/
static Inkscape::XML::Node *
-sp_object_private_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
+sp_object_private_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags)
{
if (!repr && (flags & SP_OBJECT_WRITE_BUILD)) {
{
if (!repr && (flags & SP_OBJECT_WRITE_BUILD)) {
- repr = SP_OBJECT_REPR(object)->duplicate(NULL); // FIXME
+ repr = SP_OBJECT_REPR(object)->duplicate(doc);
if (!( flags & SP_OBJECT_WRITE_EXT )) {
repr->setAttribute("inkscape:collect", NULL);
}
if (!( flags & SP_OBJECT_WRITE_EXT )) {
repr->setAttribute("inkscape:collect", NULL);
}
if (!SP_OBJECT_IS_CLONED(this)) {
Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
if (repr) {
if (!SP_OBJECT_IS_CLONED(this)) {
Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
if (repr) {
- return updateRepr(repr, flags);
+ return updateRepr(repr->document(), repr, flags);
} else {
g_critical("Attempt to update non-existent repr");
return NULL;
} else {
g_critical("Attempt to update non-existent repr");
return NULL;
* saving as "Plain SVG"
*/
Inkscape::XML::Node *
* saving as "Plain SVG"
*/
Inkscape::XML::Node *
-SPObject::updateRepr(Inkscape::XML::Node *repr, unsigned int flags) {
+SPObject::updateRepr(Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, unsigned int flags) {
+ g_assert(doc != NULL);
+
if (SP_OBJECT_IS_CLONED(this)) {
/* cloned objects have no repr */
return NULL;
if (SP_OBJECT_IS_CLONED(this)) {
/* cloned objects have no repr */
return NULL;
if (!(flags & SP_OBJECT_WRITE_BUILD) && !repr) {
repr = SP_OBJECT_REPR(this);
}
if (!(flags & SP_OBJECT_WRITE_BUILD) && !repr) {
repr = SP_OBJECT_REPR(this);
}
- return ((SPObjectClass *) G_OBJECT_GET_CLASS(this))->write(this, repr, flags);
+ return ((SPObjectClass *) G_OBJECT_GET_CLASS(this))->write(this, doc, repr, flags);
} else {
g_warning("Class %s does not implement ::write", G_OBJECT_TYPE_NAME(this));
if (!repr) {
if (flags & SP_OBJECT_WRITE_BUILD) {
} else {
g_warning("Class %s does not implement ::write", G_OBJECT_TYPE_NAME(this));
if (!repr) {
if (flags & SP_OBJECT_WRITE_BUILD) {
- /// \todo FIXME: Plumb an appropriate XML::Document into this
- repr = SP_OBJECT_REPR(this)->duplicate(NULL);
+ repr = SP_OBJECT_REPR(this)->duplicate(doc);
}
/// \todo FIXME: else probably error (Lauris) */
} else {
}
/// \todo FIXME: else probably error (Lauris) */
} else {
g_object_unref(G_OBJECT(this));
}
g_object_unref(G_OBJECT(this));
}
-/*
- * Get and set descriptive parameters
- *
- * These are inefficent, so they are not intended to be used interactively
- */
-
-gchar const *
-sp_object_title_get(SPObject *object)
-{
- return NULL;
-}
-
-gchar const *
-sp_object_description_get(SPObject *object)
-{
- return NULL;
-}
-
-unsigned int
-sp_object_title_set(SPObject *object, gchar const *title)
-{
- return FALSE;
-}
-
-unsigned int
-sp_object_description_set(SPObject *object, gchar const *desc)
-{
- return FALSE;
-}
-
gchar const *
sp_object_tagName_get(SPObject const *object, SPException *ex)
{
gchar const *
sp_object_tagName_get(SPObject const *object, SPException *ex)
{
@@ -1421,9 +1395,7 @@ sp_object_setAttribute(SPObject *object, gchar const *key, gchar const *value, S
g_return_if_fail(SP_EXCEPTION_IS_OK(ex));
/// \todo fixme: Exception if object is NULL? */
g_return_if_fail(SP_EXCEPTION_IS_OK(ex));
/// \todo fixme: Exception if object is NULL? */
- if (!sp_repr_set_attr(object->repr, key, value)) {
- ex->code = SP_NO_MODIFICATION_ALLOWED_ERR;
- }
+ object->repr->setAttribute(key, value, false);
}
void
}
void
@@ -1433,9 +1405,7 @@ sp_object_removeAttribute(SPObject *object, gchar const *key, SPException *ex)
g_return_if_fail(SP_EXCEPTION_IS_OK(ex));
/// \todo fixme: Exception if object is NULL? */
g_return_if_fail(SP_EXCEPTION_IS_OK(ex));
/// \todo fixme: Exception if object is NULL? */
- if (!sp_repr_set_attr(object->repr, key, NULL)) {
- ex->code = SP_NO_MODIFICATION_ALLOWED_ERR;
- }
+ object->repr->setAttribute(key, NULL, false);
}
/* Helper */
}
/* Helper */
return NULL;
}
return NULL;
}
+/* Titles and descriptions */
+
+/* Note:
+ Titles and descriptions are stored in 'title' and 'desc' child elements
+ (see section 5.4 of the SVG 1.0 and 1.1 specifications). The spec allows
+ an element to have more than one 'title' child element, but strongly
+ recommends against this and requires using the first one if a choice must
+ be made. The same applies to 'desc' elements. Therefore, these functions
+ ignore all but the first 'title' child element and first 'desc' child
+ element, except when deleting a title or description.
+*/
+
+/**
+ * Returns the title of this object, or NULL if there is none.
+ * The caller must free the returned string using g_free() - see comment
+ * for getTitleOrDesc() below.
+ */
+gchar *
+SPObject::title() const
+{
+ return getTitleOrDesc("svg:title");
+}
+
+/**
+ * Sets the title of this object
+ * A NULL or purely whitespace argument is interpreted as meaning that
+ * the existing title (if any) should be deleted.
+ */
+void
+SPObject::setTitle(gchar const *title)
+{
+ setTitleOrDesc(title, "svg:title");
+}
+
+/**
+ * Returns the description of this object, or NULL if there is none.
+ * The caller must free the returned string using g_free() - see comment
+ * for getTitleOrDesc() below.
+ */
+gchar *
+SPObject::desc() const
+{
+ return getTitleOrDesc("svg:desc");
+}
+
+/**
+ * Sets the description of this object.
+ * A NULL or purely whitespace argument is interpreted as meaning that
+ * the existing description (if any) should be deleted.
+ */
+void
+SPObject::setDesc(gchar const *desc)
+{
+ setTitleOrDesc(desc, "svg:desc");
+}
+
+/**
+ * Returns the title or description of this object, or NULL if there is none.
+ *
+ * The SVG spec allows 'title' and 'desc' elements to contain text marked up
+ * using elements from other namespaces. Therefore, this function cannot
+ * in general just return a pointer to an existing string - it must instead
+ * construct a string containing the title or description without the mark-up.
+ * Consequently, the return value is a newly allocated string (or NULL), and
+ * must be freed (using g_free()) by the caller.
+ */
+gchar *
+SPObject::getTitleOrDesc(gchar const *svg_tagname) const
+{
+ SPObject *elem = findFirstChild(svg_tagname);
+ if (elem == NULL) return NULL;
+ return g_string_free(elem->textualContent(), FALSE);
+}
+
+/**
+ * Sets or deletes the title or description of this object.
+ */
+void
+SPObject::setTitleOrDesc(gchar const *value, gchar const *svg_tagname)
+{
+ SPObject *elem = findFirstChild(svg_tagname);
+
+ // if the new title/description is NULL, or just whitespace,
+ // then delete any existing title/description
+ bool just_whitespace = true;
+ if (value)
+ for (const gchar *cp = value; *cp; ++cp) {
+ if (!std::strchr("\r\n \t", *cp)) {
+ just_whitespace = false;
+ break;
+ }
+ }
+ if (just_whitespace) {
+ // delete the title/description(s)
+ while (elem) {
+ elem->deleteObject();
+ elem = findFirstChild(svg_tagname);
+ }
+ return;
+ }
+
+ Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
+
+ if (elem == NULL) {
+ // create a new 'title' or 'desc' element, putting it at the
+ // beginning (in accordance with the spec's recommendations)
+ Inkscape::XML::Node *xml_elem = xml_doc->createElement(svg_tagname);
+ repr->addChild(xml_elem, NULL);
+ elem = document->getObjectByRepr(xml_elem);
+ }
+ else {
+ // remove the current content of the 'text' or 'desc' element
+ SPObject *child;
+ while (NULL != (child = elem->firstChild())) child->deleteObject();
+ }
+
+ // add the new content
+ elem->appendChildRepr(xml_doc->createTextNode(value));
+}
+
+/**
+ * Find the first child of this object with a given tag name,
+ * and return it. Returns NULL if there is no matching child.
+ */
+SPObject *
+SPObject::findFirstChild(gchar const *tagname) const
+{
+ for (SPObject *child = children; child; child = child->next)
+ {
+ if (child->repr->type() == Inkscape::XML::ELEMENT_NODE &&
+ !strcmp(child->repr->name(), tagname)) return child;
+ }
+ return NULL;
+}
+
+/**
+ * Return the full textual content of an element (typically all the
+ * content except the tags).
+ * Must not be used on anything except elements.
+ */
+GString*
+SPObject::textualContent() const
+{
+ GString* text = g_string_new("");
+
+ for (const SPObject *child = firstChild(); child; child = child->next)
+ {
+ Inkscape::XML::NodeType child_type = child->repr->type();
+
+ if (child_type == Inkscape::XML::ELEMENT_NODE) {
+ GString * new_text = child->textualContent();
+ g_string_append(text, new_text->str);
+ g_string_free(new_text, TRUE);
+ }
+ else if (child_type == Inkscape::XML::TEXT_NODE) {
+ g_string_append(text, child->repr->content());
+ }
+ }
+ return text;
+}
/*
Local Variables:
/*
Local Variables: