07b79b2596047c4fac865df8a49de8c8453105a4
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 "selection-chemistry.h"
25 #include <gtkmm/clipboard.h>
27 #include "svg/svg.h"
28 #include "inkscape.h"
29 #include "desktop.h"
30 #include "desktop-style.h"
31 #include "selection.h"
32 #include "tools-switch.h"
33 #include "desktop-handles.h"
34 #include "message-stack.h"
35 #include "sp-item-transform.h"
36 #include "marker.h"
37 #include "sp-use.h"
38 #include "sp-textpath.h"
39 #include "sp-tspan.h"
40 #include "sp-tref.h"
41 #include "sp-flowtext.h"
42 #include "sp-flowregion.h"
43 #include "text-editing.h"
44 #include "text-context.h"
45 #include "connector-context.h"
46 #include "sp-path.h"
47 #include "sp-conn-end.h"
48 #include "dropper-context.h"
49 #include <glibmm/i18n.h>
50 #include "libnr/nr-matrix-rotate-ops.h"
51 #include "libnr/nr-matrix-translate-ops.h"
52 #include "libnr/nr-rotate-fns.h"
53 #include "libnr/nr-scale-ops.h"
54 #include "libnr/nr-scale-translate-ops.h"
55 #include "libnr/nr-translate-matrix-ops.h"
56 #include "libnr/nr-translate-scale-ops.h"
57 #include "xml/repr.h"
58 #include "style.h"
59 #include "document-private.h"
60 #include "sp-gradient.h"
61 #include "sp-gradient-reference.h"
62 #include "sp-linear-gradient-fns.h"
63 #include "sp-pattern.h"
64 #include "sp-radial-gradient-fns.h"
65 #include "sp-namedview.h"
66 #include "prefs-utils.h"
67 #include "sp-offset.h"
68 #include "sp-clippath.h"
69 #include "sp-mask.h"
70 #include "file.h"
71 #include "helper/png-write.h"
72 #include "layer-fns.h"
73 #include "context-fns.h"
74 #include <map>
75 #include <cstring>
76 #include <string>
77 #include "helper/units.h"
78 #include "sp-item.h"
79 #include "box3d.h"
80 #include "unit-constants.h"
81 #include "xml/simple-document.h"
82 #include "sp-filter-reference.h"
83 #include "gradient-drag.h"
84 #include "uri-references.h"
85 #include "live_effects/lpeobject.h"
86 #include "live_effects/parameter/path.h"
87 #include "libnr/nr-convert2geom.h"
89 using NR::X;
90 using NR::Y;
92 /* fixme: find a better place */
93 Inkscape::XML::Document *clipboard_document = NULL;
94 GSList *clipboard = NULL;
95 GSList *defs_clipboard = NULL;
96 SPCSSAttr *style_clipboard = NULL;
97 NR::Maybe<NR::Rect> size_clipboard;
99 static void sp_copy_stuff_used_by_item(GSList **defs_clip, SPItem *item, GSList const *items, Inkscape::XML::Document* xml_doc);
101 /**
102 * Copies repr and its inherited css style elements, along with the accumulated transform 'full_t',
103 * then prepends the copy to 'clip'.
104 */
105 void sp_selection_copy_one (Inkscape::XML::Node *repr, NR::Matrix full_t, GSList **clip, Inkscape::XML::Document* xml_doc)
106 {
107 Inkscape::XML::Node *copy = repr->duplicate(xml_doc);
109 // copy complete inherited style
110 SPCSSAttr *css = sp_repr_css_attr_inherited(repr, "style");
111 sp_repr_css_set(copy, css, "style");
112 sp_repr_css_attr_unref(css);
114 // write the complete accumulated transform passed to us
115 // (we're dealing with unattached repr, so we write to its attr
116 // instead of using sp_item_set_transform)
117 gchar *affinestr=sp_svg_transform_write(full_t);
118 copy->setAttribute("transform", affinestr);
119 g_free(affinestr);
121 *clip = g_slist_prepend(*clip, copy);
122 }
124 void sp_selection_copy_impl (GSList const *items, GSList **clip, GSList **defs_clip, SPCSSAttr **style_clip, Inkscape::XML::Document* xml_doc)
125 {
127 // Copy stuff referenced by all items to defs_clip:
128 if (defs_clip) {
129 for (GSList *i = (GSList *) items; i != NULL; i = i->next) {
130 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (i->data), items, xml_doc);
131 }
132 *defs_clip = g_slist_reverse(*defs_clip);
133 }
135 // Store style:
136 if (style_clip) {
137 SPItem *item = SP_ITEM (items->data); // take from the first selected item
138 *style_clip = take_style_from_item (item);
139 }
141 if (clip) {
142 // Sort items:
143 GSList *sorted_items = g_slist_copy ((GSList *) items);
144 sorted_items = g_slist_sort((GSList *) sorted_items, (GCompareFunc) sp_object_compare_position);
146 // Copy item reprs:
147 for (GSList *i = (GSList *) sorted_items; i != NULL; i = i->next) {
148 sp_selection_copy_one (SP_OBJECT_REPR (i->data), sp_item_i2doc_affine(SP_ITEM (i->data)), clip, xml_doc);
149 }
151 *clip = g_slist_reverse(*clip);
152 g_slist_free ((GSList *) sorted_items);
153 }
154 }
156 /**
157 * Add gradients/patterns/markers referenced by copied objects to defs.
158 * Iterates through 'defs_clip', and for each item it adds the data
159 * repr into the global defs.
160 */
161 void
162 paste_defs (GSList **defs_clip, SPDocument *doc)
163 {
164 if (!defs_clip)
165 return;
167 for (GSList *gl = *defs_clip; gl != NULL; gl = gl->next) {
168 SPDefs *defs= (SPDefs *) SP_DOCUMENT_DEFS(doc);
169 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) gl->data;
170 gchar const *id = repr->attribute("id");
171 if (!id || !doc->getObjectById(id)) {
172 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
173 Inkscape::XML::Node *copy = repr->duplicate(xml_doc);
174 SP_OBJECT_REPR(defs)->addChild(copy, NULL);
175 Inkscape::GC::release(copy);
176 }
177 }
178 }
180 GSList *sp_selection_paste_impl (SPDocument *doc, SPObject *parent, GSList **clip, GSList **defs_clip)
181 {
182 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
183 paste_defs (defs_clip, doc);
185 GSList *copied = NULL;
186 // add objects to document
187 for (GSList *l = *clip; l != NULL; l = l->next) {
188 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
189 Inkscape::XML::Node *copy = repr->duplicate(xml_doc);
191 // premultiply the item transform by the accumulated parent transform in the paste layer
192 NR::Matrix local = sp_item_i2doc_affine(SP_ITEM(parent));
193 if (!local.test_identity()) {
194 gchar const *t_str = copy->attribute("transform");
195 NR::Matrix item_t (NR::identity());
196 if (t_str)
197 sp_svg_transform_read(t_str, &item_t);
198 item_t *= local.inverse();
199 // (we're dealing with unattached repr, so we write to its attr instead of using sp_item_set_transform)
200 gchar *affinestr=sp_svg_transform_write(item_t);
201 copy->setAttribute("transform", affinestr);
202 g_free(affinestr);
203 }
205 parent->appendChildRepr(copy);
206 copied = g_slist_prepend(copied, copy);
207 Inkscape::GC::release(copy);
208 }
209 return copied;
210 }
212 void sp_selection_delete_impl(GSList const *items, bool propagate = true, bool propagate_descendants = true)
213 {
214 for (GSList const *i = items ; i ; i = i->next ) {
215 sp_object_ref((SPObject *)i->data, NULL);
216 }
217 for (GSList const *i = items; i != NULL; i = i->next) {
218 SPItem *item = (SPItem *) i->data;
219 SP_OBJECT(item)->deleteObject(propagate, propagate_descendants);
220 sp_object_unref((SPObject *)item, NULL);
221 }
222 }
225 void sp_selection_delete()
226 {
227 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
228 if (desktop == NULL) {
229 return;
230 }
232 if (tools_isactive (desktop, TOOLS_TEXT))
233 if (sp_text_delete_selection(desktop->event_context)) {
234 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_TEXT,
235 _("Delete text"));
236 return;
237 }
239 Inkscape::Selection *selection = sp_desktop_selection(desktop);
241 // check if something is selected
242 if (selection->isEmpty()) {
243 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Nothing</b> was deleted."));
244 return;
245 }
247 GSList const *selected = g_slist_copy(const_cast<GSList *>(selection->itemList()));
248 selection->clear();
249 sp_selection_delete_impl (selected);
250 g_slist_free ((GSList *) selected);
252 /* a tool may have set up private information in it's selection context
253 * that depends on desktop items. I think the only sane way to deal with
254 * this currently is to reset the current tool, which will reset it's
255 * associated selection context. For example: deleting an object
256 * while moving it around the canvas.
257 */
258 tools_switch ( desktop, tools_active ( desktop ) );
260 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_DELETE,
261 _("Delete"));
262 }
264 /* fixme: sequencing */
265 void sp_selection_duplicate()
266 {
267 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
268 if (desktop == NULL)
269 return;
271 Inkscape::XML::Document* xml_doc = sp_document_repr_doc(desktop->doc());
272 Inkscape::Selection *selection = sp_desktop_selection(desktop);
274 // check if something is selected
275 if (selection->isEmpty()) {
276 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to duplicate."));
277 return;
278 }
280 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
282 selection->clear();
284 // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
285 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
287 GSList *newsel = NULL;
289 while (reprs) {
290 Inkscape::XML::Node *parent = ((Inkscape::XML::Node *) reprs->data)->parent();
291 Inkscape::XML::Node *copy = ((Inkscape::XML::Node *) reprs->data)->duplicate(xml_doc);
293 parent->appendChild(copy);
295 newsel = g_slist_prepend(newsel, copy);
296 reprs = g_slist_remove(reprs, reprs->data);
297 Inkscape::GC::release(copy);
298 }
300 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_DUPLICATE,
301 _("Duplicate"));
303 selection->setReprList(newsel);
305 g_slist_free(newsel);
306 }
308 void sp_edit_clear_all()
309 {
310 SPDesktop *dt = SP_ACTIVE_DESKTOP;
311 if (!dt)
312 return;
314 SPDocument *doc = sp_desktop_document(dt);
315 sp_desktop_selection(dt)->clear();
317 g_return_if_fail(SP_IS_GROUP(dt->currentLayer()));
318 GSList *items = sp_item_group_item_list(SP_GROUP(dt->currentLayer()));
320 while (items) {
321 SP_OBJECT (items->data)->deleteObject();
322 items = g_slist_remove(items, items->data);
323 }
325 sp_document_done(doc, SP_VERB_EDIT_CLEAR_ALL,
326 _("Delete all"));
327 }
329 GSList *
330 get_all_items (GSList *list, SPObject *from, SPDesktop *desktop, bool onlyvisible, bool onlysensitive, GSList const *exclude)
331 {
332 for (SPObject *child = sp_object_first_child(SP_OBJECT(from)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
333 if (SP_IS_ITEM(child) &&
334 !desktop->isLayer(SP_ITEM(child)) &&
335 (!onlysensitive || !SP_ITEM(child)->isLocked()) &&
336 (!onlyvisible || !desktop->itemIsHidden(SP_ITEM(child))) &&
337 (!exclude || !g_slist_find ((GSList *) exclude, child))
338 )
339 {
340 list = g_slist_prepend (list, SP_ITEM(child));
341 }
343 if (SP_IS_ITEM(child) && desktop->isLayer(SP_ITEM(child))) {
344 list = get_all_items (list, child, desktop, onlyvisible, onlysensitive, exclude);
345 }
346 }
348 return list;
349 }
351 void sp_edit_select_all_full (bool force_all_layers, bool invert)
352 {
353 SPDesktop *dt = SP_ACTIVE_DESKTOP;
354 if (!dt)
355 return;
357 Inkscape::Selection *selection = sp_desktop_selection(dt);
359 g_return_if_fail(SP_IS_GROUP(dt->currentLayer()));
361 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
362 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
363 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
365 GSList *items = NULL;
367 GSList const *exclude = NULL;
368 if (invert) {
369 exclude = selection->itemList();
370 }
372 if (force_all_layers)
373 inlayer = PREFS_SELECTION_ALL;
375 switch (inlayer) {
376 case PREFS_SELECTION_LAYER: {
377 if ( (onlysensitive && SP_ITEM(dt->currentLayer())->isLocked()) ||
378 (onlyvisible && dt->itemIsHidden(SP_ITEM(dt->currentLayer()))) )
379 return;
381 GSList *all_items = sp_item_group_item_list(SP_GROUP(dt->currentLayer()));
383 for (GSList *i = all_items; i; i = i->next) {
384 SPItem *item = SP_ITEM (i->data);
386 if (item && (!onlysensitive || !item->isLocked())) {
387 if (!onlyvisible || !dt->itemIsHidden(item)) {
388 if (!dt->isLayer(item)) {
389 if (!invert || !g_slist_find ((GSList *) exclude, item)) {
390 items = g_slist_prepend (items, item); // leave it in the list
391 }
392 }
393 }
394 }
395 }
397 g_slist_free (all_items);
398 break;
399 }
400 case PREFS_SELECTION_LAYER_RECURSIVE: {
401 items = get_all_items (NULL, dt->currentLayer(), dt, onlyvisible, onlysensitive, exclude);
402 break;
403 }
404 default: {
405 items = get_all_items (NULL, dt->currentRoot(), dt, onlyvisible, onlysensitive, exclude);
406 break;
407 }
408 }
410 selection->setList (items);
412 if (items) {
413 g_slist_free (items);
414 }
415 }
417 void sp_edit_select_all ()
418 {
419 sp_edit_select_all_full (false, false);
420 }
422 void sp_edit_select_all_in_all_layers ()
423 {
424 sp_edit_select_all_full (true, false);
425 }
427 void sp_edit_invert ()
428 {
429 sp_edit_select_all_full (false, true);
430 }
432 void sp_edit_invert_in_all_layers ()
433 {
434 sp_edit_select_all_full (true, true);
435 }
437 void sp_selection_group()
438 {
439 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
440 if (desktop == NULL)
441 return;
443 SPDocument *doc = sp_desktop_document (desktop);
444 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
446 Inkscape::Selection *selection = sp_desktop_selection(desktop);
448 // Check if something is selected.
449 if (selection->isEmpty()) {
450 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>some objects</b> to group."));
451 return;
452 }
454 GSList const *l = (GSList *) selection->reprList();
456 GSList *p = g_slist_copy((GSList *) l);
458 selection->clear();
460 p = g_slist_sort(p, (GCompareFunc) sp_repr_compare_position);
462 // Remember the position and parent of the topmost object.
463 gint topmost = ((Inkscape::XML::Node *) g_slist_last(p)->data)->position();
464 Inkscape::XML::Node *topmost_parent = ((Inkscape::XML::Node *) g_slist_last(p)->data)->parent();
466 Inkscape::XML::Node *group = xml_doc->createElement("svg:g");
468 while (p) {
469 Inkscape::XML::Node *current = (Inkscape::XML::Node *) p->data;
471 if (current->parent() == topmost_parent) {
472 Inkscape::XML::Node *spnew = current->duplicate(xml_doc);
473 sp_repr_unparent(current);
474 group->appendChild(spnew);
475 Inkscape::GC::release(spnew);
476 topmost --; // only reduce count for those items deleted from topmost_parent
477 } else { // move it to topmost_parent first
478 GSList *temp_clip = NULL;
480 // At this point, current may already have no item, due to its being a clone whose original is already moved away
481 // So we copy it artificially calculating the transform from its repr->attr("transform") and the parent transform
482 gchar const *t_str = current->attribute("transform");
483 NR::Matrix item_t (NR::identity());
484 if (t_str)
485 sp_svg_transform_read(t_str, &item_t);
486 item_t *= sp_item_i2doc_affine(SP_ITEM(doc->getObjectByRepr(current->parent())));
487 //FIXME: when moving both clone and original from a transformed group (either by
488 //grouping into another parent, or by cut/paste) the transform from the original's
489 //parent becomes embedded into original itself, and this affects its clones. Fix
490 //this by remembering the transform diffs we write to each item into an array and
491 //then, if this is clone, looking up its original in that array and pre-multiplying
492 //it by the inverse of that original's transform diff.
494 sp_selection_copy_one (current, item_t, &temp_clip, xml_doc);
495 sp_repr_unparent(current);
497 // paste into topmost_parent (temporarily)
498 GSList *copied = sp_selection_paste_impl (doc, doc->getObjectByRepr(topmost_parent), &temp_clip, NULL);
499 if (temp_clip) g_slist_free (temp_clip);
500 if (copied) { // if success,
501 // take pasted object (now in topmost_parent)
502 Inkscape::XML::Node *in_topmost = (Inkscape::XML::Node *) copied->data;
503 // make a copy
504 Inkscape::XML::Node *spnew = in_topmost->duplicate(xml_doc);
505 // remove pasted
506 sp_repr_unparent(in_topmost);
507 // put its copy into group
508 group->appendChild(spnew);
509 Inkscape::GC::release(spnew);
510 g_slist_free (copied);
511 }
512 }
513 p = g_slist_remove(p, current);
514 }
516 // Add the new group to the topmost members' parent
517 topmost_parent->appendChild(group);
519 // Move to the position of the topmost, reduced by the number of items deleted from topmost_parent
520 group->setPosition(topmost + 1);
522 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_GROUP,
523 _("Group"));
525 selection->set(group);
526 Inkscape::GC::release(group);
527 }
529 void sp_selection_ungroup()
530 {
531 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
532 if (desktop == NULL)
533 return;
535 Inkscape::Selection *selection = sp_desktop_selection(desktop);
537 if (selection->isEmpty()) {
538 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>group</b> to ungroup."));
539 return;
540 }
542 GSList *items = g_slist_copy((GSList *) selection->itemList());
543 selection->clear();
545 // Get a copy of current selection.
546 GSList *new_select = NULL;
547 bool ungrouped = false;
548 for (GSList *i = items;
549 i != NULL;
550 i = i->next)
551 {
552 SPItem *group = (SPItem *) i->data;
554 // when ungrouping cloned groups with their originals, some objects that were selected may no more exist due to unlinking
555 if (!SP_IS_OBJECT(group)) {
556 continue;
557 }
559 /* We do not allow ungrouping <svg> etc. (lauris) */
560 if (strcmp(SP_OBJECT_REPR(group)->name(), "svg:g") && strcmp(SP_OBJECT_REPR(group)->name(), "svg:switch")) {
561 // keep the non-group item in the new selection
562 selection->add(group);
563 continue;
564 }
566 GSList *children = NULL;
567 /* This is not strictly required, but is nicer to rely on group ::destroy (lauris) */
568 sp_item_group_ungroup(SP_GROUP(group), &children, false);
569 ungrouped = true;
570 // Add ungrouped items to the new selection.
571 new_select = g_slist_concat(new_select, children);
572 }
574 if (new_select) { // Set new selection.
575 selection->addList(new_select);
576 g_slist_free(new_select);
577 }
578 if (!ungrouped) {
579 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No groups</b> to ungroup in the selection."));
580 }
582 g_slist_free(items);
584 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_UNGROUP,
585 _("Ungroup"));
586 }
588 static SPGroup *
589 sp_item_list_common_parent_group(GSList const *items)
590 {
591 if (!items) {
592 return NULL;
593 }
594 SPObject *parent = SP_OBJECT_PARENT(items->data);
595 /* Strictly speaking this CAN happen, if user selects <svg> from Inkscape::XML editor */
596 if (!SP_IS_GROUP(parent)) {
597 return NULL;
598 }
599 for (items = items->next; items; items = items->next) {
600 if (SP_OBJECT_PARENT(items->data) != parent) {
601 return NULL;
602 }
603 }
605 return SP_GROUP(parent);
606 }
608 /** Finds out the minimum common bbox of the selected items. */
609 static NR::Maybe<NR::Rect>
610 enclose_items(GSList const *items)
611 {
612 g_assert(items != NULL);
614 NR::Maybe<NR::Rect> r = NR::Nothing();
615 for (GSList const *i = items; i; i = i->next) {
616 r = NR::union_bounds(r, sp_item_bbox_desktop((SPItem *) i->data));
617 }
618 return r;
619 }
621 SPObject *
622 prev_sibling(SPObject *child)
623 {
624 SPObject *parent = SP_OBJECT_PARENT(child);
625 if (!SP_IS_GROUP(parent)) {
626 return NULL;
627 }
628 for ( SPObject *i = sp_object_first_child(parent) ; i; i = SP_OBJECT_NEXT(i) ) {
629 if (i->next == child)
630 return i;
631 }
632 return NULL;
633 }
635 void
636 sp_selection_raise()
637 {
638 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
639 if (!desktop)
640 return;
642 Inkscape::Selection *selection = sp_desktop_selection(desktop);
644 GSList const *items = (GSList *) selection->itemList();
645 if (!items) {
646 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise."));
647 return;
648 }
650 SPGroup const *group = sp_item_list_common_parent_group(items);
651 if (!group) {
652 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
653 return;
654 }
656 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
658 /* Construct reverse-ordered list of selected children. */
659 GSList *rev = g_slist_copy((GSList *) items);
660 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
662 // Determine the common bbox of the selected items.
663 NR::Maybe<NR::Rect> selected = enclose_items(items);
665 // Iterate over all objects in the selection (starting from top).
666 if (selected) {
667 while (rev) {
668 SPObject *child = SP_OBJECT(rev->data);
669 // for each selected object, find the next sibling
670 for (SPObject *newref = child->next; newref; newref = newref->next) {
671 // if the sibling is an item AND overlaps our selection,
672 if (SP_IS_ITEM(newref)) {
673 NR::Maybe<NR::Rect> newref_bbox = sp_item_bbox_desktop(SP_ITEM(newref));
674 if ( newref_bbox && selected->intersects(*newref_bbox) ) {
675 // AND if it's not one of our selected objects,
676 if (!g_slist_find((GSList *) items, newref)) {
677 // move the selected object after that sibling
678 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(newref));
679 }
680 break;
681 }
682 }
683 }
684 rev = g_slist_remove(rev, child);
685 }
686 } else {
687 g_slist_free(rev);
688 }
690 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_RAISE,
691 _("Raise"));
692 }
694 void sp_selection_raise_to_top()
695 {
696 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
697 if (desktop == NULL)
698 return;
700 SPDocument *document = sp_desktop_document(desktop);
701 Inkscape::Selection *selection = sp_desktop_selection(desktop);
703 if (selection->isEmpty()) {
704 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise to top."));
705 return;
706 }
708 GSList const *items = (GSList *) selection->itemList();
710 SPGroup const *group = sp_item_list_common_parent_group(items);
711 if (!group) {
712 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
713 return;
714 }
716 GSList *rl = g_slist_copy((GSList *) selection->reprList());
717 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
719 for (GSList *l = rl; l != NULL; l = l->next) {
720 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
721 repr->setPosition(-1);
722 }
724 g_slist_free(rl);
726 sp_document_done(document, SP_VERB_SELECTION_TO_FRONT,
727 _("Raise to top"));
728 }
730 void
731 sp_selection_lower()
732 {
733 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
734 if (desktop == NULL)
735 return;
737 Inkscape::Selection *selection = sp_desktop_selection(desktop);
739 GSList const *items = (GSList *) selection->itemList();
740 if (!items) {
741 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower."));
742 return;
743 }
745 SPGroup const *group = sp_item_list_common_parent_group(items);
746 if (!group) {
747 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
748 return;
749 }
751 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
753 // Determine the common bbox of the selected items.
754 NR::Maybe<NR::Rect> selected = enclose_items(items);
756 /* Construct direct-ordered list of selected children. */
757 GSList *rev = g_slist_copy((GSList *) items);
758 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
759 rev = g_slist_reverse(rev);
761 // Iterate over all objects in the selection (starting from top).
762 if (selected) {
763 while (rev) {
764 SPObject *child = SP_OBJECT(rev->data);
765 // for each selected object, find the prev sibling
766 for (SPObject *newref = prev_sibling(child); newref; newref = prev_sibling(newref)) {
767 // if the sibling is an item AND overlaps our selection,
768 if (SP_IS_ITEM(newref)) {
769 NR::Maybe<NR::Rect> ref_bbox = sp_item_bbox_desktop(SP_ITEM(newref));
770 if ( ref_bbox && selected->intersects(*ref_bbox) ) {
771 // AND if it's not one of our selected objects,
772 if (!g_slist_find((GSList *) items, newref)) {
773 // move the selected object before that sibling
774 SPObject *put_after = prev_sibling(newref);
775 if (put_after)
776 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(put_after));
777 else
778 SP_OBJECT_REPR(child)->setPosition(0);
779 }
780 break;
781 }
782 }
783 }
784 rev = g_slist_remove(rev, child);
785 }
786 } else {
787 g_slist_free(rev);
788 }
790 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_LOWER,
791 _("Lower"));
792 }
794 void sp_selection_lower_to_bottom()
795 {
796 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
797 if (desktop == NULL)
798 return;
800 SPDocument *document = sp_desktop_document(desktop);
801 Inkscape::Selection *selection = sp_desktop_selection(desktop);
803 if (selection->isEmpty()) {
804 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower to bottom."));
805 return;
806 }
808 GSList const *items = (GSList *) selection->itemList();
810 SPGroup const *group = sp_item_list_common_parent_group(items);
811 if (!group) {
812 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
813 return;
814 }
816 GSList *rl;
817 rl = g_slist_copy((GSList *) selection->reprList());
818 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
819 rl = g_slist_reverse(rl);
821 for (GSList *l = rl; l != NULL; l = l->next) {
822 gint minpos;
823 SPObject *pp, *pc;
824 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
825 pp = document->getObjectByRepr(sp_repr_parent(repr));
826 minpos = 0;
827 g_assert(SP_IS_GROUP(pp));
828 pc = sp_object_first_child(pp);
829 while (!SP_IS_ITEM(pc)) {
830 minpos += 1;
831 pc = pc->next;
832 }
833 repr->setPosition(minpos);
834 }
836 g_slist_free(rl);
838 sp_document_done(document, SP_VERB_SELECTION_TO_BACK,
839 _("Lower to bottom"));
840 }
842 void
843 sp_undo(SPDesktop *desktop, SPDocument *)
844 {
845 if (!sp_document_undo(sp_desktop_document(desktop)))
846 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to undo."));
847 }
849 void
850 sp_redo(SPDesktop *desktop, SPDocument *)
851 {
852 if (!sp_document_redo(sp_desktop_document(desktop)))
853 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to redo."));
854 }
856 void sp_selection_cut()
857 {
858 sp_selection_copy();
859 sp_selection_delete();
860 }
862 void sp_copy_gradient (GSList **defs_clip, SPGradient *gradient, Inkscape::XML::Document* xml_doc)
863 {
864 SPGradient *ref = gradient;
866 while (ref) {
867 // climb up the refs, copying each one in the chain
868 Inkscape::XML::Node *grad_repr = SP_OBJECT_REPR(ref)->duplicate(xml_doc);
869 *defs_clip = g_slist_prepend (*defs_clip, grad_repr);
871 ref = ref->ref->getObject();
872 }
873 }
875 void sp_copy_pattern (GSList **defs_clip, SPPattern *pattern, Inkscape::XML::Document* xml_doc)
876 {
877 SPPattern *ref = pattern;
879 while (ref) {
880 // climb up the refs, copying each one in the chain
881 Inkscape::XML::Node *pattern_repr = SP_OBJECT_REPR(ref)->duplicate(xml_doc);
882 *defs_clip = g_slist_prepend (*defs_clip, pattern_repr);
884 // items in the pattern may also use gradients and other patterns, so we need to recurse here as well
885 for (SPObject *child = sp_object_first_child(SP_OBJECT(ref)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
886 if (!SP_IS_ITEM (child))
887 continue;
888 sp_copy_stuff_used_by_item (defs_clip, (SPItem *) child, NULL, xml_doc);
889 }
891 ref = ref->ref->getObject();
892 }
893 }
895 void sp_copy_single (GSList **defs_clip, SPObject *thing, Inkscape::XML::Document* xml_doc)
896 {
897 Inkscape::XML::Node *duplicate_repr = SP_OBJECT_REPR(thing)->duplicate(xml_doc);
898 *defs_clip = g_slist_prepend (*defs_clip, duplicate_repr);
899 }
902 void sp_copy_textpath_path (GSList **defs_clip, SPTextPath *tp, GSList const *items, Inkscape::XML::Document* xml_doc)
903 {
904 SPItem *path = sp_textpath_get_path_item (tp);
905 if (!path)
906 return;
907 if (items && g_slist_find ((GSList *) items, path)) // do not copy it to defs if it is already in the list of items copied
908 return;
909 Inkscape::XML::Node *repr = SP_OBJECT_REPR(path)->duplicate(xml_doc);
910 *defs_clip = g_slist_prepend (*defs_clip, repr);
911 }
913 /**
914 * Copies things like patterns, markers, gradients, etc.
915 */
916 void sp_copy_stuff_used_by_item (GSList **defs_clip, SPItem *item, GSList const *items, Inkscape::XML::Document* xml_doc)
917 {
918 SPStyle *style = SP_OBJECT_STYLE (item);
920 if (style && (style->fill.isPaintserver())) {
921 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
922 if (SP_IS_LINEARGRADIENT (server) || SP_IS_RADIALGRADIENT (server))
923 sp_copy_gradient (defs_clip, SP_GRADIENT(server), xml_doc);
924 if (SP_IS_PATTERN (server))
925 sp_copy_pattern (defs_clip, SP_PATTERN(server), xml_doc);
926 }
928 if (style && (style->stroke.isPaintserver())) {
929 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
930 if (SP_IS_LINEARGRADIENT (server) || SP_IS_RADIALGRADIENT (server))
931 sp_copy_gradient (defs_clip, SP_GRADIENT(server), xml_doc);
932 if (SP_IS_PATTERN (server))
933 sp_copy_pattern (defs_clip, SP_PATTERN(server), xml_doc);
934 }
936 // For shapes, copy all of the shape's markers into defs_clip
937 if (SP_IS_SHAPE (item)) {
938 SPShape *shape = SP_SHAPE (item);
939 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
940 if (shape->marker[i]) {
941 sp_copy_single (defs_clip, SP_OBJECT (shape->marker[i]), xml_doc);
942 }
943 }
945 // For shapes, also copy liveeffect if applicable
946 if (sp_shape_has_path_effect(shape)) {
947 sp_copy_single (defs_clip, SP_OBJECT(sp_shape_get_livepatheffectobject(shape)), xml_doc);
948 }
949 }
951 if (SP_IS_TEXT_TEXTPATH (item)) {
952 sp_copy_textpath_path (defs_clip, SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))), items, xml_doc);
953 }
955 if (item->clip_ref->getObject()) {
956 sp_copy_single (defs_clip, item->clip_ref->getObject(), xml_doc);
957 }
959 if (item->mask_ref->getObject()) {
960 SPObject *mask = item->mask_ref->getObject();
961 sp_copy_single (defs_clip, mask, xml_doc);
962 // recurse into the mask for its gradients etc.
963 for (SPObject *o = SP_OBJECT(mask)->children; o != NULL; o = o->next) {
964 if (SP_IS_ITEM(o))
965 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (o), items, xml_doc);
966 }
967 }
969 if (style->getFilter()) {
970 SPObject *filter = style->getFilter();
971 if (SP_IS_FILTER(filter)) {
972 sp_copy_single (defs_clip, filter, xml_doc);
973 }
974 }
976 // recurse
977 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
978 if (SP_IS_ITEM(o))
979 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (o), items, xml_doc);
980 }
981 }
983 void
984 sp_set_style_clipboard (SPCSSAttr *css)
985 {
986 if (css != NULL) {
987 // clear style clipboard
988 if (style_clipboard) {
989 sp_repr_css_attr_unref (style_clipboard);
990 style_clipboard = NULL;
991 }
992 //sp_repr_css_print (css);
993 style_clipboard = css;
994 }
995 }
997 /**
998 * \pre item != NULL
999 */
1000 SPCSSAttr *
1001 take_style_from_item (SPItem *item)
1002 {
1003 // write the complete cascaded style, context-free
1004 SPCSSAttr *css = sp_css_attr_from_object (SP_OBJECT(item), SP_STYLE_FLAG_ALWAYS);
1005 if (css == NULL)
1006 return NULL;
1008 if ((SP_IS_GROUP(item) && SP_OBJECT(item)->children) ||
1009 (SP_IS_TEXT (item) && SP_OBJECT(item)->children && SP_OBJECT(item)->children->next == NULL)) {
1010 // if this is a text with exactly one tspan child, merge the style of that tspan as well
1011 // If this is a group, merge the style of its topmost (last) child with style
1012 for (SPObject *last_element = item->lastChild(); last_element != NULL; last_element = SP_OBJECT_PREV (last_element)) {
1013 if (SP_OBJECT_STYLE (last_element) != NULL) {
1014 SPCSSAttr *temp = sp_css_attr_from_object (last_element, SP_STYLE_FLAG_IFSET);
1015 if (temp) {
1016 sp_repr_css_merge (css, temp);
1017 sp_repr_css_attr_unref (temp);
1018 }
1019 break;
1020 }
1021 }
1022 }
1023 if (!(SP_IS_TEXT (item) || SP_IS_TSPAN (item) || SP_IS_TREF(item) || SP_IS_STRING (item))) {
1024 // do not copy text properties from non-text objects, it's confusing
1025 css = sp_css_attr_unset_text (css);
1026 }
1028 // FIXME: also transform gradient/pattern fills, by forking? NO, this must be nondestructive
1029 double ex = NR::expansion(sp_item_i2doc_affine(item));
1030 if (ex != 1.0) {
1031 css = sp_css_attr_scale (css, ex);
1032 }
1034 return css;
1035 }
1038 void sp_selection_copy()
1039 {
1040 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1041 if (desktop == NULL)
1042 return;
1044 if (!clipboard_document) {
1045 clipboard_document = new Inkscape::XML::SimpleDocument();
1046 }
1048 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1050 if (tools_isactive (desktop, TOOLS_DROPPER)) {
1051 sp_dropper_context_copy(desktop->event_context);
1052 return; // copied color under cursor, nothing else to do
1053 }
1055 if (desktop->event_context->get_drag() && desktop->event_context->get_drag()->copy()) {
1056 return; // copied selected stop(s), nothing else to do
1057 }
1059 // check if something is selected
1060 if (selection->isEmpty()) {
1061 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing was copied."));
1062 return;
1063 }
1065 GSList const *items = g_slist_copy ((GSList *) selection->itemList());
1067 // 0. Copy text to system clipboard
1068 // FIXME: for non-texts, put serialized Inkscape::XML as text to the clipboard;
1069 //for this sp_repr_write_stream needs to be rewritten with iostream instead of FILE
1070 Glib::ustring text;
1071 if (tools_isactive (desktop, TOOLS_TEXT)) {
1072 text = sp_text_get_selected_text(desktop->event_context);
1073 }
1075 if (text.empty()) {
1076 guint texts = 0;
1077 for (GSList *i = (GSList *) items; i; i = i->next) {
1078 SPItem *item = SP_ITEM (i->data);
1079 if (SP_IS_TEXT (item) || SP_IS_FLOWTEXT(item)) {
1080 if (texts > 0) // if more than one text object is copied, separate them by spaces
1081 text += " ";
1082 gchar *this_text = sp_te_get_string_multiline (item);
1083 if (this_text) {
1084 text += this_text;
1085 g_free(this_text);
1086 }
1087 texts++;
1088 }
1089 }
1090 }
1091 if (!text.empty()) {
1092 Glib::RefPtr<Gtk::Clipboard> refClipboard = Gtk::Clipboard::get();
1093 refClipboard->set_text(text);
1094 }
1096 // clear old defs clipboard
1097 while (defs_clipboard) {
1098 Inkscape::GC::release((Inkscape::XML::Node *) defs_clipboard->data);
1099 defs_clipboard = g_slist_remove (defs_clipboard, defs_clipboard->data);
1100 }
1102 // clear style clipboard
1103 if (style_clipboard) {
1104 sp_repr_css_attr_unref (style_clipboard);
1105 style_clipboard = NULL;
1106 }
1108 //clear main clipboard
1109 while (clipboard) {
1110 Inkscape::GC::release((Inkscape::XML::Node *) clipboard->data);
1111 clipboard = g_slist_remove(clipboard, clipboard->data);
1112 }
1114 sp_selection_copy_impl (items, &clipboard, &defs_clipboard, &style_clipboard, clipboard_document);
1116 if (tools_isactive (desktop, TOOLS_TEXT)) { // take style from cursor/text selection, overwriting the style just set by copy_impl
1117 SPStyle *const query = sp_style_new(SP_ACTIVE_DOCUMENT);
1118 if (sp_desktop_query_style_all (desktop, query)) {
1119 SPCSSAttr *css = sp_css_attr_from_style (query, SP_STYLE_FLAG_ALWAYS);
1120 sp_set_style_clipboard (css);
1121 }
1122 sp_style_unref(query);
1123 }
1125 size_clipboard = selection->bounds();
1127 g_slist_free ((GSList *) items);
1128 }
1131 void sp_selection_copy_lpe_pathparam(Inkscape::LivePathEffect::PathParam * pathparam)
1132 {
1133 if (pathparam == NULL)
1134 return;
1136 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1137 if (desktop == NULL)
1138 return;
1140 if (!clipboard_document) {
1141 clipboard_document = new Inkscape::XML::SimpleDocument();
1142 }
1144 // clear old defs clipboard
1145 while (defs_clipboard) {
1146 Inkscape::GC::release((Inkscape::XML::Node *) defs_clipboard->data);
1147 defs_clipboard = g_slist_remove (defs_clipboard, defs_clipboard->data);
1148 }
1150 // clear style clipboard
1151 if (style_clipboard) {
1152 sp_repr_css_attr_unref (style_clipboard);
1153 style_clipboard = NULL;
1154 }
1156 //clear main clipboard
1157 while (clipboard) {
1158 Inkscape::GC::release((Inkscape::XML::Node *) clipboard->data);
1159 clipboard = g_slist_remove(clipboard, clipboard->data);
1160 }
1162 // make new path node and put svgd as 'd' attribute
1163 Inkscape::XML::Node *newnode = clipboard_document->createElement("svg:path");
1164 gchar * svgd = pathparam->param_writeSVGValue();
1165 newnode->setAttribute("d", svgd);
1166 g_free(svgd);
1168 clipboard = g_slist_prepend(clipboard, newnode);
1170 Geom::Rect bnds = Geom::bounds_exact(*pathparam);
1171 size_clipboard = from_2geom(bnds);
1172 }
1175 //____________________________________________________________________________
1177 /** Paste the bitmap in the clipboard if one is in there.
1178 The bitmap is saved to a PNG file then imported into the document
1180 @return true if a bitmap was detected and pasted; false if no bitmap
1181 */
1182 static bool pastedPicFromClipboard()
1183 {
1184 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1185 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1186 if ( desktop == NULL || doc == NULL)
1187 return false;
1189 Glib::RefPtr<Gtk::Clipboard> refClipboard = Gtk::Clipboard::get();
1190 Glib::RefPtr<Gdk::Pixbuf> pic = refClipboard->wait_for_image();
1192 // Stop if the system clipboard doesn't have a bitmap.
1193 if ( pic == 0 )
1194 {
1195 return false;
1196 } //if
1197 else
1198 {
1199 // Write into a file, then import the file into the document.
1200 // Make a file name based on current time; use the current working dir.
1201 time_t rawtime;
1202 char filename[50];
1203 const char* path;
1205 time ( &rawtime );
1206 strftime (filename,50,"pastedpic_%m%d%Y_%H%M%S.png",localtime( &rawtime ));
1207 path = (char *)prefs_get_string_attribute("dialogs.save_as", "path");
1208 Glib::ustring finalPath = path;
1209 finalPath.append(G_DIR_SEPARATOR_S).append(filename);
1210 pic->save( finalPath, "png" );
1211 file_import(doc, finalPath, NULL);
1213 // Clear the clipboard so that the bitmap in there won't always over
1214 // ride the normal inkscape clipboard.This isn't the ideal solution.
1215 refClipboard->set_text("");
1216 return true;
1217 } //else
1219 return false;
1220 } //pastedPicFromClipboard
1222 //____________________________________________________________________________
1224 void sp_selection_paste(bool in_place)
1225 {
1226 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1228 if (desktop == NULL) {
1229 return;
1230 }
1232 SPDocument *document = sp_desktop_document(desktop);
1234 if (Inkscape::have_viable_layer(desktop, desktop->messageStack()) == false) {
1235 return;
1236 }
1238 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1240 if (tools_isactive (desktop, TOOLS_TEXT)) {
1241 if (sp_text_paste_inline(desktop->event_context))
1242 return; // pasted from system clipboard into text, nothing else to do
1243 }
1245 // check if something is in the clipboard
1247 // Stop if successfully pasted a clipboard bitmap.
1248 if ( pastedPicFromClipboard() )
1249 return;
1252 if (clipboard == NULL) {
1253 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing in the clipboard."));
1254 return;
1255 }
1257 GSList *copied = sp_selection_paste_impl(document, desktop->currentLayer(), &clipboard, &defs_clipboard);
1258 // add pasted objects to selection
1259 selection->setReprList((GSList const *) copied);
1260 g_slist_free (copied);
1262 if (!in_place) {
1263 sp_document_ensure_up_to_date(document);
1265 NR::Maybe<NR::Rect> sel_bbox = selection->bounds();
1266 NR::Point m( desktop->point() );
1267 if (sel_bbox) {
1268 m -= sel_bbox->midpoint();
1269 }
1271 sp_selection_move_relative(selection, m);
1272 }
1274 sp_document_done(document, SP_VERB_EDIT_PASTE, _("Paste"));
1275 }
1277 void sp_selection_paste_style()
1278 {
1279 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1280 if (desktop == NULL) return;
1282 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1284 // check if something is in the clipboard
1285 if (style_clipboard == NULL) {
1286 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the style clipboard."));
1287 return;
1288 }
1290 // check if something is selected
1291 if (selection->isEmpty()) {
1292 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste style to."));
1293 return;
1294 }
1296 paste_defs (&defs_clipboard, sp_desktop_document(desktop));
1298 sp_desktop_set_style (desktop, style_clipboard);
1300 sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_STYLE,
1301 _("Paste style"));
1302 }
1304 void sp_selection_paste_livepatheffect()
1305 {
1306 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1307 if (desktop == NULL) return;
1309 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1311 // check if something is in the clipboard
1312 if (clipboard == NULL) {
1313 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1314 return;
1315 }
1317 // check if something is selected
1318 if (selection->isEmpty()) {
1319 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste live path effect to."));
1320 return;
1321 }
1323 SPDocument *doc = sp_desktop_document(desktop);
1324 paste_defs (&defs_clipboard, doc);
1326 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) clipboard->data;
1327 char const *effecturi = repr->attribute("inkscape:path-effect");
1328 if (!effecturi) {
1329 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Clipboard does not contain a live path effect."));
1330 return;
1331 }
1333 for ( GSList const *itemlist = selection->itemList(); itemlist != NULL; itemlist = g_slist_next(itemlist) ) {
1334 SPItem *item = reinterpret_cast<SPItem*>(itemlist->data);
1335 if ( item && SP_IS_SHAPE(item) ) {
1336 SPShape * shape = SP_SHAPE(item);
1338 // create a private LPE object!
1339 SPObject * obj = sp_uri_reference_resolve(doc, effecturi);
1340 LivePathEffectObject * lpeobj = LIVEPATHEFFECT(obj)->fork_private_if_necessary(0);
1342 sp_shape_set_path_effect(shape, lpeobj);
1344 // set inkscape:original-d for paths. the other shapes don't need this.
1345 if ( SP_IS_PATH(item) ) {
1346 Inkscape::XML::Node *pathrepr = SP_OBJECT_REPR(item);
1347 if ( ! pathrepr->attribute("inkscape:original-d") ) {
1348 pathrepr->setAttribute("inkscape:original-d", pathrepr->attribute("d"));
1349 }
1350 }
1351 }
1352 }
1354 sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_LIVEPATHEFFECT,
1355 _("Paste live path effect"));
1356 }
1358 void sp_selection_paste_size (bool apply_x, bool apply_y)
1359 {
1360 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1361 if (desktop == NULL) return;
1363 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1365 // check if something is in the clipboard
1366 if (!size_clipboard) {
1367 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1368 return;
1369 }
1371 // check if something is selected
1372 if (selection->isEmpty()) {
1373 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste size to."));
1374 return;
1375 }
1377 NR::Maybe<NR::Rect> current = selection->bounds();
1378 if ( !current || current->isEmpty() ) {
1379 return;
1380 }
1382 double scale_x = size_clipboard->extent(NR::X) / current->extent(NR::X);
1383 double scale_y = size_clipboard->extent(NR::Y) / current->extent(NR::Y);
1385 sp_selection_scale_relative (selection, current->midpoint(),
1386 NR::scale(
1387 apply_x? scale_x : (desktop->isToolboxButtonActive ("lock")? scale_y : 1.0),
1388 apply_y? scale_y : (desktop->isToolboxButtonActive ("lock")? scale_x : 1.0)));
1390 sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_SIZE,
1391 _("Paste size"));
1392 }
1394 void sp_selection_paste_size_separately (bool apply_x, bool apply_y)
1395 {
1396 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1397 if (desktop == NULL) return;
1399 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1401 // check if something is in the clipboard
1402 if ( !size_clipboard ) {
1403 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1404 return;
1405 }
1407 // check if something is selected
1408 if (selection->isEmpty()) {
1409 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste size to."));
1410 return;
1411 }
1413 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1414 SPItem *item = SP_ITEM(l->data);
1416 NR::Maybe<NR::Rect> current = sp_item_bbox_desktop(item);
1417 if ( !current || current->isEmpty() ) {
1418 continue;
1419 }
1421 double scale_x = size_clipboard->extent(NR::X) / current->extent(NR::X);
1422 double scale_y = size_clipboard->extent(NR::Y) / current->extent(NR::Y);
1424 sp_item_scale_rel (item,
1425 NR::scale(
1426 apply_x? scale_x : (desktop->isToolboxButtonActive ("lock")? scale_y : 1.0),
1427 apply_y? scale_y : (desktop->isToolboxButtonActive ("lock")? scale_x : 1.0)));
1429 }
1431 sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_SIZE_SEPARATELY,
1432 _("Paste size separately"));
1433 }
1435 void sp_selection_to_next_layer ()
1436 {
1437 SPDesktop *dt = SP_ACTIVE_DESKTOP;
1439 Inkscape::Selection *selection = sp_desktop_selection(dt);
1441 // check if something is selected
1442 if (selection->isEmpty()) {
1443 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer above."));
1444 return;
1445 }
1447 GSList const *items = g_slist_copy ((GSList *) selection->itemList());
1449 bool no_more = false; // Set to true, if no more layers above
1450 SPObject *next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer());
1451 if (next) {
1452 GSList *temp_clip = NULL;
1453 sp_selection_copy_impl (items, &temp_clip, NULL, NULL, sp_document_repr_doc(dt->doc())); // we're in the same doc, so no need to copy defs
1454 sp_selection_delete_impl (items, false, false);
1455 next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
1456 GSList *copied;
1457 if(next) {
1458 copied = sp_selection_paste_impl (sp_desktop_document (dt), next, &temp_clip, NULL);
1459 } else {
1460 copied = sp_selection_paste_impl (sp_desktop_document (dt), dt->currentLayer(), &temp_clip, NULL);
1461 no_more = true;
1462 }
1463 selection->setReprList((GSList const *) copied);
1464 g_slist_free (copied);
1465 if (temp_clip) g_slist_free (temp_clip);
1466 if (next) dt->setCurrentLayer(next);
1467 sp_document_done(sp_desktop_document (dt), SP_VERB_LAYER_MOVE_TO_NEXT,
1468 _("Raise to next layer"));
1469 } else {
1470 no_more = true;
1471 }
1473 if (no_more) {
1474 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers above."));
1475 }
1477 g_slist_free ((GSList *) items);
1478 }
1480 void sp_selection_to_prev_layer ()
1481 {
1482 SPDesktop *dt = SP_ACTIVE_DESKTOP;
1484 Inkscape::Selection *selection = sp_desktop_selection(dt);
1486 // check if something is selected
1487 if (selection->isEmpty()) {
1488 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer below."));
1489 return;
1490 }
1492 GSList const *items = g_slist_copy ((GSList *) selection->itemList());
1494 bool no_more = false; // Set to true, if no more layers below
1495 SPObject *next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer());
1496 if (next) {
1497 GSList *temp_clip = NULL;
1498 sp_selection_copy_impl (items, &temp_clip, NULL, NULL, sp_document_repr_doc(dt->doc())); // we're in the same doc, so no need to copy defs
1499 sp_selection_delete_impl (items, false, false);
1500 next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
1501 GSList *copied;
1502 if(next) {
1503 copied = sp_selection_paste_impl (sp_desktop_document (dt), next, &temp_clip, NULL);
1504 } else {
1505 copied = sp_selection_paste_impl (sp_desktop_document (dt), dt->currentLayer(), &temp_clip, NULL);
1506 no_more = true;
1507 }
1508 selection->setReprList((GSList const *) copied);
1509 g_slist_free (copied);
1510 if (temp_clip) g_slist_free (temp_clip);
1511 if (next) dt->setCurrentLayer(next);
1512 sp_document_done(sp_desktop_document (dt), SP_VERB_LAYER_MOVE_TO_PREV,
1513 _("Lower to previous layer"));
1514 } else {
1515 no_more = true;
1516 }
1518 if (no_more) {
1519 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers below."));
1520 }
1522 g_slist_free ((GSList *) items);
1523 }
1525 bool
1526 selection_contains_original (SPItem *item, Inkscape::Selection *selection)
1527 {
1528 bool contains_original = false;
1530 bool is_use = SP_IS_USE(item);
1531 SPItem *item_use = item;
1532 SPItem *item_use_first = item;
1533 while (is_use && item_use && !contains_original)
1534 {
1535 item_use = sp_use_get_original (SP_USE(item_use));
1536 contains_original |= selection->includes(item_use);
1537 if (item_use == item_use_first)
1538 break;
1539 is_use = SP_IS_USE(item_use);
1540 }
1542 // If it's a tref, check whether the object containing the character
1543 // data is part of the selection
1544 if (!contains_original && SP_IS_TREF(item)) {
1545 contains_original = selection->includes(SP_TREF(item)->getObjectReferredTo());
1546 }
1548 return contains_original;
1549 }
1552 bool
1553 selection_contains_both_clone_and_original (Inkscape::Selection *selection)
1554 {
1555 bool clone_with_original = false;
1556 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1557 SPItem *item = SP_ITEM(l->data);
1558 clone_with_original |= selection_contains_original(item, selection);
1559 if (clone_with_original)
1560 break;
1561 }
1562 return clone_with_original;
1563 }
1566 /** Apply matrix to the selection. \a set_i2d is normally true, which means objects are in the
1567 original transform, synced with their reprs, and need to jump to the new transform in one go. A
1568 value of set_i2d==false is only used by seltrans when it's dragging objects live (not outlines); in
1569 that case, items are already in the new position, but the repr is in the old, and this function
1570 then simply updates the repr from item->transform.
1571 */
1572 void sp_selection_apply_affine(Inkscape::Selection *selection, NR::Matrix const &affine, bool set_i2d)
1573 {
1574 if (selection->isEmpty())
1575 return;
1577 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1578 SPItem *item = SP_ITEM(l->data);
1580 NR::Point old_center(0,0);
1581 if (set_i2d && item->isCenterSet())
1582 old_center = item->getCenter();
1584 #if 0 /* Re-enable this once persistent guides have a graphical indication.
1585 At the time of writing, this is the only place to re-enable. */
1586 sp_item_update_cns(*item, selection->desktop());
1587 #endif
1589 // we're moving both a clone and its original or any ancestor in clone chain?
1590 bool transform_clone_with_original = selection_contains_original(item, selection);
1591 // ...both a text-on-path and its path?
1592 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)))) ));
1593 // ...both a flowtext and its frame?
1594 bool transform_flowtext_with_frame = (SP_IS_FLOWTEXT(item) && selection->includes( SP_FLOWTEXT(item)->get_frame (NULL))); // (only the first frame is checked so far)
1595 // ...both an offset and its source?
1596 bool transform_offset_with_source = (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) && selection->includes( sp_offset_get_source (SP_OFFSET(item)) );
1598 // If we're moving a connector, we want to detach it
1599 // from shapes that aren't part of the selection, but
1600 // leave it attached if they are
1601 if (cc_item_is_connector(item)) {
1602 SPItem *attItem[2];
1603 SP_PATH(item)->connEndPair.getAttachedItems(attItem);
1605 for (int n = 0; n < 2; ++n) {
1606 if (!selection->includes(attItem[n])) {
1607 sp_conn_end_detach(item, n);
1608 }
1609 }
1610 }
1612 // "clones are unmoved when original is moved" preference
1613 int compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
1614 bool prefs_unmoved = (compensation == SP_CLONE_COMPENSATION_UNMOVED);
1615 bool prefs_parallel = (compensation == SP_CLONE_COMPENSATION_PARALLEL);
1617 // If this is a clone and it's selected along with its original, do not move it; it will feel the
1618 // transform of its original and respond to it itself. Without this, a clone is doubly
1619 // transformed, very unintuitive.
1620 // Same for textpath if we are also doing ANY transform to its path: do not touch textpath,
1621 // letters cannot be squeezed or rotated anyway, they only refill the changed path.
1622 // Same for linked offset if we are also moving its source: do not move it.
1623 if (transform_textpath_with_path || transform_offset_with_source) {
1624 // restore item->transform field from the repr, in case it was changed by seltrans
1625 sp_object_read_attr (SP_OBJECT (item), "transform");
1627 } else if (transform_flowtext_with_frame) {
1628 // apply the inverse of the region's transform to the <use> so that the flow remains
1629 // the same (even though the output itself gets transformed)
1630 for (SPObject *region = item->firstChild() ; region ; region = SP_OBJECT_NEXT(region)) {
1631 if (!SP_IS_FLOWREGION(region) && !SP_IS_FLOWREGIONEXCLUDE(region))
1632 continue;
1633 for (SPObject *use = region->firstChild() ; use ; use = SP_OBJECT_NEXT(use)) {
1634 if (!SP_IS_USE(use)) continue;
1635 sp_item_write_transform(SP_USE(use), SP_OBJECT_REPR(use), item->transform.inverse(), NULL);
1636 }
1637 }
1638 } else if (transform_clone_with_original) {
1639 // We are transforming a clone along with its original. The below matrix juggling is
1640 // necessary to ensure that they transform as a whole, i.e. the clone's induced
1641 // transform and its move compensation are both cancelled out.
1643 // restore item->transform field from the repr, in case it was changed by seltrans
1644 sp_object_read_attr (SP_OBJECT (item), "transform");
1646 // calculate the matrix we need to apply to the clone to cancel its induced transform from its original
1647 NR::Matrix parent_transform = sp_item_i2root_affine(SP_ITEM(SP_OBJECT_PARENT (item)));
1648 NR::Matrix t = parent_transform * matrix_to_desktop (matrix_from_desktop (affine, item), item) * parent_transform.inverse();
1649 NR::Matrix t_inv =parent_transform * matrix_to_desktop (matrix_from_desktop (affine.inverse(), item), item) * parent_transform.inverse();
1650 NR::Matrix result = t_inv * item->transform * t;
1652 if ((prefs_parallel || prefs_unmoved) && affine.is_translation()) {
1653 // we need to cancel out the move compensation, too
1655 // find out the clone move, same as in sp_use_move_compensate
1656 NR::Matrix parent = sp_use_get_parent_transform (SP_USE(item));
1657 NR::Matrix clone_move = parent.inverse() * t * parent;
1659 if (prefs_parallel) {
1660 NR::Matrix move = result * clone_move * t_inv;
1661 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &move);
1663 } else if (prefs_unmoved) {
1664 //if (SP_IS_USE(sp_use_get_original(SP_USE(item))))
1665 // clone_move = NR::identity();
1666 NR::Matrix move = result * clone_move;
1667 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &t);
1668 }
1670 } else {
1671 // just apply the result
1672 sp_item_write_transform(item, SP_OBJECT_REPR(item), result, &t);
1673 }
1675 } else {
1676 if (set_i2d) {
1677 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * affine);
1678 }
1679 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, NULL);
1680 }
1682 // if we're moving the actual object, not just updating the repr, we can transform the
1683 // center by the same matrix (only necessary for non-translations)
1684 if (set_i2d && item->isCenterSet() && !affine.is_translation()) {
1685 item->setCenter(old_center * affine);
1686 SP_OBJECT(item)->updateRepr();
1687 }
1688 }
1689 }
1691 void sp_selection_remove_transform()
1692 {
1693 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1694 if (desktop == NULL)
1695 return;
1697 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1699 GSList const *l = (GSList *) selection->reprList();
1700 while (l != NULL) {
1701 sp_repr_set_attr((Inkscape::XML::Node*)l->data, "transform", NULL);
1702 l = l->next;
1703 }
1705 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_FLATTEN,
1706 _("Remove transform"));
1707 }
1709 void
1710 sp_selection_scale_absolute(Inkscape::Selection *selection,
1711 double const x0, double const x1,
1712 double const y0, double const y1)
1713 {
1714 if (selection->isEmpty())
1715 return;
1717 NR::Maybe<NR::Rect> const bbox(selection->bounds());
1718 if ( !bbox || bbox->isEmpty() ) {
1719 return;
1720 }
1722 NR::translate const p2o(-bbox->min());
1724 NR::scale const newSize(x1 - x0,
1725 y1 - y0);
1726 NR::scale const scale( newSize / NR::scale(bbox->dimensions()) );
1727 NR::translate const o2n(x0, y0);
1728 NR::Matrix const final( p2o * scale * o2n );
1730 sp_selection_apply_affine(selection, final);
1731 }
1734 void sp_selection_scale_relative(Inkscape::Selection *selection, NR::Point const &align, NR::scale const &scale)
1735 {
1736 if (selection->isEmpty())
1737 return;
1739 NR::Maybe<NR::Rect> const bbox(selection->bounds());
1741 if ( !bbox || bbox->isEmpty() ) {
1742 return;
1743 }
1745 // FIXME: ARBITRARY LIMIT: don't try to scale above 1 Mpx, it won't display properly and will crash sooner or later anyway
1746 if ( bbox->extent(NR::X) * scale[NR::X] > 1e6 ||
1747 bbox->extent(NR::Y) * scale[NR::Y] > 1e6 )
1748 {
1749 return;
1750 }
1752 NR::translate const n2d(-align);
1753 NR::translate const d2n(align);
1754 NR::Matrix const final( n2d * scale * d2n );
1755 sp_selection_apply_affine(selection, final);
1756 }
1758 void
1759 sp_selection_rotate_relative(Inkscape::Selection *selection, NR::Point const ¢er, gdouble const angle_degrees)
1760 {
1761 NR::translate const d2n(center);
1762 NR::translate const n2d(-center);
1763 NR::rotate const rotate(rotate_degrees(angle_degrees));
1764 NR::Matrix const final( NR::Matrix(n2d) * rotate * d2n );
1765 sp_selection_apply_affine(selection, final);
1766 }
1768 void
1769 sp_selection_skew_relative(Inkscape::Selection *selection, NR::Point const &align, double dx, double dy)
1770 {
1771 NR::translate const d2n(align);
1772 NR::translate const n2d(-align);
1773 NR::Matrix const skew(1, dy,
1774 dx, 1,
1775 0, 0);
1776 NR::Matrix const final( n2d * skew * d2n );
1777 sp_selection_apply_affine(selection, final);
1778 }
1780 void sp_selection_move_relative(Inkscape::Selection *selection, NR::Point const &move)
1781 {
1782 sp_selection_apply_affine(selection, NR::Matrix(NR::translate(move)));
1783 }
1785 void sp_selection_move_relative(Inkscape::Selection *selection, double dx, double dy)
1786 {
1787 sp_selection_apply_affine(selection, NR::Matrix(NR::translate(dx, dy)));
1788 }
1791 /**
1792 * \brief sp_selection_rotate_90
1793 *
1794 * This function rotates selected objects 90 degrees clockwise.
1795 *
1796 */
1798 void sp_selection_rotate_90_cw()
1799 {
1800 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1802 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1804 if (selection->isEmpty())
1805 return;
1807 GSList const *l = selection->itemList();
1808 NR::rotate const rot_neg_90(NR::Point(0, -1));
1809 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1810 SPItem *item = SP_ITEM(l2->data);
1811 sp_item_rotate_rel(item, rot_neg_90);
1812 }
1814 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_ROTATE_90_CCW,
1815 _("Rotate 90° CW"));
1816 }
1819 /**
1820 * \brief sp_selection_rotate_90_ccw
1821 *
1822 * This function rotates selected objects 90 degrees counter-clockwise.
1823 *
1824 */
1826 void sp_selection_rotate_90_ccw()
1827 {
1828 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1830 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1832 if (selection->isEmpty())
1833 return;
1835 GSList const *l = selection->itemList();
1836 NR::rotate const rot_neg_90(NR::Point(0, 1));
1837 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1838 SPItem *item = SP_ITEM(l2->data);
1839 sp_item_rotate_rel(item, rot_neg_90);
1840 }
1842 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_ROTATE_90_CW,
1843 _("Rotate 90° CCW"));
1844 }
1846 void
1847 sp_selection_rotate(Inkscape::Selection *selection, gdouble const angle_degrees)
1848 {
1849 if (selection->isEmpty())
1850 return;
1852 NR::Maybe<NR::Point> center = selection->center();
1853 if (!center) {
1854 return;
1855 }
1857 sp_selection_rotate_relative(selection, *center, angle_degrees);
1859 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1860 ( ( angle_degrees > 0 )
1861 ? "selector:rotate:ccw"
1862 : "selector:rotate:cw" ),
1863 SP_VERB_CONTEXT_SELECT,
1864 _("Rotate"));
1865 }
1867 /**
1868 \param angle the angle in "angular pixels", i.e. how many visible pixels must move the outermost point of the rotated object
1869 */
1870 void
1871 sp_selection_rotate_screen(Inkscape::Selection *selection, gdouble angle)
1872 {
1873 if (selection->isEmpty())
1874 return;
1876 NR::Maybe<NR::Rect> const bbox(selection->bounds());
1877 NR::Maybe<NR::Point> center = selection->center();
1879 if ( !bbox || !center ) {
1880 return;
1881 }
1883 gdouble const zoom = selection->desktop()->current_zoom();
1884 gdouble const zmove = angle / zoom;
1885 gdouble const r = NR::L2(bbox->cornerFarthestFrom(*center) - *center);
1887 gdouble const zangle = 180 * atan2(zmove, r) / M_PI;
1889 sp_selection_rotate_relative(selection, *center, zangle);
1891 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1892 ( (angle > 0)
1893 ? "selector:rotate:ccw"
1894 : "selector:rotate:cw" ),
1895 SP_VERB_CONTEXT_SELECT,
1896 _("Rotate by pixels"));
1897 }
1899 void
1900 sp_selection_scale(Inkscape::Selection *selection, gdouble grow)
1901 {
1902 if (selection->isEmpty())
1903 return;
1905 NR::Maybe<NR::Rect> const bbox(selection->bounds());
1906 if (!bbox) {
1907 return;
1908 }
1910 NR::Point const center(bbox->midpoint());
1912 // you can't scale "do nizhe pola" (below zero)
1913 double const max_len = bbox->maxExtent();
1914 if ( max_len + grow <= 1e-3 ) {
1915 return;
1916 }
1918 double const times = 1.0 + grow / max_len;
1919 sp_selection_scale_relative(selection, center, NR::scale(times, times));
1921 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1922 ( (grow > 0)
1923 ? "selector:scale:larger"
1924 : "selector:scale:smaller" ),
1925 SP_VERB_CONTEXT_SELECT,
1926 _("Scale"));
1927 }
1929 void
1930 sp_selection_scale_screen(Inkscape::Selection *selection, gdouble grow_pixels)
1931 {
1932 sp_selection_scale(selection,
1933 grow_pixels / selection->desktop()->current_zoom());
1934 }
1936 void
1937 sp_selection_scale_times(Inkscape::Selection *selection, gdouble times)
1938 {
1939 if (selection->isEmpty())
1940 return;
1942 NR::Maybe<NR::Rect> sel_bbox = selection->bounds();
1944 if (!sel_bbox) {
1945 return;
1946 }
1948 NR::Point const center(sel_bbox->midpoint());
1949 sp_selection_scale_relative(selection, center, NR::scale(times, times));
1950 sp_document_done(sp_desktop_document(selection->desktop()), SP_VERB_CONTEXT_SELECT,
1951 _("Scale by whole factor"));
1952 }
1954 void
1955 sp_selection_move(gdouble dx, gdouble dy)
1956 {
1957 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1958 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1959 if (selection->isEmpty()) {
1960 return;
1961 }
1963 sp_selection_move_relative(selection, dx, dy);
1965 if (dx == 0) {
1966 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical", SP_VERB_CONTEXT_SELECT,
1967 _("Move vertically"));
1968 } else if (dy == 0) {
1969 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal", SP_VERB_CONTEXT_SELECT,
1970 _("Move horizontally"));
1971 } else {
1972 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SELECT,
1973 _("Move"));
1974 }
1975 }
1977 void
1978 sp_selection_move_screen(gdouble dx, gdouble dy)
1979 {
1980 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1982 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1983 if (selection->isEmpty()) {
1984 return;
1985 }
1987 // same as sp_selection_move but divide deltas by zoom factor
1988 gdouble const zoom = desktop->current_zoom();
1989 gdouble const zdx = dx / zoom;
1990 gdouble const zdy = dy / zoom;
1991 sp_selection_move_relative(selection, zdx, zdy);
1993 if (dx == 0) {
1994 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical", SP_VERB_CONTEXT_SELECT,
1995 _("Move vertically by pixels"));
1996 } else if (dy == 0) {
1997 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal", SP_VERB_CONTEXT_SELECT,
1998 _("Move horizontally by pixels"));
1999 } else {
2000 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SELECT,
2001 _("Move"));
2002 }
2003 }
2005 namespace {
2007 template <typename D>
2008 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
2009 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
2011 template <typename D>
2012 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items, SPObject *root,
2013 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
2015 struct Forward {
2016 typedef SPObject *Iterator;
2018 static Iterator children(SPObject *o) { return sp_object_first_child(o); }
2019 static Iterator siblings_after(SPObject *o) { return SP_OBJECT_NEXT(o); }
2020 static void dispose(Iterator /*i*/) {}
2022 static SPObject *object(Iterator i) { return i; }
2023 static Iterator next(Iterator i) { return SP_OBJECT_NEXT(i); }
2024 };
2026 struct Reverse {
2027 typedef GSList *Iterator;
2029 static Iterator children(SPObject *o) {
2030 return make_list(o->firstChild(), NULL);
2031 }
2032 static Iterator siblings_after(SPObject *o) {
2033 return make_list(SP_OBJECT_PARENT(o)->firstChild(), o);
2034 }
2035 static void dispose(Iterator i) {
2036 g_slist_free(i);
2037 }
2039 static SPObject *object(Iterator i) {
2040 return reinterpret_cast<SPObject *>(i->data);
2041 }
2042 static Iterator next(Iterator i) { return i->next; }
2044 private:
2045 static GSList *make_list(SPObject *object, SPObject *limit) {
2046 GSList *list=NULL;
2047 while ( object != limit ) {
2048 list = g_slist_prepend(list, object);
2049 object = SP_OBJECT_NEXT(object);
2050 }
2051 return list;
2052 }
2053 };
2055 }
2057 void
2058 sp_selection_item_next(void)
2059 {
2060 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2061 g_return_if_fail(desktop != NULL);
2062 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2064 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
2065 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
2066 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
2068 SPObject *root;
2069 if (PREFS_SELECTION_ALL != inlayer) {
2070 root = selection->activeContext();
2071 } else {
2072 root = desktop->currentRoot();
2073 }
2075 SPItem *item=next_item_from_list<Forward>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
2077 if (item) {
2078 selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
2079 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
2080 scroll_to_show_item(desktop, item);
2081 }
2082 }
2083 }
2085 void
2086 sp_selection_item_prev(void)
2087 {
2088 SPDocument *document = SP_ACTIVE_DOCUMENT;
2089 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2090 g_return_if_fail(document != NULL);
2091 g_return_if_fail(desktop != NULL);
2092 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2094 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
2095 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
2096 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
2098 SPObject *root;
2099 if (PREFS_SELECTION_ALL != inlayer) {
2100 root = selection->activeContext();
2101 } else {
2102 root = desktop->currentRoot();
2103 }
2105 SPItem *item=next_item_from_list<Reverse>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
2107 if (item) {
2108 selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
2109 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
2110 scroll_to_show_item(desktop, item);
2111 }
2112 }
2113 }
2115 void sp_selection_next_patheffect_param(SPDesktop * dt)
2116 {
2117 if (!dt) return;
2119 Inkscape::Selection *selection = sp_desktop_selection(dt);
2120 if ( selection && !selection->isEmpty() ) {
2121 SPItem *item = selection->singleItem();
2122 if ( item && SP_IS_SHAPE(item)) {
2123 SPShape *shape = SP_SHAPE(item);
2124 if (sp_shape_has_path_effect(shape)) {
2125 sp_shape_edit_next_param_oncanvas(shape, dt);
2126 } else {
2127 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("The selection has no applied path effect."));
2128 }
2129 }
2130 }
2131 }
2133 namespace {
2135 template <typename D>
2136 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items,
2137 SPObject *root, bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
2138 {
2139 SPObject *current=root;
2140 while (items) {
2141 SPItem *item=SP_ITEM(items->data);
2142 if ( root->isAncestorOf(item) &&
2143 ( !only_in_viewport || desktop->isWithinViewport(item) ) )
2144 {
2145 current = item;
2146 break;
2147 }
2148 items = items->next;
2149 }
2151 GSList *path=NULL;
2152 while ( current != root ) {
2153 path = g_slist_prepend(path, current);
2154 current = SP_OBJECT_PARENT(current);
2155 }
2157 SPItem *next;
2158 // first, try from the current object
2159 next = next_item<D>(desktop, path, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
2160 g_slist_free(path);
2162 if (!next) { // if we ran out of objects, start over at the root
2163 next = next_item<D>(desktop, NULL, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
2164 }
2166 return next;
2167 }
2169 template <typename D>
2170 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
2171 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
2172 {
2173 typename D::Iterator children;
2174 typename D::Iterator iter;
2176 SPItem *found=NULL;
2178 if (path) {
2179 SPObject *object=reinterpret_cast<SPObject *>(path->data);
2180 g_assert(SP_OBJECT_PARENT(object) == root);
2181 if (desktop->isLayer(object)) {
2182 found = next_item<D>(desktop, path->next, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
2183 }
2184 iter = children = D::siblings_after(object);
2185 } else {
2186 iter = children = D::children(root);
2187 }
2189 while ( iter && !found ) {
2190 SPObject *object=D::object(iter);
2191 if (desktop->isLayer(object)) {
2192 if (PREFS_SELECTION_LAYER != inlayer) { // recurse into sublayers
2193 found = next_item<D>(desktop, NULL, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
2194 }
2195 } else if ( SP_IS_ITEM(object) &&
2196 ( !only_in_viewport || desktop->isWithinViewport(SP_ITEM(object)) ) &&
2197 ( !onlyvisible || !desktop->itemIsHidden(SP_ITEM(object))) &&
2198 ( !onlysensitive || !SP_ITEM(object)->isLocked()) &&
2199 !desktop->isLayer(SP_ITEM(object)) )
2200 {
2201 found = SP_ITEM(object);
2202 }
2203 iter = D::next(iter);
2204 }
2206 D::dispose(children);
2208 return found;
2209 }
2211 }
2213 /**
2214 * If \a item is not entirely visible then adjust visible area to centre on the centre on of
2215 * \a item.
2216 */
2217 void scroll_to_show_item(SPDesktop *desktop, SPItem *item)
2218 {
2219 NR::Rect dbox = desktop->get_display_area();
2220 NR::Maybe<NR::Rect> sbox = sp_item_bbox_desktop(item);
2222 if ( sbox && dbox.contains(*sbox) == false ) {
2223 NR::Point const s_dt = sbox->midpoint();
2224 NR::Point const s_w = desktop->d2w(s_dt);
2225 NR::Point const d_dt = dbox.midpoint();
2226 NR::Point const d_w = desktop->d2w(d_dt);
2227 NR::Point const moved_w( d_w - s_w );
2228 gint const dx = (gint) moved_w[X];
2229 gint const dy = (gint) moved_w[Y];
2230 desktop->scroll_world(dx, dy);
2231 }
2232 }
2235 void
2236 sp_selection_clone()
2237 {
2238 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2239 if (desktop == NULL)
2240 return;
2242 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2244 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
2246 // check if something is selected
2247 if (selection->isEmpty()) {
2248 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object</b> to clone."));
2249 return;
2250 }
2252 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
2254 selection->clear();
2256 // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
2257 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
2259 GSList *newsel = NULL;
2261 while (reprs) {
2262 Inkscape::XML::Node *sel_repr = (Inkscape::XML::Node *) reprs->data;
2263 Inkscape::XML::Node *parent = sp_repr_parent(sel_repr);
2265 Inkscape::XML::Node *clone = xml_doc->createElement("svg:use");
2266 sp_repr_set_attr(clone, "x", "0");
2267 sp_repr_set_attr(clone, "y", "0");
2268 sp_repr_set_attr(clone, "xlink:href", g_strdup_printf("#%s", sel_repr->attribute("id")));
2270 sp_repr_set_attr(clone, "inkscape:transform-center-x", sel_repr->attribute("inkscape:transform-center-x"));
2271 sp_repr_set_attr(clone, "inkscape:transform-center-y", sel_repr->attribute("inkscape:transform-center-y"));
2273 // add the new clone to the top of the original's parent
2274 parent->appendChild(clone);
2276 newsel = g_slist_prepend(newsel, clone);
2277 reprs = g_slist_remove(reprs, sel_repr);
2278 Inkscape::GC::release(clone);
2279 }
2281 // TRANSLATORS: only translate "string" in "context|string".
2282 // For more details, see http://developer.gnome.org/doc/API/2.0/glib/glib-I18N.html#Q-:CAPS
2283 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_CLONE,
2284 Q_("action|Clone"));
2286 selection->setReprList(newsel);
2288 g_slist_free(newsel);
2289 }
2291 void
2292 sp_selection_unlink()
2293 {
2294 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2295 if (!desktop)
2296 return;
2298 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2300 if (selection->isEmpty()) {
2301 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>clone</b> to unlink."));
2302 return;
2303 }
2305 // Get a copy of current selection.
2306 GSList *new_select = NULL;
2307 bool unlinked = false;
2308 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2309 items != NULL;
2310 items = items->next)
2311 {
2312 SPItem *item = (SPItem *) items->data;
2314 if (SP_IS_TEXT(item)) {
2315 SPObject *tspan = sp_tref_convert_to_tspan(SP_OBJECT(item));
2317 if (tspan) {
2318 SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
2319 }
2321 // Set unlink to true, and fall into the next if which
2322 // will include this text item in the new selection
2323 unlinked = true;
2324 }
2326 if (!(SP_IS_USE(item) || SP_IS_TREF(item))) {
2327 // keep the non-use item in the new selection
2328 new_select = g_slist_prepend(new_select, item);
2329 continue;
2330 }
2332 SPItem *unlink;
2333 if (SP_IS_USE(item)) {
2334 unlink = sp_use_unlink(SP_USE(item));
2335 } else /*if (SP_IS_TREF(use))*/ {
2336 unlink = SP_ITEM(sp_tref_convert_to_tspan(SP_OBJECT(item)));
2337 }
2339 unlinked = true;
2340 // Add ungrouped items to the new selection.
2341 new_select = g_slist_prepend(new_select, unlink);
2342 }
2344 if (new_select) { // set new selection
2345 selection->clear();
2346 selection->setList(new_select);
2347 g_slist_free(new_select);
2348 }
2349 if (!unlinked) {
2350 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No clones to unlink</b> in the selection."));
2351 }
2353 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNLINK_CLONE,
2354 _("Unlink clone"));
2355 }
2357 void
2358 sp_select_clone_original()
2359 {
2360 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2361 if (desktop == NULL)
2362 return;
2364 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2366 SPItem *item = selection->singleItem();
2368 gchar const *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.");
2370 // Check if other than two objects are selected
2371 if (g_slist_length((GSList *) selection->itemList()) != 1 || !item) {
2372 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
2373 return;
2374 }
2376 SPItem *original = NULL;
2377 if (SP_IS_USE(item)) {
2378 original = sp_use_get_original (SP_USE(item));
2379 } else if (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) {
2380 original = sp_offset_get_source (SP_OFFSET(item));
2381 } else if (SP_IS_TEXT_TEXTPATH(item)) {
2382 original = sp_textpath_get_path_item (SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))));
2383 } else if (SP_IS_FLOWTEXT(item)) {
2384 original = SP_FLOWTEXT(item)->get_frame (NULL); // first frame only
2385 } else { // it's an object that we don't know what to do with
2386 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
2387 return;
2388 }
2390 if (!original) {
2391 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>Cannot find</b> the object to select (orphaned clone, offset, textpath, flowed text?)"));
2392 return;
2393 }
2395 for (SPObject *o = original; o && !SP_IS_ROOT(o); o = SP_OBJECT_PARENT (o)) {
2396 if (SP_IS_DEFS (o)) {
2397 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("The object you're trying to select is <b>not visible</b> (it is in <defs>)"));
2398 return;
2399 }
2400 }
2402 if (original) {
2403 selection->clear();
2404 selection->set(original);
2405 if (SP_CYCLING == SP_CYCLE_FOCUS) {
2406 scroll_to_show_item(desktop, original);
2407 }
2408 }
2409 }
2412 void sp_selection_to_marker(bool apply)
2413 {
2414 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2415 if (desktop == NULL)
2416 return;
2418 SPDocument *doc = sp_desktop_document(desktop);
2419 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2421 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2423 // check if something is selected
2424 if (selection->isEmpty()) {
2425 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to marker."));
2426 return;
2427 }
2429 sp_document_ensure_up_to_date(doc);
2430 NR::Maybe<NR::Rect> r = selection->bounds();
2431 if ( !r || r->isEmpty() ) {
2432 return;
2433 }
2435 // calculate the transform to be applied to objects to move them to 0,0
2436 NR::Point move_p = NR::Point(0, sp_document_height(doc)) - (r->min() + NR::Point ((r->extent(NR::X))/2, (r->extent(NR::Y))/2));
2437 move_p[NR::Y] = -move_p[NR::Y];
2438 NR::Matrix move = NR::Matrix (NR::translate (move_p));
2440 GSList *items = g_slist_copy((GSList *) selection->itemList());
2442 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2444 // bottommost object, after sorting
2445 SPObject *parent = SP_OBJECT_PARENT (items->data);
2447 NR::Matrix parent_transform = sp_item_i2root_affine(SP_ITEM(parent));
2449 // remember the position of the first item
2450 gint pos = SP_OBJECT_REPR (items->data)->position();
2451 (void)pos; // TODO check why this was remembered
2453 // create a list of duplicates
2454 GSList *repr_copies = NULL;
2455 for (GSList *i = items; i != NULL; i = i->next) {
2456 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2457 repr_copies = g_slist_prepend (repr_copies, dup);
2458 }
2460 NR::Rect bounds(desktop->dt2doc(r->min()), desktop->dt2doc(r->max()));
2462 if (apply) {
2463 // delete objects so that their clones don't get alerted; this object will be restored shortly
2464 for (GSList *i = items; i != NULL; i = i->next) {
2465 SPObject *item = SP_OBJECT (i->data);
2466 item->deleteObject (false);
2467 }
2468 }
2470 // Hack: Temporarily set clone compensation to unmoved, so that we can move clone-originals
2471 // without disturbing clones.
2472 // See ActorAlign::on_button_click() in src/ui/dialog/align-and-distribute.cpp
2473 int saved_compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2474 prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2476 gchar const *mark_id = generate_marker(repr_copies, bounds, doc,
2477 ( NR::Matrix(NR::translate(desktop->dt2doc(NR::Point(r->min()[NR::X],
2478 r->max()[NR::Y]))))
2479 * parent_transform.inverse() ),
2480 parent_transform * move);
2481 (void)mark_id;
2483 // restore compensation setting
2484 prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
2487 g_slist_free (items);
2489 sp_document_done (doc, SP_VERB_EDIT_SELECTION_2_MARKER,
2490 _("Objects to marker"));
2491 }
2493 static void sp_selection_to_guides_recursive(SPItem *item, bool deleteitem) {
2494 if (SP_IS_GROUP(item) && !SP_IS_BOX3D(item)) {
2495 for (GSList *i = sp_item_group_item_list (SP_GROUP(item)); i != NULL; i = i->next) {
2496 sp_selection_to_guides_recursive(SP_ITEM(i->data), deleteitem);
2497 }
2498 } else {
2499 sp_item_convert_item_to_guides(item);
2501 if (deleteitem) {
2502 SP_OBJECT(item)->deleteObject(true);
2503 }
2504 }
2505 }
2507 void sp_selection_to_guides()
2508 {
2509 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2510 if (desktop == NULL)
2511 return;
2513 SPDocument *doc = sp_desktop_document(desktop);
2514 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2515 // we need to copy the list because it gets reset when objects are deleted
2516 GSList *items = g_slist_copy((GSList *) selection->itemList());
2518 if (!items) {
2519 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to guides."));
2520 return;
2521 }
2523 bool deleteitem = (prefs_get_int_attribute("tools", "cvg_keep_objects", 0) == 0);
2525 for (GSList const *i = items; i != NULL; i = i->next) {
2526 sp_selection_to_guides_recursive(SP_ITEM(i->data), deleteitem);
2527 }
2529 sp_document_done (doc, SP_VERB_EDIT_SELECTION_2_GUIDES, _("Objects to guides"));
2530 }
2532 void
2533 sp_selection_tile(bool apply)
2534 {
2535 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2536 if (desktop == NULL)
2537 return;
2539 SPDocument *doc = sp_desktop_document(desktop);
2540 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2542 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2544 // check if something is selected
2545 if (selection->isEmpty()) {
2546 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to pattern."));
2547 return;
2548 }
2550 sp_document_ensure_up_to_date(doc);
2551 NR::Maybe<NR::Rect> r = selection->bounds();
2552 if ( !r || r->isEmpty() ) {
2553 return;
2554 }
2556 // calculate the transform to be applied to objects to move them to 0,0
2557 NR::Point move_p = NR::Point(0, sp_document_height(doc)) - (r->min() + NR::Point (0, r->extent(NR::Y)));
2558 move_p[NR::Y] = -move_p[NR::Y];
2559 NR::Matrix move = NR::Matrix (NR::translate (move_p));
2561 GSList *items = g_slist_copy((GSList *) selection->itemList());
2563 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2565 // bottommost object, after sorting
2566 SPObject *parent = SP_OBJECT_PARENT (items->data);
2568 NR::Matrix parent_transform = sp_item_i2root_affine(SP_ITEM(parent));
2570 // remember the position of the first item
2571 gint pos = SP_OBJECT_REPR (items->data)->position();
2573 // create a list of duplicates
2574 GSList *repr_copies = NULL;
2575 for (GSList *i = items; i != NULL; i = i->next) {
2576 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2577 repr_copies = g_slist_prepend (repr_copies, dup);
2578 }
2580 NR::Rect bounds(desktop->dt2doc(r->min()), desktop->dt2doc(r->max()));
2582 if (apply) {
2583 // delete objects so that their clones don't get alerted; this object will be restored shortly
2584 for (GSList *i = items; i != NULL; i = i->next) {
2585 SPObject *item = SP_OBJECT (i->data);
2586 item->deleteObject (false);
2587 }
2588 }
2590 // Hack: Temporarily set clone compensation to unmoved, so that we can move clone-originals
2591 // without disturbing clones.
2592 // See ActorAlign::on_button_click() in src/ui/dialog/align-and-distribute.cpp
2593 int saved_compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2594 prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2596 gchar const *pat_id = pattern_tile(repr_copies, bounds, doc,
2597 ( NR::Matrix(NR::translate(desktop->dt2doc(NR::Point(r->min()[NR::X],
2598 r->max()[NR::Y]))))
2599 * parent_transform.inverse() ),
2600 parent_transform * move);
2602 // restore compensation setting
2603 prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
2605 if (apply) {
2606 Inkscape::XML::Node *rect = xml_doc->createElement("svg:rect");
2607 rect->setAttribute("style", g_strdup_printf("stroke:none;fill:url(#%s)", pat_id));
2609 NR::Point min = bounds.min() * parent_transform.inverse();
2610 NR::Point max = bounds.max() * parent_transform.inverse();
2612 sp_repr_set_svg_double(rect, "width", max[NR::X] - min[NR::X]);
2613 sp_repr_set_svg_double(rect, "height", max[NR::Y] - min[NR::Y]);
2614 sp_repr_set_svg_double(rect, "x", min[NR::X]);
2615 sp_repr_set_svg_double(rect, "y", min[NR::Y]);
2617 // restore parent and position
2618 SP_OBJECT_REPR (parent)->appendChild(rect);
2619 rect->setPosition(pos > 0 ? pos : 0);
2620 SPItem *rectangle = (SPItem *) sp_desktop_document (desktop)->getObjectByRepr(rect);
2622 Inkscape::GC::release(rect);
2624 selection->clear();
2625 selection->set(rectangle);
2626 }
2628 g_slist_free (items);
2630 sp_document_done (doc, SP_VERB_EDIT_TILE,
2631 _("Objects to pattern"));
2632 }
2634 void
2635 sp_selection_untile()
2636 {
2637 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2638 if (desktop == NULL)
2639 return;
2641 SPDocument *doc = sp_desktop_document(desktop);
2642 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2644 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2646 // check if something is selected
2647 if (selection->isEmpty()) {
2648 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object with pattern fill</b> to extract objects from."));
2649 return;
2650 }
2652 GSList *new_select = NULL;
2654 bool did = false;
2656 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2657 items != NULL;
2658 items = items->next) {
2660 SPItem *item = (SPItem *) items->data;
2662 SPStyle *style = SP_OBJECT_STYLE (item);
2664 if (!style || !style->fill.isPaintserver())
2665 continue;
2667 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
2669 if (!SP_IS_PATTERN(server))
2670 continue;
2672 did = true;
2674 SPPattern *pattern = pattern_getroot (SP_PATTERN (server));
2676 NR::Matrix pat_transform = pattern_patternTransform (SP_PATTERN (server));
2677 pat_transform *= item->transform;
2679 for (SPObject *child = sp_object_first_child(SP_OBJECT(pattern)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2680 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate(xml_doc);
2681 SPItem *i = SP_ITEM (desktop->currentLayer()->appendChildRepr(copy));
2683 // FIXME: relink clones to the new canvas objects
2684 // use SPObject::setid when mental finishes it to steal ids of
2686 // this is needed to make sure the new item has curve (simply requestDisplayUpdate does not work)
2687 sp_document_ensure_up_to_date (doc);
2689 NR::Matrix transform( i->transform * pat_transform );
2690 sp_item_write_transform(i, SP_OBJECT_REPR(i), transform);
2692 new_select = g_slist_prepend(new_select, i);
2693 }
2695 SPCSSAttr *css = sp_repr_css_attr_new ();
2696 sp_repr_css_set_property (css, "fill", "none");
2697 sp_repr_css_change (SP_OBJECT_REPR (item), css, "style");
2698 }
2700 if (!did) {
2701 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No pattern fills</b> in the selection."));
2702 } else {
2703 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNTILE,
2704 _("Pattern to objects"));
2705 selection->setList(new_select);
2706 }
2707 }
2709 void
2710 sp_selection_get_export_hints (Inkscape::Selection *selection, char const **filename, float *xdpi, float *ydpi)
2711 {
2712 if (selection->isEmpty()) {
2713 return;
2714 }
2716 GSList const *reprlst = selection->reprList();
2717 bool filename_search = TRUE;
2718 bool xdpi_search = TRUE;
2719 bool ydpi_search = TRUE;
2721 for(; reprlst != NULL &&
2722 filename_search &&
2723 xdpi_search &&
2724 ydpi_search;
2725 reprlst = reprlst->next) {
2726 gchar const *dpi_string;
2727 Inkscape::XML::Node * repr = (Inkscape::XML::Node *)reprlst->data;
2729 if (filename_search) {
2730 *filename = repr->attribute("inkscape:export-filename");
2731 if (*filename != NULL)
2732 filename_search = FALSE;
2733 }
2735 if (xdpi_search) {
2736 dpi_string = NULL;
2737 dpi_string = repr->attribute("inkscape:export-xdpi");
2738 if (dpi_string != NULL) {
2739 *xdpi = atof(dpi_string);
2740 xdpi_search = FALSE;
2741 }
2742 }
2744 if (ydpi_search) {
2745 dpi_string = NULL;
2746 dpi_string = repr->attribute("inkscape:export-ydpi");
2747 if (dpi_string != NULL) {
2748 *ydpi = atof(dpi_string);
2749 ydpi_search = FALSE;
2750 }
2751 }
2752 }
2753 }
2755 void
2756 sp_document_get_export_hints (SPDocument *doc, char const **filename, float *xdpi, float *ydpi)
2757 {
2758 Inkscape::XML::Node * repr = sp_document_repr_root(doc);
2759 gchar const *dpi_string;
2761 *filename = repr->attribute("inkscape:export-filename");
2763 dpi_string = NULL;
2764 dpi_string = repr->attribute("inkscape:export-xdpi");
2765 if (dpi_string != NULL) {
2766 *xdpi = atof(dpi_string);
2767 }
2769 dpi_string = NULL;
2770 dpi_string = repr->attribute("inkscape:export-ydpi");
2771 if (dpi_string != NULL) {
2772 *ydpi = atof(dpi_string);
2773 }
2774 }
2776 void
2777 sp_selection_create_bitmap_copy ()
2778 {
2779 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2780 if (desktop == NULL)
2781 return;
2783 SPDocument *document = sp_desktop_document(desktop);
2784 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
2786 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2788 // check if something is selected
2789 if (selection->isEmpty()) {
2790 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to make a bitmap copy."));
2791 return;
2792 }
2794 // Get the bounding box of the selection
2795 NRRect bbox;
2796 sp_document_ensure_up_to_date (document);
2797 selection->bounds(&bbox);
2798 if (NR_RECT_DFLS_TEST_EMPTY(&bbox)) {
2799 return; // exceptional situation, so not bother with a translatable error message, just quit quietly
2800 }
2802 // List of the items to show; all others will be hidden
2803 GSList *items = g_slist_copy ((GSList *) selection->itemList());
2805 // Sort items so that the topmost comes last
2806 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
2808 // Generate a random value from the current time (you may create bitmap from the same object(s)
2809 // multiple times, and this is done so that they don't clash)
2810 GTimeVal cu;
2811 g_get_current_time (&cu);
2812 guint current = (int) (cu.tv_sec * 1000000 + cu.tv_usec) % 1024;
2814 // Create the filename
2815 gchar *filename = g_strdup_printf ("%s-%s-%u.png", document->name, SP_OBJECT_REPR(items->data)->attribute("id"), current);
2816 // Imagemagick is known not to handle spaces in filenames, so we replace anything but letters,
2817 // digits, and a few other chars, with "_"
2818 filename = g_strcanon (filename, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.=+~$#@^&!?", '_');
2819 // Build the complete path by adding document->base if set
2820 gchar *filepath = g_build_filename (document->base?document->base:"", filename, NULL);
2822 //g_print ("%s\n", filepath);
2824 // Remember parent and z-order of the topmost one
2825 gint pos = SP_OBJECT_REPR(g_slist_last(items)->data)->position();
2826 SPObject *parent_object = SP_OBJECT_PARENT(g_slist_last(items)->data);
2827 Inkscape::XML::Node *parent = SP_OBJECT_REPR(parent_object);
2829 // Calculate resolution
2830 double res;
2831 int const prefs_res = prefs_get_int_attribute ("options.createbitmap", "resolution", 0);
2832 int const prefs_min = prefs_get_int_attribute ("options.createbitmap", "minsize", 0);
2833 if (0 < prefs_res) {
2834 // If it's given explicitly in prefs, take it
2835 res = prefs_res;
2836 } else if (0 < prefs_min) {
2837 // If minsize is given, look up minimum bitmap size (default 250 pixels) and calculate resolution from it
2838 res = PX_PER_IN * prefs_min / MIN ((bbox.x1 - bbox.x0), (bbox.y1 - bbox.y0));
2839 } else {
2840 float hint_xdpi = 0, hint_ydpi = 0;
2841 char const *hint_filename;
2842 // take resolution hint from the selected objects
2843 sp_selection_get_export_hints (selection, &hint_filename, &hint_xdpi, &hint_ydpi);
2844 if (hint_xdpi != 0) {
2845 res = hint_xdpi;
2846 } else {
2847 // take resolution hint from the document
2848 sp_document_get_export_hints (document, &hint_filename, &hint_xdpi, &hint_ydpi);
2849 if (hint_xdpi != 0) {
2850 res = hint_xdpi;
2851 } else {
2852 // if all else fails, take the default 90 dpi
2853 res = PX_PER_IN;
2854 }
2855 }
2856 }
2858 // The width and height of the bitmap in pixels
2859 unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * res / PX_PER_IN);
2860 unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * res / PX_PER_IN);
2862 // Find out if we have to run a filter
2863 gchar const *run = NULL;
2864 gchar const *filter = prefs_get_string_attribute ("options.createbitmap", "filter");
2865 if (filter) {
2866 // filter command is given;
2867 // see if we have a parameter to pass to it
2868 gchar const *param1 = prefs_get_string_attribute ("options.createbitmap", "filter_param1");
2869 if (param1) {
2870 if (param1[strlen(param1) - 1] == '%') {
2871 // if the param string ends with %, interpret it as a percentage of the image's max dimension
2872 gchar p1[256];
2873 g_ascii_dtostr (p1, 256, ceil (g_ascii_strtod (param1, NULL) * MAX(width, height) / 100));
2874 // the first param is always the image filename, the second is param1
2875 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, p1);
2876 } else {
2877 // otherwise pass the param1 unchanged
2878 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, param1);
2879 }
2880 } else {
2881 // run without extra parameter
2882 run = g_strdup_printf ("%s \"%s\"", filter, filepath);
2883 }
2884 }
2886 // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
2887 NR::Matrix eek = sp_item_i2d_affine (SP_ITEM(parent_object));
2888 NR::Matrix t;
2890 double shift_x = bbox.x0;
2891 double shift_y = bbox.y1;
2892 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
2893 shift_x = round (shift_x);
2894 shift_y = -round (-shift_y); // this gets correct rounding despite coordinate inversion, remove the negations when the inversion is gone
2895 }
2896 t = NR::scale(1, -1) * NR::translate (shift_x, shift_y) * eek.inverse();
2898 // Do the export
2899 sp_export_png_file(document, filepath,
2900 bbox.x0, bbox.y0, bbox.x1, bbox.y1,
2901 width, height, res, res,
2902 (guint32) 0xffffff00,
2903 NULL, NULL,
2904 true, /*bool force_overwrite,*/
2905 items);
2907 g_slist_free (items);
2909 // Run filter, if any
2910 if (run) {
2911 g_print ("Running external filter: %s\n", run);
2912 system (run);
2913 }
2915 // Import the image back
2916 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filepath, NULL);
2917 if (pb) {
2918 // Create the repr for the image
2919 Inkscape::XML::Node * repr = xml_doc->createElement("svg:image");
2920 repr->setAttribute("xlink:href", filename);
2921 repr->setAttribute("sodipodi:absref", filepath);
2922 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
2923 sp_repr_set_svg_double(repr, "width", width);
2924 sp_repr_set_svg_double(repr, "height", height);
2925 } else {
2926 sp_repr_set_svg_double(repr, "width", (bbox.x1 - bbox.x0));
2927 sp_repr_set_svg_double(repr, "height", (bbox.y1 - bbox.y0));
2928 }
2930 // Write transform
2931 gchar *c=sp_svg_transform_write(t);
2932 repr->setAttribute("transform", c);
2933 g_free(c);
2935 // add the new repr to the parent
2936 parent->appendChild(repr);
2938 // move to the saved position
2939 repr->setPosition(pos > 0 ? pos + 1 : 1);
2941 // Set selection to the new image
2942 selection->clear();
2943 selection->add(repr);
2945 // Clean up
2946 Inkscape::GC::release(repr);
2947 gdk_pixbuf_unref (pb);
2949 // Complete undoable transaction
2950 sp_document_done (document, SP_VERB_SELECTION_CREATE_BITMAP,
2951 _("Create bitmap"));
2952 }
2954 g_free (filename);
2955 g_free (filepath);
2956 }
2958 /**
2959 * \brief sp_selection_set_mask
2960 *
2961 * This function creates a mask or clipPath from selection
2962 * Two different modes:
2963 * if applyToLayer, all selection is moved to DEFS as mask/clippath
2964 * and is applied to current layer
2965 * otherwise, topmost object is used as mask for other objects
2966 * If \a apply_clip_path parameter is true, clipPath is created, otherwise mask
2967 *
2968 */
2969 void
2970 sp_selection_set_mask(bool apply_clip_path, bool apply_to_layer)
2971 {
2972 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2973 if (desktop == NULL)
2974 return;
2976 SPDocument *doc = sp_desktop_document(desktop);
2977 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2979 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2981 // check if something is selected
2982 bool is_empty = selection->isEmpty();
2983 if ( apply_to_layer && is_empty) {
2984 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to create clippath or mask from."));
2985 return;
2986 } else if (!apply_to_layer && ( is_empty || NULL == selection->itemList()->next )) {
2987 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select mask object and <b>object(s)</b> to apply clippath or mask to."));
2988 return;
2989 }
2991 // FIXME: temporary patch to prevent crash!
2992 // Remove this when bboxes are fixed to not blow up on an item clipped/masked with its own clone
2993 bool clone_with_original = selection_contains_both_clone_and_original (selection);
2994 if (clone_with_original) {
2995 return; // in this version, you cannot clip/mask an object with its own clone
2996 }
2997 // /END FIXME
2999 sp_document_ensure_up_to_date(doc);
3001 GSList *items = g_slist_copy((GSList *) selection->itemList());
3003 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
3005 // create a list of duplicates
3006 GSList *mask_items = NULL;
3007 GSList *apply_to_items = NULL;
3008 GSList *items_to_delete = NULL;
3009 bool topmost = prefs_get_int_attribute ("options.maskobject", "topmost", 1);
3010 bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
3012 if (apply_to_layer) {
3013 // all selected items are used for mask, which is applied to a layer
3014 apply_to_items = g_slist_prepend (apply_to_items, desktop->currentLayer());
3016 for (GSList *i = items; i != NULL; i = i->next) {
3017 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
3018 mask_items = g_slist_prepend (mask_items, dup);
3020 if (remove_original) {
3021 SPObject *item = SP_OBJECT (i->data);
3022 items_to_delete = g_slist_prepend (items_to_delete, item);
3023 }
3024 }
3025 } else if (!topmost) {
3026 // topmost item is used as a mask, which is applied to other items in a selection
3027 GSList *i = items;
3028 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
3029 mask_items = g_slist_prepend (mask_items, dup);
3031 if (remove_original) {
3032 SPObject *item = SP_OBJECT (i->data);
3033 items_to_delete = g_slist_prepend (items_to_delete, item);
3034 }
3036 for (i = i->next; i != NULL; i = i->next) {
3037 apply_to_items = g_slist_prepend (apply_to_items, i->data);
3038 }
3039 } else {
3040 GSList *i = NULL;
3041 for (i = items; NULL != i->next; i = i->next) {
3042 apply_to_items = g_slist_prepend (apply_to_items, i->data);
3043 }
3045 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
3046 mask_items = g_slist_prepend (mask_items, dup);
3048 if (remove_original) {
3049 SPObject *item = SP_OBJECT (i->data);
3050 items_to_delete = g_slist_prepend (items_to_delete, item);
3051 }
3052 }
3054 g_slist_free (items);
3055 items = NULL;
3057 gchar const *attributeName = apply_clip_path ? "clip-path" : "mask";
3058 for (GSList *i = apply_to_items; NULL != i; i = i->next) {
3059 SPItem *item = reinterpret_cast<SPItem *>(i->data);
3060 // inverted object transform should be applied to a mask object,
3061 // as mask is calculated in user space (after applying transform)
3062 NR::Matrix maskTransform (item->transform.inverse());
3064 GSList *mask_items_dup = NULL;
3065 for (GSList *mask_item = mask_items; NULL != mask_item; mask_item = mask_item->next) {
3066 Inkscape::XML::Node *dup = reinterpret_cast<Inkscape::XML::Node *>(mask_item->data)->duplicate(xml_doc);
3067 mask_items_dup = g_slist_prepend (mask_items_dup, dup);
3068 }
3070 gchar const *mask_id = NULL;
3071 if (apply_clip_path) {
3072 mask_id = sp_clippath_create(mask_items_dup, doc, &maskTransform);
3073 } else {
3074 mask_id = sp_mask_create(mask_items_dup, doc, &maskTransform);
3075 }
3077 g_slist_free (mask_items_dup);
3078 mask_items_dup = NULL;
3080 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, g_strdup_printf("url(#%s)", mask_id));
3081 }
3083 g_slist_free (mask_items);
3084 g_slist_free (apply_to_items);
3086 for (GSList *i = items_to_delete; NULL != i; i = i->next) {
3087 SPObject *item = SP_OBJECT (i->data);
3088 item->deleteObject (false);
3089 }
3090 g_slist_free (items_to_delete);
3092 if (apply_clip_path)
3093 sp_document_done (doc, SP_VERB_OBJECT_SET_CLIPPATH, _("Set clipping path"));
3094 else
3095 sp_document_done (doc, SP_VERB_OBJECT_SET_MASK, _("Set mask"));
3096 }
3098 void sp_selection_unset_mask(bool apply_clip_path) {
3099 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
3100 if (desktop == NULL)
3101 return;
3103 SPDocument *doc = sp_desktop_document(desktop);
3104 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
3105 Inkscape::Selection *selection = sp_desktop_selection(desktop);
3107 // check if something is selected
3108 if (selection->isEmpty()) {
3109 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to remove clippath or mask from."));
3110 return;
3111 }
3113 bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
3114 sp_document_ensure_up_to_date(doc);
3116 gchar const *attributeName = apply_clip_path ? "clip-path" : "mask";
3117 std::map<SPObject*,SPItem*> referenced_objects;
3118 for (GSList const *i = selection->itemList(); NULL != i; i = i->next) {
3119 if (remove_original) {
3120 // remember referenced mask/clippath, so orphaned masks can be moved back to document
3121 SPItem *item = reinterpret_cast<SPItem *>(i->data);
3122 Inkscape::URIReference *uri_ref = NULL;
3124 if (apply_clip_path) {
3125 uri_ref = item->clip_ref;
3126 } else {
3127 uri_ref = item->mask_ref;
3128 }
3130 // collect distinct mask object (and associate with item to apply transform)
3131 if (NULL != uri_ref && NULL != uri_ref->getObject()) {
3132 referenced_objects[uri_ref->getObject()] = item;
3133 }
3134 }
3136 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, "none");
3137 }
3139 // restore mask objects into a document
3140 for ( std::map<SPObject*,SPItem*>::iterator it = referenced_objects.begin() ; it != referenced_objects.end() ; ++it) {
3141 SPObject *obj = (*it).first;
3142 GSList *items_to_move = NULL;
3143 for (SPObject *child = sp_object_first_child(obj) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
3144 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate(xml_doc);
3145 items_to_move = g_slist_prepend (items_to_move, copy);
3146 }
3148 if (!obj->isReferenced()) {
3149 // delete from defs if no other object references this mask
3150 obj->deleteObject(false);
3151 }
3153 // remember parent and position of the item to which the clippath/mask was applied
3154 Inkscape::XML::Node *parent = SP_OBJECT_REPR((*it).second)->parent();
3155 gint pos = SP_OBJECT_REPR((*it).second)->position();
3157 for (GSList *i = items_to_move; NULL != i; i = i->next) {
3158 Inkscape::XML::Node *repr = (Inkscape::XML::Node *)i->data;
3160 // insert into parent, restore pos
3161 parent->appendChild(repr);
3162 repr->setPosition((pos + 1) > 0 ? (pos + 1) : 0);
3164 SPItem *mask_item = (SPItem *) sp_desktop_document (desktop)->getObjectByRepr(repr);
3165 selection->add(repr);
3167 // transform mask, so it is moved the same spot where mask was applied
3168 NR::Matrix transform (mask_item->transform);
3169 transform *= (*it).second->transform;
3170 sp_item_write_transform(mask_item, SP_OBJECT_REPR(mask_item), transform);
3171 }
3173 g_slist_free (items_to_move);
3174 }
3176 if (apply_clip_path)
3177 sp_document_done (doc, SP_VERB_OBJECT_UNSET_CLIPPATH, _("Release clipping path"));
3178 else
3179 sp_document_done (doc, SP_VERB_OBJECT_UNSET_MASK, _("Release mask"));
3180 }
3182 void fit_canvas_to_selection(SPDesktop *desktop) {
3183 g_return_if_fail(desktop != NULL);
3184 SPDocument *doc = sp_desktop_document(desktop);
3186 g_return_if_fail(doc != NULL);
3187 g_return_if_fail(desktop->selection != NULL);
3189 if (desktop->selection->isEmpty()) {
3190 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to fit canvas to."));
3191 return;
3192 }
3193 NR::Maybe<NR::Rect> const bbox(desktop->selection->bounds());
3194 if (bbox && !bbox->isEmpty()) {
3195 doc->fitToRect(*bbox);
3196 }
3197 };
3199 void fit_canvas_to_drawing(SPDocument *doc) {
3200 g_return_if_fail(doc != NULL);
3202 sp_document_ensure_up_to_date(doc);
3203 SPItem const *const root = SP_ITEM(doc->root);
3204 NR::Maybe<NR::Rect> const bbox(root->getBounds(sp_item_i2r_affine(root)));
3205 if (bbox && !bbox->isEmpty()) {
3206 doc->fitToRect(*bbox);
3207 }
3208 };
3210 void fit_canvas_to_selection_or_drawing(SPDesktop *desktop) {
3211 g_return_if_fail(desktop != NULL);
3212 SPDocument *doc = sp_desktop_document(desktop);
3214 g_return_if_fail(doc != NULL);
3215 g_return_if_fail(desktop->selection != NULL);
3217 if (desktop->selection->isEmpty()) {
3218 fit_canvas_to_drawing(doc);
3219 } else {
3220 fit_canvas_to_selection(desktop);
3221 }
3223 sp_document_done(doc, SP_VERB_FIT_CANVAS_TO_DRAWING,
3224 _("Fit page to selection"));
3225 };
3227 static void itemtree_map(void (*f)(SPItem *, SPDesktop *), SPObject *root, SPDesktop *desktop) {
3228 // don't operate on layers
3229 if (SP_IS_ITEM(root) && !desktop->isLayer(SP_ITEM(root))) {
3230 f(SP_ITEM(root), desktop);
3231 }
3232 for ( SPObject::SiblingIterator iter = root->firstChild() ; iter ; ++iter ) {
3233 //don't recurse into locked layers
3234 if (!(SP_IS_ITEM(&*iter) && desktop->isLayer(SP_ITEM(&*iter)) && SP_ITEM(&*iter)->isLocked())) {
3235 itemtree_map(f, iter, desktop);
3236 }
3237 }
3238 }
3240 static void unlock(SPItem *item, SPDesktop */*desktop*/) {
3241 if (item->isLocked()) {
3242 item->setLocked(FALSE);
3243 }
3244 }
3246 static void unhide(SPItem *item, SPDesktop *desktop) {
3247 if (desktop->itemIsHidden(item)) {
3248 item->setExplicitlyHidden(FALSE);
3249 }
3250 }
3252 static void process_all(void (*f)(SPItem *, SPDesktop *), SPDesktop *dt, bool layer_only) {
3253 if (!dt) return;
3255 SPObject *root;
3256 if (layer_only) {
3257 root = dt->currentLayer();
3258 } else {
3259 root = dt->currentRoot();
3260 }
3262 itemtree_map(f, root, dt);
3263 }
3265 void unlock_all(SPDesktop *dt) {
3266 process_all(&unlock, dt, true);
3267 }
3269 void unlock_all_in_all_layers(SPDesktop *dt) {
3270 process_all(&unlock, dt, false);
3271 }
3273 void unhide_all(SPDesktop *dt) {
3274 process_all(&unhide, dt, true);
3275 }
3277 void unhide_all_in_all_layers(SPDesktop *dt) {
3278 process_all(&unhide, dt, false);
3279 }
3282 GSList * sp_selection_get_clipboard() {
3283 return clipboard;
3284 }
3287 /*
3288 Local Variables:
3289 mode:c++
3290 c-file-style:"stroustrup"
3291 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
3292 indent-tabs-mode:nil
3293 fill-column:99
3294 End:
3295 */
3296 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :