Code

Adding use of GtkScaleButton when available
[inkscape.git] / src / splivarot.cpp
index 6759515f2ea710b9d1610929eddd2f958b25d32b..5868140a6d25b101fe677b514e1c2cd51b84da8b 100644 (file)
 # include <config.h>
 #endif
 
+#include <cstring>
+#include <string>
 #include <vector>
 #include <glib/gmem.h>
 #include "xml/repr.h"
 #include "svg/svg.h"
 #include "sp-path.h"
 #include "sp-shape.h"
-#include "sp-marker.h"
+#include "sp-image.h"
+#include "marker.h"
 #include "enums.h"
 #include "sp-text.h"
+#include "sp-flowtext.h"
+#include "text-editing.h"
 #include "sp-item-group.h"
 #include "style.h"
 #include "inkscape.h"
@@ -66,6 +71,12 @@ sp_selected_path_union()
     sp_selected_path_boolop(bool_op_union, SP_VERB_SELECTION_UNION, _("Union"));
 }
 
+void
+sp_selected_path_union_skip_undo()
+{
+    sp_selected_path_boolop(bool_op_union, SP_VERB_NONE, _("Union"));
+}
+
 void
 sp_selected_path_intersect()
 {
@@ -78,6 +89,12 @@ sp_selected_path_diff()
     sp_selected_path_boolop(bool_op_diff, SP_VERB_SELECTION_DIFF, _("Difference"));
 }
 
+void
+sp_selected_path_diff_skip_undo()
+{
+    sp_selected_path_boolop(bool_op_diff, SP_VERB_NONE, _("Difference"));
+}
+
 void
 sp_selected_path_symdiff()
 {
@@ -103,13 +120,18 @@ sp_selected_path_boolop(bool_op bop, const unsigned int verb, const Glib::ustrin
     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
 
     Inkscape::Selection *selection = sp_desktop_selection(desktop);
-
+    
     GSList *il = (GSList *) selection->itemList();
-
-    if (g_slist_length(il) < 2) {
+    
+    // allow union on a single object for the purpose of removing self overlapse (svn log, revision 13334)
+    if ( (g_slist_length(il) < 2) && (bop != bool_op_union)) {
         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>at least 2 paths</b> to perform a boolean operation."));
         return;
     }
+    else if ( g_slist_length(il) < 1 ) {
+        desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>at least 1 path</b> to perform a boolean union."));
+        return;
+    }
 
     if (g_slist_length(il) > 2) {
         if (bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice ) {
@@ -173,7 +195,7 @@ sp_selected_path_boolop(bool_op bop, const unsigned int verb, const Glib::ustrin
     for (GSList *l = il; l != NULL; l = l->next)
     {
         SPItem *item = SP_ITEM(l->data);
-        if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
+        if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item) && !SP_IS_FLOWTEXT(item))
         {
             desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("One of the objects is <b>not a path</b>, cannot perform boolean operation."));
             g_slist_free(il);
@@ -414,22 +436,38 @@ sp_selected_path_boolop(bool_op bop, const unsigned int verb, const Glib::ustrin
         return;
     }
 
-    // remember important aspects of the source path, to be restored
-    Inkscape::XML::Node *repr_source;
+    // get the source path object
+    SPObject *source;
     if ( bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice ) {
         if (reverseOrderForOp) {
-             repr_source = SP_OBJECT_REPR(il->data);
+             source = SP_OBJECT(il->data);
         } else {
-             repr_source = SP_OBJECT_REPR(il->next->data);
+             source = SP_OBJECT(il->next->data);
         }
     } else {
         // find out the bottom object
         GSList *sorted = g_slist_copy((GSList *) selection->reprList());
 
         sorted = g_slist_sort(sorted, (GCompareFunc) sp_repr_compare_position);
-        repr_source = ((Inkscape::XML::Node *) sorted->data);
+
+        source = sp_desktop_document(desktop)->
+            getObjectByRepr((Inkscape::XML::Node *)sorted->data);
+
         g_slist_free(sorted);
     }
+
+    // adjust style properties that depend on a possible transform in the source object in order
+    // to get a correct style attribute for the new path
+    SPItem* item_source = SP_ITEM(source);
+    NR::Matrix i2root = from_2geom(sp_item_i2root_affine(item_source));
+    sp_item_adjust_stroke(item_source, NR::expansion(i2root));
+    sp_item_adjust_pattern(item_source, i2root);
+    sp_item_adjust_gradient(item_source, i2root);
+    sp_item_adjust_livepatheffect(item_source, i2root);
+
+    Inkscape::XML::Node *repr_source = SP_OBJECT_REPR(source);
+
+    // remember important aspects of the source path, to be restored
     gint pos = repr_source->position();
     Inkscape::XML::Node *parent = sp_repr_parent(repr_source);
     gchar const *id = repr_source->attribute("id");
@@ -437,7 +475,6 @@ sp_selected_path_boolop(bool_op bop, const unsigned int verb, const Glib::ustrin
     gchar const *mask = repr_source->attribute("mask");
     gchar const *clip_path = repr_source->attribute("clip-path");
 
-
     // remove source paths
     selection->clear();
     for (GSList *l = il; l != NULL; l = l->next) {
@@ -454,12 +491,8 @@ sp_selected_path_boolop(bool_op bop, const unsigned int verb, const Glib::ustrin
 
     // premultiply by the inverse of parent's repr
     SPItem *parent_item = SP_ITEM(sp_desktop_document(desktop)->getObjectByRepr(parent));
-    NR::Matrix local = sp_item_i2doc_affine(parent_item);
-    gchar affinestr[80];
-    gchar *transform = NULL;
-    if (!local.test_identity() && sp_svg_transform_write(affinestr, 79, local.inverse())) {
-        transform = affinestr;
-    }
+    NR::Matrix local = from_2geom(sp_item_i2doc_affine(parent_item));
+    gchar *transform = sp_svg_transform_write(local.inverse());
 
     // now that we have the result, add it on the canvas
     if ( bop == bool_op_cut || bop == bool_op_slice ) {
@@ -487,7 +520,8 @@ sp_selected_path_boolop(bool_op bop, const unsigned int verb, const Glib::ustrin
         for (int i=0;i<nbRP;i++) {
             gchar *d = resPath[i]->svg_dump_path();
 
-            Inkscape::XML::Node *repr = sp_repr_new("svg:path");
+            Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
+            Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
             repr->setAttribute("style", style);
             if (mask)
                 repr->setAttribute("mask", mask);
@@ -532,7 +566,8 @@ sp_selected_path_boolop(bool_op bop, const unsigned int verb, const Glib::ustrin
     } else {
         gchar *d = res->svg_dump_path();
 
-        Inkscape::XML::Node *repr = sp_repr_new("svg:path");
+        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
+        Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
         repr->setAttribute("style", style);
 
         if ( mask )
@@ -554,7 +589,11 @@ sp_selected_path_boolop(bool_op bop, const unsigned int verb, const Glib::ustrin
         Inkscape::GC::release(repr);
     }
 
-    sp_document_done(sp_desktop_document(desktop), verb, description);
+    g_free(transform);
+
+    if (verb != SP_VERB_NONE) {
+        sp_document_done(sp_desktop_document(desktop), verb, description);
+    }
 
     delete res;
 }
@@ -600,7 +639,7 @@ sp_selected_path_outline()
             gchar const *val = sp_repr_css_property(css, "stroke", NULL);
 
             if (val == NULL || strcmp(val, "none") == 0) {
-                sp_curve_unref(curve);
+                curve->unref();
                 continue;
             }
         }
@@ -628,9 +667,11 @@ sp_selected_path_outline()
         }
 
         NR::Matrix const transform(item->transform);
-        float const scale = transform.expansion();
+        float const scale = NR::expansion(transform);
         gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
         SPStyle *i_style = SP_OBJECT(item)->style;
+        gchar const *mask = SP_OBJECT_REPR(item)->attribute("mask");
+        gchar const *clip_path = SP_OBJECT_REPR(item)->attribute("clip-path");
 
         float o_width, o_miter;
         JoinType o_join;
@@ -675,7 +716,7 @@ sp_selected_path_outline()
         Path *orig = Path_for_item(item, false);
         if (orig == NULL) {
             g_free(style);
-            sp_curve_unref(curve);
+            curve->unref();
             continue;
         }
 
@@ -748,7 +789,8 @@ sp_selected_path_outline()
 
         if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
 
-            Inkscape::XML::Node *repr = sp_repr_new("svg:path");
+            Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
+            Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
 
             // restore old style
             repr->setAttribute("style", style);
@@ -762,10 +804,15 @@ sp_selected_path_outline()
             repr->setAttribute("d", str);
             g_free(str);
 
+            if (mask)
+                repr->setAttribute("mask", mask);
+            if (clip_path)
+                repr->setAttribute("clip-path", clip_path);
 
             if (SP_IS_SHAPE(item) && sp_shape_has_markers (SP_SHAPE(item))) {
 
-                Inkscape::XML::Node *g_repr = sp_repr_new("svg:g");
+                Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
+                Inkscape::XML::Node *g_repr = xml_doc->createElement("svg:g");
 
                 // add the group to the parent
                 parent->appendChild(g_repr);
@@ -779,7 +826,7 @@ sp_selected_path_outline()
 
                 SPShape *shape = SP_SHAPE(item);
 
-                for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
+                for (NArtBpath const* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
                     for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
                         if (sp_shape_marker_required (shape, m, bp)) {
 
@@ -796,7 +843,7 @@ sp_selected_path_outline()
                             tr = marker_item->transform * marker->c2p * tr * transform;
 
                             if (SP_OBJECT_REPR(marker_item)) {
-                                Inkscape::XML::Node *m_repr = SP_OBJECT_REPR(marker_item)->duplicate();
+                                Inkscape::XML::Node *m_repr = SP_OBJECT_REPR(marker_item)->duplicate(xml_doc);
                                 g_repr->appendChild(m_repr);
                                 SPItem *marker_item = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(m_repr);
                                 sp_item_write_transform(marker_item, m_repr, tr);
@@ -830,7 +877,7 @@ sp_selected_path_outline()
 
             Inkscape::GC::release(repr);
 
-            sp_curve_unref(curve);
+            curve->unref();
             selection->remove(item);
             SP_OBJECT(item)->deleteObject(false);
 
@@ -1005,7 +1052,7 @@ sp_selected_path_create_offset_object(int expand, bool updating)
     if (orig == NULL)
     {
         g_free(style);
-        sp_curve_unref(curve);
+        curve->unref();
         return;
     }
 
@@ -1042,14 +1089,17 @@ sp_selected_path_create_offset_object(int expand, bool updating)
         delete theRes;
     }
 
-    sp_curve_unref(curve);
+    curve->unref();
 
     if (res->descr_cmd.size() <= 1)
     {
         // pas vraiment de points sur le resultat
         // donc il ne reste rien
-        sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE, 
-                         _("Create offset object"));
+        sp_document_done(sp_desktop_document(desktop), 
+                         (updating ? SP_VERB_SELECTION_LINKED_OFFSET 
+                          : SP_VERB_SELECTION_DYNAMIC_OFFSET),
+                         (updating ? _("Create linked offset")
+                          : _("Create dynamic offset")));
         selection->clear();
 
         delete res;
@@ -1063,7 +1113,8 @@ sp_selected_path_create_offset_object(int expand, bool updating)
 
         tstr[79] = '\0';
 
-        repr = sp_repr_new("svg:path");
+        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
+        repr = xml_doc->createElement("svg:path");
         repr->setAttribute("sodipodi:type", "inkscape:offset");
         sp_repr_set_svg_double(repr, "inkscape:radius", ( expand > 0
                                                           ? o_width
@@ -1113,8 +1164,11 @@ sp_selected_path_create_offset_object(int expand, bool updating)
         selection->set(nitem);
     }
 
-    sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE, 
-                     _("Create offset object"));
+    sp_document_done(sp_desktop_document(desktop), 
+                     (updating ? SP_VERB_SELECTION_LINKED_OFFSET 
+                      : SP_VERB_SELECTION_DYNAMIC_OFFSET),
+                     (updating ? _("Create linked offset")
+                      : _("Create dynamic offset")));
 
     delete res;
     delete orig;
@@ -1220,7 +1274,7 @@ sp_selected_path_do_offset(bool expand, double prefOffset)
         Path *orig = Path_for_item(item, false);
         if (orig == NULL) {
             g_free(style);
-            sp_curve_unref(curve);
+            curve->unref();
             continue;
         }
 
@@ -1308,7 +1362,7 @@ sp_selected_path_do_offset(bool expand, double prefOffset)
 
         did = true;
 
-        sp_curve_unref(curve);
+        curve->unref();
         // remember the position of the item
         gint pos = SP_OBJECT_REPR(item)->position();
         // remember parent
@@ -1325,7 +1379,8 @@ sp_selected_path_do_offset(bool expand, double prefOffset)
 
             tstr[79] = '\0';
 
-            Inkscape::XML::Node *repr = sp_repr_new("svg:path");
+            Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
+            Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
 
             repr->setAttribute("style", style);
 
@@ -1356,8 +1411,9 @@ sp_selected_path_do_offset(bool expand, double prefOffset)
     }
 
     if (did) {
-        sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE, 
-                         _("Inset/outset path"));
+        sp_document_done(sp_desktop_document(desktop), 
+                         (expand ? SP_VERB_SELECTION_OFFSET : SP_VERB_SELECTION_INSET),
+                         (expand ? _("Outset path") : _("Inset path")));
     } else {
         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to inset/outset in the selection."));
         return;
@@ -1426,17 +1482,19 @@ sp_selected_path_simplify_item(SPDesktop *desktop,
     Path *orig = Path_for_item(item, false);
     if (orig == NULL) {
         g_free(style);
-        sp_curve_unref(curve);
+        curve->unref();
         return false;
     }
 
-    sp_curve_unref(curve);
+    curve->unref();
     // remember the position of the item
     gint pos = SP_OBJECT_REPR(item)->position();
     // remember parent
     Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
     // remember id
     char const *id = SP_OBJECT_REPR(item)->attribute("id");
+    // remember path effect
+    char const *patheffect = SP_OBJECT_REPR(item)->attribute("inkscape:path-effect");
 
     //If a group was selected, to not change the selection list
     if (modifySelection)
@@ -1451,7 +1509,8 @@ sp_selected_path_simplify_item(SPDesktop *desktop,
         orig->Simplify(threshold * size);
     }
 
-    Inkscape::XML::Node *repr = sp_repr_new("svg:path");
+    Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
+    Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
 
     // restore style, mask and clip-path
     repr->setAttribute("style", style);
@@ -1469,7 +1528,10 @@ sp_selected_path_simplify_item(SPDesktop *desktop,
 
     // path
     gchar *str = orig->svg_dump_path();
-    repr->setAttribute("d", str);
+    if (patheffect)
+        repr->setAttribute("inkscape:original-d", str);
+    else 
+        repr->setAttribute("d", str);
     g_free(str);
 
     // restore id
@@ -1486,6 +1548,9 @@ sp_selected_path_simplify_item(SPDesktop *desktop,
     // reapply the transform
     sp_item_write_transform(newitem, repr, transform);
 
+    // restore path effect
+    repr->setAttribute("inkscape:path-effect", patheffect);
+
     //If we are not in a selected group
     if (modifySelection)
         selection->add(repr);
@@ -1511,22 +1576,26 @@ sp_selected_path_simplify_items(SPDesktop *desktop,
   
   gchar *simplificationType;
   if (simplifyIndividualPaths) {
-    simplificationType = "individual paths";
+      simplificationType = _("Simplifying paths (separately):");
   } else {
-    simplificationType = "as a group";
+      simplificationType = _("Simplifying paths:");
   }
 
   bool didSomething = false;
 
-  NR::Rect selectionBbox = selection->bounds();
-  gdouble selectionSize  = L2(selectionBbox.dimensions());
+  NR::Maybe<NR::Rect> selectionBbox = selection->bounds();
+  if (!selectionBbox) {
+    return false;
+  }
+  gdouble selectionSize  = L2(selectionBbox->dimensions());
 
   gdouble simplifySize  = selectionSize;
   
   int pathsSimplified = 0;
   int totalPathCount  = g_slist_length(items);
   
-  desktop->disableInteraction();
+  // set "busy" cursor
+  desktop->setWaitingCursor();
   
   for (; items != NULL; items = items->next) {
       SPItem *item = (SPItem *) items->data;
@@ -1535,27 +1604,29 @@ sp_selected_path_simplify_items(SPDesktop *desktop,
           continue;
 
       if (simplifyIndividualPaths) {
-          NR::Rect itemBbox = item->invokeBbox(sp_item_i2d_affine(item));        
-          simplifySize      = L2(itemBbox.dimensions());
+          NR::Maybe<NR::Rect> itemBbox = item->getBounds(from_2geom(sp_item_i2d_affine(item)));
+          if (itemBbox) {
+              simplifySize      = L2(itemBbox->dimensions());
+          } else {
+              simplifySize      = 0;
+          }
       }
 
-
       pathsSimplified++;
 
       if (pathsSimplified % 20 == 0) {
-        gchar *message = g_strdup_printf(_("Simplifying %s - <b>%d</b> of <b>%d</b> paths simplified..."), simplificationType, pathsSimplified, totalPathCount);
-        desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, message);
-        desktop->updateCanvasNow();
+        gchar *message = g_strdup_printf(_("%s <b>%d</b> of <b>%d</b> paths simplified..."), simplificationType, pathsSimplified, totalPathCount);
+        desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, message);
       }
 
       didSomething |= sp_selected_path_simplify_item(desktop, selection, item,
                           threshold, justCoalesce, angleLimit, breakableAngles, simplifySize, modifySelection);
   }
 
-  desktop->enableInteraction();
-  
+  desktop->clearWaitingCursor();
+
   if (pathsSimplified > 20) {
-    desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, g_strdup_printf(_("Done - <b>%d</b> paths simplified."), pathsSimplified));
+    desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, g_strdup_printf(_("<b>%d</b> paths simplified."), pathsSimplified));
   }
   
   return didSomething;
@@ -1649,41 +1720,77 @@ Ancetre(Inkscape::XML::Node *a, Inkscape::XML::Node *who)
 Path *
 Path_for_item(SPItem *item, bool doTransformation, bool transformFull)
 {
-    SPCurve *curve;
+    SPCurve *curve = curve_for_item(item);
+    NArtBpath *bpath = bpath_for_curve(item, curve, doTransformation, transformFull);
+    
+    if (bpath == NULL) {
+        return NULL;
+    }
+    
+    Path *dest = bpath_to_Path(bpath);
 
-    if (!item)
+    g_free(bpath);
+
+    curve->unref();
+    
+    return dest;
+}
+
+/* 
+ * This function always returns a new NArtBpath, the caller must g_free the returned path!
+*/
+NArtBpath *
+bpath_for_curve(SPItem *item, SPCurve *curve, bool doTransformation, bool transformFull)
+{
+    if (curve == NULL)
         return NULL;
 
-    if (SP_IS_SHAPE(item))
-    {
-        curve = sp_shape_get_curve(SP_SHAPE(item));
-    }
-    else if (SP_IS_TEXT(item))
-    {
-        curve = SP_TEXT(item)->getNormalizedBpath();
+    NArtBpath const *bpath = SP_CURVE_BPATH(curve);
+    if (bpath == NULL) {
+        return NULL;
     }
-    else
-    {
-        curve = NULL;
+
+    NArtBpath *new_bpath; // we will get a duplicate which has to be freed at some point!
+    if (doTransformation) {
+        if (transformFull) {
+            new_bpath = nr_artpath_affine(bpath, from_2geom(sp_item_i2doc_affine(item)));
+        } else {
+            new_bpath = nr_artpath_affine(bpath, item->transform);
+        }
+    } else {
+        new_bpath = nr_artpath_affine(bpath, NR::identity());
     }
 
-    if (!curve)
-        return NULL;
-    NArtBpath *bpath = SP_CURVE_BPATH(curve);
-    if (bpath == NULL)
+    return new_bpath;
+}
+
+SPCurve* curve_for_item(SPItem *item)
+{
+    if (!item)
         return NULL;
 
-    if ( doTransformation ) {
-        if (transformFull)
-            bpath = nr_artpath_affine(SP_CURVE_BPATH(curve), sp_item_i2doc_affine(item));
-        else
-            bpath = nr_artpath_affine(SP_CURVE_BPATH(curve), item->transform);
-        sp_curve_unref(curve);
-        curve=NULL;
-    } else {
-        bpath=SP_CURVE_BPATH(curve);
+    SPCurve *curve = NULL;
+
+    if (SP_IS_SHAPE(item)) {
+        if (SP_IS_PATH(item)) {
+            curve = sp_path_get_curve_for_edit(SP_PATH(item));
+        } else {
+            curve = sp_shape_get_curve(SP_SHAPE(item));
+        }
+    }
+    else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item))
+    {
+        curve = te_get_layout(item)->convertToCurves();
+    }
+    else if (SP_IS_IMAGE(item))
+    {
+        curve = sp_image_get_curve(SP_IMAGE(item));
     }
+    
+    return curve; // do not forget to unref the curve at some point!
+}
 
+Path *bpath_to_Path(NArtBpath const *bpath) {
     Path *dest = new Path;
     dest->SetBackData(false);
     {
@@ -1737,19 +1844,13 @@ Path_for_item(SPItem *item, bool doTransformation, bool transformFull)
         if (closed)
             dest->Close();
     }
-
-    if ( doTransformation ) {
-        if ( bpath ) g_free(bpath);
-    } else {
-        sp_curve_unref(curve);
-    }
     return dest;
 }
 
-NR::Maybe<Path::cut_position> get_nearest_position_on_Path(Path *path, NR::Point p)
+NR::Maybe<Path::cut_position> get_nearest_position_on_Path(Path *path, NR::Point p, unsigned seg)
 {
     //get nearest position on path
-    Path::cut_position pos = path->PointToCurvilignPosition(p);
+    Path::cut_position pos = path->PointToCurvilignPosition(p, seg);
     return pos;
 }