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