e5e2cdc0177d7e41dbe20d9ce917758037055e15
1 #define __SP_SELECTION_CHEMISTRY_C__
3 /*
4 * Miscellanous operations on selected items
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * Frank Felfe <innerspace@iname.com>
9 * MenTaLguY <mental@rydia.net>
10 * bulia byak <buliabyak@users.sf.net>
11 * Andrius R. <knutux@gmail.com>
12 *
13 * Copyright (C) 1999-2006 authors
14 * Copyright (C) 2001-2002 Ximian, Inc.
15 *
16 * Released under GNU GPL, read the file 'COPYING' for more information
17 */
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
23 #include <gtkmm/clipboard.h>
25 #include "svg/svg.h"
26 #include "inkscape.h"
27 #include "desktop.h"
28 #include "desktop-style.h"
29 #include "selection.h"
30 #include "tools-switch.h"
31 #include "desktop-handles.h"
32 #include "message-stack.h"
33 #include "sp-item-transform.h"
34 #include "marker.h"
35 #include "sp-use.h"
36 #include "sp-textpath.h"
37 #include "sp-tspan.h"
38 #include "sp-tref.h"
39 #include "sp-flowtext.h"
40 #include "sp-flowregion.h"
41 #include "text-editing.h"
42 #include "text-context.h"
43 #include "connector-context.h"
44 #include "sp-path.h"
45 #include "sp-conn-end.h"
46 #include "dropper-context.h"
47 #include <glibmm/i18n.h>
48 #include "libnr/nr-matrix-rotate-ops.h"
49 #include "libnr/nr-matrix-translate-ops.h"
50 #include "libnr/nr-rotate-fns.h"
51 #include "libnr/nr-scale-ops.h"
52 #include "libnr/nr-scale-translate-ops.h"
53 #include "libnr/nr-translate-matrix-ops.h"
54 #include "libnr/nr-translate-scale-ops.h"
55 #include "xml/repr.h"
56 #include "style.h"
57 #include "document-private.h"
58 #include "sp-gradient.h"
59 #include "sp-gradient-reference.h"
60 #include "sp-linear-gradient-fns.h"
61 #include "sp-pattern.h"
62 #include "sp-radial-gradient-fns.h"
63 #include "sp-namedview.h"
64 #include "prefs-utils.h"
65 #include "sp-offset.h"
66 #include "sp-clippath.h"
67 #include "sp-mask.h"
68 #include "file.h"
69 #include "helper/png-write.h"
70 #include "layer-fns.h"
71 #include "context-fns.h"
72 #include <map>
73 #include "helper/units.h"
74 #include "sp-item.h"
75 #include "unit-constants.h"
76 #include "xml/simple-document.h"
77 #include "sp-filter-reference.h"
78 #include "gradient-drag.h"
80 using NR::X;
81 using NR::Y;
83 #include "selection-chemistry.h"
85 /* fixme: find a better place */
86 Inkscape::XML::Document *clipboard_document = NULL;
87 GSList *clipboard = NULL;
88 GSList *defs_clipboard = NULL;
89 SPCSSAttr *style_clipboard = NULL;
90 NR::Maybe<NR::Rect> size_clipboard;
92 static void sp_copy_stuff_used_by_item(GSList **defs_clip, SPItem *item, GSList const *items, Inkscape::XML::Document* xml_doc);
94 /**
95 * Copies repr and its inherited css style elements, along with the accumulated transform 'full_t',
96 * then prepends the copy to 'clip'.
97 */
98 void sp_selection_copy_one (Inkscape::XML::Node *repr, NR::Matrix full_t, GSList **clip, Inkscape::XML::Document* xml_doc)
99 {
100 Inkscape::XML::Node *copy = repr->duplicate(xml_doc);
102 // copy complete inherited style
103 SPCSSAttr *css = sp_repr_css_attr_inherited(repr, "style");
104 sp_repr_css_set(copy, css, "style");
105 sp_repr_css_attr_unref(css);
107 // write the complete accumulated transform passed to us
108 // (we're dealing with unattached repr, so we write to its attr
109 // instead of using sp_item_set_transform)
110 gchar *affinestr=sp_svg_transform_write(full_t);
111 copy->setAttribute("transform", affinestr);
112 g_free(affinestr);
114 *clip = g_slist_prepend(*clip, copy);
115 }
117 void sp_selection_copy_impl (GSList const *items, GSList **clip, GSList **defs_clip, SPCSSAttr **style_clip, Inkscape::XML::Document* xml_doc)
118 {
120 // Copy stuff referenced by all items to defs_clip:
121 if (defs_clip) {
122 for (GSList *i = (GSList *) items; i != NULL; i = i->next) {
123 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (i->data), items, xml_doc);
124 }
125 *defs_clip = g_slist_reverse(*defs_clip);
126 }
128 // Store style:
129 if (style_clip) {
130 SPItem *item = SP_ITEM (items->data); // take from the first selected item
131 *style_clip = take_style_from_item (item);
132 }
134 if (clip) {
135 // Sort items:
136 GSList *sorted_items = g_slist_copy ((GSList *) items);
137 sorted_items = g_slist_sort((GSList *) sorted_items, (GCompareFunc) sp_object_compare_position);
139 // Copy item reprs:
140 for (GSList *i = (GSList *) sorted_items; i != NULL; i = i->next) {
141 sp_selection_copy_one (SP_OBJECT_REPR (i->data), sp_item_i2doc_affine(SP_ITEM (i->data)), clip, xml_doc);
142 }
144 *clip = g_slist_reverse(*clip);
145 g_slist_free ((GSList *) sorted_items);
146 }
147 }
149 /**
150 * Add gradients/patterns/markers referenced by copied objects to defs.
151 * Iterates through 'defs_clip', and for each item it adds the data
152 * repr into the global defs.
153 */
154 void
155 paste_defs (GSList **defs_clip, SPDocument *doc)
156 {
157 if (!defs_clip)
158 return;
160 for (GSList *gl = *defs_clip; gl != NULL; gl = gl->next) {
161 SPDefs *defs= (SPDefs *) SP_DOCUMENT_DEFS(doc);
162 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) gl->data;
163 gchar const *id = repr->attribute("id");
164 if (!id || !doc->getObjectById(id)) {
165 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
166 Inkscape::XML::Node *copy = repr->duplicate(xml_doc);
167 SP_OBJECT_REPR(defs)->addChild(copy, NULL);
168 Inkscape::GC::release(copy);
169 }
170 }
171 }
173 GSList *sp_selection_paste_impl (SPDocument *doc, SPObject *parent, GSList **clip, GSList **defs_clip)
174 {
175 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
176 paste_defs (defs_clip, doc);
178 GSList *copied = NULL;
179 // add objects to document
180 for (GSList *l = *clip; l != NULL; l = l->next) {
181 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
182 Inkscape::XML::Node *copy = repr->duplicate(xml_doc);
184 // premultiply the item transform by the accumulated parent transform in the paste layer
185 NR::Matrix local = sp_item_i2doc_affine(SP_ITEM(parent));
186 if (!local.test_identity()) {
187 gchar const *t_str = copy->attribute("transform");
188 NR::Matrix item_t (NR::identity());
189 if (t_str)
190 sp_svg_transform_read(t_str, &item_t);
191 item_t *= local.inverse();
192 // (we're dealing with unattached repr, so we write to its attr instead of using sp_item_set_transform)
193 gchar *affinestr=sp_svg_transform_write(item_t);
194 copy->setAttribute("transform", affinestr);
195 g_free(affinestr);
196 }
198 parent->appendChildRepr(copy);
199 copied = g_slist_prepend(copied, copy);
200 Inkscape::GC::release(copy);
201 }
202 return copied;
203 }
205 void sp_selection_delete_impl(GSList const *items, bool propagate = true, bool propagate_descendants = true)
206 {
207 for (GSList const *i = items ; i ; i = i->next ) {
208 sp_object_ref((SPObject *)i->data, NULL);
209 }
210 for (GSList const *i = items; i != NULL; i = i->next) {
211 SPItem *item = (SPItem *) i->data;
212 SP_OBJECT(item)->deleteObject(propagate, propagate_descendants);
213 sp_object_unref((SPObject *)item, NULL);
214 }
215 }
218 void sp_selection_delete()
219 {
220 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
221 if (desktop == NULL) {
222 return;
223 }
225 if (tools_isactive (desktop, TOOLS_TEXT))
226 if (sp_text_delete_selection(desktop->event_context)) {
227 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_TEXT,
228 _("Delete text"));
229 return;
230 }
232 Inkscape::Selection *selection = sp_desktop_selection(desktop);
234 // check if something is selected
235 if (selection->isEmpty()) {
236 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Nothing</b> was deleted."));
237 return;
238 }
240 GSList const *selected = g_slist_copy(const_cast<GSList *>(selection->itemList()));
241 selection->clear();
242 sp_selection_delete_impl (selected);
243 g_slist_free ((GSList *) selected);
245 /* a tool may have set up private information in it's selection context
246 * that depends on desktop items. I think the only sane way to deal with
247 * this currently is to reset the current tool, which will reset it's
248 * associated selection context. For example: deleting an object
249 * while moving it around the canvas.
250 */
251 tools_switch ( desktop, tools_active ( desktop ) );
253 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_DELETE,
254 _("Delete"));
255 }
257 /* fixme: sequencing */
258 void sp_selection_duplicate()
259 {
260 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
261 if (desktop == NULL)
262 return;
264 Inkscape::XML::Document* xml_doc = sp_document_repr_doc(desktop->doc());
265 Inkscape::Selection *selection = sp_desktop_selection(desktop);
267 // check if something is selected
268 if (selection->isEmpty()) {
269 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to duplicate."));
270 return;
271 }
273 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
275 selection->clear();
277 // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
278 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
280 GSList *newsel = NULL;
282 while (reprs) {
283 Inkscape::XML::Node *parent = ((Inkscape::XML::Node *) reprs->data)->parent();
284 Inkscape::XML::Node *copy = ((Inkscape::XML::Node *) reprs->data)->duplicate(xml_doc);
286 parent->appendChild(copy);
288 newsel = g_slist_prepend(newsel, copy);
289 reprs = g_slist_remove(reprs, reprs->data);
290 Inkscape::GC::release(copy);
291 }
293 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_DUPLICATE,
294 _("Duplicate"));
296 selection->setReprList(newsel);
298 g_slist_free(newsel);
299 }
301 void sp_edit_clear_all()
302 {
303 SPDesktop *dt = SP_ACTIVE_DESKTOP;
304 if (!dt)
305 return;
307 SPDocument *doc = sp_desktop_document(dt);
308 sp_desktop_selection(dt)->clear();
310 g_return_if_fail(SP_IS_GROUP(dt->currentLayer()));
311 GSList *items = sp_item_group_item_list(SP_GROUP(dt->currentLayer()));
313 while (items) {
314 SP_OBJECT (items->data)->deleteObject();
315 items = g_slist_remove(items, items->data);
316 }
318 sp_document_done(doc, SP_VERB_EDIT_CLEAR_ALL,
319 _("Delete all"));
320 }
322 GSList *
323 get_all_items (GSList *list, SPObject *from, SPDesktop *desktop, bool onlyvisible, bool onlysensitive, GSList const *exclude)
324 {
325 for (SPObject *child = sp_object_first_child(SP_OBJECT(from)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
326 if (SP_IS_ITEM(child) &&
327 !desktop->isLayer(SP_ITEM(child)) &&
328 (!onlysensitive || !SP_ITEM(child)->isLocked()) &&
329 (!onlyvisible || !desktop->itemIsHidden(SP_ITEM(child))) &&
330 (!exclude || !g_slist_find ((GSList *) exclude, child))
331 )
332 {
333 list = g_slist_prepend (list, SP_ITEM(child));
334 }
336 if (SP_IS_ITEM(child) && desktop->isLayer(SP_ITEM(child))) {
337 list = get_all_items (list, child, desktop, onlyvisible, onlysensitive, exclude);
338 }
339 }
341 return list;
342 }
344 void sp_edit_select_all_full (bool force_all_layers, bool invert)
345 {
346 SPDesktop *dt = SP_ACTIVE_DESKTOP;
347 if (!dt)
348 return;
350 Inkscape::Selection *selection = sp_desktop_selection(dt);
352 g_return_if_fail(SP_IS_GROUP(dt->currentLayer()));
354 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
355 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
356 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
358 GSList *items = NULL;
360 GSList const *exclude = NULL;
361 if (invert) {
362 exclude = selection->itemList();
363 }
365 if (force_all_layers)
366 inlayer = PREFS_SELECTION_ALL;
368 switch (inlayer) {
369 case PREFS_SELECTION_LAYER: {
370 if ( (onlysensitive && SP_ITEM(dt->currentLayer())->isLocked()) ||
371 (onlyvisible && dt->itemIsHidden(SP_ITEM(dt->currentLayer()))) )
372 return;
374 GSList *all_items = sp_item_group_item_list(SP_GROUP(dt->currentLayer()));
376 for (GSList *i = all_items; i; i = i->next) {
377 SPItem *item = SP_ITEM (i->data);
379 if (item && (!onlysensitive || !item->isLocked())) {
380 if (!onlyvisible || !dt->itemIsHidden(item)) {
381 if (!dt->isLayer(item)) {
382 if (!invert || !g_slist_find ((GSList *) exclude, item)) {
383 items = g_slist_prepend (items, item); // leave it in the list
384 }
385 }
386 }
387 }
388 }
390 g_slist_free (all_items);
391 break;
392 }
393 case PREFS_SELECTION_LAYER_RECURSIVE: {
394 items = get_all_items (NULL, dt->currentLayer(), dt, onlyvisible, onlysensitive, exclude);
395 break;
396 }
397 default: {
398 items = get_all_items (NULL, dt->currentRoot(), dt, onlyvisible, onlysensitive, exclude);
399 break;
400 }
401 }
403 selection->setList (items);
405 if (items) {
406 g_slist_free (items);
407 }
408 }
410 void sp_edit_select_all ()
411 {
412 sp_edit_select_all_full (false, false);
413 }
415 void sp_edit_select_all_in_all_layers ()
416 {
417 sp_edit_select_all_full (true, false);
418 }
420 void sp_edit_invert ()
421 {
422 sp_edit_select_all_full (false, true);
423 }
425 void sp_edit_invert_in_all_layers ()
426 {
427 sp_edit_select_all_full (true, true);
428 }
430 void sp_selection_group()
431 {
432 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
433 if (desktop == NULL)
434 return;
436 SPDocument *doc = sp_desktop_document (desktop);
437 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
439 Inkscape::Selection *selection = sp_desktop_selection(desktop);
441 // Check if something is selected.
442 if (selection->isEmpty()) {
443 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>some objects</b> to group."));
444 return;
445 }
447 GSList const *l = (GSList *) selection->reprList();
449 GSList *p = g_slist_copy((GSList *) l);
451 selection->clear();
453 p = g_slist_sort(p, (GCompareFunc) sp_repr_compare_position);
455 // Remember the position and parent of the topmost object.
456 gint topmost = ((Inkscape::XML::Node *) g_slist_last(p)->data)->position();
457 Inkscape::XML::Node *topmost_parent = ((Inkscape::XML::Node *) g_slist_last(p)->data)->parent();
459 Inkscape::XML::Node *group = xml_doc->createElement("svg:g");
461 while (p) {
462 Inkscape::XML::Node *current = (Inkscape::XML::Node *) p->data;
464 if (current->parent() == topmost_parent) {
465 Inkscape::XML::Node *spnew = current->duplicate(xml_doc);
466 sp_repr_unparent(current);
467 group->appendChild(spnew);
468 Inkscape::GC::release(spnew);
469 topmost --; // only reduce count for those items deleted from topmost_parent
470 } else { // move it to topmost_parent first
471 GSList *temp_clip = NULL;
473 // At this point, current may already have no item, due to its being a clone whose original is already moved away
474 // So we copy it artificially calculating the transform from its repr->attr("transform") and the parent transform
475 gchar const *t_str = current->attribute("transform");
476 NR::Matrix item_t (NR::identity());
477 if (t_str)
478 sp_svg_transform_read(t_str, &item_t);
479 item_t *= sp_item_i2doc_affine(SP_ITEM(doc->getObjectByRepr(current->parent())));
480 //FIXME: when moving both clone and original from a transformed group (either by
481 //grouping into another parent, or by cut/paste) the transform from the original's
482 //parent becomes embedded into original itself, and this affects its clones. Fix
483 //this by remembering the transform diffs we write to each item into an array and
484 //then, if this is clone, looking up its original in that array and pre-multiplying
485 //it by the inverse of that original's transform diff.
487 sp_selection_copy_one (current, item_t, &temp_clip, xml_doc);
488 sp_repr_unparent(current);
490 // paste into topmost_parent (temporarily)
491 GSList *copied = sp_selection_paste_impl (doc, doc->getObjectByRepr(topmost_parent), &temp_clip, NULL);
492 if (temp_clip) g_slist_free (temp_clip);
493 if (copied) { // if success,
494 // take pasted object (now in topmost_parent)
495 Inkscape::XML::Node *in_topmost = (Inkscape::XML::Node *) copied->data;
496 // make a copy
497 Inkscape::XML::Node *spnew = in_topmost->duplicate(xml_doc);
498 // remove pasted
499 sp_repr_unparent(in_topmost);
500 // put its copy into group
501 group->appendChild(spnew);
502 Inkscape::GC::release(spnew);
503 g_slist_free (copied);
504 }
505 }
506 p = g_slist_remove(p, current);
507 }
509 // Add the new group to the topmost members' parent
510 topmost_parent->appendChild(group);
512 // Move to the position of the topmost, reduced by the number of items deleted from topmost_parent
513 group->setPosition(topmost + 1);
515 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_GROUP,
516 _("Group"));
518 selection->set(group);
519 Inkscape::GC::release(group);
520 }
522 void sp_selection_ungroup()
523 {
524 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
525 if (desktop == NULL)
526 return;
528 Inkscape::Selection *selection = sp_desktop_selection(desktop);
530 if (selection->isEmpty()) {
531 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>group</b> to ungroup."));
532 return;
533 }
535 GSList *items = g_slist_copy((GSList *) selection->itemList());
536 selection->clear();
538 // Get a copy of current selection.
539 GSList *new_select = NULL;
540 bool ungrouped = false;
541 for (GSList *i = items;
542 i != NULL;
543 i = i->next)
544 {
545 SPItem *group = (SPItem *) i->data;
547 // when ungrouping cloned groups with their originals, some objects that were selected may no more exist due to unlinking
548 if (!SP_IS_OBJECT(group)) {
549 continue;
550 }
552 /* We do not allow ungrouping <svg> etc. (lauris) */
553 if (strcmp(SP_OBJECT_REPR(group)->name(), "svg:g") && strcmp(SP_OBJECT_REPR(group)->name(), "svg:switch")) {
554 // keep the non-group item in the new selection
555 selection->add(group);
556 continue;
557 }
559 GSList *children = NULL;
560 /* This is not strictly required, but is nicer to rely on group ::destroy (lauris) */
561 sp_item_group_ungroup(SP_GROUP(group), &children, false);
562 ungrouped = true;
563 // Add ungrouped items to the new selection.
564 new_select = g_slist_concat(new_select, children);
565 }
567 if (new_select) { // Set new selection.
568 selection->addList(new_select);
569 g_slist_free(new_select);
570 }
571 if (!ungrouped) {
572 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No groups</b> to ungroup in the selection."));
573 }
575 g_slist_free(items);
577 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_UNGROUP,
578 _("Ungroup"));
579 }
581 static SPGroup *
582 sp_item_list_common_parent_group(GSList const *items)
583 {
584 if (!items) {
585 return NULL;
586 }
587 SPObject *parent = SP_OBJECT_PARENT(items->data);
588 /* Strictly speaking this CAN happen, if user selects <svg> from Inkscape::XML editor */
589 if (!SP_IS_GROUP(parent)) {
590 return NULL;
591 }
592 for (items = items->next; items; items = items->next) {
593 if (SP_OBJECT_PARENT(items->data) != parent) {
594 return NULL;
595 }
596 }
598 return SP_GROUP(parent);
599 }
601 /** Finds out the minimum common bbox of the selected items. */
602 static NR::Maybe<NR::Rect>
603 enclose_items(GSList const *items)
604 {
605 g_assert(items != NULL);
607 NR::Maybe<NR::Rect> r = NR::Nothing();
608 for (GSList const *i = items; i; i = i->next) {
609 r = NR::union_bounds(r, sp_item_bbox_desktop((SPItem *) i->data));
610 }
611 return r;
612 }
614 SPObject *
615 prev_sibling(SPObject *child)
616 {
617 SPObject *parent = SP_OBJECT_PARENT(child);
618 if (!SP_IS_GROUP(parent)) {
619 return NULL;
620 }
621 for ( SPObject *i = sp_object_first_child(parent) ; i; i = SP_OBJECT_NEXT(i) ) {
622 if (i->next == child)
623 return i;
624 }
625 return NULL;
626 }
628 void
629 sp_selection_raise()
630 {
631 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
632 if (!desktop)
633 return;
635 Inkscape::Selection *selection = sp_desktop_selection(desktop);
637 GSList const *items = (GSList *) selection->itemList();
638 if (!items) {
639 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise."));
640 return;
641 }
643 SPGroup const *group = sp_item_list_common_parent_group(items);
644 if (!group) {
645 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
646 return;
647 }
649 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
651 /* Construct reverse-ordered list of selected children. */
652 GSList *rev = g_slist_copy((GSList *) items);
653 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
655 // Determine the common bbox of the selected items.
656 NR::Maybe<NR::Rect> selected = enclose_items(items);
658 // Iterate over all objects in the selection (starting from top).
659 if (selected) {
660 while (rev) {
661 SPObject *child = SP_OBJECT(rev->data);
662 // for each selected object, find the next sibling
663 for (SPObject *newref = child->next; newref; newref = newref->next) {
664 // if the sibling is an item AND overlaps our selection,
665 if (SP_IS_ITEM(newref)) {
666 NR::Maybe<NR::Rect> newref_bbox = sp_item_bbox_desktop(SP_ITEM(newref));
667 if ( newref_bbox && selected->intersects(*newref_bbox) ) {
668 // AND if it's not one of our selected objects,
669 if (!g_slist_find((GSList *) items, newref)) {
670 // move the selected object after that sibling
671 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(newref));
672 }
673 break;
674 }
675 }
676 }
677 rev = g_slist_remove(rev, child);
678 }
679 } else {
680 g_slist_free(rev);
681 }
683 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_RAISE,
684 _("Raise"));
685 }
687 void sp_selection_raise_to_top()
688 {
689 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
690 if (desktop == NULL)
691 return;
693 SPDocument *document = sp_desktop_document(desktop);
694 Inkscape::Selection *selection = sp_desktop_selection(desktop);
696 if (selection->isEmpty()) {
697 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise to top."));
698 return;
699 }
701 GSList const *items = (GSList *) selection->itemList();
703 SPGroup const *group = sp_item_list_common_parent_group(items);
704 if (!group) {
705 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
706 return;
707 }
709 GSList *rl = g_slist_copy((GSList *) selection->reprList());
710 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
712 for (GSList *l = rl; l != NULL; l = l->next) {
713 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
714 repr->setPosition(-1);
715 }
717 g_slist_free(rl);
719 sp_document_done(document, SP_VERB_SELECTION_TO_FRONT,
720 _("Raise to top"));
721 }
723 void
724 sp_selection_lower()
725 {
726 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
727 if (desktop == NULL)
728 return;
730 Inkscape::Selection *selection = sp_desktop_selection(desktop);
732 GSList const *items = (GSList *) selection->itemList();
733 if (!items) {
734 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower."));
735 return;
736 }
738 SPGroup const *group = sp_item_list_common_parent_group(items);
739 if (!group) {
740 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
741 return;
742 }
744 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
746 // Determine the common bbox of the selected items.
747 NR::Maybe<NR::Rect> selected = enclose_items(items);
749 /* Construct direct-ordered list of selected children. */
750 GSList *rev = g_slist_copy((GSList *) items);
751 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
752 rev = g_slist_reverse(rev);
754 // Iterate over all objects in the selection (starting from top).
755 if (selected) {
756 while (rev) {
757 SPObject *child = SP_OBJECT(rev->data);
758 // for each selected object, find the prev sibling
759 for (SPObject *newref = prev_sibling(child); newref; newref = prev_sibling(newref)) {
760 // if the sibling is an item AND overlaps our selection,
761 if (SP_IS_ITEM(newref)) {
762 NR::Maybe<NR::Rect> ref_bbox = sp_item_bbox_desktop(SP_ITEM(newref));
763 if ( ref_bbox && selected->intersects(*ref_bbox) ) {
764 // AND if it's not one of our selected objects,
765 if (!g_slist_find((GSList *) items, newref)) {
766 // move the selected object before that sibling
767 SPObject *put_after = prev_sibling(newref);
768 if (put_after)
769 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(put_after));
770 else
771 SP_OBJECT_REPR(child)->setPosition(0);
772 }
773 break;
774 }
775 }
776 }
777 rev = g_slist_remove(rev, child);
778 }
779 } else {
780 g_slist_free(rev);
781 }
783 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_LOWER,
784 _("Lower"));
785 }
787 void sp_selection_lower_to_bottom()
788 {
789 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
790 if (desktop == NULL)
791 return;
793 SPDocument *document = sp_desktop_document(desktop);
794 Inkscape::Selection *selection = sp_desktop_selection(desktop);
796 if (selection->isEmpty()) {
797 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower to bottom."));
798 return;
799 }
801 GSList const *items = (GSList *) selection->itemList();
803 SPGroup const *group = sp_item_list_common_parent_group(items);
804 if (!group) {
805 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
806 return;
807 }
809 GSList *rl;
810 rl = g_slist_copy((GSList *) selection->reprList());
811 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
812 rl = g_slist_reverse(rl);
814 for (GSList *l = rl; l != NULL; l = l->next) {
815 gint minpos;
816 SPObject *pp, *pc;
817 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
818 pp = document->getObjectByRepr(sp_repr_parent(repr));
819 minpos = 0;
820 g_assert(SP_IS_GROUP(pp));
821 pc = sp_object_first_child(pp);
822 while (!SP_IS_ITEM(pc)) {
823 minpos += 1;
824 pc = pc->next;
825 }
826 repr->setPosition(minpos);
827 }
829 g_slist_free(rl);
831 sp_document_done(document, SP_VERB_SELECTION_TO_BACK,
832 _("Lower to bottom"));
833 }
835 void
836 sp_undo(SPDesktop *desktop, SPDocument *)
837 {
838 if (!sp_document_undo(sp_desktop_document(desktop)))
839 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to undo."));
840 }
842 void
843 sp_redo(SPDesktop *desktop, SPDocument *)
844 {
845 if (!sp_document_redo(sp_desktop_document(desktop)))
846 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to redo."));
847 }
849 void sp_selection_cut()
850 {
851 sp_selection_copy();
852 sp_selection_delete();
853 }
855 void sp_copy_gradient (GSList **defs_clip, SPGradient *gradient, Inkscape::XML::Document* xml_doc)
856 {
857 SPGradient *ref = gradient;
859 while (ref) {
860 // climb up the refs, copying each one in the chain
861 Inkscape::XML::Node *grad_repr = SP_OBJECT_REPR(ref)->duplicate(xml_doc);
862 *defs_clip = g_slist_prepend (*defs_clip, grad_repr);
864 ref = ref->ref->getObject();
865 }
866 }
868 void sp_copy_pattern (GSList **defs_clip, SPPattern *pattern, Inkscape::XML::Document* xml_doc)
869 {
870 SPPattern *ref = pattern;
872 while (ref) {
873 // climb up the refs, copying each one in the chain
874 Inkscape::XML::Node *pattern_repr = SP_OBJECT_REPR(ref)->duplicate(xml_doc);
875 *defs_clip = g_slist_prepend (*defs_clip, pattern_repr);
877 // items in the pattern may also use gradients and other patterns, so we need to recurse here as well
878 for (SPObject *child = sp_object_first_child(SP_OBJECT(ref)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
879 if (!SP_IS_ITEM (child))
880 continue;
881 sp_copy_stuff_used_by_item (defs_clip, (SPItem *) child, NULL, xml_doc);
882 }
884 ref = ref->ref->getObject();
885 }
886 }
888 void sp_copy_single (GSList **defs_clip, SPObject *thing, Inkscape::XML::Document* xml_doc)
889 {
890 Inkscape::XML::Node *duplicate_repr = SP_OBJECT_REPR(thing)->duplicate(xml_doc);
891 *defs_clip = g_slist_prepend (*defs_clip, duplicate_repr);
892 }
895 void sp_copy_textpath_path (GSList **defs_clip, SPTextPath *tp, GSList const *items, Inkscape::XML::Document* xml_doc)
896 {
897 SPItem *path = sp_textpath_get_path_item (tp);
898 if (!path)
899 return;
900 if (items && g_slist_find ((GSList *) items, path)) // do not copy it to defs if it is already in the list of items copied
901 return;
902 Inkscape::XML::Node *repr = SP_OBJECT_REPR(path)->duplicate(xml_doc);
903 *defs_clip = g_slist_prepend (*defs_clip, repr);
904 }
906 /**
907 * Copies things like patterns, markers, gradients, etc.
908 */
909 void sp_copy_stuff_used_by_item (GSList **defs_clip, SPItem *item, GSList const *items, Inkscape::XML::Document* xml_doc)
910 {
911 SPStyle *style = SP_OBJECT_STYLE (item);
913 if (style && (style->fill.isPaintserver())) {
914 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
915 if (SP_IS_LINEARGRADIENT (server) || SP_IS_RADIALGRADIENT (server))
916 sp_copy_gradient (defs_clip, SP_GRADIENT(server), xml_doc);
917 if (SP_IS_PATTERN (server))
918 sp_copy_pattern (defs_clip, SP_PATTERN(server), xml_doc);
919 }
921 if (style && (style->stroke.isPaintserver())) {
922 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
923 if (SP_IS_LINEARGRADIENT (server) || SP_IS_RADIALGRADIENT (server))
924 sp_copy_gradient (defs_clip, SP_GRADIENT(server), xml_doc);
925 if (SP_IS_PATTERN (server))
926 sp_copy_pattern (defs_clip, SP_PATTERN(server), xml_doc);
927 }
929 // For shapes, copy all of the shape's markers into defs_clip
930 if (SP_IS_SHAPE (item)) {
931 SPShape *shape = SP_SHAPE (item);
932 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
933 if (shape->marker[i]) {
934 sp_copy_single (defs_clip, SP_OBJECT (shape->marker[i]), xml_doc);
935 }
936 }
938 // For shapes, also copy liveeffect if applicable
939 if (sp_shape_has_path_effect(shape)) {
940 sp_copy_single (defs_clip, SP_OBJECT(sp_shape_get_livepatheffectobject(shape)), xml_doc);
941 }
942 }
944 if (SP_IS_TEXT_TEXTPATH (item)) {
945 sp_copy_textpath_path (defs_clip, SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))), items, xml_doc);
946 }
948 if (item->clip_ref->getObject()) {
949 sp_copy_single (defs_clip, item->clip_ref->getObject(), xml_doc);
950 }
952 if (item->mask_ref->getObject()) {
953 SPObject *mask = item->mask_ref->getObject();
954 sp_copy_single (defs_clip, mask, xml_doc);
955 // recurse into the mask for its gradients etc.
956 for (SPObject *o = SP_OBJECT(mask)->children; o != NULL; o = o->next) {
957 if (SP_IS_ITEM(o))
958 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (o), items, xml_doc);
959 }
960 }
962 if (style->getFilter()) {
963 SPObject *filter = style->getFilter();
964 if (SP_IS_FILTER(filter)) {
965 sp_copy_single (defs_clip, filter, xml_doc);
966 }
967 }
969 // recurse
970 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
971 if (SP_IS_ITEM(o))
972 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (o), items, xml_doc);
973 }
974 }
976 void
977 sp_set_style_clipboard (SPCSSAttr *css)
978 {
979 if (css != NULL) {
980 // clear style clipboard
981 if (style_clipboard) {
982 sp_repr_css_attr_unref (style_clipboard);
983 style_clipboard = NULL;
984 }
985 //sp_repr_css_print (css);
986 style_clipboard = css;
987 }
988 }
990 /**
991 * \pre item != NULL
992 */
993 SPCSSAttr *
994 take_style_from_item (SPItem *item)
995 {
996 // write the complete cascaded style, context-free
997 SPCSSAttr *css = sp_css_attr_from_object (SP_OBJECT(item), SP_STYLE_FLAG_ALWAYS);
998 if (css == NULL)
999 return NULL;
1001 if ((SP_IS_GROUP(item) && SP_OBJECT(item)->children) ||
1002 (SP_IS_TEXT (item) && SP_OBJECT(item)->children && SP_OBJECT(item)->children->next == NULL)) {
1003 // if this is a text with exactly one tspan child, merge the style of that tspan as well
1004 // If this is a group, merge the style of its topmost (last) child with style
1005 for (SPObject *last_element = item->lastChild(); last_element != NULL; last_element = SP_OBJECT_PREV (last_element)) {
1006 if (SP_OBJECT_STYLE (last_element) != NULL) {
1007 SPCSSAttr *temp = sp_css_attr_from_object (last_element, SP_STYLE_FLAG_IFSET);
1008 if (temp) {
1009 sp_repr_css_merge (css, temp);
1010 sp_repr_css_attr_unref (temp);
1011 }
1012 break;
1013 }
1014 }
1015 }
1016 if (!(SP_IS_TEXT (item) || SP_IS_TSPAN (item) || SP_IS_TREF(item) || SP_IS_STRING (item))) {
1017 // do not copy text properties from non-text objects, it's confusing
1018 css = sp_css_attr_unset_text (css);
1019 }
1021 // FIXME: also transform gradient/pattern fills, by forking? NO, this must be nondestructive
1022 double ex = NR::expansion(sp_item_i2doc_affine(item));
1023 if (ex != 1.0) {
1024 css = sp_css_attr_scale (css, ex);
1025 }
1027 return css;
1028 }
1031 void sp_selection_copy()
1032 {
1033 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1034 if (desktop == NULL)
1035 return;
1037 if (!clipboard_document) {
1038 clipboard_document = new Inkscape::XML::SimpleDocument();
1039 }
1041 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1043 if (tools_isactive (desktop, TOOLS_DROPPER)) {
1044 sp_dropper_context_copy(desktop->event_context);
1045 return; // copied color under cursor, nothing else to do
1046 }
1048 if (desktop->event_context->get_drag() && desktop->event_context->get_drag()->copy()) {
1049 return; // copied selected stop(s), nothing else to do
1050 }
1052 // check if something is selected
1053 if (selection->isEmpty()) {
1054 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing was copied."));
1055 return;
1056 }
1058 GSList const *items = g_slist_copy ((GSList *) selection->itemList());
1060 // 0. Copy text to system clipboard
1061 // FIXME: for non-texts, put serialized Inkscape::XML as text to the clipboard;
1062 //for this sp_repr_write_stream needs to be rewritten with iostream instead of FILE
1063 Glib::ustring text;
1064 if (tools_isactive (desktop, TOOLS_TEXT)) {
1065 text = sp_text_get_selected_text(desktop->event_context);
1066 }
1068 if (text.empty()) {
1069 guint texts = 0;
1070 for (GSList *i = (GSList *) items; i; i = i->next) {
1071 SPItem *item = SP_ITEM (i->data);
1072 if (SP_IS_TEXT (item) || SP_IS_FLOWTEXT(item)) {
1073 if (texts > 0) // if more than one text object is copied, separate them by spaces
1074 text += " ";
1075 gchar *this_text = sp_te_get_string_multiline (item);
1076 if (this_text) {
1077 text += this_text;
1078 g_free(this_text);
1079 }
1080 texts++;
1081 }
1082 }
1083 }
1084 if (!text.empty()) {
1085 Glib::RefPtr<Gtk::Clipboard> refClipboard = Gtk::Clipboard::get();
1086 refClipboard->set_text(text);
1087 }
1089 // clear old defs clipboard
1090 while (defs_clipboard) {
1091 Inkscape::GC::release((Inkscape::XML::Node *) defs_clipboard->data);
1092 defs_clipboard = g_slist_remove (defs_clipboard, defs_clipboard->data);
1093 }
1095 // clear style clipboard
1096 if (style_clipboard) {
1097 sp_repr_css_attr_unref (style_clipboard);
1098 style_clipboard = NULL;
1099 }
1101 //clear main clipboard
1102 while (clipboard) {
1103 Inkscape::GC::release((Inkscape::XML::Node *) clipboard->data);
1104 clipboard = g_slist_remove(clipboard, clipboard->data);
1105 }
1107 sp_selection_copy_impl (items, &clipboard, &defs_clipboard, &style_clipboard, clipboard_document);
1109 if (tools_isactive (desktop, TOOLS_TEXT)) { // take style from cursor/text selection, overwriting the style just set by copy_impl
1110 SPStyle *const query = sp_style_new(SP_ACTIVE_DOCUMENT);
1111 if (sp_desktop_query_style_all (desktop, query)) {
1112 SPCSSAttr *css = sp_css_attr_from_style (query, SP_STYLE_FLAG_ALWAYS);
1113 sp_set_style_clipboard (css);
1114 }
1115 sp_style_unref(query);
1116 }
1118 size_clipboard = selection->bounds();
1120 g_slist_free ((GSList *) items);
1121 }
1123 void sp_selection_paste(bool in_place)
1124 {
1125 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1127 if (desktop == NULL) {
1128 return;
1129 }
1131 SPDocument *document = sp_desktop_document(desktop);
1133 if (Inkscape::have_viable_layer(desktop, desktop->messageStack()) == false) {
1134 return;
1135 }
1137 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1139 if (tools_isactive (desktop, TOOLS_TEXT)) {
1140 if (sp_text_paste_inline(desktop->event_context))
1141 return; // pasted from system clipboard into text, nothing else to do
1142 }
1144 // check if something is in the clipboard
1145 if (clipboard == NULL) {
1146 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1147 return;
1148 }
1150 GSList *copied = sp_selection_paste_impl(document, desktop->currentLayer(), &clipboard, &defs_clipboard);
1151 // add pasted objects to selection
1152 selection->setReprList((GSList const *) copied);
1153 g_slist_free (copied);
1155 if (!in_place) {
1156 sp_document_ensure_up_to_date(document);
1158 NR::Maybe<NR::Rect> sel_bbox = selection->bounds();
1159 NR::Point m( desktop->point() );
1160 if (sel_bbox) {
1161 m -= sel_bbox->midpoint();
1162 }
1164 sp_selection_move_relative(selection, m);
1165 }
1167 sp_document_done(document, SP_VERB_EDIT_PASTE, _("Paste"));
1168 }
1170 void sp_selection_paste_style()
1171 {
1172 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1173 if (desktop == NULL) return;
1175 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1177 // check if something is in the clipboard
1178 if (style_clipboard == NULL) {
1179 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the style clipboard."));
1180 return;
1181 }
1183 // check if something is selected
1184 if (selection->isEmpty()) {
1185 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste style to."));
1186 return;
1187 }
1189 paste_defs (&defs_clipboard, sp_desktop_document(desktop));
1191 sp_desktop_set_style (desktop, style_clipboard);
1193 sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_STYLE,
1194 _("Paste style"));
1195 }
1197 void sp_selection_paste_livepatheffect()
1198 {
1199 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1200 if (desktop == NULL) return;
1202 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1204 // check if something is in the clipboard
1205 if (clipboard == NULL) {
1206 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1207 return;
1208 }
1210 // check if something is selected
1211 if (selection->isEmpty()) {
1212 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste live path effect to."));
1213 return;
1214 }
1216 paste_defs (&defs_clipboard, sp_desktop_document(desktop));
1218 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) clipboard->data;
1219 char const *effectstr = repr->attribute("inkscape:path-effect");
1220 if (!effectstr) {
1221 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Clipboard does not contain a live path effect."));
1222 return;
1223 }
1225 for ( GSList const *itemlist = selection->itemList(); itemlist != NULL; itemlist = g_slist_next(itemlist) ) {
1226 SPItem *item = reinterpret_cast<SPItem*>(itemlist->data);
1227 if ( item && SP_IS_SHAPE(item) ) {
1228 Inkscape::XML::Node *selrepr = (Inkscape::XML::Node *) SP_OBJECT_REPR(item);
1229 selrepr->setAttribute("inkscape:path-effect", effectstr);
1231 // set inkscape:original-d for paths. the other shapes don't need this.
1232 if ( SP_IS_PATH(item) ) {
1233 Inkscape::XML::Node *pathrepr = SP_OBJECT_REPR(item);
1234 if ( ! pathrepr->attribute("inkscape:original-d") ) {
1235 pathrepr->setAttribute("inkscape:original-d", pathrepr->attribute("d"));
1236 }
1237 }
1238 }
1239 }
1241 sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_LIVEPATHEFFECT,
1242 _("Paste live path effect"));
1243 }
1245 void sp_selection_paste_size (bool apply_x, bool apply_y)
1246 {
1247 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1248 if (desktop == NULL) return;
1250 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1252 // check if something is in the clipboard
1253 if (!size_clipboard) {
1254 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1255 return;
1256 }
1258 // check if something is selected
1259 if (selection->isEmpty()) {
1260 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste size to."));
1261 return;
1262 }
1264 NR::Maybe<NR::Rect> current = selection->bounds();
1265 if ( !current || current->isEmpty() ) {
1266 return;
1267 }
1269 double scale_x = size_clipboard->extent(NR::X) / current->extent(NR::X);
1270 double scale_y = size_clipboard->extent(NR::Y) / current->extent(NR::Y);
1272 sp_selection_scale_relative (selection, current->midpoint(),
1273 NR::scale(
1274 apply_x? scale_x : (desktop->isToolboxButtonActive ("lock")? scale_y : 1.0),
1275 apply_y? scale_y : (desktop->isToolboxButtonActive ("lock")? scale_x : 1.0)));
1277 sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_SIZE,
1278 _("Paste size"));
1279 }
1281 void sp_selection_paste_size_separately (bool apply_x, bool apply_y)
1282 {
1283 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1284 if (desktop == NULL) return;
1286 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1288 // check if something is in the clipboard
1289 if ( !size_clipboard ) {
1290 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1291 return;
1292 }
1294 // check if something is selected
1295 if (selection->isEmpty()) {
1296 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste size to."));
1297 return;
1298 }
1300 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1301 SPItem *item = SP_ITEM(l->data);
1303 NR::Maybe<NR::Rect> current = sp_item_bbox_desktop(item);
1304 if ( !current || current->isEmpty() ) {
1305 continue;
1306 }
1308 double scale_x = size_clipboard->extent(NR::X) / current->extent(NR::X);
1309 double scale_y = size_clipboard->extent(NR::Y) / current->extent(NR::Y);
1311 sp_item_scale_rel (item,
1312 NR::scale(
1313 apply_x? scale_x : (desktop->isToolboxButtonActive ("lock")? scale_y : 1.0),
1314 apply_y? scale_y : (desktop->isToolboxButtonActive ("lock")? scale_x : 1.0)));
1316 }
1318 sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_SIZE_SEPARATELY,
1319 _("Paste size separately"));
1320 }
1322 void sp_selection_to_next_layer ()
1323 {
1324 SPDesktop *dt = SP_ACTIVE_DESKTOP;
1326 Inkscape::Selection *selection = sp_desktop_selection(dt);
1328 // check if something is selected
1329 if (selection->isEmpty()) {
1330 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer above."));
1331 return;
1332 }
1334 GSList const *items = g_slist_copy ((GSList *) selection->itemList());
1336 bool no_more = false; // Set to true, if no more layers above
1337 SPObject *next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer());
1338 if (next) {
1339 GSList *temp_clip = NULL;
1340 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
1341 sp_selection_delete_impl (items, false, false);
1342 next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
1343 GSList *copied;
1344 if(next) {
1345 copied = sp_selection_paste_impl (sp_desktop_document (dt), next, &temp_clip, NULL);
1346 } else {
1347 copied = sp_selection_paste_impl (sp_desktop_document (dt), dt->currentLayer(), &temp_clip, NULL);
1348 no_more = true;
1349 }
1350 selection->setReprList((GSList const *) copied);
1351 g_slist_free (copied);
1352 if (temp_clip) g_slist_free (temp_clip);
1353 if (next) dt->setCurrentLayer(next);
1354 sp_document_done(sp_desktop_document (dt), SP_VERB_LAYER_MOVE_TO_NEXT,
1355 _("Raise to next layer"));
1356 } else {
1357 no_more = true;
1358 }
1360 if (no_more) {
1361 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers above."));
1362 }
1364 g_slist_free ((GSList *) items);
1365 }
1367 void sp_selection_to_prev_layer ()
1368 {
1369 SPDesktop *dt = SP_ACTIVE_DESKTOP;
1371 Inkscape::Selection *selection = sp_desktop_selection(dt);
1373 // check if something is selected
1374 if (selection->isEmpty()) {
1375 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer below."));
1376 return;
1377 }
1379 GSList const *items = g_slist_copy ((GSList *) selection->itemList());
1381 bool no_more = false; // Set to true, if no more layers below
1382 SPObject *next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer());
1383 if (next) {
1384 GSList *temp_clip = NULL;
1385 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
1386 sp_selection_delete_impl (items, false, false);
1387 next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
1388 GSList *copied;
1389 if(next) {
1390 copied = sp_selection_paste_impl (sp_desktop_document (dt), next, &temp_clip, NULL);
1391 } else {
1392 copied = sp_selection_paste_impl (sp_desktop_document (dt), dt->currentLayer(), &temp_clip, NULL);
1393 no_more = true;
1394 }
1395 selection->setReprList((GSList const *) copied);
1396 g_slist_free (copied);
1397 if (temp_clip) g_slist_free (temp_clip);
1398 if (next) dt->setCurrentLayer(next);
1399 sp_document_done(sp_desktop_document (dt), SP_VERB_LAYER_MOVE_TO_PREV,
1400 _("Lower to previous layer"));
1401 } else {
1402 no_more = true;
1403 }
1405 if (no_more) {
1406 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers below."));
1407 }
1409 g_slist_free ((GSList *) items);
1410 }
1412 bool
1413 selection_contains_original (SPItem *item, Inkscape::Selection *selection)
1414 {
1415 bool contains_original = false;
1417 bool is_use = SP_IS_USE(item);
1418 SPItem *item_use = item;
1419 SPItem *item_use_first = item;
1420 while (is_use && item_use && !contains_original)
1421 {
1422 item_use = sp_use_get_original (SP_USE(item_use));
1423 contains_original |= selection->includes(item_use);
1424 if (item_use == item_use_first)
1425 break;
1426 is_use = SP_IS_USE(item_use);
1427 }
1429 // If it's a tref, check whether the object containing the character
1430 // data is part of the selection
1431 if (!contains_original && SP_IS_TREF(item)) {
1432 contains_original = selection->includes(SP_TREF(item)->getObjectReferredTo());
1433 }
1435 return contains_original;
1436 }
1439 bool
1440 selection_contains_both_clone_and_original (Inkscape::Selection *selection)
1441 {
1442 bool clone_with_original = false;
1443 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1444 SPItem *item = SP_ITEM(l->data);
1445 clone_with_original |= selection_contains_original(item, selection);
1446 if (clone_with_original)
1447 break;
1448 }
1449 return clone_with_original;
1450 }
1453 /** Apply matrix to the selection. \a set_i2d is normally true, which means objects are in the
1454 original transform, synced with their reprs, and need to jump to the new transform in one go. A
1455 value of set_i2d==false is only used by seltrans when it's dragging objects live (not outlines); in
1456 that case, items are already in the new position, but the repr is in the old, and this function
1457 then simply updates the repr from item->transform.
1458 */
1459 void sp_selection_apply_affine(Inkscape::Selection *selection, NR::Matrix const &affine, bool set_i2d)
1460 {
1461 if (selection->isEmpty())
1462 return;
1464 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1465 SPItem *item = SP_ITEM(l->data);
1467 NR::Point old_center(0,0);
1468 if (set_i2d && item->isCenterSet())
1469 old_center = item->getCenter();
1471 #if 0 /* Re-enable this once persistent guides have a graphical indication.
1472 At the time of writing, this is the only place to re-enable. */
1473 sp_item_update_cns(*item, selection->desktop());
1474 #endif
1476 // we're moving both a clone and its original or any ancestor in clone chain?
1477 bool transform_clone_with_original = selection_contains_original(item, selection);
1478 // ...both a text-on-path and its path?
1479 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)))) ));
1480 // ...both a flowtext and its frame?
1481 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)
1482 // ...both an offset and its source?
1483 bool transform_offset_with_source = (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) && selection->includes( sp_offset_get_source (SP_OFFSET(item)) );
1485 // If we're moving a connector, we want to detach it
1486 // from shapes that aren't part of the selection, but
1487 // leave it attached if they are
1488 if (cc_item_is_connector(item)) {
1489 SPItem *attItem[2];
1490 SP_PATH(item)->connEndPair.getAttachedItems(attItem);
1492 for (int n = 0; n < 2; ++n) {
1493 if (!selection->includes(attItem[n])) {
1494 sp_conn_end_detach(item, n);
1495 }
1496 }
1497 }
1499 // "clones are unmoved when original is moved" preference
1500 int compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
1501 bool prefs_unmoved = (compensation == SP_CLONE_COMPENSATION_UNMOVED);
1502 bool prefs_parallel = (compensation == SP_CLONE_COMPENSATION_PARALLEL);
1504 // If this is a clone and it's selected along with its original, do not move it; it will feel the
1505 // transform of its original and respond to it itself. Without this, a clone is doubly
1506 // transformed, very unintuitive.
1507 // Same for textpath if we are also doing ANY transform to its path: do not touch textpath,
1508 // letters cannot be squeezed or rotated anyway, they only refill the changed path.
1509 // Same for linked offset if we are also moving its source: do not move it.
1510 if (transform_textpath_with_path || transform_offset_with_source) {
1511 // restore item->transform field from the repr, in case it was changed by seltrans
1512 sp_object_read_attr (SP_OBJECT (item), "transform");
1514 } else if (transform_flowtext_with_frame) {
1515 // apply the inverse of the region's transform to the <use> so that the flow remains
1516 // the same (even though the output itself gets transformed)
1517 for (SPObject *region = item->firstChild() ; region ; region = SP_OBJECT_NEXT(region)) {
1518 if (!SP_IS_FLOWREGION(region) && !SP_IS_FLOWREGIONEXCLUDE(region))
1519 continue;
1520 for (SPObject *use = region->firstChild() ; use ; use = SP_OBJECT_NEXT(use)) {
1521 if (!SP_IS_USE(use)) continue;
1522 sp_item_write_transform(SP_USE(use), SP_OBJECT_REPR(use), item->transform.inverse(), NULL);
1523 }
1524 }
1525 } else if (transform_clone_with_original) {
1526 // We are transforming a clone along with its original. The below matrix juggling is
1527 // necessary to ensure that they transform as a whole, i.e. the clone's induced
1528 // transform and its move compensation are both cancelled out.
1530 // restore item->transform field from the repr, in case it was changed by seltrans
1531 sp_object_read_attr (SP_OBJECT (item), "transform");
1533 // calculate the matrix we need to apply to the clone to cancel its induced transform from its original
1534 NR::Matrix parent_transform = sp_item_i2root_affine(SP_ITEM(SP_OBJECT_PARENT (item)));
1535 NR::Matrix t = parent_transform * matrix_to_desktop (matrix_from_desktop (affine, item), item) * parent_transform.inverse();
1536 NR::Matrix t_inv =parent_transform * matrix_to_desktop (matrix_from_desktop (affine.inverse(), item), item) * parent_transform.inverse();
1537 NR::Matrix result = t_inv * item->transform * t;
1539 if ((prefs_parallel || prefs_unmoved) && affine.is_translation()) {
1540 // we need to cancel out the move compensation, too
1542 // find out the clone move, same as in sp_use_move_compensate
1543 NR::Matrix parent = sp_use_get_parent_transform (SP_USE(item));
1544 NR::Matrix clone_move = parent.inverse() * t * parent;
1546 if (prefs_parallel) {
1547 NR::Matrix move = result * clone_move * t_inv;
1548 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &move);
1550 } else if (prefs_unmoved) {
1551 //if (SP_IS_USE(sp_use_get_original(SP_USE(item))))
1552 // clone_move = NR::identity();
1553 NR::Matrix move = result * clone_move;
1554 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &t);
1555 }
1557 } else {
1558 // just apply the result
1559 sp_item_write_transform(item, SP_OBJECT_REPR(item), result, &t);
1560 }
1562 } else {
1563 if (set_i2d) {
1564 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * affine);
1565 }
1566 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, NULL);
1567 }
1569 // if we're moving the actual object, not just updating the repr, we can transform the
1570 // center by the same matrix (only necessary for non-translations)
1571 if (set_i2d && item->isCenterSet() && !affine.is_translation()) {
1572 item->setCenter(old_center * affine);
1573 SP_OBJECT(item)->updateRepr();
1574 }
1575 }
1576 }
1578 void sp_selection_remove_transform()
1579 {
1580 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1581 if (desktop == NULL)
1582 return;
1584 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1586 GSList const *l = (GSList *) selection->reprList();
1587 while (l != NULL) {
1588 sp_repr_set_attr((Inkscape::XML::Node*)l->data, "transform", NULL);
1589 l = l->next;
1590 }
1592 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_FLATTEN,
1593 _("Remove transform"));
1594 }
1596 void
1597 sp_selection_scale_absolute(Inkscape::Selection *selection,
1598 double const x0, double const x1,
1599 double const y0, double const y1)
1600 {
1601 if (selection->isEmpty())
1602 return;
1604 NR::Maybe<NR::Rect> const bbox(selection->bounds());
1605 if ( !bbox || bbox->isEmpty() ) {
1606 return;
1607 }
1609 NR::translate const p2o(-bbox->min());
1611 NR::scale const newSize(x1 - x0,
1612 y1 - y0);
1613 NR::scale const scale( newSize / NR::scale(bbox->dimensions()) );
1614 NR::translate const o2n(x0, y0);
1615 NR::Matrix const final( p2o * scale * o2n );
1617 sp_selection_apply_affine(selection, final);
1618 }
1621 void sp_selection_scale_relative(Inkscape::Selection *selection, NR::Point const &align, NR::scale const &scale)
1622 {
1623 if (selection->isEmpty())
1624 return;
1626 NR::Maybe<NR::Rect> const bbox(selection->bounds());
1628 if ( !bbox || bbox->isEmpty() ) {
1629 return;
1630 }
1632 // FIXME: ARBITRARY LIMIT: don't try to scale above 1 Mpx, it won't display properly and will crash sooner or later anyway
1633 if ( bbox->extent(NR::X) * scale[NR::X] > 1e6 ||
1634 bbox->extent(NR::Y) * scale[NR::Y] > 1e6 )
1635 {
1636 return;
1637 }
1639 NR::translate const n2d(-align);
1640 NR::translate const d2n(align);
1641 NR::Matrix const final( n2d * scale * d2n );
1642 sp_selection_apply_affine(selection, final);
1643 }
1645 void
1646 sp_selection_rotate_relative(Inkscape::Selection *selection, NR::Point const ¢er, gdouble const angle_degrees)
1647 {
1648 NR::translate const d2n(center);
1649 NR::translate const n2d(-center);
1650 NR::rotate const rotate(rotate_degrees(angle_degrees));
1651 NR::Matrix const final( NR::Matrix(n2d) * rotate * d2n );
1652 sp_selection_apply_affine(selection, final);
1653 }
1655 void
1656 sp_selection_skew_relative(Inkscape::Selection *selection, NR::Point const &align, double dx, double dy)
1657 {
1658 NR::translate const d2n(align);
1659 NR::translate const n2d(-align);
1660 NR::Matrix const skew(1, dy,
1661 dx, 1,
1662 0, 0);
1663 NR::Matrix const final( n2d * skew * d2n );
1664 sp_selection_apply_affine(selection, final);
1665 }
1667 void sp_selection_move_relative(Inkscape::Selection *selection, NR::Point const &move)
1668 {
1669 sp_selection_apply_affine(selection, NR::Matrix(NR::translate(move)));
1670 }
1672 void sp_selection_move_relative(Inkscape::Selection *selection, double dx, double dy)
1673 {
1674 sp_selection_apply_affine(selection, NR::Matrix(NR::translate(dx, dy)));
1675 }
1678 /**
1679 * \brief sp_selection_rotate_90
1680 *
1681 * This function rotates selected objects 90 degrees clockwise.
1682 *
1683 */
1685 void sp_selection_rotate_90_cw()
1686 {
1687 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1689 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1691 if (selection->isEmpty())
1692 return;
1694 GSList const *l = selection->itemList();
1695 NR::rotate const rot_neg_90(NR::Point(0, -1));
1696 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1697 SPItem *item = SP_ITEM(l2->data);
1698 sp_item_rotate_rel(item, rot_neg_90);
1699 }
1701 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_ROTATE_90_CCW,
1702 _("Rotate 90° CW"));
1703 }
1706 /**
1707 * \brief sp_selection_rotate_90_ccw
1708 *
1709 * This function rotates selected objects 90 degrees counter-clockwise.
1710 *
1711 */
1713 void sp_selection_rotate_90_ccw()
1714 {
1715 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1717 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1719 if (selection->isEmpty())
1720 return;
1722 GSList const *l = selection->itemList();
1723 NR::rotate const rot_neg_90(NR::Point(0, 1));
1724 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1725 SPItem *item = SP_ITEM(l2->data);
1726 sp_item_rotate_rel(item, rot_neg_90);
1727 }
1729 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_ROTATE_90_CW,
1730 _("Rotate 90° CCW"));
1731 }
1733 void
1734 sp_selection_rotate(Inkscape::Selection *selection, gdouble const angle_degrees)
1735 {
1736 if (selection->isEmpty())
1737 return;
1739 NR::Maybe<NR::Point> center = selection->center();
1740 if (!center) {
1741 return;
1742 }
1744 sp_selection_rotate_relative(selection, *center, angle_degrees);
1746 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1747 ( ( angle_degrees > 0 )
1748 ? "selector:rotate:ccw"
1749 : "selector:rotate:cw" ),
1750 SP_VERB_CONTEXT_SELECT,
1751 _("Rotate"));
1752 }
1754 /**
1755 \param angle the angle in "angular pixels", i.e. how many visible pixels must move the outermost point of the rotated object
1756 */
1757 void
1758 sp_selection_rotate_screen(Inkscape::Selection *selection, gdouble angle)
1759 {
1760 if (selection->isEmpty())
1761 return;
1763 NR::Maybe<NR::Rect> const bbox(selection->bounds());
1764 NR::Maybe<NR::Point> center = selection->center();
1766 if ( !bbox || !center ) {
1767 return;
1768 }
1770 gdouble const zoom = selection->desktop()->current_zoom();
1771 gdouble const zmove = angle / zoom;
1772 gdouble const r = NR::L2(bbox->cornerFarthestFrom(*center) - *center);
1774 gdouble const zangle = 180 * atan2(zmove, r) / M_PI;
1776 sp_selection_rotate_relative(selection, *center, zangle);
1778 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1779 ( (angle > 0)
1780 ? "selector:rotate:ccw"
1781 : "selector:rotate:cw" ),
1782 SP_VERB_CONTEXT_SELECT,
1783 _("Rotate by pixels"));
1784 }
1786 void
1787 sp_selection_scale(Inkscape::Selection *selection, gdouble grow)
1788 {
1789 if (selection->isEmpty())
1790 return;
1792 NR::Maybe<NR::Rect> const bbox(selection->bounds());
1793 if (!bbox) {
1794 return;
1795 }
1797 NR::Point const center(bbox->midpoint());
1799 // you can't scale "do nizhe pola" (below zero)
1800 double const max_len = bbox->maxExtent();
1801 if ( max_len + grow <= 1e-3 ) {
1802 return;
1803 }
1805 double const times = 1.0 + grow / max_len;
1806 sp_selection_scale_relative(selection, center, NR::scale(times, times));
1808 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1809 ( (grow > 0)
1810 ? "selector:scale:larger"
1811 : "selector:scale:smaller" ),
1812 SP_VERB_CONTEXT_SELECT,
1813 _("Scale"));
1814 }
1816 void
1817 sp_selection_scale_screen(Inkscape::Selection *selection, gdouble grow_pixels)
1818 {
1819 sp_selection_scale(selection,
1820 grow_pixels / selection->desktop()->current_zoom());
1821 }
1823 void
1824 sp_selection_scale_times(Inkscape::Selection *selection, gdouble times)
1825 {
1826 if (selection->isEmpty())
1827 return;
1829 NR::Maybe<NR::Rect> sel_bbox = selection->bounds();
1831 if (!sel_bbox) {
1832 return;
1833 }
1835 NR::Point const center(sel_bbox->midpoint());
1836 sp_selection_scale_relative(selection, center, NR::scale(times, times));
1837 sp_document_done(sp_desktop_document(selection->desktop()), SP_VERB_CONTEXT_SELECT,
1838 _("Scale by whole factor"));
1839 }
1841 void
1842 sp_selection_move(gdouble dx, gdouble dy)
1843 {
1844 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1845 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1846 if (selection->isEmpty()) {
1847 return;
1848 }
1850 sp_selection_move_relative(selection, dx, dy);
1852 if (dx == 0) {
1853 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical", SP_VERB_CONTEXT_SELECT,
1854 _("Move vertically"));
1855 } else if (dy == 0) {
1856 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal", SP_VERB_CONTEXT_SELECT,
1857 _("Move horizontally"));
1858 } else {
1859 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SELECT,
1860 _("Move"));
1861 }
1862 }
1864 void
1865 sp_selection_move_screen(gdouble dx, gdouble dy)
1866 {
1867 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1869 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1870 if (selection->isEmpty()) {
1871 return;
1872 }
1874 // same as sp_selection_move but divide deltas by zoom factor
1875 gdouble const zoom = desktop->current_zoom();
1876 gdouble const zdx = dx / zoom;
1877 gdouble const zdy = dy / zoom;
1878 sp_selection_move_relative(selection, zdx, zdy);
1880 if (dx == 0) {
1881 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical", SP_VERB_CONTEXT_SELECT,
1882 _("Move vertically by pixels"));
1883 } else if (dy == 0) {
1884 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal", SP_VERB_CONTEXT_SELECT,
1885 _("Move horizontally by pixels"));
1886 } else {
1887 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SELECT,
1888 _("Move"));
1889 }
1890 }
1892 namespace {
1894 template <typename D>
1895 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
1896 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
1898 template <typename D>
1899 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items, SPObject *root,
1900 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
1902 struct Forward {
1903 typedef SPObject *Iterator;
1905 static Iterator children(SPObject *o) { return sp_object_first_child(o); }
1906 static Iterator siblings_after(SPObject *o) { return SP_OBJECT_NEXT(o); }
1907 static void dispose(Iterator /*i*/) {}
1909 static SPObject *object(Iterator i) { return i; }
1910 static Iterator next(Iterator i) { return SP_OBJECT_NEXT(i); }
1911 };
1913 struct Reverse {
1914 typedef GSList *Iterator;
1916 static Iterator children(SPObject *o) {
1917 return make_list(o->firstChild(), NULL);
1918 }
1919 static Iterator siblings_after(SPObject *o) {
1920 return make_list(SP_OBJECT_PARENT(o)->firstChild(), o);
1921 }
1922 static void dispose(Iterator i) {
1923 g_slist_free(i);
1924 }
1926 static SPObject *object(Iterator i) {
1927 return reinterpret_cast<SPObject *>(i->data);
1928 }
1929 static Iterator next(Iterator i) { return i->next; }
1931 private:
1932 static GSList *make_list(SPObject *object, SPObject *limit) {
1933 GSList *list=NULL;
1934 while ( object != limit ) {
1935 list = g_slist_prepend(list, object);
1936 object = SP_OBJECT_NEXT(object);
1937 }
1938 return list;
1939 }
1940 };
1942 }
1944 void
1945 sp_selection_item_next(void)
1946 {
1947 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1948 g_return_if_fail(desktop != NULL);
1949 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1951 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
1952 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
1953 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
1955 SPObject *root;
1956 if (PREFS_SELECTION_ALL != inlayer) {
1957 root = selection->activeContext();
1958 } else {
1959 root = desktop->currentRoot();
1960 }
1962 SPItem *item=next_item_from_list<Forward>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1964 if (item) {
1965 selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
1966 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1967 scroll_to_show_item(desktop, item);
1968 }
1969 }
1970 }
1972 void
1973 sp_selection_item_prev(void)
1974 {
1975 SPDocument *document = SP_ACTIVE_DOCUMENT;
1976 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1977 g_return_if_fail(document != NULL);
1978 g_return_if_fail(desktop != NULL);
1979 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1981 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
1982 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
1983 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
1985 SPObject *root;
1986 if (PREFS_SELECTION_ALL != inlayer) {
1987 root = selection->activeContext();
1988 } else {
1989 root = desktop->currentRoot();
1990 }
1992 SPItem *item=next_item_from_list<Reverse>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1994 if (item) {
1995 selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
1996 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1997 scroll_to_show_item(desktop, item);
1998 }
1999 }
2000 }
2002 void sp_selection_next_patheffect_param(SPDesktop * dt)
2003 {
2004 if (!dt) return;
2006 Inkscape::Selection *selection = sp_desktop_selection(dt);
2007 if ( selection && !selection->isEmpty() ) {
2008 SPItem *item = selection->singleItem();
2009 if ( item && SP_IS_SHAPE(item)) {
2010 SPShape *shape = SP_SHAPE(item);
2011 if (sp_shape_has_path_effect(shape)) {
2012 sp_shape_edit_next_param_oncanvas(shape, dt);
2013 } else {
2014 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("The selection has no applied path effect."));
2015 }
2016 }
2017 }
2018 }
2020 namespace {
2022 template <typename D>
2023 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items,
2024 SPObject *root, bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
2025 {
2026 SPObject *current=root;
2027 while (items) {
2028 SPItem *item=SP_ITEM(items->data);
2029 if ( root->isAncestorOf(item) &&
2030 ( !only_in_viewport || desktop->isWithinViewport(item) ) )
2031 {
2032 current = item;
2033 break;
2034 }
2035 items = items->next;
2036 }
2038 GSList *path=NULL;
2039 while ( current != root ) {
2040 path = g_slist_prepend(path, current);
2041 current = SP_OBJECT_PARENT(current);
2042 }
2044 SPItem *next;
2045 // first, try from the current object
2046 next = next_item<D>(desktop, path, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
2047 g_slist_free(path);
2049 if (!next) { // if we ran out of objects, start over at the root
2050 next = next_item<D>(desktop, NULL, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
2051 }
2053 return next;
2054 }
2056 template <typename D>
2057 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
2058 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
2059 {
2060 typename D::Iterator children;
2061 typename D::Iterator iter;
2063 SPItem *found=NULL;
2065 if (path) {
2066 SPObject *object=reinterpret_cast<SPObject *>(path->data);
2067 g_assert(SP_OBJECT_PARENT(object) == root);
2068 if (desktop->isLayer(object)) {
2069 found = next_item<D>(desktop, path->next, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
2070 }
2071 iter = children = D::siblings_after(object);
2072 } else {
2073 iter = children = D::children(root);
2074 }
2076 while ( iter && !found ) {
2077 SPObject *object=D::object(iter);
2078 if (desktop->isLayer(object)) {
2079 if (PREFS_SELECTION_LAYER != inlayer) { // recurse into sublayers
2080 found = next_item<D>(desktop, NULL, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
2081 }
2082 } else if ( SP_IS_ITEM(object) &&
2083 ( !only_in_viewport || desktop->isWithinViewport(SP_ITEM(object)) ) &&
2084 ( !onlyvisible || !desktop->itemIsHidden(SP_ITEM(object))) &&
2085 ( !onlysensitive || !SP_ITEM(object)->isLocked()) &&
2086 !desktop->isLayer(SP_ITEM(object)) )
2087 {
2088 found = SP_ITEM(object);
2089 }
2090 iter = D::next(iter);
2091 }
2093 D::dispose(children);
2095 return found;
2096 }
2098 }
2100 /**
2101 * If \a item is not entirely visible then adjust visible area to centre on the centre on of
2102 * \a item.
2103 */
2104 void scroll_to_show_item(SPDesktop *desktop, SPItem *item)
2105 {
2106 NR::Rect dbox = desktop->get_display_area();
2107 NR::Maybe<NR::Rect> sbox = sp_item_bbox_desktop(item);
2109 if ( sbox && dbox.contains(*sbox) == false ) {
2110 NR::Point const s_dt = sbox->midpoint();
2111 NR::Point const s_w = desktop->d2w(s_dt);
2112 NR::Point const d_dt = dbox.midpoint();
2113 NR::Point const d_w = desktop->d2w(d_dt);
2114 NR::Point const moved_w( d_w - s_w );
2115 gint const dx = (gint) moved_w[X];
2116 gint const dy = (gint) moved_w[Y];
2117 desktop->scroll_world(dx, dy);
2118 }
2119 }
2122 void
2123 sp_selection_clone()
2124 {
2125 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2126 if (desktop == NULL)
2127 return;
2129 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2131 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
2133 // check if something is selected
2134 if (selection->isEmpty()) {
2135 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object</b> to clone."));
2136 return;
2137 }
2139 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
2141 selection->clear();
2143 // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
2144 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
2146 GSList *newsel = NULL;
2148 while (reprs) {
2149 Inkscape::XML::Node *sel_repr = (Inkscape::XML::Node *) reprs->data;
2150 Inkscape::XML::Node *parent = sp_repr_parent(sel_repr);
2152 Inkscape::XML::Node *clone = xml_doc->createElement("svg:use");
2153 sp_repr_set_attr(clone, "x", "0");
2154 sp_repr_set_attr(clone, "y", "0");
2155 sp_repr_set_attr(clone, "xlink:href", g_strdup_printf("#%s", sel_repr->attribute("id")));
2157 sp_repr_set_attr(clone, "inkscape:transform-center-x", sel_repr->attribute("inkscape:transform-center-x"));
2158 sp_repr_set_attr(clone, "inkscape:transform-center-y", sel_repr->attribute("inkscape:transform-center-y"));
2160 // add the new clone to the top of the original's parent
2161 parent->appendChild(clone);
2163 newsel = g_slist_prepend(newsel, clone);
2164 reprs = g_slist_remove(reprs, sel_repr);
2165 Inkscape::GC::release(clone);
2166 }
2168 // TRANSLATORS: only translate "string" in "context|string".
2169 // For more details, see http://developer.gnome.org/doc/API/2.0/glib/glib-I18N.html#Q-:CAPS
2170 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_CLONE,
2171 Q_("action|Clone"));
2173 selection->setReprList(newsel);
2175 g_slist_free(newsel);
2176 }
2178 void
2179 sp_selection_unlink()
2180 {
2181 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2182 if (!desktop)
2183 return;
2185 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2187 if (selection->isEmpty()) {
2188 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>clone</b> to unlink."));
2189 return;
2190 }
2192 // Get a copy of current selection.
2193 GSList *new_select = NULL;
2194 bool unlinked = false;
2195 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2196 items != NULL;
2197 items = items->next)
2198 {
2199 SPItem *item = (SPItem *) items->data;
2201 if (SP_IS_TEXT(item)) {
2202 SPObject *tspan = sp_tref_convert_to_tspan(SP_OBJECT(item));
2204 if (tspan) {
2205 SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
2206 }
2208 // Set unlink to true, and fall into the next if which
2209 // will include this text item in the new selection
2210 unlinked = true;
2211 }
2213 if (!(SP_IS_USE(item) || SP_IS_TREF(item))) {
2214 // keep the non-use item in the new selection
2215 new_select = g_slist_prepend(new_select, item);
2216 continue;
2217 }
2219 SPItem *unlink;
2220 if (SP_IS_USE(item)) {
2221 unlink = sp_use_unlink(SP_USE(item));
2222 } else /*if (SP_IS_TREF(use))*/ {
2223 unlink = SP_ITEM(sp_tref_convert_to_tspan(SP_OBJECT(item)));
2224 }
2226 unlinked = true;
2227 // Add ungrouped items to the new selection.
2228 new_select = g_slist_prepend(new_select, unlink);
2229 }
2231 if (new_select) { // set new selection
2232 selection->clear();
2233 selection->setList(new_select);
2234 g_slist_free(new_select);
2235 }
2236 if (!unlinked) {
2237 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No clones to unlink</b> in the selection."));
2238 }
2240 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNLINK_CLONE,
2241 _("Unlink clone"));
2242 }
2244 void
2245 sp_select_clone_original()
2246 {
2247 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2248 if (desktop == NULL)
2249 return;
2251 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2253 SPItem *item = selection->singleItem();
2255 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.");
2257 // Check if other than two objects are selected
2258 if (g_slist_length((GSList *) selection->itemList()) != 1 || !item) {
2259 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
2260 return;
2261 }
2263 SPItem *original = NULL;
2264 if (SP_IS_USE(item)) {
2265 original = sp_use_get_original (SP_USE(item));
2266 } else if (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) {
2267 original = sp_offset_get_source (SP_OFFSET(item));
2268 } else if (SP_IS_TEXT_TEXTPATH(item)) {
2269 original = sp_textpath_get_path_item (SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))));
2270 } else if (SP_IS_FLOWTEXT(item)) {
2271 original = SP_FLOWTEXT(item)->get_frame (NULL); // first frame only
2272 } else { // it's an object that we don't know what to do with
2273 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
2274 return;
2275 }
2277 if (!original) {
2278 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>Cannot find</b> the object to select (orphaned clone, offset, textpath, flowed text?)"));
2279 return;
2280 }
2282 for (SPObject *o = original; o && !SP_IS_ROOT(o); o = SP_OBJECT_PARENT (o)) {
2283 if (SP_IS_DEFS (o)) {
2284 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("The object you're trying to select is <b>not visible</b> (it is in <defs>)"));
2285 return;
2286 }
2287 }
2289 if (original) {
2290 selection->clear();
2291 selection->set(original);
2292 if (SP_CYCLING == SP_CYCLE_FOCUS) {
2293 scroll_to_show_item(desktop, original);
2294 }
2295 }
2296 }
2299 void sp_selection_to_marker(bool apply)
2300 {
2301 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2302 if (desktop == NULL)
2303 return;
2305 SPDocument *doc = sp_desktop_document(desktop);
2306 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2308 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2310 // check if something is selected
2311 if (selection->isEmpty()) {
2312 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to marker."));
2313 return;
2314 }
2316 sp_document_ensure_up_to_date(doc);
2317 NR::Maybe<NR::Rect> r = selection->bounds();
2318 if ( !r || r->isEmpty() ) {
2319 return;
2320 }
2322 // calculate the transform to be applied to objects to move them to 0,0
2323 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));
2324 move_p[NR::Y] = -move_p[NR::Y];
2325 NR::Matrix move = NR::Matrix (NR::translate (move_p));
2327 GSList *items = g_slist_copy((GSList *) selection->itemList());
2329 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2331 // bottommost object, after sorting
2332 SPObject *parent = SP_OBJECT_PARENT (items->data);
2334 NR::Matrix parent_transform = sp_item_i2root_affine(SP_ITEM(parent));
2336 // remember the position of the first item
2337 gint pos = SP_OBJECT_REPR (items->data)->position();
2338 (void)pos; // TODO check why this was remembered
2340 // create a list of duplicates
2341 GSList *repr_copies = NULL;
2342 for (GSList *i = items; i != NULL; i = i->next) {
2343 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2344 repr_copies = g_slist_prepend (repr_copies, dup);
2345 }
2347 NR::Rect bounds(desktop->dt2doc(r->min()), desktop->dt2doc(r->max()));
2349 if (apply) {
2350 // delete objects so that their clones don't get alerted; this object will be restored shortly
2351 for (GSList *i = items; i != NULL; i = i->next) {
2352 SPObject *item = SP_OBJECT (i->data);
2353 item->deleteObject (false);
2354 }
2355 }
2357 // Hack: Temporarily set clone compensation to unmoved, so that we can move clone-originals
2358 // without disturbing clones.
2359 // See ActorAlign::on_button_click() in src/ui/dialog/align-and-distribute.cpp
2360 int saved_compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2361 prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2363 gchar const *mark_id = generate_marker(repr_copies, bounds, doc,
2364 ( NR::Matrix(NR::translate(desktop->dt2doc(NR::Point(r->min()[NR::X],
2365 r->max()[NR::Y]))))
2366 * parent_transform.inverse() ),
2367 parent_transform * move);
2368 (void)mark_id;
2370 // restore compensation setting
2371 prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
2374 g_slist_free (items);
2376 sp_document_done (doc, SP_VERB_EDIT_SELECTION_2_MARKER,
2377 _("Objects to marker"));
2378 }
2380 void
2381 sp_selection_tile(bool apply)
2382 {
2383 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2384 if (desktop == NULL)
2385 return;
2387 SPDocument *doc = sp_desktop_document(desktop);
2388 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2390 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2392 // check if something is selected
2393 if (selection->isEmpty()) {
2394 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to pattern."));
2395 return;
2396 }
2398 sp_document_ensure_up_to_date(doc);
2399 NR::Maybe<NR::Rect> r = selection->bounds();
2400 if ( !r || r->isEmpty() ) {
2401 return;
2402 }
2404 // calculate the transform to be applied to objects to move them to 0,0
2405 NR::Point move_p = NR::Point(0, sp_document_height(doc)) - (r->min() + NR::Point (0, r->extent(NR::Y)));
2406 move_p[NR::Y] = -move_p[NR::Y];
2407 NR::Matrix move = NR::Matrix (NR::translate (move_p));
2409 GSList *items = g_slist_copy((GSList *) selection->itemList());
2411 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2413 // bottommost object, after sorting
2414 SPObject *parent = SP_OBJECT_PARENT (items->data);
2416 NR::Matrix parent_transform = sp_item_i2root_affine(SP_ITEM(parent));
2418 // remember the position of the first item
2419 gint pos = SP_OBJECT_REPR (items->data)->position();
2421 // create a list of duplicates
2422 GSList *repr_copies = NULL;
2423 for (GSList *i = items; i != NULL; i = i->next) {
2424 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2425 repr_copies = g_slist_prepend (repr_copies, dup);
2426 }
2428 NR::Rect bounds(desktop->dt2doc(r->min()), desktop->dt2doc(r->max()));
2430 if (apply) {
2431 // delete objects so that their clones don't get alerted; this object will be restored shortly
2432 for (GSList *i = items; i != NULL; i = i->next) {
2433 SPObject *item = SP_OBJECT (i->data);
2434 item->deleteObject (false);
2435 }
2436 }
2438 // Hack: Temporarily set clone compensation to unmoved, so that we can move clone-originals
2439 // without disturbing clones.
2440 // See ActorAlign::on_button_click() in src/ui/dialog/align-and-distribute.cpp
2441 int saved_compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2442 prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2444 gchar const *pat_id = pattern_tile(repr_copies, bounds, doc,
2445 ( NR::Matrix(NR::translate(desktop->dt2doc(NR::Point(r->min()[NR::X],
2446 r->max()[NR::Y]))))
2447 * parent_transform.inverse() ),
2448 parent_transform * move);
2450 // restore compensation setting
2451 prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
2453 if (apply) {
2454 Inkscape::XML::Node *rect = xml_doc->createElement("svg:rect");
2455 rect->setAttribute("style", g_strdup_printf("stroke:none;fill:url(#%s)", pat_id));
2457 NR::Point min = bounds.min() * parent_transform.inverse();
2458 NR::Point max = bounds.max() * parent_transform.inverse();
2460 sp_repr_set_svg_double(rect, "width", max[NR::X] - min[NR::X]);
2461 sp_repr_set_svg_double(rect, "height", max[NR::Y] - min[NR::Y]);
2462 sp_repr_set_svg_double(rect, "x", min[NR::X]);
2463 sp_repr_set_svg_double(rect, "y", min[NR::Y]);
2465 // restore parent and position
2466 SP_OBJECT_REPR (parent)->appendChild(rect);
2467 rect->setPosition(pos > 0 ? pos : 0);
2468 SPItem *rectangle = (SPItem *) sp_desktop_document (desktop)->getObjectByRepr(rect);
2470 Inkscape::GC::release(rect);
2472 selection->clear();
2473 selection->set(rectangle);
2474 }
2476 g_slist_free (items);
2478 sp_document_done (doc, SP_VERB_EDIT_TILE,
2479 _("Objects to pattern"));
2480 }
2482 void
2483 sp_selection_untile()
2484 {
2485 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2486 if (desktop == NULL)
2487 return;
2489 SPDocument *doc = sp_desktop_document(desktop);
2490 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2492 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2494 // check if something is selected
2495 if (selection->isEmpty()) {
2496 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object with pattern fill</b> to extract objects from."));
2497 return;
2498 }
2500 GSList *new_select = NULL;
2502 bool did = false;
2504 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2505 items != NULL;
2506 items = items->next) {
2508 SPItem *item = (SPItem *) items->data;
2510 SPStyle *style = SP_OBJECT_STYLE (item);
2512 if (!style || !style->fill.isPaintserver())
2513 continue;
2515 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
2517 if (!SP_IS_PATTERN(server))
2518 continue;
2520 did = true;
2522 SPPattern *pattern = pattern_getroot (SP_PATTERN (server));
2524 NR::Matrix pat_transform = pattern_patternTransform (SP_PATTERN (server));
2525 pat_transform *= item->transform;
2527 for (SPObject *child = sp_object_first_child(SP_OBJECT(pattern)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2528 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate(xml_doc);
2529 SPItem *i = SP_ITEM (desktop->currentLayer()->appendChildRepr(copy));
2531 // FIXME: relink clones to the new canvas objects
2532 // use SPObject::setid when mental finishes it to steal ids of
2534 // this is needed to make sure the new item has curve (simply requestDisplayUpdate does not work)
2535 sp_document_ensure_up_to_date (doc);
2537 NR::Matrix transform( i->transform * pat_transform );
2538 sp_item_write_transform(i, SP_OBJECT_REPR(i), transform);
2540 new_select = g_slist_prepend(new_select, i);
2541 }
2543 SPCSSAttr *css = sp_repr_css_attr_new ();
2544 sp_repr_css_set_property (css, "fill", "none");
2545 sp_repr_css_change (SP_OBJECT_REPR (item), css, "style");
2546 }
2548 if (!did) {
2549 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No pattern fills</b> in the selection."));
2550 } else {
2551 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNTILE,
2552 _("Pattern to objects"));
2553 selection->setList(new_select);
2554 }
2555 }
2557 void
2558 sp_selection_get_export_hints (Inkscape::Selection *selection, char const **filename, float *xdpi, float *ydpi)
2559 {
2560 if (selection->isEmpty()) {
2561 return;
2562 }
2564 GSList const *reprlst = selection->reprList();
2565 bool filename_search = TRUE;
2566 bool xdpi_search = TRUE;
2567 bool ydpi_search = TRUE;
2569 for(; reprlst != NULL &&
2570 filename_search &&
2571 xdpi_search &&
2572 ydpi_search;
2573 reprlst = reprlst->next) {
2574 gchar const *dpi_string;
2575 Inkscape::XML::Node * repr = (Inkscape::XML::Node *)reprlst->data;
2577 if (filename_search) {
2578 *filename = repr->attribute("inkscape:export-filename");
2579 if (*filename != NULL)
2580 filename_search = FALSE;
2581 }
2583 if (xdpi_search) {
2584 dpi_string = NULL;
2585 dpi_string = repr->attribute("inkscape:export-xdpi");
2586 if (dpi_string != NULL) {
2587 *xdpi = atof(dpi_string);
2588 xdpi_search = FALSE;
2589 }
2590 }
2592 if (ydpi_search) {
2593 dpi_string = NULL;
2594 dpi_string = repr->attribute("inkscape:export-ydpi");
2595 if (dpi_string != NULL) {
2596 *ydpi = atof(dpi_string);
2597 ydpi_search = FALSE;
2598 }
2599 }
2600 }
2601 }
2603 void
2604 sp_document_get_export_hints (SPDocument *doc, char const **filename, float *xdpi, float *ydpi)
2605 {
2606 Inkscape::XML::Node * repr = sp_document_repr_root(doc);
2607 gchar const *dpi_string;
2609 *filename = repr->attribute("inkscape:export-filename");
2611 dpi_string = NULL;
2612 dpi_string = repr->attribute("inkscape:export-xdpi");
2613 if (dpi_string != NULL) {
2614 *xdpi = atof(dpi_string);
2615 }
2617 dpi_string = NULL;
2618 dpi_string = repr->attribute("inkscape:export-ydpi");
2619 if (dpi_string != NULL) {
2620 *ydpi = atof(dpi_string);
2621 }
2622 }
2624 void
2625 sp_selection_create_bitmap_copy ()
2626 {
2627 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2628 if (desktop == NULL)
2629 return;
2631 SPDocument *document = sp_desktop_document(desktop);
2632 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
2634 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2636 // check if something is selected
2637 if (selection->isEmpty()) {
2638 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to make a bitmap copy."));
2639 return;
2640 }
2642 // Get the bounding box of the selection
2643 NRRect bbox;
2644 sp_document_ensure_up_to_date (document);
2645 selection->bounds(&bbox);
2646 if (NR_RECT_DFLS_TEST_EMPTY(&bbox)) {
2647 return; // exceptional situation, so not bother with a translatable error message, just quit quietly
2648 }
2650 // List of the items to show; all others will be hidden
2651 GSList *items = g_slist_copy ((GSList *) selection->itemList());
2653 // Sort items so that the topmost comes last
2654 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
2656 // Generate a random value from the current time (you may create bitmap from the same object(s)
2657 // multiple times, and this is done so that they don't clash)
2658 GTimeVal cu;
2659 g_get_current_time (&cu);
2660 guint current = (int) (cu.tv_sec * 1000000 + cu.tv_usec) % 1024;
2662 // Create the filename
2663 gchar *filename = g_strdup_printf ("%s-%s-%u.png", document->name, SP_OBJECT_REPR(items->data)->attribute("id"), current);
2664 // Imagemagick is known not to handle spaces in filenames, so we replace anything but letters,
2665 // digits, and a few other chars, with "_"
2666 filename = g_strcanon (filename, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.=+~$#@^&!?", '_');
2667 // Build the complete path by adding document->base if set
2668 gchar *filepath = g_build_filename (document->base?document->base:"", filename, NULL);
2670 //g_print ("%s\n", filepath);
2672 // Remember parent and z-order of the topmost one
2673 gint pos = SP_OBJECT_REPR(g_slist_last(items)->data)->position();
2674 SPObject *parent_object = SP_OBJECT_PARENT(g_slist_last(items)->data);
2675 Inkscape::XML::Node *parent = SP_OBJECT_REPR(parent_object);
2677 // Calculate resolution
2678 double res;
2679 int const prefs_res = prefs_get_int_attribute ("options.createbitmap", "resolution", 0);
2680 int const prefs_min = prefs_get_int_attribute ("options.createbitmap", "minsize", 0);
2681 if (0 < prefs_res) {
2682 // If it's given explicitly in prefs, take it
2683 res = prefs_res;
2684 } else if (0 < prefs_min) {
2685 // If minsize is given, look up minimum bitmap size (default 250 pixels) and calculate resolution from it
2686 res = PX_PER_IN * prefs_min / MIN ((bbox.x1 - bbox.x0), (bbox.y1 - bbox.y0));
2687 } else {
2688 float hint_xdpi = 0, hint_ydpi = 0;
2689 char const *hint_filename;
2690 // take resolution hint from the selected objects
2691 sp_selection_get_export_hints (selection, &hint_filename, &hint_xdpi, &hint_ydpi);
2692 if (hint_xdpi != 0) {
2693 res = hint_xdpi;
2694 } else {
2695 // take resolution hint from the document
2696 sp_document_get_export_hints (document, &hint_filename, &hint_xdpi, &hint_ydpi);
2697 if (hint_xdpi != 0) {
2698 res = hint_xdpi;
2699 } else {
2700 // if all else fails, take the default 90 dpi
2701 res = PX_PER_IN;
2702 }
2703 }
2704 }
2706 // The width and height of the bitmap in pixels
2707 unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * res / PX_PER_IN);
2708 unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * res / PX_PER_IN);
2710 // Find out if we have to run a filter
2711 gchar const *run = NULL;
2712 gchar const *filter = prefs_get_string_attribute ("options.createbitmap", "filter");
2713 if (filter) {
2714 // filter command is given;
2715 // see if we have a parameter to pass to it
2716 gchar const *param1 = prefs_get_string_attribute ("options.createbitmap", "filter_param1");
2717 if (param1) {
2718 if (param1[strlen(param1) - 1] == '%') {
2719 // if the param string ends with %, interpret it as a percentage of the image's max dimension
2720 gchar p1[256];
2721 g_ascii_dtostr (p1, 256, ceil (g_ascii_strtod (param1, NULL) * MAX(width, height) / 100));
2722 // the first param is always the image filename, the second is param1
2723 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, p1);
2724 } else {
2725 // otherwise pass the param1 unchanged
2726 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, param1);
2727 }
2728 } else {
2729 // run without extra parameter
2730 run = g_strdup_printf ("%s \"%s\"", filter, filepath);
2731 }
2732 }
2734 // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
2735 NR::Matrix eek = sp_item_i2d_affine (SP_ITEM(parent_object));
2736 NR::Matrix t;
2738 double shift_x = bbox.x0;
2739 double shift_y = bbox.y1;
2740 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
2741 shift_x = round (shift_x);
2742 shift_y = -round (-shift_y); // this gets correct rounding despite coordinate inversion, remove the negations when the inversion is gone
2743 }
2744 t = NR::scale(1, -1) * NR::translate (shift_x, shift_y) * eek.inverse();
2746 // Do the export
2747 sp_export_png_file(document, filepath,
2748 bbox.x0, bbox.y0, bbox.x1, bbox.y1,
2749 width, height, res, res,
2750 (guint32) 0xffffff00,
2751 NULL, NULL,
2752 true, /*bool force_overwrite,*/
2753 items);
2755 g_slist_free (items);
2757 // Run filter, if any
2758 if (run) {
2759 g_print ("Running external filter: %s\n", run);
2760 system (run);
2761 }
2763 // Import the image back
2764 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filepath, NULL);
2765 if (pb) {
2766 // Create the repr for the image
2767 Inkscape::XML::Node * repr = xml_doc->createElement("svg:image");
2768 repr->setAttribute("xlink:href", filename);
2769 repr->setAttribute("sodipodi:absref", filepath);
2770 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
2771 sp_repr_set_svg_double(repr, "width", width);
2772 sp_repr_set_svg_double(repr, "height", height);
2773 } else {
2774 sp_repr_set_svg_double(repr, "width", (bbox.x1 - bbox.x0));
2775 sp_repr_set_svg_double(repr, "height", (bbox.y1 - bbox.y0));
2776 }
2778 // Write transform
2779 gchar *c=sp_svg_transform_write(t);
2780 repr->setAttribute("transform", c);
2781 g_free(c);
2783 // add the new repr to the parent
2784 parent->appendChild(repr);
2786 // move to the saved position
2787 repr->setPosition(pos > 0 ? pos + 1 : 1);
2789 // Set selection to the new image
2790 selection->clear();
2791 selection->add(repr);
2793 // Clean up
2794 Inkscape::GC::release(repr);
2795 gdk_pixbuf_unref (pb);
2797 // Complete undoable transaction
2798 sp_document_done (document, SP_VERB_SELECTION_CREATE_BITMAP,
2799 _("Create bitmap"));
2800 }
2802 g_free (filename);
2803 g_free (filepath);
2804 }
2806 /**
2807 * \brief sp_selection_set_mask
2808 *
2809 * This function creates a mask or clipPath from selection
2810 * Two different modes:
2811 * if applyToLayer, all selection is moved to DEFS as mask/clippath
2812 * and is applied to current layer
2813 * otherwise, topmost object is used as mask for other objects
2814 * If \a apply_clip_path parameter is true, clipPath is created, otherwise mask
2815 *
2816 */
2817 void
2818 sp_selection_set_mask(bool apply_clip_path, bool apply_to_layer)
2819 {
2820 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2821 if (desktop == NULL)
2822 return;
2824 SPDocument *doc = sp_desktop_document(desktop);
2825 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2827 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2829 // check if something is selected
2830 bool is_empty = selection->isEmpty();
2831 if ( apply_to_layer && is_empty) {
2832 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to create clippath or mask from."));
2833 return;
2834 } else if (!apply_to_layer && ( is_empty || NULL == selection->itemList()->next )) {
2835 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select mask object and <b>object(s)</b> to apply clippath or mask to."));
2836 return;
2837 }
2839 // FIXME: temporary patch to prevent crash!
2840 // Remove this when bboxes are fixed to not blow up on an item clipped/masked with its own clone
2841 bool clone_with_original = selection_contains_both_clone_and_original (selection);
2842 if (clone_with_original) {
2843 return; // in this version, you cannot clip/mask an object with its own clone
2844 }
2845 // /END FIXME
2847 sp_document_ensure_up_to_date(doc);
2849 GSList *items = g_slist_copy((GSList *) selection->itemList());
2851 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2853 // create a list of duplicates
2854 GSList *mask_items = NULL;
2855 GSList *apply_to_items = NULL;
2856 GSList *items_to_delete = NULL;
2857 bool topmost = prefs_get_int_attribute ("options.maskobject", "topmost", 1);
2858 bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
2860 if (apply_to_layer) {
2861 // all selected items are used for mask, which is applied to a layer
2862 apply_to_items = g_slist_prepend (apply_to_items, desktop->currentLayer());
2864 for (GSList *i = items; i != NULL; i = i->next) {
2865 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2866 mask_items = g_slist_prepend (mask_items, dup);
2868 if (remove_original) {
2869 SPObject *item = SP_OBJECT (i->data);
2870 items_to_delete = g_slist_prepend (items_to_delete, item);
2871 }
2872 }
2873 } else if (!topmost) {
2874 // topmost item is used as a mask, which is applied to other items in a selection
2875 GSList *i = items;
2876 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2877 mask_items = g_slist_prepend (mask_items, dup);
2879 if (remove_original) {
2880 SPObject *item = SP_OBJECT (i->data);
2881 items_to_delete = g_slist_prepend (items_to_delete, item);
2882 }
2884 for (i = i->next; i != NULL; i = i->next) {
2885 apply_to_items = g_slist_prepend (apply_to_items, i->data);
2886 }
2887 } else {
2888 GSList *i = NULL;
2889 for (i = items; NULL != i->next; i = i->next) {
2890 apply_to_items = g_slist_prepend (apply_to_items, i->data);
2891 }
2893 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2894 mask_items = g_slist_prepend (mask_items, dup);
2896 if (remove_original) {
2897 SPObject *item = SP_OBJECT (i->data);
2898 items_to_delete = g_slist_prepend (items_to_delete, item);
2899 }
2900 }
2902 g_slist_free (items);
2903 items = NULL;
2905 gchar const *attributeName = apply_clip_path ? "clip-path" : "mask";
2906 for (GSList *i = apply_to_items; NULL != i; i = i->next) {
2907 SPItem *item = reinterpret_cast<SPItem *>(i->data);
2908 // inverted object transform should be applied to a mask object,
2909 // as mask is calculated in user space (after applying transform)
2910 NR::Matrix maskTransform (item->transform.inverse());
2912 GSList *mask_items_dup = NULL;
2913 for (GSList *mask_item = mask_items; NULL != mask_item; mask_item = mask_item->next) {
2914 Inkscape::XML::Node *dup = reinterpret_cast<Inkscape::XML::Node *>(mask_item->data)->duplicate(xml_doc);
2915 mask_items_dup = g_slist_prepend (mask_items_dup, dup);
2916 }
2918 gchar const *mask_id = NULL;
2919 if (apply_clip_path) {
2920 mask_id = sp_clippath_create(mask_items_dup, doc, &maskTransform);
2921 } else {
2922 mask_id = sp_mask_create(mask_items_dup, doc, &maskTransform);
2923 }
2925 g_slist_free (mask_items_dup);
2926 mask_items_dup = NULL;
2928 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, g_strdup_printf("url(#%s)", mask_id));
2929 }
2931 g_slist_free (mask_items);
2932 g_slist_free (apply_to_items);
2934 for (GSList *i = items_to_delete; NULL != i; i = i->next) {
2935 SPObject *item = SP_OBJECT (i->data);
2936 item->deleteObject (false);
2937 }
2938 g_slist_free (items_to_delete);
2940 if (apply_clip_path)
2941 sp_document_done (doc, SP_VERB_OBJECT_SET_CLIPPATH, _("Set clipping path"));
2942 else
2943 sp_document_done (doc, SP_VERB_OBJECT_SET_MASK, _("Set mask"));
2944 }
2946 void sp_selection_unset_mask(bool apply_clip_path) {
2947 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2948 if (desktop == NULL)
2949 return;
2951 SPDocument *doc = sp_desktop_document(desktop);
2952 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2953 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2955 // check if something is selected
2956 if (selection->isEmpty()) {
2957 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to remove clippath or mask from."));
2958 return;
2959 }
2961 bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
2962 sp_document_ensure_up_to_date(doc);
2964 gchar const *attributeName = apply_clip_path ? "clip-path" : "mask";
2965 std::map<SPObject*,SPItem*> referenced_objects;
2966 for (GSList const *i = selection->itemList(); NULL != i; i = i->next) {
2967 if (remove_original) {
2968 // remember referenced mask/clippath, so orphaned masks can be moved back to document
2969 SPItem *item = reinterpret_cast<SPItem *>(i->data);
2970 Inkscape::URIReference *uri_ref = NULL;
2972 if (apply_clip_path) {
2973 uri_ref = item->clip_ref;
2974 } else {
2975 uri_ref = item->mask_ref;
2976 }
2978 // collect distinct mask object (and associate with item to apply transform)
2979 if (NULL != uri_ref && NULL != uri_ref->getObject()) {
2980 referenced_objects[uri_ref->getObject()] = item;
2981 }
2982 }
2984 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, "none");
2985 }
2987 // restore mask objects into a document
2988 for ( std::map<SPObject*,SPItem*>::iterator it = referenced_objects.begin() ; it != referenced_objects.end() ; ++it) {
2989 SPObject *obj = (*it).first;
2990 GSList *items_to_move = NULL;
2991 for (SPObject *child = sp_object_first_child(obj) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2992 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate(xml_doc);
2993 items_to_move = g_slist_prepend (items_to_move, copy);
2994 }
2996 if (!obj->isReferenced()) {
2997 // delete from defs if no other object references this mask
2998 obj->deleteObject(false);
2999 }
3001 // remember parent and position of the item to which the clippath/mask was applied
3002 Inkscape::XML::Node *parent = SP_OBJECT_REPR((*it).second)->parent();
3003 gint pos = SP_OBJECT_REPR((*it).second)->position();
3005 for (GSList *i = items_to_move; NULL != i; i = i->next) {
3006 Inkscape::XML::Node *repr = (Inkscape::XML::Node *)i->data;
3008 // insert into parent, restore pos
3009 parent->appendChild(repr);
3010 repr->setPosition((pos + 1) > 0 ? (pos + 1) : 0);
3012 SPItem *mask_item = (SPItem *) sp_desktop_document (desktop)->getObjectByRepr(repr);
3013 selection->add(repr);
3015 // transform mask, so it is moved the same spot where mask was applied
3016 NR::Matrix transform (mask_item->transform);
3017 transform *= (*it).second->transform;
3018 sp_item_write_transform(mask_item, SP_OBJECT_REPR(mask_item), transform);
3019 }
3021 g_slist_free (items_to_move);
3022 }
3024 if (apply_clip_path)
3025 sp_document_done (doc, SP_VERB_OBJECT_UNSET_CLIPPATH, _("Release clipping path"));
3026 else
3027 sp_document_done (doc, SP_VERB_OBJECT_UNSET_MASK, _("Release mask"));
3028 }
3030 void fit_canvas_to_selection(SPDesktop *desktop) {
3031 g_return_if_fail(desktop != NULL);
3032 SPDocument *doc = sp_desktop_document(desktop);
3034 g_return_if_fail(doc != NULL);
3035 g_return_if_fail(desktop->selection != NULL);
3037 if (desktop->selection->isEmpty()) {
3038 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to fit canvas to."));
3039 return;
3040 }
3041 NR::Maybe<NR::Rect> const bbox(desktop->selection->bounds());
3042 if (bbox && !bbox->isEmpty()) {
3043 doc->fitToRect(*bbox);
3044 }
3045 };
3047 void fit_canvas_to_drawing(SPDocument *doc) {
3048 g_return_if_fail(doc != NULL);
3050 sp_document_ensure_up_to_date(doc);
3051 SPItem const *const root = SP_ITEM(doc->root);
3052 NR::Maybe<NR::Rect> const bbox(root->getBounds(sp_item_i2r_affine(root)));
3053 if (bbox && !bbox->isEmpty()) {
3054 doc->fitToRect(*bbox);
3055 }
3056 };
3058 void fit_canvas_to_selection_or_drawing(SPDesktop *desktop) {
3059 g_return_if_fail(desktop != NULL);
3060 SPDocument *doc = sp_desktop_document(desktop);
3062 g_return_if_fail(doc != NULL);
3063 g_return_if_fail(desktop->selection != NULL);
3065 if (desktop->selection->isEmpty()) {
3066 fit_canvas_to_drawing(doc);
3067 } else {
3068 fit_canvas_to_selection(desktop);
3069 }
3071 sp_document_done(doc, SP_VERB_FIT_CANVAS_TO_DRAWING,
3072 _("Fit page to selection"));
3073 };
3075 static void itemtree_map(void (*f)(SPItem *, SPDesktop *), SPObject *root, SPDesktop *desktop) {
3076 // don't operate on layers
3077 if (SP_IS_ITEM(root) && !desktop->isLayer(SP_ITEM(root))) {
3078 f(SP_ITEM(root), desktop);
3079 }
3080 for ( SPObject::SiblingIterator iter = root->firstChild() ; iter ; ++iter ) {
3081 //don't recurse into locked layers
3082 if (!(SP_IS_ITEM(&*iter) && desktop->isLayer(SP_ITEM(&*iter)) && SP_ITEM(&*iter)->isLocked())) {
3083 itemtree_map(f, iter, desktop);
3084 }
3085 }
3086 }
3088 static void unlock(SPItem *item, SPDesktop */*desktop*/) {
3089 if (item->isLocked()) {
3090 item->setLocked(FALSE);
3091 }
3092 }
3094 static void unhide(SPItem *item, SPDesktop *desktop) {
3095 if (desktop->itemIsHidden(item)) {
3096 item->setExplicitlyHidden(FALSE);
3097 }
3098 }
3100 static void process_all(void (*f)(SPItem *, SPDesktop *), SPDesktop *dt, bool layer_only) {
3101 if (!dt) return;
3103 SPObject *root;
3104 if (layer_only) {
3105 root = dt->currentLayer();
3106 } else {
3107 root = dt->currentRoot();
3108 }
3110 itemtree_map(f, root, dt);
3111 }
3113 void unlock_all(SPDesktop *dt) {
3114 process_all(&unlock, dt, true);
3115 }
3117 void unlock_all_in_all_layers(SPDesktop *dt) {
3118 process_all(&unlock, dt, false);
3119 }
3121 void unhide_all(SPDesktop *dt) {
3122 process_all(&unhide, dt, true);
3123 }
3125 void unhide_all_in_all_layers(SPDesktop *dt) {
3126 process_all(&unhide, dt, false);
3127 }
3130 GSList * sp_selection_get_clipboard() {
3131 return clipboard;
3132 }
3135 /*
3136 Local Variables:
3137 mode:c++
3138 c-file-style:"stroustrup"
3139 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
3140 indent-tabs-mode:nil
3141 fill-column:99
3142 End:
3143 */
3144 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :