Code

Filter effects dialog:
[inkscape.git] / src / text-editing.cpp
index 125d1e106931f8bf1fd6870d188add29e54c5fc1..bed837cefabf2ff65903734a16ad15c3e5bd8e56 100644 (file)
 # include "config.h"
 #endif
 
+#include <glibmm/i18n.h>
+
 #include "desktop.h"
+#include "inkscape.h"
+#include "message-stack.h"
 #include "style.h"
 #include "unit-constants.h"
 
+#include "document.h"
 #include "xml/repr.h"
 #include "xml/attribute-record.h"
 
 #include "sp-flowtext.h"
 #include "sp-flowdiv.h"
 #include "sp-flowregion.h"
+#include "sp-tref.h"
 #include "sp-tspan.h"
 
 #include "text-editing.h"
 
+static const gchar *tref_edit_message = _("You cannot edit <b>cloned character data</b>.");
+
 static bool tidy_xml_tree_recursively(SPObject *root);
 
 Inkscape::Text::Layout const * te_get_layout (SPItem const *item)
@@ -150,13 +158,15 @@ static bool is_line_break_object(SPObject const *object)
 }
 
 /** returns the attributes for an object, or NULL if it isn't a text,
-tspan or textpath. */
+tspan, tref, or textpath. */
 static TextTagAttributes* attributes_for_object(SPObject *object)
 {
     if (SP_IS_TSPAN(object))
         return &SP_TSPAN(object)->attributes;
     if (SP_IS_TEXT(object))
         return &SP_TEXT(object)->attributes;
+    if (SP_IS_TREF(object))
+        return &SP_TREF(object)->attributes;
     if (SP_IS_TEXTPATH(object))
         return &SP_TEXTPATH(object)->attributes;
     return NULL;
@@ -176,7 +186,9 @@ unsigned sp_text_get_length(SPObject const *item)
     unsigned length = 0;
 
     if (SP_IS_STRING(item)) return SP_STRING(item)->string.length();
+    
     if (is_line_break_object(item)) length++;
+    
     for (SPObject const *child = item->firstChild() ; child ; child = SP_OBJECT_NEXT(child)) {
         if (SP_IS_STRING(child)) length += SP_STRING(child)->string.length();
         else length += sp_text_get_length(child);
@@ -190,15 +202,20 @@ unsigned sp_text_get_length_upto(SPObject const *item, SPObject const *upto)
 {
     unsigned length = 0;
 
+    // The string is the lowest level and the length can be counted directly. 
     if (SP_IS_STRING(item)) {
         return SP_STRING(item)->string.length();
     }
+    
+    // Take care of new lines...
     if (is_line_break_object(item) && !SP_IS_TEXT(item)) {
         if (item != SP_OBJECT_PARENT(item)->firstChild()) {
             // add 1 for each newline
             length++;
         }
     }
+    
+    // Count the length of the children
     for (SPObject const *child = item->firstChild() ; child ; child = SP_OBJECT_NEXT(child)) {
         if (upto && child == upto) {
             // hit upto, return immediately
@@ -221,11 +238,11 @@ unsigned sp_text_get_length_upto(SPObject const *item, SPObject const *upto)
     return length;
 }
 
-static Inkscape::XML::Node* duplicate_node_without_children(Inkscape::XML::Node const *old_node)
+static Inkscape::XML::Node* duplicate_node_without_children(Inkscape::XML::Document *xml_doc, Inkscape::XML::Node const *old_node)
 {
     switch (old_node->type()) {
         case Inkscape::XML::ELEMENT_NODE: {
-            Inkscape::XML::Node *new_node = sp_repr_new(old_node->name());
+            Inkscape::XML::Node *new_node = xml_doc->createElement(old_node->name());
             Inkscape::Util::List<Inkscape::XML::AttributeRecord const> attributes = old_node->attributeList();
             GQuark const id_key = g_quark_from_string("id");
             for ( ; attributes ; attributes++) {
@@ -236,10 +253,10 @@ static Inkscape::XML::Node* duplicate_node_without_children(Inkscape::XML::Node
         }
 
         case Inkscape::XML::TEXT_NODE:
-            return sp_repr_new_text(old_node->content());
+            return xml_doc->createTextNode(old_node->content());
 
         case Inkscape::XML::COMMENT_NODE:
-            return sp_repr_new_comment(old_node->content());
+            return xml_doc->createComment(old_node->content());
 
         case Inkscape::XML::DOCUMENT_NODE:
             return NULL;   // this had better never happen
@@ -275,8 +292,9 @@ parent of the first line break node encountered.
 */
 static SPObject* split_text_object_tree_at(SPObject *split_obj, unsigned char_index)
 {
+    Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(split_obj));
     if (is_line_break_object(split_obj)) {
-        Inkscape::XML::Node *new_node = duplicate_node_without_children(SP_OBJECT_REPR(split_obj));
+        Inkscape::XML::Node *new_node = duplicate_node_without_children(xml_doc, SP_OBJECT_REPR(split_obj));
         SP_OBJECT_REPR(SP_OBJECT_PARENT(split_obj))->addChild(new_node, SP_OBJECT_REPR(split_obj));
         Inkscape::GC::release(new_node);
         split_attributes(split_obj, SP_OBJECT_NEXT(split_obj), char_index);
@@ -286,7 +304,7 @@ static SPObject* split_text_object_tree_at(SPObject *split_obj, unsigned char_in
     unsigned char_count_before = sum_sibling_text_lengths_before(split_obj);
     SPObject *duplicate_obj = split_text_object_tree_at(SP_OBJECT_PARENT(split_obj), char_index + char_count_before);
     // copy the split node
-    Inkscape::XML::Node *new_node = duplicate_node_without_children(SP_OBJECT_REPR(split_obj));
+    Inkscape::XML::Node *new_node = duplicate_node_without_children(xml_doc, SP_OBJECT_REPR(split_obj));
     SP_OBJECT_REPR(duplicate_obj)->appendChild(new_node);
     Inkscape::GC::release(new_node);
 
@@ -318,6 +336,8 @@ Inkscape::Text::Layout::iterator sp_te_insert_line (SPItem *item, Inkscape::Text
     // texpaths attached to the same path, with a vertical shift
     if (SP_IS_TEXT_TEXTPATH (item))
         return position;
+        
+    SPDesktop *desktop = SP_ACTIVE_DESKTOP; 
 
     Inkscape::Text::Layout const *layout = te_get_layout(item);
     SPObject *split_obj = 0;
@@ -331,11 +351,18 @@ Inkscape::Text::Layout::iterator sp_te_insert_line (SPItem *item, Inkscape::Text
     if (split_obj == 0 || is_line_break_object(split_obj)) {
         if (split_obj == 0) split_obj = item->lastChild();
         if (split_obj) {
-            Inkscape::XML::Node *new_node = duplicate_node_without_children(SP_OBJECT_REPR(split_obj));
+            Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(split_obj));
+            Inkscape::XML::Node *new_node = duplicate_node_without_children(xml_doc, SP_OBJECT_REPR(split_obj));
             SP_OBJECT_REPR(SP_OBJECT_PARENT(split_obj))->addChild(new_node, SP_OBJECT_REPR(split_obj));
             Inkscape::GC::release(new_node);
         }
     } else if (SP_IS_STRING(split_obj)) {
+        // If the parent is a tref, editing on this particular string is disallowed.
+        if (SP_IS_TREF(SP_OBJECT_PARENT(split_obj))) {
+            desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, tref_edit_message);
+            return position;
+        }
+        
         Glib::ustring *string = &SP_STRING(split_obj)->string;
         unsigned char_index = 0;
         for (Glib::ustring::iterator it = string->begin() ; it != split_text_iter ; it++)
@@ -405,6 +432,8 @@ sp_te_insert(SPItem *item, Inkscape::Text::Layout::iterator const &position, gch
         g_warning("Trying to insert invalid utf8");
         return position;
     }
+    
+    SPDesktop *desktop = SP_ACTIVE_DESKTOP;
 
     Inkscape::Text::Layout const *layout = te_get_layout(item);
     SPObject *source_obj = 0;
@@ -418,12 +447,19 @@ sp_te_insert(SPItem *item, Inkscape::Text::Layout::iterator const &position, gch
     layout->getSourceOfCharacter(it_prev_char, &rawptr, &iter_text);
     source_obj = SP_OBJECT(rawptr);
     if (SP_IS_STRING(source_obj)) {
-        // the simple case
+        // If the parent is a tref, editing on this particular string is disallowed.
+        if (SP_IS_TREF(SP_OBJECT_PARENT(source_obj))) {
+            desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, tref_edit_message);
+            return position;
+        }
+        
+        // Now the simple case can begin...
         if (!cursor_at_start) iter_text++;
         SPString *string_item = SP_STRING(source_obj);
         insert_into_spstring(string_item, cursor_at_end ? string_item->string.end() : iter_text, utf8);
     } else {
         // the not-so-simple case where we're at a line break or other control char; add to the next child/sibling SPString
+        Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(item)->document();
         if (cursor_at_start) {
             source_obj = item;
             if (source_obj->hasChildren()) {
@@ -436,7 +472,7 @@ sp_te_insert(SPItem *item, Inkscape::Text::Layout::iterator const &position, gch
                 }
             }
             if (source_obj == item && SP_IS_FLOWTEXT(item)) {
-                Inkscape::XML::Node *para = sp_repr_new("svg:flowPara");
+                Inkscape::XML::Node *para = xml_doc->createElement("svg:flowPara");
                 SP_OBJECT_REPR(item)->appendChild(para);
                 source_obj = item->lastChild();
             }
@@ -447,12 +483,18 @@ sp_te_insert(SPItem *item, Inkscape::Text::Layout::iterator const &position, gch
             SPString *string_item = sp_te_seek_next_string_recursive(source_obj);
             if (string_item == NULL) {
                 // need to add an SPString in this (pathological) case
-                Inkscape::XML::Node *rstring = sp_repr_new_text("");
+                Inkscape::XML::Node *rstring = xml_doc->createTextNode("");
                 SP_OBJECT_REPR(source_obj)->addChild(rstring, NULL);
                 Inkscape::GC::release(rstring);
                 g_assert(SP_IS_STRING(source_obj->firstChild()));
                 string_item = SP_STRING(source_obj->firstChild());
             }
+            // If the parent is a tref, editing on this particular string is disallowed.
+            if (SP_IS_TREF(SP_OBJECT_PARENT(string_item))) {
+                desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, tref_edit_message);
+                return position;
+            }
+            
             insert_into_spstring(string_item, cursor_at_end ? string_item->string.end() : string_item->string.begin(), utf8);
         }
     }
@@ -524,7 +566,8 @@ static SPObject* delete_line_break(SPObject *root, SPObject *item, bool *next_is
       <p><div></div>*text</p>
       <p><div></div></p><p>*text</p>
     */
-    Inkscape::XML::Node *new_span_repr = sp_repr_new(span_name_for_text_object(root));
+    Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(item)->document();
+    Inkscape::XML::Node *new_span_repr = xml_doc->createElement(span_name_for_text_object(root));
 
     if (gchar const *a = this_repr->attribute("dx"))
         new_span_repr->setAttribute("dx", a);
@@ -628,6 +671,9 @@ sp_te_delete (SPItem *item, Inkscape::Text::Layout::iterator const &start, Inksc
         first = end;
         last = start;
     }
+    
+    SPDesktop *desktop = SP_ACTIVE_DESKTOP;
+    
     Inkscape::Text::Layout const *layout = te_get_layout(item);
     SPObject *start_item = 0, *end_item = 0;
     void *rawptr = 0;
@@ -652,6 +698,12 @@ sp_te_delete (SPItem *item, Inkscape::Text::Layout::iterator const &start, Inksc
     if (start_item == end_item) {
         // the quick case where we're deleting stuff all from the same string
         if (SP_IS_STRING(start_item)) {     // always true (if it_start != it_end anyway)
+            // If the parent is a tref, editing on this particular string is disallowed.
+            if (SP_IS_TREF(SP_OBJECT_PARENT(start_item))) {
+                desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, tref_edit_message);
+                return end;
+            }
+            
             erase_from_spstring(SP_STRING(start_item), start_text_iter, end_text_iter);
         }
     } else {
@@ -660,6 +712,12 @@ sp_te_delete (SPItem *item, Inkscape::Text::Layout::iterator const &start, Inksc
         while (sub_item != item) {
             if (sub_item == end_item) {
                 if (SP_IS_STRING(sub_item)) {
+                    // If the parent is a tref, editing on this particular string is disallowed.
+                    if (SP_IS_TREF(SP_OBJECT_PARENT(sub_item))) {
+                        desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, tref_edit_message);
+                        return end;
+                    }
+            
                     Glib::ustring *string = &SP_STRING(sub_item)->string;
                     erase_from_spstring(SP_STRING(sub_item), string->begin(), end_text_iter);
                 }
@@ -775,6 +833,7 @@ sp_te_set_repr_text_multiline(SPItem *text, gchar const *str)
     g_return_if_fail (text != NULL);
     g_return_if_fail (SP_IS_TEXT(text) || SP_IS_FLOWTEXT(text));
 
+    Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(text)->document();
     Inkscape::XML::Node *repr;
     SPObject *object;
     bool is_textpath = false;
@@ -808,12 +867,12 @@ sp_te_set_repr_text_multiline(SPItem *text, gchar const *str)
             if (e) *e = '\0';
             Inkscape::XML::Node *rtspan;
             if (SP_IS_TEXT(text)) { // create a tspan for each line
-                rtspan = sp_repr_new ("svg:tspan");
+                rtspan = xml_doc->createElement("svg:tspan");
                 rtspan->setAttribute("sodipodi:role", "line");
             } else { // create a flowPara for each line
-                rtspan = sp_repr_new ("svg:flowPara");
+                rtspan = xml_doc->createElement("svg:flowPara");
             }
-            Inkscape::XML::Node *rstr = sp_repr_new_text(p);
+            Inkscape::XML::Node *rstr = xml_doc->createTextNode(p);
             rtspan->addChild(rstr, NULL);
             Inkscape::GC::release(rstr);
             repr->appendChild(rtspan);
@@ -822,7 +881,7 @@ sp_te_set_repr_text_multiline(SPItem *text, gchar const *str)
         p = (e) ? e + 1 : NULL;
     }
     if (is_textpath) {
-        Inkscape::XML::Node *rstr = sp_repr_new_text(content);
+        Inkscape::XML::Node *rstr = xml_doc->createTextNode(content);
         repr->addChild(rstr, NULL);
         Inkscape::GC::release(rstr);
     }
@@ -1097,7 +1156,7 @@ as opposed to sp_style_merge_from_style_string which merges its parameter
 underneath the existing styles (ie ignoring already set properties). */
 static void overwrite_style_with_string(SPObject *item, gchar const *style_string)
 {
-    SPStyle *new_style = sp_style_new();
+    SPStyle *new_style = sp_style_new(SP_OBJECT_DOCUMENT(item));
     sp_style_merge_from_style_string(new_style, style_string);
     gchar const *item_style_string = SP_OBJECT_REPR(item)->attribute("style");
     if (item_style_string && *item_style_string)
@@ -1122,7 +1181,7 @@ static bool objects_have_equal_style(SPObject const *parent, SPObject const *chi
     gchar *parent_style = sp_style_write_string(parent->style, SP_STYLE_FLAG_ALWAYS);
     // we have to write parent_style then read it again, because some properties format their values
     // differently depending on whether they're set or not (*cough*dash-offset*cough*)
-    SPStyle *parent_spstyle = sp_style_new();
+    SPStyle *parent_spstyle = sp_style_new(SP_OBJECT_DOCUMENT(parent));
     sp_style_merge_from_style_string(parent_spstyle, parent_style);
     g_free(parent_style);
     parent_style = sp_style_write_string(parent_spstyle, SP_STYLE_FLAG_ALWAYS);
@@ -1138,7 +1197,7 @@ static bool objects_have_equal_style(SPObject const *parent, SPObject const *chi
         }
         child = SP_OBJECT_PARENT(child);
     }
-    SPStyle *child_spstyle = sp_style_new();
+    SPStyle *child_spstyle = sp_style_new(SP_OBJECT_DOCUMENT(parent));
     sp_style_merge_from_style_string(child_spstyle, child_style_construction.c_str());
     gchar *child_style = sp_style_write_string(child_spstyle, SP_STYLE_FLAG_ALWAYS);
     sp_style_unref(child_spstyle);
@@ -1199,7 +1258,8 @@ name of the xml for a text span (ie tspan or flowspan). */
 static void recursively_apply_style(SPObject *common_ancestor, SPCSSAttr const *css, SPObject *start_item, Glib::ustring::iterator start_text_iter, SPObject *end_item, Glib::ustring::iterator end_text_iter, char const *span_object_name)
 {
     bool passed_start = start_item == NULL ? true : false;
-
+    Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(common_ancestor));
+    
     for (SPObject *child = common_ancestor->firstChild() ; child != NULL ; child = SP_OBJECT_NEXT(child)) {
         if (start_item == child)
             passed_start = true;
@@ -1217,7 +1277,7 @@ static void recursively_apply_style(SPObject *common_ancestor, SPCSSAttr const *
                 SPString *string_item = SP_STRING(child);
                 bool surround_entire_string = true;
 
-                Inkscape::XML::Node *child_span = sp_repr_new(span_object_name);
+                Inkscape::XML::Node *child_span = xml_doc->createElement(span_object_name);
                 sp_repr_css_set(child_span, const_cast<SPCSSAttr*>(css), "style");   // better hope that prototype wasn't nonconst for a good reason
                 SPObject *prev_item = SP_OBJECT_PREV(child);
                 Inkscape::XML::Node *prev_repr = prev_item ? SP_OBJECT_REPR(prev_item) : NULL;
@@ -1229,11 +1289,11 @@ static void recursively_apply_style(SPObject *common_ancestor, SPCSSAttr const *
                         unsigned start_char_index = char_index_of_iterator(string_item->string, start_text_iter);
                         unsigned end_char_index = char_index_of_iterator(string_item->string, end_text_iter);
 
-                        Inkscape::XML::Node *text_before = sp_repr_new_text(string_item->string.substr(0, start_char_index).c_str());
+                        Inkscape::XML::Node *text_before = xml_doc->createTextNode(string_item->string.substr(0, start_char_index).c_str());
                         SP_OBJECT_REPR(common_ancestor)->addChild(text_before, prev_repr);
                         SP_OBJECT_REPR(common_ancestor)->addChild(child_span, text_before);
                         Inkscape::GC::release(text_before);
-                        Inkscape::XML::Node *text_in_span = sp_repr_new_text(string_item->string.substr(start_char_index, end_char_index - start_char_index).c_str());
+                        Inkscape::XML::Node *text_in_span = xml_doc->createTextNode(string_item->string.substr(start_char_index, end_char_index - start_char_index).c_str());
                         child_span->appendChild(text_in_span);
                         Inkscape::GC::release(text_in_span);
                         SP_OBJECT_REPR(child)->setContent(string_item->string.substr(end_char_index).c_str());
@@ -1245,7 +1305,7 @@ static void recursively_apply_style(SPObject *common_ancestor, SPCSSAttr const *
                         unsigned end_char_index = char_index_of_iterator(string_item->string, end_text_iter);
 
                         SP_OBJECT_REPR(common_ancestor)->addChild(child_span, prev_repr);
-                        Inkscape::XML::Node *text_in_span = sp_repr_new_text(string_item->string.substr(0, end_char_index).c_str());
+                        Inkscape::XML::Node *text_in_span = xml_doc->createTextNode(string_item->string.substr(0, end_char_index).c_str());
                         child_span->appendChild(text_in_span);
                         Inkscape::GC::release(text_in_span);
                         SP_OBJECT_REPR(child)->setContent(string_item->string.substr(end_char_index).c_str());
@@ -1254,11 +1314,11 @@ static void recursively_apply_style(SPObject *common_ancestor, SPCSSAttr const *
                         // eg "abcDEF" -> "abc"<span>"DEF"</span>
                         unsigned start_char_index = char_index_of_iterator(string_item->string, start_text_iter);
 
-                        Inkscape::XML::Node *text_before = sp_repr_new_text(string_item->string.substr(0, start_char_index).c_str());
+                        Inkscape::XML::Node *text_before = xml_doc->createTextNode(string_item->string.substr(0, start_char_index).c_str());
                         SP_OBJECT_REPR(common_ancestor)->addChild(text_before, prev_repr);
                         SP_OBJECT_REPR(common_ancestor)->addChild(child_span, text_before);
                         Inkscape::GC::release(text_before);
-                        Inkscape::XML::Node *text_in_span = sp_repr_new_text(string_item->string.substr(start_char_index).c_str());
+                        Inkscape::XML::Node *text_in_span = xml_doc->createTextNode(string_item->string.substr(start_char_index).c_str());
                         child_span->appendChild(text_in_span);
                         Inkscape::GC::release(text_in_span);
                         child->deleteObject();
@@ -1502,7 +1562,8 @@ static bool redundant_semi_nesting_processor(SPObject **item, SPObject *child, b
     sp_repr_css_attr_unref(css_child_only);
     if (!equal) return false;
 
-    Inkscape::XML::Node *new_span = sp_repr_new(SP_OBJECT_REPR(*item)->name());
+    Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(*item)->document();
+    Inkscape::XML::Node *new_span = xml_doc->createElement(SP_OBJECT_REPR(*item)->name());
     if (prepend) {
         SPObject *prev = SP_OBJECT_PREV(*item);
         SP_OBJECT_REPR(SP_OBJECT_PARENT(*item))->addChild(new_span, prev ? SP_OBJECT_REPR(prev) : NULL);
@@ -1675,6 +1736,16 @@ void sp_te_apply_style(SPItem *text, Inkscape::Text::Layout::iterator const &sta
     if (is_line_break_object(end_item))
         end_item = SP_OBJECT_NEXT(end_item);
     if (end_item == 0) end_item = text;
+    
+    
+    /* Special case: With a tref, we only want to change its style when the whole
+     * string is selected, in which case the style can be applied directly to the
+     * tref node.  If only part of the tref's string child is selected, just return. */
+     
+    if (!sp_tref_fully_contained(start_item, start_text_iter, end_item, end_text_iter)) {
+        
+        return;
+    } 
 
     /* stage 1: applying the style. Go up to the closest common ancestor of
     start and end and then semi-recursively apply the style to all the