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;
57 Inkscape::Selection *selection = sp_desktop_selection(desktop);
58 GSList *items = (GSList *) selection->itemList();
60 if (g_slist_length(items) < 2) {
61 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>at least two objects</b> to combine."));
62 return;
63 }
65 for (GSList *i = items; i != NULL; i = i->next) {
66 SPItem *item = (SPItem *) i->data;
67 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item)) {
68 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("At least one of the objects is <b>not a path</b>, cannot combine."));
69 return;
70 }
71 }
73 Inkscape::XML::Node *parent = SP_OBJECT_REPR((SPItem *) items->data)->parent();
74 for (GSList *i = items; i != NULL; i = i->next) {
75 if ( SP_OBJECT_REPR((SPItem *) i->data)->parent() != parent ) {
76 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("You cannot combine objects from <b>different groups</b> or <b>layers</b>."));
77 return;
78 }
79 }
81 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Combining paths..."));
82 // set "busy" cursor
83 desktop->setWaitingCursor();
85 sp_selected_path_to_curves0(FALSE, 0);
87 items = g_slist_copy((GSList *) selection->itemList());
88 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
89 selection->clear();
91 // remember the position of the topmost object
92 gint topmost = (SP_OBJECT_REPR((SPItem *) g_slist_last(items)->data))->position();
94 // remember the id of the bottomost object
95 char const *id = SP_OBJECT_REPR((SPItem *) items->data)->attribute("id");
97 // FIXME: merge styles of combined objects instead of using the first one's style
98 gchar *style = g_strdup(SP_OBJECT_REPR((SPItem *) items->data)->attribute("style"));
100 GString *dstring = g_string_new("");
101 for (GSList *i = items; i != NULL; i = i->next) {
103 SPPath *path = (SPPath *) i->data;
104 SPCurve *c = sp_shape_get_curve(SP_SHAPE(path));
106 NArtBpath *abp = nr_artpath_affine(SP_CURVE_BPATH(c), SP_ITEM(path)->transform);
107 sp_curve_unref(c);
108 gchar *str = sp_svg_write_path(abp);
109 g_free(abp);
111 dstring = g_string_append(dstring, str);
112 g_free(str);
114 // if this is the bottommost object,
115 if (!strcmp(SP_OBJECT_REPR(path)->attribute("id"), id)) {
116 // delete it so that its clones don't get alerted; this object will be restored shortly, with the same id
117 SP_OBJECT(path)->deleteObject(false);
118 } else {
119 // delete the object for real, so that its clones can take appropriate action
120 SP_OBJECT(path)->deleteObject();
121 }
123 topmost--;
124 }
126 g_slist_free(items);
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
132 repr->setAttribute("id", id);
134 repr->setAttribute("style", style);
135 g_free(style);
137 repr->setAttribute("d", dstring->str);
138 g_string_free(dstring, TRUE);
140 // add the new group to the group members' common parent
141 parent->appendChild(repr);
143 // move to the position of the topmost, reduced by the number of deleted items
144 repr->setPosition(topmost > 0 ? topmost + 1 : 0);
146 desktop->clearWaitingCursor();
148 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_COMBINE,
149 _("Combine"));
151 selection->set(repr);
153 Inkscape::GC::release(repr);
154 }
156 void
157 sp_selected_path_break_apart(void)
158 {
159 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
161 Inkscape::Selection *selection = sp_desktop_selection(desktop);
163 if (selection->isEmpty()) {
164 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to break apart."));
165 return;
166 }
168 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Breaking apart paths..."));
169 // set "busy" cursor
170 desktop->setWaitingCursor();
172 bool did = false;
174 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
175 items != NULL;
176 items = items->next) {
178 SPItem *item = (SPItem *) items->data;
180 if (!SP_IS_PATH(item))
181 continue;
183 SPPath *path = SP_PATH(item);
185 SPCurve *curve = sp_shape_get_curve(SP_SHAPE(path));
186 if (curve == NULL)
187 continue;
189 did = true;
191 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
192 gint pos = SP_OBJECT_REPR(item)->position();
193 char const *id = SP_OBJECT_REPR(item)->attribute("id");
195 gchar *style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
197 NArtBpath *abp = nr_artpath_affine(SP_CURVE_BPATH(curve), (SP_ITEM(path))->transform);
199 sp_curve_unref(curve);
201 // it's going to resurrect as one of the pieces, so we delete without advertisement
202 SP_OBJECT(item)->deleteObject(false);
204 curve = sp_curve_new_from_bpath(abp);
205 g_assert(curve != NULL);
207 GSList *list = sp_curve_split(curve);
209 sp_curve_unref(curve);
211 GSList *reprs = NULL;
212 for (GSList *l = list; l != NULL; l = l->next) {
213 curve = (SPCurve *) l->data;
215 Inkscape::XML::Node *repr = parent->document()->createElement("svg:path");
216 repr->setAttribute("style", style);
218 gchar *str = sp_svg_write_path(SP_CURVE_BPATH(curve));
219 repr->setAttribute("d", str);
220 g_free(str);
222 // add the new repr to the parent
223 parent->appendChild(repr);
225 // move to the saved position
226 repr->setPosition(pos > 0 ? pos : 0);
228 // if it's the first one, restore id
229 if (l == list)
230 repr->setAttribute("id", id);
232 reprs = g_slist_prepend (reprs, repr);
234 Inkscape::GC::release(repr);
235 }
237 selection->setReprList(reprs);
239 g_slist_free(reprs);
240 g_slist_free(list);
241 g_free(style);
243 }
245 desktop->clearWaitingCursor();
247 if (did) {
248 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_BREAK_APART,
249 _("Break apart"));
250 } else {
251 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to break apart in the selection."));
252 return;
253 }
254 }
256 /* This function is an entry point from GUI */
257 void
258 sp_selected_path_to_curves(void)
259 {
260 sp_selected_path_to_curves0(TRUE, SP_TOCURVE_INTERACTIVE);
261 }
263 static void
264 sp_selected_path_to_curves0(gboolean interactive, guint32 /*text_grouping_policy*/)
265 {
266 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
268 Inkscape::Selection *selection = sp_desktop_selection(desktop);
270 if (selection->isEmpty()) {
271 if (interactive)
272 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to path."));
273 return;
274 }
276 bool did = false;
277 if (interactive) {
278 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Converting objects to paths..."));
279 // set "busy" cursor
280 desktop->setWaitingCursor();
281 }
283 GSList *selected = g_slist_copy((GSList *) selection->itemList());
284 GSList *to_select = NULL;
285 selection->clear();
286 GSList *items = g_slist_copy(selected);
288 for (;
289 items != NULL;
290 items = items->next) {
292 SPItem *item = SP_ITEM(items->data);
294 if (SP_IS_PATH(item) && !SP_PATH(item)->original_curve) {
295 continue; // already a path, and no path effect
296 }
298 if (SP_IS_BOX3D(item)) {
299 // convert 3D box to ordinary group of paths; replace the old element in 'selected' with the new group
300 GSList *sel_it = g_slist_find(selected, item);
301 sel_it->data = box3d_convert_to_group(SP_BOX3D(item));
302 item = SP_ITEM(sel_it->data);
304 did = true;
305 selected = g_slist_remove (selected, item);
307 continue;
308 }
310 Inkscape::XML::Node *repr = sp_selected_item_to_curved_repr(item, 0);
311 if (!repr)
312 continue;
314 did = true;
315 selected = g_slist_remove (selected, item);
317 // remember the position of the item
318 gint pos = SP_OBJECT_REPR(item)->position();
319 // remember parent
320 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
321 // remember id
322 char const *id = SP_OBJECT_REPR(item)->attribute("id");
324 // It's going to resurrect, so we delete without notifying listeners.
325 SP_OBJECT(item)->deleteObject(false);
327 // restore id
328 repr->setAttribute("id", id);
329 // add the new repr to the parent
330 parent->appendChild(repr);
331 // move to the saved position
332 repr->setPosition(pos > 0 ? pos : 0);
334 /* Buglet: We don't re-add the (new version of the) object to the selection of any other
335 * desktops where it was previously selected. */
336 to_select = g_slist_prepend (to_select, repr);
337 Inkscape::GC::release(repr);
338 }
340 g_slist_free (items);
341 selection->setReprList(to_select);
342 selection->addList(selected);
343 g_slist_free (to_select);
344 g_slist_free (selected);
346 if (interactive) {
347 desktop->clearWaitingCursor();
348 if (did) {
349 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_TO_CURVE,
350 _("Object to path"));
351 } else {
352 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No objects</b> to convert to path in the selection."));
353 return;
354 }
355 }
356 }
358 Inkscape::XML::Node *
359 sp_selected_item_to_curved_repr(SPItem *item, guint32 /*text_grouping_policy*/)
360 {
361 if (!item)
362 return NULL;
364 SPCurve *curve = NULL;
365 if (SP_IS_SHAPE(item)) {
366 curve = sp_shape_get_curve(SP_SHAPE(item));
367 } else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
368 curve = te_get_layout(item)->convertToCurves();
369 }
371 if (!curve)
372 return NULL;
374 Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(item)->document();
375 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
376 /* Transformation */
377 repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
378 /* Style */
379 gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
380 SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
381 repr->setAttribute("style", style_str);
382 g_free(style_str);
384 /* Mask */
385 gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
386 if ( mask_str )
387 repr->setAttribute("mask", mask_str);
389 /* Clip path */
390 gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
391 if ( clip_path_str )
392 repr->setAttribute("clip-path", clip_path_str);
394 /* Rotation center */
395 sp_repr_set_attr(repr, "inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"));
396 sp_repr_set_attr(repr, "inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"));
398 /* Definition */
399 gchar *def_str = sp_svg_write_path(SP_CURVE_BPATH(curve));
400 repr->setAttribute("d", def_str);
401 g_free(def_str);
402 sp_curve_unref(curve);
403 return repr;
404 }
407 // FIXME: THIS DOES NOT REVERSE THE NODETYPES ORDER!
408 void
409 sp_selected_path_reverse()
410 {
411 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
413 Inkscape::Selection *selection = sp_desktop_selection(desktop);
414 GSList *items = (GSList *) selection->itemList();
416 if (!items) {
417 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to reverse."));
418 return;
419 }
422 // set "busy" cursor
423 desktop->setWaitingCursor();
425 bool did = false;
426 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Reversing paths..."));
428 for (GSList *i = items; i != NULL; i = i->next) {
430 if (!SP_IS_PATH(i->data))
431 continue;
433 did = true;
434 SPPath *path = SP_PATH(i->data);
436 SPCurve *rcurve = sp_curve_reverse(sp_path_get_curve_reference(path));
438 gchar *str = sp_svg_write_path(SP_CURVE_BPATH(rcurve));
439 if ( sp_shape_has_path_effect(SP_SHAPE(path)) ) {
440 SP_OBJECT_REPR(path)->setAttribute("inkscape:original-d", str);
441 } else {
442 SP_OBJECT_REPR(path)->setAttribute("d", str);
443 }
444 g_free(str);
446 sp_curve_unref(rcurve);
447 }
449 desktop->clearWaitingCursor();
451 if (did) {
452 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_REVERSE,
453 _("Reverse path"));
454 } else {
455 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to reverse in the selection."));
456 }
457 }
459 /*
460 Local Variables:
461 mode:c++
462 c-file-style:"stroustrup"
463 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
464 indent-tabs-mode:nil
465 fill-column:99
466 End:
467 */
468 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :