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 "document.h"
32 #include "message-stack.h"
33 #include "selection.h"
34 #include "desktop-handles.h"
36 /* Helper functions for sp_selected_path_to_curves */
37 static void sp_selected_path_to_curves0(gboolean do_document_done, guint32 text_grouping_policy);
38 static Inkscape::XML::Node *sp_selected_item_to_curved_repr(SPItem *item, guint32 text_grouping_policy);
39 enum {
40 /* Not used yet. This is the placeholder of Lauris's idea. */
41 SP_TOCURVE_INTERACTIVE = 1 << 0,
42 SP_TOCURVE_GROUPING_BY_WORD = 1 << 1,
43 SP_TOCURVE_GROUPING_BY_LINE = 1 << 2,
44 SP_TOCURVE_GROUPING_BY_WHOLE = 1 << 3
45 };
47 void
48 sp_selected_path_combine(void)
49 {
50 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
52 Inkscape::Selection *selection = sp_desktop_selection(desktop);
53 GSList *items = (GSList *) selection->itemList();
55 if (g_slist_length(items) < 2) {
56 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>at least two objects</b> to combine."));
57 return;
58 }
60 for (GSList *i = items; i != NULL; i = i->next) {
61 SPItem *item = (SPItem *) i->data;
62 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item)) {
63 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("At least one of the objects is <b>not a path</b>, cannot combine."));
64 return;
65 }
66 }
68 Inkscape::XML::Node *parent = SP_OBJECT_REPR((SPItem *) items->data)->parent();
69 for (GSList *i = items; i != NULL; i = i->next) {
70 if ( SP_OBJECT_REPR((SPItem *) i->data)->parent() != parent ) {
71 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("You cannot combine objects from <b>different groups</b> or <b>layers</b>."));
72 return;
73 }
74 }
76 sp_selected_path_to_curves0(FALSE, 0);
78 items = (GSList *) selection->itemList();
80 items = g_slist_copy(items);
82 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
84 // remember the position of the topmost object
85 gint topmost = (SP_OBJECT_REPR((SPItem *) g_slist_last(items)->data))->position();
87 // remember the id of the bottomost object
88 char const *id = SP_OBJECT_REPR((SPItem *) items->data)->attribute("id");
90 // FIXME: merge styles of combined objects instead of using the first one's style
91 gchar *style = g_strdup(SP_OBJECT_REPR((SPItem *) items->data)->attribute("style"));
93 GString *dstring = g_string_new("");
94 for (GSList *i = items; i != NULL; i = i->next) {
96 SPPath *path = (SPPath *) i->data;
97 SPCurve *c = sp_shape_get_curve(SP_SHAPE(path));
99 NArtBpath *abp = nr_artpath_affine(SP_CURVE_BPATH(c), SP_ITEM(path)->transform);
100 sp_curve_unref(c);
101 gchar *str = sp_svg_write_path(abp);
102 g_free(abp);
104 dstring = g_string_append(dstring, str);
105 g_free(str);
107 // if this is the bottommost object,
108 if (!strcmp(SP_OBJECT_REPR(path)->attribute("id"), id)) {
109 // delete it so that its clones don't get alerted; this object will be restored shortly, with the same id
110 SP_OBJECT(path)->deleteObject(false);
111 } else {
112 // delete the object for real, so that its clones can take appropriate action
113 SP_OBJECT(path)->deleteObject();
114 }
116 topmost--;
117 }
119 g_slist_free(items);
121 Inkscape::XML::Node *repr = sp_repr_new("svg:path");
123 // restore id
124 repr->setAttribute("id", id);
126 repr->setAttribute("style", style);
127 g_free(style);
129 repr->setAttribute("d", dstring->str);
130 g_string_free(dstring, TRUE);
132 // add the new group to the group members' common parent
133 parent->appendChild(repr);
135 // move to the position of the topmost, reduced by the number of deleted items
136 repr->setPosition(topmost > 0 ? topmost + 1 : 0);
138 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_COMBINE,
139 _("Combine"));
141 selection->set(repr);
143 Inkscape::GC::release(repr);
144 }
146 void
147 sp_selected_path_break_apart(void)
148 {
149 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
151 Inkscape::Selection *selection = sp_desktop_selection(desktop);
153 if (selection->isEmpty()) {
154 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to break apart."));
155 return;
156 }
158 bool did = false;
160 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
161 items != NULL;
162 items = items->next) {
164 SPItem *item = (SPItem *) items->data;
166 if (!SP_IS_PATH(item))
167 continue;
169 SPPath *path = SP_PATH(item);
171 SPCurve *curve = sp_shape_get_curve(SP_SHAPE(path));
172 if (curve == NULL)
173 continue;
175 did = true;
177 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
178 gint pos = SP_OBJECT_REPR(item)->position();
179 char const *id = SP_OBJECT_REPR(item)->attribute("id");
181 gchar *style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
183 NArtBpath *abp = nr_artpath_affine(SP_CURVE_BPATH(curve), (SP_ITEM(path))->transform);
185 sp_curve_unref(curve);
187 // it's going to resurrect as one of the pieces, so we delete without advertisement
188 SP_OBJECT(item)->deleteObject(false);
190 curve = sp_curve_new_from_bpath(abp);
191 g_assert(curve != NULL);
193 GSList *list = sp_curve_split(curve);
195 sp_curve_unref(curve);
197 GSList *reprs = NULL;
198 for (GSList *l = list; l != NULL; l = l->next) {
199 curve = (SPCurve *) l->data;
201 Inkscape::XML::Node *repr = sp_repr_new("svg:path");
202 repr->setAttribute("style", style);
204 gchar *str = sp_svg_write_path(SP_CURVE_BPATH(curve));
205 repr->setAttribute("d", str);
206 g_free(str);
208 // add the new repr to the parent
209 parent->appendChild(repr);
211 // move to the saved position
212 repr->setPosition(pos > 0 ? pos : 0);
214 // if it's the first one, restore id
215 if (l == list)
216 repr->setAttribute("id", id);
218 reprs = g_slist_prepend (reprs, repr);
220 Inkscape::GC::release(repr);
221 }
223 selection->setReprList(reprs);
225 g_slist_free(reprs);
226 g_slist_free(list);
227 g_free(style);
229 }
231 if (did) {
232 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_BREAK_APART,
233 _("Break Apart"));
234 } else {
235 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to break apart in the selection."));
236 return;
237 }
238 }
240 /* This function is an entry point from GUI */
241 void
242 sp_selected_path_to_curves(void)
243 {
244 sp_selected_path_to_curves0(TRUE, SP_TOCURVE_INTERACTIVE);
245 }
247 static void
248 sp_selected_path_to_curves0(gboolean interactive, guint32 text_grouping_policy)
249 {
250 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
252 Inkscape::Selection *selection = sp_desktop_selection(desktop);
254 if (selection->isEmpty()) {
255 if (interactive)
256 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to path."));
257 return;
258 }
260 bool did = false;
262 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
263 items != NULL;
264 items = items->next) {
266 SPItem *item = SP_ITEM(items->data);
268 Inkscape::XML::Node *repr = sp_selected_item_to_curved_repr(item, 0);
269 if (!repr)
270 continue;
272 did = true;
274 // remember the position of the item
275 gint pos = SP_OBJECT_REPR(item)->position();
276 // remember parent
277 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
278 // remember id
279 char const *id = SP_OBJECT_REPR(item)->attribute("id");
281 selection->remove(item);
283 // it's going to resurrect, so we delete without advertisement
284 SP_OBJECT(item)->deleteObject(false);
286 // restore id
287 repr->setAttribute("id", id);
288 // add the new repr to the parent
289 parent->appendChild(repr);
290 // move to the saved position
291 repr->setPosition(pos > 0 ? pos : 0);
293 selection->add(repr);
294 Inkscape::GC::release(repr);
295 }
297 if (interactive) {
298 if (did) {
299 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_TO_CURVE,
300 _("Object to Path"));
301 } else {
302 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No objects</b> to convert to path in the selection."));
303 return;
304 }
305 }
306 }
308 static Inkscape::XML::Node *
309 sp_selected_item_to_curved_repr(SPItem *item, guint32 text_grouping_policy)
310 {
311 if (!item)
312 return NULL;
314 SPCurve *curve = NULL;
315 if (SP_IS_SHAPE(item)) {
316 curve = sp_shape_get_curve(SP_SHAPE(item));
317 } else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
318 curve = te_get_layout(item)->convertToCurves();
319 }
321 if (!curve)
322 return NULL;
324 Inkscape::XML::Node *repr = sp_repr_new("svg:path");
325 /* Transformation */
326 repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
327 /* Style */
328 gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
329 SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
330 repr->setAttribute("style", style_str);
331 g_free(style_str);
332 /* Rotation center */
333 sp_repr_set_attr(repr, "inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"));
334 sp_repr_set_attr(repr, "inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"));
336 /* Definition */
337 gchar *def_str = sp_svg_write_path(SP_CURVE_BPATH(curve));
338 repr->setAttribute("d", def_str);
339 g_free(def_str);
340 sp_curve_unref(curve);
341 return repr;
342 }
344 void
345 sp_selected_path_reverse()
346 {
347 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
349 Inkscape::Selection *selection = sp_desktop_selection(desktop);
350 GSList *items = (GSList *) selection->itemList();
352 if (!items) {
353 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to reverse."));
354 return;
355 }
358 bool did = false;
359 for (GSList *i = items; i != NULL; i = i->next) {
361 if (!SP_IS_SHAPE(i->data))
362 continue;
364 did = true;
365 SPShape *shape = SP_SHAPE(i->data);
367 SPCurve *rcurve = sp_curve_reverse(shape->curve);
369 gchar *str = sp_svg_write_path(SP_CURVE_BPATH(rcurve));
370 SP_OBJECT_REPR(shape)->setAttribute("d", str);
371 g_free(str);
373 sp_curve_unref(rcurve);
374 }
376 if (did) {
377 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_REVERSE,
378 /* TODO: annotate */ "path-chemistry.cpp:378");
379 } else {
380 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to reverse in the selection."));
381 }
382 }
384 /*
385 Local Variables:
386 mode:c++
387 c-file-style:"stroustrup"
388 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
389 indent-tabs-mode:nil
390 fill-column:99
391 End:
392 */
393 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :