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 * Jasper van de Gronde <th.v.d.gronde@hccnet.nl>
10 *
11 * Copyright (C) 1999-2008 Authors
12 * Copyright (C) 2001-2002 Ximian, Inc.
13 *
14 * Released under GNU GPL, read the file 'COPYING' for more information
15 */
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
20 #include <cstring>
21 #include <string>
22 #include "xml/repr.h"
23 #include "svg/svg.h"
24 #include "display/curve.h"
25 #include <glib/gmem.h>
26 #include <glibmm/i18n.h>
27 #include "sp-path.h"
28 #include "sp-text.h"
29 #include "sp-flowtext.h"
30 #include "libnr/nr-path.h"
31 #include "text-editing.h"
32 #include "style.h"
33 #include "inkscape.h"
34 #include "desktop.h"
35 #include "document.h"
36 #include "message-stack.h"
37 #include "selection.h"
38 #include "desktop-handles.h"
39 #include "box3d.h"
41 #include "path-chemistry.h"
43 /* Helper functions for sp_selected_path_to_curves */
44 static void sp_selected_path_to_curves0(gboolean do_document_done, guint32 text_grouping_policy);
45 static bool sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_select);
47 enum {
48 /* Not used yet. This is the placeholder of Lauris's idea. */
49 SP_TOCURVE_INTERACTIVE = 1 << 0,
50 SP_TOCURVE_GROUPING_BY_WORD = 1 << 1,
51 SP_TOCURVE_GROUPING_BY_LINE = 1 << 2,
52 SP_TOCURVE_GROUPING_BY_WHOLE = 1 << 3
53 };
55 void
56 sp_selected_path_combine(void)
57 {
58 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
59 Inkscape::Selection *selection = sp_desktop_selection(desktop);
61 if (g_slist_length((GSList *) selection->itemList()) < 2) {
62 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>at least two objects</b> to combine."));
63 return;
64 }
66 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Combining paths..."));
67 // set "busy" cursor
68 desktop->setWaitingCursor();
70 sp_selected_path_to_curves0(FALSE, 0);
72 GSList *items = g_slist_copy((GSList *) selection->itemList());
73 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
74 items = g_slist_reverse(items);
76 // remember the position, id and style of the topmost path, they will be assigned to the combined one
77 gint position = 0;
78 char const *id = NULL;
79 gchar *style = NULL;
81 SPCurve* curve = 0;
82 bool did = false;
83 SPItem *first = NULL;
84 Inkscape::XML::Node *parent = NULL;
86 for (GSList *i = items; i != NULL; i = i->next) { // going from top to bottom
88 SPItem *item = (SPItem *) i->data;
89 if (!SP_IS_PATH(item))
90 continue;
91 did = true;
93 SPCurve *c = sp_shape_get_curve(SP_SHAPE(item));
94 if (first == NULL) { // this is the topmost path
95 first = item;
96 parent = SP_OBJECT_REPR(first)->parent();
97 position = SP_OBJECT_REPR(first)->position();
98 id = SP_OBJECT_REPR(first)->attribute("id");
99 // FIXME: merge styles of combined objects instead of using the first one's style
100 style = g_strdup(SP_OBJECT_REPR(first)->attribute("style"));
101 sp_curve_transform(c, item->transform);
102 curve = c;
103 } else {
104 sp_curve_transform(c, item->getRelativeTransform(SP_OBJECT(first)));
105 sp_curve_append(curve, c, false);
106 sp_curve_unref(c);
107 }
109 // unless this is the topmost object,
110 if (item != first) {
111 // reduce position only if the same parent
112 if (SP_OBJECT_REPR(item)->parent() == parent)
113 position--;
114 // delete the object for real, so that its clones can take appropriate action
115 SP_OBJECT(item)->deleteObject();
116 }
117 }
119 g_slist_free(items);
121 if (did) {
122 selection->clear();
124 // delete the topmost one so that its clones don't get alerted; this object will be
125 // restored shortly, with the same id
126 SP_OBJECT(first)->deleteObject(false);
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 and style
132 repr->setAttribute("id", id);
134 repr->setAttribute("style", style);
135 g_free(style);
137 // set path data corresponding to new curve
138 gchar *dstring = sp_svg_write_path(SP_CURVE_BPATH(curve));
139 sp_curve_unref(curve);
140 repr->setAttribute("d", dstring);
141 g_free(dstring);
143 // add the new group to the parent of the topmost
144 parent->appendChild(repr);
146 // move to the position of the topmost, reduced by the number of deleted items
147 repr->setPosition(position > 0 ? position : 0);
149 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_COMBINE,
150 _("Combine"));
152 selection->set(repr);
154 Inkscape::GC::release(repr);
156 } else {
157 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to combine in the selection."));
158 }
160 desktop->clearWaitingCursor();
161 }
163 void
164 sp_selected_path_break_apart(void)
165 {
166 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
168 Inkscape::Selection *selection = sp_desktop_selection(desktop);
170 if (selection->isEmpty()) {
171 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to break apart."));
172 return;
173 }
175 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Breaking apart paths..."));
176 // set "busy" cursor
177 desktop->setWaitingCursor();
179 bool did = false;
181 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
182 items != NULL;
183 items = items->next) {
185 SPItem *item = (SPItem *) items->data;
187 if (!SP_IS_PATH(item))
188 continue;
190 SPPath *path = SP_PATH(item);
192 SPCurve *curve = sp_shape_get_curve(SP_SHAPE(path));
193 if (curve == NULL)
194 continue;
196 did = true;
198 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
199 gint pos = SP_OBJECT_REPR(item)->position();
200 char const *id = SP_OBJECT_REPR(item)->attribute("id");
202 gchar *style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
204 NArtBpath *abp = nr_artpath_affine(SP_CURVE_BPATH(curve), (SP_ITEM(path))->transform);
206 sp_curve_unref(curve);
208 // it's going to resurrect as one of the pieces, so we delete without advertisement
209 SP_OBJECT(item)->deleteObject(false);
211 curve = sp_curve_new_from_bpath(abp);
212 g_assert(curve != NULL);
214 GSList *list = sp_curve_split(curve);
216 sp_curve_unref(curve);
218 GSList *reprs = NULL;
219 for (GSList *l = list; l != NULL; l = l->next) {
220 curve = (SPCurve *) l->data;
222 Inkscape::XML::Node *repr = parent->document()->createElement("svg:path");
223 repr->setAttribute("style", style);
225 gchar *str = sp_svg_write_path(SP_CURVE_BPATH(curve));
226 repr->setAttribute("d", str);
227 g_free(str);
229 // add the new repr to the parent
230 parent->appendChild(repr);
232 // move to the saved position
233 repr->setPosition(pos > 0 ? pos : 0);
235 // if it's the first one, restore id
236 if (l == list)
237 repr->setAttribute("id", id);
239 reprs = g_slist_prepend (reprs, repr);
241 Inkscape::GC::release(repr);
242 }
244 selection->setReprList(reprs);
246 g_slist_free(reprs);
247 g_slist_free(list);
248 g_free(style);
250 }
252 desktop->clearWaitingCursor();
254 if (did) {
255 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_BREAK_APART,
256 _("Break apart"));
257 } else {
258 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to break apart in the selection."));
259 }
260 }
262 /* This function is an entry point from GUI */
263 void
264 sp_selected_path_to_curves(void)
265 {
266 sp_selected_path_to_curves0(TRUE, SP_TOCURVE_INTERACTIVE);
267 }
269 static void
270 sp_selected_path_to_curves0(gboolean interactive, guint32 /*text_grouping_policy*/)
271 {
272 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
274 Inkscape::Selection *selection = sp_desktop_selection(desktop);
276 if (selection->isEmpty()) {
277 if (interactive)
278 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to path."));
279 return;
280 }
282 bool did = false;
283 if (interactive) {
284 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Converting objects to paths..."));
285 // set "busy" cursor
286 desktop->setWaitingCursor();
287 }
289 GSList *selected = g_slist_copy((GSList *) selection->itemList());
290 GSList *to_select = NULL;
291 selection->clear();
292 GSList *items = g_slist_copy(selected);
294 did = sp_item_list_to_curves(items, &selected, &to_select);
296 g_slist_free (items);
297 selection->setReprList(to_select);
298 selection->addList(selected);
299 g_slist_free (to_select);
300 g_slist_free (selected);
302 if (interactive) {
303 desktop->clearWaitingCursor();
304 if (did) {
305 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_TO_CURVE,
306 _("Object to path"));
307 } else {
308 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No objects</b> to convert to path in the selection."));
309 return;
310 }
311 }
312 }
314 static bool
315 sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_select)
316 {
317 bool did = false;
319 for (;
320 items != NULL;
321 items = items->next) {
323 SPItem *item = SP_ITEM(items->data);
325 if (SP_IS_PATH(item) && !SP_PATH(item)->original_curve) {
326 continue; // already a path, and no path effect
327 }
329 if (SP_IS_BOX3D(item)) {
330 // convert 3D box to ordinary group of paths; replace the old element in 'selected' with the new group
331 Inkscape::XML::Node *repr = box3d_convert_to_group(SP_BOX3D(item));
333 if (repr) {
334 *to_select = g_slist_prepend (*to_select, repr);
335 did = true;
336 *selected = g_slist_remove (*selected, item);
337 }
339 continue;
340 }
342 if (SP_IS_GROUP(item)) {
343 sp_lpe_item_remove_path_effect(SP_LPE_ITEM(item), true);
344 GSList *item_list = sp_item_group_item_list(SP_GROUP(item));
346 GSList *item_to_select = NULL;
347 GSList *item_selected = NULL;
349 if (sp_item_list_to_curves(item_list, &item_selected, &item_to_select))
350 did = true;
352 g_slist_free(item_list);
353 g_slist_free(item_to_select);
354 g_slist_free(item_selected);
356 continue;
357 }
359 Inkscape::XML::Node *repr = sp_selected_item_to_curved_repr(item, 0);
360 if (!repr)
361 continue;
363 did = true;
364 *selected = g_slist_remove (*selected, item);
366 // remember the position of the item
367 gint pos = SP_OBJECT_REPR(item)->position();
368 // remember parent
369 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
370 // remember id
371 char const *id = SP_OBJECT_REPR(item)->attribute("id");
373 // It's going to resurrect, so we delete without notifying listeners.
374 SP_OBJECT(item)->deleteObject(false);
376 // restore id
377 repr->setAttribute("id", id);
378 // add the new repr to the parent
379 parent->appendChild(repr);
380 // move to the saved position
381 repr->setPosition(pos > 0 ? pos : 0);
383 /* Buglet: We don't re-add the (new version of the) object to the selection of any other
384 * desktops where it was previously selected. */
385 *to_select = g_slist_prepend (*to_select, repr);
386 Inkscape::GC::release(repr);
387 }
389 return did;
390 }
392 Inkscape::XML::Node *
393 sp_selected_item_to_curved_repr(SPItem *item, guint32 /*text_grouping_policy*/)
394 {
395 if (!item)
396 return NULL;
398 SPCurve *curve = NULL;
399 if (SP_IS_SHAPE(item)) {
400 curve = sp_shape_get_curve(SP_SHAPE(item));
401 } else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
402 curve = te_get_layout(item)->convertToCurves();
403 }
405 if (!curve)
406 return NULL;
408 // Prevent empty paths from being added to the document
409 // otherwise we end up with zomby markup in the SVG file
410 if(curve->end <= 0)
411 {
412 sp_curve_unref(curve);
413 return NULL;
414 }
416 Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(item)->document();
417 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
418 /* Transformation */
419 repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
420 /* Style */
421 gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
422 SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
423 repr->setAttribute("style", style_str);
424 g_free(style_str);
426 /* Mask */
427 gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
428 if ( mask_str )
429 repr->setAttribute("mask", mask_str);
431 /* Clip path */
432 gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
433 if ( clip_path_str )
434 repr->setAttribute("clip-path", clip_path_str);
436 /* Rotation center */
437 sp_repr_set_attr(repr, "inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"));
438 sp_repr_set_attr(repr, "inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"));
440 /* Definition */
441 gchar *def_str = sp_svg_write_path(SP_CURVE_BPATH(curve));
442 repr->setAttribute("d", def_str);
443 g_free(def_str);
444 sp_curve_unref(curve);
445 return repr;
446 }
449 // FIXME: THIS DOES NOT REVERSE THE NODETYPES ORDER!
450 void
451 sp_selected_path_reverse()
452 {
453 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
455 Inkscape::Selection *selection = sp_desktop_selection(desktop);
456 GSList *items = (GSList *) selection->itemList();
458 if (!items) {
459 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to reverse."));
460 return;
461 }
464 // set "busy" cursor
465 desktop->setWaitingCursor();
467 bool did = false;
468 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Reversing paths..."));
470 for (GSList *i = items; i != NULL; i = i->next) {
472 if (!SP_IS_PATH(i->data))
473 continue;
475 did = true;
476 SPPath *path = SP_PATH(i->data);
478 SPCurve *rcurve = sp_curve_reverse(sp_path_get_curve_reference(path));
480 gchar *str = sp_svg_write_path(SP_CURVE_BPATH(rcurve));
481 if ( sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path)) ) {
482 SP_OBJECT_REPR(path)->setAttribute("inkscape:original-d", str);
483 } else {
484 SP_OBJECT_REPR(path)->setAttribute("d", str);
485 }
486 g_free(str);
488 sp_curve_unref(rcurve);
489 }
491 desktop->clearWaitingCursor();
493 if (did) {
494 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_REVERSE,
495 _("Reverse path"));
496 } else {
497 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to reverse in the selection."));
498 }
499 }
501 /*
502 Local Variables:
503 mode:c++
504 c-file-style:"stroustrup"
505 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
506 indent-tabs-mode:nil
507 fill-column:99
508 End:
509 */
510 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :