Code

Convert 3D boxes to ordinary groups before tweaking, ungrouping or applying 'convert...
[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 "xml/repr.h"
20 #include "svg/svg.h"
21 #include "display/curve.h"
22 #include <glib/gmem.h>
23 #include <glibmm/i18n.h>
24 #include "sp-path.h"
25 #include "sp-text.h"
26 #include "sp-flowtext.h"
27 #include "libnr/nr-path.h"
28 #include "text-editing.h"
29 #include "style.h"
30 #include "inkscape.h"
31 #include "desktop.h"
32 #include "document.h"
33 #include "message-stack.h"
34 #include "selection.h"
35 #include "desktop-handles.h"
36 #include "box3d.h"
38 #include "path-chemistry.h"
40 /* Helper functions for sp_selected_path_to_curves */
41 static void sp_selected_path_to_curves0(gboolean do_document_done, guint32 text_grouping_policy);
42 enum {
43     /* Not used yet. This is the placeholder of Lauris's idea. */
44     SP_TOCURVE_INTERACTIVE       = 1 << 0,
45     SP_TOCURVE_GROUPING_BY_WORD  = 1 << 1,
46     SP_TOCURVE_GROUPING_BY_LINE  = 1 << 2,
47     SP_TOCURVE_GROUPING_BY_WHOLE = 1 << 3
48 };
50 void
51 sp_selected_path_combine(void)
52 {
53     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
55     Inkscape::Selection *selection = sp_desktop_selection(desktop);
56     GSList *items = (GSList *) selection->itemList();
58     if (g_slist_length(items) < 2) {
59         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>at least two objects</b> to combine."));
60         return;
61     }
63     for (GSList *i = items; i != NULL; i = i->next) {
64         SPItem *item = (SPItem *) i->data;
65         if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item)) {
66             sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("At least one of the objects is <b>not a path</b>, cannot combine."));
67             return;
68         }
69     }
71     Inkscape::XML::Node *parent = SP_OBJECT_REPR((SPItem *) items->data)->parent();
72     for (GSList *i = items; i != NULL; i = i->next) {
73         if ( SP_OBJECT_REPR((SPItem *) i->data)->parent() != parent ) {
74             sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("You cannot combine objects from <b>different groups</b> or <b>layers</b>."));
75             return;
76         }
77     }
79     desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Combining paths..."));
80     // set "busy" cursor
81     desktop->setWaitingCursor();
83     sp_selected_path_to_curves0(FALSE, 0);
85     items = g_slist_copy((GSList *) selection->itemList());
86     items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
87     selection->clear();
89     // remember the position of the topmost object
90     gint topmost = (SP_OBJECT_REPR((SPItem *) g_slist_last(items)->data))->position();
92     // remember the id of the bottomost object
93     char const *id = SP_OBJECT_REPR((SPItem *) items->data)->attribute("id");
95     // FIXME: merge styles of combined objects instead of using the first one's style
96     gchar *style = g_strdup(SP_OBJECT_REPR((SPItem *) items->data)->attribute("style"));
98     GString *dstring = g_string_new("");
99     for (GSList *i = items; i != NULL; i = i->next) {
101         SPPath *path = (SPPath *) i->data;
102         SPCurve *c = sp_shape_get_curve(SP_SHAPE(path));
104         NArtBpath *abp = nr_artpath_affine(SP_CURVE_BPATH(c), SP_ITEM(path)->transform);
105         sp_curve_unref(c);
106         gchar *str = sp_svg_write_path(abp);
107         g_free(abp);
109         dstring = g_string_append(dstring, str);
110         g_free(str);
112         // if this is the bottommost object,
113         if (!strcmp(SP_OBJECT_REPR(path)->attribute("id"), id)) {
114             // delete it so that its clones don't get alerted; this object will be restored shortly, with the same id
115             SP_OBJECT(path)->deleteObject(false);
116         } else {
117             // delete the object for real, so that its clones can take appropriate action
118             SP_OBJECT(path)->deleteObject();
119         }
121         topmost--;
122     }
124     g_slist_free(items);
126     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
127     Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
129     // restore id
130     repr->setAttribute("id", id);
132     repr->setAttribute("style", style);
133     g_free(style);
135     repr->setAttribute("d", dstring->str);
136     g_string_free(dstring, TRUE);
138     // add the new group to the group members' common parent
139     parent->appendChild(repr);
141     // move to the position of the topmost, reduced by the number of deleted items
142     repr->setPosition(topmost > 0 ? topmost + 1 : 0);
144     desktop->clearWaitingCursor();
146     sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_COMBINE, 
147                          _("Combine"));
149     selection->set(repr);
151     Inkscape::GC::release(repr);
154 void
155 sp_selected_path_break_apart(void)
157     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
159     Inkscape::Selection *selection = sp_desktop_selection(desktop);
161     if (selection->isEmpty()) {
162         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to break apart."));
163         return;
164     }
166     desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Breaking apart paths..."));
167     // set "busy" cursor
168     desktop->setWaitingCursor();
170     bool did = false;
172     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
173          items != NULL;
174          items = items->next) {
176         SPItem *item = (SPItem *) items->data;
178         if (!SP_IS_PATH(item))
179             continue;
181         SPPath *path = SP_PATH(item);
183         SPCurve *curve = sp_shape_get_curve(SP_SHAPE(path));
184         if (curve == NULL)
185             continue;
187         did = true;
189         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
190         gint pos = SP_OBJECT_REPR(item)->position();
191         char const *id = SP_OBJECT_REPR(item)->attribute("id");
193         gchar *style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
195         NArtBpath *abp = nr_artpath_affine(SP_CURVE_BPATH(curve), (SP_ITEM(path))->transform);
197         sp_curve_unref(curve);
199         // it's going to resurrect as one of the pieces, so we delete without advertisement
200         SP_OBJECT(item)->deleteObject(false);
202         curve = sp_curve_new_from_bpath(abp);
203         g_assert(curve != NULL);
205         GSList *list = sp_curve_split(curve);
207         sp_curve_unref(curve);
209         GSList *reprs = NULL;
210         for (GSList *l = list; l != NULL; l = l->next) {
211             curve = (SPCurve *) l->data;
213             Inkscape::XML::Node *repr = parent->document()->createElement("svg:path");
214             repr->setAttribute("style", style);
216             gchar *str = sp_svg_write_path(SP_CURVE_BPATH(curve));
217             repr->setAttribute("d", str);
218             g_free(str);
220             // add the new repr to the parent
221             parent->appendChild(repr);
223             // move to the saved position
224             repr->setPosition(pos > 0 ? pos : 0);
226             // if it's the first one, restore id
227             if (l == list)
228                 repr->setAttribute("id", id);
230             reprs = g_slist_prepend (reprs, repr);
232             Inkscape::GC::release(repr);
233         }
235         selection->setReprList(reprs);
237         g_slist_free(reprs);
238         g_slist_free(list);
239         g_free(style);
241     }
243     desktop->clearWaitingCursor();
245     if (did) {
246         sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_BREAK_APART, 
247                          _("Break apart"));
248     } else {
249         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to break apart in the selection."));
250         return;
251     }
254 /* This function is an entry point from GUI */
255 void
256 sp_selected_path_to_curves(void)
258     sp_selected_path_to_curves0(TRUE, SP_TOCURVE_INTERACTIVE);
261 static void
262 sp_selected_path_to_curves0(gboolean interactive, guint32 /*text_grouping_policy*/)
264     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
266     Inkscape::Selection *selection = sp_desktop_selection(desktop);
268     if (selection->isEmpty()) {
269         if (interactive)
270             sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to path."));
271         return;
272     }
274     bool did = false;
275     if (interactive) {
276         desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Converting objects to paths..."));
277         // set "busy" cursor
278         desktop->setWaitingCursor();
279     }
281     GSList *selected = g_slist_copy((GSList *) selection->itemList());
282     GSList *to_select = NULL;
283     selection->clear();
284     GSList *items = g_slist_copy(selected);
286     for (;
287          items != NULL;
288          items = items->next) {
290         SPItem *item = SP_ITEM(items->data);
292         if (SP_IS_PATH(item) && !SP_PATH(item)->original_curve) {
293             continue; // already a path, and no path effect
294         }
296         if (SP_IS_BOX3D(item)) {
297             // convert 3D box to ordinary group of paths; replace the old element in 'selected' with the new group
298             GSList *sel_it = g_slist_find(selected, item);
299             sel_it->data = box3d_convert_to_group(SP_BOX3D(item));
300             item = SP_ITEM(sel_it->data);
302             did = true;
303             selected = g_slist_remove (selected, item);
305             continue;
306         }
308         Inkscape::XML::Node *repr = sp_selected_item_to_curved_repr(item, 0);
309         if (!repr)
310             continue;
312         did = true;
313         selected = g_slist_remove (selected, item);
315         // remember the position of the item
316         gint pos = SP_OBJECT_REPR(item)->position();
317         // remember parent
318         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
319         // remember id
320         char const *id = SP_OBJECT_REPR(item)->attribute("id");
322         // It's going to resurrect, so we delete without notifying listeners.
323         SP_OBJECT(item)->deleteObject(false);
325         // restore id
326         repr->setAttribute("id", id);
327         // add the new repr to the parent
328         parent->appendChild(repr);
329         // move to the saved position
330         repr->setPosition(pos > 0 ? pos : 0);
332         /* Buglet: We don't re-add the (new version of the) object to the selection of any other
333          * desktops where it was previously selected. */
334         to_select = g_slist_prepend (to_select, repr);
335         Inkscape::GC::release(repr);
336     }
338     g_slist_free (items);
339     selection->setReprList(to_select);
340     selection->addList(selected);
341     g_slist_free (to_select);
342     g_slist_free (selected);
344     if (interactive) {
345         desktop->clearWaitingCursor();
346         if (did) {
347             sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_TO_CURVE, 
348                              _("Object to path"));
349         } else {
350             sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No objects</b> to convert to path in the selection."));
351             return;
352         }
353     }
356 Inkscape::XML::Node *
357 sp_selected_item_to_curved_repr(SPItem *item, guint32 /*text_grouping_policy*/)
359     if (!item)
360         return NULL;
362     SPCurve *curve = NULL;
363     if (SP_IS_SHAPE(item)) {
364         curve = sp_shape_get_curve(SP_SHAPE(item));
365     } else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
366         curve = te_get_layout(item)->convertToCurves();
367     }
369     if (!curve)
370         return NULL;
372     Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(item)->document();
373     Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
374     /* Transformation */
375     repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
376     /* Style */
377     gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
378                                                  SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
379     repr->setAttribute("style", style_str);
380     g_free(style_str);
382     /* Mask */
383     gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
384     if ( mask_str )
385         repr->setAttribute("mask", mask_str);
387     /* Clip path */
388     gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
389     if ( clip_path_str )
390         repr->setAttribute("clip-path", clip_path_str);
392     /* Rotation center */
393     sp_repr_set_attr(repr, "inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"));
394     sp_repr_set_attr(repr, "inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"));
396     /* Definition */
397     gchar *def_str = sp_svg_write_path(SP_CURVE_BPATH(curve));
398     repr->setAttribute("d", def_str);
399     g_free(def_str);
400     sp_curve_unref(curve);
401     return repr;
405 // FIXME: THIS DOES NOT REVERSE THE NODETYPES ORDER!
406 void
407 sp_selected_path_reverse()
409     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
411     Inkscape::Selection *selection = sp_desktop_selection(desktop);
412     GSList *items = (GSList *) selection->itemList();
414     if (!items) {
415         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to reverse."));
416         return;
417     }
420     // set "busy" cursor
421     desktop->setWaitingCursor();
423     bool did = false;
424     desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Reversing paths..."));
426     for (GSList *i = items; i != NULL; i = i->next) {
428         if (!SP_IS_PATH(i->data))
429             continue;
431         did = true;
432         SPPath *path = SP_PATH(i->data);
434         SPCurve *rcurve = sp_curve_reverse(sp_path_get_curve_reference(path));
436         gchar *str = sp_svg_write_path(SP_CURVE_BPATH(rcurve));
437         if ( sp_shape_has_path_effect(SP_SHAPE(path)) ) {
438             SP_OBJECT_REPR(path)->setAttribute("inkscape:original-d", str);
439         } else {
440             SP_OBJECT_REPR(path)->setAttribute("d", str);
441         }
442         g_free(str);
444         sp_curve_unref(rcurve);
445     }
447     desktop->clearWaitingCursor();
449     if (did) {
450         sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_REVERSE,
451                          _("Reverse path"));
452     } else {
453         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to reverse in the selection."));
454     }
457 /*
458   Local Variables:
459   mode:c++
460   c-file-style:"stroustrup"
461   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
462   indent-tabs-mode:nil
463   fill-column:99
464   End:
465 */
466 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :