1 #define __SP_SELECTION_CHEMISTRY_C__
3 /*
4 * Miscellanous operations on selected items
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * Frank Felfe <innerspace@iname.com>
9 * MenTaLguY <mental@rydia.net>
10 * bulia byak <buliabyak@users.sf.net>
11 * Andrius R. <knutux@gmail.com>
12 *
13 * Copyright (C) 1999-2006 authors
14 * Copyright (C) 2001-2002 Ximian, Inc.
15 *
16 * Released under GNU GPL, read the file 'COPYING' for more information
17 */
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
23 #include <gtkmm/clipboard.h>
25 #include "svg/svg.h"
26 #include "inkscape.h"
27 #include "desktop.h"
28 #include "desktop-style.h"
29 #include "selection.h"
30 #include "tools-switch.h"
31 #include "desktop-handles.h"
32 #include "message-stack.h"
33 #include "sp-item-transform.h"
34 #include "sp-marker.h"
35 #include "sp-use.h"
36 #include "sp-textpath.h"
37 #include "sp-tspan.h"
38 #include "sp-flowtext.h"
39 #include "sp-flowregion.h"
40 #include "text-editing.h"
41 #include "text-context.h"
42 #include "connector-context.h"
43 #include "sp-path.h"
44 #include "sp-conn-end.h"
45 #include "dropper-context.h"
46 #include <glibmm/i18n.h>
47 #include "libnr/nr-matrix-rotate-ops.h"
48 #include "libnr/nr-matrix-translate-ops.h"
49 #include "libnr/nr-rotate-fns.h"
50 #include "libnr/nr-scale-ops.h"
51 #include "libnr/nr-scale-translate-ops.h"
52 #include "libnr/nr-translate-matrix-ops.h"
53 #include "libnr/nr-translate-scale-ops.h"
54 #include "xml/repr.h"
55 #include "style.h"
56 #include "document-private.h"
57 #include "sp-gradient.h"
58 #include "sp-gradient-reference.h"
59 #include "sp-linear-gradient-fns.h"
60 #include "sp-pattern.h"
61 #include "sp-radial-gradient-fns.h"
62 #include "sp-namedview.h"
63 #include "prefs-utils.h"
64 #include "sp-offset.h"
65 #include "sp-clippath.h"
66 #include "sp-mask.h"
67 #include "file.h"
68 #include "layer-fns.h"
69 #include "context-fns.h"
70 #include <map>
71 #include "helper/units.h"
72 #include "sp-item.h"
73 using NR::X;
74 using NR::Y;
76 #include "selection-chemistry.h"
78 /* fixme: find a better place */
79 GSList *clipboard = NULL;
80 GSList *defs_clipboard = NULL;
81 SPCSSAttr *style_clipboard = NULL;
82 NR::Rect size_clipboard(NR::Point(0,0), NR::Point(0,0));
84 static void sp_copy_stuff_used_by_item(GSList **defs_clip, SPItem *item, const GSList *items);
86 void sp_selection_copy_one (Inkscape::XML::Node *repr, NR::Matrix full_t, GSList **clip)
87 {
88 Inkscape::XML::Node *copy = repr->duplicate();
90 // copy complete inherited style
91 SPCSSAttr *css = sp_repr_css_attr_inherited(repr, "style");
92 sp_repr_css_set(copy, css, "style");
93 sp_repr_css_attr_unref(css);
95 // write the complete accummulated transform passed to us
96 // (we're dealing with unattached repr, so we write to its attr instead of using sp_item_set_transform)
97 gchar affinestr[80];
98 if (sp_svg_transform_write(affinestr, 79, full_t)) {
99 copy->setAttribute("transform", affinestr);
100 } else {
101 copy->setAttribute("transform", NULL);
102 }
104 *clip = g_slist_prepend(*clip, copy);
105 }
107 void sp_selection_copy_impl (const GSList *items, GSList **clip, GSList **defs_clip, SPCSSAttr **style_clip)
108 {
110 // Copy stuff referenced by all items to defs_clip:
111 if (defs_clip) {
112 for (GSList *i = (GSList *) items; i != NULL; i = i->next) {
113 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (i->data), items);
114 }
115 *defs_clip = g_slist_reverse(*defs_clip);
116 }
118 // Store style:
119 if (style_clip) {
120 SPItem *item = SP_ITEM (items->data); // take from the first selected item
121 *style_clip = take_style_from_item (item);
122 }
124 if (clip) {
125 // Sort items:
126 GSList *sorted_items = g_slist_copy ((GSList *) items);
127 sorted_items = g_slist_sort((GSList *) sorted_items, (GCompareFunc) sp_object_compare_position);
129 // Copy item reprs:
130 for (GSList *i = (GSList *) sorted_items; i != NULL; i = i->next) {
131 sp_selection_copy_one (SP_OBJECT_REPR (i->data), sp_item_i2doc_affine(SP_ITEM (i->data)), clip);
132 }
134 *clip = g_slist_reverse(*clip);
135 g_slist_free ((GSList *) sorted_items);
136 }
137 }
139 /**
140 Add gradients/patterns/markers referenced by copied objects to defs
141 */
142 void
143 paste_defs (GSList **defs_clip, SPDocument *doc)
144 {
145 if (!defs_clip)
146 return;
148 for (GSList *gl = *defs_clip; gl != NULL; gl = gl->next) {
149 SPDefs *defs= (SPDefs *) SP_DOCUMENT_DEFS(doc);
150 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) gl->data;
151 gchar const *id = repr->attribute("id");
152 if (!id || !doc->getObjectById(id)) {
153 Inkscape::XML::Node *copy = repr->duplicate();
154 SP_OBJECT_REPR(defs)->addChild(copy, NULL);
155 Inkscape::GC::release(copy);
156 }
157 }
158 }
160 GSList *sp_selection_paste_impl (SPDocument *document, SPObject *parent, GSList **clip, GSList **defs_clip)
161 {
162 paste_defs (defs_clip, document);
164 GSList *copied = NULL;
165 // add objects to document
166 for (GSList *l = *clip; l != NULL; l = l->next) {
167 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
168 Inkscape::XML::Node *copy = repr->duplicate();
170 // premultiply the item transform by the accumulated parent transform in the paste layer
171 NR::Matrix local = sp_item_i2doc_affine(SP_ITEM(parent));
172 if (!local.test_identity()) {
173 gchar const *t_str = copy->attribute("transform");
174 NR::Matrix item_t (NR::identity());
175 if (t_str)
176 sp_svg_transform_read(t_str, &item_t);
177 item_t *= local.inverse();
178 // (we're dealing with unattached repr, so we write to its attr instead of using sp_item_set_transform)
179 gchar affinestr[80];
180 if (sp_svg_transform_write(affinestr, 79, item_t)) {
181 copy->setAttribute("transform", affinestr);
182 } else {
183 copy->setAttribute("transform", NULL);
184 }
185 }
187 parent->appendChildRepr(copy);
188 copied = g_slist_prepend(copied, copy);
189 Inkscape::GC::release(copy);
190 }
191 return copied;
192 }
194 void sp_selection_delete_impl(const GSList *items)
195 {
196 for (const GSList *i = items ; i ; i = i->next ) {
197 sp_object_ref((SPObject *)i->data, NULL);
198 }
199 for (const GSList *i = items; i != NULL; i = i->next) {
200 SPItem *item = (SPItem *) i->data;
201 SP_OBJECT(item)->deleteObject();
202 sp_object_unref((SPObject *)item, NULL);
203 }
204 }
207 void sp_selection_delete()
208 {
209 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
210 if (desktop == NULL) {
211 return;
212 }
214 if (tools_isactive (desktop, TOOLS_TEXT))
215 if (sp_text_delete_selection(desktop->event_context)) {
216 sp_document_done(sp_desktop_document(desktop));
217 return;
218 }
220 Inkscape::Selection *selection = sp_desktop_selection(desktop);
222 // check if something is selected
223 if (selection->isEmpty()) {
224 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Nothing</b> was deleted."));
225 return;
226 }
228 const GSList *selected = g_slist_copy(const_cast<GSList *>(selection->itemList()));
229 selection->clear();
230 sp_selection_delete_impl (selected);
231 g_slist_free ((GSList *) selected);
233 /* a tool may have set up private information in it's selection context
234 * that depends on desktop items. I think the only sane way to deal with
235 * this currently is to reset the current tool, which will reset it's
236 * associated selection context. For example: deleting an object
237 * while moving it around the canvas.
238 */
239 tools_switch ( desktop, tools_active ( desktop ) );
241 sp_document_done(sp_desktop_document(desktop));
242 }
244 /* fixme: sequencing */
245 void sp_selection_duplicate()
246 {
247 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
248 if (desktop == NULL)
249 return;
251 Inkscape::Selection *selection = sp_desktop_selection(desktop);
253 // check if something is selected
254 if (selection->isEmpty()) {
255 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to duplicate."));
256 return;
257 }
259 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
261 selection->clear();
263 // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
264 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
266 GSList *newsel = NULL;
268 while (reprs) {
269 Inkscape::XML::Node *parent = ((Inkscape::XML::Node *) reprs->data)->parent();
270 Inkscape::XML::Node *copy = ((Inkscape::XML::Node *) reprs->data)->duplicate();
272 parent->appendChild(copy);
274 newsel = g_slist_prepend(newsel, copy);
275 reprs = g_slist_remove(reprs, reprs->data);
276 Inkscape::GC::release(copy);
277 }
279 sp_document_done(sp_desktop_document(desktop));
281 selection->setReprList(newsel);
283 g_slist_free(newsel);
284 }
286 void sp_edit_clear_all()
287 {
288 SPDesktop *dt = SP_ACTIVE_DESKTOP;
289 if (!dt)
290 return;
292 SPDocument *doc = sp_desktop_document(dt);
293 sp_desktop_selection(dt)->clear();
295 g_return_if_fail(SP_IS_GROUP(dt->currentLayer()));
296 GSList *items = sp_item_group_item_list(SP_GROUP(dt->currentLayer()));
298 while (items) {
299 SP_OBJECT (items->data)->deleteObject();
300 items = g_slist_remove(items, items->data);
301 }
303 sp_document_done(doc);
304 }
306 GSList *
307 get_all_items (GSList *list, SPObject *from, SPDesktop *desktop, bool onlyvisible, bool onlysensitive, const GSList *exclude)
308 {
309 for (SPObject *child = sp_object_first_child(SP_OBJECT(from)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
310 if (SP_IS_ITEM(child) &&
311 !desktop->isLayer(SP_ITEM(child)) &&
312 (!onlysensitive || !SP_ITEM(child)->isLocked()) &&
313 (!onlyvisible || !desktop->itemIsHidden(SP_ITEM(child))) &&
314 (!exclude || !g_slist_find ((GSList *) exclude, child))
315 )
316 {
317 list = g_slist_prepend (list, SP_ITEM(child));
318 }
320 if (SP_IS_ITEM(child) && desktop->isLayer(SP_ITEM(child))) {
321 list = get_all_items (list, child, desktop, onlyvisible, onlysensitive, exclude);
322 }
323 }
325 return list;
326 }
328 void sp_edit_select_all_full (bool force_all_layers, bool invert)
329 {
330 SPDesktop *dt = SP_ACTIVE_DESKTOP;
331 if (!dt)
332 return;
334 Inkscape::Selection *selection = sp_desktop_selection(dt);
336 g_return_if_fail(SP_IS_GROUP(dt->currentLayer()));
338 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
339 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
340 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
342 GSList *items = NULL;
344 const GSList *exclude = NULL;
345 if (invert) {
346 exclude = selection->itemList();
347 }
349 if (force_all_layers)
350 inlayer = PREFS_SELECTION_ALL;
352 switch (inlayer) {
353 case PREFS_SELECTION_LAYER: {
354 if ( (onlysensitive && SP_ITEM(dt->currentLayer())->isLocked()) ||
355 (onlyvisible && dt->itemIsHidden(SP_ITEM(dt->currentLayer()))) )
356 return;
358 GSList *all_items = sp_item_group_item_list(SP_GROUP(dt->currentLayer()));
360 for (GSList *i = all_items; i; i = i->next) {
361 SPItem *item = SP_ITEM (i->data);
363 if (item && (!onlysensitive || !item->isLocked())) {
364 if (!onlyvisible || !dt->itemIsHidden(item)) {
365 if (!dt->isLayer(item)) {
366 if (!invert || !g_slist_find ((GSList *) exclude, item)) {
367 items = g_slist_prepend (items, item); // leave it in the list
368 }
369 }
370 }
371 }
372 }
374 g_slist_free (all_items);
375 break;
376 }
377 case PREFS_SELECTION_LAYER_RECURSIVE: {
378 items = get_all_items (NULL, dt->currentLayer(), dt, onlyvisible, onlysensitive, exclude);
379 break;
380 }
381 default: {
382 items = get_all_items (NULL, dt->currentRoot(), dt, onlyvisible, onlysensitive, exclude);
383 break;
384 }
385 }
387 selection->setList (items);
389 if (items) {
390 g_slist_free (items);
391 }
392 }
394 void sp_edit_select_all ()
395 {
396 sp_edit_select_all_full (false, false);
397 }
399 void sp_edit_select_all_in_all_layers ()
400 {
401 sp_edit_select_all_full (true, false);
402 }
404 void sp_edit_invert ()
405 {
406 sp_edit_select_all_full (false, true);
407 }
409 void sp_edit_invert_in_all_layers ()
410 {
411 sp_edit_select_all_full (true, true);
412 }
414 void sp_selection_group()
415 {
416 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
417 if (desktop == NULL)
418 return;
420 SPDocument *document = sp_desktop_document (desktop);
422 Inkscape::Selection *selection = sp_desktop_selection(desktop);
424 // Check if something is selected.
425 if (selection->isEmpty()) {
426 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>two or more objects</b> to group."));
427 return;
428 }
430 GSList const *l = (GSList *) selection->reprList();
432 // Check if at least two objects are selected.
433 if (l->next == NULL) {
434 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>at least two objects</b> to group."));
435 return;
436 }
438 GSList *p = g_slist_copy((GSList *) l);
440 selection->clear();
442 p = g_slist_sort(p, (GCompareFunc) sp_repr_compare_position);
444 // Remember the position and parent of the topmost object.
445 gint topmost = ((Inkscape::XML::Node *) g_slist_last(p)->data)->position();
446 Inkscape::XML::Node *topmost_parent = ((Inkscape::XML::Node *) g_slist_last(p)->data)->parent();
448 Inkscape::XML::Node *group = sp_repr_new("svg:g");
450 while (p) {
451 Inkscape::XML::Node *current = (Inkscape::XML::Node *) p->data;
453 if (current->parent() == topmost_parent) {
454 Inkscape::XML::Node *spnew = current->duplicate();
455 sp_repr_unparent(current);
456 group->appendChild(spnew);
457 Inkscape::GC::release(spnew);
458 topmost --; // only reduce count for those items deleted from topmost_parent
459 } else { // move it to topmost_parent first
460 GSList *temp_clip = NULL;
462 // At this point, current may already have no item, due to its being a clone whose original is already moved away
463 // So we copy it artificially calculating the transform from its repr->attr("transform") and the parent transform
464 gchar const *t_str = current->attribute("transform");
465 NR::Matrix item_t (NR::identity());
466 if (t_str)
467 sp_svg_transform_read(t_str, &item_t);
468 item_t *= sp_item_i2doc_affine(SP_ITEM(document->getObjectByRepr(current->parent())));
469 //FIXME: when moving both clone and original from a transformed group (either by
470 //grouping into another parent, or by cut/paste) the transform from the original's
471 //parent becomes embedded into original itself, and this affects its clones. Fix
472 //this by remembering the transform diffs we write to each item into an array and
473 //then, if this is clone, looking up its original in that array and pre-multiplying
474 //it by the inverse of that original's transform diff.
476 sp_selection_copy_one (current, item_t, &temp_clip);
477 sp_repr_unparent(current);
479 // paste into topmost_parent (temporarily)
480 GSList *copied = sp_selection_paste_impl (document, document->getObjectByRepr(topmost_parent), &temp_clip, NULL);
481 if (temp_clip) g_slist_free (temp_clip);
482 if (copied) { // if success,
483 // take pasted object (now in topmost_parent)
484 Inkscape::XML::Node *in_topmost = (Inkscape::XML::Node *) copied->data;
485 // make a copy
486 Inkscape::XML::Node *spnew = in_topmost->duplicate();
487 // remove pasted
488 sp_repr_unparent(in_topmost);
489 // put its copy into group
490 group->appendChild(spnew);
491 Inkscape::GC::release(spnew);
492 g_slist_free (copied);
493 }
494 }
495 p = g_slist_remove(p, current);
496 }
498 // Add the new group to the topmost members' parent
499 topmost_parent->appendChild(group);
501 // Move to the position of the topmost, reduced by the number of items deleted from topmost_parent
502 group->setPosition(topmost + 1);
504 sp_document_done(sp_desktop_document(desktop));
506 selection->set(group);
507 Inkscape::GC::release(group);
508 }
510 void sp_selection_ungroup()
511 {
512 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
513 if (desktop == NULL)
514 return;
516 Inkscape::Selection *selection = sp_desktop_selection(desktop);
518 if (selection->isEmpty()) {
519 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>group</b> to ungroup."));
520 return;
521 }
523 GSList *items = g_slist_copy((GSList *) selection->itemList());
524 selection->clear();
526 // Get a copy of current selection.
527 GSList *new_select = NULL;
528 bool ungrouped = false;
529 for (GSList *i = items;
530 i != NULL;
531 i = i->next)
532 {
533 SPItem *group = (SPItem *) i->data;
535 // when ungrouping cloned groups with their originals, some objects that were selected may no more exist due to unlinking
536 if (!SP_IS_OBJECT(group)) {
537 continue;
538 }
540 /* We do not allow ungrouping <svg> etc. (lauris) */
541 if (strcmp(SP_OBJECT_REPR(group)->name(), "svg:g") && strcmp(SP_OBJECT_REPR(group)->name(), "svg:switch")) {
542 // keep the non-group item in the new selection
543 selection->add(group);
544 continue;
545 }
547 GSList *children = NULL;
548 /* This is not strictly required, but is nicer to rely on group ::destroy (lauris) */
549 sp_item_group_ungroup(SP_GROUP(group), &children, false);
550 ungrouped = true;
551 // Add ungrouped items to the new selection.
552 new_select = g_slist_concat(new_select, children);
553 }
555 if (new_select) { // Set new selection.
556 selection->addList(new_select);
557 g_slist_free(new_select);
558 }
559 if (!ungrouped) {
560 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No groups</b> to ungroup in the selection."));
561 }
563 g_slist_free(items);
565 sp_document_done(sp_desktop_document(desktop));
566 }
568 static SPGroup *
569 sp_item_list_common_parent_group(const GSList *items)
570 {
571 if (!items) {
572 return NULL;
573 }
574 SPObject *parent = SP_OBJECT_PARENT(items->data);
575 /* Strictly speaking this CAN happen, if user selects <svg> from XML editor */
576 if (!SP_IS_GROUP(parent)) {
577 return NULL;
578 }
579 for (items = items->next; items; items = items->next) {
580 if (SP_OBJECT_PARENT(items->data) != parent) {
581 return NULL;
582 }
583 }
585 return SP_GROUP(parent);
586 }
588 /** Finds out the minimum common bbox of the selected items
589 */
590 NR::Rect
591 enclose_items(const GSList *items)
592 {
593 g_assert(items != NULL);
595 NR::Rect r = sp_item_bbox_desktop((SPItem *) items->data);
597 for (GSList *i = items->next; i; i = i->next) {
598 r = NR::Rect::union_bounds(r, sp_item_bbox_desktop((SPItem *) i->data));
599 }
601 return r;
602 }
604 SPObject *
605 prev_sibling(SPObject *child)
606 {
607 SPObject *parent = SP_OBJECT_PARENT(child);
608 if (!SP_IS_GROUP(parent)) {
609 return NULL;
610 }
611 for ( SPObject *i = sp_object_first_child(parent) ; i; i = SP_OBJECT_NEXT(i) ) {
612 if (i->next == child)
613 return i;
614 }
615 return NULL;
616 }
618 void
619 sp_selection_raise()
620 {
621 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
622 if (!desktop)
623 return;
625 Inkscape::Selection *selection = sp_desktop_selection(desktop);
627 GSList const *items = (GSList *) selection->itemList();
628 if (!items) {
629 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise."));
630 return;
631 }
633 SPGroup const *group = sp_item_list_common_parent_group(items);
634 if (!group) {
635 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
636 return;
637 }
639 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
641 /* construct reverse-ordered list of selected children */
642 GSList *rev = g_slist_copy((GSList *) items);
643 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
645 // find out the common bbox of the selected items
646 NR::Rect selected = enclose_items(items);
648 // for all objects in the selection (starting from top)
649 while (rev) {
650 SPObject *child = SP_OBJECT(rev->data);
651 // for each selected object, find the next sibling
652 for (SPObject *newref = child->next; newref; newref = newref->next) {
653 // if the sibling is an item AND overlaps our selection,
654 if (SP_IS_ITEM(newref) && selected.intersects(sp_item_bbox_desktop(SP_ITEM(newref)))) {
655 // AND if it's not one of our selected objects,
656 if (!g_slist_find((GSList *) items, newref)) {
657 // move the selected object after that sibling
658 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(newref));
659 }
660 break;
661 }
662 }
663 rev = g_slist_remove(rev, child);
664 }
666 sp_document_done(sp_desktop_document(desktop));
667 }
669 void sp_selection_raise_to_top()
670 {
671 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
672 if (desktop == NULL)
673 return;
675 SPDocument *document = sp_desktop_document(desktop);
676 Inkscape::Selection *selection = sp_desktop_selection(desktop);
678 if (selection->isEmpty()) {
679 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise to top."));
680 return;
681 }
683 GSList const *items = (GSList *) selection->itemList();
685 SPGroup const *group = sp_item_list_common_parent_group(items);
686 if (!group) {
687 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
688 return;
689 }
691 GSList *rl = g_slist_copy((GSList *) selection->reprList());
692 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
694 for (GSList *l = rl; l != NULL; l = l->next) {
695 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
696 repr->setPosition(-1);
697 }
699 g_slist_free(rl);
701 sp_document_done(document);
702 }
704 void
705 sp_selection_lower()
706 {
707 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
708 if (desktop == NULL)
709 return;
711 Inkscape::Selection *selection = sp_desktop_selection(desktop);
713 GSList const *items = (GSList *) selection->itemList();
714 if (!items) {
715 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower."));
716 return;
717 }
719 SPGroup const *group = sp_item_list_common_parent_group(items);
720 if (!group) {
721 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
722 return;
723 }
725 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
727 // find out the common bbox of the selected items
728 NR::Rect selected = enclose_items(items);
730 /* construct direct-ordered list of selected children */
731 GSList *rev = g_slist_copy((GSList *) items);
732 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
733 rev = g_slist_reverse(rev);
735 // for all objects in the selection (starting from top)
736 while (rev) {
737 SPObject *child = SP_OBJECT(rev->data);
738 // for each selected object, find the prev sibling
739 for (SPObject *newref = prev_sibling(child); newref; newref = prev_sibling(newref)) {
740 // if the sibling is an item AND overlaps our selection,
741 if (SP_IS_ITEM(newref) && selected.intersects(sp_item_bbox_desktop(SP_ITEM(newref)))) {
742 // AND if it's not one of our selected objects,
743 if (!g_slist_find((GSList *) items, newref)) {
744 // move the selected object before that sibling
745 SPObject *put_after = prev_sibling(newref);
746 if (put_after)
747 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(put_after));
748 else
749 SP_OBJECT_REPR(child)->setPosition(0);
750 }
751 break;
752 }
753 }
754 rev = g_slist_remove(rev, child);
755 }
757 sp_document_done(sp_desktop_document(desktop));
759 }
761 void sp_selection_lower_to_bottom()
762 {
763 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
764 if (desktop == NULL)
765 return;
767 SPDocument *document = sp_desktop_document(desktop);
768 Inkscape::Selection *selection = sp_desktop_selection(desktop);
770 if (selection->isEmpty()) {
771 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower to bottom."));
772 return;
773 }
775 GSList const *items = (GSList *) selection->itemList();
777 SPGroup const *group = sp_item_list_common_parent_group(items);
778 if (!group) {
779 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
780 return;
781 }
783 GSList *rl;
784 rl = g_slist_copy((GSList *) selection->reprList());
785 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
786 rl = g_slist_reverse(rl);
788 for (GSList *l = rl; l != NULL; l = l->next) {
789 gint minpos;
790 SPObject *pp, *pc;
791 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
792 pp = document->getObjectByRepr(sp_repr_parent(repr));
793 minpos = 0;
794 g_assert(SP_IS_GROUP(pp));
795 pc = sp_object_first_child(pp);
796 while (!SP_IS_ITEM(pc)) {
797 minpos += 1;
798 pc = pc->next;
799 }
800 repr->setPosition(minpos);
801 }
803 g_slist_free(rl);
805 sp_document_done(document);
806 }
808 void
809 sp_undo(SPDesktop *desktop, SPDocument *)
810 {
811 if (!sp_document_undo(sp_desktop_document(desktop)))
812 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to undo."));
813 }
815 void
816 sp_redo(SPDesktop *desktop, SPDocument *)
817 {
818 if (!sp_document_redo(sp_desktop_document(desktop)))
819 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to redo."));
820 }
822 void sp_selection_cut()
823 {
824 sp_selection_copy();
825 sp_selection_delete();
826 }
828 void sp_copy_gradient (GSList **defs_clip, SPGradient *gradient)
829 {
830 SPGradient *ref = gradient;
832 while (ref) {
833 // climb up the refs, copying each one in the chain
834 Inkscape::XML::Node *grad_repr = SP_OBJECT_REPR(ref)->duplicate();
835 *defs_clip = g_slist_prepend (*defs_clip, grad_repr);
837 ref = ref->ref->getObject();
838 }
839 }
841 void sp_copy_pattern (GSList **defs_clip, SPPattern *pattern)
842 {
843 SPPattern *ref = pattern;
845 while (ref) {
846 // climb up the refs, copying each one in the chain
847 Inkscape::XML::Node *pattern_repr = SP_OBJECT_REPR(ref)->duplicate();
848 *defs_clip = g_slist_prepend (*defs_clip, pattern_repr);
850 // items in the pattern may also use gradients and other patterns, so we need to recurse here as well
851 for (SPObject *child = sp_object_first_child(SP_OBJECT(ref)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
852 if (!SP_IS_ITEM (child))
853 continue;
854 sp_copy_stuff_used_by_item (defs_clip, (SPItem *) child, NULL);
855 }
857 ref = ref->ref->getObject();
858 }
859 }
861 void sp_copy_single (GSList **defs_clip, SPObject *thing)
862 {
863 Inkscape::XML::Node *duplicate_repr = SP_OBJECT_REPR(thing)->duplicate();
864 *defs_clip = g_slist_prepend (*defs_clip, duplicate_repr);
865 }
868 void sp_copy_textpath_path (GSList **defs_clip, SPTextPath *tp, const GSList *items)
869 {
870 SPItem *path = sp_textpath_get_path_item (tp);
871 if (!path)
872 return;
873 if (items && g_slist_find ((GSList *) items, path)) // do not copy it to defs if it is already in the list of items copied
874 return;
875 Inkscape::XML::Node *repr = SP_OBJECT_REPR(path)->duplicate();
876 *defs_clip = g_slist_prepend (*defs_clip, repr);
877 }
879 void sp_copy_stuff_used_by_item (GSList **defs_clip, SPItem *item, const GSList *items)
880 {
881 SPStyle *style = SP_OBJECT_STYLE (item);
883 if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
884 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
885 if (SP_IS_LINEARGRADIENT (server) || SP_IS_RADIALGRADIENT (server))
886 sp_copy_gradient (defs_clip, SP_GRADIENT(server));
887 if (SP_IS_PATTERN (server))
888 sp_copy_pattern (defs_clip, SP_PATTERN(server));
889 }
891 if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
892 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
893 if (SP_IS_LINEARGRADIENT (server) || SP_IS_RADIALGRADIENT (server))
894 sp_copy_gradient (defs_clip, SP_GRADIENT(server));
895 if (SP_IS_PATTERN (server))
896 sp_copy_pattern (defs_clip, SP_PATTERN(server));
897 }
899 if (SP_IS_SHAPE (item)) {
900 SPShape *shape = SP_SHAPE (item);
901 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
902 if (shape->marker[i]) {
903 sp_copy_single (defs_clip, SP_OBJECT (shape->marker[i]));
904 }
905 }
906 }
908 if (SP_IS_TEXT_TEXTPATH (item)) {
909 sp_copy_textpath_path (defs_clip, SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))), items);
910 }
912 if (item->clip_ref->getObject()) {
913 sp_copy_single (defs_clip, item->clip_ref->getObject());
914 }
916 if (item->mask_ref->getObject()) {
917 SPObject *mask = item->mask_ref->getObject();
918 sp_copy_single (defs_clip, mask);
919 // recurse into the mask for its gradients etc.
920 for (SPObject *o = SP_OBJECT(mask)->children; o != NULL; o = o->next) {
921 if (SP_IS_ITEM(o))
922 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (o), items);
923 }
924 }
926 // recurse
927 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
928 if (SP_IS_ITEM(o))
929 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (o), items);
930 }
931 }
933 /**
934 * \pre item != NULL
935 */
936 SPCSSAttr *
937 take_style_from_item (SPItem *item)
938 {
939 // write the complete cascaded style, context-free
940 SPCSSAttr *css = sp_css_attr_from_object (SP_OBJECT(item), SP_STYLE_FLAG_ALWAYS);
941 if (css == NULL)
942 return NULL;
944 if ((SP_IS_GROUP(item) && SP_OBJECT(item)->children) ||
945 (SP_IS_TEXT (item) && SP_OBJECT(item)->children && SP_OBJECT(item)->children->next == NULL)) {
946 // if this is a text with exactly one tspan child, merge the style of that tspan as well
947 // If this is a group, merge the style of its topmost (last) child with style
948 for (SPObject *last_element = item->lastChild(); last_element != NULL; last_element = SP_OBJECT_PREV (last_element)) {
949 if (SP_OBJECT_STYLE (last_element) != NULL) {
950 SPCSSAttr *temp = sp_css_attr_from_object (last_element, SP_STYLE_FLAG_IFSET);
951 if (temp) {
952 sp_repr_css_merge (css, temp);
953 sp_repr_css_attr_unref (temp);
954 }
955 break;
956 }
957 }
958 }
959 if (!(SP_IS_TEXT (item) || SP_IS_TSPAN (item) || SP_IS_STRING (item))) {
960 // do not copy text properties from non-text objects, it's confusing
961 css = sp_css_attr_unset_text (css);
962 }
964 // FIXME: also transform gradient/pattern fills, by forking? NO, this must be nondestructive
965 double ex = NR::expansion(sp_item_i2doc_affine(item));
966 if (ex != 1.0) {
967 css = sp_css_attr_scale (css, ex);
968 }
970 return css;
971 }
974 void sp_selection_copy()
975 {
976 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
977 if (desktop == NULL)
978 return;
980 Inkscape::Selection *selection = sp_desktop_selection(desktop);
982 if (tools_isactive (desktop, TOOLS_DROPPER)) {
983 sp_dropper_context_copy(desktop->event_context);
984 return; // copied color under cursor, nothing else to do
985 }
987 // check if something is selected
988 if (selection->isEmpty()) {
989 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing was copied."));
990 return;
991 }
993 const GSList *items = g_slist_copy ((GSList *) selection->itemList());
995 // 0. Copy text to system clipboard
996 // FIXME: for non-texts, put serialized XML as text to the clipboard;
997 //for this sp_repr_write_stream needs to be rewritten with iostream instead of FILE
998 Glib::ustring text;
999 if (tools_isactive (desktop, TOOLS_TEXT)) {
1000 text = sp_text_get_selected_text(desktop->event_context);
1001 }
1003 if (text.empty()) {
1004 guint texts = 0;
1005 for (GSList *i = (GSList *) items; i; i = i->next) {
1006 SPItem *item = SP_ITEM (i->data);
1007 if (SP_IS_TEXT (item) || SP_IS_FLOWTEXT(item)) {
1008 if (texts > 0) // if more than one text object is copied, separate them by spaces
1009 text += " ";
1010 gchar *this_text = sp_te_get_string_multiline (item);
1011 if (this_text) {
1012 text += this_text;
1013 g_free(this_text);
1014 }
1015 texts++;
1016 }
1017 }
1018 }
1019 if (!text.empty()) {
1020 Glib::RefPtr<Gtk::Clipboard> refClipboard = Gtk::Clipboard::get();
1021 refClipboard->set_text(text);
1022 }
1024 // clear old defs clipboard
1025 while (defs_clipboard) {
1026 Inkscape::GC::release((Inkscape::XML::Node *) defs_clipboard->data);
1027 defs_clipboard = g_slist_remove (defs_clipboard, defs_clipboard->data);
1028 }
1030 // clear style clipboard
1031 if (style_clipboard) {
1032 sp_repr_css_attr_unref (style_clipboard);
1033 style_clipboard = NULL;
1034 }
1036 //clear main clipboard
1037 while (clipboard) {
1038 Inkscape::GC::release((Inkscape::XML::Node *) clipboard->data);
1039 clipboard = g_slist_remove(clipboard, clipboard->data);
1040 }
1042 sp_selection_copy_impl (items, &clipboard, &defs_clipboard, &style_clipboard);
1044 if (tools_isactive (desktop, TOOLS_TEXT)) { // take style from cursor/text selection, overwriting the style just set by copy_impl
1045 SPStyle *const query = sp_style_new();
1046 if (sp_desktop_query_style_all (desktop, query)) {
1047 SPCSSAttr *css = sp_css_attr_from_style (query, SP_STYLE_FLAG_ALWAYS);
1048 if (css != NULL) {
1049 // clear style clipboard
1050 if (style_clipboard) {
1051 sp_repr_css_attr_unref (style_clipboard);
1052 style_clipboard = NULL;
1053 }
1054 //sp_repr_css_print (css);
1055 style_clipboard = css;
1056 }
1057 }
1058 g_free (query);
1059 }
1061 size_clipboard = selection->bounds();
1063 g_slist_free ((GSList *) items);
1064 }
1066 void sp_selection_paste(bool in_place)
1067 {
1068 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1070 if (desktop == NULL) {
1071 return;
1072 }
1074 SPDocument *document = sp_desktop_document(desktop);
1076 if (Inkscape::have_viable_layer(desktop, desktop->messageStack()) == false) {
1077 return;
1078 }
1080 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1082 if (tools_isactive (desktop, TOOLS_TEXT)) {
1083 if (sp_text_paste_inline(desktop->event_context))
1084 return; // pasted from system clipboard into text, nothing else to do
1085 }
1087 // check if something is in the clipboard
1088 if (clipboard == NULL) {
1089 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1090 return;
1091 }
1093 GSList *copied = sp_selection_paste_impl(document, desktop->currentLayer(), &clipboard, &defs_clipboard);
1094 // add pasted objects to selection
1095 selection->setReprList((GSList const *) copied);
1096 g_slist_free (copied);
1098 if (!in_place) {
1099 sp_document_ensure_up_to_date(document);
1101 NR::Point m( desktop->point() - selection->bounds().midpoint() );
1103 /* Snap the offset of the new item(s) to the grid */
1104 /* FIXME: this gridsnap fiddling is a hack. */
1105 Inkscape::GridSnapper &s = desktop->namedview->snap_manager.grid;
1106 gdouble const curr_gridsnap = s.getDistance();
1107 s.setDistance(NR_HUGE);
1108 m = s.freeSnap(Inkscape::Snapper::SNAP_POINT, m, NULL).getPoint();
1109 s.setDistance(curr_gridsnap);
1110 sp_selection_move_relative(selection, m);
1111 }
1113 sp_document_done(document);
1114 }
1116 void sp_selection_paste_style()
1117 {
1118 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1119 if (desktop == NULL) return;
1121 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1123 // check if something is in the clipboard
1124 if (clipboard == NULL) {
1125 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1126 return;
1127 }
1129 // check if something is selected
1130 if (selection->isEmpty()) {
1131 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste style to."));
1132 return;
1133 }
1135 paste_defs (&defs_clipboard, sp_desktop_document(desktop));
1137 sp_desktop_set_style (desktop, style_clipboard);
1139 sp_document_done(sp_desktop_document (desktop));
1140 }
1142 void sp_selection_paste_size (bool apply_x, bool apply_y)
1143 {
1144 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1145 if (desktop == NULL) return;
1147 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1149 // check if something is in the clipboard
1150 if (size_clipboard.extent(NR::X) < 1e-6 || size_clipboard.extent(NR::Y) < 1e-6) {
1151 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1152 return;
1153 }
1155 // check if something is selected
1156 if (selection->isEmpty()) {
1157 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste size to."));
1158 return;
1159 }
1161 NR::Rect current = selection->bounds();
1162 if (current.extent(NR::X) < 1e-6 || current.extent(NR::Y) < 1e-6) {
1163 return;
1164 }
1166 double scale_x = size_clipboard.extent(NR::X) / current.extent(NR::X);
1167 double scale_y = size_clipboard.extent(NR::Y) / current.extent(NR::Y);
1169 sp_selection_scale_relative (selection, current.midpoint(),
1170 NR::scale(
1171 apply_x? scale_x : (desktop->isToolboxButtonActive ("lock")? scale_y : 1.0),
1172 apply_y? scale_y : (desktop->isToolboxButtonActive ("lock")? scale_x : 1.0)));
1174 sp_document_done(sp_desktop_document (desktop));
1175 }
1177 void sp_selection_paste_size_separately (bool apply_x, bool apply_y)
1178 {
1179 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1180 if (desktop == NULL) return;
1182 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1184 // check if something is in the clipboard
1185 if (size_clipboard.extent(NR::X) < 1e-6 || size_clipboard.extent(NR::Y) < 1e-6) {
1186 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1187 return;
1188 }
1190 // check if something is selected
1191 if (selection->isEmpty()) {
1192 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste size to."));
1193 return;
1194 }
1196 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1197 SPItem *item = SP_ITEM(l->data);
1199 NR::Rect current = sp_item_bbox_desktop(item);
1200 if (current.extent(NR::X) < 1e-6 || current.extent(NR::Y) < 1e-6) {
1201 continue;
1202 }
1204 double scale_x = size_clipboard.extent(NR::X) / current.extent(NR::X);
1205 double scale_y = size_clipboard.extent(NR::Y) / current.extent(NR::Y);
1207 sp_item_scale_rel (item,
1208 NR::scale(
1209 apply_x? scale_x : (desktop->isToolboxButtonActive ("lock")? scale_y : 1.0),
1210 apply_y? scale_y : (desktop->isToolboxButtonActive ("lock")? scale_x : 1.0)));
1212 }
1214 sp_document_done(sp_desktop_document (desktop));
1215 }
1217 void sp_selection_to_next_layer ()
1218 {
1219 SPDesktop *dt = SP_ACTIVE_DESKTOP;
1221 Inkscape::Selection *selection = sp_desktop_selection(dt);
1223 // check if something is selected
1224 if (selection->isEmpty()) {
1225 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer above."));
1226 return;
1227 }
1229 const GSList *items = g_slist_copy ((GSList *) selection->itemList());
1231 bool no_more = false; // Set to true, if no more layers above
1232 SPObject *next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer());
1233 if (next) {
1234 GSList *temp_clip = NULL;
1235 sp_selection_copy_impl (items, &temp_clip, NULL, NULL); // we're in the same doc, so no need to copy defs
1236 sp_selection_delete_impl (items);
1237 next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
1238 GSList *copied;
1239 if(next) {
1240 copied = sp_selection_paste_impl (sp_desktop_document (dt), next, &temp_clip, NULL);
1241 } else {
1242 copied = sp_selection_paste_impl (sp_desktop_document (dt), dt->currentLayer(), &temp_clip, NULL);
1243 no_more = true;
1244 }
1245 selection->setReprList((GSList const *) copied);
1246 g_slist_free (copied);
1247 if (temp_clip) g_slist_free (temp_clip);
1248 if (next) dt->setCurrentLayer(next);
1249 sp_document_done(sp_desktop_document (dt));
1250 } else {
1251 no_more = true;
1252 }
1254 if (no_more) {
1255 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers above."));
1256 }
1258 g_slist_free ((GSList *) items);
1259 }
1261 void sp_selection_to_prev_layer ()
1262 {
1263 SPDesktop *dt = SP_ACTIVE_DESKTOP;
1265 Inkscape::Selection *selection = sp_desktop_selection(dt);
1267 // check if something is selected
1268 if (selection->isEmpty()) {
1269 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer below."));
1270 return;
1271 }
1273 const GSList *items = g_slist_copy ((GSList *) selection->itemList());
1275 bool no_more = false; // Set to true, if no more layers below
1276 SPObject *next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer());
1277 if (next) {
1278 GSList *temp_clip = NULL;
1279 sp_selection_copy_impl (items, &temp_clip, NULL, NULL); // we're in the same doc, so no need to copy defs
1280 sp_selection_delete_impl (items);
1281 next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
1282 GSList *copied;
1283 if(next) {
1284 copied = sp_selection_paste_impl (sp_desktop_document (dt), next, &temp_clip, NULL);
1285 } else {
1286 copied = sp_selection_paste_impl (sp_desktop_document (dt), dt->currentLayer(), &temp_clip, NULL);
1287 no_more = true;
1288 }
1289 selection->setReprList((GSList const *) copied);
1290 g_slist_free (copied);
1291 if (temp_clip) g_slist_free (temp_clip);
1292 if (next) dt->setCurrentLayer(next);
1293 sp_document_done(sp_desktop_document (dt));
1294 } else {
1295 no_more = true;
1296 }
1298 if (no_more) {
1299 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers below."));
1300 }
1302 g_slist_free ((GSList *) items);
1303 }
1305 /** Apply matrix to the selection. \a set_i2d is normally true, which means objects are in the
1306 original transform, synced with their reprs, and need to jump to the new transform in one go. A
1307 value of set_i2d==false is only used by seltrans when it's dragging objects live (not outlines); in
1308 that case, items are already in the new position, but the repr is in the old, and this function
1309 then simply updates the repr from item->transform.
1310 */
1311 void sp_selection_apply_affine(Inkscape::Selection *selection, NR::Matrix const &affine, bool set_i2d)
1312 {
1313 if (selection->isEmpty())
1314 return;
1316 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1317 SPItem *item = SP_ITEM(l->data);
1319 NR::Point old_center(0,0);
1320 if (set_i2d && item->isCenterSet())
1321 old_center = item->getCenter();
1323 #if 0 /* Re-enable this once persistent guides have a graphical indication.
1324 At the time of writing, this is the only place to re-enable. */
1325 sp_item_update_cns(*item, selection->desktop());
1326 #endif
1328 // we're moving both a clone and its original
1329 bool transform_clone_with_original = (SP_IS_USE(item) && selection->includes( sp_use_get_original (SP_USE(item)) ));
1330 bool transform_textpath_with_path = (SP_IS_TEXT_TEXTPATH(item) && selection->includes( sp_textpath_get_path_item (SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item)))) ));
1331 bool transform_flowtext_with_frame = (SP_IS_FLOWTEXT(item) && selection->includes( SP_FLOWTEXT(item)->get_frame (NULL))); // only the first frame so far
1332 bool transform_offset_with_source = (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) && selection->includes( sp_offset_get_source (SP_OFFSET(item)) );
1334 // If we're moving a connector, we want to detach it
1335 // from shapes that aren't part of the selection, but
1336 // leave it attached if they are
1337 if (cc_item_is_connector(item)) {
1338 SPItem *attItem[2];
1339 SP_PATH(item)->connEndPair.getAttachedItems(attItem);
1341 for (int n = 0; n < 2; ++n) {
1342 if (!selection->includes(attItem[n])) {
1343 sp_conn_end_detach(item, n);
1344 }
1345 }
1346 }
1348 // "clones are unmoved when original is moved" preference
1349 int compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
1350 bool prefs_unmoved = (compensation == SP_CLONE_COMPENSATION_UNMOVED);
1351 bool prefs_parallel = (compensation == SP_CLONE_COMPENSATION_PARALLEL);
1353 // If this is a clone and it's selected along with its original, do not move it; it will feel the
1354 // transform of its original and respond to it itself. Without this, a clone is doubly
1355 // transformed, very unintuitive.
1356 // Same for textpath if we are also doing ANY transform to its path: do not touch textpath,
1357 // letters cannot be squeezed or rotated anyway, they only refill the changed path.
1358 // Same for linked offset if we are also moving its source: do not move it.
1359 if (transform_textpath_with_path || transform_offset_with_source) {
1360 // restore item->transform field from the repr, in case it was changed by seltrans
1361 sp_object_read_attr (SP_OBJECT (item), "transform");
1363 } else if (transform_flowtext_with_frame) {
1364 // apply the inverse of the region's transform to the <use> so that the flow remains
1365 // the same (even though the output itself gets transformed)
1366 for (SPObject *region = item->firstChild() ; region ; region = SP_OBJECT_NEXT(region)) {
1367 if (!SP_IS_FLOWREGION(region) && !SP_IS_FLOWREGIONEXCLUDE(region))
1368 continue;
1369 for (SPObject *use = region->firstChild() ; use ; use = SP_OBJECT_NEXT(use)) {
1370 if (!SP_IS_USE(use)) continue;
1371 sp_item_write_transform(SP_USE(use), SP_OBJECT_REPR(use), item->transform.inverse(), NULL);
1372 }
1373 }
1374 } else if (transform_clone_with_original) {
1375 // We are transforming a clone along with its original. The below matrix juggling is
1376 // necessary to ensure that they transform as a whole, i.e. the clone's induced
1377 // transform and its move compensation are both cancelled out.
1379 // restore item->transform field from the repr, in case it was changed by seltrans
1380 sp_object_read_attr (SP_OBJECT (item), "transform");
1382 // calculate the matrix we need to apply to the clone to cancel its induced transform from its original
1383 NR::Matrix t = matrix_to_desktop (matrix_from_desktop (affine, item), item);
1384 NR::Matrix t_inv = matrix_to_desktop (matrix_from_desktop (affine.inverse(), item), item);
1385 NR::Matrix result = t_inv * item->transform * t;
1387 if ((prefs_parallel || prefs_unmoved) && affine.is_translation()) {
1388 // we need to cancel out the move compensation, too
1390 // find out the clone move, same as in sp_use_move_compensate
1391 NR::Matrix parent = sp_use_get_parent_transform (SP_USE(item));
1392 NR::Matrix clone_move = parent.inverse() * t * parent;
1394 if (prefs_parallel) {
1395 NR::Matrix move = result * clone_move * t_inv;
1396 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &move);
1398 } else if (prefs_unmoved) {
1399 if (SP_IS_USE(sp_use_get_original(SP_USE(item))))
1400 clone_move = NR::identity();
1401 NR::Matrix move = result * clone_move;
1402 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &move);
1403 }
1405 } else {
1406 // just apply the result
1407 sp_item_write_transform(item, SP_OBJECT_REPR(item), result, &result);
1408 }
1410 } else {
1411 if (set_i2d) {
1412 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * affine);
1413 }
1414 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, NULL);
1415 }
1417 // if we're moving the actual object, not just updating the repr, we can transform the
1418 // center by the same matrix (only necessary for non-translations)
1419 if (set_i2d && item->isCenterSet() && !affine.is_translation()) {
1420 item->setCenter(old_center * affine);
1421 SP_OBJECT(item)->updateRepr();
1422 }
1423 }
1424 }
1426 void sp_selection_remove_transform()
1427 {
1428 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1429 if (desktop == NULL)
1430 return;
1432 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1434 GSList const *l = (GSList *) selection->reprList();
1435 while (l != NULL) {
1436 sp_repr_set_attr((Inkscape::XML::Node*)l->data, "transform", NULL);
1437 l = l->next;
1438 }
1440 sp_document_done(sp_desktop_document(desktop));
1441 }
1443 void
1444 sp_selection_scale_absolute(Inkscape::Selection *selection,
1445 double const x0, double const x1,
1446 double const y0, double const y1)
1447 {
1448 if (selection->isEmpty())
1449 return;
1451 NR::Rect const bbox(selection->bounds());
1452 if (bbox.isEmpty()) {
1453 return;
1454 }
1456 NR::translate const p2o(-bbox.min());
1458 NR::scale const newSize(x1 - x0,
1459 y1 - y0);
1460 NR::scale const scale( newSize / NR::scale(bbox.dimensions()) );
1461 NR::translate const o2n(x0, y0);
1462 NR::Matrix const final( p2o * scale * o2n );
1464 sp_selection_apply_affine(selection, final);
1465 }
1468 void sp_selection_scale_relative(Inkscape::Selection *selection, NR::Point const &align, NR::scale const &scale)
1469 {
1470 if (selection->isEmpty())
1471 return;
1473 NR::Rect const bbox(selection->bounds());
1475 if (bbox.isEmpty()) {
1476 return;
1477 }
1479 // FIXME: ARBITRARY LIMIT: don't try to scale above 1 Mpx, it won't display properly and will crash sooner or later anyway
1480 if ( bbox.extent(NR::X) * scale[NR::X] > 1e6 ||
1481 bbox.extent(NR::Y) * scale[NR::Y] > 1e6 )
1482 {
1483 return;
1484 }
1486 NR::translate const n2d(-align);
1487 NR::translate const d2n(align);
1488 NR::Matrix const final( n2d * scale * d2n );
1489 sp_selection_apply_affine(selection, final);
1490 }
1492 void
1493 sp_selection_rotate_relative(Inkscape::Selection *selection, NR::Point const ¢er, gdouble const angle_degrees)
1494 {
1495 NR::translate const d2n(center);
1496 NR::translate const n2d(-center);
1497 NR::rotate const rotate(rotate_degrees(angle_degrees));
1498 NR::Matrix const final( NR::Matrix(n2d) * rotate * d2n );
1499 sp_selection_apply_affine(selection, final);
1500 }
1502 void
1503 sp_selection_skew_relative(Inkscape::Selection *selection, NR::Point const &align, double dx, double dy)
1504 {
1505 NR::translate const d2n(align);
1506 NR::translate const n2d(-align);
1507 NR::Matrix const skew(1, dy,
1508 dx, 1,
1509 0, 0);
1510 NR::Matrix const final( n2d * skew * d2n );
1511 sp_selection_apply_affine(selection, final);
1512 }
1514 void sp_selection_move_relative(Inkscape::Selection *selection, NR::Point const &move)
1515 {
1516 sp_selection_apply_affine(selection, NR::Matrix(NR::translate(move)));
1517 }
1519 void sp_selection_move_relative(Inkscape::Selection *selection, double dx, double dy)
1520 {
1521 sp_selection_apply_affine(selection, NR::Matrix(NR::translate(dx, dy)));
1522 }
1525 /**
1526 * \brief sp_selection_rotate_90
1527 *
1528 * This function rotates selected objects 90 degrees clockwise.
1529 *
1530 */
1532 void sp_selection_rotate_90_cw()
1533 {
1534 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1536 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1538 if (selection->isEmpty())
1539 return;
1541 GSList const *l = selection->itemList();
1542 NR::rotate const rot_neg_90(NR::Point(0, -1));
1543 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1544 SPItem *item = SP_ITEM(l2->data);
1545 sp_item_rotate_rel(item, rot_neg_90);
1546 }
1548 sp_document_done(sp_desktop_document(desktop));
1549 }
1552 /**
1553 * \brief sp_selection_rotate_90_ccw
1554 *
1555 * This function rotates selected objects 90 degrees counter-clockwise.
1556 *
1557 */
1559 void sp_selection_rotate_90_ccw()
1560 {
1561 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1563 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1565 if (selection->isEmpty())
1566 return;
1568 GSList const *l = selection->itemList();
1569 NR::rotate const rot_neg_90(NR::Point(0, 1));
1570 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1571 SPItem *item = SP_ITEM(l2->data);
1572 sp_item_rotate_rel(item, rot_neg_90);
1573 }
1575 sp_document_done(sp_desktop_document(desktop));
1576 }
1578 void
1579 sp_selection_rotate(Inkscape::Selection *selection, gdouble const angle_degrees)
1580 {
1581 if (selection->isEmpty())
1582 return;
1584 NR::Point center = selection->center();
1586 sp_selection_rotate_relative(selection, center, angle_degrees);
1588 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1589 ( ( angle_degrees > 0 )
1590 ? "selector:rotate:ccw"
1591 : "selector:rotate:cw" ));
1592 }
1594 /**
1595 \param angle the angle in "angular pixels", i.e. how many visible pixels must move the outermost point of the rotated object
1596 */
1597 void
1598 sp_selection_rotate_screen(Inkscape::Selection *selection, gdouble angle)
1599 {
1600 if (selection->isEmpty())
1601 return;
1603 NR::Rect const bbox(selection->bounds());
1605 NR::Point center = selection->center();
1607 gdouble const zoom = selection->desktop()->current_zoom();
1608 gdouble const zmove = angle / zoom;
1609 gdouble const r = NR::L2(bbox.max() - center);
1611 gdouble const zangle = 180 * atan2(zmove, r) / M_PI;
1613 sp_selection_rotate_relative(selection, center, zangle);
1615 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1616 ( (angle > 0)
1617 ? "selector:rotate:ccw"
1618 : "selector:rotate:cw" ));
1619 }
1621 void
1622 sp_selection_scale(Inkscape::Selection *selection, gdouble grow)
1623 {
1624 if (selection->isEmpty())
1625 return;
1627 NR::Rect const bbox(selection->bounds());
1628 NR::Point const center(bbox.midpoint());
1629 double const max_len = bbox.maxExtent();
1631 // you can't scale "do nizhe pola" (below zero)
1632 if ( max_len + grow <= 1e-3 ) {
1633 return;
1634 }
1636 double const times = 1.0 + grow / max_len;
1637 sp_selection_scale_relative(selection, center, NR::scale(times, times));
1639 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1640 ( (grow > 0)
1641 ? "selector:scale:larger"
1642 : "selector:scale:smaller" ));
1643 }
1645 void
1646 sp_selection_scale_screen(Inkscape::Selection *selection, gdouble grow_pixels)
1647 {
1648 sp_selection_scale(selection,
1649 grow_pixels / selection->desktop()->current_zoom());
1650 }
1652 void
1653 sp_selection_scale_times(Inkscape::Selection *selection, gdouble times)
1654 {
1655 if (selection->isEmpty())
1656 return;
1658 NR::Point const center(selection->bounds().midpoint());
1659 sp_selection_scale_relative(selection, center, NR::scale(times, times));
1660 sp_document_done(sp_desktop_document(selection->desktop()));
1661 }
1663 void
1664 sp_selection_move(gdouble dx, gdouble dy)
1665 {
1666 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1667 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1668 if (selection->isEmpty()) {
1669 return;
1670 }
1672 sp_selection_move_relative(selection, dx, dy);
1674 if (dx == 0) {
1675 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical");
1676 } else if (dy == 0) {
1677 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal");
1678 } else {
1679 sp_document_done(sp_desktop_document(desktop));
1680 }
1681 }
1683 void
1684 sp_selection_move_screen(gdouble dx, gdouble dy)
1685 {
1686 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1688 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1689 if (selection->isEmpty()) {
1690 return;
1691 }
1693 // same as sp_selection_move but divide deltas by zoom factor
1694 gdouble const zoom = desktop->current_zoom();
1695 gdouble const zdx = dx / zoom;
1696 gdouble const zdy = dy / zoom;
1697 sp_selection_move_relative(selection, zdx, zdy);
1699 if (dx == 0) {
1700 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical");
1701 } else if (dy == 0) {
1702 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal");
1703 } else {
1704 sp_document_done(sp_desktop_document(desktop));
1705 }
1706 }
1708 namespace {
1710 template <typename D>
1711 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
1712 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
1714 template <typename D>
1715 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items, SPObject *root,
1716 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
1718 struct Forward {
1719 typedef SPObject *Iterator;
1721 static Iterator children(SPObject *o) { return sp_object_first_child(o); }
1722 static Iterator siblings_after(SPObject *o) { return SP_OBJECT_NEXT(o); }
1723 static void dispose(Iterator i) {}
1725 static SPObject *object(Iterator i) { return i; }
1726 static Iterator next(Iterator i) { return SP_OBJECT_NEXT(i); }
1727 };
1729 struct Reverse {
1730 typedef GSList *Iterator;
1732 static Iterator children(SPObject *o) {
1733 return make_list(o->firstChild(), NULL);
1734 }
1735 static Iterator siblings_after(SPObject *o) {
1736 return make_list(SP_OBJECT_PARENT(o)->firstChild(), o);
1737 }
1738 static void dispose(Iterator i) {
1739 g_slist_free(i);
1740 }
1742 static SPObject *object(Iterator i) {
1743 return reinterpret_cast<SPObject *>(i->data);
1744 }
1745 static Iterator next(Iterator i) { return i->next; }
1747 private:
1748 static GSList *make_list(SPObject *object, SPObject *limit) {
1749 GSList *list=NULL;
1750 while ( object != limit ) {
1751 list = g_slist_prepend(list, object);
1752 object = SP_OBJECT_NEXT(object);
1753 }
1754 return list;
1755 }
1756 };
1758 }
1760 void
1761 sp_selection_item_next(void)
1762 {
1763 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1764 g_return_if_fail(desktop != NULL);
1765 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1767 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
1768 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
1769 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
1771 SPObject *root;
1772 if (PREFS_SELECTION_ALL != inlayer) {
1773 root = selection->activeContext();
1774 } else {
1775 root = desktop->currentRoot();
1776 }
1778 SPItem *item=next_item_from_list<Forward>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1780 if (item) {
1781 selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
1782 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1783 scroll_to_show_item(desktop, item);
1784 }
1785 }
1786 }
1788 void
1789 sp_selection_item_prev(void)
1790 {
1791 SPDocument *document = SP_ACTIVE_DOCUMENT;
1792 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1793 g_return_if_fail(document != NULL);
1794 g_return_if_fail(desktop != NULL);
1795 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1797 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
1798 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
1799 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
1801 SPObject *root;
1802 if (PREFS_SELECTION_ALL != inlayer) {
1803 root = selection->activeContext();
1804 } else {
1805 root = desktop->currentRoot();
1806 }
1808 SPItem *item=next_item_from_list<Reverse>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1810 if (item) {
1811 selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
1812 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1813 scroll_to_show_item(desktop, item);
1814 }
1815 }
1816 }
1818 namespace {
1820 template <typename D>
1821 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items,
1822 SPObject *root, bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
1823 {
1824 SPObject *current=root;
1825 while (items) {
1826 SPItem *item=SP_ITEM(items->data);
1827 if ( root->isAncestorOf(item) &&
1828 ( !only_in_viewport || desktop->isWithinViewport(item) ) )
1829 {
1830 current = item;
1831 break;
1832 }
1833 items = items->next;
1834 }
1836 GSList *path=NULL;
1837 while ( current != root ) {
1838 path = g_slist_prepend(path, current);
1839 current = SP_OBJECT_PARENT(current);
1840 }
1842 SPItem *next;
1843 // first, try from the current object
1844 next = next_item<D>(desktop, path, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1845 g_slist_free(path);
1847 if (!next) { // if we ran out of objects, start over at the root
1848 next = next_item<D>(desktop, NULL, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1849 }
1851 return next;
1852 }
1854 template <typename D>
1855 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
1856 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
1857 {
1858 typename D::Iterator children;
1859 typename D::Iterator iter;
1861 SPItem *found=NULL;
1863 if (path) {
1864 SPObject *object=reinterpret_cast<SPObject *>(path->data);
1865 g_assert(SP_OBJECT_PARENT(object) == root);
1866 if (desktop->isLayer(object)) {
1867 found = next_item<D>(desktop, path->next, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1868 }
1869 iter = children = D::siblings_after(object);
1870 } else {
1871 iter = children = D::children(root);
1872 }
1874 while ( iter && !found ) {
1875 SPObject *object=D::object(iter);
1876 if (desktop->isLayer(object)) {
1877 if (PREFS_SELECTION_LAYER != inlayer) { // recurse into sublayers
1878 found = next_item<D>(desktop, NULL, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1879 }
1880 } else if ( SP_IS_ITEM(object) &&
1881 ( !only_in_viewport || desktop->isWithinViewport(SP_ITEM(object)) ) &&
1882 ( !onlyvisible || !desktop->itemIsHidden(SP_ITEM(object))) &&
1883 ( !onlysensitive || !SP_ITEM(object)->isLocked()) &&
1884 !desktop->isLayer(SP_ITEM(object)) )
1885 {
1886 found = SP_ITEM(object);
1887 }
1888 iter = D::next(iter);
1889 }
1891 D::dispose(children);
1893 return found;
1894 }
1896 }
1898 /**
1899 * If \a item is not entirely visible then adjust visible area to centre on the centre on of
1900 * \a item.
1901 */
1902 void scroll_to_show_item(SPDesktop *desktop, SPItem *item)
1903 {
1904 NR::Rect dbox = desktop->get_display_area();
1905 NR::Rect sbox = sp_item_bbox_desktop(item);
1907 if (dbox.contains(sbox) == false) {
1908 NR::Point const s_dt = sbox.midpoint();
1909 NR::Point const s_w = desktop->d2w(s_dt);
1910 NR::Point const d_dt = dbox.midpoint();
1911 NR::Point const d_w = desktop->d2w(d_dt);
1912 NR::Point const moved_w( d_w - s_w );
1913 gint const dx = (gint) moved_w[X];
1914 gint const dy = (gint) moved_w[Y];
1915 desktop->scroll_world(dx, dy);
1916 }
1917 }
1920 void
1921 sp_selection_clone()
1922 {
1923 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1924 if (desktop == NULL)
1925 return;
1927 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1929 // check if something is selected
1930 if (selection->isEmpty()) {
1931 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object</b> to clone."));
1932 return;
1933 }
1935 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
1937 selection->clear();
1939 // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
1940 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
1942 GSList *newsel = NULL;
1944 while (reprs) {
1945 Inkscape::XML::Node *sel_repr = (Inkscape::XML::Node *) reprs->data;
1946 Inkscape::XML::Node *parent = sp_repr_parent(sel_repr);
1948 Inkscape::XML::Node *clone = sp_repr_new("svg:use");
1949 sp_repr_set_attr(clone, "x", "0");
1950 sp_repr_set_attr(clone, "y", "0");
1951 sp_repr_set_attr(clone, "xlink:href", g_strdup_printf("#%s", sel_repr->attribute("id")));
1953 sp_repr_set_attr(clone, "inkscape:transform-center-x", sel_repr->attribute("inkscape:transform-center-x"));
1954 sp_repr_set_attr(clone, "inkscape:transform-center-y", sel_repr->attribute("inkscape:transform-center-y"));
1956 // add the new clone to the top of the original's parent
1957 parent->appendChild(clone);
1959 newsel = g_slist_prepend(newsel, clone);
1960 reprs = g_slist_remove(reprs, sel_repr);
1961 Inkscape::GC::release(clone);
1962 }
1964 sp_document_done(sp_desktop_document(desktop));
1966 selection->setReprList(newsel);
1968 g_slist_free(newsel);
1969 }
1971 void
1972 sp_selection_unlink()
1973 {
1974 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1975 if (!desktop)
1976 return;
1978 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1980 if (selection->isEmpty()) {
1981 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>clone</b> to unlink."));
1982 return;
1983 }
1985 // Get a copy of current selection.
1986 GSList *new_select = NULL;
1987 bool unlinked = false;
1988 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
1989 items != NULL;
1990 items = items->next)
1991 {
1992 SPItem *use = (SPItem *) items->data;
1994 if (!SP_IS_USE(use)) {
1995 // keep the non-yse item in the new selection
1996 new_select = g_slist_prepend(new_select, use);
1997 continue;
1998 }
2000 SPItem *unlink = sp_use_unlink(SP_USE(use));
2001 unlinked = true;
2002 // Add ungrouped items to the new selection.
2003 new_select = g_slist_prepend(new_select, unlink);
2004 }
2006 if (new_select) { // set new selection
2007 selection->clear();
2008 selection->setList(new_select);
2009 g_slist_free(new_select);
2010 }
2011 if (!unlinked) {
2012 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No clones to unlink</b> in the selection."));
2013 }
2015 sp_document_done(sp_desktop_document(desktop));
2016 }
2018 void
2019 sp_select_clone_original()
2020 {
2021 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2022 if (desktop == NULL)
2023 return;
2025 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2027 SPItem *item = selection->singleItem();
2029 const gchar *error = _("Select a <b>clone</b> to go to its original. Select a <b>linked offset</b> to go to its source. Select a <b>text on path</b> to go to the path. Select a <b>flowed text</b> to go to its frame.");
2031 // Check if other than two objects are selected
2032 if (g_slist_length((GSList *) selection->itemList()) != 1 || !item) {
2033 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
2034 return;
2035 }
2037 SPItem *original = NULL;
2038 if (SP_IS_USE(item)) {
2039 original = sp_use_get_original (SP_USE(item));
2040 } else if (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) {
2041 original = sp_offset_get_source (SP_OFFSET(item));
2042 } else if (SP_IS_TEXT_TEXTPATH(item)) {
2043 original = sp_textpath_get_path_item (SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))));
2044 } else if (SP_IS_FLOWTEXT(item)) {
2045 original = SP_FLOWTEXT(item)->get_frame (NULL); // first frame only
2046 } else { // it's an object that we don't know what to do with
2047 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
2048 return;
2049 }
2051 if (!original) {
2052 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>Cannot find</b> the object to select (orphaned clone, offset, textpath, flowed text?)"));
2053 return;
2054 }
2056 for (SPObject *o = original; o && !SP_IS_ROOT(o); o = SP_OBJECT_PARENT (o)) {
2057 if (SP_IS_DEFS (o)) {
2058 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("The object you're trying to select is <b>not visible</b> (it is in <defs>)"));
2059 return;
2060 }
2061 }
2063 if (original) {
2064 selection->clear();
2065 selection->set(original);
2066 if (SP_CYCLING == SP_CYCLE_FOCUS) {
2067 scroll_to_show_item(desktop, original);
2068 }
2069 }
2070 }
2072 void
2073 sp_selection_tile(bool apply)
2074 {
2075 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2076 if (desktop == NULL)
2077 return;
2079 SPDocument *document = sp_desktop_document(desktop);
2081 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2083 // check if something is selected
2084 if (selection->isEmpty()) {
2085 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to pattern."));
2086 return;
2087 }
2089 sp_document_ensure_up_to_date(document);
2090 NR::Rect r = selection->bounds();
2091 if (r.isEmpty()) {
2092 return;
2093 }
2095 // calculate the transform to be applied to objects to move them to 0,0
2096 NR::Point move_p = NR::Point(0, sp_document_height(document)) - (r.min() + NR::Point (0, r.extent(NR::Y)));
2097 move_p[NR::Y] = -move_p[NR::Y];
2098 NR::Matrix move = NR::Matrix (NR::translate (move_p));
2100 GSList *items = g_slist_copy((GSList *) selection->itemList());
2102 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2104 // bottommost object, after sorting
2105 SPObject *parent = SP_OBJECT_PARENT (items->data);
2107 // remember the position of the first item
2108 gint pos = SP_OBJECT_REPR (items->data)->position();
2110 // create a list of duplicates
2111 GSList *repr_copies = NULL;
2112 for (GSList *i = items; i != NULL; i = i->next) {
2113 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate();
2114 repr_copies = g_slist_prepend (repr_copies, dup);
2115 }
2117 NR::Rect bounds(desktop->dt2doc(r.min()), desktop->dt2doc(r.max()));
2119 if (apply) {
2120 // delete objects so that their clones don't get alerted; this object will be restored shortly
2121 for (GSList *i = items; i != NULL; i = i->next) {
2122 SPObject *item = SP_OBJECT (i->data);
2123 item->deleteObject (false);
2124 }
2125 }
2127 // Hack: Temporarily set clone compensation to unmoved, so that we can move clone-originals
2128 // without disturbing clones.
2129 // See ActorAlign::on_button_click() in src/ui/dialog/align-and-distribute.cpp
2130 int saved_compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2131 prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2133 const gchar *pat_id = pattern_tile (repr_copies, bounds, document,
2134 NR::Matrix(NR::translate(desktop->dt2doc(NR::Point(r.min()[NR::X], r.max()[NR::Y])))), move);
2136 // restore compensation setting
2137 prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
2139 if (apply) {
2140 Inkscape::XML::Node *rect = sp_repr_new ("svg:rect");
2141 rect->setAttribute("style", g_strdup_printf("stroke:none;fill:url(#%s)", pat_id));
2142 sp_repr_set_svg_double(rect, "width", bounds.extent(NR::X));
2143 sp_repr_set_svg_double(rect, "height", bounds.extent(NR::Y));
2144 sp_repr_set_svg_double(rect, "x", bounds.min()[NR::X]);
2145 sp_repr_set_svg_double(rect, "y", bounds.min()[NR::Y]);
2147 // restore parent and position
2148 SP_OBJECT_REPR (parent)->appendChild(rect);
2149 rect->setPosition(pos > 0 ? pos : 0);
2150 SPItem *rectangle = (SPItem *) sp_desktop_document (desktop)->getObjectByRepr(rect);
2152 Inkscape::GC::release(rect);
2154 selection->clear();
2155 selection->set(rectangle);
2156 }
2158 g_slist_free (items);
2160 sp_document_done (document);
2161 }
2163 void
2164 sp_selection_untile()
2165 {
2166 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2167 if (desktop == NULL)
2168 return;
2170 SPDocument *document = sp_desktop_document(desktop);
2172 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2174 // check if something is selected
2175 if (selection->isEmpty()) {
2176 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object with pattern fill</b> to extract objects from."));
2177 return;
2178 }
2180 GSList *new_select = NULL;
2182 bool did = false;
2184 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2185 items != NULL;
2186 items = items->next) {
2188 SPItem *item = (SPItem *) items->data;
2190 SPStyle *style = SP_OBJECT_STYLE (item);
2192 if (!style || style->fill.type != SP_PAINT_TYPE_PAINTSERVER)
2193 continue;
2195 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
2197 if (!SP_IS_PATTERN(server))
2198 continue;
2200 did = true;
2202 SPPattern *pattern = pattern_getroot (SP_PATTERN (server));
2204 NR::Matrix pat_transform = pattern_patternTransform (SP_PATTERN (server));
2205 pat_transform *= item->transform;
2207 for (SPObject *child = sp_object_first_child(SP_OBJECT(pattern)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2208 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate();
2209 SPItem *i = SP_ITEM (desktop->currentLayer()->appendChildRepr(copy));
2211 // FIXME: relink clones to the new canvas objects
2212 // use SPObject::setid when mental finishes it to steal ids of
2214 // this is needed to make sure the new item has curve (simply requestDisplayUpdate does not work)
2215 sp_document_ensure_up_to_date (document);
2217 NR::Matrix transform( i->transform * pat_transform );
2218 sp_item_write_transform(i, SP_OBJECT_REPR(i), transform);
2220 new_select = g_slist_prepend(new_select, i);
2221 }
2223 SPCSSAttr *css = sp_repr_css_attr_new ();
2224 sp_repr_css_set_property (css, "fill", "none");
2225 sp_repr_css_change (SP_OBJECT_REPR (item), css, "style");
2226 }
2228 if (!did) {
2229 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No pattern fills</b> in the selection."));
2230 } else {
2231 sp_document_done(sp_desktop_document(desktop));
2232 selection->setList(new_select);
2233 }
2234 }
2236 void
2237 sp_selection_create_bitmap_copy ()
2238 {
2239 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2240 if (desktop == NULL)
2241 return;
2243 SPDocument *document = sp_desktop_document(desktop);
2245 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2247 // check if something is selected
2248 if (selection->isEmpty()) {
2249 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to make a bitmap copy."));
2250 return;
2251 }
2253 // Get the bounding box of the selection
2254 NRRect bbox;
2255 sp_document_ensure_up_to_date (document);
2256 selection->bounds(&bbox);
2257 if (NR_RECT_DFLS_TEST_EMPTY(&bbox)) {
2258 return; // exceptional situation, so not bother with a translatable error message, just quit quietly
2259 }
2261 // List of the items to show; all others will be hidden
2262 GSList *items = g_slist_copy ((GSList *) selection->itemList());
2264 // Sort items so that the topmost comes last
2265 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
2267 // Generate a random value from the current time (you may create bitmap from the same object(s)
2268 // multiple times, and this is done so that they don't clash)
2269 GTimeVal cu;
2270 g_get_current_time (&cu);
2271 guint current = (int) (cu.tv_sec * 1000000 + cu.tv_usec) % 1024;
2273 // Create the filename
2274 gchar *filename = g_strdup_printf ("%s-%s-%u.png", document->name, SP_OBJECT_REPR(items->data)->attribute("id"), current);
2275 // Imagemagick is known not to handle spaces in filenames, so we replace anything but letters,
2276 // digits, and a few other chars, with "_"
2277 filename = g_strcanon (filename, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.=+~$#@^&!?", '_');
2278 // Build the complete path by adding document->base if set
2279 gchar *filepath = g_build_filename (document->base?document->base:"", filename, NULL);
2281 //g_print ("%s\n", filepath);
2283 // Remember parent and z-order of the topmost one
2284 gint pos = SP_OBJECT_REPR(g_slist_last(items)->data)->position();
2285 SPObject *parent_object = SP_OBJECT_PARENT(g_slist_last(items)->data);
2286 Inkscape::XML::Node *parent = SP_OBJECT_REPR(parent_object);
2288 // Calculate resolution
2289 double res;
2290 int const prefs_res = prefs_get_int_attribute ("options.createbitmap", "resolution", 0);
2291 if (0 < prefs_res) {
2292 // If it's given explicitly in prefs, take it
2293 res = prefs_res;
2294 } else {
2295 // Otherwise look up minimum bitmap size (default 250 pixels) and calculate resolution from it
2296 int const prefs_min = prefs_get_int_attribute ("options.createbitmap", "minsize", 250);
2297 res = prefs_min / MIN ((bbox.x1 - bbox.x0), (bbox.y1 - bbox.y0));
2298 }
2300 // The width and height of the bitmap in pixels
2301 unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * res);
2302 unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * res);
2304 // Find out if we have to run a filter
2305 const gchar *run = NULL;
2306 const gchar *filter = prefs_get_string_attribute ("options.createbitmap", "filter");
2307 if (filter) {
2308 // filter command is given;
2309 // see if we have a parameter to pass to it
2310 const gchar *param1 = prefs_get_string_attribute ("options.createbitmap", "filter_param1");
2311 if (param1) {
2312 if (param1[strlen(param1) - 1] == '%') {
2313 // if the param string ends with %, interpret it as a percentage of the image's max dimension
2314 gchar p1[256];
2315 g_ascii_dtostr (p1, 256, ceil (g_ascii_strtod (param1, NULL) * MAX(width, height) / 100));
2316 // the first param is always the image filename, the second is param1
2317 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, p1);
2318 } else {
2319 // otherwise pass the param1 unchanged
2320 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, param1);
2321 }
2322 } else {
2323 // run without extra parameter
2324 run = g_strdup_printf ("%s \"%s\"", filter, filepath);
2325 }
2326 }
2328 // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
2329 NR::Matrix eek = sp_item_i2d_affine (SP_ITEM(parent_object));
2330 NR::Matrix t = NR::scale (1/res, -1/res) * NR::translate (bbox.x0, bbox.y1) * eek.inverse();
2332 // Do the export
2333 sp_export_png_file(document, filepath,
2334 bbox.x0, bbox.y0, bbox.x1, bbox.y1,
2335 width, height,
2336 (guint32) 0xffffff00,
2337 NULL, NULL,
2338 true, /*bool force_overwrite,*/
2339 items);
2341 g_slist_free (items);
2343 // Run filter, if any
2344 if (run) {
2345 g_print ("Running external filter: %s\n", run);
2346 system (run);
2347 }
2349 // Import the image back
2350 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filepath, NULL);
2351 if (pb) {
2352 // Create the repr for the image
2353 Inkscape::XML::Node * repr = sp_repr_new ("svg:image");
2354 repr->setAttribute("xlink:href", filename);
2355 repr->setAttribute("sodipodi:absref", filepath);
2356 sp_repr_set_svg_double(repr, "width", gdk_pixbuf_get_width(pb));
2357 sp_repr_set_svg_double(repr, "height", gdk_pixbuf_get_height(pb));
2359 // Write transform
2360 gchar c[256];
2361 if (sp_svg_transform_write(c, 256, t)) {
2362 repr->setAttribute("transform", c);
2363 }
2365 // add the new repr to the parent
2366 parent->appendChild(repr);
2368 // move to the saved position
2369 repr->setPosition(pos > 0 ? pos + 1 : 1);
2371 // Set selection to the new image
2372 selection->clear();
2373 selection->add(repr);
2375 // Clean up
2376 Inkscape::GC::release(repr);
2377 gdk_pixbuf_unref (pb);
2379 // Complete undoable transaction
2380 sp_document_done (document);
2381 }
2383 g_free (filename);
2384 g_free (filepath);
2385 }
2387 /**
2388 * \brief sp_selection_set_mask
2389 *
2390 * This function creates a mask or clipPath from selection
2391 * Two different modes:
2392 * if applyToLayer, all selection is moved to DEFS as mask/clippath
2393 * and is applied to current layer
2394 * otherwise, topmost object is used as mask for other objects
2395 * If \a apply_clip_path parameter is true, clipPath is created, otherwise mask
2396 *
2397 */
2398 void
2399 sp_selection_set_mask(bool apply_clip_path, bool apply_to_layer)
2400 {
2401 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2402 if (desktop == NULL)
2403 return;
2405 SPDocument *document = sp_desktop_document(desktop);
2407 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2409 // check if something is selected
2410 bool is_empty = selection->isEmpty();
2411 if ( apply_to_layer && is_empty) {
2412 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to create clippath or mask from."));
2413 return;
2414 } else if (!apply_to_layer && ( is_empty || NULL == selection->itemList()->next )) {
2415 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select mask object and <b>object(s)</b> to apply clippath or mask to."));
2416 return;
2417 }
2419 sp_document_ensure_up_to_date(document);
2421 GSList *items = g_slist_copy((GSList *) selection->itemList());
2423 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2425 // create a list of duplicates
2426 GSList *mask_items = NULL;
2427 GSList *apply_to_items = NULL;
2428 bool topmost = prefs_get_int_attribute ("options.maskobject", "topmost", 1);
2429 bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
2431 if (apply_to_layer) {
2432 // all selected items are used for mask, which is applied to a layer
2433 apply_to_items = g_slist_prepend (apply_to_items, desktop->currentLayer());
2435 for (GSList *i = items; i != NULL; i = i->next) {
2436 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate();
2437 mask_items = g_slist_prepend (mask_items, dup);
2439 if (remove_original) {
2440 SPObject *item = SP_OBJECT (i->data);
2441 item->deleteObject (false);
2442 }
2443 }
2444 } else if (!topmost) {
2445 // topmost item is used as a mask, which is applied to other items in a selection
2446 GSList *i = items;
2447 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate();
2448 mask_items = g_slist_prepend (mask_items, dup);
2450 if (remove_original) {
2451 SPObject *item = SP_OBJECT (i->data);
2452 item->deleteObject (false);
2453 }
2455 for (i = i->next; i != NULL; i = i->next) {
2456 apply_to_items = g_slist_prepend (apply_to_items, i->data);
2457 }
2458 } else {
2459 GSList *i = NULL;
2460 for (i = items; NULL != i->next; i = i->next) {
2461 apply_to_items = g_slist_prepend (apply_to_items, i->data);
2462 }
2464 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate();
2465 mask_items = g_slist_prepend (mask_items, dup);
2467 if (remove_original) {
2468 SPObject *item = SP_OBJECT (i->data);
2469 item->deleteObject (false);
2470 }
2471 }
2473 g_slist_free (items);
2474 items = NULL;
2476 gchar const* attributeName = apply_clip_path ? "clip-path" : "mask";
2477 for (GSList *i = apply_to_items; NULL != i; i = i->next) {
2478 SPItem *item = reinterpret_cast<SPItem *>(i->data);
2479 // inverted object transform should be applied to a mask object,
2480 // as mask is calculated in user space (after applying transform)
2481 NR::Matrix maskTransform (item->transform.inverse());
2483 GSList *mask_items_dup = NULL;
2484 for (GSList *mask_item = mask_items; NULL != mask_item; mask_item = mask_item->next) {
2485 Inkscape::XML::Node *dup = reinterpret_cast<Inkscape::XML::Node *>(mask_item->data)->duplicate();
2486 mask_items_dup = g_slist_prepend (mask_items_dup, dup);
2487 }
2489 const gchar *mask_id = NULL;
2490 if (apply_clip_path) {
2491 mask_id = sp_clippath_create(mask_items_dup, document, &maskTransform);
2492 } else {
2493 mask_id = sp_mask_create(mask_items_dup, document, &maskTransform);
2494 }
2496 g_slist_free (mask_items_dup);
2497 mask_items_dup = NULL;
2499 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, g_strdup_printf("url(#%s)", mask_id));
2500 }
2502 g_slist_free (mask_items);
2503 g_slist_free (apply_to_items);
2505 sp_document_done (document);
2506 }
2508 void sp_selection_unset_mask(bool apply_clip_path) {
2509 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2510 if (desktop == NULL)
2511 return;
2513 SPDocument *document = sp_desktop_document(desktop);
2514 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2516 // check if something is selected
2517 if (selection->isEmpty()) {
2518 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to remove clippath or mask from."));
2519 return;
2520 }
2522 bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
2523 sp_document_ensure_up_to_date(document);
2525 gchar const* attributeName = apply_clip_path ? "clip-path" : "mask";
2526 std::map<SPObject*,SPItem*> referenced_objects;
2527 for (GSList const*i = selection->itemList(); NULL != i; i = i->next) {
2528 if (remove_original) {
2529 // remember referenced mask/clippath, so orphaned masks can be moved back to document
2530 SPItem *item = reinterpret_cast<SPItem *>(i->data);
2531 Inkscape::URIReference *uri_ref = NULL;
2533 if (apply_clip_path) {
2534 uri_ref = item->clip_ref;
2535 } else {
2536 uri_ref = item->mask_ref;
2537 }
2539 // collect distinct mask object (and associate with item to apply transform)
2540 if (NULL != uri_ref && NULL != uri_ref->getObject()) {
2541 referenced_objects[uri_ref->getObject()] = item;
2542 }
2543 }
2545 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, "none");
2546 }
2548 // restore mask objects into a document
2549 for ( std::map<SPObject*,SPItem*>::iterator it = referenced_objects.begin() ; it != referenced_objects.end() ; ++it) {
2550 SPObject *obj = (*it).first;
2551 GSList *items_to_move = NULL;
2552 for (SPObject *child = sp_object_first_child(obj) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2553 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate();
2554 items_to_move = g_slist_prepend (items_to_move, copy);
2555 }
2557 if (!obj->isReferenced()) {
2558 // delete from defs if no other object references this mask
2559 obj->deleteObject(false);
2560 }
2562 // remember parent and position of the item to which the clippath/mask was applied
2563 Inkscape::XML::Node *parent = SP_OBJECT_REPR((*it).second)->parent();
2564 gint pos = SP_OBJECT_REPR((*it).second)->position();
2566 for (GSList *i = items_to_move; NULL != i; i = i->next) {
2567 Inkscape::XML::Node *repr = (Inkscape::XML::Node *)i->data;
2569 // insert into parent, restore pos
2570 parent->appendChild(repr);
2571 repr->setPosition((pos + 1) > 0 ? (pos + 1) : 0);
2573 SPItem *mask_item = (SPItem *) sp_desktop_document (desktop)->getObjectByRepr(repr);
2574 selection->add(repr);
2576 // transform mask, so it is moved the same spot where mask was applied
2577 NR::Matrix transform (mask_item->transform);
2578 transform *= (*it).second->transform;
2579 sp_item_write_transform(mask_item, SP_OBJECT_REPR(mask_item), transform);
2580 }
2582 g_slist_free (items_to_move);
2583 }
2585 sp_document_done (document);
2586 }
2588 void fit_canvas_to_selection(SPDesktop *desktop) {
2589 g_return_if_fail(desktop != NULL);
2590 SPDocument *doc = sp_desktop_document(desktop);
2592 g_return_if_fail(doc != NULL);
2593 g_return_if_fail(desktop->selection != NULL);
2594 g_return_if_fail(!desktop->selection->isEmpty());
2595 NRRect bbox = {0,0,0,0};
2597 desktop->selection->bounds(&bbox);
2598 if (!empty(bbox)) {
2599 doc->fitToRect(bbox);
2600 }
2601 };
2603 void fit_canvas_to_drawing(SPDocument *doc) {
2604 g_return_if_fail(doc != NULL);
2605 NRRect bbox = {0,0,0,0};
2607 sp_document_ensure_up_to_date (doc);
2608 sp_item_invoke_bbox(SP_ITEM(doc->root), &bbox, sp_item_i2r_affine(SP_ITEM(doc->root)), TRUE);
2610 if (!empty(bbox)) {
2611 doc->fitToRect(bbox);
2612 }
2613 };
2615 void fit_canvas_to_selection_or_drawing(SPDesktop *desktop) {
2616 g_return_if_fail(desktop != NULL);
2617 SPDocument *doc = sp_desktop_document(desktop);
2619 g_return_if_fail(doc != NULL);
2620 g_return_if_fail(desktop->selection != NULL);
2622 if (desktop->selection->isEmpty()) {
2623 fit_canvas_to_drawing(doc);
2624 } else {
2625 fit_canvas_to_selection(desktop);
2626 }
2628 sp_document_done(doc);
2629 };
2631 /*
2632 Local Variables:
2633 mode:c++
2634 c-file-style:"stroustrup"
2635 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2636 indent-tabs-mode:nil
2637 fill-column:99
2638 End:
2639 */
2640 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :