d01bb3e50561555d8f9ad1d16a821429406a7fbb
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;
77 NR::Rect size_clipboard(NR::Point(0,0), NR::Point(0,0));
79 static void sp_copy_stuff_used_by_item(GSList **defs_clip, SPItem *item, const GSList *items);
81 void sp_selection_copy_one (Inkscape::XML::Node *repr, NR::Matrix full_t, GSList **clip)
82 {
83 Inkscape::XML::Node *copy = repr->duplicate();
85 // copy complete inherited style
86 SPCSSAttr *css = sp_repr_css_attr_inherited(repr, "style");
87 sp_repr_css_set(copy, css, "style");
88 sp_repr_css_attr_unref(css);
90 // write the complete accummulated transform passed to us
91 // (we're dealing with unattached repr, so we write to its attr instead of using sp_item_set_transform)
92 gchar affinestr[80];
93 if (sp_svg_transform_write(affinestr, 79, full_t)) {
94 copy->setAttribute("transform", affinestr);
95 } else {
96 copy->setAttribute("transform", NULL);
97 }
99 *clip = g_slist_prepend(*clip, copy);
100 }
102 void sp_selection_copy_impl (const GSList *items, GSList **clip, GSList **defs_clip, SPCSSAttr **style_clip)
103 {
105 // Copy stuff referenced by all items to defs_clip:
106 if (defs_clip) {
107 for (GSList *i = (GSList *) items; i != NULL; i = i->next) {
108 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (i->data), items);
109 }
110 *defs_clip = g_slist_reverse(*defs_clip);
111 }
113 // Store style:
114 if (style_clip) {
115 SPItem *item = SP_ITEM (items->data); // take from the first selected item
116 *style_clip = take_style_from_item (item);
117 }
119 if (clip) {
120 // Sort items:
121 GSList *sorted_items = g_slist_copy ((GSList *) items);
122 sorted_items = g_slist_sort((GSList *) sorted_items, (GCompareFunc) sp_object_compare_position);
124 // Copy item reprs:
125 for (GSList *i = (GSList *) sorted_items; i != NULL; i = i->next) {
126 sp_selection_copy_one (SP_OBJECT_REPR (i->data), sp_item_i2doc_affine(SP_ITEM (i->data)), clip);
127 }
129 *clip = g_slist_reverse(*clip);
130 g_slist_free ((GSList *) sorted_items);
131 }
132 }
134 /**
135 Add gradients/patterns/markers referenced by copied objects to defs
136 */
137 void
138 paste_defs (GSList **defs_clip, SPDocument *doc)
139 {
140 if (!defs_clip)
141 return;
143 for (GSList *gl = *defs_clip; gl != NULL; gl = gl->next) {
144 SPDefs *defs= (SPDefs *) SP_DOCUMENT_DEFS(doc);
145 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) gl->data;
146 gchar const *id = repr->attribute("id");
147 if (!id || !doc->getObjectById(id)) {
148 Inkscape::XML::Node *copy = repr->duplicate();
149 SP_OBJECT_REPR(defs)->addChild(copy, NULL);
150 Inkscape::GC::release(copy);
151 }
152 }
153 }
155 GSList *sp_selection_paste_impl (SPDocument *document, SPObject *parent, GSList **clip, GSList **defs_clip)
156 {
157 paste_defs (defs_clip, document);
159 GSList *copied = NULL;
160 // add objects to document
161 for (GSList *l = *clip; l != NULL; l = l->next) {
162 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
163 Inkscape::XML::Node *copy = repr->duplicate();
165 // premultiply the item transform by the accumulated parent transform in the paste layer
166 NR::Matrix local = sp_item_i2doc_affine(SP_ITEM(parent));
167 if (!local.test_identity()) {
168 gchar const *t_str = copy->attribute("transform");
169 NR::Matrix item_t (NR::identity());
170 if (t_str)
171 sp_svg_transform_read(t_str, &item_t);
172 item_t *= local.inverse();
173 // (we're dealing with unattached repr, so we write to its attr instead of using sp_item_set_transform)
174 gchar affinestr[80];
175 if (sp_svg_transform_write(affinestr, 79, item_t)) {
176 copy->setAttribute("transform", affinestr);
177 } else {
178 copy->setAttribute("transform", NULL);
179 }
180 }
182 parent->appendChildRepr(copy);
183 copied = g_slist_prepend(copied, copy);
184 Inkscape::GC::release(copy);
185 }
186 return copied;
187 }
189 void sp_selection_delete_impl(const GSList *items)
190 {
191 for (const GSList *i = items ; i ; i = i->next ) {
192 sp_object_ref((SPObject *)i->data, NULL);
193 }
194 for (const GSList *i = items; i != NULL; i = i->next) {
195 SPItem *item = (SPItem *) i->data;
196 SP_OBJECT(item)->deleteObject();
197 sp_object_unref((SPObject *)item, NULL);
198 }
199 }
202 void sp_selection_delete()
203 {
204 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
205 if (desktop == NULL) {
206 return;
207 }
209 if (tools_isactive (desktop, TOOLS_TEXT))
210 if (sp_text_delete_selection(desktop->event_context)) {
211 sp_document_done(SP_DT_DOCUMENT(desktop));
212 return;
213 }
215 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
217 // check if something is selected
218 if (selection->isEmpty()) {
219 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Nothing</b> was deleted."));
220 return;
221 }
223 const GSList *selected = g_slist_copy(const_cast<GSList *>(selection->itemList()));
224 selection->clear();
225 sp_selection_delete_impl (selected);
226 g_slist_free ((GSList *) selected);
228 /* a tool may have set up private information in it's selection context
229 * that depends on desktop items. I think the only sane way to deal with
230 * this currently is to reset the current tool, which will reset it's
231 * associated selection context. For example: deleting an object
232 * while moving it around the canvas.
233 */
234 tools_switch ( desktop, tools_active ( desktop ) );
236 sp_document_done(SP_DT_DOCUMENT(desktop));
237 }
239 /* fixme: sequencing */
240 void sp_selection_duplicate()
241 {
242 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
243 if (desktop == NULL)
244 return;
246 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
248 // check if something is selected
249 if (selection->isEmpty()) {
250 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to duplicate."));
251 return;
252 }
254 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
256 selection->clear();
258 // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
259 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
261 GSList *newsel = NULL;
263 while (reprs) {
264 Inkscape::XML::Node *parent = ((Inkscape::XML::Node *) reprs->data)->parent();
265 Inkscape::XML::Node *copy = ((Inkscape::XML::Node *) reprs->data)->duplicate();
267 parent->appendChild(copy);
269 newsel = g_slist_prepend(newsel, copy);
270 reprs = g_slist_remove(reprs, reprs->data);
271 Inkscape::GC::release(copy);
272 }
274 sp_document_done(SP_DT_DOCUMENT(desktop));
276 selection->setReprList(newsel);
278 g_slist_free(newsel);
279 }
281 void sp_edit_clear_all()
282 {
283 SPDesktop *dt = SP_ACTIVE_DESKTOP;
284 if (!dt)
285 return;
287 SPDocument *doc = SP_DT_DOCUMENT(dt);
288 SP_DT_SELECTION(dt)->clear();
290 g_return_if_fail(SP_IS_GROUP(dt->currentLayer()));
291 GSList *items = sp_item_group_item_list(SP_GROUP(dt->currentLayer()));
293 while (items) {
294 SP_OBJECT (items->data)->deleteObject();
295 items = g_slist_remove(items, items->data);
296 }
298 sp_document_done(doc);
299 }
301 GSList *
302 get_all_items (GSList *list, SPObject *from, SPDesktop *desktop, bool onlyvisible, bool onlysensitive, const GSList *exclude)
303 {
304 for (SPObject *child = sp_object_first_child(SP_OBJECT(from)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
305 if (SP_IS_ITEM(child) &&
306 !desktop->isLayer(SP_ITEM(child)) &&
307 (!onlysensitive || !SP_ITEM(child)->isLocked()) &&
308 (!onlyvisible || !desktop->itemIsHidden(SP_ITEM(child))) &&
309 (!exclude || !g_slist_find ((GSList *) exclude, child))
310 )
311 {
312 list = g_slist_prepend (list, SP_ITEM(child));
313 }
315 if (SP_IS_ITEM(child) && desktop->isLayer(SP_ITEM(child))) {
316 list = get_all_items (list, child, desktop, onlyvisible, onlysensitive, exclude);
317 }
318 }
320 return list;
321 }
323 void sp_edit_select_all_full (bool force_all_layers, bool invert)
324 {
325 SPDesktop *dt = SP_ACTIVE_DESKTOP;
326 if (!dt)
327 return;
329 Inkscape::Selection *selection = SP_DT_SELECTION(dt);
331 g_return_if_fail(SP_IS_GROUP(dt->currentLayer()));
333 bool inlayer = prefs_get_int_attribute ("options.kbselection", "inlayer", 1);
334 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
335 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
337 GSList *items = NULL;
339 const GSList *exclude = NULL;
340 if (invert) {
341 exclude = selection->itemList();
342 }
344 if (inlayer && !force_all_layers) {
346 if ( (onlysensitive && SP_ITEM(dt->currentLayer())->isLocked()) ||
347 (onlyvisible && dt->itemIsHidden(SP_ITEM(dt->currentLayer()))) )
348 return;
350 GSList *all_items = sp_item_group_item_list(SP_GROUP(dt->currentLayer()));
352 for (GSList *i = all_items; i; i = i->next) {
353 SPItem *item = SP_ITEM (i->data);
355 if (item && (!onlysensitive || !item->isLocked())) {
356 if (!onlyvisible || !dt->itemIsHidden(item)) {
357 if (!dt->isLayer(item)) {
358 if (!invert || !g_slist_find ((GSList *) exclude, item)) {
359 items = g_slist_prepend (items, item); // leave it in the list
360 }
361 }
362 }
363 }
364 }
366 g_slist_free (all_items);
368 } else {
369 items = get_all_items (NULL, dt->currentRoot(), dt, onlyvisible, onlysensitive, exclude);
370 }
372 selection->setList (items);
374 if (items) {
375 g_slist_free (items);
376 }
377 }
379 void sp_edit_select_all ()
380 {
381 sp_edit_select_all_full (false, false);
382 }
384 void sp_edit_select_all_in_all_layers ()
385 {
386 sp_edit_select_all_full (true, false);
387 }
389 void sp_edit_invert ()
390 {
391 sp_edit_select_all_full (false, true);
392 }
394 void sp_edit_invert_in_all_layers ()
395 {
396 sp_edit_select_all_full (true, true);
397 }
399 void sp_selection_group()
400 {
401 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
402 if (desktop == NULL)
403 return;
405 SPDocument *document = SP_DT_DOCUMENT (desktop);
407 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
409 // Check if something is selected.
410 if (selection->isEmpty()) {
411 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>two or more objects</b> to group."));
412 return;
413 }
415 GSList const *l = (GSList *) selection->reprList();
417 // Check if at least two objects are selected.
418 if (l->next == NULL) {
419 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>at least two objects</b> to group."));
420 return;
421 }
423 GSList *p = g_slist_copy((GSList *) l);
425 selection->clear();
427 p = g_slist_sort(p, (GCompareFunc) sp_repr_compare_position);
429 // Remember the position and parent of the topmost object.
430 gint topmost = ((Inkscape::XML::Node *) g_slist_last(p)->data)->position();
431 Inkscape::XML::Node *topmost_parent = ((Inkscape::XML::Node *) g_slist_last(p)->data)->parent();
433 Inkscape::XML::Node *group = sp_repr_new("svg:g");
435 while (p) {
436 Inkscape::XML::Node *current = (Inkscape::XML::Node *) p->data;
438 if (current->parent() == topmost_parent) {
439 Inkscape::XML::Node *spnew = current->duplicate();
440 sp_repr_unparent(current);
441 group->appendChild(spnew);
442 Inkscape::GC::release(spnew);
443 topmost --; // only reduce count for those items deleted from topmost_parent
444 } else { // move it to topmost_parent first
445 GSList *temp_clip = NULL;
447 // At this point, current may already have no item, due to its being a clone whose original is already moved away
448 // So we copy it artificially calculating the transform from its repr->attr("transform") and the parent transform
449 gchar const *t_str = current->attribute("transform");
450 NR::Matrix item_t (NR::identity());
451 if (t_str)
452 sp_svg_transform_read(t_str, &item_t);
453 item_t *= sp_item_i2doc_affine(SP_ITEM(document->getObjectByRepr(current->parent())));
454 //FIXME: when moving both clone and original from a transformed group (either by
455 //grouping into another parent, or by cut/paste) the transform from the original's
456 //parent becomes embedded into original itself, and this affects its clones. Fix
457 //this by remembering the transform diffs we write to each item into an array and
458 //then, if this is clone, looking up its original in that array and pre-multiplying
459 //it by the inverse of that original's transform diff.
461 sp_selection_copy_one (current, item_t, &temp_clip);
462 sp_repr_unparent(current);
464 // paste into topmost_parent (temporarily)
465 GSList *copied = sp_selection_paste_impl (document, document->getObjectByRepr(topmost_parent), &temp_clip, NULL);
466 if (temp_clip) g_slist_free (temp_clip);
467 if (copied) { // if success,
468 // take pasted object (now in topmost_parent)
469 Inkscape::XML::Node *in_topmost = (Inkscape::XML::Node *) copied->data;
470 // make a copy
471 Inkscape::XML::Node *spnew = in_topmost->duplicate();
472 // remove pasted
473 sp_repr_unparent(in_topmost);
474 // put its copy into group
475 group->appendChild(spnew);
476 Inkscape::GC::release(spnew);
477 g_slist_free (copied);
478 }
479 }
480 p = g_slist_remove(p, current);
481 }
483 // Add the new group to the topmost members' parent
484 topmost_parent->appendChild(group);
486 // Move to the position of the topmost, reduced by the number of items deleted from topmost_parent
487 group->setPosition(topmost + 1);
489 sp_document_done(SP_DT_DOCUMENT(desktop));
491 selection->set(group);
492 Inkscape::GC::release(group);
493 }
495 void sp_selection_ungroup()
496 {
497 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
498 if (desktop == NULL)
499 return;
501 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
503 if (selection->isEmpty()) {
504 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>group</b> to ungroup."));
505 return;
506 }
508 GSList *items = g_slist_copy((GSList *) selection->itemList());
509 selection->clear();
511 // Get a copy of current selection.
512 GSList *new_select = NULL;
513 bool ungrouped = false;
514 for (GSList *i = items;
515 i != NULL;
516 i = i->next)
517 {
518 SPItem *group = (SPItem *) i->data;
520 // when ungrouping cloned groups with their originals, some objects that were selected may no more exist due to unlinking
521 if (!SP_IS_OBJECT(group)) {
522 continue;
523 }
525 /* We do not allow ungrouping <svg> etc. (lauris) */
526 if (strcmp(SP_OBJECT_REPR(group)->name(), "svg:g") && strcmp(SP_OBJECT_REPR(group)->name(), "svg:switch")) {
527 // keep the non-group item in the new selection
528 selection->add(group);
529 continue;
530 }
532 GSList *children = NULL;
533 /* This is not strictly required, but is nicer to rely on group ::destroy (lauris) */
534 sp_item_group_ungroup(SP_GROUP(group), &children, false);
535 ungrouped = true;
536 // Add ungrouped items to the new selection.
537 new_select = g_slist_concat(new_select, children);
538 }
540 if (new_select) { // Set new selection.
541 selection->addList(new_select);
542 g_slist_free(new_select);
543 }
544 if (!ungrouped) {
545 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No groups</b> to ungroup in the selection."));
546 }
548 g_slist_free(items);
550 sp_document_done(SP_DT_DOCUMENT(desktop));
551 }
553 static SPGroup *
554 sp_item_list_common_parent_group(const GSList *items)
555 {
556 if (!items) {
557 return NULL;
558 }
559 SPObject *parent = SP_OBJECT_PARENT(items->data);
560 /* Strictly speaking this CAN happen, if user selects <svg> from XML editor */
561 if (!SP_IS_GROUP(parent)) {
562 return NULL;
563 }
564 for (items = items->next; items; items = items->next) {
565 if (SP_OBJECT_PARENT(items->data) != parent) {
566 return NULL;
567 }
568 }
570 return SP_GROUP(parent);
571 }
573 /** Finds out the minimum common bbox of the selected items
574 */
575 NR::Rect
576 enclose_items(const GSList *items)
577 {
578 g_assert(items != NULL);
580 NR::Rect r = sp_item_bbox_desktop((SPItem *) items->data);
582 for (GSList *i = items->next; i; i = i->next) {
583 r = NR::Rect::union_bounds(r, sp_item_bbox_desktop((SPItem *) i->data));
584 }
586 return r;
587 }
589 SPObject *
590 prev_sibling(SPObject *child)
591 {
592 SPObject *parent = SP_OBJECT_PARENT(child);
593 if (!SP_IS_GROUP(parent)) {
594 return NULL;
595 }
596 for ( SPObject *i = sp_object_first_child(parent) ; i; i = SP_OBJECT_NEXT(i) ) {
597 if (i->next == child)
598 return i;
599 }
600 return NULL;
601 }
603 void
604 sp_selection_raise()
605 {
606 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
607 if (!desktop)
608 return;
610 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
612 GSList const *items = (GSList *) selection->itemList();
613 if (!items) {
614 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise."));
615 return;
616 }
618 SPGroup const *group = sp_item_list_common_parent_group(items);
619 if (!group) {
620 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
621 return;
622 }
624 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
626 /* construct reverse-ordered list of selected children */
627 GSList *rev = g_slist_copy((GSList *) items);
628 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
630 // find out the common bbox of the selected items
631 NR::Rect selected = enclose_items(items);
633 // for all objects in the selection (starting from top)
634 while (rev) {
635 SPObject *child = SP_OBJECT(rev->data);
636 // for each selected object, find the next sibling
637 for (SPObject *newref = child->next; newref; newref = newref->next) {
638 // if the sibling is an item AND overlaps our selection,
639 if (SP_IS_ITEM(newref) && selected.intersects(sp_item_bbox_desktop(SP_ITEM(newref)))) {
640 // AND if it's not one of our selected objects,
641 if (!g_slist_find((GSList *) items, newref)) {
642 // move the selected object after that sibling
643 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(newref));
644 }
645 break;
646 }
647 }
648 rev = g_slist_remove(rev, child);
649 }
651 sp_document_done(SP_DT_DOCUMENT(desktop));
652 }
654 void sp_selection_raise_to_top()
655 {
656 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
657 if (desktop == NULL)
658 return;
660 SPDocument *document = SP_DT_DOCUMENT(desktop);
661 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
663 if (selection->isEmpty()) {
664 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise to top."));
665 return;
666 }
668 GSList const *items = (GSList *) selection->itemList();
670 SPGroup const *group = sp_item_list_common_parent_group(items);
671 if (!group) {
672 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
673 return;
674 }
676 GSList *rl = g_slist_copy((GSList *) selection->reprList());
677 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
679 for (GSList *l = rl; l != NULL; l = l->next) {
680 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
681 repr->setPosition(-1);
682 }
684 g_slist_free(rl);
686 sp_document_done(document);
687 }
689 void
690 sp_selection_lower()
691 {
692 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
693 if (desktop == NULL)
694 return;
696 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
698 GSList const *items = (GSList *) selection->itemList();
699 if (!items) {
700 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower."));
701 return;
702 }
704 SPGroup const *group = sp_item_list_common_parent_group(items);
705 if (!group) {
706 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
707 return;
708 }
710 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
712 // find out the common bbox of the selected items
713 NR::Rect selected = enclose_items(items);
715 /* construct direct-ordered list of selected children */
716 GSList *rev = g_slist_copy((GSList *) items);
717 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
718 rev = g_slist_reverse(rev);
720 // for all objects in the selection (starting from top)
721 while (rev) {
722 SPObject *child = SP_OBJECT(rev->data);
723 // for each selected object, find the prev sibling
724 for (SPObject *newref = prev_sibling(child); newref; newref = prev_sibling(newref)) {
725 // if the sibling is an item AND overlaps our selection,
726 if (SP_IS_ITEM(newref) && selected.intersects(sp_item_bbox_desktop(SP_ITEM(newref)))) {
727 // AND if it's not one of our selected objects,
728 if (!g_slist_find((GSList *) items, newref)) {
729 // move the selected object before that sibling
730 SPObject *put_after = prev_sibling(newref);
731 if (put_after)
732 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(put_after));
733 else
734 SP_OBJECT_REPR(child)->setPosition(0);
735 }
736 break;
737 }
738 }
739 rev = g_slist_remove(rev, child);
740 }
742 sp_document_done(SP_DT_DOCUMENT(desktop));
744 }
746 void sp_selection_lower_to_bottom()
747 {
748 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
749 if (desktop == NULL)
750 return;
752 SPDocument *document = SP_DT_DOCUMENT(desktop);
753 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
755 if (selection->isEmpty()) {
756 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower to bottom."));
757 return;
758 }
760 GSList const *items = (GSList *) selection->itemList();
762 SPGroup const *group = sp_item_list_common_parent_group(items);
763 if (!group) {
764 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
765 return;
766 }
768 GSList *rl;
769 rl = g_slist_copy((GSList *) selection->reprList());
770 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
771 rl = g_slist_reverse(rl);
773 for (GSList *l = rl; l != NULL; l = l->next) {
774 gint minpos;
775 SPObject *pp, *pc;
776 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
777 pp = document->getObjectByRepr(sp_repr_parent(repr));
778 minpos = 0;
779 g_assert(SP_IS_GROUP(pp));
780 pc = sp_object_first_child(pp);
781 while (!SP_IS_ITEM(pc)) {
782 minpos += 1;
783 pc = pc->next;
784 }
785 repr->setPosition(minpos);
786 }
788 g_slist_free(rl);
790 sp_document_done(document);
791 }
793 void
794 sp_undo(SPDesktop *desktop, SPDocument *)
795 {
796 if (!sp_document_undo(SP_DT_DOCUMENT(desktop)))
797 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to undo."));
798 }
800 void
801 sp_redo(SPDesktop *desktop, SPDocument *)
802 {
803 if (!sp_document_redo(SP_DT_DOCUMENT(desktop)))
804 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to redo."));
805 }
807 void sp_selection_cut()
808 {
809 sp_selection_copy();
810 sp_selection_delete();
811 }
813 void sp_copy_gradient (GSList **defs_clip, SPGradient *gradient)
814 {
815 SPGradient *ref = gradient;
817 while (ref) {
818 // climb up the refs, copying each one in the chain
819 Inkscape::XML::Node *grad_repr = SP_OBJECT_REPR(ref)->duplicate();
820 *defs_clip = g_slist_prepend (*defs_clip, grad_repr);
822 ref = ref->ref->getObject();
823 }
824 }
826 void sp_copy_pattern (GSList **defs_clip, SPPattern *pattern)
827 {
828 SPPattern *ref = pattern;
830 while (ref) {
831 // climb up the refs, copying each one in the chain
832 Inkscape::XML::Node *pattern_repr = SP_OBJECT_REPR(ref)->duplicate();
833 *defs_clip = g_slist_prepend (*defs_clip, pattern_repr);
835 // items in the pattern may also use gradients and other patterns, so we need to recurse here as well
836 for (SPObject *child = sp_object_first_child(SP_OBJECT(ref)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
837 if (!SP_IS_ITEM (child))
838 continue;
839 sp_copy_stuff_used_by_item (defs_clip, (SPItem *) child, NULL);
840 }
842 ref = ref->ref->getObject();
843 }
844 }
846 void sp_copy_single (GSList **defs_clip, SPObject *thing)
847 {
848 Inkscape::XML::Node *duplicate_repr = SP_OBJECT_REPR(thing)->duplicate();
849 *defs_clip = g_slist_prepend (*defs_clip, duplicate_repr);
850 }
853 void sp_copy_textpath_path (GSList **defs_clip, SPTextPath *tp, const GSList *items)
854 {
855 SPItem *path = sp_textpath_get_path_item (tp);
856 if (!path)
857 return;
858 if (items && g_slist_find ((GSList *) items, path)) // do not copy it to defs if it is already in the list of items copied
859 return;
860 Inkscape::XML::Node *repr = SP_OBJECT_REPR(path)->duplicate();
861 *defs_clip = g_slist_prepend (*defs_clip, repr);
862 }
864 void sp_copy_stuff_used_by_item (GSList **defs_clip, SPItem *item, const GSList *items)
865 {
866 SPStyle *style = SP_OBJECT_STYLE (item);
868 if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
869 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
870 if (SP_IS_LINEARGRADIENT (server) || SP_IS_RADIALGRADIENT (server))
871 sp_copy_gradient (defs_clip, SP_GRADIENT(server));
872 if (SP_IS_PATTERN (server))
873 sp_copy_pattern (defs_clip, SP_PATTERN(server));
874 }
876 if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
877 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
878 if (SP_IS_LINEARGRADIENT (server) || SP_IS_RADIALGRADIENT (server))
879 sp_copy_gradient (defs_clip, SP_GRADIENT(server));
880 if (SP_IS_PATTERN (server))
881 sp_copy_pattern (defs_clip, SP_PATTERN(server));
882 }
884 if (SP_IS_SHAPE (item)) {
885 SPShape *shape = SP_SHAPE (item);
886 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
887 if (shape->marker[i]) {
888 sp_copy_single (defs_clip, SP_OBJECT (shape->marker[i]));
889 }
890 }
891 }
893 if (SP_IS_TEXT_TEXTPATH (item)) {
894 sp_copy_textpath_path (defs_clip, SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))), items);
895 }
897 if (item->clip_ref->getObject()) {
898 sp_copy_single (defs_clip, item->clip_ref->getObject());
899 }
901 if (item->mask_ref->getObject()) {
902 SPObject *mask = item->mask_ref->getObject();
903 sp_copy_single (defs_clip, mask);
904 // recurse into the mask for its gradients etc.
905 for (SPObject *o = SP_OBJECT(mask)->children; o != NULL; o = o->next) {
906 if (SP_IS_ITEM(o))
907 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (o), items);
908 }
909 }
911 // recurse
912 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
913 if (SP_IS_ITEM(o))
914 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (o), items);
915 }
916 }
918 /**
919 * \pre item != NULL
920 */
921 SPCSSAttr *
922 take_style_from_item (SPItem *item)
923 {
924 // write the complete cascaded style, context-free
925 SPCSSAttr *css = sp_css_attr_from_object (SP_OBJECT(item), SP_STYLE_FLAG_ALWAYS);
926 if (css == NULL)
927 return NULL;
929 if ((SP_IS_GROUP(item) && SP_OBJECT(item)->children) ||
930 (SP_IS_TEXT (item) && SP_OBJECT(item)->children && SP_OBJECT(item)->children->next == NULL)) {
931 // if this is a text with exactly one tspan child, merge the style of that tspan as well
932 // If this is a group, merge the style of its topmost (last) child with style
933 for (SPObject *last_element = item->lastChild(); last_element != NULL; last_element = SP_OBJECT_PREV (last_element)) {
934 if (SP_OBJECT_STYLE (last_element) != NULL) {
935 SPCSSAttr *temp = sp_css_attr_from_object (last_element, SP_STYLE_FLAG_IFSET);
936 if (temp) {
937 sp_repr_css_merge (css, temp);
938 sp_repr_css_attr_unref (temp);
939 }
940 break;
941 }
942 }
943 }
944 if (!(SP_IS_TEXT (item) || SP_IS_TSPAN (item) || SP_IS_STRING (item))) {
945 // do not copy text properties from non-text objects, it's confusing
946 css = sp_css_attr_unset_text (css);
947 }
949 // FIXME: also transform gradient/pattern fills, by forking? NO, this must be nondestructive
950 double ex = NR::expansion(sp_item_i2doc_affine(item));
951 if (ex != 1.0) {
952 css = sp_css_attr_scale (css, ex);
953 }
955 return css;
956 }
959 void sp_selection_copy()
960 {
961 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
962 if (desktop == NULL)
963 return;
965 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
967 if (tools_isactive (desktop, TOOLS_DROPPER)) {
968 sp_dropper_context_copy(desktop->event_context);
969 return; // copied color under cursor, nothing else to do
970 }
972 // check if something is selected
973 if (selection->isEmpty()) {
974 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing was copied."));
975 return;
976 }
978 const GSList *items = g_slist_copy ((GSList *) selection->itemList());
980 // 0. Copy text to system clipboard
981 // FIXME: for non-texts, put serialized XML as text to the clipboard;
982 //for this sp_repr_write_stream needs to be rewritten with iostream instead of FILE
983 Glib::ustring text;
984 if (tools_isactive (desktop, TOOLS_TEXT)) {
985 text = sp_text_get_selected_text(desktop->event_context);
986 }
988 if (text.empty()) {
989 guint texts = 0;
990 for (GSList *i = (GSList *) items; i; i = i->next) {
991 SPItem *item = SP_ITEM (i->data);
992 if (SP_IS_TEXT (item) || SP_IS_FLOWTEXT(item)) {
993 if (texts > 0) // if more than one text object is copied, separate them by spaces
994 text += " ";
995 gchar *this_text = sp_te_get_string_multiline (item);
996 if (this_text) {
997 text += this_text;
998 g_free(this_text);
999 }
1000 texts++;
1001 }
1002 }
1003 }
1004 if (!text.empty()) {
1005 Glib::RefPtr<Gtk::Clipboard> refClipboard = Gtk::Clipboard::get();
1006 refClipboard->set_text(text);
1007 }
1009 // clear old defs clipboard
1010 while (defs_clipboard) {
1011 Inkscape::GC::release((Inkscape::XML::Node *) defs_clipboard->data);
1012 defs_clipboard = g_slist_remove (defs_clipboard, defs_clipboard->data);
1013 }
1015 // clear style clipboard
1016 if (style_clipboard) {
1017 sp_repr_css_attr_unref (style_clipboard);
1018 style_clipboard = NULL;
1019 }
1021 //clear main clipboard
1022 while (clipboard) {
1023 Inkscape::GC::release((Inkscape::XML::Node *) clipboard->data);
1024 clipboard = g_slist_remove(clipboard, clipboard->data);
1025 }
1027 sp_selection_copy_impl (items, &clipboard, &defs_clipboard, &style_clipboard);
1029 if (tools_isactive (desktop, TOOLS_TEXT)) { // take style from cursor/text selection, overwriting the style just set by copy_impl
1030 SPStyle *const query = sp_style_new();
1031 if (sp_desktop_query_style_all (desktop, query)) {
1032 SPCSSAttr *css = sp_css_attr_from_style (query, SP_STYLE_FLAG_ALWAYS);
1033 if (css != NULL) {
1034 // clear style clipboard
1035 if (style_clipboard) {
1036 sp_repr_css_attr_unref (style_clipboard);
1037 style_clipboard = NULL;
1038 }
1039 //sp_repr_css_print (css);
1040 style_clipboard = css;
1041 }
1042 }
1043 g_free (query);
1044 }
1046 size_clipboard = selection->bounds();
1048 g_slist_free ((GSList *) items);
1049 }
1051 void sp_selection_paste(bool in_place)
1052 {
1053 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1055 if (desktop == NULL) {
1056 return;
1057 }
1059 SPDocument *document = SP_DT_DOCUMENT(desktop);
1061 if (Inkscape::have_viable_layer(desktop, desktop->messageStack()) == false) {
1062 return;
1063 }
1065 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1067 if (tools_isactive (desktop, TOOLS_TEXT)) {
1068 if (sp_text_paste_inline(desktop->event_context))
1069 return; // pasted from system clipboard into text, nothing else to do
1070 }
1072 // check if something is in the clipboard
1073 if (clipboard == NULL) {
1074 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1075 return;
1076 }
1078 GSList *copied = sp_selection_paste_impl(document, desktop->currentLayer(), &clipboard, &defs_clipboard);
1079 // add pasted objects to selection
1080 selection->setReprList((GSList const *) copied);
1081 g_slist_free (copied);
1083 if (!in_place) {
1084 sp_document_ensure_up_to_date(document);
1086 NR::Point m( desktop->point() - selection->bounds().midpoint() );
1088 /* Snap the offset of the new item(s) to the grid */
1089 /* FIXME: this gridsnap fiddling is a hack. */
1090 Inkscape::GridSnapper &s = desktop->namedview->grid_snapper;
1091 gdouble const curr_gridsnap = s.getDistance();
1092 s.setDistance(NR_HUGE);
1093 m = s.freeSnap(Inkscape::Snapper::SNAP_POINT, m, NULL).getPoint();
1094 s.setDistance(curr_gridsnap);
1095 sp_selection_move_relative(selection, m);
1096 }
1098 sp_document_done(document);
1099 }
1101 void sp_selection_paste_style()
1102 {
1103 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1104 if (desktop == NULL) return;
1106 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1108 // check if something is in the clipboard
1109 if (clipboard == NULL) {
1110 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1111 return;
1112 }
1114 // check if something is selected
1115 if (selection->isEmpty()) {
1116 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste style to."));
1117 return;
1118 }
1120 paste_defs (&defs_clipboard, SP_DT_DOCUMENT(desktop));
1122 sp_desktop_set_style (desktop, style_clipboard);
1124 sp_document_done(SP_DT_DOCUMENT (desktop));
1125 }
1127 void sp_selection_paste_size (bool apply_x, bool apply_y)
1128 {
1129 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1130 if (desktop == NULL) return;
1132 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1134 // check if something is in the clipboard
1135 if (size_clipboard.extent(NR::X) < 1e-6 || size_clipboard.extent(NR::Y) < 1e-6) {
1136 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1137 return;
1138 }
1140 // check if something is selected
1141 if (selection->isEmpty()) {
1142 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste size to."));
1143 return;
1144 }
1146 NR::Rect current = selection->bounds();
1147 if (current.extent(NR::X) < 1e-6 || current.extent(NR::Y) < 1e-6) {
1148 return;
1149 }
1151 double scale_x = size_clipboard.extent(NR::X) / current.extent(NR::X);
1152 double scale_y = size_clipboard.extent(NR::Y) / current.extent(NR::Y);
1154 sp_selection_scale_relative (selection, current.midpoint(), NR::scale(apply_x? scale_x : 1.0, apply_y? scale_y : 1.0));
1156 sp_document_done(SP_DT_DOCUMENT (desktop));
1157 }
1159 void sp_selection_paste_size_separately (bool apply_x, bool apply_y)
1160 {
1161 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1162 if (desktop == NULL) return;
1164 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1166 // check if something is in the clipboard
1167 if (size_clipboard.extent(NR::X) < 1e-6 || size_clipboard.extent(NR::Y) < 1e-6) {
1168 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1169 return;
1170 }
1172 // check if something is selected
1173 if (selection->isEmpty()) {
1174 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste size to."));
1175 return;
1176 }
1178 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1179 SPItem *item = SP_ITEM(l->data);
1181 NR::Rect current = sp_item_bbox_desktop(item);
1182 if (current.extent(NR::X) < 1e-6 || current.extent(NR::Y) < 1e-6) {
1183 continue;
1184 }
1186 double scale_x = size_clipboard.extent(NR::X) / current.extent(NR::X);
1187 double scale_y = size_clipboard.extent(NR::Y) / current.extent(NR::Y);
1189 sp_item_scale_rel (item, NR::scale(apply_x? scale_x : 1.0, apply_y? scale_y : 1.0));
1190 }
1192 sp_document_done(SP_DT_DOCUMENT (desktop));
1193 }
1195 void sp_selection_to_next_layer ()
1196 {
1197 SPDesktop *dt = SP_ACTIVE_DESKTOP;
1199 Inkscape::Selection *selection = SP_DT_SELECTION(dt);
1201 // check if something is selected
1202 if (selection->isEmpty()) {
1203 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer above."));
1204 return;
1205 }
1207 const GSList *items = g_slist_copy ((GSList *) selection->itemList());
1209 SPObject *next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer());
1210 if (next) {
1211 GSList *temp_clip = NULL;
1212 sp_selection_copy_impl (items, &temp_clip, NULL, NULL); // we're in the same doc, so no need to copy defs
1213 sp_selection_delete_impl (items);
1214 GSList *copied = sp_selection_paste_impl (SP_DT_DOCUMENT (dt), next, &temp_clip, NULL);
1215 selection->setReprList((GSList const *) copied);
1216 g_slist_free (copied);
1217 if (temp_clip) g_slist_free (temp_clip);
1218 dt->setCurrentLayer(next);
1219 sp_document_done(SP_DT_DOCUMENT (dt));
1220 } else {
1221 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers above."));
1222 }
1224 g_slist_free ((GSList *) items);
1225 }
1227 void sp_selection_to_prev_layer ()
1228 {
1229 SPDesktop *dt = SP_ACTIVE_DESKTOP;
1231 Inkscape::Selection *selection = SP_DT_SELECTION(dt);
1233 // check if something is selected
1234 if (selection->isEmpty()) {
1235 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer below."));
1236 return;
1237 }
1239 const GSList *items = g_slist_copy ((GSList *) selection->itemList());
1241 SPObject *next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer());
1242 if (next) {
1243 GSList *temp_clip = NULL;
1244 sp_selection_copy_impl (items, &temp_clip, NULL, NULL); // we're in the same doc, so no need to copy defs
1245 sp_selection_delete_impl (items);
1246 GSList *copied = sp_selection_paste_impl (SP_DT_DOCUMENT (dt), next, &temp_clip, NULL);
1247 selection->setReprList((GSList const *) copied);
1248 g_slist_free (copied);
1249 if (temp_clip) g_slist_free (temp_clip);
1250 dt->setCurrentLayer(next);
1251 sp_document_done(SP_DT_DOCUMENT (dt));
1252 } else {
1253 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers below."));
1254 }
1256 g_slist_free ((GSList *) items);
1257 }
1259 /** Apply matrix to the selection. \a set_i2d is normally true, which means objects are in the
1260 original transform, synced with their reprs, and need to jump to the new transform in one go. A
1261 value of set_i2d==false is only used by seltrans when it's dragging objects live (not outlines); in
1262 that case, items are already in the new position, but the repr is in the old, and this function
1263 then simply updates the repr from item->transform.
1264 */
1265 void sp_selection_apply_affine(Inkscape::Selection *selection, NR::Matrix const &affine, bool set_i2d)
1266 {
1267 if (selection->isEmpty())
1268 return;
1270 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1271 SPItem *item = SP_ITEM(l->data);
1273 NR::Point old_center(0,0);
1274 if (set_i2d && item->isCenterSet())
1275 old_center = item->getCenter();
1277 #if 0 /* Re-enable this once persistent guides have a graphical indication.
1278 At the time of writing, this is the only place to re-enable. */
1279 sp_item_update_cns(*item, selection->desktop());
1280 #endif
1282 // we're moving both a clone and its original
1283 bool transform_clone_with_original = (SP_IS_USE(item) && selection->includes( sp_use_get_original (SP_USE(item)) ));
1284 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)))) ));
1285 bool transform_flowtext_with_frame = (SP_IS_FLOWTEXT(item) && selection->includes( SP_FLOWTEXT(item)->get_frame (NULL))); // only the first frame so far
1286 bool transform_offset_with_source = (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) && selection->includes( sp_offset_get_source (SP_OFFSET(item)) );
1288 // "clones are unmoved when original is moved" preference
1289 int compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
1290 bool prefs_unmoved = (compensation == SP_CLONE_COMPENSATION_UNMOVED);
1291 bool prefs_parallel = (compensation == SP_CLONE_COMPENSATION_PARALLEL);
1293 // If this is a clone and it's selected along with its original, do not move it; it will feel the
1294 // transform of its original and respond to it itself. Without this, a clone is doubly
1295 // transformed, very unintuitive.
1296 // Same for textpath if we are also doing ANY transform to its path: do not touch textpath,
1297 // letters cannot be squeezed or rotated anyway, they only refill the changed path.
1298 // Same for linked offset if we are also moving its source: do not move it.
1299 if (transform_textpath_with_path || transform_offset_with_source) {
1300 // restore item->transform field from the repr, in case it was changed by seltrans
1301 sp_object_read_attr (SP_OBJECT (item), "transform");
1303 } else if (transform_flowtext_with_frame) {
1304 // apply the inverse of the region's transform to the <use> so that the flow remains
1305 // the same (even though the output itself gets transformed)
1306 for (SPObject *region = item->firstChild() ; region ; region = SP_OBJECT_NEXT(region)) {
1307 if (!SP_IS_FLOWREGION(region) && !SP_IS_FLOWREGIONEXCLUDE(region))
1308 continue;
1309 for (SPObject *use = region->firstChild() ; use ; use = SP_OBJECT_NEXT(use)) {
1310 if (!SP_IS_USE(use)) continue;
1311 sp_item_write_transform(SP_USE(use), SP_OBJECT_REPR(use), item->transform.inverse(), NULL);
1312 }
1313 }
1314 } else if (transform_clone_with_original) {
1315 // We are transforming a clone along with its original. The below matrix juggling is
1316 // necessary to ensure that they transform as a whole, i.e. the clone's induced
1317 // transform and its move compensation are both cancelled out.
1319 // restore item->transform field from the repr, in case it was changed by seltrans
1320 sp_object_read_attr (SP_OBJECT (item), "transform");
1322 // calculate the matrix we need to apply to the clone to cancel its induced transform from its original
1323 NR::Matrix t = matrix_to_desktop (matrix_from_desktop (affine, item), item);
1324 NR::Matrix t_inv = matrix_to_desktop (matrix_from_desktop (affine.inverse(), item), item);
1325 NR::Matrix result = t_inv * item->transform * t;
1327 if ((prefs_parallel || prefs_unmoved) && affine.is_translation()) {
1328 // we need to cancel out the move compensation, too
1330 // find out the clone move, same as in sp_use_move_compensate
1331 NR::Matrix parent = sp_use_get_parent_transform (SP_USE(item));
1332 NR::Matrix clone_move = parent.inverse() * t * parent;
1334 if (prefs_parallel) {
1335 NR::Matrix move = result * clone_move * t_inv;
1336 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &move);
1338 } else if (prefs_unmoved) {
1339 if (SP_IS_USE(sp_use_get_original(SP_USE(item))))
1340 clone_move = NR::identity();
1341 NR::Matrix move = result * clone_move;
1342 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &move);
1343 }
1345 } else {
1346 // just apply the result
1347 sp_item_write_transform(item, SP_OBJECT_REPR(item), result, &result);
1348 }
1350 } else {
1351 if (set_i2d) {
1352 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * affine);
1353 }
1354 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, NULL);
1355 }
1357 // if we're moving the actual object, not just updating the repr, we can transform the
1358 // center by the same matrix (only necessary for non-translations)
1359 if (set_i2d && item->isCenterSet() && !affine.is_translation()) {
1360 item->setCenter(old_center * affine);
1361 SP_OBJECT(item)->updateRepr();
1362 }
1363 }
1364 }
1366 void sp_selection_remove_transform()
1367 {
1368 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1369 if (desktop == NULL)
1370 return;
1372 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1374 GSList const *l = (GSList *) selection->reprList();
1375 while (l != NULL) {
1376 sp_repr_set_attr((Inkscape::XML::Node*)l->data, "transform", NULL);
1377 l = l->next;
1378 }
1380 sp_document_done(SP_DT_DOCUMENT(desktop));
1381 }
1383 void
1384 sp_selection_scale_absolute(Inkscape::Selection *selection,
1385 double const x0, double const x1,
1386 double const y0, double const y1)
1387 {
1388 if (selection->isEmpty())
1389 return;
1391 NR::Rect const bbox(selection->bounds());
1392 if (bbox.isEmpty()) {
1393 return;
1394 }
1396 NR::translate const p2o(-bbox.min());
1398 NR::scale const newSize(x1 - x0,
1399 y1 - y0);
1400 NR::scale const scale( newSize / NR::scale(bbox.dimensions()) );
1401 NR::translate const o2n(x0, y0);
1402 NR::Matrix const final( p2o * scale * o2n );
1404 sp_selection_apply_affine(selection, final);
1405 }
1408 void sp_selection_scale_relative(Inkscape::Selection *selection, NR::Point const &align, NR::scale const &scale)
1409 {
1410 if (selection->isEmpty())
1411 return;
1413 NR::Rect const bbox(selection->bounds());
1415 if (bbox.isEmpty()) {
1416 return;
1417 }
1419 // FIXME: ARBITRARY LIMIT: don't try to scale above 1 Mpx, it won't display properly and will crash sooner or later anyway
1420 if ( bbox.extent(NR::X) * scale[NR::X] > 1e6 ||
1421 bbox.extent(NR::Y) * scale[NR::Y] > 1e6 )
1422 {
1423 return;
1424 }
1426 NR::translate const n2d(-align);
1427 NR::translate const d2n(align);
1428 NR::Matrix const final( n2d * scale * d2n );
1429 sp_selection_apply_affine(selection, final);
1430 }
1432 void
1433 sp_selection_rotate_relative(Inkscape::Selection *selection, NR::Point const ¢er, gdouble const angle_degrees)
1434 {
1435 NR::translate const d2n(center);
1436 NR::translate const n2d(-center);
1437 NR::rotate const rotate(rotate_degrees(angle_degrees));
1438 NR::Matrix const final( NR::Matrix(n2d) * rotate * d2n );
1439 sp_selection_apply_affine(selection, final);
1440 }
1442 void
1443 sp_selection_skew_relative(Inkscape::Selection *selection, NR::Point const &align, double dx, double dy)
1444 {
1445 NR::translate const d2n(align);
1446 NR::translate const n2d(-align);
1447 NR::Matrix const skew(1, dy,
1448 dx, 1,
1449 0, 0);
1450 NR::Matrix const final( n2d * skew * d2n );
1451 sp_selection_apply_affine(selection, final);
1452 }
1454 void sp_selection_move_relative(Inkscape::Selection *selection, NR::Point const &move)
1455 {
1456 sp_selection_apply_affine(selection, NR::Matrix(NR::translate(move)));
1457 }
1459 void sp_selection_move_relative(Inkscape::Selection *selection, double dx, double dy)
1460 {
1461 sp_selection_apply_affine(selection, NR::Matrix(NR::translate(dx, dy)));
1462 }
1465 /**
1466 * \brief sp_selection_rotate_90
1467 *
1468 * This function rotates selected objects 90 degrees clockwise.
1469 *
1470 */
1472 void sp_selection_rotate_90_cw()
1473 {
1474 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1476 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1478 if (selection->isEmpty())
1479 return;
1481 GSList const *l = selection->itemList();
1482 NR::rotate const rot_neg_90(NR::Point(0, -1));
1483 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1484 SPItem *item = SP_ITEM(l2->data);
1485 sp_item_rotate_rel(item, rot_neg_90);
1486 }
1488 sp_document_done(SP_DT_DOCUMENT(desktop));
1489 }
1492 /**
1493 * \brief sp_selection_rotate_90_ccw
1494 *
1495 * This function rotates selected objects 90 degrees counter-clockwise.
1496 *
1497 */
1499 void sp_selection_rotate_90_ccw()
1500 {
1501 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1503 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1505 if (selection->isEmpty())
1506 return;
1508 GSList const *l = selection->itemList();
1509 NR::rotate const rot_neg_90(NR::Point(0, 1));
1510 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1511 SPItem *item = SP_ITEM(l2->data);
1512 sp_item_rotate_rel(item, rot_neg_90);
1513 }
1515 sp_document_done(SP_DT_DOCUMENT(desktop));
1516 }
1518 void
1519 sp_selection_rotate(Inkscape::Selection *selection, gdouble const angle_degrees)
1520 {
1521 if (selection->isEmpty())
1522 return;
1524 NR::Point center = selection->center();
1526 sp_selection_rotate_relative(selection, center, angle_degrees);
1528 sp_document_maybe_done(SP_DT_DOCUMENT(selection->desktop()),
1529 ( ( angle_degrees > 0 )
1530 ? "selector:rotate:ccw"
1531 : "selector:rotate:cw" ));
1532 }
1534 /**
1535 \param angle the angle in "angular pixels", i.e. how many visible pixels must move the outermost point of the rotated object
1536 */
1537 void
1538 sp_selection_rotate_screen(Inkscape::Selection *selection, gdouble angle)
1539 {
1540 if (selection->isEmpty())
1541 return;
1543 NR::Rect const bbox(selection->bounds());
1545 NR::Point center = selection->center();
1547 gdouble const zoom = selection->desktop()->current_zoom();
1548 gdouble const zmove = angle / zoom;
1549 gdouble const r = NR::L2(bbox.max() - center);
1551 gdouble const zangle = 180 * atan2(zmove, r) / M_PI;
1553 sp_selection_rotate_relative(selection, center, zangle);
1555 sp_document_maybe_done(SP_DT_DOCUMENT(selection->desktop()),
1556 ( (angle > 0)
1557 ? "selector:rotate:ccw"
1558 : "selector:rotate:cw" ));
1559 }
1561 void
1562 sp_selection_scale(Inkscape::Selection *selection, gdouble grow)
1563 {
1564 if (selection->isEmpty())
1565 return;
1567 NR::Rect const bbox(selection->bounds());
1568 NR::Point const center(bbox.midpoint());
1569 double const max_len = bbox.maxExtent();
1571 // you can't scale "do nizhe pola" (below zero)
1572 if ( max_len + grow <= 1e-3 ) {
1573 return;
1574 }
1576 double const times = 1.0 + grow / max_len;
1577 sp_selection_scale_relative(selection, center, NR::scale(times, times));
1579 sp_document_maybe_done(SP_DT_DOCUMENT(selection->desktop()),
1580 ( (grow > 0)
1581 ? "selector:scale:larger"
1582 : "selector:scale:smaller" ));
1583 }
1585 void
1586 sp_selection_scale_screen(Inkscape::Selection *selection, gdouble grow_pixels)
1587 {
1588 sp_selection_scale(selection,
1589 grow_pixels / selection->desktop()->current_zoom());
1590 }
1592 void
1593 sp_selection_scale_times(Inkscape::Selection *selection, gdouble times)
1594 {
1595 if (selection->isEmpty())
1596 return;
1598 NR::Point const center(selection->bounds().midpoint());
1599 sp_selection_scale_relative(selection, center, NR::scale(times, times));
1600 sp_document_done(SP_DT_DOCUMENT(selection->desktop()));
1601 }
1603 void
1604 sp_selection_move(gdouble dx, gdouble dy)
1605 {
1606 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1607 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1608 if (selection->isEmpty()) {
1609 return;
1610 }
1612 sp_selection_move_relative(selection, dx, dy);
1614 if (dx == 0) {
1615 sp_document_maybe_done(SP_DT_DOCUMENT(desktop), "selector:move:vertical");
1616 } else if (dy == 0) {
1617 sp_document_maybe_done(SP_DT_DOCUMENT(desktop), "selector:move:horizontal");
1618 } else {
1619 sp_document_done(SP_DT_DOCUMENT(desktop));
1620 }
1621 }
1623 void
1624 sp_selection_move_screen(gdouble dx, gdouble dy)
1625 {
1626 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1628 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1629 if (selection->isEmpty()) {
1630 return;
1631 }
1633 // same as sp_selection_move but divide deltas by zoom factor
1634 gdouble const zoom = desktop->current_zoom();
1635 gdouble const zdx = dx / zoom;
1636 gdouble const zdy = dy / zoom;
1637 sp_selection_move_relative(selection, zdx, zdy);
1639 if (dx == 0) {
1640 sp_document_maybe_done(SP_DT_DOCUMENT(desktop), "selector:move:vertical");
1641 } else if (dy == 0) {
1642 sp_document_maybe_done(SP_DT_DOCUMENT(desktop), "selector:move:horizontal");
1643 } else {
1644 sp_document_done(SP_DT_DOCUMENT(desktop));
1645 }
1646 }
1648 namespace {
1650 template <typename D>
1651 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
1652 bool only_in_viewport, bool inlayer, bool onlyvisible, bool onlysensitive);
1654 template <typename D>
1655 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items, SPObject *root,
1656 bool only_in_viewport, bool inlayer, bool onlyvisible, bool onlysensitive);
1658 struct Forward {
1659 typedef SPObject *Iterator;
1661 static Iterator children(SPObject *o) { return sp_object_first_child(o); }
1662 static Iterator siblings_after(SPObject *o) { return SP_OBJECT_NEXT(o); }
1663 static void dispose(Iterator i) {}
1665 static SPObject *object(Iterator i) { return i; }
1666 static Iterator next(Iterator i) { return SP_OBJECT_NEXT(i); }
1667 };
1669 struct Reverse {
1670 typedef GSList *Iterator;
1672 static Iterator children(SPObject *o) {
1673 return make_list(o->firstChild(), NULL);
1674 }
1675 static Iterator siblings_after(SPObject *o) {
1676 return make_list(SP_OBJECT_PARENT(o)->firstChild(), o);
1677 }
1678 static void dispose(Iterator i) {
1679 g_slist_free(i);
1680 }
1682 static SPObject *object(Iterator i) {
1683 return reinterpret_cast<SPObject *>(i->data);
1684 }
1685 static Iterator next(Iterator i) { return i->next; }
1687 private:
1688 static GSList *make_list(SPObject *object, SPObject *limit) {
1689 GSList *list=NULL;
1690 while ( object != limit ) {
1691 list = g_slist_prepend(list, object);
1692 object = SP_OBJECT_NEXT(object);
1693 }
1694 return list;
1695 }
1696 };
1698 }
1700 void
1701 sp_selection_item_next(void)
1702 {
1703 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1704 g_return_if_fail(desktop != NULL);
1705 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1707 bool inlayer = prefs_get_int_attribute ("options.kbselection", "inlayer", 1);
1708 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
1709 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
1711 SPObject *root;
1712 if (inlayer) {
1713 root = desktop->currentLayer();
1714 } else {
1715 root = desktop->currentRoot();
1716 }
1718 SPItem *item=next_item_from_list<Forward>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1720 if (item) {
1721 selection->set(item);
1722 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1723 scroll_to_show_item(desktop, item);
1724 }
1725 }
1726 }
1728 void
1729 sp_selection_item_prev(void)
1730 {
1731 SPDocument *document = SP_ACTIVE_DOCUMENT;
1732 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1733 g_return_if_fail(document != NULL);
1734 g_return_if_fail(desktop != NULL);
1735 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1737 bool inlayer = prefs_get_int_attribute ("options.kbselection", "inlayer", 1);
1738 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
1739 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
1741 SPObject *root;
1742 if (inlayer) {
1743 root = desktop->currentLayer();
1744 } else {
1745 root = desktop->currentRoot();
1746 }
1748 SPItem *item=next_item_from_list<Reverse>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1750 if (item) {
1751 selection->set(item);
1752 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1753 scroll_to_show_item(desktop, item);
1754 }
1755 }
1756 }
1758 namespace {
1760 template <typename D>
1761 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items,
1762 SPObject *root, bool only_in_viewport, bool inlayer, bool onlyvisible, bool onlysensitive)
1763 {
1764 SPObject *current=root;
1765 while (items) {
1766 SPItem *item=SP_ITEM(items->data);
1767 if ( root->isAncestorOf(item) &&
1768 ( !only_in_viewport || desktop->isWithinViewport(item) ) )
1769 {
1770 current = item;
1771 break;
1772 }
1773 items = items->next;
1774 }
1776 GSList *path=NULL;
1777 while ( current != root ) {
1778 path = g_slist_prepend(path, current);
1779 current = SP_OBJECT_PARENT(current);
1780 }
1782 SPItem *next;
1783 // first, try from the current object
1784 next = next_item<D>(desktop, path, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1785 g_slist_free(path);
1787 if (!next) { // if we ran out of objects, start over at the root
1788 next = next_item<D>(desktop, NULL, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1789 }
1791 return next;
1792 }
1794 template <typename D>
1795 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
1796 bool only_in_viewport, bool inlayer, bool onlyvisible, bool onlysensitive)
1797 {
1798 typename D::Iterator children;
1799 typename D::Iterator iter;
1801 SPItem *found=NULL;
1803 if (path) {
1804 SPObject *object=reinterpret_cast<SPObject *>(path->data);
1805 g_assert(SP_OBJECT_PARENT(object) == root);
1806 if (desktop->isLayer(object)) {
1807 found = next_item<D>(desktop, path->next, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1808 }
1809 iter = children = D::siblings_after(object);
1810 } else {
1811 iter = children = D::children(root);
1812 }
1814 while ( iter && !found ) {
1815 SPObject *object=D::object(iter);
1816 if (desktop->isLayer(object)) {
1817 if (!inlayer) { // recurse into sublayers
1818 found = next_item<D>(desktop, NULL, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1819 }
1820 } else if ( SP_IS_ITEM(object) &&
1821 ( !only_in_viewport || desktop->isWithinViewport(SP_ITEM(object)) ) &&
1822 ( !onlyvisible || !desktop->itemIsHidden(SP_ITEM(object))) &&
1823 ( !onlysensitive || !SP_ITEM(object)->isLocked()) &&
1824 !desktop->isLayer(SP_ITEM(object)) )
1825 {
1826 found = SP_ITEM(object);
1827 }
1828 iter = D::next(iter);
1829 }
1831 D::dispose(children);
1833 return found;
1834 }
1836 }
1838 /**
1839 * If \a item is not entirely visible then adjust visible area to centre on the centre on of
1840 * \a item.
1841 */
1842 void scroll_to_show_item(SPDesktop *desktop, SPItem *item)
1843 {
1844 NR::Rect dbox = desktop->get_display_area();
1845 NR::Rect sbox = sp_item_bbox_desktop(item);
1847 if (dbox.contains(sbox) == false) {
1848 NR::Point const s_dt = sbox.midpoint();
1849 NR::Point const s_w = desktop->d2w(s_dt);
1850 NR::Point const d_dt = dbox.midpoint();
1851 NR::Point const d_w = desktop->d2w(d_dt);
1852 NR::Point const moved_w( d_w - s_w );
1853 gint const dx = (gint) moved_w[X];
1854 gint const dy = (gint) moved_w[Y];
1855 desktop->scroll_world(dx, dy);
1856 }
1857 }
1860 void
1861 sp_selection_clone()
1862 {
1863 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1864 if (desktop == NULL)
1865 return;
1867 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1869 // check if something is selected
1870 if (selection->isEmpty()) {
1871 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object</b> to clone."));
1872 return;
1873 }
1875 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
1877 selection->clear();
1879 // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
1880 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
1882 GSList *newsel = NULL;
1884 while (reprs) {
1885 Inkscape::XML::Node *sel_repr = (Inkscape::XML::Node *) reprs->data;
1886 Inkscape::XML::Node *parent = sp_repr_parent(sel_repr);
1888 Inkscape::XML::Node *clone = sp_repr_new("svg:use");
1889 sp_repr_set_attr(clone, "x", "0");
1890 sp_repr_set_attr(clone, "y", "0");
1891 sp_repr_set_attr(clone, "xlink:href", g_strdup_printf("#%s", sel_repr->attribute("id")));
1893 // add the new clone to the top of the original's parent
1894 parent->appendChild(clone);
1896 newsel = g_slist_prepend(newsel, clone);
1897 reprs = g_slist_remove(reprs, sel_repr);
1898 Inkscape::GC::release(clone);
1899 }
1901 sp_document_done(SP_DT_DOCUMENT(desktop));
1903 selection->setReprList(newsel);
1905 g_slist_free(newsel);
1906 }
1908 void
1909 sp_selection_unlink()
1910 {
1911 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1912 if (!desktop)
1913 return;
1915 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1917 if (selection->isEmpty()) {
1918 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>clone</b> to unlink."));
1919 return;
1920 }
1922 // Get a copy of current selection.
1923 GSList *new_select = NULL;
1924 bool unlinked = false;
1925 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
1926 items != NULL;
1927 items = items->next)
1928 {
1929 SPItem *use = (SPItem *) items->data;
1931 if (!SP_IS_USE(use)) {
1932 // keep the non-yse item in the new selection
1933 new_select = g_slist_prepend(new_select, use);
1934 continue;
1935 }
1937 SPItem *unlink = sp_use_unlink(SP_USE(use));
1938 unlinked = true;
1939 // Add ungrouped items to the new selection.
1940 new_select = g_slist_prepend(new_select, unlink);
1941 }
1943 if (new_select) { // set new selection
1944 selection->clear();
1945 selection->setList(new_select);
1946 g_slist_free(new_select);
1947 }
1948 if (!unlinked) {
1949 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No clones to unlink</b> in the selection."));
1950 }
1952 sp_document_done(SP_DT_DOCUMENT(desktop));
1953 }
1955 void
1956 sp_select_clone_original()
1957 {
1958 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1959 if (desktop == NULL)
1960 return;
1962 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1964 SPItem *item = selection->singleItem();
1966 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.");
1968 // Check if other than two objects are selected
1969 if (g_slist_length((GSList *) selection->itemList()) != 1 || !item) {
1970 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
1971 return;
1972 }
1974 SPItem *original = NULL;
1975 if (SP_IS_USE(item)) {
1976 original = sp_use_get_original (SP_USE(item));
1977 } else if (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) {
1978 original = sp_offset_get_source (SP_OFFSET(item));
1979 } else if (SP_IS_TEXT_TEXTPATH(item)) {
1980 original = sp_textpath_get_path_item (SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))));
1981 } else if (SP_IS_FLOWTEXT(item)) {
1982 original = SP_FLOWTEXT(item)->get_frame (NULL); // first frame only
1983 } else { // it's an object that we don't know what to do with
1984 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
1985 return;
1986 }
1988 if (!original) {
1989 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>Cannot find</b> the object to select (orphaned clone, offset, textpath, flowed text?)"));
1990 return;
1991 }
1993 for (SPObject *o = original; o && !SP_IS_ROOT(o); o = SP_OBJECT_PARENT (o)) {
1994 if (SP_IS_DEFS (o)) {
1995 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("The object you're trying to select is <b>not visible</b> (it is in <defs>)"));
1996 return;
1997 }
1998 }
2000 if (original) {
2001 selection->clear();
2002 selection->set(original);
2003 if (SP_CYCLING == SP_CYCLE_FOCUS) {
2004 scroll_to_show_item(desktop, original);
2005 }
2006 }
2007 }
2009 void
2010 sp_selection_tile(bool apply)
2011 {
2012 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2013 if (desktop == NULL)
2014 return;
2016 SPDocument *document = SP_DT_DOCUMENT(desktop);
2018 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
2020 // check if something is selected
2021 if (selection->isEmpty()) {
2022 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to pattern."));
2023 return;
2024 }
2026 sp_document_ensure_up_to_date(document);
2027 NR::Rect r = selection->bounds();
2028 if (r.isEmpty()) {
2029 return;
2030 }
2032 // calculate the transform to be applied to objects to move them to 0,0
2033 NR::Point move_p = NR::Point(0, sp_document_height(document)) - (r.min() + NR::Point (0, r.extent(NR::Y)));
2034 move_p[NR::Y] = -move_p[NR::Y];
2035 NR::Matrix move = NR::Matrix (NR::translate (move_p));
2037 GSList *items = g_slist_copy((GSList *) selection->itemList());
2039 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2041 // bottommost object, after sorting
2042 SPObject *parent = SP_OBJECT_PARENT (items->data);
2044 // remember the position of the first item
2045 gint pos = SP_OBJECT_REPR (items->data)->position();
2047 // create a list of duplicates
2048 GSList *repr_copies = NULL;
2049 for (GSList *i = items; i != NULL; i = i->next) {
2050 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate();
2051 repr_copies = g_slist_prepend (repr_copies, dup);
2052 }
2054 NR::Rect bounds(desktop->dt2doc(r.min()), desktop->dt2doc(r.max()));
2056 if (apply) {
2057 // delete objects so that their clones don't get alerted; this object will be restored shortly
2058 for (GSList *i = items; i != NULL; i = i->next) {
2059 SPObject *item = SP_OBJECT (i->data);
2060 item->deleteObject (false);
2061 }
2062 }
2064 // Hack: Temporarily set clone compensation to unmoved, so that we can move clone-originals
2065 // without disturbing clones.
2066 // See ActorAlign::on_button_click() in src/ui/dialog/align-and-distribute.cpp
2067 int saved_compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2068 prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2070 const gchar *pat_id = pattern_tile (repr_copies, bounds, document,
2071 NR::Matrix(NR::translate(desktop->dt2doc(NR::Point(r.min()[NR::X], r.max()[NR::Y])))), move);
2073 // restore compensation setting
2074 prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
2076 if (apply) {
2077 Inkscape::XML::Node *rect = sp_repr_new ("svg:rect");
2078 rect->setAttribute("style", g_strdup_printf("stroke:none;fill:url(#%s)", pat_id));
2079 sp_repr_set_svg_double(rect, "width", bounds.extent(NR::X));
2080 sp_repr_set_svg_double(rect, "height", bounds.extent(NR::Y));
2081 sp_repr_set_svg_double(rect, "x", bounds.min()[NR::X]);
2082 sp_repr_set_svg_double(rect, "y", bounds.min()[NR::Y]);
2084 // restore parent and position
2085 SP_OBJECT_REPR (parent)->appendChild(rect);
2086 rect->setPosition(pos > 0 ? pos : 0);
2087 SPItem *rectangle = (SPItem *) SP_DT_DOCUMENT (desktop)->getObjectByRepr(rect);
2089 Inkscape::GC::release(rect);
2091 selection->clear();
2092 selection->set(rectangle);
2093 }
2095 g_slist_free (items);
2097 sp_document_done (document);
2098 }
2100 void
2101 sp_selection_untile()
2102 {
2103 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2104 if (desktop == NULL)
2105 return;
2107 SPDocument *document = SP_DT_DOCUMENT(desktop);
2109 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
2111 // check if something is selected
2112 if (selection->isEmpty()) {
2113 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object with pattern fill</b> to extract objects from."));
2114 return;
2115 }
2117 GSList *new_select = NULL;
2119 bool did = false;
2121 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2122 items != NULL;
2123 items = items->next) {
2125 SPItem *item = (SPItem *) items->data;
2127 SPStyle *style = SP_OBJECT_STYLE (item);
2129 if (!style || style->fill.type != SP_PAINT_TYPE_PAINTSERVER)
2130 continue;
2132 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
2134 if (!SP_IS_PATTERN(server))
2135 continue;
2137 did = true;
2139 SPPattern *pattern = pattern_getroot (SP_PATTERN (server));
2141 NR::Matrix pat_transform = pattern_patternTransform (SP_PATTERN (server));
2142 pat_transform *= item->transform;
2144 for (SPObject *child = sp_object_first_child(SP_OBJECT(pattern)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2145 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate();
2146 SPItem *i = SP_ITEM (desktop->currentLayer()->appendChildRepr(copy));
2148 // FIXME: relink clones to the new canvas objects
2149 // use SPObject::setid when mental finishes it to steal ids of
2151 // this is needed to make sure the new item has curve (simply requestDisplayUpdate does not work)
2152 sp_document_ensure_up_to_date (document);
2154 NR::Matrix transform( i->transform * pat_transform );
2155 sp_item_write_transform(i, SP_OBJECT_REPR(i), transform);
2157 new_select = g_slist_prepend(new_select, i);
2158 }
2160 SPCSSAttr *css = sp_repr_css_attr_new ();
2161 sp_repr_css_set_property (css, "fill", "none");
2162 sp_repr_css_change (SP_OBJECT_REPR (item), css, "style");
2163 }
2165 if (!did) {
2166 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No pattern fills</b> in the selection."));
2167 } else {
2168 sp_document_done(SP_DT_DOCUMENT(desktop));
2169 selection->setList(new_select);
2170 }
2171 }
2173 void
2174 sp_selection_create_bitmap_copy ()
2175 {
2176 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2177 if (desktop == NULL)
2178 return;
2180 SPDocument *document = SP_DT_DOCUMENT(desktop);
2182 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
2184 // check if something is selected
2185 if (selection->isEmpty()) {
2186 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to make a bitmap copy."));
2187 return;
2188 }
2190 // Get the bounding box of the selection
2191 NRRect bbox;
2192 sp_document_ensure_up_to_date (document);
2193 selection->bounds(&bbox);
2194 if (NR_RECT_DFLS_TEST_EMPTY(&bbox)) {
2195 return; // exceptional situation, so not bother with a translatable error message, just quit quietly
2196 }
2198 // List of the items to show; all others will be hidden
2199 GSList *items = g_slist_copy ((GSList *) selection->itemList());
2201 // Sort items so that the topmost comes last
2202 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
2204 // Generate a random value from the current time (you may create bitmap from the same object(s)
2205 // multiple times, and this is done so that they don't clash)
2206 GTimeVal cu;
2207 g_get_current_time (&cu);
2208 guint current = (int) (cu.tv_sec * 1000000 + cu.tv_usec) % 1024;
2210 // Create the filename
2211 gchar *filename = g_strdup_printf ("%s-%s-%u.png", document->name, SP_OBJECT_REPR(items->data)->attribute("id"), current);
2212 // Imagemagick is known not to handle spaces in filenames, so we replace anything but letters,
2213 // digits, and a few other chars, with "_"
2214 filename = g_strcanon (filename, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.=+~$#@^&!?", '_');
2215 // Build the complete path by adding document->base if set
2216 gchar *filepath = g_build_filename (document->base?document->base:"", filename, NULL);
2218 //g_print ("%s\n", filepath);
2220 // Remember parent and z-order of the topmost one
2221 gint pos = SP_OBJECT_REPR(g_slist_last(items)->data)->position();
2222 SPObject *parent_object = SP_OBJECT_PARENT(g_slist_last(items)->data);
2223 Inkscape::XML::Node *parent = SP_OBJECT_REPR(parent_object);
2225 // Calculate resolution
2226 double res;
2227 int const prefs_res = prefs_get_int_attribute ("options.createbitmap", "resolution", 0);
2228 if (0 < prefs_res) {
2229 // If it's given explicitly in prefs, take it
2230 res = prefs_res;
2231 } else {
2232 // Otherwise look up minimum bitmap size (default 250 pixels) and calculate resolution from it
2233 int const prefs_min = prefs_get_int_attribute ("options.createbitmap", "minsize", 250);
2234 res = prefs_min / MIN ((bbox.x1 - bbox.x0), (bbox.y1 - bbox.y0));
2235 }
2237 // The width and height of the bitmap in pixels
2238 unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * res);
2239 unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * res);
2241 // Find out if we have to run a filter
2242 const gchar *run = NULL;
2243 const gchar *filter = prefs_get_string_attribute ("options.createbitmap", "filter");
2244 if (filter) {
2245 // filter command is given;
2246 // see if we have a parameter to pass to it
2247 const gchar *param1 = prefs_get_string_attribute ("options.createbitmap", "filter_param1");
2248 if (param1) {
2249 if (param1[strlen(param1) - 1] == '%') {
2250 // if the param string ends with %, interpret it as a percentage of the image's max dimension
2251 gchar p1[256];
2252 g_ascii_dtostr (p1, 256, ceil (g_ascii_strtod (param1, NULL) * MAX(width, height) / 100));
2253 // the first param is always the image filename, the second is param1
2254 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, p1);
2255 } else {
2256 // otherwise pass the param1 unchanged
2257 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, param1);
2258 }
2259 } else {
2260 // run without extra parameter
2261 run = g_strdup_printf ("%s \"%s\"", filter, filepath);
2262 }
2263 }
2265 // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
2266 NR::Matrix eek = sp_item_i2d_affine (SP_ITEM(parent_object));
2267 NR::Matrix t = NR::scale (1/res, -1/res) * NR::translate (bbox.x0, bbox.y1) * eek.inverse();
2269 // Do the export
2270 sp_export_png_file(document, filepath,
2271 bbox.x0, bbox.y0, bbox.x1, bbox.y1,
2272 width, height,
2273 (guint32) 0xffffff00,
2274 NULL, NULL,
2275 true, /*bool force_overwrite,*/
2276 items);
2278 g_slist_free (items);
2280 // Run filter, if any
2281 if (run) {
2282 g_print ("Running external filter: %s\n", run);
2283 system (run);
2284 }
2286 // Import the image back
2287 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filepath, NULL);
2288 if (pb) {
2289 // Create the repr for the image
2290 Inkscape::XML::Node * repr = sp_repr_new ("svg:image");
2291 repr->setAttribute("xlink:href", filename);
2292 repr->setAttribute("sodipodi:absref", filepath);
2293 sp_repr_set_svg_double(repr, "width", gdk_pixbuf_get_width(pb));
2294 sp_repr_set_svg_double(repr, "height", gdk_pixbuf_get_height(pb));
2296 // Write transform
2297 gchar c[256];
2298 if (sp_svg_transform_write(c, 256, t)) {
2299 repr->setAttribute("transform", c);
2300 }
2302 // add the new repr to the parent
2303 parent->appendChild(repr);
2305 // move to the saved position
2306 repr->setPosition(pos > 0 ? pos + 1 : 1);
2308 // Set selection to the new image
2309 selection->clear();
2310 selection->add(repr);
2312 // Clean up
2313 Inkscape::GC::release(repr);
2314 gdk_pixbuf_unref (pb);
2316 // Complete undoable transaction
2317 sp_document_done (document);
2318 }
2320 g_free (filename);
2321 g_free (filepath);
2322 }
2324 /**
2325 * \brief sp_selection_set_mask
2326 *
2327 * This function creates a mask or clipPath from selection
2328 * Two different modes:
2329 * if applyToLayer, all selection is moved to DEFS as mask/clippath
2330 * and is applied to current layer
2331 * otherwise, topmost object is used as mask for other objects
2332 * If \a apply_clip_path parameter is true, clipPath is created, otherwise mask
2333 *
2334 */
2335 void
2336 sp_selection_set_mask(bool apply_clip_path, bool apply_to_layer)
2337 {
2338 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2339 if (desktop == NULL)
2340 return;
2342 SPDocument *document = SP_DT_DOCUMENT(desktop);
2344 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
2346 // check if something is selected
2347 bool is_empty = selection->isEmpty();
2348 if ( apply_to_layer && is_empty) {
2349 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to create mask from."));
2350 return;
2351 } else if (!apply_to_layer && ( is_empty || NULL == selection->itemList()->next )) {
2352 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select mask object and <b>object(s)</b> to apply mask to."));
2353 return;
2354 }
2356 sp_document_ensure_up_to_date(document);
2358 GSList *items = g_slist_copy((GSList *) selection->itemList());
2360 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2362 // create a list of duplicates
2363 GSList *mask_items = NULL;
2364 GSList *apply_to_items = NULL;
2365 bool topmost = prefs_get_int_attribute ("options.maskobject", "topmost", 1);
2366 bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
2368 if (apply_to_layer) {
2369 // all selected items are used for mask, which is applied to a layer
2370 apply_to_items = g_slist_prepend (apply_to_items, desktop->currentLayer());
2372 for (GSList *i = items; i != NULL; i = i->next) {
2373 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate();
2374 mask_items = g_slist_prepend (mask_items, dup);
2376 if (remove_original) {
2377 SPObject *item = SP_OBJECT (i->data);
2378 item->deleteObject (false);
2379 }
2380 }
2381 } else if (!topmost) {
2382 // topmost item is used as a mask, which is applied to other items in a selection
2383 GSList *i = items;
2384 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate();
2385 mask_items = g_slist_prepend (mask_items, dup);
2387 if (remove_original) {
2388 SPObject *item = SP_OBJECT (i->data);
2389 item->deleteObject (false);
2390 }
2392 for (i = i->next; i != NULL; i = i->next) {
2393 apply_to_items = g_slist_prepend (apply_to_items, i->data);
2394 }
2395 } else {
2396 GSList *i = NULL;
2397 for (i = items; NULL != i->next; i = i->next) {
2398 apply_to_items = g_slist_prepend (apply_to_items, i->data);
2399 }
2401 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate();
2402 mask_items = g_slist_prepend (mask_items, dup);
2404 if (remove_original) {
2405 SPObject *item = SP_OBJECT (i->data);
2406 item->deleteObject (false);
2407 }
2408 }
2410 g_slist_free (items);
2411 items = NULL;
2413 gchar const* attributeName = apply_clip_path ? "clip-path" : "mask";
2414 for (GSList *i = apply_to_items; NULL != i; i = i->next) {
2415 SPItem *item = reinterpret_cast<SPItem *>(i->data);
2416 // inverted object transform should be applied to a mask object,
2417 // as mask is calculated in user space (after applying transform)
2418 NR::Matrix maskTransform (item->transform.inverse());
2420 GSList *mask_items_dup = NULL;
2421 for (GSList *mask_item = mask_items; NULL != mask_item; mask_item = mask_item->next) {
2422 Inkscape::XML::Node *dup = reinterpret_cast<Inkscape::XML::Node *>(mask_item->data)->duplicate();
2423 mask_items_dup = g_slist_prepend (mask_items_dup, dup);
2424 }
2426 const gchar *mask_id = NULL;
2427 if (apply_clip_path) {
2428 mask_id = sp_clippath_create(mask_items_dup, document, &maskTransform);
2429 } else {
2430 mask_id = sp_mask_create(mask_items_dup, document, &maskTransform);
2431 }
2433 g_slist_free (mask_items_dup);
2434 mask_items_dup = NULL;
2436 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, g_strdup_printf("url(#%s)", mask_id));
2437 }
2439 g_slist_free (mask_items);
2440 g_slist_free (apply_to_items);
2442 sp_document_done (document);
2443 }
2445 void sp_selection_unset_mask(bool apply_clip_path) {
2446 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2447 if (desktop == NULL)
2448 return;
2450 SPDocument *document = SP_DT_DOCUMENT(desktop);
2451 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
2453 // check if something is selected
2454 if (selection->isEmpty()) {
2455 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to remove mask from."));
2456 return;
2457 }
2459 bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
2460 sp_document_ensure_up_to_date(document);
2462 gchar const* attributeName = apply_clip_path ? "clip-path" : "mask";
2463 std::map<SPObject*,SPItem*> referenced_objects;
2464 for (GSList const*i = selection->itemList(); NULL != i; i = i->next) {
2465 if (remove_original) {
2466 // remember referenced mask/clippath, so orphaned masks can be moved back to document
2467 SPItem *item = reinterpret_cast<SPItem *>(i->data);
2468 Inkscape::URIReference *uri_ref = NULL;
2470 if (apply_clip_path) {
2471 uri_ref = item->clip_ref;
2472 } else {
2473 uri_ref = item->mask_ref;
2474 }
2476 // collect distinct mask object (and associate with item to apply transform)
2477 if (NULL != uri_ref && NULL != uri_ref->getObject()) {
2478 referenced_objects[uri_ref->getObject()] = item;
2479 }
2480 }
2482 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, "none");
2483 }
2485 // restore mask objects into a document
2486 for ( std::map<SPObject*,SPItem*>::iterator it = referenced_objects.begin() ; it != referenced_objects.end() ; ++it) {
2487 SPObject *obj = (*it).first;
2488 GSList *items_to_move = NULL;
2489 for (SPObject *child = sp_object_first_child(obj) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2490 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate();
2491 items_to_move = g_slist_prepend (items_to_move, copy);
2492 }
2494 if (!obj->isReferenced()) {
2495 // delete from defs if no other object references this mask
2496 obj->deleteObject(false);
2497 }
2499 for (GSList *i = items_to_move; NULL != i; i = i->next) {
2500 SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr((Inkscape::XML::Node *)i->data));
2501 selection->add((Inkscape::XML::Node *)i->data);
2503 // transform mask, so it is moved the same spot there mask was applied
2504 NR::Matrix transform (item->transform);
2505 transform *= (*it).second->transform;
2506 sp_item_write_transform(item, SP_OBJECT_REPR(item), transform);
2507 }
2509 g_slist_free (items_to_move);
2510 }
2512 sp_document_done (document);
2513 }
2515 /*
2516 Local Variables:
2517 mode:c++
2518 c-file-style:"stroustrup"
2519 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2520 indent-tabs-mode:nil
2521 fill-column:99
2522 End:
2523 */
2524 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :