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 "dropper-context.h"
43 #include <glibmm/i18n.h>
44 #include "libnr/nr-matrix-rotate-ops.h"
45 #include "libnr/nr-matrix-translate-ops.h"
46 #include "libnr/nr-rotate-fns.h"
47 #include "libnr/nr-scale-ops.h"
48 #include "libnr/nr-scale-translate-ops.h"
49 #include "libnr/nr-translate-matrix-ops.h"
50 #include "libnr/nr-translate-scale-ops.h"
51 #include "xml/repr.h"
52 #include "style.h"
53 #include "document-private.h"
54 #include "sp-gradient.h"
55 #include "sp-gradient-reference.h"
56 #include "sp-linear-gradient-fns.h"
57 #include "sp-pattern.h"
58 #include "sp-radial-gradient-fns.h"
59 #include "sp-namedview.h"
60 #include "prefs-utils.h"
61 #include "sp-offset.h"
62 #include "sp-clippath.h"
63 #include "sp-mask.h"
64 #include "file.h"
65 #include "layer-fns.h"
66 #include "context-fns.h"
67 #include <map>
68 #include "helper/units.h"
69 #include "sp-item.h"
70 using NR::X;
71 using NR::Y;
73 #include "selection-chemistry.h"
75 /* fixme: find a better place */
76 GSList *clipboard = NULL;
77 GSList *defs_clipboard = NULL;
78 SPCSSAttr *style_clipboard = NULL;
79 NR::Rect size_clipboard(NR::Point(0,0), NR::Point(0,0));
81 static void sp_copy_stuff_used_by_item(GSList **defs_clip, SPItem *item, const GSList *items);
83 void sp_selection_copy_one (Inkscape::XML::Node *repr, NR::Matrix full_t, GSList **clip)
84 {
85 Inkscape::XML::Node *copy = repr->duplicate();
87 // copy complete inherited style
88 SPCSSAttr *css = sp_repr_css_attr_inherited(repr, "style");
89 sp_repr_css_set(copy, css, "style");
90 sp_repr_css_attr_unref(css);
92 // write the complete accummulated transform passed to us
93 // (we're dealing with unattached repr, so we write to its attr instead of using sp_item_set_transform)
94 gchar affinestr[80];
95 if (sp_svg_transform_write(affinestr, 79, full_t)) {
96 copy->setAttribute("transform", affinestr);
97 } else {
98 copy->setAttribute("transform", NULL);
99 }
101 *clip = g_slist_prepend(*clip, copy);
102 }
104 void sp_selection_copy_impl (const GSList *items, GSList **clip, GSList **defs_clip, SPCSSAttr **style_clip)
105 {
107 // Copy stuff referenced by all items to defs_clip:
108 if (defs_clip) {
109 for (GSList *i = (GSList *) items; i != NULL; i = i->next) {
110 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (i->data), items);
111 }
112 *defs_clip = g_slist_reverse(*defs_clip);
113 }
115 // Store style:
116 if (style_clip) {
117 SPItem *item = SP_ITEM (items->data); // take from the first selected item
118 *style_clip = take_style_from_item (item);
119 }
121 if (clip) {
122 // Sort items:
123 GSList *sorted_items = g_slist_copy ((GSList *) items);
124 sorted_items = g_slist_sort((GSList *) sorted_items, (GCompareFunc) sp_object_compare_position);
126 // Copy item reprs:
127 for (GSList *i = (GSList *) sorted_items; i != NULL; i = i->next) {
128 sp_selection_copy_one (SP_OBJECT_REPR (i->data), sp_item_i2doc_affine(SP_ITEM (i->data)), clip);
129 }
131 *clip = g_slist_reverse(*clip);
132 g_slist_free ((GSList *) sorted_items);
133 }
134 }
136 /**
137 Add gradients/patterns/markers referenced by copied objects to defs
138 */
139 void
140 paste_defs (GSList **defs_clip, SPDocument *doc)
141 {
142 if (!defs_clip)
143 return;
145 for (GSList *gl = *defs_clip; gl != NULL; gl = gl->next) {
146 SPDefs *defs= (SPDefs *) SP_DOCUMENT_DEFS(doc);
147 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) gl->data;
148 gchar const *id = repr->attribute("id");
149 if (!id || !doc->getObjectById(id)) {
150 Inkscape::XML::Node *copy = repr->duplicate();
151 SP_OBJECT_REPR(defs)->addChild(copy, NULL);
152 Inkscape::GC::release(copy);
153 }
154 }
155 }
157 GSList *sp_selection_paste_impl (SPDocument *document, SPObject *parent, GSList **clip, GSList **defs_clip)
158 {
159 paste_defs (defs_clip, document);
161 GSList *copied = NULL;
162 // add objects to document
163 for (GSList *l = *clip; l != NULL; l = l->next) {
164 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
165 Inkscape::XML::Node *copy = repr->duplicate();
167 // premultiply the item transform by the accumulated parent transform in the paste layer
168 NR::Matrix local = sp_item_i2doc_affine(SP_ITEM(parent));
169 if (!local.test_identity()) {
170 gchar const *t_str = copy->attribute("transform");
171 NR::Matrix item_t (NR::identity());
172 if (t_str)
173 sp_svg_transform_read(t_str, &item_t);
174 item_t *= local.inverse();
175 // (we're dealing with unattached repr, so we write to its attr instead of using sp_item_set_transform)
176 gchar affinestr[80];
177 if (sp_svg_transform_write(affinestr, 79, item_t)) {
178 copy->setAttribute("transform", affinestr);
179 } else {
180 copy->setAttribute("transform", NULL);
181 }
182 }
184 parent->appendChildRepr(copy);
185 copied = g_slist_prepend(copied, copy);
186 Inkscape::GC::release(copy);
187 }
188 return copied;
189 }
191 void sp_selection_delete_impl(const GSList *items)
192 {
193 for (const GSList *i = items ; i ; i = i->next ) {
194 sp_object_ref((SPObject *)i->data, NULL);
195 }
196 for (const GSList *i = items; i != NULL; i = i->next) {
197 SPItem *item = (SPItem *) i->data;
198 SP_OBJECT(item)->deleteObject();
199 sp_object_unref((SPObject *)item, NULL);
200 }
201 }
204 void sp_selection_delete()
205 {
206 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
207 if (desktop == NULL) {
208 return;
209 }
211 if (tools_isactive (desktop, TOOLS_TEXT))
212 if (sp_text_delete_selection(desktop->event_context)) {
213 sp_document_done(sp_desktop_document(desktop));
214 return;
215 }
217 Inkscape::Selection *selection = sp_desktop_selection(desktop);
219 // check if something is selected
220 if (selection->isEmpty()) {
221 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Nothing</b> was deleted."));
222 return;
223 }
225 const GSList *selected = g_slist_copy(const_cast<GSList *>(selection->itemList()));
226 selection->clear();
227 sp_selection_delete_impl (selected);
228 g_slist_free ((GSList *) selected);
230 /* a tool may have set up private information in it's selection context
231 * that depends on desktop items. I think the only sane way to deal with
232 * this currently is to reset the current tool, which will reset it's
233 * associated selection context. For example: deleting an object
234 * while moving it around the canvas.
235 */
236 tools_switch ( desktop, tools_active ( desktop ) );
238 sp_document_done(sp_desktop_document(desktop));
239 }
241 /* fixme: sequencing */
242 void sp_selection_duplicate()
243 {
244 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
245 if (desktop == NULL)
246 return;
248 Inkscape::Selection *selection = sp_desktop_selection(desktop);
250 // check if something is selected
251 if (selection->isEmpty()) {
252 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to duplicate."));
253 return;
254 }
256 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
258 selection->clear();
260 // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
261 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
263 GSList *newsel = NULL;
265 while (reprs) {
266 Inkscape::XML::Node *parent = ((Inkscape::XML::Node *) reprs->data)->parent();
267 Inkscape::XML::Node *copy = ((Inkscape::XML::Node *) reprs->data)->duplicate();
269 parent->appendChild(copy);
271 newsel = g_slist_prepend(newsel, copy);
272 reprs = g_slist_remove(reprs, reprs->data);
273 Inkscape::GC::release(copy);
274 }
276 sp_document_done(sp_desktop_document(desktop));
278 selection->setReprList(newsel);
280 g_slist_free(newsel);
281 }
283 void sp_edit_clear_all()
284 {
285 SPDesktop *dt = SP_ACTIVE_DESKTOP;
286 if (!dt)
287 return;
289 SPDocument *doc = sp_desktop_document(dt);
290 sp_desktop_selection(dt)->clear();
292 g_return_if_fail(SP_IS_GROUP(dt->currentLayer()));
293 GSList *items = sp_item_group_item_list(SP_GROUP(dt->currentLayer()));
295 while (items) {
296 SP_OBJECT (items->data)->deleteObject();
297 items = g_slist_remove(items, items->data);
298 }
300 sp_document_done(doc);
301 }
303 GSList *
304 get_all_items (GSList *list, SPObject *from, SPDesktop *desktop, bool onlyvisible, bool onlysensitive, const GSList *exclude)
305 {
306 for (SPObject *child = sp_object_first_child(SP_OBJECT(from)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
307 if (SP_IS_ITEM(child) &&
308 !desktop->isLayer(SP_ITEM(child)) &&
309 (!onlysensitive || !SP_ITEM(child)->isLocked()) &&
310 (!onlyvisible || !desktop->itemIsHidden(SP_ITEM(child))) &&
311 (!exclude || !g_slist_find ((GSList *) exclude, child))
312 )
313 {
314 list = g_slist_prepend (list, SP_ITEM(child));
315 }
317 if (SP_IS_ITEM(child) && desktop->isLayer(SP_ITEM(child))) {
318 list = get_all_items (list, child, desktop, onlyvisible, onlysensitive, exclude);
319 }
320 }
322 return list;
323 }
325 void sp_edit_select_all_full (bool force_all_layers, bool invert)
326 {
327 SPDesktop *dt = SP_ACTIVE_DESKTOP;
328 if (!dt)
329 return;
331 Inkscape::Selection *selection = sp_desktop_selection(dt);
333 g_return_if_fail(SP_IS_GROUP(dt->currentLayer()));
335 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
336 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
337 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
339 GSList *items = NULL;
341 const GSList *exclude = NULL;
342 if (invert) {
343 exclude = selection->itemList();
344 }
346 if (force_all_layers)
347 inlayer = PREFS_SELECTION_ALL;
349 switch (inlayer) {
350 case PREFS_SELECTION_LAYER: {
351 if ( (onlysensitive && SP_ITEM(dt->currentLayer())->isLocked()) ||
352 (onlyvisible && dt->itemIsHidden(SP_ITEM(dt->currentLayer()))) )
353 return;
355 GSList *all_items = sp_item_group_item_list(SP_GROUP(dt->currentLayer()));
357 for (GSList *i = all_items; i; i = i->next) {
358 SPItem *item = SP_ITEM (i->data);
360 if (item && (!onlysensitive || !item->isLocked())) {
361 if (!onlyvisible || !dt->itemIsHidden(item)) {
362 if (!dt->isLayer(item)) {
363 if (!invert || !g_slist_find ((GSList *) exclude, item)) {
364 items = g_slist_prepend (items, item); // leave it in the list
365 }
366 }
367 }
368 }
369 }
371 g_slist_free (all_items);
372 break;
373 }
374 case PREFS_SELECTION_LAYER_RECURSIVE: {
375 items = get_all_items (NULL, dt->currentLayer(), dt, onlyvisible, onlysensitive, exclude);
376 break;
377 }
378 default: {
379 items = get_all_items (NULL, dt->currentRoot(), dt, onlyvisible, onlysensitive, exclude);
380 break;
381 }
382 }
384 selection->setList (items);
386 if (items) {
387 g_slist_free (items);
388 }
389 }
391 void sp_edit_select_all ()
392 {
393 sp_edit_select_all_full (false, false);
394 }
396 void sp_edit_select_all_in_all_layers ()
397 {
398 sp_edit_select_all_full (true, false);
399 }
401 void sp_edit_invert ()
402 {
403 sp_edit_select_all_full (false, true);
404 }
406 void sp_edit_invert_in_all_layers ()
407 {
408 sp_edit_select_all_full (true, true);
409 }
411 void sp_selection_group()
412 {
413 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
414 if (desktop == NULL)
415 return;
417 SPDocument *document = sp_desktop_document (desktop);
419 Inkscape::Selection *selection = sp_desktop_selection(desktop);
421 // Check if something is selected.
422 if (selection->isEmpty()) {
423 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>two or more objects</b> to group."));
424 return;
425 }
427 GSList const *l = (GSList *) selection->reprList();
429 // Check if at least two objects are selected.
430 if (l->next == NULL) {
431 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>at least two objects</b> to group."));
432 return;
433 }
435 GSList *p = g_slist_copy((GSList *) l);
437 selection->clear();
439 p = g_slist_sort(p, (GCompareFunc) sp_repr_compare_position);
441 // Remember the position and parent of the topmost object.
442 gint topmost = ((Inkscape::XML::Node *) g_slist_last(p)->data)->position();
443 Inkscape::XML::Node *topmost_parent = ((Inkscape::XML::Node *) g_slist_last(p)->data)->parent();
445 Inkscape::XML::Node *group = sp_repr_new("svg:g");
447 while (p) {
448 Inkscape::XML::Node *current = (Inkscape::XML::Node *) p->data;
450 if (current->parent() == topmost_parent) {
451 Inkscape::XML::Node *spnew = current->duplicate();
452 sp_repr_unparent(current);
453 group->appendChild(spnew);
454 Inkscape::GC::release(spnew);
455 topmost --; // only reduce count for those items deleted from topmost_parent
456 } else { // move it to topmost_parent first
457 GSList *temp_clip = NULL;
459 // At this point, current may already have no item, due to its being a clone whose original is already moved away
460 // So we copy it artificially calculating the transform from its repr->attr("transform") and the parent transform
461 gchar const *t_str = current->attribute("transform");
462 NR::Matrix item_t (NR::identity());
463 if (t_str)
464 sp_svg_transform_read(t_str, &item_t);
465 item_t *= sp_item_i2doc_affine(SP_ITEM(document->getObjectByRepr(current->parent())));
466 //FIXME: when moving both clone and original from a transformed group (either by
467 //grouping into another parent, or by cut/paste) the transform from the original's
468 //parent becomes embedded into original itself, and this affects its clones. Fix
469 //this by remembering the transform diffs we write to each item into an array and
470 //then, if this is clone, looking up its original in that array and pre-multiplying
471 //it by the inverse of that original's transform diff.
473 sp_selection_copy_one (current, item_t, &temp_clip);
474 sp_repr_unparent(current);
476 // paste into topmost_parent (temporarily)
477 GSList *copied = sp_selection_paste_impl (document, document->getObjectByRepr(topmost_parent), &temp_clip, NULL);
478 if (temp_clip) g_slist_free (temp_clip);
479 if (copied) { // if success,
480 // take pasted object (now in topmost_parent)
481 Inkscape::XML::Node *in_topmost = (Inkscape::XML::Node *) copied->data;
482 // make a copy
483 Inkscape::XML::Node *spnew = in_topmost->duplicate();
484 // remove pasted
485 sp_repr_unparent(in_topmost);
486 // put its copy into group
487 group->appendChild(spnew);
488 Inkscape::GC::release(spnew);
489 g_slist_free (copied);
490 }
491 }
492 p = g_slist_remove(p, current);
493 }
495 // Add the new group to the topmost members' parent
496 topmost_parent->appendChild(group);
498 // Move to the position of the topmost, reduced by the number of items deleted from topmost_parent
499 group->setPosition(topmost + 1);
501 sp_document_done(sp_desktop_document(desktop));
503 selection->set(group);
504 Inkscape::GC::release(group);
505 }
507 void sp_selection_ungroup()
508 {
509 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
510 if (desktop == NULL)
511 return;
513 Inkscape::Selection *selection = sp_desktop_selection(desktop);
515 if (selection->isEmpty()) {
516 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>group</b> to ungroup."));
517 return;
518 }
520 GSList *items = g_slist_copy((GSList *) selection->itemList());
521 selection->clear();
523 // Get a copy of current selection.
524 GSList *new_select = NULL;
525 bool ungrouped = false;
526 for (GSList *i = items;
527 i != NULL;
528 i = i->next)
529 {
530 SPItem *group = (SPItem *) i->data;
532 // when ungrouping cloned groups with their originals, some objects that were selected may no more exist due to unlinking
533 if (!SP_IS_OBJECT(group)) {
534 continue;
535 }
537 /* We do not allow ungrouping <svg> etc. (lauris) */
538 if (strcmp(SP_OBJECT_REPR(group)->name(), "svg:g") && strcmp(SP_OBJECT_REPR(group)->name(), "svg:switch")) {
539 // keep the non-group item in the new selection
540 selection->add(group);
541 continue;
542 }
544 GSList *children = NULL;
545 /* This is not strictly required, but is nicer to rely on group ::destroy (lauris) */
546 sp_item_group_ungroup(SP_GROUP(group), &children, false);
547 ungrouped = true;
548 // Add ungrouped items to the new selection.
549 new_select = g_slist_concat(new_select, children);
550 }
552 if (new_select) { // Set new selection.
553 selection->addList(new_select);
554 g_slist_free(new_select);
555 }
556 if (!ungrouped) {
557 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No groups</b> to ungroup in the selection."));
558 }
560 g_slist_free(items);
562 sp_document_done(sp_desktop_document(desktop));
563 }
565 static SPGroup *
566 sp_item_list_common_parent_group(const GSList *items)
567 {
568 if (!items) {
569 return NULL;
570 }
571 SPObject *parent = SP_OBJECT_PARENT(items->data);
572 /* Strictly speaking this CAN happen, if user selects <svg> from XML editor */
573 if (!SP_IS_GROUP(parent)) {
574 return NULL;
575 }
576 for (items = items->next; items; items = items->next) {
577 if (SP_OBJECT_PARENT(items->data) != parent) {
578 return NULL;
579 }
580 }
582 return SP_GROUP(parent);
583 }
585 /** Finds out the minimum common bbox of the selected items
586 */
587 NR::Rect
588 enclose_items(const GSList *items)
589 {
590 g_assert(items != NULL);
592 NR::Rect r = sp_item_bbox_desktop((SPItem *) items->data);
594 for (GSList *i = items->next; i; i = i->next) {
595 r = NR::Rect::union_bounds(r, sp_item_bbox_desktop((SPItem *) i->data));
596 }
598 return r;
599 }
601 SPObject *
602 prev_sibling(SPObject *child)
603 {
604 SPObject *parent = SP_OBJECT_PARENT(child);
605 if (!SP_IS_GROUP(parent)) {
606 return NULL;
607 }
608 for ( SPObject *i = sp_object_first_child(parent) ; i; i = SP_OBJECT_NEXT(i) ) {
609 if (i->next == child)
610 return i;
611 }
612 return NULL;
613 }
615 void
616 sp_selection_raise()
617 {
618 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
619 if (!desktop)
620 return;
622 Inkscape::Selection *selection = sp_desktop_selection(desktop);
624 GSList const *items = (GSList *) selection->itemList();
625 if (!items) {
626 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise."));
627 return;
628 }
630 SPGroup const *group = sp_item_list_common_parent_group(items);
631 if (!group) {
632 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
633 return;
634 }
636 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
638 /* construct reverse-ordered list of selected children */
639 GSList *rev = g_slist_copy((GSList *) items);
640 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
642 // find out the common bbox of the selected items
643 NR::Rect selected = enclose_items(items);
645 // for all objects in the selection (starting from top)
646 while (rev) {
647 SPObject *child = SP_OBJECT(rev->data);
648 // for each selected object, find the next sibling
649 for (SPObject *newref = child->next; newref; newref = newref->next) {
650 // if the sibling is an item AND overlaps our selection,
651 if (SP_IS_ITEM(newref) && selected.intersects(sp_item_bbox_desktop(SP_ITEM(newref)))) {
652 // AND if it's not one of our selected objects,
653 if (!g_slist_find((GSList *) items, newref)) {
654 // move the selected object after that sibling
655 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(newref));
656 }
657 break;
658 }
659 }
660 rev = g_slist_remove(rev, child);
661 }
663 sp_document_done(sp_desktop_document(desktop));
664 }
666 void sp_selection_raise_to_top()
667 {
668 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
669 if (desktop == NULL)
670 return;
672 SPDocument *document = sp_desktop_document(desktop);
673 Inkscape::Selection *selection = sp_desktop_selection(desktop);
675 if (selection->isEmpty()) {
676 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise to top."));
677 return;
678 }
680 GSList const *items = (GSList *) selection->itemList();
682 SPGroup const *group = sp_item_list_common_parent_group(items);
683 if (!group) {
684 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
685 return;
686 }
688 GSList *rl = g_slist_copy((GSList *) selection->reprList());
689 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
691 for (GSList *l = rl; l != NULL; l = l->next) {
692 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
693 repr->setPosition(-1);
694 }
696 g_slist_free(rl);
698 sp_document_done(document);
699 }
701 void
702 sp_selection_lower()
703 {
704 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
705 if (desktop == NULL)
706 return;
708 Inkscape::Selection *selection = sp_desktop_selection(desktop);
710 GSList const *items = (GSList *) selection->itemList();
711 if (!items) {
712 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower."));
713 return;
714 }
716 SPGroup const *group = sp_item_list_common_parent_group(items);
717 if (!group) {
718 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
719 return;
720 }
722 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
724 // find out the common bbox of the selected items
725 NR::Rect selected = enclose_items(items);
727 /* construct direct-ordered list of selected children */
728 GSList *rev = g_slist_copy((GSList *) items);
729 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
730 rev = g_slist_reverse(rev);
732 // for all objects in the selection (starting from top)
733 while (rev) {
734 SPObject *child = SP_OBJECT(rev->data);
735 // for each selected object, find the prev sibling
736 for (SPObject *newref = prev_sibling(child); newref; newref = prev_sibling(newref)) {
737 // if the sibling is an item AND overlaps our selection,
738 if (SP_IS_ITEM(newref) && selected.intersects(sp_item_bbox_desktop(SP_ITEM(newref)))) {
739 // AND if it's not one of our selected objects,
740 if (!g_slist_find((GSList *) items, newref)) {
741 // move the selected object before that sibling
742 SPObject *put_after = prev_sibling(newref);
743 if (put_after)
744 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(put_after));
745 else
746 SP_OBJECT_REPR(child)->setPosition(0);
747 }
748 break;
749 }
750 }
751 rev = g_slist_remove(rev, child);
752 }
754 sp_document_done(sp_desktop_document(desktop));
756 }
758 void sp_selection_lower_to_bottom()
759 {
760 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
761 if (desktop == NULL)
762 return;
764 SPDocument *document = sp_desktop_document(desktop);
765 Inkscape::Selection *selection = sp_desktop_selection(desktop);
767 if (selection->isEmpty()) {
768 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower to bottom."));
769 return;
770 }
772 GSList const *items = (GSList *) selection->itemList();
774 SPGroup const *group = sp_item_list_common_parent_group(items);
775 if (!group) {
776 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
777 return;
778 }
780 GSList *rl;
781 rl = g_slist_copy((GSList *) selection->reprList());
782 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
783 rl = g_slist_reverse(rl);
785 for (GSList *l = rl; l != NULL; l = l->next) {
786 gint minpos;
787 SPObject *pp, *pc;
788 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
789 pp = document->getObjectByRepr(sp_repr_parent(repr));
790 minpos = 0;
791 g_assert(SP_IS_GROUP(pp));
792 pc = sp_object_first_child(pp);
793 while (!SP_IS_ITEM(pc)) {
794 minpos += 1;
795 pc = pc->next;
796 }
797 repr->setPosition(minpos);
798 }
800 g_slist_free(rl);
802 sp_document_done(document);
803 }
805 void
806 sp_undo(SPDesktop *desktop, SPDocument *)
807 {
808 if (!sp_document_undo(sp_desktop_document(desktop)))
809 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to undo."));
810 }
812 void
813 sp_redo(SPDesktop *desktop, SPDocument *)
814 {
815 if (!sp_document_redo(sp_desktop_document(desktop)))
816 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to redo."));
817 }
819 void sp_selection_cut()
820 {
821 sp_selection_copy();
822 sp_selection_delete();
823 }
825 void sp_copy_gradient (GSList **defs_clip, SPGradient *gradient)
826 {
827 SPGradient *ref = gradient;
829 while (ref) {
830 // climb up the refs, copying each one in the chain
831 Inkscape::XML::Node *grad_repr = SP_OBJECT_REPR(ref)->duplicate();
832 *defs_clip = g_slist_prepend (*defs_clip, grad_repr);
834 ref = ref->ref->getObject();
835 }
836 }
838 void sp_copy_pattern (GSList **defs_clip, SPPattern *pattern)
839 {
840 SPPattern *ref = pattern;
842 while (ref) {
843 // climb up the refs, copying each one in the chain
844 Inkscape::XML::Node *pattern_repr = SP_OBJECT_REPR(ref)->duplicate();
845 *defs_clip = g_slist_prepend (*defs_clip, pattern_repr);
847 // items in the pattern may also use gradients and other patterns, so we need to recurse here as well
848 for (SPObject *child = sp_object_first_child(SP_OBJECT(ref)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
849 if (!SP_IS_ITEM (child))
850 continue;
851 sp_copy_stuff_used_by_item (defs_clip, (SPItem *) child, NULL);
852 }
854 ref = ref->ref->getObject();
855 }
856 }
858 void sp_copy_single (GSList **defs_clip, SPObject *thing)
859 {
860 Inkscape::XML::Node *duplicate_repr = SP_OBJECT_REPR(thing)->duplicate();
861 *defs_clip = g_slist_prepend (*defs_clip, duplicate_repr);
862 }
865 void sp_copy_textpath_path (GSList **defs_clip, SPTextPath *tp, const GSList *items)
866 {
867 SPItem *path = sp_textpath_get_path_item (tp);
868 if (!path)
869 return;
870 if (items && g_slist_find ((GSList *) items, path)) // do not copy it to defs if it is already in the list of items copied
871 return;
872 Inkscape::XML::Node *repr = SP_OBJECT_REPR(path)->duplicate();
873 *defs_clip = g_slist_prepend (*defs_clip, repr);
874 }
876 void sp_copy_stuff_used_by_item (GSList **defs_clip, SPItem *item, const GSList *items)
877 {
878 SPStyle *style = SP_OBJECT_STYLE (item);
880 if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
881 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
882 if (SP_IS_LINEARGRADIENT (server) || SP_IS_RADIALGRADIENT (server))
883 sp_copy_gradient (defs_clip, SP_GRADIENT(server));
884 if (SP_IS_PATTERN (server))
885 sp_copy_pattern (defs_clip, SP_PATTERN(server));
886 }
888 if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
889 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
890 if (SP_IS_LINEARGRADIENT (server) || SP_IS_RADIALGRADIENT (server))
891 sp_copy_gradient (defs_clip, SP_GRADIENT(server));
892 if (SP_IS_PATTERN (server))
893 sp_copy_pattern (defs_clip, SP_PATTERN(server));
894 }
896 if (SP_IS_SHAPE (item)) {
897 SPShape *shape = SP_SHAPE (item);
898 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
899 if (shape->marker[i]) {
900 sp_copy_single (defs_clip, SP_OBJECT (shape->marker[i]));
901 }
902 }
903 }
905 if (SP_IS_TEXT_TEXTPATH (item)) {
906 sp_copy_textpath_path (defs_clip, SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))), items);
907 }
909 if (item->clip_ref->getObject()) {
910 sp_copy_single (defs_clip, item->clip_ref->getObject());
911 }
913 if (item->mask_ref->getObject()) {
914 SPObject *mask = item->mask_ref->getObject();
915 sp_copy_single (defs_clip, mask);
916 // recurse into the mask for its gradients etc.
917 for (SPObject *o = SP_OBJECT(mask)->children; o != NULL; o = o->next) {
918 if (SP_IS_ITEM(o))
919 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (o), items);
920 }
921 }
923 // recurse
924 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
925 if (SP_IS_ITEM(o))
926 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (o), items);
927 }
928 }
930 /**
931 * \pre item != NULL
932 */
933 SPCSSAttr *
934 take_style_from_item (SPItem *item)
935 {
936 // write the complete cascaded style, context-free
937 SPCSSAttr *css = sp_css_attr_from_object (SP_OBJECT(item), SP_STYLE_FLAG_ALWAYS);
938 if (css == NULL)
939 return NULL;
941 if ((SP_IS_GROUP(item) && SP_OBJECT(item)->children) ||
942 (SP_IS_TEXT (item) && SP_OBJECT(item)->children && SP_OBJECT(item)->children->next == NULL)) {
943 // if this is a text with exactly one tspan child, merge the style of that tspan as well
944 // If this is a group, merge the style of its topmost (last) child with style
945 for (SPObject *last_element = item->lastChild(); last_element != NULL; last_element = SP_OBJECT_PREV (last_element)) {
946 if (SP_OBJECT_STYLE (last_element) != NULL) {
947 SPCSSAttr *temp = sp_css_attr_from_object (last_element, SP_STYLE_FLAG_IFSET);
948 if (temp) {
949 sp_repr_css_merge (css, temp);
950 sp_repr_css_attr_unref (temp);
951 }
952 break;
953 }
954 }
955 }
956 if (!(SP_IS_TEXT (item) || SP_IS_TSPAN (item) || SP_IS_STRING (item))) {
957 // do not copy text properties from non-text objects, it's confusing
958 css = sp_css_attr_unset_text (css);
959 }
961 // FIXME: also transform gradient/pattern fills, by forking? NO, this must be nondestructive
962 double ex = NR::expansion(sp_item_i2doc_affine(item));
963 if (ex != 1.0) {
964 css = sp_css_attr_scale (css, ex);
965 }
967 return css;
968 }
971 void sp_selection_copy()
972 {
973 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
974 if (desktop == NULL)
975 return;
977 Inkscape::Selection *selection = sp_desktop_selection(desktop);
979 if (tools_isactive (desktop, TOOLS_DROPPER)) {
980 sp_dropper_context_copy(desktop->event_context);
981 return; // copied color under cursor, nothing else to do
982 }
984 // check if something is selected
985 if (selection->isEmpty()) {
986 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing was copied."));
987 return;
988 }
990 const GSList *items = g_slist_copy ((GSList *) selection->itemList());
992 // 0. Copy text to system clipboard
993 // FIXME: for non-texts, put serialized XML as text to the clipboard;
994 //for this sp_repr_write_stream needs to be rewritten with iostream instead of FILE
995 Glib::ustring text;
996 if (tools_isactive (desktop, TOOLS_TEXT)) {
997 text = sp_text_get_selected_text(desktop->event_context);
998 }
1000 if (text.empty()) {
1001 guint texts = 0;
1002 for (GSList *i = (GSList *) items; i; i = i->next) {
1003 SPItem *item = SP_ITEM (i->data);
1004 if (SP_IS_TEXT (item) || SP_IS_FLOWTEXT(item)) {
1005 if (texts > 0) // if more than one text object is copied, separate them by spaces
1006 text += " ";
1007 gchar *this_text = sp_te_get_string_multiline (item);
1008 if (this_text) {
1009 text += this_text;
1010 g_free(this_text);
1011 }
1012 texts++;
1013 }
1014 }
1015 }
1016 if (!text.empty()) {
1017 Glib::RefPtr<Gtk::Clipboard> refClipboard = Gtk::Clipboard::get();
1018 refClipboard->set_text(text);
1019 }
1021 // clear old defs clipboard
1022 while (defs_clipboard) {
1023 Inkscape::GC::release((Inkscape::XML::Node *) defs_clipboard->data);
1024 defs_clipboard = g_slist_remove (defs_clipboard, defs_clipboard->data);
1025 }
1027 // clear style clipboard
1028 if (style_clipboard) {
1029 sp_repr_css_attr_unref (style_clipboard);
1030 style_clipboard = NULL;
1031 }
1033 //clear main clipboard
1034 while (clipboard) {
1035 Inkscape::GC::release((Inkscape::XML::Node *) clipboard->data);
1036 clipboard = g_slist_remove(clipboard, clipboard->data);
1037 }
1039 sp_selection_copy_impl (items, &clipboard, &defs_clipboard, &style_clipboard);
1041 if (tools_isactive (desktop, TOOLS_TEXT)) { // take style from cursor/text selection, overwriting the style just set by copy_impl
1042 SPStyle *const query = sp_style_new();
1043 if (sp_desktop_query_style_all (desktop, query)) {
1044 SPCSSAttr *css = sp_css_attr_from_style (query, SP_STYLE_FLAG_ALWAYS);
1045 if (css != NULL) {
1046 // clear style clipboard
1047 if (style_clipboard) {
1048 sp_repr_css_attr_unref (style_clipboard);
1049 style_clipboard = NULL;
1050 }
1051 //sp_repr_css_print (css);
1052 style_clipboard = css;
1053 }
1054 }
1055 g_free (query);
1056 }
1058 size_clipboard = selection->bounds();
1060 g_slist_free ((GSList *) items);
1061 }
1063 void sp_selection_paste(bool in_place)
1064 {
1065 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1067 if (desktop == NULL) {
1068 return;
1069 }
1071 SPDocument *document = sp_desktop_document(desktop);
1073 if (Inkscape::have_viable_layer(desktop, desktop->messageStack()) == false) {
1074 return;
1075 }
1077 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1079 if (tools_isactive (desktop, TOOLS_TEXT)) {
1080 if (sp_text_paste_inline(desktop->event_context))
1081 return; // pasted from system clipboard into text, nothing else to do
1082 }
1084 // check if something is in the clipboard
1085 if (clipboard == NULL) {
1086 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1087 return;
1088 }
1090 GSList *copied = sp_selection_paste_impl(document, desktop->currentLayer(), &clipboard, &defs_clipboard);
1091 // add pasted objects to selection
1092 selection->setReprList((GSList const *) copied);
1093 g_slist_free (copied);
1095 if (!in_place) {
1096 sp_document_ensure_up_to_date(document);
1098 NR::Point m( desktop->point() - selection->bounds().midpoint() );
1100 /* Snap the offset of the new item(s) to the grid */
1101 /* FIXME: this gridsnap fiddling is a hack. */
1102 Inkscape::GridSnapper &s = desktop->namedview->snap_manager.grid;
1103 gdouble const curr_gridsnap = s.getDistance();
1104 s.setDistance(NR_HUGE);
1105 m = s.freeSnap(Inkscape::Snapper::SNAP_POINT, m, NULL).getPoint();
1106 s.setDistance(curr_gridsnap);
1107 sp_selection_move_relative(selection, m);
1108 }
1110 sp_document_done(document);
1111 }
1113 void sp_selection_paste_style()
1114 {
1115 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1116 if (desktop == NULL) return;
1118 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1120 // check if something is in the clipboard
1121 if (clipboard == NULL) {
1122 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1123 return;
1124 }
1126 // check if something is selected
1127 if (selection->isEmpty()) {
1128 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste style to."));
1129 return;
1130 }
1132 paste_defs (&defs_clipboard, sp_desktop_document(desktop));
1134 sp_desktop_set_style (desktop, style_clipboard);
1136 sp_document_done(sp_desktop_document (desktop));
1137 }
1139 void sp_selection_paste_size (bool apply_x, bool apply_y)
1140 {
1141 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1142 if (desktop == NULL) return;
1144 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1146 // check if something is in the clipboard
1147 if (size_clipboard.extent(NR::X) < 1e-6 || size_clipboard.extent(NR::Y) < 1e-6) {
1148 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1149 return;
1150 }
1152 // check if something is selected
1153 if (selection->isEmpty()) {
1154 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste size to."));
1155 return;
1156 }
1158 NR::Rect current = selection->bounds();
1159 if (current.extent(NR::X) < 1e-6 || current.extent(NR::Y) < 1e-6) {
1160 return;
1161 }
1163 double scale_x = size_clipboard.extent(NR::X) / current.extent(NR::X);
1164 double scale_y = size_clipboard.extent(NR::Y) / current.extent(NR::Y);
1166 sp_selection_scale_relative (selection, current.midpoint(),
1167 NR::scale(
1168 apply_x? scale_x : (desktop->isToolboxButtonActive ("lock")? scale_y : 1.0),
1169 apply_y? scale_y : (desktop->isToolboxButtonActive ("lock")? scale_x : 1.0)));
1171 sp_document_done(sp_desktop_document (desktop));
1172 }
1174 void sp_selection_paste_size_separately (bool apply_x, bool apply_y)
1175 {
1176 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1177 if (desktop == NULL) return;
1179 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1181 // check if something is in the clipboard
1182 if (size_clipboard.extent(NR::X) < 1e-6 || size_clipboard.extent(NR::Y) < 1e-6) {
1183 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1184 return;
1185 }
1187 // check if something is selected
1188 if (selection->isEmpty()) {
1189 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste size to."));
1190 return;
1191 }
1193 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1194 SPItem *item = SP_ITEM(l->data);
1196 NR::Rect current = sp_item_bbox_desktop(item);
1197 if (current.extent(NR::X) < 1e-6 || current.extent(NR::Y) < 1e-6) {
1198 continue;
1199 }
1201 double scale_x = size_clipboard.extent(NR::X) / current.extent(NR::X);
1202 double scale_y = size_clipboard.extent(NR::Y) / current.extent(NR::Y);
1204 sp_item_scale_rel (item,
1205 NR::scale(
1206 apply_x? scale_x : (desktop->isToolboxButtonActive ("lock")? scale_y : 1.0),
1207 apply_y? scale_y : (desktop->isToolboxButtonActive ("lock")? scale_x : 1.0)));
1209 }
1211 sp_document_done(sp_desktop_document (desktop));
1212 }
1214 void sp_selection_to_next_layer ()
1215 {
1216 SPDesktop *dt = SP_ACTIVE_DESKTOP;
1218 Inkscape::Selection *selection = sp_desktop_selection(dt);
1220 // check if something is selected
1221 if (selection->isEmpty()) {
1222 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer above."));
1223 return;
1224 }
1226 const GSList *items = g_slist_copy ((GSList *) selection->itemList());
1228 SPObject *next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer());
1229 if (next) {
1230 GSList *temp_clip = NULL;
1231 sp_selection_copy_impl (items, &temp_clip, NULL, NULL); // we're in the same doc, so no need to copy defs
1232 sp_selection_delete_impl (items);
1233 next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
1234 GSList *copied;
1235 if(next) {
1236 copied = sp_selection_paste_impl (sp_desktop_document (dt), next, &temp_clip, NULL);
1237 } else {
1238 copied = sp_selection_paste_impl (sp_desktop_document (dt), dt->currentLayer(), &temp_clip, NULL);
1239 }
1240 selection->setReprList((GSList const *) copied);
1241 g_slist_free (copied);
1242 if (temp_clip) g_slist_free (temp_clip);
1243 dt->setCurrentLayer(next);
1244 sp_document_done(sp_desktop_document (dt));
1245 } else {
1246 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers above."));
1247 }
1249 g_slist_free ((GSList *) items);
1250 }
1252 void sp_selection_to_prev_layer ()
1253 {
1254 SPDesktop *dt = SP_ACTIVE_DESKTOP;
1256 Inkscape::Selection *selection = sp_desktop_selection(dt);
1258 // check if something is selected
1259 if (selection->isEmpty()) {
1260 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer below."));
1261 return;
1262 }
1264 const GSList *items = g_slist_copy ((GSList *) selection->itemList());
1266 SPObject *next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer());
1267 if (next) {
1268 GSList *temp_clip = NULL;
1269 sp_selection_copy_impl (items, &temp_clip, NULL, NULL); // we're in the same doc, so no need to copy defs
1270 sp_selection_delete_impl (items);
1271 next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
1272 GSList *copied;
1273 if(next) {
1274 copied = sp_selection_paste_impl (sp_desktop_document (dt), next, &temp_clip, NULL);
1275 } else {
1276 copied = sp_selection_paste_impl (sp_desktop_document (dt), dt->currentLayer(), &temp_clip, NULL);
1277 }
1278 selection->setReprList((GSList const *) copied);
1279 g_slist_free (copied);
1280 if (temp_clip) g_slist_free (temp_clip);
1281 dt->setCurrentLayer(next);
1282 sp_document_done(sp_desktop_document (dt));
1283 } else {
1284 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers below."));
1285 }
1287 g_slist_free ((GSList *) items);
1288 }
1290 /** Apply matrix to the selection. \a set_i2d is normally true, which means objects are in the
1291 original transform, synced with their reprs, and need to jump to the new transform in one go. A
1292 value of set_i2d==false is only used by seltrans when it's dragging objects live (not outlines); in
1293 that case, items are already in the new position, but the repr is in the old, and this function
1294 then simply updates the repr from item->transform.
1295 */
1296 void sp_selection_apply_affine(Inkscape::Selection *selection, NR::Matrix const &affine, bool set_i2d)
1297 {
1298 if (selection->isEmpty())
1299 return;
1301 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1302 SPItem *item = SP_ITEM(l->data);
1304 NR::Point old_center(0,0);
1305 if (set_i2d && item->isCenterSet())
1306 old_center = item->getCenter();
1308 #if 0 /* Re-enable this once persistent guides have a graphical indication.
1309 At the time of writing, this is the only place to re-enable. */
1310 sp_item_update_cns(*item, selection->desktop());
1311 #endif
1313 // we're moving both a clone and its original
1314 bool transform_clone_with_original = (SP_IS_USE(item) && selection->includes( sp_use_get_original (SP_USE(item)) ));
1315 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)))) ));
1316 bool transform_flowtext_with_frame = (SP_IS_FLOWTEXT(item) && selection->includes( SP_FLOWTEXT(item)->get_frame (NULL))); // only the first frame so far
1317 bool transform_offset_with_source = (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) && selection->includes( sp_offset_get_source (SP_OFFSET(item)) );
1319 // "clones are unmoved when original is moved" preference
1320 int compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
1321 bool prefs_unmoved = (compensation == SP_CLONE_COMPENSATION_UNMOVED);
1322 bool prefs_parallel = (compensation == SP_CLONE_COMPENSATION_PARALLEL);
1324 // If this is a clone and it's selected along with its original, do not move it; it will feel the
1325 // transform of its original and respond to it itself. Without this, a clone is doubly
1326 // transformed, very unintuitive.
1327 // Same for textpath if we are also doing ANY transform to its path: do not touch textpath,
1328 // letters cannot be squeezed or rotated anyway, they only refill the changed path.
1329 // Same for linked offset if we are also moving its source: do not move it.
1330 if (transform_textpath_with_path || transform_offset_with_source) {
1331 // restore item->transform field from the repr, in case it was changed by seltrans
1332 sp_object_read_attr (SP_OBJECT (item), "transform");
1334 } else if (transform_flowtext_with_frame) {
1335 // apply the inverse of the region's transform to the <use> so that the flow remains
1336 // the same (even though the output itself gets transformed)
1337 for (SPObject *region = item->firstChild() ; region ; region = SP_OBJECT_NEXT(region)) {
1338 if (!SP_IS_FLOWREGION(region) && !SP_IS_FLOWREGIONEXCLUDE(region))
1339 continue;
1340 for (SPObject *use = region->firstChild() ; use ; use = SP_OBJECT_NEXT(use)) {
1341 if (!SP_IS_USE(use)) continue;
1342 sp_item_write_transform(SP_USE(use), SP_OBJECT_REPR(use), item->transform.inverse(), NULL);
1343 }
1344 }
1345 } else if (transform_clone_with_original) {
1346 // We are transforming a clone along with its original. The below matrix juggling is
1347 // necessary to ensure that they transform as a whole, i.e. the clone's induced
1348 // transform and its move compensation are both cancelled out.
1350 // restore item->transform field from the repr, in case it was changed by seltrans
1351 sp_object_read_attr (SP_OBJECT (item), "transform");
1353 // calculate the matrix we need to apply to the clone to cancel its induced transform from its original
1354 NR::Matrix t = matrix_to_desktop (matrix_from_desktop (affine, item), item);
1355 NR::Matrix t_inv = matrix_to_desktop (matrix_from_desktop (affine.inverse(), item), item);
1356 NR::Matrix result = t_inv * item->transform * t;
1358 if ((prefs_parallel || prefs_unmoved) && affine.is_translation()) {
1359 // we need to cancel out the move compensation, too
1361 // find out the clone move, same as in sp_use_move_compensate
1362 NR::Matrix parent = sp_use_get_parent_transform (SP_USE(item));
1363 NR::Matrix clone_move = parent.inverse() * t * parent;
1365 if (prefs_parallel) {
1366 NR::Matrix move = result * clone_move * t_inv;
1367 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &move);
1369 } else if (prefs_unmoved) {
1370 if (SP_IS_USE(sp_use_get_original(SP_USE(item))))
1371 clone_move = NR::identity();
1372 NR::Matrix move = result * clone_move;
1373 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &move);
1374 }
1376 } else {
1377 // just apply the result
1378 sp_item_write_transform(item, SP_OBJECT_REPR(item), result, &result);
1379 }
1381 } else {
1382 if (set_i2d) {
1383 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * affine);
1384 }
1385 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, NULL);
1386 }
1388 // if we're moving the actual object, not just updating the repr, we can transform the
1389 // center by the same matrix (only necessary for non-translations)
1390 if (set_i2d && item->isCenterSet() && !affine.is_translation()) {
1391 item->setCenter(old_center * affine);
1392 SP_OBJECT(item)->updateRepr();
1393 }
1394 }
1395 }
1397 void sp_selection_remove_transform()
1398 {
1399 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1400 if (desktop == NULL)
1401 return;
1403 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1405 GSList const *l = (GSList *) selection->reprList();
1406 while (l != NULL) {
1407 sp_repr_set_attr((Inkscape::XML::Node*)l->data, "transform", NULL);
1408 l = l->next;
1409 }
1411 sp_document_done(sp_desktop_document(desktop));
1412 }
1414 void
1415 sp_selection_scale_absolute(Inkscape::Selection *selection,
1416 double const x0, double const x1,
1417 double const y0, double const y1)
1418 {
1419 if (selection->isEmpty())
1420 return;
1422 NR::Rect const bbox(selection->bounds());
1423 if (bbox.isEmpty()) {
1424 return;
1425 }
1427 NR::translate const p2o(-bbox.min());
1429 NR::scale const newSize(x1 - x0,
1430 y1 - y0);
1431 NR::scale const scale( newSize / NR::scale(bbox.dimensions()) );
1432 NR::translate const o2n(x0, y0);
1433 NR::Matrix const final( p2o * scale * o2n );
1435 sp_selection_apply_affine(selection, final);
1436 }
1439 void sp_selection_scale_relative(Inkscape::Selection *selection, NR::Point const &align, NR::scale const &scale)
1440 {
1441 if (selection->isEmpty())
1442 return;
1444 NR::Rect const bbox(selection->bounds());
1446 if (bbox.isEmpty()) {
1447 return;
1448 }
1450 // FIXME: ARBITRARY LIMIT: don't try to scale above 1 Mpx, it won't display properly and will crash sooner or later anyway
1451 if ( bbox.extent(NR::X) * scale[NR::X] > 1e6 ||
1452 bbox.extent(NR::Y) * scale[NR::Y] > 1e6 )
1453 {
1454 return;
1455 }
1457 NR::translate const n2d(-align);
1458 NR::translate const d2n(align);
1459 NR::Matrix const final( n2d * scale * d2n );
1460 sp_selection_apply_affine(selection, final);
1461 }
1463 void
1464 sp_selection_rotate_relative(Inkscape::Selection *selection, NR::Point const ¢er, gdouble const angle_degrees)
1465 {
1466 NR::translate const d2n(center);
1467 NR::translate const n2d(-center);
1468 NR::rotate const rotate(rotate_degrees(angle_degrees));
1469 NR::Matrix const final( NR::Matrix(n2d) * rotate * d2n );
1470 sp_selection_apply_affine(selection, final);
1471 }
1473 void
1474 sp_selection_skew_relative(Inkscape::Selection *selection, NR::Point const &align, double dx, double dy)
1475 {
1476 NR::translate const d2n(align);
1477 NR::translate const n2d(-align);
1478 NR::Matrix const skew(1, dy,
1479 dx, 1,
1480 0, 0);
1481 NR::Matrix const final( n2d * skew * d2n );
1482 sp_selection_apply_affine(selection, final);
1483 }
1485 void sp_selection_move_relative(Inkscape::Selection *selection, NR::Point const &move)
1486 {
1487 sp_selection_apply_affine(selection, NR::Matrix(NR::translate(move)));
1488 }
1490 void sp_selection_move_relative(Inkscape::Selection *selection, double dx, double dy)
1491 {
1492 sp_selection_apply_affine(selection, NR::Matrix(NR::translate(dx, dy)));
1493 }
1496 /**
1497 * \brief sp_selection_rotate_90
1498 *
1499 * This function rotates selected objects 90 degrees clockwise.
1500 *
1501 */
1503 void sp_selection_rotate_90_cw()
1504 {
1505 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1507 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1509 if (selection->isEmpty())
1510 return;
1512 GSList const *l = selection->itemList();
1513 NR::rotate const rot_neg_90(NR::Point(0, -1));
1514 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1515 SPItem *item = SP_ITEM(l2->data);
1516 sp_item_rotate_rel(item, rot_neg_90);
1517 }
1519 sp_document_done(sp_desktop_document(desktop));
1520 }
1523 /**
1524 * \brief sp_selection_rotate_90_ccw
1525 *
1526 * This function rotates selected objects 90 degrees counter-clockwise.
1527 *
1528 */
1530 void sp_selection_rotate_90_ccw()
1531 {
1532 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1534 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1536 if (selection->isEmpty())
1537 return;
1539 GSList const *l = selection->itemList();
1540 NR::rotate const rot_neg_90(NR::Point(0, 1));
1541 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1542 SPItem *item = SP_ITEM(l2->data);
1543 sp_item_rotate_rel(item, rot_neg_90);
1544 }
1546 sp_document_done(sp_desktop_document(desktop));
1547 }
1549 void
1550 sp_selection_rotate(Inkscape::Selection *selection, gdouble const angle_degrees)
1551 {
1552 if (selection->isEmpty())
1553 return;
1555 NR::Point center = selection->center();
1557 sp_selection_rotate_relative(selection, center, angle_degrees);
1559 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1560 ( ( angle_degrees > 0 )
1561 ? "selector:rotate:ccw"
1562 : "selector:rotate:cw" ));
1563 }
1565 /**
1566 \param angle the angle in "angular pixels", i.e. how many visible pixels must move the outermost point of the rotated object
1567 */
1568 void
1569 sp_selection_rotate_screen(Inkscape::Selection *selection, gdouble angle)
1570 {
1571 if (selection->isEmpty())
1572 return;
1574 NR::Rect const bbox(selection->bounds());
1576 NR::Point center = selection->center();
1578 gdouble const zoom = selection->desktop()->current_zoom();
1579 gdouble const zmove = angle / zoom;
1580 gdouble const r = NR::L2(bbox.max() - center);
1582 gdouble const zangle = 180 * atan2(zmove, r) / M_PI;
1584 sp_selection_rotate_relative(selection, center, zangle);
1586 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1587 ( (angle > 0)
1588 ? "selector:rotate:ccw"
1589 : "selector:rotate:cw" ));
1590 }
1592 void
1593 sp_selection_scale(Inkscape::Selection *selection, gdouble grow)
1594 {
1595 if (selection->isEmpty())
1596 return;
1598 NR::Rect const bbox(selection->bounds());
1599 NR::Point const center(bbox.midpoint());
1600 double const max_len = bbox.maxExtent();
1602 // you can't scale "do nizhe pola" (below zero)
1603 if ( max_len + grow <= 1e-3 ) {
1604 return;
1605 }
1607 double const times = 1.0 + grow / max_len;
1608 sp_selection_scale_relative(selection, center, NR::scale(times, times));
1610 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1611 ( (grow > 0)
1612 ? "selector:scale:larger"
1613 : "selector:scale:smaller" ));
1614 }
1616 void
1617 sp_selection_scale_screen(Inkscape::Selection *selection, gdouble grow_pixels)
1618 {
1619 sp_selection_scale(selection,
1620 grow_pixels / selection->desktop()->current_zoom());
1621 }
1623 void
1624 sp_selection_scale_times(Inkscape::Selection *selection, gdouble times)
1625 {
1626 if (selection->isEmpty())
1627 return;
1629 NR::Point const center(selection->bounds().midpoint());
1630 sp_selection_scale_relative(selection, center, NR::scale(times, times));
1631 sp_document_done(sp_desktop_document(selection->desktop()));
1632 }
1634 void
1635 sp_selection_move(gdouble dx, gdouble dy)
1636 {
1637 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1638 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1639 if (selection->isEmpty()) {
1640 return;
1641 }
1643 sp_selection_move_relative(selection, dx, dy);
1645 if (dx == 0) {
1646 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical");
1647 } else if (dy == 0) {
1648 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal");
1649 } else {
1650 sp_document_done(sp_desktop_document(desktop));
1651 }
1652 }
1654 void
1655 sp_selection_move_screen(gdouble dx, gdouble dy)
1656 {
1657 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1659 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1660 if (selection->isEmpty()) {
1661 return;
1662 }
1664 // same as sp_selection_move but divide deltas by zoom factor
1665 gdouble const zoom = desktop->current_zoom();
1666 gdouble const zdx = dx / zoom;
1667 gdouble const zdy = dy / zoom;
1668 sp_selection_move_relative(selection, zdx, zdy);
1670 if (dx == 0) {
1671 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical");
1672 } else if (dy == 0) {
1673 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal");
1674 } else {
1675 sp_document_done(sp_desktop_document(desktop));
1676 }
1677 }
1679 namespace {
1681 template <typename D>
1682 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
1683 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
1685 template <typename D>
1686 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items, SPObject *root,
1687 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
1689 struct Forward {
1690 typedef SPObject *Iterator;
1692 static Iterator children(SPObject *o) { return sp_object_first_child(o); }
1693 static Iterator siblings_after(SPObject *o) { return SP_OBJECT_NEXT(o); }
1694 static void dispose(Iterator i) {}
1696 static SPObject *object(Iterator i) { return i; }
1697 static Iterator next(Iterator i) { return SP_OBJECT_NEXT(i); }
1698 };
1700 struct Reverse {
1701 typedef GSList *Iterator;
1703 static Iterator children(SPObject *o) {
1704 return make_list(o->firstChild(), NULL);
1705 }
1706 static Iterator siblings_after(SPObject *o) {
1707 return make_list(SP_OBJECT_PARENT(o)->firstChild(), o);
1708 }
1709 static void dispose(Iterator i) {
1710 g_slist_free(i);
1711 }
1713 static SPObject *object(Iterator i) {
1714 return reinterpret_cast<SPObject *>(i->data);
1715 }
1716 static Iterator next(Iterator i) { return i->next; }
1718 private:
1719 static GSList *make_list(SPObject *object, SPObject *limit) {
1720 GSList *list=NULL;
1721 while ( object != limit ) {
1722 list = g_slist_prepend(list, object);
1723 object = SP_OBJECT_NEXT(object);
1724 }
1725 return list;
1726 }
1727 };
1729 }
1731 void
1732 sp_selection_item_next(void)
1733 {
1734 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1735 g_return_if_fail(desktop != NULL);
1736 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1738 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
1739 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
1740 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
1742 SPObject *root;
1743 if (PREFS_SELECTION_ALL != inlayer) {
1744 root = selection->activeContext();
1745 } else {
1746 root = desktop->currentRoot();
1747 }
1749 SPItem *item=next_item_from_list<Forward>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1751 if (item) {
1752 selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
1753 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1754 scroll_to_show_item(desktop, item);
1755 }
1756 }
1757 }
1759 void
1760 sp_selection_item_prev(void)
1761 {
1762 SPDocument *document = SP_ACTIVE_DOCUMENT;
1763 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1764 g_return_if_fail(document != NULL);
1765 g_return_if_fail(desktop != NULL);
1766 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1768 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
1769 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
1770 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
1772 SPObject *root;
1773 if (PREFS_SELECTION_ALL != inlayer) {
1774 root = selection->activeContext();
1775 } else {
1776 root = desktop->currentRoot();
1777 }
1779 SPItem *item=next_item_from_list<Reverse>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1781 if (item) {
1782 selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
1783 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1784 scroll_to_show_item(desktop, item);
1785 }
1786 }
1787 }
1789 namespace {
1791 template <typename D>
1792 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items,
1793 SPObject *root, bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
1794 {
1795 SPObject *current=root;
1796 while (items) {
1797 SPItem *item=SP_ITEM(items->data);
1798 if ( root->isAncestorOf(item) &&
1799 ( !only_in_viewport || desktop->isWithinViewport(item) ) )
1800 {
1801 current = item;
1802 break;
1803 }
1804 items = items->next;
1805 }
1807 GSList *path=NULL;
1808 while ( current != root ) {
1809 path = g_slist_prepend(path, current);
1810 current = SP_OBJECT_PARENT(current);
1811 }
1813 SPItem *next;
1814 // first, try from the current object
1815 next = next_item<D>(desktop, path, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1816 g_slist_free(path);
1818 if (!next) { // if we ran out of objects, start over at the root
1819 next = next_item<D>(desktop, NULL, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1820 }
1822 return next;
1823 }
1825 template <typename D>
1826 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
1827 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
1828 {
1829 typename D::Iterator children;
1830 typename D::Iterator iter;
1832 SPItem *found=NULL;
1834 if (path) {
1835 SPObject *object=reinterpret_cast<SPObject *>(path->data);
1836 g_assert(SP_OBJECT_PARENT(object) == root);
1837 if (desktop->isLayer(object)) {
1838 found = next_item<D>(desktop, path->next, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1839 }
1840 iter = children = D::siblings_after(object);
1841 } else {
1842 iter = children = D::children(root);
1843 }
1845 while ( iter && !found ) {
1846 SPObject *object=D::object(iter);
1847 if (desktop->isLayer(object)) {
1848 if (PREFS_SELECTION_LAYER != inlayer) { // recurse into sublayers
1849 found = next_item<D>(desktop, NULL, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1850 }
1851 } else if ( SP_IS_ITEM(object) &&
1852 ( !only_in_viewport || desktop->isWithinViewport(SP_ITEM(object)) ) &&
1853 ( !onlyvisible || !desktop->itemIsHidden(SP_ITEM(object))) &&
1854 ( !onlysensitive || !SP_ITEM(object)->isLocked()) &&
1855 !desktop->isLayer(SP_ITEM(object)) )
1856 {
1857 found = SP_ITEM(object);
1858 }
1859 iter = D::next(iter);
1860 }
1862 D::dispose(children);
1864 return found;
1865 }
1867 }
1869 /**
1870 * If \a item is not entirely visible then adjust visible area to centre on the centre on of
1871 * \a item.
1872 */
1873 void scroll_to_show_item(SPDesktop *desktop, SPItem *item)
1874 {
1875 NR::Rect dbox = desktop->get_display_area();
1876 NR::Rect sbox = sp_item_bbox_desktop(item);
1878 if (dbox.contains(sbox) == false) {
1879 NR::Point const s_dt = sbox.midpoint();
1880 NR::Point const s_w = desktop->d2w(s_dt);
1881 NR::Point const d_dt = dbox.midpoint();
1882 NR::Point const d_w = desktop->d2w(d_dt);
1883 NR::Point const moved_w( d_w - s_w );
1884 gint const dx = (gint) moved_w[X];
1885 gint const dy = (gint) moved_w[Y];
1886 desktop->scroll_world(dx, dy);
1887 }
1888 }
1891 void
1892 sp_selection_clone()
1893 {
1894 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1895 if (desktop == NULL)
1896 return;
1898 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1900 // check if something is selected
1901 if (selection->isEmpty()) {
1902 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object</b> to clone."));
1903 return;
1904 }
1906 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
1908 selection->clear();
1910 // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
1911 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
1913 GSList *newsel = NULL;
1915 while (reprs) {
1916 Inkscape::XML::Node *sel_repr = (Inkscape::XML::Node *) reprs->data;
1917 Inkscape::XML::Node *parent = sp_repr_parent(sel_repr);
1919 Inkscape::XML::Node *clone = sp_repr_new("svg:use");
1920 sp_repr_set_attr(clone, "x", "0");
1921 sp_repr_set_attr(clone, "y", "0");
1922 sp_repr_set_attr(clone, "xlink:href", g_strdup_printf("#%s", sel_repr->attribute("id")));
1924 sp_repr_set_attr(clone, "inkscape:transform-center-x", sel_repr->attribute("inkscape:transform-center-x"));
1925 sp_repr_set_attr(clone, "inkscape:transform-center-y", sel_repr->attribute("inkscape:transform-center-y"));
1927 // add the new clone to the top of the original's parent
1928 parent->appendChild(clone);
1930 newsel = g_slist_prepend(newsel, clone);
1931 reprs = g_slist_remove(reprs, sel_repr);
1932 Inkscape::GC::release(clone);
1933 }
1935 sp_document_done(sp_desktop_document(desktop));
1937 selection->setReprList(newsel);
1939 g_slist_free(newsel);
1940 }
1942 void
1943 sp_selection_unlink()
1944 {
1945 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1946 if (!desktop)
1947 return;
1949 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1951 if (selection->isEmpty()) {
1952 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>clone</b> to unlink."));
1953 return;
1954 }
1956 // Get a copy of current selection.
1957 GSList *new_select = NULL;
1958 bool unlinked = false;
1959 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
1960 items != NULL;
1961 items = items->next)
1962 {
1963 SPItem *use = (SPItem *) items->data;
1965 if (!SP_IS_USE(use)) {
1966 // keep the non-yse item in the new selection
1967 new_select = g_slist_prepend(new_select, use);
1968 continue;
1969 }
1971 SPItem *unlink = sp_use_unlink(SP_USE(use));
1972 unlinked = true;
1973 // Add ungrouped items to the new selection.
1974 new_select = g_slist_prepend(new_select, unlink);
1975 }
1977 if (new_select) { // set new selection
1978 selection->clear();
1979 selection->setList(new_select);
1980 g_slist_free(new_select);
1981 }
1982 if (!unlinked) {
1983 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No clones to unlink</b> in the selection."));
1984 }
1986 sp_document_done(sp_desktop_document(desktop));
1987 }
1989 void
1990 sp_select_clone_original()
1991 {
1992 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1993 if (desktop == NULL)
1994 return;
1996 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1998 SPItem *item = selection->singleItem();
2000 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.");
2002 // Check if other than two objects are selected
2003 if (g_slist_length((GSList *) selection->itemList()) != 1 || !item) {
2004 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
2005 return;
2006 }
2008 SPItem *original = NULL;
2009 if (SP_IS_USE(item)) {
2010 original = sp_use_get_original (SP_USE(item));
2011 } else if (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) {
2012 original = sp_offset_get_source (SP_OFFSET(item));
2013 } else if (SP_IS_TEXT_TEXTPATH(item)) {
2014 original = sp_textpath_get_path_item (SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))));
2015 } else if (SP_IS_FLOWTEXT(item)) {
2016 original = SP_FLOWTEXT(item)->get_frame (NULL); // first frame only
2017 } else { // it's an object that we don't know what to do with
2018 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
2019 return;
2020 }
2022 if (!original) {
2023 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>Cannot find</b> the object to select (orphaned clone, offset, textpath, flowed text?)"));
2024 return;
2025 }
2027 for (SPObject *o = original; o && !SP_IS_ROOT(o); o = SP_OBJECT_PARENT (o)) {
2028 if (SP_IS_DEFS (o)) {
2029 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("The object you're trying to select is <b>not visible</b> (it is in <defs>)"));
2030 return;
2031 }
2032 }
2034 if (original) {
2035 selection->clear();
2036 selection->set(original);
2037 if (SP_CYCLING == SP_CYCLE_FOCUS) {
2038 scroll_to_show_item(desktop, original);
2039 }
2040 }
2041 }
2043 void
2044 sp_selection_tile(bool apply)
2045 {
2046 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2047 if (desktop == NULL)
2048 return;
2050 SPDocument *document = sp_desktop_document(desktop);
2052 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2054 // check if something is selected
2055 if (selection->isEmpty()) {
2056 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to pattern."));
2057 return;
2058 }
2060 sp_document_ensure_up_to_date(document);
2061 NR::Rect r = selection->bounds();
2062 if (r.isEmpty()) {
2063 return;
2064 }
2066 // calculate the transform to be applied to objects to move them to 0,0
2067 NR::Point move_p = NR::Point(0, sp_document_height(document)) - (r.min() + NR::Point (0, r.extent(NR::Y)));
2068 move_p[NR::Y] = -move_p[NR::Y];
2069 NR::Matrix move = NR::Matrix (NR::translate (move_p));
2071 GSList *items = g_slist_copy((GSList *) selection->itemList());
2073 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2075 // bottommost object, after sorting
2076 SPObject *parent = SP_OBJECT_PARENT (items->data);
2078 // remember the position of the first item
2079 gint pos = SP_OBJECT_REPR (items->data)->position();
2081 // create a list of duplicates
2082 GSList *repr_copies = NULL;
2083 for (GSList *i = items; i != NULL; i = i->next) {
2084 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate();
2085 repr_copies = g_slist_prepend (repr_copies, dup);
2086 }
2088 NR::Rect bounds(desktop->dt2doc(r.min()), desktop->dt2doc(r.max()));
2090 if (apply) {
2091 // delete objects so that their clones don't get alerted; this object will be restored shortly
2092 for (GSList *i = items; i != NULL; i = i->next) {
2093 SPObject *item = SP_OBJECT (i->data);
2094 item->deleteObject (false);
2095 }
2096 }
2098 // Hack: Temporarily set clone compensation to unmoved, so that we can move clone-originals
2099 // without disturbing clones.
2100 // See ActorAlign::on_button_click() in src/ui/dialog/align-and-distribute.cpp
2101 int saved_compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2102 prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2104 const gchar *pat_id = pattern_tile (repr_copies, bounds, document,
2105 NR::Matrix(NR::translate(desktop->dt2doc(NR::Point(r.min()[NR::X], r.max()[NR::Y])))), move);
2107 // restore compensation setting
2108 prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
2110 if (apply) {
2111 Inkscape::XML::Node *rect = sp_repr_new ("svg:rect");
2112 rect->setAttribute("style", g_strdup_printf("stroke:none;fill:url(#%s)", pat_id));
2113 sp_repr_set_svg_double(rect, "width", bounds.extent(NR::X));
2114 sp_repr_set_svg_double(rect, "height", bounds.extent(NR::Y));
2115 sp_repr_set_svg_double(rect, "x", bounds.min()[NR::X]);
2116 sp_repr_set_svg_double(rect, "y", bounds.min()[NR::Y]);
2118 // restore parent and position
2119 SP_OBJECT_REPR (parent)->appendChild(rect);
2120 rect->setPosition(pos > 0 ? pos : 0);
2121 SPItem *rectangle = (SPItem *) sp_desktop_document (desktop)->getObjectByRepr(rect);
2123 Inkscape::GC::release(rect);
2125 selection->clear();
2126 selection->set(rectangle);
2127 }
2129 g_slist_free (items);
2131 sp_document_done (document);
2132 }
2134 void
2135 sp_selection_untile()
2136 {
2137 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2138 if (desktop == NULL)
2139 return;
2141 SPDocument *document = sp_desktop_document(desktop);
2143 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2145 // check if something is selected
2146 if (selection->isEmpty()) {
2147 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object with pattern fill</b> to extract objects from."));
2148 return;
2149 }
2151 GSList *new_select = NULL;
2153 bool did = false;
2155 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2156 items != NULL;
2157 items = items->next) {
2159 SPItem *item = (SPItem *) items->data;
2161 SPStyle *style = SP_OBJECT_STYLE (item);
2163 if (!style || style->fill.type != SP_PAINT_TYPE_PAINTSERVER)
2164 continue;
2166 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
2168 if (!SP_IS_PATTERN(server))
2169 continue;
2171 did = true;
2173 SPPattern *pattern = pattern_getroot (SP_PATTERN (server));
2175 NR::Matrix pat_transform = pattern_patternTransform (SP_PATTERN (server));
2176 pat_transform *= item->transform;
2178 for (SPObject *child = sp_object_first_child(SP_OBJECT(pattern)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2179 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate();
2180 SPItem *i = SP_ITEM (desktop->currentLayer()->appendChildRepr(copy));
2182 // FIXME: relink clones to the new canvas objects
2183 // use SPObject::setid when mental finishes it to steal ids of
2185 // this is needed to make sure the new item has curve (simply requestDisplayUpdate does not work)
2186 sp_document_ensure_up_to_date (document);
2188 NR::Matrix transform( i->transform * pat_transform );
2189 sp_item_write_transform(i, SP_OBJECT_REPR(i), transform);
2191 new_select = g_slist_prepend(new_select, i);
2192 }
2194 SPCSSAttr *css = sp_repr_css_attr_new ();
2195 sp_repr_css_set_property (css, "fill", "none");
2196 sp_repr_css_change (SP_OBJECT_REPR (item), css, "style");
2197 }
2199 if (!did) {
2200 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No pattern fills</b> in the selection."));
2201 } else {
2202 sp_document_done(sp_desktop_document(desktop));
2203 selection->setList(new_select);
2204 }
2205 }
2207 void
2208 sp_selection_create_bitmap_copy ()
2209 {
2210 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2211 if (desktop == NULL)
2212 return;
2214 SPDocument *document = sp_desktop_document(desktop);
2216 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2218 // check if something is selected
2219 if (selection->isEmpty()) {
2220 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to make a bitmap copy."));
2221 return;
2222 }
2224 // Get the bounding box of the selection
2225 NRRect bbox;
2226 sp_document_ensure_up_to_date (document);
2227 selection->bounds(&bbox);
2228 if (NR_RECT_DFLS_TEST_EMPTY(&bbox)) {
2229 return; // exceptional situation, so not bother with a translatable error message, just quit quietly
2230 }
2232 // List of the items to show; all others will be hidden
2233 GSList *items = g_slist_copy ((GSList *) selection->itemList());
2235 // Sort items so that the topmost comes last
2236 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
2238 // Generate a random value from the current time (you may create bitmap from the same object(s)
2239 // multiple times, and this is done so that they don't clash)
2240 GTimeVal cu;
2241 g_get_current_time (&cu);
2242 guint current = (int) (cu.tv_sec * 1000000 + cu.tv_usec) % 1024;
2244 // Create the filename
2245 gchar *filename = g_strdup_printf ("%s-%s-%u.png", document->name, SP_OBJECT_REPR(items->data)->attribute("id"), current);
2246 // Imagemagick is known not to handle spaces in filenames, so we replace anything but letters,
2247 // digits, and a few other chars, with "_"
2248 filename = g_strcanon (filename, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.=+~$#@^&!?", '_');
2249 // Build the complete path by adding document->base if set
2250 gchar *filepath = g_build_filename (document->base?document->base:"", filename, NULL);
2252 //g_print ("%s\n", filepath);
2254 // Remember parent and z-order of the topmost one
2255 gint pos = SP_OBJECT_REPR(g_slist_last(items)->data)->position();
2256 SPObject *parent_object = SP_OBJECT_PARENT(g_slist_last(items)->data);
2257 Inkscape::XML::Node *parent = SP_OBJECT_REPR(parent_object);
2259 // Calculate resolution
2260 double res;
2261 int const prefs_res = prefs_get_int_attribute ("options.createbitmap", "resolution", 0);
2262 if (0 < prefs_res) {
2263 // If it's given explicitly in prefs, take it
2264 res = prefs_res;
2265 } else {
2266 // Otherwise look up minimum bitmap size (default 250 pixels) and calculate resolution from it
2267 int const prefs_min = prefs_get_int_attribute ("options.createbitmap", "minsize", 250);
2268 res = prefs_min / MIN ((bbox.x1 - bbox.x0), (bbox.y1 - bbox.y0));
2269 }
2271 // The width and height of the bitmap in pixels
2272 unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * res);
2273 unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * res);
2275 // Find out if we have to run a filter
2276 const gchar *run = NULL;
2277 const gchar *filter = prefs_get_string_attribute ("options.createbitmap", "filter");
2278 if (filter) {
2279 // filter command is given;
2280 // see if we have a parameter to pass to it
2281 const gchar *param1 = prefs_get_string_attribute ("options.createbitmap", "filter_param1");
2282 if (param1) {
2283 if (param1[strlen(param1) - 1] == '%') {
2284 // if the param string ends with %, interpret it as a percentage of the image's max dimension
2285 gchar p1[256];
2286 g_ascii_dtostr (p1, 256, ceil (g_ascii_strtod (param1, NULL) * MAX(width, height) / 100));
2287 // the first param is always the image filename, the second is param1
2288 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, p1);
2289 } else {
2290 // otherwise pass the param1 unchanged
2291 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, param1);
2292 }
2293 } else {
2294 // run without extra parameter
2295 run = g_strdup_printf ("%s \"%s\"", filter, filepath);
2296 }
2297 }
2299 // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
2300 NR::Matrix eek = sp_item_i2d_affine (SP_ITEM(parent_object));
2301 NR::Matrix t = NR::scale (1/res, -1/res) * NR::translate (bbox.x0, bbox.y1) * eek.inverse();
2303 // Do the export
2304 sp_export_png_file(document, filepath,
2305 bbox.x0, bbox.y0, bbox.x1, bbox.y1,
2306 width, height,
2307 (guint32) 0xffffff00,
2308 NULL, NULL,
2309 true, /*bool force_overwrite,*/
2310 items);
2312 g_slist_free (items);
2314 // Run filter, if any
2315 if (run) {
2316 g_print ("Running external filter: %s\n", run);
2317 system (run);
2318 }
2320 // Import the image back
2321 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filepath, NULL);
2322 if (pb) {
2323 // Create the repr for the image
2324 Inkscape::XML::Node * repr = sp_repr_new ("svg:image");
2325 repr->setAttribute("xlink:href", filename);
2326 repr->setAttribute("sodipodi:absref", filepath);
2327 sp_repr_set_svg_double(repr, "width", gdk_pixbuf_get_width(pb));
2328 sp_repr_set_svg_double(repr, "height", gdk_pixbuf_get_height(pb));
2330 // Write transform
2331 gchar c[256];
2332 if (sp_svg_transform_write(c, 256, t)) {
2333 repr->setAttribute("transform", c);
2334 }
2336 // add the new repr to the parent
2337 parent->appendChild(repr);
2339 // move to the saved position
2340 repr->setPosition(pos > 0 ? pos + 1 : 1);
2342 // Set selection to the new image
2343 selection->clear();
2344 selection->add(repr);
2346 // Clean up
2347 Inkscape::GC::release(repr);
2348 gdk_pixbuf_unref (pb);
2350 // Complete undoable transaction
2351 sp_document_done (document);
2352 }
2354 g_free (filename);
2355 g_free (filepath);
2356 }
2358 /**
2359 * \brief sp_selection_set_mask
2360 *
2361 * This function creates a mask or clipPath from selection
2362 * Two different modes:
2363 * if applyToLayer, all selection is moved to DEFS as mask/clippath
2364 * and is applied to current layer
2365 * otherwise, topmost object is used as mask for other objects
2366 * If \a apply_clip_path parameter is true, clipPath is created, otherwise mask
2367 *
2368 */
2369 void
2370 sp_selection_set_mask(bool apply_clip_path, bool apply_to_layer)
2371 {
2372 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2373 if (desktop == NULL)
2374 return;
2376 SPDocument *document = sp_desktop_document(desktop);
2378 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2380 // check if something is selected
2381 bool is_empty = selection->isEmpty();
2382 if ( apply_to_layer && is_empty) {
2383 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to create clippath or mask from."));
2384 return;
2385 } else if (!apply_to_layer && ( is_empty || NULL == selection->itemList()->next )) {
2386 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select mask object and <b>object(s)</b> to apply clippath or mask to."));
2387 return;
2388 }
2390 sp_document_ensure_up_to_date(document);
2392 GSList *items = g_slist_copy((GSList *) selection->itemList());
2394 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2396 // create a list of duplicates
2397 GSList *mask_items = NULL;
2398 GSList *apply_to_items = NULL;
2399 bool topmost = prefs_get_int_attribute ("options.maskobject", "topmost", 1);
2400 bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
2402 if (apply_to_layer) {
2403 // all selected items are used for mask, which is applied to a layer
2404 apply_to_items = g_slist_prepend (apply_to_items, desktop->currentLayer());
2406 for (GSList *i = items; i != NULL; i = i->next) {
2407 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate();
2408 mask_items = g_slist_prepend (mask_items, dup);
2410 if (remove_original) {
2411 SPObject *item = SP_OBJECT (i->data);
2412 item->deleteObject (false);
2413 }
2414 }
2415 } else if (!topmost) {
2416 // topmost item is used as a mask, which is applied to other items in a selection
2417 GSList *i = items;
2418 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate();
2419 mask_items = g_slist_prepend (mask_items, dup);
2421 if (remove_original) {
2422 SPObject *item = SP_OBJECT (i->data);
2423 item->deleteObject (false);
2424 }
2426 for (i = i->next; i != NULL; i = i->next) {
2427 apply_to_items = g_slist_prepend (apply_to_items, i->data);
2428 }
2429 } else {
2430 GSList *i = NULL;
2431 for (i = items; NULL != i->next; i = i->next) {
2432 apply_to_items = g_slist_prepend (apply_to_items, i->data);
2433 }
2435 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate();
2436 mask_items = g_slist_prepend (mask_items, dup);
2438 if (remove_original) {
2439 SPObject *item = SP_OBJECT (i->data);
2440 item->deleteObject (false);
2441 }
2442 }
2444 g_slist_free (items);
2445 items = NULL;
2447 gchar const* attributeName = apply_clip_path ? "clip-path" : "mask";
2448 for (GSList *i = apply_to_items; NULL != i; i = i->next) {
2449 SPItem *item = reinterpret_cast<SPItem *>(i->data);
2450 // inverted object transform should be applied to a mask object,
2451 // as mask is calculated in user space (after applying transform)
2452 NR::Matrix maskTransform (item->transform.inverse());
2454 GSList *mask_items_dup = NULL;
2455 for (GSList *mask_item = mask_items; NULL != mask_item; mask_item = mask_item->next) {
2456 Inkscape::XML::Node *dup = reinterpret_cast<Inkscape::XML::Node *>(mask_item->data)->duplicate();
2457 mask_items_dup = g_slist_prepend (mask_items_dup, dup);
2458 }
2460 const gchar *mask_id = NULL;
2461 if (apply_clip_path) {
2462 mask_id = sp_clippath_create(mask_items_dup, document, &maskTransform);
2463 } else {
2464 mask_id = sp_mask_create(mask_items_dup, document, &maskTransform);
2465 }
2467 g_slist_free (mask_items_dup);
2468 mask_items_dup = NULL;
2470 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, g_strdup_printf("url(#%s)", mask_id));
2471 }
2473 g_slist_free (mask_items);
2474 g_slist_free (apply_to_items);
2476 sp_document_done (document);
2477 }
2479 void sp_selection_unset_mask(bool apply_clip_path) {
2480 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2481 if (desktop == NULL)
2482 return;
2484 SPDocument *document = sp_desktop_document(desktop);
2485 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2487 // check if something is selected
2488 if (selection->isEmpty()) {
2489 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to remove clippath or mask from."));
2490 return;
2491 }
2493 bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
2494 sp_document_ensure_up_to_date(document);
2496 gchar const* attributeName = apply_clip_path ? "clip-path" : "mask";
2497 std::map<SPObject*,SPItem*> referenced_objects;
2498 for (GSList const*i = selection->itemList(); NULL != i; i = i->next) {
2499 if (remove_original) {
2500 // remember referenced mask/clippath, so orphaned masks can be moved back to document
2501 SPItem *item = reinterpret_cast<SPItem *>(i->data);
2502 Inkscape::URIReference *uri_ref = NULL;
2504 if (apply_clip_path) {
2505 uri_ref = item->clip_ref;
2506 } else {
2507 uri_ref = item->mask_ref;
2508 }
2510 // collect distinct mask object (and associate with item to apply transform)
2511 if (NULL != uri_ref && NULL != uri_ref->getObject()) {
2512 referenced_objects[uri_ref->getObject()] = item;
2513 }
2514 }
2516 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, "none");
2517 }
2519 // restore mask objects into a document
2520 for ( std::map<SPObject*,SPItem*>::iterator it = referenced_objects.begin() ; it != referenced_objects.end() ; ++it) {
2521 SPObject *obj = (*it).first;
2522 GSList *items_to_move = NULL;
2523 for (SPObject *child = sp_object_first_child(obj) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2524 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate();
2525 items_to_move = g_slist_prepend (items_to_move, copy);
2526 }
2528 if (!obj->isReferenced()) {
2529 // delete from defs if no other object references this mask
2530 obj->deleteObject(false);
2531 }
2533 // remember parent and position of the item to which the clippath/mask was applied
2534 Inkscape::XML::Node *parent = SP_OBJECT_REPR((*it).second)->parent();
2535 gint pos = SP_OBJECT_REPR((*it).second)->position();
2537 for (GSList *i = items_to_move; NULL != i; i = i->next) {
2538 Inkscape::XML::Node *repr = (Inkscape::XML::Node *)i->data;
2540 // insert into parent, restore pos
2541 parent->appendChild(repr);
2542 repr->setPosition((pos + 1) > 0 ? (pos + 1) : 0);
2544 SPItem *mask_item = (SPItem *) sp_desktop_document (desktop)->getObjectByRepr(repr);
2545 selection->add(repr);
2547 // transform mask, so it is moved the same spot where mask was applied
2548 NR::Matrix transform (mask_item->transform);
2549 transform *= (*it).second->transform;
2550 sp_item_write_transform(mask_item, SP_OBJECT_REPR(mask_item), transform);
2551 }
2553 g_slist_free (items_to_move);
2554 }
2556 sp_document_done (document);
2557 }
2559 void fit_canvas_to_selection(SPDesktop *desktop) {
2560 g_return_if_fail(desktop != NULL);
2561 SPDocument *doc = sp_desktop_document(desktop);
2563 g_return_if_fail(doc != NULL);
2564 g_return_if_fail(desktop->selection != NULL);
2565 g_return_if_fail(!desktop->selection->isEmpty());
2566 NRRect bbox = {0,0,0,0};
2568 desktop->selection->bounds(&bbox);
2569 if (!empty(bbox)) {
2570 doc->fitToRect(bbox);
2571 }
2572 };
2574 void fit_canvas_to_drawing(SPDocument *doc) {
2575 g_return_if_fail(doc != NULL);
2576 NRRect bbox = {0,0,0,0};
2578 sp_document_ensure_up_to_date (doc);
2579 sp_item_invoke_bbox(SP_ITEM(doc->root), &bbox, sp_item_i2r_affine(SP_ITEM(doc->root)), TRUE);
2581 if (!empty(bbox)) {
2582 doc->fitToRect(bbox);
2583 }
2584 };
2586 void fit_canvas_to_selection_or_drawing(SPDesktop *desktop) {
2587 g_return_if_fail(desktop != NULL);
2588 SPDocument *doc = sp_desktop_document(desktop);
2590 g_return_if_fail(doc != NULL);
2591 g_return_if_fail(desktop->selection != NULL);
2593 if (desktop->selection->isEmpty()) {
2594 fit_canvas_to_drawing(doc);
2595 } else {
2596 fit_canvas_to_selection(desktop);
2597 }
2599 sp_document_done(doc);
2600 };
2602 /*
2603 Local Variables:
2604 mode:c++
2605 c-file-style:"stroustrup"
2606 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2607 indent-tabs-mode:nil
2608 fill-column:99
2609 End:
2610 */
2611 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :