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 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Combining paths..."));
79 // set "busy" cursor
80 desktop->setWaitingCursor();
82 sp_selected_path_to_curves0(FALSE, 0);
84 items = g_slist_copy((GSList *) selection->itemList());
85 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
86 selection->clear();
88 // remember the position of the topmost object
89 gint topmost = (SP_OBJECT_REPR((SPItem *) g_slist_last(items)->data))->position();
91 // remember the id of the bottomost object
92 char const *id = SP_OBJECT_REPR((SPItem *) items->data)->attribute("id");
94 // FIXME: merge styles of combined objects instead of using the first one's style
95 gchar *style = g_strdup(SP_OBJECT_REPR((SPItem *) items->data)->attribute("style"));
97 GString *dstring = g_string_new("");
98 for (GSList *i = items; i != NULL; i = i->next) {
100 SPPath *path = (SPPath *) i->data;
101 SPCurve *c = sp_shape_get_curve(SP_SHAPE(path));
103 NArtBpath *abp = nr_artpath_affine(SP_CURVE_BPATH(c), SP_ITEM(path)->transform);
104 sp_curve_unref(c);
105 gchar *str = sp_svg_write_path(abp);
106 g_free(abp);
108 dstring = g_string_append(dstring, str);
109 g_free(str);
111 // if this is the bottommost object,
112 if (!strcmp(SP_OBJECT_REPR(path)->attribute("id"), id)) {
113 // delete it so that its clones don't get alerted; this object will be restored shortly, with the same id
114 SP_OBJECT(path)->deleteObject(false);
115 } else {
116 // delete the object for real, so that its clones can take appropriate action
117 SP_OBJECT(path)->deleteObject();
118 }
120 topmost--;
121 }
123 g_slist_free(items);
125 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
126 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
128 // restore id
129 repr->setAttribute("id", id);
131 repr->setAttribute("style", style);
132 g_free(style);
134 repr->setAttribute("d", dstring->str);
135 g_string_free(dstring, TRUE);
137 // add the new group to the group members' common parent
138 parent->appendChild(repr);
140 // move to the position of the topmost, reduced by the number of deleted items
141 repr->setPosition(topmost > 0 ? topmost + 1 : 0);
143 desktop->clearWaitingCursor();
145 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_COMBINE,
146 _("Combine"));
148 selection->set(repr);
150 Inkscape::GC::release(repr);
151 }
153 void
154 sp_selected_path_break_apart(void)
155 {
156 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
158 Inkscape::Selection *selection = sp_desktop_selection(desktop);
160 if (selection->isEmpty()) {
161 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to break apart."));
162 return;
163 }
165 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Breaking apart paths..."));
166 // set "busy" cursor
167 desktop->setWaitingCursor();
169 bool did = false;
171 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
172 items != NULL;
173 items = items->next) {
175 SPItem *item = (SPItem *) items->data;
177 if (!SP_IS_PATH(item))
178 continue;
180 SPPath *path = SP_PATH(item);
182 SPCurve *curve = sp_shape_get_curve(SP_SHAPE(path));
183 if (curve == NULL)
184 continue;
186 did = true;
188 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
189 gint pos = SP_OBJECT_REPR(item)->position();
190 char const *id = SP_OBJECT_REPR(item)->attribute("id");
192 gchar *style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
194 NArtBpath *abp = nr_artpath_affine(SP_CURVE_BPATH(curve), (SP_ITEM(path))->transform);
196 sp_curve_unref(curve);
198 // it's going to resurrect as one of the pieces, so we delete without advertisement
199 SP_OBJECT(item)->deleteObject(false);
201 curve = sp_curve_new_from_bpath(abp);
202 g_assert(curve != NULL);
204 GSList *list = sp_curve_split(curve);
206 sp_curve_unref(curve);
208 GSList *reprs = NULL;
209 for (GSList *l = list; l != NULL; l = l->next) {
210 curve = (SPCurve *) l->data;
212 Inkscape::XML::Node *repr = parent->document()->createElement("svg:path");
213 repr->setAttribute("style", style);
215 gchar *str = sp_svg_write_path(SP_CURVE_BPATH(curve));
216 repr->setAttribute("d", str);
217 g_free(str);
219 // add the new repr to the parent
220 parent->appendChild(repr);
222 // move to the saved position
223 repr->setPosition(pos > 0 ? pos : 0);
225 // if it's the first one, restore id
226 if (l == list)
227 repr->setAttribute("id", id);
229 reprs = g_slist_prepend (reprs, repr);
231 Inkscape::GC::release(repr);
232 }
234 selection->setReprList(reprs);
236 g_slist_free(reprs);
237 g_slist_free(list);
238 g_free(style);
240 }
242 desktop->clearWaitingCursor();
244 if (did) {
245 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_BREAK_APART,
246 _("Break apart"));
247 } else {
248 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to break apart in the selection."));
249 return;
250 }
251 }
253 /* This function is an entry point from GUI */
254 void
255 sp_selected_path_to_curves(void)
256 {
257 sp_selected_path_to_curves0(TRUE, SP_TOCURVE_INTERACTIVE);
258 }
260 static void
261 sp_selected_path_to_curves0(gboolean interactive, guint32 /*text_grouping_policy*/)
262 {
263 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
265 Inkscape::Selection *selection = sp_desktop_selection(desktop);
267 if (selection->isEmpty()) {
268 if (interactive)
269 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to path."));
270 return;
271 }
273 bool did = false;
274 if (interactive) {
275 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Converting objects to paths..."));
276 // set "busy" cursor
277 desktop->setWaitingCursor();
278 }
280 GSList *selected = g_slist_copy((GSList *) selection->itemList());
281 GSList *to_select = NULL;
282 selection->clear();
283 GSList *items = g_slist_copy(selected);
285 for (;
286 items != NULL;
287 items = items->next) {
289 SPItem *item = SP_ITEM(items->data);
291 if (SP_IS_PATH(item) && !SP_PATH(item)->original_curve) {
292 continue; // already a path, and no path effect
293 }
295 Inkscape::XML::Node *repr = sp_selected_item_to_curved_repr(item, 0);
296 if (!repr)
297 continue;
299 did = true;
300 selected = g_slist_remove (selected, item);
302 // remember the position of the item
303 gint pos = SP_OBJECT_REPR(item)->position();
304 // remember parent
305 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
306 // remember id
307 char const *id = SP_OBJECT_REPR(item)->attribute("id");
309 // It's going to resurrect, so we delete without notifying listeners.
310 SP_OBJECT(item)->deleteObject(false);
312 // restore id
313 repr->setAttribute("id", id);
314 // add the new repr to the parent
315 parent->appendChild(repr);
316 // move to the saved position
317 repr->setPosition(pos > 0 ? pos : 0);
319 /* Buglet: We don't re-add the (new version of the) object to the selection of any other
320 * desktops where it was previously selected. */
321 to_select = g_slist_prepend (to_select, repr);
322 Inkscape::GC::release(repr);
323 }
325 g_slist_free (items);
326 selection->setReprList(to_select);
327 selection->addList(selected);
328 g_slist_free (to_select);
329 g_slist_free (selected);
331 if (interactive) {
332 desktop->clearWaitingCursor();
333 if (did) {
334 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_TO_CURVE,
335 _("Object to path"));
336 } else {
337 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No objects</b> to convert to path in the selection."));
338 return;
339 }
340 }
341 }
343 Inkscape::XML::Node *
344 sp_selected_item_to_curved_repr(SPItem *item, guint32 /*text_grouping_policy*/)
345 {
346 if (!item)
347 return NULL;
349 SPCurve *curve = NULL;
350 if (SP_IS_SHAPE(item)) {
351 curve = sp_shape_get_curve(SP_SHAPE(item));
352 } else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
353 curve = te_get_layout(item)->convertToCurves();
354 }
356 if (!curve)
357 return NULL;
359 Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(item)->document();
360 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
361 /* Transformation */
362 repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
363 /* Style */
364 gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
365 SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
366 repr->setAttribute("style", style_str);
367 g_free(style_str);
369 /* Mask */
370 gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
371 if ( mask_str )
372 repr->setAttribute("mask", mask_str);
374 /* Clip path */
375 gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
376 if ( clip_path_str )
377 repr->setAttribute("clip-path", clip_path_str);
379 /* Rotation center */
380 sp_repr_set_attr(repr, "inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"));
381 sp_repr_set_attr(repr, "inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"));
383 /* Definition */
384 gchar *def_str = sp_svg_write_path(SP_CURVE_BPATH(curve));
385 repr->setAttribute("d", def_str);
386 g_free(def_str);
387 sp_curve_unref(curve);
388 return repr;
389 }
391 void
392 sp_selected_path_reverse()
393 {
394 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
396 Inkscape::Selection *selection = sp_desktop_selection(desktop);
397 GSList *items = (GSList *) selection->itemList();
399 if (!items) {
400 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to reverse."));
401 return;
402 }
405 // set "busy" cursor
406 desktop->setWaitingCursor();
408 bool did = false;
409 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Reversing paths..."));
411 for (GSList *i = items; i != NULL; i = i->next) {
413 if (!SP_IS_SHAPE(i->data))
414 continue;
416 did = true;
417 SPShape *shape = SP_SHAPE(i->data);
419 SPCurve *rcurve = sp_curve_reverse(shape->curve);
421 gchar *str = sp_svg_write_path(SP_CURVE_BPATH(rcurve));
422 SP_OBJECT_REPR(shape)->setAttribute("d", str);
423 g_free(str);
425 sp_curve_unref(rcurve);
426 }
428 desktop->clearWaitingCursor();
430 if (did) {
431 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_REVERSE,
432 _("Reverse path"));
433 } else {
434 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to reverse in the selection."));
435 }
436 }
438 /*
439 Local Variables:
440 mode:c++
441 c-file-style:"stroustrup"
442 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
443 indent-tabs-mode:nil
444 fill-column:99
445 End:
446 */
447 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :