Code

restored pedro/work and added it to make.exclude
[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 "document.h"
32 #include "message-stack.h"
33 #include "selection.h"
34 #include "desktop-handles.h"
36 /* Helper functions for sp_selected_path_to_curves */
37 static void sp_selected_path_to_curves0(gboolean do_document_done, guint32 text_grouping_policy);
38 static Inkscape::XML::Node *sp_selected_item_to_curved_repr(SPItem *item, guint32 text_grouping_policy);
39 enum {
40     /* Not used yet. This is the placeholder of Lauris's idea. */
41     SP_TOCURVE_INTERACTIVE       = 1 << 0,
42     SP_TOCURVE_GROUPING_BY_WORD  = 1 << 1,
43     SP_TOCURVE_GROUPING_BY_LINE  = 1 << 2,
44     SP_TOCURVE_GROUPING_BY_WHOLE = 1 << 3
45 };
47 void
48 sp_selected_path_combine(void)
49 {
50     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
52     Inkscape::Selection *selection = sp_desktop_selection(desktop);
53     GSList *items = (GSList *) selection->itemList();
55     if (g_slist_length(items) < 2) {
56         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>at least two objects</b> to combine."));
57         return;
58     }
60     for (GSList *i = items; i != NULL; i = i->next) {
61         SPItem *item = (SPItem *) i->data;
62         if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item)) {
63             sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("At least one of the objects is <b>not a path</b>, cannot combine."));
64             return;
65         }
66     }
68     Inkscape::XML::Node *parent = SP_OBJECT_REPR((SPItem *) items->data)->parent();
69     for (GSList *i = items; i != NULL; i = i->next) {
70         if ( SP_OBJECT_REPR((SPItem *) i->data)->parent() != parent ) {
71             sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("You cannot combine objects from <b>different groups</b> or <b>layers</b>."));
72             return;
73         }
74     }
76     sp_selected_path_to_curves0(FALSE, 0);
78     items = (GSList *) selection->itemList();
80     items = g_slist_copy(items);
82     items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
84     // remember the position of the topmost object
85     gint topmost = (SP_OBJECT_REPR((SPItem *) g_slist_last(items)->data))->position();
87     // remember the id of the bottomost object
88     char const *id = SP_OBJECT_REPR((SPItem *) items->data)->attribute("id");
90     // FIXME: merge styles of combined objects instead of using the first one's style
91     gchar *style = g_strdup(SP_OBJECT_REPR((SPItem *) items->data)->attribute("style"));
93     GString *dstring = g_string_new("");
94     for (GSList *i = items; i != NULL; i = i->next) {
96         SPPath *path = (SPPath *) i->data;
97         SPCurve *c = sp_shape_get_curve(SP_SHAPE(path));
99         NArtBpath *abp = nr_artpath_affine(SP_CURVE_BPATH(c), SP_ITEM(path)->transform);
100         sp_curve_unref(c);
101         gchar *str = sp_svg_write_path(abp);
102         g_free(abp);
104         dstring = g_string_append(dstring, str);
105         g_free(str);
107         // if this is the bottommost object,
108         if (!strcmp(SP_OBJECT_REPR(path)->attribute("id"), id)) {
109             // delete it so that its clones don't get alerted; this object will be restored shortly, with the same id
110             SP_OBJECT(path)->deleteObject(false);
111         } else {
112             // delete the object for real, so that its clones can take appropriate action
113             SP_OBJECT(path)->deleteObject();
114         }
116         topmost--;
117     }
119     g_slist_free(items);
121     Inkscape::XML::Node *repr = sp_repr_new("svg:path");
123     // restore id
124     repr->setAttribute("id", id);
126     repr->setAttribute("style", style);
127     g_free(style);
129     repr->setAttribute("d", dstring->str);
130     g_string_free(dstring, TRUE);
132     // add the new group to the group members' common parent
133     parent->appendChild(repr);
135     // move to the position of the topmost, reduced by the number of deleted items
136     repr->setPosition(topmost > 0 ? topmost + 1 : 0);
138     sp_document_done(sp_desktop_document(desktop));
140     selection->set(repr);
142     Inkscape::GC::release(repr);
145 void
146 sp_selected_path_break_apart(void)
148     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
150     Inkscape::Selection *selection = sp_desktop_selection(desktop);
152     if (selection->isEmpty()) {
153         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to break apart."));
154         return;
155     }
157     bool did = false;
159     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
160          items != NULL;
161          items = items->next) {
163         SPItem *item = (SPItem *) items->data;
165         if (!SP_IS_PATH(item))
166             continue;
168         SPPath *path = SP_PATH(item);
170         SPCurve *curve = sp_shape_get_curve(SP_SHAPE(path));
171         if (curve == NULL)
172             continue;
174         did = true;
176         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
177         gint pos = SP_OBJECT_REPR(item)->position();
178         char const *id = SP_OBJECT_REPR(item)->attribute("id");
180         gchar *style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
182         NArtBpath *abp = nr_artpath_affine(SP_CURVE_BPATH(curve), (SP_ITEM(path))->transform);
184         sp_curve_unref(curve);
186         // it's going to resurrect as one of the pieces, so we delete without advertisement
187         SP_OBJECT(item)->deleteObject(false);
189         curve = sp_curve_new_from_bpath(abp);
190         g_assert(curve != NULL);
192         GSList *list = sp_curve_split(curve);
194         sp_curve_unref(curve);
196         GSList *reprs = NULL;
197         for (GSList *l = list; l != NULL; l = l->next) {
198             curve = (SPCurve *) l->data;
200             Inkscape::XML::Node *repr = sp_repr_new("svg:path");
201             repr->setAttribute("style", style);
203             gchar *str = sp_svg_write_path(SP_CURVE_BPATH(curve));
204             repr->setAttribute("d", str);
205             g_free(str);
207             // add the new repr to the parent
208             parent->appendChild(repr);
210             // move to the saved position
211             repr->setPosition(pos > 0 ? pos : 0);
213             // if it's the first one, restore id
214             if (l == list)
215                 repr->setAttribute("id", id);
217             reprs = g_slist_prepend (reprs, repr);
219             Inkscape::GC::release(repr);
220         }
222         selection->setReprList(reprs);
224         g_slist_free(reprs);
225         g_slist_free(list);
226         g_free(style);
228     }
230     if (did) {
231         sp_document_done(sp_desktop_document(desktop));
232     } else {
233         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to break apart in the selection."));
234         return;
235     }
238 /* This function is an entry point from GUI */
239 void
240 sp_selected_path_to_curves(void)
242     sp_selected_path_to_curves0(TRUE, SP_TOCURVE_INTERACTIVE);
245 static void
246 sp_selected_path_to_curves0(gboolean interactive, guint32 text_grouping_policy)
248     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
250     Inkscape::Selection *selection = sp_desktop_selection(desktop);
252     if (selection->isEmpty()) {
253         if (interactive)
254             sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to path."));
255         return;
256     }
258     bool did = false;
260     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
261          items != NULL;
262          items = items->next) {
264         SPItem *item = SP_ITEM(items->data);
266         Inkscape::XML::Node *repr = sp_selected_item_to_curved_repr(item, 0);
267         if (!repr)
268             continue;
270         did = true;
272         // remember the position of the item
273         gint pos = SP_OBJECT_REPR(item)->position();
274         // remember parent
275         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
276         // remember id
277         char const *id = SP_OBJECT_REPR(item)->attribute("id");
279         selection->remove(item);
281         // it's going to resurrect, so we delete without advertisement
282         SP_OBJECT(item)->deleteObject(false);
284         // restore id
285         repr->setAttribute("id", id);
286         // add the new repr to the parent
287         parent->appendChild(repr);
288         // move to the saved position
289         repr->setPosition(pos > 0 ? pos : 0);
291         selection->add(repr);
292         Inkscape::GC::release(repr);
293     }
295     if (interactive) {
296         if (did) {
297             sp_document_done(sp_desktop_document(desktop));
298         } else {
299             sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No objects</b> to convert to path in the selection."));
300             return;
301         }
302     }
305 static Inkscape::XML::Node *
306 sp_selected_item_to_curved_repr(SPItem *item, guint32 text_grouping_policy)
308     if (!item)
309         return NULL;
311     SPCurve *curve = NULL;
312     if (SP_IS_SHAPE(item)) {
313         curve = sp_shape_get_curve(SP_SHAPE(item));
314     } else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
315         curve = te_get_layout(item)->convertToCurves();
316     }
318     if (!curve)
319         return NULL;
321     Inkscape::XML::Node *repr = sp_repr_new("svg:path");
322     /* Transformation */
323     repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
324     /* Style */
325     gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
326                                                  SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
327     repr->setAttribute("style", style_str);
328     g_free(style_str);
329     /* Rotation center */
330     sp_repr_set_attr(repr, "inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"));
331     sp_repr_set_attr(repr, "inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"));
333     /* Definition */
334     gchar *def_str = sp_svg_write_path(SP_CURVE_BPATH(curve));
335     repr->setAttribute("d", def_str);
336     g_free(def_str);
337     sp_curve_unref(curve);
338     return repr;
341 void
342 sp_selected_path_reverse()
344     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
346     Inkscape::Selection *selection = sp_desktop_selection(desktop);
347     GSList *items = (GSList *) selection->itemList();
349     if (!items) {
350         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to reverse."));
351         return;
352     }
355     bool did = false;
356     for (GSList *i = items; i != NULL; i = i->next) {
358         if (!SP_IS_SHAPE(i->data))
359             continue;
361         did = true;
362         SPShape *shape = SP_SHAPE(i->data);
364         SPCurve *rcurve = sp_curve_reverse(shape->curve);
366         gchar *str = sp_svg_write_path(SP_CURVE_BPATH(rcurve));
367         SP_OBJECT_REPR(shape)->setAttribute("d", str);
368         g_free(str);
370         sp_curve_unref(rcurve);
371     }
373     if (did) {
374         sp_document_done(sp_desktop_document(desktop));
375     } else {
376         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to reverse in the selection."));
377     }
380 /*
381   Local Variables:
382   mode:c++
383   c-file-style:"stroustrup"
384   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
385   indent-tabs-mode:nil
386   fill-column:99
387   End:
388 */
389 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :