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 /* Helper functions for sp_selected_path_to_curves */
38 static void sp_selected_path_to_curves0(gboolean do_document_done, guint32 text_grouping_policy);
39 static Inkscape::XML::Node *sp_selected_item_to_curved_repr(SPItem *item, guint32 text_grouping_policy);
40 enum {
41 /* Not used yet. This is the placeholder of Lauris's idea. */
42 SP_TOCURVE_INTERACTIVE = 1 << 0,
43 SP_TOCURVE_GROUPING_BY_WORD = 1 << 1,
44 SP_TOCURVE_GROUPING_BY_LINE = 1 << 2,
45 SP_TOCURVE_GROUPING_BY_WHOLE = 1 << 3
46 };
48 void
49 sp_selected_path_combine(void)
50 {
51 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
53 Inkscape::Selection *selection = sp_desktop_selection(desktop);
54 GSList *items = (GSList *) selection->itemList();
56 if (g_slist_length(items) < 2) {
57 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>at least two objects</b> to combine."));
58 return;
59 }
61 for (GSList *i = items; i != NULL; i = i->next) {
62 SPItem *item = (SPItem *) i->data;
63 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item)) {
64 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("At least one of the objects is <b>not a path</b>, cannot combine."));
65 return;
66 }
67 }
69 Inkscape::XML::Node *parent = SP_OBJECT_REPR((SPItem *) items->data)->parent();
70 for (GSList *i = items; i != NULL; i = i->next) {
71 if ( SP_OBJECT_REPR((SPItem *) i->data)->parent() != parent ) {
72 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("You cannot combine objects from <b>different groups</b> or <b>layers</b>."));
73 return;
74 }
75 }
77 sp_selected_path_to_curves0(FALSE, 0);
79 items = (GSList *) selection->itemList();
81 items = g_slist_copy(items);
83 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
85 // remember the position of the topmost object
86 gint topmost = (SP_OBJECT_REPR((SPItem *) g_slist_last(items)->data))->position();
88 // remember the id of the bottomost object
89 char const *id = SP_OBJECT_REPR((SPItem *) items->data)->attribute("id");
91 // FIXME: merge styles of combined objects instead of using the first one's style
92 gchar *style = g_strdup(SP_OBJECT_REPR((SPItem *) items->data)->attribute("style"));
94 GString *dstring = g_string_new("");
95 for (GSList *i = items; i != NULL; i = i->next) {
97 SPPath *path = (SPPath *) i->data;
98 SPCurve *c = sp_shape_get_curve(SP_SHAPE(path));
100 NArtBpath *abp = nr_artpath_affine(SP_CURVE_BPATH(c), SP_ITEM(path)->transform);
101 sp_curve_unref(c);
102 gchar *str = sp_svg_write_path(abp);
103 g_free(abp);
105 dstring = g_string_append(dstring, str);
106 g_free(str);
108 // if this is the bottommost object,
109 if (!strcmp(SP_OBJECT_REPR(path)->attribute("id"), id)) {
110 // delete it so that its clones don't get alerted; this object will be restored shortly, with the same id
111 SP_OBJECT(path)->deleteObject(false);
112 } else {
113 // delete the object for real, so that its clones can take appropriate action
114 SP_OBJECT(path)->deleteObject();
115 }
117 topmost--;
118 }
120 g_slist_free(items);
122 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
123 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
125 // restore id
126 repr->setAttribute("id", id);
128 repr->setAttribute("style", style);
129 g_free(style);
131 repr->setAttribute("d", dstring->str);
132 g_string_free(dstring, TRUE);
134 // add the new group to the group members' common parent
135 parent->appendChild(repr);
137 // move to the position of the topmost, reduced by the number of deleted items
138 repr->setPosition(topmost > 0 ? topmost + 1 : 0);
140 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_COMBINE,
141 _("Combine"));
143 selection->set(repr);
145 Inkscape::GC::release(repr);
146 }
148 void
149 sp_selected_path_break_apart(void)
150 {
151 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
153 Inkscape::Selection *selection = sp_desktop_selection(desktop);
155 if (selection->isEmpty()) {
156 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to break apart."));
157 return;
158 }
160 bool did = false;
162 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
163 items != NULL;
164 items = items->next) {
166 SPItem *item = (SPItem *) items->data;
168 if (!SP_IS_PATH(item))
169 continue;
171 SPPath *path = SP_PATH(item);
173 SPCurve *curve = sp_shape_get_curve(SP_SHAPE(path));
174 if (curve == NULL)
175 continue;
177 did = true;
179 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
180 gint pos = SP_OBJECT_REPR(item)->position();
181 char const *id = SP_OBJECT_REPR(item)->attribute("id");
183 gchar *style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
185 NArtBpath *abp = nr_artpath_affine(SP_CURVE_BPATH(curve), (SP_ITEM(path))->transform);
187 sp_curve_unref(curve);
189 // it's going to resurrect as one of the pieces, so we delete without advertisement
190 SP_OBJECT(item)->deleteObject(false);
192 curve = sp_curve_new_from_bpath(abp);
193 g_assert(curve != NULL);
195 GSList *list = sp_curve_split(curve);
197 sp_curve_unref(curve);
199 GSList *reprs = NULL;
200 for (GSList *l = list; l != NULL; l = l->next) {
201 curve = (SPCurve *) l->data;
203 Inkscape::XML::Node *repr = parent->document()->createElement("svg:path");
204 repr->setAttribute("style", style);
206 gchar *str = sp_svg_write_path(SP_CURVE_BPATH(curve));
207 repr->setAttribute("d", str);
208 g_free(str);
210 // add the new repr to the parent
211 parent->appendChild(repr);
213 // move to the saved position
214 repr->setPosition(pos > 0 ? pos : 0);
216 // if it's the first one, restore id
217 if (l == list)
218 repr->setAttribute("id", id);
220 reprs = g_slist_prepend (reprs, repr);
222 Inkscape::GC::release(repr);
223 }
225 selection->setReprList(reprs);
227 g_slist_free(reprs);
228 g_slist_free(list);
229 g_free(style);
231 }
233 if (did) {
234 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_BREAK_APART,
235 _("Break apart"));
236 } else {
237 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to break apart in the selection."));
238 return;
239 }
240 }
242 /* This function is an entry point from GUI */
243 void
244 sp_selected_path_to_curves(void)
245 {
246 sp_selected_path_to_curves0(TRUE, SP_TOCURVE_INTERACTIVE);
247 }
249 static void
250 sp_selected_path_to_curves0(gboolean interactive, guint32 text_grouping_policy)
251 {
252 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
254 Inkscape::Selection *selection = sp_desktop_selection(desktop);
256 if (selection->isEmpty()) {
257 if (interactive)
258 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to path."));
259 return;
260 }
262 bool did = false;
264 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
265 items != NULL;
266 items = items->next) {
268 SPItem *item = SP_ITEM(items->data);
270 Inkscape::XML::Node *repr = sp_selected_item_to_curved_repr(item, 0);
271 if (!repr)
272 continue;
274 did = true;
276 // remember the position of the item
277 gint pos = SP_OBJECT_REPR(item)->position();
278 // remember parent
279 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
280 // remember id
281 char const *id = SP_OBJECT_REPR(item)->attribute("id");
283 selection->remove(item);
285 // It's going to resurrect, so we delete without notifying listeners.
286 SP_OBJECT(item)->deleteObject(false);
288 // restore id
289 repr->setAttribute("id", id);
290 // add the new repr to the parent
291 parent->appendChild(repr);
292 // move to the saved position
293 repr->setPosition(pos > 0 ? pos : 0);
295 /* Buglet: We don't re-add the (new version of the) object to the selection of any other
296 * desktops where it was previously selected. */
297 selection->add(repr);
298 Inkscape::GC::release(repr);
299 }
301 if (interactive) {
302 if (did) {
303 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_TO_CURVE,
304 _("Object to path"));
305 } else {
306 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No objects</b> to convert to path in the selection."));
307 return;
308 }
309 }
310 }
312 static Inkscape::XML::Node *
313 sp_selected_item_to_curved_repr(SPItem *item, guint32 text_grouping_policy)
314 {
315 if (!item)
316 return NULL;
318 SPCurve *curve = NULL;
319 if (SP_IS_SHAPE(item)) {
320 curve = sp_shape_get_curve(SP_SHAPE(item));
321 } else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
322 curve = te_get_layout(item)->convertToCurves();
323 }
325 if (!curve)
326 return NULL;
328 Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(item)->document();
329 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
330 /* Transformation */
331 repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
332 /* Style */
333 gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
334 SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
335 repr->setAttribute("style", style_str);
336 g_free(style_str);
338 /* Mask */
339 gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
340 if ( mask_str )
341 repr->setAttribute("mask", mask_str);
343 /* Clip path */
344 gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
345 if ( clip_path_str )
346 repr->setAttribute("clip-path", clip_path_str);
348 /* Rotation center */
349 sp_repr_set_attr(repr, "inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"));
350 sp_repr_set_attr(repr, "inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"));
352 /* Definition */
353 gchar *def_str = sp_svg_write_path(SP_CURVE_BPATH(curve));
354 repr->setAttribute("d", def_str);
355 g_free(def_str);
356 sp_curve_unref(curve);
357 return repr;
358 }
360 void
361 sp_selected_path_reverse()
362 {
363 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
365 Inkscape::Selection *selection = sp_desktop_selection(desktop);
366 GSList *items = (GSList *) selection->itemList();
368 if (!items) {
369 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to reverse."));
370 return;
371 }
374 bool did = false;
375 for (GSList *i = items; i != NULL; i = i->next) {
377 if (!SP_IS_SHAPE(i->data))
378 continue;
380 did = true;
381 SPShape *shape = SP_SHAPE(i->data);
383 SPCurve *rcurve = sp_curve_reverse(shape->curve);
385 gchar *str = sp_svg_write_path(SP_CURVE_BPATH(rcurve));
386 SP_OBJECT_REPR(shape)->setAttribute("d", str);
387 g_free(str);
389 sp_curve_unref(rcurve);
390 }
392 if (did) {
393 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_REVERSE,
394 _("Reverse path"));
395 } else {
396 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to reverse in the selection."));
397 }
398 }
400 /*
401 Local Variables:
402 mode:c++
403 c-file-style:"stroustrup"
404 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
405 indent-tabs-mode:nil
406 fill-column:99
407 End:
408 */
409 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :