ed8d6379e4aaae48dd74060d3f0a0cf6b029abd3
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-flowtext.h"
39 #include "sp-flowregion.h"
40 #include "text-editing.h"
41 #include "text-context.h"
42 #include "connector-context.h"
43 #include "sp-path.h"
44 #include "sp-conn-end.h"
45 #include "dropper-context.h"
46 #include <glibmm/i18n.h>
47 #include "libnr/nr-matrix-rotate-ops.h"
48 #include "libnr/nr-matrix-translate-ops.h"
49 #include "libnr/nr-rotate-fns.h"
50 #include "libnr/nr-scale-ops.h"
51 #include "libnr/nr-scale-translate-ops.h"
52 #include "libnr/nr-translate-matrix-ops.h"
53 #include "libnr/nr-translate-scale-ops.h"
54 #include "xml/repr.h"
55 #include "style.h"
56 #include "document-private.h"
57 #include "sp-gradient.h"
58 #include "sp-gradient-reference.h"
59 #include "sp-linear-gradient-fns.h"
60 #include "sp-pattern.h"
61 #include "sp-radial-gradient-fns.h"
62 #include "sp-namedview.h"
63 #include "prefs-utils.h"
64 #include "sp-offset.h"
65 #include "sp-clippath.h"
66 #include "sp-mask.h"
67 #include "file.h"
68 #include "helper/png-write.h"
69 #include "layer-fns.h"
70 #include "context-fns.h"
71 #include <map>
72 #include "helper/units.h"
73 #include "sp-item.h"
74 #include "unit-constants.h"
75 #include "xml/simple-document.h"
77 using NR::X;
78 using NR::Y;
80 #include "selection-chemistry.h"
82 /* fixme: find a better place */
83 Inkscape::XML::Document *clipboard_document = NULL;
84 GSList *clipboard = NULL;
85 GSList *defs_clipboard = NULL;
86 SPCSSAttr *style_clipboard = NULL;
87 NR::Maybe<NR::Rect> size_clipboard;
89 static void sp_copy_stuff_used_by_item(GSList **defs_clip, SPItem *item, const GSList *items, Inkscape::XML::Document* xml_doc);
91 /**
92 * Copies repr and its inherited css style elements, along with the accumulated transform 'full_t',
93 * then prepends the copy to 'clip'.
94 */
95 void sp_selection_copy_one (Inkscape::XML::Node *repr, NR::Matrix full_t, GSList **clip, Inkscape::XML::Document* xml_doc)
96 {
97 Inkscape::XML::Node *copy = repr->duplicate(xml_doc);
99 // copy complete inherited style
100 SPCSSAttr *css = sp_repr_css_attr_inherited(repr, "style");
101 sp_repr_css_set(copy, css, "style");
102 sp_repr_css_attr_unref(css);
104 // write the complete accumulated transform passed to us
105 // (we're dealing with unattached repr, so we write to its attr
106 // instead of using sp_item_set_transform)
107 gchar *affinestr=sp_svg_transform_write(full_t);
108 copy->setAttribute("transform", affinestr);
109 g_free(affinestr);
111 *clip = g_slist_prepend(*clip, copy);
112 }
114 void sp_selection_copy_impl (const GSList *items, GSList **clip, GSList **defs_clip, SPCSSAttr **style_clip, Inkscape::XML::Document* xml_doc)
115 {
117 // Copy stuff referenced by all items to defs_clip:
118 if (defs_clip) {
119 for (GSList *i = (GSList *) items; i != NULL; i = i->next) {
120 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (i->data), items, xml_doc);
121 }
122 *defs_clip = g_slist_reverse(*defs_clip);
123 }
125 // Store style:
126 if (style_clip) {
127 SPItem *item = SP_ITEM (items->data); // take from the first selected item
128 *style_clip = take_style_from_item (item);
129 }
131 if (clip) {
132 // Sort items:
133 GSList *sorted_items = g_slist_copy ((GSList *) items);
134 sorted_items = g_slist_sort((GSList *) sorted_items, (GCompareFunc) sp_object_compare_position);
136 // Copy item reprs:
137 for (GSList *i = (GSList *) sorted_items; i != NULL; i = i->next) {
138 sp_selection_copy_one (SP_OBJECT_REPR (i->data), sp_item_i2doc_affine(SP_ITEM (i->data)), clip, xml_doc);
139 }
141 *clip = g_slist_reverse(*clip);
142 g_slist_free ((GSList *) sorted_items);
143 }
144 }
146 /**
147 * Add gradients/patterns/markers referenced by copied objects to defs.
148 * Iterates through 'defs_clip', and for each item it adds the data
149 * repr into the global defs.
150 */
151 void
152 paste_defs (GSList **defs_clip, SPDocument *doc)
153 {
154 if (!defs_clip)
155 return;
157 for (GSList *gl = *defs_clip; gl != NULL; gl = gl->next) {
158 SPDefs *defs= (SPDefs *) SP_DOCUMENT_DEFS(doc);
159 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) gl->data;
160 gchar const *id = repr->attribute("id");
161 if (!id || !doc->getObjectById(id)) {
162 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
163 Inkscape::XML::Node *copy = repr->duplicate(xml_doc);
164 SP_OBJECT_REPR(defs)->addChild(copy, NULL);
165 Inkscape::GC::release(copy);
166 }
167 }
168 }
170 GSList *sp_selection_paste_impl (SPDocument *doc, SPObject *parent, GSList **clip, GSList **defs_clip)
171 {
172 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
173 paste_defs (defs_clip, doc);
175 GSList *copied = NULL;
176 // add objects to document
177 for (GSList *l = *clip; l != NULL; l = l->next) {
178 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
179 Inkscape::XML::Node *copy = repr->duplicate(xml_doc);
181 // premultiply the item transform by the accumulated parent transform in the paste layer
182 NR::Matrix local = sp_item_i2doc_affine(SP_ITEM(parent));
183 if (!local.test_identity()) {
184 gchar const *t_str = copy->attribute("transform");
185 NR::Matrix item_t (NR::identity());
186 if (t_str)
187 sp_svg_transform_read(t_str, &item_t);
188 item_t *= local.inverse();
189 // (we're dealing with unattached repr, so we write to its attr instead of using sp_item_set_transform)
190 gchar *affinestr=sp_svg_transform_write(item_t);
191 copy->setAttribute("transform", affinestr);
192 g_free(affinestr);
193 }
195 parent->appendChildRepr(copy);
196 copied = g_slist_prepend(copied, copy);
197 Inkscape::GC::release(copy);
198 }
199 return copied;
200 }
202 void sp_selection_delete_impl(const GSList *items)
203 {
204 for (const GSList *i = items ; i ; i = i->next ) {
205 sp_object_ref((SPObject *)i->data, NULL);
206 }
207 for (const GSList *i = items; i != NULL; i = i->next) {
208 SPItem *item = (SPItem *) i->data;
209 SP_OBJECT(item)->deleteObject();
210 sp_object_unref((SPObject *)item, NULL);
211 }
212 }
215 void sp_selection_delete()
216 {
217 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
218 if (desktop == NULL) {
219 return;
220 }
222 if (tools_isactive (desktop, TOOLS_TEXT))
223 if (sp_text_delete_selection(desktop->event_context)) {
224 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_TEXT,
225 _("Delete text"));
226 return;
227 }
229 Inkscape::Selection *selection = sp_desktop_selection(desktop);
231 // check if something is selected
232 if (selection->isEmpty()) {
233 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Nothing</b> was deleted."));
234 return;
235 }
237 const GSList *selected = g_slist_copy(const_cast<GSList *>(selection->itemList()));
238 selection->clear();
239 sp_selection_delete_impl (selected);
240 g_slist_free ((GSList *) selected);
242 /* a tool may have set up private information in it's selection context
243 * that depends on desktop items. I think the only sane way to deal with
244 * this currently is to reset the current tool, which will reset it's
245 * associated selection context. For example: deleting an object
246 * while moving it around the canvas.
247 */
248 tools_switch ( desktop, tools_active ( desktop ) );
250 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_DELETE,
251 _("Delete"));
252 }
254 /* fixme: sequencing */
255 void sp_selection_duplicate()
256 {
257 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
258 if (desktop == NULL)
259 return;
261 Inkscape::XML::Document* xml_doc = sp_document_repr_doc(desktop->doc());
262 Inkscape::Selection *selection = sp_desktop_selection(desktop);
264 // check if something is selected
265 if (selection->isEmpty()) {
266 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to duplicate."));
267 return;
268 }
270 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
272 selection->clear();
274 // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
275 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
277 GSList *newsel = NULL;
279 while (reprs) {
280 Inkscape::XML::Node *parent = ((Inkscape::XML::Node *) reprs->data)->parent();
281 Inkscape::XML::Node *copy = ((Inkscape::XML::Node *) reprs->data)->duplicate(xml_doc);
283 parent->appendChild(copy);
285 newsel = g_slist_prepend(newsel, copy);
286 reprs = g_slist_remove(reprs, reprs->data);
287 Inkscape::GC::release(copy);
288 }
290 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_DUPLICATE,
291 _("Duplicate"));
293 selection->setReprList(newsel);
295 g_slist_free(newsel);
296 }
298 void sp_edit_clear_all()
299 {
300 SPDesktop *dt = SP_ACTIVE_DESKTOP;
301 if (!dt)
302 return;
304 SPDocument *doc = sp_desktop_document(dt);
305 sp_desktop_selection(dt)->clear();
307 g_return_if_fail(SP_IS_GROUP(dt->currentLayer()));
308 GSList *items = sp_item_group_item_list(SP_GROUP(dt->currentLayer()));
310 while (items) {
311 SP_OBJECT (items->data)->deleteObject();
312 items = g_slist_remove(items, items->data);
313 }
315 sp_document_done(doc, SP_VERB_EDIT_CLEAR_ALL,
316 _("Delete all"));
317 }
319 GSList *
320 get_all_items (GSList *list, SPObject *from, SPDesktop *desktop, bool onlyvisible, bool onlysensitive, const GSList *exclude)
321 {
322 for (SPObject *child = sp_object_first_child(SP_OBJECT(from)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
323 if (SP_IS_ITEM(child) &&
324 !desktop->isLayer(SP_ITEM(child)) &&
325 (!onlysensitive || !SP_ITEM(child)->isLocked()) &&
326 (!onlyvisible || !desktop->itemIsHidden(SP_ITEM(child))) &&
327 (!exclude || !g_slist_find ((GSList *) exclude, child))
328 )
329 {
330 list = g_slist_prepend (list, SP_ITEM(child));
331 }
333 if (SP_IS_ITEM(child) && desktop->isLayer(SP_ITEM(child))) {
334 list = get_all_items (list, child, desktop, onlyvisible, onlysensitive, exclude);
335 }
336 }
338 return list;
339 }
341 void sp_edit_select_all_full (bool force_all_layers, bool invert)
342 {
343 SPDesktop *dt = SP_ACTIVE_DESKTOP;
344 if (!dt)
345 return;
347 Inkscape::Selection *selection = sp_desktop_selection(dt);
349 g_return_if_fail(SP_IS_GROUP(dt->currentLayer()));
351 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
352 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
353 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
355 GSList *items = NULL;
357 const GSList *exclude = NULL;
358 if (invert) {
359 exclude = selection->itemList();
360 }
362 if (force_all_layers)
363 inlayer = PREFS_SELECTION_ALL;
365 switch (inlayer) {
366 case PREFS_SELECTION_LAYER: {
367 if ( (onlysensitive && SP_ITEM(dt->currentLayer())->isLocked()) ||
368 (onlyvisible && dt->itemIsHidden(SP_ITEM(dt->currentLayer()))) )
369 return;
371 GSList *all_items = sp_item_group_item_list(SP_GROUP(dt->currentLayer()));
373 for (GSList *i = all_items; i; i = i->next) {
374 SPItem *item = SP_ITEM (i->data);
376 if (item && (!onlysensitive || !item->isLocked())) {
377 if (!onlyvisible || !dt->itemIsHidden(item)) {
378 if (!dt->isLayer(item)) {
379 if (!invert || !g_slist_find ((GSList *) exclude, item)) {
380 items = g_slist_prepend (items, item); // leave it in the list
381 }
382 }
383 }
384 }
385 }
387 g_slist_free (all_items);
388 break;
389 }
390 case PREFS_SELECTION_LAYER_RECURSIVE: {
391 items = get_all_items (NULL, dt->currentLayer(), dt, onlyvisible, onlysensitive, exclude);
392 break;
393 }
394 default: {
395 items = get_all_items (NULL, dt->currentRoot(), dt, onlyvisible, onlysensitive, exclude);
396 break;
397 }
398 }
400 selection->setList (items);
402 if (items) {
403 g_slist_free (items);
404 }
405 }
407 void sp_edit_select_all ()
408 {
409 sp_edit_select_all_full (false, false);
410 }
412 void sp_edit_select_all_in_all_layers ()
413 {
414 sp_edit_select_all_full (true, false);
415 }
417 void sp_edit_invert ()
418 {
419 sp_edit_select_all_full (false, true);
420 }
422 void sp_edit_invert_in_all_layers ()
423 {
424 sp_edit_select_all_full (true, true);
425 }
427 void sp_selection_group()
428 {
429 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
430 if (desktop == NULL)
431 return;
433 SPDocument *doc = sp_desktop_document (desktop);
434 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
436 Inkscape::Selection *selection = sp_desktop_selection(desktop);
438 // Check if something is selected.
439 if (selection->isEmpty()) {
440 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>some objects</b> to group."));
441 return;
442 }
444 GSList const *l = (GSList *) selection->reprList();
446 GSList *p = g_slist_copy((GSList *) l);
448 selection->clear();
450 p = g_slist_sort(p, (GCompareFunc) sp_repr_compare_position);
452 // Remember the position and parent of the topmost object.
453 gint topmost = ((Inkscape::XML::Node *) g_slist_last(p)->data)->position();
454 Inkscape::XML::Node *topmost_parent = ((Inkscape::XML::Node *) g_slist_last(p)->data)->parent();
456 Inkscape::XML::Node *group = xml_doc->createElement("svg:g");
458 while (p) {
459 Inkscape::XML::Node *current = (Inkscape::XML::Node *) p->data;
461 if (current->parent() == topmost_parent) {
462 Inkscape::XML::Node *spnew = current->duplicate(xml_doc);
463 sp_repr_unparent(current);
464 group->appendChild(spnew);
465 Inkscape::GC::release(spnew);
466 topmost --; // only reduce count for those items deleted from topmost_parent
467 } else { // move it to topmost_parent first
468 GSList *temp_clip = NULL;
470 // At this point, current may already have no item, due to its being a clone whose original is already moved away
471 // So we copy it artificially calculating the transform from its repr->attr("transform") and the parent transform
472 gchar const *t_str = current->attribute("transform");
473 NR::Matrix item_t (NR::identity());
474 if (t_str)
475 sp_svg_transform_read(t_str, &item_t);
476 item_t *= sp_item_i2doc_affine(SP_ITEM(doc->getObjectByRepr(current->parent())));
477 //FIXME: when moving both clone and original from a transformed group (either by
478 //grouping into another parent, or by cut/paste) the transform from the original's
479 //parent becomes embedded into original itself, and this affects its clones. Fix
480 //this by remembering the transform diffs we write to each item into an array and
481 //then, if this is clone, looking up its original in that array and pre-multiplying
482 //it by the inverse of that original's transform diff.
484 sp_selection_copy_one (current, item_t, &temp_clip, xml_doc);
485 sp_repr_unparent(current);
487 // paste into topmost_parent (temporarily)
488 GSList *copied = sp_selection_paste_impl (doc, doc->getObjectByRepr(topmost_parent), &temp_clip, NULL);
489 if (temp_clip) g_slist_free (temp_clip);
490 if (copied) { // if success,
491 // take pasted object (now in topmost_parent)
492 Inkscape::XML::Node *in_topmost = (Inkscape::XML::Node *) copied->data;
493 // make a copy
494 Inkscape::XML::Node *spnew = in_topmost->duplicate(xml_doc);
495 // remove pasted
496 sp_repr_unparent(in_topmost);
497 // put its copy into group
498 group->appendChild(spnew);
499 Inkscape::GC::release(spnew);
500 g_slist_free (copied);
501 }
502 }
503 p = g_slist_remove(p, current);
504 }
506 // Add the new group to the topmost members' parent
507 topmost_parent->appendChild(group);
509 // Move to the position of the topmost, reduced by the number of items deleted from topmost_parent
510 group->setPosition(topmost + 1);
512 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_GROUP,
513 _("Group"));
515 selection->set(group);
516 Inkscape::GC::release(group);
517 }
519 void sp_selection_ungroup()
520 {
521 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
522 if (desktop == NULL)
523 return;
525 Inkscape::Selection *selection = sp_desktop_selection(desktop);
527 if (selection->isEmpty()) {
528 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>group</b> to ungroup."));
529 return;
530 }
532 GSList *items = g_slist_copy((GSList *) selection->itemList());
533 selection->clear();
535 // Get a copy of current selection.
536 GSList *new_select = NULL;
537 bool ungrouped = false;
538 for (GSList *i = items;
539 i != NULL;
540 i = i->next)
541 {
542 SPItem *group = (SPItem *) i->data;
544 // when ungrouping cloned groups with their originals, some objects that were selected may no more exist due to unlinking
545 if (!SP_IS_OBJECT(group)) {
546 continue;
547 }
549 /* We do not allow ungrouping <svg> etc. (lauris) */
550 if (strcmp(SP_OBJECT_REPR(group)->name(), "svg:g") && strcmp(SP_OBJECT_REPR(group)->name(), "svg:switch")) {
551 // keep the non-group item in the new selection
552 selection->add(group);
553 continue;
554 }
556 GSList *children = NULL;
557 /* This is not strictly required, but is nicer to rely on group ::destroy (lauris) */
558 sp_item_group_ungroup(SP_GROUP(group), &children, false);
559 ungrouped = true;
560 // Add ungrouped items to the new selection.
561 new_select = g_slist_concat(new_select, children);
562 }
564 if (new_select) { // Set new selection.
565 selection->addList(new_select);
566 g_slist_free(new_select);
567 }
568 if (!ungrouped) {
569 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No groups</b> to ungroup in the selection."));
570 }
572 g_slist_free(items);
574 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_UNGROUP,
575 _("Ungroup"));
576 }
578 static SPGroup *
579 sp_item_list_common_parent_group(const GSList *items)
580 {
581 if (!items) {
582 return NULL;
583 }
584 SPObject *parent = SP_OBJECT_PARENT(items->data);
585 /* Strictly speaking this CAN happen, if user selects <svg> from Inkscape::XML editor */
586 if (!SP_IS_GROUP(parent)) {
587 return NULL;
588 }
589 for (items = items->next; items; items = items->next) {
590 if (SP_OBJECT_PARENT(items->data) != parent) {
591 return NULL;
592 }
593 }
595 return SP_GROUP(parent);
596 }
598 /** Finds out the minimum common bbox of the selected items
599 */
600 static NR::Maybe<NR::Rect>
601 enclose_items(const GSList *items)
602 {
603 g_assert(items != NULL);
605 NR::Maybe<NR::Rect> r = NR::Nothing();
606 for (GSList const *i = items; i; i = i->next) {
607 r = NR::union_bounds(r, sp_item_bbox_desktop((SPItem *) i->data));
608 }
609 return r;
610 }
612 SPObject *
613 prev_sibling(SPObject *child)
614 {
615 SPObject *parent = SP_OBJECT_PARENT(child);
616 if (!SP_IS_GROUP(parent)) {
617 return NULL;
618 }
619 for ( SPObject *i = sp_object_first_child(parent) ; i; i = SP_OBJECT_NEXT(i) ) {
620 if (i->next == child)
621 return i;
622 }
623 return NULL;
624 }
626 void
627 sp_selection_raise()
628 {
629 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
630 if (!desktop)
631 return;
633 Inkscape::Selection *selection = sp_desktop_selection(desktop);
635 GSList const *items = (GSList *) selection->itemList();
636 if (!items) {
637 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise."));
638 return;
639 }
641 SPGroup const *group = sp_item_list_common_parent_group(items);
642 if (!group) {
643 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
644 return;
645 }
647 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
649 /* construct reverse-ordered list of selected children */
650 GSList *rev = g_slist_copy((GSList *) items);
651 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
653 // find out the common bbox of the selected items
654 NR::Maybe<NR::Rect> selected = enclose_items(items);
656 // for all objects in the selection (starting from top)
657 if (selected) {
658 while (rev) {
659 SPObject *child = SP_OBJECT(rev->data);
660 // for each selected object, find the next sibling
661 for (SPObject *newref = child->next; newref; newref = newref->next) {
662 // if the sibling is an item AND overlaps our selection,
663 if (SP_IS_ITEM(newref)) {
664 NR::Maybe<NR::Rect> newref_bbox = sp_item_bbox_desktop(SP_ITEM(newref));
665 if ( newref_bbox && selected->intersects(*newref_bbox) ) {
666 // AND if it's not one of our selected objects,
667 if (!g_slist_find((GSList *) items, newref)) {
668 // move the selected object after that sibling
669 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(newref));
670 }
671 break;
672 }
673 }
674 }
675 rev = g_slist_remove(rev, child);
676 }
677 } else {
678 g_slist_free(rev);
679 }
681 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_RAISE,
682 _("Raise"));
683 }
685 void sp_selection_raise_to_top()
686 {
687 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
688 if (desktop == NULL)
689 return;
691 SPDocument *document = sp_desktop_document(desktop);
692 Inkscape::Selection *selection = sp_desktop_selection(desktop);
694 if (selection->isEmpty()) {
695 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise to top."));
696 return;
697 }
699 GSList const *items = (GSList *) selection->itemList();
701 SPGroup const *group = sp_item_list_common_parent_group(items);
702 if (!group) {
703 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
704 return;
705 }
707 GSList *rl = g_slist_copy((GSList *) selection->reprList());
708 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
710 for (GSList *l = rl; l != NULL; l = l->next) {
711 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
712 repr->setPosition(-1);
713 }
715 g_slist_free(rl);
717 sp_document_done(document, SP_VERB_SELECTION_TO_FRONT,
718 _("Raise to top"));
719 }
721 void
722 sp_selection_lower()
723 {
724 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
725 if (desktop == NULL)
726 return;
728 Inkscape::Selection *selection = sp_desktop_selection(desktop);
730 GSList const *items = (GSList *) selection->itemList();
731 if (!items) {
732 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower."));
733 return;
734 }
736 SPGroup const *group = sp_item_list_common_parent_group(items);
737 if (!group) {
738 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
739 return;
740 }
742 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
744 // find out the common bbox of the selected items
745 NR::Maybe<NR::Rect> selected = enclose_items(items);
747 /* construct direct-ordered list of selected children */
748 GSList *rev = g_slist_copy((GSList *) items);
749 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
750 rev = g_slist_reverse(rev);
752 // for all objects in the selection (starting from top)
753 if (selected) {
754 while (rev) {
755 SPObject *child = SP_OBJECT(rev->data);
756 // for each selected object, find the prev sibling
757 for (SPObject *newref = prev_sibling(child); newref; newref = prev_sibling(newref)) {
758 // if the sibling is an item AND overlaps our selection,
759 if (SP_IS_ITEM(newref)) {
760 NR::Maybe<NR::Rect> ref_bbox = sp_item_bbox_desktop(SP_ITEM(newref));
761 if ( ref_bbox && selected->intersects(*ref_bbox) ) {
762 // AND if it's not one of our selected objects,
763 if (!g_slist_find((GSList *) items, newref)) {
764 // move the selected object before that sibling
765 SPObject *put_after = prev_sibling(newref);
766 if (put_after)
767 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(put_after));
768 else
769 SP_OBJECT_REPR(child)->setPosition(0);
770 }
771 break;
772 }
773 }
774 }
775 rev = g_slist_remove(rev, child);
776 }
777 } else {
778 g_slist_free(rev);
779 }
781 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_LOWER,
782 _("Lower"));
783 }
785 void sp_selection_lower_to_bottom()
786 {
787 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
788 if (desktop == NULL)
789 return;
791 SPDocument *document = sp_desktop_document(desktop);
792 Inkscape::Selection *selection = sp_desktop_selection(desktop);
794 if (selection->isEmpty()) {
795 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower to bottom."));
796 return;
797 }
799 GSList const *items = (GSList *) selection->itemList();
801 SPGroup const *group = sp_item_list_common_parent_group(items);
802 if (!group) {
803 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
804 return;
805 }
807 GSList *rl;
808 rl = g_slist_copy((GSList *) selection->reprList());
809 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
810 rl = g_slist_reverse(rl);
812 for (GSList *l = rl; l != NULL; l = l->next) {
813 gint minpos;
814 SPObject *pp, *pc;
815 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
816 pp = document->getObjectByRepr(sp_repr_parent(repr));
817 minpos = 0;
818 g_assert(SP_IS_GROUP(pp));
819 pc = sp_object_first_child(pp);
820 while (!SP_IS_ITEM(pc)) {
821 minpos += 1;
822 pc = pc->next;
823 }
824 repr->setPosition(minpos);
825 }
827 g_slist_free(rl);
829 sp_document_done(document, SP_VERB_SELECTION_TO_BACK,
830 _("Lower to bottom"));
831 }
833 void
834 sp_undo(SPDesktop *desktop, SPDocument *)
835 {
836 if (!sp_document_undo(sp_desktop_document(desktop)))
837 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to undo."));
838 }
840 void
841 sp_redo(SPDesktop *desktop, SPDocument *)
842 {
843 if (!sp_document_redo(sp_desktop_document(desktop)))
844 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to redo."));
845 }
847 void sp_selection_cut()
848 {
849 sp_selection_copy();
850 sp_selection_delete();
851 }
853 void sp_copy_gradient (GSList **defs_clip, SPGradient *gradient, Inkscape::XML::Document* xml_doc)
854 {
855 SPGradient *ref = gradient;
857 while (ref) {
858 // climb up the refs, copying each one in the chain
859 Inkscape::XML::Node *grad_repr = SP_OBJECT_REPR(ref)->duplicate(xml_doc);
860 *defs_clip = g_slist_prepend (*defs_clip, grad_repr);
862 ref = ref->ref->getObject();
863 }
864 }
866 void sp_copy_pattern (GSList **defs_clip, SPPattern *pattern, Inkscape::XML::Document* xml_doc)
867 {
868 SPPattern *ref = pattern;
870 while (ref) {
871 // climb up the refs, copying each one in the chain
872 Inkscape::XML::Node *pattern_repr = SP_OBJECT_REPR(ref)->duplicate(xml_doc);
873 *defs_clip = g_slist_prepend (*defs_clip, pattern_repr);
875 // items in the pattern may also use gradients and other patterns, so we need to recurse here as well
876 for (SPObject *child = sp_object_first_child(SP_OBJECT(ref)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
877 if (!SP_IS_ITEM (child))
878 continue;
879 sp_copy_stuff_used_by_item (defs_clip, (SPItem *) child, NULL, xml_doc);
880 }
882 ref = ref->ref->getObject();
883 }
884 }
886 void sp_copy_single (GSList **defs_clip, SPObject *thing, Inkscape::XML::Document* xml_doc)
887 {
888 Inkscape::XML::Node *duplicate_repr = SP_OBJECT_REPR(thing)->duplicate(xml_doc);
889 *defs_clip = g_slist_prepend (*defs_clip, duplicate_repr);
890 }
893 void sp_copy_textpath_path (GSList **defs_clip, SPTextPath *tp, const GSList *items, Inkscape::XML::Document* xml_doc)
894 {
895 SPItem *path = sp_textpath_get_path_item (tp);
896 if (!path)
897 return;
898 if (items && g_slist_find ((GSList *) items, path)) // do not copy it to defs if it is already in the list of items copied
899 return;
900 Inkscape::XML::Node *repr = SP_OBJECT_REPR(path)->duplicate(xml_doc);
901 *defs_clip = g_slist_prepend (*defs_clip, repr);
902 }
904 /**
905 * Copies things like patterns, markers, gradients, etc.
906 */
907 void sp_copy_stuff_used_by_item (GSList **defs_clip, SPItem *item, const GSList *items, Inkscape::XML::Document* xml_doc)
908 {
909 SPStyle *style = SP_OBJECT_STYLE (item);
911 if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
912 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
913 if (SP_IS_LINEARGRADIENT (server) || SP_IS_RADIALGRADIENT (server))
914 sp_copy_gradient (defs_clip, SP_GRADIENT(server), xml_doc);
915 if (SP_IS_PATTERN (server))
916 sp_copy_pattern (defs_clip, SP_PATTERN(server), xml_doc);
917 }
919 if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
920 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
921 if (SP_IS_LINEARGRADIENT (server) || SP_IS_RADIALGRADIENT (server))
922 sp_copy_gradient (defs_clip, SP_GRADIENT(server), xml_doc);
923 if (SP_IS_PATTERN (server))
924 sp_copy_pattern (defs_clip, SP_PATTERN(server), xml_doc);
925 }
927 // For shapes, copy all of the shape's markers into defs_clip
928 if (SP_IS_SHAPE (item)) {
929 SPShape *shape = SP_SHAPE (item);
930 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
931 if (shape->marker[i]) {
932 sp_copy_single (defs_clip, SP_OBJECT (shape->marker[i]), xml_doc);
933 }
934 }
935 }
937 if (SP_IS_TEXT_TEXTPATH (item)) {
938 sp_copy_textpath_path (defs_clip, SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))), items, xml_doc);
939 }
941 if (item->clip_ref->getObject()) {
942 sp_copy_single (defs_clip, item->clip_ref->getObject(), xml_doc);
943 }
945 if (item->mask_ref->getObject()) {
946 SPObject *mask = item->mask_ref->getObject();
947 sp_copy_single (defs_clip, mask, xml_doc);
948 // recurse into the mask for its gradients etc.
949 for (SPObject *o = SP_OBJECT(mask)->children; o != NULL; o = o->next) {
950 if (SP_IS_ITEM(o))
951 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (o), items, xml_doc);
952 }
953 }
955 if (style->filter.filter) {
956 SPObject *filter = style->filter.filter;
957 if (SP_IS_FILTER(filter)) {
958 sp_copy_single (defs_clip, filter, xml_doc);
959 }
960 }
962 // recurse
963 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
964 if (SP_IS_ITEM(o))
965 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (o), items, xml_doc);
966 }
967 }
969 /**
970 * \pre item != NULL
971 */
972 SPCSSAttr *
973 take_style_from_item (SPItem *item)
974 {
975 // write the complete cascaded style, context-free
976 SPCSSAttr *css = sp_css_attr_from_object (SP_OBJECT(item), SP_STYLE_FLAG_ALWAYS);
977 if (css == NULL)
978 return NULL;
980 if ((SP_IS_GROUP(item) && SP_OBJECT(item)->children) ||
981 (SP_IS_TEXT (item) && SP_OBJECT(item)->children && SP_OBJECT(item)->children->next == NULL)) {
982 // if this is a text with exactly one tspan child, merge the style of that tspan as well
983 // If this is a group, merge the style of its topmost (last) child with style
984 for (SPObject *last_element = item->lastChild(); last_element != NULL; last_element = SP_OBJECT_PREV (last_element)) {
985 if (SP_OBJECT_STYLE (last_element) != NULL) {
986 SPCSSAttr *temp = sp_css_attr_from_object (last_element, SP_STYLE_FLAG_IFSET);
987 if (temp) {
988 sp_repr_css_merge (css, temp);
989 sp_repr_css_attr_unref (temp);
990 }
991 break;
992 }
993 }
994 }
995 if (!(SP_IS_TEXT (item) || SP_IS_TSPAN (item) || SP_IS_STRING (item))) {
996 // do not copy text properties from non-text objects, it's confusing
997 css = sp_css_attr_unset_text (css);
998 }
1000 // FIXME: also transform gradient/pattern fills, by forking? NO, this must be nondestructive
1001 double ex = NR::expansion(sp_item_i2doc_affine(item));
1002 if (ex != 1.0) {
1003 css = sp_css_attr_scale (css, ex);
1004 }
1006 return css;
1007 }
1010 void sp_selection_copy()
1011 {
1012 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1013 if (desktop == NULL)
1014 return;
1016 if (!clipboard_document) {
1017 clipboard_document = new Inkscape::XML::SimpleDocument();
1018 }
1020 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1022 if (tools_isactive (desktop, TOOLS_DROPPER)) {
1023 sp_dropper_context_copy(desktop->event_context);
1024 return; // copied color under cursor, nothing else to do
1025 }
1027 // check if something is selected
1028 if (selection->isEmpty()) {
1029 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing was copied."));
1030 return;
1031 }
1033 const GSList *items = g_slist_copy ((GSList *) selection->itemList());
1035 // 0. Copy text to system clipboard
1036 // FIXME: for non-texts, put serialized Inkscape::XML as text to the clipboard;
1037 //for this sp_repr_write_stream needs to be rewritten with iostream instead of FILE
1038 Glib::ustring text;
1039 if (tools_isactive (desktop, TOOLS_TEXT)) {
1040 text = sp_text_get_selected_text(desktop->event_context);
1041 }
1043 if (text.empty()) {
1044 guint texts = 0;
1045 for (GSList *i = (GSList *) items; i; i = i->next) {
1046 SPItem *item = SP_ITEM (i->data);
1047 if (SP_IS_TEXT (item) || SP_IS_FLOWTEXT(item)) {
1048 if (texts > 0) // if more than one text object is copied, separate them by spaces
1049 text += " ";
1050 gchar *this_text = sp_te_get_string_multiline (item);
1051 if (this_text) {
1052 text += this_text;
1053 g_free(this_text);
1054 }
1055 texts++;
1056 }
1057 }
1058 }
1059 if (!text.empty()) {
1060 Glib::RefPtr<Gtk::Clipboard> refClipboard = Gtk::Clipboard::get();
1061 refClipboard->set_text(text);
1062 }
1064 // clear old defs clipboard
1065 while (defs_clipboard) {
1066 Inkscape::GC::release((Inkscape::XML::Node *) defs_clipboard->data);
1067 defs_clipboard = g_slist_remove (defs_clipboard, defs_clipboard->data);
1068 }
1070 // clear style clipboard
1071 if (style_clipboard) {
1072 sp_repr_css_attr_unref (style_clipboard);
1073 style_clipboard = NULL;
1074 }
1076 //clear main clipboard
1077 while (clipboard) {
1078 Inkscape::GC::release((Inkscape::XML::Node *) clipboard->data);
1079 clipboard = g_slist_remove(clipboard, clipboard->data);
1080 }
1082 sp_selection_copy_impl (items, &clipboard, &defs_clipboard, &style_clipboard, clipboard_document);
1084 if (tools_isactive (desktop, TOOLS_TEXT)) { // take style from cursor/text selection, overwriting the style just set by copy_impl
1085 SPStyle *const query = sp_style_new();
1086 if (sp_desktop_query_style_all (desktop, query)) {
1087 SPCSSAttr *css = sp_css_attr_from_style (query, SP_STYLE_FLAG_ALWAYS);
1088 if (css != NULL) {
1089 // clear style clipboard
1090 if (style_clipboard) {
1091 sp_repr_css_attr_unref (style_clipboard);
1092 style_clipboard = NULL;
1093 }
1094 //sp_repr_css_print (css);
1095 style_clipboard = css;
1096 }
1097 }
1098 sp_style_unref(query);
1099 }
1101 size_clipboard = selection->bounds();
1103 g_slist_free ((GSList *) items);
1104 }
1106 void sp_selection_paste(bool in_place)
1107 {
1108 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1110 if (desktop == NULL) {
1111 return;
1112 }
1114 SPDocument *document = sp_desktop_document(desktop);
1116 if (Inkscape::have_viable_layer(desktop, desktop->messageStack()) == false) {
1117 return;
1118 }
1120 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1122 if (tools_isactive (desktop, TOOLS_TEXT)) {
1123 if (sp_text_paste_inline(desktop->event_context))
1124 return; // pasted from system clipboard into text, nothing else to do
1125 }
1127 // check if something is in the clipboard
1128 if (clipboard == NULL) {
1129 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1130 return;
1131 }
1133 GSList *copied = sp_selection_paste_impl(document, desktop->currentLayer(), &clipboard, &defs_clipboard);
1134 // add pasted objects to selection
1135 selection->setReprList((GSList const *) copied);
1136 g_slist_free (copied);
1138 if (!in_place) {
1139 sp_document_ensure_up_to_date(document);
1141 NR::Maybe<NR::Rect> sel_bbox = selection->bounds();
1142 NR::Point m( desktop->point() );
1143 if (sel_bbox) {
1144 m -= sel_bbox->midpoint();
1145 }
1147 /* Snap the offset of the new item(s) to the grid */
1148 SnapManager &sm = desktop->namedview->snap_manager;
1149 SnapManager::SnapperList gs = sm.getGridSnappers();
1150 m = sm.freeSnapAlways(Inkscape::Snapper::SNAP_POINT, m, NULL, gs).getPoint();
1151 sp_selection_move_relative(selection, m);
1152 }
1154 sp_document_done(document, SP_VERB_EDIT_PASTE,
1155 _("Paste"));
1156 }
1158 void sp_selection_paste_style()
1159 {
1160 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1161 if (desktop == NULL) return;
1163 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1165 // check if something is in the clipboard
1166 if (clipboard == NULL) {
1167 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1168 return;
1169 }
1171 // check if something is selected
1172 if (selection->isEmpty()) {
1173 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste style to."));
1174 return;
1175 }
1177 paste_defs (&defs_clipboard, sp_desktop_document(desktop));
1179 sp_desktop_set_style (desktop, style_clipboard);
1181 sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_STYLE,
1182 _("Paste style"));
1183 }
1185 void sp_selection_paste_size (bool apply_x, bool apply_y)
1186 {
1187 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1188 if (desktop == NULL) return;
1190 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1192 // check if something is in the clipboard
1193 if (!size_clipboard) {
1194 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1195 return;
1196 }
1198 // check if something is selected
1199 if (selection->isEmpty()) {
1200 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste size to."));
1201 return;
1202 }
1204 NR::Maybe<NR::Rect> current = selection->bounds();
1205 if ( !current || current->isEmpty() ) {
1206 return;
1207 }
1209 double scale_x = size_clipboard->extent(NR::X) / current->extent(NR::X);
1210 double scale_y = size_clipboard->extent(NR::Y) / current->extent(NR::Y);
1212 sp_selection_scale_relative (selection, current->midpoint(),
1213 NR::scale(
1214 apply_x? scale_x : (desktop->isToolboxButtonActive ("lock")? scale_y : 1.0),
1215 apply_y? scale_y : (desktop->isToolboxButtonActive ("lock")? scale_x : 1.0)));
1217 sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_SIZE,
1218 _("Paste size"));
1219 }
1221 void sp_selection_paste_size_separately (bool apply_x, bool apply_y)
1222 {
1223 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1224 if (desktop == NULL) return;
1226 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1228 // check if something is in the clipboard
1229 if ( !size_clipboard ) {
1230 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1231 return;
1232 }
1234 // check if something is selected
1235 if (selection->isEmpty()) {
1236 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste size to."));
1237 return;
1238 }
1240 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1241 SPItem *item = SP_ITEM(l->data);
1243 NR::Maybe<NR::Rect> current = sp_item_bbox_desktop(item);
1244 if ( !current || current->isEmpty() ) {
1245 continue;
1246 }
1248 double scale_x = size_clipboard->extent(NR::X) / current->extent(NR::X);
1249 double scale_y = size_clipboard->extent(NR::Y) / current->extent(NR::Y);
1251 sp_item_scale_rel (item,
1252 NR::scale(
1253 apply_x? scale_x : (desktop->isToolboxButtonActive ("lock")? scale_y : 1.0),
1254 apply_y? scale_y : (desktop->isToolboxButtonActive ("lock")? scale_x : 1.0)));
1256 }
1258 sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_SIZE_SEPARATELY,
1259 _("Paste size separately"));
1260 }
1262 void sp_selection_to_next_layer ()
1263 {
1264 SPDesktop *dt = SP_ACTIVE_DESKTOP;
1266 Inkscape::Selection *selection = sp_desktop_selection(dt);
1268 // check if something is selected
1269 if (selection->isEmpty()) {
1270 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer above."));
1271 return;
1272 }
1274 const GSList *items = g_slist_copy ((GSList *) selection->itemList());
1276 bool no_more = false; // Set to true, if no more layers above
1277 SPObject *next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer());
1278 if (next) {
1279 GSList *temp_clip = NULL;
1280 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
1281 sp_selection_delete_impl (items);
1282 next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
1283 GSList *copied;
1284 if(next) {
1285 copied = sp_selection_paste_impl (sp_desktop_document (dt), next, &temp_clip, NULL);
1286 } else {
1287 copied = sp_selection_paste_impl (sp_desktop_document (dt), dt->currentLayer(), &temp_clip, NULL);
1288 no_more = true;
1289 }
1290 selection->setReprList((GSList const *) copied);
1291 g_slist_free (copied);
1292 if (temp_clip) g_slist_free (temp_clip);
1293 if (next) dt->setCurrentLayer(next);
1294 sp_document_done(sp_desktop_document (dt), SP_VERB_LAYER_MOVE_TO_NEXT,
1295 _("Raise to next layer"));
1296 } else {
1297 no_more = true;
1298 }
1300 if (no_more) {
1301 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers above."));
1302 }
1304 g_slist_free ((GSList *) items);
1305 }
1307 void sp_selection_to_prev_layer ()
1308 {
1309 SPDesktop *dt = SP_ACTIVE_DESKTOP;
1311 Inkscape::Selection *selection = sp_desktop_selection(dt);
1313 // check if something is selected
1314 if (selection->isEmpty()) {
1315 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer below."));
1316 return;
1317 }
1319 const GSList *items = g_slist_copy ((GSList *) selection->itemList());
1321 bool no_more = false; // Set to true, if no more layers below
1322 SPObject *next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer());
1323 if (next) {
1324 GSList *temp_clip = NULL;
1325 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
1326 sp_selection_delete_impl (items);
1327 next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
1328 GSList *copied;
1329 if(next) {
1330 copied = sp_selection_paste_impl (sp_desktop_document (dt), next, &temp_clip, NULL);
1331 } else {
1332 copied = sp_selection_paste_impl (sp_desktop_document (dt), dt->currentLayer(), &temp_clip, NULL);
1333 no_more = true;
1334 }
1335 selection->setReprList((GSList const *) copied);
1336 g_slist_free (copied);
1337 if (temp_clip) g_slist_free (temp_clip);
1338 if (next) dt->setCurrentLayer(next);
1339 sp_document_done(sp_desktop_document (dt), SP_VERB_LAYER_MOVE_TO_PREV,
1340 _("Lower to previous layer"));
1341 } else {
1342 no_more = true;
1343 }
1345 if (no_more) {
1346 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers below."));
1347 }
1349 g_slist_free ((GSList *) items);
1350 }
1352 bool
1353 selection_contains_original (SPItem *item, Inkscape::Selection *selection)
1354 {
1355 bool contains_original = false;
1356 bool is_use = SP_IS_USE(item);
1357 SPItem *item_use = item;
1358 SPItem *item_use_first = item;
1359 while (is_use && item_use && !contains_original)
1360 {
1361 item_use = sp_use_get_original (SP_USE(item_use));
1362 contains_original |= selection->includes(item_use);
1363 if (item_use == item_use_first)
1364 break;
1365 is_use = SP_IS_USE(item_use);
1366 }
1367 return contains_original;
1368 }
1371 bool
1372 selection_contains_both_clone_and_original (Inkscape::Selection *selection)
1373 {
1374 bool clone_with_original = false;
1375 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1376 SPItem *item = SP_ITEM(l->data);
1377 clone_with_original |= selection_contains_original(item, selection);
1378 if (clone_with_original)
1379 break;
1380 }
1381 return clone_with_original;
1382 }
1385 /** Apply matrix to the selection. \a set_i2d is normally true, which means objects are in the
1386 original transform, synced with their reprs, and need to jump to the new transform in one go. A
1387 value of set_i2d==false is only used by seltrans when it's dragging objects live (not outlines); in
1388 that case, items are already in the new position, but the repr is in the old, and this function
1389 then simply updates the repr from item->transform.
1390 */
1391 void sp_selection_apply_affine(Inkscape::Selection *selection, NR::Matrix const &affine, bool set_i2d)
1392 {
1393 if (selection->isEmpty())
1394 return;
1396 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1397 SPItem *item = SP_ITEM(l->data);
1399 NR::Point old_center(0,0);
1400 if (set_i2d && item->isCenterSet())
1401 old_center = item->getCenter();
1403 #if 0 /* Re-enable this once persistent guides have a graphical indication.
1404 At the time of writing, this is the only place to re-enable. */
1405 sp_item_update_cns(*item, selection->desktop());
1406 #endif
1408 // we're moving both a clone and its original or any ancestor in clone chain?
1409 bool transform_clone_with_original = selection_contains_original(item, selection);
1410 // ...both a text-on-path and its path?
1411 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)))) ));
1412 // ...both a flowtext and its frame?
1413 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)
1414 // ...both an offset and its source?
1415 bool transform_offset_with_source = (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) && selection->includes( sp_offset_get_source (SP_OFFSET(item)) );
1417 // If we're moving a connector, we want to detach it
1418 // from shapes that aren't part of the selection, but
1419 // leave it attached if they are
1420 if (cc_item_is_connector(item)) {
1421 SPItem *attItem[2];
1422 SP_PATH(item)->connEndPair.getAttachedItems(attItem);
1424 for (int n = 0; n < 2; ++n) {
1425 if (!selection->includes(attItem[n])) {
1426 sp_conn_end_detach(item, n);
1427 }
1428 }
1429 }
1431 // "clones are unmoved when original is moved" preference
1432 int compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
1433 bool prefs_unmoved = (compensation == SP_CLONE_COMPENSATION_UNMOVED);
1434 bool prefs_parallel = (compensation == SP_CLONE_COMPENSATION_PARALLEL);
1436 // If this is a clone and it's selected along with its original, do not move it; it will feel the
1437 // transform of its original and respond to it itself. Without this, a clone is doubly
1438 // transformed, very unintuitive.
1439 // Same for textpath if we are also doing ANY transform to its path: do not touch textpath,
1440 // letters cannot be squeezed or rotated anyway, they only refill the changed path.
1441 // Same for linked offset if we are also moving its source: do not move it.
1442 if (transform_textpath_with_path || transform_offset_with_source) {
1443 // restore item->transform field from the repr, in case it was changed by seltrans
1444 sp_object_read_attr (SP_OBJECT (item), "transform");
1446 } else if (transform_flowtext_with_frame) {
1447 // apply the inverse of the region's transform to the <use> so that the flow remains
1448 // the same (even though the output itself gets transformed)
1449 for (SPObject *region = item->firstChild() ; region ; region = SP_OBJECT_NEXT(region)) {
1450 if (!SP_IS_FLOWREGION(region) && !SP_IS_FLOWREGIONEXCLUDE(region))
1451 continue;
1452 for (SPObject *use = region->firstChild() ; use ; use = SP_OBJECT_NEXT(use)) {
1453 if (!SP_IS_USE(use)) continue;
1454 sp_item_write_transform(SP_USE(use), SP_OBJECT_REPR(use), item->transform.inverse(), NULL);
1455 }
1456 }
1457 } else if (transform_clone_with_original) {
1458 // We are transforming a clone along with its original. The below matrix juggling is
1459 // necessary to ensure that they transform as a whole, i.e. the clone's induced
1460 // transform and its move compensation are both cancelled out.
1462 // restore item->transform field from the repr, in case it was changed by seltrans
1463 sp_object_read_attr (SP_OBJECT (item), "transform");
1465 // calculate the matrix we need to apply to the clone to cancel its induced transform from its original
1466 NR::Matrix parent_transform = sp_item_i2root_affine(SP_ITEM(SP_OBJECT_PARENT (item)));
1467 NR::Matrix t = parent_transform * matrix_to_desktop (matrix_from_desktop (affine, item), item) * parent_transform.inverse();
1468 NR::Matrix t_inv =parent_transform * matrix_to_desktop (matrix_from_desktop (affine.inverse(), item), item) * parent_transform.inverse();
1469 NR::Matrix result = t_inv * item->transform * t;
1471 if ((prefs_parallel || prefs_unmoved) && affine.is_translation()) {
1472 // we need to cancel out the move compensation, too
1474 // find out the clone move, same as in sp_use_move_compensate
1475 NR::Matrix parent = sp_use_get_parent_transform (SP_USE(item));
1476 NR::Matrix clone_move = parent.inverse() * t * parent;
1478 if (prefs_parallel) {
1479 NR::Matrix move = result * clone_move * t_inv;
1480 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &move);
1482 } else if (prefs_unmoved) {
1483 //if (SP_IS_USE(sp_use_get_original(SP_USE(item))))
1484 // clone_move = NR::identity();
1485 NR::Matrix move = result * clone_move;
1486 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &t);
1487 }
1489 } else {
1490 // just apply the result
1491 sp_item_write_transform(item, SP_OBJECT_REPR(item), result, &t);
1492 }
1494 } else {
1495 if (set_i2d) {
1496 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * affine);
1497 }
1498 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, NULL);
1499 }
1501 // if we're moving the actual object, not just updating the repr, we can transform the
1502 // center by the same matrix (only necessary for non-translations)
1503 if (set_i2d && item->isCenterSet() && !affine.is_translation()) {
1504 item->setCenter(old_center * affine);
1505 SP_OBJECT(item)->updateRepr();
1506 }
1507 }
1508 }
1510 void sp_selection_remove_transform()
1511 {
1512 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1513 if (desktop == NULL)
1514 return;
1516 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1518 GSList const *l = (GSList *) selection->reprList();
1519 while (l != NULL) {
1520 sp_repr_set_attr((Inkscape::XML::Node*)l->data, "transform", NULL);
1521 l = l->next;
1522 }
1524 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_FLATTEN,
1525 _("Remove transform"));
1526 }
1528 void
1529 sp_selection_scale_absolute(Inkscape::Selection *selection,
1530 double const x0, double const x1,
1531 double const y0, double const y1)
1532 {
1533 if (selection->isEmpty())
1534 return;
1536 NR::Maybe<NR::Rect> const bbox(selection->bounds());
1537 if ( !bbox || bbox->isEmpty() ) {
1538 return;
1539 }
1541 NR::translate const p2o(-bbox->min());
1543 NR::scale const newSize(x1 - x0,
1544 y1 - y0);
1545 NR::scale const scale( newSize / NR::scale(bbox->dimensions()) );
1546 NR::translate const o2n(x0, y0);
1547 NR::Matrix const final( p2o * scale * o2n );
1549 sp_selection_apply_affine(selection, final);
1550 }
1553 void sp_selection_scale_relative(Inkscape::Selection *selection, NR::Point const &align, NR::scale const &scale)
1554 {
1555 if (selection->isEmpty())
1556 return;
1558 NR::Maybe<NR::Rect> const bbox(selection->bounds());
1560 if ( !bbox || bbox->isEmpty() ) {
1561 return;
1562 }
1564 // FIXME: ARBITRARY LIMIT: don't try to scale above 1 Mpx, it won't display properly and will crash sooner or later anyway
1565 if ( bbox->extent(NR::X) * scale[NR::X] > 1e6 ||
1566 bbox->extent(NR::Y) * scale[NR::Y] > 1e6 )
1567 {
1568 return;
1569 }
1571 NR::translate const n2d(-align);
1572 NR::translate const d2n(align);
1573 NR::Matrix const final( n2d * scale * d2n );
1574 sp_selection_apply_affine(selection, final);
1575 }
1577 void
1578 sp_selection_rotate_relative(Inkscape::Selection *selection, NR::Point const ¢er, gdouble const angle_degrees)
1579 {
1580 NR::translate const d2n(center);
1581 NR::translate const n2d(-center);
1582 NR::rotate const rotate(rotate_degrees(angle_degrees));
1583 NR::Matrix const final( NR::Matrix(n2d) * rotate * d2n );
1584 sp_selection_apply_affine(selection, final);
1585 }
1587 void
1588 sp_selection_skew_relative(Inkscape::Selection *selection, NR::Point const &align, double dx, double dy)
1589 {
1590 NR::translate const d2n(align);
1591 NR::translate const n2d(-align);
1592 NR::Matrix const skew(1, dy,
1593 dx, 1,
1594 0, 0);
1595 NR::Matrix const final( n2d * skew * d2n );
1596 sp_selection_apply_affine(selection, final);
1597 }
1599 void sp_selection_move_relative(Inkscape::Selection *selection, NR::Point const &move)
1600 {
1601 sp_selection_apply_affine(selection, NR::Matrix(NR::translate(move)));
1602 }
1604 void sp_selection_move_relative(Inkscape::Selection *selection, double dx, double dy)
1605 {
1606 sp_selection_apply_affine(selection, NR::Matrix(NR::translate(dx, dy)));
1607 }
1610 /**
1611 * \brief sp_selection_rotate_90
1612 *
1613 * This function rotates selected objects 90 degrees clockwise.
1614 *
1615 */
1617 void sp_selection_rotate_90_cw()
1618 {
1619 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1621 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1623 if (selection->isEmpty())
1624 return;
1626 GSList const *l = selection->itemList();
1627 NR::rotate const rot_neg_90(NR::Point(0, -1));
1628 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1629 SPItem *item = SP_ITEM(l2->data);
1630 sp_item_rotate_rel(item, rot_neg_90);
1631 }
1633 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_ROTATE_90_CCW,
1634 _("Rotate 90° CW"));
1635 }
1638 /**
1639 * \brief sp_selection_rotate_90_ccw
1640 *
1641 * This function rotates selected objects 90 degrees counter-clockwise.
1642 *
1643 */
1645 void sp_selection_rotate_90_ccw()
1646 {
1647 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1649 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1651 if (selection->isEmpty())
1652 return;
1654 GSList const *l = selection->itemList();
1655 NR::rotate const rot_neg_90(NR::Point(0, 1));
1656 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1657 SPItem *item = SP_ITEM(l2->data);
1658 sp_item_rotate_rel(item, rot_neg_90);
1659 }
1661 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_ROTATE_90_CW,
1662 _("Rotate 90° CCW"));
1663 }
1665 void
1666 sp_selection_rotate(Inkscape::Selection *selection, gdouble const angle_degrees)
1667 {
1668 if (selection->isEmpty())
1669 return;
1671 NR::Maybe<NR::Point> center = selection->center();
1672 if (!center) {
1673 return;
1674 }
1676 sp_selection_rotate_relative(selection, *center, angle_degrees);
1678 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1679 ( ( angle_degrees > 0 )
1680 ? "selector:rotate:ccw"
1681 : "selector:rotate:cw" ),
1682 SP_VERB_CONTEXT_SELECT,
1683 _("Rotate"));
1684 }
1686 /**
1687 \param angle the angle in "angular pixels", i.e. how many visible pixels must move the outermost point of the rotated object
1688 */
1689 void
1690 sp_selection_rotate_screen(Inkscape::Selection *selection, gdouble angle)
1691 {
1692 if (selection->isEmpty())
1693 return;
1695 NR::Maybe<NR::Rect> const bbox(selection->bounds());
1696 NR::Maybe<NR::Point> center = selection->center();
1698 if ( !bbox || !center ) {
1699 return;
1700 }
1702 gdouble const zoom = selection->desktop()->current_zoom();
1703 gdouble const zmove = angle / zoom;
1704 gdouble const r = NR::L2(bbox->max() - *center);
1706 gdouble const zangle = 180 * atan2(zmove, r) / M_PI;
1708 sp_selection_rotate_relative(selection, *center, zangle);
1710 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1711 ( (angle > 0)
1712 ? "selector:rotate:ccw"
1713 : "selector:rotate:cw" ),
1714 SP_VERB_CONTEXT_SELECT,
1715 _("Rotate by pixels"));
1716 }
1718 void
1719 sp_selection_scale(Inkscape::Selection *selection, gdouble grow)
1720 {
1721 if (selection->isEmpty())
1722 return;
1724 NR::Maybe<NR::Rect> const bbox(selection->bounds());
1725 if (!bbox) {
1726 return;
1727 }
1729 NR::Point const center(bbox->midpoint());
1731 // you can't scale "do nizhe pola" (below zero)
1732 double const max_len = bbox->maxExtent();
1733 if ( max_len + grow <= 1e-3 ) {
1734 return;
1735 }
1737 double const times = 1.0 + grow / max_len;
1738 sp_selection_scale_relative(selection, center, NR::scale(times, times));
1740 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1741 ( (grow > 0)
1742 ? "selector:scale:larger"
1743 : "selector:scale:smaller" ),
1744 SP_VERB_CONTEXT_SELECT,
1745 _("Scale"));
1746 }
1748 void
1749 sp_selection_scale_screen(Inkscape::Selection *selection, gdouble grow_pixels)
1750 {
1751 sp_selection_scale(selection,
1752 grow_pixels / selection->desktop()->current_zoom());
1753 }
1755 void
1756 sp_selection_scale_times(Inkscape::Selection *selection, gdouble times)
1757 {
1758 if (selection->isEmpty())
1759 return;
1761 NR::Maybe<NR::Rect> sel_bbox = selection->bounds();
1763 if (!sel_bbox) {
1764 return;
1765 }
1767 NR::Point const center(sel_bbox->midpoint());
1768 sp_selection_scale_relative(selection, center, NR::scale(times, times));
1769 sp_document_done(sp_desktop_document(selection->desktop()), SP_VERB_CONTEXT_SELECT,
1770 _("Scale by whole factor"));
1771 }
1773 void
1774 sp_selection_move(gdouble dx, gdouble dy)
1775 {
1776 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1777 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1778 if (selection->isEmpty()) {
1779 return;
1780 }
1782 sp_selection_move_relative(selection, dx, dy);
1784 if (dx == 0) {
1785 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical", SP_VERB_CONTEXT_SELECT,
1786 _("Move vertically"));
1787 } else if (dy == 0) {
1788 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal", SP_VERB_CONTEXT_SELECT,
1789 _("Move horizontally"));
1790 } else {
1791 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SELECT,
1792 _("Move"));
1793 }
1794 }
1796 void
1797 sp_selection_move_screen(gdouble dx, gdouble dy)
1798 {
1799 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1801 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1802 if (selection->isEmpty()) {
1803 return;
1804 }
1806 // same as sp_selection_move but divide deltas by zoom factor
1807 gdouble const zoom = desktop->current_zoom();
1808 gdouble const zdx = dx / zoom;
1809 gdouble const zdy = dy / zoom;
1810 sp_selection_move_relative(selection, zdx, zdy);
1812 if (dx == 0) {
1813 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical", SP_VERB_CONTEXT_SELECT,
1814 _("Move vertically by pixels"));
1815 } else if (dy == 0) {
1816 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal", SP_VERB_CONTEXT_SELECT,
1817 _("Move horizontally by pixels"));
1818 } else {
1819 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SELECT,
1820 _("Move"));
1821 }
1822 }
1824 namespace {
1826 template <typename D>
1827 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
1828 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
1830 template <typename D>
1831 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items, SPObject *root,
1832 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
1834 struct Forward {
1835 typedef SPObject *Iterator;
1837 static Iterator children(SPObject *o) { return sp_object_first_child(o); }
1838 static Iterator siblings_after(SPObject *o) { return SP_OBJECT_NEXT(o); }
1839 static void dispose(Iterator i) {}
1841 static SPObject *object(Iterator i) { return i; }
1842 static Iterator next(Iterator i) { return SP_OBJECT_NEXT(i); }
1843 };
1845 struct Reverse {
1846 typedef GSList *Iterator;
1848 static Iterator children(SPObject *o) {
1849 return make_list(o->firstChild(), NULL);
1850 }
1851 static Iterator siblings_after(SPObject *o) {
1852 return make_list(SP_OBJECT_PARENT(o)->firstChild(), o);
1853 }
1854 static void dispose(Iterator i) {
1855 g_slist_free(i);
1856 }
1858 static SPObject *object(Iterator i) {
1859 return reinterpret_cast<SPObject *>(i->data);
1860 }
1861 static Iterator next(Iterator i) { return i->next; }
1863 private:
1864 static GSList *make_list(SPObject *object, SPObject *limit) {
1865 GSList *list=NULL;
1866 while ( object != limit ) {
1867 list = g_slist_prepend(list, object);
1868 object = SP_OBJECT_NEXT(object);
1869 }
1870 return list;
1871 }
1872 };
1874 }
1876 void
1877 sp_selection_item_next(void)
1878 {
1879 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1880 g_return_if_fail(desktop != NULL);
1881 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1883 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
1884 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
1885 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
1887 SPObject *root;
1888 if (PREFS_SELECTION_ALL != inlayer) {
1889 root = selection->activeContext();
1890 } else {
1891 root = desktop->currentRoot();
1892 }
1894 SPItem *item=next_item_from_list<Forward>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1896 if (item) {
1897 selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
1898 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1899 scroll_to_show_item(desktop, item);
1900 }
1901 }
1902 }
1904 void
1905 sp_selection_item_prev(void)
1906 {
1907 SPDocument *document = SP_ACTIVE_DOCUMENT;
1908 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1909 g_return_if_fail(document != NULL);
1910 g_return_if_fail(desktop != NULL);
1911 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1913 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
1914 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
1915 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
1917 SPObject *root;
1918 if (PREFS_SELECTION_ALL != inlayer) {
1919 root = selection->activeContext();
1920 } else {
1921 root = desktop->currentRoot();
1922 }
1924 SPItem *item=next_item_from_list<Reverse>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1926 if (item) {
1927 selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
1928 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1929 scroll_to_show_item(desktop, item);
1930 }
1931 }
1932 }
1934 namespace {
1936 template <typename D>
1937 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items,
1938 SPObject *root, bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
1939 {
1940 SPObject *current=root;
1941 while (items) {
1942 SPItem *item=SP_ITEM(items->data);
1943 if ( root->isAncestorOf(item) &&
1944 ( !only_in_viewport || desktop->isWithinViewport(item) ) )
1945 {
1946 current = item;
1947 break;
1948 }
1949 items = items->next;
1950 }
1952 GSList *path=NULL;
1953 while ( current != root ) {
1954 path = g_slist_prepend(path, current);
1955 current = SP_OBJECT_PARENT(current);
1956 }
1958 SPItem *next;
1959 // first, try from the current object
1960 next = next_item<D>(desktop, path, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1961 g_slist_free(path);
1963 if (!next) { // if we ran out of objects, start over at the root
1964 next = next_item<D>(desktop, NULL, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1965 }
1967 return next;
1968 }
1970 template <typename D>
1971 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
1972 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
1973 {
1974 typename D::Iterator children;
1975 typename D::Iterator iter;
1977 SPItem *found=NULL;
1979 if (path) {
1980 SPObject *object=reinterpret_cast<SPObject *>(path->data);
1981 g_assert(SP_OBJECT_PARENT(object) == root);
1982 if (desktop->isLayer(object)) {
1983 found = next_item<D>(desktop, path->next, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1984 }
1985 iter = children = D::siblings_after(object);
1986 } else {
1987 iter = children = D::children(root);
1988 }
1990 while ( iter && !found ) {
1991 SPObject *object=D::object(iter);
1992 if (desktop->isLayer(object)) {
1993 if (PREFS_SELECTION_LAYER != inlayer) { // recurse into sublayers
1994 found = next_item<D>(desktop, NULL, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1995 }
1996 } else if ( SP_IS_ITEM(object) &&
1997 ( !only_in_viewport || desktop->isWithinViewport(SP_ITEM(object)) ) &&
1998 ( !onlyvisible || !desktop->itemIsHidden(SP_ITEM(object))) &&
1999 ( !onlysensitive || !SP_ITEM(object)->isLocked()) &&
2000 !desktop->isLayer(SP_ITEM(object)) )
2001 {
2002 found = SP_ITEM(object);
2003 }
2004 iter = D::next(iter);
2005 }
2007 D::dispose(children);
2009 return found;
2010 }
2012 }
2014 /**
2015 * If \a item is not entirely visible then adjust visible area to centre on the centre on of
2016 * \a item.
2017 */
2018 void scroll_to_show_item(SPDesktop *desktop, SPItem *item)
2019 {
2020 NR::Rect dbox = desktop->get_display_area();
2021 NR::Maybe<NR::Rect> sbox = sp_item_bbox_desktop(item);
2023 if ( sbox && dbox.contains(*sbox) == false ) {
2024 NR::Point const s_dt = sbox->midpoint();
2025 NR::Point const s_w = desktop->d2w(s_dt);
2026 NR::Point const d_dt = dbox.midpoint();
2027 NR::Point const d_w = desktop->d2w(d_dt);
2028 NR::Point const moved_w( d_w - s_w );
2029 gint const dx = (gint) moved_w[X];
2030 gint const dy = (gint) moved_w[Y];
2031 desktop->scroll_world(dx, dy);
2032 }
2033 }
2036 void
2037 sp_selection_clone()
2038 {
2039 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2040 if (desktop == NULL)
2041 return;
2043 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2045 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
2047 // check if something is selected
2048 if (selection->isEmpty()) {
2049 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object</b> to clone."));
2050 return;
2051 }
2053 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
2055 selection->clear();
2057 // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
2058 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
2060 GSList *newsel = NULL;
2062 while (reprs) {
2063 Inkscape::XML::Node *sel_repr = (Inkscape::XML::Node *) reprs->data;
2064 Inkscape::XML::Node *parent = sp_repr_parent(sel_repr);
2066 Inkscape::XML::Node *clone = xml_doc->createElement("svg:use");
2067 sp_repr_set_attr(clone, "x", "0");
2068 sp_repr_set_attr(clone, "y", "0");
2069 sp_repr_set_attr(clone, "xlink:href", g_strdup_printf("#%s", sel_repr->attribute("id")));
2071 sp_repr_set_attr(clone, "inkscape:transform-center-x", sel_repr->attribute("inkscape:transform-center-x"));
2072 sp_repr_set_attr(clone, "inkscape:transform-center-y", sel_repr->attribute("inkscape:transform-center-y"));
2074 // add the new clone to the top of the original's parent
2075 parent->appendChild(clone);
2077 newsel = g_slist_prepend(newsel, clone);
2078 reprs = g_slist_remove(reprs, sel_repr);
2079 Inkscape::GC::release(clone);
2080 }
2082 // TRANSLATORS: only translate "string" in "context|string".
2083 // For more details, see http://developer.gnome.org/doc/API/2.0/glib/glib-I18N.html#Q-:CAPS
2084 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_CLONE,
2085 Q_("action|Clone"));
2087 selection->setReprList(newsel);
2089 g_slist_free(newsel);
2090 }
2092 void
2093 sp_selection_unlink()
2094 {
2095 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2096 if (!desktop)
2097 return;
2099 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2101 if (selection->isEmpty()) {
2102 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>clone</b> to unlink."));
2103 return;
2104 }
2106 // Get a copy of current selection.
2107 GSList *new_select = NULL;
2108 bool unlinked = false;
2109 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2110 items != NULL;
2111 items = items->next)
2112 {
2113 SPItem *use = (SPItem *) items->data;
2115 if (!SP_IS_USE(use)) {
2116 // keep the non-yse item in the new selection
2117 new_select = g_slist_prepend(new_select, use);
2118 continue;
2119 }
2121 SPItem *unlink = sp_use_unlink(SP_USE(use));
2122 unlinked = true;
2123 // Add ungrouped items to the new selection.
2124 new_select = g_slist_prepend(new_select, unlink);
2125 }
2127 if (new_select) { // set new selection
2128 selection->clear();
2129 selection->setList(new_select);
2130 g_slist_free(new_select);
2131 }
2132 if (!unlinked) {
2133 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No clones to unlink</b> in the selection."));
2134 }
2136 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNLINK_CLONE,
2137 _("Unlink clone"));
2138 }
2140 void
2141 sp_select_clone_original()
2142 {
2143 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2144 if (desktop == NULL)
2145 return;
2147 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2149 SPItem *item = selection->singleItem();
2151 const gchar *error = _("Select a <b>clone</b> to go to its original. Select a <b>linked offset</b> to go to its source. Select a <b>text on path</b> to go to the path. Select a <b>flowed text</b> to go to its frame.");
2153 // Check if other than two objects are selected
2154 if (g_slist_length((GSList *) selection->itemList()) != 1 || !item) {
2155 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
2156 return;
2157 }
2159 SPItem *original = NULL;
2160 if (SP_IS_USE(item)) {
2161 original = sp_use_get_original (SP_USE(item));
2162 } else if (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) {
2163 original = sp_offset_get_source (SP_OFFSET(item));
2164 } else if (SP_IS_TEXT_TEXTPATH(item)) {
2165 original = sp_textpath_get_path_item (SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))));
2166 } else if (SP_IS_FLOWTEXT(item)) {
2167 original = SP_FLOWTEXT(item)->get_frame (NULL); // first frame only
2168 } else { // it's an object that we don't know what to do with
2169 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
2170 return;
2171 }
2173 if (!original) {
2174 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>Cannot find</b> the object to select (orphaned clone, offset, textpath, flowed text?)"));
2175 return;
2176 }
2178 for (SPObject *o = original; o && !SP_IS_ROOT(o); o = SP_OBJECT_PARENT (o)) {
2179 if (SP_IS_DEFS (o)) {
2180 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("The object you're trying to select is <b>not visible</b> (it is in <defs>)"));
2181 return;
2182 }
2183 }
2185 if (original) {
2186 selection->clear();
2187 selection->set(original);
2188 if (SP_CYCLING == SP_CYCLE_FOCUS) {
2189 scroll_to_show_item(desktop, original);
2190 }
2191 }
2192 }
2194 void
2195 sp_selection_tile(bool apply)
2196 {
2197 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2198 if (desktop == NULL)
2199 return;
2201 SPDocument *doc = sp_desktop_document(desktop);
2202 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2204 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2206 // check if something is selected
2207 if (selection->isEmpty()) {
2208 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to pattern."));
2209 return;
2210 }
2212 sp_document_ensure_up_to_date(doc);
2213 NR::Maybe<NR::Rect> r = selection->bounds();
2214 if ( !r || r->isEmpty() ) {
2215 return;
2216 }
2218 // calculate the transform to be applied to objects to move them to 0,0
2219 NR::Point move_p = NR::Point(0, sp_document_height(doc)) - (r->min() + NR::Point (0, r->extent(NR::Y)));
2220 move_p[NR::Y] = -move_p[NR::Y];
2221 NR::Matrix move = NR::Matrix (NR::translate (move_p));
2223 GSList *items = g_slist_copy((GSList *) selection->itemList());
2225 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2227 // bottommost object, after sorting
2228 SPObject *parent = SP_OBJECT_PARENT (items->data);
2230 NR::Matrix parent_transform = sp_item_i2root_affine(SP_ITEM(parent));
2232 // remember the position of the first item
2233 gint pos = SP_OBJECT_REPR (items->data)->position();
2235 // create a list of duplicates
2236 GSList *repr_copies = NULL;
2237 for (GSList *i = items; i != NULL; i = i->next) {
2238 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2239 repr_copies = g_slist_prepend (repr_copies, dup);
2240 }
2242 NR::Rect bounds(desktop->dt2doc(r->min()), desktop->dt2doc(r->max()));
2244 if (apply) {
2245 // delete objects so that their clones don't get alerted; this object will be restored shortly
2246 for (GSList *i = items; i != NULL; i = i->next) {
2247 SPObject *item = SP_OBJECT (i->data);
2248 item->deleteObject (false);
2249 }
2250 }
2252 // Hack: Temporarily set clone compensation to unmoved, so that we can move clone-originals
2253 // without disturbing clones.
2254 // See ActorAlign::on_button_click() in src/ui/dialog/align-and-distribute.cpp
2255 int saved_compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2256 prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2258 const gchar *pat_id = pattern_tile (repr_copies, bounds, doc,
2259 NR::Matrix(NR::translate(desktop->dt2doc(NR::Point(r->min()[NR::X], r->max()[NR::Y])))) * parent_transform.inverse(), parent_transform * move);
2261 // restore compensation setting
2262 prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
2264 if (apply) {
2265 Inkscape::XML::Node *rect = xml_doc->createElement("svg:rect");
2266 rect->setAttribute("style", g_strdup_printf("stroke:none;fill:url(#%s)", pat_id));
2268 NR::Point min = bounds.min() * parent_transform.inverse();
2269 NR::Point max = bounds.max() * parent_transform.inverse();
2271 sp_repr_set_svg_double(rect, "width", max[NR::X] - min[NR::X]);
2272 sp_repr_set_svg_double(rect, "height", max[NR::Y] - min[NR::Y]);
2273 sp_repr_set_svg_double(rect, "x", min[NR::X]);
2274 sp_repr_set_svg_double(rect, "y", min[NR::Y]);
2276 // restore parent and position
2277 SP_OBJECT_REPR (parent)->appendChild(rect);
2278 rect->setPosition(pos > 0 ? pos : 0);
2279 SPItem *rectangle = (SPItem *) sp_desktop_document (desktop)->getObjectByRepr(rect);
2281 Inkscape::GC::release(rect);
2283 selection->clear();
2284 selection->set(rectangle);
2285 }
2287 g_slist_free (items);
2289 sp_document_done (doc, SP_VERB_EDIT_TILE,
2290 _("Objects to pattern"));
2291 }
2293 void
2294 sp_selection_untile()
2295 {
2296 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2297 if (desktop == NULL)
2298 return;
2300 SPDocument *doc = sp_desktop_document(desktop);
2301 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2303 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2305 // check if something is selected
2306 if (selection->isEmpty()) {
2307 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object with pattern fill</b> to extract objects from."));
2308 return;
2309 }
2311 GSList *new_select = NULL;
2313 bool did = false;
2315 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2316 items != NULL;
2317 items = items->next) {
2319 SPItem *item = (SPItem *) items->data;
2321 SPStyle *style = SP_OBJECT_STYLE (item);
2323 if (!style || style->fill.type != SP_PAINT_TYPE_PAINTSERVER)
2324 continue;
2326 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
2328 if (!SP_IS_PATTERN(server))
2329 continue;
2331 did = true;
2333 SPPattern *pattern = pattern_getroot (SP_PATTERN (server));
2335 NR::Matrix pat_transform = pattern_patternTransform (SP_PATTERN (server));
2336 pat_transform *= item->transform;
2338 for (SPObject *child = sp_object_first_child(SP_OBJECT(pattern)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2339 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate(xml_doc);
2340 SPItem *i = SP_ITEM (desktop->currentLayer()->appendChildRepr(copy));
2342 // FIXME: relink clones to the new canvas objects
2343 // use SPObject::setid when mental finishes it to steal ids of
2345 // this is needed to make sure the new item has curve (simply requestDisplayUpdate does not work)
2346 sp_document_ensure_up_to_date (doc);
2348 NR::Matrix transform( i->transform * pat_transform );
2349 sp_item_write_transform(i, SP_OBJECT_REPR(i), transform);
2351 new_select = g_slist_prepend(new_select, i);
2352 }
2354 SPCSSAttr *css = sp_repr_css_attr_new ();
2355 sp_repr_css_set_property (css, "fill", "none");
2356 sp_repr_css_change (SP_OBJECT_REPR (item), css, "style");
2357 }
2359 if (!did) {
2360 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No pattern fills</b> in the selection."));
2361 } else {
2362 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNTILE,
2363 _("Pattern to objects"));
2364 selection->setList(new_select);
2365 }
2366 }
2368 void
2369 sp_selection_get_export_hints (Inkscape::Selection *selection, const char **filename, float *xdpi, float *ydpi)
2370 {
2371 if (selection->isEmpty()) {
2372 return;
2373 }
2375 const GSList * reprlst = selection->reprList();
2376 bool filename_search = TRUE;
2377 bool xdpi_search = TRUE;
2378 bool ydpi_search = TRUE;
2380 for(; reprlst != NULL &&
2381 filename_search &&
2382 xdpi_search &&
2383 ydpi_search;
2384 reprlst = reprlst->next) {
2385 const gchar * dpi_string;
2386 Inkscape::XML::Node * repr = (Inkscape::XML::Node *)reprlst->data;
2388 if (filename_search) {
2389 *filename = repr->attribute("inkscape:export-filename");
2390 if (*filename != NULL)
2391 filename_search = FALSE;
2392 }
2394 if (xdpi_search) {
2395 dpi_string = NULL;
2396 dpi_string = repr->attribute("inkscape:export-xdpi");
2397 if (dpi_string != NULL) {
2398 *xdpi = atof(dpi_string);
2399 xdpi_search = FALSE;
2400 }
2401 }
2403 if (ydpi_search) {
2404 dpi_string = NULL;
2405 dpi_string = repr->attribute("inkscape:export-ydpi");
2406 if (dpi_string != NULL) {
2407 *ydpi = atof(dpi_string);
2408 ydpi_search = FALSE;
2409 }
2410 }
2411 }
2412 }
2414 void
2415 sp_document_get_export_hints (SPDocument * doc, const char **filename, float *xdpi, float *ydpi)
2416 {
2417 Inkscape::XML::Node * repr = sp_document_repr_root(doc);
2418 const gchar * dpi_string;
2420 *filename = repr->attribute("inkscape:export-filename");
2422 dpi_string = NULL;
2423 dpi_string = repr->attribute("inkscape:export-xdpi");
2424 if (dpi_string != NULL) {
2425 *xdpi = atof(dpi_string);
2426 }
2428 dpi_string = NULL;
2429 dpi_string = repr->attribute("inkscape:export-ydpi");
2430 if (dpi_string != NULL) {
2431 *ydpi = atof(dpi_string);
2432 }
2433 }
2435 void
2436 sp_selection_create_bitmap_copy ()
2437 {
2438 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2439 if (desktop == NULL)
2440 return;
2442 SPDocument *document = sp_desktop_document(desktop);
2443 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
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 make a bitmap copy."));
2450 return;
2451 }
2453 // Get the bounding box of the selection
2454 NRRect bbox;
2455 sp_document_ensure_up_to_date (document);
2456 selection->bounds(&bbox);
2457 if (NR_RECT_DFLS_TEST_EMPTY(&bbox)) {
2458 return; // exceptional situation, so not bother with a translatable error message, just quit quietly
2459 }
2461 // List of the items to show; all others will be hidden
2462 GSList *items = g_slist_copy ((GSList *) selection->itemList());
2464 // Sort items so that the topmost comes last
2465 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
2467 // Generate a random value from the current time (you may create bitmap from the same object(s)
2468 // multiple times, and this is done so that they don't clash)
2469 GTimeVal cu;
2470 g_get_current_time (&cu);
2471 guint current = (int) (cu.tv_sec * 1000000 + cu.tv_usec) % 1024;
2473 // Create the filename
2474 gchar *filename = g_strdup_printf ("%s-%s-%u.png", document->name, SP_OBJECT_REPR(items->data)->attribute("id"), current);
2475 // Imagemagick is known not to handle spaces in filenames, so we replace anything but letters,
2476 // digits, and a few other chars, with "_"
2477 filename = g_strcanon (filename, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.=+~$#@^&!?", '_');
2478 // Build the complete path by adding document->base if set
2479 gchar *filepath = g_build_filename (document->base?document->base:"", filename, NULL);
2481 //g_print ("%s\n", filepath);
2483 // Remember parent and z-order of the topmost one
2484 gint pos = SP_OBJECT_REPR(g_slist_last(items)->data)->position();
2485 SPObject *parent_object = SP_OBJECT_PARENT(g_slist_last(items)->data);
2486 Inkscape::XML::Node *parent = SP_OBJECT_REPR(parent_object);
2488 // Calculate resolution
2489 double res;
2490 int const prefs_res = prefs_get_int_attribute ("options.createbitmap", "resolution", 0);
2491 int const prefs_min = prefs_get_int_attribute ("options.createbitmap", "minsize", 0);
2492 if (0 < prefs_res) {
2493 // If it's given explicitly in prefs, take it
2494 res = prefs_res;
2495 } else if (0 < prefs_min) {
2496 // If minsize is given, look up minimum bitmap size (default 250 pixels) and calculate resolution from it
2497 res = PX_PER_IN * prefs_min / MIN ((bbox.x1 - bbox.x0), (bbox.y1 - bbox.y0));
2498 } else {
2499 float hint_xdpi = 0, hint_ydpi = 0;
2500 const char *hint_filename;
2501 // take resolution hint from the selected objects
2502 sp_selection_get_export_hints (selection, &hint_filename, &hint_xdpi, &hint_ydpi);
2503 if (hint_xdpi != 0) {
2504 res = hint_xdpi;
2505 } else {
2506 // take resolution hint from the document
2507 sp_document_get_export_hints (document, &hint_filename, &hint_xdpi, &hint_ydpi);
2508 if (hint_xdpi != 0) {
2509 res = hint_xdpi;
2510 } else {
2511 // if all else fails, take the default 90 dpi
2512 res = PX_PER_IN;
2513 }
2514 }
2515 }
2517 // The width and height of the bitmap in pixels
2518 unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * res / PX_PER_IN);
2519 unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * res / PX_PER_IN);
2521 // Find out if we have to run a filter
2522 const gchar *run = NULL;
2523 const gchar *filter = prefs_get_string_attribute ("options.createbitmap", "filter");
2524 if (filter) {
2525 // filter command is given;
2526 // see if we have a parameter to pass to it
2527 const gchar *param1 = prefs_get_string_attribute ("options.createbitmap", "filter_param1");
2528 if (param1) {
2529 if (param1[strlen(param1) - 1] == '%') {
2530 // if the param string ends with %, interpret it as a percentage of the image's max dimension
2531 gchar p1[256];
2532 g_ascii_dtostr (p1, 256, ceil (g_ascii_strtod (param1, NULL) * MAX(width, height) / 100));
2533 // the first param is always the image filename, the second is param1
2534 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, p1);
2535 } else {
2536 // otherwise pass the param1 unchanged
2537 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, param1);
2538 }
2539 } else {
2540 // run without extra parameter
2541 run = g_strdup_printf ("%s \"%s\"", filter, filepath);
2542 }
2543 }
2545 // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
2546 NR::Matrix eek = sp_item_i2d_affine (SP_ITEM(parent_object));
2547 NR::Matrix t;
2549 double shift_x = bbox.x0;
2550 double shift_y = bbox.y1;
2551 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
2552 shift_x = round (shift_x);
2553 shift_y = -round (-shift_y); // this gets correct rounding despite coordinate inversion, remove the negations when the inversion is gone
2554 }
2555 t = NR::scale(1, -1) * NR::translate (shift_x, shift_y) * eek.inverse();
2557 // Do the export
2558 sp_export_png_file(document, filepath,
2559 bbox.x0, bbox.y0, bbox.x1, bbox.y1,
2560 width, height, res, res,
2561 (guint32) 0xffffff00,
2562 NULL, NULL,
2563 true, /*bool force_overwrite,*/
2564 items);
2566 g_slist_free (items);
2568 // Run filter, if any
2569 if (run) {
2570 g_print ("Running external filter: %s\n", run);
2571 system (run);
2572 }
2574 // Import the image back
2575 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filepath, NULL);
2576 if (pb) {
2577 // Create the repr for the image
2578 Inkscape::XML::Node * repr = xml_doc->createElement("svg:image");
2579 repr->setAttribute("xlink:href", filename);
2580 repr->setAttribute("sodipodi:absref", filepath);
2581 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
2582 sp_repr_set_svg_double(repr, "width", width);
2583 sp_repr_set_svg_double(repr, "height", height);
2584 } else {
2585 sp_repr_set_svg_double(repr, "width", (bbox.x1 - bbox.x0));
2586 sp_repr_set_svg_double(repr, "height", (bbox.y1 - bbox.y0));
2587 }
2589 // Write transform
2590 gchar *c=sp_svg_transform_write(t);
2591 repr->setAttribute("transform", c);
2592 g_free(c);
2594 // add the new repr to the parent
2595 parent->appendChild(repr);
2597 // move to the saved position
2598 repr->setPosition(pos > 0 ? pos + 1 : 1);
2600 // Set selection to the new image
2601 selection->clear();
2602 selection->add(repr);
2604 // Clean up
2605 Inkscape::GC::release(repr);
2606 gdk_pixbuf_unref (pb);
2608 // Complete undoable transaction
2609 sp_document_done (document, SP_VERB_SELECTION_CREATE_BITMAP,
2610 _("Create bitmap"));
2611 }
2613 g_free (filename);
2614 g_free (filepath);
2615 }
2617 /**
2618 * \brief sp_selection_set_mask
2619 *
2620 * This function creates a mask or clipPath from selection
2621 * Two different modes:
2622 * if applyToLayer, all selection is moved to DEFS as mask/clippath
2623 * and is applied to current layer
2624 * otherwise, topmost object is used as mask for other objects
2625 * If \a apply_clip_path parameter is true, clipPath is created, otherwise mask
2626 *
2627 */
2628 void
2629 sp_selection_set_mask(bool apply_clip_path, bool apply_to_layer)
2630 {
2631 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2632 if (desktop == NULL)
2633 return;
2635 SPDocument *doc = sp_desktop_document(desktop);
2636 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2638 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2640 // check if something is selected
2641 bool is_empty = selection->isEmpty();
2642 if ( apply_to_layer && is_empty) {
2643 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to create clippath or mask from."));
2644 return;
2645 } else if (!apply_to_layer && ( is_empty || NULL == selection->itemList()->next )) {
2646 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select mask object and <b>object(s)</b> to apply clippath or mask to."));
2647 return;
2648 }
2650 // FIXME: temporary patch to prevent crash!
2651 // Remove this when bboxes are fixed to not blow up on an item clipped/masked with its own clone
2652 bool clone_with_original = selection_contains_both_clone_and_original (selection);
2653 if (clone_with_original) {
2654 return; // in this version, you cannot clip/mask an object with its own clone
2655 }
2656 // /END FIXME
2658 sp_document_ensure_up_to_date(doc);
2660 GSList *items = g_slist_copy((GSList *) selection->itemList());
2662 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2664 // create a list of duplicates
2665 GSList *mask_items = NULL;
2666 GSList *apply_to_items = NULL;
2667 GSList *items_to_delete = NULL;
2668 bool topmost = prefs_get_int_attribute ("options.maskobject", "topmost", 1);
2669 bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
2671 if (apply_to_layer) {
2672 // all selected items are used for mask, which is applied to a layer
2673 apply_to_items = g_slist_prepend (apply_to_items, desktop->currentLayer());
2675 for (GSList *i = items; i != NULL; i = i->next) {
2676 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2677 mask_items = g_slist_prepend (mask_items, dup);
2679 if (remove_original) {
2680 SPObject *item = SP_OBJECT (i->data);
2681 items_to_delete = g_slist_prepend (items_to_delete, item);
2682 }
2683 }
2684 } else if (!topmost) {
2685 // topmost item is used as a mask, which is applied to other items in a selection
2686 GSList *i = items;
2687 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2688 mask_items = g_slist_prepend (mask_items, dup);
2690 if (remove_original) {
2691 SPObject *item = SP_OBJECT (i->data);
2692 items_to_delete = g_slist_prepend (items_to_delete, item);
2693 }
2695 for (i = i->next; i != NULL; i = i->next) {
2696 apply_to_items = g_slist_prepend (apply_to_items, i->data);
2697 }
2698 } else {
2699 GSList *i = NULL;
2700 for (i = items; NULL != i->next; i = i->next) {
2701 apply_to_items = g_slist_prepend (apply_to_items, i->data);
2702 }
2704 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2705 mask_items = g_slist_prepend (mask_items, dup);
2707 if (remove_original) {
2708 SPObject *item = SP_OBJECT (i->data);
2709 items_to_delete = g_slist_prepend (items_to_delete, item);
2710 }
2711 }
2713 g_slist_free (items);
2714 items = NULL;
2716 gchar const* attributeName = apply_clip_path ? "clip-path" : "mask";
2717 for (GSList *i = apply_to_items; NULL != i; i = i->next) {
2718 SPItem *item = reinterpret_cast<SPItem *>(i->data);
2719 // inverted object transform should be applied to a mask object,
2720 // as mask is calculated in user space (after applying transform)
2721 NR::Matrix maskTransform (item->transform.inverse());
2723 GSList *mask_items_dup = NULL;
2724 for (GSList *mask_item = mask_items; NULL != mask_item; mask_item = mask_item->next) {
2725 Inkscape::XML::Node *dup = reinterpret_cast<Inkscape::XML::Node *>(mask_item->data)->duplicate(xml_doc);
2726 mask_items_dup = g_slist_prepend (mask_items_dup, dup);
2727 }
2729 const gchar *mask_id = NULL;
2730 if (apply_clip_path) {
2731 mask_id = sp_clippath_create(mask_items_dup, doc, &maskTransform);
2732 } else {
2733 mask_id = sp_mask_create(mask_items_dup, doc, &maskTransform);
2734 }
2736 g_slist_free (mask_items_dup);
2737 mask_items_dup = NULL;
2739 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, g_strdup_printf("url(#%s)", mask_id));
2740 }
2742 g_slist_free (mask_items);
2743 g_slist_free (apply_to_items);
2745 for (GSList *i = items_to_delete; NULL != i; i = i->next) {
2746 SPObject *item = SP_OBJECT (i->data);
2747 item->deleteObject (false);
2748 }
2749 g_slist_free (items_to_delete);
2751 if (apply_clip_path)
2752 sp_document_done (doc, SP_VERB_OBJECT_SET_CLIPPATH, _("Set clipping path"));
2753 else
2754 sp_document_done (doc, SP_VERB_OBJECT_SET_MASK, _("Set mask"));
2755 }
2757 void sp_selection_unset_mask(bool apply_clip_path) {
2758 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2759 if (desktop == NULL)
2760 return;
2762 SPDocument *doc = sp_desktop_document(desktop);
2763 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2764 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2766 // check if something is selected
2767 if (selection->isEmpty()) {
2768 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to remove clippath or mask from."));
2769 return;
2770 }
2772 bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
2773 sp_document_ensure_up_to_date(doc);
2775 gchar const* attributeName = apply_clip_path ? "clip-path" : "mask";
2776 std::map<SPObject*,SPItem*> referenced_objects;
2777 for (GSList const*i = selection->itemList(); NULL != i; i = i->next) {
2778 if (remove_original) {
2779 // remember referenced mask/clippath, so orphaned masks can be moved back to document
2780 SPItem *item = reinterpret_cast<SPItem *>(i->data);
2781 Inkscape::URIReference *uri_ref = NULL;
2783 if (apply_clip_path) {
2784 uri_ref = item->clip_ref;
2785 } else {
2786 uri_ref = item->mask_ref;
2787 }
2789 // collect distinct mask object (and associate with item to apply transform)
2790 if (NULL != uri_ref && NULL != uri_ref->getObject()) {
2791 referenced_objects[uri_ref->getObject()] = item;
2792 }
2793 }
2795 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, "none");
2796 }
2798 // restore mask objects into a document
2799 for ( std::map<SPObject*,SPItem*>::iterator it = referenced_objects.begin() ; it != referenced_objects.end() ; ++it) {
2800 SPObject *obj = (*it).first;
2801 GSList *items_to_move = NULL;
2802 for (SPObject *child = sp_object_first_child(obj) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2803 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate(xml_doc);
2804 items_to_move = g_slist_prepend (items_to_move, copy);
2805 }
2807 if (!obj->isReferenced()) {
2808 // delete from defs if no other object references this mask
2809 obj->deleteObject(false);
2810 }
2812 // remember parent and position of the item to which the clippath/mask was applied
2813 Inkscape::XML::Node *parent = SP_OBJECT_REPR((*it).second)->parent();
2814 gint pos = SP_OBJECT_REPR((*it).second)->position();
2816 for (GSList *i = items_to_move; NULL != i; i = i->next) {
2817 Inkscape::XML::Node *repr = (Inkscape::XML::Node *)i->data;
2819 // insert into parent, restore pos
2820 parent->appendChild(repr);
2821 repr->setPosition((pos + 1) > 0 ? (pos + 1) : 0);
2823 SPItem *mask_item = (SPItem *) sp_desktop_document (desktop)->getObjectByRepr(repr);
2824 selection->add(repr);
2826 // transform mask, so it is moved the same spot where mask was applied
2827 NR::Matrix transform (mask_item->transform);
2828 transform *= (*it).second->transform;
2829 sp_item_write_transform(mask_item, SP_OBJECT_REPR(mask_item), transform);
2830 }
2832 g_slist_free (items_to_move);
2833 }
2835 if (apply_clip_path)
2836 sp_document_done (doc, SP_VERB_OBJECT_UNSET_CLIPPATH, _("Release clipping path"));
2837 else
2838 sp_document_done (doc, SP_VERB_OBJECT_UNSET_MASK, _("Release mask"));
2839 }
2841 void fit_canvas_to_selection(SPDesktop *desktop) {
2842 g_return_if_fail(desktop != NULL);
2843 SPDocument *doc = sp_desktop_document(desktop);
2845 g_return_if_fail(doc != NULL);
2846 g_return_if_fail(desktop->selection != NULL);
2847 g_return_if_fail(!desktop->selection->isEmpty());
2849 NR::Maybe<NR::Rect> const bbox(desktop->selection->bounds());
2850 if (bbox && !bbox->isEmpty()) {
2851 doc->fitToRect(*bbox);
2852 }
2853 };
2855 void fit_canvas_to_drawing(SPDocument *doc) {
2856 g_return_if_fail(doc != NULL);
2858 sp_document_ensure_up_to_date(doc);
2859 SPItem const *const root = SP_ITEM(doc->root);
2860 NR::Maybe<NR::Rect> const bbox(root->getBounds(sp_item_i2r_affine(root)));
2861 if (bbox && !bbox->isEmpty()) {
2862 doc->fitToRect(*bbox);
2863 }
2864 };
2866 void fit_canvas_to_selection_or_drawing(SPDesktop *desktop) {
2867 g_return_if_fail(desktop != NULL);
2868 SPDocument *doc = sp_desktop_document(desktop);
2870 g_return_if_fail(doc != NULL);
2871 g_return_if_fail(desktop->selection != NULL);
2873 if (desktop->selection->isEmpty()) {
2874 fit_canvas_to_drawing(doc);
2875 } else {
2876 fit_canvas_to_selection(desktop);
2877 }
2879 sp_document_done(doc, SP_VERB_FIT_CANVAS_TO_DRAWING,
2880 _("Fit page to selection"));
2881 };
2883 static void itemtree_map(void (*f)(SPItem *, SPDesktop *), SPObject *root, SPDesktop *desktop) {
2884 // don't operate on layers
2885 if (SP_IS_ITEM(root) && !desktop->isLayer(SP_ITEM(root))) {
2886 f(SP_ITEM(root), desktop);
2887 }
2888 for ( SPObject::SiblingIterator iter = root->firstChild() ; iter ; ++iter ) {
2889 //don't recurse into locked layers
2890 if (!(SP_IS_ITEM(&*iter) && desktop->isLayer(SP_ITEM(&*iter)) && SP_ITEM(&*iter)->isLocked())) {
2891 itemtree_map(f, iter, desktop);
2892 }
2893 }
2894 }
2896 static void unlock(SPItem *item, SPDesktop *desktop) {
2897 if (item->isLocked()) {
2898 item->setLocked(FALSE);
2899 }
2900 }
2902 static void unhide(SPItem *item, SPDesktop *desktop) {
2903 if (desktop->itemIsHidden(item)) {
2904 item->setExplicitlyHidden(FALSE);
2905 }
2906 }
2908 static void process_all(void (*f)(SPItem *, SPDesktop *), SPDesktop *dt, bool layer_only) {
2909 if (!dt) return;
2911 SPObject *root;
2912 if (layer_only) {
2913 root = dt->currentLayer();
2914 } else {
2915 root = dt->currentRoot();
2916 }
2918 itemtree_map(f, root, dt);
2919 }
2921 void unlock_all(SPDesktop *dt) {
2922 process_all(&unlock, dt, true);
2923 }
2925 void unlock_all_in_all_layers(SPDesktop *dt) {
2926 process_all(&unlock, dt, false);
2927 }
2929 void unhide_all(SPDesktop *dt) {
2930 process_all(&unhide, dt, true);
2931 }
2933 void unhide_all_in_all_layers(SPDesktop *dt) {
2934 process_all(&unhide, dt, false);
2935 }
2937 /*
2938 Local Variables:
2939 mode:c++
2940 c-file-style:"stroustrup"
2941 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2942 indent-tabs-mode:nil
2943 fill-column:99
2944 End:
2945 */
2946 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :