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 <cstring>
20 #include <string>
21 #include "xml/repr.h"
22 #include "svg/svg.h"
23 #include "display/curve.h"
24 #include <glib/gmem.h>
25 #include <glibmm/i18n.h>
26 #include "sp-path.h"
27 #include "sp-text.h"
28 #include "sp-flowtext.h"
29 #include "libnr/nr-path.h"
30 #include "text-editing.h"
31 #include "style.h"
32 #include "inkscape.h"
33 #include "desktop.h"
34 #include "document.h"
35 #include "message-stack.h"
36 #include "selection.h"
37 #include "desktop-handles.h"
38 #include "box3d.h"
40 #include "path-chemistry.h"
42 /* Helper functions for sp_selected_path_to_curves */
43 static void sp_selected_path_to_curves0(gboolean do_document_done, guint32 text_grouping_policy);
44 enum {
45 /* Not used yet. This is the placeholder of Lauris's idea. */
46 SP_TOCURVE_INTERACTIVE = 1 << 0,
47 SP_TOCURVE_GROUPING_BY_WORD = 1 << 1,
48 SP_TOCURVE_GROUPING_BY_LINE = 1 << 2,
49 SP_TOCURVE_GROUPING_BY_WHOLE = 1 << 3
50 };
52 void
53 sp_selected_path_combine(void)
54 {
55 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
56 Inkscape::Selection *selection = sp_desktop_selection(desktop);
58 if (g_slist_length((GSList *) selection->itemList()) < 2) {
59 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>at least two objects</b> to combine."));
60 return;
61 }
63 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Combining paths..."));
64 // set "busy" cursor
65 desktop->setWaitingCursor();
67 sp_selected_path_to_curves0(FALSE, 0);
69 GSList *items = g_slist_copy((GSList *) selection->itemList());
70 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
71 items = g_slist_reverse(items);
73 // remember the position, id and style of the topmost path, they will be assigned to the combined one
74 gint position = 0;
75 char const *id = NULL;
76 gchar *style = NULL;
78 GString *dstring = g_string_new("");
79 bool did = false;
80 SPItem *first = NULL;
81 Inkscape::XML::Node *parent = NULL;
83 for (GSList *i = items; i != NULL; i = i->next) { // going from top to bottom
85 SPItem *item = (SPItem *) i->data;
86 if (!SP_IS_PATH(item))
87 continue;
88 did = true;
90 NArtBpath *abp = NULL;
91 SPCurve *c = sp_shape_get_curve(SP_SHAPE(item));
92 if (first == NULL) { // this is the topmost path
93 first = item;
94 parent = SP_OBJECT_REPR(first)->parent();
95 position = SP_OBJECT_REPR(first)->position();
96 id = SP_OBJECT_REPR(first)->attribute("id");
97 // FIXME: merge styles of combined objects instead of using the first one's style
98 style = g_strdup(SP_OBJECT_REPR(first)->attribute("style"));
99 abp = nr_artpath_affine(SP_CURVE_BPATH(c), item->transform);
100 } else {
101 abp = nr_artpath_affine(SP_CURVE_BPATH(c),
102 item->getRelativeTransform(SP_OBJECT(first)));
103 }
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 // unless this is the topmost object,
112 if (item != first) {
113 // reduce position only if the same parent
114 if (SP_OBJECT_REPR(item)->parent() == parent)
115 position--;
116 // delete the object for real, so that its clones can take appropriate action
117 SP_OBJECT(item)->deleteObject();
118 }
119 }
121 g_slist_free(items);
123 if (did) {
124 selection->clear();
126 // delete the topmost one so that its clones don't get alerted; this object will be
127 // restored shortly, with the same id
128 SP_OBJECT(first)->deleteObject(false);
130 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
131 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
133 // restore id
134 repr->setAttribute("id", id);
136 repr->setAttribute("style", style);
137 g_free(style);
139 repr->setAttribute("d", dstring->str);
140 g_string_free(dstring, TRUE);
142 // add the new group to the parent of the topmost
143 parent->appendChild(repr);
145 // move to the position of the topmost, reduced by the number of deleted items
146 repr->setPosition(position > 0 ? position : 0);
148 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_COMBINE,
149 _("Combine"));
151 selection->set(repr);
153 Inkscape::GC::release(repr);
155 } else {
156 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to combine in the selection."));
157 }
159 desktop->clearWaitingCursor();
160 }
162 void
163 sp_selected_path_break_apart(void)
164 {
165 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
167 Inkscape::Selection *selection = sp_desktop_selection(desktop);
169 if (selection->isEmpty()) {
170 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to break apart."));
171 return;
172 }
174 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Breaking apart paths..."));
175 // set "busy" cursor
176 desktop->setWaitingCursor();
178 bool did = false;
180 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
181 items != NULL;
182 items = items->next) {
184 SPItem *item = (SPItem *) items->data;
186 if (!SP_IS_PATH(item))
187 continue;
189 SPPath *path = SP_PATH(item);
191 SPCurve *curve = sp_shape_get_curve(SP_SHAPE(path));
192 if (curve == NULL)
193 continue;
195 did = true;
197 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
198 gint pos = SP_OBJECT_REPR(item)->position();
199 char const *id = SP_OBJECT_REPR(item)->attribute("id");
201 gchar *style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
203 NArtBpath *abp = nr_artpath_affine(SP_CURVE_BPATH(curve), (SP_ITEM(path))->transform);
205 sp_curve_unref(curve);
207 // it's going to resurrect as one of the pieces, so we delete without advertisement
208 SP_OBJECT(item)->deleteObject(false);
210 curve = sp_curve_new_from_bpath(abp);
211 g_assert(curve != NULL);
213 GSList *list = sp_curve_split(curve);
215 sp_curve_unref(curve);
217 GSList *reprs = NULL;
218 for (GSList *l = list; l != NULL; l = l->next) {
219 curve = (SPCurve *) l->data;
221 Inkscape::XML::Node *repr = parent->document()->createElement("svg:path");
222 repr->setAttribute("style", style);
224 gchar *str = sp_svg_write_path(SP_CURVE_BPATH(curve));
225 repr->setAttribute("d", str);
226 g_free(str);
228 // add the new repr to the parent
229 parent->appendChild(repr);
231 // move to the saved position
232 repr->setPosition(pos > 0 ? pos : 0);
234 // if it's the first one, restore id
235 if (l == list)
236 repr->setAttribute("id", id);
238 reprs = g_slist_prepend (reprs, repr);
240 Inkscape::GC::release(repr);
241 }
243 selection->setReprList(reprs);
245 g_slist_free(reprs);
246 g_slist_free(list);
247 g_free(style);
249 }
251 desktop->clearWaitingCursor();
253 if (did) {
254 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_BREAK_APART,
255 _("Break apart"));
256 } else {
257 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to break apart in the selection."));
258 }
259 }
261 /* This function is an entry point from GUI */
262 void
263 sp_selected_path_to_curves(void)
264 {
265 sp_selected_path_to_curves0(TRUE, SP_TOCURVE_INTERACTIVE);
266 }
268 static void
269 sp_selected_path_to_curves0(gboolean interactive, guint32 /*text_grouping_policy*/)
270 {
271 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
273 Inkscape::Selection *selection = sp_desktop_selection(desktop);
275 if (selection->isEmpty()) {
276 if (interactive)
277 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to path."));
278 return;
279 }
281 bool did = false;
282 if (interactive) {
283 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Converting objects to paths..."));
284 // set "busy" cursor
285 desktop->setWaitingCursor();
286 }
288 GSList *selected = g_slist_copy((GSList *) selection->itemList());
289 GSList *to_select = NULL;
290 selection->clear();
291 GSList *items = g_slist_copy(selected);
293 for (;
294 items != NULL;
295 items = items->next) {
297 SPItem *item = SP_ITEM(items->data);
299 if (SP_IS_PATH(item) && !SP_PATH(item)->original_curve) {
300 continue; // already a path, and no path effect
301 }
303 if (SP_IS_BOX3D(item)) {
304 // convert 3D box to ordinary group of paths; replace the old element in 'selected' with the new group
305 Inkscape::XML::Node *repr = box3d_convert_to_group(SP_BOX3D(item));
307 if (repr) {
308 to_select = g_slist_prepend (to_select, repr);
309 did = true;
310 selected = g_slist_remove (selected, item);
311 }
313 continue;
314 }
316 Inkscape::XML::Node *repr = sp_selected_item_to_curved_repr(item, 0);
317 if (!repr)
318 continue;
320 did = true;
321 selected = g_slist_remove (selected, item);
323 // remember the position of the item
324 gint pos = SP_OBJECT_REPR(item)->position();
325 // remember parent
326 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
327 // remember id
328 char const *id = SP_OBJECT_REPR(item)->attribute("id");
330 // It's going to resurrect, so we delete without notifying listeners.
331 SP_OBJECT(item)->deleteObject(false);
333 // restore id
334 repr->setAttribute("id", id);
335 // add the new repr to the parent
336 parent->appendChild(repr);
337 // move to the saved position
338 repr->setPosition(pos > 0 ? pos : 0);
340 /* Buglet: We don't re-add the (new version of the) object to the selection of any other
341 * desktops where it was previously selected. */
342 to_select = g_slist_prepend (to_select, repr);
343 Inkscape::GC::release(repr);
344 }
346 g_slist_free (items);
347 selection->setReprList(to_select);
348 selection->addList(selected);
349 g_slist_free (to_select);
350 g_slist_free (selected);
352 if (interactive) {
353 desktop->clearWaitingCursor();
354 if (did) {
355 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_TO_CURVE,
356 _("Object to path"));
357 } else {
358 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No objects</b> to convert to path in the selection."));
359 return;
360 }
361 }
362 }
364 Inkscape::XML::Node *
365 sp_selected_item_to_curved_repr(SPItem *item, guint32 /*text_grouping_policy*/)
366 {
367 if (!item)
368 return NULL;
370 SPCurve *curve = NULL;
371 if (SP_IS_SHAPE(item)) {
372 curve = sp_shape_get_curve(SP_SHAPE(item));
373 } else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
374 curve = te_get_layout(item)->convertToCurves();
375 }
377 if (!curve)
378 return NULL;
380 // Prevent empty paths from being added to the document
381 // otherwise we end up with zomby markup in the SVG file
382 if(curve->end <= 0)
383 {
384 sp_curve_unref(curve);
385 return NULL;
386 }
388 Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(item)->document();
389 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
390 /* Transformation */
391 repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
392 /* Style */
393 gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
394 SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
395 repr->setAttribute("style", style_str);
396 g_free(style_str);
398 /* Mask */
399 gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
400 if ( mask_str )
401 repr->setAttribute("mask", mask_str);
403 /* Clip path */
404 gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
405 if ( clip_path_str )
406 repr->setAttribute("clip-path", clip_path_str);
408 /* Rotation center */
409 sp_repr_set_attr(repr, "inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"));
410 sp_repr_set_attr(repr, "inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"));
412 /* Definition */
413 gchar *def_str = sp_svg_write_path(SP_CURVE_BPATH(curve));
414 repr->setAttribute("d", def_str);
415 g_free(def_str);
416 sp_curve_unref(curve);
417 return repr;
418 }
421 // FIXME: THIS DOES NOT REVERSE THE NODETYPES ORDER!
422 void
423 sp_selected_path_reverse()
424 {
425 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
427 Inkscape::Selection *selection = sp_desktop_selection(desktop);
428 GSList *items = (GSList *) selection->itemList();
430 if (!items) {
431 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to reverse."));
432 return;
433 }
436 // set "busy" cursor
437 desktop->setWaitingCursor();
439 bool did = false;
440 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Reversing paths..."));
442 for (GSList *i = items; i != NULL; i = i->next) {
444 if (!SP_IS_PATH(i->data))
445 continue;
447 did = true;
448 SPPath *path = SP_PATH(i->data);
450 SPCurve *rcurve = sp_curve_reverse(sp_path_get_curve_reference(path));
452 gchar *str = sp_svg_write_path(SP_CURVE_BPATH(rcurve));
453 if ( sp_shape_has_path_effect(SP_SHAPE(path)) ) {
454 SP_OBJECT_REPR(path)->setAttribute("inkscape:original-d", str);
455 } else {
456 SP_OBJECT_REPR(path)->setAttribute("d", str);
457 }
458 g_free(str);
460 sp_curve_unref(rcurve);
461 }
463 desktop->clearWaitingCursor();
465 if (did) {
466 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_REVERSE,
467 _("Reverse path"));
468 } else {
469 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to reverse in the selection."));
470 }
471 }
473 /*
474 Local Variables:
475 mode:c++
476 c-file-style:"stroustrup"
477 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
478 indent-tabs-mode:nil
479 fill-column:99
480 End:
481 */
482 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :