Code

fix converting 3d boxes to path and ungrouping - do not lose selection, preserve...
[inkscape.git] / src / path-chemistry.cpp
1 #define __SP_PATH_CHEMISTRY_C__
3 /*
4  * Here are handlers for modifying selections, specific to paths
5  *
6  * Authors:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   bulia byak <buliabyak@users.sf.net>
9  *
10  * Copyright (C) 1999-2004 Authors
11  * Copyright (C) 2001-2002 Ximian, Inc.
12  *
13  * Released under GNU GPL, read the file 'COPYING' for more information
14  */
16 #ifdef HAVE_CONFIG_H
17 # include <config.h>
18 #endif
19 #include <cstring>
20 #include <string>
21 #include "xml/repr.h"
22 #include "svg/svg.h"
23 #include "display/curve.h"
24 #include <glib/gmem.h>
25 #include <glibmm/i18n.h>
26 #include "sp-path.h"
27 #include "sp-text.h"
28 #include "sp-flowtext.h"
29 #include "libnr/nr-path.h"
30 #include "text-editing.h"
31 #include "style.h"
32 #include "inkscape.h"
33 #include "desktop.h"
34 #include "document.h"
35 #include "message-stack.h"
36 #include "selection.h"
37 #include "desktop-handles.h"
38 #include "box3d.h"
40 #include "path-chemistry.h"
42 /* Helper functions for sp_selected_path_to_curves */
43 static void sp_selected_path_to_curves0(gboolean do_document_done, guint32 text_grouping_policy);
44 enum {
45     /* Not used yet. This is the placeholder of Lauris's idea. */
46     SP_TOCURVE_INTERACTIVE       = 1 << 0,
47     SP_TOCURVE_GROUPING_BY_WORD  = 1 << 1,
48     SP_TOCURVE_GROUPING_BY_LINE  = 1 << 2,
49     SP_TOCURVE_GROUPING_BY_WHOLE = 1 << 3
50 };
52 void
53 sp_selected_path_combine(void)
54 {
55     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
57     Inkscape::Selection *selection = sp_desktop_selection(desktop);
58     GSList *items = (GSList *) selection->itemList();
60     if (g_slist_length(items) < 2) {
61         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>at least two objects</b> to combine."));
62         return;
63     }
65     for (GSList *i = items; i != NULL; i = i->next) {
66         SPItem *item = (SPItem *) i->data;
67         if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item)) {
68             sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("At least one of the objects is <b>not a path</b>, cannot combine."));
69             return;
70         }
71     }
73     Inkscape::XML::Node *parent = SP_OBJECT_REPR((SPItem *) items->data)->parent();
74     for (GSList *i = items; i != NULL; i = i->next) {
75         if ( SP_OBJECT_REPR((SPItem *) i->data)->parent() != parent ) {
76             sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("You cannot combine objects from <b>different groups</b> or <b>layers</b>."));
77             return;
78         }
79     }
81     desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Combining paths..."));
82     // set "busy" cursor
83     desktop->setWaitingCursor();
85     sp_selected_path_to_curves0(FALSE, 0);
87     items = g_slist_copy((GSList *) selection->itemList());
88     items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
89     selection->clear();
91     // remember the position of the topmost object
92     gint topmost = (SP_OBJECT_REPR((SPItem *) g_slist_last(items)->data))->position();
94     // remember the id of the bottomost object
95     char const *id = SP_OBJECT_REPR((SPItem *) items->data)->attribute("id");
97     // FIXME: merge styles of combined objects instead of using the first one's style
98     gchar *style = g_strdup(SP_OBJECT_REPR((SPItem *) items->data)->attribute("style"));
100     GString *dstring = g_string_new("");
101     for (GSList *i = items; i != NULL; i = i->next) {
103         SPPath *path = (SPPath *) i->data;
104         SPCurve *c = sp_shape_get_curve(SP_SHAPE(path));
106         NArtBpath *abp = nr_artpath_affine(SP_CURVE_BPATH(c), SP_ITEM(path)->transform);
107         sp_curve_unref(c);
108         gchar *str = sp_svg_write_path(abp);
109         g_free(abp);
111         dstring = g_string_append(dstring, str);
112         g_free(str);
114         // if this is the bottommost object,
115         if (!strcmp(SP_OBJECT_REPR(path)->attribute("id"), id)) {
116             // delete it so that its clones don't get alerted; this object will be restored shortly, with the same id
117             SP_OBJECT(path)->deleteObject(false);
118         } else {
119             // delete the object for real, so that its clones can take appropriate action
120             SP_OBJECT(path)->deleteObject();
121         }
123         topmost--;
124     }
126     g_slist_free(items);
128     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
129     Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
131     // restore id
132     repr->setAttribute("id", id);
134     repr->setAttribute("style", style);
135     g_free(style);
137     repr->setAttribute("d", dstring->str);
138     g_string_free(dstring, TRUE);
140     // add the new group to the group members' common parent
141     parent->appendChild(repr);
143     // move to the position of the topmost, reduced by the number of deleted items
144     repr->setPosition(topmost > 0 ? topmost + 1 : 0);
146     desktop->clearWaitingCursor();
148     sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_COMBINE, 
149                          _("Combine"));
151     selection->set(repr);
153     Inkscape::GC::release(repr);
156 void
157 sp_selected_path_break_apart(void)
159     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
161     Inkscape::Selection *selection = sp_desktop_selection(desktop);
163     if (selection->isEmpty()) {
164         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to break apart."));
165         return;
166     }
168     desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Breaking apart paths..."));
169     // set "busy" cursor
170     desktop->setWaitingCursor();
172     bool did = false;
174     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
175          items != NULL;
176          items = items->next) {
178         SPItem *item = (SPItem *) items->data;
180         if (!SP_IS_PATH(item))
181             continue;
183         SPPath *path = SP_PATH(item);
185         SPCurve *curve = sp_shape_get_curve(SP_SHAPE(path));
186         if (curve == NULL)
187             continue;
189         did = true;
191         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
192         gint pos = SP_OBJECT_REPR(item)->position();
193         char const *id = SP_OBJECT_REPR(item)->attribute("id");
195         gchar *style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
197         NArtBpath *abp = nr_artpath_affine(SP_CURVE_BPATH(curve), (SP_ITEM(path))->transform);
199         sp_curve_unref(curve);
201         // it's going to resurrect as one of the pieces, so we delete without advertisement
202         SP_OBJECT(item)->deleteObject(false);
204         curve = sp_curve_new_from_bpath(abp);
205         g_assert(curve != NULL);
207         GSList *list = sp_curve_split(curve);
209         sp_curve_unref(curve);
211         GSList *reprs = NULL;
212         for (GSList *l = list; l != NULL; l = l->next) {
213             curve = (SPCurve *) l->data;
215             Inkscape::XML::Node *repr = parent->document()->createElement("svg:path");
216             repr->setAttribute("style", style);
218             gchar *str = sp_svg_write_path(SP_CURVE_BPATH(curve));
219             repr->setAttribute("d", str);
220             g_free(str);
222             // add the new repr to the parent
223             parent->appendChild(repr);
225             // move to the saved position
226             repr->setPosition(pos > 0 ? pos : 0);
228             // if it's the first one, restore id
229             if (l == list)
230                 repr->setAttribute("id", id);
232             reprs = g_slist_prepend (reprs, repr);
234             Inkscape::GC::release(repr);
235         }
237         selection->setReprList(reprs);
239         g_slist_free(reprs);
240         g_slist_free(list);
241         g_free(style);
243     }
245     desktop->clearWaitingCursor();
247     if (did) {
248         sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_BREAK_APART, 
249                          _("Break apart"));
250     } else {
251         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to break apart in the selection."));
252         return;
253     }
256 /* This function is an entry point from GUI */
257 void
258 sp_selected_path_to_curves(void)
260     sp_selected_path_to_curves0(TRUE, SP_TOCURVE_INTERACTIVE);
263 static void
264 sp_selected_path_to_curves0(gboolean interactive, guint32 /*text_grouping_policy*/)
266     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
268     Inkscape::Selection *selection = sp_desktop_selection(desktop);
270     if (selection->isEmpty()) {
271         if (interactive)
272             sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to path."));
273         return;
274     }
276     bool did = false;
277     if (interactive) {
278         desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Converting objects to paths..."));
279         // set "busy" cursor
280         desktop->setWaitingCursor();
281     }
283     GSList *selected = g_slist_copy((GSList *) selection->itemList());
284     GSList *to_select = NULL;
285     selection->clear();
286     GSList *items = g_slist_copy(selected);
288     for (;
289          items != NULL;
290          items = items->next) {
292         SPItem *item = SP_ITEM(items->data);
294         if (SP_IS_PATH(item) && !SP_PATH(item)->original_curve) {
295             continue; // already a path, and no path effect
296         }
298         if (SP_IS_BOX3D(item)) {
299             // convert 3D box to ordinary group of paths; replace the old element in 'selected' with the new group
300             Inkscape::XML::Node *repr = box3d_convert_to_group(SP_BOX3D(item));
301             
302             if (repr) {
303                 to_select = g_slist_prepend (to_select, repr);
304                 did = true;
305                 selected = g_slist_remove (selected, item);
306             }
308             continue;
309         }
311         Inkscape::XML::Node *repr = sp_selected_item_to_curved_repr(item, 0);
312         if (!repr)
313             continue;
315         did = true;
316         selected = g_slist_remove (selected, item);
318         // remember the position of the item
319         gint pos = SP_OBJECT_REPR(item)->position();
320         // remember parent
321         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
322         // remember id
323         char const *id = SP_OBJECT_REPR(item)->attribute("id");
325         // It's going to resurrect, so we delete without notifying listeners.
326         SP_OBJECT(item)->deleteObject(false);
328         // restore id
329         repr->setAttribute("id", id);
330         // add the new repr to the parent
331         parent->appendChild(repr);
332         // move to the saved position
333         repr->setPosition(pos > 0 ? pos : 0);
335         /* Buglet: We don't re-add the (new version of the) object to the selection of any other
336          * desktops where it was previously selected. */
337         to_select = g_slist_prepend (to_select, repr);
338         Inkscape::GC::release(repr);
339     }
341     g_slist_free (items);
342     selection->setReprList(to_select);
343     selection->addList(selected);
344     g_slist_free (to_select);
345     g_slist_free (selected);
347     if (interactive) {
348         desktop->clearWaitingCursor();
349         if (did) {
350             sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_TO_CURVE, 
351                              _("Object to path"));
352         } else {
353             sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No objects</b> to convert to path in the selection."));
354             return;
355         }
356     }
359 Inkscape::XML::Node *
360 sp_selected_item_to_curved_repr(SPItem *item, guint32 /*text_grouping_policy*/)
362     if (!item)
363         return NULL;
365     SPCurve *curve = NULL;
366     if (SP_IS_SHAPE(item)) {
367         curve = sp_shape_get_curve(SP_SHAPE(item));
368     } else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
369         curve = te_get_layout(item)->convertToCurves();
370     }
372     if (!curve)
373         return NULL;
375     Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(item)->document();
376     Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
377     /* Transformation */
378     repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
379     /* Style */
380     gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
381                                                  SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
382     repr->setAttribute("style", style_str);
383     g_free(style_str);
385     /* Mask */
386     gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
387     if ( mask_str )
388         repr->setAttribute("mask", mask_str);
390     /* Clip path */
391     gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
392     if ( clip_path_str )
393         repr->setAttribute("clip-path", clip_path_str);
395     /* Rotation center */
396     sp_repr_set_attr(repr, "inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"));
397     sp_repr_set_attr(repr, "inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"));
399     /* Definition */
400     gchar *def_str = sp_svg_write_path(SP_CURVE_BPATH(curve));
401     repr->setAttribute("d", def_str);
402     g_free(def_str);
403     sp_curve_unref(curve);
404     return repr;
408 // FIXME: THIS DOES NOT REVERSE THE NODETYPES ORDER!
409 void
410 sp_selected_path_reverse()
412     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
414     Inkscape::Selection *selection = sp_desktop_selection(desktop);
415     GSList *items = (GSList *) selection->itemList();
417     if (!items) {
418         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to reverse."));
419         return;
420     }
423     // set "busy" cursor
424     desktop->setWaitingCursor();
426     bool did = false;
427     desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Reversing paths..."));
429     for (GSList *i = items; i != NULL; i = i->next) {
431         if (!SP_IS_PATH(i->data))
432             continue;
434         did = true;
435         SPPath *path = SP_PATH(i->data);
437         SPCurve *rcurve = sp_curve_reverse(sp_path_get_curve_reference(path));
439         gchar *str = sp_svg_write_path(SP_CURVE_BPATH(rcurve));
440         if ( sp_shape_has_path_effect(SP_SHAPE(path)) ) {
441             SP_OBJECT_REPR(path)->setAttribute("inkscape:original-d", str);
442         } else {
443             SP_OBJECT_REPR(path)->setAttribute("d", str);
444         }
445         g_free(str);
447         sp_curve_unref(rcurve);
448     }
450     desktop->clearWaitingCursor();
452     if (did) {
453         sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_REVERSE,
454                          _("Reverse path"));
455     } else {
456         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to reverse in the selection."));
457     }
460 /*
461   Local Variables:
462   mode:c++
463   c-file-style:"stroustrup"
464   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
465   indent-tabs-mode:nil
466   fill-column:99
467   End:
468 */
469 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :