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"
37 #include "path-chemistry.h"
39 /* Helper functions for sp_selected_path_to_curves */
40 static void sp_selected_path_to_curves0(gboolean do_document_done, guint32 text_grouping_policy);
41 enum {
42 /* Not used yet. This is the placeholder of Lauris's idea. */
43 SP_TOCURVE_INTERACTIVE = 1 << 0,
44 SP_TOCURVE_GROUPING_BY_WORD = 1 << 1,
45 SP_TOCURVE_GROUPING_BY_LINE = 1 << 2,
46 SP_TOCURVE_GROUPING_BY_WHOLE = 1 << 3
47 };
49 void
50 sp_selected_path_combine(void)
51 {
52 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
54 Inkscape::Selection *selection = sp_desktop_selection(desktop);
55 GSList *items = (GSList *) selection->itemList();
57 if (g_slist_length(items) < 2) {
58 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>at least two objects</b> to combine."));
59 return;
60 }
62 for (GSList *i = items; i != NULL; i = i->next) {
63 SPItem *item = (SPItem *) i->data;
64 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item)) {
65 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("At least one of the objects is <b>not a path</b>, cannot combine."));
66 return;
67 }
68 }
70 Inkscape::XML::Node *parent = SP_OBJECT_REPR((SPItem *) items->data)->parent();
71 for (GSList *i = items; i != NULL; i = i->next) {
72 if ( SP_OBJECT_REPR((SPItem *) i->data)->parent() != parent ) {
73 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("You cannot combine objects from <b>different groups</b> or <b>layers</b>."));
74 return;
75 }
76 }
78 sp_selected_path_to_curves0(FALSE, 0);
80 items = (GSList *) selection->itemList();
82 items = g_slist_copy(items);
84 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
86 // remember the position of the topmost object
87 gint topmost = (SP_OBJECT_REPR((SPItem *) g_slist_last(items)->data))->position();
89 // remember the id of the bottomost object
90 char const *id = SP_OBJECT_REPR((SPItem *) items->data)->attribute("id");
92 // FIXME: merge styles of combined objects instead of using the first one's style
93 gchar *style = g_strdup(SP_OBJECT_REPR((SPItem *) items->data)->attribute("style"));
95 GString *dstring = g_string_new("");
96 for (GSList *i = items; i != NULL; i = i->next) {
98 SPPath *path = (SPPath *) i->data;
99 SPCurve *c = sp_shape_get_curve(SP_SHAPE(path));
101 NArtBpath *abp = nr_artpath_affine(SP_CURVE_BPATH(c), SP_ITEM(path)->transform);
102 sp_curve_unref(c);
103 gchar *str = sp_svg_write_path(abp);
104 g_free(abp);
106 dstring = g_string_append(dstring, str);
107 g_free(str);
109 // if this is the bottommost object,
110 if (!strcmp(SP_OBJECT_REPR(path)->attribute("id"), id)) {
111 // delete it so that its clones don't get alerted; this object will be restored shortly, with the same id
112 SP_OBJECT(path)->deleteObject(false);
113 } else {
114 // delete the object for real, so that its clones can take appropriate action
115 SP_OBJECT(path)->deleteObject();
116 }
118 topmost--;
119 }
121 g_slist_free(items);
123 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
124 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
126 // restore id
127 repr->setAttribute("id", id);
129 repr->setAttribute("style", style);
130 g_free(style);
132 repr->setAttribute("d", dstring->str);
133 g_string_free(dstring, TRUE);
135 // add the new group to the group members' common parent
136 parent->appendChild(repr);
138 // move to the position of the topmost, reduced by the number of deleted items
139 repr->setPosition(topmost > 0 ? topmost + 1 : 0);
141 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_COMBINE,
142 _("Combine"));
144 selection->set(repr);
146 Inkscape::GC::release(repr);
147 }
149 void
150 sp_selected_path_break_apart(void)
151 {
152 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
154 Inkscape::Selection *selection = sp_desktop_selection(desktop);
156 if (selection->isEmpty()) {
157 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to break apart."));
158 return;
159 }
161 bool did = false;
163 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
164 items != NULL;
165 items = items->next) {
167 SPItem *item = (SPItem *) items->data;
169 if (!SP_IS_PATH(item))
170 continue;
172 SPPath *path = SP_PATH(item);
174 SPCurve *curve = sp_shape_get_curve(SP_SHAPE(path));
175 if (curve == NULL)
176 continue;
178 did = true;
180 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
181 gint pos = SP_OBJECT_REPR(item)->position();
182 char const *id = SP_OBJECT_REPR(item)->attribute("id");
184 gchar *style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
186 NArtBpath *abp = nr_artpath_affine(SP_CURVE_BPATH(curve), (SP_ITEM(path))->transform);
188 sp_curve_unref(curve);
190 // it's going to resurrect as one of the pieces, so we delete without advertisement
191 SP_OBJECT(item)->deleteObject(false);
193 curve = sp_curve_new_from_bpath(abp);
194 g_assert(curve != NULL);
196 GSList *list = sp_curve_split(curve);
198 sp_curve_unref(curve);
200 GSList *reprs = NULL;
201 for (GSList *l = list; l != NULL; l = l->next) {
202 curve = (SPCurve *) l->data;
204 Inkscape::XML::Node *repr = parent->document()->createElement("svg:path");
205 repr->setAttribute("style", style);
207 gchar *str = sp_svg_write_path(SP_CURVE_BPATH(curve));
208 repr->setAttribute("d", str);
209 g_free(str);
211 // add the new repr to the parent
212 parent->appendChild(repr);
214 // move to the saved position
215 repr->setPosition(pos > 0 ? pos : 0);
217 // if it's the first one, restore id
218 if (l == list)
219 repr->setAttribute("id", id);
221 reprs = g_slist_prepend (reprs, repr);
223 Inkscape::GC::release(repr);
224 }
226 selection->setReprList(reprs);
228 g_slist_free(reprs);
229 g_slist_free(list);
230 g_free(style);
232 }
234 if (did) {
235 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_BREAK_APART,
236 _("Break apart"));
237 } else {
238 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to break apart in the selection."));
239 return;
240 }
241 }
243 /* This function is an entry point from GUI */
244 void
245 sp_selected_path_to_curves(void)
246 {
247 sp_selected_path_to_curves0(TRUE, SP_TOCURVE_INTERACTIVE);
248 }
250 static void
251 sp_selected_path_to_curves0(gboolean interactive, guint32 text_grouping_policy)
252 {
253 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
255 Inkscape::Selection *selection = sp_desktop_selection(desktop);
257 if (selection->isEmpty()) {
258 if (interactive)
259 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to path."));
260 return;
261 }
263 bool did = false;
265 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
266 items != NULL;
267 items = items->next) {
269 SPItem *item = SP_ITEM(items->data);
271 Inkscape::XML::Node *repr = sp_selected_item_to_curved_repr(item, 0);
272 if (!repr)
273 continue;
275 did = true;
277 // remember the position of the item
278 gint pos = SP_OBJECT_REPR(item)->position();
279 // remember parent
280 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
281 // remember id
282 char const *id = SP_OBJECT_REPR(item)->attribute("id");
284 selection->remove(item);
286 // It's going to resurrect, so we delete without notifying listeners.
287 SP_OBJECT(item)->deleteObject(false);
289 // restore id
290 repr->setAttribute("id", id);
291 // add the new repr to the parent
292 parent->appendChild(repr);
293 // move to the saved position
294 repr->setPosition(pos > 0 ? pos : 0);
296 /* Buglet: We don't re-add the (new version of the) object to the selection of any other
297 * desktops where it was previously selected. */
298 selection->add(repr);
299 Inkscape::GC::release(repr);
300 }
302 if (interactive) {
303 if (did) {
304 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_TO_CURVE,
305 _("Object to path"));
306 } else {
307 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No objects</b> to convert to path in the selection."));
308 return;
309 }
310 }
311 }
313 Inkscape::XML::Node *
314 sp_selected_item_to_curved_repr(SPItem *item, guint32 text_grouping_policy)
315 {
316 if (!item)
317 return NULL;
319 SPCurve *curve = NULL;
320 if (SP_IS_SHAPE(item)) {
321 curve = sp_shape_get_curve(SP_SHAPE(item));
322 } else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
323 curve = te_get_layout(item)->convertToCurves();
324 }
326 if (!curve)
327 return NULL;
329 Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(item)->document();
330 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
331 /* Transformation */
332 repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
333 /* Style */
334 gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
335 SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
336 repr->setAttribute("style", style_str);
337 g_free(style_str);
339 /* Mask */
340 gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
341 if ( mask_str )
342 repr->setAttribute("mask", mask_str);
344 /* Clip path */
345 gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
346 if ( clip_path_str )
347 repr->setAttribute("clip-path", clip_path_str);
349 /* Rotation center */
350 sp_repr_set_attr(repr, "inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"));
351 sp_repr_set_attr(repr, "inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"));
353 /* Definition */
354 gchar *def_str = sp_svg_write_path(SP_CURVE_BPATH(curve));
355 repr->setAttribute("d", def_str);
356 g_free(def_str);
357 sp_curve_unref(curve);
358 return repr;
359 }
361 void
362 sp_selected_path_reverse()
363 {
364 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
366 Inkscape::Selection *selection = sp_desktop_selection(desktop);
367 GSList *items = (GSList *) selection->itemList();
369 if (!items) {
370 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to reverse."));
371 return;
372 }
375 bool did = false;
376 for (GSList *i = items; i != NULL; i = i->next) {
378 if (!SP_IS_SHAPE(i->data))
379 continue;
381 did = true;
382 SPShape *shape = SP_SHAPE(i->data);
384 SPCurve *rcurve = sp_curve_reverse(shape->curve);
386 gchar *str = sp_svg_write_path(SP_CURVE_BPATH(rcurve));
387 SP_OBJECT_REPR(shape)->setAttribute("d", str);
388 g_free(str);
390 sp_curve_unref(rcurve);
391 }
393 if (did) {
394 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_REVERSE,
395 _("Reverse path"));
396 } else {
397 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to reverse in the selection."));
398 }
399 }
401 /*
402 Local Variables:
403 mode:c++
404 c-file-style:"stroustrup"
405 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
406 indent-tabs-mode:nil
407 fill-column:99
408 End:
409 */
410 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :