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(),
1155 NR::scale(
1156 apply_x? scale_x : (desktop->isToolboxButtonActive ("lock")? scale_y : 1.0),
1157 apply_y? scale_y : (desktop->isToolboxButtonActive ("lock")? scale_x : 1.0)));
1159 sp_document_done(SP_DT_DOCUMENT (desktop));
1160 }
1162 void sp_selection_paste_size_separately (bool apply_x, bool apply_y)
1163 {
1164 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1165 if (desktop == NULL) return;
1167 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1169 // check if something is in the clipboard
1170 if (size_clipboard.extent(NR::X) < 1e-6 || size_clipboard.extent(NR::Y) < 1e-6) {
1171 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1172 return;
1173 }
1175 // check if something is selected
1176 if (selection->isEmpty()) {
1177 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste size to."));
1178 return;
1179 }
1181 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1182 SPItem *item = SP_ITEM(l->data);
1184 NR::Rect current = sp_item_bbox_desktop(item);
1185 if (current.extent(NR::X) < 1e-6 || current.extent(NR::Y) < 1e-6) {
1186 continue;
1187 }
1189 double scale_x = size_clipboard.extent(NR::X) / current.extent(NR::X);
1190 double scale_y = size_clipboard.extent(NR::Y) / current.extent(NR::Y);
1192 sp_item_scale_rel (item,
1193 NR::scale(
1194 apply_x? scale_x : (desktop->isToolboxButtonActive ("lock")? scale_y : 1.0),
1195 apply_y? scale_y : (desktop->isToolboxButtonActive ("lock")? scale_x : 1.0)));
1197 }
1199 sp_document_done(SP_DT_DOCUMENT (desktop));
1200 }
1202 void sp_selection_to_next_layer ()
1203 {
1204 SPDesktop *dt = SP_ACTIVE_DESKTOP;
1206 Inkscape::Selection *selection = SP_DT_SELECTION(dt);
1208 // check if something is selected
1209 if (selection->isEmpty()) {
1210 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer above."));
1211 return;
1212 }
1214 const GSList *items = g_slist_copy ((GSList *) selection->itemList());
1216 SPObject *next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer());
1217 if (next) {
1218 GSList *temp_clip = NULL;
1219 sp_selection_copy_impl (items, &temp_clip, NULL, NULL); // we're in the same doc, so no need to copy defs
1220 sp_selection_delete_impl (items);
1221 GSList *copied = sp_selection_paste_impl (SP_DT_DOCUMENT (dt), next, &temp_clip, NULL);
1222 selection->setReprList((GSList const *) copied);
1223 g_slist_free (copied);
1224 if (temp_clip) g_slist_free (temp_clip);
1225 dt->setCurrentLayer(next);
1226 sp_document_done(SP_DT_DOCUMENT (dt));
1227 } else {
1228 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers above."));
1229 }
1231 g_slist_free ((GSList *) items);
1232 }
1234 void sp_selection_to_prev_layer ()
1235 {
1236 SPDesktop *dt = SP_ACTIVE_DESKTOP;
1238 Inkscape::Selection *selection = SP_DT_SELECTION(dt);
1240 // check if something is selected
1241 if (selection->isEmpty()) {
1242 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer below."));
1243 return;
1244 }
1246 const GSList *items = g_slist_copy ((GSList *) selection->itemList());
1248 SPObject *next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer());
1249 if (next) {
1250 GSList *temp_clip = NULL;
1251 sp_selection_copy_impl (items, &temp_clip, NULL, NULL); // we're in the same doc, so no need to copy defs
1252 sp_selection_delete_impl (items);
1253 GSList *copied = sp_selection_paste_impl (SP_DT_DOCUMENT (dt), next, &temp_clip, NULL);
1254 selection->setReprList((GSList const *) copied);
1255 g_slist_free (copied);
1256 if (temp_clip) g_slist_free (temp_clip);
1257 dt->setCurrentLayer(next);
1258 sp_document_done(SP_DT_DOCUMENT (dt));
1259 } else {
1260 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers below."));
1261 }
1263 g_slist_free ((GSList *) items);
1264 }
1266 /** Apply matrix to the selection. \a set_i2d is normally true, which means objects are in the
1267 original transform, synced with their reprs, and need to jump to the new transform in one go. A
1268 value of set_i2d==false is only used by seltrans when it's dragging objects live (not outlines); in
1269 that case, items are already in the new position, but the repr is in the old, and this function
1270 then simply updates the repr from item->transform.
1271 */
1272 void sp_selection_apply_affine(Inkscape::Selection *selection, NR::Matrix const &affine, bool set_i2d)
1273 {
1274 if (selection->isEmpty())
1275 return;
1277 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1278 SPItem *item = SP_ITEM(l->data);
1280 NR::Point old_center(0,0);
1281 if (set_i2d && item->isCenterSet())
1282 old_center = item->getCenter();
1284 #if 0 /* Re-enable this once persistent guides have a graphical indication.
1285 At the time of writing, this is the only place to re-enable. */
1286 sp_item_update_cns(*item, selection->desktop());
1287 #endif
1289 // we're moving both a clone and its original
1290 bool transform_clone_with_original = (SP_IS_USE(item) && selection->includes( sp_use_get_original (SP_USE(item)) ));
1291 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)))) ));
1292 bool transform_flowtext_with_frame = (SP_IS_FLOWTEXT(item) && selection->includes( SP_FLOWTEXT(item)->get_frame (NULL))); // only the first frame so far
1293 bool transform_offset_with_source = (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) && selection->includes( sp_offset_get_source (SP_OFFSET(item)) );
1295 // "clones are unmoved when original is moved" preference
1296 int compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
1297 bool prefs_unmoved = (compensation == SP_CLONE_COMPENSATION_UNMOVED);
1298 bool prefs_parallel = (compensation == SP_CLONE_COMPENSATION_PARALLEL);
1300 // If this is a clone and it's selected along with its original, do not move it; it will feel the
1301 // transform of its original and respond to it itself. Without this, a clone is doubly
1302 // transformed, very unintuitive.
1303 // Same for textpath if we are also doing ANY transform to its path: do not touch textpath,
1304 // letters cannot be squeezed or rotated anyway, they only refill the changed path.
1305 // Same for linked offset if we are also moving its source: do not move it.
1306 if (transform_textpath_with_path || transform_offset_with_source) {
1307 // restore item->transform field from the repr, in case it was changed by seltrans
1308 sp_object_read_attr (SP_OBJECT (item), "transform");
1310 } else if (transform_flowtext_with_frame) {
1311 // apply the inverse of the region's transform to the <use> so that the flow remains
1312 // the same (even though the output itself gets transformed)
1313 for (SPObject *region = item->firstChild() ; region ; region = SP_OBJECT_NEXT(region)) {
1314 if (!SP_IS_FLOWREGION(region) && !SP_IS_FLOWREGIONEXCLUDE(region))
1315 continue;
1316 for (SPObject *use = region->firstChild() ; use ; use = SP_OBJECT_NEXT(use)) {
1317 if (!SP_IS_USE(use)) continue;
1318 sp_item_write_transform(SP_USE(use), SP_OBJECT_REPR(use), item->transform.inverse(), NULL);
1319 }
1320 }
1321 } else if (transform_clone_with_original) {
1322 // We are transforming a clone along with its original. The below matrix juggling is
1323 // necessary to ensure that they transform as a whole, i.e. the clone's induced
1324 // transform and its move compensation are both cancelled out.
1326 // restore item->transform field from the repr, in case it was changed by seltrans
1327 sp_object_read_attr (SP_OBJECT (item), "transform");
1329 // calculate the matrix we need to apply to the clone to cancel its induced transform from its original
1330 NR::Matrix t = matrix_to_desktop (matrix_from_desktop (affine, item), item);
1331 NR::Matrix t_inv = matrix_to_desktop (matrix_from_desktop (affine.inverse(), item), item);
1332 NR::Matrix result = t_inv * item->transform * t;
1334 if ((prefs_parallel || prefs_unmoved) && affine.is_translation()) {
1335 // we need to cancel out the move compensation, too
1337 // find out the clone move, same as in sp_use_move_compensate
1338 NR::Matrix parent = sp_use_get_parent_transform (SP_USE(item));
1339 NR::Matrix clone_move = parent.inverse() * t * parent;
1341 if (prefs_parallel) {
1342 NR::Matrix move = result * clone_move * t_inv;
1343 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &move);
1345 } else if (prefs_unmoved) {
1346 if (SP_IS_USE(sp_use_get_original(SP_USE(item))))
1347 clone_move = NR::identity();
1348 NR::Matrix move = result * clone_move;
1349 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &move);
1350 }
1352 } else {
1353 // just apply the result
1354 sp_item_write_transform(item, SP_OBJECT_REPR(item), result, &result);
1355 }
1357 } else {
1358 if (set_i2d) {
1359 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * affine);
1360 }
1361 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, NULL);
1362 }
1364 // if we're moving the actual object, not just updating the repr, we can transform the
1365 // center by the same matrix (only necessary for non-translations)
1366 if (set_i2d && item->isCenterSet() && !affine.is_translation()) {
1367 item->setCenter(old_center * affine);
1368 SP_OBJECT(item)->updateRepr();
1369 }
1370 }
1371 }
1373 void sp_selection_remove_transform()
1374 {
1375 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1376 if (desktop == NULL)
1377 return;
1379 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1381 GSList const *l = (GSList *) selection->reprList();
1382 while (l != NULL) {
1383 sp_repr_set_attr((Inkscape::XML::Node*)l->data, "transform", NULL);
1384 l = l->next;
1385 }
1387 sp_document_done(SP_DT_DOCUMENT(desktop));
1388 }
1390 void
1391 sp_selection_scale_absolute(Inkscape::Selection *selection,
1392 double const x0, double const x1,
1393 double const y0, double const y1)
1394 {
1395 if (selection->isEmpty())
1396 return;
1398 NR::Rect const bbox(selection->bounds());
1399 if (bbox.isEmpty()) {
1400 return;
1401 }
1403 NR::translate const p2o(-bbox.min());
1405 NR::scale const newSize(x1 - x0,
1406 y1 - y0);
1407 NR::scale const scale( newSize / NR::scale(bbox.dimensions()) );
1408 NR::translate const o2n(x0, y0);
1409 NR::Matrix const final( p2o * scale * o2n );
1411 sp_selection_apply_affine(selection, final);
1412 }
1415 void sp_selection_scale_relative(Inkscape::Selection *selection, NR::Point const &align, NR::scale const &scale)
1416 {
1417 if (selection->isEmpty())
1418 return;
1420 NR::Rect const bbox(selection->bounds());
1422 if (bbox.isEmpty()) {
1423 return;
1424 }
1426 // FIXME: ARBITRARY LIMIT: don't try to scale above 1 Mpx, it won't display properly and will crash sooner or later anyway
1427 if ( bbox.extent(NR::X) * scale[NR::X] > 1e6 ||
1428 bbox.extent(NR::Y) * scale[NR::Y] > 1e6 )
1429 {
1430 return;
1431 }
1433 NR::translate const n2d(-align);
1434 NR::translate const d2n(align);
1435 NR::Matrix const final( n2d * scale * d2n );
1436 sp_selection_apply_affine(selection, final);
1437 }
1439 void
1440 sp_selection_rotate_relative(Inkscape::Selection *selection, NR::Point const ¢er, gdouble const angle_degrees)
1441 {
1442 NR::translate const d2n(center);
1443 NR::translate const n2d(-center);
1444 NR::rotate const rotate(rotate_degrees(angle_degrees));
1445 NR::Matrix const final( NR::Matrix(n2d) * rotate * d2n );
1446 sp_selection_apply_affine(selection, final);
1447 }
1449 void
1450 sp_selection_skew_relative(Inkscape::Selection *selection, NR::Point const &align, double dx, double dy)
1451 {
1452 NR::translate const d2n(align);
1453 NR::translate const n2d(-align);
1454 NR::Matrix const skew(1, dy,
1455 dx, 1,
1456 0, 0);
1457 NR::Matrix const final( n2d * skew * d2n );
1458 sp_selection_apply_affine(selection, final);
1459 }
1461 void sp_selection_move_relative(Inkscape::Selection *selection, NR::Point const &move)
1462 {
1463 sp_selection_apply_affine(selection, NR::Matrix(NR::translate(move)));
1464 }
1466 void sp_selection_move_relative(Inkscape::Selection *selection, double dx, double dy)
1467 {
1468 sp_selection_apply_affine(selection, NR::Matrix(NR::translate(dx, dy)));
1469 }
1472 /**
1473 * \brief sp_selection_rotate_90
1474 *
1475 * This function rotates selected objects 90 degrees clockwise.
1476 *
1477 */
1479 void sp_selection_rotate_90_cw()
1480 {
1481 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1483 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1485 if (selection->isEmpty())
1486 return;
1488 GSList const *l = selection->itemList();
1489 NR::rotate const rot_neg_90(NR::Point(0, -1));
1490 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1491 SPItem *item = SP_ITEM(l2->data);
1492 sp_item_rotate_rel(item, rot_neg_90);
1493 }
1495 sp_document_done(SP_DT_DOCUMENT(desktop));
1496 }
1499 /**
1500 * \brief sp_selection_rotate_90_ccw
1501 *
1502 * This function rotates selected objects 90 degrees counter-clockwise.
1503 *
1504 */
1506 void sp_selection_rotate_90_ccw()
1507 {
1508 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1510 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1512 if (selection->isEmpty())
1513 return;
1515 GSList const *l = selection->itemList();
1516 NR::rotate const rot_neg_90(NR::Point(0, 1));
1517 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1518 SPItem *item = SP_ITEM(l2->data);
1519 sp_item_rotate_rel(item, rot_neg_90);
1520 }
1522 sp_document_done(SP_DT_DOCUMENT(desktop));
1523 }
1525 void
1526 sp_selection_rotate(Inkscape::Selection *selection, gdouble const angle_degrees)
1527 {
1528 if (selection->isEmpty())
1529 return;
1531 NR::Point center = selection->center();
1533 sp_selection_rotate_relative(selection, center, angle_degrees);
1535 sp_document_maybe_done(SP_DT_DOCUMENT(selection->desktop()),
1536 ( ( angle_degrees > 0 )
1537 ? "selector:rotate:ccw"
1538 : "selector:rotate:cw" ));
1539 }
1541 /**
1542 \param angle the angle in "angular pixels", i.e. how many visible pixels must move the outermost point of the rotated object
1543 */
1544 void
1545 sp_selection_rotate_screen(Inkscape::Selection *selection, gdouble angle)
1546 {
1547 if (selection->isEmpty())
1548 return;
1550 NR::Rect const bbox(selection->bounds());
1552 NR::Point center = selection->center();
1554 gdouble const zoom = selection->desktop()->current_zoom();
1555 gdouble const zmove = angle / zoom;
1556 gdouble const r = NR::L2(bbox.max() - center);
1558 gdouble const zangle = 180 * atan2(zmove, r) / M_PI;
1560 sp_selection_rotate_relative(selection, center, zangle);
1562 sp_document_maybe_done(SP_DT_DOCUMENT(selection->desktop()),
1563 ( (angle > 0)
1564 ? "selector:rotate:ccw"
1565 : "selector:rotate:cw" ));
1566 }
1568 void
1569 sp_selection_scale(Inkscape::Selection *selection, gdouble grow)
1570 {
1571 if (selection->isEmpty())
1572 return;
1574 NR::Rect const bbox(selection->bounds());
1575 NR::Point const center(bbox.midpoint());
1576 double const max_len = bbox.maxExtent();
1578 // you can't scale "do nizhe pola" (below zero)
1579 if ( max_len + grow <= 1e-3 ) {
1580 return;
1581 }
1583 double const times = 1.0 + grow / max_len;
1584 sp_selection_scale_relative(selection, center, NR::scale(times, times));
1586 sp_document_maybe_done(SP_DT_DOCUMENT(selection->desktop()),
1587 ( (grow > 0)
1588 ? "selector:scale:larger"
1589 : "selector:scale:smaller" ));
1590 }
1592 void
1593 sp_selection_scale_screen(Inkscape::Selection *selection, gdouble grow_pixels)
1594 {
1595 sp_selection_scale(selection,
1596 grow_pixels / selection->desktop()->current_zoom());
1597 }
1599 void
1600 sp_selection_scale_times(Inkscape::Selection *selection, gdouble times)
1601 {
1602 if (selection->isEmpty())
1603 return;
1605 NR::Point const center(selection->bounds().midpoint());
1606 sp_selection_scale_relative(selection, center, NR::scale(times, times));
1607 sp_document_done(SP_DT_DOCUMENT(selection->desktop()));
1608 }
1610 void
1611 sp_selection_move(gdouble dx, gdouble dy)
1612 {
1613 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1614 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1615 if (selection->isEmpty()) {
1616 return;
1617 }
1619 sp_selection_move_relative(selection, dx, dy);
1621 if (dx == 0) {
1622 sp_document_maybe_done(SP_DT_DOCUMENT(desktop), "selector:move:vertical");
1623 } else if (dy == 0) {
1624 sp_document_maybe_done(SP_DT_DOCUMENT(desktop), "selector:move:horizontal");
1625 } else {
1626 sp_document_done(SP_DT_DOCUMENT(desktop));
1627 }
1628 }
1630 void
1631 sp_selection_move_screen(gdouble dx, gdouble dy)
1632 {
1633 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1635 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1636 if (selection->isEmpty()) {
1637 return;
1638 }
1640 // same as sp_selection_move but divide deltas by zoom factor
1641 gdouble const zoom = desktop->current_zoom();
1642 gdouble const zdx = dx / zoom;
1643 gdouble const zdy = dy / zoom;
1644 sp_selection_move_relative(selection, zdx, zdy);
1646 if (dx == 0) {
1647 sp_document_maybe_done(SP_DT_DOCUMENT(desktop), "selector:move:vertical");
1648 } else if (dy == 0) {
1649 sp_document_maybe_done(SP_DT_DOCUMENT(desktop), "selector:move:horizontal");
1650 } else {
1651 sp_document_done(SP_DT_DOCUMENT(desktop));
1652 }
1653 }
1655 namespace {
1657 template <typename D>
1658 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
1659 bool only_in_viewport, bool inlayer, bool onlyvisible, bool onlysensitive);
1661 template <typename D>
1662 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items, SPObject *root,
1663 bool only_in_viewport, bool inlayer, bool onlyvisible, bool onlysensitive);
1665 struct Forward {
1666 typedef SPObject *Iterator;
1668 static Iterator children(SPObject *o) { return sp_object_first_child(o); }
1669 static Iterator siblings_after(SPObject *o) { return SP_OBJECT_NEXT(o); }
1670 static void dispose(Iterator i) {}
1672 static SPObject *object(Iterator i) { return i; }
1673 static Iterator next(Iterator i) { return SP_OBJECT_NEXT(i); }
1674 };
1676 struct Reverse {
1677 typedef GSList *Iterator;
1679 static Iterator children(SPObject *o) {
1680 return make_list(o->firstChild(), NULL);
1681 }
1682 static Iterator siblings_after(SPObject *o) {
1683 return make_list(SP_OBJECT_PARENT(o)->firstChild(), o);
1684 }
1685 static void dispose(Iterator i) {
1686 g_slist_free(i);
1687 }
1689 static SPObject *object(Iterator i) {
1690 return reinterpret_cast<SPObject *>(i->data);
1691 }
1692 static Iterator next(Iterator i) { return i->next; }
1694 private:
1695 static GSList *make_list(SPObject *object, SPObject *limit) {
1696 GSList *list=NULL;
1697 while ( object != limit ) {
1698 list = g_slist_prepend(list, object);
1699 object = SP_OBJECT_NEXT(object);
1700 }
1701 return list;
1702 }
1703 };
1705 }
1707 void
1708 sp_selection_item_next(void)
1709 {
1710 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1711 g_return_if_fail(desktop != NULL);
1712 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1714 bool inlayer = prefs_get_int_attribute ("options.kbselection", "inlayer", 1);
1715 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
1716 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
1718 SPObject *root;
1719 if (inlayer) {
1720 root = desktop->currentLayer();
1721 } else {
1722 root = desktop->currentRoot();
1723 }
1725 SPItem *item=next_item_from_list<Forward>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1727 if (item) {
1728 selection->set(item);
1729 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1730 scroll_to_show_item(desktop, item);
1731 }
1732 }
1733 }
1735 void
1736 sp_selection_item_prev(void)
1737 {
1738 SPDocument *document = SP_ACTIVE_DOCUMENT;
1739 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1740 g_return_if_fail(document != NULL);
1741 g_return_if_fail(desktop != NULL);
1742 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1744 bool inlayer = prefs_get_int_attribute ("options.kbselection", "inlayer", 1);
1745 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
1746 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
1748 SPObject *root;
1749 if (inlayer) {
1750 root = desktop->currentLayer();
1751 } else {
1752 root = desktop->currentRoot();
1753 }
1755 SPItem *item=next_item_from_list<Reverse>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1757 if (item) {
1758 selection->set(item);
1759 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1760 scroll_to_show_item(desktop, item);
1761 }
1762 }
1763 }
1765 namespace {
1767 template <typename D>
1768 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items,
1769 SPObject *root, bool only_in_viewport, bool inlayer, bool onlyvisible, bool onlysensitive)
1770 {
1771 SPObject *current=root;
1772 while (items) {
1773 SPItem *item=SP_ITEM(items->data);
1774 if ( root->isAncestorOf(item) &&
1775 ( !only_in_viewport || desktop->isWithinViewport(item) ) )
1776 {
1777 current = item;
1778 break;
1779 }
1780 items = items->next;
1781 }
1783 GSList *path=NULL;
1784 while ( current != root ) {
1785 path = g_slist_prepend(path, current);
1786 current = SP_OBJECT_PARENT(current);
1787 }
1789 SPItem *next;
1790 // first, try from the current object
1791 next = next_item<D>(desktop, path, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1792 g_slist_free(path);
1794 if (!next) { // if we ran out of objects, start over at the root
1795 next = next_item<D>(desktop, NULL, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1796 }
1798 return next;
1799 }
1801 template <typename D>
1802 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
1803 bool only_in_viewport, bool inlayer, bool onlyvisible, bool onlysensitive)
1804 {
1805 typename D::Iterator children;
1806 typename D::Iterator iter;
1808 SPItem *found=NULL;
1810 if (path) {
1811 SPObject *object=reinterpret_cast<SPObject *>(path->data);
1812 g_assert(SP_OBJECT_PARENT(object) == root);
1813 if (desktop->isLayer(object)) {
1814 found = next_item<D>(desktop, path->next, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1815 }
1816 iter = children = D::siblings_after(object);
1817 } else {
1818 iter = children = D::children(root);
1819 }
1821 while ( iter && !found ) {
1822 SPObject *object=D::object(iter);
1823 if (desktop->isLayer(object)) {
1824 if (!inlayer) { // recurse into sublayers
1825 found = next_item<D>(desktop, NULL, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1826 }
1827 } else if ( SP_IS_ITEM(object) &&
1828 ( !only_in_viewport || desktop->isWithinViewport(SP_ITEM(object)) ) &&
1829 ( !onlyvisible || !desktop->itemIsHidden(SP_ITEM(object))) &&
1830 ( !onlysensitive || !SP_ITEM(object)->isLocked()) &&
1831 !desktop->isLayer(SP_ITEM(object)) )
1832 {
1833 found = SP_ITEM(object);
1834 }
1835 iter = D::next(iter);
1836 }
1838 D::dispose(children);
1840 return found;
1841 }
1843 }
1845 /**
1846 * If \a item is not entirely visible then adjust visible area to centre on the centre on of
1847 * \a item.
1848 */
1849 void scroll_to_show_item(SPDesktop *desktop, SPItem *item)
1850 {
1851 NR::Rect dbox = desktop->get_display_area();
1852 NR::Rect sbox = sp_item_bbox_desktop(item);
1854 if (dbox.contains(sbox) == false) {
1855 NR::Point const s_dt = sbox.midpoint();
1856 NR::Point const s_w = desktop->d2w(s_dt);
1857 NR::Point const d_dt = dbox.midpoint();
1858 NR::Point const d_w = desktop->d2w(d_dt);
1859 NR::Point const moved_w( d_w - s_w );
1860 gint const dx = (gint) moved_w[X];
1861 gint const dy = (gint) moved_w[Y];
1862 desktop->scroll_world(dx, dy);
1863 }
1864 }
1867 void
1868 sp_selection_clone()
1869 {
1870 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1871 if (desktop == NULL)
1872 return;
1874 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1876 // check if something is selected
1877 if (selection->isEmpty()) {
1878 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object</b> to clone."));
1879 return;
1880 }
1882 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
1884 selection->clear();
1886 // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
1887 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
1889 GSList *newsel = NULL;
1891 while (reprs) {
1892 Inkscape::XML::Node *sel_repr = (Inkscape::XML::Node *) reprs->data;
1893 Inkscape::XML::Node *parent = sp_repr_parent(sel_repr);
1895 Inkscape::XML::Node *clone = sp_repr_new("svg:use");
1896 sp_repr_set_attr(clone, "x", "0");
1897 sp_repr_set_attr(clone, "y", "0");
1898 sp_repr_set_attr(clone, "xlink:href", g_strdup_printf("#%s", sel_repr->attribute("id")));
1900 sp_repr_set_attr(clone, "inkscape:transform-center-x", sel_repr->attribute("inkscape:transform-center-x"));
1901 sp_repr_set_attr(clone, "inkscape:transform-center-y", sel_repr->attribute("inkscape:transform-center-y"));
1903 // add the new clone to the top of the original's parent
1904 parent->appendChild(clone);
1906 newsel = g_slist_prepend(newsel, clone);
1907 reprs = g_slist_remove(reprs, sel_repr);
1908 Inkscape::GC::release(clone);
1909 }
1911 sp_document_done(SP_DT_DOCUMENT(desktop));
1913 selection->setReprList(newsel);
1915 g_slist_free(newsel);
1916 }
1918 void
1919 sp_selection_unlink()
1920 {
1921 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1922 if (!desktop)
1923 return;
1925 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1927 if (selection->isEmpty()) {
1928 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>clone</b> to unlink."));
1929 return;
1930 }
1932 // Get a copy of current selection.
1933 GSList *new_select = NULL;
1934 bool unlinked = false;
1935 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
1936 items != NULL;
1937 items = items->next)
1938 {
1939 SPItem *use = (SPItem *) items->data;
1941 if (!SP_IS_USE(use)) {
1942 // keep the non-yse item in the new selection
1943 new_select = g_slist_prepend(new_select, use);
1944 continue;
1945 }
1947 SPItem *unlink = sp_use_unlink(SP_USE(use));
1948 unlinked = true;
1949 // Add ungrouped items to the new selection.
1950 new_select = g_slist_prepend(new_select, unlink);
1951 }
1953 if (new_select) { // set new selection
1954 selection->clear();
1955 selection->setList(new_select);
1956 g_slist_free(new_select);
1957 }
1958 if (!unlinked) {
1959 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No clones to unlink</b> in the selection."));
1960 }
1962 sp_document_done(SP_DT_DOCUMENT(desktop));
1963 }
1965 void
1966 sp_select_clone_original()
1967 {
1968 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1969 if (desktop == NULL)
1970 return;
1972 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1974 SPItem *item = selection->singleItem();
1976 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.");
1978 // Check if other than two objects are selected
1979 if (g_slist_length((GSList *) selection->itemList()) != 1 || !item) {
1980 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
1981 return;
1982 }
1984 SPItem *original = NULL;
1985 if (SP_IS_USE(item)) {
1986 original = sp_use_get_original (SP_USE(item));
1987 } else if (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) {
1988 original = sp_offset_get_source (SP_OFFSET(item));
1989 } else if (SP_IS_TEXT_TEXTPATH(item)) {
1990 original = sp_textpath_get_path_item (SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))));
1991 } else if (SP_IS_FLOWTEXT(item)) {
1992 original = SP_FLOWTEXT(item)->get_frame (NULL); // first frame only
1993 } else { // it's an object that we don't know what to do with
1994 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
1995 return;
1996 }
1998 if (!original) {
1999 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>Cannot find</b> the object to select (orphaned clone, offset, textpath, flowed text?)"));
2000 return;
2001 }
2003 for (SPObject *o = original; o && !SP_IS_ROOT(o); o = SP_OBJECT_PARENT (o)) {
2004 if (SP_IS_DEFS (o)) {
2005 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("The object you're trying to select is <b>not visible</b> (it is in <defs>)"));
2006 return;
2007 }
2008 }
2010 if (original) {
2011 selection->clear();
2012 selection->set(original);
2013 if (SP_CYCLING == SP_CYCLE_FOCUS) {
2014 scroll_to_show_item(desktop, original);
2015 }
2016 }
2017 }
2019 void
2020 sp_selection_tile(bool apply)
2021 {
2022 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2023 if (desktop == NULL)
2024 return;
2026 SPDocument *document = SP_DT_DOCUMENT(desktop);
2028 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
2030 // check if something is selected
2031 if (selection->isEmpty()) {
2032 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to pattern."));
2033 return;
2034 }
2036 sp_document_ensure_up_to_date(document);
2037 NR::Rect r = selection->bounds();
2038 if (r.isEmpty()) {
2039 return;
2040 }
2042 // calculate the transform to be applied to objects to move them to 0,0
2043 NR::Point move_p = NR::Point(0, sp_document_height(document)) - (r.min() + NR::Point (0, r.extent(NR::Y)));
2044 move_p[NR::Y] = -move_p[NR::Y];
2045 NR::Matrix move = NR::Matrix (NR::translate (move_p));
2047 GSList *items = g_slist_copy((GSList *) selection->itemList());
2049 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2051 // bottommost object, after sorting
2052 SPObject *parent = SP_OBJECT_PARENT (items->data);
2054 // remember the position of the first item
2055 gint pos = SP_OBJECT_REPR (items->data)->position();
2057 // create a list of duplicates
2058 GSList *repr_copies = NULL;
2059 for (GSList *i = items; i != NULL; i = i->next) {
2060 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate();
2061 repr_copies = g_slist_prepend (repr_copies, dup);
2062 }
2064 NR::Rect bounds(desktop->dt2doc(r.min()), desktop->dt2doc(r.max()));
2066 if (apply) {
2067 // delete objects so that their clones don't get alerted; this object will be restored shortly
2068 for (GSList *i = items; i != NULL; i = i->next) {
2069 SPObject *item = SP_OBJECT (i->data);
2070 item->deleteObject (false);
2071 }
2072 }
2074 // Hack: Temporarily set clone compensation to unmoved, so that we can move clone-originals
2075 // without disturbing clones.
2076 // See ActorAlign::on_button_click() in src/ui/dialog/align-and-distribute.cpp
2077 int saved_compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2078 prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2080 const gchar *pat_id = pattern_tile (repr_copies, bounds, document,
2081 NR::Matrix(NR::translate(desktop->dt2doc(NR::Point(r.min()[NR::X], r.max()[NR::Y])))), move);
2083 // restore compensation setting
2084 prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
2086 if (apply) {
2087 Inkscape::XML::Node *rect = sp_repr_new ("svg:rect");
2088 rect->setAttribute("style", g_strdup_printf("stroke:none;fill:url(#%s)", pat_id));
2089 sp_repr_set_svg_double(rect, "width", bounds.extent(NR::X));
2090 sp_repr_set_svg_double(rect, "height", bounds.extent(NR::Y));
2091 sp_repr_set_svg_double(rect, "x", bounds.min()[NR::X]);
2092 sp_repr_set_svg_double(rect, "y", bounds.min()[NR::Y]);
2094 // restore parent and position
2095 SP_OBJECT_REPR (parent)->appendChild(rect);
2096 rect->setPosition(pos > 0 ? pos : 0);
2097 SPItem *rectangle = (SPItem *) SP_DT_DOCUMENT (desktop)->getObjectByRepr(rect);
2099 Inkscape::GC::release(rect);
2101 selection->clear();
2102 selection->set(rectangle);
2103 }
2105 g_slist_free (items);
2107 sp_document_done (document);
2108 }
2110 void
2111 sp_selection_untile()
2112 {
2113 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2114 if (desktop == NULL)
2115 return;
2117 SPDocument *document = SP_DT_DOCUMENT(desktop);
2119 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
2121 // check if something is selected
2122 if (selection->isEmpty()) {
2123 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object with pattern fill</b> to extract objects from."));
2124 return;
2125 }
2127 GSList *new_select = NULL;
2129 bool did = false;
2131 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2132 items != NULL;
2133 items = items->next) {
2135 SPItem *item = (SPItem *) items->data;
2137 SPStyle *style = SP_OBJECT_STYLE (item);
2139 if (!style || style->fill.type != SP_PAINT_TYPE_PAINTSERVER)
2140 continue;
2142 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
2144 if (!SP_IS_PATTERN(server))
2145 continue;
2147 did = true;
2149 SPPattern *pattern = pattern_getroot (SP_PATTERN (server));
2151 NR::Matrix pat_transform = pattern_patternTransform (SP_PATTERN (server));
2152 pat_transform *= item->transform;
2154 for (SPObject *child = sp_object_first_child(SP_OBJECT(pattern)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2155 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate();
2156 SPItem *i = SP_ITEM (desktop->currentLayer()->appendChildRepr(copy));
2158 // FIXME: relink clones to the new canvas objects
2159 // use SPObject::setid when mental finishes it to steal ids of
2161 // this is needed to make sure the new item has curve (simply requestDisplayUpdate does not work)
2162 sp_document_ensure_up_to_date (document);
2164 NR::Matrix transform( i->transform * pat_transform );
2165 sp_item_write_transform(i, SP_OBJECT_REPR(i), transform);
2167 new_select = g_slist_prepend(new_select, i);
2168 }
2170 SPCSSAttr *css = sp_repr_css_attr_new ();
2171 sp_repr_css_set_property (css, "fill", "none");
2172 sp_repr_css_change (SP_OBJECT_REPR (item), css, "style");
2173 }
2175 if (!did) {
2176 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No pattern fills</b> in the selection."));
2177 } else {
2178 sp_document_done(SP_DT_DOCUMENT(desktop));
2179 selection->setList(new_select);
2180 }
2181 }
2183 void
2184 sp_selection_create_bitmap_copy ()
2185 {
2186 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2187 if (desktop == NULL)
2188 return;
2190 SPDocument *document = SP_DT_DOCUMENT(desktop);
2192 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
2194 // check if something is selected
2195 if (selection->isEmpty()) {
2196 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to make a bitmap copy."));
2197 return;
2198 }
2200 // Get the bounding box of the selection
2201 NRRect bbox;
2202 sp_document_ensure_up_to_date (document);
2203 selection->bounds(&bbox);
2204 if (NR_RECT_DFLS_TEST_EMPTY(&bbox)) {
2205 return; // exceptional situation, so not bother with a translatable error message, just quit quietly
2206 }
2208 // List of the items to show; all others will be hidden
2209 GSList *items = g_slist_copy ((GSList *) selection->itemList());
2211 // Sort items so that the topmost comes last
2212 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
2214 // Generate a random value from the current time (you may create bitmap from the same object(s)
2215 // multiple times, and this is done so that they don't clash)
2216 GTimeVal cu;
2217 g_get_current_time (&cu);
2218 guint current = (int) (cu.tv_sec * 1000000 + cu.tv_usec) % 1024;
2220 // Create the filename
2221 gchar *filename = g_strdup_printf ("%s-%s-%u.png", document->name, SP_OBJECT_REPR(items->data)->attribute("id"), current);
2222 // Imagemagick is known not to handle spaces in filenames, so we replace anything but letters,
2223 // digits, and a few other chars, with "_"
2224 filename = g_strcanon (filename, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.=+~$#@^&!?", '_');
2225 // Build the complete path by adding document->base if set
2226 gchar *filepath = g_build_filename (document->base?document->base:"", filename, NULL);
2228 //g_print ("%s\n", filepath);
2230 // Remember parent and z-order of the topmost one
2231 gint pos = SP_OBJECT_REPR(g_slist_last(items)->data)->position();
2232 SPObject *parent_object = SP_OBJECT_PARENT(g_slist_last(items)->data);
2233 Inkscape::XML::Node *parent = SP_OBJECT_REPR(parent_object);
2235 // Calculate resolution
2236 double res;
2237 int const prefs_res = prefs_get_int_attribute ("options.createbitmap", "resolution", 0);
2238 if (0 < prefs_res) {
2239 // If it's given explicitly in prefs, take it
2240 res = prefs_res;
2241 } else {
2242 // Otherwise look up minimum bitmap size (default 250 pixels) and calculate resolution from it
2243 int const prefs_min = prefs_get_int_attribute ("options.createbitmap", "minsize", 250);
2244 res = prefs_min / MIN ((bbox.x1 - bbox.x0), (bbox.y1 - bbox.y0));
2245 }
2247 // The width and height of the bitmap in pixels
2248 unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * res);
2249 unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * res);
2251 // Find out if we have to run a filter
2252 const gchar *run = NULL;
2253 const gchar *filter = prefs_get_string_attribute ("options.createbitmap", "filter");
2254 if (filter) {
2255 // filter command is given;
2256 // see if we have a parameter to pass to it
2257 const gchar *param1 = prefs_get_string_attribute ("options.createbitmap", "filter_param1");
2258 if (param1) {
2259 if (param1[strlen(param1) - 1] == '%') {
2260 // if the param string ends with %, interpret it as a percentage of the image's max dimension
2261 gchar p1[256];
2262 g_ascii_dtostr (p1, 256, ceil (g_ascii_strtod (param1, NULL) * MAX(width, height) / 100));
2263 // the first param is always the image filename, the second is param1
2264 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, p1);
2265 } else {
2266 // otherwise pass the param1 unchanged
2267 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, param1);
2268 }
2269 } else {
2270 // run without extra parameter
2271 run = g_strdup_printf ("%s \"%s\"", filter, filepath);
2272 }
2273 }
2275 // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
2276 NR::Matrix eek = sp_item_i2d_affine (SP_ITEM(parent_object));
2277 NR::Matrix t = NR::scale (1/res, -1/res) * NR::translate (bbox.x0, bbox.y1) * eek.inverse();
2279 // Do the export
2280 sp_export_png_file(document, filepath,
2281 bbox.x0, bbox.y0, bbox.x1, bbox.y1,
2282 width, height,
2283 (guint32) 0xffffff00,
2284 NULL, NULL,
2285 true, /*bool force_overwrite,*/
2286 items);
2288 g_slist_free (items);
2290 // Run filter, if any
2291 if (run) {
2292 g_print ("Running external filter: %s\n", run);
2293 system (run);
2294 }
2296 // Import the image back
2297 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filepath, NULL);
2298 if (pb) {
2299 // Create the repr for the image
2300 Inkscape::XML::Node * repr = sp_repr_new ("svg:image");
2301 repr->setAttribute("xlink:href", filename);
2302 repr->setAttribute("sodipodi:absref", filepath);
2303 sp_repr_set_svg_double(repr, "width", gdk_pixbuf_get_width(pb));
2304 sp_repr_set_svg_double(repr, "height", gdk_pixbuf_get_height(pb));
2306 // Write transform
2307 gchar c[256];
2308 if (sp_svg_transform_write(c, 256, t)) {
2309 repr->setAttribute("transform", c);
2310 }
2312 // add the new repr to the parent
2313 parent->appendChild(repr);
2315 // move to the saved position
2316 repr->setPosition(pos > 0 ? pos + 1 : 1);
2318 // Set selection to the new image
2319 selection->clear();
2320 selection->add(repr);
2322 // Clean up
2323 Inkscape::GC::release(repr);
2324 gdk_pixbuf_unref (pb);
2326 // Complete undoable transaction
2327 sp_document_done (document);
2328 }
2330 g_free (filename);
2331 g_free (filepath);
2332 }
2334 /**
2335 * \brief sp_selection_set_mask
2336 *
2337 * This function creates a mask or clipPath from selection
2338 * Two different modes:
2339 * if applyToLayer, all selection is moved to DEFS as mask/clippath
2340 * and is applied to current layer
2341 * otherwise, topmost object is used as mask for other objects
2342 * If \a apply_clip_path parameter is true, clipPath is created, otherwise mask
2343 *
2344 */
2345 void
2346 sp_selection_set_mask(bool apply_clip_path, bool apply_to_layer)
2347 {
2348 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2349 if (desktop == NULL)
2350 return;
2352 SPDocument *document = SP_DT_DOCUMENT(desktop);
2354 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
2356 // check if something is selected
2357 bool is_empty = selection->isEmpty();
2358 if ( apply_to_layer && is_empty) {
2359 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to create mask from."));
2360 return;
2361 } else if (!apply_to_layer && ( is_empty || NULL == selection->itemList()->next )) {
2362 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select mask object and <b>object(s)</b> to apply mask to."));
2363 return;
2364 }
2366 sp_document_ensure_up_to_date(document);
2368 GSList *items = g_slist_copy((GSList *) selection->itemList());
2370 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2372 // create a list of duplicates
2373 GSList *mask_items = NULL;
2374 GSList *apply_to_items = NULL;
2375 bool topmost = prefs_get_int_attribute ("options.maskobject", "topmost", 1);
2376 bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
2378 if (apply_to_layer) {
2379 // all selected items are used for mask, which is applied to a layer
2380 apply_to_items = g_slist_prepend (apply_to_items, desktop->currentLayer());
2382 for (GSList *i = items; i != NULL; i = i->next) {
2383 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate();
2384 mask_items = g_slist_prepend (mask_items, dup);
2386 if (remove_original) {
2387 SPObject *item = SP_OBJECT (i->data);
2388 item->deleteObject (false);
2389 }
2390 }
2391 } else if (!topmost) {
2392 // topmost item is used as a mask, which is applied to other items in a selection
2393 GSList *i = items;
2394 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate();
2395 mask_items = g_slist_prepend (mask_items, dup);
2397 if (remove_original) {
2398 SPObject *item = SP_OBJECT (i->data);
2399 item->deleteObject (false);
2400 }
2402 for (i = i->next; i != NULL; i = i->next) {
2403 apply_to_items = g_slist_prepend (apply_to_items, i->data);
2404 }
2405 } else {
2406 GSList *i = NULL;
2407 for (i = items; NULL != i->next; i = i->next) {
2408 apply_to_items = g_slist_prepend (apply_to_items, i->data);
2409 }
2411 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate();
2412 mask_items = g_slist_prepend (mask_items, dup);
2414 if (remove_original) {
2415 SPObject *item = SP_OBJECT (i->data);
2416 item->deleteObject (false);
2417 }
2418 }
2420 g_slist_free (items);
2421 items = NULL;
2423 gchar const* attributeName = apply_clip_path ? "clip-path" : "mask";
2424 for (GSList *i = apply_to_items; NULL != i; i = i->next) {
2425 SPItem *item = reinterpret_cast<SPItem *>(i->data);
2426 // inverted object transform should be applied to a mask object,
2427 // as mask is calculated in user space (after applying transform)
2428 NR::Matrix maskTransform (item->transform.inverse());
2430 GSList *mask_items_dup = NULL;
2431 for (GSList *mask_item = mask_items; NULL != mask_item; mask_item = mask_item->next) {
2432 Inkscape::XML::Node *dup = reinterpret_cast<Inkscape::XML::Node *>(mask_item->data)->duplicate();
2433 mask_items_dup = g_slist_prepend (mask_items_dup, dup);
2434 }
2436 const gchar *mask_id = NULL;
2437 if (apply_clip_path) {
2438 mask_id = sp_clippath_create(mask_items_dup, document, &maskTransform);
2439 } else {
2440 mask_id = sp_mask_create(mask_items_dup, document, &maskTransform);
2441 }
2443 g_slist_free (mask_items_dup);
2444 mask_items_dup = NULL;
2446 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, g_strdup_printf("url(#%s)", mask_id));
2447 }
2449 g_slist_free (mask_items);
2450 g_slist_free (apply_to_items);
2452 sp_document_done (document);
2453 }
2455 void sp_selection_unset_mask(bool apply_clip_path) {
2456 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2457 if (desktop == NULL)
2458 return;
2460 SPDocument *document = SP_DT_DOCUMENT(desktop);
2461 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
2463 // check if something is selected
2464 if (selection->isEmpty()) {
2465 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to remove mask from."));
2466 return;
2467 }
2469 bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
2470 sp_document_ensure_up_to_date(document);
2472 gchar const* attributeName = apply_clip_path ? "clip-path" : "mask";
2473 std::map<SPObject*,SPItem*> referenced_objects;
2474 for (GSList const*i = selection->itemList(); NULL != i; i = i->next) {
2475 if (remove_original) {
2476 // remember referenced mask/clippath, so orphaned masks can be moved back to document
2477 SPItem *item = reinterpret_cast<SPItem *>(i->data);
2478 Inkscape::URIReference *uri_ref = NULL;
2480 if (apply_clip_path) {
2481 uri_ref = item->clip_ref;
2482 } else {
2483 uri_ref = item->mask_ref;
2484 }
2486 // collect distinct mask object (and associate with item to apply transform)
2487 if (NULL != uri_ref && NULL != uri_ref->getObject()) {
2488 referenced_objects[uri_ref->getObject()] = item;
2489 }
2490 }
2492 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, "none");
2493 }
2495 // restore mask objects into a document
2496 for ( std::map<SPObject*,SPItem*>::iterator it = referenced_objects.begin() ; it != referenced_objects.end() ; ++it) {
2497 SPObject *obj = (*it).first;
2498 GSList *items_to_move = NULL;
2499 for (SPObject *child = sp_object_first_child(obj) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2500 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate();
2501 items_to_move = g_slist_prepend (items_to_move, copy);
2502 }
2504 if (!obj->isReferenced()) {
2505 // delete from defs if no other object references this mask
2506 obj->deleteObject(false);
2507 }
2509 for (GSList *i = items_to_move; NULL != i; i = i->next) {
2510 SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr((Inkscape::XML::Node *)i->data));
2511 selection->add((Inkscape::XML::Node *)i->data);
2513 // transform mask, so it is moved the same spot there mask was applied
2514 NR::Matrix transform (item->transform);
2515 transform *= (*it).second->transform;
2516 sp_item_write_transform(item, SP_OBJECT_REPR(item), transform);
2517 }
2519 g_slist_free (items_to_move);
2520 }
2522 sp_document_done (document);
2523 }
2525 /*
2526 Local Variables:
2527 mode:c++
2528 c-file-style:"stroustrup"
2529 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2530 indent-tabs-mode:nil
2531 fill-column:99
2532 End:
2533 */
2534 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :