Code

LPE: implement 'edit next LPE parameter'. Accessible through key '7'.
[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, const GSList *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 (const GSList *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(const GSList *items, bool propagate=true, bool propagate_descendants=true)
207     for (const GSList *i = items ; i ; i = i->next ) {
208         sp_object_ref((SPObject *)i->data, NULL);
209     }
210     for (const GSList *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     const GSList *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, const GSList *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     const GSList *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(const GSList *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  */
603 static NR::Maybe<NR::Rect>
604 enclose_items(const GSList *items)
606     g_assert(items != NULL);
608     NR::Maybe<NR::Rect> r = NR::Nothing();
609     for (GSList const *i = items; i; i = i->next) {
610         r = NR::union_bounds(r, sp_item_bbox_desktop((SPItem *) i->data));
611     }
612     return r;
615 SPObject *
616 prev_sibling(SPObject *child)
618     SPObject *parent = SP_OBJECT_PARENT(child);
619     if (!SP_IS_GROUP(parent)) {
620         return NULL;
621     }
622     for ( SPObject *i = sp_object_first_child(parent) ; i; i = SP_OBJECT_NEXT(i) ) {
623         if (i->next == child)
624             return i;
625     }
626     return NULL;
629 void
630 sp_selection_raise()
632     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
633     if (!desktop)
634         return;
636     Inkscape::Selection *selection = sp_desktop_selection(desktop);
638     GSList const *items = (GSList *) selection->itemList();
639     if (!items) {
640         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise."));
641         return;
642     }
644     SPGroup const *group = sp_item_list_common_parent_group(items);
645     if (!group) {
646         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
647         return;
648     }
650     Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
652     /* construct reverse-ordered list of selected children */
653     GSList *rev = g_slist_copy((GSList *) items);
654     rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
656     // find out the common bbox of the selected items
657     NR::Maybe<NR::Rect> selected = enclose_items(items);
659     // for all objects in the selection (starting from top)
660     if (selected) {
661         while (rev) {
662             SPObject *child = SP_OBJECT(rev->data);
663             // for each selected object, find the next sibling
664             for (SPObject *newref = child->next; newref; newref = newref->next) {
665                 // if the sibling is an item AND overlaps our selection,
666                 if (SP_IS_ITEM(newref)) {
667                     NR::Maybe<NR::Rect> newref_bbox = sp_item_bbox_desktop(SP_ITEM(newref));
668                     if ( newref_bbox && selected->intersects(*newref_bbox) ) {
669                         // AND if it's not one of our selected objects,
670                         if (!g_slist_find((GSList *) items, newref)) {
671                             // move the selected object after that sibling
672                             grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(newref));
673                         }
674                         break;
675                     }
676                 }
677             }
678             rev = g_slist_remove(rev, child);
679         }
680     } else {
681         g_slist_free(rev);
682     }
684     sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_RAISE,
685                      _("Raise"));
688 void sp_selection_raise_to_top()
690     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
691     if (desktop == NULL)
692         return;
694     SPDocument *document = sp_desktop_document(desktop);
695     Inkscape::Selection *selection = sp_desktop_selection(desktop);
697     if (selection->isEmpty()) {
698         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise to top."));
699         return;
700     }
702     GSList const *items = (GSList *) selection->itemList();
704     SPGroup const *group = sp_item_list_common_parent_group(items);
705     if (!group) {
706         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
707         return;
708     }
710     GSList *rl = g_slist_copy((GSList *) selection->reprList());
711     rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
713     for (GSList *l = rl; l != NULL; l = l->next) {
714         Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
715         repr->setPosition(-1);
716     }
718     g_slist_free(rl);
720     sp_document_done(document, SP_VERB_SELECTION_TO_FRONT, 
721                      _("Raise to top"));
724 void
725 sp_selection_lower()
727     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
728     if (desktop == NULL)
729         return;
731     Inkscape::Selection *selection = sp_desktop_selection(desktop);
733     GSList const *items = (GSList *) selection->itemList();
734     if (!items) {
735         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower."));
736         return;
737     }
739     SPGroup const *group = sp_item_list_common_parent_group(items);
740     if (!group) {
741         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
742         return;
743     }
745     Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
747     // find out the common bbox of the selected items
748     NR::Maybe<NR::Rect> selected = enclose_items(items);
750     /* construct direct-ordered list of selected children */
751     GSList *rev = g_slist_copy((GSList *) items);
752     rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
753     rev = g_slist_reverse(rev);
755     // for all objects in the selection (starting from top)
756     if (selected) {
757         while (rev) {
758             SPObject *child = SP_OBJECT(rev->data);
759             // for each selected object, find the prev sibling
760             for (SPObject *newref = prev_sibling(child); newref; newref = prev_sibling(newref)) {
761                 // if the sibling is an item AND overlaps our selection,
762                 if (SP_IS_ITEM(newref)) {
763                     NR::Maybe<NR::Rect> ref_bbox = sp_item_bbox_desktop(SP_ITEM(newref));
764                     if ( ref_bbox && selected->intersects(*ref_bbox) ) {
765                         // AND if it's not one of our selected objects,
766                         if (!g_slist_find((GSList *) items, newref)) {
767                             // move the selected object before that sibling
768                             SPObject *put_after = prev_sibling(newref);
769                             if (put_after)
770                                 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(put_after));
771                             else
772                                 SP_OBJECT_REPR(child)->setPosition(0);
773                         }
774                         break;
775                     }
776                 }
777             }
778             rev = g_slist_remove(rev, child);
779         }
780     } else {
781         g_slist_free(rev);
782     }
784     sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_LOWER, 
785                      _("Lower"));
788 void sp_selection_lower_to_bottom()
790     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
791     if (desktop == NULL)
792         return;
794     SPDocument *document = sp_desktop_document(desktop);
795     Inkscape::Selection *selection = sp_desktop_selection(desktop);
797     if (selection->isEmpty()) {
798         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower to bottom."));
799         return;
800     }
802     GSList const *items = (GSList *) selection->itemList();
804     SPGroup const *group = sp_item_list_common_parent_group(items);
805     if (!group) {
806         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
807         return;
808     }
810     GSList *rl;
811     rl = g_slist_copy((GSList *) selection->reprList());
812     rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
813     rl = g_slist_reverse(rl);
815     for (GSList *l = rl; l != NULL; l = l->next) {
816         gint minpos;
817         SPObject *pp, *pc;
818         Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
819         pp = document->getObjectByRepr(sp_repr_parent(repr));
820         minpos = 0;
821         g_assert(SP_IS_GROUP(pp));
822         pc = sp_object_first_child(pp);
823         while (!SP_IS_ITEM(pc)) {
824             minpos += 1;
825             pc = pc->next;
826         }
827         repr->setPosition(minpos);
828     }
830     g_slist_free(rl);
832     sp_document_done(document, SP_VERB_SELECTION_TO_BACK, 
833                      _("Lower to bottom"));
836 void
837 sp_undo(SPDesktop *desktop, SPDocument *)
839         if (!sp_document_undo(sp_desktop_document(desktop)))
840             desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to undo."));
843 void
844 sp_redo(SPDesktop *desktop, SPDocument *)
846         if (!sp_document_redo(sp_desktop_document(desktop)))
847             desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to redo."));
850 void sp_selection_cut()
852     sp_selection_copy();
853     sp_selection_delete();
856 void sp_copy_gradient (GSList **defs_clip, SPGradient *gradient, Inkscape::XML::Document* xml_doc)
858     SPGradient *ref = gradient;
860     while (ref) {
861         // climb up the refs, copying each one in the chain
862         Inkscape::XML::Node *grad_repr = SP_OBJECT_REPR(ref)->duplicate(xml_doc);
863         *defs_clip = g_slist_prepend (*defs_clip, grad_repr);
865         ref = ref->ref->getObject();
866     }
869 void sp_copy_pattern (GSList **defs_clip, SPPattern *pattern, Inkscape::XML::Document* xml_doc)
871     SPPattern *ref = pattern;
873     while (ref) {
874         // climb up the refs, copying each one in the chain
875         Inkscape::XML::Node *pattern_repr = SP_OBJECT_REPR(ref)->duplicate(xml_doc);
876         *defs_clip = g_slist_prepend (*defs_clip, pattern_repr);
878         // items in the pattern may also use gradients and other patterns, so we need to recurse here as well
879         for (SPObject *child = sp_object_first_child(SP_OBJECT(ref)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
880             if (!SP_IS_ITEM (child))
881                 continue;
882             sp_copy_stuff_used_by_item (defs_clip, (SPItem *) child, NULL, xml_doc);
883         }
885         ref = ref->ref->getObject();
886     }
889 void sp_copy_single (GSList **defs_clip, SPObject *thing, Inkscape::XML::Document* xml_doc)
891     Inkscape::XML::Node *duplicate_repr = SP_OBJECT_REPR(thing)->duplicate(xml_doc);
892     *defs_clip = g_slist_prepend (*defs_clip, duplicate_repr);
896 void sp_copy_textpath_path (GSList **defs_clip, SPTextPath *tp, const GSList *items, Inkscape::XML::Document* xml_doc)
898     SPItem *path = sp_textpath_get_path_item (tp);
899     if (!path)
900         return;
901     if (items && g_slist_find ((GSList *) items, path)) // do not copy it to defs if it is already in the list of items copied
902         return;
903     Inkscape::XML::Node *repr = SP_OBJECT_REPR(path)->duplicate(xml_doc);
904     *defs_clip = g_slist_prepend (*defs_clip, repr);
907 /**
908  * Copies things like patterns, markers, gradients, etc.
909  */
910 void sp_copy_stuff_used_by_item (GSList **defs_clip, SPItem *item, const GSList *items, Inkscape::XML::Document* xml_doc)
912     SPStyle *style = SP_OBJECT_STYLE (item);
914     if (style && (style->fill.isPaintserver())) {
915         SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
916         if (SP_IS_LINEARGRADIENT (server) || SP_IS_RADIALGRADIENT (server))
917             sp_copy_gradient (defs_clip, SP_GRADIENT(server), xml_doc);
918         if (SP_IS_PATTERN (server))
919             sp_copy_pattern (defs_clip, SP_PATTERN(server), xml_doc);
920     }
922     if (style && (style->stroke.isPaintserver())) {
923         SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
924         if (SP_IS_LINEARGRADIENT (server) || SP_IS_RADIALGRADIENT (server))
925             sp_copy_gradient (defs_clip, SP_GRADIENT(server), xml_doc);
926         if (SP_IS_PATTERN (server))
927             sp_copy_pattern (defs_clip, SP_PATTERN(server), xml_doc);
928     }
930     // For shapes, copy all of the shape's markers into defs_clip
931     if (SP_IS_SHAPE (item)) {
932         SPShape *shape = SP_SHAPE (item);
933         for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
934             if (shape->marker[i]) {
935                 sp_copy_single (defs_clip, SP_OBJECT (shape->marker[i]), xml_doc);
936             }
937         }
939         // For shapes, also copy liveeffect if applicable
940         if (sp_shape_has_path_effect(shape)) {
941             sp_copy_single (defs_clip, SP_OBJECT(sp_shape_get_livepatheffectobject(shape)), xml_doc);
942         }
943     }
945     if (SP_IS_TEXT_TEXTPATH (item)) {
946         sp_copy_textpath_path (defs_clip, SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))), items, xml_doc);
947     }
949     if (item->clip_ref->getObject()) {
950         sp_copy_single (defs_clip, item->clip_ref->getObject(), xml_doc);
951     }
953     if (item->mask_ref->getObject()) {
954         SPObject *mask = item->mask_ref->getObject();
955         sp_copy_single (defs_clip, mask, xml_doc);
956         // recurse into the mask for its gradients etc.
957         for (SPObject *o = SP_OBJECT(mask)->children; o != NULL; o = o->next) {
958             if (SP_IS_ITEM(o))
959                 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (o), items, xml_doc);
960         }
961     }
963     if (style->getFilter()) {
964         SPObject *filter = style->getFilter();
965         if (SP_IS_FILTER(filter)) {
966             sp_copy_single (defs_clip, filter, xml_doc);
967         }
968     }
970     // recurse
971     for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
972         if (SP_IS_ITEM(o))
973             sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (o), items, xml_doc);
974     }
977 void
978 sp_set_style_clipboard (SPCSSAttr *css)
980     if (css != NULL) {
981         // clear style clipboard
982         if (style_clipboard) {
983             sp_repr_css_attr_unref (style_clipboard);
984             style_clipboard = NULL;
985         }
986         //sp_repr_css_print (css);
987         style_clipboard = css;
988     }
991 /**
992  * \pre item != NULL
993  */
994 SPCSSAttr *
995 take_style_from_item (SPItem *item)
997     // write the complete cascaded style, context-free
998     SPCSSAttr *css = sp_css_attr_from_object (SP_OBJECT(item), SP_STYLE_FLAG_ALWAYS);
999     if (css == NULL)
1000         return NULL;
1002     if ((SP_IS_GROUP(item) && SP_OBJECT(item)->children) ||
1003         (SP_IS_TEXT (item) && SP_OBJECT(item)->children && SP_OBJECT(item)->children->next == NULL)) {
1004         // if this is a text with exactly one tspan child, merge the style of that tspan as well
1005         // If this is a group, merge the style of its topmost (last) child with style
1006         for (SPObject *last_element = item->lastChild(); last_element != NULL; last_element = SP_OBJECT_PREV (last_element)) {
1007             if (SP_OBJECT_STYLE (last_element) != NULL) {
1008                 SPCSSAttr *temp = sp_css_attr_from_object (last_element, SP_STYLE_FLAG_IFSET);
1009                 if (temp) {
1010                     sp_repr_css_merge (css, temp);
1011                     sp_repr_css_attr_unref (temp);
1012                 }
1013                 break;
1014             }
1015         }
1016     }
1017     if (!(SP_IS_TEXT (item) || SP_IS_TSPAN (item) || SP_IS_TREF(item) || SP_IS_STRING (item))) {
1018         // do not copy text properties from non-text objects, it's confusing
1019         css = sp_css_attr_unset_text (css);
1020     }
1022     // FIXME: also transform gradient/pattern fills, by forking? NO, this must be nondestructive
1023     double ex = NR::expansion(sp_item_i2doc_affine(item));
1024     if (ex != 1.0) {
1025         css = sp_css_attr_scale (css, ex);
1026     }
1028     return css;
1032 void sp_selection_copy()
1034     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1035     if (desktop == NULL)
1036         return;
1038     if (!clipboard_document) {
1039         clipboard_document = new Inkscape::XML::SimpleDocument();
1040     }
1042     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1044     if (tools_isactive (desktop, TOOLS_DROPPER)) {
1045         sp_dropper_context_copy(desktop->event_context);
1046         return; // copied color under cursor, nothing else to do
1047     }
1049     if (desktop->event_context->get_drag() && desktop->event_context->get_drag()->copy()) {
1050         return; // copied selected stop(s), nothing else to do
1051     }
1053     // check if something is selected
1054     if (selection->isEmpty()) {
1055         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing was copied."));
1056         return;
1057     }
1059     const GSList *items = g_slist_copy ((GSList *) selection->itemList());
1061     // 0. Copy text to system clipboard
1062     // FIXME: for non-texts, put serialized Inkscape::XML as text to the clipboard;
1063     //for this sp_repr_write_stream needs to be rewritten with iostream instead of FILE
1064     Glib::ustring text;
1065     if (tools_isactive (desktop, TOOLS_TEXT)) {
1066         text = sp_text_get_selected_text(desktop->event_context);
1067     }
1069     if (text.empty()) {
1070         guint texts = 0;
1071         for (GSList *i = (GSList *) items; i; i = i->next) {
1072             SPItem *item = SP_ITEM (i->data);
1073             if (SP_IS_TEXT (item) || SP_IS_FLOWTEXT(item)) {
1074                 if (texts > 0) // if more than one text object is copied, separate them by spaces
1075                     text += " ";
1076                 gchar *this_text = sp_te_get_string_multiline (item);
1077                 if (this_text) {
1078                     text += this_text;
1079                     g_free(this_text);
1080                 }
1081                 texts++;
1082             }
1083         }
1084     }
1085     if (!text.empty()) {
1086         Glib::RefPtr<Gtk::Clipboard> refClipboard = Gtk::Clipboard::get();
1087         refClipboard->set_text(text);
1088     }
1090     // clear old defs clipboard
1091     while (defs_clipboard) {
1092         Inkscape::GC::release((Inkscape::XML::Node *) defs_clipboard->data);
1093         defs_clipboard = g_slist_remove (defs_clipboard, defs_clipboard->data);
1094     }
1096     // clear style clipboard
1097     if (style_clipboard) {
1098         sp_repr_css_attr_unref (style_clipboard);
1099         style_clipboard = NULL;
1100     }
1102     //clear main clipboard
1103     while (clipboard) {
1104         Inkscape::GC::release((Inkscape::XML::Node *) clipboard->data);
1105         clipboard = g_slist_remove(clipboard, clipboard->data);
1106     }
1108     sp_selection_copy_impl (items, &clipboard, &defs_clipboard, &style_clipboard, clipboard_document);
1110     if (tools_isactive (desktop, TOOLS_TEXT)) { // take style from cursor/text selection, overwriting the style just set by copy_impl
1111         SPStyle *const query = sp_style_new(SP_ACTIVE_DOCUMENT);
1112         if (sp_desktop_query_style_all (desktop, query)) {
1113             SPCSSAttr *css = sp_css_attr_from_style (query, SP_STYLE_FLAG_ALWAYS);
1114             sp_set_style_clipboard (css);
1115         }
1116         sp_style_unref(query);
1117     }
1119     size_clipboard = selection->bounds();
1121     g_slist_free ((GSList *) items);
1124 void sp_selection_paste(bool in_place)
1126     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1128     if (desktop == NULL) {
1129         return;
1130     }
1132     SPDocument *document = sp_desktop_document(desktop);
1134     if (Inkscape::have_viable_layer(desktop, desktop->messageStack()) == false) {
1135         return;
1136     }
1138     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1140     if (tools_isactive (desktop, TOOLS_TEXT)) {
1141         if (sp_text_paste_inline(desktop->event_context))
1142             return; // pasted from system clipboard into text, nothing else to do
1143     }
1145     // check if something is in the clipboard
1146     if (clipboard == NULL) {
1147         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1148         return;
1149     }
1151     GSList *copied = sp_selection_paste_impl(document, desktop->currentLayer(), &clipboard, &defs_clipboard);
1152     // add pasted objects to selection
1153     selection->setReprList((GSList const *) copied);
1154     g_slist_free (copied);
1156     if (!in_place) {
1157         sp_document_ensure_up_to_date(document);
1159         NR::Maybe<NR::Rect> sel_bbox = selection->bounds();
1160         NR::Point m( desktop->point() );
1161         if (sel_bbox) {
1162             m -= sel_bbox->midpoint();
1163         }
1165         /* Snap the offset of the new item(s) to the grid */
1166         SnapManager &sm = desktop->namedview->snap_manager;
1167         SnapManager::SnapperList gs = sm.getGridSnappers();
1168         m = sm.freeSnapAlways(Inkscape::Snapper::SNAPPOINT_NODE, m, NULL, gs).getPoint();
1169         sp_selection_move_relative(selection, m);
1170     }
1172     sp_document_done(document, SP_VERB_EDIT_PASTE, 
1173                      _("Paste"));
1176 void sp_selection_paste_style()
1178     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1179     if (desktop == NULL) return;
1181     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1183     // check if something is in the clipboard
1184     if (style_clipboard == NULL) {
1185         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the style clipboard."));
1186         return;
1187     }
1189     // check if something is selected
1190     if (selection->isEmpty()) {
1191         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste style to."));
1192         return;
1193     }
1195     paste_defs (&defs_clipboard, sp_desktop_document(desktop));
1197     sp_desktop_set_style (desktop, style_clipboard);
1199     sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_STYLE,
1200                      _("Paste style"));
1203 void sp_selection_paste_livepatheffect()
1205     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1206     if (desktop == NULL) return;
1208     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1210     // check if something is in the clipboard
1211     if (clipboard == NULL) {
1212         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1213         return;
1214     }
1216     // check if something is selected
1217     if (selection->isEmpty()) {
1218         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste live path effect to."));
1219         return;
1220     }
1222     paste_defs (&defs_clipboard, sp_desktop_document(desktop));
1224     Inkscape::XML::Node *repr = (Inkscape::XML::Node *) clipboard->data;
1225     const char * effectstr = repr->attribute("inkscape:path-effect");
1226     if (!effectstr) {
1227         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Clipboard does not contain a live path effect."));
1228         return;
1229     }
1231     for ( GSList const *itemlist = selection->itemList(); itemlist != NULL; itemlist = g_slist_next(itemlist) ) {
1232         SPItem *item = reinterpret_cast<SPItem*>(itemlist->data);
1233         if ( item && SP_IS_SHAPE(item) ) {
1234             Inkscape::XML::Node *selrepr = (Inkscape::XML::Node *) SP_OBJECT_REPR(item);
1235             selrepr->setAttribute("inkscape:path-effect", effectstr);
1237             // set inkscape:original-d for paths. the other shapes don't need this.
1238             if ( SP_IS_PATH(item) ) {
1239                 Inkscape::XML::Node *pathrepr = SP_OBJECT_REPR(item);
1240                 if ( ! pathrepr->attribute("inkscape:original-d") ) {
1241                     pathrepr->setAttribute("inkscape:original-d", pathrepr->attribute("d"));
1242                 }
1243             }
1244         }
1245     }
1247     sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_LIVEPATHEFFECT,
1248                      _("Paste live path effect"));
1251 void sp_selection_paste_size (bool apply_x, bool apply_y)
1253     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1254     if (desktop == NULL) return;
1256     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1258     // check if something is in the clipboard
1259     if (!size_clipboard) {
1260         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1261         return;
1262     }
1264     // check if something is selected
1265     if (selection->isEmpty()) {
1266         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste size to."));
1267         return;
1268     }
1270     NR::Maybe<NR::Rect> current = selection->bounds();
1271     if ( !current || current->isEmpty() ) {
1272         return;
1273     }
1275     double scale_x = size_clipboard->extent(NR::X) / current->extent(NR::X);
1276     double scale_y = size_clipboard->extent(NR::Y) / current->extent(NR::Y);
1278     sp_selection_scale_relative (selection, current->midpoint(),
1279                                  NR::scale(
1280                                      apply_x? scale_x : (desktop->isToolboxButtonActive ("lock")? scale_y : 1.0),
1281                                      apply_y? scale_y : (desktop->isToolboxButtonActive ("lock")? scale_x : 1.0)));
1283     sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_SIZE,
1284                      _("Paste size"));
1287 void sp_selection_paste_size_separately (bool apply_x, bool apply_y)
1289     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1290     if (desktop == NULL) return;
1292     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1294     // check if something is in the clipboard
1295     if ( !size_clipboard ) {
1296         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1297         return;
1298     }
1300     // check if something is selected
1301     if (selection->isEmpty()) {
1302         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste size to."));
1303         return;
1304     }
1306     for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1307         SPItem *item = SP_ITEM(l->data);
1309         NR::Maybe<NR::Rect> current = sp_item_bbox_desktop(item);
1310         if ( !current || current->isEmpty() ) {
1311             continue;
1312         }
1314         double scale_x = size_clipboard->extent(NR::X) / current->extent(NR::X);
1315         double scale_y = size_clipboard->extent(NR::Y) / current->extent(NR::Y);
1317         sp_item_scale_rel (item,
1318                                  NR::scale(
1319                                      apply_x? scale_x : (desktop->isToolboxButtonActive ("lock")? scale_y : 1.0),
1320                                      apply_y? scale_y : (desktop->isToolboxButtonActive ("lock")? scale_x : 1.0)));
1322     }
1324     sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_SIZE_SEPARATELY,
1325                      _("Paste size separately"));
1328 void sp_selection_to_next_layer ()
1330     SPDesktop *dt = SP_ACTIVE_DESKTOP;
1332     Inkscape::Selection *selection = sp_desktop_selection(dt);
1334     // check if something is selected
1335     if (selection->isEmpty()) {
1336         dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer above."));
1337         return;
1338     }
1340     const GSList *items = g_slist_copy ((GSList *) selection->itemList());
1342     bool no_more = false; // Set to true, if no more layers above
1343     SPObject *next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer());
1344     if (next) {
1345         GSList *temp_clip = NULL;
1346         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
1347         sp_selection_delete_impl (items, false, false);
1348         next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
1349         GSList *copied;
1350         if(next) {
1351             copied = sp_selection_paste_impl (sp_desktop_document (dt), next, &temp_clip, NULL);
1352         } else {
1353             copied = sp_selection_paste_impl (sp_desktop_document (dt), dt->currentLayer(), &temp_clip, NULL);
1354             no_more = true;
1355         }
1356         selection->setReprList((GSList const *) copied);
1357         g_slist_free (copied);
1358         if (temp_clip) g_slist_free (temp_clip);
1359         if (next) dt->setCurrentLayer(next);
1360         sp_document_done(sp_desktop_document (dt), SP_VERB_LAYER_MOVE_TO_NEXT, 
1361                          _("Raise to next layer"));
1362     } else {
1363         no_more = true;
1364     }
1366     if (no_more) {
1367         dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers above."));
1368     }
1370     g_slist_free ((GSList *) items);
1373 void sp_selection_to_prev_layer ()
1375     SPDesktop *dt = SP_ACTIVE_DESKTOP;
1377     Inkscape::Selection *selection = sp_desktop_selection(dt);
1379     // check if something is selected
1380     if (selection->isEmpty()) {
1381         dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer below."));
1382         return;
1383     }
1385     const GSList *items = g_slist_copy ((GSList *) selection->itemList());
1387     bool no_more = false; // Set to true, if no more layers below
1388     SPObject *next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer());
1389     if (next) {
1390         GSList *temp_clip = NULL;
1391         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
1392         sp_selection_delete_impl (items, false, false);
1393         next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
1394         GSList *copied;
1395         if(next) {
1396             copied = sp_selection_paste_impl (sp_desktop_document (dt), next, &temp_clip, NULL);
1397         } else {
1398             copied = sp_selection_paste_impl (sp_desktop_document (dt), dt->currentLayer(), &temp_clip, NULL);
1399             no_more = true;
1400         }
1401         selection->setReprList((GSList const *) copied);
1402         g_slist_free (copied);
1403         if (temp_clip) g_slist_free (temp_clip);
1404         if (next) dt->setCurrentLayer(next);
1405         sp_document_done(sp_desktop_document (dt), SP_VERB_LAYER_MOVE_TO_PREV,
1406                          _("Lower to previous layer"));
1407     } else {
1408         no_more = true;
1409     }
1411     if (no_more) {
1412         dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers below."));
1413     }
1415     g_slist_free ((GSList *) items);
1418 bool
1419 selection_contains_original (SPItem *item, Inkscape::Selection *selection)
1421     bool contains_original = false;
1422     
1423     bool is_use = SP_IS_USE(item);
1424     SPItem *item_use = item;
1425     SPItem *item_use_first = item;
1426     while (is_use && item_use && !contains_original)
1427     {
1428         item_use = sp_use_get_original (SP_USE(item_use));
1429         contains_original |= selection->includes(item_use);
1430         if (item_use == item_use_first)
1431             break;
1432         is_use = SP_IS_USE(item_use);
1433     }
1434     
1435     // If it's a tref, check whether the object containing the character
1436     // data is part of the selection
1437     if (!contains_original && SP_IS_TREF(item)) {
1438         contains_original = selection->includes(SP_TREF(item)->getObjectReferredTo());
1439     }
1440        
1441     return contains_original;
1445 bool
1446 selection_contains_both_clone_and_original (Inkscape::Selection *selection)
1448     bool clone_with_original = false;
1449     for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1450         SPItem *item = SP_ITEM(l->data);
1451         clone_with_original |= selection_contains_original(item, selection);
1452         if (clone_with_original) 
1453             break;
1454     }
1455     return clone_with_original;
1459 /** Apply matrix to the selection.  \a set_i2d is normally true, which means objects are in the
1460 original transform, synced with their reprs, and need to jump to the new transform in one go. A
1461 value of set_i2d==false is only used by seltrans when it's dragging objects live (not outlines); in
1462 that case, items are already in the new position, but the repr is in the old, and this function
1463 then simply updates the repr from item->transform.
1464  */
1465 void sp_selection_apply_affine(Inkscape::Selection *selection, NR::Matrix const &affine, bool set_i2d)
1467     if (selection->isEmpty())
1468         return;
1470     for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1471         SPItem *item = SP_ITEM(l->data);
1473         NR::Point old_center(0,0);
1474         if (set_i2d && item->isCenterSet())
1475             old_center = item->getCenter();
1477 #if 0 /* Re-enable this once persistent guides have a graphical indication.
1478          At the time of writing, this is the only place to re-enable. */
1479         sp_item_update_cns(*item, selection->desktop());
1480 #endif
1482         // we're moving both a clone and its original or any ancestor in clone chain?
1483         bool transform_clone_with_original = selection_contains_original(item, selection);
1484         // ...both a text-on-path and its path?
1485         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)))) ));
1486         // ...both a flowtext and its frame?
1487         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)
1488         // ...both an offset and its source?
1489         bool transform_offset_with_source = (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) && selection->includes( sp_offset_get_source (SP_OFFSET(item)) );
1490        
1491         // If we're moving a connector, we want to detach it
1492         // from shapes that aren't part of the selection, but
1493         // leave it attached if they are
1494         if (cc_item_is_connector(item)) {
1495             SPItem *attItem[2];
1496             SP_PATH(item)->connEndPair.getAttachedItems(attItem);
1497             
1498             for (int n = 0; n < 2; ++n) {
1499                 if (!selection->includes(attItem[n])) {
1500                     sp_conn_end_detach(item, n);
1501                 }
1502             }
1503         }
1504         
1505         // "clones are unmoved when original is moved" preference
1506         int compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
1507         bool prefs_unmoved = (compensation == SP_CLONE_COMPENSATION_UNMOVED);
1508         bool prefs_parallel = (compensation == SP_CLONE_COMPENSATION_PARALLEL);
1510         // If this is a clone and it's selected along with its original, do not move it; it will feel the
1511         // transform of its original and respond to it itself. Without this, a clone is doubly
1512         // transformed, very unintuitive.
1513       // Same for textpath if we are also doing ANY transform to its path: do not touch textpath,
1514       // letters cannot be squeezed or rotated anyway, they only refill the changed path.
1515       // Same for linked offset if we are also moving its source: do not move it.
1516         if (transform_textpath_with_path || transform_offset_with_source) {
1517                 // restore item->transform field from the repr, in case it was changed by seltrans
1518             sp_object_read_attr (SP_OBJECT (item), "transform");
1520         } else if (transform_flowtext_with_frame) {
1521             // apply the inverse of the region's transform to the <use> so that the flow remains
1522             // the same (even though the output itself gets transformed)
1523             for (SPObject *region = item->firstChild() ; region ; region = SP_OBJECT_NEXT(region)) {
1524                 if (!SP_IS_FLOWREGION(region) && !SP_IS_FLOWREGIONEXCLUDE(region))
1525                     continue;
1526                 for (SPObject *use = region->firstChild() ; use ; use = SP_OBJECT_NEXT(use)) {
1527                     if (!SP_IS_USE(use)) continue;
1528                     sp_item_write_transform(SP_USE(use), SP_OBJECT_REPR(use), item->transform.inverse(), NULL);
1529                 }
1530             }
1531         } else if (transform_clone_with_original) {
1532             // We are transforming a clone along with its original. The below matrix juggling is
1533             // necessary to ensure that they transform as a whole, i.e. the clone's induced
1534             // transform and its move compensation are both cancelled out.
1536             // restore item->transform field from the repr, in case it was changed by seltrans
1537             sp_object_read_attr (SP_OBJECT (item), "transform");
1539             // calculate the matrix we need to apply to the clone to cancel its induced transform from its original
1540             NR::Matrix parent_transform = sp_item_i2root_affine(SP_ITEM(SP_OBJECT_PARENT (item)));
1541             NR::Matrix t = parent_transform * matrix_to_desktop (matrix_from_desktop (affine, item), item) * parent_transform.inverse();
1542             NR::Matrix t_inv =parent_transform * matrix_to_desktop (matrix_from_desktop (affine.inverse(), item), item) * parent_transform.inverse();
1543             NR::Matrix result = t_inv * item->transform * t;
1545             if ((prefs_parallel || prefs_unmoved) && affine.is_translation()) {
1546                 // we need to cancel out the move compensation, too
1548                 // find out the clone move, same as in sp_use_move_compensate
1549                 NR::Matrix parent = sp_use_get_parent_transform (SP_USE(item));
1550                 NR::Matrix clone_move = parent.inverse() * t * parent;
1552                 if (prefs_parallel) {
1553                     NR::Matrix move = result * clone_move * t_inv;
1554                     sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &move);
1556                 } else if (prefs_unmoved) {
1557                     //if (SP_IS_USE(sp_use_get_original(SP_USE(item))))
1558                     //    clone_move = NR::identity();
1559                     NR::Matrix move = result * clone_move;
1560                     sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &t);
1561                 }
1563             } else {
1564                 // just apply the result
1565                 sp_item_write_transform(item, SP_OBJECT_REPR(item), result, &t);
1566             }
1568         } else {
1569             if (set_i2d) {
1570                 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * affine);
1571             }
1572             sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, NULL);
1573         }
1575         // if we're moving the actual object, not just updating the repr, we can transform the
1576         // center by the same matrix (only necessary for non-translations)
1577         if (set_i2d && item->isCenterSet() && !affine.is_translation()) {
1578             item->setCenter(old_center * affine);
1579             SP_OBJECT(item)->updateRepr();
1580         }
1581     }
1584 void sp_selection_remove_transform()
1586     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1587     if (desktop == NULL)
1588         return;
1590     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1592     GSList const *l = (GSList *) selection->reprList();
1593     while (l != NULL) {
1594         sp_repr_set_attr((Inkscape::XML::Node*)l->data, "transform", NULL);
1595         l = l->next;
1596     }
1598     sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_FLATTEN, 
1599                      _("Remove transform"));
1602 void
1603 sp_selection_scale_absolute(Inkscape::Selection *selection,
1604                             double const x0, double const x1,
1605                             double const y0, double const y1)
1607     if (selection->isEmpty())
1608         return;
1610     NR::Maybe<NR::Rect> const bbox(selection->bounds());
1611     if ( !bbox || bbox->isEmpty() ) {
1612         return;
1613     }
1615     NR::translate const p2o(-bbox->min());
1617     NR::scale const newSize(x1 - x0,
1618                             y1 - y0);
1619     NR::scale const scale( newSize / NR::scale(bbox->dimensions()) );
1620     NR::translate const o2n(x0, y0);
1621     NR::Matrix const final( p2o * scale * o2n );
1623     sp_selection_apply_affine(selection, final);
1627 void sp_selection_scale_relative(Inkscape::Selection *selection, NR::Point const &align, NR::scale const &scale)
1629     if (selection->isEmpty())
1630         return;
1632     NR::Maybe<NR::Rect> const bbox(selection->bounds());
1634     if ( !bbox || bbox->isEmpty() ) {
1635         return;
1636     }
1638     // FIXME: ARBITRARY LIMIT: don't try to scale above 1 Mpx, it won't display properly and will crash sooner or later anyway
1639     if ( bbox->extent(NR::X) * scale[NR::X] > 1e6  ||
1640          bbox->extent(NR::Y) * scale[NR::Y] > 1e6 )
1641     {
1642         return;
1643     }
1645     NR::translate const n2d(-align);
1646     NR::translate const d2n(align);
1647     NR::Matrix const final( n2d * scale * d2n );
1648     sp_selection_apply_affine(selection, final);
1651 void
1652 sp_selection_rotate_relative(Inkscape::Selection *selection, NR::Point const &center, gdouble const angle_degrees)
1654     NR::translate const d2n(center);
1655     NR::translate const n2d(-center);
1656     NR::rotate const rotate(rotate_degrees(angle_degrees));
1657     NR::Matrix const final( NR::Matrix(n2d) * rotate * d2n );
1658     sp_selection_apply_affine(selection, final);
1661 void
1662 sp_selection_skew_relative(Inkscape::Selection *selection, NR::Point const &align, double dx, double dy)
1664     NR::translate const d2n(align);
1665     NR::translate const n2d(-align);
1666     NR::Matrix const skew(1, dy,
1667                           dx, 1,
1668                           0, 0);
1669     NR::Matrix const final( n2d * skew * d2n );
1670     sp_selection_apply_affine(selection, final);
1673 void sp_selection_move_relative(Inkscape::Selection *selection, NR::Point const &move)
1675     sp_selection_apply_affine(selection, NR::Matrix(NR::translate(move)));
1678 void sp_selection_move_relative(Inkscape::Selection *selection, double dx, double dy)
1680     sp_selection_apply_affine(selection, NR::Matrix(NR::translate(dx, dy)));
1684 /**
1685  * \brief sp_selection_rotate_90
1686  *
1687  * This function rotates selected objects 90 degrees clockwise.
1688  *
1689  */
1691 void sp_selection_rotate_90_cw()
1693     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1695     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1697     if (selection->isEmpty())
1698         return;
1700     GSList const *l = selection->itemList();
1701     NR::rotate const rot_neg_90(NR::Point(0, -1));
1702     for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1703         SPItem *item = SP_ITEM(l2->data);
1704         sp_item_rotate_rel(item, rot_neg_90);
1705     }
1707     sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_ROTATE_90_CCW, 
1708                      _("Rotate 90&#176; CW"));
1712 /**
1713  * \brief sp_selection_rotate_90_ccw
1714  *
1715  * This function rotates selected objects 90 degrees counter-clockwise.
1716  *
1717  */
1719 void sp_selection_rotate_90_ccw()
1721     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1723     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1725     if (selection->isEmpty())
1726         return;
1728     GSList const *l = selection->itemList();
1729     NR::rotate const rot_neg_90(NR::Point(0, 1));
1730     for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1731         SPItem *item = SP_ITEM(l2->data);
1732         sp_item_rotate_rel(item, rot_neg_90);
1733     }
1735     sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_ROTATE_90_CW,
1736                      _("Rotate 90&#176; CCW"));
1739 void
1740 sp_selection_rotate(Inkscape::Selection *selection, gdouble const angle_degrees)
1742     if (selection->isEmpty())
1743         return;
1745     NR::Maybe<NR::Point> center = selection->center();
1746     if (!center) {
1747         return;
1748     }
1750     sp_selection_rotate_relative(selection, *center, angle_degrees);
1752     sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1753                            ( ( angle_degrees > 0 )
1754                              ? "selector:rotate:ccw"
1755                              : "selector:rotate:cw" ), 
1756                            SP_VERB_CONTEXT_SELECT, 
1757                            _("Rotate"));
1760 /**
1761 \param  angle   the angle in "angular pixels", i.e. how many visible pixels must move the outermost point of the rotated object
1762 */
1763 void
1764 sp_selection_rotate_screen(Inkscape::Selection *selection, gdouble angle)
1766     if (selection->isEmpty())
1767         return;
1769     NR::Maybe<NR::Rect> const bbox(selection->bounds());
1770     NR::Maybe<NR::Point> center = selection->center();
1772     if ( !bbox || !center ) {
1773         return;
1774     }
1776     gdouble const zoom = selection->desktop()->current_zoom();
1777     gdouble const zmove = angle / zoom;
1778     gdouble const r = NR::L2(bbox->cornerFarthestFrom(*center) - *center);
1780     gdouble const zangle = 180 * atan2(zmove, r) / M_PI;
1782     sp_selection_rotate_relative(selection, *center, zangle);
1784     sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1785                            ( (angle > 0)
1786                              ? "selector:rotate:ccw"
1787                              : "selector:rotate:cw" ),
1788                            SP_VERB_CONTEXT_SELECT, 
1789                            _("Rotate by pixels"));
1792 void
1793 sp_selection_scale(Inkscape::Selection *selection, gdouble grow)
1795     if (selection->isEmpty())
1796         return;
1798     NR::Maybe<NR::Rect> const bbox(selection->bounds());
1799     if (!bbox) {
1800         return;
1801     }
1803     NR::Point const center(bbox->midpoint());
1805     // you can't scale "do nizhe pola" (below zero)
1806     double const max_len = bbox->maxExtent();
1807     if ( max_len + grow <= 1e-3 ) {
1808         return;
1809     }
1811     double const times = 1.0 + grow / max_len;
1812     sp_selection_scale_relative(selection, center, NR::scale(times, times));
1814     sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1815                            ( (grow > 0)
1816                              ? "selector:scale:larger"
1817                              : "selector:scale:smaller" ),
1818                            SP_VERB_CONTEXT_SELECT,
1819                            _("Scale"));
1822 void
1823 sp_selection_scale_screen(Inkscape::Selection *selection, gdouble grow_pixels)
1825     sp_selection_scale(selection,
1826                        grow_pixels / selection->desktop()->current_zoom());
1829 void
1830 sp_selection_scale_times(Inkscape::Selection *selection, gdouble times)
1832     if (selection->isEmpty())
1833         return;
1835     NR::Maybe<NR::Rect> sel_bbox = selection->bounds();
1837     if (!sel_bbox) {
1838         return;
1839     }
1841     NR::Point const center(sel_bbox->midpoint());
1842     sp_selection_scale_relative(selection, center, NR::scale(times, times));
1843     sp_document_done(sp_desktop_document(selection->desktop()), SP_VERB_CONTEXT_SELECT, 
1844                      _("Scale by whole factor"));
1847 void
1848 sp_selection_move(gdouble dx, gdouble dy)
1850     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1851     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1852     if (selection->isEmpty()) {
1853         return;
1854     }
1856     sp_selection_move_relative(selection, dx, dy);
1858     if (dx == 0) {
1859         sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical", SP_VERB_CONTEXT_SELECT, 
1860                                _("Move vertically"));
1861     } else if (dy == 0) {
1862         sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal", SP_VERB_CONTEXT_SELECT, 
1863                                _("Move horizontally"));
1864     } else {
1865         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SELECT, 
1866                          _("Move"));
1867     }
1870 void
1871 sp_selection_move_screen(gdouble dx, gdouble dy)
1873     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1875     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1876     if (selection->isEmpty()) {
1877         return;
1878     }
1880     // same as sp_selection_move but divide deltas by zoom factor
1881     gdouble const zoom = desktop->current_zoom();
1882     gdouble const zdx = dx / zoom;
1883     gdouble const zdy = dy / zoom;
1884     sp_selection_move_relative(selection, zdx, zdy);
1886     if (dx == 0) {
1887         sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical", SP_VERB_CONTEXT_SELECT, 
1888                                _("Move vertically by pixels"));
1889     } else if (dy == 0) {
1890         sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal", SP_VERB_CONTEXT_SELECT, 
1891                                _("Move horizontally by pixels"));
1892     } else {
1893         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SELECT, 
1894                          _("Move"));
1895     }
1898 namespace {
1900 template <typename D>
1901 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
1902                   bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
1904 template <typename D>
1905 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items, SPObject *root,
1906                   bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
1908 struct Forward {
1909     typedef SPObject *Iterator;
1911     static Iterator children(SPObject *o) { return sp_object_first_child(o); }
1912     static Iterator siblings_after(SPObject *o) { return SP_OBJECT_NEXT(o); }
1913     static void dispose(Iterator i) {}
1915     static SPObject *object(Iterator i) { return i; }
1916     static Iterator next(Iterator i) { return SP_OBJECT_NEXT(i); }
1917 };
1919 struct Reverse {
1920     typedef GSList *Iterator;
1922     static Iterator children(SPObject *o) {
1923         return make_list(o->firstChild(), NULL);
1924     }
1925     static Iterator siblings_after(SPObject *o) {
1926         return make_list(SP_OBJECT_PARENT(o)->firstChild(), o);
1927     }
1928     static void dispose(Iterator i) {
1929         g_slist_free(i);
1930     }
1932     static SPObject *object(Iterator i) {
1933         return reinterpret_cast<SPObject *>(i->data);
1934     }
1935     static Iterator next(Iterator i) { return i->next; }
1937 private:
1938     static GSList *make_list(SPObject *object, SPObject *limit) {
1939         GSList *list=NULL;
1940         while ( object != limit ) {
1941             list = g_slist_prepend(list, object);
1942             object = SP_OBJECT_NEXT(object);
1943         }
1944         return list;
1945     }
1946 };
1950 void
1951 sp_selection_item_next(void)
1953     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1954     g_return_if_fail(desktop != NULL);
1955     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1957     PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
1958     bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
1959     bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
1961     SPObject *root;
1962     if (PREFS_SELECTION_ALL != inlayer) {
1963         root = selection->activeContext();
1964     } else {
1965         root = desktop->currentRoot();
1966     }
1968     SPItem *item=next_item_from_list<Forward>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1970     if (item) {
1971         selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
1972         if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1973             scroll_to_show_item(desktop, item);
1974         }
1975     }
1978 void
1979 sp_selection_item_prev(void)
1981     SPDocument *document = SP_ACTIVE_DOCUMENT;
1982     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1983     g_return_if_fail(document != NULL);
1984     g_return_if_fail(desktop != NULL);
1985     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1987     PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
1988     bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
1989     bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
1991     SPObject *root;
1992     if (PREFS_SELECTION_ALL != inlayer) {
1993         root = selection->activeContext();
1994     } else {
1995         root = desktop->currentRoot();
1996     }
1998     SPItem *item=next_item_from_list<Reverse>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
2000     if (item) {
2001         selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
2002         if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
2003             scroll_to_show_item(desktop, item);
2004         }
2005     }
2008 void sp_selection_next_patheffect_param(SPDesktop * dt)
2010     if (!dt) return;
2012     Inkscape::Selection *selection = sp_desktop_selection(dt);
2013     if ( selection && !selection->isEmpty() ) {
2014         SPItem *item = selection->singleItem();
2015         if ( item && SP_IS_SHAPE(item)) {
2016             SPShape *shape = SP_SHAPE(item);
2017             if (sp_shape_has_path_effect(shape)) {
2018                 sp_shape_edit_next_param_oncanvas(shape, dt);
2019             } else {
2020                 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("The selection has no applied path effect."));
2021             }
2022         }
2023     }
2026 namespace {
2028 template <typename D>
2029 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items,
2030                             SPObject *root, bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
2032     SPObject *current=root;
2033     while (items) {
2034         SPItem *item=SP_ITEM(items->data);
2035         if ( root->isAncestorOf(item) &&
2036              ( !only_in_viewport || desktop->isWithinViewport(item) ) )
2037         {
2038             current = item;
2039             break;
2040         }
2041         items = items->next;
2042     }
2044     GSList *path=NULL;
2045     while ( current != root ) {
2046         path = g_slist_prepend(path, current);
2047         current = SP_OBJECT_PARENT(current);
2048     }
2050     SPItem *next;
2051     // first, try from the current object
2052     next = next_item<D>(desktop, path, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
2053     g_slist_free(path);
2055     if (!next) { // if we ran out of objects, start over at the root
2056         next = next_item<D>(desktop, NULL, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
2057     }
2059     return next;
2062 template <typename D>
2063 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
2064                   bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
2066     typename D::Iterator children;
2067     typename D::Iterator iter;
2069     SPItem *found=NULL;
2071     if (path) {
2072         SPObject *object=reinterpret_cast<SPObject *>(path->data);
2073         g_assert(SP_OBJECT_PARENT(object) == root);
2074         if (desktop->isLayer(object)) {
2075             found = next_item<D>(desktop, path->next, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
2076         }
2077         iter = children = D::siblings_after(object);
2078     } else {
2079         iter = children = D::children(root);
2080     }
2082     while ( iter && !found ) {
2083         SPObject *object=D::object(iter);
2084         if (desktop->isLayer(object)) {
2085             if (PREFS_SELECTION_LAYER != inlayer) { // recurse into sublayers
2086                 found = next_item<D>(desktop, NULL, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
2087             }
2088         } else if ( SP_IS_ITEM(object) &&
2089                     ( !only_in_viewport || desktop->isWithinViewport(SP_ITEM(object)) ) &&
2090                     ( !onlyvisible || !desktop->itemIsHidden(SP_ITEM(object))) &&
2091                     ( !onlysensitive || !SP_ITEM(object)->isLocked()) &&
2092                     !desktop->isLayer(SP_ITEM(object)) )
2093         {
2094             found = SP_ITEM(object);
2095         }
2096         iter = D::next(iter);
2097     }
2099     D::dispose(children);
2101     return found;
2106 /**
2107  * If \a item is not entirely visible then adjust visible area to centre on the centre on of
2108  * \a item.
2109  */
2110 void scroll_to_show_item(SPDesktop *desktop, SPItem *item)
2112     NR::Rect dbox = desktop->get_display_area();
2113     NR::Maybe<NR::Rect> sbox = sp_item_bbox_desktop(item);
2115     if ( sbox && dbox.contains(*sbox) == false ) {
2116         NR::Point const s_dt = sbox->midpoint();
2117         NR::Point const s_w = desktop->d2w(s_dt);
2118         NR::Point const d_dt = dbox.midpoint();
2119         NR::Point const d_w = desktop->d2w(d_dt);
2120         NR::Point const moved_w( d_w - s_w );
2121         gint const dx = (gint) moved_w[X];
2122         gint const dy = (gint) moved_w[Y];
2123         desktop->scroll_world(dx, dy);
2124     }
2128 void
2129 sp_selection_clone()
2131     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2132     if (desktop == NULL)
2133         return;
2135     Inkscape::Selection *selection = sp_desktop_selection(desktop);
2137     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
2139     // check if something is selected
2140     if (selection->isEmpty()) {
2141         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object</b> to clone."));
2142         return;
2143     }
2145     GSList *reprs = g_slist_copy((GSList *) selection->reprList());
2146   
2147     selection->clear();
2148   
2149     // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
2150     reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
2152     GSList *newsel = NULL;
2153  
2154     while (reprs) {
2155         Inkscape::XML::Node *sel_repr = (Inkscape::XML::Node *) reprs->data;
2156         Inkscape::XML::Node *parent = sp_repr_parent(sel_repr);
2158         Inkscape::XML::Node *clone = xml_doc->createElement("svg:use");
2159         sp_repr_set_attr(clone, "x", "0");
2160         sp_repr_set_attr(clone, "y", "0");
2161         sp_repr_set_attr(clone, "xlink:href", g_strdup_printf("#%s", sel_repr->attribute("id")));
2163         sp_repr_set_attr(clone, "inkscape:transform-center-x", sel_repr->attribute("inkscape:transform-center-x"));
2164         sp_repr_set_attr(clone, "inkscape:transform-center-y", sel_repr->attribute("inkscape:transform-center-y"));
2165         
2166         // add the new clone to the top of the original's parent
2167         parent->appendChild(clone);
2169         newsel = g_slist_prepend(newsel, clone);
2170         reprs = g_slist_remove(reprs, sel_repr);
2171         Inkscape::GC::release(clone);
2172     }
2173     
2174     // TRANSLATORS: only translate "string" in "context|string".
2175     // For more details, see http://developer.gnome.org/doc/API/2.0/glib/glib-I18N.html#Q-:CAPS
2176     sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_CLONE, 
2177                      Q_("action|Clone"));
2179     selection->setReprList(newsel);
2180  
2181     g_slist_free(newsel);
2184 void
2185 sp_selection_unlink()
2187     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2188     if (!desktop)
2189         return;
2191     Inkscape::Selection *selection = sp_desktop_selection(desktop);
2193     if (selection->isEmpty()) {
2194         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>clone</b> to unlink."));
2195         return;
2196     }
2198     // Get a copy of current selection.
2199     GSList *new_select = NULL;
2200     bool unlinked = false;
2201     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2202          items != NULL;
2203          items = items->next)
2204     {
2205         SPItem *item = (SPItem *) items->data;
2207         if (SP_IS_TEXT(item)) {
2208             SPObject *tspan = sp_tref_convert_to_tspan(SP_OBJECT(item));
2209             
2210             if (tspan) {            
2211                 SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
2212             }
2213             
2214             // Set unlink to true, and fall into the next if which
2215             // will include this text item in the new selection
2216             unlinked = true;
2217         }
2219         if (!(SP_IS_USE(item) || SP_IS_TREF(item))) {
2220             // keep the non-use item in the new selection
2221             new_select = g_slist_prepend(new_select, item);
2222             continue;
2223         }
2225         SPItem *unlink;
2226         if (SP_IS_USE(item)) { 
2227             unlink = sp_use_unlink(SP_USE(item));
2228         } else /*if (SP_IS_TREF(use))*/ {
2229             unlink = SP_ITEM(sp_tref_convert_to_tspan(SP_OBJECT(item)));
2230         }
2231         
2232         unlinked = true;
2233         // Add ungrouped items to the new selection.
2234         new_select = g_slist_prepend(new_select, unlink);
2235     }
2237     if (new_select) { // set new selection
2238         selection->clear();
2239         selection->setList(new_select);
2240         g_slist_free(new_select);
2241     }
2242     if (!unlinked) {
2243         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No clones to unlink</b> in the selection."));
2244     }
2246     sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNLINK_CLONE,
2247                      _("Unlink clone"));
2250 void
2251 sp_select_clone_original()
2253     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2254     if (desktop == NULL)
2255         return;
2257     Inkscape::Selection *selection = sp_desktop_selection(desktop);
2259     SPItem *item = selection->singleItem();
2261     const gchar *error = _("Select a <b>clone</b> to go to its original. Select a <b>linked offset</b> to go to its source. Select a <b>text on path</b> to go to the path. Select a <b>flowed text</b> to go to its frame.");
2263     // Check if other than two objects are selected
2264     if (g_slist_length((GSList *) selection->itemList()) != 1 || !item) {
2265         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
2266         return;
2267     }
2269     SPItem *original = NULL;
2270     if (SP_IS_USE(item)) {
2271         original = sp_use_get_original (SP_USE(item));
2272     } else if (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) {
2273         original = sp_offset_get_source (SP_OFFSET(item));
2274     } else if (SP_IS_TEXT_TEXTPATH(item)) {
2275         original = sp_textpath_get_path_item (SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))));
2276     } else if (SP_IS_FLOWTEXT(item)) {
2277         original = SP_FLOWTEXT(item)->get_frame (NULL); // first frame only
2278     } else { // it's an object that we don't know what to do with
2279         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
2280         return;
2281     }
2283     if (!original) {
2284         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>Cannot find</b> the object to select (orphaned clone, offset, textpath, flowed text?)"));
2285         return;
2286     }
2288     for (SPObject *o = original; o && !SP_IS_ROOT(o); o = SP_OBJECT_PARENT (o)) {
2289         if (SP_IS_DEFS (o)) {
2290             desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("The object you're trying to select is <b>not visible</b> (it is in &lt;defs&gt;)"));
2291             return;
2292         }
2293     }
2295     if (original) {
2296         selection->clear();
2297         selection->set(original);
2298         if (SP_CYCLING == SP_CYCLE_FOCUS) {
2299             scroll_to_show_item(desktop, original);
2300         }
2301     }
2305 void sp_selection_to_marker(bool apply)
2307     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2308     if (desktop == NULL)
2309         return;
2311     SPDocument *doc = sp_desktop_document(desktop);
2312     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2314     Inkscape::Selection *selection = sp_desktop_selection(desktop);
2316     // check if something is selected
2317     if (selection->isEmpty()) {
2318         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to marker."));
2319         return;
2320     }
2322     sp_document_ensure_up_to_date(doc);
2323     NR::Maybe<NR::Rect> r = selection->bounds();
2324     if ( !r || r->isEmpty() ) {
2325         return;
2326     }
2328     // calculate the transform to be applied to objects to move them to 0,0
2329     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));
2330     move_p[NR::Y] = -move_p[NR::Y];
2331     NR::Matrix move = NR::Matrix (NR::translate (move_p));
2333     GSList *items = g_slist_copy((GSList *) selection->itemList());
2335     items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2337     // bottommost object, after sorting
2338     SPObject *parent = SP_OBJECT_PARENT (items->data);
2340     NR::Matrix parent_transform = sp_item_i2root_affine(SP_ITEM(parent));
2342     // remember the position of the first item
2343     gint pos = SP_OBJECT_REPR (items->data)->position();
2345     // create a list of duplicates
2346     GSList *repr_copies = NULL;
2347     for (GSList *i = items; i != NULL; i = i->next) {
2348         Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2349         repr_copies = g_slist_prepend (repr_copies, dup);
2350     }
2352     NR::Rect bounds(desktop->dt2doc(r->min()), desktop->dt2doc(r->max()));
2354     if (apply) {
2355         // delete objects so that their clones don't get alerted; this object will be restored shortly
2356         for (GSList *i = items; i != NULL; i = i->next) {
2357             SPObject *item = SP_OBJECT (i->data);
2358             item->deleteObject (false);
2359         }
2360     }
2362     // Hack: Temporarily set clone compensation to unmoved, so that we can move clone-originals
2363     // without disturbing clones.
2364     // See ActorAlign::on_button_click() in src/ui/dialog/align-and-distribute.cpp
2365     int saved_compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2366     prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2368     const gchar *mark_id = generate_marker (repr_copies, bounds, doc,
2369                                         NR::Matrix(NR::translate(desktop->dt2doc(NR::Point(r->min()[NR::X], r->max()[NR::Y])))) * parent_transform.inverse(), parent_transform * move);
2371     // restore compensation setting
2372     prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
2375     g_slist_free (items);
2377     sp_document_done (doc, SP_VERB_EDIT_SELECTION_2_MARKER,
2378                       _("Objects to marker"));
2381 void
2382 sp_selection_tile(bool apply)
2384     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2385     if (desktop == NULL)
2386         return;
2388     SPDocument *doc = sp_desktop_document(desktop);
2389     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2391     Inkscape::Selection *selection = sp_desktop_selection(desktop);
2393     // check if something is selected
2394     if (selection->isEmpty()) {
2395         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to pattern."));
2396         return;
2397     }
2399     sp_document_ensure_up_to_date(doc);
2400     NR::Maybe<NR::Rect> r = selection->bounds();
2401     if ( !r || r->isEmpty() ) {
2402         return;
2403     }
2405     // calculate the transform to be applied to objects to move them to 0,0
2406     NR::Point move_p = NR::Point(0, sp_document_height(doc)) - (r->min() + NR::Point (0, r->extent(NR::Y)));
2407     move_p[NR::Y] = -move_p[NR::Y];
2408     NR::Matrix move = NR::Matrix (NR::translate (move_p));
2410     GSList *items = g_slist_copy((GSList *) selection->itemList());
2412     items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2414     // bottommost object, after sorting
2415     SPObject *parent = SP_OBJECT_PARENT (items->data);
2417     NR::Matrix parent_transform = sp_item_i2root_affine(SP_ITEM(parent));
2419     // remember the position of the first item
2420     gint pos = SP_OBJECT_REPR (items->data)->position();
2422     // create a list of duplicates
2423     GSList *repr_copies = NULL;
2424     for (GSList *i = items; i != NULL; i = i->next) {
2425         Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2426         repr_copies = g_slist_prepend (repr_copies, dup);
2427     }
2429     NR::Rect bounds(desktop->dt2doc(r->min()), desktop->dt2doc(r->max()));
2431     if (apply) {
2432         // delete objects so that their clones don't get alerted; this object will be restored shortly
2433         for (GSList *i = items; i != NULL; i = i->next) {
2434             SPObject *item = SP_OBJECT (i->data);
2435             item->deleteObject (false);
2436         }
2437     }
2439     // Hack: Temporarily set clone compensation to unmoved, so that we can move clone-originals
2440     // without disturbing clones.
2441     // See ActorAlign::on_button_click() in src/ui/dialog/align-and-distribute.cpp
2442     int saved_compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2443     prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2445     const gchar *pat_id = pattern_tile (repr_copies, bounds, doc,
2446                                         NR::Matrix(NR::translate(desktop->dt2doc(NR::Point(r->min()[NR::X], r->max()[NR::Y])))) * parent_transform.inverse(), parent_transform * move);
2448     // restore compensation setting
2449     prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
2451     if (apply) {
2452         Inkscape::XML::Node *rect = xml_doc->createElement("svg:rect");
2453         rect->setAttribute("style", g_strdup_printf("stroke:none;fill:url(#%s)", pat_id));
2455         NR::Point min = bounds.min() * parent_transform.inverse();
2456         NR::Point max = bounds.max() * parent_transform.inverse();
2458         sp_repr_set_svg_double(rect, "width", max[NR::X] - min[NR::X]);
2459         sp_repr_set_svg_double(rect, "height", max[NR::Y] - min[NR::Y]);
2460         sp_repr_set_svg_double(rect, "x", min[NR::X]);
2461         sp_repr_set_svg_double(rect, "y", min[NR::Y]);
2463         // restore parent and position
2464         SP_OBJECT_REPR (parent)->appendChild(rect);
2465         rect->setPosition(pos > 0 ? pos : 0);
2466         SPItem *rectangle = (SPItem *) sp_desktop_document (desktop)->getObjectByRepr(rect);
2468         Inkscape::GC::release(rect);
2470         selection->clear();
2471         selection->set(rectangle);
2472     }
2474     g_slist_free (items);
2476     sp_document_done (doc, SP_VERB_EDIT_TILE, 
2477                       _("Objects to pattern"));
2480 void
2481 sp_selection_untile()
2483     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2484     if (desktop == NULL)
2485         return;
2487     SPDocument *doc = sp_desktop_document(desktop);
2488     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2490     Inkscape::Selection *selection = sp_desktop_selection(desktop);
2492     // check if something is selected
2493     if (selection->isEmpty()) {
2494         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object with pattern fill</b> to extract objects from."));
2495         return;
2496     }
2498     GSList *new_select = NULL;
2500     bool did = false;
2502     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2503          items != NULL;
2504          items = items->next) {
2506         SPItem *item = (SPItem *) items->data;
2508         SPStyle *style = SP_OBJECT_STYLE (item);
2510         if (!style || !style->fill.isPaintserver())
2511             continue;
2513         SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
2515         if (!SP_IS_PATTERN(server))
2516             continue;
2518         did = true;
2520         SPPattern *pattern = pattern_getroot (SP_PATTERN (server));
2522         NR::Matrix pat_transform = pattern_patternTransform (SP_PATTERN (server));
2523         pat_transform *= item->transform;
2525         for (SPObject *child = sp_object_first_child(SP_OBJECT(pattern)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2526             Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate(xml_doc);
2527             SPItem *i = SP_ITEM (desktop->currentLayer()->appendChildRepr(copy));
2529            // FIXME: relink clones to the new canvas objects
2530            // use SPObject::setid when mental finishes it to steal ids of
2532             // this is needed to make sure the new item has curve (simply requestDisplayUpdate does not work)
2533             sp_document_ensure_up_to_date (doc);
2535             NR::Matrix transform( i->transform * pat_transform );
2536             sp_item_write_transform(i, SP_OBJECT_REPR(i), transform);
2538             new_select = g_slist_prepend(new_select, i);
2539         }
2541         SPCSSAttr *css = sp_repr_css_attr_new ();
2542         sp_repr_css_set_property (css, "fill", "none");
2543         sp_repr_css_change (SP_OBJECT_REPR (item), css, "style");
2544     }
2546     if (!did) {
2547         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No pattern fills</b> in the selection."));
2548     } else {
2549         sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNTILE, 
2550                          _("Pattern to objects"));
2551         selection->setList(new_select);
2552     }
2555 void
2556 sp_selection_get_export_hints (Inkscape::Selection *selection, const char **filename, float *xdpi, float *ydpi) 
2558     if (selection->isEmpty()) {
2559         return;
2560     }
2562     const GSList * reprlst = selection->reprList();
2563     bool filename_search = TRUE;
2564     bool xdpi_search = TRUE;
2565     bool ydpi_search = TRUE;
2567     for(; reprlst != NULL &&
2568             filename_search &&
2569             xdpi_search &&
2570             ydpi_search;
2571         reprlst = reprlst->next) {
2572         const gchar * dpi_string;
2573         Inkscape::XML::Node * repr = (Inkscape::XML::Node *)reprlst->data;
2575         if (filename_search) {
2576             *filename = repr->attribute("inkscape:export-filename");
2577             if (*filename != NULL)
2578                 filename_search = FALSE;
2579         }
2581         if (xdpi_search) {
2582             dpi_string = NULL;
2583             dpi_string = repr->attribute("inkscape:export-xdpi");
2584             if (dpi_string != NULL) {
2585                 *xdpi = atof(dpi_string);
2586                 xdpi_search = FALSE;
2587             }
2588         }
2590         if (ydpi_search) {
2591             dpi_string = NULL;
2592             dpi_string = repr->attribute("inkscape:export-ydpi");
2593             if (dpi_string != NULL) {
2594                 *ydpi = atof(dpi_string);
2595                 ydpi_search = FALSE;
2596             }
2597         }
2598     }
2601 void
2602 sp_document_get_export_hints (SPDocument * doc, const char **filename, float *xdpi, float *ydpi) 
2604     Inkscape::XML::Node * repr = sp_document_repr_root(doc);
2605     const gchar * dpi_string;
2607     *filename = repr->attribute("inkscape:export-filename");
2609     dpi_string = NULL;
2610     dpi_string = repr->attribute("inkscape:export-xdpi");
2611     if (dpi_string != NULL) {
2612         *xdpi = atof(dpi_string);
2613     }
2615     dpi_string = NULL;
2616     dpi_string = repr->attribute("inkscape:export-ydpi");
2617     if (dpi_string != NULL) {
2618         *ydpi = atof(dpi_string);
2619     }
2622 void
2623 sp_selection_create_bitmap_copy ()
2625     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2626     if (desktop == NULL)
2627         return;
2629     SPDocument *document = sp_desktop_document(desktop);
2630     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
2632     Inkscape::Selection *selection = sp_desktop_selection(desktop);
2634     // check if something is selected
2635     if (selection->isEmpty()) {
2636         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to make a bitmap copy."));
2637         return;
2638     }
2640     // Get the bounding box of the selection
2641     NRRect bbox;
2642     sp_document_ensure_up_to_date (document);
2643     selection->bounds(&bbox);
2644     if (NR_RECT_DFLS_TEST_EMPTY(&bbox)) {
2645         return; // exceptional situation, so not bother with a translatable error message, just quit quietly
2646     }
2648     // List of the items to show; all others will be hidden
2649     GSList *items = g_slist_copy ((GSList *) selection->itemList());
2651     // Sort items so that the topmost comes last
2652     items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
2654     // Generate a random value from the current time (you may create bitmap from the same object(s)
2655     // multiple times, and this is done so that they don't clash)
2656     GTimeVal cu;
2657     g_get_current_time (&cu);
2658     guint current = (int) (cu.tv_sec * 1000000 + cu.tv_usec) % 1024;
2660     // Create the filename
2661     gchar *filename = g_strdup_printf ("%s-%s-%u.png", document->name, SP_OBJECT_REPR(items->data)->attribute("id"), current);
2662     // Imagemagick is known not to handle spaces in filenames, so we replace anything but letters,
2663     // digits, and a few other chars, with "_"
2664     filename = g_strcanon (filename, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.=+~$#@^&!?", '_');
2665     // Build the complete path by adding document->base if set
2666     gchar *filepath = g_build_filename (document->base?document->base:"", filename, NULL);
2668     //g_print ("%s\n", filepath);
2670     // Remember parent and z-order of the topmost one
2671     gint pos = SP_OBJECT_REPR(g_slist_last(items)->data)->position();
2672     SPObject *parent_object = SP_OBJECT_PARENT(g_slist_last(items)->data);
2673     Inkscape::XML::Node *parent = SP_OBJECT_REPR(parent_object);
2675     // Calculate resolution
2676     double res;
2677     int const prefs_res = prefs_get_int_attribute ("options.createbitmap", "resolution", 0);
2678     int const prefs_min = prefs_get_int_attribute ("options.createbitmap", "minsize", 0);
2679     if (0 < prefs_res) {
2680         // If it's given explicitly in prefs, take it
2681         res = prefs_res;
2682     } else if (0 < prefs_min) {
2683         // If minsize is given, look up minimum bitmap size (default 250 pixels) and calculate resolution from it
2684         res = PX_PER_IN * prefs_min / MIN ((bbox.x1 - bbox.x0), (bbox.y1 - bbox.y0));
2685     } else {
2686         float hint_xdpi = 0, hint_ydpi = 0;
2687         const char *hint_filename;
2688         // take resolution hint from the selected objects
2689         sp_selection_get_export_hints (selection, &hint_filename, &hint_xdpi, &hint_ydpi);
2690         if (hint_xdpi != 0) {
2691             res = hint_xdpi;
2692         } else {
2693             // take resolution hint from the document
2694             sp_document_get_export_hints (document, &hint_filename, &hint_xdpi, &hint_ydpi);
2695             if (hint_xdpi != 0) {
2696                 res = hint_xdpi;
2697             } else {
2698                 // if all else fails, take the default 90 dpi
2699                 res = PX_PER_IN;
2700             }
2701         }
2702     }
2704     // The width and height of the bitmap in pixels
2705     unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * res / PX_PER_IN);
2706     unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * res / PX_PER_IN);
2708     // Find out if we have to run a filter
2709     const gchar *run = NULL;
2710     const gchar *filter = prefs_get_string_attribute ("options.createbitmap", "filter");
2711     if (filter) {
2712         // filter command is given;
2713         // see if we have a parameter to pass to it
2714         const gchar *param1 = prefs_get_string_attribute ("options.createbitmap", "filter_param1");
2715         if (param1) {
2716             if (param1[strlen(param1) - 1] == '%') {
2717                 // if the param string ends with %, interpret it as a percentage of the image's max dimension
2718                 gchar p1[256];
2719                 g_ascii_dtostr (p1, 256, ceil (g_ascii_strtod (param1, NULL) * MAX(width, height) / 100));
2720                 // the first param is always the image filename, the second is param1
2721                 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, p1);
2722             } else {
2723                 // otherwise pass the param1 unchanged
2724                 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, param1);
2725             }
2726         } else {
2727             // run without extra parameter
2728             run = g_strdup_printf ("%s \"%s\"", filter, filepath);
2729         }
2730     }
2732     // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
2733     NR::Matrix eek = sp_item_i2d_affine (SP_ITEM(parent_object));
2734     NR::Matrix t;
2736     double shift_x = bbox.x0;
2737     double shift_y = bbox.y1; 
2738     if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
2739         shift_x = round (shift_x);
2740         shift_y = -round (-shift_y); // this gets correct rounding despite coordinate inversion, remove the negations when the inversion is gone
2741     }
2742     t = NR::scale(1, -1) * NR::translate (shift_x, shift_y) * eek.inverse();
2744     // Do the export
2745     sp_export_png_file(document, filepath,
2746                    bbox.x0, bbox.y0, bbox.x1, bbox.y1,
2747                    width, height, res, res,
2748                    (guint32) 0xffffff00,
2749                    NULL, NULL,
2750                    true,  /*bool force_overwrite,*/
2751                    items);
2753     g_slist_free (items);
2755     // Run filter, if any
2756     if (run) {
2757         g_print ("Running external filter: %s\n", run);
2758         system (run);
2759     }
2761     // Import the image back
2762     GdkPixbuf *pb = gdk_pixbuf_new_from_file (filepath, NULL);
2763     if (pb) {
2764         // Create the repr for the image
2765         Inkscape::XML::Node * repr = xml_doc->createElement("svg:image");
2766         repr->setAttribute("xlink:href", filename);
2767         repr->setAttribute("sodipodi:absref", filepath);
2768         if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
2769             sp_repr_set_svg_double(repr, "width", width);
2770             sp_repr_set_svg_double(repr, "height", height);
2771         } else {
2772             sp_repr_set_svg_double(repr, "width", (bbox.x1 - bbox.x0));
2773             sp_repr_set_svg_double(repr, "height", (bbox.y1 - bbox.y0));
2774         }
2776         // Write transform
2777         gchar *c=sp_svg_transform_write(t);
2778         repr->setAttribute("transform", c);
2779         g_free(c);
2781         // add the new repr to the parent
2782         parent->appendChild(repr);
2784         // move to the saved position
2785         repr->setPosition(pos > 0 ? pos + 1 : 1);
2787         // Set selection to the new image
2788         selection->clear();
2789         selection->add(repr);
2791         // Clean up
2792         Inkscape::GC::release(repr);
2793         gdk_pixbuf_unref (pb);
2795         // Complete undoable transaction
2796         sp_document_done (document, SP_VERB_SELECTION_CREATE_BITMAP,
2797                           _("Create bitmap"));
2798     }
2800     g_free (filename);
2801     g_free (filepath);
2804 /**
2805  * \brief sp_selection_set_mask
2806  *
2807  * This function creates a mask or clipPath from selection
2808  * Two different modes:
2809  *  if applyToLayer, all selection is moved to DEFS as mask/clippath
2810  *       and is applied to current layer
2811  *  otherwise, topmost object is used as mask for other objects
2812  * If \a apply_clip_path parameter is true, clipPath is created, otherwise mask
2813  * 
2814  */
2815 void
2816 sp_selection_set_mask(bool apply_clip_path, bool apply_to_layer)
2818     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2819     if (desktop == NULL)
2820         return;
2822     SPDocument *doc = sp_desktop_document(desktop);
2823     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2824     
2825     Inkscape::Selection *selection = sp_desktop_selection(desktop);
2827     // check if something is selected
2828     bool is_empty = selection->isEmpty();
2829     if ( apply_to_layer && is_empty) {
2830         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to create clippath or mask from."));
2831         return;
2832     } else if (!apply_to_layer && ( is_empty || NULL == selection->itemList()->next )) {
2833         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select mask object and <b>object(s)</b> to apply clippath or mask to."));
2834         return;
2835     }
2837     // FIXME: temporary patch to prevent crash! 
2838     // Remove this when bboxes are fixed to not blow up on an item clipped/masked with its own clone
2839     bool clone_with_original = selection_contains_both_clone_and_original (selection);
2840     if (clone_with_original) {
2841         return; // in this version, you cannot clip/mask an object with its own clone
2842     }
2843     // /END FIXME
2844     
2845     sp_document_ensure_up_to_date(doc);
2847     GSList *items = g_slist_copy((GSList *) selection->itemList());
2848     
2849     items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2851     // create a list of duplicates
2852     GSList *mask_items = NULL;
2853     GSList *apply_to_items = NULL;
2854     GSList *items_to_delete = NULL;
2855     bool topmost = prefs_get_int_attribute ("options.maskobject", "topmost", 1);
2856     bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
2857     
2858     if (apply_to_layer) {
2859         // all selected items are used for mask, which is applied to a layer
2860         apply_to_items = g_slist_prepend (apply_to_items, desktop->currentLayer());
2862         for (GSList *i = items; i != NULL; i = i->next) {
2863             Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2864             mask_items = g_slist_prepend (mask_items, dup);
2866             if (remove_original) {
2867                 SPObject *item = SP_OBJECT (i->data);
2868                 items_to_delete = g_slist_prepend (items_to_delete, item);
2869             }
2870         }
2871     } else if (!topmost) {
2872         // topmost item is used as a mask, which is applied to other items in a selection
2873         GSList *i = items;
2874         Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2875         mask_items = g_slist_prepend (mask_items, dup);
2877         if (remove_original) {
2878             SPObject *item = SP_OBJECT (i->data);
2879             items_to_delete = g_slist_prepend (items_to_delete, item);
2880         }
2881         
2882         for (i = i->next; i != NULL; i = i->next) {
2883             apply_to_items = g_slist_prepend (apply_to_items, i->data);
2884         }
2885     } else {
2886         GSList *i = NULL;
2887         for (i = items; NULL != i->next; i = i->next) {
2888             apply_to_items = g_slist_prepend (apply_to_items, i->data);
2889         }
2891         Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2892         mask_items = g_slist_prepend (mask_items, dup);
2894         if (remove_original) {
2895             SPObject *item = SP_OBJECT (i->data);
2896             items_to_delete = g_slist_prepend (items_to_delete, item);
2897         }
2898     }
2899     
2900     g_slist_free (items);
2901     items = NULL;
2902             
2903     gchar const* attributeName = apply_clip_path ? "clip-path" : "mask";
2904     for (GSList *i = apply_to_items; NULL != i; i = i->next) {
2905         SPItem *item = reinterpret_cast<SPItem *>(i->data);
2906         // inverted object transform should be applied to a mask object,
2907         // as mask is calculated in user space (after applying transform)
2908         NR::Matrix maskTransform (item->transform.inverse());
2910         GSList *mask_items_dup = NULL;
2911         for (GSList *mask_item = mask_items; NULL != mask_item; mask_item = mask_item->next) {
2912             Inkscape::XML::Node *dup = reinterpret_cast<Inkscape::XML::Node *>(mask_item->data)->duplicate(xml_doc);
2913             mask_items_dup = g_slist_prepend (mask_items_dup, dup);
2914         }
2916         const gchar *mask_id = NULL;
2917         if (apply_clip_path) {
2918             mask_id = sp_clippath_create(mask_items_dup, doc, &maskTransform);
2919         } else {
2920             mask_id = sp_mask_create(mask_items_dup, doc, &maskTransform);
2921         }
2923         g_slist_free (mask_items_dup);
2924         mask_items_dup = NULL;
2926         SP_OBJECT_REPR(i->data)->setAttribute(attributeName, g_strdup_printf("url(#%s)", mask_id));
2927     }
2929     g_slist_free (mask_items);
2930     g_slist_free (apply_to_items);
2932     for (GSList *i = items_to_delete; NULL != i; i = i->next) {
2933         SPObject *item = SP_OBJECT (i->data);
2934         item->deleteObject (false);
2935     }
2936     g_slist_free (items_to_delete);
2938     if (apply_clip_path) 
2939         sp_document_done (doc, SP_VERB_OBJECT_SET_CLIPPATH, _("Set clipping path"));
2940     else 
2941         sp_document_done (doc, SP_VERB_OBJECT_SET_MASK, _("Set mask"));
2944 void sp_selection_unset_mask(bool apply_clip_path) {
2945     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2946     if (desktop == NULL)
2947         return;
2948     
2949     SPDocument *doc = sp_desktop_document(desktop);    
2950     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2951     Inkscape::Selection *selection = sp_desktop_selection(desktop);
2953     // check if something is selected
2954     if (selection->isEmpty()) {
2955         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to remove clippath or mask from."));
2956         return;
2957     }
2958     
2959     bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
2960     sp_document_ensure_up_to_date(doc);
2962     gchar const* attributeName = apply_clip_path ? "clip-path" : "mask";
2963     std::map<SPObject*,SPItem*> referenced_objects;
2964     for (GSList const*i = selection->itemList(); NULL != i; i = i->next) {
2965         if (remove_original) {
2966             // remember referenced mask/clippath, so orphaned masks can be moved back to document
2967             SPItem *item = reinterpret_cast<SPItem *>(i->data);
2968             Inkscape::URIReference *uri_ref = NULL;
2969         
2970             if (apply_clip_path) {
2971                 uri_ref = item->clip_ref;
2972             } else {
2973                 uri_ref = item->mask_ref;
2974             }
2976             // collect distinct mask object (and associate with item to apply transform)
2977             if (NULL != uri_ref && NULL != uri_ref->getObject()) {
2978                 referenced_objects[uri_ref->getObject()] = item;
2979             }
2980         }
2982         SP_OBJECT_REPR(i->data)->setAttribute(attributeName, "none");
2983     }
2985     // restore mask objects into a document
2986     for ( std::map<SPObject*,SPItem*>::iterator it = referenced_objects.begin() ; it != referenced_objects.end() ; ++it) {
2987         SPObject *obj = (*it).first;
2988         GSList *items_to_move = NULL;
2989         for (SPObject *child = sp_object_first_child(obj) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2990             Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate(xml_doc);
2991             items_to_move = g_slist_prepend (items_to_move, copy);
2992         }
2994         if (!obj->isReferenced()) {
2995             // delete from defs if no other object references this mask
2996             obj->deleteObject(false);
2997         }
2999         // remember parent and position of the item to which the clippath/mask was applied
3000         Inkscape::XML::Node *parent = SP_OBJECT_REPR((*it).second)->parent();
3001         gint pos = SP_OBJECT_REPR((*it).second)->position();
3003         for (GSList *i = items_to_move; NULL != i; i = i->next) {
3004             Inkscape::XML::Node *repr = (Inkscape::XML::Node *)i->data;
3006             // insert into parent, restore pos
3007             parent->appendChild(repr);
3008             repr->setPosition((pos + 1) > 0 ? (pos + 1) : 0);
3010             SPItem *mask_item = (SPItem *) sp_desktop_document (desktop)->getObjectByRepr(repr);
3011             selection->add(repr);
3013             // transform mask, so it is moved the same spot where mask was applied
3014             NR::Matrix transform (mask_item->transform);
3015             transform *= (*it).second->transform;
3016             sp_item_write_transform(mask_item, SP_OBJECT_REPR(mask_item), transform);
3017         }
3019         g_slist_free (items_to_move);
3020     }
3022     if (apply_clip_path) 
3023         sp_document_done (doc, SP_VERB_OBJECT_UNSET_CLIPPATH, _("Release clipping path"));
3024     else 
3025         sp_document_done (doc, SP_VERB_OBJECT_UNSET_MASK, _("Release mask"));
3028 void fit_canvas_to_selection(SPDesktop *desktop) {
3029     g_return_if_fail(desktop != NULL);
3030     SPDocument *doc = sp_desktop_document(desktop);
3032     g_return_if_fail(doc != NULL);
3033     g_return_if_fail(desktop->selection != NULL);
3035     if (desktop->selection->isEmpty()) {
3036         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to fit canvas to."));
3037         return;
3038     }
3039     NR::Maybe<NR::Rect> const bbox(desktop->selection->bounds());
3040     if (bbox && !bbox->isEmpty()) {
3041         doc->fitToRect(*bbox);
3042     }
3043 };
3045 void fit_canvas_to_drawing(SPDocument *doc) {
3046     g_return_if_fail(doc != NULL);
3048     sp_document_ensure_up_to_date(doc);
3049     SPItem const *const root = SP_ITEM(doc->root);
3050     NR::Maybe<NR::Rect> const bbox(root->getBounds(sp_item_i2r_affine(root)));
3051     if (bbox && !bbox->isEmpty()) {
3052         doc->fitToRect(*bbox);
3053     }
3054 };
3056 void fit_canvas_to_selection_or_drawing(SPDesktop *desktop) {
3057     g_return_if_fail(desktop != NULL);
3058     SPDocument *doc = sp_desktop_document(desktop);
3060     g_return_if_fail(doc != NULL);
3061     g_return_if_fail(desktop->selection != NULL);
3063     if (desktop->selection->isEmpty()) {
3064         fit_canvas_to_drawing(doc);
3065     } else {
3066         fit_canvas_to_selection(desktop);
3067     }
3069     sp_document_done(doc, SP_VERB_FIT_CANVAS_TO_DRAWING, 
3070                      _("Fit page to selection"));
3071 };
3073 static void itemtree_map(void (*f)(SPItem *, SPDesktop *), SPObject *root, SPDesktop *desktop) {
3074     // don't operate on layers
3075     if (SP_IS_ITEM(root) && !desktop->isLayer(SP_ITEM(root))) {
3076         f(SP_ITEM(root), desktop);
3077     }
3078     for ( SPObject::SiblingIterator iter = root->firstChild() ; iter ; ++iter ) {
3079         //don't recurse into locked layers
3080         if (!(SP_IS_ITEM(&*iter) && desktop->isLayer(SP_ITEM(&*iter)) && SP_ITEM(&*iter)->isLocked())) {
3081             itemtree_map(f, iter, desktop);
3082         }
3083     }
3086 static void unlock(SPItem *item, SPDesktop *desktop) {
3087     if (item->isLocked()) {
3088         item->setLocked(FALSE);
3089     }
3092 static void unhide(SPItem *item, SPDesktop *desktop) {
3093     if (desktop->itemIsHidden(item)) {
3094         item->setExplicitlyHidden(FALSE);
3095     }
3098 static void process_all(void (*f)(SPItem *, SPDesktop *), SPDesktop *dt, bool layer_only) {
3099     if (!dt) return;
3100         
3101     SPObject *root;
3102     if (layer_only) {
3103         root = dt->currentLayer();
3104     } else {
3105         root = dt->currentRoot();
3106     }
3107     
3108     itemtree_map(f, root, dt);
3111 void unlock_all(SPDesktop *dt) {
3112     process_all(&unlock, dt, true);
3115 void unlock_all_in_all_layers(SPDesktop *dt) {
3116     process_all(&unlock, dt, false);
3119 void unhide_all(SPDesktop *dt) {
3120     process_all(&unhide, dt, true);
3123 void unhide_all_in_all_layers(SPDesktop *dt) {
3124     process_all(&unhide, dt, false);
3128 GSList * sp_selection_get_clipboard() {
3129     return clipboard;
3132 /*
3133   Local Variables:
3134   mode:c++
3135   c-file-style:"stroustrup"
3136   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
3137   indent-tabs-mode:nil
3138   fill-column:99
3139   End:
3140 */
3141 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :