Code

ce14bd0f521affc65389b4343f8cfcd92082d6a4
[inkscape.git] / src / selection-chemistry.cpp
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);
117 void sp_selection_copy_impl (GSList const *items, GSList **clip, GSList **defs_clip, SPCSSAttr **style_clip, Inkscape::XML::Document* xml_doc)
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     }
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)
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     }
173 GSList *sp_selection_paste_impl (SPDocument *doc, SPObject *parent, GSList **clip, GSList **defs_clip)
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;
205 void sp_selection_delete_impl(GSList const *items, bool propagate = true, bool propagate_descendants = true)
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     }
218 void sp_selection_delete()
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"));
257 /* fixme: sequencing */
258 void sp_selection_duplicate()
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);
301 void sp_edit_clear_all()
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"));
322 GSList *
323 get_all_items (GSList *list, SPObject *from, SPDesktop *desktop, bool onlyvisible, bool onlysensitive, GSList const *exclude)
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;
344 void sp_edit_select_all_full (bool force_all_layers, bool invert)
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     }
410 void sp_edit_select_all ()
412     sp_edit_select_all_full (false, false);
415 void sp_edit_select_all_in_all_layers ()
417     sp_edit_select_all_full (true, false);
420 void sp_edit_invert ()
422     sp_edit_select_all_full (false, true);
425 void sp_edit_invert_in_all_layers ()
427     sp_edit_select_all_full (true, true);
430 void sp_selection_group()
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);
522 void sp_selection_ungroup()
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"));
581 static SPGroup *
582 sp_item_list_common_parent_group(GSList const *items)
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);
601 /** Finds out the minimum common bbox of the selected items. */
602 static NR::Maybe<NR::Rect>
603 enclose_items(GSList const *items)
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;
614 SPObject *
615 prev_sibling(SPObject *child)
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;
628 void
629 sp_selection_raise()
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"));
687 void sp_selection_raise_to_top()
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"));
723 void
724 sp_selection_lower()
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"));
787 void sp_selection_lower_to_bottom()
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"));
835 void
836 sp_undo(SPDesktop *desktop, SPDocument *)
838         if (!sp_document_undo(sp_desktop_document(desktop)))
839             desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to undo."));
842 void
843 sp_redo(SPDesktop *desktop, SPDocument *)
845         if (!sp_document_redo(sp_desktop_document(desktop)))
846             desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to redo."));
849 void sp_selection_cut()
851     sp_selection_copy();
852     sp_selection_delete();
855 void sp_copy_gradient (GSList **defs_clip, SPGradient *gradient, Inkscape::XML::Document* xml_doc)
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     }
868 void sp_copy_pattern (GSList **defs_clip, SPPattern *pattern, Inkscape::XML::Document* xml_doc)
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     }
888 void sp_copy_single (GSList **defs_clip, SPObject *thing, Inkscape::XML::Document* xml_doc)
890     Inkscape::XML::Node *duplicate_repr = SP_OBJECT_REPR(thing)->duplicate(xml_doc);
891     *defs_clip = g_slist_prepend (*defs_clip, duplicate_repr);
895 void sp_copy_textpath_path (GSList **defs_clip, SPTextPath *tp, GSList const *items, Inkscape::XML::Document* xml_doc)
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);
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)
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     }
976 void
977 sp_set_style_clipboard (SPCSSAttr *css)
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     }
990 /**
991  * \pre item != NULL
992  */
993 SPCSSAttr *
994 take_style_from_item (SPItem *item)
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;
1031 void sp_selection_copy()
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);
1123 void sp_selection_paste(bool in_place)
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         /* Snap the offset of the new item(s) to the grid */
1165         SnapManager &sm = desktop->namedview->snap_manager;
1166         SnapManager::SnapperList gs = sm.getGridSnappers();
1167         m = sm.freeSnapAlways(Inkscape::Snapper::SNAPPOINT_NODE, m, NULL, gs).getPoint();
1168         sp_selection_move_relative(selection, m);
1169     }
1171     sp_document_done(document, SP_VERB_EDIT_PASTE, 
1172                      _("Paste"));
1175 void sp_selection_paste_style()
1177     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1178     if (desktop == NULL) return;
1180     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1182     // check if something is in the clipboard
1183     if (style_clipboard == NULL) {
1184         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the style clipboard."));
1185         return;
1186     }
1188     // check if something is selected
1189     if (selection->isEmpty()) {
1190         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste style to."));
1191         return;
1192     }
1194     paste_defs (&defs_clipboard, sp_desktop_document(desktop));
1196     sp_desktop_set_style (desktop, style_clipboard);
1198     sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_STYLE,
1199                      _("Paste style"));
1202 void sp_selection_paste_livepatheffect()
1204     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1205     if (desktop == NULL) return;
1207     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1209     // check if something is in the clipboard
1210     if (clipboard == NULL) {
1211         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1212         return;
1213     }
1215     // check if something is selected
1216     if (selection->isEmpty()) {
1217         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste live path effect to."));
1218         return;
1219     }
1221     paste_defs (&defs_clipboard, sp_desktop_document(desktop));
1223     Inkscape::XML::Node *repr = (Inkscape::XML::Node *) clipboard->data;
1224     char const *effectstr = repr->attribute("inkscape:path-effect");
1225     if (!effectstr) {
1226         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Clipboard does not contain a live path effect."));
1227         return;
1228     }
1230     for ( GSList const *itemlist = selection->itemList(); itemlist != NULL; itemlist = g_slist_next(itemlist) ) {
1231         SPItem *item = reinterpret_cast<SPItem*>(itemlist->data);
1232         if ( item && SP_IS_SHAPE(item) ) {
1233             Inkscape::XML::Node *selrepr = (Inkscape::XML::Node *) SP_OBJECT_REPR(item);
1234             selrepr->setAttribute("inkscape:path-effect", effectstr);
1236             // set inkscape:original-d for paths. the other shapes don't need this.
1237             if ( SP_IS_PATH(item) ) {
1238                 Inkscape::XML::Node *pathrepr = SP_OBJECT_REPR(item);
1239                 if ( ! pathrepr->attribute("inkscape:original-d") ) {
1240                     pathrepr->setAttribute("inkscape:original-d", pathrepr->attribute("d"));
1241                 }
1242             }
1243         }
1244     }
1246     sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_LIVEPATHEFFECT,
1247                      _("Paste live path effect"));
1250 void sp_selection_paste_size (bool apply_x, bool apply_y)
1252     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1253     if (desktop == NULL) return;
1255     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1257     // check if something is in the clipboard
1258     if (!size_clipboard) {
1259         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1260         return;
1261     }
1263     // check if something is selected
1264     if (selection->isEmpty()) {
1265         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste size to."));
1266         return;
1267     }
1269     NR::Maybe<NR::Rect> current = selection->bounds();
1270     if ( !current || current->isEmpty() ) {
1271         return;
1272     }
1274     double scale_x = size_clipboard->extent(NR::X) / current->extent(NR::X);
1275     double scale_y = size_clipboard->extent(NR::Y) / current->extent(NR::Y);
1277     sp_selection_scale_relative (selection, current->midpoint(),
1278                                  NR::scale(
1279                                      apply_x? scale_x : (desktop->isToolboxButtonActive ("lock")? scale_y : 1.0),
1280                                      apply_y? scale_y : (desktop->isToolboxButtonActive ("lock")? scale_x : 1.0)));
1282     sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_SIZE,
1283                      _("Paste size"));
1286 void sp_selection_paste_size_separately (bool apply_x, bool apply_y)
1288     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1289     if (desktop == NULL) return;
1291     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1293     // check if something is in the clipboard
1294     if ( !size_clipboard ) {
1295         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1296         return;
1297     }
1299     // check if something is selected
1300     if (selection->isEmpty()) {
1301         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste size to."));
1302         return;
1303     }
1305     for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1306         SPItem *item = SP_ITEM(l->data);
1308         NR::Maybe<NR::Rect> current = sp_item_bbox_desktop(item);
1309         if ( !current || current->isEmpty() ) {
1310             continue;
1311         }
1313         double scale_x = size_clipboard->extent(NR::X) / current->extent(NR::X);
1314         double scale_y = size_clipboard->extent(NR::Y) / current->extent(NR::Y);
1316         sp_item_scale_rel (item,
1317                                  NR::scale(
1318                                      apply_x? scale_x : (desktop->isToolboxButtonActive ("lock")? scale_y : 1.0),
1319                                      apply_y? scale_y : (desktop->isToolboxButtonActive ("lock")? scale_x : 1.0)));
1321     }
1323     sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_SIZE_SEPARATELY,
1324                      _("Paste size separately"));
1327 void sp_selection_to_next_layer ()
1329     SPDesktop *dt = SP_ACTIVE_DESKTOP;
1331     Inkscape::Selection *selection = sp_desktop_selection(dt);
1333     // check if something is selected
1334     if (selection->isEmpty()) {
1335         dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer above."));
1336         return;
1337     }
1339     GSList const *items = g_slist_copy ((GSList *) selection->itemList());
1341     bool no_more = false; // Set to true, if no more layers above
1342     SPObject *next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer());
1343     if (next) {
1344         GSList *temp_clip = NULL;
1345         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
1346         sp_selection_delete_impl (items, false, false);
1347         next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
1348         GSList *copied;
1349         if(next) {
1350             copied = sp_selection_paste_impl (sp_desktop_document (dt), next, &temp_clip, NULL);
1351         } else {
1352             copied = sp_selection_paste_impl (sp_desktop_document (dt), dt->currentLayer(), &temp_clip, NULL);
1353             no_more = true;
1354         }
1355         selection->setReprList((GSList const *) copied);
1356         g_slist_free (copied);
1357         if (temp_clip) g_slist_free (temp_clip);
1358         if (next) dt->setCurrentLayer(next);
1359         sp_document_done(sp_desktop_document (dt), SP_VERB_LAYER_MOVE_TO_NEXT, 
1360                          _("Raise to next layer"));
1361     } else {
1362         no_more = true;
1363     }
1365     if (no_more) {
1366         dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers above."));
1367     }
1369     g_slist_free ((GSList *) items);
1372 void sp_selection_to_prev_layer ()
1374     SPDesktop *dt = SP_ACTIVE_DESKTOP;
1376     Inkscape::Selection *selection = sp_desktop_selection(dt);
1378     // check if something is selected
1379     if (selection->isEmpty()) {
1380         dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer below."));
1381         return;
1382     }
1384     GSList const *items = g_slist_copy ((GSList *) selection->itemList());
1386     bool no_more = false; // Set to true, if no more layers below
1387     SPObject *next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer());
1388     if (next) {
1389         GSList *temp_clip = NULL;
1390         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
1391         sp_selection_delete_impl (items, false, false);
1392         next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
1393         GSList *copied;
1394         if(next) {
1395             copied = sp_selection_paste_impl (sp_desktop_document (dt), next, &temp_clip, NULL);
1396         } else {
1397             copied = sp_selection_paste_impl (sp_desktop_document (dt), dt->currentLayer(), &temp_clip, NULL);
1398             no_more = true;
1399         }
1400         selection->setReprList((GSList const *) copied);
1401         g_slist_free (copied);
1402         if (temp_clip) g_slist_free (temp_clip);
1403         if (next) dt->setCurrentLayer(next);
1404         sp_document_done(sp_desktop_document (dt), SP_VERB_LAYER_MOVE_TO_PREV,
1405                          _("Lower to previous layer"));
1406     } else {
1407         no_more = true;
1408     }
1410     if (no_more) {
1411         dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers below."));
1412     }
1414     g_slist_free ((GSList *) items);
1417 bool
1418 selection_contains_original (SPItem *item, Inkscape::Selection *selection)
1420     bool contains_original = false;
1421     
1422     bool is_use = SP_IS_USE(item);
1423     SPItem *item_use = item;
1424     SPItem *item_use_first = item;
1425     while (is_use && item_use && !contains_original)
1426     {
1427         item_use = sp_use_get_original (SP_USE(item_use));
1428         contains_original |= selection->includes(item_use);
1429         if (item_use == item_use_first)
1430             break;
1431         is_use = SP_IS_USE(item_use);
1432     }
1433     
1434     // If it's a tref, check whether the object containing the character
1435     // data is part of the selection
1436     if (!contains_original && SP_IS_TREF(item)) {
1437         contains_original = selection->includes(SP_TREF(item)->getObjectReferredTo());
1438     }
1439        
1440     return contains_original;
1444 bool
1445 selection_contains_both_clone_and_original (Inkscape::Selection *selection)
1447     bool clone_with_original = false;
1448     for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1449         SPItem *item = SP_ITEM(l->data);
1450         clone_with_original |= selection_contains_original(item, selection);
1451         if (clone_with_original) 
1452             break;
1453     }
1454     return clone_with_original;
1458 /** Apply matrix to the selection.  \a set_i2d is normally true, which means objects are in the
1459 original transform, synced with their reprs, and need to jump to the new transform in one go. A
1460 value of set_i2d==false is only used by seltrans when it's dragging objects live (not outlines); in
1461 that case, items are already in the new position, but the repr is in the old, and this function
1462 then simply updates the repr from item->transform.
1463  */
1464 void sp_selection_apply_affine(Inkscape::Selection *selection, NR::Matrix const &affine, bool set_i2d)
1466     if (selection->isEmpty())
1467         return;
1469     for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1470         SPItem *item = SP_ITEM(l->data);
1472         NR::Point old_center(0,0);
1473         if (set_i2d && item->isCenterSet())
1474             old_center = item->getCenter();
1476 #if 0 /* Re-enable this once persistent guides have a graphical indication.
1477          At the time of writing, this is the only place to re-enable. */
1478         sp_item_update_cns(*item, selection->desktop());
1479 #endif
1481         // we're moving both a clone and its original or any ancestor in clone chain?
1482         bool transform_clone_with_original = selection_contains_original(item, selection);
1483         // ...both a text-on-path and its path?
1484         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)))) ));
1485         // ...both a flowtext and its frame?
1486         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)
1487         // ...both an offset and its source?
1488         bool transform_offset_with_source = (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) && selection->includes( sp_offset_get_source (SP_OFFSET(item)) );
1489        
1490         // If we're moving a connector, we want to detach it
1491         // from shapes that aren't part of the selection, but
1492         // leave it attached if they are
1493         if (cc_item_is_connector(item)) {
1494             SPItem *attItem[2];
1495             SP_PATH(item)->connEndPair.getAttachedItems(attItem);
1496             
1497             for (int n = 0; n < 2; ++n) {
1498                 if (!selection->includes(attItem[n])) {
1499                     sp_conn_end_detach(item, n);
1500                 }
1501             }
1502         }
1503         
1504         // "clones are unmoved when original is moved" preference
1505         int compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
1506         bool prefs_unmoved = (compensation == SP_CLONE_COMPENSATION_UNMOVED);
1507         bool prefs_parallel = (compensation == SP_CLONE_COMPENSATION_PARALLEL);
1509         // If this is a clone and it's selected along with its original, do not move it; it will feel the
1510         // transform of its original and respond to it itself. Without this, a clone is doubly
1511         // transformed, very unintuitive.
1512       // Same for textpath if we are also doing ANY transform to its path: do not touch textpath,
1513       // letters cannot be squeezed or rotated anyway, they only refill the changed path.
1514       // Same for linked offset if we are also moving its source: do not move it.
1515         if (transform_textpath_with_path || transform_offset_with_source) {
1516                 // restore item->transform field from the repr, in case it was changed by seltrans
1517             sp_object_read_attr (SP_OBJECT (item), "transform");
1519         } else if (transform_flowtext_with_frame) {
1520             // apply the inverse of the region's transform to the <use> so that the flow remains
1521             // the same (even though the output itself gets transformed)
1522             for (SPObject *region = item->firstChild() ; region ; region = SP_OBJECT_NEXT(region)) {
1523                 if (!SP_IS_FLOWREGION(region) && !SP_IS_FLOWREGIONEXCLUDE(region))
1524                     continue;
1525                 for (SPObject *use = region->firstChild() ; use ; use = SP_OBJECT_NEXT(use)) {
1526                     if (!SP_IS_USE(use)) continue;
1527                     sp_item_write_transform(SP_USE(use), SP_OBJECT_REPR(use), item->transform.inverse(), NULL);
1528                 }
1529             }
1530         } else if (transform_clone_with_original) {
1531             // We are transforming a clone along with its original. The below matrix juggling is
1532             // necessary to ensure that they transform as a whole, i.e. the clone's induced
1533             // transform and its move compensation are both cancelled out.
1535             // restore item->transform field from the repr, in case it was changed by seltrans
1536             sp_object_read_attr (SP_OBJECT (item), "transform");
1538             // calculate the matrix we need to apply to the clone to cancel its induced transform from its original
1539             NR::Matrix parent_transform = sp_item_i2root_affine(SP_ITEM(SP_OBJECT_PARENT (item)));
1540             NR::Matrix t = parent_transform * matrix_to_desktop (matrix_from_desktop (affine, item), item) * parent_transform.inverse();
1541             NR::Matrix t_inv =parent_transform * matrix_to_desktop (matrix_from_desktop (affine.inverse(), item), item) * parent_transform.inverse();
1542             NR::Matrix result = t_inv * item->transform * t;
1544             if ((prefs_parallel || prefs_unmoved) && affine.is_translation()) {
1545                 // we need to cancel out the move compensation, too
1547                 // find out the clone move, same as in sp_use_move_compensate
1548                 NR::Matrix parent = sp_use_get_parent_transform (SP_USE(item));
1549                 NR::Matrix clone_move = parent.inverse() * t * parent;
1551                 if (prefs_parallel) {
1552                     NR::Matrix move = result * clone_move * t_inv;
1553                     sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &move);
1555                 } else if (prefs_unmoved) {
1556                     //if (SP_IS_USE(sp_use_get_original(SP_USE(item))))
1557                     //    clone_move = NR::identity();
1558                     NR::Matrix move = result * clone_move;
1559                     sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &t);
1560                 }
1562             } else {
1563                 // just apply the result
1564                 sp_item_write_transform(item, SP_OBJECT_REPR(item), result, &t);
1565             }
1567         } else {
1568             if (set_i2d) {
1569                 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * affine);
1570             }
1571             sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, NULL);
1572         }
1574         // if we're moving the actual object, not just updating the repr, we can transform the
1575         // center by the same matrix (only necessary for non-translations)
1576         if (set_i2d && item->isCenterSet() && !affine.is_translation()) {
1577             item->setCenter(old_center * affine);
1578             SP_OBJECT(item)->updateRepr();
1579         }
1580     }
1583 void sp_selection_remove_transform()
1585     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1586     if (desktop == NULL)
1587         return;
1589     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1591     GSList const *l = (GSList *) selection->reprList();
1592     while (l != NULL) {
1593         sp_repr_set_attr((Inkscape::XML::Node*)l->data, "transform", NULL);
1594         l = l->next;
1595     }
1597     sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_FLATTEN, 
1598                      _("Remove transform"));
1601 void
1602 sp_selection_scale_absolute(Inkscape::Selection *selection,
1603                             double const x0, double const x1,
1604                             double const y0, double const y1)
1606     if (selection->isEmpty())
1607         return;
1609     NR::Maybe<NR::Rect> const bbox(selection->bounds());
1610     if ( !bbox || bbox->isEmpty() ) {
1611         return;
1612     }
1614     NR::translate const p2o(-bbox->min());
1616     NR::scale const newSize(x1 - x0,
1617                             y1 - y0);
1618     NR::scale const scale( newSize / NR::scale(bbox->dimensions()) );
1619     NR::translate const o2n(x0, y0);
1620     NR::Matrix const final( p2o * scale * o2n );
1622     sp_selection_apply_affine(selection, final);
1626 void sp_selection_scale_relative(Inkscape::Selection *selection, NR::Point const &align, NR::scale const &scale)
1628     if (selection->isEmpty())
1629         return;
1631     NR::Maybe<NR::Rect> const bbox(selection->bounds());
1633     if ( !bbox || bbox->isEmpty() ) {
1634         return;
1635     }
1637     // FIXME: ARBITRARY LIMIT: don't try to scale above 1 Mpx, it won't display properly and will crash sooner or later anyway
1638     if ( bbox->extent(NR::X) * scale[NR::X] > 1e6  ||
1639          bbox->extent(NR::Y) * scale[NR::Y] > 1e6 )
1640     {
1641         return;
1642     }
1644     NR::translate const n2d(-align);
1645     NR::translate const d2n(align);
1646     NR::Matrix const final( n2d * scale * d2n );
1647     sp_selection_apply_affine(selection, final);
1650 void
1651 sp_selection_rotate_relative(Inkscape::Selection *selection, NR::Point const &center, gdouble const angle_degrees)
1653     NR::translate const d2n(center);
1654     NR::translate const n2d(-center);
1655     NR::rotate const rotate(rotate_degrees(angle_degrees));
1656     NR::Matrix const final( NR::Matrix(n2d) * rotate * d2n );
1657     sp_selection_apply_affine(selection, final);
1660 void
1661 sp_selection_skew_relative(Inkscape::Selection *selection, NR::Point const &align, double dx, double dy)
1663     NR::translate const d2n(align);
1664     NR::translate const n2d(-align);
1665     NR::Matrix const skew(1, dy,
1666                           dx, 1,
1667                           0, 0);
1668     NR::Matrix const final( n2d * skew * d2n );
1669     sp_selection_apply_affine(selection, final);
1672 void sp_selection_move_relative(Inkscape::Selection *selection, NR::Point const &move)
1674     sp_selection_apply_affine(selection, NR::Matrix(NR::translate(move)));
1677 void sp_selection_move_relative(Inkscape::Selection *selection, double dx, double dy)
1679     sp_selection_apply_affine(selection, NR::Matrix(NR::translate(dx, dy)));
1683 /**
1684  * \brief sp_selection_rotate_90
1685  *
1686  * This function rotates selected objects 90 degrees clockwise.
1687  *
1688  */
1690 void sp_selection_rotate_90_cw()
1692     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1694     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1696     if (selection->isEmpty())
1697         return;
1699     GSList const *l = selection->itemList();
1700     NR::rotate const rot_neg_90(NR::Point(0, -1));
1701     for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1702         SPItem *item = SP_ITEM(l2->data);
1703         sp_item_rotate_rel(item, rot_neg_90);
1704     }
1706     sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_ROTATE_90_CCW, 
1707                      _("Rotate 90&#176; CW"));
1711 /**
1712  * \brief sp_selection_rotate_90_ccw
1713  *
1714  * This function rotates selected objects 90 degrees counter-clockwise.
1715  *
1716  */
1718 void sp_selection_rotate_90_ccw()
1720     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1722     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1724     if (selection->isEmpty())
1725         return;
1727     GSList const *l = selection->itemList();
1728     NR::rotate const rot_neg_90(NR::Point(0, 1));
1729     for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1730         SPItem *item = SP_ITEM(l2->data);
1731         sp_item_rotate_rel(item, rot_neg_90);
1732     }
1734     sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_ROTATE_90_CW,
1735                      _("Rotate 90&#176; CCW"));
1738 void
1739 sp_selection_rotate(Inkscape::Selection *selection, gdouble const angle_degrees)
1741     if (selection->isEmpty())
1742         return;
1744     NR::Maybe<NR::Point> center = selection->center();
1745     if (!center) {
1746         return;
1747     }
1749     sp_selection_rotate_relative(selection, *center, angle_degrees);
1751     sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1752                            ( ( angle_degrees > 0 )
1753                              ? "selector:rotate:ccw"
1754                              : "selector:rotate:cw" ), 
1755                            SP_VERB_CONTEXT_SELECT, 
1756                            _("Rotate"));
1759 /**
1760 \param  angle   the angle in "angular pixels", i.e. how many visible pixels must move the outermost point of the rotated object
1761 */
1762 void
1763 sp_selection_rotate_screen(Inkscape::Selection *selection, gdouble angle)
1765     if (selection->isEmpty())
1766         return;
1768     NR::Maybe<NR::Rect> const bbox(selection->bounds());
1769     NR::Maybe<NR::Point> center = selection->center();
1771     if ( !bbox || !center ) {
1772         return;
1773     }
1775     gdouble const zoom = selection->desktop()->current_zoom();
1776     gdouble const zmove = angle / zoom;
1777     gdouble const r = NR::L2(bbox->cornerFarthestFrom(*center) - *center);
1779     gdouble const zangle = 180 * atan2(zmove, r) / M_PI;
1781     sp_selection_rotate_relative(selection, *center, zangle);
1783     sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1784                            ( (angle > 0)
1785                              ? "selector:rotate:ccw"
1786                              : "selector:rotate:cw" ),
1787                            SP_VERB_CONTEXT_SELECT, 
1788                            _("Rotate by pixels"));
1791 void
1792 sp_selection_scale(Inkscape::Selection *selection, gdouble grow)
1794     if (selection->isEmpty())
1795         return;
1797     NR::Maybe<NR::Rect> const bbox(selection->bounds());
1798     if (!bbox) {
1799         return;
1800     }
1802     NR::Point const center(bbox->midpoint());
1804     // you can't scale "do nizhe pola" (below zero)
1805     double const max_len = bbox->maxExtent();
1806     if ( max_len + grow <= 1e-3 ) {
1807         return;
1808     }
1810     double const times = 1.0 + grow / max_len;
1811     sp_selection_scale_relative(selection, center, NR::scale(times, times));
1813     sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1814                            ( (grow > 0)
1815                              ? "selector:scale:larger"
1816                              : "selector:scale:smaller" ),
1817                            SP_VERB_CONTEXT_SELECT,
1818                            _("Scale"));
1821 void
1822 sp_selection_scale_screen(Inkscape::Selection *selection, gdouble grow_pixels)
1824     sp_selection_scale(selection,
1825                        grow_pixels / selection->desktop()->current_zoom());
1828 void
1829 sp_selection_scale_times(Inkscape::Selection *selection, gdouble times)
1831     if (selection->isEmpty())
1832         return;
1834     NR::Maybe<NR::Rect> sel_bbox = selection->bounds();
1836     if (!sel_bbox) {
1837         return;
1838     }
1840     NR::Point const center(sel_bbox->midpoint());
1841     sp_selection_scale_relative(selection, center, NR::scale(times, times));
1842     sp_document_done(sp_desktop_document(selection->desktop()), SP_VERB_CONTEXT_SELECT, 
1843                      _("Scale by whole factor"));
1846 void
1847 sp_selection_move(gdouble dx, gdouble dy)
1849     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1850     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1851     if (selection->isEmpty()) {
1852         return;
1853     }
1855     sp_selection_move_relative(selection, dx, dy);
1857     if (dx == 0) {
1858         sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical", SP_VERB_CONTEXT_SELECT, 
1859                                _("Move vertically"));
1860     } else if (dy == 0) {
1861         sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal", SP_VERB_CONTEXT_SELECT, 
1862                                _("Move horizontally"));
1863     } else {
1864         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SELECT, 
1865                          _("Move"));
1866     }
1869 void
1870 sp_selection_move_screen(gdouble dx, gdouble dy)
1872     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1874     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1875     if (selection->isEmpty()) {
1876         return;
1877     }
1879     // same as sp_selection_move but divide deltas by zoom factor
1880     gdouble const zoom = desktop->current_zoom();
1881     gdouble const zdx = dx / zoom;
1882     gdouble const zdy = dy / zoom;
1883     sp_selection_move_relative(selection, zdx, zdy);
1885     if (dx == 0) {
1886         sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical", SP_VERB_CONTEXT_SELECT, 
1887                                _("Move vertically by pixels"));
1888     } else if (dy == 0) {
1889         sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal", SP_VERB_CONTEXT_SELECT, 
1890                                _("Move horizontally by pixels"));
1891     } else {
1892         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SELECT, 
1893                          _("Move"));
1894     }
1897 namespace {
1899 template <typename D>
1900 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
1901                   bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
1903 template <typename D>
1904 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items, SPObject *root,
1905                   bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
1907 struct Forward {
1908     typedef SPObject *Iterator;
1910     static Iterator children(SPObject *o) { return sp_object_first_child(o); }
1911     static Iterator siblings_after(SPObject *o) { return SP_OBJECT_NEXT(o); }
1912     static void dispose(Iterator i) {}
1914     static SPObject *object(Iterator i) { return i; }
1915     static Iterator next(Iterator i) { return SP_OBJECT_NEXT(i); }
1916 };
1918 struct Reverse {
1919     typedef GSList *Iterator;
1921     static Iterator children(SPObject *o) {
1922         return make_list(o->firstChild(), NULL);
1923     }
1924     static Iterator siblings_after(SPObject *o) {
1925         return make_list(SP_OBJECT_PARENT(o)->firstChild(), o);
1926     }
1927     static void dispose(Iterator i) {
1928         g_slist_free(i);
1929     }
1931     static SPObject *object(Iterator i) {
1932         return reinterpret_cast<SPObject *>(i->data);
1933     }
1934     static Iterator next(Iterator i) { return i->next; }
1936 private:
1937     static GSList *make_list(SPObject *object, SPObject *limit) {
1938         GSList *list=NULL;
1939         while ( object != limit ) {
1940             list = g_slist_prepend(list, object);
1941             object = SP_OBJECT_NEXT(object);
1942         }
1943         return list;
1944     }
1945 };
1949 void
1950 sp_selection_item_next(void)
1952     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1953     g_return_if_fail(desktop != NULL);
1954     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1956     PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
1957     bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
1958     bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
1960     SPObject *root;
1961     if (PREFS_SELECTION_ALL != inlayer) {
1962         root = selection->activeContext();
1963     } else {
1964         root = desktop->currentRoot();
1965     }
1967     SPItem *item=next_item_from_list<Forward>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1969     if (item) {
1970         selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
1971         if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1972             scroll_to_show_item(desktop, item);
1973         }
1974     }
1977 void
1978 sp_selection_item_prev(void)
1980     SPDocument *document = SP_ACTIVE_DOCUMENT;
1981     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1982     g_return_if_fail(document != NULL);
1983     g_return_if_fail(desktop != NULL);
1984     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1986     PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
1987     bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
1988     bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
1990     SPObject *root;
1991     if (PREFS_SELECTION_ALL != inlayer) {
1992         root = selection->activeContext();
1993     } else {
1994         root = desktop->currentRoot();
1995     }
1997     SPItem *item=next_item_from_list<Reverse>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1999     if (item) {
2000         selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
2001         if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
2002             scroll_to_show_item(desktop, item);
2003         }
2004     }
2007 void sp_selection_next_patheffect_param(SPDesktop * dt)
2009     if (!dt) return;
2011     Inkscape::Selection *selection = sp_desktop_selection(dt);
2012     if ( selection && !selection->isEmpty() ) {
2013         SPItem *item = selection->singleItem();
2014         if ( item && SP_IS_SHAPE(item)) {
2015             SPShape *shape = SP_SHAPE(item);
2016             if (sp_shape_has_path_effect(shape)) {
2017                 sp_shape_edit_next_param_oncanvas(shape, dt);
2018             } else {
2019                 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("The selection has no applied path effect."));
2020             }
2021         }
2022     }
2025 namespace {
2027 template <typename D>
2028 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items,
2029                             SPObject *root, bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
2031     SPObject *current=root;
2032     while (items) {
2033         SPItem *item=SP_ITEM(items->data);
2034         if ( root->isAncestorOf(item) &&
2035              ( !only_in_viewport || desktop->isWithinViewport(item) ) )
2036         {
2037             current = item;
2038             break;
2039         }
2040         items = items->next;
2041     }
2043     GSList *path=NULL;
2044     while ( current != root ) {
2045         path = g_slist_prepend(path, current);
2046         current = SP_OBJECT_PARENT(current);
2047     }
2049     SPItem *next;
2050     // first, try from the current object
2051     next = next_item<D>(desktop, path, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
2052     g_slist_free(path);
2054     if (!next) { // if we ran out of objects, start over at the root
2055         next = next_item<D>(desktop, NULL, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
2056     }
2058     return next;
2061 template <typename D>
2062 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
2063                   bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
2065     typename D::Iterator children;
2066     typename D::Iterator iter;
2068     SPItem *found=NULL;
2070     if (path) {
2071         SPObject *object=reinterpret_cast<SPObject *>(path->data);
2072         g_assert(SP_OBJECT_PARENT(object) == root);
2073         if (desktop->isLayer(object)) {
2074             found = next_item<D>(desktop, path->next, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
2075         }
2076         iter = children = D::siblings_after(object);
2077     } else {
2078         iter = children = D::children(root);
2079     }
2081     while ( iter && !found ) {
2082         SPObject *object=D::object(iter);
2083         if (desktop->isLayer(object)) {
2084             if (PREFS_SELECTION_LAYER != inlayer) { // recurse into sublayers
2085                 found = next_item<D>(desktop, NULL, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
2086             }
2087         } else if ( SP_IS_ITEM(object) &&
2088                     ( !only_in_viewport || desktop->isWithinViewport(SP_ITEM(object)) ) &&
2089                     ( !onlyvisible || !desktop->itemIsHidden(SP_ITEM(object))) &&
2090                     ( !onlysensitive || !SP_ITEM(object)->isLocked()) &&
2091                     !desktop->isLayer(SP_ITEM(object)) )
2092         {
2093             found = SP_ITEM(object);
2094         }
2095         iter = D::next(iter);
2096     }
2098     D::dispose(children);
2100     return found;
2105 /**
2106  * If \a item is not entirely visible then adjust visible area to centre on the centre on of
2107  * \a item.
2108  */
2109 void scroll_to_show_item(SPDesktop *desktop, SPItem *item)
2111     NR::Rect dbox = desktop->get_display_area();
2112     NR::Maybe<NR::Rect> sbox = sp_item_bbox_desktop(item);
2114     if ( sbox && dbox.contains(*sbox) == false ) {
2115         NR::Point const s_dt = sbox->midpoint();
2116         NR::Point const s_w = desktop->d2w(s_dt);
2117         NR::Point const d_dt = dbox.midpoint();
2118         NR::Point const d_w = desktop->d2w(d_dt);
2119         NR::Point const moved_w( d_w - s_w );
2120         gint const dx = (gint) moved_w[X];
2121         gint const dy = (gint) moved_w[Y];
2122         desktop->scroll_world(dx, dy);
2123     }
2127 void
2128 sp_selection_clone()
2130     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2131     if (desktop == NULL)
2132         return;
2134     Inkscape::Selection *selection = sp_desktop_selection(desktop);
2136     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
2138     // check if something is selected
2139     if (selection->isEmpty()) {
2140         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object</b> to clone."));
2141         return;
2142     }
2144     GSList *reprs = g_slist_copy((GSList *) selection->reprList());
2145   
2146     selection->clear();
2147   
2148     // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
2149     reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
2151     GSList *newsel = NULL;
2152  
2153     while (reprs) {
2154         Inkscape::XML::Node *sel_repr = (Inkscape::XML::Node *) reprs->data;
2155         Inkscape::XML::Node *parent = sp_repr_parent(sel_repr);
2157         Inkscape::XML::Node *clone = xml_doc->createElement("svg:use");
2158         sp_repr_set_attr(clone, "x", "0");
2159         sp_repr_set_attr(clone, "y", "0");
2160         sp_repr_set_attr(clone, "xlink:href", g_strdup_printf("#%s", sel_repr->attribute("id")));
2162         sp_repr_set_attr(clone, "inkscape:transform-center-x", sel_repr->attribute("inkscape:transform-center-x"));
2163         sp_repr_set_attr(clone, "inkscape:transform-center-y", sel_repr->attribute("inkscape:transform-center-y"));
2164         
2165         // add the new clone to the top of the original's parent
2166         parent->appendChild(clone);
2168         newsel = g_slist_prepend(newsel, clone);
2169         reprs = g_slist_remove(reprs, sel_repr);
2170         Inkscape::GC::release(clone);
2171     }
2172     
2173     // TRANSLATORS: only translate "string" in "context|string".
2174     // For more details, see http://developer.gnome.org/doc/API/2.0/glib/glib-I18N.html#Q-:CAPS
2175     sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_CLONE, 
2176                      Q_("action|Clone"));
2178     selection->setReprList(newsel);
2179  
2180     g_slist_free(newsel);
2183 void
2184 sp_selection_unlink()
2186     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2187     if (!desktop)
2188         return;
2190     Inkscape::Selection *selection = sp_desktop_selection(desktop);
2192     if (selection->isEmpty()) {
2193         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>clone</b> to unlink."));
2194         return;
2195     }
2197     // Get a copy of current selection.
2198     GSList *new_select = NULL;
2199     bool unlinked = false;
2200     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2201          items != NULL;
2202          items = items->next)
2203     {
2204         SPItem *item = (SPItem *) items->data;
2206         if (SP_IS_TEXT(item)) {
2207             SPObject *tspan = sp_tref_convert_to_tspan(SP_OBJECT(item));
2208             
2209             if (tspan) {            
2210                 SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
2211             }
2212             
2213             // Set unlink to true, and fall into the next if which
2214             // will include this text item in the new selection
2215             unlinked = true;
2216         }
2218         if (!(SP_IS_USE(item) || SP_IS_TREF(item))) {
2219             // keep the non-use item in the new selection
2220             new_select = g_slist_prepend(new_select, item);
2221             continue;
2222         }
2224         SPItem *unlink;
2225         if (SP_IS_USE(item)) { 
2226             unlink = sp_use_unlink(SP_USE(item));
2227         } else /*if (SP_IS_TREF(use))*/ {
2228             unlink = SP_ITEM(sp_tref_convert_to_tspan(SP_OBJECT(item)));
2229         }
2230         
2231         unlinked = true;
2232         // Add ungrouped items to the new selection.
2233         new_select = g_slist_prepend(new_select, unlink);
2234     }
2236     if (new_select) { // set new selection
2237         selection->clear();
2238         selection->setList(new_select);
2239         g_slist_free(new_select);
2240     }
2241     if (!unlinked) {
2242         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No clones to unlink</b> in the selection."));
2243     }
2245     sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNLINK_CLONE,
2246                      _("Unlink clone"));
2249 void
2250 sp_select_clone_original()
2252     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2253     if (desktop == NULL)
2254         return;
2256     Inkscape::Selection *selection = sp_desktop_selection(desktop);
2258     SPItem *item = selection->singleItem();
2260     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.");
2262     // Check if other than two objects are selected
2263     if (g_slist_length((GSList *) selection->itemList()) != 1 || !item) {
2264         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
2265         return;
2266     }
2268     SPItem *original = NULL;
2269     if (SP_IS_USE(item)) {
2270         original = sp_use_get_original (SP_USE(item));
2271     } else if (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) {
2272         original = sp_offset_get_source (SP_OFFSET(item));
2273     } else if (SP_IS_TEXT_TEXTPATH(item)) {
2274         original = sp_textpath_get_path_item (SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))));
2275     } else if (SP_IS_FLOWTEXT(item)) {
2276         original = SP_FLOWTEXT(item)->get_frame (NULL); // first frame only
2277     } else { // it's an object that we don't know what to do with
2278         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
2279         return;
2280     }
2282     if (!original) {
2283         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>Cannot find</b> the object to select (orphaned clone, offset, textpath, flowed text?)"));
2284         return;
2285     }
2287     for (SPObject *o = original; o && !SP_IS_ROOT(o); o = SP_OBJECT_PARENT (o)) {
2288         if (SP_IS_DEFS (o)) {
2289             desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("The object you're trying to select is <b>not visible</b> (it is in &lt;defs&gt;)"));
2290             return;
2291         }
2292     }
2294     if (original) {
2295         selection->clear();
2296         selection->set(original);
2297         if (SP_CYCLING == SP_CYCLE_FOCUS) {
2298             scroll_to_show_item(desktop, original);
2299         }
2300     }
2304 void sp_selection_to_marker(bool apply)
2306     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2307     if (desktop == NULL)
2308         return;
2310     SPDocument *doc = sp_desktop_document(desktop);
2311     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2313     Inkscape::Selection *selection = sp_desktop_selection(desktop);
2315     // check if something is selected
2316     if (selection->isEmpty()) {
2317         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to marker."));
2318         return;
2319     }
2321     sp_document_ensure_up_to_date(doc);
2322     NR::Maybe<NR::Rect> r = selection->bounds();
2323     if ( !r || r->isEmpty() ) {
2324         return;
2325     }
2327     // calculate the transform to be applied to objects to move them to 0,0
2328     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));
2329     move_p[NR::Y] = -move_p[NR::Y];
2330     NR::Matrix move = NR::Matrix (NR::translate (move_p));
2332     GSList *items = g_slist_copy((GSList *) selection->itemList());
2334     items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2336     // bottommost object, after sorting
2337     SPObject *parent = SP_OBJECT_PARENT (items->data);
2339     NR::Matrix parent_transform = sp_item_i2root_affine(SP_ITEM(parent));
2341     // remember the position of the first item
2342     gint pos = SP_OBJECT_REPR (items->data)->position();
2344     // create a list of duplicates
2345     GSList *repr_copies = NULL;
2346     for (GSList *i = items; i != NULL; i = i->next) {
2347         Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2348         repr_copies = g_slist_prepend (repr_copies, dup);
2349     }
2351     NR::Rect bounds(desktop->dt2doc(r->min()), desktop->dt2doc(r->max()));
2353     if (apply) {
2354         // delete objects so that their clones don't get alerted; this object will be restored shortly
2355         for (GSList *i = items; i != NULL; i = i->next) {
2356             SPObject *item = SP_OBJECT (i->data);
2357             item->deleteObject (false);
2358         }
2359     }
2361     // Hack: Temporarily set clone compensation to unmoved, so that we can move clone-originals
2362     // without disturbing clones.
2363     // See ActorAlign::on_button_click() in src/ui/dialog/align-and-distribute.cpp
2364     int saved_compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2365     prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2367     gchar const *mark_id = generate_marker(repr_copies, bounds, doc,
2368                                            ( NR::Matrix(NR::translate(desktop->dt2doc(NR::Point(r->min()[NR::X],
2369                                                                                                 r->max()[NR::Y]))))
2370                                              * parent_transform.inverse() ),
2371                                            parent_transform * move);
2373     // restore compensation setting
2374     prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
2377     g_slist_free (items);
2379     sp_document_done (doc, SP_VERB_EDIT_SELECTION_2_MARKER,
2380                       _("Objects to marker"));
2383 void
2384 sp_selection_tile(bool apply)
2386     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2387     if (desktop == NULL)
2388         return;
2390     SPDocument *doc = sp_desktop_document(desktop);
2391     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2393     Inkscape::Selection *selection = sp_desktop_selection(desktop);
2395     // check if something is selected
2396     if (selection->isEmpty()) {
2397         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to pattern."));
2398         return;
2399     }
2401     sp_document_ensure_up_to_date(doc);
2402     NR::Maybe<NR::Rect> r = selection->bounds();
2403     if ( !r || r->isEmpty() ) {
2404         return;
2405     }
2407     // calculate the transform to be applied to objects to move them to 0,0
2408     NR::Point move_p = NR::Point(0, sp_document_height(doc)) - (r->min() + NR::Point (0, r->extent(NR::Y)));
2409     move_p[NR::Y] = -move_p[NR::Y];
2410     NR::Matrix move = NR::Matrix (NR::translate (move_p));
2412     GSList *items = g_slist_copy((GSList *) selection->itemList());
2414     items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2416     // bottommost object, after sorting
2417     SPObject *parent = SP_OBJECT_PARENT (items->data);
2419     NR::Matrix parent_transform = sp_item_i2root_affine(SP_ITEM(parent));
2421     // remember the position of the first item
2422     gint pos = SP_OBJECT_REPR (items->data)->position();
2424     // create a list of duplicates
2425     GSList *repr_copies = NULL;
2426     for (GSList *i = items; i != NULL; i = i->next) {
2427         Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2428         repr_copies = g_slist_prepend (repr_copies, dup);
2429     }
2431     NR::Rect bounds(desktop->dt2doc(r->min()), desktop->dt2doc(r->max()));
2433     if (apply) {
2434         // delete objects so that their clones don't get alerted; this object will be restored shortly
2435         for (GSList *i = items; i != NULL; i = i->next) {
2436             SPObject *item = SP_OBJECT (i->data);
2437             item->deleteObject (false);
2438         }
2439     }
2441     // Hack: Temporarily set clone compensation to unmoved, so that we can move clone-originals
2442     // without disturbing clones.
2443     // See ActorAlign::on_button_click() in src/ui/dialog/align-and-distribute.cpp
2444     int saved_compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2445     prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2447     gchar const *pat_id = pattern_tile(repr_copies, bounds, doc,
2448                                        ( NR::Matrix(NR::translate(desktop->dt2doc(NR::Point(r->min()[NR::X],
2449                                                                                             r->max()[NR::Y]))))
2450                                          * parent_transform.inverse() ),
2451                                        parent_transform * move);
2453     // restore compensation setting
2454     prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
2456     if (apply) {
2457         Inkscape::XML::Node *rect = xml_doc->createElement("svg:rect");
2458         rect->setAttribute("style", g_strdup_printf("stroke:none;fill:url(#%s)", pat_id));
2460         NR::Point min = bounds.min() * parent_transform.inverse();
2461         NR::Point max = bounds.max() * parent_transform.inverse();
2463         sp_repr_set_svg_double(rect, "width", max[NR::X] - min[NR::X]);
2464         sp_repr_set_svg_double(rect, "height", max[NR::Y] - min[NR::Y]);
2465         sp_repr_set_svg_double(rect, "x", min[NR::X]);
2466         sp_repr_set_svg_double(rect, "y", min[NR::Y]);
2468         // restore parent and position
2469         SP_OBJECT_REPR (parent)->appendChild(rect);
2470         rect->setPosition(pos > 0 ? pos : 0);
2471         SPItem *rectangle = (SPItem *) sp_desktop_document (desktop)->getObjectByRepr(rect);
2473         Inkscape::GC::release(rect);
2475         selection->clear();
2476         selection->set(rectangle);
2477     }
2479     g_slist_free (items);
2481     sp_document_done (doc, SP_VERB_EDIT_TILE, 
2482                       _("Objects to pattern"));
2485 void
2486 sp_selection_untile()
2488     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2489     if (desktop == NULL)
2490         return;
2492     SPDocument *doc = sp_desktop_document(desktop);
2493     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2495     Inkscape::Selection *selection = sp_desktop_selection(desktop);
2497     // check if something is selected
2498     if (selection->isEmpty()) {
2499         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object with pattern fill</b> to extract objects from."));
2500         return;
2501     }
2503     GSList *new_select = NULL;
2505     bool did = false;
2507     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2508          items != NULL;
2509          items = items->next) {
2511         SPItem *item = (SPItem *) items->data;
2513         SPStyle *style = SP_OBJECT_STYLE (item);
2515         if (!style || !style->fill.isPaintserver())
2516             continue;
2518         SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
2520         if (!SP_IS_PATTERN(server))
2521             continue;
2523         did = true;
2525         SPPattern *pattern = pattern_getroot (SP_PATTERN (server));
2527         NR::Matrix pat_transform = pattern_patternTransform (SP_PATTERN (server));
2528         pat_transform *= item->transform;
2530         for (SPObject *child = sp_object_first_child(SP_OBJECT(pattern)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2531             Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate(xml_doc);
2532             SPItem *i = SP_ITEM (desktop->currentLayer()->appendChildRepr(copy));
2534            // FIXME: relink clones to the new canvas objects
2535            // use SPObject::setid when mental finishes it to steal ids of
2537             // this is needed to make sure the new item has curve (simply requestDisplayUpdate does not work)
2538             sp_document_ensure_up_to_date (doc);
2540             NR::Matrix transform( i->transform * pat_transform );
2541             sp_item_write_transform(i, SP_OBJECT_REPR(i), transform);
2543             new_select = g_slist_prepend(new_select, i);
2544         }
2546         SPCSSAttr *css = sp_repr_css_attr_new ();
2547         sp_repr_css_set_property (css, "fill", "none");
2548         sp_repr_css_change (SP_OBJECT_REPR (item), css, "style");
2549     }
2551     if (!did) {
2552         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No pattern fills</b> in the selection."));
2553     } else {
2554         sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNTILE, 
2555                          _("Pattern to objects"));
2556         selection->setList(new_select);
2557     }
2560 void
2561 sp_selection_get_export_hints (Inkscape::Selection *selection, char const **filename, float *xdpi, float *ydpi) 
2563     if (selection->isEmpty()) {
2564         return;
2565     }
2567     GSList const *reprlst = selection->reprList();
2568     bool filename_search = TRUE;
2569     bool xdpi_search = TRUE;
2570     bool ydpi_search = TRUE;
2572     for(; reprlst != NULL &&
2573             filename_search &&
2574             xdpi_search &&
2575             ydpi_search;
2576         reprlst = reprlst->next) {
2577         gchar const *dpi_string;
2578         Inkscape::XML::Node * repr = (Inkscape::XML::Node *)reprlst->data;
2580         if (filename_search) {
2581             *filename = repr->attribute("inkscape:export-filename");
2582             if (*filename != NULL)
2583                 filename_search = FALSE;
2584         }
2586         if (xdpi_search) {
2587             dpi_string = NULL;
2588             dpi_string = repr->attribute("inkscape:export-xdpi");
2589             if (dpi_string != NULL) {
2590                 *xdpi = atof(dpi_string);
2591                 xdpi_search = FALSE;
2592             }
2593         }
2595         if (ydpi_search) {
2596             dpi_string = NULL;
2597             dpi_string = repr->attribute("inkscape:export-ydpi");
2598             if (dpi_string != NULL) {
2599                 *ydpi = atof(dpi_string);
2600                 ydpi_search = FALSE;
2601             }
2602         }
2603     }
2606 void
2607 sp_document_get_export_hints (SPDocument *doc, char const **filename, float *xdpi, float *ydpi)
2609     Inkscape::XML::Node * repr = sp_document_repr_root(doc);
2610     gchar const *dpi_string;
2612     *filename = repr->attribute("inkscape:export-filename");
2614     dpi_string = NULL;
2615     dpi_string = repr->attribute("inkscape:export-xdpi");
2616     if (dpi_string != NULL) {
2617         *xdpi = atof(dpi_string);
2618     }
2620     dpi_string = NULL;
2621     dpi_string = repr->attribute("inkscape:export-ydpi");
2622     if (dpi_string != NULL) {
2623         *ydpi = atof(dpi_string);
2624     }
2627 void
2628 sp_selection_create_bitmap_copy ()
2630     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2631     if (desktop == NULL)
2632         return;
2634     SPDocument *document = sp_desktop_document(desktop);
2635     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
2637     Inkscape::Selection *selection = sp_desktop_selection(desktop);
2639     // check if something is selected
2640     if (selection->isEmpty()) {
2641         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to make a bitmap copy."));
2642         return;
2643     }
2645     // Get the bounding box of the selection
2646     NRRect bbox;
2647     sp_document_ensure_up_to_date (document);
2648     selection->bounds(&bbox);
2649     if (NR_RECT_DFLS_TEST_EMPTY(&bbox)) {
2650         return; // exceptional situation, so not bother with a translatable error message, just quit quietly
2651     }
2653     // List of the items to show; all others will be hidden
2654     GSList *items = g_slist_copy ((GSList *) selection->itemList());
2656     // Sort items so that the topmost comes last
2657     items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
2659     // Generate a random value from the current time (you may create bitmap from the same object(s)
2660     // multiple times, and this is done so that they don't clash)
2661     GTimeVal cu;
2662     g_get_current_time (&cu);
2663     guint current = (int) (cu.tv_sec * 1000000 + cu.tv_usec) % 1024;
2665     // Create the filename
2666     gchar *filename = g_strdup_printf ("%s-%s-%u.png", document->name, SP_OBJECT_REPR(items->data)->attribute("id"), current);
2667     // Imagemagick is known not to handle spaces in filenames, so we replace anything but letters,
2668     // digits, and a few other chars, with "_"
2669     filename = g_strcanon (filename, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.=+~$#@^&!?", '_');
2670     // Build the complete path by adding document->base if set
2671     gchar *filepath = g_build_filename (document->base?document->base:"", filename, NULL);
2673     //g_print ("%s\n", filepath);
2675     // Remember parent and z-order of the topmost one
2676     gint pos = SP_OBJECT_REPR(g_slist_last(items)->data)->position();
2677     SPObject *parent_object = SP_OBJECT_PARENT(g_slist_last(items)->data);
2678     Inkscape::XML::Node *parent = SP_OBJECT_REPR(parent_object);
2680     // Calculate resolution
2681     double res;
2682     int const prefs_res = prefs_get_int_attribute ("options.createbitmap", "resolution", 0);
2683     int const prefs_min = prefs_get_int_attribute ("options.createbitmap", "minsize", 0);
2684     if (0 < prefs_res) {
2685         // If it's given explicitly in prefs, take it
2686         res = prefs_res;
2687     } else if (0 < prefs_min) {
2688         // If minsize is given, look up minimum bitmap size (default 250 pixels) and calculate resolution from it
2689         res = PX_PER_IN * prefs_min / MIN ((bbox.x1 - bbox.x0), (bbox.y1 - bbox.y0));
2690     } else {
2691         float hint_xdpi = 0, hint_ydpi = 0;
2692         char const *hint_filename;
2693         // take resolution hint from the selected objects
2694         sp_selection_get_export_hints (selection, &hint_filename, &hint_xdpi, &hint_ydpi);
2695         if (hint_xdpi != 0) {
2696             res = hint_xdpi;
2697         } else {
2698             // take resolution hint from the document
2699             sp_document_get_export_hints (document, &hint_filename, &hint_xdpi, &hint_ydpi);
2700             if (hint_xdpi != 0) {
2701                 res = hint_xdpi;
2702             } else {
2703                 // if all else fails, take the default 90 dpi
2704                 res = PX_PER_IN;
2705             }
2706         }
2707     }
2709     // The width and height of the bitmap in pixels
2710     unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * res / PX_PER_IN);
2711     unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * res / PX_PER_IN);
2713     // Find out if we have to run a filter
2714     gchar const *run = NULL;
2715     gchar const *filter = prefs_get_string_attribute ("options.createbitmap", "filter");
2716     if (filter) {
2717         // filter command is given;
2718         // see if we have a parameter to pass to it
2719         gchar const *param1 = prefs_get_string_attribute ("options.createbitmap", "filter_param1");
2720         if (param1) {
2721             if (param1[strlen(param1) - 1] == '%') {
2722                 // if the param string ends with %, interpret it as a percentage of the image's max dimension
2723                 gchar p1[256];
2724                 g_ascii_dtostr (p1, 256, ceil (g_ascii_strtod (param1, NULL) * MAX(width, height) / 100));
2725                 // the first param is always the image filename, the second is param1
2726                 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, p1);
2727             } else {
2728                 // otherwise pass the param1 unchanged
2729                 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, param1);
2730             }
2731         } else {
2732             // run without extra parameter
2733             run = g_strdup_printf ("%s \"%s\"", filter, filepath);
2734         }
2735     }
2737     // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
2738     NR::Matrix eek = sp_item_i2d_affine (SP_ITEM(parent_object));
2739     NR::Matrix t;
2741     double shift_x = bbox.x0;
2742     double shift_y = bbox.y1; 
2743     if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
2744         shift_x = round (shift_x);
2745         shift_y = -round (-shift_y); // this gets correct rounding despite coordinate inversion, remove the negations when the inversion is gone
2746     }
2747     t = NR::scale(1, -1) * NR::translate (shift_x, shift_y) * eek.inverse();
2749     // Do the export
2750     sp_export_png_file(document, filepath,
2751                    bbox.x0, bbox.y0, bbox.x1, bbox.y1,
2752                    width, height, res, res,
2753                    (guint32) 0xffffff00,
2754                    NULL, NULL,
2755                    true,  /*bool force_overwrite,*/
2756                    items);
2758     g_slist_free (items);
2760     // Run filter, if any
2761     if (run) {
2762         g_print ("Running external filter: %s\n", run);
2763         system (run);
2764     }
2766     // Import the image back
2767     GdkPixbuf *pb = gdk_pixbuf_new_from_file (filepath, NULL);
2768     if (pb) {
2769         // Create the repr for the image
2770         Inkscape::XML::Node * repr = xml_doc->createElement("svg:image");
2771         repr->setAttribute("xlink:href", filename);
2772         repr->setAttribute("sodipodi:absref", filepath);
2773         if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
2774             sp_repr_set_svg_double(repr, "width", width);
2775             sp_repr_set_svg_double(repr, "height", height);
2776         } else {
2777             sp_repr_set_svg_double(repr, "width", (bbox.x1 - bbox.x0));
2778             sp_repr_set_svg_double(repr, "height", (bbox.y1 - bbox.y0));
2779         }
2781         // Write transform
2782         gchar *c=sp_svg_transform_write(t);
2783         repr->setAttribute("transform", c);
2784         g_free(c);
2786         // add the new repr to the parent
2787         parent->appendChild(repr);
2789         // move to the saved position
2790         repr->setPosition(pos > 0 ? pos + 1 : 1);
2792         // Set selection to the new image
2793         selection->clear();
2794         selection->add(repr);
2796         // Clean up
2797         Inkscape::GC::release(repr);
2798         gdk_pixbuf_unref (pb);
2800         // Complete undoable transaction
2801         sp_document_done (document, SP_VERB_SELECTION_CREATE_BITMAP,
2802                           _("Create bitmap"));
2803     }
2805     g_free (filename);
2806     g_free (filepath);
2809 /**
2810  * \brief sp_selection_set_mask
2811  *
2812  * This function creates a mask or clipPath from selection
2813  * Two different modes:
2814  *  if applyToLayer, all selection is moved to DEFS as mask/clippath
2815  *       and is applied to current layer
2816  *  otherwise, topmost object is used as mask for other objects
2817  * If \a apply_clip_path parameter is true, clipPath is created, otherwise mask
2818  * 
2819  */
2820 void
2821 sp_selection_set_mask(bool apply_clip_path, bool apply_to_layer)
2823     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2824     if (desktop == NULL)
2825         return;
2827     SPDocument *doc = sp_desktop_document(desktop);
2828     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2829     
2830     Inkscape::Selection *selection = sp_desktop_selection(desktop);
2832     // check if something is selected
2833     bool is_empty = selection->isEmpty();
2834     if ( apply_to_layer && is_empty) {
2835         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to create clippath or mask from."));
2836         return;
2837     } else if (!apply_to_layer && ( is_empty || NULL == selection->itemList()->next )) {
2838         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select mask object and <b>object(s)</b> to apply clippath or mask to."));
2839         return;
2840     }
2842     // FIXME: temporary patch to prevent crash! 
2843     // Remove this when bboxes are fixed to not blow up on an item clipped/masked with its own clone
2844     bool clone_with_original = selection_contains_both_clone_and_original (selection);
2845     if (clone_with_original) {
2846         return; // in this version, you cannot clip/mask an object with its own clone
2847     }
2848     // /END FIXME
2849     
2850     sp_document_ensure_up_to_date(doc);
2852     GSList *items = g_slist_copy((GSList *) selection->itemList());
2853     
2854     items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2856     // create a list of duplicates
2857     GSList *mask_items = NULL;
2858     GSList *apply_to_items = NULL;
2859     GSList *items_to_delete = NULL;
2860     bool topmost = prefs_get_int_attribute ("options.maskobject", "topmost", 1);
2861     bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
2862     
2863     if (apply_to_layer) {
2864         // all selected items are used for mask, which is applied to a layer
2865         apply_to_items = g_slist_prepend (apply_to_items, desktop->currentLayer());
2867         for (GSList *i = items; i != NULL; i = i->next) {
2868             Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2869             mask_items = g_slist_prepend (mask_items, dup);
2871             if (remove_original) {
2872                 SPObject *item = SP_OBJECT (i->data);
2873                 items_to_delete = g_slist_prepend (items_to_delete, item);
2874             }
2875         }
2876     } else if (!topmost) {
2877         // topmost item is used as a mask, which is applied to other items in a selection
2878         GSList *i = items;
2879         Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2880         mask_items = g_slist_prepend (mask_items, dup);
2882         if (remove_original) {
2883             SPObject *item = SP_OBJECT (i->data);
2884             items_to_delete = g_slist_prepend (items_to_delete, item);
2885         }
2886         
2887         for (i = i->next; i != NULL; i = i->next) {
2888             apply_to_items = g_slist_prepend (apply_to_items, i->data);
2889         }
2890     } else {
2891         GSList *i = NULL;
2892         for (i = items; NULL != i->next; i = i->next) {
2893             apply_to_items = g_slist_prepend (apply_to_items, i->data);
2894         }
2896         Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2897         mask_items = g_slist_prepend (mask_items, dup);
2899         if (remove_original) {
2900             SPObject *item = SP_OBJECT (i->data);
2901             items_to_delete = g_slist_prepend (items_to_delete, item);
2902         }
2903     }
2904     
2905     g_slist_free (items);
2906     items = NULL;
2907             
2908     gchar const *attributeName = apply_clip_path ? "clip-path" : "mask";
2909     for (GSList *i = apply_to_items; NULL != i; i = i->next) {
2910         SPItem *item = reinterpret_cast<SPItem *>(i->data);
2911         // inverted object transform should be applied to a mask object,
2912         // as mask is calculated in user space (after applying transform)
2913         NR::Matrix maskTransform (item->transform.inverse());
2915         GSList *mask_items_dup = NULL;
2916         for (GSList *mask_item = mask_items; NULL != mask_item; mask_item = mask_item->next) {
2917             Inkscape::XML::Node *dup = reinterpret_cast<Inkscape::XML::Node *>(mask_item->data)->duplicate(xml_doc);
2918             mask_items_dup = g_slist_prepend (mask_items_dup, dup);
2919         }
2921         gchar const *mask_id = NULL;
2922         if (apply_clip_path) {
2923             mask_id = sp_clippath_create(mask_items_dup, doc, &maskTransform);
2924         } else {
2925             mask_id = sp_mask_create(mask_items_dup, doc, &maskTransform);
2926         }
2928         g_slist_free (mask_items_dup);
2929         mask_items_dup = NULL;
2931         SP_OBJECT_REPR(i->data)->setAttribute(attributeName, g_strdup_printf("url(#%s)", mask_id));
2932     }
2934     g_slist_free (mask_items);
2935     g_slist_free (apply_to_items);
2937     for (GSList *i = items_to_delete; NULL != i; i = i->next) {
2938         SPObject *item = SP_OBJECT (i->data);
2939         item->deleteObject (false);
2940     }
2941     g_slist_free (items_to_delete);
2943     if (apply_clip_path) 
2944         sp_document_done (doc, SP_VERB_OBJECT_SET_CLIPPATH, _("Set clipping path"));
2945     else 
2946         sp_document_done (doc, SP_VERB_OBJECT_SET_MASK, _("Set mask"));
2949 void sp_selection_unset_mask(bool apply_clip_path) {
2950     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2951     if (desktop == NULL)
2952         return;
2953     
2954     SPDocument *doc = sp_desktop_document(desktop);    
2955     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2956     Inkscape::Selection *selection = sp_desktop_selection(desktop);
2958     // check if something is selected
2959     if (selection->isEmpty()) {
2960         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to remove clippath or mask from."));
2961         return;
2962     }
2963     
2964     bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
2965     sp_document_ensure_up_to_date(doc);
2967     gchar const *attributeName = apply_clip_path ? "clip-path" : "mask";
2968     std::map<SPObject*,SPItem*> referenced_objects;
2969     for (GSList const *i = selection->itemList(); NULL != i; i = i->next) {
2970         if (remove_original) {
2971             // remember referenced mask/clippath, so orphaned masks can be moved back to document
2972             SPItem *item = reinterpret_cast<SPItem *>(i->data);
2973             Inkscape::URIReference *uri_ref = NULL;
2974         
2975             if (apply_clip_path) {
2976                 uri_ref = item->clip_ref;
2977             } else {
2978                 uri_ref = item->mask_ref;
2979             }
2981             // collect distinct mask object (and associate with item to apply transform)
2982             if (NULL != uri_ref && NULL != uri_ref->getObject()) {
2983                 referenced_objects[uri_ref->getObject()] = item;
2984             }
2985         }
2987         SP_OBJECT_REPR(i->data)->setAttribute(attributeName, "none");
2988     }
2990     // restore mask objects into a document
2991     for ( std::map<SPObject*,SPItem*>::iterator it = referenced_objects.begin() ; it != referenced_objects.end() ; ++it) {
2992         SPObject *obj = (*it).first;
2993         GSList *items_to_move = NULL;
2994         for (SPObject *child = sp_object_first_child(obj) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2995             Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate(xml_doc);
2996             items_to_move = g_slist_prepend (items_to_move, copy);
2997         }
2999         if (!obj->isReferenced()) {
3000             // delete from defs if no other object references this mask
3001             obj->deleteObject(false);
3002         }
3004         // remember parent and position of the item to which the clippath/mask was applied
3005         Inkscape::XML::Node *parent = SP_OBJECT_REPR((*it).second)->parent();
3006         gint pos = SP_OBJECT_REPR((*it).second)->position();
3008         for (GSList *i = items_to_move; NULL != i; i = i->next) {
3009             Inkscape::XML::Node *repr = (Inkscape::XML::Node *)i->data;
3011             // insert into parent, restore pos
3012             parent->appendChild(repr);
3013             repr->setPosition((pos + 1) > 0 ? (pos + 1) : 0);
3015             SPItem *mask_item = (SPItem *) sp_desktop_document (desktop)->getObjectByRepr(repr);
3016             selection->add(repr);
3018             // transform mask, so it is moved the same spot where mask was applied
3019             NR::Matrix transform (mask_item->transform);
3020             transform *= (*it).second->transform;
3021             sp_item_write_transform(mask_item, SP_OBJECT_REPR(mask_item), transform);
3022         }
3024         g_slist_free (items_to_move);
3025     }
3027     if (apply_clip_path) 
3028         sp_document_done (doc, SP_VERB_OBJECT_UNSET_CLIPPATH, _("Release clipping path"));
3029     else 
3030         sp_document_done (doc, SP_VERB_OBJECT_UNSET_MASK, _("Release mask"));
3033 void fit_canvas_to_selection(SPDesktop *desktop) {
3034     g_return_if_fail(desktop != NULL);
3035     SPDocument *doc = sp_desktop_document(desktop);
3037     g_return_if_fail(doc != NULL);
3038     g_return_if_fail(desktop->selection != NULL);
3040     if (desktop->selection->isEmpty()) {
3041         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to fit canvas to."));
3042         return;
3043     }
3044     NR::Maybe<NR::Rect> const bbox(desktop->selection->bounds());
3045     if (bbox && !bbox->isEmpty()) {
3046         doc->fitToRect(*bbox);
3047     }
3048 };
3050 void fit_canvas_to_drawing(SPDocument *doc) {
3051     g_return_if_fail(doc != NULL);
3053     sp_document_ensure_up_to_date(doc);
3054     SPItem const *const root = SP_ITEM(doc->root);
3055     NR::Maybe<NR::Rect> const bbox(root->getBounds(sp_item_i2r_affine(root)));
3056     if (bbox && !bbox->isEmpty()) {
3057         doc->fitToRect(*bbox);
3058     }
3059 };
3061 void fit_canvas_to_selection_or_drawing(SPDesktop *desktop) {
3062     g_return_if_fail(desktop != NULL);
3063     SPDocument *doc = sp_desktop_document(desktop);
3065     g_return_if_fail(doc != NULL);
3066     g_return_if_fail(desktop->selection != NULL);
3068     if (desktop->selection->isEmpty()) {
3069         fit_canvas_to_drawing(doc);
3070     } else {
3071         fit_canvas_to_selection(desktop);
3072     }
3074     sp_document_done(doc, SP_VERB_FIT_CANVAS_TO_DRAWING, 
3075                      _("Fit page to selection"));
3076 };
3078 static void itemtree_map(void (*f)(SPItem *, SPDesktop *), SPObject *root, SPDesktop *desktop) {
3079     // don't operate on layers
3080     if (SP_IS_ITEM(root) && !desktop->isLayer(SP_ITEM(root))) {
3081         f(SP_ITEM(root), desktop);
3082     }
3083     for ( SPObject::SiblingIterator iter = root->firstChild() ; iter ; ++iter ) {
3084         //don't recurse into locked layers
3085         if (!(SP_IS_ITEM(&*iter) && desktop->isLayer(SP_ITEM(&*iter)) && SP_ITEM(&*iter)->isLocked())) {
3086             itemtree_map(f, iter, desktop);
3087         }
3088     }
3091 static void unlock(SPItem *item, SPDesktop *desktop) {
3092     if (item->isLocked()) {
3093         item->setLocked(FALSE);
3094     }
3097 static void unhide(SPItem *item, SPDesktop *desktop) {
3098     if (desktop->itemIsHidden(item)) {
3099         item->setExplicitlyHidden(FALSE);
3100     }
3103 static void process_all(void (*f)(SPItem *, SPDesktop *), SPDesktop *dt, bool layer_only) {
3104     if (!dt) return;
3105         
3106     SPObject *root;
3107     if (layer_only) {
3108         root = dt->currentLayer();
3109     } else {
3110         root = dt->currentRoot();
3111     }
3112     
3113     itemtree_map(f, root, dt);
3116 void unlock_all(SPDesktop *dt) {
3117     process_all(&unlock, dt, true);
3120 void unlock_all_in_all_layers(SPDesktop *dt) {
3121     process_all(&unlock, dt, false);
3124 void unhide_all(SPDesktop *dt) {
3125     process_all(&unhide, dt, true);
3128 void unhide_all_in_all_layers(SPDesktop *dt) {
3129     process_all(&unhide, dt, false);
3133 GSList * sp_selection_get_clipboard() {
3134     return clipboard;
3138 /*
3139   Local Variables:
3140   mode:c++
3141   c-file-style:"stroustrup"
3142   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
3143   indent-tabs-mode:nil
3144   fill-column:99
3145   End:
3146 */
3147 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :