e7e3ec4a35e25436ccbfc7001efc85d8d52909ec
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 using NR::X;
69 using NR::Y;
71 #include "selection-chemistry.h"
73 /* fixme: find a better place */
74 GSList *clipboard = NULL;
75 GSList *defs_clipboard = NULL;
76 SPCSSAttr *style_clipboard = NULL;
78 static void sp_copy_stuff_used_by_item(GSList **defs_clip, SPItem *item, const GSList *items);
80 void sp_selection_copy_one (Inkscape::XML::Node *repr, NR::Matrix full_t, GSList **clip)
81 {
82 Inkscape::XML::Node *copy = repr->duplicate();
84 // copy complete inherited style
85 SPCSSAttr *css = sp_repr_css_attr_inherited(repr, "style");
86 sp_repr_css_set(copy, css, "style");
87 sp_repr_css_attr_unref(css);
89 // write the complete accummulated transform passed to us
90 // (we're dealing with unattached repr, so we write to its attr instead of using sp_item_set_transform)
91 gchar affinestr[80];
92 if (sp_svg_transform_write(affinestr, 79, full_t)) {
93 copy->setAttribute("transform", affinestr);
94 } else {
95 copy->setAttribute("transform", NULL);
96 }
98 *clip = g_slist_prepend(*clip, copy);
99 }
101 void sp_selection_copy_impl (const GSList *items, GSList **clip, GSList **defs_clip, SPCSSAttr **style_clip)
102 {
104 // Copy stuff referenced by all items to defs_clip:
105 if (defs_clip) {
106 for (GSList *i = (GSList *) items; i != NULL; i = i->next) {
107 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (i->data), items);
108 }
109 *defs_clip = g_slist_reverse(*defs_clip);
110 }
112 // Store style:
113 if (style_clip) {
114 SPItem *item = SP_ITEM (items->data); // take from the first selected item
115 *style_clip = take_style_from_item (item);
116 }
118 if (clip) {
119 // Sort items:
120 GSList *sorted_items = g_slist_copy ((GSList *) items);
121 sorted_items = g_slist_sort((GSList *) sorted_items, (GCompareFunc) sp_object_compare_position);
123 // Copy item reprs:
124 for (GSList *i = (GSList *) sorted_items; i != NULL; i = i->next) {
125 sp_selection_copy_one (SP_OBJECT_REPR (i->data), sp_item_i2doc_affine(SP_ITEM (i->data)), clip);
126 }
128 *clip = g_slist_reverse(*clip);
129 g_slist_free ((GSList *) sorted_items);
130 }
131 }
133 /**
134 Add gradients/patterns/markers referenced by copied objects to defs
135 */
136 void
137 paste_defs (GSList **defs_clip, SPDocument *doc)
138 {
139 if (!defs_clip)
140 return;
142 for (GSList *gl = *defs_clip; gl != NULL; gl = gl->next) {
143 SPDefs *defs= (SPDefs *) SP_DOCUMENT_DEFS(doc);
144 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) gl->data;
145 gchar const *id = repr->attribute("id");
146 if (!id || !doc->getObjectById(id)) {
147 Inkscape::XML::Node *copy = repr->duplicate();
148 SP_OBJECT_REPR(defs)->addChild(copy, NULL);
149 Inkscape::GC::release(copy);
150 }
151 }
152 }
154 GSList *sp_selection_paste_impl (SPDocument *document, SPObject *parent, GSList **clip, GSList **defs_clip)
155 {
156 paste_defs (defs_clip, document);
158 GSList *copied = NULL;
159 // add objects to document
160 for (GSList *l = *clip; l != NULL; l = l->next) {
161 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
162 Inkscape::XML::Node *copy = repr->duplicate();
164 // premultiply the item transform by the accumulated parent transform in the paste layer
165 NR::Matrix local = sp_item_i2doc_affine(SP_ITEM(parent));
166 if (!local.test_identity()) {
167 gchar const *t_str = copy->attribute("transform");
168 NR::Matrix item_t (NR::identity());
169 if (t_str)
170 sp_svg_transform_read(t_str, &item_t);
171 item_t *= local.inverse();
172 // (we're dealing with unattached repr, so we write to its attr instead of using sp_item_set_transform)
173 gchar affinestr[80];
174 if (sp_svg_transform_write(affinestr, 79, item_t)) {
175 copy->setAttribute("transform", affinestr);
176 } else {
177 copy->setAttribute("transform", NULL);
178 }
179 }
181 parent->appendChildRepr(copy);
182 copied = g_slist_prepend(copied, copy);
183 Inkscape::GC::release(copy);
184 }
185 return copied;
186 }
188 void sp_selection_delete_impl(const GSList *items)
189 {
190 for (const GSList *i = items ; i ; i = i->next ) {
191 sp_object_ref((SPObject *)i->data, NULL);
192 }
193 for (const GSList *i = items; i != NULL; i = i->next) {
194 SPItem *item = (SPItem *) i->data;
195 SP_OBJECT(item)->deleteObject();
196 sp_object_unref((SPObject *)item, NULL);
197 }
198 }
201 void sp_selection_delete()
202 {
203 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
204 if (desktop == NULL) {
205 return;
206 }
208 if (tools_isactive (desktop, TOOLS_TEXT))
209 if (sp_text_delete_selection(desktop->event_context)) {
210 sp_document_done(SP_DT_DOCUMENT(desktop));
211 return;
212 }
214 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
216 // check if something is selected
217 if (selection->isEmpty()) {
218 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Nothing</b> was deleted."));
219 return;
220 }
222 const GSList *selected = g_slist_copy(const_cast<GSList *>(selection->itemList()));
223 selection->clear();
224 sp_selection_delete_impl (selected);
225 g_slist_free ((GSList *) selected);
227 /* a tool may have set up private information in it's selection context
228 * that depends on desktop items. I think the only sane way to deal with
229 * this currently is to reset the current tool, which will reset it's
230 * associated selection context. For example: deleting an object
231 * while moving it around the canvas.
232 */
233 tools_switch ( desktop, tools_active ( desktop ) );
235 sp_document_done(SP_DT_DOCUMENT(desktop));
236 }
238 /* fixme: sequencing */
239 void sp_selection_duplicate()
240 {
241 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
242 if (desktop == NULL)
243 return;
245 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
247 // check if something is selected
248 if (selection->isEmpty()) {
249 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to duplicate."));
250 return;
251 }
253 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
255 selection->clear();
257 // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
258 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
260 GSList *newsel = NULL;
262 while (reprs) {
263 Inkscape::XML::Node *parent = ((Inkscape::XML::Node *) reprs->data)->parent();
264 Inkscape::XML::Node *copy = ((Inkscape::XML::Node *) reprs->data)->duplicate();
266 parent->appendChild(copy);
268 newsel = g_slist_prepend(newsel, copy);
269 reprs = g_slist_remove(reprs, reprs->data);
270 Inkscape::GC::release(copy);
271 }
273 sp_document_done(SP_DT_DOCUMENT(desktop));
275 selection->setReprList(newsel);
277 g_slist_free(newsel);
278 }
280 void sp_edit_clear_all()
281 {
282 SPDesktop *dt = SP_ACTIVE_DESKTOP;
283 if (!dt)
284 return;
286 SPDocument *doc = SP_DT_DOCUMENT(dt);
287 SP_DT_SELECTION(dt)->clear();
289 g_return_if_fail(SP_IS_GROUP(dt->currentLayer()));
290 GSList *items = sp_item_group_item_list(SP_GROUP(dt->currentLayer()));
292 while (items) {
293 SP_OBJECT (items->data)->deleteObject();
294 items = g_slist_remove(items, items->data);
295 }
297 sp_document_done(doc);
298 }
300 GSList *
301 get_all_items (GSList *list, SPObject *from, SPDesktop *desktop, bool onlyvisible, bool onlysensitive, const GSList *exclude)
302 {
303 for (SPObject *child = sp_object_first_child(SP_OBJECT(from)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
304 if (SP_IS_ITEM(child) &&
305 !desktop->isLayer(SP_ITEM(child)) &&
306 (!onlysensitive || !SP_ITEM(child)->isLocked()) &&
307 (!onlyvisible || !desktop->itemIsHidden(SP_ITEM(child))) &&
308 (!exclude || !g_slist_find ((GSList *) exclude, child))
309 )
310 {
311 list = g_slist_prepend (list, SP_ITEM(child));
312 }
314 if (SP_IS_ITEM(child) && desktop->isLayer(SP_ITEM(child))) {
315 list = get_all_items (list, child, desktop, onlyvisible, onlysensitive, exclude);
316 }
317 }
319 return list;
320 }
322 void sp_edit_select_all_full (bool force_all_layers, bool invert)
323 {
324 SPDesktop *dt = SP_ACTIVE_DESKTOP;
325 if (!dt)
326 return;
328 Inkscape::Selection *selection = SP_DT_SELECTION(dt);
330 g_return_if_fail(SP_IS_GROUP(dt->currentLayer()));
332 bool inlayer = prefs_get_int_attribute ("options.kbselection", "inlayer", 1);
333 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
334 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
336 GSList *items = NULL;
338 const GSList *exclude = NULL;
339 if (invert) {
340 exclude = selection->itemList();
341 }
343 if (inlayer && !force_all_layers) {
345 if ( (onlysensitive && SP_ITEM(dt->currentLayer())->isLocked()) ||
346 (onlyvisible && dt->itemIsHidden(SP_ITEM(dt->currentLayer()))) )
347 return;
349 GSList *all_items = sp_item_group_item_list(SP_GROUP(dt->currentLayer()));
351 for (GSList *i = all_items; i; i = i->next) {
352 SPItem *item = SP_ITEM (i->data);
354 if (item && (!onlysensitive || !item->isLocked())) {
355 if (!onlyvisible || !dt->itemIsHidden(item)) {
356 if (!dt->isLayer(item)) {
357 if (!invert || !g_slist_find ((GSList *) exclude, item)) {
358 items = g_slist_prepend (items, item); // leave it in the list
359 }
360 }
361 }
362 }
363 }
365 g_slist_free (all_items);
367 } else {
368 items = get_all_items (NULL, dt->currentRoot(), dt, onlyvisible, onlysensitive, exclude);
369 }
371 selection->setList (items);
373 if (items) {
374 g_slist_free (items);
375 }
376 }
378 void sp_edit_select_all ()
379 {
380 sp_edit_select_all_full (false, false);
381 }
383 void sp_edit_select_all_in_all_layers ()
384 {
385 sp_edit_select_all_full (true, false);
386 }
388 void sp_edit_invert ()
389 {
390 sp_edit_select_all_full (false, true);
391 }
393 void sp_edit_invert_in_all_layers ()
394 {
395 sp_edit_select_all_full (true, true);
396 }
398 void sp_selection_group()
399 {
400 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
401 if (desktop == NULL)
402 return;
404 SPDocument *document = SP_DT_DOCUMENT (desktop);
406 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
408 // Check if something is selected.
409 if (selection->isEmpty()) {
410 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>two or more objects</b> to group."));
411 return;
412 }
414 GSList const *l = (GSList *) selection->reprList();
416 // Check if at least two objects are selected.
417 if (l->next == NULL) {
418 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>at least two objects</b> to group."));
419 return;
420 }
422 GSList *p = g_slist_copy((GSList *) l);
424 selection->clear();
426 p = g_slist_sort(p, (GCompareFunc) sp_repr_compare_position);
428 // Remember the position and parent of the topmost object.
429 gint topmost = ((Inkscape::XML::Node *) g_slist_last(p)->data)->position();
430 Inkscape::XML::Node *topmost_parent = ((Inkscape::XML::Node *) g_slist_last(p)->data)->parent();
432 Inkscape::XML::Node *group = sp_repr_new("svg:g");
434 while (p) {
435 Inkscape::XML::Node *current = (Inkscape::XML::Node *) p->data;
437 if (current->parent() == topmost_parent) {
438 Inkscape::XML::Node *spnew = current->duplicate();
439 sp_repr_unparent(current);
440 group->appendChild(spnew);
441 Inkscape::GC::release(spnew);
442 topmost --; // only reduce count for those items deleted from topmost_parent
443 } else { // move it to topmost_parent first
444 GSList *temp_clip = NULL;
446 // At this point, current may already have no item, due to its being a clone whose original is already moved away
447 // So we copy it artificially calculating the transform from its repr->attr("transform") and the parent transform
448 gchar const *t_str = current->attribute("transform");
449 NR::Matrix item_t (NR::identity());
450 if (t_str)
451 sp_svg_transform_read(t_str, &item_t);
452 item_t *= sp_item_i2doc_affine(SP_ITEM(document->getObjectByRepr(current->parent())));
453 //FIXME: when moving both clone and original from a transformed group (either by
454 //grouping into another parent, or by cut/paste) the transform from the original's
455 //parent becomes embedded into original itself, and this affects its clones. Fix
456 //this by remembering the transform diffs we write to each item into an array and
457 //then, if this is clone, looking up its original in that array and pre-multiplying
458 //it by the inverse of that original's transform diff.
460 sp_selection_copy_one (current, item_t, &temp_clip);
461 sp_repr_unparent(current);
463 // paste into topmost_parent (temporarily)
464 GSList *copied = sp_selection_paste_impl (document, document->getObjectByRepr(topmost_parent), &temp_clip, NULL);
465 if (temp_clip) g_slist_free (temp_clip);
466 if (copied) { // if success,
467 // take pasted object (now in topmost_parent)
468 Inkscape::XML::Node *in_topmost = (Inkscape::XML::Node *) copied->data;
469 // make a copy
470 Inkscape::XML::Node *spnew = in_topmost->duplicate();
471 // remove pasted
472 sp_repr_unparent(in_topmost);
473 // put its copy into group
474 group->appendChild(spnew);
475 Inkscape::GC::release(spnew);
476 g_slist_free (copied);
477 }
478 }
479 p = g_slist_remove(p, current);
480 }
482 // Add the new group to the topmost members' parent
483 topmost_parent->appendChild(group);
485 // Move to the position of the topmost, reduced by the number of items deleted from topmost_parent
486 group->setPosition(topmost + 1);
488 sp_document_done(SP_DT_DOCUMENT(desktop));
490 selection->set(group);
491 Inkscape::GC::release(group);
492 }
494 void sp_selection_ungroup()
495 {
496 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
497 if (desktop == NULL)
498 return;
500 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
502 if (selection->isEmpty()) {
503 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>group</b> to ungroup."));
504 return;
505 }
507 GSList *items = g_slist_copy((GSList *) selection->itemList());
508 selection->clear();
510 // Get a copy of current selection.
511 GSList *new_select = NULL;
512 bool ungrouped = false;
513 for (GSList *i = items;
514 i != NULL;
515 i = i->next)
516 {
517 SPItem *group = (SPItem *) i->data;
519 // when ungrouping cloned groups with their originals, some objects that were selected may no more exist due to unlinking
520 if (!SP_IS_OBJECT(group)) {
521 continue;
522 }
524 /* We do not allow ungrouping <svg> etc. (lauris) */
525 if (strcmp(SP_OBJECT_REPR(group)->name(), "svg:g") && strcmp(SP_OBJECT_REPR(group)->name(), "svg:switch")) {
526 // keep the non-group item in the new selection
527 selection->add(group);
528 continue;
529 }
531 GSList *children = NULL;
532 /* This is not strictly required, but is nicer to rely on group ::destroy (lauris) */
533 sp_item_group_ungroup(SP_GROUP(group), &children, false);
534 ungrouped = true;
535 // Add ungrouped items to the new selection.
536 new_select = g_slist_concat(new_select, children);
537 }
539 if (new_select) { // Set new selection.
540 selection->addList(new_select);
541 g_slist_free(new_select);
542 }
543 if (!ungrouped) {
544 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No groups</b> to ungroup in the selection."));
545 }
547 g_slist_free(items);
549 sp_document_done(SP_DT_DOCUMENT(desktop));
550 }
552 static SPGroup *
553 sp_item_list_common_parent_group(const GSList *items)
554 {
555 if (!items) {
556 return NULL;
557 }
558 SPObject *parent = SP_OBJECT_PARENT(items->data);
559 /* Strictly speaking this CAN happen, if user selects <svg> from XML editor */
560 if (!SP_IS_GROUP(parent)) {
561 return NULL;
562 }
563 for (items = items->next; items; items = items->next) {
564 if (SP_OBJECT_PARENT(items->data) != parent) {
565 return NULL;
566 }
567 }
569 return SP_GROUP(parent);
570 }
572 /** Finds out the minimum common bbox of the selected items
573 */
574 NR::Rect
575 enclose_items(const GSList *items)
576 {
577 g_assert(items != NULL);
579 NR::Rect r = sp_item_bbox_desktop((SPItem *) items->data);
581 for (GSList *i = items->next; i; i = i->next) {
582 r = NR::Rect::union_bounds(r, sp_item_bbox_desktop((SPItem *) i->data));
583 }
585 return r;
586 }
588 SPObject *
589 prev_sibling(SPObject *child)
590 {
591 SPObject *parent = SP_OBJECT_PARENT(child);
592 if (!SP_IS_GROUP(parent)) {
593 return NULL;
594 }
595 for ( SPObject *i = sp_object_first_child(parent) ; i; i = SP_OBJECT_NEXT(i) ) {
596 if (i->next == child)
597 return i;
598 }
599 return NULL;
600 }
602 void
603 sp_selection_raise()
604 {
605 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
606 if (!desktop)
607 return;
609 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
611 GSList const *items = (GSList *) selection->itemList();
612 if (!items) {
613 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise."));
614 return;
615 }
617 SPGroup const *group = sp_item_list_common_parent_group(items);
618 if (!group) {
619 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
620 return;
621 }
623 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
625 /* construct reverse-ordered list of selected children */
626 GSList *rev = g_slist_copy((GSList *) items);
627 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
629 // find out the common bbox of the selected items
630 NR::Rect selected = enclose_items(items);
632 // for all objects in the selection (starting from top)
633 while (rev) {
634 SPObject *child = SP_OBJECT(rev->data);
635 // for each selected object, find the next sibling
636 for (SPObject *newref = child->next; newref; newref = newref->next) {
637 // if the sibling is an item AND overlaps our selection,
638 if (SP_IS_ITEM(newref) && selected.intersects(sp_item_bbox_desktop(SP_ITEM(newref)))) {
639 // AND if it's not one of our selected objects,
640 if (!g_slist_find((GSList *) items, newref)) {
641 // move the selected object after that sibling
642 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(newref));
643 }
644 break;
645 }
646 }
647 rev = g_slist_remove(rev, child);
648 }
650 sp_document_done(SP_DT_DOCUMENT(desktop));
651 }
653 void sp_selection_raise_to_top()
654 {
655 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
656 if (desktop == NULL)
657 return;
659 SPDocument *document = SP_DT_DOCUMENT(desktop);
660 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
662 if (selection->isEmpty()) {
663 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise to top."));
664 return;
665 }
667 GSList const *items = (GSList *) selection->itemList();
669 SPGroup const *group = sp_item_list_common_parent_group(items);
670 if (!group) {
671 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
672 return;
673 }
675 GSList *rl = g_slist_copy((GSList *) selection->reprList());
676 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
678 for (GSList *l = rl; l != NULL; l = l->next) {
679 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
680 repr->setPosition(-1);
681 }
683 g_slist_free(rl);
685 sp_document_done(document);
686 }
688 void
689 sp_selection_lower()
690 {
691 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
692 if (desktop == NULL)
693 return;
695 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
697 GSList const *items = (GSList *) selection->itemList();
698 if (!items) {
699 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower."));
700 return;
701 }
703 SPGroup const *group = sp_item_list_common_parent_group(items);
704 if (!group) {
705 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
706 return;
707 }
709 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
711 // find out the common bbox of the selected items
712 NR::Rect selected = enclose_items(items);
714 /* construct direct-ordered list of selected children */
715 GSList *rev = g_slist_copy((GSList *) items);
716 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
717 rev = g_slist_reverse(rev);
719 // for all objects in the selection (starting from top)
720 while (rev) {
721 SPObject *child = SP_OBJECT(rev->data);
722 // for each selected object, find the prev sibling
723 for (SPObject *newref = prev_sibling(child); newref; newref = prev_sibling(newref)) {
724 // if the sibling is an item AND overlaps our selection,
725 if (SP_IS_ITEM(newref) && selected.intersects(sp_item_bbox_desktop(SP_ITEM(newref)))) {
726 // AND if it's not one of our selected objects,
727 if (!g_slist_find((GSList *) items, newref)) {
728 // move the selected object before that sibling
729 SPObject *put_after = prev_sibling(newref);
730 if (put_after)
731 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(put_after));
732 else
733 SP_OBJECT_REPR(child)->setPosition(0);
734 }
735 break;
736 }
737 }
738 rev = g_slist_remove(rev, child);
739 }
741 sp_document_done(SP_DT_DOCUMENT(desktop));
743 }
745 void sp_selection_lower_to_bottom()
746 {
747 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
748 if (desktop == NULL)
749 return;
751 SPDocument *document = SP_DT_DOCUMENT(desktop);
752 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
754 if (selection->isEmpty()) {
755 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower to bottom."));
756 return;
757 }
759 GSList const *items = (GSList *) selection->itemList();
761 SPGroup const *group = sp_item_list_common_parent_group(items);
762 if (!group) {
763 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
764 return;
765 }
767 GSList *rl;
768 rl = g_slist_copy((GSList *) selection->reprList());
769 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
770 rl = g_slist_reverse(rl);
772 for (GSList *l = rl; l != NULL; l = l->next) {
773 gint minpos;
774 SPObject *pp, *pc;
775 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
776 pp = document->getObjectByRepr(sp_repr_parent(repr));
777 minpos = 0;
778 g_assert(SP_IS_GROUP(pp));
779 pc = sp_object_first_child(pp);
780 while (!SP_IS_ITEM(pc)) {
781 minpos += 1;
782 pc = pc->next;
783 }
784 repr->setPosition(minpos);
785 }
787 g_slist_free(rl);
789 sp_document_done(document);
790 }
792 void
793 sp_undo(SPDesktop *desktop, SPDocument *)
794 {
795 if (!sp_document_undo(SP_DT_DOCUMENT(desktop)))
796 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to undo."));
797 }
799 void
800 sp_redo(SPDesktop *desktop, SPDocument *)
801 {
802 if (!sp_document_redo(SP_DT_DOCUMENT(desktop)))
803 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to redo."));
804 }
806 void sp_selection_cut()
807 {
808 sp_selection_copy();
809 sp_selection_delete();
810 }
812 void sp_copy_gradient (GSList **defs_clip, SPGradient *gradient)
813 {
814 SPGradient *ref = gradient;
816 while (ref) {
817 // climb up the refs, copying each one in the chain
818 Inkscape::XML::Node *grad_repr = SP_OBJECT_REPR(ref)->duplicate();
819 *defs_clip = g_slist_prepend (*defs_clip, grad_repr);
821 ref = ref->ref->getObject();
822 }
823 }
825 void sp_copy_pattern (GSList **defs_clip, SPPattern *pattern)
826 {
827 SPPattern *ref = pattern;
829 while (ref) {
830 // climb up the refs, copying each one in the chain
831 Inkscape::XML::Node *pattern_repr = SP_OBJECT_REPR(ref)->duplicate();
832 *defs_clip = g_slist_prepend (*defs_clip, pattern_repr);
834 // items in the pattern may also use gradients and other patterns, so we need to recurse here as well
835 for (SPObject *child = sp_object_first_child(SP_OBJECT(ref)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
836 if (!SP_IS_ITEM (child))
837 continue;
838 sp_copy_stuff_used_by_item (defs_clip, (SPItem *) child, NULL);
839 }
841 ref = ref->ref->getObject();
842 }
843 }
845 void sp_copy_single (GSList **defs_clip, SPObject *thing)
846 {
847 Inkscape::XML::Node *duplicate_repr = SP_OBJECT_REPR(thing)->duplicate();
848 *defs_clip = g_slist_prepend (*defs_clip, duplicate_repr);
849 }
852 void sp_copy_textpath_path (GSList **defs_clip, SPTextPath *tp, const GSList *items)
853 {
854 SPItem *path = sp_textpath_get_path_item (tp);
855 if (!path)
856 return;
857 if (items && g_slist_find ((GSList *) items, path)) // do not copy it to defs if it is already in the list of items copied
858 return;
859 Inkscape::XML::Node *repr = SP_OBJECT_REPR(path)->duplicate();
860 *defs_clip = g_slist_prepend (*defs_clip, repr);
861 }
863 void sp_copy_stuff_used_by_item (GSList **defs_clip, SPItem *item, const GSList *items)
864 {
865 SPStyle *style = SP_OBJECT_STYLE (item);
867 if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
868 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
869 if (SP_IS_LINEARGRADIENT (server) || SP_IS_RADIALGRADIENT (server))
870 sp_copy_gradient (defs_clip, SP_GRADIENT(server));
871 if (SP_IS_PATTERN (server))
872 sp_copy_pattern (defs_clip, SP_PATTERN(server));
873 }
875 if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
876 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
877 if (SP_IS_LINEARGRADIENT (server) || SP_IS_RADIALGRADIENT (server))
878 sp_copy_gradient (defs_clip, SP_GRADIENT(server));
879 if (SP_IS_PATTERN (server))
880 sp_copy_pattern (defs_clip, SP_PATTERN(server));
881 }
883 if (SP_IS_SHAPE (item)) {
884 SPShape *shape = SP_SHAPE (item);
885 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
886 if (shape->marker[i]) {
887 sp_copy_single (defs_clip, SP_OBJECT (shape->marker[i]));
888 }
889 }
890 }
892 if (SP_IS_TEXT_TEXTPATH (item)) {
893 sp_copy_textpath_path (defs_clip, SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))), items);
894 }
896 if (item->clip_ref->getObject()) {
897 sp_copy_single (defs_clip, item->clip_ref->getObject());
898 }
900 if (item->mask_ref->getObject()) {
901 SPObject *mask = item->mask_ref->getObject();
902 sp_copy_single (defs_clip, mask);
903 // recurse into the mask for its gradients etc.
904 for (SPObject *o = SP_OBJECT(mask)->children; o != NULL; o = o->next) {
905 if (SP_IS_ITEM(o))
906 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (o), items);
907 }
908 }
910 // recurse
911 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
912 if (SP_IS_ITEM(o))
913 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (o), items);
914 }
915 }
917 /**
918 * \pre item != NULL
919 */
920 SPCSSAttr *
921 take_style_from_item (SPItem *item)
922 {
923 // write the complete cascaded style, context-free
924 SPCSSAttr *css = sp_css_attr_from_object (SP_OBJECT(item), SP_STYLE_FLAG_ALWAYS);
925 if (css == NULL)
926 return NULL;
928 if ((SP_IS_GROUP(item) && SP_OBJECT(item)->children) ||
929 (SP_IS_TEXT (item) && SP_OBJECT(item)->children && SP_OBJECT(item)->children->next == NULL)) {
930 // if this is a text with exactly one tspan child, merge the style of that tspan as well
931 // If this is a group, merge the style of its topmost (last) child with style
932 for (SPObject *last_element = item->lastChild(); last_element != NULL; last_element = SP_OBJECT_PREV (last_element)) {
933 if (SP_OBJECT_STYLE (last_element) != NULL) {
934 SPCSSAttr *temp = sp_css_attr_from_object (last_element, SP_STYLE_FLAG_IFSET);
935 if (temp) {
936 sp_repr_css_merge (css, temp);
937 sp_repr_css_attr_unref (temp);
938 }
939 break;
940 }
941 }
942 }
943 if (!(SP_IS_TEXT (item) || SP_IS_TSPAN (item) || SP_IS_STRING (item))) {
944 // do not copy text properties from non-text objects, it's confusing
945 css = sp_css_attr_unset_text (css);
946 }
948 // FIXME: also transform gradient/pattern fills, by forking? NO, this must be nondestructive
949 double ex = NR::expansion(sp_item_i2doc_affine(item));
950 if (ex != 1.0) {
951 css = sp_css_attr_scale (css, ex);
952 }
954 return css;
955 }
958 void sp_selection_copy()
959 {
960 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
961 if (desktop == NULL)
962 return;
964 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
966 if (tools_isactive (desktop, TOOLS_DROPPER)) {
967 sp_dropper_context_copy(desktop->event_context);
968 return; // copied color under cursor, nothing else to do
969 }
971 // check if something is selected
972 if (selection->isEmpty()) {
973 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing was copied."));
974 return;
975 }
977 const GSList *items = g_slist_copy ((GSList *) selection->itemList());
979 // 0. Copy text to system clipboard
980 // FIXME: for non-texts, put serialized XML as text to the clipboard;
981 //for this sp_repr_write_stream needs to be rewritten with iostream instead of FILE
982 Glib::ustring text;
983 if (tools_isactive (desktop, TOOLS_TEXT)) {
984 text = sp_text_get_selected_text(desktop->event_context);
985 }
987 if (text.empty()) {
988 guint texts = 0;
989 for (GSList *i = (GSList *) items; i; i = i->next) {
990 SPItem *item = SP_ITEM (i->data);
991 if (SP_IS_TEXT (item) || SP_IS_FLOWTEXT(item)) {
992 if (texts > 0) // if more than one text object is copied, separate them by spaces
993 text += " ";
994 gchar *this_text = sp_te_get_string_multiline (item);
995 if (this_text) {
996 text += this_text;
997 g_free(this_text);
998 }
999 texts++;
1000 }
1001 }
1002 }
1003 if (!text.empty()) {
1004 Glib::RefPtr<Gtk::Clipboard> refClipboard = Gtk::Clipboard::get();
1005 refClipboard->set_text(text);
1006 }
1008 // clear old defs clipboard
1009 while (defs_clipboard) {
1010 Inkscape::GC::release((Inkscape::XML::Node *) defs_clipboard->data);
1011 defs_clipboard = g_slist_remove (defs_clipboard, defs_clipboard->data);
1012 }
1014 // clear style clipboard
1015 if (style_clipboard) {
1016 sp_repr_css_attr_unref (style_clipboard);
1017 style_clipboard = NULL;
1018 }
1020 //clear main clipboard
1021 while (clipboard) {
1022 Inkscape::GC::release((Inkscape::XML::Node *) clipboard->data);
1023 clipboard = g_slist_remove(clipboard, clipboard->data);
1024 }
1026 sp_selection_copy_impl (items, &clipboard, &defs_clipboard, &style_clipboard);
1028 if (tools_isactive (desktop, TOOLS_TEXT)) { // take style from cursor/text selection, overwriting the style just set by copy_impl
1029 SPStyle *const query = sp_style_new();
1030 if (sp_desktop_query_style_all (desktop, query)) {
1031 SPCSSAttr *css = sp_css_attr_from_style (query, SP_STYLE_FLAG_ALWAYS);
1032 if (css != NULL) {
1033 // clear style clipboard
1034 if (style_clipboard) {
1035 sp_repr_css_attr_unref (style_clipboard);
1036 style_clipboard = NULL;
1037 }
1038 //sp_repr_css_print (css);
1039 style_clipboard = css;
1040 }
1041 }
1042 g_free (query);
1043 }
1045 g_slist_free ((GSList *) items);
1046 }
1048 void sp_selection_paste(bool in_place)
1049 {
1050 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1052 if (desktop == NULL) {
1053 return;
1054 }
1056 SPDocument *document = SP_DT_DOCUMENT(desktop);
1058 if (Inkscape::have_viable_layer(desktop, desktop->messageStack()) == false) {
1059 return;
1060 }
1062 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1064 if (tools_isactive (desktop, TOOLS_TEXT)) {
1065 if (sp_text_paste_inline(desktop->event_context))
1066 return; // pasted from system clipboard into text, nothing else to do
1067 }
1069 // check if something is in the clipboard
1070 if (clipboard == NULL) {
1071 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1072 return;
1073 }
1075 GSList *copied = sp_selection_paste_impl(document, desktop->currentLayer(), &clipboard, &defs_clipboard);
1076 // add pasted objects to selection
1077 selection->setReprList((GSList const *) copied);
1078 g_slist_free (copied);
1080 if (!in_place) {
1081 sp_document_ensure_up_to_date(document);
1083 NR::Point m( desktop->point() - selection->bounds().midpoint() );
1085 /* Snap the offset of the new item(s) to the grid */
1086 /* FIXME: this gridsnap fiddling is a hack. */
1087 Inkscape::GridSnapper &s = desktop->namedview->grid_snapper;
1088 gdouble const curr_gridsnap = s.getDistance();
1089 s.setDistance(NR_HUGE);
1090 m = s.freeSnap(Inkscape::Snapper::SNAP_POINT, m, NULL).getPoint();
1091 s.setDistance(curr_gridsnap);
1092 sp_selection_move_relative(selection, m);
1093 }
1095 sp_document_done(document);
1096 }
1098 void sp_selection_paste_style()
1099 {
1100 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1101 if (desktop == NULL) return;
1103 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1105 // check if something is in the clipboard
1106 if (clipboard == NULL) {
1107 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1108 return;
1109 }
1111 // check if something is selected
1112 if (selection->isEmpty()) {
1113 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste style to."));
1114 return;
1115 }
1117 paste_defs (&defs_clipboard, SP_DT_DOCUMENT(desktop));
1119 sp_desktop_set_style (desktop, style_clipboard);
1121 sp_document_done(SP_DT_DOCUMENT (desktop));
1122 }
1124 void sp_selection_to_next_layer ()
1125 {
1126 SPDesktop *dt = SP_ACTIVE_DESKTOP;
1128 Inkscape::Selection *selection = SP_DT_SELECTION(dt);
1130 // check if something is selected
1131 if (selection->isEmpty()) {
1132 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer above."));
1133 return;
1134 }
1136 const GSList *items = g_slist_copy ((GSList *) selection->itemList());
1138 SPObject *next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer());
1139 if (next) {
1140 GSList *temp_clip = NULL;
1141 sp_selection_copy_impl (items, &temp_clip, NULL, NULL); // we're in the same doc, so no need to copy defs
1142 sp_selection_delete_impl (items);
1143 GSList *copied = sp_selection_paste_impl (SP_DT_DOCUMENT (dt), next, &temp_clip, NULL);
1144 selection->setReprList((GSList const *) copied);
1145 g_slist_free (copied);
1146 if (temp_clip) g_slist_free (temp_clip);
1147 dt->setCurrentLayer(next);
1148 sp_document_done(SP_DT_DOCUMENT (dt));
1149 } else {
1150 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers above."));
1151 }
1153 g_slist_free ((GSList *) items);
1154 }
1156 void sp_selection_to_prev_layer ()
1157 {
1158 SPDesktop *dt = SP_ACTIVE_DESKTOP;
1160 Inkscape::Selection *selection = SP_DT_SELECTION(dt);
1162 // check if something is selected
1163 if (selection->isEmpty()) {
1164 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer below."));
1165 return;
1166 }
1168 const GSList *items = g_slist_copy ((GSList *) selection->itemList());
1170 SPObject *next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer());
1171 if (next) {
1172 GSList *temp_clip = NULL;
1173 sp_selection_copy_impl (items, &temp_clip, NULL, NULL); // we're in the same doc, so no need to copy defs
1174 sp_selection_delete_impl (items);
1175 GSList *copied = sp_selection_paste_impl (SP_DT_DOCUMENT (dt), next, &temp_clip, NULL);
1176 selection->setReprList((GSList const *) copied);
1177 g_slist_free (copied);
1178 if (temp_clip) g_slist_free (temp_clip);
1179 dt->setCurrentLayer(next);
1180 sp_document_done(SP_DT_DOCUMENT (dt));
1181 } else {
1182 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers below."));
1183 }
1185 g_slist_free ((GSList *) items);
1186 }
1188 /** Apply matrix to the selection. \a set_i2d is normally true, which means objects are in the
1189 original transform, synced with their reprs, and need to jump to the new transform in one go. A
1190 value of set_i2d==false is only used by seltrans when it's dragging objects live (not outlines); in
1191 that case, items are already in the new position, but the repr is in the old, and this function
1192 then simply updates the repr from item->transform.
1193 */
1194 void sp_selection_apply_affine(Inkscape::Selection *selection, NR::Matrix const &affine, bool set_i2d)
1195 {
1196 if (selection->isEmpty())
1197 return;
1199 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1200 SPItem *item = SP_ITEM(l->data);
1202 NR::Point old_center(0,0);
1203 if (set_i2d && item->isCenterSet())
1204 old_center = item->getCenter();
1206 #if 0 /* Re-enable this once persistent guides have a graphical indication.
1207 At the time of writing, this is the only place to re-enable. */
1208 sp_item_update_cns(*item, selection->desktop());
1209 #endif
1211 // we're moving both a clone and its original
1212 bool transform_clone_with_original = (SP_IS_USE(item) && selection->includes( sp_use_get_original (SP_USE(item)) ));
1213 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)))) ));
1214 bool transform_flowtext_with_frame = (SP_IS_FLOWTEXT(item) && selection->includes( SP_FLOWTEXT(item)->get_frame (NULL))); // only the first frame so far
1215 bool transform_offset_with_source = (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) && selection->includes( sp_offset_get_source (SP_OFFSET(item)) );
1217 // "clones are unmoved when original is moved" preference
1218 int compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
1219 bool prefs_unmoved = (compensation == SP_CLONE_COMPENSATION_UNMOVED);
1220 bool prefs_parallel = (compensation == SP_CLONE_COMPENSATION_PARALLEL);
1222 // If this is a clone and it's selected along with its original, do not move it; it will feel the
1223 // transform of its original and respond to it itself. Without this, a clone is doubly
1224 // transformed, very unintuitive.
1225 // Same for textpath if we are also doing ANY transform to its path: do not touch textpath,
1226 // letters cannot be squeezed or rotated anyway, they only refill the changed path.
1227 // Same for linked offset if we are also moving its source: do not move it.
1228 if (transform_textpath_with_path || transform_offset_with_source) {
1229 // restore item->transform field from the repr, in case it was changed by seltrans
1230 sp_object_read_attr (SP_OBJECT (item), "transform");
1232 } else if (transform_flowtext_with_frame) {
1233 // apply the inverse of the region's transform to the <use> so that the flow remains
1234 // the same (even though the output itself gets transformed)
1235 for (SPObject *region = item->firstChild() ; region ; region = SP_OBJECT_NEXT(region)) {
1236 if (!SP_IS_FLOWREGION(region) && !SP_IS_FLOWREGIONEXCLUDE(region))
1237 continue;
1238 for (SPObject *use = region->firstChild() ; use ; use = SP_OBJECT_NEXT(use)) {
1239 if (!SP_IS_USE(use)) continue;
1240 sp_item_write_transform(SP_USE(use), SP_OBJECT_REPR(use), item->transform.inverse(), NULL);
1241 }
1242 }
1243 } else if (transform_clone_with_original) {
1244 // We are transforming a clone along with its original. The below matrix juggling is
1245 // necessary to ensure that they transform as a whole, i.e. the clone's induced
1246 // transform and its move compensation are both cancelled out.
1248 // restore item->transform field from the repr, in case it was changed by seltrans
1249 sp_object_read_attr (SP_OBJECT (item), "transform");
1251 // calculate the matrix we need to apply to the clone to cancel its induced transform from its original
1252 NR::Matrix t = matrix_to_desktop (matrix_from_desktop (affine, item), item);
1253 NR::Matrix t_inv = matrix_to_desktop (matrix_from_desktop (affine.inverse(), item), item);
1254 NR::Matrix result = t_inv * item->transform * t;
1256 if ((prefs_parallel || prefs_unmoved) && affine.is_translation()) {
1257 // we need to cancel out the move compensation, too
1259 // find out the clone move, same as in sp_use_move_compensate
1260 NR::Matrix parent = sp_use_get_parent_transform (SP_USE(item));
1261 NR::Matrix clone_move = parent.inverse() * t * parent;
1263 if (prefs_parallel) {
1264 NR::Matrix move = result * clone_move * t_inv;
1265 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &move);
1267 } else if (prefs_unmoved) {
1268 if (SP_IS_USE(sp_use_get_original(SP_USE(item))))
1269 clone_move = NR::identity();
1270 NR::Matrix move = result * clone_move;
1271 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &move);
1272 }
1274 } else {
1275 // just apply the result
1276 sp_item_write_transform(item, SP_OBJECT_REPR(item), result, &result);
1277 }
1279 } else {
1280 if (set_i2d) {
1281 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * affine);
1282 }
1283 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, NULL);
1284 }
1286 // if we're moving the actual object, not just updating the repr, we can transform the
1287 // center by the same matrix (only necessary for non-translations)
1288 if (set_i2d && item->isCenterSet() && !affine.is_translation()) {
1289 item->setCenter(old_center * affine);
1290 SP_OBJECT(item)->updateRepr();
1291 }
1292 }
1293 }
1295 void sp_selection_remove_transform()
1296 {
1297 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1298 if (desktop == NULL)
1299 return;
1301 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1303 GSList const *l = (GSList *) selection->reprList();
1304 while (l != NULL) {
1305 sp_repr_set_attr((Inkscape::XML::Node*)l->data, "transform", NULL);
1306 l = l->next;
1307 }
1309 sp_document_done(SP_DT_DOCUMENT(desktop));
1310 }
1312 void
1313 sp_selection_scale_absolute(Inkscape::Selection *selection,
1314 double const x0, double const x1,
1315 double const y0, double const y1)
1316 {
1317 if (selection->isEmpty())
1318 return;
1320 NR::Rect const bbox(selection->bounds());
1321 if (bbox.isEmpty()) {
1322 return;
1323 }
1325 NR::translate const p2o(-bbox.min());
1327 NR::scale const newSize(x1 - x0,
1328 y1 - y0);
1329 NR::scale const scale( newSize / NR::scale(bbox.dimensions()) );
1330 NR::translate const o2n(x0, y0);
1331 NR::Matrix const final( p2o * scale * o2n );
1333 sp_selection_apply_affine(selection, final);
1334 }
1337 void sp_selection_scale_relative(Inkscape::Selection *selection, NR::Point const &align, NR::scale const &scale)
1338 {
1339 if (selection->isEmpty())
1340 return;
1342 NR::Rect const bbox(selection->bounds());
1344 if (bbox.isEmpty()) {
1345 return;
1346 }
1348 // FIXME: ARBITRARY LIMIT: don't try to scale above 1 Mpx, it won't display properly and will crash sooner or later anyway
1349 if ( bbox.extent(NR::X) * scale[NR::X] > 1e6 ||
1350 bbox.extent(NR::Y) * scale[NR::Y] > 1e6 )
1351 {
1352 return;
1353 }
1355 NR::translate const n2d(-align);
1356 NR::translate const d2n(align);
1357 NR::Matrix const final( n2d * scale * d2n );
1358 sp_selection_apply_affine(selection, final);
1359 }
1361 void
1362 sp_selection_rotate_relative(Inkscape::Selection *selection, NR::Point const ¢er, gdouble const angle_degrees)
1363 {
1364 NR::translate const d2n(center);
1365 NR::translate const n2d(-center);
1366 NR::rotate const rotate(rotate_degrees(angle_degrees));
1367 NR::Matrix const final( NR::Matrix(n2d) * rotate * d2n );
1368 sp_selection_apply_affine(selection, final);
1369 }
1371 void
1372 sp_selection_skew_relative(Inkscape::Selection *selection, NR::Point const &align, double dx, double dy)
1373 {
1374 NR::translate const d2n(align);
1375 NR::translate const n2d(-align);
1376 NR::Matrix const skew(1, dy,
1377 dx, 1,
1378 0, 0);
1379 NR::Matrix const final( n2d * skew * d2n );
1380 sp_selection_apply_affine(selection, final);
1381 }
1383 void sp_selection_move_relative(Inkscape::Selection *selection, NR::Point const &move)
1384 {
1385 sp_selection_apply_affine(selection, NR::Matrix(NR::translate(move)));
1386 }
1388 void sp_selection_move_relative(Inkscape::Selection *selection, double dx, double dy)
1389 {
1390 sp_selection_apply_affine(selection, NR::Matrix(NR::translate(dx, dy)));
1391 }
1394 /**
1395 * \brief sp_selection_rotate_90
1396 *
1397 * This function rotates selected objects 90 degrees clockwise.
1398 *
1399 */
1401 void sp_selection_rotate_90_cw()
1402 {
1403 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1405 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1407 if (selection->isEmpty())
1408 return;
1410 GSList const *l = selection->itemList();
1411 NR::rotate const rot_neg_90(NR::Point(0, -1));
1412 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1413 SPItem *item = SP_ITEM(l2->data);
1414 sp_item_rotate_rel(item, rot_neg_90);
1415 }
1417 sp_document_done(SP_DT_DOCUMENT(desktop));
1418 }
1421 /**
1422 * \brief sp_selection_rotate_90_ccw
1423 *
1424 * This function rotates selected objects 90 degrees counter-clockwise.
1425 *
1426 */
1428 void sp_selection_rotate_90_ccw()
1429 {
1430 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1432 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1434 if (selection->isEmpty())
1435 return;
1437 GSList const *l = selection->itemList();
1438 NR::rotate const rot_neg_90(NR::Point(0, 1));
1439 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1440 SPItem *item = SP_ITEM(l2->data);
1441 sp_item_rotate_rel(item, rot_neg_90);
1442 }
1444 sp_document_done(SP_DT_DOCUMENT(desktop));
1445 }
1447 void
1448 sp_selection_rotate(Inkscape::Selection *selection, gdouble const angle_degrees)
1449 {
1450 if (selection->isEmpty())
1451 return;
1453 NR::Point const center(selection->bounds().midpoint());
1455 sp_selection_rotate_relative(selection, center, angle_degrees);
1457 sp_document_maybe_done(SP_DT_DOCUMENT(selection->desktop()),
1458 ( ( angle_degrees > 0 )
1459 ? "selector:rotate:ccw"
1460 : "selector:rotate:cw" ));
1461 }
1463 /**
1464 \param angle the angle in "angular pixels", i.e. how many visible pixels must move the outermost point of the rotated object
1465 */
1466 void
1467 sp_selection_rotate_screen(Inkscape::Selection *selection, gdouble angle)
1468 {
1469 if (selection->isEmpty())
1470 return;
1472 NR::Rect const bbox(selection->bounds());
1473 NR::Point const center(bbox.midpoint());
1475 gdouble const zoom = selection->desktop()->current_zoom();
1476 gdouble const zmove = angle / zoom;
1477 gdouble const r = NR::L2(bbox.max() - center);
1479 gdouble const zangle = 180 * atan2(zmove, r) / M_PI;
1481 sp_selection_rotate_relative(selection, center, zangle);
1483 sp_document_maybe_done(SP_DT_DOCUMENT(selection->desktop()),
1484 ( (angle > 0)
1485 ? "selector:rotate:ccw"
1486 : "selector:rotate:cw" ));
1487 }
1489 void
1490 sp_selection_scale(Inkscape::Selection *selection, gdouble grow)
1491 {
1492 if (selection->isEmpty())
1493 return;
1495 NR::Rect const bbox(selection->bounds());
1496 NR::Point const center(bbox.midpoint());
1497 double const max_len = bbox.maxExtent();
1499 // you can't scale "do nizhe pola" (below zero)
1500 if ( max_len + grow <= 1e-3 ) {
1501 return;
1502 }
1504 double const times = 1.0 + grow / max_len;
1505 sp_selection_scale_relative(selection, center, NR::scale(times, times));
1507 sp_document_maybe_done(SP_DT_DOCUMENT(selection->desktop()),
1508 ( (grow > 0)
1509 ? "selector:scale:larger"
1510 : "selector:scale:smaller" ));
1511 }
1513 void
1514 sp_selection_scale_screen(Inkscape::Selection *selection, gdouble grow_pixels)
1515 {
1516 sp_selection_scale(selection,
1517 grow_pixels / selection->desktop()->current_zoom());
1518 }
1520 void
1521 sp_selection_scale_times(Inkscape::Selection *selection, gdouble times)
1522 {
1523 if (selection->isEmpty())
1524 return;
1526 NR::Point const center(selection->bounds().midpoint());
1527 sp_selection_scale_relative(selection, center, NR::scale(times, times));
1528 sp_document_done(SP_DT_DOCUMENT(selection->desktop()));
1529 }
1531 void
1532 sp_selection_move(gdouble dx, gdouble dy)
1533 {
1534 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1535 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1536 if (selection->isEmpty()) {
1537 return;
1538 }
1540 sp_selection_move_relative(selection, dx, dy);
1542 if (dx == 0) {
1543 sp_document_maybe_done(SP_DT_DOCUMENT(desktop), "selector:move:vertical");
1544 } else if (dy == 0) {
1545 sp_document_maybe_done(SP_DT_DOCUMENT(desktop), "selector:move:horizontal");
1546 } else {
1547 sp_document_done(SP_DT_DOCUMENT(desktop));
1548 }
1549 }
1551 void
1552 sp_selection_move_screen(gdouble dx, gdouble dy)
1553 {
1554 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1556 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1557 if (selection->isEmpty()) {
1558 return;
1559 }
1561 // same as sp_selection_move but divide deltas by zoom factor
1562 gdouble const zoom = desktop->current_zoom();
1563 gdouble const zdx = dx / zoom;
1564 gdouble const zdy = dy / zoom;
1565 sp_selection_move_relative(selection, zdx, zdy);
1567 if (dx == 0) {
1568 sp_document_maybe_done(SP_DT_DOCUMENT(desktop), "selector:move:vertical");
1569 } else if (dy == 0) {
1570 sp_document_maybe_done(SP_DT_DOCUMENT(desktop), "selector:move:horizontal");
1571 } else {
1572 sp_document_done(SP_DT_DOCUMENT(desktop));
1573 }
1574 }
1576 namespace {
1578 template <typename D>
1579 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
1580 bool only_in_viewport, bool inlayer, bool onlyvisible, bool onlysensitive);
1582 template <typename D>
1583 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items, SPObject *root,
1584 bool only_in_viewport, bool inlayer, bool onlyvisible, bool onlysensitive);
1586 struct Forward {
1587 typedef SPObject *Iterator;
1589 static Iterator children(SPObject *o) { return sp_object_first_child(o); }
1590 static Iterator siblings_after(SPObject *o) { return SP_OBJECT_NEXT(o); }
1591 static void dispose(Iterator i) {}
1593 static SPObject *object(Iterator i) { return i; }
1594 static Iterator next(Iterator i) { return SP_OBJECT_NEXT(i); }
1595 };
1597 struct Reverse {
1598 typedef GSList *Iterator;
1600 static Iterator children(SPObject *o) {
1601 return make_list(o->firstChild(), NULL);
1602 }
1603 static Iterator siblings_after(SPObject *o) {
1604 return make_list(SP_OBJECT_PARENT(o)->firstChild(), o);
1605 }
1606 static void dispose(Iterator i) {
1607 g_slist_free(i);
1608 }
1610 static SPObject *object(Iterator i) {
1611 return reinterpret_cast<SPObject *>(i->data);
1612 }
1613 static Iterator next(Iterator i) { return i->next; }
1615 private:
1616 static GSList *make_list(SPObject *object, SPObject *limit) {
1617 GSList *list=NULL;
1618 while ( object != limit ) {
1619 list = g_slist_prepend(list, object);
1620 object = SP_OBJECT_NEXT(object);
1621 }
1622 return list;
1623 }
1624 };
1626 }
1628 void
1629 sp_selection_item_next(void)
1630 {
1631 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1632 g_return_if_fail(desktop != NULL);
1633 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1635 bool inlayer = prefs_get_int_attribute ("options.kbselection", "inlayer", 1);
1636 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
1637 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
1639 SPObject *root;
1640 if (inlayer) {
1641 root = desktop->currentLayer();
1642 } else {
1643 root = desktop->currentRoot();
1644 }
1646 SPItem *item=next_item_from_list<Forward>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1648 if (item) {
1649 selection->set(item);
1650 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1651 scroll_to_show_item(desktop, item);
1652 }
1653 }
1654 }
1656 void
1657 sp_selection_item_prev(void)
1658 {
1659 SPDocument *document = SP_ACTIVE_DOCUMENT;
1660 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1661 g_return_if_fail(document != NULL);
1662 g_return_if_fail(desktop != NULL);
1663 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1665 bool inlayer = prefs_get_int_attribute ("options.kbselection", "inlayer", 1);
1666 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
1667 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
1669 SPObject *root;
1670 if (inlayer) {
1671 root = desktop->currentLayer();
1672 } else {
1673 root = desktop->currentRoot();
1674 }
1676 SPItem *item=next_item_from_list<Reverse>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1678 if (item) {
1679 selection->set(item);
1680 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1681 scroll_to_show_item(desktop, item);
1682 }
1683 }
1684 }
1686 namespace {
1688 template <typename D>
1689 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items,
1690 SPObject *root, bool only_in_viewport, bool inlayer, bool onlyvisible, bool onlysensitive)
1691 {
1692 SPObject *current=root;
1693 while (items) {
1694 SPItem *item=SP_ITEM(items->data);
1695 if ( root->isAncestorOf(item) &&
1696 ( !only_in_viewport || desktop->isWithinViewport(item) ) )
1697 {
1698 current = item;
1699 break;
1700 }
1701 items = items->next;
1702 }
1704 GSList *path=NULL;
1705 while ( current != root ) {
1706 path = g_slist_prepend(path, current);
1707 current = SP_OBJECT_PARENT(current);
1708 }
1710 SPItem *next;
1711 // first, try from the current object
1712 next = next_item<D>(desktop, path, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1713 g_slist_free(path);
1715 if (!next) { // if we ran out of objects, start over at the root
1716 next = next_item<D>(desktop, NULL, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1717 }
1719 return next;
1720 }
1722 template <typename D>
1723 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
1724 bool only_in_viewport, bool inlayer, bool onlyvisible, bool onlysensitive)
1725 {
1726 typename D::Iterator children;
1727 typename D::Iterator iter;
1729 SPItem *found=NULL;
1731 if (path) {
1732 SPObject *object=reinterpret_cast<SPObject *>(path->data);
1733 g_assert(SP_OBJECT_PARENT(object) == root);
1734 if (desktop->isLayer(object)) {
1735 found = next_item<D>(desktop, path->next, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1736 }
1737 iter = children = D::siblings_after(object);
1738 } else {
1739 iter = children = D::children(root);
1740 }
1742 while ( iter && !found ) {
1743 SPObject *object=D::object(iter);
1744 if (desktop->isLayer(object)) {
1745 if (!inlayer) { // recurse into sublayers
1746 found = next_item<D>(desktop, NULL, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1747 }
1748 } else if ( SP_IS_ITEM(object) &&
1749 ( !only_in_viewport || desktop->isWithinViewport(SP_ITEM(object)) ) &&
1750 ( !onlyvisible || !desktop->itemIsHidden(SP_ITEM(object))) &&
1751 ( !onlysensitive || !SP_ITEM(object)->isLocked()) &&
1752 !desktop->isLayer(SP_ITEM(object)) )
1753 {
1754 found = SP_ITEM(object);
1755 }
1756 iter = D::next(iter);
1757 }
1759 D::dispose(children);
1761 return found;
1762 }
1764 }
1766 /**
1767 * If \a item is not entirely visible then adjust visible area to centre on the centre on of
1768 * \a item.
1769 */
1770 void scroll_to_show_item(SPDesktop *desktop, SPItem *item)
1771 {
1772 NR::Rect dbox = desktop->get_display_area();
1773 NR::Rect sbox = sp_item_bbox_desktop(item);
1775 if (dbox.contains(sbox) == false) {
1776 NR::Point const s_dt = sbox.midpoint();
1777 NR::Point const s_w = desktop->d2w(s_dt);
1778 NR::Point const d_dt = dbox.midpoint();
1779 NR::Point const d_w = desktop->d2w(d_dt);
1780 NR::Point const moved_w( d_w - s_w );
1781 gint const dx = (gint) moved_w[X];
1782 gint const dy = (gint) moved_w[Y];
1783 desktop->scroll_world(dx, dy);
1784 }
1785 }
1788 void
1789 sp_selection_clone()
1790 {
1791 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1792 if (desktop == NULL)
1793 return;
1795 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1797 // check if something is selected
1798 if (selection->isEmpty()) {
1799 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object</b> to clone."));
1800 return;
1801 }
1803 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
1805 selection->clear();
1807 // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
1808 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
1810 GSList *newsel = NULL;
1812 while (reprs) {
1813 Inkscape::XML::Node *sel_repr = (Inkscape::XML::Node *) reprs->data;
1814 Inkscape::XML::Node *parent = sp_repr_parent(sel_repr);
1816 Inkscape::XML::Node *clone = sp_repr_new("svg:use");
1817 sp_repr_set_attr(clone, "x", "0");
1818 sp_repr_set_attr(clone, "y", "0");
1819 sp_repr_set_attr(clone, "xlink:href", g_strdup_printf("#%s", sel_repr->attribute("id")));
1821 // add the new clone to the top of the original's parent
1822 parent->appendChild(clone);
1824 newsel = g_slist_prepend(newsel, clone);
1825 reprs = g_slist_remove(reprs, sel_repr);
1826 Inkscape::GC::release(clone);
1827 }
1829 sp_document_done(SP_DT_DOCUMENT(desktop));
1831 selection->setReprList(newsel);
1833 g_slist_free(newsel);
1834 }
1836 void
1837 sp_selection_unlink()
1838 {
1839 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1840 if (!desktop)
1841 return;
1843 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1845 if (selection->isEmpty()) {
1846 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>clone</b> to unlink."));
1847 return;
1848 }
1850 // Get a copy of current selection.
1851 GSList *new_select = NULL;
1852 bool unlinked = false;
1853 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
1854 items != NULL;
1855 items = items->next)
1856 {
1857 SPItem *use = (SPItem *) items->data;
1859 if (!SP_IS_USE(use)) {
1860 // keep the non-yse item in the new selection
1861 new_select = g_slist_prepend(new_select, use);
1862 continue;
1863 }
1865 SPItem *unlink = sp_use_unlink(SP_USE(use));
1866 unlinked = true;
1867 // Add ungrouped items to the new selection.
1868 new_select = g_slist_prepend(new_select, unlink);
1869 }
1871 if (new_select) { // set new selection
1872 selection->clear();
1873 selection->setList(new_select);
1874 g_slist_free(new_select);
1875 }
1876 if (!unlinked) {
1877 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No clones to unlink</b> in the selection."));
1878 }
1880 sp_document_done(SP_DT_DOCUMENT(desktop));
1881 }
1883 void
1884 sp_select_clone_original()
1885 {
1886 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1887 if (desktop == NULL)
1888 return;
1890 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1892 SPItem *item = selection->singleItem();
1894 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.");
1896 // Check if other than two objects are selected
1897 if (g_slist_length((GSList *) selection->itemList()) != 1 || !item) {
1898 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
1899 return;
1900 }
1902 SPItem *original = NULL;
1903 if (SP_IS_USE(item)) {
1904 original = sp_use_get_original (SP_USE(item));
1905 } else if (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) {
1906 original = sp_offset_get_source (SP_OFFSET(item));
1907 } else if (SP_IS_TEXT_TEXTPATH(item)) {
1908 original = sp_textpath_get_path_item (SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))));
1909 } else if (SP_IS_FLOWTEXT(item)) {
1910 original = SP_FLOWTEXT(item)->get_frame (NULL); // first frame only
1911 } else { // it's an object that we don't know what to do with
1912 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
1913 return;
1914 }
1916 if (!original) {
1917 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>Cannot find</b> the object to select (orphaned clone, offset, textpath, flowed text?)"));
1918 return;
1919 }
1921 for (SPObject *o = original; o && !SP_IS_ROOT(o); o = SP_OBJECT_PARENT (o)) {
1922 if (SP_IS_DEFS (o)) {
1923 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("The object you're trying to select is <b>not visible</b> (it is in <defs>)"));
1924 return;
1925 }
1926 }
1928 if (original) {
1929 selection->clear();
1930 selection->set(original);
1931 if (SP_CYCLING == SP_CYCLE_FOCUS) {
1932 scroll_to_show_item(desktop, original);
1933 }
1934 }
1935 }
1937 void
1938 sp_selection_tile(bool apply)
1939 {
1940 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1941 if (desktop == NULL)
1942 return;
1944 SPDocument *document = SP_DT_DOCUMENT(desktop);
1946 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1948 // check if something is selected
1949 if (selection->isEmpty()) {
1950 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to pattern."));
1951 return;
1952 }
1954 sp_document_ensure_up_to_date(document);
1955 NR::Rect r = selection->bounds();
1956 if (r.isEmpty()) {
1957 return;
1958 }
1960 // calculate the transform to be applied to objects to move them to 0,0
1961 NR::Point move_p = NR::Point(0, sp_document_height(document)) - (r.min() + NR::Point (0, r.extent(NR::Y)));
1962 move_p[NR::Y] = -move_p[NR::Y];
1963 NR::Matrix move = NR::Matrix (NR::translate (move_p));
1965 GSList *items = g_slist_copy((GSList *) selection->itemList());
1967 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
1969 // bottommost object, after sorting
1970 SPObject *parent = SP_OBJECT_PARENT (items->data);
1972 // remember the position of the first item
1973 gint pos = SP_OBJECT_REPR (items->data)->position();
1975 // create a list of duplicates
1976 GSList *repr_copies = NULL;
1977 for (GSList *i = items; i != NULL; i = i->next) {
1978 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate();
1979 repr_copies = g_slist_prepend (repr_copies, dup);
1980 }
1982 NR::Rect bounds(desktop->dt2doc(r.min()), desktop->dt2doc(r.max()));
1984 if (apply) {
1985 // delete objects so that their clones don't get alerted; this object will be restored shortly
1986 for (GSList *i = items; i != NULL; i = i->next) {
1987 SPObject *item = SP_OBJECT (i->data);
1988 item->deleteObject (false);
1989 }
1990 }
1992 // Hack: Temporarily set clone compensation to unmoved, so that we can move clone-originals
1993 // without disturbing clones.
1994 // See ActorAlign::on_button_click() in src/ui/dialog/align-and-distribute.cpp
1995 int saved_compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
1996 prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
1998 const gchar *pat_id = pattern_tile (repr_copies, bounds, document,
1999 NR::Matrix(NR::translate(desktop->dt2doc(NR::Point(r.min()[NR::X], r.max()[NR::Y])))), move);
2001 // restore compensation setting
2002 prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
2004 if (apply) {
2005 Inkscape::XML::Node *rect = sp_repr_new ("svg:rect");
2006 rect->setAttribute("style", g_strdup_printf("stroke:none;fill:url(#%s)", pat_id));
2007 sp_repr_set_svg_double(rect, "width", bounds.extent(NR::X));
2008 sp_repr_set_svg_double(rect, "height", bounds.extent(NR::Y));
2009 sp_repr_set_svg_double(rect, "x", bounds.min()[NR::X]);
2010 sp_repr_set_svg_double(rect, "y", bounds.min()[NR::Y]);
2012 // restore parent and position
2013 SP_OBJECT_REPR (parent)->appendChild(rect);
2014 rect->setPosition(pos > 0 ? pos : 0);
2015 SPItem *rectangle = (SPItem *) SP_DT_DOCUMENT (desktop)->getObjectByRepr(rect);
2017 Inkscape::GC::release(rect);
2019 selection->clear();
2020 selection->set(rectangle);
2021 }
2023 g_slist_free (items);
2025 sp_document_done (document);
2026 }
2028 void
2029 sp_selection_untile()
2030 {
2031 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2032 if (desktop == NULL)
2033 return;
2035 SPDocument *document = SP_DT_DOCUMENT(desktop);
2037 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
2039 // check if something is selected
2040 if (selection->isEmpty()) {
2041 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object with pattern fill</b> to extract objects from."));
2042 return;
2043 }
2045 GSList *new_select = NULL;
2047 bool did = false;
2049 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2050 items != NULL;
2051 items = items->next) {
2053 SPItem *item = (SPItem *) items->data;
2055 SPStyle *style = SP_OBJECT_STYLE (item);
2057 if (!style || style->fill.type != SP_PAINT_TYPE_PAINTSERVER)
2058 continue;
2060 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
2062 if (!SP_IS_PATTERN(server))
2063 continue;
2065 did = true;
2067 SPPattern *pattern = pattern_getroot (SP_PATTERN (server));
2069 NR::Matrix pat_transform = pattern_patternTransform (SP_PATTERN (server));
2070 pat_transform *= item->transform;
2072 for (SPObject *child = sp_object_first_child(SP_OBJECT(pattern)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2073 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate();
2074 SPItem *i = SP_ITEM (desktop->currentLayer()->appendChildRepr(copy));
2076 // FIXME: relink clones to the new canvas objects
2077 // use SPObject::setid when mental finishes it to steal ids of
2079 // this is needed to make sure the new item has curve (simply requestDisplayUpdate does not work)
2080 sp_document_ensure_up_to_date (document);
2082 NR::Matrix transform( i->transform * pat_transform );
2083 sp_item_write_transform(i, SP_OBJECT_REPR(i), transform);
2085 new_select = g_slist_prepend(new_select, i);
2086 }
2088 SPCSSAttr *css = sp_repr_css_attr_new ();
2089 sp_repr_css_set_property (css, "fill", "none");
2090 sp_repr_css_change (SP_OBJECT_REPR (item), css, "style");
2091 }
2093 if (!did) {
2094 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No pattern fills</b> in the selection."));
2095 } else {
2096 sp_document_done(SP_DT_DOCUMENT(desktop));
2097 selection->setList(new_select);
2098 }
2099 }
2101 void
2102 sp_selection_create_bitmap_copy ()
2103 {
2104 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2105 if (desktop == NULL)
2106 return;
2108 SPDocument *document = SP_DT_DOCUMENT(desktop);
2110 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
2112 // check if something is selected
2113 if (selection->isEmpty()) {
2114 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to make a bitmap copy."));
2115 return;
2116 }
2118 // Get the bounding box of the selection
2119 NRRect bbox;
2120 sp_document_ensure_up_to_date (document);
2121 selection->bounds(&bbox);
2122 if (NR_RECT_DFLS_TEST_EMPTY(&bbox)) {
2123 return; // exceptional situation, so not bother with a translatable error message, just quit quietly
2124 }
2126 // List of the items to show; all others will be hidden
2127 GSList *items = g_slist_copy ((GSList *) selection->itemList());
2129 // Sort items so that the topmost comes last
2130 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
2132 // Generate a random value from the current time (you may create bitmap from the same object(s)
2133 // multiple times, and this is done so that they don't clash)
2134 GTimeVal cu;
2135 g_get_current_time (&cu);
2136 guint current = (int) (cu.tv_sec * 1000000 + cu.tv_usec) % 1024;
2138 // Create the filename
2139 gchar *filename = g_strdup_printf ("%s-%s-%u.png", document->name, SP_OBJECT_REPR(items->data)->attribute("id"), current);
2140 // Imagemagick is known not to handle spaces in filenames, so we replace anything but letters,
2141 // digits, and a few other chars, with "_"
2142 filename = g_strcanon (filename, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.=+~$#@^&!?", '_');
2143 // Build the complete path by adding document->base if set
2144 gchar *filepath = g_build_filename (document->base?document->base:"", filename, NULL);
2146 //g_print ("%s\n", filepath);
2148 // Remember parent and z-order of the topmost one
2149 gint pos = SP_OBJECT_REPR(g_slist_last(items)->data)->position();
2150 SPObject *parent_object = SP_OBJECT_PARENT(g_slist_last(items)->data);
2151 Inkscape::XML::Node *parent = SP_OBJECT_REPR(parent_object);
2153 // Calculate resolution
2154 double res;
2155 int const prefs_res = prefs_get_int_attribute ("options.createbitmap", "resolution", 0);
2156 if (0 < prefs_res) {
2157 // If it's given explicitly in prefs, take it
2158 res = prefs_res;
2159 } else {
2160 // Otherwise look up minimum bitmap size (default 250 pixels) and calculate resolution from it
2161 int const prefs_min = prefs_get_int_attribute ("options.createbitmap", "minsize", 250);
2162 res = prefs_min / MIN ((bbox.x1 - bbox.x0), (bbox.y1 - bbox.y0));
2163 }
2165 // The width and height of the bitmap in pixels
2166 unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * res);
2167 unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * res);
2169 // Find out if we have to run a filter
2170 const gchar *run = NULL;
2171 const gchar *filter = prefs_get_string_attribute ("options.createbitmap", "filter");
2172 if (filter) {
2173 // filter command is given;
2174 // see if we have a parameter to pass to it
2175 const gchar *param1 = prefs_get_string_attribute ("options.createbitmap", "filter_param1");
2176 if (param1) {
2177 if (param1[strlen(param1) - 1] == '%') {
2178 // if the param string ends with %, interpret it as a percentage of the image's max dimension
2179 gchar p1[256];
2180 g_ascii_dtostr (p1, 256, ceil (g_ascii_strtod (param1, NULL) * MAX(width, height) / 100));
2181 // the first param is always the image filename, the second is param1
2182 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, p1);
2183 } else {
2184 // otherwise pass the param1 unchanged
2185 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, param1);
2186 }
2187 } else {
2188 // run without extra parameter
2189 run = g_strdup_printf ("%s \"%s\"", filter, filepath);
2190 }
2191 }
2193 // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
2194 NR::Matrix eek = sp_item_i2d_affine (SP_ITEM(parent_object));
2195 NR::Matrix t = NR::scale (1/res, -1/res) * NR::translate (bbox.x0, bbox.y1) * eek.inverse();
2197 // Do the export
2198 sp_export_png_file(document, filepath,
2199 bbox.x0, bbox.y0, bbox.x1, bbox.y1,
2200 width, height,
2201 (guint32) 0xffffff00,
2202 NULL, NULL,
2203 true, /*bool force_overwrite,*/
2204 items);
2206 g_slist_free (items);
2208 // Run filter, if any
2209 if (run) {
2210 g_print ("Running external filter: %s\n", run);
2211 system (run);
2212 }
2214 // Import the image back
2215 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filepath, NULL);
2216 if (pb) {
2217 // Create the repr for the image
2218 Inkscape::XML::Node * repr = sp_repr_new ("svg:image");
2219 repr->setAttribute("xlink:href", filename);
2220 repr->setAttribute("sodipodi:absref", filepath);
2221 sp_repr_set_svg_double(repr, "width", gdk_pixbuf_get_width(pb));
2222 sp_repr_set_svg_double(repr, "height", gdk_pixbuf_get_height(pb));
2224 // Write transform
2225 gchar c[256];
2226 if (sp_svg_transform_write(c, 256, t)) {
2227 repr->setAttribute("transform", c);
2228 }
2230 // add the new repr to the parent
2231 parent->appendChild(repr);
2233 // move to the saved position
2234 repr->setPosition(pos > 0 ? pos + 1 : 1);
2236 // Set selection to the new image
2237 selection->clear();
2238 selection->add(repr);
2240 // Clean up
2241 Inkscape::GC::release(repr);
2242 gdk_pixbuf_unref (pb);
2244 // Complete undoable transaction
2245 sp_document_done (document);
2246 }
2248 g_free (filename);
2249 g_free (filepath);
2250 }
2252 /**
2253 * \brief sp_selection_set_mask
2254 *
2255 * This function creates a mask or clipPath from selection
2256 * Two different modes:
2257 * if applyToLayer, all selection is moved to DEFS as mask/clippath
2258 * and is applied to current layer
2259 * otherwise, topmost object is used as mask for other objects
2260 * If \a apply_clip_path parameter is true, clipPath is created, otherwise mask
2261 *
2262 */
2263 void
2264 sp_selection_set_mask(bool apply_clip_path, bool apply_to_layer)
2265 {
2266 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2267 if (desktop == NULL)
2268 return;
2270 SPDocument *document = SP_DT_DOCUMENT(desktop);
2272 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
2274 // check if something is selected
2275 bool is_empty = selection->isEmpty();
2276 if ( apply_to_layer && is_empty) {
2277 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to create mask from."));
2278 return;
2279 } else if (!apply_to_layer && ( is_empty || NULL == selection->itemList()->next )) {
2280 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select mask object and <b>object(s)</b> to apply mask to."));
2281 return;
2282 }
2284 sp_document_ensure_up_to_date(document);
2286 GSList *items = g_slist_copy((GSList *) selection->itemList());
2288 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2290 // create a list of duplicates
2291 GSList *mask_items = NULL;
2292 GSList *apply_to_items = NULL;
2293 bool topmost = prefs_get_int_attribute ("options.maskobject", "topmost", 1);
2294 bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
2296 if (apply_to_layer) {
2297 // all selected items are used for mask, which is applied to a layer
2298 apply_to_items = g_slist_prepend (apply_to_items, desktop->currentLayer());
2300 for (GSList *i = items; i != NULL; i = i->next) {
2301 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate();
2302 mask_items = g_slist_prepend (mask_items, dup);
2304 if (remove_original) {
2305 SPObject *item = SP_OBJECT (i->data);
2306 item->deleteObject (false);
2307 }
2308 }
2309 } else if (!topmost) {
2310 // topmost item is used as a mask, which is applied to other items in a selection
2311 GSList *i = items;
2312 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate();
2313 mask_items = g_slist_prepend (mask_items, dup);
2315 if (remove_original) {
2316 SPObject *item = SP_OBJECT (i->data);
2317 item->deleteObject (false);
2318 }
2320 for (i = i->next; i != NULL; i = i->next) {
2321 apply_to_items = g_slist_prepend (apply_to_items, i->data);
2322 }
2323 } else {
2324 GSList *i = NULL;
2325 for (i = items; NULL != i->next; i = i->next) {
2326 apply_to_items = g_slist_prepend (apply_to_items, i->data);
2327 }
2329 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate();
2330 mask_items = g_slist_prepend (mask_items, dup);
2332 if (remove_original) {
2333 SPObject *item = SP_OBJECT (i->data);
2334 item->deleteObject (false);
2335 }
2336 }
2338 g_slist_free (items);
2339 items = NULL;
2341 gchar const* attributeName = apply_clip_path ? "clip-path" : "mask";
2342 for (GSList *i = apply_to_items; NULL != i; i = i->next) {
2343 SPItem *item = reinterpret_cast<SPItem *>(i->data);
2344 // inverted object transform should be applied to a mask object,
2345 // as mask is calculated in user space (after applying transform)
2346 NR::Matrix maskTransform (item->transform.inverse());
2348 GSList *mask_items_dup = NULL;
2349 for (GSList *mask_item = mask_items; NULL != mask_item; mask_item = mask_item->next) {
2350 Inkscape::XML::Node *dup = reinterpret_cast<Inkscape::XML::Node *>(mask_item->data)->duplicate();
2351 mask_items_dup = g_slist_prepend (mask_items_dup, dup);
2352 }
2354 const gchar *mask_id = NULL;
2355 if (apply_clip_path) {
2356 mask_id = sp_clippath_create(mask_items_dup, document, &maskTransform);
2357 } else {
2358 mask_id = sp_mask_create(mask_items_dup, document, &maskTransform);
2359 }
2361 g_slist_free (mask_items_dup);
2362 mask_items_dup = NULL;
2364 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, g_strdup_printf("url(#%s)", mask_id));
2365 }
2367 g_slist_free (mask_items);
2368 g_slist_free (apply_to_items);
2370 sp_document_done (document);
2371 }
2373 void sp_selection_unset_mask(bool apply_clip_path) {
2374 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2375 if (desktop == NULL)
2376 return;
2378 SPDocument *document = SP_DT_DOCUMENT(desktop);
2379 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
2381 // check if something is selected
2382 if (selection->isEmpty()) {
2383 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to remove mask from."));
2384 return;
2385 }
2387 bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
2388 sp_document_ensure_up_to_date(document);
2390 gchar const* attributeName = apply_clip_path ? "clip-path" : "mask";
2391 std::map<SPObject*,SPItem*> referenced_objects;
2392 for (GSList const*i = selection->itemList(); NULL != i; i = i->next) {
2393 if (remove_original) {
2394 // remember referenced mask/clippath, so orphaned masks can be moved back to document
2395 SPItem *item = reinterpret_cast<SPItem *>(i->data);
2396 Inkscape::URIReference *uri_ref = NULL;
2398 if (apply_clip_path) {
2399 uri_ref = item->clip_ref;
2400 } else {
2401 uri_ref = item->mask_ref;
2402 }
2404 // collect distinct mask object (and associate with item to apply transform)
2405 if (NULL != uri_ref && NULL != uri_ref->getObject()) {
2406 referenced_objects[uri_ref->getObject()] = item;
2407 }
2408 }
2410 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, "none");
2411 }
2413 // restore mask objects into a document
2414 for ( std::map<SPObject*,SPItem*>::iterator it = referenced_objects.begin() ; it != referenced_objects.end() ; ++it) {
2415 SPObject *obj = (*it).first;
2416 GSList *items_to_move = NULL;
2417 for (SPObject *child = sp_object_first_child(obj) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2418 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate();
2419 items_to_move = g_slist_prepend (items_to_move, copy);
2420 }
2422 if (!obj->isReferenced()) {
2423 // delete from defs if no other object references this mask
2424 obj->deleteObject(false);
2425 }
2427 for (GSList *i = items_to_move; NULL != i; i = i->next) {
2428 SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr((Inkscape::XML::Node *)i->data));
2429 selection->add((Inkscape::XML::Node *)i->data);
2431 // transform mask, so it is moved the same spot there mask was applied
2432 NR::Matrix transform (item->transform);
2433 transform *= (*it).second->transform;
2434 sp_item_write_transform(item, SP_OBJECT_REPR(item), transform);
2435 }
2437 g_slist_free (items_to_move);
2438 }
2440 sp_document_done (document);
2441 }
2443 /*
2444 Local Variables:
2445 mode:c++
2446 c-file-style:"stroustrup"
2447 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2448 indent-tabs-mode:nil
2449 fill-column:99
2450 End:
2451 */
2452 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :