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 "text-editing.h"
31 #include "style.h"
32 #include "desktop.h"
33 #include "document.h"
34 #include "message-stack.h"
35 #include "selection.h"
36 #include "desktop-handles.h"
37 #include "box3d.h"
38 #include <2geom/pathvector.h>
39 #include "path-chemistry.h"
41 /* Helper functions for sp_selected_path_to_curves */
42 static void sp_selected_path_to_curves0(SPDesktop *desktop, bool do_document_done, guint32 text_grouping_policy);
43 static bool sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_select);
45 enum {
46 /* Not used yet. This is the placeholder of Lauris's idea. */
47 SP_TOCURVE_INTERACTIVE = 1 << 0,
48 SP_TOCURVE_GROUPING_BY_WORD = 1 << 1,
49 SP_TOCURVE_GROUPING_BY_LINE = 1 << 2,
50 SP_TOCURVE_GROUPING_BY_WHOLE = 1 << 3
51 };
53 void
54 sp_selected_path_combine(SPDesktop *desktop)
55 {
56 Inkscape::Selection *selection = sp_desktop_selection(desktop);
57 SPDocument *doc = sp_desktop_document(desktop);
59 if (g_slist_length((GSList *) selection->itemList()) < 2) {
60 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>at least two objects</b> to combine."));
61 return;
62 }
64 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Combining paths..."));
65 // set "busy" cursor
66 desktop->setWaitingCursor();
68 GSList *items = g_slist_copy((GSList *) selection->itemList());
69 GSList *to_paths = NULL;
70 for (GSList *i = items; i != NULL; i = i->next) {
71 SPItem *item = (SPItem *) i->data;
72 if (!SP_IS_PATH(item))
73 to_paths = g_slist_prepend(to_paths, item);
74 }
75 GSList *converted = NULL;
76 bool did = sp_item_list_to_curves(to_paths, &items, &converted);
77 g_slist_free(to_paths);
78 for (GSList *i = converted; i != NULL; i = i->next)
79 items = g_slist_prepend(items, doc->getObjectByRepr((Inkscape::XML::Node*)(i->data)));
81 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
82 items = g_slist_reverse(items);
84 // remember the position, id, transform and style of the topmost path, they will be assigned to the combined one
85 gint position = 0;
86 char const *id = NULL;
87 char const *transform = NULL;
88 gchar *style = NULL;
89 gchar *path_effect = NULL;
91 SPCurve* curve = 0;
92 SPItem *first = NULL;
93 Inkscape::XML::Node *parent = NULL;
95 for (GSList *i = items; i != NULL; i = i->next) { // going from top to bottom
97 SPItem *item = (SPItem *) i->data;
98 if (!SP_IS_PATH(item))
99 continue;
100 did = true;
102 SPCurve *c = sp_path_get_curve_for_edit(SP_PATH(item));
103 if (first == NULL) { // this is the topmost path
104 first = item;
105 parent = SP_OBJECT_REPR(first)->parent();
106 position = SP_OBJECT_REPR(first)->position();
107 id = SP_OBJECT_REPR(first)->attribute("id");
108 transform = SP_OBJECT_REPR(first)->attribute("transform");
109 // FIXME: merge styles of combined objects instead of using the first one's style
110 style = g_strdup(SP_OBJECT_REPR(first)->attribute("style"));
111 path_effect = g_strdup(SP_OBJECT_REPR(first)->attribute("inkscape:path-effect"));
112 //c->transform(item->transform);
113 curve = c;
114 } else {
115 c->transform(item->getRelativeTransform(SP_OBJECT(first)));
116 curve->append(c, false);
117 c->unref();
118 }
120 // unless this is the topmost object,
121 if (item != first) {
122 // reduce position only if the same parent
123 if (SP_OBJECT_REPR(item)->parent() == parent)
124 position--;
125 // delete the object for real, so that its clones can take appropriate action
126 SP_OBJECT(item)->deleteObject();
127 }
128 }
130 g_slist_free(items);
132 if (did) {
133 selection->clear();
135 // delete the topmost one so that its clones don't get alerted; this object will be
136 // restored shortly, with the same id
137 SP_OBJECT(first)->deleteObject(false);
139 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
140 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
142 // restore id, transform, path effect, and style
143 repr->setAttribute("id", id);
144 if (transform) repr->setAttribute("transform", transform);
145 repr->setAttribute("style", style);
146 g_free(style);
148 // set path data corresponding to new curve
149 gchar *dstring = sp_svg_write_path(curve->get_pathvector());
150 curve->unref();
151 repr->setAttribute("d", dstring);
152 if (path_effect)
153 repr->setAttribute("inkscape:original-d", dstring);
154 g_free(dstring);
156 repr->setAttribute("inkscape:path-effect", path_effect);
157 g_free(path_effect);
159 // add the new group to the parent of the topmost
160 parent->appendChild(repr);
162 // move to the position of the topmost, reduced by the number of deleted items
163 repr->setPosition(position > 0 ? position : 0);
165 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_COMBINE,
166 _("Combine"));
168 selection->set(repr);
170 Inkscape::GC::release(repr);
172 } else {
173 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to combine in the selection."));
174 }
176 desktop->clearWaitingCursor();
177 }
179 void
180 sp_selected_path_break_apart(SPDesktop *desktop)
181 {
182 Inkscape::Selection *selection = sp_desktop_selection(desktop);
184 if (selection->isEmpty()) {
185 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to break apart."));
186 return;
187 }
189 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Breaking apart paths..."));
190 // set "busy" cursor
191 desktop->setWaitingCursor();
193 bool did = false;
195 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
196 items != NULL;
197 items = items->next) {
199 SPItem *item = (SPItem *) items->data;
201 if (!SP_IS_PATH(item))
202 continue;
204 SPPath *path = SP_PATH(item);
206 SPCurve *curve = sp_path_get_curve_for_edit(SP_PATH(path));
207 if (curve == NULL)
208 continue;
210 did = true;
212 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
213 gint pos = SP_OBJECT_REPR(item)->position();
214 char const *id = SP_OBJECT_REPR(item)->attribute("id");
216 gchar *style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
217 gchar *path_effect = g_strdup(SP_OBJECT(item)->repr->attribute("inkscape:path-effect"));
219 Geom::PathVector apv = curve->get_pathvector() * SP_ITEM(path)->transform;
221 curve->unref();
223 // it's going to resurrect as one of the pieces, so we delete without advertisement
224 SP_OBJECT(item)->deleteObject(false);
226 curve = new SPCurve(apv);
227 g_assert(curve != NULL);
229 GSList *list = curve->split();
231 curve->unref();
233 GSList *reprs = NULL;
234 for (GSList *l = list; l != NULL; l = l->next) {
235 curve = (SPCurve *) l->data;
237 Inkscape::XML::Node *repr = parent->document()->createElement("svg:path");
238 repr->setAttribute("style", style);
240 gchar *str = sp_svg_write_path(curve->get_pathvector());
241 repr->setAttribute("d", str);
242 if (path_effect)
243 repr->setAttribute("inkscape:original-d", str);
244 g_free(str);
246 repr->setAttribute("inkscape:path-effect", path_effect);
248 // add the new repr to the parent
249 parent->appendChild(repr);
251 // move to the saved position
252 repr->setPosition(pos > 0 ? pos : 0);
254 // if it's the first one, restore id
255 if (l == list)
256 repr->setAttribute("id", id);
258 reprs = g_slist_prepend (reprs, repr);
260 Inkscape::GC::release(repr);
261 }
263 selection->setReprList(reprs);
265 g_slist_free(reprs);
266 g_slist_free(list);
267 g_free(style);
269 }
271 desktop->clearWaitingCursor();
273 if (did) {
274 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_BREAK_APART,
275 _("Break apart"));
276 } else {
277 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to break apart in the selection."));
278 }
279 }
281 /* This function is an entry point from GUI */
282 void
283 sp_selected_path_to_curves(SPDesktop *desktop, bool interactive)
284 {
285 sp_selected_path_to_curves0(desktop, interactive, interactive ? SP_TOCURVE_INTERACTIVE : 0);
286 }
288 static void
289 sp_selected_path_to_curves0(SPDesktop *desktop, bool interactive, guint32 /*text_grouping_policy*/)
290 {
291 Inkscape::Selection *selection = sp_desktop_selection(desktop);
293 if (selection->isEmpty()) {
294 if (interactive)
295 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to path."));
296 return;
297 }
299 bool did = false;
300 if (interactive) {
301 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Converting objects to paths..."));
302 // set "busy" cursor
303 desktop->setWaitingCursor();
304 }
306 GSList *selected = g_slist_copy((GSList *) selection->itemList());
307 GSList *to_select = NULL;
308 selection->clear();
309 GSList *items = g_slist_copy(selected);
311 did = sp_item_list_to_curves(items, &selected, &to_select);
313 g_slist_free (items);
314 selection->setReprList(to_select);
315 selection->addList(selected);
316 g_slist_free (to_select);
317 g_slist_free (selected);
319 if (interactive) {
320 desktop->clearWaitingCursor();
321 if (did) {
322 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_TO_CURVE,
323 _("Object to path"));
324 } else {
325 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No objects</b> to convert to path in the selection."));
326 return;
327 }
328 }
329 }
331 static bool
332 sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_select)
333 {
334 bool did = false;
336 for (;
337 items != NULL;
338 items = items->next) {
340 SPItem *item = SP_ITEM(items->data);
341 SPDocument *document = item->document;
343 if (SP_IS_PATH(item) && !SP_PATH(item)->original_curve) {
344 continue; // already a path, and no path effect
345 }
347 if (SP_IS_BOX3D(item)) {
348 // convert 3D box to ordinary group of paths; replace the old element in 'selected' with the new group
349 Inkscape::XML::Node *repr = SP_OBJECT_REPR(box3d_convert_to_group(SP_BOX3D(item)));
351 if (repr) {
352 *to_select = g_slist_prepend (*to_select, repr);
353 did = true;
354 *selected = g_slist_remove (*selected, item);
355 }
357 continue;
358 }
360 if (SP_IS_GROUP(item)) {
361 sp_lpe_item_remove_all_path_effects(SP_LPE_ITEM(item), true);
362 GSList *item_list = sp_item_group_item_list(SP_GROUP(item));
364 GSList *item_to_select = NULL;
365 GSList *item_selected = NULL;
367 if (sp_item_list_to_curves(item_list, &item_selected, &item_to_select))
368 did = true;
370 g_slist_free(item_list);
371 g_slist_free(item_to_select);
372 g_slist_free(item_selected);
374 continue;
375 }
377 Inkscape::XML::Node *repr = sp_selected_item_to_curved_repr(item, 0);
378 if (!repr)
379 continue;
381 did = true;
382 *selected = g_slist_remove (*selected, item);
384 // remember the position of the item
385 gint pos = SP_OBJECT_REPR(item)->position();
386 // remember parent
387 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
388 // remember id
389 char const *id = SP_OBJECT_REPR(item)->attribute("id");
390 // remember title
391 gchar *title = item->title();
392 // remember description
393 gchar *desc = item->desc();
395 // It's going to resurrect, so we delete without notifying listeners.
396 SP_OBJECT(item)->deleteObject(false);
398 // restore id
399 repr->setAttribute("id", id);
400 // add the new repr to the parent
401 parent->appendChild(repr);
402 SPObject* newObj = document->getObjectByRepr(repr);
403 if (title && newObj) {
404 newObj->setTitle(title);
405 g_free(title);
406 }
407 if (desc && newObj) {
408 newObj->setDesc(desc);
409 g_free(desc);
410 }
412 // move to the saved position
413 repr->setPosition(pos > 0 ? pos : 0);
415 /* Buglet: We don't re-add the (new version of the) object to the selection of any other
416 * desktops where it was previously selected. */
417 *to_select = g_slist_prepend (*to_select, repr);
418 Inkscape::GC::release(repr);
419 }
421 return did;
422 }
424 Inkscape::XML::Node *
425 sp_selected_item_to_curved_repr(SPItem *item, guint32 /*text_grouping_policy*/)
426 {
427 if (!item)
428 return NULL;
430 SPCurve *curve = NULL;
431 if (SP_IS_SHAPE(item)) {
432 curve = sp_shape_get_curve(SP_SHAPE(item));
433 } else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
434 curve = te_get_layout(item)->convertToCurves();
435 }
437 if (!curve)
438 return NULL;
440 // Prevent empty paths from being added to the document
441 // otherwise we end up with zomby markup in the SVG file
442 if(curve->is_empty())
443 {
444 curve->unref();
445 return NULL;
446 }
448 Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(item)->document();
449 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
450 /* Transformation */
451 repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
452 /* Style */
453 gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
454 SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
455 repr->setAttribute("style", style_str);
456 g_free(style_str);
458 /* Mask */
459 gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
460 if ( mask_str )
461 repr->setAttribute("mask", mask_str);
463 /* Clip path */
464 gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
465 if ( clip_path_str )
466 repr->setAttribute("clip-path", clip_path_str);
468 /* Rotation center */
469 repr->setAttribute("inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"), false);
470 repr->setAttribute("inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"), false);
472 /* Definition */
473 gchar *def_str = sp_svg_write_path(curve->get_pathvector());
474 repr->setAttribute("d", def_str);
475 g_free(def_str);
476 curve->unref();
477 return repr;
478 }
481 // FIXME: THIS DOES NOT REVERSE THE NODETYPES ORDER!
482 void
483 sp_selected_path_reverse(SPDesktop *desktop)
484 {
485 Inkscape::Selection *selection = sp_desktop_selection(desktop);
486 GSList *items = (GSList *) selection->itemList();
488 if (!items) {
489 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to reverse."));
490 return;
491 }
494 // set "busy" cursor
495 desktop->setWaitingCursor();
497 bool did = false;
498 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Reversing paths..."));
500 for (GSList *i = items; i != NULL; i = i->next) {
502 if (!SP_IS_PATH(i->data))
503 continue;
505 did = true;
506 SPPath *path = SP_PATH(i->data);
508 SPCurve *rcurve = sp_path_get_curve_reference(path)->create_reverse();
510 gchar *str = sp_svg_write_path(rcurve->get_pathvector());
511 if ( sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path)) ) {
512 SP_OBJECT_REPR(path)->setAttribute("inkscape:original-d", str);
513 } else {
514 SP_OBJECT_REPR(path)->setAttribute("d", str);
515 }
516 g_free(str);
518 rcurve->unref();
519 }
521 desktop->clearWaitingCursor();
523 if (did) {
524 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_REVERSE,
525 _("Reverse path"));
526 } else {
527 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to reverse in the selection."));
528 }
529 }
531 /*
532 Local Variables:
533 mode:c++
534 c-file-style:"stroustrup"
535 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
536 indent-tabs-mode:nil
537 fill-column:99
538 End:
539 */
540 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :