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 "connector-context.h"
43 #include "sp-path.h"
44 #include "sp-conn-end.h"
45 #include "dropper-context.h"
46 #include <glibmm/i18n.h>
47 #include "libnr/nr-matrix-rotate-ops.h"
48 #include "libnr/nr-matrix-translate-ops.h"
49 #include "libnr/nr-rotate-fns.h"
50 #include "libnr/nr-scale-ops.h"
51 #include "libnr/nr-scale-translate-ops.h"
52 #include "libnr/nr-translate-matrix-ops.h"
53 #include "libnr/nr-translate-scale-ops.h"
54 #include "xml/repr.h"
55 #include "style.h"
56 #include "document-private.h"
57 #include "sp-gradient.h"
58 #include "sp-gradient-reference.h"
59 #include "sp-linear-gradient-fns.h"
60 #include "sp-pattern.h"
61 #include "sp-radial-gradient-fns.h"
62 #include "sp-namedview.h"
63 #include "prefs-utils.h"
64 #include "sp-offset.h"
65 #include "sp-clippath.h"
66 #include "sp-mask.h"
67 #include "file.h"
68 #include "helper/png-write.h"
69 #include "layer-fns.h"
70 #include "context-fns.h"
71 #include <map>
72 #include "helper/units.h"
73 #include "sp-item.h"
74 #include "unit-constants.h"
75 using NR::X;
76 using NR::Y;
78 #include "selection-chemistry.h"
80 /* fixme: find a better place */
81 GSList *clipboard = NULL;
82 GSList *defs_clipboard = NULL;
83 SPCSSAttr *style_clipboard = NULL;
84 NR::Rect size_clipboard(NR::Point(0,0), NR::Point(0,0));
86 static void sp_copy_stuff_used_by_item(GSList **defs_clip, SPItem *item, const GSList *items);
88 /**
89 * Copies repr and its inherited css style elements, along with the accumulated transform 'full_t',
90 * then prepends the copy to 'clip'.
91 */
92 void sp_selection_copy_one (Inkscape::XML::Node *repr, NR::Matrix full_t, GSList **clip)
93 {
94 Inkscape::XML::Node *copy = repr->duplicate();
96 // copy complete inherited style
97 SPCSSAttr *css = sp_repr_css_attr_inherited(repr, "style");
98 sp_repr_css_set(copy, css, "style");
99 sp_repr_css_attr_unref(css);
101 // write the complete accumulated transform passed to us
102 // (we're dealing with unattached repr, so we write to its attr
103 // instead of using sp_item_set_transform)
104 gchar affinestr[80];
105 if (sp_svg_transform_write(affinestr, 79, full_t)) {
106 copy->setAttribute("transform", affinestr);
107 } else {
108 copy->setAttribute("transform", NULL);
109 }
111 *clip = g_slist_prepend(*clip, copy);
112 }
114 void sp_selection_copy_impl (const GSList *items, GSList **clip, GSList **defs_clip, SPCSSAttr **style_clip)
115 {
117 // Copy stuff referenced by all items to defs_clip:
118 if (defs_clip) {
119 for (GSList *i = (GSList *) items; i != NULL; i = i->next) {
120 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (i->data), items);
121 }
122 *defs_clip = g_slist_reverse(*defs_clip);
123 }
125 // Store style:
126 if (style_clip) {
127 SPItem *item = SP_ITEM (items->data); // take from the first selected item
128 *style_clip = take_style_from_item (item);
129 }
131 if (clip) {
132 // Sort items:
133 GSList *sorted_items = g_slist_copy ((GSList *) items);
134 sorted_items = g_slist_sort((GSList *) sorted_items, (GCompareFunc) sp_object_compare_position);
136 // Copy item reprs:
137 for (GSList *i = (GSList *) sorted_items; i != NULL; i = i->next) {
138 sp_selection_copy_one (SP_OBJECT_REPR (i->data), sp_item_i2doc_affine(SP_ITEM (i->data)), clip);
139 }
141 *clip = g_slist_reverse(*clip);
142 g_slist_free ((GSList *) sorted_items);
143 }
144 }
146 /**
147 * Add gradients/patterns/markers referenced by copied objects to defs.
148 * Iterates through 'defs_clip', and for each item it adds the data
149 * repr into the global defs.
150 */
151 void
152 paste_defs (GSList **defs_clip, SPDocument *doc)
153 {
154 if (!defs_clip)
155 return;
157 for (GSList *gl = *defs_clip; gl != NULL; gl = gl->next) {
158 SPDefs *defs= (SPDefs *) SP_DOCUMENT_DEFS(doc);
159 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) gl->data;
160 gchar const *id = repr->attribute("id");
161 if (!id || !doc->getObjectById(id)) {
162 Inkscape::XML::Node *copy = repr->duplicate();
163 SP_OBJECT_REPR(defs)->addChild(copy, NULL);
164 Inkscape::GC::release(copy);
165 }
166 }
167 }
169 GSList *sp_selection_paste_impl (SPDocument *document, SPObject *parent, GSList **clip, GSList **defs_clip)
170 {
171 paste_defs (defs_clip, document);
173 GSList *copied = NULL;
174 // add objects to document
175 for (GSList *l = *clip; l != NULL; l = l->next) {
176 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
177 Inkscape::XML::Node *copy = repr->duplicate();
179 // premultiply the item transform by the accumulated parent transform in the paste layer
180 NR::Matrix local = sp_item_i2doc_affine(SP_ITEM(parent));
181 if (!local.test_identity()) {
182 gchar const *t_str = copy->attribute("transform");
183 NR::Matrix item_t (NR::identity());
184 if (t_str)
185 sp_svg_transform_read(t_str, &item_t);
186 item_t *= local.inverse();
187 // (we're dealing with unattached repr, so we write to its attr instead of using sp_item_set_transform)
188 gchar affinestr[80];
189 if (sp_svg_transform_write(affinestr, 79, item_t)) {
190 copy->setAttribute("transform", affinestr);
191 } else {
192 copy->setAttribute("transform", NULL);
193 }
194 }
196 parent->appendChildRepr(copy);
197 copied = g_slist_prepend(copied, copy);
198 Inkscape::GC::release(copy);
199 }
200 return copied;
201 }
203 void sp_selection_delete_impl(const GSList *items)
204 {
205 for (const GSList *i = items ; i ; i = i->next ) {
206 sp_object_ref((SPObject *)i->data, NULL);
207 }
208 for (const GSList *i = items; i != NULL; i = i->next) {
209 SPItem *item = (SPItem *) i->data;
210 SP_OBJECT(item)->deleteObject();
211 sp_object_unref((SPObject *)item, NULL);
212 }
213 }
216 void sp_selection_delete()
217 {
218 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
219 if (desktop == NULL) {
220 return;
221 }
223 if (tools_isactive (desktop, TOOLS_TEXT))
224 if (sp_text_delete_selection(desktop->event_context)) {
225 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_TEXT,
226 _("Delete text"));
227 return;
228 }
230 Inkscape::Selection *selection = sp_desktop_selection(desktop);
232 // check if something is selected
233 if (selection->isEmpty()) {
234 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Nothing</b> was deleted."));
235 return;
236 }
238 const GSList *selected = g_slist_copy(const_cast<GSList *>(selection->itemList()));
239 selection->clear();
240 sp_selection_delete_impl (selected);
241 g_slist_free ((GSList *) selected);
243 /* a tool may have set up private information in it's selection context
244 * that depends on desktop items. I think the only sane way to deal with
245 * this currently is to reset the current tool, which will reset it's
246 * associated selection context. For example: deleting an object
247 * while moving it around the canvas.
248 */
249 tools_switch ( desktop, tools_active ( desktop ) );
251 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_DELETE,
252 _("Delete"));
253 }
255 /* fixme: sequencing */
256 void sp_selection_duplicate()
257 {
258 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
259 if (desktop == NULL)
260 return;
262 Inkscape::Selection *selection = sp_desktop_selection(desktop);
264 // check if something is selected
265 if (selection->isEmpty()) {
266 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to duplicate."));
267 return;
268 }
270 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
272 selection->clear();
274 // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
275 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
277 GSList *newsel = NULL;
279 while (reprs) {
280 Inkscape::XML::Node *parent = ((Inkscape::XML::Node *) reprs->data)->parent();
281 Inkscape::XML::Node *copy = ((Inkscape::XML::Node *) reprs->data)->duplicate();
283 parent->appendChild(copy);
285 newsel = g_slist_prepend(newsel, copy);
286 reprs = g_slist_remove(reprs, reprs->data);
287 Inkscape::GC::release(copy);
288 }
290 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_DUPLICATE,
291 _("Duplicate"));
293 selection->setReprList(newsel);
295 g_slist_free(newsel);
296 }
298 void sp_edit_clear_all()
299 {
300 SPDesktop *dt = SP_ACTIVE_DESKTOP;
301 if (!dt)
302 return;
304 SPDocument *doc = sp_desktop_document(dt);
305 sp_desktop_selection(dt)->clear();
307 g_return_if_fail(SP_IS_GROUP(dt->currentLayer()));
308 GSList *items = sp_item_group_item_list(SP_GROUP(dt->currentLayer()));
310 while (items) {
311 SP_OBJECT (items->data)->deleteObject();
312 items = g_slist_remove(items, items->data);
313 }
315 sp_document_done(doc, SP_VERB_EDIT_CLEAR_ALL,
316 _("Delete all"));
317 }
319 GSList *
320 get_all_items (GSList *list, SPObject *from, SPDesktop *desktop, bool onlyvisible, bool onlysensitive, const GSList *exclude)
321 {
322 for (SPObject *child = sp_object_first_child(SP_OBJECT(from)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
323 if (SP_IS_ITEM(child) &&
324 !desktop->isLayer(SP_ITEM(child)) &&
325 (!onlysensitive || !SP_ITEM(child)->isLocked()) &&
326 (!onlyvisible || !desktop->itemIsHidden(SP_ITEM(child))) &&
327 (!exclude || !g_slist_find ((GSList *) exclude, child))
328 )
329 {
330 list = g_slist_prepend (list, SP_ITEM(child));
331 }
333 if (SP_IS_ITEM(child) && desktop->isLayer(SP_ITEM(child))) {
334 list = get_all_items (list, child, desktop, onlyvisible, onlysensitive, exclude);
335 }
336 }
338 return list;
339 }
341 void sp_edit_select_all_full (bool force_all_layers, bool invert)
342 {
343 SPDesktop *dt = SP_ACTIVE_DESKTOP;
344 if (!dt)
345 return;
347 Inkscape::Selection *selection = sp_desktop_selection(dt);
349 g_return_if_fail(SP_IS_GROUP(dt->currentLayer()));
351 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
352 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
353 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
355 GSList *items = NULL;
357 const GSList *exclude = NULL;
358 if (invert) {
359 exclude = selection->itemList();
360 }
362 if (force_all_layers)
363 inlayer = PREFS_SELECTION_ALL;
365 switch (inlayer) {
366 case PREFS_SELECTION_LAYER: {
367 if ( (onlysensitive && SP_ITEM(dt->currentLayer())->isLocked()) ||
368 (onlyvisible && dt->itemIsHidden(SP_ITEM(dt->currentLayer()))) )
369 return;
371 GSList *all_items = sp_item_group_item_list(SP_GROUP(dt->currentLayer()));
373 for (GSList *i = all_items; i; i = i->next) {
374 SPItem *item = SP_ITEM (i->data);
376 if (item && (!onlysensitive || !item->isLocked())) {
377 if (!onlyvisible || !dt->itemIsHidden(item)) {
378 if (!dt->isLayer(item)) {
379 if (!invert || !g_slist_find ((GSList *) exclude, item)) {
380 items = g_slist_prepend (items, item); // leave it in the list
381 }
382 }
383 }
384 }
385 }
387 g_slist_free (all_items);
388 break;
389 }
390 case PREFS_SELECTION_LAYER_RECURSIVE: {
391 items = get_all_items (NULL, dt->currentLayer(), dt, onlyvisible, onlysensitive, exclude);
392 break;
393 }
394 default: {
395 items = get_all_items (NULL, dt->currentRoot(), dt, onlyvisible, onlysensitive, exclude);
396 break;
397 }
398 }
400 selection->setList (items);
402 if (items) {
403 g_slist_free (items);
404 }
405 }
407 void sp_edit_select_all ()
408 {
409 sp_edit_select_all_full (false, false);
410 }
412 void sp_edit_select_all_in_all_layers ()
413 {
414 sp_edit_select_all_full (true, false);
415 }
417 void sp_edit_invert ()
418 {
419 sp_edit_select_all_full (false, true);
420 }
422 void sp_edit_invert_in_all_layers ()
423 {
424 sp_edit_select_all_full (true, true);
425 }
427 void sp_selection_group()
428 {
429 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
430 if (desktop == NULL)
431 return;
433 SPDocument *document = sp_desktop_document (desktop);
435 Inkscape::Selection *selection = sp_desktop_selection(desktop);
437 // Check if something is selected.
438 if (selection->isEmpty()) {
439 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>two or more objects</b> to group."));
440 return;
441 }
443 GSList const *l = (GSList *) selection->reprList();
445 // Check if at least two objects are selected.
446 if (l->next == NULL) {
447 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>at least two objects</b> to group."));
448 return;
449 }
451 GSList *p = g_slist_copy((GSList *) l);
453 selection->clear();
455 p = g_slist_sort(p, (GCompareFunc) sp_repr_compare_position);
457 // Remember the position and parent of the topmost object.
458 gint topmost = ((Inkscape::XML::Node *) g_slist_last(p)->data)->position();
459 Inkscape::XML::Node *topmost_parent = ((Inkscape::XML::Node *) g_slist_last(p)->data)->parent();
461 Inkscape::XML::Node *group = sp_repr_new("svg:g");
463 while (p) {
464 Inkscape::XML::Node *current = (Inkscape::XML::Node *) p->data;
466 if (current->parent() == topmost_parent) {
467 Inkscape::XML::Node *spnew = current->duplicate();
468 sp_repr_unparent(current);
469 group->appendChild(spnew);
470 Inkscape::GC::release(spnew);
471 topmost --; // only reduce count for those items deleted from topmost_parent
472 } else { // move it to topmost_parent first
473 GSList *temp_clip = NULL;
475 // At this point, current may already have no item, due to its being a clone whose original is already moved away
476 // So we copy it artificially calculating the transform from its repr->attr("transform") and the parent transform
477 gchar const *t_str = current->attribute("transform");
478 NR::Matrix item_t (NR::identity());
479 if (t_str)
480 sp_svg_transform_read(t_str, &item_t);
481 item_t *= sp_item_i2doc_affine(SP_ITEM(document->getObjectByRepr(current->parent())));
482 //FIXME: when moving both clone and original from a transformed group (either by
483 //grouping into another parent, or by cut/paste) the transform from the original's
484 //parent becomes embedded into original itself, and this affects its clones. Fix
485 //this by remembering the transform diffs we write to each item into an array and
486 //then, if this is clone, looking up its original in that array and pre-multiplying
487 //it by the inverse of that original's transform diff.
489 sp_selection_copy_one (current, item_t, &temp_clip);
490 sp_repr_unparent(current);
492 // paste into topmost_parent (temporarily)
493 GSList *copied = sp_selection_paste_impl (document, document->getObjectByRepr(topmost_parent), &temp_clip, NULL);
494 if (temp_clip) g_slist_free (temp_clip);
495 if (copied) { // if success,
496 // take pasted object (now in topmost_parent)
497 Inkscape::XML::Node *in_topmost = (Inkscape::XML::Node *) copied->data;
498 // make a copy
499 Inkscape::XML::Node *spnew = in_topmost->duplicate();
500 // remove pasted
501 sp_repr_unparent(in_topmost);
502 // put its copy into group
503 group->appendChild(spnew);
504 Inkscape::GC::release(spnew);
505 g_slist_free (copied);
506 }
507 }
508 p = g_slist_remove(p, current);
509 }
511 // Add the new group to the topmost members' parent
512 topmost_parent->appendChild(group);
514 // Move to the position of the topmost, reduced by the number of items deleted from topmost_parent
515 group->setPosition(topmost + 1);
517 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_GROUP,
518 _("Group"));
520 selection->set(group);
521 Inkscape::GC::release(group);
522 }
524 void sp_selection_ungroup()
525 {
526 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
527 if (desktop == NULL)
528 return;
530 Inkscape::Selection *selection = sp_desktop_selection(desktop);
532 if (selection->isEmpty()) {
533 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>group</b> to ungroup."));
534 return;
535 }
537 GSList *items = g_slist_copy((GSList *) selection->itemList());
538 selection->clear();
540 // Get a copy of current selection.
541 GSList *new_select = NULL;
542 bool ungrouped = false;
543 for (GSList *i = items;
544 i != NULL;
545 i = i->next)
546 {
547 SPItem *group = (SPItem *) i->data;
549 // when ungrouping cloned groups with their originals, some objects that were selected may no more exist due to unlinking
550 if (!SP_IS_OBJECT(group)) {
551 continue;
552 }
554 /* We do not allow ungrouping <svg> etc. (lauris) */
555 if (strcmp(SP_OBJECT_REPR(group)->name(), "svg:g") && strcmp(SP_OBJECT_REPR(group)->name(), "svg:switch")) {
556 // keep the non-group item in the new selection
557 selection->add(group);
558 continue;
559 }
561 GSList *children = NULL;
562 /* This is not strictly required, but is nicer to rely on group ::destroy (lauris) */
563 sp_item_group_ungroup(SP_GROUP(group), &children, false);
564 ungrouped = true;
565 // Add ungrouped items to the new selection.
566 new_select = g_slist_concat(new_select, children);
567 }
569 if (new_select) { // Set new selection.
570 selection->addList(new_select);
571 g_slist_free(new_select);
572 }
573 if (!ungrouped) {
574 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No groups</b> to ungroup in the selection."));
575 }
577 g_slist_free(items);
579 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_UNGROUP,
580 _("Ungroup"));
581 }
583 static SPGroup *
584 sp_item_list_common_parent_group(const GSList *items)
585 {
586 if (!items) {
587 return NULL;
588 }
589 SPObject *parent = SP_OBJECT_PARENT(items->data);
590 /* Strictly speaking this CAN happen, if user selects <svg> from XML editor */
591 if (!SP_IS_GROUP(parent)) {
592 return NULL;
593 }
594 for (items = items->next; items; items = items->next) {
595 if (SP_OBJECT_PARENT(items->data) != parent) {
596 return NULL;
597 }
598 }
600 return SP_GROUP(parent);
601 }
603 /** Finds out the minimum common bbox of the selected items
604 */
605 NR::Rect
606 enclose_items(const GSList *items)
607 {
608 g_assert(items != NULL);
610 NR::Rect r = sp_item_bbox_desktop((SPItem *) items->data);
612 for (GSList *i = items->next; i; i = i->next) {
613 r = NR::Rect::union_bounds(r, sp_item_bbox_desktop((SPItem *) i->data));
614 }
616 return r;
617 }
619 SPObject *
620 prev_sibling(SPObject *child)
621 {
622 SPObject *parent = SP_OBJECT_PARENT(child);
623 if (!SP_IS_GROUP(parent)) {
624 return NULL;
625 }
626 for ( SPObject *i = sp_object_first_child(parent) ; i; i = SP_OBJECT_NEXT(i) ) {
627 if (i->next == child)
628 return i;
629 }
630 return NULL;
631 }
633 void
634 sp_selection_raise()
635 {
636 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
637 if (!desktop)
638 return;
640 Inkscape::Selection *selection = sp_desktop_selection(desktop);
642 GSList const *items = (GSList *) selection->itemList();
643 if (!items) {
644 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise."));
645 return;
646 }
648 SPGroup const *group = sp_item_list_common_parent_group(items);
649 if (!group) {
650 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
651 return;
652 }
654 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
656 /* construct reverse-ordered list of selected children */
657 GSList *rev = g_slist_copy((GSList *) items);
658 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
660 // find out the common bbox of the selected items
661 NR::Rect selected = enclose_items(items);
663 // for all objects in the selection (starting from top)
664 while (rev) {
665 SPObject *child = SP_OBJECT(rev->data);
666 // for each selected object, find the next sibling
667 for (SPObject *newref = child->next; newref; newref = newref->next) {
668 // if the sibling is an item AND overlaps our selection,
669 if (SP_IS_ITEM(newref) && selected.intersects(sp_item_bbox_desktop(SP_ITEM(newref)))) {
670 // AND if it's not one of our selected objects,
671 if (!g_slist_find((GSList *) items, newref)) {
672 // move the selected object after that sibling
673 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(newref));
674 }
675 break;
676 }
677 }
678 rev = g_slist_remove(rev, child);
679 }
681 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_RAISE,
682 _("Raise"));
683 }
685 void sp_selection_raise_to_top()
686 {
687 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
688 if (desktop == NULL)
689 return;
691 SPDocument *document = sp_desktop_document(desktop);
692 Inkscape::Selection *selection = sp_desktop_selection(desktop);
694 if (selection->isEmpty()) {
695 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise to top."));
696 return;
697 }
699 GSList const *items = (GSList *) selection->itemList();
701 SPGroup const *group = sp_item_list_common_parent_group(items);
702 if (!group) {
703 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
704 return;
705 }
707 GSList *rl = g_slist_copy((GSList *) selection->reprList());
708 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
710 for (GSList *l = rl; l != NULL; l = l->next) {
711 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
712 repr->setPosition(-1);
713 }
715 g_slist_free(rl);
717 sp_document_done(document, SP_VERB_SELECTION_TO_FRONT,
718 _("Raise to top"));
719 }
721 void
722 sp_selection_lower()
723 {
724 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
725 if (desktop == NULL)
726 return;
728 Inkscape::Selection *selection = sp_desktop_selection(desktop);
730 GSList const *items = (GSList *) selection->itemList();
731 if (!items) {
732 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower."));
733 return;
734 }
736 SPGroup const *group = sp_item_list_common_parent_group(items);
737 if (!group) {
738 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
739 return;
740 }
742 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
744 // find out the common bbox of the selected items
745 NR::Rect selected = enclose_items(items);
747 /* construct direct-ordered list of selected children */
748 GSList *rev = g_slist_copy((GSList *) items);
749 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
750 rev = g_slist_reverse(rev);
752 // for all objects in the selection (starting from top)
753 while (rev) {
754 SPObject *child = SP_OBJECT(rev->data);
755 // for each selected object, find the prev sibling
756 for (SPObject *newref = prev_sibling(child); newref; newref = prev_sibling(newref)) {
757 // if the sibling is an item AND overlaps our selection,
758 if (SP_IS_ITEM(newref) && selected.intersects(sp_item_bbox_desktop(SP_ITEM(newref)))) {
759 // AND if it's not one of our selected objects,
760 if (!g_slist_find((GSList *) items, newref)) {
761 // move the selected object before that sibling
762 SPObject *put_after = prev_sibling(newref);
763 if (put_after)
764 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(put_after));
765 else
766 SP_OBJECT_REPR(child)->setPosition(0);
767 }
768 break;
769 }
770 }
771 rev = g_slist_remove(rev, child);
772 }
774 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_LOWER,
775 _("Lower"));
777 }
779 void sp_selection_lower_to_bottom()
780 {
781 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
782 if (desktop == NULL)
783 return;
785 SPDocument *document = sp_desktop_document(desktop);
786 Inkscape::Selection *selection = sp_desktop_selection(desktop);
788 if (selection->isEmpty()) {
789 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower to bottom."));
790 return;
791 }
793 GSList const *items = (GSList *) selection->itemList();
795 SPGroup const *group = sp_item_list_common_parent_group(items);
796 if (!group) {
797 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
798 return;
799 }
801 GSList *rl;
802 rl = g_slist_copy((GSList *) selection->reprList());
803 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
804 rl = g_slist_reverse(rl);
806 for (GSList *l = rl; l != NULL; l = l->next) {
807 gint minpos;
808 SPObject *pp, *pc;
809 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
810 pp = document->getObjectByRepr(sp_repr_parent(repr));
811 minpos = 0;
812 g_assert(SP_IS_GROUP(pp));
813 pc = sp_object_first_child(pp);
814 while (!SP_IS_ITEM(pc)) {
815 minpos += 1;
816 pc = pc->next;
817 }
818 repr->setPosition(minpos);
819 }
821 g_slist_free(rl);
823 sp_document_done(document, SP_VERB_SELECTION_TO_BACK,
824 _("Lower to bottom"));
825 }
827 void
828 sp_undo(SPDesktop *desktop, SPDocument *)
829 {
830 if (!sp_document_undo(sp_desktop_document(desktop)))
831 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to undo."));
832 }
834 void
835 sp_redo(SPDesktop *desktop, SPDocument *)
836 {
837 if (!sp_document_redo(sp_desktop_document(desktop)))
838 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to redo."));
839 }
841 void sp_selection_cut()
842 {
843 sp_selection_copy();
844 sp_selection_delete();
845 }
847 void sp_copy_gradient (GSList **defs_clip, SPGradient *gradient)
848 {
849 SPGradient *ref = gradient;
851 while (ref) {
852 // climb up the refs, copying each one in the chain
853 Inkscape::XML::Node *grad_repr = SP_OBJECT_REPR(ref)->duplicate();
854 *defs_clip = g_slist_prepend (*defs_clip, grad_repr);
856 ref = ref->ref->getObject();
857 }
858 }
860 void sp_copy_pattern (GSList **defs_clip, SPPattern *pattern)
861 {
862 SPPattern *ref = pattern;
864 while (ref) {
865 // climb up the refs, copying each one in the chain
866 Inkscape::XML::Node *pattern_repr = SP_OBJECT_REPR(ref)->duplicate();
867 *defs_clip = g_slist_prepend (*defs_clip, pattern_repr);
869 // items in the pattern may also use gradients and other patterns, so we need to recurse here as well
870 for (SPObject *child = sp_object_first_child(SP_OBJECT(ref)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
871 if (!SP_IS_ITEM (child))
872 continue;
873 sp_copy_stuff_used_by_item (defs_clip, (SPItem *) child, NULL);
874 }
876 ref = ref->ref->getObject();
877 }
878 }
880 void sp_copy_single (GSList **defs_clip, SPObject *thing)
881 {
882 Inkscape::XML::Node *duplicate_repr = SP_OBJECT_REPR(thing)->duplicate();
883 *defs_clip = g_slist_prepend (*defs_clip, duplicate_repr);
884 }
887 void sp_copy_textpath_path (GSList **defs_clip, SPTextPath *tp, const GSList *items)
888 {
889 SPItem *path = sp_textpath_get_path_item (tp);
890 if (!path)
891 return;
892 if (items && g_slist_find ((GSList *) items, path)) // do not copy it to defs if it is already in the list of items copied
893 return;
894 Inkscape::XML::Node *repr = SP_OBJECT_REPR(path)->duplicate();
895 *defs_clip = g_slist_prepend (*defs_clip, repr);
896 }
898 /**
899 * Copies things like patterns, markers, gradients, etc.
900 */
901 void sp_copy_stuff_used_by_item (GSList **defs_clip, SPItem *item, const GSList *items)
902 {
903 SPStyle *style = SP_OBJECT_STYLE (item);
905 if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
906 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
907 if (SP_IS_LINEARGRADIENT (server) || SP_IS_RADIALGRADIENT (server))
908 sp_copy_gradient (defs_clip, SP_GRADIENT(server));
909 if (SP_IS_PATTERN (server))
910 sp_copy_pattern (defs_clip, SP_PATTERN(server));
911 }
913 if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
914 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
915 if (SP_IS_LINEARGRADIENT (server) || SP_IS_RADIALGRADIENT (server))
916 sp_copy_gradient (defs_clip, SP_GRADIENT(server));
917 if (SP_IS_PATTERN (server))
918 sp_copy_pattern (defs_clip, SP_PATTERN(server));
919 }
921 // For shapes, copy all of the shape's markers into defs_clip
922 if (SP_IS_SHAPE (item)) {
923 SPShape *shape = SP_SHAPE (item);
924 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
925 if (shape->marker[i]) {
926 sp_copy_single (defs_clip, SP_OBJECT (shape->marker[i]));
927 }
928 }
929 }
931 if (SP_IS_TEXT_TEXTPATH (item)) {
932 sp_copy_textpath_path (defs_clip, SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))), items);
933 }
935 if (item->clip_ref->getObject()) {
936 sp_copy_single (defs_clip, item->clip_ref->getObject());
937 }
939 if (item->mask_ref->getObject()) {
940 SPObject *mask = item->mask_ref->getObject();
941 sp_copy_single (defs_clip, mask);
942 // recurse into the mask for its gradients etc.
943 for (SPObject *o = SP_OBJECT(mask)->children; o != NULL; o = o->next) {
944 if (SP_IS_ITEM(o))
945 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (o), items);
946 }
947 }
949 if (style->filter.filter) {
950 SPObject *filter = style->filter.filter;
951 if (SP_IS_FILTER(filter)) {
952 sp_copy_single (defs_clip, filter);
953 }
954 }
956 // recurse
957 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
958 if (SP_IS_ITEM(o))
959 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (o), items);
960 }
961 }
963 /**
964 * \pre item != NULL
965 */
966 SPCSSAttr *
967 take_style_from_item (SPItem *item)
968 {
969 // write the complete cascaded style, context-free
970 SPCSSAttr *css = sp_css_attr_from_object (SP_OBJECT(item), SP_STYLE_FLAG_ALWAYS);
971 if (css == NULL)
972 return NULL;
974 if ((SP_IS_GROUP(item) && SP_OBJECT(item)->children) ||
975 (SP_IS_TEXT (item) && SP_OBJECT(item)->children && SP_OBJECT(item)->children->next == NULL)) {
976 // if this is a text with exactly one tspan child, merge the style of that tspan as well
977 // If this is a group, merge the style of its topmost (last) child with style
978 for (SPObject *last_element = item->lastChild(); last_element != NULL; last_element = SP_OBJECT_PREV (last_element)) {
979 if (SP_OBJECT_STYLE (last_element) != NULL) {
980 SPCSSAttr *temp = sp_css_attr_from_object (last_element, SP_STYLE_FLAG_IFSET);
981 if (temp) {
982 sp_repr_css_merge (css, temp);
983 sp_repr_css_attr_unref (temp);
984 }
985 break;
986 }
987 }
988 }
989 if (!(SP_IS_TEXT (item) || SP_IS_TSPAN (item) || SP_IS_STRING (item))) {
990 // do not copy text properties from non-text objects, it's confusing
991 css = sp_css_attr_unset_text (css);
992 }
994 // FIXME: also transform gradient/pattern fills, by forking? NO, this must be nondestructive
995 double ex = NR::expansion(sp_item_i2doc_affine(item));
996 if (ex != 1.0) {
997 css = sp_css_attr_scale (css, ex);
998 }
1000 return css;
1001 }
1004 void sp_selection_copy()
1005 {
1006 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1007 if (desktop == NULL)
1008 return;
1010 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1012 if (tools_isactive (desktop, TOOLS_DROPPER)) {
1013 sp_dropper_context_copy(desktop->event_context);
1014 return; // copied color under cursor, nothing else to do
1015 }
1017 // check if something is selected
1018 if (selection->isEmpty()) {
1019 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing was copied."));
1020 return;
1021 }
1023 const GSList *items = g_slist_copy ((GSList *) selection->itemList());
1025 // 0. Copy text to system clipboard
1026 // FIXME: for non-texts, put serialized XML as text to the clipboard;
1027 //for this sp_repr_write_stream needs to be rewritten with iostream instead of FILE
1028 Glib::ustring text;
1029 if (tools_isactive (desktop, TOOLS_TEXT)) {
1030 text = sp_text_get_selected_text(desktop->event_context);
1031 }
1033 if (text.empty()) {
1034 guint texts = 0;
1035 for (GSList *i = (GSList *) items; i; i = i->next) {
1036 SPItem *item = SP_ITEM (i->data);
1037 if (SP_IS_TEXT (item) || SP_IS_FLOWTEXT(item)) {
1038 if (texts > 0) // if more than one text object is copied, separate them by spaces
1039 text += " ";
1040 gchar *this_text = sp_te_get_string_multiline (item);
1041 if (this_text) {
1042 text += this_text;
1043 g_free(this_text);
1044 }
1045 texts++;
1046 }
1047 }
1048 }
1049 if (!text.empty()) {
1050 Glib::RefPtr<Gtk::Clipboard> refClipboard = Gtk::Clipboard::get();
1051 refClipboard->set_text(text);
1052 }
1054 // clear old defs clipboard
1055 while (defs_clipboard) {
1056 Inkscape::GC::release((Inkscape::XML::Node *) defs_clipboard->data);
1057 defs_clipboard = g_slist_remove (defs_clipboard, defs_clipboard->data);
1058 }
1060 // clear style clipboard
1061 if (style_clipboard) {
1062 sp_repr_css_attr_unref (style_clipboard);
1063 style_clipboard = NULL;
1064 }
1066 //clear main clipboard
1067 while (clipboard) {
1068 Inkscape::GC::release((Inkscape::XML::Node *) clipboard->data);
1069 clipboard = g_slist_remove(clipboard, clipboard->data);
1070 }
1072 sp_selection_copy_impl (items, &clipboard, &defs_clipboard, &style_clipboard);
1074 if (tools_isactive (desktop, TOOLS_TEXT)) { // take style from cursor/text selection, overwriting the style just set by copy_impl
1075 SPStyle *const query = sp_style_new();
1076 if (sp_desktop_query_style_all (desktop, query)) {
1077 SPCSSAttr *css = sp_css_attr_from_style (query, SP_STYLE_FLAG_ALWAYS);
1078 if (css != NULL) {
1079 // clear style clipboard
1080 if (style_clipboard) {
1081 sp_repr_css_attr_unref (style_clipboard);
1082 style_clipboard = NULL;
1083 }
1084 //sp_repr_css_print (css);
1085 style_clipboard = css;
1086 }
1087 }
1088 g_free (query);
1089 }
1091 size_clipboard = selection->bounds();
1093 g_slist_free ((GSList *) items);
1094 }
1096 void sp_selection_paste(bool in_place)
1097 {
1098 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1100 if (desktop == NULL) {
1101 return;
1102 }
1104 SPDocument *document = sp_desktop_document(desktop);
1106 if (Inkscape::have_viable_layer(desktop, desktop->messageStack()) == false) {
1107 return;
1108 }
1110 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1112 if (tools_isactive (desktop, TOOLS_TEXT)) {
1113 if (sp_text_paste_inline(desktop->event_context))
1114 return; // pasted from system clipboard into text, nothing else to do
1115 }
1117 // check if something is in the clipboard
1118 if (clipboard == NULL) {
1119 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1120 return;
1121 }
1123 GSList *copied = sp_selection_paste_impl(document, desktop->currentLayer(), &clipboard, &defs_clipboard);
1124 // add pasted objects to selection
1125 selection->setReprList((GSList const *) copied);
1126 g_slist_free (copied);
1128 if (!in_place) {
1129 sp_document_ensure_up_to_date(document);
1131 NR::Point m( desktop->point() - selection->bounds().midpoint() );
1133 /* Snap the offset of the new item(s) to the grid */
1134 /* FIXME: this gridsnap fiddling is a hack. */
1135 Inkscape::GridSnapper &s = desktop->namedview->snap_manager.grid;
1136 gdouble const curr_gridsnap = s.getDistance();
1137 s.setDistance(NR_HUGE);
1138 m = s.freeSnap(Inkscape::Snapper::SNAP_POINT, m, NULL).getPoint();
1139 s.setDistance(curr_gridsnap);
1140 sp_selection_move_relative(selection, m);
1141 }
1143 sp_document_done(document, SP_VERB_EDIT_PASTE,
1144 _("Paste"));
1145 }
1147 void sp_selection_paste_style()
1148 {
1149 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1150 if (desktop == NULL) return;
1152 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1154 // check if something is in the clipboard
1155 if (clipboard == NULL) {
1156 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1157 return;
1158 }
1160 // check if something is selected
1161 if (selection->isEmpty()) {
1162 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste style to."));
1163 return;
1164 }
1166 paste_defs (&defs_clipboard, sp_desktop_document(desktop));
1168 sp_desktop_set_style (desktop, style_clipboard);
1170 sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_STYLE,
1171 _("Paste style"));
1172 }
1174 void sp_selection_paste_size (bool apply_x, bool apply_y)
1175 {
1176 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1177 if (desktop == NULL) return;
1179 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1181 // check if something is in the clipboard
1182 if (size_clipboard.extent(NR::X) < 1e-6 || size_clipboard.extent(NR::Y) < 1e-6) {
1183 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1184 return;
1185 }
1187 // check if something is selected
1188 if (selection->isEmpty()) {
1189 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste size to."));
1190 return;
1191 }
1193 NR::Rect current = selection->bounds();
1194 if (current.extent(NR::X) < 1e-6 || current.extent(NR::Y) < 1e-6) {
1195 return;
1196 }
1198 double scale_x = size_clipboard.extent(NR::X) / current.extent(NR::X);
1199 double scale_y = size_clipboard.extent(NR::Y) / current.extent(NR::Y);
1201 sp_selection_scale_relative (selection, current.midpoint(),
1202 NR::scale(
1203 apply_x? scale_x : (desktop->isToolboxButtonActive ("lock")? scale_y : 1.0),
1204 apply_y? scale_y : (desktop->isToolboxButtonActive ("lock")? scale_x : 1.0)));
1206 sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_SIZE,
1207 _("Paste size"));
1208 }
1210 void sp_selection_paste_size_separately (bool apply_x, bool apply_y)
1211 {
1212 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1213 if (desktop == NULL) return;
1215 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1217 // check if something is in the clipboard
1218 if (size_clipboard.extent(NR::X) < 1e-6 || size_clipboard.extent(NR::Y) < 1e-6) {
1219 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1220 return;
1221 }
1223 // check if something is selected
1224 if (selection->isEmpty()) {
1225 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste size to."));
1226 return;
1227 }
1229 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1230 SPItem *item = SP_ITEM(l->data);
1232 NR::Rect current = sp_item_bbox_desktop(item);
1233 if (current.extent(NR::X) < 1e-6 || current.extent(NR::Y) < 1e-6) {
1234 continue;
1235 }
1237 double scale_x = size_clipboard.extent(NR::X) / current.extent(NR::X);
1238 double scale_y = size_clipboard.extent(NR::Y) / current.extent(NR::Y);
1240 sp_item_scale_rel (item,
1241 NR::scale(
1242 apply_x? scale_x : (desktop->isToolboxButtonActive ("lock")? scale_y : 1.0),
1243 apply_y? scale_y : (desktop->isToolboxButtonActive ("lock")? scale_x : 1.0)));
1245 }
1247 sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_SIZE_SEPARATELY,
1248 _("Paste size separately"));
1249 }
1251 void sp_selection_to_next_layer ()
1252 {
1253 SPDesktop *dt = SP_ACTIVE_DESKTOP;
1255 Inkscape::Selection *selection = sp_desktop_selection(dt);
1257 // check if something is selected
1258 if (selection->isEmpty()) {
1259 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer above."));
1260 return;
1261 }
1263 const GSList *items = g_slist_copy ((GSList *) selection->itemList());
1265 bool no_more = false; // Set to true, if no more layers above
1266 SPObject *next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer());
1267 if (next) {
1268 GSList *temp_clip = NULL;
1269 sp_selection_copy_impl (items, &temp_clip, NULL, NULL); // we're in the same doc, so no need to copy defs
1270 sp_selection_delete_impl (items);
1271 next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
1272 GSList *copied;
1273 if(next) {
1274 copied = sp_selection_paste_impl (sp_desktop_document (dt), next, &temp_clip, NULL);
1275 } else {
1276 copied = sp_selection_paste_impl (sp_desktop_document (dt), dt->currentLayer(), &temp_clip, NULL);
1277 no_more = true;
1278 }
1279 selection->setReprList((GSList const *) copied);
1280 g_slist_free (copied);
1281 if (temp_clip) g_slist_free (temp_clip);
1282 if (next) dt->setCurrentLayer(next);
1283 sp_document_done(sp_desktop_document (dt), SP_VERB_LAYER_MOVE_TO_NEXT,
1284 _("Raise to next layer"));
1285 } else {
1286 no_more = true;
1287 }
1289 if (no_more) {
1290 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers above."));
1291 }
1293 g_slist_free ((GSList *) items);
1294 }
1296 void sp_selection_to_prev_layer ()
1297 {
1298 SPDesktop *dt = SP_ACTIVE_DESKTOP;
1300 Inkscape::Selection *selection = sp_desktop_selection(dt);
1302 // check if something is selected
1303 if (selection->isEmpty()) {
1304 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer below."));
1305 return;
1306 }
1308 const GSList *items = g_slist_copy ((GSList *) selection->itemList());
1310 bool no_more = false; // Set to true, if no more layers below
1311 SPObject *next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer());
1312 if (next) {
1313 GSList *temp_clip = NULL;
1314 sp_selection_copy_impl (items, &temp_clip, NULL, NULL); // we're in the same doc, so no need to copy defs
1315 sp_selection_delete_impl (items);
1316 next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
1317 GSList *copied;
1318 if(next) {
1319 copied = sp_selection_paste_impl (sp_desktop_document (dt), next, &temp_clip, NULL);
1320 } else {
1321 copied = sp_selection_paste_impl (sp_desktop_document (dt), dt->currentLayer(), &temp_clip, NULL);
1322 no_more = true;
1323 }
1324 selection->setReprList((GSList const *) copied);
1325 g_slist_free (copied);
1326 if (temp_clip) g_slist_free (temp_clip);
1327 if (next) dt->setCurrentLayer(next);
1328 sp_document_done(sp_desktop_document (dt), SP_VERB_LAYER_MOVE_TO_PREV,
1329 _("Lower to previous layer"));
1330 } else {
1331 no_more = true;
1332 }
1334 if (no_more) {
1335 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers below."));
1336 }
1338 g_slist_free ((GSList *) items);
1339 }
1341 /** Apply matrix to the selection. \a set_i2d is normally true, which means objects are in the
1342 original transform, synced with their reprs, and need to jump to the new transform in one go. A
1343 value of set_i2d==false is only used by seltrans when it's dragging objects live (not outlines); in
1344 that case, items are already in the new position, but the repr is in the old, and this function
1345 then simply updates the repr from item->transform.
1346 */
1347 void sp_selection_apply_affine(Inkscape::Selection *selection, NR::Matrix const &affine, bool set_i2d)
1348 {
1349 if (selection->isEmpty())
1350 return;
1352 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1353 SPItem *item = SP_ITEM(l->data);
1355 NR::Point old_center(0,0);
1356 if (set_i2d && item->isCenterSet())
1357 old_center = item->getCenter();
1359 #if 0 /* Re-enable this once persistent guides have a graphical indication.
1360 At the time of writing, this is the only place to re-enable. */
1361 sp_item_update_cns(*item, selection->desktop());
1362 #endif
1364 // we're moving both a clone and its original
1365 bool transform_clone_with_original = (SP_IS_USE(item) && selection->includes( sp_use_get_original (SP_USE(item)) ));
1366 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)))) ));
1367 bool transform_flowtext_with_frame = (SP_IS_FLOWTEXT(item) && selection->includes( SP_FLOWTEXT(item)->get_frame (NULL))); // only the first frame so far
1368 bool transform_offset_with_source = (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) && selection->includes( sp_offset_get_source (SP_OFFSET(item)) );
1370 // If we're moving a connector, we want to detach it
1371 // from shapes that aren't part of the selection, but
1372 // leave it attached if they are
1373 if (cc_item_is_connector(item)) {
1374 SPItem *attItem[2];
1375 SP_PATH(item)->connEndPair.getAttachedItems(attItem);
1377 for (int n = 0; n < 2; ++n) {
1378 if (!selection->includes(attItem[n])) {
1379 sp_conn_end_detach(item, n);
1380 }
1381 }
1382 }
1384 // "clones are unmoved when original is moved" preference
1385 int compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
1386 bool prefs_unmoved = (compensation == SP_CLONE_COMPENSATION_UNMOVED);
1387 bool prefs_parallel = (compensation == SP_CLONE_COMPENSATION_PARALLEL);
1389 // If this is a clone and it's selected along with its original, do not move it; it will feel the
1390 // transform of its original and respond to it itself. Without this, a clone is doubly
1391 // transformed, very unintuitive.
1392 // Same for textpath if we are also doing ANY transform to its path: do not touch textpath,
1393 // letters cannot be squeezed or rotated anyway, they only refill the changed path.
1394 // Same for linked offset if we are also moving its source: do not move it.
1395 if (transform_textpath_with_path || transform_offset_with_source) {
1396 // restore item->transform field from the repr, in case it was changed by seltrans
1397 sp_object_read_attr (SP_OBJECT (item), "transform");
1399 } else if (transform_flowtext_with_frame) {
1400 // apply the inverse of the region's transform to the <use> so that the flow remains
1401 // the same (even though the output itself gets transformed)
1402 for (SPObject *region = item->firstChild() ; region ; region = SP_OBJECT_NEXT(region)) {
1403 if (!SP_IS_FLOWREGION(region) && !SP_IS_FLOWREGIONEXCLUDE(region))
1404 continue;
1405 for (SPObject *use = region->firstChild() ; use ; use = SP_OBJECT_NEXT(use)) {
1406 if (!SP_IS_USE(use)) continue;
1407 sp_item_write_transform(SP_USE(use), SP_OBJECT_REPR(use), item->transform.inverse(), NULL);
1408 }
1409 }
1410 } else if (transform_clone_with_original) {
1411 // We are transforming a clone along with its original. The below matrix juggling is
1412 // necessary to ensure that they transform as a whole, i.e. the clone's induced
1413 // transform and its move compensation are both cancelled out.
1415 // restore item->transform field from the repr, in case it was changed by seltrans
1416 sp_object_read_attr (SP_OBJECT (item), "transform");
1418 // calculate the matrix we need to apply to the clone to cancel its induced transform from its original
1419 NR::Matrix t = matrix_to_desktop (matrix_from_desktop (affine, item), item);
1420 NR::Matrix t_inv = matrix_to_desktop (matrix_from_desktop (affine.inverse(), item), item);
1421 NR::Matrix result = t_inv * item->transform * t;
1423 if ((prefs_parallel || prefs_unmoved) && affine.is_translation()) {
1424 // we need to cancel out the move compensation, too
1426 // find out the clone move, same as in sp_use_move_compensate
1427 NR::Matrix parent = sp_use_get_parent_transform (SP_USE(item));
1428 NR::Matrix clone_move = parent.inverse() * t * parent;
1430 if (prefs_parallel) {
1431 NR::Matrix move = result * clone_move * t_inv;
1432 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &move);
1434 } else if (prefs_unmoved) {
1435 if (SP_IS_USE(sp_use_get_original(SP_USE(item))))
1436 clone_move = NR::identity();
1437 NR::Matrix move = result * clone_move;
1438 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &move);
1439 }
1441 } else {
1442 // just apply the result
1443 sp_item_write_transform(item, SP_OBJECT_REPR(item), result, &result);
1444 }
1446 } else {
1447 if (set_i2d) {
1448 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * affine);
1449 }
1450 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, NULL);
1451 }
1453 // if we're moving the actual object, not just updating the repr, we can transform the
1454 // center by the same matrix (only necessary for non-translations)
1455 if (set_i2d && item->isCenterSet() && !affine.is_translation()) {
1456 item->setCenter(old_center * affine);
1457 SP_OBJECT(item)->updateRepr();
1458 }
1459 }
1460 }
1462 void sp_selection_remove_transform()
1463 {
1464 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1465 if (desktop == NULL)
1466 return;
1468 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1470 GSList const *l = (GSList *) selection->reprList();
1471 while (l != NULL) {
1472 sp_repr_set_attr((Inkscape::XML::Node*)l->data, "transform", NULL);
1473 l = l->next;
1474 }
1476 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_FLATTEN,
1477 _("Remove transform"));
1478 }
1480 void
1481 sp_selection_scale_absolute(Inkscape::Selection *selection,
1482 double const x0, double const x1,
1483 double const y0, double const y1)
1484 {
1485 if (selection->isEmpty())
1486 return;
1488 NR::Rect const bbox(selection->bounds());
1489 if (bbox.isEmpty()) {
1490 return;
1491 }
1493 NR::translate const p2o(-bbox.min());
1495 NR::scale const newSize(x1 - x0,
1496 y1 - y0);
1497 NR::scale const scale( newSize / NR::scale(bbox.dimensions()) );
1498 NR::translate const o2n(x0, y0);
1499 NR::Matrix const final( p2o * scale * o2n );
1501 sp_selection_apply_affine(selection, final);
1502 }
1505 void sp_selection_scale_relative(Inkscape::Selection *selection, NR::Point const &align, NR::scale const &scale)
1506 {
1507 if (selection->isEmpty())
1508 return;
1510 NR::Rect const bbox(selection->bounds());
1512 if (bbox.isEmpty()) {
1513 return;
1514 }
1516 // FIXME: ARBITRARY LIMIT: don't try to scale above 1 Mpx, it won't display properly and will crash sooner or later anyway
1517 if ( bbox.extent(NR::X) * scale[NR::X] > 1e6 ||
1518 bbox.extent(NR::Y) * scale[NR::Y] > 1e6 )
1519 {
1520 return;
1521 }
1523 NR::translate const n2d(-align);
1524 NR::translate const d2n(align);
1525 NR::Matrix const final( n2d * scale * d2n );
1526 sp_selection_apply_affine(selection, final);
1527 }
1529 void
1530 sp_selection_rotate_relative(Inkscape::Selection *selection, NR::Point const ¢er, gdouble const angle_degrees)
1531 {
1532 NR::translate const d2n(center);
1533 NR::translate const n2d(-center);
1534 NR::rotate const rotate(rotate_degrees(angle_degrees));
1535 NR::Matrix const final( NR::Matrix(n2d) * rotate * d2n );
1536 sp_selection_apply_affine(selection, final);
1537 }
1539 void
1540 sp_selection_skew_relative(Inkscape::Selection *selection, NR::Point const &align, double dx, double dy)
1541 {
1542 NR::translate const d2n(align);
1543 NR::translate const n2d(-align);
1544 NR::Matrix const skew(1, dy,
1545 dx, 1,
1546 0, 0);
1547 NR::Matrix const final( n2d * skew * d2n );
1548 sp_selection_apply_affine(selection, final);
1549 }
1551 void sp_selection_move_relative(Inkscape::Selection *selection, NR::Point const &move)
1552 {
1553 sp_selection_apply_affine(selection, NR::Matrix(NR::translate(move)));
1554 }
1556 void sp_selection_move_relative(Inkscape::Selection *selection, double dx, double dy)
1557 {
1558 sp_selection_apply_affine(selection, NR::Matrix(NR::translate(dx, dy)));
1559 }
1562 /**
1563 * \brief sp_selection_rotate_90
1564 *
1565 * This function rotates selected objects 90 degrees clockwise.
1566 *
1567 */
1569 void sp_selection_rotate_90_cw()
1570 {
1571 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1573 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1575 if (selection->isEmpty())
1576 return;
1578 GSList const *l = selection->itemList();
1579 NR::rotate const rot_neg_90(NR::Point(0, -1));
1580 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1581 SPItem *item = SP_ITEM(l2->data);
1582 sp_item_rotate_rel(item, rot_neg_90);
1583 }
1585 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_ROTATE_90_CCW,
1586 _("Rotate 90° CW"));
1587 }
1590 /**
1591 * \brief sp_selection_rotate_90_ccw
1592 *
1593 * This function rotates selected objects 90 degrees counter-clockwise.
1594 *
1595 */
1597 void sp_selection_rotate_90_ccw()
1598 {
1599 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1601 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1603 if (selection->isEmpty())
1604 return;
1606 GSList const *l = selection->itemList();
1607 NR::rotate const rot_neg_90(NR::Point(0, 1));
1608 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1609 SPItem *item = SP_ITEM(l2->data);
1610 sp_item_rotate_rel(item, rot_neg_90);
1611 }
1613 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_ROTATE_90_CW,
1614 _("Rotate 90° CCW"));
1615 }
1617 void
1618 sp_selection_rotate(Inkscape::Selection *selection, gdouble const angle_degrees)
1619 {
1620 if (selection->isEmpty())
1621 return;
1623 NR::Point center = selection->center();
1625 sp_selection_rotate_relative(selection, center, angle_degrees);
1627 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1628 ( ( angle_degrees > 0 )
1629 ? "selector:rotate:ccw"
1630 : "selector:rotate:cw" ),
1631 SP_VERB_CONTEXT_SELECT,
1632 _("Rotate"));
1633 }
1635 /**
1636 \param angle the angle in "angular pixels", i.e. how many visible pixels must move the outermost point of the rotated object
1637 */
1638 void
1639 sp_selection_rotate_screen(Inkscape::Selection *selection, gdouble angle)
1640 {
1641 if (selection->isEmpty())
1642 return;
1644 NR::Rect const bbox(selection->bounds());
1646 NR::Point center = selection->center();
1648 gdouble const zoom = selection->desktop()->current_zoom();
1649 gdouble const zmove = angle / zoom;
1650 gdouble const r = NR::L2(bbox.max() - center);
1652 gdouble const zangle = 180 * atan2(zmove, r) / M_PI;
1654 sp_selection_rotate_relative(selection, center, zangle);
1656 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1657 ( (angle > 0)
1658 ? "selector:rotate:ccw"
1659 : "selector:rotate:cw" ),
1660 SP_VERB_CONTEXT_SELECT,
1661 _("Rotate by pixels"));
1662 }
1664 void
1665 sp_selection_scale(Inkscape::Selection *selection, gdouble grow)
1666 {
1667 if (selection->isEmpty())
1668 return;
1670 NR::Rect const bbox(selection->bounds());
1671 NR::Point const center(bbox.midpoint());
1672 double const max_len = bbox.maxExtent();
1674 // you can't scale "do nizhe pola" (below zero)
1675 if ( max_len + grow <= 1e-3 ) {
1676 return;
1677 }
1679 double const times = 1.0 + grow / max_len;
1680 sp_selection_scale_relative(selection, center, NR::scale(times, times));
1682 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1683 ( (grow > 0)
1684 ? "selector:scale:larger"
1685 : "selector:scale:smaller" ),
1686 SP_VERB_CONTEXT_SELECT,
1687 _("Scale"));
1688 }
1690 void
1691 sp_selection_scale_screen(Inkscape::Selection *selection, gdouble grow_pixels)
1692 {
1693 sp_selection_scale(selection,
1694 grow_pixels / selection->desktop()->current_zoom());
1695 }
1697 void
1698 sp_selection_scale_times(Inkscape::Selection *selection, gdouble times)
1699 {
1700 if (selection->isEmpty())
1701 return;
1703 NR::Point const center(selection->bounds().midpoint());
1704 sp_selection_scale_relative(selection, center, NR::scale(times, times));
1705 sp_document_done(sp_desktop_document(selection->desktop()), SP_VERB_CONTEXT_SELECT,
1706 _("Scale by whole factor"));
1707 }
1709 void
1710 sp_selection_move(gdouble dx, gdouble dy)
1711 {
1712 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1713 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1714 if (selection->isEmpty()) {
1715 return;
1716 }
1718 sp_selection_move_relative(selection, dx, dy);
1720 if (dx == 0) {
1721 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical", SP_VERB_CONTEXT_SELECT,
1722 _("Move vertically"));
1723 } else if (dy == 0) {
1724 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal", SP_VERB_CONTEXT_SELECT,
1725 _("Move horizontally"));
1726 } else {
1727 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SELECT,
1728 _("Move"));
1729 }
1730 }
1732 void
1733 sp_selection_move_screen(gdouble dx, gdouble dy)
1734 {
1735 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1737 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1738 if (selection->isEmpty()) {
1739 return;
1740 }
1742 // same as sp_selection_move but divide deltas by zoom factor
1743 gdouble const zoom = desktop->current_zoom();
1744 gdouble const zdx = dx / zoom;
1745 gdouble const zdy = dy / zoom;
1746 sp_selection_move_relative(selection, zdx, zdy);
1748 if (dx == 0) {
1749 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical", SP_VERB_CONTEXT_SELECT,
1750 _("Nudge vertically by pixels"));
1751 } else if (dy == 0) {
1752 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal", SP_VERB_CONTEXT_SELECT,
1753 _("Nudge horizontally by pixels"));
1754 } else {
1755 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SELECT,
1756 _("Move"));
1757 }
1758 }
1760 namespace {
1762 template <typename D>
1763 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
1764 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
1766 template <typename D>
1767 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items, SPObject *root,
1768 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
1770 struct Forward {
1771 typedef SPObject *Iterator;
1773 static Iterator children(SPObject *o) { return sp_object_first_child(o); }
1774 static Iterator siblings_after(SPObject *o) { return SP_OBJECT_NEXT(o); }
1775 static void dispose(Iterator i) {}
1777 static SPObject *object(Iterator i) { return i; }
1778 static Iterator next(Iterator i) { return SP_OBJECT_NEXT(i); }
1779 };
1781 struct Reverse {
1782 typedef GSList *Iterator;
1784 static Iterator children(SPObject *o) {
1785 return make_list(o->firstChild(), NULL);
1786 }
1787 static Iterator siblings_after(SPObject *o) {
1788 return make_list(SP_OBJECT_PARENT(o)->firstChild(), o);
1789 }
1790 static void dispose(Iterator i) {
1791 g_slist_free(i);
1792 }
1794 static SPObject *object(Iterator i) {
1795 return reinterpret_cast<SPObject *>(i->data);
1796 }
1797 static Iterator next(Iterator i) { return i->next; }
1799 private:
1800 static GSList *make_list(SPObject *object, SPObject *limit) {
1801 GSList *list=NULL;
1802 while ( object != limit ) {
1803 list = g_slist_prepend(list, object);
1804 object = SP_OBJECT_NEXT(object);
1805 }
1806 return list;
1807 }
1808 };
1810 }
1812 void
1813 sp_selection_item_next(void)
1814 {
1815 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1816 g_return_if_fail(desktop != NULL);
1817 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1819 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
1820 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
1821 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
1823 SPObject *root;
1824 if (PREFS_SELECTION_ALL != inlayer) {
1825 root = selection->activeContext();
1826 } else {
1827 root = desktop->currentRoot();
1828 }
1830 SPItem *item=next_item_from_list<Forward>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1832 if (item) {
1833 selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
1834 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1835 scroll_to_show_item(desktop, item);
1836 }
1837 }
1838 }
1840 void
1841 sp_selection_item_prev(void)
1842 {
1843 SPDocument *document = SP_ACTIVE_DOCUMENT;
1844 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1845 g_return_if_fail(document != NULL);
1846 g_return_if_fail(desktop != NULL);
1847 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1849 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
1850 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
1851 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
1853 SPObject *root;
1854 if (PREFS_SELECTION_ALL != inlayer) {
1855 root = selection->activeContext();
1856 } else {
1857 root = desktop->currentRoot();
1858 }
1860 SPItem *item=next_item_from_list<Reverse>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1862 if (item) {
1863 selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
1864 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1865 scroll_to_show_item(desktop, item);
1866 }
1867 }
1868 }
1870 namespace {
1872 template <typename D>
1873 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items,
1874 SPObject *root, bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
1875 {
1876 SPObject *current=root;
1877 while (items) {
1878 SPItem *item=SP_ITEM(items->data);
1879 if ( root->isAncestorOf(item) &&
1880 ( !only_in_viewport || desktop->isWithinViewport(item) ) )
1881 {
1882 current = item;
1883 break;
1884 }
1885 items = items->next;
1886 }
1888 GSList *path=NULL;
1889 while ( current != root ) {
1890 path = g_slist_prepend(path, current);
1891 current = SP_OBJECT_PARENT(current);
1892 }
1894 SPItem *next;
1895 // first, try from the current object
1896 next = next_item<D>(desktop, path, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1897 g_slist_free(path);
1899 if (!next) { // if we ran out of objects, start over at the root
1900 next = next_item<D>(desktop, NULL, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1901 }
1903 return next;
1904 }
1906 template <typename D>
1907 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
1908 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
1909 {
1910 typename D::Iterator children;
1911 typename D::Iterator iter;
1913 SPItem *found=NULL;
1915 if (path) {
1916 SPObject *object=reinterpret_cast<SPObject *>(path->data);
1917 g_assert(SP_OBJECT_PARENT(object) == root);
1918 if (desktop->isLayer(object)) {
1919 found = next_item<D>(desktop, path->next, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1920 }
1921 iter = children = D::siblings_after(object);
1922 } else {
1923 iter = children = D::children(root);
1924 }
1926 while ( iter && !found ) {
1927 SPObject *object=D::object(iter);
1928 if (desktop->isLayer(object)) {
1929 if (PREFS_SELECTION_LAYER != inlayer) { // recurse into sublayers
1930 found = next_item<D>(desktop, NULL, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1931 }
1932 } else if ( SP_IS_ITEM(object) &&
1933 ( !only_in_viewport || desktop->isWithinViewport(SP_ITEM(object)) ) &&
1934 ( !onlyvisible || !desktop->itemIsHidden(SP_ITEM(object))) &&
1935 ( !onlysensitive || !SP_ITEM(object)->isLocked()) &&
1936 !desktop->isLayer(SP_ITEM(object)) )
1937 {
1938 found = SP_ITEM(object);
1939 }
1940 iter = D::next(iter);
1941 }
1943 D::dispose(children);
1945 return found;
1946 }
1948 }
1950 /**
1951 * If \a item is not entirely visible then adjust visible area to centre on the centre on of
1952 * \a item.
1953 */
1954 void scroll_to_show_item(SPDesktop *desktop, SPItem *item)
1955 {
1956 NR::Rect dbox = desktop->get_display_area();
1957 NR::Rect sbox = sp_item_bbox_desktop(item);
1959 if (dbox.contains(sbox) == false) {
1960 NR::Point const s_dt = sbox.midpoint();
1961 NR::Point const s_w = desktop->d2w(s_dt);
1962 NR::Point const d_dt = dbox.midpoint();
1963 NR::Point const d_w = desktop->d2w(d_dt);
1964 NR::Point const moved_w( d_w - s_w );
1965 gint const dx = (gint) moved_w[X];
1966 gint const dy = (gint) moved_w[Y];
1967 desktop->scroll_world(dx, dy);
1968 }
1969 }
1972 void
1973 sp_selection_clone()
1974 {
1975 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1976 if (desktop == NULL)
1977 return;
1979 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1981 // check if something is selected
1982 if (selection->isEmpty()) {
1983 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object</b> to clone."));
1984 return;
1985 }
1987 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
1989 selection->clear();
1991 // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
1992 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
1994 GSList *newsel = NULL;
1996 while (reprs) {
1997 Inkscape::XML::Node *sel_repr = (Inkscape::XML::Node *) reprs->data;
1998 Inkscape::XML::Node *parent = sp_repr_parent(sel_repr);
2000 Inkscape::XML::Node *clone = sp_repr_new("svg:use");
2001 sp_repr_set_attr(clone, "x", "0");
2002 sp_repr_set_attr(clone, "y", "0");
2003 sp_repr_set_attr(clone, "xlink:href", g_strdup_printf("#%s", sel_repr->attribute("id")));
2005 sp_repr_set_attr(clone, "inkscape:transform-center-x", sel_repr->attribute("inkscape:transform-center-x"));
2006 sp_repr_set_attr(clone, "inkscape:transform-center-y", sel_repr->attribute("inkscape:transform-center-y"));
2008 // add the new clone to the top of the original's parent
2009 parent->appendChild(clone);
2011 newsel = g_slist_prepend(newsel, clone);
2012 reprs = g_slist_remove(reprs, sel_repr);
2013 Inkscape::GC::release(clone);
2014 }
2016 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_CLONE,
2017 _("Clone"));
2019 selection->setReprList(newsel);
2021 g_slist_free(newsel);
2022 }
2024 void
2025 sp_selection_unlink()
2026 {
2027 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2028 if (!desktop)
2029 return;
2031 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2033 if (selection->isEmpty()) {
2034 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>clone</b> to unlink."));
2035 return;
2036 }
2038 // Get a copy of current selection.
2039 GSList *new_select = NULL;
2040 bool unlinked = false;
2041 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2042 items != NULL;
2043 items = items->next)
2044 {
2045 SPItem *use = (SPItem *) items->data;
2047 if (!SP_IS_USE(use)) {
2048 // keep the non-yse item in the new selection
2049 new_select = g_slist_prepend(new_select, use);
2050 continue;
2051 }
2053 SPItem *unlink = sp_use_unlink(SP_USE(use));
2054 unlinked = true;
2055 // Add ungrouped items to the new selection.
2056 new_select = g_slist_prepend(new_select, unlink);
2057 }
2059 if (new_select) { // set new selection
2060 selection->clear();
2061 selection->setList(new_select);
2062 g_slist_free(new_select);
2063 }
2064 if (!unlinked) {
2065 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No clones to unlink</b> in the selection."));
2066 }
2068 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNLINK_CLONE,
2069 _("Unlink clone"));
2070 }
2072 void
2073 sp_select_clone_original()
2074 {
2075 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2076 if (desktop == NULL)
2077 return;
2079 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2081 SPItem *item = selection->singleItem();
2083 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.");
2085 // Check if other than two objects are selected
2086 if (g_slist_length((GSList *) selection->itemList()) != 1 || !item) {
2087 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
2088 return;
2089 }
2091 SPItem *original = NULL;
2092 if (SP_IS_USE(item)) {
2093 original = sp_use_get_original (SP_USE(item));
2094 } else if (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) {
2095 original = sp_offset_get_source (SP_OFFSET(item));
2096 } else if (SP_IS_TEXT_TEXTPATH(item)) {
2097 original = sp_textpath_get_path_item (SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))));
2098 } else if (SP_IS_FLOWTEXT(item)) {
2099 original = SP_FLOWTEXT(item)->get_frame (NULL); // first frame only
2100 } else { // it's an object that we don't know what to do with
2101 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
2102 return;
2103 }
2105 if (!original) {
2106 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>Cannot find</b> the object to select (orphaned clone, offset, textpath, flowed text?)"));
2107 return;
2108 }
2110 for (SPObject *o = original; o && !SP_IS_ROOT(o); o = SP_OBJECT_PARENT (o)) {
2111 if (SP_IS_DEFS (o)) {
2112 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("The object you're trying to select is <b>not visible</b> (it is in <defs>)"));
2113 return;
2114 }
2115 }
2117 if (original) {
2118 selection->clear();
2119 selection->set(original);
2120 if (SP_CYCLING == SP_CYCLE_FOCUS) {
2121 scroll_to_show_item(desktop, original);
2122 }
2123 }
2124 }
2126 void
2127 sp_selection_tile(bool apply)
2128 {
2129 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2130 if (desktop == NULL)
2131 return;
2133 SPDocument *document = sp_desktop_document(desktop);
2135 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2137 // check if something is selected
2138 if (selection->isEmpty()) {
2139 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to pattern."));
2140 return;
2141 }
2143 sp_document_ensure_up_to_date(document);
2144 NR::Rect r = selection->bounds();
2145 if (r.isEmpty()) {
2146 return;
2147 }
2149 // calculate the transform to be applied to objects to move them to 0,0
2150 NR::Point move_p = NR::Point(0, sp_document_height(document)) - (r.min() + NR::Point (0, r.extent(NR::Y)));
2151 move_p[NR::Y] = -move_p[NR::Y];
2152 NR::Matrix move = NR::Matrix (NR::translate (move_p));
2154 GSList *items = g_slist_copy((GSList *) selection->itemList());
2156 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2158 // bottommost object, after sorting
2159 SPObject *parent = SP_OBJECT_PARENT (items->data);
2161 NR::Matrix parent_transform = sp_item_i2root_affine(SP_ITEM(parent));
2163 // remember the position of the first item
2164 gint pos = SP_OBJECT_REPR (items->data)->position();
2166 // create a list of duplicates
2167 GSList *repr_copies = NULL;
2168 for (GSList *i = items; i != NULL; i = i->next) {
2169 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate();
2170 repr_copies = g_slist_prepend (repr_copies, dup);
2171 }
2173 NR::Rect bounds(desktop->dt2doc(r.min()), desktop->dt2doc(r.max()));
2175 if (apply) {
2176 // delete objects so that their clones don't get alerted; this object will be restored shortly
2177 for (GSList *i = items; i != NULL; i = i->next) {
2178 SPObject *item = SP_OBJECT (i->data);
2179 item->deleteObject (false);
2180 }
2181 }
2183 // Hack: Temporarily set clone compensation to unmoved, so that we can move clone-originals
2184 // without disturbing clones.
2185 // See ActorAlign::on_button_click() in src/ui/dialog/align-and-distribute.cpp
2186 int saved_compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2187 prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2189 const gchar *pat_id = pattern_tile (repr_copies, bounds, document,
2190 NR::Matrix(NR::translate(desktop->dt2doc(NR::Point(r.min()[NR::X], r.max()[NR::Y])))) * parent_transform.inverse(), parent_transform * move);
2192 // restore compensation setting
2193 prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
2195 if (apply) {
2196 Inkscape::XML::Node *rect = sp_repr_new ("svg:rect");
2197 rect->setAttribute("style", g_strdup_printf("stroke:none;fill:url(#%s)", pat_id));
2199 NR::Point min = bounds.min() * parent_transform.inverse();
2200 NR::Point max = bounds.max() * parent_transform.inverse();
2202 sp_repr_set_svg_double(rect, "width", max[NR::X] - min[NR::X]);
2203 sp_repr_set_svg_double(rect, "height", max[NR::Y] - min[NR::Y]);
2204 sp_repr_set_svg_double(rect, "x", min[NR::X]);
2205 sp_repr_set_svg_double(rect, "y", min[NR::Y]);
2207 // restore parent and position
2208 SP_OBJECT_REPR (parent)->appendChild(rect);
2209 rect->setPosition(pos > 0 ? pos : 0);
2210 SPItem *rectangle = (SPItem *) sp_desktop_document (desktop)->getObjectByRepr(rect);
2212 Inkscape::GC::release(rect);
2214 selection->clear();
2215 selection->set(rectangle);
2216 }
2218 g_slist_free (items);
2220 sp_document_done (document, SP_VERB_EDIT_TILE,
2221 _("Objects to pattern"));
2222 }
2224 void
2225 sp_selection_untile()
2226 {
2227 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2228 if (desktop == NULL)
2229 return;
2231 SPDocument *document = sp_desktop_document(desktop);
2233 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2235 // check if something is selected
2236 if (selection->isEmpty()) {
2237 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object with pattern fill</b> to extract objects from."));
2238 return;
2239 }
2241 GSList *new_select = NULL;
2243 bool did = false;
2245 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2246 items != NULL;
2247 items = items->next) {
2249 SPItem *item = (SPItem *) items->data;
2251 SPStyle *style = SP_OBJECT_STYLE (item);
2253 if (!style || style->fill.type != SP_PAINT_TYPE_PAINTSERVER)
2254 continue;
2256 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
2258 if (!SP_IS_PATTERN(server))
2259 continue;
2261 did = true;
2263 SPPattern *pattern = pattern_getroot (SP_PATTERN (server));
2265 NR::Matrix pat_transform = pattern_patternTransform (SP_PATTERN (server));
2266 pat_transform *= item->transform;
2268 for (SPObject *child = sp_object_first_child(SP_OBJECT(pattern)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2269 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate();
2270 SPItem *i = SP_ITEM (desktop->currentLayer()->appendChildRepr(copy));
2272 // FIXME: relink clones to the new canvas objects
2273 // use SPObject::setid when mental finishes it to steal ids of
2275 // this is needed to make sure the new item has curve (simply requestDisplayUpdate does not work)
2276 sp_document_ensure_up_to_date (document);
2278 NR::Matrix transform( i->transform * pat_transform );
2279 sp_item_write_transform(i, SP_OBJECT_REPR(i), transform);
2281 new_select = g_slist_prepend(new_select, i);
2282 }
2284 SPCSSAttr *css = sp_repr_css_attr_new ();
2285 sp_repr_css_set_property (css, "fill", "none");
2286 sp_repr_css_change (SP_OBJECT_REPR (item), css, "style");
2287 }
2289 if (!did) {
2290 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No pattern fills</b> in the selection."));
2291 } else {
2292 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNTILE,
2293 _("Pattern to objects"));
2294 selection->setList(new_select);
2295 }
2296 }
2298 void
2299 sp_selection_get_export_hints (Inkscape::Selection *selection, const char **filename, float *xdpi, float *ydpi)
2300 {
2301 if (selection->isEmpty()) {
2302 return;
2303 }
2305 const GSList * reprlst = selection->reprList();
2306 bool filename_search = TRUE;
2307 bool xdpi_search = TRUE;
2308 bool ydpi_search = TRUE;
2310 for(; reprlst != NULL &&
2311 filename_search &&
2312 xdpi_search &&
2313 ydpi_search;
2314 reprlst = reprlst->next) {
2315 const gchar * dpi_string;
2316 Inkscape::XML::Node * repr = (Inkscape::XML::Node *)reprlst->data;
2318 if (filename_search) {
2319 *filename = repr->attribute("inkscape:export-filename");
2320 if (*filename != NULL)
2321 filename_search = FALSE;
2322 }
2324 if (xdpi_search) {
2325 dpi_string = NULL;
2326 dpi_string = repr->attribute("inkscape:export-xdpi");
2327 if (dpi_string != NULL) {
2328 *xdpi = atof(dpi_string);
2329 xdpi_search = FALSE;
2330 }
2331 }
2333 if (ydpi_search) {
2334 dpi_string = NULL;
2335 dpi_string = repr->attribute("inkscape:export-ydpi");
2336 if (dpi_string != NULL) {
2337 *ydpi = atof(dpi_string);
2338 ydpi_search = FALSE;
2339 }
2340 }
2341 }
2342 }
2344 void
2345 sp_document_get_export_hints (SPDocument * doc, const char **filename, float *xdpi, float *ydpi)
2346 {
2347 Inkscape::XML::Node * repr = sp_document_repr_root(doc);
2348 const gchar * dpi_string;
2350 *filename = repr->attribute("inkscape:export-filename");
2352 dpi_string = NULL;
2353 dpi_string = repr->attribute("inkscape:export-xdpi");
2354 if (dpi_string != NULL) {
2355 *xdpi = atof(dpi_string);
2356 }
2358 dpi_string = NULL;
2359 dpi_string = repr->attribute("inkscape:export-ydpi");
2360 if (dpi_string != NULL) {
2361 *ydpi = atof(dpi_string);
2362 }
2363 }
2365 void
2366 sp_selection_create_bitmap_copy ()
2367 {
2368 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2369 if (desktop == NULL)
2370 return;
2372 SPDocument *document = sp_desktop_document(desktop);
2374 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2376 // check if something is selected
2377 if (selection->isEmpty()) {
2378 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to make a bitmap copy."));
2379 return;
2380 }
2382 // Get the bounding box of the selection
2383 NRRect bbox;
2384 sp_document_ensure_up_to_date (document);
2385 selection->bounds(&bbox);
2386 if (NR_RECT_DFLS_TEST_EMPTY(&bbox)) {
2387 return; // exceptional situation, so not bother with a translatable error message, just quit quietly
2388 }
2390 // List of the items to show; all others will be hidden
2391 GSList *items = g_slist_copy ((GSList *) selection->itemList());
2393 // Sort items so that the topmost comes last
2394 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
2396 // Generate a random value from the current time (you may create bitmap from the same object(s)
2397 // multiple times, and this is done so that they don't clash)
2398 GTimeVal cu;
2399 g_get_current_time (&cu);
2400 guint current = (int) (cu.tv_sec * 1000000 + cu.tv_usec) % 1024;
2402 // Create the filename
2403 gchar *filename = g_strdup_printf ("%s-%s-%u.png", document->name, SP_OBJECT_REPR(items->data)->attribute("id"), current);
2404 // Imagemagick is known not to handle spaces in filenames, so we replace anything but letters,
2405 // digits, and a few other chars, with "_"
2406 filename = g_strcanon (filename, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.=+~$#@^&!?", '_');
2407 // Build the complete path by adding document->base if set
2408 gchar *filepath = g_build_filename (document->base?document->base:"", filename, NULL);
2410 //g_print ("%s\n", filepath);
2412 // Remember parent and z-order of the topmost one
2413 gint pos = SP_OBJECT_REPR(g_slist_last(items)->data)->position();
2414 SPObject *parent_object = SP_OBJECT_PARENT(g_slist_last(items)->data);
2415 Inkscape::XML::Node *parent = SP_OBJECT_REPR(parent_object);
2417 // Calculate resolution
2418 double res;
2419 int const prefs_res = prefs_get_int_attribute ("options.createbitmap", "resolution", 0);
2420 int const prefs_min = prefs_get_int_attribute ("options.createbitmap", "minsize", 0);
2421 if (0 < prefs_res) {
2422 // If it's given explicitly in prefs, take it
2423 res = prefs_res;
2424 } else if (0 < prefs_min) {
2425 // If minsize is given, look up minimum bitmap size (default 250 pixels) and calculate resolution from it
2426 res = PX_PER_IN * prefs_min / MIN ((bbox.x1 - bbox.x0), (bbox.y1 - bbox.y0));
2427 } else {
2428 float hint_xdpi = 0, hint_ydpi = 0;
2429 const char *hint_filename;
2430 // take resolution hint from the selected objects
2431 sp_selection_get_export_hints (selection, &hint_filename, &hint_xdpi, &hint_ydpi);
2432 if (hint_xdpi != 0) {
2433 res = hint_xdpi;
2434 } else {
2435 // take resolution hint from the document
2436 sp_document_get_export_hints (document, &hint_filename, &hint_xdpi, &hint_ydpi);
2437 if (hint_xdpi != 0) {
2438 res = hint_xdpi;
2439 } else {
2440 // if all else fails, take the default 90 dpi
2441 res = PX_PER_IN;
2442 }
2443 }
2444 }
2446 // The width and height of the bitmap in pixels
2447 unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * res / PX_PER_IN);
2448 unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * res / PX_PER_IN);
2450 // Find out if we have to run a filter
2451 const gchar *run = NULL;
2452 const gchar *filter = prefs_get_string_attribute ("options.createbitmap", "filter");
2453 if (filter) {
2454 // filter command is given;
2455 // see if we have a parameter to pass to it
2456 const gchar *param1 = prefs_get_string_attribute ("options.createbitmap", "filter_param1");
2457 if (param1) {
2458 if (param1[strlen(param1) - 1] == '%') {
2459 // if the param string ends with %, interpret it as a percentage of the image's max dimension
2460 gchar p1[256];
2461 g_ascii_dtostr (p1, 256, ceil (g_ascii_strtod (param1, NULL) * MAX(width, height) / 100));
2462 // the first param is always the image filename, the second is param1
2463 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, p1);
2464 } else {
2465 // otherwise pass the param1 unchanged
2466 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, param1);
2467 }
2468 } else {
2469 // run without extra parameter
2470 run = g_strdup_printf ("%s \"%s\"", filter, filepath);
2471 }
2472 }
2474 // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
2475 NR::Matrix eek = sp_item_i2d_affine (SP_ITEM(parent_object));
2476 NR::Matrix t;
2477 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
2478 t = NR::scale (1, -1) * NR::translate ((unsigned) (bbox.x0 + 0.5), (unsigned) (bbox.y1 + 0.5)) * eek.inverse();
2479 } else {
2480 t = NR::scale (1, -1) * NR::translate (bbox.x0, bbox.y1) * eek.inverse();
2481 }
2483 // Do the export
2484 sp_export_png_file(document, filepath,
2485 bbox.x0, bbox.y0, bbox.x1, bbox.y1,
2486 width, height, res, res,
2487 (guint32) 0xffffff00,
2488 NULL, NULL,
2489 true, /*bool force_overwrite,*/
2490 items);
2492 g_slist_free (items);
2494 // Run filter, if any
2495 if (run) {
2496 g_print ("Running external filter: %s\n", run);
2497 system (run);
2498 }
2500 // Import the image back
2501 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filepath, NULL);
2502 if (pb) {
2503 // Create the repr for the image
2504 Inkscape::XML::Node * repr = sp_repr_new ("svg:image");
2505 repr->setAttribute("xlink:href", filename);
2506 repr->setAttribute("sodipodi:absref", filepath);
2507 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
2508 sp_repr_set_svg_double(repr, "width", width);
2509 sp_repr_set_svg_double(repr, "height", height);
2510 } else {
2511 sp_repr_set_svg_double(repr, "width", (bbox.x1 - bbox.x0));
2512 sp_repr_set_svg_double(repr, "height", (bbox.y1 - bbox.y0));
2513 }
2515 // Write transform
2516 gchar c[256];
2517 if (sp_svg_transform_write(c, 256, t)) {
2518 repr->setAttribute("transform", c);
2519 }
2521 // add the new repr to the parent
2522 parent->appendChild(repr);
2524 // move to the saved position
2525 repr->setPosition(pos > 0 ? pos + 1 : 1);
2527 // Set selection to the new image
2528 selection->clear();
2529 selection->add(repr);
2531 // Clean up
2532 Inkscape::GC::release(repr);
2533 gdk_pixbuf_unref (pb);
2535 // Complete undoable transaction
2536 sp_document_done (document, SP_VERB_SELECTION_CREATE_BITMAP,
2537 _("Create bitmap"));
2538 }
2540 g_free (filename);
2541 g_free (filepath);
2542 }
2544 /**
2545 * \brief sp_selection_set_mask
2546 *
2547 * This function creates a mask or clipPath from selection
2548 * Two different modes:
2549 * if applyToLayer, all selection is moved to DEFS as mask/clippath
2550 * and is applied to current layer
2551 * otherwise, topmost object is used as mask for other objects
2552 * If \a apply_clip_path parameter is true, clipPath is created, otherwise mask
2553 *
2554 */
2555 void
2556 sp_selection_set_mask(bool apply_clip_path, bool apply_to_layer)
2557 {
2558 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2559 if (desktop == NULL)
2560 return;
2562 SPDocument *document = sp_desktop_document(desktop);
2564 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2566 // check if something is selected
2567 bool is_empty = selection->isEmpty();
2568 if ( apply_to_layer && is_empty) {
2569 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to create clippath or mask from."));
2570 return;
2571 } else if (!apply_to_layer && ( is_empty || NULL == selection->itemList()->next )) {
2572 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select mask object and <b>object(s)</b> to apply clippath or mask to."));
2573 return;
2574 }
2576 sp_document_ensure_up_to_date(document);
2578 GSList *items = g_slist_copy((GSList *) selection->itemList());
2580 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2582 // create a list of duplicates
2583 GSList *mask_items = NULL;
2584 GSList *apply_to_items = NULL;
2585 GSList *items_to_delete = NULL;
2586 bool topmost = prefs_get_int_attribute ("options.maskobject", "topmost", 1);
2587 bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
2589 if (apply_to_layer) {
2590 // all selected items are used for mask, which is applied to a layer
2591 apply_to_items = g_slist_prepend (apply_to_items, desktop->currentLayer());
2593 for (GSList *i = items; i != NULL; i = i->next) {
2594 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate();
2595 mask_items = g_slist_prepend (mask_items, dup);
2597 if (remove_original) {
2598 SPObject *item = SP_OBJECT (i->data);
2599 items_to_delete = g_slist_prepend (items_to_delete, item);
2600 }
2601 }
2602 } else if (!topmost) {
2603 // topmost item is used as a mask, which is applied to other items in a selection
2604 GSList *i = items;
2605 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate();
2606 mask_items = g_slist_prepend (mask_items, dup);
2608 if (remove_original) {
2609 SPObject *item = SP_OBJECT (i->data);
2610 items_to_delete = g_slist_prepend (items_to_delete, item);
2611 }
2613 for (i = i->next; i != NULL; i = i->next) {
2614 apply_to_items = g_slist_prepend (apply_to_items, i->data);
2615 }
2616 } else {
2617 GSList *i = NULL;
2618 for (i = items; NULL != i->next; i = i->next) {
2619 apply_to_items = g_slist_prepend (apply_to_items, i->data);
2620 }
2622 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate();
2623 mask_items = g_slist_prepend (mask_items, dup);
2625 if (remove_original) {
2626 SPObject *item = SP_OBJECT (i->data);
2627 items_to_delete = g_slist_prepend (items_to_delete, item);
2628 }
2629 }
2631 g_slist_free (items);
2632 items = NULL;
2634 gchar const* attributeName = apply_clip_path ? "clip-path" : "mask";
2635 for (GSList *i = apply_to_items; NULL != i; i = i->next) {
2636 SPItem *item = reinterpret_cast<SPItem *>(i->data);
2637 // inverted object transform should be applied to a mask object,
2638 // as mask is calculated in user space (after applying transform)
2639 NR::Matrix maskTransform (item->transform.inverse());
2641 GSList *mask_items_dup = NULL;
2642 for (GSList *mask_item = mask_items; NULL != mask_item; mask_item = mask_item->next) {
2643 Inkscape::XML::Node *dup = reinterpret_cast<Inkscape::XML::Node *>(mask_item->data)->duplicate();
2644 mask_items_dup = g_slist_prepend (mask_items_dup, dup);
2645 }
2647 const gchar *mask_id = NULL;
2648 if (apply_clip_path) {
2649 mask_id = sp_clippath_create(mask_items_dup, document, &maskTransform);
2650 } else {
2651 mask_id = sp_mask_create(mask_items_dup, document, &maskTransform);
2652 }
2654 g_slist_free (mask_items_dup);
2655 mask_items_dup = NULL;
2657 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, g_strdup_printf("url(#%s)", mask_id));
2658 }
2660 g_slist_free (mask_items);
2661 g_slist_free (apply_to_items);
2663 for (GSList *i = items_to_delete; NULL != i; i = i->next) {
2664 SPObject *item = SP_OBJECT (i->data);
2665 item->deleteObject (false);
2666 }
2667 g_slist_free (items_to_delete);
2669 if (apply_clip_path)
2670 sp_document_done (document, SP_VERB_OBJECT_SET_CLIPPATH, _("Set clipping path"));
2671 else
2672 sp_document_done (document, SP_VERB_OBJECT_SET_MASK, _("Set mask"));
2673 }
2675 void sp_selection_unset_mask(bool apply_clip_path) {
2676 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2677 if (desktop == NULL)
2678 return;
2680 SPDocument *document = sp_desktop_document(desktop);
2681 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2683 // check if something is selected
2684 if (selection->isEmpty()) {
2685 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to remove clippath or mask from."));
2686 return;
2687 }
2689 bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
2690 sp_document_ensure_up_to_date(document);
2692 gchar const* attributeName = apply_clip_path ? "clip-path" : "mask";
2693 std::map<SPObject*,SPItem*> referenced_objects;
2694 for (GSList const*i = selection->itemList(); NULL != i; i = i->next) {
2695 if (remove_original) {
2696 // remember referenced mask/clippath, so orphaned masks can be moved back to document
2697 SPItem *item = reinterpret_cast<SPItem *>(i->data);
2698 Inkscape::URIReference *uri_ref = NULL;
2700 if (apply_clip_path) {
2701 uri_ref = item->clip_ref;
2702 } else {
2703 uri_ref = item->mask_ref;
2704 }
2706 // collect distinct mask object (and associate with item to apply transform)
2707 if (NULL != uri_ref && NULL != uri_ref->getObject()) {
2708 referenced_objects[uri_ref->getObject()] = item;
2709 }
2710 }
2712 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, "none");
2713 }
2715 // restore mask objects into a document
2716 for ( std::map<SPObject*,SPItem*>::iterator it = referenced_objects.begin() ; it != referenced_objects.end() ; ++it) {
2717 SPObject *obj = (*it).first;
2718 GSList *items_to_move = NULL;
2719 for (SPObject *child = sp_object_first_child(obj) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2720 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate();
2721 items_to_move = g_slist_prepend (items_to_move, copy);
2722 }
2724 if (!obj->isReferenced()) {
2725 // delete from defs if no other object references this mask
2726 obj->deleteObject(false);
2727 }
2729 // remember parent and position of the item to which the clippath/mask was applied
2730 Inkscape::XML::Node *parent = SP_OBJECT_REPR((*it).second)->parent();
2731 gint pos = SP_OBJECT_REPR((*it).second)->position();
2733 for (GSList *i = items_to_move; NULL != i; i = i->next) {
2734 Inkscape::XML::Node *repr = (Inkscape::XML::Node *)i->data;
2736 // insert into parent, restore pos
2737 parent->appendChild(repr);
2738 repr->setPosition((pos + 1) > 0 ? (pos + 1) : 0);
2740 SPItem *mask_item = (SPItem *) sp_desktop_document (desktop)->getObjectByRepr(repr);
2741 selection->add(repr);
2743 // transform mask, so it is moved the same spot where mask was applied
2744 NR::Matrix transform (mask_item->transform);
2745 transform *= (*it).second->transform;
2746 sp_item_write_transform(mask_item, SP_OBJECT_REPR(mask_item), transform);
2747 }
2749 g_slist_free (items_to_move);
2750 }
2752 if (apply_clip_path)
2753 sp_document_done (document, SP_VERB_OBJECT_UNSET_CLIPPATH, _("Release clipping path"));
2754 else
2755 sp_document_done (document, SP_VERB_OBJECT_UNSET_MASK, _("Release mask"));
2756 }
2758 void fit_canvas_to_selection(SPDesktop *desktop) {
2759 g_return_if_fail(desktop != NULL);
2760 SPDocument *doc = sp_desktop_document(desktop);
2762 g_return_if_fail(doc != NULL);
2763 g_return_if_fail(desktop->selection != NULL);
2764 g_return_if_fail(!desktop->selection->isEmpty());
2765 NRRect bbox = {0,0,0,0};
2767 desktop->selection->bounds(&bbox);
2768 if (!empty(bbox)) {
2769 doc->fitToRect(bbox);
2770 }
2771 };
2773 void fit_canvas_to_drawing(SPDocument *doc) {
2774 g_return_if_fail(doc != NULL);
2775 NRRect bbox = {0,0,0,0};
2777 sp_document_ensure_up_to_date (doc);
2778 sp_item_invoke_bbox(SP_ITEM(doc->root), &bbox, sp_item_i2r_affine(SP_ITEM(doc->root)), TRUE);
2780 if (!empty(bbox)) {
2781 doc->fitToRect(bbox);
2782 }
2783 };
2785 void fit_canvas_to_selection_or_drawing(SPDesktop *desktop) {
2786 g_return_if_fail(desktop != NULL);
2787 SPDocument *doc = sp_desktop_document(desktop);
2789 g_return_if_fail(doc != NULL);
2790 g_return_if_fail(desktop->selection != NULL);
2792 if (desktop->selection->isEmpty()) {
2793 fit_canvas_to_drawing(doc);
2794 } else {
2795 fit_canvas_to_selection(desktop);
2796 }
2798 sp_document_done(doc, SP_VERB_FIT_CANVAS_TO_DRAWING,
2799 _("Fit page to selection"));
2800 };
2802 /*
2803 Local Variables:
2804 mode:c++
2805 c-file-style:"stroustrup"
2806 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2807 indent-tabs-mode:nil
2808 fill-column:99
2809 End:
2810 */
2811 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :