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