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 using NR::X;
76 using NR::Y;
78 #include "selection-chemistry.h"
80 /* fixme: find a better place */
81 GSList *clipboard = NULL;
82 GSList *defs_clipboard = NULL;
83 SPCSSAttr *style_clipboard = NULL;
84 NR::Rect size_clipboard(NR::Point(0,0), NR::Point(0,0));
86 static void sp_copy_stuff_used_by_item(GSList **defs_clip, SPItem *item, const GSList *items);
88 /**
89 * Copies repr and its inherited css style elements, along with the accumulated transform 'full_t',
90 * then prepends the copy to 'clip'.
91 */
92 void sp_selection_copy_one (Inkscape::XML::Node *repr, NR::Matrix full_t, GSList **clip)
93 {
94 Inkscape::XML::Node *copy = repr->duplicate();
96 // copy complete inherited style
97 SPCSSAttr *css = sp_repr_css_attr_inherited(repr, "style");
98 sp_repr_css_set(copy, css, "style");
99 sp_repr_css_attr_unref(css);
101 // write the complete accumulated transform passed to us
102 // (we're dealing with unattached repr, so we write to its attr
103 // instead of using sp_item_set_transform)
104 gchar affinestr[80];
105 if (sp_svg_transform_write(affinestr, 79, full_t)) {
106 copy->setAttribute("transform", affinestr);
107 } else {
108 copy->setAttribute("transform", NULL);
109 }
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)
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);
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);
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::Node *copy = repr->duplicate();
163 SP_OBJECT_REPR(defs)->addChild(copy, NULL);
164 Inkscape::GC::release(copy);
165 }
166 }
167 }
169 GSList *sp_selection_paste_impl (SPDocument *document, SPObject *parent, GSList **clip, GSList **defs_clip)
170 {
171 paste_defs (defs_clip, document);
173 GSList *copied = NULL;
174 // add objects to document
175 for (GSList *l = *clip; l != NULL; l = l->next) {
176 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
177 Inkscape::XML::Node *copy = repr->duplicate();
179 // premultiply the item transform by the accumulated parent transform in the paste layer
180 NR::Matrix local = sp_item_i2doc_affine(SP_ITEM(parent));
181 if (!local.test_identity()) {
182 gchar const *t_str = copy->attribute("transform");
183 NR::Matrix item_t (NR::identity());
184 if (t_str)
185 sp_svg_transform_read(t_str, &item_t);
186 item_t *= local.inverse();
187 // (we're dealing with unattached repr, so we write to its attr instead of using sp_item_set_transform)
188 gchar affinestr[80];
189 if (sp_svg_transform_write(affinestr, 79, item_t)) {
190 copy->setAttribute("transform", affinestr);
191 } else {
192 copy->setAttribute("transform", NULL);
193 }
194 }
196 parent->appendChildRepr(copy);
197 copied = g_slist_prepend(copied, copy);
198 Inkscape::GC::release(copy);
199 }
200 return copied;
201 }
203 void sp_selection_delete_impl(const GSList *items)
204 {
205 for (const GSList *i = items ; i ; i = i->next ) {
206 sp_object_ref((SPObject *)i->data, NULL);
207 }
208 for (const GSList *i = items; i != NULL; i = i->next) {
209 SPItem *item = (SPItem *) i->data;
210 SP_OBJECT(item)->deleteObject();
211 sp_object_unref((SPObject *)item, NULL);
212 }
213 }
216 void sp_selection_delete()
217 {
218 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
219 if (desktop == NULL) {
220 return;
221 }
223 if (tools_isactive (desktop, TOOLS_TEXT))
224 if (sp_text_delete_selection(desktop->event_context)) {
225 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_TEXT,
226 _("Delete text"));
227 return;
228 }
230 Inkscape::Selection *selection = sp_desktop_selection(desktop);
232 // check if something is selected
233 if (selection->isEmpty()) {
234 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Nothing</b> was deleted."));
235 return;
236 }
238 const GSList *selected = g_slist_copy(const_cast<GSList *>(selection->itemList()));
239 selection->clear();
240 sp_selection_delete_impl (selected);
241 g_slist_free ((GSList *) selected);
243 /* a tool may have set up private information in it's selection context
244 * that depends on desktop items. I think the only sane way to deal with
245 * this currently is to reset the current tool, which will reset it's
246 * associated selection context. For example: deleting an object
247 * while moving it around the canvas.
248 */
249 tools_switch ( desktop, tools_active ( desktop ) );
251 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_DELETE,
252 _("Delete"));
253 }
255 /* fixme: sequencing */
256 void sp_selection_duplicate()
257 {
258 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
259 if (desktop == NULL)
260 return;
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();
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 *document = sp_desktop_document (desktop);
434 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
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();
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(document->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);
485 sp_repr_unparent(current);
487 // paste into topmost_parent (temporarily)
488 GSList *copied = sp_selection_paste_impl (document, document->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();
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 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 NR::Rect
601 enclose_items(const GSList *items)
602 {
603 g_assert(items != NULL);
605 NR::Rect r = sp_item_bbox_desktop((SPItem *) items->data);
607 for (GSList *i = items->next; i; i = i->next) {
608 r = NR::Rect::union_bounds(r, sp_item_bbox_desktop((SPItem *) i->data));
609 }
611 return r;
612 }
614 SPObject *
615 prev_sibling(SPObject *child)
616 {
617 SPObject *parent = SP_OBJECT_PARENT(child);
618 if (!SP_IS_GROUP(parent)) {
619 return NULL;
620 }
621 for ( SPObject *i = sp_object_first_child(parent) ; i; i = SP_OBJECT_NEXT(i) ) {
622 if (i->next == child)
623 return i;
624 }
625 return NULL;
626 }
628 void
629 sp_selection_raise()
630 {
631 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
632 if (!desktop)
633 return;
635 Inkscape::Selection *selection = sp_desktop_selection(desktop);
637 GSList const *items = (GSList *) selection->itemList();
638 if (!items) {
639 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise."));
640 return;
641 }
643 SPGroup const *group = sp_item_list_common_parent_group(items);
644 if (!group) {
645 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
646 return;
647 }
649 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
651 /* construct reverse-ordered list of selected children */
652 GSList *rev = g_slist_copy((GSList *) items);
653 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
655 // find out the common bbox of the selected items
656 NR::Rect selected = enclose_items(items);
658 // for all objects in the selection (starting from top)
659 while (rev) {
660 SPObject *child = SP_OBJECT(rev->data);
661 // for each selected object, find the next sibling
662 for (SPObject *newref = child->next; newref; newref = newref->next) {
663 // if the sibling is an item AND overlaps our selection,
664 if (SP_IS_ITEM(newref) && selected.intersects(sp_item_bbox_desktop(SP_ITEM(newref)))) {
665 // AND if it's not one of our selected objects,
666 if (!g_slist_find((GSList *) items, newref)) {
667 // move the selected object after that sibling
668 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(newref));
669 }
670 break;
671 }
672 }
673 rev = g_slist_remove(rev, child);
674 }
676 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_RAISE,
677 _("Raise"));
678 }
680 void sp_selection_raise_to_top()
681 {
682 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
683 if (desktop == NULL)
684 return;
686 SPDocument *document = sp_desktop_document(desktop);
687 Inkscape::Selection *selection = sp_desktop_selection(desktop);
689 if (selection->isEmpty()) {
690 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise to top."));
691 return;
692 }
694 GSList const *items = (GSList *) selection->itemList();
696 SPGroup const *group = sp_item_list_common_parent_group(items);
697 if (!group) {
698 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
699 return;
700 }
702 GSList *rl = g_slist_copy((GSList *) selection->reprList());
703 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
705 for (GSList *l = rl; l != NULL; l = l->next) {
706 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
707 repr->setPosition(-1);
708 }
710 g_slist_free(rl);
712 sp_document_done(document, SP_VERB_SELECTION_TO_FRONT,
713 _("Raise to top"));
714 }
716 void
717 sp_selection_lower()
718 {
719 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
720 if (desktop == NULL)
721 return;
723 Inkscape::Selection *selection = sp_desktop_selection(desktop);
725 GSList const *items = (GSList *) selection->itemList();
726 if (!items) {
727 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower."));
728 return;
729 }
731 SPGroup const *group = sp_item_list_common_parent_group(items);
732 if (!group) {
733 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
734 return;
735 }
737 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
739 // find out the common bbox of the selected items
740 NR::Rect selected = enclose_items(items);
742 /* construct direct-ordered list of selected children */
743 GSList *rev = g_slist_copy((GSList *) items);
744 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
745 rev = g_slist_reverse(rev);
747 // for all objects in the selection (starting from top)
748 while (rev) {
749 SPObject *child = SP_OBJECT(rev->data);
750 // for each selected object, find the prev sibling
751 for (SPObject *newref = prev_sibling(child); newref; newref = prev_sibling(newref)) {
752 // if the sibling is an item AND overlaps our selection,
753 if (SP_IS_ITEM(newref) && selected.intersects(sp_item_bbox_desktop(SP_ITEM(newref)))) {
754 // AND if it's not one of our selected objects,
755 if (!g_slist_find((GSList *) items, newref)) {
756 // move the selected object before that sibling
757 SPObject *put_after = prev_sibling(newref);
758 if (put_after)
759 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(put_after));
760 else
761 SP_OBJECT_REPR(child)->setPosition(0);
762 }
763 break;
764 }
765 }
766 rev = g_slist_remove(rev, child);
767 }
769 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_LOWER,
770 _("Lower"));
772 }
774 void sp_selection_lower_to_bottom()
775 {
776 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
777 if (desktop == NULL)
778 return;
780 SPDocument *document = sp_desktop_document(desktop);
781 Inkscape::Selection *selection = sp_desktop_selection(desktop);
783 if (selection->isEmpty()) {
784 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower to bottom."));
785 return;
786 }
788 GSList const *items = (GSList *) selection->itemList();
790 SPGroup const *group = sp_item_list_common_parent_group(items);
791 if (!group) {
792 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
793 return;
794 }
796 GSList *rl;
797 rl = g_slist_copy((GSList *) selection->reprList());
798 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
799 rl = g_slist_reverse(rl);
801 for (GSList *l = rl; l != NULL; l = l->next) {
802 gint minpos;
803 SPObject *pp, *pc;
804 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
805 pp = document->getObjectByRepr(sp_repr_parent(repr));
806 minpos = 0;
807 g_assert(SP_IS_GROUP(pp));
808 pc = sp_object_first_child(pp);
809 while (!SP_IS_ITEM(pc)) {
810 minpos += 1;
811 pc = pc->next;
812 }
813 repr->setPosition(minpos);
814 }
816 g_slist_free(rl);
818 sp_document_done(document, SP_VERB_SELECTION_TO_BACK,
819 _("Lower to bottom"));
820 }
822 void
823 sp_undo(SPDesktop *desktop, SPDocument *)
824 {
825 if (!sp_document_undo(sp_desktop_document(desktop)))
826 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to undo."));
827 }
829 void
830 sp_redo(SPDesktop *desktop, SPDocument *)
831 {
832 if (!sp_document_redo(sp_desktop_document(desktop)))
833 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to redo."));
834 }
836 void sp_selection_cut()
837 {
838 sp_selection_copy();
839 sp_selection_delete();
840 }
842 void sp_copy_gradient (GSList **defs_clip, SPGradient *gradient)
843 {
844 SPGradient *ref = gradient;
846 while (ref) {
847 // climb up the refs, copying each one in the chain
848 Inkscape::XML::Node *grad_repr = SP_OBJECT_REPR(ref)->duplicate();
849 *defs_clip = g_slist_prepend (*defs_clip, grad_repr);
851 ref = ref->ref->getObject();
852 }
853 }
855 void sp_copy_pattern (GSList **defs_clip, SPPattern *pattern)
856 {
857 SPPattern *ref = pattern;
859 while (ref) {
860 // climb up the refs, copying each one in the chain
861 Inkscape::XML::Node *pattern_repr = SP_OBJECT_REPR(ref)->duplicate();
862 *defs_clip = g_slist_prepend (*defs_clip, pattern_repr);
864 // items in the pattern may also use gradients and other patterns, so we need to recurse here as well
865 for (SPObject *child = sp_object_first_child(SP_OBJECT(ref)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
866 if (!SP_IS_ITEM (child))
867 continue;
868 sp_copy_stuff_used_by_item (defs_clip, (SPItem *) child, NULL);
869 }
871 ref = ref->ref->getObject();
872 }
873 }
875 void sp_copy_single (GSList **defs_clip, SPObject *thing)
876 {
877 Inkscape::XML::Node *duplicate_repr = SP_OBJECT_REPR(thing)->duplicate();
878 *defs_clip = g_slist_prepend (*defs_clip, duplicate_repr);
879 }
882 void sp_copy_textpath_path (GSList **defs_clip, SPTextPath *tp, const GSList *items)
883 {
884 SPItem *path = sp_textpath_get_path_item (tp);
885 if (!path)
886 return;
887 if (items && g_slist_find ((GSList *) items, path)) // do not copy it to defs if it is already in the list of items copied
888 return;
889 Inkscape::XML::Node *repr = SP_OBJECT_REPR(path)->duplicate();
890 *defs_clip = g_slist_prepend (*defs_clip, repr);
891 }
893 /**
894 * Copies things like patterns, markers, gradients, etc.
895 */
896 void sp_copy_stuff_used_by_item (GSList **defs_clip, SPItem *item, const GSList *items)
897 {
898 SPStyle *style = SP_OBJECT_STYLE (item);
900 if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
901 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
902 if (SP_IS_LINEARGRADIENT (server) || SP_IS_RADIALGRADIENT (server))
903 sp_copy_gradient (defs_clip, SP_GRADIENT(server));
904 if (SP_IS_PATTERN (server))
905 sp_copy_pattern (defs_clip, SP_PATTERN(server));
906 }
908 if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
909 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
910 if (SP_IS_LINEARGRADIENT (server) || SP_IS_RADIALGRADIENT (server))
911 sp_copy_gradient (defs_clip, SP_GRADIENT(server));
912 if (SP_IS_PATTERN (server))
913 sp_copy_pattern (defs_clip, SP_PATTERN(server));
914 }
916 // For shapes, copy all of the shape's markers into defs_clip
917 if (SP_IS_SHAPE (item)) {
918 SPShape *shape = SP_SHAPE (item);
919 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
920 if (shape->marker[i]) {
921 sp_copy_single (defs_clip, SP_OBJECT (shape->marker[i]));
922 }
923 }
924 }
926 if (SP_IS_TEXT_TEXTPATH (item)) {
927 sp_copy_textpath_path (defs_clip, SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))), items);
928 }
930 if (item->clip_ref->getObject()) {
931 sp_copy_single (defs_clip, item->clip_ref->getObject());
932 }
934 if (item->mask_ref->getObject()) {
935 SPObject *mask = item->mask_ref->getObject();
936 sp_copy_single (defs_clip, mask);
937 // recurse into the mask for its gradients etc.
938 for (SPObject *o = SP_OBJECT(mask)->children; o != NULL; o = o->next) {
939 if (SP_IS_ITEM(o))
940 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (o), items);
941 }
942 }
944 if (style->filter.filter) {
945 SPObject *filter = style->filter.filter;
946 if (SP_IS_FILTER(filter)) {
947 sp_copy_single (defs_clip, filter);
948 }
949 }
951 // recurse
952 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
953 if (SP_IS_ITEM(o))
954 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (o), items);
955 }
956 }
958 /**
959 * \pre item != NULL
960 */
961 SPCSSAttr *
962 take_style_from_item (SPItem *item)
963 {
964 // write the complete cascaded style, context-free
965 SPCSSAttr *css = sp_css_attr_from_object (SP_OBJECT(item), SP_STYLE_FLAG_ALWAYS);
966 if (css == NULL)
967 return NULL;
969 if ((SP_IS_GROUP(item) && SP_OBJECT(item)->children) ||
970 (SP_IS_TEXT (item) && SP_OBJECT(item)->children && SP_OBJECT(item)->children->next == NULL)) {
971 // if this is a text with exactly one tspan child, merge the style of that tspan as well
972 // If this is a group, merge the style of its topmost (last) child with style
973 for (SPObject *last_element = item->lastChild(); last_element != NULL; last_element = SP_OBJECT_PREV (last_element)) {
974 if (SP_OBJECT_STYLE (last_element) != NULL) {
975 SPCSSAttr *temp = sp_css_attr_from_object (last_element, SP_STYLE_FLAG_IFSET);
976 if (temp) {
977 sp_repr_css_merge (css, temp);
978 sp_repr_css_attr_unref (temp);
979 }
980 break;
981 }
982 }
983 }
984 if (!(SP_IS_TEXT (item) || SP_IS_TSPAN (item) || SP_IS_STRING (item))) {
985 // do not copy text properties from non-text objects, it's confusing
986 css = sp_css_attr_unset_text (css);
987 }
989 // FIXME: also transform gradient/pattern fills, by forking? NO, this must be nondestructive
990 double ex = NR::expansion(sp_item_i2doc_affine(item));
991 if (ex != 1.0) {
992 css = sp_css_attr_scale (css, ex);
993 }
995 return css;
996 }
999 void sp_selection_copy()
1000 {
1001 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1002 if (desktop == NULL)
1003 return;
1005 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1007 if (tools_isactive (desktop, TOOLS_DROPPER)) {
1008 sp_dropper_context_copy(desktop->event_context);
1009 return; // copied color under cursor, nothing else to do
1010 }
1012 // check if something is selected
1013 if (selection->isEmpty()) {
1014 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing was copied."));
1015 return;
1016 }
1018 const GSList *items = g_slist_copy ((GSList *) selection->itemList());
1020 // 0. Copy text to system clipboard
1021 // FIXME: for non-texts, put serialized XML as text to the clipboard;
1022 //for this sp_repr_write_stream needs to be rewritten with iostream instead of FILE
1023 Glib::ustring text;
1024 if (tools_isactive (desktop, TOOLS_TEXT)) {
1025 text = sp_text_get_selected_text(desktop->event_context);
1026 }
1028 if (text.empty()) {
1029 guint texts = 0;
1030 for (GSList *i = (GSList *) items; i; i = i->next) {
1031 SPItem *item = SP_ITEM (i->data);
1032 if (SP_IS_TEXT (item) || SP_IS_FLOWTEXT(item)) {
1033 if (texts > 0) // if more than one text object is copied, separate them by spaces
1034 text += " ";
1035 gchar *this_text = sp_te_get_string_multiline (item);
1036 if (this_text) {
1037 text += this_text;
1038 g_free(this_text);
1039 }
1040 texts++;
1041 }
1042 }
1043 }
1044 if (!text.empty()) {
1045 Glib::RefPtr<Gtk::Clipboard> refClipboard = Gtk::Clipboard::get();
1046 refClipboard->set_text(text);
1047 }
1049 // clear old defs clipboard
1050 while (defs_clipboard) {
1051 Inkscape::GC::release((Inkscape::XML::Node *) defs_clipboard->data);
1052 defs_clipboard = g_slist_remove (defs_clipboard, defs_clipboard->data);
1053 }
1055 // clear style clipboard
1056 if (style_clipboard) {
1057 sp_repr_css_attr_unref (style_clipboard);
1058 style_clipboard = NULL;
1059 }
1061 //clear main clipboard
1062 while (clipboard) {
1063 Inkscape::GC::release((Inkscape::XML::Node *) clipboard->data);
1064 clipboard = g_slist_remove(clipboard, clipboard->data);
1065 }
1067 sp_selection_copy_impl (items, &clipboard, &defs_clipboard, &style_clipboard);
1069 if (tools_isactive (desktop, TOOLS_TEXT)) { // take style from cursor/text selection, overwriting the style just set by copy_impl
1070 SPStyle *const query = sp_style_new();
1071 if (sp_desktop_query_style_all (desktop, query)) {
1072 SPCSSAttr *css = sp_css_attr_from_style (query, SP_STYLE_FLAG_ALWAYS);
1073 if (css != NULL) {
1074 // clear style clipboard
1075 if (style_clipboard) {
1076 sp_repr_css_attr_unref (style_clipboard);
1077 style_clipboard = NULL;
1078 }
1079 //sp_repr_css_print (css);
1080 style_clipboard = css;
1081 }
1082 }
1083 g_free (query);
1084 }
1086 size_clipboard = selection->bounds();
1088 g_slist_free ((GSList *) items);
1089 }
1091 void sp_selection_paste(bool in_place)
1092 {
1093 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1095 if (desktop == NULL) {
1096 return;
1097 }
1099 SPDocument *document = sp_desktop_document(desktop);
1101 if (Inkscape::have_viable_layer(desktop, desktop->messageStack()) == false) {
1102 return;
1103 }
1105 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1107 if (tools_isactive (desktop, TOOLS_TEXT)) {
1108 if (sp_text_paste_inline(desktop->event_context))
1109 return; // pasted from system clipboard into text, nothing else to do
1110 }
1112 // check if something is in the clipboard
1113 if (clipboard == NULL) {
1114 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1115 return;
1116 }
1118 GSList *copied = sp_selection_paste_impl(document, desktop->currentLayer(), &clipboard, &defs_clipboard);
1119 // add pasted objects to selection
1120 selection->setReprList((GSList const *) copied);
1121 g_slist_free (copied);
1123 if (!in_place) {
1124 sp_document_ensure_up_to_date(document);
1126 NR::Point m( desktop->point() - selection->bounds().midpoint() );
1128 /* Snap the offset of the new item(s) to the grid */
1129 /* FIXME: this gridsnap fiddling is a hack. */
1130 Inkscape::GridSnapper &s = desktop->namedview->snap_manager.grid;
1131 gdouble const curr_gridsnap = s.getDistance();
1132 s.setDistance(NR_HUGE);
1133 m = s.freeSnap(Inkscape::Snapper::SNAP_POINT, m, NULL).getPoint();
1134 s.setDistance(curr_gridsnap);
1135 sp_selection_move_relative(selection, m);
1136 }
1138 sp_document_done(document, SP_VERB_EDIT_PASTE,
1139 _("Paste"));
1140 }
1142 void sp_selection_paste_style()
1143 {
1144 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1145 if (desktop == NULL) return;
1147 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1149 // check if something is in the clipboard
1150 if (clipboard == NULL) {
1151 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1152 return;
1153 }
1155 // check if something is selected
1156 if (selection->isEmpty()) {
1157 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste style to."));
1158 return;
1159 }
1161 paste_defs (&defs_clipboard, sp_desktop_document(desktop));
1163 sp_desktop_set_style (desktop, style_clipboard);
1165 sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_STYLE,
1166 _("Paste style"));
1167 }
1169 void sp_selection_paste_size (bool apply_x, bool apply_y)
1170 {
1171 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1172 if (desktop == NULL) return;
1174 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1176 // check if something is in the clipboard
1177 if (size_clipboard.extent(NR::X) < 1e-6 || size_clipboard.extent(NR::Y) < 1e-6) {
1178 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1179 return;
1180 }
1182 // check if something is selected
1183 if (selection->isEmpty()) {
1184 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste size to."));
1185 return;
1186 }
1188 NR::Rect current = selection->bounds();
1189 if (current.extent(NR::X) < 1e-6 || current.extent(NR::Y) < 1e-6) {
1190 return;
1191 }
1193 double scale_x = size_clipboard.extent(NR::X) / current.extent(NR::X);
1194 double scale_y = size_clipboard.extent(NR::Y) / current.extent(NR::Y);
1196 sp_selection_scale_relative (selection, current.midpoint(),
1197 NR::scale(
1198 apply_x? scale_x : (desktop->isToolboxButtonActive ("lock")? scale_y : 1.0),
1199 apply_y? scale_y : (desktop->isToolboxButtonActive ("lock")? scale_x : 1.0)));
1201 sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_SIZE,
1202 _("Paste size"));
1203 }
1205 void sp_selection_paste_size_separately (bool apply_x, bool apply_y)
1206 {
1207 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1208 if (desktop == NULL) return;
1210 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1212 // check if something is in the clipboard
1213 if (size_clipboard.extent(NR::X) < 1e-6 || size_clipboard.extent(NR::Y) < 1e-6) {
1214 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1215 return;
1216 }
1218 // check if something is selected
1219 if (selection->isEmpty()) {
1220 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste size to."));
1221 return;
1222 }
1224 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1225 SPItem *item = SP_ITEM(l->data);
1227 NR::Rect current = sp_item_bbox_desktop(item);
1228 if (current.extent(NR::X) < 1e-6 || current.extent(NR::Y) < 1e-6) {
1229 continue;
1230 }
1232 double scale_x = size_clipboard.extent(NR::X) / current.extent(NR::X);
1233 double scale_y = size_clipboard.extent(NR::Y) / current.extent(NR::Y);
1235 sp_item_scale_rel (item,
1236 NR::scale(
1237 apply_x? scale_x : (desktop->isToolboxButtonActive ("lock")? scale_y : 1.0),
1238 apply_y? scale_y : (desktop->isToolboxButtonActive ("lock")? scale_x : 1.0)));
1240 }
1242 sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_SIZE_SEPARATELY,
1243 _("Paste size separately"));
1244 }
1246 void sp_selection_to_next_layer ()
1247 {
1248 SPDesktop *dt = SP_ACTIVE_DESKTOP;
1250 Inkscape::Selection *selection = sp_desktop_selection(dt);
1252 // check if something is selected
1253 if (selection->isEmpty()) {
1254 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer above."));
1255 return;
1256 }
1258 const GSList *items = g_slist_copy ((GSList *) selection->itemList());
1260 bool no_more = false; // Set to true, if no more layers above
1261 SPObject *next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer());
1262 if (next) {
1263 GSList *temp_clip = NULL;
1264 sp_selection_copy_impl (items, &temp_clip, NULL, NULL); // we're in the same doc, so no need to copy defs
1265 sp_selection_delete_impl (items);
1266 next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
1267 GSList *copied;
1268 if(next) {
1269 copied = sp_selection_paste_impl (sp_desktop_document (dt), next, &temp_clip, NULL);
1270 } else {
1271 copied = sp_selection_paste_impl (sp_desktop_document (dt), dt->currentLayer(), &temp_clip, NULL);
1272 no_more = true;
1273 }
1274 selection->setReprList((GSList const *) copied);
1275 g_slist_free (copied);
1276 if (temp_clip) g_slist_free (temp_clip);
1277 if (next) dt->setCurrentLayer(next);
1278 sp_document_done(sp_desktop_document (dt), SP_VERB_LAYER_MOVE_TO_NEXT,
1279 _("Raise to next layer"));
1280 } else {
1281 no_more = true;
1282 }
1284 if (no_more) {
1285 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers above."));
1286 }
1288 g_slist_free ((GSList *) items);
1289 }
1291 void sp_selection_to_prev_layer ()
1292 {
1293 SPDesktop *dt = SP_ACTIVE_DESKTOP;
1295 Inkscape::Selection *selection = sp_desktop_selection(dt);
1297 // check if something is selected
1298 if (selection->isEmpty()) {
1299 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer below."));
1300 return;
1301 }
1303 const GSList *items = g_slist_copy ((GSList *) selection->itemList());
1305 bool no_more = false; // Set to true, if no more layers below
1306 SPObject *next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer());
1307 if (next) {
1308 GSList *temp_clip = NULL;
1309 sp_selection_copy_impl (items, &temp_clip, NULL, NULL); // we're in the same doc, so no need to copy defs
1310 sp_selection_delete_impl (items);
1311 next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
1312 GSList *copied;
1313 if(next) {
1314 copied = sp_selection_paste_impl (sp_desktop_document (dt), next, &temp_clip, NULL);
1315 } else {
1316 copied = sp_selection_paste_impl (sp_desktop_document (dt), dt->currentLayer(), &temp_clip, NULL);
1317 no_more = true;
1318 }
1319 selection->setReprList((GSList const *) copied);
1320 g_slist_free (copied);
1321 if (temp_clip) g_slist_free (temp_clip);
1322 if (next) dt->setCurrentLayer(next);
1323 sp_document_done(sp_desktop_document (dt), SP_VERB_LAYER_MOVE_TO_PREV,
1324 _("Lower to previous layer"));
1325 } else {
1326 no_more = true;
1327 }
1329 if (no_more) {
1330 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers below."));
1331 }
1333 g_slist_free ((GSList *) items);
1334 }
1336 bool
1337 selection_contains_original (SPItem *item, Inkscape::Selection *selection)
1338 {
1339 bool contains_original = false;
1340 bool is_use = SP_IS_USE(item);
1341 SPItem *item_use = item;
1342 SPItem *item_use_first = item;
1343 while (is_use && item_use && !contains_original)
1344 {
1345 item_use = sp_use_get_original (SP_USE(item_use));
1346 contains_original |= selection->includes(item_use);
1347 if (item_use == item_use_first)
1348 break;
1349 is_use = SP_IS_USE(item_use);
1350 }
1351 return contains_original;
1352 }
1355 bool
1356 selection_contains_both_clone_and_original (Inkscape::Selection *selection)
1357 {
1358 bool clone_with_original = false;
1359 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1360 SPItem *item = SP_ITEM(l->data);
1361 clone_with_original |= selection_contains_original(item, selection);
1362 if (clone_with_original)
1363 break;
1364 }
1365 return clone_with_original;
1366 }
1369 /** Apply matrix to the selection. \a set_i2d is normally true, which means objects are in the
1370 original transform, synced with their reprs, and need to jump to the new transform in one go. A
1371 value of set_i2d==false is only used by seltrans when it's dragging objects live (not outlines); in
1372 that case, items are already in the new position, but the repr is in the old, and this function
1373 then simply updates the repr from item->transform.
1374 */
1375 void sp_selection_apply_affine(Inkscape::Selection *selection, NR::Matrix const &affine, bool set_i2d)
1376 {
1377 if (selection->isEmpty())
1378 return;
1380 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1381 SPItem *item = SP_ITEM(l->data);
1383 NR::Point old_center(0,0);
1384 if (set_i2d && item->isCenterSet())
1385 old_center = item->getCenter();
1387 #if 0 /* Re-enable this once persistent guides have a graphical indication.
1388 At the time of writing, this is the only place to re-enable. */
1389 sp_item_update_cns(*item, selection->desktop());
1390 #endif
1392 // we're moving both a clone and its original or any ancestor in clone chain?
1393 bool transform_clone_with_original = selection_contains_original(item, selection);
1394 // ...both a text-on-path and its path?
1395 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)))) ));
1396 // ...both a flowtext and its frame?
1397 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)
1398 // ...both an offset and its source?
1399 bool transform_offset_with_source = (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) && selection->includes( sp_offset_get_source (SP_OFFSET(item)) );
1401 // If we're moving a connector, we want to detach it
1402 // from shapes that aren't part of the selection, but
1403 // leave it attached if they are
1404 if (cc_item_is_connector(item)) {
1405 SPItem *attItem[2];
1406 SP_PATH(item)->connEndPair.getAttachedItems(attItem);
1408 for (int n = 0; n < 2; ++n) {
1409 if (!selection->includes(attItem[n])) {
1410 sp_conn_end_detach(item, n);
1411 }
1412 }
1413 }
1415 // "clones are unmoved when original is moved" preference
1416 int compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
1417 bool prefs_unmoved = (compensation == SP_CLONE_COMPENSATION_UNMOVED);
1418 bool prefs_parallel = (compensation == SP_CLONE_COMPENSATION_PARALLEL);
1420 // If this is a clone and it's selected along with its original, do not move it; it will feel the
1421 // transform of its original and respond to it itself. Without this, a clone is doubly
1422 // transformed, very unintuitive.
1423 // Same for textpath if we are also doing ANY transform to its path: do not touch textpath,
1424 // letters cannot be squeezed or rotated anyway, they only refill the changed path.
1425 // Same for linked offset if we are also moving its source: do not move it.
1426 if (transform_textpath_with_path || transform_offset_with_source) {
1427 // restore item->transform field from the repr, in case it was changed by seltrans
1428 sp_object_read_attr (SP_OBJECT (item), "transform");
1430 } else if (transform_flowtext_with_frame) {
1431 // apply the inverse of the region's transform to the <use> so that the flow remains
1432 // the same (even though the output itself gets transformed)
1433 for (SPObject *region = item->firstChild() ; region ; region = SP_OBJECT_NEXT(region)) {
1434 if (!SP_IS_FLOWREGION(region) && !SP_IS_FLOWREGIONEXCLUDE(region))
1435 continue;
1436 for (SPObject *use = region->firstChild() ; use ; use = SP_OBJECT_NEXT(use)) {
1437 if (!SP_IS_USE(use)) continue;
1438 sp_item_write_transform(SP_USE(use), SP_OBJECT_REPR(use), item->transform.inverse(), NULL);
1439 }
1440 }
1441 } else if (transform_clone_with_original) {
1442 // We are transforming a clone along with its original. The below matrix juggling is
1443 // necessary to ensure that they transform as a whole, i.e. the clone's induced
1444 // transform and its move compensation are both cancelled out.
1446 // restore item->transform field from the repr, in case it was changed by seltrans
1447 sp_object_read_attr (SP_OBJECT (item), "transform");
1449 // calculate the matrix we need to apply to the clone to cancel its induced transform from its original
1450 NR::Matrix parent_transform = sp_item_i2root_affine(SP_ITEM(SP_OBJECT_PARENT (item)));
1451 NR::Matrix t = parent_transform * matrix_to_desktop (matrix_from_desktop (affine, item), item) * parent_transform.inverse();
1452 NR::Matrix t_inv =parent_transform * matrix_to_desktop (matrix_from_desktop (affine.inverse(), item), item) * parent_transform.inverse();
1453 NR::Matrix result = t_inv * item->transform * t;
1455 if ((prefs_parallel || prefs_unmoved) && affine.is_translation()) {
1456 // we need to cancel out the move compensation, too
1458 // find out the clone move, same as in sp_use_move_compensate
1459 NR::Matrix parent = sp_use_get_parent_transform (SP_USE(item));
1460 NR::Matrix clone_move = parent.inverse() * t * parent;
1462 if (prefs_parallel) {
1463 NR::Matrix move = result * clone_move * t_inv;
1464 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &move);
1466 } else if (prefs_unmoved) {
1467 //if (SP_IS_USE(sp_use_get_original(SP_USE(item))))
1468 // clone_move = NR::identity();
1469 NR::Matrix move = result * clone_move;
1470 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &t);
1471 }
1473 } else {
1474 // just apply the result
1475 sp_item_write_transform(item, SP_OBJECT_REPR(item), result, &t);
1476 }
1478 } else {
1479 if (set_i2d) {
1480 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * affine);
1481 }
1482 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, NULL);
1483 }
1485 // if we're moving the actual object, not just updating the repr, we can transform the
1486 // center by the same matrix (only necessary for non-translations)
1487 if (set_i2d && item->isCenterSet() && !affine.is_translation()) {
1488 item->setCenter(old_center * affine);
1489 SP_OBJECT(item)->updateRepr();
1490 }
1491 }
1492 }
1494 void sp_selection_remove_transform()
1495 {
1496 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1497 if (desktop == NULL)
1498 return;
1500 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1502 GSList const *l = (GSList *) selection->reprList();
1503 while (l != NULL) {
1504 sp_repr_set_attr((Inkscape::XML::Node*)l->data, "transform", NULL);
1505 l = l->next;
1506 }
1508 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_FLATTEN,
1509 _("Remove transform"));
1510 }
1512 void
1513 sp_selection_scale_absolute(Inkscape::Selection *selection,
1514 double const x0, double const x1,
1515 double const y0, double const y1)
1516 {
1517 if (selection->isEmpty())
1518 return;
1520 NR::Rect const bbox(selection->bounds());
1521 if (bbox.isEmpty()) {
1522 return;
1523 }
1525 NR::translate const p2o(-bbox.min());
1527 NR::scale const newSize(x1 - x0,
1528 y1 - y0);
1529 NR::scale const scale( newSize / NR::scale(bbox.dimensions()) );
1530 NR::translate const o2n(x0, y0);
1531 NR::Matrix const final( p2o * scale * o2n );
1533 sp_selection_apply_affine(selection, final);
1534 }
1537 void sp_selection_scale_relative(Inkscape::Selection *selection, NR::Point const &align, NR::scale const &scale)
1538 {
1539 if (selection->isEmpty())
1540 return;
1542 NR::Rect const bbox(selection->bounds());
1544 if (bbox.isEmpty()) {
1545 return;
1546 }
1548 // FIXME: ARBITRARY LIMIT: don't try to scale above 1 Mpx, it won't display properly and will crash sooner or later anyway
1549 if ( bbox.extent(NR::X) * scale[NR::X] > 1e6 ||
1550 bbox.extent(NR::Y) * scale[NR::Y] > 1e6 )
1551 {
1552 return;
1553 }
1555 NR::translate const n2d(-align);
1556 NR::translate const d2n(align);
1557 NR::Matrix const final( n2d * scale * d2n );
1558 sp_selection_apply_affine(selection, final);
1559 }
1561 void
1562 sp_selection_rotate_relative(Inkscape::Selection *selection, NR::Point const ¢er, gdouble const angle_degrees)
1563 {
1564 NR::translate const d2n(center);
1565 NR::translate const n2d(-center);
1566 NR::rotate const rotate(rotate_degrees(angle_degrees));
1567 NR::Matrix const final( NR::Matrix(n2d) * rotate * d2n );
1568 sp_selection_apply_affine(selection, final);
1569 }
1571 void
1572 sp_selection_skew_relative(Inkscape::Selection *selection, NR::Point const &align, double dx, double dy)
1573 {
1574 NR::translate const d2n(align);
1575 NR::translate const n2d(-align);
1576 NR::Matrix const skew(1, dy,
1577 dx, 1,
1578 0, 0);
1579 NR::Matrix const final( n2d * skew * d2n );
1580 sp_selection_apply_affine(selection, final);
1581 }
1583 void sp_selection_move_relative(Inkscape::Selection *selection, NR::Point const &move)
1584 {
1585 sp_selection_apply_affine(selection, NR::Matrix(NR::translate(move)));
1586 }
1588 void sp_selection_move_relative(Inkscape::Selection *selection, double dx, double dy)
1589 {
1590 sp_selection_apply_affine(selection, NR::Matrix(NR::translate(dx, dy)));
1591 }
1594 /**
1595 * \brief sp_selection_rotate_90
1596 *
1597 * This function rotates selected objects 90 degrees clockwise.
1598 *
1599 */
1601 void sp_selection_rotate_90_cw()
1602 {
1603 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1605 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1607 if (selection->isEmpty())
1608 return;
1610 GSList const *l = selection->itemList();
1611 NR::rotate const rot_neg_90(NR::Point(0, -1));
1612 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1613 SPItem *item = SP_ITEM(l2->data);
1614 sp_item_rotate_rel(item, rot_neg_90);
1615 }
1617 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_ROTATE_90_CCW,
1618 _("Rotate 90° CW"));
1619 }
1622 /**
1623 * \brief sp_selection_rotate_90_ccw
1624 *
1625 * This function rotates selected objects 90 degrees counter-clockwise.
1626 *
1627 */
1629 void sp_selection_rotate_90_ccw()
1630 {
1631 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1633 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1635 if (selection->isEmpty())
1636 return;
1638 GSList const *l = selection->itemList();
1639 NR::rotate const rot_neg_90(NR::Point(0, 1));
1640 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1641 SPItem *item = SP_ITEM(l2->data);
1642 sp_item_rotate_rel(item, rot_neg_90);
1643 }
1645 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_ROTATE_90_CW,
1646 _("Rotate 90° CCW"));
1647 }
1649 void
1650 sp_selection_rotate(Inkscape::Selection *selection, gdouble const angle_degrees)
1651 {
1652 if (selection->isEmpty())
1653 return;
1655 NR::Point center = selection->center();
1657 sp_selection_rotate_relative(selection, center, angle_degrees);
1659 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1660 ( ( angle_degrees > 0 )
1661 ? "selector:rotate:ccw"
1662 : "selector:rotate:cw" ),
1663 SP_VERB_CONTEXT_SELECT,
1664 _("Rotate"));
1665 }
1667 /**
1668 \param angle the angle in "angular pixels", i.e. how many visible pixels must move the outermost point of the rotated object
1669 */
1670 void
1671 sp_selection_rotate_screen(Inkscape::Selection *selection, gdouble angle)
1672 {
1673 if (selection->isEmpty())
1674 return;
1676 NR::Rect const bbox(selection->bounds());
1678 NR::Point center = selection->center();
1680 gdouble const zoom = selection->desktop()->current_zoom();
1681 gdouble const zmove = angle / zoom;
1682 gdouble const r = NR::L2(bbox.max() - center);
1684 gdouble const zangle = 180 * atan2(zmove, r) / M_PI;
1686 sp_selection_rotate_relative(selection, center, zangle);
1688 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1689 ( (angle > 0)
1690 ? "selector:rotate:ccw"
1691 : "selector:rotate:cw" ),
1692 SP_VERB_CONTEXT_SELECT,
1693 _("Rotate by pixels"));
1694 }
1696 void
1697 sp_selection_scale(Inkscape::Selection *selection, gdouble grow)
1698 {
1699 if (selection->isEmpty())
1700 return;
1702 NR::Rect const bbox(selection->bounds());
1703 NR::Point const center(bbox.midpoint());
1704 double const max_len = bbox.maxExtent();
1706 // you can't scale "do nizhe pola" (below zero)
1707 if ( max_len + grow <= 1e-3 ) {
1708 return;
1709 }
1711 double const times = 1.0 + grow / max_len;
1712 sp_selection_scale_relative(selection, center, NR::scale(times, times));
1714 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1715 ( (grow > 0)
1716 ? "selector:scale:larger"
1717 : "selector:scale:smaller" ),
1718 SP_VERB_CONTEXT_SELECT,
1719 _("Scale"));
1720 }
1722 void
1723 sp_selection_scale_screen(Inkscape::Selection *selection, gdouble grow_pixels)
1724 {
1725 sp_selection_scale(selection,
1726 grow_pixels / selection->desktop()->current_zoom());
1727 }
1729 void
1730 sp_selection_scale_times(Inkscape::Selection *selection, gdouble times)
1731 {
1732 if (selection->isEmpty())
1733 return;
1735 NR::Point const center(selection->bounds().midpoint());
1736 sp_selection_scale_relative(selection, center, NR::scale(times, times));
1737 sp_document_done(sp_desktop_document(selection->desktop()), SP_VERB_CONTEXT_SELECT,
1738 _("Scale by whole factor"));
1739 }
1741 void
1742 sp_selection_move(gdouble dx, gdouble dy)
1743 {
1744 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1745 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1746 if (selection->isEmpty()) {
1747 return;
1748 }
1750 sp_selection_move_relative(selection, dx, dy);
1752 if (dx == 0) {
1753 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical", SP_VERB_CONTEXT_SELECT,
1754 _("Move vertically"));
1755 } else if (dy == 0) {
1756 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal", SP_VERB_CONTEXT_SELECT,
1757 _("Move horizontally"));
1758 } else {
1759 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SELECT,
1760 _("Move"));
1761 }
1762 }
1764 void
1765 sp_selection_move_screen(gdouble dx, gdouble dy)
1766 {
1767 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1769 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1770 if (selection->isEmpty()) {
1771 return;
1772 }
1774 // same as sp_selection_move but divide deltas by zoom factor
1775 gdouble const zoom = desktop->current_zoom();
1776 gdouble const zdx = dx / zoom;
1777 gdouble const zdy = dy / zoom;
1778 sp_selection_move_relative(selection, zdx, zdy);
1780 if (dx == 0) {
1781 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical", SP_VERB_CONTEXT_SELECT,
1782 _("Move vertically by pixels"));
1783 } else if (dy == 0) {
1784 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal", SP_VERB_CONTEXT_SELECT,
1785 _("Move horizontally by pixels"));
1786 } else {
1787 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SELECT,
1788 _("Move"));
1789 }
1790 }
1792 namespace {
1794 template <typename D>
1795 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
1796 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
1798 template <typename D>
1799 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items, SPObject *root,
1800 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
1802 struct Forward {
1803 typedef SPObject *Iterator;
1805 static Iterator children(SPObject *o) { return sp_object_first_child(o); }
1806 static Iterator siblings_after(SPObject *o) { return SP_OBJECT_NEXT(o); }
1807 static void dispose(Iterator i) {}
1809 static SPObject *object(Iterator i) { return i; }
1810 static Iterator next(Iterator i) { return SP_OBJECT_NEXT(i); }
1811 };
1813 struct Reverse {
1814 typedef GSList *Iterator;
1816 static Iterator children(SPObject *o) {
1817 return make_list(o->firstChild(), NULL);
1818 }
1819 static Iterator siblings_after(SPObject *o) {
1820 return make_list(SP_OBJECT_PARENT(o)->firstChild(), o);
1821 }
1822 static void dispose(Iterator i) {
1823 g_slist_free(i);
1824 }
1826 static SPObject *object(Iterator i) {
1827 return reinterpret_cast<SPObject *>(i->data);
1828 }
1829 static Iterator next(Iterator i) { return i->next; }
1831 private:
1832 static GSList *make_list(SPObject *object, SPObject *limit) {
1833 GSList *list=NULL;
1834 while ( object != limit ) {
1835 list = g_slist_prepend(list, object);
1836 object = SP_OBJECT_NEXT(object);
1837 }
1838 return list;
1839 }
1840 };
1842 }
1844 void
1845 sp_selection_item_next(void)
1846 {
1847 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1848 g_return_if_fail(desktop != NULL);
1849 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1851 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
1852 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
1853 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
1855 SPObject *root;
1856 if (PREFS_SELECTION_ALL != inlayer) {
1857 root = selection->activeContext();
1858 } else {
1859 root = desktop->currentRoot();
1860 }
1862 SPItem *item=next_item_from_list<Forward>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1864 if (item) {
1865 selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
1866 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1867 scroll_to_show_item(desktop, item);
1868 }
1869 }
1870 }
1872 void
1873 sp_selection_item_prev(void)
1874 {
1875 SPDocument *document = SP_ACTIVE_DOCUMENT;
1876 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1877 g_return_if_fail(document != NULL);
1878 g_return_if_fail(desktop != NULL);
1879 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1881 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
1882 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
1883 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
1885 SPObject *root;
1886 if (PREFS_SELECTION_ALL != inlayer) {
1887 root = selection->activeContext();
1888 } else {
1889 root = desktop->currentRoot();
1890 }
1892 SPItem *item=next_item_from_list<Reverse>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1894 if (item) {
1895 selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
1896 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1897 scroll_to_show_item(desktop, item);
1898 }
1899 }
1900 }
1902 namespace {
1904 template <typename D>
1905 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items,
1906 SPObject *root, bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
1907 {
1908 SPObject *current=root;
1909 while (items) {
1910 SPItem *item=SP_ITEM(items->data);
1911 if ( root->isAncestorOf(item) &&
1912 ( !only_in_viewport || desktop->isWithinViewport(item) ) )
1913 {
1914 current = item;
1915 break;
1916 }
1917 items = items->next;
1918 }
1920 GSList *path=NULL;
1921 while ( current != root ) {
1922 path = g_slist_prepend(path, current);
1923 current = SP_OBJECT_PARENT(current);
1924 }
1926 SPItem *next;
1927 // first, try from the current object
1928 next = next_item<D>(desktop, path, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1929 g_slist_free(path);
1931 if (!next) { // if we ran out of objects, start over at the root
1932 next = next_item<D>(desktop, NULL, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1933 }
1935 return next;
1936 }
1938 template <typename D>
1939 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
1940 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
1941 {
1942 typename D::Iterator children;
1943 typename D::Iterator iter;
1945 SPItem *found=NULL;
1947 if (path) {
1948 SPObject *object=reinterpret_cast<SPObject *>(path->data);
1949 g_assert(SP_OBJECT_PARENT(object) == root);
1950 if (desktop->isLayer(object)) {
1951 found = next_item<D>(desktop, path->next, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1952 }
1953 iter = children = D::siblings_after(object);
1954 } else {
1955 iter = children = D::children(root);
1956 }
1958 while ( iter && !found ) {
1959 SPObject *object=D::object(iter);
1960 if (desktop->isLayer(object)) {
1961 if (PREFS_SELECTION_LAYER != inlayer) { // recurse into sublayers
1962 found = next_item<D>(desktop, NULL, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1963 }
1964 } else if ( SP_IS_ITEM(object) &&
1965 ( !only_in_viewport || desktop->isWithinViewport(SP_ITEM(object)) ) &&
1966 ( !onlyvisible || !desktop->itemIsHidden(SP_ITEM(object))) &&
1967 ( !onlysensitive || !SP_ITEM(object)->isLocked()) &&
1968 !desktop->isLayer(SP_ITEM(object)) )
1969 {
1970 found = SP_ITEM(object);
1971 }
1972 iter = D::next(iter);
1973 }
1975 D::dispose(children);
1977 return found;
1978 }
1980 }
1982 /**
1983 * If \a item is not entirely visible then adjust visible area to centre on the centre on of
1984 * \a item.
1985 */
1986 void scroll_to_show_item(SPDesktop *desktop, SPItem *item)
1987 {
1988 NR::Rect dbox = desktop->get_display_area();
1989 NR::Rect sbox = sp_item_bbox_desktop(item);
1991 if (dbox.contains(sbox) == false) {
1992 NR::Point const s_dt = sbox.midpoint();
1993 NR::Point const s_w = desktop->d2w(s_dt);
1994 NR::Point const d_dt = dbox.midpoint();
1995 NR::Point const d_w = desktop->d2w(d_dt);
1996 NR::Point const moved_w( d_w - s_w );
1997 gint const dx = (gint) moved_w[X];
1998 gint const dy = (gint) moved_w[Y];
1999 desktop->scroll_world(dx, dy);
2000 }
2001 }
2004 void
2005 sp_selection_clone()
2006 {
2007 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2008 if (desktop == NULL)
2009 return;
2011 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2013 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
2015 // check if something is selected
2016 if (selection->isEmpty()) {
2017 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object</b> to clone."));
2018 return;
2019 }
2021 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
2023 selection->clear();
2025 // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
2026 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
2028 GSList *newsel = NULL;
2030 while (reprs) {
2031 Inkscape::XML::Node *sel_repr = (Inkscape::XML::Node *) reprs->data;
2032 Inkscape::XML::Node *parent = sp_repr_parent(sel_repr);
2034 Inkscape::XML::Node *clone = xml_doc->createElement("svg:use");
2035 sp_repr_set_attr(clone, "x", "0");
2036 sp_repr_set_attr(clone, "y", "0");
2037 sp_repr_set_attr(clone, "xlink:href", g_strdup_printf("#%s", sel_repr->attribute("id")));
2039 sp_repr_set_attr(clone, "inkscape:transform-center-x", sel_repr->attribute("inkscape:transform-center-x"));
2040 sp_repr_set_attr(clone, "inkscape:transform-center-y", sel_repr->attribute("inkscape:transform-center-y"));
2042 // add the new clone to the top of the original's parent
2043 parent->appendChild(clone);
2045 newsel = g_slist_prepend(newsel, clone);
2046 reprs = g_slist_remove(reprs, sel_repr);
2047 Inkscape::GC::release(clone);
2048 }
2050 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_CLONE,
2051 _("Clone"));
2053 selection->setReprList(newsel);
2055 g_slist_free(newsel);
2056 }
2058 void
2059 sp_selection_unlink()
2060 {
2061 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2062 if (!desktop)
2063 return;
2065 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2067 if (selection->isEmpty()) {
2068 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>clone</b> to unlink."));
2069 return;
2070 }
2072 // Get a copy of current selection.
2073 GSList *new_select = NULL;
2074 bool unlinked = false;
2075 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2076 items != NULL;
2077 items = items->next)
2078 {
2079 SPItem *use = (SPItem *) items->data;
2081 if (!SP_IS_USE(use)) {
2082 // keep the non-yse item in the new selection
2083 new_select = g_slist_prepend(new_select, use);
2084 continue;
2085 }
2087 SPItem *unlink = sp_use_unlink(SP_USE(use));
2088 unlinked = true;
2089 // Add ungrouped items to the new selection.
2090 new_select = g_slist_prepend(new_select, unlink);
2091 }
2093 if (new_select) { // set new selection
2094 selection->clear();
2095 selection->setList(new_select);
2096 g_slist_free(new_select);
2097 }
2098 if (!unlinked) {
2099 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No clones to unlink</b> in the selection."));
2100 }
2102 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNLINK_CLONE,
2103 _("Unlink clone"));
2104 }
2106 void
2107 sp_select_clone_original()
2108 {
2109 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2110 if (desktop == NULL)
2111 return;
2113 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2115 SPItem *item = selection->singleItem();
2117 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.");
2119 // Check if other than two objects are selected
2120 if (g_slist_length((GSList *) selection->itemList()) != 1 || !item) {
2121 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
2122 return;
2123 }
2125 SPItem *original = NULL;
2126 if (SP_IS_USE(item)) {
2127 original = sp_use_get_original (SP_USE(item));
2128 } else if (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) {
2129 original = sp_offset_get_source (SP_OFFSET(item));
2130 } else if (SP_IS_TEXT_TEXTPATH(item)) {
2131 original = sp_textpath_get_path_item (SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))));
2132 } else if (SP_IS_FLOWTEXT(item)) {
2133 original = SP_FLOWTEXT(item)->get_frame (NULL); // first frame only
2134 } else { // it's an object that we don't know what to do with
2135 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
2136 return;
2137 }
2139 if (!original) {
2140 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>Cannot find</b> the object to select (orphaned clone, offset, textpath, flowed text?)"));
2141 return;
2142 }
2144 for (SPObject *o = original; o && !SP_IS_ROOT(o); o = SP_OBJECT_PARENT (o)) {
2145 if (SP_IS_DEFS (o)) {
2146 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("The object you're trying to select is <b>not visible</b> (it is in <defs>)"));
2147 return;
2148 }
2149 }
2151 if (original) {
2152 selection->clear();
2153 selection->set(original);
2154 if (SP_CYCLING == SP_CYCLE_FOCUS) {
2155 scroll_to_show_item(desktop, original);
2156 }
2157 }
2158 }
2160 void
2161 sp_selection_tile(bool apply)
2162 {
2163 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2164 if (desktop == NULL)
2165 return;
2167 SPDocument *document = sp_desktop_document(desktop);
2168 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
2170 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2172 // check if something is selected
2173 if (selection->isEmpty()) {
2174 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to pattern."));
2175 return;
2176 }
2178 sp_document_ensure_up_to_date(document);
2179 NR::Rect r = selection->bounds();
2180 if (r.isEmpty()) {
2181 return;
2182 }
2184 // calculate the transform to be applied to objects to move them to 0,0
2185 NR::Point move_p = NR::Point(0, sp_document_height(document)) - (r.min() + NR::Point (0, r.extent(NR::Y)));
2186 move_p[NR::Y] = -move_p[NR::Y];
2187 NR::Matrix move = NR::Matrix (NR::translate (move_p));
2189 GSList *items = g_slist_copy((GSList *) selection->itemList());
2191 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2193 // bottommost object, after sorting
2194 SPObject *parent = SP_OBJECT_PARENT (items->data);
2196 NR::Matrix parent_transform = sp_item_i2root_affine(SP_ITEM(parent));
2198 // remember the position of the first item
2199 gint pos = SP_OBJECT_REPR (items->data)->position();
2201 // create a list of duplicates
2202 GSList *repr_copies = NULL;
2203 for (GSList *i = items; i != NULL; i = i->next) {
2204 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate();
2205 repr_copies = g_slist_prepend (repr_copies, dup);
2206 }
2208 NR::Rect bounds(desktop->dt2doc(r.min()), desktop->dt2doc(r.max()));
2210 if (apply) {
2211 // delete objects so that their clones don't get alerted; this object will be restored shortly
2212 for (GSList *i = items; i != NULL; i = i->next) {
2213 SPObject *item = SP_OBJECT (i->data);
2214 item->deleteObject (false);
2215 }
2216 }
2218 // Hack: Temporarily set clone compensation to unmoved, so that we can move clone-originals
2219 // without disturbing clones.
2220 // See ActorAlign::on_button_click() in src/ui/dialog/align-and-distribute.cpp
2221 int saved_compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2222 prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2224 const gchar *pat_id = pattern_tile (repr_copies, bounds, document,
2225 NR::Matrix(NR::translate(desktop->dt2doc(NR::Point(r.min()[NR::X], r.max()[NR::Y])))) * parent_transform.inverse(), parent_transform * move);
2227 // restore compensation setting
2228 prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
2230 if (apply) {
2231 Inkscape::XML::Node *rect = xml_doc->createElement("svg:rect");
2232 rect->setAttribute("style", g_strdup_printf("stroke:none;fill:url(#%s)", pat_id));
2234 NR::Point min = bounds.min() * parent_transform.inverse();
2235 NR::Point max = bounds.max() * parent_transform.inverse();
2237 sp_repr_set_svg_double(rect, "width", max[NR::X] - min[NR::X]);
2238 sp_repr_set_svg_double(rect, "height", max[NR::Y] - min[NR::Y]);
2239 sp_repr_set_svg_double(rect, "x", min[NR::X]);
2240 sp_repr_set_svg_double(rect, "y", min[NR::Y]);
2242 // restore parent and position
2243 SP_OBJECT_REPR (parent)->appendChild(rect);
2244 rect->setPosition(pos > 0 ? pos : 0);
2245 SPItem *rectangle = (SPItem *) sp_desktop_document (desktop)->getObjectByRepr(rect);
2247 Inkscape::GC::release(rect);
2249 selection->clear();
2250 selection->set(rectangle);
2251 }
2253 g_slist_free (items);
2255 sp_document_done (document, SP_VERB_EDIT_TILE,
2256 _("Objects to pattern"));
2257 }
2259 void
2260 sp_selection_untile()
2261 {
2262 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2263 if (desktop == NULL)
2264 return;
2266 SPDocument *document = sp_desktop_document(desktop);
2268 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2270 // check if something is selected
2271 if (selection->isEmpty()) {
2272 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object with pattern fill</b> to extract objects from."));
2273 return;
2274 }
2276 GSList *new_select = NULL;
2278 bool did = false;
2280 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2281 items != NULL;
2282 items = items->next) {
2284 SPItem *item = (SPItem *) items->data;
2286 SPStyle *style = SP_OBJECT_STYLE (item);
2288 if (!style || style->fill.type != SP_PAINT_TYPE_PAINTSERVER)
2289 continue;
2291 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
2293 if (!SP_IS_PATTERN(server))
2294 continue;
2296 did = true;
2298 SPPattern *pattern = pattern_getroot (SP_PATTERN (server));
2300 NR::Matrix pat_transform = pattern_patternTransform (SP_PATTERN (server));
2301 pat_transform *= item->transform;
2303 for (SPObject *child = sp_object_first_child(SP_OBJECT(pattern)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2304 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate();
2305 SPItem *i = SP_ITEM (desktop->currentLayer()->appendChildRepr(copy));
2307 // FIXME: relink clones to the new canvas objects
2308 // use SPObject::setid when mental finishes it to steal ids of
2310 // this is needed to make sure the new item has curve (simply requestDisplayUpdate does not work)
2311 sp_document_ensure_up_to_date (document);
2313 NR::Matrix transform( i->transform * pat_transform );
2314 sp_item_write_transform(i, SP_OBJECT_REPR(i), transform);
2316 new_select = g_slist_prepend(new_select, i);
2317 }
2319 SPCSSAttr *css = sp_repr_css_attr_new ();
2320 sp_repr_css_set_property (css, "fill", "none");
2321 sp_repr_css_change (SP_OBJECT_REPR (item), css, "style");
2322 }
2324 if (!did) {
2325 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No pattern fills</b> in the selection."));
2326 } else {
2327 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNTILE,
2328 _("Pattern to objects"));
2329 selection->setList(new_select);
2330 }
2331 }
2333 void
2334 sp_selection_get_export_hints (Inkscape::Selection *selection, const char **filename, float *xdpi, float *ydpi)
2335 {
2336 if (selection->isEmpty()) {
2337 return;
2338 }
2340 const GSList * reprlst = selection->reprList();
2341 bool filename_search = TRUE;
2342 bool xdpi_search = TRUE;
2343 bool ydpi_search = TRUE;
2345 for(; reprlst != NULL &&
2346 filename_search &&
2347 xdpi_search &&
2348 ydpi_search;
2349 reprlst = reprlst->next) {
2350 const gchar * dpi_string;
2351 Inkscape::XML::Node * repr = (Inkscape::XML::Node *)reprlst->data;
2353 if (filename_search) {
2354 *filename = repr->attribute("inkscape:export-filename");
2355 if (*filename != NULL)
2356 filename_search = FALSE;
2357 }
2359 if (xdpi_search) {
2360 dpi_string = NULL;
2361 dpi_string = repr->attribute("inkscape:export-xdpi");
2362 if (dpi_string != NULL) {
2363 *xdpi = atof(dpi_string);
2364 xdpi_search = FALSE;
2365 }
2366 }
2368 if (ydpi_search) {
2369 dpi_string = NULL;
2370 dpi_string = repr->attribute("inkscape:export-ydpi");
2371 if (dpi_string != NULL) {
2372 *ydpi = atof(dpi_string);
2373 ydpi_search = FALSE;
2374 }
2375 }
2376 }
2377 }
2379 void
2380 sp_document_get_export_hints (SPDocument * doc, const char **filename, float *xdpi, float *ydpi)
2381 {
2382 Inkscape::XML::Node * repr = sp_document_repr_root(doc);
2383 const gchar * dpi_string;
2385 *filename = repr->attribute("inkscape:export-filename");
2387 dpi_string = NULL;
2388 dpi_string = repr->attribute("inkscape:export-xdpi");
2389 if (dpi_string != NULL) {
2390 *xdpi = atof(dpi_string);
2391 }
2393 dpi_string = NULL;
2394 dpi_string = repr->attribute("inkscape:export-ydpi");
2395 if (dpi_string != NULL) {
2396 *ydpi = atof(dpi_string);
2397 }
2398 }
2400 void
2401 sp_selection_create_bitmap_copy ()
2402 {
2403 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2404 if (desktop == NULL)
2405 return;
2407 SPDocument *document = sp_desktop_document(desktop);
2408 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
2410 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2412 // check if something is selected
2413 if (selection->isEmpty()) {
2414 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to make a bitmap copy."));
2415 return;
2416 }
2418 // Get the bounding box of the selection
2419 NRRect bbox;
2420 sp_document_ensure_up_to_date (document);
2421 selection->bounds(&bbox);
2422 if (NR_RECT_DFLS_TEST_EMPTY(&bbox)) {
2423 return; // exceptional situation, so not bother with a translatable error message, just quit quietly
2424 }
2426 // List of the items to show; all others will be hidden
2427 GSList *items = g_slist_copy ((GSList *) selection->itemList());
2429 // Sort items so that the topmost comes last
2430 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
2432 // Generate a random value from the current time (you may create bitmap from the same object(s)
2433 // multiple times, and this is done so that they don't clash)
2434 GTimeVal cu;
2435 g_get_current_time (&cu);
2436 guint current = (int) (cu.tv_sec * 1000000 + cu.tv_usec) % 1024;
2438 // Create the filename
2439 gchar *filename = g_strdup_printf ("%s-%s-%u.png", document->name, SP_OBJECT_REPR(items->data)->attribute("id"), current);
2440 // Imagemagick is known not to handle spaces in filenames, so we replace anything but letters,
2441 // digits, and a few other chars, with "_"
2442 filename = g_strcanon (filename, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.=+~$#@^&!?", '_');
2443 // Build the complete path by adding document->base if set
2444 gchar *filepath = g_build_filename (document->base?document->base:"", filename, NULL);
2446 //g_print ("%s\n", filepath);
2448 // Remember parent and z-order of the topmost one
2449 gint pos = SP_OBJECT_REPR(g_slist_last(items)->data)->position();
2450 SPObject *parent_object = SP_OBJECT_PARENT(g_slist_last(items)->data);
2451 Inkscape::XML::Node *parent = SP_OBJECT_REPR(parent_object);
2453 // Calculate resolution
2454 double res;
2455 int const prefs_res = prefs_get_int_attribute ("options.createbitmap", "resolution", 0);
2456 int const prefs_min = prefs_get_int_attribute ("options.createbitmap", "minsize", 0);
2457 if (0 < prefs_res) {
2458 // If it's given explicitly in prefs, take it
2459 res = prefs_res;
2460 } else if (0 < prefs_min) {
2461 // If minsize is given, look up minimum bitmap size (default 250 pixels) and calculate resolution from it
2462 res = PX_PER_IN * prefs_min / MIN ((bbox.x1 - bbox.x0), (bbox.y1 - bbox.y0));
2463 } else {
2464 float hint_xdpi = 0, hint_ydpi = 0;
2465 const char *hint_filename;
2466 // take resolution hint from the selected objects
2467 sp_selection_get_export_hints (selection, &hint_filename, &hint_xdpi, &hint_ydpi);
2468 if (hint_xdpi != 0) {
2469 res = hint_xdpi;
2470 } else {
2471 // take resolution hint from the document
2472 sp_document_get_export_hints (document, &hint_filename, &hint_xdpi, &hint_ydpi);
2473 if (hint_xdpi != 0) {
2474 res = hint_xdpi;
2475 } else {
2476 // if all else fails, take the default 90 dpi
2477 res = PX_PER_IN;
2478 }
2479 }
2480 }
2482 // The width and height of the bitmap in pixels
2483 unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * res / PX_PER_IN);
2484 unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * res / PX_PER_IN);
2486 // Find out if we have to run a filter
2487 const gchar *run = NULL;
2488 const gchar *filter = prefs_get_string_attribute ("options.createbitmap", "filter");
2489 if (filter) {
2490 // filter command is given;
2491 // see if we have a parameter to pass to it
2492 const gchar *param1 = prefs_get_string_attribute ("options.createbitmap", "filter_param1");
2493 if (param1) {
2494 if (param1[strlen(param1) - 1] == '%') {
2495 // if the param string ends with %, interpret it as a percentage of the image's max dimension
2496 gchar p1[256];
2497 g_ascii_dtostr (p1, 256, ceil (g_ascii_strtod (param1, NULL) * MAX(width, height) / 100));
2498 // the first param is always the image filename, the second is param1
2499 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, p1);
2500 } else {
2501 // otherwise pass the param1 unchanged
2502 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, param1);
2503 }
2504 } else {
2505 // run without extra parameter
2506 run = g_strdup_printf ("%s \"%s\"", filter, filepath);
2507 }
2508 }
2510 // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
2511 NR::Matrix eek = sp_item_i2d_affine (SP_ITEM(parent_object));
2512 NR::Matrix t;
2513 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
2514 t = NR::scale (1, -1) * NR::translate ((unsigned) (bbox.x0 + 0.5), (unsigned) (bbox.y1 + 0.5)) * eek.inverse();
2515 } else {
2516 t = NR::scale (1, -1) * NR::translate (bbox.x0, bbox.y1) * eek.inverse();
2517 }
2519 // Do the export
2520 sp_export_png_file(document, filepath,
2521 bbox.x0, bbox.y0, bbox.x1, bbox.y1,
2522 width, height, res, res,
2523 (guint32) 0xffffff00,
2524 NULL, NULL,
2525 true, /*bool force_overwrite,*/
2526 items);
2528 g_slist_free (items);
2530 // Run filter, if any
2531 if (run) {
2532 g_print ("Running external filter: %s\n", run);
2533 system (run);
2534 }
2536 // Import the image back
2537 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filepath, NULL);
2538 if (pb) {
2539 // Create the repr for the image
2540 Inkscape::XML::Node * repr = xml_doc->createElement("svg:image");
2541 repr->setAttribute("xlink:href", filename);
2542 repr->setAttribute("sodipodi:absref", filepath);
2543 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
2544 sp_repr_set_svg_double(repr, "width", width);
2545 sp_repr_set_svg_double(repr, "height", height);
2546 } else {
2547 sp_repr_set_svg_double(repr, "width", (bbox.x1 - bbox.x0));
2548 sp_repr_set_svg_double(repr, "height", (bbox.y1 - bbox.y0));
2549 }
2551 // Write transform
2552 gchar c[256];
2553 if (sp_svg_transform_write(c, 256, t)) {
2554 repr->setAttribute("transform", c);
2555 }
2557 // add the new repr to the parent
2558 parent->appendChild(repr);
2560 // move to the saved position
2561 repr->setPosition(pos > 0 ? pos + 1 : 1);
2563 // Set selection to the new image
2564 selection->clear();
2565 selection->add(repr);
2567 // Clean up
2568 Inkscape::GC::release(repr);
2569 gdk_pixbuf_unref (pb);
2571 // Complete undoable transaction
2572 sp_document_done (document, SP_VERB_SELECTION_CREATE_BITMAP,
2573 _("Create bitmap"));
2574 }
2576 g_free (filename);
2577 g_free (filepath);
2578 }
2580 /**
2581 * \brief sp_selection_set_mask
2582 *
2583 * This function creates a mask or clipPath from selection
2584 * Two different modes:
2585 * if applyToLayer, all selection is moved to DEFS as mask/clippath
2586 * and is applied to current layer
2587 * otherwise, topmost object is used as mask for other objects
2588 * If \a apply_clip_path parameter is true, clipPath is created, otherwise mask
2589 *
2590 */
2591 void
2592 sp_selection_set_mask(bool apply_clip_path, bool apply_to_layer)
2593 {
2594 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2595 if (desktop == NULL)
2596 return;
2598 SPDocument *document = sp_desktop_document(desktop);
2600 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2602 // check if something is selected
2603 bool is_empty = selection->isEmpty();
2604 if ( apply_to_layer && is_empty) {
2605 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to create clippath or mask from."));
2606 return;
2607 } else if (!apply_to_layer && ( is_empty || NULL == selection->itemList()->next )) {
2608 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select mask object and <b>object(s)</b> to apply clippath or mask to."));
2609 return;
2610 }
2612 // FIXME: temporary patch to prevent crash!
2613 // Remove this when bboxes are fixed to not blow up on an item clipped/masked with its own clone
2614 bool clone_with_original = selection_contains_both_clone_and_original (selection);
2615 if (clone_with_original) {
2616 return; // in this version, you cannot clip/mask an object with its own clone
2617 }
2618 // /END FIXME
2620 sp_document_ensure_up_to_date(document);
2622 GSList *items = g_slist_copy((GSList *) selection->itemList());
2624 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2626 // create a list of duplicates
2627 GSList *mask_items = NULL;
2628 GSList *apply_to_items = NULL;
2629 GSList *items_to_delete = NULL;
2630 bool topmost = prefs_get_int_attribute ("options.maskobject", "topmost", 1);
2631 bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
2633 if (apply_to_layer) {
2634 // all selected items are used for mask, which is applied to a layer
2635 apply_to_items = g_slist_prepend (apply_to_items, desktop->currentLayer());
2637 for (GSList *i = items; i != NULL; i = i->next) {
2638 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate();
2639 mask_items = g_slist_prepend (mask_items, dup);
2641 if (remove_original) {
2642 SPObject *item = SP_OBJECT (i->data);
2643 items_to_delete = g_slist_prepend (items_to_delete, item);
2644 }
2645 }
2646 } else if (!topmost) {
2647 // topmost item is used as a mask, which is applied to other items in a selection
2648 GSList *i = items;
2649 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate();
2650 mask_items = g_slist_prepend (mask_items, dup);
2652 if (remove_original) {
2653 SPObject *item = SP_OBJECT (i->data);
2654 items_to_delete = g_slist_prepend (items_to_delete, item);
2655 }
2657 for (i = i->next; i != NULL; i = i->next) {
2658 apply_to_items = g_slist_prepend (apply_to_items, i->data);
2659 }
2660 } else {
2661 GSList *i = NULL;
2662 for (i = items; NULL != i->next; i = i->next) {
2663 apply_to_items = g_slist_prepend (apply_to_items, i->data);
2664 }
2666 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate();
2667 mask_items = g_slist_prepend (mask_items, dup);
2669 if (remove_original) {
2670 SPObject *item = SP_OBJECT (i->data);
2671 items_to_delete = g_slist_prepend (items_to_delete, item);
2672 }
2673 }
2675 g_slist_free (items);
2676 items = NULL;
2678 gchar const* attributeName = apply_clip_path ? "clip-path" : "mask";
2679 for (GSList *i = apply_to_items; NULL != i; i = i->next) {
2680 SPItem *item = reinterpret_cast<SPItem *>(i->data);
2681 // inverted object transform should be applied to a mask object,
2682 // as mask is calculated in user space (after applying transform)
2683 NR::Matrix maskTransform (item->transform.inverse());
2685 GSList *mask_items_dup = NULL;
2686 for (GSList *mask_item = mask_items; NULL != mask_item; mask_item = mask_item->next) {
2687 Inkscape::XML::Node *dup = reinterpret_cast<Inkscape::XML::Node *>(mask_item->data)->duplicate();
2688 mask_items_dup = g_slist_prepend (mask_items_dup, dup);
2689 }
2691 const gchar *mask_id = NULL;
2692 if (apply_clip_path) {
2693 mask_id = sp_clippath_create(mask_items_dup, document, &maskTransform);
2694 } else {
2695 mask_id = sp_mask_create(mask_items_dup, document, &maskTransform);
2696 }
2698 g_slist_free (mask_items_dup);
2699 mask_items_dup = NULL;
2701 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, g_strdup_printf("url(#%s)", mask_id));
2702 }
2704 g_slist_free (mask_items);
2705 g_slist_free (apply_to_items);
2707 for (GSList *i = items_to_delete; NULL != i; i = i->next) {
2708 SPObject *item = SP_OBJECT (i->data);
2709 item->deleteObject (false);
2710 }
2711 g_slist_free (items_to_delete);
2713 if (apply_clip_path)
2714 sp_document_done (document, SP_VERB_OBJECT_SET_CLIPPATH, _("Set clipping path"));
2715 else
2716 sp_document_done (document, SP_VERB_OBJECT_SET_MASK, _("Set mask"));
2717 }
2719 void sp_selection_unset_mask(bool apply_clip_path) {
2720 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2721 if (desktop == NULL)
2722 return;
2724 SPDocument *document = sp_desktop_document(desktop);
2725 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2727 // check if something is selected
2728 if (selection->isEmpty()) {
2729 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to remove clippath or mask from."));
2730 return;
2731 }
2733 bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
2734 sp_document_ensure_up_to_date(document);
2736 gchar const* attributeName = apply_clip_path ? "clip-path" : "mask";
2737 std::map<SPObject*,SPItem*> referenced_objects;
2738 for (GSList const*i = selection->itemList(); NULL != i; i = i->next) {
2739 if (remove_original) {
2740 // remember referenced mask/clippath, so orphaned masks can be moved back to document
2741 SPItem *item = reinterpret_cast<SPItem *>(i->data);
2742 Inkscape::URIReference *uri_ref = NULL;
2744 if (apply_clip_path) {
2745 uri_ref = item->clip_ref;
2746 } else {
2747 uri_ref = item->mask_ref;
2748 }
2750 // collect distinct mask object (and associate with item to apply transform)
2751 if (NULL != uri_ref && NULL != uri_ref->getObject()) {
2752 referenced_objects[uri_ref->getObject()] = item;
2753 }
2754 }
2756 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, "none");
2757 }
2759 // restore mask objects into a document
2760 for ( std::map<SPObject*,SPItem*>::iterator it = referenced_objects.begin() ; it != referenced_objects.end() ; ++it) {
2761 SPObject *obj = (*it).first;
2762 GSList *items_to_move = NULL;
2763 for (SPObject *child = sp_object_first_child(obj) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2764 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate();
2765 items_to_move = g_slist_prepend (items_to_move, copy);
2766 }
2768 if (!obj->isReferenced()) {
2769 // delete from defs if no other object references this mask
2770 obj->deleteObject(false);
2771 }
2773 // remember parent and position of the item to which the clippath/mask was applied
2774 Inkscape::XML::Node *parent = SP_OBJECT_REPR((*it).second)->parent();
2775 gint pos = SP_OBJECT_REPR((*it).second)->position();
2777 for (GSList *i = items_to_move; NULL != i; i = i->next) {
2778 Inkscape::XML::Node *repr = (Inkscape::XML::Node *)i->data;
2780 // insert into parent, restore pos
2781 parent->appendChild(repr);
2782 repr->setPosition((pos + 1) > 0 ? (pos + 1) : 0);
2784 SPItem *mask_item = (SPItem *) sp_desktop_document (desktop)->getObjectByRepr(repr);
2785 selection->add(repr);
2787 // transform mask, so it is moved the same spot where mask was applied
2788 NR::Matrix transform (mask_item->transform);
2789 transform *= (*it).second->transform;
2790 sp_item_write_transform(mask_item, SP_OBJECT_REPR(mask_item), transform);
2791 }
2793 g_slist_free (items_to_move);
2794 }
2796 if (apply_clip_path)
2797 sp_document_done (document, SP_VERB_OBJECT_UNSET_CLIPPATH, _("Release clipping path"));
2798 else
2799 sp_document_done (document, SP_VERB_OBJECT_UNSET_MASK, _("Release mask"));
2800 }
2802 void fit_canvas_to_selection(SPDesktop *desktop) {
2803 g_return_if_fail(desktop != NULL);
2804 SPDocument *doc = sp_desktop_document(desktop);
2806 g_return_if_fail(doc != NULL);
2807 g_return_if_fail(desktop->selection != NULL);
2808 g_return_if_fail(!desktop->selection->isEmpty());
2809 NRRect bbox = {0,0,0,0};
2811 desktop->selection->bounds(&bbox);
2812 if (!empty(bbox)) {
2813 doc->fitToRect(bbox);
2814 }
2815 };
2817 void fit_canvas_to_drawing(SPDocument *doc) {
2818 g_return_if_fail(doc != NULL);
2819 NRRect bbox = {0,0,0,0};
2821 sp_document_ensure_up_to_date (doc);
2822 sp_item_invoke_bbox(SP_ITEM(doc->root), &bbox, sp_item_i2r_affine(SP_ITEM(doc->root)), TRUE);
2824 if (!empty(bbox)) {
2825 doc->fitToRect(bbox);
2826 }
2827 };
2829 void fit_canvas_to_selection_or_drawing(SPDesktop *desktop) {
2830 g_return_if_fail(desktop != NULL);
2831 SPDocument *doc = sp_desktop_document(desktop);
2833 g_return_if_fail(doc != NULL);
2834 g_return_if_fail(desktop->selection != NULL);
2836 if (desktop->selection->isEmpty()) {
2837 fit_canvas_to_drawing(doc);
2838 } else {
2839 fit_canvas_to_selection(desktop);
2840 }
2842 sp_document_done(doc, SP_VERB_FIT_CANVAS_TO_DRAWING,
2843 _("Fit page to selection"));
2844 };
2846 /*
2847 Local Variables:
2848 mode:c++
2849 c-file-style:"stroustrup"
2850 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2851 indent-tabs-mode:nil
2852 fill-column:99
2853 End:
2854 */
2855 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :