1 #define __SP_SELECTION_CHEMISTRY_C__
3 /*
4 * Miscellanous operations on selected items
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * Frank Felfe <innerspace@iname.com>
9 * MenTaLguY <mental@rydia.net>
10 * bulia byak <buliabyak@users.sf.net>
11 * Andrius R. <knutux@gmail.com>
12 *
13 * Copyright (C) 1999-2006 authors
14 * Copyright (C) 2001-2002 Ximian, Inc.
15 *
16 * Released under GNU GPL, read the file 'COPYING' for more information
17 */
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
23 #include <gtkmm/clipboard.h>
25 #include "svg/svg.h"
26 #include "inkscape.h"
27 #include "desktop.h"
28 #include "desktop-style.h"
29 #include "selection.h"
30 #include "tools-switch.h"
31 #include "desktop-handles.h"
32 #include "message-stack.h"
33 #include "sp-item-transform.h"
34 #include "marker.h"
35 #include "sp-use.h"
36 #include "sp-textpath.h"
37 #include "sp-tspan.h"
38 #include "sp-tref.h"
39 #include "sp-flowtext.h"
40 #include "sp-flowregion.h"
41 #include "text-editing.h"
42 #include "text-context.h"
43 #include "connector-context.h"
44 #include "sp-path.h"
45 #include "sp-conn-end.h"
46 #include "dropper-context.h"
47 #include <glibmm/i18n.h>
48 #include "libnr/nr-matrix-rotate-ops.h"
49 #include "libnr/nr-matrix-translate-ops.h"
50 #include "libnr/nr-rotate-fns.h"
51 #include "libnr/nr-scale-ops.h"
52 #include "libnr/nr-scale-translate-ops.h"
53 #include "libnr/nr-translate-matrix-ops.h"
54 #include "libnr/nr-translate-scale-ops.h"
55 #include "xml/repr.h"
56 #include "style.h"
57 #include "document-private.h"
58 #include "sp-gradient.h"
59 #include "sp-gradient-reference.h"
60 #include "sp-linear-gradient-fns.h"
61 #include "sp-pattern.h"
62 #include "sp-radial-gradient-fns.h"
63 #include "sp-namedview.h"
64 #include "prefs-utils.h"
65 #include "sp-offset.h"
66 #include "sp-clippath.h"
67 #include "sp-mask.h"
68 #include "file.h"
69 #include "helper/png-write.h"
70 #include "layer-fns.h"
71 #include "context-fns.h"
72 #include <map>
73 #include "helper/units.h"
74 #include "sp-item.h"
75 #include "unit-constants.h"
76 #include "xml/simple-document.h"
77 #include "sp-filter-reference.h"
79 using NR::X;
80 using NR::Y;
82 #include "selection-chemistry.h"
84 /* fixme: find a better place */
85 Inkscape::XML::Document *clipboard_document = NULL;
86 GSList *clipboard = NULL;
87 GSList *defs_clipboard = NULL;
88 SPCSSAttr *style_clipboard = NULL;
89 NR::Maybe<NR::Rect> size_clipboard;
91 static void sp_copy_stuff_used_by_item(GSList **defs_clip, SPItem *item, const GSList *items, Inkscape::XML::Document* xml_doc);
93 /**
94 * Copies repr and its inherited css style elements, along with the accumulated transform 'full_t',
95 * then prepends the copy to 'clip'.
96 */
97 void sp_selection_copy_one (Inkscape::XML::Node *repr, NR::Matrix full_t, GSList **clip, Inkscape::XML::Document* xml_doc)
98 {
99 Inkscape::XML::Node *copy = repr->duplicate(xml_doc);
101 // copy complete inherited style
102 SPCSSAttr *css = sp_repr_css_attr_inherited(repr, "style");
103 sp_repr_css_set(copy, css, "style");
104 sp_repr_css_attr_unref(css);
106 // write the complete accumulated transform passed to us
107 // (we're dealing with unattached repr, so we write to its attr
108 // instead of using sp_item_set_transform)
109 gchar *affinestr=sp_svg_transform_write(full_t);
110 copy->setAttribute("transform", affinestr);
111 g_free(affinestr);
113 *clip = g_slist_prepend(*clip, copy);
114 }
116 void sp_selection_copy_impl (const GSList *items, GSList **clip, GSList **defs_clip, SPCSSAttr **style_clip, Inkscape::XML::Document* xml_doc)
117 {
119 // Copy stuff referenced by all items to defs_clip:
120 if (defs_clip) {
121 for (GSList *i = (GSList *) items; i != NULL; i = i->next) {
122 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (i->data), items, xml_doc);
123 }
124 *defs_clip = g_slist_reverse(*defs_clip);
125 }
127 // Store style:
128 if (style_clip) {
129 SPItem *item = SP_ITEM (items->data); // take from the first selected item
130 *style_clip = take_style_from_item (item);
131 }
133 if (clip) {
134 // Sort items:
135 GSList *sorted_items = g_slist_copy ((GSList *) items);
136 sorted_items = g_slist_sort((GSList *) sorted_items, (GCompareFunc) sp_object_compare_position);
138 // Copy item reprs:
139 for (GSList *i = (GSList *) sorted_items; i != NULL; i = i->next) {
140 sp_selection_copy_one (SP_OBJECT_REPR (i->data), sp_item_i2doc_affine(SP_ITEM (i->data)), clip, xml_doc);
141 }
143 *clip = g_slist_reverse(*clip);
144 g_slist_free ((GSList *) sorted_items);
145 }
146 }
148 /**
149 * Add gradients/patterns/markers referenced by copied objects to defs.
150 * Iterates through 'defs_clip', and for each item it adds the data
151 * repr into the global defs.
152 */
153 void
154 paste_defs (GSList **defs_clip, SPDocument *doc)
155 {
156 if (!defs_clip)
157 return;
159 for (GSList *gl = *defs_clip; gl != NULL; gl = gl->next) {
160 SPDefs *defs= (SPDefs *) SP_DOCUMENT_DEFS(doc);
161 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) gl->data;
162 gchar const *id = repr->attribute("id");
163 if (!id || !doc->getObjectById(id)) {
164 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
165 Inkscape::XML::Node *copy = repr->duplicate(xml_doc);
166 SP_OBJECT_REPR(defs)->addChild(copy, NULL);
167 Inkscape::GC::release(copy);
168 }
169 }
170 }
172 GSList *sp_selection_paste_impl (SPDocument *doc, SPObject *parent, GSList **clip, GSList **defs_clip)
173 {
174 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
175 paste_defs (defs_clip, doc);
177 GSList *copied = NULL;
178 // add objects to document
179 for (GSList *l = *clip; l != NULL; l = l->next) {
180 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
181 Inkscape::XML::Node *copy = repr->duplicate(xml_doc);
183 // premultiply the item transform by the accumulated parent transform in the paste layer
184 NR::Matrix local = sp_item_i2doc_affine(SP_ITEM(parent));
185 if (!local.test_identity()) {
186 gchar const *t_str = copy->attribute("transform");
187 NR::Matrix item_t (NR::identity());
188 if (t_str)
189 sp_svg_transform_read(t_str, &item_t);
190 item_t *= local.inverse();
191 // (we're dealing with unattached repr, so we write to its attr instead of using sp_item_set_transform)
192 gchar *affinestr=sp_svg_transform_write(item_t);
193 copy->setAttribute("transform", affinestr);
194 g_free(affinestr);
195 }
197 parent->appendChildRepr(copy);
198 copied = g_slist_prepend(copied, copy);
199 Inkscape::GC::release(copy);
200 }
201 return copied;
202 }
204 void sp_selection_delete_impl(const GSList *items)
205 {
206 for (const GSList *i = items ; i ; i = i->next ) {
207 sp_object_ref((SPObject *)i->data, NULL);
208 }
209 for (const GSList *i = items; i != NULL; i = i->next) {
210 SPItem *item = (SPItem *) i->data;
211 SP_OBJECT(item)->deleteObject();
212 sp_object_unref((SPObject *)item, NULL);
213 }
214 }
217 void sp_selection_delete()
218 {
219 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
220 if (desktop == NULL) {
221 return;
222 }
224 if (tools_isactive (desktop, TOOLS_TEXT))
225 if (sp_text_delete_selection(desktop->event_context)) {
226 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_TEXT,
227 _("Delete text"));
228 return;
229 }
231 Inkscape::Selection *selection = sp_desktop_selection(desktop);
233 // check if something is selected
234 if (selection->isEmpty()) {
235 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Nothing</b> was deleted."));
236 return;
237 }
239 const GSList *selected = g_slist_copy(const_cast<GSList *>(selection->itemList()));
240 selection->clear();
241 sp_selection_delete_impl (selected);
242 g_slist_free ((GSList *) selected);
244 /* a tool may have set up private information in it's selection context
245 * that depends on desktop items. I think the only sane way to deal with
246 * this currently is to reset the current tool, which will reset it's
247 * associated selection context. For example: deleting an object
248 * while moving it around the canvas.
249 */
250 tools_switch ( desktop, tools_active ( desktop ) );
252 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_DELETE,
253 _("Delete"));
254 }
256 /* fixme: sequencing */
257 void sp_selection_duplicate()
258 {
259 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
260 if (desktop == NULL)
261 return;
263 Inkscape::XML::Document* xml_doc = sp_document_repr_doc(desktop->doc());
264 Inkscape::Selection *selection = sp_desktop_selection(desktop);
266 // check if something is selected
267 if (selection->isEmpty()) {
268 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to duplicate."));
269 return;
270 }
272 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
274 selection->clear();
276 // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
277 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
279 GSList *newsel = NULL;
281 while (reprs) {
282 Inkscape::XML::Node *parent = ((Inkscape::XML::Node *) reprs->data)->parent();
283 Inkscape::XML::Node *copy = ((Inkscape::XML::Node *) reprs->data)->duplicate(xml_doc);
285 parent->appendChild(copy);
287 newsel = g_slist_prepend(newsel, copy);
288 reprs = g_slist_remove(reprs, reprs->data);
289 Inkscape::GC::release(copy);
290 }
292 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_DUPLICATE,
293 _("Duplicate"));
295 selection->setReprList(newsel);
297 g_slist_free(newsel);
298 }
300 void sp_edit_clear_all()
301 {
302 SPDesktop *dt = SP_ACTIVE_DESKTOP;
303 if (!dt)
304 return;
306 SPDocument *doc = sp_desktop_document(dt);
307 sp_desktop_selection(dt)->clear();
309 g_return_if_fail(SP_IS_GROUP(dt->currentLayer()));
310 GSList *items = sp_item_group_item_list(SP_GROUP(dt->currentLayer()));
312 while (items) {
313 SP_OBJECT (items->data)->deleteObject();
314 items = g_slist_remove(items, items->data);
315 }
317 sp_document_done(doc, SP_VERB_EDIT_CLEAR_ALL,
318 _("Delete all"));
319 }
321 GSList *
322 get_all_items (GSList *list, SPObject *from, SPDesktop *desktop, bool onlyvisible, bool onlysensitive, const GSList *exclude)
323 {
324 for (SPObject *child = sp_object_first_child(SP_OBJECT(from)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
325 if (SP_IS_ITEM(child) &&
326 !desktop->isLayer(SP_ITEM(child)) &&
327 (!onlysensitive || !SP_ITEM(child)->isLocked()) &&
328 (!onlyvisible || !desktop->itemIsHidden(SP_ITEM(child))) &&
329 (!exclude || !g_slist_find ((GSList *) exclude, child))
330 )
331 {
332 list = g_slist_prepend (list, SP_ITEM(child));
333 }
335 if (SP_IS_ITEM(child) && desktop->isLayer(SP_ITEM(child))) {
336 list = get_all_items (list, child, desktop, onlyvisible, onlysensitive, exclude);
337 }
338 }
340 return list;
341 }
343 void sp_edit_select_all_full (bool force_all_layers, bool invert)
344 {
345 SPDesktop *dt = SP_ACTIVE_DESKTOP;
346 if (!dt)
347 return;
349 Inkscape::Selection *selection = sp_desktop_selection(dt);
351 g_return_if_fail(SP_IS_GROUP(dt->currentLayer()));
353 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
354 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
355 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
357 GSList *items = NULL;
359 const GSList *exclude = NULL;
360 if (invert) {
361 exclude = selection->itemList();
362 }
364 if (force_all_layers)
365 inlayer = PREFS_SELECTION_ALL;
367 switch (inlayer) {
368 case PREFS_SELECTION_LAYER: {
369 if ( (onlysensitive && SP_ITEM(dt->currentLayer())->isLocked()) ||
370 (onlyvisible && dt->itemIsHidden(SP_ITEM(dt->currentLayer()))) )
371 return;
373 GSList *all_items = sp_item_group_item_list(SP_GROUP(dt->currentLayer()));
375 for (GSList *i = all_items; i; i = i->next) {
376 SPItem *item = SP_ITEM (i->data);
378 if (item && (!onlysensitive || !item->isLocked())) {
379 if (!onlyvisible || !dt->itemIsHidden(item)) {
380 if (!dt->isLayer(item)) {
381 if (!invert || !g_slist_find ((GSList *) exclude, item)) {
382 items = g_slist_prepend (items, item); // leave it in the list
383 }
384 }
385 }
386 }
387 }
389 g_slist_free (all_items);
390 break;
391 }
392 case PREFS_SELECTION_LAYER_RECURSIVE: {
393 items = get_all_items (NULL, dt->currentLayer(), dt, onlyvisible, onlysensitive, exclude);
394 break;
395 }
396 default: {
397 items = get_all_items (NULL, dt->currentRoot(), dt, onlyvisible, onlysensitive, exclude);
398 break;
399 }
400 }
402 selection->setList (items);
404 if (items) {
405 g_slist_free (items);
406 }
407 }
409 void sp_edit_select_all ()
410 {
411 sp_edit_select_all_full (false, false);
412 }
414 void sp_edit_select_all_in_all_layers ()
415 {
416 sp_edit_select_all_full (true, false);
417 }
419 void sp_edit_invert ()
420 {
421 sp_edit_select_all_full (false, true);
422 }
424 void sp_edit_invert_in_all_layers ()
425 {
426 sp_edit_select_all_full (true, true);
427 }
429 void sp_selection_group()
430 {
431 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
432 if (desktop == NULL)
433 return;
435 SPDocument *doc = sp_desktop_document (desktop);
436 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
438 Inkscape::Selection *selection = sp_desktop_selection(desktop);
440 // Check if something is selected.
441 if (selection->isEmpty()) {
442 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>some objects</b> to group."));
443 return;
444 }
446 GSList const *l = (GSList *) selection->reprList();
448 GSList *p = g_slist_copy((GSList *) l);
450 selection->clear();
452 p = g_slist_sort(p, (GCompareFunc) sp_repr_compare_position);
454 // Remember the position and parent of the topmost object.
455 gint topmost = ((Inkscape::XML::Node *) g_slist_last(p)->data)->position();
456 Inkscape::XML::Node *topmost_parent = ((Inkscape::XML::Node *) g_slist_last(p)->data)->parent();
458 Inkscape::XML::Node *group = xml_doc->createElement("svg:g");
460 while (p) {
461 Inkscape::XML::Node *current = (Inkscape::XML::Node *) p->data;
463 if (current->parent() == topmost_parent) {
464 Inkscape::XML::Node *spnew = current->duplicate(xml_doc);
465 sp_repr_unparent(current);
466 group->appendChild(spnew);
467 Inkscape::GC::release(spnew);
468 topmost --; // only reduce count for those items deleted from topmost_parent
469 } else { // move it to topmost_parent first
470 GSList *temp_clip = NULL;
472 // At this point, current may already have no item, due to its being a clone whose original is already moved away
473 // So we copy it artificially calculating the transform from its repr->attr("transform") and the parent transform
474 gchar const *t_str = current->attribute("transform");
475 NR::Matrix item_t (NR::identity());
476 if (t_str)
477 sp_svg_transform_read(t_str, &item_t);
478 item_t *= sp_item_i2doc_affine(SP_ITEM(doc->getObjectByRepr(current->parent())));
479 //FIXME: when moving both clone and original from a transformed group (either by
480 //grouping into another parent, or by cut/paste) the transform from the original's
481 //parent becomes embedded into original itself, and this affects its clones. Fix
482 //this by remembering the transform diffs we write to each item into an array and
483 //then, if this is clone, looking up its original in that array and pre-multiplying
484 //it by the inverse of that original's transform diff.
486 sp_selection_copy_one (current, item_t, &temp_clip, xml_doc);
487 sp_repr_unparent(current);
489 // paste into topmost_parent (temporarily)
490 GSList *copied = sp_selection_paste_impl (doc, doc->getObjectByRepr(topmost_parent), &temp_clip, NULL);
491 if (temp_clip) g_slist_free (temp_clip);
492 if (copied) { // if success,
493 // take pasted object (now in topmost_parent)
494 Inkscape::XML::Node *in_topmost = (Inkscape::XML::Node *) copied->data;
495 // make a copy
496 Inkscape::XML::Node *spnew = in_topmost->duplicate(xml_doc);
497 // remove pasted
498 sp_repr_unparent(in_topmost);
499 // put its copy into group
500 group->appendChild(spnew);
501 Inkscape::GC::release(spnew);
502 g_slist_free (copied);
503 }
504 }
505 p = g_slist_remove(p, current);
506 }
508 // Add the new group to the topmost members' parent
509 topmost_parent->appendChild(group);
511 // Move to the position of the topmost, reduced by the number of items deleted from topmost_parent
512 group->setPosition(topmost + 1);
514 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_GROUP,
515 _("Group"));
517 selection->set(group);
518 Inkscape::GC::release(group);
519 }
521 void sp_selection_ungroup()
522 {
523 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
524 if (desktop == NULL)
525 return;
527 Inkscape::Selection *selection = sp_desktop_selection(desktop);
529 if (selection->isEmpty()) {
530 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>group</b> to ungroup."));
531 return;
532 }
534 GSList *items = g_slist_copy((GSList *) selection->itemList());
535 selection->clear();
537 // Get a copy of current selection.
538 GSList *new_select = NULL;
539 bool ungrouped = false;
540 for (GSList *i = items;
541 i != NULL;
542 i = i->next)
543 {
544 SPItem *group = (SPItem *) i->data;
546 // when ungrouping cloned groups with their originals, some objects that were selected may no more exist due to unlinking
547 if (!SP_IS_OBJECT(group)) {
548 continue;
549 }
551 /* We do not allow ungrouping <svg> etc. (lauris) */
552 if (strcmp(SP_OBJECT_REPR(group)->name(), "svg:g") && strcmp(SP_OBJECT_REPR(group)->name(), "svg:switch")) {
553 // keep the non-group item in the new selection
554 selection->add(group);
555 continue;
556 }
558 GSList *children = NULL;
559 /* This is not strictly required, but is nicer to rely on group ::destroy (lauris) */
560 sp_item_group_ungroup(SP_GROUP(group), &children, false);
561 ungrouped = true;
562 // Add ungrouped items to the new selection.
563 new_select = g_slist_concat(new_select, children);
564 }
566 if (new_select) { // Set new selection.
567 selection->addList(new_select);
568 g_slist_free(new_select);
569 }
570 if (!ungrouped) {
571 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No groups</b> to ungroup in the selection."));
572 }
574 g_slist_free(items);
576 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_UNGROUP,
577 _("Ungroup"));
578 }
580 static SPGroup *
581 sp_item_list_common_parent_group(const GSList *items)
582 {
583 if (!items) {
584 return NULL;
585 }
586 SPObject *parent = SP_OBJECT_PARENT(items->data);
587 /* Strictly speaking this CAN happen, if user selects <svg> from Inkscape::XML editor */
588 if (!SP_IS_GROUP(parent)) {
589 return NULL;
590 }
591 for (items = items->next; items; items = items->next) {
592 if (SP_OBJECT_PARENT(items->data) != parent) {
593 return NULL;
594 }
595 }
597 return SP_GROUP(parent);
598 }
600 /** Finds out the minimum common bbox of the selected items
601 */
602 static NR::Maybe<NR::Rect>
603 enclose_items(const GSList *items)
604 {
605 g_assert(items != NULL);
607 NR::Maybe<NR::Rect> r = NR::Nothing();
608 for (GSList const *i = items; i; i = i->next) {
609 r = NR::union_bounds(r, sp_item_bbox_desktop((SPItem *) i->data));
610 }
611 return r;
612 }
614 SPObject *
615 prev_sibling(SPObject *child)
616 {
617 SPObject *parent = SP_OBJECT_PARENT(child);
618 if (!SP_IS_GROUP(parent)) {
619 return NULL;
620 }
621 for ( SPObject *i = sp_object_first_child(parent) ; i; i = SP_OBJECT_NEXT(i) ) {
622 if (i->next == child)
623 return i;
624 }
625 return NULL;
626 }
628 void
629 sp_selection_raise()
630 {
631 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
632 if (!desktop)
633 return;
635 Inkscape::Selection *selection = sp_desktop_selection(desktop);
637 GSList const *items = (GSList *) selection->itemList();
638 if (!items) {
639 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise."));
640 return;
641 }
643 SPGroup const *group = sp_item_list_common_parent_group(items);
644 if (!group) {
645 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
646 return;
647 }
649 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
651 /* construct reverse-ordered list of selected children */
652 GSList *rev = g_slist_copy((GSList *) items);
653 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
655 // find out the common bbox of the selected items
656 NR::Maybe<NR::Rect> selected = enclose_items(items);
658 // for all objects in the selection (starting from top)
659 if (selected) {
660 while (rev) {
661 SPObject *child = SP_OBJECT(rev->data);
662 // for each selected object, find the next sibling
663 for (SPObject *newref = child->next; newref; newref = newref->next) {
664 // if the sibling is an item AND overlaps our selection,
665 if (SP_IS_ITEM(newref)) {
666 NR::Maybe<NR::Rect> newref_bbox = sp_item_bbox_desktop(SP_ITEM(newref));
667 if ( newref_bbox && selected->intersects(*newref_bbox) ) {
668 // AND if it's not one of our selected objects,
669 if (!g_slist_find((GSList *) items, newref)) {
670 // move the selected object after that sibling
671 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(newref));
672 }
673 break;
674 }
675 }
676 }
677 rev = g_slist_remove(rev, child);
678 }
679 } else {
680 g_slist_free(rev);
681 }
683 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_RAISE,
684 _("Raise"));
685 }
687 void sp_selection_raise_to_top()
688 {
689 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
690 if (desktop == NULL)
691 return;
693 SPDocument *document = sp_desktop_document(desktop);
694 Inkscape::Selection *selection = sp_desktop_selection(desktop);
696 if (selection->isEmpty()) {
697 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise to top."));
698 return;
699 }
701 GSList const *items = (GSList *) selection->itemList();
703 SPGroup const *group = sp_item_list_common_parent_group(items);
704 if (!group) {
705 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
706 return;
707 }
709 GSList *rl = g_slist_copy((GSList *) selection->reprList());
710 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
712 for (GSList *l = rl; l != NULL; l = l->next) {
713 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
714 repr->setPosition(-1);
715 }
717 g_slist_free(rl);
719 sp_document_done(document, SP_VERB_SELECTION_TO_FRONT,
720 _("Raise to top"));
721 }
723 void
724 sp_selection_lower()
725 {
726 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
727 if (desktop == NULL)
728 return;
730 Inkscape::Selection *selection = sp_desktop_selection(desktop);
732 GSList const *items = (GSList *) selection->itemList();
733 if (!items) {
734 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower."));
735 return;
736 }
738 SPGroup const *group = sp_item_list_common_parent_group(items);
739 if (!group) {
740 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
741 return;
742 }
744 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
746 // find out the common bbox of the selected items
747 NR::Maybe<NR::Rect> selected = enclose_items(items);
749 /* construct direct-ordered list of selected children */
750 GSList *rev = g_slist_copy((GSList *) items);
751 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
752 rev = g_slist_reverse(rev);
754 // for all objects in the selection (starting from top)
755 if (selected) {
756 while (rev) {
757 SPObject *child = SP_OBJECT(rev->data);
758 // for each selected object, find the prev sibling
759 for (SPObject *newref = prev_sibling(child); newref; newref = prev_sibling(newref)) {
760 // if the sibling is an item AND overlaps our selection,
761 if (SP_IS_ITEM(newref)) {
762 NR::Maybe<NR::Rect> ref_bbox = sp_item_bbox_desktop(SP_ITEM(newref));
763 if ( ref_bbox && selected->intersects(*ref_bbox) ) {
764 // AND if it's not one of our selected objects,
765 if (!g_slist_find((GSList *) items, newref)) {
766 // move the selected object before that sibling
767 SPObject *put_after = prev_sibling(newref);
768 if (put_after)
769 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(put_after));
770 else
771 SP_OBJECT_REPR(child)->setPosition(0);
772 }
773 break;
774 }
775 }
776 }
777 rev = g_slist_remove(rev, child);
778 }
779 } else {
780 g_slist_free(rev);
781 }
783 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_LOWER,
784 _("Lower"));
785 }
787 void sp_selection_lower_to_bottom()
788 {
789 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
790 if (desktop == NULL)
791 return;
793 SPDocument *document = sp_desktop_document(desktop);
794 Inkscape::Selection *selection = sp_desktop_selection(desktop);
796 if (selection->isEmpty()) {
797 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower to bottom."));
798 return;
799 }
801 GSList const *items = (GSList *) selection->itemList();
803 SPGroup const *group = sp_item_list_common_parent_group(items);
804 if (!group) {
805 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
806 return;
807 }
809 GSList *rl;
810 rl = g_slist_copy((GSList *) selection->reprList());
811 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
812 rl = g_slist_reverse(rl);
814 for (GSList *l = rl; l != NULL; l = l->next) {
815 gint minpos;
816 SPObject *pp, *pc;
817 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
818 pp = document->getObjectByRepr(sp_repr_parent(repr));
819 minpos = 0;
820 g_assert(SP_IS_GROUP(pp));
821 pc = sp_object_first_child(pp);
822 while (!SP_IS_ITEM(pc)) {
823 minpos += 1;
824 pc = pc->next;
825 }
826 repr->setPosition(minpos);
827 }
829 g_slist_free(rl);
831 sp_document_done(document, SP_VERB_SELECTION_TO_BACK,
832 _("Lower to bottom"));
833 }
835 void
836 sp_undo(SPDesktop *desktop, SPDocument *)
837 {
838 if (!sp_document_undo(sp_desktop_document(desktop)))
839 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to undo."));
840 }
842 void
843 sp_redo(SPDesktop *desktop, SPDocument *)
844 {
845 if (!sp_document_redo(sp_desktop_document(desktop)))
846 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to redo."));
847 }
849 void sp_selection_cut()
850 {
851 sp_selection_copy();
852 sp_selection_delete();
853 }
855 void sp_copy_gradient (GSList **defs_clip, SPGradient *gradient, Inkscape::XML::Document* xml_doc)
856 {
857 SPGradient *ref = gradient;
859 while (ref) {
860 // climb up the refs, copying each one in the chain
861 Inkscape::XML::Node *grad_repr = SP_OBJECT_REPR(ref)->duplicate(xml_doc);
862 *defs_clip = g_slist_prepend (*defs_clip, grad_repr);
864 ref = ref->ref->getObject();
865 }
866 }
868 void sp_copy_pattern (GSList **defs_clip, SPPattern *pattern, Inkscape::XML::Document* xml_doc)
869 {
870 SPPattern *ref = pattern;
872 while (ref) {
873 // climb up the refs, copying each one in the chain
874 Inkscape::XML::Node *pattern_repr = SP_OBJECT_REPR(ref)->duplicate(xml_doc);
875 *defs_clip = g_slist_prepend (*defs_clip, pattern_repr);
877 // items in the pattern may also use gradients and other patterns, so we need to recurse here as well
878 for (SPObject *child = sp_object_first_child(SP_OBJECT(ref)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
879 if (!SP_IS_ITEM (child))
880 continue;
881 sp_copy_stuff_used_by_item (defs_clip, (SPItem *) child, NULL, xml_doc);
882 }
884 ref = ref->ref->getObject();
885 }
886 }
888 void sp_copy_single (GSList **defs_clip, SPObject *thing, Inkscape::XML::Document* xml_doc)
889 {
890 Inkscape::XML::Node *duplicate_repr = SP_OBJECT_REPR(thing)->duplicate(xml_doc);
891 *defs_clip = g_slist_prepend (*defs_clip, duplicate_repr);
892 }
895 void sp_copy_textpath_path (GSList **defs_clip, SPTextPath *tp, const GSList *items, Inkscape::XML::Document* xml_doc)
896 {
897 SPItem *path = sp_textpath_get_path_item (tp);
898 if (!path)
899 return;
900 if (items && g_slist_find ((GSList *) items, path)) // do not copy it to defs if it is already in the list of items copied
901 return;
902 Inkscape::XML::Node *repr = SP_OBJECT_REPR(path)->duplicate(xml_doc);
903 *defs_clip = g_slist_prepend (*defs_clip, repr);
904 }
906 /**
907 * Copies things like patterns, markers, gradients, etc.
908 */
909 void sp_copy_stuff_used_by_item (GSList **defs_clip, SPItem *item, const GSList *items, Inkscape::XML::Document* xml_doc)
910 {
911 SPStyle *style = SP_OBJECT_STYLE (item);
913 if (style && (style->fill.isPaintserver())) {
914 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
915 if (SP_IS_LINEARGRADIENT (server) || SP_IS_RADIALGRADIENT (server))
916 sp_copy_gradient (defs_clip, SP_GRADIENT(server), xml_doc);
917 if (SP_IS_PATTERN (server))
918 sp_copy_pattern (defs_clip, SP_PATTERN(server), xml_doc);
919 }
921 if (style && (style->stroke.isPaintserver())) {
922 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
923 if (SP_IS_LINEARGRADIENT (server) || SP_IS_RADIALGRADIENT (server))
924 sp_copy_gradient (defs_clip, SP_GRADIENT(server), xml_doc);
925 if (SP_IS_PATTERN (server))
926 sp_copy_pattern (defs_clip, SP_PATTERN(server), xml_doc);
927 }
929 // For shapes, copy all of the shape's markers into defs_clip
930 if (SP_IS_SHAPE (item)) {
931 SPShape *shape = SP_SHAPE (item);
932 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
933 if (shape->marker[i]) {
934 sp_copy_single (defs_clip, SP_OBJECT (shape->marker[i]), xml_doc);
935 }
936 }
937 }
939 if (SP_IS_TEXT_TEXTPATH (item)) {
940 sp_copy_textpath_path (defs_clip, SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))), items, xml_doc);
941 }
943 if (item->clip_ref->getObject()) {
944 sp_copy_single (defs_clip, item->clip_ref->getObject(), xml_doc);
945 }
947 if (item->mask_ref->getObject()) {
948 SPObject *mask = item->mask_ref->getObject();
949 sp_copy_single (defs_clip, mask, xml_doc);
950 // recurse into the mask for its gradients etc.
951 for (SPObject *o = SP_OBJECT(mask)->children; o != NULL; o = o->next) {
952 if (SP_IS_ITEM(o))
953 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (o), items, xml_doc);
954 }
955 }
957 if (style->getFilter()) {
958 SPObject *filter = style->getFilter();
959 if (SP_IS_FILTER(filter)) {
960 sp_copy_single (defs_clip, filter, xml_doc);
961 }
962 }
964 // recurse
965 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
966 if (SP_IS_ITEM(o))
967 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (o), items, xml_doc);
968 }
969 }
971 /**
972 * \pre item != NULL
973 */
974 SPCSSAttr *
975 take_style_from_item (SPItem *item)
976 {
977 // write the complete cascaded style, context-free
978 SPCSSAttr *css = sp_css_attr_from_object (SP_OBJECT(item), SP_STYLE_FLAG_ALWAYS);
979 if (css == NULL)
980 return NULL;
982 if ((SP_IS_GROUP(item) && SP_OBJECT(item)->children) ||
983 (SP_IS_TEXT (item) && SP_OBJECT(item)->children && SP_OBJECT(item)->children->next == NULL)) {
984 // if this is a text with exactly one tspan child, merge the style of that tspan as well
985 // If this is a group, merge the style of its topmost (last) child with style
986 for (SPObject *last_element = item->lastChild(); last_element != NULL; last_element = SP_OBJECT_PREV (last_element)) {
987 if (SP_OBJECT_STYLE (last_element) != NULL) {
988 SPCSSAttr *temp = sp_css_attr_from_object (last_element, SP_STYLE_FLAG_IFSET);
989 if (temp) {
990 sp_repr_css_merge (css, temp);
991 sp_repr_css_attr_unref (temp);
992 }
993 break;
994 }
995 }
996 }
997 if (!(SP_IS_TEXT (item) || SP_IS_TSPAN (item) || SP_IS_TREF(item) || SP_IS_STRING (item))) {
998 // do not copy text properties from non-text objects, it's confusing
999 css = sp_css_attr_unset_text (css);
1000 }
1002 // FIXME: also transform gradient/pattern fills, by forking? NO, this must be nondestructive
1003 double ex = NR::expansion(sp_item_i2doc_affine(item));
1004 if (ex != 1.0) {
1005 css = sp_css_attr_scale (css, ex);
1006 }
1008 return css;
1009 }
1012 void sp_selection_copy()
1013 {
1014 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1015 if (desktop == NULL)
1016 return;
1018 if (!clipboard_document) {
1019 clipboard_document = new Inkscape::XML::SimpleDocument();
1020 }
1022 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1024 if (tools_isactive (desktop, TOOLS_DROPPER)) {
1025 sp_dropper_context_copy(desktop->event_context);
1026 return; // copied color under cursor, nothing else to do
1027 }
1029 // check if something is selected
1030 if (selection->isEmpty()) {
1031 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing was copied."));
1032 return;
1033 }
1035 const GSList *items = g_slist_copy ((GSList *) selection->itemList());
1037 // 0. Copy text to system clipboard
1038 // FIXME: for non-texts, put serialized Inkscape::XML as text to the clipboard;
1039 //for this sp_repr_write_stream needs to be rewritten with iostream instead of FILE
1040 Glib::ustring text;
1041 if (tools_isactive (desktop, TOOLS_TEXT)) {
1042 text = sp_text_get_selected_text(desktop->event_context);
1043 }
1045 if (text.empty()) {
1046 guint texts = 0;
1047 for (GSList *i = (GSList *) items; i; i = i->next) {
1048 SPItem *item = SP_ITEM (i->data);
1049 if (SP_IS_TEXT (item) || SP_IS_FLOWTEXT(item)) {
1050 if (texts > 0) // if more than one text object is copied, separate them by spaces
1051 text += " ";
1052 gchar *this_text = sp_te_get_string_multiline (item);
1053 if (this_text) {
1054 text += this_text;
1055 g_free(this_text);
1056 }
1057 texts++;
1058 }
1059 }
1060 }
1061 if (!text.empty()) {
1062 Glib::RefPtr<Gtk::Clipboard> refClipboard = Gtk::Clipboard::get();
1063 refClipboard->set_text(text);
1064 }
1066 // clear old defs clipboard
1067 while (defs_clipboard) {
1068 Inkscape::GC::release((Inkscape::XML::Node *) defs_clipboard->data);
1069 defs_clipboard = g_slist_remove (defs_clipboard, defs_clipboard->data);
1070 }
1072 // clear style clipboard
1073 if (style_clipboard) {
1074 sp_repr_css_attr_unref (style_clipboard);
1075 style_clipboard = NULL;
1076 }
1078 //clear main clipboard
1079 while (clipboard) {
1080 Inkscape::GC::release((Inkscape::XML::Node *) clipboard->data);
1081 clipboard = g_slist_remove(clipboard, clipboard->data);
1082 }
1084 sp_selection_copy_impl (items, &clipboard, &defs_clipboard, &style_clipboard, clipboard_document);
1086 if (tools_isactive (desktop, TOOLS_TEXT)) { // take style from cursor/text selection, overwriting the style just set by copy_impl
1087 SPStyle *const query = sp_style_new(SP_ACTIVE_DOCUMENT);
1088 if (sp_desktop_query_style_all (desktop, query)) {
1089 SPCSSAttr *css = sp_css_attr_from_style (query, SP_STYLE_FLAG_ALWAYS);
1090 if (css != NULL) {
1091 // clear style clipboard
1092 if (style_clipboard) {
1093 sp_repr_css_attr_unref (style_clipboard);
1094 style_clipboard = NULL;
1095 }
1096 //sp_repr_css_print (css);
1097 style_clipboard = css;
1098 }
1099 }
1100 sp_style_unref(query);
1101 }
1103 size_clipboard = selection->bounds();
1105 g_slist_free ((GSList *) items);
1106 }
1108 void sp_selection_paste(bool in_place)
1109 {
1110 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1112 if (desktop == NULL) {
1113 return;
1114 }
1116 SPDocument *document = sp_desktop_document(desktop);
1118 if (Inkscape::have_viable_layer(desktop, desktop->messageStack()) == false) {
1119 return;
1120 }
1122 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1124 if (tools_isactive (desktop, TOOLS_TEXT)) {
1125 if (sp_text_paste_inline(desktop->event_context))
1126 return; // pasted from system clipboard into text, nothing else to do
1127 }
1129 // check if something is in the clipboard
1130 if (clipboard == NULL) {
1131 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1132 return;
1133 }
1135 GSList *copied = sp_selection_paste_impl(document, desktop->currentLayer(), &clipboard, &defs_clipboard);
1136 // add pasted objects to selection
1137 selection->setReprList((GSList const *) copied);
1138 g_slist_free (copied);
1140 if (!in_place) {
1141 sp_document_ensure_up_to_date(document);
1143 NR::Maybe<NR::Rect> sel_bbox = selection->bounds();
1144 NR::Point m( desktop->point() );
1145 if (sel_bbox) {
1146 m -= sel_bbox->midpoint();
1147 }
1149 /* Snap the offset of the new item(s) to the grid */
1150 SnapManager &sm = desktop->namedview->snap_manager;
1151 SnapManager::SnapperList gs = sm.getGridSnappers();
1152 m = sm.freeSnapAlways(Inkscape::Snapper::SNAPPOINT_NODE, m, NULL, gs).getPoint();
1153 sp_selection_move_relative(selection, m);
1154 }
1156 sp_document_done(document, SP_VERB_EDIT_PASTE,
1157 _("Paste"));
1158 }
1160 void sp_selection_paste_style()
1161 {
1162 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1163 if (desktop == NULL) return;
1165 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1167 // check if something is in the clipboard
1168 if (clipboard == NULL) {
1169 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1170 return;
1171 }
1173 // check if something is selected
1174 if (selection->isEmpty()) {
1175 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste style to."));
1176 return;
1177 }
1179 paste_defs (&defs_clipboard, sp_desktop_document(desktop));
1181 sp_desktop_set_style (desktop, style_clipboard);
1183 sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_STYLE,
1184 _("Paste style"));
1185 }
1187 void sp_selection_paste_livepatheffect()
1188 {
1189 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1190 if (desktop == NULL) return;
1192 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1194 // check if something is in the clipboard
1195 if (clipboard == NULL) {
1196 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1197 return;
1198 }
1200 // check if something is selected
1201 if (selection->isEmpty()) {
1202 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste live path effect to."));
1203 return;
1204 }
1206 paste_defs (&defs_clipboard, sp_desktop_document(desktop));
1208 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) clipboard->data;
1209 const char * effectstr = repr->attribute("inkscape:path-effect");
1210 if (!effectstr) {
1211 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Clipboard does not contain a live path effect."));
1212 return;
1213 }
1215 for ( GSList const *itemlist = selection->itemList(); itemlist != NULL; itemlist = g_slist_next(itemlist) ) {
1216 SPItem *item = reinterpret_cast<SPItem*>(itemlist->data);
1217 if ( item && SP_IS_SHAPE(item) ) {
1218 Inkscape::XML::Node *selrepr = (Inkscape::XML::Node *) SP_OBJECT_REPR(item);
1219 selrepr->setAttribute("inkscape:path-effect", effectstr);
1221 // set inkscape:original-d for paths. the other shapes don't need this.
1222 if ( SP_IS_PATH(item) ) {
1223 Inkscape::XML::Node *pathrepr = SP_OBJECT_REPR(item);
1224 if ( ! pathrepr->attribute("inkscape:original-d") ) {
1225 pathrepr->setAttribute("inkscape:original-d", pathrepr->attribute("d"));
1226 }
1227 }
1228 }
1229 }
1231 sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_LIVEPATHEFFECT,
1232 _("Paste live path effect"));
1233 }
1235 void sp_selection_paste_size (bool apply_x, bool apply_y)
1236 {
1237 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1238 if (desktop == NULL) return;
1240 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1242 // check if something is in the clipboard
1243 if (!size_clipboard) {
1244 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1245 return;
1246 }
1248 // check if something is selected
1249 if (selection->isEmpty()) {
1250 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste size to."));
1251 return;
1252 }
1254 NR::Maybe<NR::Rect> current = selection->bounds();
1255 if ( !current || current->isEmpty() ) {
1256 return;
1257 }
1259 double scale_x = size_clipboard->extent(NR::X) / current->extent(NR::X);
1260 double scale_y = size_clipboard->extent(NR::Y) / current->extent(NR::Y);
1262 sp_selection_scale_relative (selection, current->midpoint(),
1263 NR::scale(
1264 apply_x? scale_x : (desktop->isToolboxButtonActive ("lock")? scale_y : 1.0),
1265 apply_y? scale_y : (desktop->isToolboxButtonActive ("lock")? scale_x : 1.0)));
1267 sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_SIZE,
1268 _("Paste size"));
1269 }
1271 void sp_selection_paste_size_separately (bool apply_x, bool apply_y)
1272 {
1273 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1274 if (desktop == NULL) return;
1276 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1278 // check if something is in the clipboard
1279 if ( !size_clipboard ) {
1280 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1281 return;
1282 }
1284 // check if something is selected
1285 if (selection->isEmpty()) {
1286 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste size to."));
1287 return;
1288 }
1290 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1291 SPItem *item = SP_ITEM(l->data);
1293 NR::Maybe<NR::Rect> current = sp_item_bbox_desktop(item);
1294 if ( !current || current->isEmpty() ) {
1295 continue;
1296 }
1298 double scale_x = size_clipboard->extent(NR::X) / current->extent(NR::X);
1299 double scale_y = size_clipboard->extent(NR::Y) / current->extent(NR::Y);
1301 sp_item_scale_rel (item,
1302 NR::scale(
1303 apply_x? scale_x : (desktop->isToolboxButtonActive ("lock")? scale_y : 1.0),
1304 apply_y? scale_y : (desktop->isToolboxButtonActive ("lock")? scale_x : 1.0)));
1306 }
1308 sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_SIZE_SEPARATELY,
1309 _("Paste size separately"));
1310 }
1312 void sp_selection_to_next_layer ()
1313 {
1314 SPDesktop *dt = SP_ACTIVE_DESKTOP;
1316 Inkscape::Selection *selection = sp_desktop_selection(dt);
1318 // check if something is selected
1319 if (selection->isEmpty()) {
1320 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer above."));
1321 return;
1322 }
1324 const GSList *items = g_slist_copy ((GSList *) selection->itemList());
1326 bool no_more = false; // Set to true, if no more layers above
1327 SPObject *next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer());
1328 if (next) {
1329 GSList *temp_clip = NULL;
1330 sp_selection_copy_impl (items, &temp_clip, NULL, NULL, sp_document_repr_doc(dt->doc())); // we're in the same doc, so no need to copy defs
1331 sp_selection_delete_impl (items);
1332 next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
1333 GSList *copied;
1334 if(next) {
1335 copied = sp_selection_paste_impl (sp_desktop_document (dt), next, &temp_clip, NULL);
1336 } else {
1337 copied = sp_selection_paste_impl (sp_desktop_document (dt), dt->currentLayer(), &temp_clip, NULL);
1338 no_more = true;
1339 }
1340 selection->setReprList((GSList const *) copied);
1341 g_slist_free (copied);
1342 if (temp_clip) g_slist_free (temp_clip);
1343 if (next) dt->setCurrentLayer(next);
1344 sp_document_done(sp_desktop_document (dt), SP_VERB_LAYER_MOVE_TO_NEXT,
1345 _("Raise to next layer"));
1346 } else {
1347 no_more = true;
1348 }
1350 if (no_more) {
1351 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers above."));
1352 }
1354 g_slist_free ((GSList *) items);
1355 }
1357 void sp_selection_to_prev_layer ()
1358 {
1359 SPDesktop *dt = SP_ACTIVE_DESKTOP;
1361 Inkscape::Selection *selection = sp_desktop_selection(dt);
1363 // check if something is selected
1364 if (selection->isEmpty()) {
1365 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer below."));
1366 return;
1367 }
1369 const GSList *items = g_slist_copy ((GSList *) selection->itemList());
1371 bool no_more = false; // Set to true, if no more layers below
1372 SPObject *next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer());
1373 if (next) {
1374 GSList *temp_clip = NULL;
1375 sp_selection_copy_impl (items, &temp_clip, NULL, NULL, sp_document_repr_doc(dt->doc())); // we're in the same doc, so no need to copy defs
1376 sp_selection_delete_impl (items);
1377 next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
1378 GSList *copied;
1379 if(next) {
1380 copied = sp_selection_paste_impl (sp_desktop_document (dt), next, &temp_clip, NULL);
1381 } else {
1382 copied = sp_selection_paste_impl (sp_desktop_document (dt), dt->currentLayer(), &temp_clip, NULL);
1383 no_more = true;
1384 }
1385 selection->setReprList((GSList const *) copied);
1386 g_slist_free (copied);
1387 if (temp_clip) g_slist_free (temp_clip);
1388 if (next) dt->setCurrentLayer(next);
1389 sp_document_done(sp_desktop_document (dt), SP_VERB_LAYER_MOVE_TO_PREV,
1390 _("Lower to previous layer"));
1391 } else {
1392 no_more = true;
1393 }
1395 if (no_more) {
1396 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers below."));
1397 }
1399 g_slist_free ((GSList *) items);
1400 }
1402 bool
1403 selection_contains_original (SPItem *item, Inkscape::Selection *selection)
1404 {
1405 bool contains_original = false;
1407 bool is_use = SP_IS_USE(item);
1408 SPItem *item_use = item;
1409 SPItem *item_use_first = item;
1410 while (is_use && item_use && !contains_original)
1411 {
1412 item_use = sp_use_get_original (SP_USE(item_use));
1413 contains_original |= selection->includes(item_use);
1414 if (item_use == item_use_first)
1415 break;
1416 is_use = SP_IS_USE(item_use);
1417 }
1419 // If it's a tref, check whether the object containing the character
1420 // data is part of the selection
1421 if (!contains_original && SP_IS_TREF(item)) {
1422 contains_original = selection->includes(SP_TREF(item)->getObjectReferredTo());
1423 }
1425 return contains_original;
1426 }
1429 bool
1430 selection_contains_both_clone_and_original (Inkscape::Selection *selection)
1431 {
1432 bool clone_with_original = false;
1433 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1434 SPItem *item = SP_ITEM(l->data);
1435 clone_with_original |= selection_contains_original(item, selection);
1436 if (clone_with_original)
1437 break;
1438 }
1439 return clone_with_original;
1440 }
1443 /** Apply matrix to the selection. \a set_i2d is normally true, which means objects are in the
1444 original transform, synced with their reprs, and need to jump to the new transform in one go. A
1445 value of set_i2d==false is only used by seltrans when it's dragging objects live (not outlines); in
1446 that case, items are already in the new position, but the repr is in the old, and this function
1447 then simply updates the repr from item->transform.
1448 */
1449 void sp_selection_apply_affine(Inkscape::Selection *selection, NR::Matrix const &affine, bool set_i2d)
1450 {
1451 if (selection->isEmpty())
1452 return;
1454 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1455 SPItem *item = SP_ITEM(l->data);
1457 NR::Point old_center(0,0);
1458 if (set_i2d && item->isCenterSet())
1459 old_center = item->getCenter();
1461 #if 0 /* Re-enable this once persistent guides have a graphical indication.
1462 At the time of writing, this is the only place to re-enable. */
1463 sp_item_update_cns(*item, selection->desktop());
1464 #endif
1466 // we're moving both a clone and its original or any ancestor in clone chain?
1467 bool transform_clone_with_original = selection_contains_original(item, selection);
1468 // ...both a text-on-path and its path?
1469 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)))) ));
1470 // ...both a flowtext and its frame?
1471 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)
1472 // ...both an offset and its source?
1473 bool transform_offset_with_source = (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) && selection->includes( sp_offset_get_source (SP_OFFSET(item)) );
1475 // If we're moving a connector, we want to detach it
1476 // from shapes that aren't part of the selection, but
1477 // leave it attached if they are
1478 if (cc_item_is_connector(item)) {
1479 SPItem *attItem[2];
1480 SP_PATH(item)->connEndPair.getAttachedItems(attItem);
1482 for (int n = 0; n < 2; ++n) {
1483 if (!selection->includes(attItem[n])) {
1484 sp_conn_end_detach(item, n);
1485 }
1486 }
1487 }
1489 // "clones are unmoved when original is moved" preference
1490 int compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
1491 bool prefs_unmoved = (compensation == SP_CLONE_COMPENSATION_UNMOVED);
1492 bool prefs_parallel = (compensation == SP_CLONE_COMPENSATION_PARALLEL);
1494 // If this is a clone and it's selected along with its original, do not move it; it will feel the
1495 // transform of its original and respond to it itself. Without this, a clone is doubly
1496 // transformed, very unintuitive.
1497 // Same for textpath if we are also doing ANY transform to its path: do not touch textpath,
1498 // letters cannot be squeezed or rotated anyway, they only refill the changed path.
1499 // Same for linked offset if we are also moving its source: do not move it.
1500 if (transform_textpath_with_path || transform_offset_with_source) {
1501 // restore item->transform field from the repr, in case it was changed by seltrans
1502 sp_object_read_attr (SP_OBJECT (item), "transform");
1504 } else if (transform_flowtext_with_frame) {
1505 // apply the inverse of the region's transform to the <use> so that the flow remains
1506 // the same (even though the output itself gets transformed)
1507 for (SPObject *region = item->firstChild() ; region ; region = SP_OBJECT_NEXT(region)) {
1508 if (!SP_IS_FLOWREGION(region) && !SP_IS_FLOWREGIONEXCLUDE(region))
1509 continue;
1510 for (SPObject *use = region->firstChild() ; use ; use = SP_OBJECT_NEXT(use)) {
1511 if (!SP_IS_USE(use)) continue;
1512 sp_item_write_transform(SP_USE(use), SP_OBJECT_REPR(use), item->transform.inverse(), NULL);
1513 }
1514 }
1515 } else if (transform_clone_with_original) {
1516 // We are transforming a clone along with its original. The below matrix juggling is
1517 // necessary to ensure that they transform as a whole, i.e. the clone's induced
1518 // transform and its move compensation are both cancelled out.
1520 // restore item->transform field from the repr, in case it was changed by seltrans
1521 sp_object_read_attr (SP_OBJECT (item), "transform");
1523 // calculate the matrix we need to apply to the clone to cancel its induced transform from its original
1524 NR::Matrix parent_transform = sp_item_i2root_affine(SP_ITEM(SP_OBJECT_PARENT (item)));
1525 NR::Matrix t = parent_transform * matrix_to_desktop (matrix_from_desktop (affine, item), item) * parent_transform.inverse();
1526 NR::Matrix t_inv =parent_transform * matrix_to_desktop (matrix_from_desktop (affine.inverse(), item), item) * parent_transform.inverse();
1527 NR::Matrix result = t_inv * item->transform * t;
1529 if ((prefs_parallel || prefs_unmoved) && affine.is_translation()) {
1530 // we need to cancel out the move compensation, too
1532 // find out the clone move, same as in sp_use_move_compensate
1533 NR::Matrix parent = sp_use_get_parent_transform (SP_USE(item));
1534 NR::Matrix clone_move = parent.inverse() * t * parent;
1536 if (prefs_parallel) {
1537 NR::Matrix move = result * clone_move * t_inv;
1538 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &move);
1540 } else if (prefs_unmoved) {
1541 //if (SP_IS_USE(sp_use_get_original(SP_USE(item))))
1542 // clone_move = NR::identity();
1543 NR::Matrix move = result * clone_move;
1544 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &t);
1545 }
1547 } else {
1548 // just apply the result
1549 sp_item_write_transform(item, SP_OBJECT_REPR(item), result, &t);
1550 }
1552 } else {
1553 if (set_i2d) {
1554 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * affine);
1555 }
1556 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, NULL);
1557 }
1559 // if we're moving the actual object, not just updating the repr, we can transform the
1560 // center by the same matrix (only necessary for non-translations)
1561 if (set_i2d && item->isCenterSet() && !affine.is_translation()) {
1562 item->setCenter(old_center * affine);
1563 SP_OBJECT(item)->updateRepr();
1564 }
1565 }
1566 }
1568 void sp_selection_remove_transform()
1569 {
1570 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1571 if (desktop == NULL)
1572 return;
1574 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1576 GSList const *l = (GSList *) selection->reprList();
1577 while (l != NULL) {
1578 sp_repr_set_attr((Inkscape::XML::Node*)l->data, "transform", NULL);
1579 l = l->next;
1580 }
1582 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_FLATTEN,
1583 _("Remove transform"));
1584 }
1586 void
1587 sp_selection_scale_absolute(Inkscape::Selection *selection,
1588 double const x0, double const x1,
1589 double const y0, double const y1)
1590 {
1591 if (selection->isEmpty())
1592 return;
1594 NR::Maybe<NR::Rect> const bbox(selection->bounds());
1595 if ( !bbox || bbox->isEmpty() ) {
1596 return;
1597 }
1599 NR::translate const p2o(-bbox->min());
1601 NR::scale const newSize(x1 - x0,
1602 y1 - y0);
1603 NR::scale const scale( newSize / NR::scale(bbox->dimensions()) );
1604 NR::translate const o2n(x0, y0);
1605 NR::Matrix const final( p2o * scale * o2n );
1607 sp_selection_apply_affine(selection, final);
1608 }
1611 void sp_selection_scale_relative(Inkscape::Selection *selection, NR::Point const &align, NR::scale const &scale)
1612 {
1613 if (selection->isEmpty())
1614 return;
1616 NR::Maybe<NR::Rect> const bbox(selection->bounds());
1618 if ( !bbox || bbox->isEmpty() ) {
1619 return;
1620 }
1622 // FIXME: ARBITRARY LIMIT: don't try to scale above 1 Mpx, it won't display properly and will crash sooner or later anyway
1623 if ( bbox->extent(NR::X) * scale[NR::X] > 1e6 ||
1624 bbox->extent(NR::Y) * scale[NR::Y] > 1e6 )
1625 {
1626 return;
1627 }
1629 NR::translate const n2d(-align);
1630 NR::translate const d2n(align);
1631 NR::Matrix const final( n2d * scale * d2n );
1632 sp_selection_apply_affine(selection, final);
1633 }
1635 void
1636 sp_selection_rotate_relative(Inkscape::Selection *selection, NR::Point const ¢er, gdouble const angle_degrees)
1637 {
1638 NR::translate const d2n(center);
1639 NR::translate const n2d(-center);
1640 NR::rotate const rotate(rotate_degrees(angle_degrees));
1641 NR::Matrix const final( NR::Matrix(n2d) * rotate * d2n );
1642 sp_selection_apply_affine(selection, final);
1643 }
1645 void
1646 sp_selection_skew_relative(Inkscape::Selection *selection, NR::Point const &align, double dx, double dy)
1647 {
1648 NR::translate const d2n(align);
1649 NR::translate const n2d(-align);
1650 NR::Matrix const skew(1, dy,
1651 dx, 1,
1652 0, 0);
1653 NR::Matrix const final( n2d * skew * d2n );
1654 sp_selection_apply_affine(selection, final);
1655 }
1657 void sp_selection_move_relative(Inkscape::Selection *selection, NR::Point const &move)
1658 {
1659 sp_selection_apply_affine(selection, NR::Matrix(NR::translate(move)));
1660 }
1662 void sp_selection_move_relative(Inkscape::Selection *selection, double dx, double dy)
1663 {
1664 sp_selection_apply_affine(selection, NR::Matrix(NR::translate(dx, dy)));
1665 }
1668 /**
1669 * \brief sp_selection_rotate_90
1670 *
1671 * This function rotates selected objects 90 degrees clockwise.
1672 *
1673 */
1675 void sp_selection_rotate_90_cw()
1676 {
1677 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1679 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1681 if (selection->isEmpty())
1682 return;
1684 GSList const *l = selection->itemList();
1685 NR::rotate const rot_neg_90(NR::Point(0, -1));
1686 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1687 SPItem *item = SP_ITEM(l2->data);
1688 sp_item_rotate_rel(item, rot_neg_90);
1689 }
1691 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_ROTATE_90_CCW,
1692 _("Rotate 90° CW"));
1693 }
1696 /**
1697 * \brief sp_selection_rotate_90_ccw
1698 *
1699 * This function rotates selected objects 90 degrees counter-clockwise.
1700 *
1701 */
1703 void sp_selection_rotate_90_ccw()
1704 {
1705 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1707 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1709 if (selection->isEmpty())
1710 return;
1712 GSList const *l = selection->itemList();
1713 NR::rotate const rot_neg_90(NR::Point(0, 1));
1714 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1715 SPItem *item = SP_ITEM(l2->data);
1716 sp_item_rotate_rel(item, rot_neg_90);
1717 }
1719 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_ROTATE_90_CW,
1720 _("Rotate 90° CCW"));
1721 }
1723 void
1724 sp_selection_rotate(Inkscape::Selection *selection, gdouble const angle_degrees)
1725 {
1726 if (selection->isEmpty())
1727 return;
1729 NR::Maybe<NR::Point> center = selection->center();
1730 if (!center) {
1731 return;
1732 }
1734 sp_selection_rotate_relative(selection, *center, angle_degrees);
1736 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1737 ( ( angle_degrees > 0 )
1738 ? "selector:rotate:ccw"
1739 : "selector:rotate:cw" ),
1740 SP_VERB_CONTEXT_SELECT,
1741 _("Rotate"));
1742 }
1744 /**
1745 \param angle the angle in "angular pixels", i.e. how many visible pixels must move the outermost point of the rotated object
1746 */
1747 void
1748 sp_selection_rotate_screen(Inkscape::Selection *selection, gdouble angle)
1749 {
1750 if (selection->isEmpty())
1751 return;
1753 NR::Maybe<NR::Rect> const bbox(selection->bounds());
1754 NR::Maybe<NR::Point> center = selection->center();
1756 if ( !bbox || !center ) {
1757 return;
1758 }
1760 gdouble const zoom = selection->desktop()->current_zoom();
1761 gdouble const zmove = angle / zoom;
1762 gdouble const r = NR::L2(bbox->max() - *center);
1764 gdouble const zangle = 180 * atan2(zmove, r) / M_PI;
1766 sp_selection_rotate_relative(selection, *center, zangle);
1768 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1769 ( (angle > 0)
1770 ? "selector:rotate:ccw"
1771 : "selector:rotate:cw" ),
1772 SP_VERB_CONTEXT_SELECT,
1773 _("Rotate by pixels"));
1774 }
1776 void
1777 sp_selection_scale(Inkscape::Selection *selection, gdouble grow)
1778 {
1779 if (selection->isEmpty())
1780 return;
1782 NR::Maybe<NR::Rect> const bbox(selection->bounds());
1783 if (!bbox) {
1784 return;
1785 }
1787 NR::Point const center(bbox->midpoint());
1789 // you can't scale "do nizhe pola" (below zero)
1790 double const max_len = bbox->maxExtent();
1791 if ( max_len + grow <= 1e-3 ) {
1792 return;
1793 }
1795 double const times = 1.0 + grow / max_len;
1796 sp_selection_scale_relative(selection, center, NR::scale(times, times));
1798 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1799 ( (grow > 0)
1800 ? "selector:scale:larger"
1801 : "selector:scale:smaller" ),
1802 SP_VERB_CONTEXT_SELECT,
1803 _("Scale"));
1804 }
1806 void
1807 sp_selection_scale_screen(Inkscape::Selection *selection, gdouble grow_pixels)
1808 {
1809 sp_selection_scale(selection,
1810 grow_pixels / selection->desktop()->current_zoom());
1811 }
1813 void
1814 sp_selection_scale_times(Inkscape::Selection *selection, gdouble times)
1815 {
1816 if (selection->isEmpty())
1817 return;
1819 NR::Maybe<NR::Rect> sel_bbox = selection->bounds();
1821 if (!sel_bbox) {
1822 return;
1823 }
1825 NR::Point const center(sel_bbox->midpoint());
1826 sp_selection_scale_relative(selection, center, NR::scale(times, times));
1827 sp_document_done(sp_desktop_document(selection->desktop()), SP_VERB_CONTEXT_SELECT,
1828 _("Scale by whole factor"));
1829 }
1831 void
1832 sp_selection_move(gdouble dx, gdouble dy)
1833 {
1834 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1835 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1836 if (selection->isEmpty()) {
1837 return;
1838 }
1840 sp_selection_move_relative(selection, dx, dy);
1842 if (dx == 0) {
1843 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical", SP_VERB_CONTEXT_SELECT,
1844 _("Move vertically"));
1845 } else if (dy == 0) {
1846 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal", SP_VERB_CONTEXT_SELECT,
1847 _("Move horizontally"));
1848 } else {
1849 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SELECT,
1850 _("Move"));
1851 }
1852 }
1854 void
1855 sp_selection_move_screen(gdouble dx, gdouble dy)
1856 {
1857 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1859 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1860 if (selection->isEmpty()) {
1861 return;
1862 }
1864 // same as sp_selection_move but divide deltas by zoom factor
1865 gdouble const zoom = desktop->current_zoom();
1866 gdouble const zdx = dx / zoom;
1867 gdouble const zdy = dy / zoom;
1868 sp_selection_move_relative(selection, zdx, zdy);
1870 if (dx == 0) {
1871 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical", SP_VERB_CONTEXT_SELECT,
1872 _("Move vertically by pixels"));
1873 } else if (dy == 0) {
1874 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal", SP_VERB_CONTEXT_SELECT,
1875 _("Move horizontally by pixels"));
1876 } else {
1877 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SELECT,
1878 _("Move"));
1879 }
1880 }
1882 namespace {
1884 template <typename D>
1885 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
1886 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
1888 template <typename D>
1889 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items, SPObject *root,
1890 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
1892 struct Forward {
1893 typedef SPObject *Iterator;
1895 static Iterator children(SPObject *o) { return sp_object_first_child(o); }
1896 static Iterator siblings_after(SPObject *o) { return SP_OBJECT_NEXT(o); }
1897 static void dispose(Iterator i) {}
1899 static SPObject *object(Iterator i) { return i; }
1900 static Iterator next(Iterator i) { return SP_OBJECT_NEXT(i); }
1901 };
1903 struct Reverse {
1904 typedef GSList *Iterator;
1906 static Iterator children(SPObject *o) {
1907 return make_list(o->firstChild(), NULL);
1908 }
1909 static Iterator siblings_after(SPObject *o) {
1910 return make_list(SP_OBJECT_PARENT(o)->firstChild(), o);
1911 }
1912 static void dispose(Iterator i) {
1913 g_slist_free(i);
1914 }
1916 static SPObject *object(Iterator i) {
1917 return reinterpret_cast<SPObject *>(i->data);
1918 }
1919 static Iterator next(Iterator i) { return i->next; }
1921 private:
1922 static GSList *make_list(SPObject *object, SPObject *limit) {
1923 GSList *list=NULL;
1924 while ( object != limit ) {
1925 list = g_slist_prepend(list, object);
1926 object = SP_OBJECT_NEXT(object);
1927 }
1928 return list;
1929 }
1930 };
1932 }
1934 void
1935 sp_selection_item_next(void)
1936 {
1937 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1938 g_return_if_fail(desktop != NULL);
1939 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1941 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
1942 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
1943 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
1945 SPObject *root;
1946 if (PREFS_SELECTION_ALL != inlayer) {
1947 root = selection->activeContext();
1948 } else {
1949 root = desktop->currentRoot();
1950 }
1952 SPItem *item=next_item_from_list<Forward>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1954 if (item) {
1955 selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
1956 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1957 scroll_to_show_item(desktop, item);
1958 }
1959 }
1960 }
1962 void
1963 sp_selection_item_prev(void)
1964 {
1965 SPDocument *document = SP_ACTIVE_DOCUMENT;
1966 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1967 g_return_if_fail(document != NULL);
1968 g_return_if_fail(desktop != NULL);
1969 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1971 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
1972 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
1973 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
1975 SPObject *root;
1976 if (PREFS_SELECTION_ALL != inlayer) {
1977 root = selection->activeContext();
1978 } else {
1979 root = desktop->currentRoot();
1980 }
1982 SPItem *item=next_item_from_list<Reverse>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1984 if (item) {
1985 selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
1986 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1987 scroll_to_show_item(desktop, item);
1988 }
1989 }
1990 }
1992 namespace {
1994 template <typename D>
1995 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items,
1996 SPObject *root, bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
1997 {
1998 SPObject *current=root;
1999 while (items) {
2000 SPItem *item=SP_ITEM(items->data);
2001 if ( root->isAncestorOf(item) &&
2002 ( !only_in_viewport || desktop->isWithinViewport(item) ) )
2003 {
2004 current = item;
2005 break;
2006 }
2007 items = items->next;
2008 }
2010 GSList *path=NULL;
2011 while ( current != root ) {
2012 path = g_slist_prepend(path, current);
2013 current = SP_OBJECT_PARENT(current);
2014 }
2016 SPItem *next;
2017 // first, try from the current object
2018 next = next_item<D>(desktop, path, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
2019 g_slist_free(path);
2021 if (!next) { // if we ran out of objects, start over at the root
2022 next = next_item<D>(desktop, NULL, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
2023 }
2025 return next;
2026 }
2028 template <typename D>
2029 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
2030 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
2031 {
2032 typename D::Iterator children;
2033 typename D::Iterator iter;
2035 SPItem *found=NULL;
2037 if (path) {
2038 SPObject *object=reinterpret_cast<SPObject *>(path->data);
2039 g_assert(SP_OBJECT_PARENT(object) == root);
2040 if (desktop->isLayer(object)) {
2041 found = next_item<D>(desktop, path->next, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
2042 }
2043 iter = children = D::siblings_after(object);
2044 } else {
2045 iter = children = D::children(root);
2046 }
2048 while ( iter && !found ) {
2049 SPObject *object=D::object(iter);
2050 if (desktop->isLayer(object)) {
2051 if (PREFS_SELECTION_LAYER != inlayer) { // recurse into sublayers
2052 found = next_item<D>(desktop, NULL, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
2053 }
2054 } else if ( SP_IS_ITEM(object) &&
2055 ( !only_in_viewport || desktop->isWithinViewport(SP_ITEM(object)) ) &&
2056 ( !onlyvisible || !desktop->itemIsHidden(SP_ITEM(object))) &&
2057 ( !onlysensitive || !SP_ITEM(object)->isLocked()) &&
2058 !desktop->isLayer(SP_ITEM(object)) )
2059 {
2060 found = SP_ITEM(object);
2061 }
2062 iter = D::next(iter);
2063 }
2065 D::dispose(children);
2067 return found;
2068 }
2070 }
2072 /**
2073 * If \a item is not entirely visible then adjust visible area to centre on the centre on of
2074 * \a item.
2075 */
2076 void scroll_to_show_item(SPDesktop *desktop, SPItem *item)
2077 {
2078 NR::Rect dbox = desktop->get_display_area();
2079 NR::Maybe<NR::Rect> sbox = sp_item_bbox_desktop(item);
2081 if ( sbox && dbox.contains(*sbox) == false ) {
2082 NR::Point const s_dt = sbox->midpoint();
2083 NR::Point const s_w = desktop->d2w(s_dt);
2084 NR::Point const d_dt = dbox.midpoint();
2085 NR::Point const d_w = desktop->d2w(d_dt);
2086 NR::Point const moved_w( d_w - s_w );
2087 gint const dx = (gint) moved_w[X];
2088 gint const dy = (gint) moved_w[Y];
2089 desktop->scroll_world(dx, dy);
2090 }
2091 }
2094 void
2095 sp_selection_clone()
2096 {
2097 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2098 if (desktop == NULL)
2099 return;
2101 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2103 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
2105 // check if something is selected
2106 if (selection->isEmpty()) {
2107 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object</b> to clone."));
2108 return;
2109 }
2111 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
2113 selection->clear();
2115 // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
2116 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
2118 GSList *newsel = NULL;
2120 while (reprs) {
2121 Inkscape::XML::Node *sel_repr = (Inkscape::XML::Node *) reprs->data;
2122 Inkscape::XML::Node *parent = sp_repr_parent(sel_repr);
2124 Inkscape::XML::Node *clone = xml_doc->createElement("svg:use");
2125 sp_repr_set_attr(clone, "x", "0");
2126 sp_repr_set_attr(clone, "y", "0");
2127 sp_repr_set_attr(clone, "xlink:href", g_strdup_printf("#%s", sel_repr->attribute("id")));
2129 sp_repr_set_attr(clone, "inkscape:transform-center-x", sel_repr->attribute("inkscape:transform-center-x"));
2130 sp_repr_set_attr(clone, "inkscape:transform-center-y", sel_repr->attribute("inkscape:transform-center-y"));
2132 // add the new clone to the top of the original's parent
2133 parent->appendChild(clone);
2135 newsel = g_slist_prepend(newsel, clone);
2136 reprs = g_slist_remove(reprs, sel_repr);
2137 Inkscape::GC::release(clone);
2138 }
2140 // TRANSLATORS: only translate "string" in "context|string".
2141 // For more details, see http://developer.gnome.org/doc/API/2.0/glib/glib-I18N.html#Q-:CAPS
2142 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_CLONE,
2143 Q_("action|Clone"));
2145 selection->setReprList(newsel);
2147 g_slist_free(newsel);
2148 }
2150 void
2151 sp_selection_unlink()
2152 {
2153 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2154 if (!desktop)
2155 return;
2157 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2159 if (selection->isEmpty()) {
2160 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>clone</b> to unlink."));
2161 return;
2162 }
2164 // Get a copy of current selection.
2165 GSList *new_select = NULL;
2166 bool unlinked = false;
2167 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2168 items != NULL;
2169 items = items->next)
2170 {
2171 SPItem *item = (SPItem *) items->data;
2173 if (SP_IS_TEXT(item)) {
2174 SPObject *tspan = sp_tref_convert_to_tspan(SP_OBJECT(item));
2176 if (tspan) {
2177 SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
2178 }
2180 // Set unlink to true, and fall into the next if which
2181 // will include this text item in the new selection
2182 unlinked = true;
2183 }
2185 if (!(SP_IS_USE(item) || SP_IS_TREF(item))) {
2186 // keep the non-use item in the new selection
2187 new_select = g_slist_prepend(new_select, item);
2188 continue;
2189 }
2191 SPItem *unlink;
2192 if (SP_IS_USE(item)) {
2193 unlink = sp_use_unlink(SP_USE(item));
2194 } else /*if (SP_IS_TREF(use))*/ {
2195 unlink = SP_ITEM(sp_tref_convert_to_tspan(SP_OBJECT(item)));
2196 }
2198 unlinked = true;
2199 // Add ungrouped items to the new selection.
2200 new_select = g_slist_prepend(new_select, unlink);
2201 }
2203 if (new_select) { // set new selection
2204 selection->clear();
2205 selection->setList(new_select);
2206 g_slist_free(new_select);
2207 }
2208 if (!unlinked) {
2209 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No clones to unlink</b> in the selection."));
2210 }
2212 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNLINK_CLONE,
2213 _("Unlink clone"));
2214 }
2216 void
2217 sp_select_clone_original()
2218 {
2219 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2220 if (desktop == NULL)
2221 return;
2223 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2225 SPItem *item = selection->singleItem();
2227 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.");
2229 // Check if other than two objects are selected
2230 if (g_slist_length((GSList *) selection->itemList()) != 1 || !item) {
2231 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
2232 return;
2233 }
2235 SPItem *original = NULL;
2236 if (SP_IS_USE(item)) {
2237 original = sp_use_get_original (SP_USE(item));
2238 } else if (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) {
2239 original = sp_offset_get_source (SP_OFFSET(item));
2240 } else if (SP_IS_TEXT_TEXTPATH(item)) {
2241 original = sp_textpath_get_path_item (SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))));
2242 } else if (SP_IS_FLOWTEXT(item)) {
2243 original = SP_FLOWTEXT(item)->get_frame (NULL); // first frame only
2244 } else { // it's an object that we don't know what to do with
2245 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
2246 return;
2247 }
2249 if (!original) {
2250 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>Cannot find</b> the object to select (orphaned clone, offset, textpath, flowed text?)"));
2251 return;
2252 }
2254 for (SPObject *o = original; o && !SP_IS_ROOT(o); o = SP_OBJECT_PARENT (o)) {
2255 if (SP_IS_DEFS (o)) {
2256 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("The object you're trying to select is <b>not visible</b> (it is in <defs>)"));
2257 return;
2258 }
2259 }
2261 if (original) {
2262 selection->clear();
2263 selection->set(original);
2264 if (SP_CYCLING == SP_CYCLE_FOCUS) {
2265 scroll_to_show_item(desktop, original);
2266 }
2267 }
2268 }
2270 void
2271 sp_selection_tile(bool apply)
2272 {
2273 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2274 if (desktop == NULL)
2275 return;
2277 SPDocument *doc = sp_desktop_document(desktop);
2278 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2280 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2282 // check if something is selected
2283 if (selection->isEmpty()) {
2284 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to pattern."));
2285 return;
2286 }
2288 sp_document_ensure_up_to_date(doc);
2289 NR::Maybe<NR::Rect> r = selection->bounds();
2290 if ( !r || r->isEmpty() ) {
2291 return;
2292 }
2294 // calculate the transform to be applied to objects to move them to 0,0
2295 NR::Point move_p = NR::Point(0, sp_document_height(doc)) - (r->min() + NR::Point (0, r->extent(NR::Y)));
2296 move_p[NR::Y] = -move_p[NR::Y];
2297 NR::Matrix move = NR::Matrix (NR::translate (move_p));
2299 GSList *items = g_slist_copy((GSList *) selection->itemList());
2301 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2303 // bottommost object, after sorting
2304 SPObject *parent = SP_OBJECT_PARENT (items->data);
2306 NR::Matrix parent_transform = sp_item_i2root_affine(SP_ITEM(parent));
2308 // remember the position of the first item
2309 gint pos = SP_OBJECT_REPR (items->data)->position();
2311 // create a list of duplicates
2312 GSList *repr_copies = NULL;
2313 for (GSList *i = items; i != NULL; i = i->next) {
2314 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2315 repr_copies = g_slist_prepend (repr_copies, dup);
2316 }
2318 NR::Rect bounds(desktop->dt2doc(r->min()), desktop->dt2doc(r->max()));
2320 if (apply) {
2321 // delete objects so that their clones don't get alerted; this object will be restored shortly
2322 for (GSList *i = items; i != NULL; i = i->next) {
2323 SPObject *item = SP_OBJECT (i->data);
2324 item->deleteObject (false);
2325 }
2326 }
2328 // Hack: Temporarily set clone compensation to unmoved, so that we can move clone-originals
2329 // without disturbing clones.
2330 // See ActorAlign::on_button_click() in src/ui/dialog/align-and-distribute.cpp
2331 int saved_compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2332 prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2334 const gchar *pat_id = pattern_tile (repr_copies, bounds, doc,
2335 NR::Matrix(NR::translate(desktop->dt2doc(NR::Point(r->min()[NR::X], r->max()[NR::Y])))) * parent_transform.inverse(), parent_transform * move);
2337 // restore compensation setting
2338 prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
2340 if (apply) {
2341 Inkscape::XML::Node *rect = xml_doc->createElement("svg:rect");
2342 rect->setAttribute("style", g_strdup_printf("stroke:none;fill:url(#%s)", pat_id));
2344 NR::Point min = bounds.min() * parent_transform.inverse();
2345 NR::Point max = bounds.max() * parent_transform.inverse();
2347 sp_repr_set_svg_double(rect, "width", max[NR::X] - min[NR::X]);
2348 sp_repr_set_svg_double(rect, "height", max[NR::Y] - min[NR::Y]);
2349 sp_repr_set_svg_double(rect, "x", min[NR::X]);
2350 sp_repr_set_svg_double(rect, "y", min[NR::Y]);
2352 // restore parent and position
2353 SP_OBJECT_REPR (parent)->appendChild(rect);
2354 rect->setPosition(pos > 0 ? pos : 0);
2355 SPItem *rectangle = (SPItem *) sp_desktop_document (desktop)->getObjectByRepr(rect);
2357 Inkscape::GC::release(rect);
2359 selection->clear();
2360 selection->set(rectangle);
2361 }
2363 g_slist_free (items);
2365 sp_document_done (doc, SP_VERB_EDIT_TILE,
2366 _("Objects to pattern"));
2367 }
2369 void
2370 sp_selection_untile()
2371 {
2372 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2373 if (desktop == NULL)
2374 return;
2376 SPDocument *doc = sp_desktop_document(desktop);
2377 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2379 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2381 // check if something is selected
2382 if (selection->isEmpty()) {
2383 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object with pattern fill</b> to extract objects from."));
2384 return;
2385 }
2387 GSList *new_select = NULL;
2389 bool did = false;
2391 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2392 items != NULL;
2393 items = items->next) {
2395 SPItem *item = (SPItem *) items->data;
2397 SPStyle *style = SP_OBJECT_STYLE (item);
2399 if (!style || !style->fill.isPaintserver())
2400 continue;
2402 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
2404 if (!SP_IS_PATTERN(server))
2405 continue;
2407 did = true;
2409 SPPattern *pattern = pattern_getroot (SP_PATTERN (server));
2411 NR::Matrix pat_transform = pattern_patternTransform (SP_PATTERN (server));
2412 pat_transform *= item->transform;
2414 for (SPObject *child = sp_object_first_child(SP_OBJECT(pattern)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2415 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate(xml_doc);
2416 SPItem *i = SP_ITEM (desktop->currentLayer()->appendChildRepr(copy));
2418 // FIXME: relink clones to the new canvas objects
2419 // use SPObject::setid when mental finishes it to steal ids of
2421 // this is needed to make sure the new item has curve (simply requestDisplayUpdate does not work)
2422 sp_document_ensure_up_to_date (doc);
2424 NR::Matrix transform( i->transform * pat_transform );
2425 sp_item_write_transform(i, SP_OBJECT_REPR(i), transform);
2427 new_select = g_slist_prepend(new_select, i);
2428 }
2430 SPCSSAttr *css = sp_repr_css_attr_new ();
2431 sp_repr_css_set_property (css, "fill", "none");
2432 sp_repr_css_change (SP_OBJECT_REPR (item), css, "style");
2433 }
2435 if (!did) {
2436 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No pattern fills</b> in the selection."));
2437 } else {
2438 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNTILE,
2439 _("Pattern to objects"));
2440 selection->setList(new_select);
2441 }
2442 }
2444 void
2445 sp_selection_get_export_hints (Inkscape::Selection *selection, const char **filename, float *xdpi, float *ydpi)
2446 {
2447 if (selection->isEmpty()) {
2448 return;
2449 }
2451 const GSList * reprlst = selection->reprList();
2452 bool filename_search = TRUE;
2453 bool xdpi_search = TRUE;
2454 bool ydpi_search = TRUE;
2456 for(; reprlst != NULL &&
2457 filename_search &&
2458 xdpi_search &&
2459 ydpi_search;
2460 reprlst = reprlst->next) {
2461 const gchar * dpi_string;
2462 Inkscape::XML::Node * repr = (Inkscape::XML::Node *)reprlst->data;
2464 if (filename_search) {
2465 *filename = repr->attribute("inkscape:export-filename");
2466 if (*filename != NULL)
2467 filename_search = FALSE;
2468 }
2470 if (xdpi_search) {
2471 dpi_string = NULL;
2472 dpi_string = repr->attribute("inkscape:export-xdpi");
2473 if (dpi_string != NULL) {
2474 *xdpi = atof(dpi_string);
2475 xdpi_search = FALSE;
2476 }
2477 }
2479 if (ydpi_search) {
2480 dpi_string = NULL;
2481 dpi_string = repr->attribute("inkscape:export-ydpi");
2482 if (dpi_string != NULL) {
2483 *ydpi = atof(dpi_string);
2484 ydpi_search = FALSE;
2485 }
2486 }
2487 }
2488 }
2490 void
2491 sp_document_get_export_hints (SPDocument * doc, const char **filename, float *xdpi, float *ydpi)
2492 {
2493 Inkscape::XML::Node * repr = sp_document_repr_root(doc);
2494 const gchar * dpi_string;
2496 *filename = repr->attribute("inkscape:export-filename");
2498 dpi_string = NULL;
2499 dpi_string = repr->attribute("inkscape:export-xdpi");
2500 if (dpi_string != NULL) {
2501 *xdpi = atof(dpi_string);
2502 }
2504 dpi_string = NULL;
2505 dpi_string = repr->attribute("inkscape:export-ydpi");
2506 if (dpi_string != NULL) {
2507 *ydpi = atof(dpi_string);
2508 }
2509 }
2511 void
2512 sp_selection_create_bitmap_copy ()
2513 {
2514 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2515 if (desktop == NULL)
2516 return;
2518 SPDocument *document = sp_desktop_document(desktop);
2519 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
2521 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2523 // check if something is selected
2524 if (selection->isEmpty()) {
2525 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to make a bitmap copy."));
2526 return;
2527 }
2529 // Get the bounding box of the selection
2530 NRRect bbox;
2531 sp_document_ensure_up_to_date (document);
2532 selection->bounds(&bbox);
2533 if (NR_RECT_DFLS_TEST_EMPTY(&bbox)) {
2534 return; // exceptional situation, so not bother with a translatable error message, just quit quietly
2535 }
2537 // List of the items to show; all others will be hidden
2538 GSList *items = g_slist_copy ((GSList *) selection->itemList());
2540 // Sort items so that the topmost comes last
2541 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
2543 // Generate a random value from the current time (you may create bitmap from the same object(s)
2544 // multiple times, and this is done so that they don't clash)
2545 GTimeVal cu;
2546 g_get_current_time (&cu);
2547 guint current = (int) (cu.tv_sec * 1000000 + cu.tv_usec) % 1024;
2549 // Create the filename
2550 gchar *filename = g_strdup_printf ("%s-%s-%u.png", document->name, SP_OBJECT_REPR(items->data)->attribute("id"), current);
2551 // Imagemagick is known not to handle spaces in filenames, so we replace anything but letters,
2552 // digits, and a few other chars, with "_"
2553 filename = g_strcanon (filename, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.=+~$#@^&!?", '_');
2554 // Build the complete path by adding document->base if set
2555 gchar *filepath = g_build_filename (document->base?document->base:"", filename, NULL);
2557 //g_print ("%s\n", filepath);
2559 // Remember parent and z-order of the topmost one
2560 gint pos = SP_OBJECT_REPR(g_slist_last(items)->data)->position();
2561 SPObject *parent_object = SP_OBJECT_PARENT(g_slist_last(items)->data);
2562 Inkscape::XML::Node *parent = SP_OBJECT_REPR(parent_object);
2564 // Calculate resolution
2565 double res;
2566 int const prefs_res = prefs_get_int_attribute ("options.createbitmap", "resolution", 0);
2567 int const prefs_min = prefs_get_int_attribute ("options.createbitmap", "minsize", 0);
2568 if (0 < prefs_res) {
2569 // If it's given explicitly in prefs, take it
2570 res = prefs_res;
2571 } else if (0 < prefs_min) {
2572 // If minsize is given, look up minimum bitmap size (default 250 pixels) and calculate resolution from it
2573 res = PX_PER_IN * prefs_min / MIN ((bbox.x1 - bbox.x0), (bbox.y1 - bbox.y0));
2574 } else {
2575 float hint_xdpi = 0, hint_ydpi = 0;
2576 const char *hint_filename;
2577 // take resolution hint from the selected objects
2578 sp_selection_get_export_hints (selection, &hint_filename, &hint_xdpi, &hint_ydpi);
2579 if (hint_xdpi != 0) {
2580 res = hint_xdpi;
2581 } else {
2582 // take resolution hint from the document
2583 sp_document_get_export_hints (document, &hint_filename, &hint_xdpi, &hint_ydpi);
2584 if (hint_xdpi != 0) {
2585 res = hint_xdpi;
2586 } else {
2587 // if all else fails, take the default 90 dpi
2588 res = PX_PER_IN;
2589 }
2590 }
2591 }
2593 // The width and height of the bitmap in pixels
2594 unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * res / PX_PER_IN);
2595 unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * res / PX_PER_IN);
2597 // Find out if we have to run a filter
2598 const gchar *run = NULL;
2599 const gchar *filter = prefs_get_string_attribute ("options.createbitmap", "filter");
2600 if (filter) {
2601 // filter command is given;
2602 // see if we have a parameter to pass to it
2603 const gchar *param1 = prefs_get_string_attribute ("options.createbitmap", "filter_param1");
2604 if (param1) {
2605 if (param1[strlen(param1) - 1] == '%') {
2606 // if the param string ends with %, interpret it as a percentage of the image's max dimension
2607 gchar p1[256];
2608 g_ascii_dtostr (p1, 256, ceil (g_ascii_strtod (param1, NULL) * MAX(width, height) / 100));
2609 // the first param is always the image filename, the second is param1
2610 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, p1);
2611 } else {
2612 // otherwise pass the param1 unchanged
2613 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, param1);
2614 }
2615 } else {
2616 // run without extra parameter
2617 run = g_strdup_printf ("%s \"%s\"", filter, filepath);
2618 }
2619 }
2621 // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
2622 NR::Matrix eek = sp_item_i2d_affine (SP_ITEM(parent_object));
2623 NR::Matrix t;
2625 double shift_x = bbox.x0;
2626 double shift_y = bbox.y1;
2627 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
2628 shift_x = round (shift_x);
2629 shift_y = -round (-shift_y); // this gets correct rounding despite coordinate inversion, remove the negations when the inversion is gone
2630 }
2631 t = NR::scale(1, -1) * NR::translate (shift_x, shift_y) * eek.inverse();
2633 // Do the export
2634 sp_export_png_file(document, filepath,
2635 bbox.x0, bbox.y0, bbox.x1, bbox.y1,
2636 width, height, res, res,
2637 (guint32) 0xffffff00,
2638 NULL, NULL,
2639 true, /*bool force_overwrite,*/
2640 items);
2642 g_slist_free (items);
2644 // Run filter, if any
2645 if (run) {
2646 g_print ("Running external filter: %s\n", run);
2647 system (run);
2648 }
2650 // Import the image back
2651 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filepath, NULL);
2652 if (pb) {
2653 // Create the repr for the image
2654 Inkscape::XML::Node * repr = xml_doc->createElement("svg:image");
2655 repr->setAttribute("xlink:href", filename);
2656 repr->setAttribute("sodipodi:absref", filepath);
2657 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
2658 sp_repr_set_svg_double(repr, "width", width);
2659 sp_repr_set_svg_double(repr, "height", height);
2660 } else {
2661 sp_repr_set_svg_double(repr, "width", (bbox.x1 - bbox.x0));
2662 sp_repr_set_svg_double(repr, "height", (bbox.y1 - bbox.y0));
2663 }
2665 // Write transform
2666 gchar *c=sp_svg_transform_write(t);
2667 repr->setAttribute("transform", c);
2668 g_free(c);
2670 // add the new repr to the parent
2671 parent->appendChild(repr);
2673 // move to the saved position
2674 repr->setPosition(pos > 0 ? pos + 1 : 1);
2676 // Set selection to the new image
2677 selection->clear();
2678 selection->add(repr);
2680 // Clean up
2681 Inkscape::GC::release(repr);
2682 gdk_pixbuf_unref (pb);
2684 // Complete undoable transaction
2685 sp_document_done (document, SP_VERB_SELECTION_CREATE_BITMAP,
2686 _("Create bitmap"));
2687 }
2689 g_free (filename);
2690 g_free (filepath);
2691 }
2693 /**
2694 * \brief sp_selection_set_mask
2695 *
2696 * This function creates a mask or clipPath from selection
2697 * Two different modes:
2698 * if applyToLayer, all selection is moved to DEFS as mask/clippath
2699 * and is applied to current layer
2700 * otherwise, topmost object is used as mask for other objects
2701 * If \a apply_clip_path parameter is true, clipPath is created, otherwise mask
2702 *
2703 */
2704 void
2705 sp_selection_set_mask(bool apply_clip_path, bool apply_to_layer)
2706 {
2707 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2708 if (desktop == NULL)
2709 return;
2711 SPDocument *doc = sp_desktop_document(desktop);
2712 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2714 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2716 // check if something is selected
2717 bool is_empty = selection->isEmpty();
2718 if ( apply_to_layer && is_empty) {
2719 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to create clippath or mask from."));
2720 return;
2721 } else if (!apply_to_layer && ( is_empty || NULL == selection->itemList()->next )) {
2722 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select mask object and <b>object(s)</b> to apply clippath or mask to."));
2723 return;
2724 }
2726 // FIXME: temporary patch to prevent crash!
2727 // Remove this when bboxes are fixed to not blow up on an item clipped/masked with its own clone
2728 bool clone_with_original = selection_contains_both_clone_and_original (selection);
2729 if (clone_with_original) {
2730 return; // in this version, you cannot clip/mask an object with its own clone
2731 }
2732 // /END FIXME
2734 sp_document_ensure_up_to_date(doc);
2736 GSList *items = g_slist_copy((GSList *) selection->itemList());
2738 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2740 // create a list of duplicates
2741 GSList *mask_items = NULL;
2742 GSList *apply_to_items = NULL;
2743 GSList *items_to_delete = NULL;
2744 bool topmost = prefs_get_int_attribute ("options.maskobject", "topmost", 1);
2745 bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
2747 if (apply_to_layer) {
2748 // all selected items are used for mask, which is applied to a layer
2749 apply_to_items = g_slist_prepend (apply_to_items, desktop->currentLayer());
2751 for (GSList *i = items; i != NULL; i = i->next) {
2752 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2753 mask_items = g_slist_prepend (mask_items, dup);
2755 if (remove_original) {
2756 SPObject *item = SP_OBJECT (i->data);
2757 items_to_delete = g_slist_prepend (items_to_delete, item);
2758 }
2759 }
2760 } else if (!topmost) {
2761 // topmost item is used as a mask, which is applied to other items in a selection
2762 GSList *i = items;
2763 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2764 mask_items = g_slist_prepend (mask_items, dup);
2766 if (remove_original) {
2767 SPObject *item = SP_OBJECT (i->data);
2768 items_to_delete = g_slist_prepend (items_to_delete, item);
2769 }
2771 for (i = i->next; i != NULL; i = i->next) {
2772 apply_to_items = g_slist_prepend (apply_to_items, i->data);
2773 }
2774 } else {
2775 GSList *i = NULL;
2776 for (i = items; NULL != i->next; i = i->next) {
2777 apply_to_items = g_slist_prepend (apply_to_items, i->data);
2778 }
2780 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2781 mask_items = g_slist_prepend (mask_items, dup);
2783 if (remove_original) {
2784 SPObject *item = SP_OBJECT (i->data);
2785 items_to_delete = g_slist_prepend (items_to_delete, item);
2786 }
2787 }
2789 g_slist_free (items);
2790 items = NULL;
2792 gchar const* attributeName = apply_clip_path ? "clip-path" : "mask";
2793 for (GSList *i = apply_to_items; NULL != i; i = i->next) {
2794 SPItem *item = reinterpret_cast<SPItem *>(i->data);
2795 // inverted object transform should be applied to a mask object,
2796 // as mask is calculated in user space (after applying transform)
2797 NR::Matrix maskTransform (item->transform.inverse());
2799 GSList *mask_items_dup = NULL;
2800 for (GSList *mask_item = mask_items; NULL != mask_item; mask_item = mask_item->next) {
2801 Inkscape::XML::Node *dup = reinterpret_cast<Inkscape::XML::Node *>(mask_item->data)->duplicate(xml_doc);
2802 mask_items_dup = g_slist_prepend (mask_items_dup, dup);
2803 }
2805 const gchar *mask_id = NULL;
2806 if (apply_clip_path) {
2807 mask_id = sp_clippath_create(mask_items_dup, doc, &maskTransform);
2808 } else {
2809 mask_id = sp_mask_create(mask_items_dup, doc, &maskTransform);
2810 }
2812 g_slist_free (mask_items_dup);
2813 mask_items_dup = NULL;
2815 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, g_strdup_printf("url(#%s)", mask_id));
2816 }
2818 g_slist_free (mask_items);
2819 g_slist_free (apply_to_items);
2821 for (GSList *i = items_to_delete; NULL != i; i = i->next) {
2822 SPObject *item = SP_OBJECT (i->data);
2823 item->deleteObject (false);
2824 }
2825 g_slist_free (items_to_delete);
2827 if (apply_clip_path)
2828 sp_document_done (doc, SP_VERB_OBJECT_SET_CLIPPATH, _("Set clipping path"));
2829 else
2830 sp_document_done (doc, SP_VERB_OBJECT_SET_MASK, _("Set mask"));
2831 }
2833 void sp_selection_unset_mask(bool apply_clip_path) {
2834 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2835 if (desktop == NULL)
2836 return;
2838 SPDocument *doc = sp_desktop_document(desktop);
2839 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2840 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2842 // check if something is selected
2843 if (selection->isEmpty()) {
2844 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to remove clippath or mask from."));
2845 return;
2846 }
2848 bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
2849 sp_document_ensure_up_to_date(doc);
2851 gchar const* attributeName = apply_clip_path ? "clip-path" : "mask";
2852 std::map<SPObject*,SPItem*> referenced_objects;
2853 for (GSList const*i = selection->itemList(); NULL != i; i = i->next) {
2854 if (remove_original) {
2855 // remember referenced mask/clippath, so orphaned masks can be moved back to document
2856 SPItem *item = reinterpret_cast<SPItem *>(i->data);
2857 Inkscape::URIReference *uri_ref = NULL;
2859 if (apply_clip_path) {
2860 uri_ref = item->clip_ref;
2861 } else {
2862 uri_ref = item->mask_ref;
2863 }
2865 // collect distinct mask object (and associate with item to apply transform)
2866 if (NULL != uri_ref && NULL != uri_ref->getObject()) {
2867 referenced_objects[uri_ref->getObject()] = item;
2868 }
2869 }
2871 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, "none");
2872 }
2874 // restore mask objects into a document
2875 for ( std::map<SPObject*,SPItem*>::iterator it = referenced_objects.begin() ; it != referenced_objects.end() ; ++it) {
2876 SPObject *obj = (*it).first;
2877 GSList *items_to_move = NULL;
2878 for (SPObject *child = sp_object_first_child(obj) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2879 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate(xml_doc);
2880 items_to_move = g_slist_prepend (items_to_move, copy);
2881 }
2883 if (!obj->isReferenced()) {
2884 // delete from defs if no other object references this mask
2885 obj->deleteObject(false);
2886 }
2888 // remember parent and position of the item to which the clippath/mask was applied
2889 Inkscape::XML::Node *parent = SP_OBJECT_REPR((*it).second)->parent();
2890 gint pos = SP_OBJECT_REPR((*it).second)->position();
2892 for (GSList *i = items_to_move; NULL != i; i = i->next) {
2893 Inkscape::XML::Node *repr = (Inkscape::XML::Node *)i->data;
2895 // insert into parent, restore pos
2896 parent->appendChild(repr);
2897 repr->setPosition((pos + 1) > 0 ? (pos + 1) : 0);
2899 SPItem *mask_item = (SPItem *) sp_desktop_document (desktop)->getObjectByRepr(repr);
2900 selection->add(repr);
2902 // transform mask, so it is moved the same spot where mask was applied
2903 NR::Matrix transform (mask_item->transform);
2904 transform *= (*it).second->transform;
2905 sp_item_write_transform(mask_item, SP_OBJECT_REPR(mask_item), transform);
2906 }
2908 g_slist_free (items_to_move);
2909 }
2911 if (apply_clip_path)
2912 sp_document_done (doc, SP_VERB_OBJECT_UNSET_CLIPPATH, _("Release clipping path"));
2913 else
2914 sp_document_done (doc, SP_VERB_OBJECT_UNSET_MASK, _("Release mask"));
2915 }
2917 void fit_canvas_to_selection(SPDesktop *desktop) {
2918 g_return_if_fail(desktop != NULL);
2919 SPDocument *doc = sp_desktop_document(desktop);
2921 g_return_if_fail(doc != NULL);
2922 g_return_if_fail(desktop->selection != NULL);
2923 g_return_if_fail(!desktop->selection->isEmpty());
2925 NR::Maybe<NR::Rect> const bbox(desktop->selection->bounds());
2926 if (bbox && !bbox->isEmpty()) {
2927 doc->fitToRect(*bbox);
2928 }
2929 };
2931 void fit_canvas_to_drawing(SPDocument *doc) {
2932 g_return_if_fail(doc != NULL);
2934 sp_document_ensure_up_to_date(doc);
2935 SPItem const *const root = SP_ITEM(doc->root);
2936 NR::Maybe<NR::Rect> const bbox(root->getBounds(sp_item_i2r_affine(root)));
2937 if (bbox && !bbox->isEmpty()) {
2938 doc->fitToRect(*bbox);
2939 }
2940 };
2942 void fit_canvas_to_selection_or_drawing(SPDesktop *desktop) {
2943 g_return_if_fail(desktop != NULL);
2944 SPDocument *doc = sp_desktop_document(desktop);
2946 g_return_if_fail(doc != NULL);
2947 g_return_if_fail(desktop->selection != NULL);
2949 if (desktop->selection->isEmpty()) {
2950 fit_canvas_to_drawing(doc);
2951 } else {
2952 fit_canvas_to_selection(desktop);
2953 }
2955 sp_document_done(doc, SP_VERB_FIT_CANVAS_TO_DRAWING,
2956 _("Fit page to selection"));
2957 };
2959 static void itemtree_map(void (*f)(SPItem *, SPDesktop *), SPObject *root, SPDesktop *desktop) {
2960 // don't operate on layers
2961 if (SP_IS_ITEM(root) && !desktop->isLayer(SP_ITEM(root))) {
2962 f(SP_ITEM(root), desktop);
2963 }
2964 for ( SPObject::SiblingIterator iter = root->firstChild() ; iter ; ++iter ) {
2965 //don't recurse into locked layers
2966 if (!(SP_IS_ITEM(&*iter) && desktop->isLayer(SP_ITEM(&*iter)) && SP_ITEM(&*iter)->isLocked())) {
2967 itemtree_map(f, iter, desktop);
2968 }
2969 }
2970 }
2972 static void unlock(SPItem *item, SPDesktop *desktop) {
2973 if (item->isLocked()) {
2974 item->setLocked(FALSE);
2975 }
2976 }
2978 static void unhide(SPItem *item, SPDesktop *desktop) {
2979 if (desktop->itemIsHidden(item)) {
2980 item->setExplicitlyHidden(FALSE);
2981 }
2982 }
2984 static void process_all(void (*f)(SPItem *, SPDesktop *), SPDesktop *dt, bool layer_only) {
2985 if (!dt) return;
2987 SPObject *root;
2988 if (layer_only) {
2989 root = dt->currentLayer();
2990 } else {
2991 root = dt->currentRoot();
2992 }
2994 itemtree_map(f, root, dt);
2995 }
2997 void unlock_all(SPDesktop *dt) {
2998 process_all(&unlock, dt, true);
2999 }
3001 void unlock_all_in_all_layers(SPDesktop *dt) {
3002 process_all(&unlock, dt, false);
3003 }
3005 void unhide_all(SPDesktop *dt) {
3006 process_all(&unhide, dt, true);
3007 }
3009 void unhide_all_in_all_layers(SPDesktop *dt) {
3010 process_all(&unhide, dt, false);
3011 }
3014 GSList * sp_selection_get_clipboard() {
3015 return clipboard;
3016 }
3018 /*
3019 Local Variables:
3020 mode:c++
3021 c-file-style:"stroustrup"
3022 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
3023 indent-tabs-mode:nil
3024 fill-column:99
3025 End:
3026 */
3027 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :