From: sasilver Date: Mon, 14 Jul 2008 10:19:53 +0000 (+0000) Subject: Some improvements to my title/desc stuff of revision 18759 (see bug 171024). X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=037770d289d0ef4c78c279e1dd3f3b4f2140c751;p=inkscape.git Some improvements to my title/desc stuff of revision 18759 (see bug 171024). --- diff --git a/src/dialogs/item-properties.cpp b/src/dialogs/item-properties.cpp index fb7753d00..25eafa9a1 100644 --- a/src/dialogs/item-properties.cpp +++ b/src/dialogs/item-properties.cpp @@ -442,14 +442,9 @@ sp_item_widget_label_changed( GtkWidget */*widget*/, SPWidget *spw ) /* Retrieve the title */ GtkWidget *w = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), "title")); gchar *title = (gchar *)gtk_entry_get_text(GTK_ENTRY (w)); - g_assert(title != NULL); - gchar *old_title = obj->title(); - if (old_title == NULL || strcmp(title, old_title)) { - obj->setTitle(title); + if (obj->setTitle(title)) sp_document_done(SP_ACTIVE_DOCUMENT, SP_VERB_DIALOG_ITEM, _("Set object title")); - } - g_free(old_title); /* Retrieve the description */ GtkTextView *tv = GTK_TEXT_VIEW(gtk_object_get_data(GTK_OBJECT(spw), "desc")); @@ -457,14 +452,9 @@ sp_item_widget_label_changed( GtkWidget */*widget*/, SPWidget *spw ) GtkTextIter start, end; gtk_text_buffer_get_bounds(buf, &start, &end); gchar *desc = gtk_text_buffer_get_text(buf, &start, &end, TRUE); - g_assert(desc != NULL); - gchar *old_desc = obj->desc(); - if (old_desc == NULL || strcmp(desc, old_desc)) { - obj->setDesc(desc); + if (obj->setDesc(desc)) sp_document_done(SP_ACTIVE_DOCUMENT, SP_VERB_DIALOG_ITEM, _("Set object description")); - } - g_free(old_desc); g_free(desc); gtk_object_set_data (GTK_OBJECT (spw), "blocked", GUINT_TO_POINTER (FALSE)); diff --git a/src/sp-object.cpp b/src/sp-object.cpp index cd90ab7ab..83bb7282c 100644 --- a/src/sp-object.cpp +++ b/src/sp-object.cpp @@ -1588,13 +1588,14 @@ SPObject::title() const /** * 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. + * A NULL first argument is interpreted as meaning that the existing title + * (if any) should be deleted. + * The second argument is optional - see setTitleOrDesc() below for details. */ -void -SPObject::setTitle(gchar const *title) +bool +SPObject::setTitle(gchar const *title, bool verbatim) { - setTitleOrDesc(title, "svg:title"); + return setTitleOrDesc(title, "svg:title", verbatim); } /** @@ -1610,13 +1611,14 @@ SPObject::desc() const /** * 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. + * A NULL first argument is interpreted as meaning that the existing + * description (if any) should be deleted. + * The second argument is optional - see setTitleOrDesc() below for details. */ -void -SPObject::setDesc(gchar const *desc) +bool +SPObject::setDesc(gchar const *desc, bool verbatim) { - setTitleOrDesc(desc, "svg:desc"); + return setTitleOrDesc(desc, "svg:desc", verbatim); } /** @@ -1639,29 +1641,58 @@ SPObject::getTitleOrDesc(gchar const *svg_tagname) const /** * Sets or deletes the title or description of this object. + * A NULL 'value' argument causes the title or description to be deleted. + * + * 'verbatim' parameter: + * If verbatim==true, then the title or description is set to exactly the + * specified value. If verbatim==false then two exceptions are made: + * (1) If the specified value is just whitespace, then the title/description + * is deleted. + * (2) If the specified value is the same as the current value except for + * mark-up, then the current value is left unchanged. + * This is usually the desired behaviour, so 'verbatim' defaults to false for + * setTitle() and setDesc(). + * + * The return value is true if a change was made to the title/description, + * and usually false otherwise. */ -void -SPObject::setTitleOrDesc(gchar const *value, gchar const *svg_tagname) +bool +SPObject::setTitleOrDesc(gchar const *value, gchar const *svg_tagname, bool verbatim) { - 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 (!verbatim) { + // If the new title/description is just whitespace, + // treat it as though it were NULL. + if (value) { + bool just_whitespace = true; + for (const gchar *cp = value; *cp; ++cp) { + if (!std::strchr("\r\n \t", *cp)) { + just_whitespace = false; + break; + } } + if (just_whitespace) value = NULL; } - if (just_whitespace) { + // Don't stomp on mark-up if there is no real change. + if (value) { + gchar *current_value = getTitleOrDesc(svg_tagname); + if (current_value) { + bool different = std::strcmp(current_value, value); + g_free(current_value); + if (!different) return false; + } + } + } + + SPObject *elem = findFirstChild(svg_tagname); + + if (value == NULL) { + if (elem == NULL) return false; // delete the title/description(s) while (elem) { elem->deleteObject(); elem = findFirstChild(svg_tagname); } - return; + return true; } Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document); @@ -1672,6 +1703,7 @@ SPObject::setTitleOrDesc(gchar const *value, gchar const *svg_tagname) Inkscape::XML::Node *xml_elem = xml_doc->createElement(svg_tagname); repr->addChild(xml_elem, NULL); elem = document->getObjectByRepr(xml_elem); + Inkscape::GC::release(xml_elem); } else { // remove the current content of the 'text' or 'desc' element @@ -1681,6 +1713,7 @@ SPObject::setTitleOrDesc(gchar const *value, gchar const *svg_tagname) // add the new content elem->appendChildRepr(xml_doc->createTextNode(value)); + return true; } /** diff --git a/src/sp-object.h b/src/sp-object.h index ccc063d5f..095494a6f 100644 --- a/src/sp-object.h +++ b/src/sp-object.h @@ -241,12 +241,12 @@ struct SPObject : public GObject { /** Retrieves the title of this object */ gchar *title() const; /** Sets the title of this object */ - void setTitle(gchar const *title); + bool setTitle(gchar const *title, bool verbatim=false); /** Retrieves the description of this object */ gchar *desc() const; /** Sets the description of this object */ - void setDesc(gchar const *desc); + bool setDesc(gchar const *desc, bool verbatim=false); /** @brief Set the policy under which this object will be * orphan-collected. @@ -491,7 +491,7 @@ struct SPObject : public GObject { private: // Private member functions used in the definitions of setTitle(), // setDesc(), title() and desc(). - void setTitleOrDesc(gchar const *value, gchar const *svg_tagname); + bool setTitleOrDesc(gchar const *value, gchar const *svg_tagname, bool verbatim); gchar * getTitleOrDesc(gchar const *svg_tagname) const; SPObject * findFirstChild(gchar const *tagname) const; GString * textualContent() const;