1 #define __SP_SELECTION_CHEMISTRY_C__
3 /** @file
4 * @brief 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 "selection-chemistry.h"
25 #include <gtkmm/clipboard.h>
27 #include "svg/svg.h"
28 #include "inkscape.h"
29 #include "desktop.h"
30 //#include "desktop-style.h"
31 #include "selection.h"
32 #include "tools-switch.h"
33 #include "desktop-handles.h"
34 #include "message-stack.h"
35 #include "sp-item-transform.h"
36 #include "marker.h"
37 #include "sp-use.h"
38 #include "sp-textpath.h"
39 #include "sp-tspan.h"
40 #include "sp-tref.h"
41 #include "sp-flowtext.h"
42 #include "sp-flowregion.h"
43 #include "text-editing.h"
44 #include "text-context.h"
45 #include "connector-context.h"
46 #include "sp-path.h"
47 #include "sp-conn-end.h"
48 #include "dropper-context.h"
49 #include <glibmm/i18n.h>
50 #include "libnr/nr-matrix-rotate-ops.h"
51 #include "libnr/nr-matrix-translate-ops.h"
52 #include "libnr/nr-rotate-fns.h"
53 #include "libnr/nr-scale-ops.h"
54 #include "libnr/nr-scale-translate-ops.h"
55 #include "libnr/nr-translate-matrix-ops.h"
56 #include "libnr/nr-translate-scale-ops.h"
57 #include "xml/repr.h"
58 #include "style.h"
59 #include "document-private.h"
60 #include "sp-gradient.h"
61 #include "sp-gradient-reference.h"
62 #include "sp-linear-gradient-fns.h"
63 #include "sp-pattern.h"
64 #include "sp-radial-gradient-fns.h"
65 #include "sp-namedview.h"
66 #include "prefs-utils.h"
67 #include "sp-offset.h"
68 #include "sp-clippath.h"
69 #include "sp-mask.h"
70 #include "file.h"
71 #include "helper/png-write.h"
72 #include "layer-fns.h"
73 #include "context-fns.h"
74 #include <map>
75 #include <cstring>
76 #include <string>
77 #include "helper/units.h"
78 #include "sp-item.h"
79 #include "box3d.h"
80 #include "unit-constants.h"
81 #include "xml/simple-document.h"
82 #include "sp-filter-reference.h"
83 #include "gradient-drag.h"
84 #include "uri-references.h"
85 #include "libnr/nr-convert2geom.h"
87 // For clippath editing
88 #include "tools-switch.h"
89 #include "shape-editor.h"
90 #include "node-context.h"
91 #include "nodepath.h"
93 #include "ui/clipboard.h"
95 using NR::X;
96 using NR::Y;
98 /* The clipboard handling is in ui/clipboard.cpp now. There are some legacy functions left here,
99 because the layer manipulation code uses them. It should be rewritten specifically
100 for that purpose. */
102 /**
103 * Copies repr and its inherited css style elements, along with the accumulated transform 'full_t',
104 * then prepends the copy to 'clip'.
105 */
106 void sp_selection_copy_one (Inkscape::XML::Node *repr, NR::Matrix full_t, GSList **clip, Inkscape::XML::Document* xml_doc)
107 {
108 Inkscape::XML::Node *copy = repr->duplicate(xml_doc);
110 // copy complete inherited style
111 SPCSSAttr *css = sp_repr_css_attr_inherited(repr, "style");
112 sp_repr_css_set(copy, css, "style");
113 sp_repr_css_attr_unref(css);
115 // write the complete accumulated transform passed to us
116 // (we're dealing with unattached repr, so we write to its attr
117 // instead of using sp_item_set_transform)
118 gchar *affinestr=sp_svg_transform_write(full_t);
119 copy->setAttribute("transform", affinestr);
120 g_free(affinestr);
122 *clip = g_slist_prepend(*clip, copy);
123 }
125 void sp_selection_copy_impl (GSList const *items, GSList **clip, Inkscape::XML::Document* xml_doc)
126 {
127 // Sort items:
128 GSList *sorted_items = g_slist_copy ((GSList *) items);
129 sorted_items = g_slist_sort((GSList *) sorted_items, (GCompareFunc) sp_object_compare_position);
131 // Copy item reprs:
132 for (GSList *i = (GSList *) sorted_items; i != NULL; i = i->next) {
133 sp_selection_copy_one (SP_OBJECT_REPR (i->data), sp_item_i2doc_affine(SP_ITEM (i->data)), clip, xml_doc);
134 }
136 *clip = g_slist_reverse(*clip);
137 g_slist_free ((GSList *) sorted_items);
138 }
140 GSList *sp_selection_paste_impl (SPDocument *doc, SPObject *parent, GSList **clip)
141 {
142 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
144 GSList *copied = NULL;
145 // add objects to document
146 for (GSList *l = *clip; l != NULL; l = l->next) {
147 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
148 Inkscape::XML::Node *copy = repr->duplicate(xml_doc);
150 // premultiply the item transform by the accumulated parent transform in the paste layer
151 NR::Matrix local = sp_item_i2doc_affine(SP_ITEM(parent));
152 if (!local.test_identity()) {
153 gchar const *t_str = copy->attribute("transform");
154 NR::Matrix item_t (NR::identity());
155 if (t_str)
156 sp_svg_transform_read(t_str, &item_t);
157 item_t *= local.inverse();
158 // (we're dealing with unattached repr, so we write to its attr instead of using sp_item_set_transform)
159 gchar *affinestr=sp_svg_transform_write(item_t);
160 copy->setAttribute("transform", affinestr);
161 g_free(affinestr);
162 }
164 parent->appendChildRepr(copy);
165 copied = g_slist_prepend(copied, copy);
166 Inkscape::GC::release(copy);
167 }
168 return copied;
169 }
171 void sp_selection_delete_impl(GSList const *items, bool propagate = true, bool propagate_descendants = true)
172 {
173 for (GSList const *i = items ; i ; i = i->next ) {
174 sp_object_ref((SPObject *)i->data, NULL);
175 }
176 for (GSList const *i = items; i != NULL; i = i->next) {
177 SPItem *item = (SPItem *) i->data;
178 SP_OBJECT(item)->deleteObject(propagate, propagate_descendants);
179 sp_object_unref((SPObject *)item, NULL);
180 }
181 }
184 void sp_selection_delete()
185 {
186 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
187 if (desktop == NULL) {
188 return;
189 }
191 if (tools_isactive (desktop, TOOLS_TEXT))
192 if (sp_text_delete_selection(desktop->event_context)) {
193 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_TEXT,
194 _("Delete text"));
195 return;
196 }
198 Inkscape::Selection *selection = sp_desktop_selection(desktop);
200 // check if something is selected
201 if (selection->isEmpty()) {
202 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Nothing</b> was deleted."));
203 return;
204 }
206 GSList const *selected = g_slist_copy(const_cast<GSList *>(selection->itemList()));
207 selection->clear();
208 sp_selection_delete_impl (selected);
209 g_slist_free ((GSList *) selected);
211 /* a tool may have set up private information in it's selection context
212 * that depends on desktop items. I think the only sane way to deal with
213 * this currently is to reset the current tool, which will reset it's
214 * associated selection context. For example: deleting an object
215 * while moving it around the canvas.
216 */
217 tools_switch ( desktop, tools_active ( desktop ) );
219 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_DELETE,
220 _("Delete"));
221 }
223 /* fixme: sequencing */
224 void sp_selection_duplicate()
225 {
226 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
227 if (desktop == NULL)
228 return;
230 Inkscape::XML::Document* xml_doc = sp_document_repr_doc(desktop->doc());
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, _("Select <b>object(s)</b> to duplicate."));
236 return;
237 }
239 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
241 selection->clear();
243 // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
244 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
246 GSList *newsel = NULL;
248 while (reprs) {
249 Inkscape::XML::Node *parent = ((Inkscape::XML::Node *) reprs->data)->parent();
250 Inkscape::XML::Node *copy = ((Inkscape::XML::Node *) reprs->data)->duplicate(xml_doc);
252 parent->appendChild(copy);
254 newsel = g_slist_prepend(newsel, copy);
255 reprs = g_slist_remove(reprs, reprs->data);
256 Inkscape::GC::release(copy);
257 }
259 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_DUPLICATE,
260 _("Duplicate"));
262 selection->setReprList(newsel);
264 g_slist_free(newsel);
265 }
267 void sp_edit_clear_all()
268 {
269 SPDesktop *dt = SP_ACTIVE_DESKTOP;
270 if (!dt)
271 return;
273 SPDocument *doc = sp_desktop_document(dt);
274 sp_desktop_selection(dt)->clear();
276 g_return_if_fail(SP_IS_GROUP(dt->currentLayer()));
277 GSList *items = sp_item_group_item_list(SP_GROUP(dt->currentLayer()));
279 while (items) {
280 SP_OBJECT (items->data)->deleteObject();
281 items = g_slist_remove(items, items->data);
282 }
284 sp_document_done(doc, SP_VERB_EDIT_CLEAR_ALL,
285 _("Delete all"));
286 }
288 GSList *
289 get_all_items (GSList *list, SPObject *from, SPDesktop *desktop, bool onlyvisible, bool onlysensitive, GSList const *exclude)
290 {
291 for (SPObject *child = sp_object_first_child(SP_OBJECT(from)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
292 if (SP_IS_ITEM(child) &&
293 !desktop->isLayer(SP_ITEM(child)) &&
294 (!onlysensitive || !SP_ITEM(child)->isLocked()) &&
295 (!onlyvisible || !desktop->itemIsHidden(SP_ITEM(child))) &&
296 (!exclude || !g_slist_find ((GSList *) exclude, child))
297 )
298 {
299 list = g_slist_prepend (list, SP_ITEM(child));
300 }
302 if (SP_IS_ITEM(child) && desktop->isLayer(SP_ITEM(child))) {
303 list = get_all_items (list, child, desktop, onlyvisible, onlysensitive, exclude);
304 }
305 }
307 return list;
308 }
310 void sp_edit_select_all_full (bool force_all_layers, bool invert)
311 {
312 SPDesktop *dt = SP_ACTIVE_DESKTOP;
313 if (!dt)
314 return;
316 Inkscape::Selection *selection = sp_desktop_selection(dt);
318 g_return_if_fail(SP_IS_GROUP(dt->currentLayer()));
320 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
321 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
322 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
324 GSList *items = NULL;
326 GSList const *exclude = NULL;
327 if (invert) {
328 exclude = selection->itemList();
329 }
331 if (force_all_layers)
332 inlayer = PREFS_SELECTION_ALL;
334 switch (inlayer) {
335 case PREFS_SELECTION_LAYER: {
336 if ( (onlysensitive && SP_ITEM(dt->currentLayer())->isLocked()) ||
337 (onlyvisible && dt->itemIsHidden(SP_ITEM(dt->currentLayer()))) )
338 return;
340 GSList *all_items = sp_item_group_item_list(SP_GROUP(dt->currentLayer()));
342 for (GSList *i = all_items; i; i = i->next) {
343 SPItem *item = SP_ITEM (i->data);
345 if (item && (!onlysensitive || !item->isLocked())) {
346 if (!onlyvisible || !dt->itemIsHidden(item)) {
347 if (!dt->isLayer(item)) {
348 if (!invert || !g_slist_find ((GSList *) exclude, item)) {
349 items = g_slist_prepend (items, item); // leave it in the list
350 }
351 }
352 }
353 }
354 }
356 g_slist_free (all_items);
357 break;
358 }
359 case PREFS_SELECTION_LAYER_RECURSIVE: {
360 items = get_all_items (NULL, dt->currentLayer(), dt, onlyvisible, onlysensitive, exclude);
361 break;
362 }
363 default: {
364 items = get_all_items (NULL, dt->currentRoot(), dt, onlyvisible, onlysensitive, exclude);
365 break;
366 }
367 }
369 selection->setList (items);
371 if (items) {
372 g_slist_free (items);
373 }
374 }
376 void sp_edit_select_all ()
377 {
378 sp_edit_select_all_full (false, false);
379 }
381 void sp_edit_select_all_in_all_layers ()
382 {
383 sp_edit_select_all_full (true, false);
384 }
386 void sp_edit_invert ()
387 {
388 sp_edit_select_all_full (false, true);
389 }
391 void sp_edit_invert_in_all_layers ()
392 {
393 sp_edit_select_all_full (true, true);
394 }
396 void sp_selection_group()
397 {
398 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
399 if (desktop == NULL)
400 return;
402 SPDocument *doc = sp_desktop_document (desktop);
403 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
405 Inkscape::Selection *selection = sp_desktop_selection(desktop);
407 // Check if something is selected.
408 if (selection->isEmpty()) {
409 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>some objects</b> to group."));
410 return;
411 }
413 GSList const *l = (GSList *) selection->reprList();
415 GSList *p = g_slist_copy((GSList *) l);
417 selection->clear();
419 p = g_slist_sort(p, (GCompareFunc) sp_repr_compare_position);
421 // Remember the position and parent of the topmost object.
422 gint topmost = ((Inkscape::XML::Node *) g_slist_last(p)->data)->position();
423 Inkscape::XML::Node *topmost_parent = ((Inkscape::XML::Node *) g_slist_last(p)->data)->parent();
425 Inkscape::XML::Node *group = xml_doc->createElement("svg:g");
427 while (p) {
428 Inkscape::XML::Node *current = (Inkscape::XML::Node *) p->data;
430 if (current->parent() == topmost_parent) {
431 Inkscape::XML::Node *spnew = current->duplicate(xml_doc);
432 sp_repr_unparent(current);
433 group->appendChild(spnew);
434 Inkscape::GC::release(spnew);
435 topmost --; // only reduce count for those items deleted from topmost_parent
436 } else { // move it to topmost_parent first
437 GSList *temp_clip = NULL;
439 // At this point, current may already have no item, due to its being a clone whose original is already moved away
440 // So we copy it artificially calculating the transform from its repr->attr("transform") and the parent transform
441 gchar const *t_str = current->attribute("transform");
442 NR::Matrix item_t (NR::identity());
443 if (t_str)
444 sp_svg_transform_read(t_str, &item_t);
445 item_t *= sp_item_i2doc_affine(SP_ITEM(doc->getObjectByRepr(current->parent())));
446 //FIXME: when moving both clone and original from a transformed group (either by
447 //grouping into another parent, or by cut/paste) the transform from the original's
448 //parent becomes embedded into original itself, and this affects its clones. Fix
449 //this by remembering the transform diffs we write to each item into an array and
450 //then, if this is clone, looking up its original in that array and pre-multiplying
451 //it by the inverse of that original's transform diff.
453 sp_selection_copy_one (current, item_t, &temp_clip, xml_doc);
454 sp_repr_unparent(current);
456 // paste into topmost_parent (temporarily)
457 GSList *copied = sp_selection_paste_impl (doc, doc->getObjectByRepr(topmost_parent), &temp_clip);
458 if (temp_clip) g_slist_free (temp_clip);
459 if (copied) { // if success,
460 // take pasted object (now in topmost_parent)
461 Inkscape::XML::Node *in_topmost = (Inkscape::XML::Node *) copied->data;
462 // make a copy
463 Inkscape::XML::Node *spnew = in_topmost->duplicate(xml_doc);
464 // remove pasted
465 sp_repr_unparent(in_topmost);
466 // put its copy into group
467 group->appendChild(spnew);
468 Inkscape::GC::release(spnew);
469 g_slist_free (copied);
470 }
471 }
472 p = g_slist_remove(p, current);
473 }
475 // Add the new group to the topmost members' parent
476 topmost_parent->appendChild(group);
478 // Move to the position of the topmost, reduced by the number of items deleted from topmost_parent
479 group->setPosition(topmost + 1);
481 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_GROUP,
482 _("Group"));
484 selection->set(group);
485 Inkscape::GC::release(group);
486 }
488 void sp_selection_ungroup()
489 {
490 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
491 if (desktop == NULL)
492 return;
494 Inkscape::Selection *selection = sp_desktop_selection(desktop);
496 if (selection->isEmpty()) {
497 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>group</b> to ungroup."));
498 return;
499 }
501 GSList *items = g_slist_copy((GSList *) selection->itemList());
502 selection->clear();
504 // Get a copy of current selection.
505 GSList *new_select = NULL;
506 bool ungrouped = false;
507 for (GSList *i = items;
508 i != NULL;
509 i = i->next)
510 {
511 SPItem *group = (SPItem *) i->data;
513 // when ungrouping cloned groups with their originals, some objects that were selected may no more exist due to unlinking
514 if (!SP_IS_OBJECT(group)) {
515 continue;
516 }
518 /* We do not allow ungrouping <svg> etc. (lauris) */
519 if (strcmp(SP_OBJECT_REPR(group)->name(), "svg:g") && strcmp(SP_OBJECT_REPR(group)->name(), "svg:switch")) {
520 // keep the non-group item in the new selection
521 selection->add(group);
522 continue;
523 }
525 GSList *children = NULL;
526 /* This is not strictly required, but is nicer to rely on group ::destroy (lauris) */
527 sp_item_group_ungroup(SP_GROUP(group), &children, false);
528 ungrouped = true;
529 // Add ungrouped items to the new selection.
530 new_select = g_slist_concat(new_select, children);
531 }
533 if (new_select) { // Set new selection.
534 selection->addList(new_select);
535 g_slist_free(new_select);
536 }
537 if (!ungrouped) {
538 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No groups</b> to ungroup in the selection."));
539 }
541 g_slist_free(items);
543 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_UNGROUP,
544 _("Ungroup"));
545 }
547 static SPGroup *
548 sp_item_list_common_parent_group(GSList const *items)
549 {
550 if (!items) {
551 return NULL;
552 }
553 SPObject *parent = SP_OBJECT_PARENT(items->data);
554 /* Strictly speaking this CAN happen, if user selects <svg> from Inkscape::XML editor */
555 if (!SP_IS_GROUP(parent)) {
556 return NULL;
557 }
558 for (items = items->next; items; items = items->next) {
559 if (SP_OBJECT_PARENT(items->data) != parent) {
560 return NULL;
561 }
562 }
564 return SP_GROUP(parent);
565 }
567 /** Finds out the minimum common bbox of the selected items. */
568 static NR::Maybe<NR::Rect>
569 enclose_items(GSList const *items)
570 {
571 g_assert(items != NULL);
573 NR::Maybe<NR::Rect> r = NR::Nothing();
574 for (GSList const *i = items; i; i = i->next) {
575 r = NR::union_bounds(r, sp_item_bbox_desktop((SPItem *) i->data));
576 }
577 return r;
578 }
580 SPObject *
581 prev_sibling(SPObject *child)
582 {
583 SPObject *parent = SP_OBJECT_PARENT(child);
584 if (!SP_IS_GROUP(parent)) {
585 return NULL;
586 }
587 for ( SPObject *i = sp_object_first_child(parent) ; i; i = SP_OBJECT_NEXT(i) ) {
588 if (i->next == child)
589 return i;
590 }
591 return NULL;
592 }
594 void
595 sp_selection_raise()
596 {
597 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
598 if (!desktop)
599 return;
601 Inkscape::Selection *selection = sp_desktop_selection(desktop);
603 GSList const *items = (GSList *) selection->itemList();
604 if (!items) {
605 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise."));
606 return;
607 }
609 SPGroup const *group = sp_item_list_common_parent_group(items);
610 if (!group) {
611 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
612 return;
613 }
615 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
617 /* Construct reverse-ordered list of selected children. */
618 GSList *rev = g_slist_copy((GSList *) items);
619 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
621 // Determine the common bbox of the selected items.
622 NR::Maybe<NR::Rect> selected = enclose_items(items);
624 // Iterate over all objects in the selection (starting from top).
625 if (selected) {
626 while (rev) {
627 SPObject *child = SP_OBJECT(rev->data);
628 // for each selected object, find the next sibling
629 for (SPObject *newref = child->next; newref; newref = newref->next) {
630 // if the sibling is an item AND overlaps our selection,
631 if (SP_IS_ITEM(newref)) {
632 NR::Maybe<NR::Rect> newref_bbox = sp_item_bbox_desktop(SP_ITEM(newref));
633 if ( newref_bbox && selected->intersects(*newref_bbox) ) {
634 // AND if it's not one of our selected objects,
635 if (!g_slist_find((GSList *) items, newref)) {
636 // move the selected object after that sibling
637 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(newref));
638 }
639 break;
640 }
641 }
642 }
643 rev = g_slist_remove(rev, child);
644 }
645 } else {
646 g_slist_free(rev);
647 }
649 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_RAISE,
650 _("Raise"));
651 }
653 void sp_selection_raise_to_top()
654 {
655 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
656 if (desktop == NULL)
657 return;
659 SPDocument *document = sp_desktop_document(desktop);
660 Inkscape::Selection *selection = sp_desktop_selection(desktop);
662 if (selection->isEmpty()) {
663 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise to top."));
664 return;
665 }
667 GSList const *items = (GSList *) selection->itemList();
669 SPGroup const *group = sp_item_list_common_parent_group(items);
670 if (!group) {
671 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
672 return;
673 }
675 GSList *rl = g_slist_copy((GSList *) selection->reprList());
676 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
678 for (GSList *l = rl; l != NULL; l = l->next) {
679 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
680 repr->setPosition(-1);
681 }
683 g_slist_free(rl);
685 sp_document_done(document, SP_VERB_SELECTION_TO_FRONT,
686 _("Raise to top"));
687 }
689 void
690 sp_selection_lower()
691 {
692 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
693 if (desktop == NULL)
694 return;
696 Inkscape::Selection *selection = sp_desktop_selection(desktop);
698 GSList const *items = (GSList *) selection->itemList();
699 if (!items) {
700 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower."));
701 return;
702 }
704 SPGroup const *group = sp_item_list_common_parent_group(items);
705 if (!group) {
706 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
707 return;
708 }
710 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
712 // Determine the common bbox of the selected items.
713 NR::Maybe<NR::Rect> selected = enclose_items(items);
715 /* Construct direct-ordered list of selected children. */
716 GSList *rev = g_slist_copy((GSList *) items);
717 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
718 rev = g_slist_reverse(rev);
720 // Iterate over all objects in the selection (starting from top).
721 if (selected) {
722 while (rev) {
723 SPObject *child = SP_OBJECT(rev->data);
724 // for each selected object, find the prev sibling
725 for (SPObject *newref = prev_sibling(child); newref; newref = prev_sibling(newref)) {
726 // if the sibling is an item AND overlaps our selection,
727 if (SP_IS_ITEM(newref)) {
728 NR::Maybe<NR::Rect> ref_bbox = sp_item_bbox_desktop(SP_ITEM(newref));
729 if ( ref_bbox && selected->intersects(*ref_bbox) ) {
730 // AND if it's not one of our selected objects,
731 if (!g_slist_find((GSList *) items, newref)) {
732 // move the selected object before that sibling
733 SPObject *put_after = prev_sibling(newref);
734 if (put_after)
735 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(put_after));
736 else
737 SP_OBJECT_REPR(child)->setPosition(0);
738 }
739 break;
740 }
741 }
742 }
743 rev = g_slist_remove(rev, child);
744 }
745 } else {
746 g_slist_free(rev);
747 }
749 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_LOWER,
750 _("Lower"));
751 }
753 void sp_selection_lower_to_bottom()
754 {
755 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
756 if (desktop == NULL)
757 return;
759 SPDocument *document = sp_desktop_document(desktop);
760 Inkscape::Selection *selection = sp_desktop_selection(desktop);
762 if (selection->isEmpty()) {
763 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower to bottom."));
764 return;
765 }
767 GSList const *items = (GSList *) selection->itemList();
769 SPGroup const *group = sp_item_list_common_parent_group(items);
770 if (!group) {
771 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
772 return;
773 }
775 GSList *rl;
776 rl = g_slist_copy((GSList *) selection->reprList());
777 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
778 rl = g_slist_reverse(rl);
780 for (GSList *l = rl; l != NULL; l = l->next) {
781 gint minpos;
782 SPObject *pp, *pc;
783 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
784 pp = document->getObjectByRepr(sp_repr_parent(repr));
785 minpos = 0;
786 g_assert(SP_IS_GROUP(pp));
787 pc = sp_object_first_child(pp);
788 while (!SP_IS_ITEM(pc)) {
789 minpos += 1;
790 pc = pc->next;
791 }
792 repr->setPosition(minpos);
793 }
795 g_slist_free(rl);
797 sp_document_done(document, SP_VERB_SELECTION_TO_BACK,
798 _("Lower to bottom"));
799 }
801 void
802 sp_undo(SPDesktop *desktop, SPDocument *)
803 {
804 if (!sp_document_undo(sp_desktop_document(desktop)))
805 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to undo."));
806 }
808 void
809 sp_redo(SPDesktop *desktop, SPDocument *)
810 {
811 if (!sp_document_redo(sp_desktop_document(desktop)))
812 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to redo."));
813 }
815 void sp_selection_cut()
816 {
817 sp_selection_copy();
818 sp_selection_delete();
819 }
821 /**
822 * \pre item != NULL
823 */
824 SPCSSAttr *
825 take_style_from_item (SPItem *item)
826 {
827 // write the complete cascaded style, context-free
828 SPCSSAttr *css = sp_css_attr_from_object (SP_OBJECT(item), SP_STYLE_FLAG_ALWAYS);
829 if (css == NULL)
830 return NULL;
832 if ((SP_IS_GROUP(item) && SP_OBJECT(item)->children) ||
833 (SP_IS_TEXT (item) && SP_OBJECT(item)->children && SP_OBJECT(item)->children->next == NULL)) {
834 // if this is a text with exactly one tspan child, merge the style of that tspan as well
835 // If this is a group, merge the style of its topmost (last) child with style
836 for (SPObject *last_element = item->lastChild(); last_element != NULL; last_element = SP_OBJECT_PREV (last_element)) {
837 if (SP_OBJECT_STYLE (last_element) != NULL) {
838 SPCSSAttr *temp = sp_css_attr_from_object (last_element, SP_STYLE_FLAG_IFSET);
839 if (temp) {
840 sp_repr_css_merge (css, temp);
841 sp_repr_css_attr_unref (temp);
842 }
843 break;
844 }
845 }
846 }
847 if (!(SP_IS_TEXT (item) || SP_IS_TSPAN (item) || SP_IS_TREF(item) || SP_IS_STRING (item))) {
848 // do not copy text properties from non-text objects, it's confusing
849 css = sp_css_attr_unset_text (css);
850 }
852 // FIXME: also transform gradient/pattern fills, by forking? NO, this must be nondestructive
853 double ex = NR::expansion(sp_item_i2doc_affine(item));
854 if (ex != 1.0) {
855 css = sp_css_attr_scale (css, ex);
856 }
858 return css;
859 }
862 void sp_selection_copy()
863 {
864 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
865 cm->copy();
866 }
868 void sp_selection_paste(bool in_place)
869 {
870 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
871 if(cm->paste(in_place))
872 sp_document_done(SP_ACTIVE_DOCUMENT, SP_VERB_EDIT_PASTE, _("Paste"));
873 }
875 void sp_selection_paste_style()
876 {
877 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
878 if(cm->pasteStyle())
879 sp_document_done(SP_ACTIVE_DOCUMENT, SP_VERB_EDIT_PASTE_STYLE, _("Paste style"));
880 }
883 void sp_selection_paste_livepatheffect()
884 {
885 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
886 if(cm->pastePathEffect())
887 sp_document_done(SP_ACTIVE_DOCUMENT, SP_VERB_EDIT_PASTE_LIVEPATHEFFECT,
888 _("Paste live path effect"));
889 }
892 void sp_selection_remove_livepatheffect_impl(SPItem *item)
893 {
894 if ( item && SP_IS_LPE_ITEM(item) ) {
895 sp_lpe_item_remove_path_effect(SP_LPE_ITEM(item), false);
896 }
897 }
899 void sp_selection_remove_livepatheffect()
900 {
901 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
902 if (desktop == NULL) return;
904 Inkscape::Selection *selection = sp_desktop_selection(desktop);
906 // check if something is selected
907 if (selection->isEmpty()) {
908 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to remove live path effects from."));
909 return;
910 }
912 for ( GSList const *itemlist = selection->itemList(); itemlist != NULL; itemlist = g_slist_next(itemlist) ) {
913 SPItem *item = reinterpret_cast<SPItem*>(itemlist->data);
915 sp_selection_remove_livepatheffect_impl(item);
917 }
919 sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_LIVEPATHEFFECT,
920 _("Remove live path effect"));
921 }
923 void sp_selection_paste_size (bool apply_x, bool apply_y)
924 {
925 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
926 if(cm->pasteSize(false, apply_x, apply_y))
927 sp_document_done(sp_desktop_document(SP_ACTIVE_DESKTOP), SP_VERB_EDIT_PASTE_SIZE,
928 _("Paste size"));
929 }
931 void sp_selection_paste_size_separately (bool apply_x, bool apply_y)
932 {
933 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
934 if(cm->pasteSize(true, apply_x, apply_y))
935 sp_document_done(sp_desktop_document(SP_ACTIVE_DESKTOP), SP_VERB_EDIT_PASTE_SIZE_SEPARATELY,
936 _("Paste size separately"));
937 }
939 void sp_selection_to_next_layer ()
940 {
941 SPDesktop *dt = SP_ACTIVE_DESKTOP;
943 Inkscape::Selection *selection = sp_desktop_selection(dt);
945 // check if something is selected
946 if (selection->isEmpty()) {
947 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer above."));
948 return;
949 }
951 GSList const *items = g_slist_copy ((GSList *) selection->itemList());
953 bool no_more = false; // Set to true, if no more layers above
954 SPObject *next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer());
955 if (next) {
956 GSList *temp_clip = NULL;
957 sp_selection_copy_impl (items, &temp_clip, sp_document_repr_doc(dt->doc()));
958 sp_selection_delete_impl (items, false, false);
959 next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
960 GSList *copied;
961 if(next) {
962 copied = sp_selection_paste_impl (sp_desktop_document (dt), next, &temp_clip);
963 } else {
964 copied = sp_selection_paste_impl (sp_desktop_document (dt), dt->currentLayer(), &temp_clip);
965 no_more = true;
966 }
967 selection->setReprList((GSList const *) copied);
968 g_slist_free (copied);
969 if (temp_clip) g_slist_free (temp_clip);
970 if (next) dt->setCurrentLayer(next);
971 sp_document_done(sp_desktop_document (dt), SP_VERB_LAYER_MOVE_TO_NEXT,
972 _("Raise to next layer"));
973 } else {
974 no_more = true;
975 }
977 if (no_more) {
978 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers above."));
979 }
981 g_slist_free ((GSList *) items);
982 }
984 void sp_selection_to_prev_layer ()
985 {
986 SPDesktop *dt = SP_ACTIVE_DESKTOP;
988 Inkscape::Selection *selection = sp_desktop_selection(dt);
990 // check if something is selected
991 if (selection->isEmpty()) {
992 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer below."));
993 return;
994 }
996 GSList const *items = g_slist_copy ((GSList *) selection->itemList());
998 bool no_more = false; // Set to true, if no more layers below
999 SPObject *next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer());
1000 if (next) {
1001 GSList *temp_clip = NULL;
1002 sp_selection_copy_impl (items, &temp_clip, sp_document_repr_doc(dt->doc())); // we're in the same doc, so no need to copy defs
1003 sp_selection_delete_impl (items, false, false);
1004 next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
1005 GSList *copied;
1006 if(next) {
1007 copied = sp_selection_paste_impl (sp_desktop_document (dt), next, &temp_clip);
1008 } else {
1009 copied = sp_selection_paste_impl (sp_desktop_document (dt), dt->currentLayer(), &temp_clip);
1010 no_more = true;
1011 }
1012 selection->setReprList((GSList const *) copied);
1013 g_slist_free (copied);
1014 if (temp_clip) g_slist_free (temp_clip);
1015 if (next) dt->setCurrentLayer(next);
1016 sp_document_done(sp_desktop_document (dt), SP_VERB_LAYER_MOVE_TO_PREV,
1017 _("Lower to previous layer"));
1018 } else {
1019 no_more = true;
1020 }
1022 if (no_more) {
1023 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers below."));
1024 }
1026 g_slist_free ((GSList *) items);
1027 }
1029 bool
1030 selection_contains_original (SPItem *item, Inkscape::Selection *selection)
1031 {
1032 bool contains_original = false;
1034 bool is_use = SP_IS_USE(item);
1035 SPItem *item_use = item;
1036 SPItem *item_use_first = item;
1037 while (is_use && item_use && !contains_original)
1038 {
1039 item_use = sp_use_get_original (SP_USE(item_use));
1040 contains_original |= selection->includes(item_use);
1041 if (item_use == item_use_first)
1042 break;
1043 is_use = SP_IS_USE(item_use);
1044 }
1046 // If it's a tref, check whether the object containing the character
1047 // data is part of the selection
1048 if (!contains_original && SP_IS_TREF(item)) {
1049 contains_original = selection->includes(SP_TREF(item)->getObjectReferredTo());
1050 }
1052 return contains_original;
1053 }
1056 bool
1057 selection_contains_both_clone_and_original (Inkscape::Selection *selection)
1058 {
1059 bool clone_with_original = false;
1060 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1061 SPItem *item = SP_ITEM(l->data);
1062 clone_with_original |= selection_contains_original(item, selection);
1063 if (clone_with_original)
1064 break;
1065 }
1066 return clone_with_original;
1067 }
1070 /** Apply matrix to the selection. \a set_i2d is normally true, which means objects are in the
1071 original transform, synced with their reprs, and need to jump to the new transform in one go. A
1072 value of set_i2d==false is only used by seltrans when it's dragging objects live (not outlines); in
1073 that case, items are already in the new position, but the repr is in the old, and this function
1074 then simply updates the repr from item->transform.
1075 */
1076 void sp_selection_apply_affine(Inkscape::Selection *selection, NR::Matrix const &affine, bool set_i2d)
1077 {
1078 if (selection->isEmpty())
1079 return;
1081 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1082 SPItem *item = SP_ITEM(l->data);
1084 NR::Point old_center(0,0);
1085 if (set_i2d && item->isCenterSet())
1086 old_center = item->getCenter();
1088 #if 0 /* Re-enable this once persistent guides have a graphical indication.
1089 At the time of writing, this is the only place to re-enable. */
1090 sp_item_update_cns(*item, selection->desktop());
1091 #endif
1093 // we're moving both a clone and its original or any ancestor in clone chain?
1094 bool transform_clone_with_original = selection_contains_original(item, selection);
1095 // ...both a text-on-path and its path?
1096 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)))) ));
1097 // ...both a flowtext and its frame?
1098 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)
1099 // ...both an offset and its source?
1100 bool transform_offset_with_source = (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) && selection->includes( sp_offset_get_source (SP_OFFSET(item)) );
1102 // If we're moving a connector, we want to detach it
1103 // from shapes that aren't part of the selection, but
1104 // leave it attached if they are
1105 if (cc_item_is_connector(item)) {
1106 SPItem *attItem[2];
1107 SP_PATH(item)->connEndPair.getAttachedItems(attItem);
1109 for (int n = 0; n < 2; ++n) {
1110 if (!selection->includes(attItem[n])) {
1111 sp_conn_end_detach(item, n);
1112 }
1113 }
1114 }
1116 // "clones are unmoved when original is moved" preference
1117 int compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
1118 bool prefs_unmoved = (compensation == SP_CLONE_COMPENSATION_UNMOVED);
1119 bool prefs_parallel = (compensation == SP_CLONE_COMPENSATION_PARALLEL);
1121 // If this is a clone and it's selected along with its original, do not move it; it will feel the
1122 // transform of its original and respond to it itself. Without this, a clone is doubly
1123 // transformed, very unintuitive.
1124 // Same for textpath if we are also doing ANY transform to its path: do not touch textpath,
1125 // letters cannot be squeezed or rotated anyway, they only refill the changed path.
1126 // Same for linked offset if we are also moving its source: do not move it.
1127 if (transform_textpath_with_path || transform_offset_with_source) {
1128 // restore item->transform field from the repr, in case it was changed by seltrans
1129 sp_object_read_attr (SP_OBJECT (item), "transform");
1131 } else if (transform_flowtext_with_frame) {
1132 // apply the inverse of the region's transform to the <use> so that the flow remains
1133 // the same (even though the output itself gets transformed)
1134 for (SPObject *region = item->firstChild() ; region ; region = SP_OBJECT_NEXT(region)) {
1135 if (!SP_IS_FLOWREGION(region) && !SP_IS_FLOWREGIONEXCLUDE(region))
1136 continue;
1137 for (SPObject *use = region->firstChild() ; use ; use = SP_OBJECT_NEXT(use)) {
1138 if (!SP_IS_USE(use)) continue;
1139 sp_item_write_transform(SP_USE(use), SP_OBJECT_REPR(use), item->transform.inverse(), NULL);
1140 }
1141 }
1142 } else if (transform_clone_with_original) {
1143 // We are transforming a clone along with its original. The below matrix juggling is
1144 // necessary to ensure that they transform as a whole, i.e. the clone's induced
1145 // transform and its move compensation are both cancelled out.
1147 // restore item->transform field from the repr, in case it was changed by seltrans
1148 sp_object_read_attr (SP_OBJECT (item), "transform");
1150 // calculate the matrix we need to apply to the clone to cancel its induced transform from its original
1151 NR::Matrix parent_transform = sp_item_i2root_affine(SP_ITEM(SP_OBJECT_PARENT (item)));
1152 NR::Matrix t = parent_transform * matrix_to_desktop (matrix_from_desktop (affine, item), item) * parent_transform.inverse();
1153 NR::Matrix t_inv =parent_transform * matrix_to_desktop (matrix_from_desktop (affine.inverse(), item), item) * parent_transform.inverse();
1154 NR::Matrix result = t_inv * item->transform * t;
1156 if ((prefs_parallel || prefs_unmoved) && affine.is_translation()) {
1157 // we need to cancel out the move compensation, too
1159 // find out the clone move, same as in sp_use_move_compensate
1160 NR::Matrix parent = sp_use_get_parent_transform (SP_USE(item));
1161 NR::Matrix clone_move = parent.inverse() * t * parent;
1163 if (prefs_parallel) {
1164 NR::Matrix move = result * clone_move * t_inv;
1165 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &move);
1167 } else if (prefs_unmoved) {
1168 //if (SP_IS_USE(sp_use_get_original(SP_USE(item))))
1169 // clone_move = NR::identity();
1170 NR::Matrix move = result * clone_move;
1171 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &t);
1172 }
1174 } else {
1175 // just apply the result
1176 sp_item_write_transform(item, SP_OBJECT_REPR(item), result, &t);
1177 }
1179 } else {
1180 if (set_i2d) {
1181 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * affine);
1182 }
1183 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, NULL);
1184 }
1186 // if we're moving the actual object, not just updating the repr, we can transform the
1187 // center by the same matrix (only necessary for non-translations)
1188 if (set_i2d && item->isCenterSet() && !affine.is_translation()) {
1189 item->setCenter(old_center * affine);
1190 SP_OBJECT(item)->updateRepr();
1191 }
1192 }
1193 }
1195 void sp_selection_remove_transform()
1196 {
1197 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1198 if (desktop == NULL)
1199 return;
1201 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1203 GSList const *l = (GSList *) selection->reprList();
1204 while (l != NULL) {
1205 sp_repr_set_attr((Inkscape::XML::Node*)l->data, "transform", NULL);
1206 l = l->next;
1207 }
1209 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_FLATTEN,
1210 _("Remove transform"));
1211 }
1213 void
1214 sp_selection_scale_absolute(Inkscape::Selection *selection,
1215 double const x0, double const x1,
1216 double const y0, double const y1)
1217 {
1218 if (selection->isEmpty())
1219 return;
1221 NR::Maybe<NR::Rect> const bbox(selection->bounds());
1222 if ( !bbox || bbox->isEmpty() ) {
1223 return;
1224 }
1226 NR::translate const p2o(-bbox->min());
1228 NR::scale const newSize(x1 - x0,
1229 y1 - y0);
1230 NR::scale const scale( newSize / NR::scale(bbox->dimensions()) );
1231 NR::translate const o2n(x0, y0);
1232 NR::Matrix const final( p2o * scale * o2n );
1234 sp_selection_apply_affine(selection, final);
1235 }
1238 void sp_selection_scale_relative(Inkscape::Selection *selection, NR::Point const &align, NR::scale const &scale)
1239 {
1240 if (selection->isEmpty())
1241 return;
1243 NR::Maybe<NR::Rect> const bbox(selection->bounds());
1245 if ( !bbox || bbox->isEmpty() ) {
1246 return;
1247 }
1249 // FIXME: ARBITRARY LIMIT: don't try to scale above 1 Mpx, it won't display properly and will crash sooner or later anyway
1250 if ( bbox->extent(NR::X) * scale[NR::X] > 1e6 ||
1251 bbox->extent(NR::Y) * scale[NR::Y] > 1e6 )
1252 {
1253 return;
1254 }
1256 NR::translate const n2d(-align);
1257 NR::translate const d2n(align);
1258 NR::Matrix const final( n2d * scale * d2n );
1259 sp_selection_apply_affine(selection, final);
1260 }
1262 void
1263 sp_selection_rotate_relative(Inkscape::Selection *selection, NR::Point const ¢er, gdouble const angle_degrees)
1264 {
1265 NR::translate const d2n(center);
1266 NR::translate const n2d(-center);
1267 NR::rotate const rotate(rotate_degrees(angle_degrees));
1268 NR::Matrix const final( NR::Matrix(n2d) * rotate * d2n );
1269 sp_selection_apply_affine(selection, final);
1270 }
1272 void
1273 sp_selection_skew_relative(Inkscape::Selection *selection, NR::Point const &align, double dx, double dy)
1274 {
1275 NR::translate const d2n(align);
1276 NR::translate const n2d(-align);
1277 NR::Matrix const skew(1, dy,
1278 dx, 1,
1279 0, 0);
1280 NR::Matrix const final( n2d * skew * d2n );
1281 sp_selection_apply_affine(selection, final);
1282 }
1284 void sp_selection_move_relative(Inkscape::Selection *selection, NR::Point const &move)
1285 {
1286 sp_selection_apply_affine(selection, NR::Matrix(NR::translate(move)));
1287 }
1289 void sp_selection_move_relative(Inkscape::Selection *selection, double dx, double dy)
1290 {
1291 sp_selection_apply_affine(selection, NR::Matrix(NR::translate(dx, dy)));
1292 }
1295 /**
1296 * \brief sp_selection_rotate_90
1297 *
1298 * This function rotates selected objects 90 degrees clockwise.
1299 *
1300 */
1302 void sp_selection_rotate_90_cw()
1303 {
1304 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1306 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1308 if (selection->isEmpty())
1309 return;
1311 GSList const *l = selection->itemList();
1312 NR::rotate const rot_neg_90(NR::Point(0, -1));
1313 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1314 SPItem *item = SP_ITEM(l2->data);
1315 sp_item_rotate_rel(item, rot_neg_90);
1316 }
1318 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_ROTATE_90_CCW,
1319 _("Rotate 90° CW"));
1320 }
1323 /**
1324 * @brief Rotates selected objects 90 degrees counter-clockwise.
1325 */
1326 void sp_selection_rotate_90_ccw()
1327 {
1328 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1330 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1332 if (selection->isEmpty())
1333 return;
1335 GSList const *l = selection->itemList();
1336 NR::rotate const rot_neg_90(NR::Point(0, 1));
1337 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1338 SPItem *item = SP_ITEM(l2->data);
1339 sp_item_rotate_rel(item, rot_neg_90);
1340 }
1342 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_ROTATE_90_CW,
1343 _("Rotate 90° CCW"));
1344 }
1346 void
1347 sp_selection_rotate(Inkscape::Selection *selection, gdouble const angle_degrees)
1348 {
1349 if (selection->isEmpty())
1350 return;
1352 NR::Maybe<NR::Point> center = selection->center();
1353 if (!center) {
1354 return;
1355 }
1357 sp_selection_rotate_relative(selection, *center, angle_degrees);
1359 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1360 ( ( angle_degrees > 0 )
1361 ? "selector:rotate:ccw"
1362 : "selector:rotate:cw" ),
1363 SP_VERB_CONTEXT_SELECT,
1364 _("Rotate"));
1365 }
1367 /**
1368 \param angle the angle in "angular pixels", i.e. how many visible pixels must move the outermost point of the rotated object
1369 */
1370 void
1371 sp_selection_rotate_screen(Inkscape::Selection *selection, gdouble angle)
1372 {
1373 if (selection->isEmpty())
1374 return;
1376 NR::Maybe<NR::Rect> const bbox(selection->bounds());
1377 NR::Maybe<NR::Point> center = selection->center();
1379 if ( !bbox || !center ) {
1380 return;
1381 }
1383 gdouble const zoom = selection->desktop()->current_zoom();
1384 gdouble const zmove = angle / zoom;
1385 gdouble const r = NR::L2(bbox->cornerFarthestFrom(*center) - *center);
1387 gdouble const zangle = 180 * atan2(zmove, r) / M_PI;
1389 sp_selection_rotate_relative(selection, *center, zangle);
1391 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1392 ( (angle > 0)
1393 ? "selector:rotate:ccw"
1394 : "selector:rotate:cw" ),
1395 SP_VERB_CONTEXT_SELECT,
1396 _("Rotate by pixels"));
1397 }
1399 void
1400 sp_selection_scale(Inkscape::Selection *selection, gdouble grow)
1401 {
1402 if (selection->isEmpty())
1403 return;
1405 NR::Maybe<NR::Rect> const bbox(selection->bounds());
1406 if (!bbox) {
1407 return;
1408 }
1410 NR::Point const center(bbox->midpoint());
1412 // you can't scale "do nizhe pola" (below zero)
1413 double const max_len = bbox->maxExtent();
1414 if ( max_len + grow <= 1e-3 ) {
1415 return;
1416 }
1418 double const times = 1.0 + grow / max_len;
1419 sp_selection_scale_relative(selection, center, NR::scale(times, times));
1421 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1422 ( (grow > 0)
1423 ? "selector:scale:larger"
1424 : "selector:scale:smaller" ),
1425 SP_VERB_CONTEXT_SELECT,
1426 _("Scale"));
1427 }
1429 void
1430 sp_selection_scale_screen(Inkscape::Selection *selection, gdouble grow_pixels)
1431 {
1432 sp_selection_scale(selection,
1433 grow_pixels / selection->desktop()->current_zoom());
1434 }
1436 void
1437 sp_selection_scale_times(Inkscape::Selection *selection, gdouble times)
1438 {
1439 if (selection->isEmpty())
1440 return;
1442 NR::Maybe<NR::Rect> sel_bbox = selection->bounds();
1444 if (!sel_bbox) {
1445 return;
1446 }
1448 NR::Point const center(sel_bbox->midpoint());
1449 sp_selection_scale_relative(selection, center, NR::scale(times, times));
1450 sp_document_done(sp_desktop_document(selection->desktop()), SP_VERB_CONTEXT_SELECT,
1451 _("Scale by whole factor"));
1452 }
1454 void
1455 sp_selection_move(gdouble dx, gdouble dy)
1456 {
1457 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1458 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1459 if (selection->isEmpty()) {
1460 return;
1461 }
1463 sp_selection_move_relative(selection, dx, dy);
1465 if (dx == 0) {
1466 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical", SP_VERB_CONTEXT_SELECT,
1467 _("Move vertically"));
1468 } else if (dy == 0) {
1469 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal", SP_VERB_CONTEXT_SELECT,
1470 _("Move horizontally"));
1471 } else {
1472 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SELECT,
1473 _("Move"));
1474 }
1475 }
1477 void
1478 sp_selection_move_screen(gdouble dx, gdouble dy)
1479 {
1480 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1482 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1483 if (selection->isEmpty()) {
1484 return;
1485 }
1487 // same as sp_selection_move but divide deltas by zoom factor
1488 gdouble const zoom = desktop->current_zoom();
1489 gdouble const zdx = dx / zoom;
1490 gdouble const zdy = dy / zoom;
1491 sp_selection_move_relative(selection, zdx, zdy);
1493 if (dx == 0) {
1494 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical", SP_VERB_CONTEXT_SELECT,
1495 _("Move vertically by pixels"));
1496 } else if (dy == 0) {
1497 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal", SP_VERB_CONTEXT_SELECT,
1498 _("Move horizontally by pixels"));
1499 } else {
1500 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SELECT,
1501 _("Move"));
1502 }
1503 }
1505 namespace {
1507 template <typename D>
1508 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
1509 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
1511 template <typename D>
1512 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items, SPObject *root,
1513 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
1515 struct Forward {
1516 typedef SPObject *Iterator;
1518 static Iterator children(SPObject *o) { return sp_object_first_child(o); }
1519 static Iterator siblings_after(SPObject *o) { return SP_OBJECT_NEXT(o); }
1520 static void dispose(Iterator /*i*/) {}
1522 static SPObject *object(Iterator i) { return i; }
1523 static Iterator next(Iterator i) { return SP_OBJECT_NEXT(i); }
1524 };
1526 struct Reverse {
1527 typedef GSList *Iterator;
1529 static Iterator children(SPObject *o) {
1530 return make_list(o->firstChild(), NULL);
1531 }
1532 static Iterator siblings_after(SPObject *o) {
1533 return make_list(SP_OBJECT_PARENT(o)->firstChild(), o);
1534 }
1535 static void dispose(Iterator i) {
1536 g_slist_free(i);
1537 }
1539 static SPObject *object(Iterator i) {
1540 return reinterpret_cast<SPObject *>(i->data);
1541 }
1542 static Iterator next(Iterator i) { return i->next; }
1544 private:
1545 static GSList *make_list(SPObject *object, SPObject *limit) {
1546 GSList *list=NULL;
1547 while ( object != limit ) {
1548 list = g_slist_prepend(list, object);
1549 object = SP_OBJECT_NEXT(object);
1550 }
1551 return list;
1552 }
1553 };
1555 }
1557 void
1558 sp_selection_item_next(void)
1559 {
1560 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1561 g_return_if_fail(desktop != NULL);
1562 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1564 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
1565 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
1566 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
1568 SPObject *root;
1569 if (PREFS_SELECTION_ALL != inlayer) {
1570 root = selection->activeContext();
1571 } else {
1572 root = desktop->currentRoot();
1573 }
1575 SPItem *item=next_item_from_list<Forward>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1577 if (item) {
1578 selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
1579 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1580 scroll_to_show_item(desktop, item);
1581 }
1582 }
1583 }
1585 void
1586 sp_selection_item_prev(void)
1587 {
1588 SPDocument *document = SP_ACTIVE_DOCUMENT;
1589 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1590 g_return_if_fail(document != NULL);
1591 g_return_if_fail(desktop != NULL);
1592 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1594 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
1595 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
1596 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
1598 SPObject *root;
1599 if (PREFS_SELECTION_ALL != inlayer) {
1600 root = selection->activeContext();
1601 } else {
1602 root = desktop->currentRoot();
1603 }
1605 SPItem *item=next_item_from_list<Reverse>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1607 if (item) {
1608 selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
1609 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1610 scroll_to_show_item(desktop, item);
1611 }
1612 }
1613 }
1615 void sp_selection_next_patheffect_param(SPDesktop * dt)
1616 {
1617 if (!dt) return;
1619 Inkscape::Selection *selection = sp_desktop_selection(dt);
1620 if ( selection && !selection->isEmpty() ) {
1621 SPItem *item = selection->singleItem();
1622 if ( item && SP_IS_SHAPE(item)) {
1623 if (sp_lpe_item_has_path_effect(SP_LPE_ITEM(item))) {
1624 sp_lpe_item_edit_next_param_oncanvas(SP_LPE_ITEM(item), dt);
1625 } else {
1626 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("The selection has no applied path effect."));
1627 }
1628 }
1629 }
1630 }
1632 void sp_selection_edit_clip_or_mask(SPDesktop * dt, bool clip)
1633 {
1634 if (!dt) return;
1636 Inkscape::Selection *selection = sp_desktop_selection(dt);
1637 if ( selection && !selection->isEmpty() ) {
1638 SPItem *item = selection->singleItem();
1639 if ( item ) {
1640 SPObject *obj = NULL;
1641 if (clip)
1642 obj = item->clip_ref ? SP_OBJECT(item->clip_ref->getObject()) : NULL;
1643 else
1644 obj = item->mask_ref ? SP_OBJECT(item->mask_ref->getObject()) : NULL;
1646 if (obj) {
1647 // obj is a group object, the children are the actual clippers
1648 for ( SPObject *child = obj->children ; child ; child = child->next ) {
1649 if ( SP_IS_ITEM(child) ) {
1650 // If not already in nodecontext, goto it!
1651 if (!tools_isactive(dt, TOOLS_NODES)) {
1652 tools_switch_current(TOOLS_NODES);
1653 }
1655 ShapeEditor * shape_editor = SP_NODE_CONTEXT( dt->event_context )->shape_editor;
1656 shape_editor->set_item(SP_ITEM(child));
1657 Inkscape::NodePath::Path *np = shape_editor->get_nodepath();
1658 if (np) {
1659 // take colors from prefs (same as used in outline mode)
1660 np->helperpath_rgba = clip ?
1661 prefs_get_int_attribute("options.wireframecolors", "clips", 0x00ff00ff) :
1662 prefs_get_int_attribute("options.wireframecolors", "masks", 0x0000ffff);
1663 np->helperpath_width = 1.0;
1664 sp_nodepath_show_helperpath(np, true);
1665 }
1666 break; // break out of for loop after 1st encountered item
1667 }
1668 }
1669 } else if (clip) {
1670 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("The selection has no applied clip path."));
1671 } else {
1672 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("The selection has no applied mask."));
1673 }
1674 }
1675 }
1676 }
1679 namespace {
1681 template <typename D>
1682 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items,
1683 SPObject *root, bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
1684 {
1685 SPObject *current=root;
1686 while (items) {
1687 SPItem *item=SP_ITEM(items->data);
1688 if ( root->isAncestorOf(item) &&
1689 ( !only_in_viewport || desktop->isWithinViewport(item) ) )
1690 {
1691 current = item;
1692 break;
1693 }
1694 items = items->next;
1695 }
1697 GSList *path=NULL;
1698 while ( current != root ) {
1699 path = g_slist_prepend(path, current);
1700 current = SP_OBJECT_PARENT(current);
1701 }
1703 SPItem *next;
1704 // first, try from the current object
1705 next = next_item<D>(desktop, path, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1706 g_slist_free(path);
1708 if (!next) { // if we ran out of objects, start over at the root
1709 next = next_item<D>(desktop, NULL, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1710 }
1712 return next;
1713 }
1715 template <typename D>
1716 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
1717 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
1718 {
1719 typename D::Iterator children;
1720 typename D::Iterator iter;
1722 SPItem *found=NULL;
1724 if (path) {
1725 SPObject *object=reinterpret_cast<SPObject *>(path->data);
1726 g_assert(SP_OBJECT_PARENT(object) == root);
1727 if (desktop->isLayer(object)) {
1728 found = next_item<D>(desktop, path->next, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1729 }
1730 iter = children = D::siblings_after(object);
1731 } else {
1732 iter = children = D::children(root);
1733 }
1735 while ( iter && !found ) {
1736 SPObject *object=D::object(iter);
1737 if (desktop->isLayer(object)) {
1738 if (PREFS_SELECTION_LAYER != inlayer) { // recurse into sublayers
1739 found = next_item<D>(desktop, NULL, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1740 }
1741 } else if ( SP_IS_ITEM(object) &&
1742 ( !only_in_viewport || desktop->isWithinViewport(SP_ITEM(object)) ) &&
1743 ( !onlyvisible || !desktop->itemIsHidden(SP_ITEM(object))) &&
1744 ( !onlysensitive || !SP_ITEM(object)->isLocked()) &&
1745 !desktop->isLayer(SP_ITEM(object)) )
1746 {
1747 found = SP_ITEM(object);
1748 }
1749 iter = D::next(iter);
1750 }
1752 D::dispose(children);
1754 return found;
1755 }
1757 }
1759 /**
1760 * If \a item is not entirely visible then adjust visible area to centre on the centre on of
1761 * \a item.
1762 */
1763 void scroll_to_show_item(SPDesktop *desktop, SPItem *item)
1764 {
1765 NR::Rect dbox = desktop->get_display_area();
1766 NR::Maybe<NR::Rect> sbox = sp_item_bbox_desktop(item);
1768 if ( sbox && dbox.contains(*sbox) == false ) {
1769 NR::Point const s_dt = sbox->midpoint();
1770 NR::Point const s_w = desktop->d2w(s_dt);
1771 NR::Point const d_dt = dbox.midpoint();
1772 NR::Point const d_w = desktop->d2w(d_dt);
1773 NR::Point const moved_w( d_w - s_w );
1774 gint const dx = (gint) moved_w[X];
1775 gint const dy = (gint) moved_w[Y];
1776 desktop->scroll_world(dx, dy);
1777 }
1778 }
1781 void
1782 sp_selection_clone()
1783 {
1784 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1785 if (desktop == NULL)
1786 return;
1788 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1790 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
1792 // check if something is selected
1793 if (selection->isEmpty()) {
1794 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object</b> to clone."));
1795 return;
1796 }
1798 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
1800 selection->clear();
1802 // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
1803 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
1805 GSList *newsel = NULL;
1807 while (reprs) {
1808 Inkscape::XML::Node *sel_repr = (Inkscape::XML::Node *) reprs->data;
1809 Inkscape::XML::Node *parent = sp_repr_parent(sel_repr);
1811 Inkscape::XML::Node *clone = xml_doc->createElement("svg:use");
1812 sp_repr_set_attr(clone, "x", "0");
1813 sp_repr_set_attr(clone, "y", "0");
1814 sp_repr_set_attr(clone, "xlink:href", g_strdup_printf("#%s", sel_repr->attribute("id")));
1816 sp_repr_set_attr(clone, "inkscape:transform-center-x", sel_repr->attribute("inkscape:transform-center-x"));
1817 sp_repr_set_attr(clone, "inkscape:transform-center-y", sel_repr->attribute("inkscape:transform-center-y"));
1819 // add the new clone to the top of the original's parent
1820 parent->appendChild(clone);
1822 newsel = g_slist_prepend(newsel, clone);
1823 reprs = g_slist_remove(reprs, sel_repr);
1824 Inkscape::GC::release(clone);
1825 }
1827 // TRANSLATORS: only translate "string" in "context|string".
1828 // For more details, see http://developer.gnome.org/doc/API/2.0/glib/glib-I18N.html#Q-:CAPS
1829 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_CLONE,
1830 Q_("action|Clone"));
1832 selection->setReprList(newsel);
1834 g_slist_free(newsel);
1835 }
1837 void
1838 sp_selection_unlink()
1839 {
1840 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1841 if (!desktop)
1842 return;
1844 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1846 if (selection->isEmpty()) {
1847 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>clone</b> to unlink."));
1848 return;
1849 }
1851 // Get a copy of current selection.
1852 GSList *new_select = NULL;
1853 bool unlinked = false;
1854 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
1855 items != NULL;
1856 items = items->next)
1857 {
1858 SPItem *item = (SPItem *) items->data;
1860 if (SP_IS_TEXT(item)) {
1861 SPObject *tspan = sp_tref_convert_to_tspan(SP_OBJECT(item));
1863 if (tspan) {
1864 SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1865 }
1867 // Set unlink to true, and fall into the next if which
1868 // will include this text item in the new selection
1869 unlinked = true;
1870 }
1872 if (!(SP_IS_USE(item) || SP_IS_TREF(item))) {
1873 // keep the non-use item in the new selection
1874 new_select = g_slist_prepend(new_select, item);
1875 continue;
1876 }
1878 SPItem *unlink;
1879 if (SP_IS_USE(item)) {
1880 unlink = sp_use_unlink(SP_USE(item));
1881 } else /*if (SP_IS_TREF(use))*/ {
1882 unlink = SP_ITEM(sp_tref_convert_to_tspan(SP_OBJECT(item)));
1883 }
1885 unlinked = true;
1886 // Add ungrouped items to the new selection.
1887 new_select = g_slist_prepend(new_select, unlink);
1888 }
1890 if (new_select) { // set new selection
1891 selection->clear();
1892 selection->setList(new_select);
1893 g_slist_free(new_select);
1894 }
1895 if (!unlinked) {
1896 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No clones to unlink</b> in the selection."));
1897 }
1899 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNLINK_CLONE,
1900 _("Unlink clone"));
1901 }
1903 void
1904 sp_select_clone_original()
1905 {
1906 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1907 if (desktop == NULL)
1908 return;
1910 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1912 SPItem *item = selection->singleItem();
1914 gchar const *error = _("Select a <b>clone</b> to go to its original. Select a <b>linked offset</b> to go to its source. Select a <b>text on path</b> to go to the path. Select a <b>flowed text</b> to go to its frame.");
1916 // Check if other than two objects are selected
1917 if (g_slist_length((GSList *) selection->itemList()) != 1 || !item) {
1918 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
1919 return;
1920 }
1922 SPItem *original = NULL;
1923 if (SP_IS_USE(item)) {
1924 original = sp_use_get_original (SP_USE(item));
1925 } else if (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) {
1926 original = sp_offset_get_source (SP_OFFSET(item));
1927 } else if (SP_IS_TEXT_TEXTPATH(item)) {
1928 original = sp_textpath_get_path_item (SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))));
1929 } else if (SP_IS_FLOWTEXT(item)) {
1930 original = SP_FLOWTEXT(item)->get_frame (NULL); // first frame only
1931 } else { // it's an object that we don't know what to do with
1932 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
1933 return;
1934 }
1936 if (!original) {
1937 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>Cannot find</b> the object to select (orphaned clone, offset, textpath, flowed text?)"));
1938 return;
1939 }
1941 for (SPObject *o = original; o && !SP_IS_ROOT(o); o = SP_OBJECT_PARENT (o)) {
1942 if (SP_IS_DEFS (o)) {
1943 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("The object you're trying to select is <b>not visible</b> (it is in <defs>)"));
1944 return;
1945 }
1946 }
1948 if (original) {
1949 selection->clear();
1950 selection->set(original);
1951 if (SP_CYCLING == SP_CYCLE_FOCUS) {
1952 scroll_to_show_item(desktop, original);
1953 }
1954 }
1955 }
1958 void sp_selection_to_marker(bool apply)
1959 {
1960 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1961 if (desktop == NULL)
1962 return;
1964 SPDocument *doc = sp_desktop_document(desktop);
1965 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
1967 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1969 // check if something is selected
1970 if (selection->isEmpty()) {
1971 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to marker."));
1972 return;
1973 }
1975 sp_document_ensure_up_to_date(doc);
1976 NR::Maybe<NR::Rect> r = selection->bounds();
1977 NR::Maybe<NR::Point> c = selection->center();
1978 if ( !r || !c || r->isEmpty() ) {
1979 return;
1980 }
1982 // calculate the transform to be applied to objects to move them to 0,0
1983 NR::Point move_p = NR::Point(0, sp_document_height(doc)) - *c;
1984 move_p[NR::Y] = -move_p[NR::Y];
1985 NR::Matrix move = NR::Matrix (NR::translate (move_p));
1987 GSList *items = g_slist_copy((GSList *) selection->itemList());
1989 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
1991 // bottommost object, after sorting
1992 SPObject *parent = SP_OBJECT_PARENT (items->data);
1994 NR::Matrix parent_transform = sp_item_i2root_affine(SP_ITEM(parent));
1996 // remember the position of the first item
1997 gint pos = SP_OBJECT_REPR (items->data)->position();
1998 (void)pos; // TODO check why this was remembered
2000 // create a list of duplicates
2001 GSList *repr_copies = NULL;
2002 for (GSList *i = items; i != NULL; i = i->next) {
2003 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2004 repr_copies = g_slist_prepend (repr_copies, dup);
2005 }
2007 NR::Rect bounds(desktop->dt2doc(r->min()), desktop->dt2doc(r->max()));
2009 if (apply) {
2010 // delete objects so that their clones don't get alerted; this object will be restored shortly
2011 for (GSList *i = items; i != NULL; i = i->next) {
2012 SPObject *item = SP_OBJECT (i->data);
2013 item->deleteObject (false);
2014 }
2015 }
2017 // Hack: Temporarily set clone compensation to unmoved, so that we can move clone-originals
2018 // without disturbing clones.
2019 // See ActorAlign::on_button_click() in src/ui/dialog/align-and-distribute.cpp
2020 int saved_compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2021 prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2023 gchar const *mark_id = generate_marker(repr_copies, bounds, doc,
2024 ( NR::Matrix(NR::translate(desktop->dt2doc(NR::Point(r->min()[NR::X],
2025 r->max()[NR::Y]))))
2026 * parent_transform.inverse() ),
2027 parent_transform * move);
2028 (void)mark_id;
2030 // restore compensation setting
2031 prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
2034 g_slist_free (items);
2036 sp_document_done (doc, SP_VERB_EDIT_SELECTION_2_MARKER,
2037 _("Objects to marker"));
2038 }
2040 static void sp_selection_to_guides_recursive(SPItem *item, bool deleteitem, bool wholegroups) {
2041 if (SP_IS_GROUP(item) && !SP_IS_BOX3D(item) && !wholegroups) {
2042 for (GSList *i = sp_item_group_item_list (SP_GROUP(item)); i != NULL; i = i->next) {
2043 sp_selection_to_guides_recursive(SP_ITEM(i->data), deleteitem, wholegroups);
2044 }
2045 } else {
2046 sp_item_convert_item_to_guides(item);
2048 if (deleteitem) {
2049 SP_OBJECT(item)->deleteObject(true);
2050 }
2051 }
2052 }
2054 void sp_selection_to_guides()
2055 {
2056 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2057 if (desktop == NULL)
2058 return;
2060 SPDocument *doc = sp_desktop_document(desktop);
2061 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2062 // we need to copy the list because it gets reset when objects are deleted
2063 GSList *items = g_slist_copy((GSList *) selection->itemList());
2065 if (!items) {
2066 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to guides."));
2067 return;
2068 }
2070 bool deleteitem = (prefs_get_int_attribute("tools", "cvg_keep_objects", 0) == 0);
2071 bool wholegroups = (prefs_get_int_attribute("tools", "cvg_convert_whole_groups", 0) != 0);
2073 for (GSList const *i = items; i != NULL; i = i->next) {
2074 sp_selection_to_guides_recursive(SP_ITEM(i->data), deleteitem, wholegroups);
2075 }
2077 sp_document_done (doc, SP_VERB_EDIT_SELECTION_2_GUIDES, _("Objects to guides"));
2078 }
2080 void
2081 sp_selection_tile(bool apply)
2082 {
2083 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2084 if (desktop == NULL)
2085 return;
2087 SPDocument *doc = sp_desktop_document(desktop);
2088 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2090 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2092 // check if something is selected
2093 if (selection->isEmpty()) {
2094 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to pattern."));
2095 return;
2096 }
2098 sp_document_ensure_up_to_date(doc);
2099 NR::Maybe<NR::Rect> r = selection->bounds();
2100 if ( !r || r->isEmpty() ) {
2101 return;
2102 }
2104 // calculate the transform to be applied to objects to move them to 0,0
2105 NR::Point move_p = NR::Point(0, sp_document_height(doc)) - (r->min() + NR::Point (0, r->extent(NR::Y)));
2106 move_p[NR::Y] = -move_p[NR::Y];
2107 NR::Matrix move = NR::Matrix (NR::translate (move_p));
2109 GSList *items = g_slist_copy((GSList *) selection->itemList());
2111 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2113 // bottommost object, after sorting
2114 SPObject *parent = SP_OBJECT_PARENT (items->data);
2116 NR::Matrix parent_transform = sp_item_i2root_affine(SP_ITEM(parent));
2118 // remember the position of the first item
2119 gint pos = SP_OBJECT_REPR (items->data)->position();
2121 // create a list of duplicates
2122 GSList *repr_copies = NULL;
2123 for (GSList *i = items; i != NULL; i = i->next) {
2124 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2125 repr_copies = g_slist_prepend (repr_copies, dup);
2126 }
2127 // restore the z-order after prepends
2128 repr_copies = g_slist_reverse (repr_copies);
2130 NR::Rect bounds(desktop->dt2doc(r->min()), desktop->dt2doc(r->max()));
2132 if (apply) {
2133 // delete objects so that their clones don't get alerted; this object will be restored shortly
2134 for (GSList *i = items; i != NULL; i = i->next) {
2135 SPObject *item = SP_OBJECT (i->data);
2136 item->deleteObject (false);
2137 }
2138 }
2140 // Hack: Temporarily set clone compensation to unmoved, so that we can move clone-originals
2141 // without disturbing clones.
2142 // See ActorAlign::on_button_click() in src/ui/dialog/align-and-distribute.cpp
2143 int saved_compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2144 prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2146 gchar const *pat_id = pattern_tile(repr_copies, bounds, doc,
2147 ( NR::Matrix(NR::translate(desktop->dt2doc(NR::Point(r->min()[NR::X],
2148 r->max()[NR::Y]))))
2149 * parent_transform.inverse() ),
2150 parent_transform * move);
2152 // restore compensation setting
2153 prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
2155 if (apply) {
2156 Inkscape::XML::Node *rect = xml_doc->createElement("svg:rect");
2157 rect->setAttribute("style", g_strdup_printf("stroke:none;fill:url(#%s)", pat_id));
2159 NR::Point min = bounds.min() * parent_transform.inverse();
2160 NR::Point max = bounds.max() * parent_transform.inverse();
2162 sp_repr_set_svg_double(rect, "width", max[NR::X] - min[NR::X]);
2163 sp_repr_set_svg_double(rect, "height", max[NR::Y] - min[NR::Y]);
2164 sp_repr_set_svg_double(rect, "x", min[NR::X]);
2165 sp_repr_set_svg_double(rect, "y", min[NR::Y]);
2167 // restore parent and position
2168 SP_OBJECT_REPR (parent)->appendChild(rect);
2169 rect->setPosition(pos > 0 ? pos : 0);
2170 SPItem *rectangle = (SPItem *) sp_desktop_document (desktop)->getObjectByRepr(rect);
2172 Inkscape::GC::release(rect);
2174 selection->clear();
2175 selection->set(rectangle);
2176 }
2178 g_slist_free (items);
2180 sp_document_done (doc, SP_VERB_EDIT_TILE,
2181 _("Objects to pattern"));
2182 }
2184 void
2185 sp_selection_untile()
2186 {
2187 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2188 if (desktop == NULL)
2189 return;
2191 SPDocument *doc = sp_desktop_document(desktop);
2192 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2194 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2196 // check if something is selected
2197 if (selection->isEmpty()) {
2198 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object with pattern fill</b> to extract objects from."));
2199 return;
2200 }
2202 GSList *new_select = NULL;
2204 bool did = false;
2206 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2207 items != NULL;
2208 items = items->next) {
2210 SPItem *item = (SPItem *) items->data;
2212 SPStyle *style = SP_OBJECT_STYLE (item);
2214 if (!style || !style->fill.isPaintserver())
2215 continue;
2217 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
2219 if (!SP_IS_PATTERN(server))
2220 continue;
2222 did = true;
2224 SPPattern *pattern = pattern_getroot (SP_PATTERN (server));
2226 NR::Matrix pat_transform = pattern_patternTransform (SP_PATTERN (server));
2227 pat_transform *= item->transform;
2229 for (SPObject *child = sp_object_first_child(SP_OBJECT(pattern)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2230 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate(xml_doc);
2231 SPItem *i = SP_ITEM (desktop->currentLayer()->appendChildRepr(copy));
2233 // FIXME: relink clones to the new canvas objects
2234 // use SPObject::setid when mental finishes it to steal ids of
2236 // this is needed to make sure the new item has curve (simply requestDisplayUpdate does not work)
2237 sp_document_ensure_up_to_date (doc);
2239 NR::Matrix transform( i->transform * pat_transform );
2240 sp_item_write_transform(i, SP_OBJECT_REPR(i), transform);
2242 new_select = g_slist_prepend(new_select, i);
2243 }
2245 SPCSSAttr *css = sp_repr_css_attr_new ();
2246 sp_repr_css_set_property (css, "fill", "none");
2247 sp_repr_css_change (SP_OBJECT_REPR (item), css, "style");
2248 }
2250 if (!did) {
2251 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No pattern fills</b> in the selection."));
2252 } else {
2253 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNTILE,
2254 _("Pattern to objects"));
2255 selection->setList(new_select);
2256 }
2257 }
2259 void
2260 sp_selection_get_export_hints (Inkscape::Selection *selection, char const **filename, float *xdpi, float *ydpi)
2261 {
2262 if (selection->isEmpty()) {
2263 return;
2264 }
2266 GSList const *reprlst = selection->reprList();
2267 bool filename_search = TRUE;
2268 bool xdpi_search = TRUE;
2269 bool ydpi_search = TRUE;
2271 for(; reprlst != NULL &&
2272 filename_search &&
2273 xdpi_search &&
2274 ydpi_search;
2275 reprlst = reprlst->next) {
2276 gchar const *dpi_string;
2277 Inkscape::XML::Node * repr = (Inkscape::XML::Node *)reprlst->data;
2279 if (filename_search) {
2280 *filename = repr->attribute("inkscape:export-filename");
2281 if (*filename != NULL)
2282 filename_search = FALSE;
2283 }
2285 if (xdpi_search) {
2286 dpi_string = NULL;
2287 dpi_string = repr->attribute("inkscape:export-xdpi");
2288 if (dpi_string != NULL) {
2289 *xdpi = atof(dpi_string);
2290 xdpi_search = FALSE;
2291 }
2292 }
2294 if (ydpi_search) {
2295 dpi_string = NULL;
2296 dpi_string = repr->attribute("inkscape:export-ydpi");
2297 if (dpi_string != NULL) {
2298 *ydpi = atof(dpi_string);
2299 ydpi_search = FALSE;
2300 }
2301 }
2302 }
2303 }
2305 void
2306 sp_document_get_export_hints (SPDocument *doc, char const **filename, float *xdpi, float *ydpi)
2307 {
2308 Inkscape::XML::Node * repr = sp_document_repr_root(doc);
2309 gchar const *dpi_string;
2311 *filename = repr->attribute("inkscape:export-filename");
2313 dpi_string = NULL;
2314 dpi_string = repr->attribute("inkscape:export-xdpi");
2315 if (dpi_string != NULL) {
2316 *xdpi = atof(dpi_string);
2317 }
2319 dpi_string = NULL;
2320 dpi_string = repr->attribute("inkscape:export-ydpi");
2321 if (dpi_string != NULL) {
2322 *ydpi = atof(dpi_string);
2323 }
2324 }
2326 void
2327 sp_selection_create_bitmap_copy ()
2328 {
2329 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2330 if (desktop == NULL)
2331 return;
2333 SPDocument *document = sp_desktop_document(desktop);
2334 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
2336 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2338 // check if something is selected
2339 if (selection->isEmpty()) {
2340 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to make a bitmap copy."));
2341 return;
2342 }
2344 // Get the bounding box of the selection
2345 NRRect bbox;
2346 sp_document_ensure_up_to_date (document);
2347 selection->bounds(&bbox);
2348 if (NR_RECT_DFLS_TEST_EMPTY(&bbox)) {
2349 return; // exceptional situation, so not bother with a translatable error message, just quit quietly
2350 }
2352 // List of the items to show; all others will be hidden
2353 GSList *items = g_slist_copy ((GSList *) selection->itemList());
2355 // Sort items so that the topmost comes last
2356 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
2358 // Generate a random value from the current time (you may create bitmap from the same object(s)
2359 // multiple times, and this is done so that they don't clash)
2360 GTimeVal cu;
2361 g_get_current_time (&cu);
2362 guint current = (int) (cu.tv_sec * 1000000 + cu.tv_usec) % 1024;
2364 // Create the filename
2365 gchar *filename = g_strdup_printf ("%s-%s-%u.png", document->name, SP_OBJECT_REPR(items->data)->attribute("id"), current);
2366 // Imagemagick is known not to handle spaces in filenames, so we replace anything but letters,
2367 // digits, and a few other chars, with "_"
2368 filename = g_strcanon (filename, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.=+~$#@^&!?", '_');
2369 // Build the complete path by adding document->base if set
2370 gchar *filepath = g_build_filename (document->base?document->base:"", filename, NULL);
2372 //g_print ("%s\n", filepath);
2374 // Remember parent and z-order of the topmost one
2375 gint pos = SP_OBJECT_REPR(g_slist_last(items)->data)->position();
2376 SPObject *parent_object = SP_OBJECT_PARENT(g_slist_last(items)->data);
2377 Inkscape::XML::Node *parent = SP_OBJECT_REPR(parent_object);
2379 // Calculate resolution
2380 double res;
2381 int const prefs_res = prefs_get_int_attribute ("options.createbitmap", "resolution", 0);
2382 int const prefs_min = prefs_get_int_attribute ("options.createbitmap", "minsize", 0);
2383 if (0 < prefs_res) {
2384 // If it's given explicitly in prefs, take it
2385 res = prefs_res;
2386 } else if (0 < prefs_min) {
2387 // If minsize is given, look up minimum bitmap size (default 250 pixels) and calculate resolution from it
2388 res = PX_PER_IN * prefs_min / MIN ((bbox.x1 - bbox.x0), (bbox.y1 - bbox.y0));
2389 } else {
2390 float hint_xdpi = 0, hint_ydpi = 0;
2391 char const *hint_filename;
2392 // take resolution hint from the selected objects
2393 sp_selection_get_export_hints (selection, &hint_filename, &hint_xdpi, &hint_ydpi);
2394 if (hint_xdpi != 0) {
2395 res = hint_xdpi;
2396 } else {
2397 // take resolution hint from the document
2398 sp_document_get_export_hints (document, &hint_filename, &hint_xdpi, &hint_ydpi);
2399 if (hint_xdpi != 0) {
2400 res = hint_xdpi;
2401 } else {
2402 // if all else fails, take the default 90 dpi
2403 res = PX_PER_IN;
2404 }
2405 }
2406 }
2408 // The width and height of the bitmap in pixels
2409 unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * res / PX_PER_IN);
2410 unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * res / PX_PER_IN);
2412 // Find out if we have to run a filter
2413 gchar const *run = NULL;
2414 gchar const *filter = prefs_get_string_attribute ("options.createbitmap", "filter");
2415 if (filter) {
2416 // filter command is given;
2417 // see if we have a parameter to pass to it
2418 gchar const *param1 = prefs_get_string_attribute ("options.createbitmap", "filter_param1");
2419 if (param1) {
2420 if (param1[strlen(param1) - 1] == '%') {
2421 // if the param string ends with %, interpret it as a percentage of the image's max dimension
2422 gchar p1[256];
2423 g_ascii_dtostr (p1, 256, ceil (g_ascii_strtod (param1, NULL) * MAX(width, height) / 100));
2424 // the first param is always the image filename, the second is param1
2425 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, p1);
2426 } else {
2427 // otherwise pass the param1 unchanged
2428 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, param1);
2429 }
2430 } else {
2431 // run without extra parameter
2432 run = g_strdup_printf ("%s \"%s\"", filter, filepath);
2433 }
2434 }
2436 // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
2437 NR::Matrix eek = sp_item_i2d_affine (SP_ITEM(parent_object));
2438 NR::Matrix t;
2440 double shift_x = bbox.x0;
2441 double shift_y = bbox.y1;
2442 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
2443 shift_x = round (shift_x);
2444 shift_y = -round (-shift_y); // this gets correct rounding despite coordinate inversion, remove the negations when the inversion is gone
2445 }
2446 t = NR::scale(1, -1) * NR::translate (shift_x, shift_y) * eek.inverse();
2448 // Do the export
2449 sp_export_png_file(document, filepath,
2450 bbox.x0, bbox.y0, bbox.x1, bbox.y1,
2451 width, height, res, res,
2452 (guint32) 0xffffff00,
2453 NULL, NULL,
2454 true, /*bool force_overwrite,*/
2455 items);
2457 g_slist_free (items);
2459 // Run filter, if any
2460 if (run) {
2461 g_print ("Running external filter: %s\n", run);
2462 system (run);
2463 }
2465 // Import the image back
2466 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filepath, NULL);
2467 if (pb) {
2468 // Create the repr for the image
2469 Inkscape::XML::Node * repr = xml_doc->createElement("svg:image");
2470 repr->setAttribute("xlink:href", filename);
2471 repr->setAttribute("sodipodi:absref", filepath);
2472 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
2473 sp_repr_set_svg_double(repr, "width", width);
2474 sp_repr_set_svg_double(repr, "height", height);
2475 } else {
2476 sp_repr_set_svg_double(repr, "width", (bbox.x1 - bbox.x0));
2477 sp_repr_set_svg_double(repr, "height", (bbox.y1 - bbox.y0));
2478 }
2480 // Write transform
2481 gchar *c=sp_svg_transform_write(t);
2482 repr->setAttribute("transform", c);
2483 g_free(c);
2485 // add the new repr to the parent
2486 parent->appendChild(repr);
2488 // move to the saved position
2489 repr->setPosition(pos > 0 ? pos + 1 : 1);
2491 // Set selection to the new image
2492 selection->clear();
2493 selection->add(repr);
2495 // Clean up
2496 Inkscape::GC::release(repr);
2497 gdk_pixbuf_unref (pb);
2499 // Complete undoable transaction
2500 sp_document_done (document, SP_VERB_SELECTION_CREATE_BITMAP,
2501 _("Create bitmap"));
2502 }
2504 g_free (filename);
2505 g_free (filepath);
2506 }
2508 /**
2509 * \brief Creates a mask or clipPath from selection
2510 * Two different modes:
2511 * if applyToLayer, all selection is moved to DEFS as mask/clippath
2512 * and is applied to current layer
2513 * otherwise, topmost object is used as mask for other objects
2514 * If \a apply_clip_path parameter is true, clipPath is created, otherwise mask
2515 *
2516 */
2517 void
2518 sp_selection_set_mask(bool apply_clip_path, bool apply_to_layer)
2519 {
2520 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2521 if (desktop == NULL)
2522 return;
2524 SPDocument *doc = sp_desktop_document(desktop);
2525 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2527 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2529 // check if something is selected
2530 bool is_empty = selection->isEmpty();
2531 if ( apply_to_layer && is_empty) {
2532 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to create clippath or mask from."));
2533 return;
2534 } else if (!apply_to_layer && ( is_empty || NULL == selection->itemList()->next )) {
2535 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select mask object and <b>object(s)</b> to apply clippath or mask to."));
2536 return;
2537 }
2539 // FIXME: temporary patch to prevent crash!
2540 // Remove this when bboxes are fixed to not blow up on an item clipped/masked with its own clone
2541 bool clone_with_original = selection_contains_both_clone_and_original (selection);
2542 if (clone_with_original) {
2543 return; // in this version, you cannot clip/mask an object with its own clone
2544 }
2545 // /END FIXME
2547 sp_document_ensure_up_to_date(doc);
2549 GSList *items = g_slist_copy((GSList *) selection->itemList());
2551 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2553 // create a list of duplicates
2554 GSList *mask_items = NULL;
2555 GSList *apply_to_items = NULL;
2556 GSList *items_to_delete = NULL;
2557 bool topmost = prefs_get_int_attribute ("options.maskobject", "topmost", 1);
2558 bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
2560 if (apply_to_layer) {
2561 // all selected items are used for mask, which is applied to a layer
2562 apply_to_items = g_slist_prepend (apply_to_items, desktop->currentLayer());
2564 for (GSList *i = items; i != NULL; i = i->next) {
2565 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2566 mask_items = g_slist_prepend (mask_items, dup);
2568 if (remove_original) {
2569 SPObject *item = SP_OBJECT (i->data);
2570 items_to_delete = g_slist_prepend (items_to_delete, item);
2571 }
2572 }
2573 } else if (!topmost) {
2574 // topmost item is used as a mask, which is applied to other items in a selection
2575 GSList *i = items;
2576 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2577 mask_items = g_slist_prepend (mask_items, dup);
2579 if (remove_original) {
2580 SPObject *item = SP_OBJECT (i->data);
2581 items_to_delete = g_slist_prepend (items_to_delete, item);
2582 }
2584 for (i = i->next; i != NULL; i = i->next) {
2585 apply_to_items = g_slist_prepend (apply_to_items, i->data);
2586 }
2587 } else {
2588 GSList *i = NULL;
2589 for (i = items; NULL != i->next; i = i->next) {
2590 apply_to_items = g_slist_prepend (apply_to_items, i->data);
2591 }
2593 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2594 mask_items = g_slist_prepend (mask_items, dup);
2596 if (remove_original) {
2597 SPObject *item = SP_OBJECT (i->data);
2598 items_to_delete = g_slist_prepend (items_to_delete, item);
2599 }
2600 }
2602 g_slist_free (items);
2603 items = NULL;
2605 gchar const *attributeName = apply_clip_path ? "clip-path" : "mask";
2606 for (GSList *i = apply_to_items; NULL != i; i = i->next) {
2607 SPItem *item = reinterpret_cast<SPItem *>(i->data);
2608 // inverted object transform should be applied to a mask object,
2609 // as mask is calculated in user space (after applying transform)
2610 NR::Matrix maskTransform (item->transform.inverse());
2612 GSList *mask_items_dup = NULL;
2613 for (GSList *mask_item = mask_items; NULL != mask_item; mask_item = mask_item->next) {
2614 Inkscape::XML::Node *dup = reinterpret_cast<Inkscape::XML::Node *>(mask_item->data)->duplicate(xml_doc);
2615 mask_items_dup = g_slist_prepend (mask_items_dup, dup);
2616 }
2618 gchar const *mask_id = NULL;
2619 if (apply_clip_path) {
2620 mask_id = sp_clippath_create(mask_items_dup, doc, &maskTransform);
2621 } else {
2622 mask_id = sp_mask_create(mask_items_dup, doc, &maskTransform);
2623 }
2625 g_slist_free (mask_items_dup);
2626 mask_items_dup = NULL;
2628 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, g_strdup_printf("url(#%s)", mask_id));
2629 }
2631 g_slist_free (mask_items);
2632 g_slist_free (apply_to_items);
2634 for (GSList *i = items_to_delete; NULL != i; i = i->next) {
2635 SPObject *item = SP_OBJECT (i->data);
2636 item->deleteObject (false);
2637 }
2638 g_slist_free (items_to_delete);
2640 if (apply_clip_path)
2641 sp_document_done (doc, SP_VERB_OBJECT_SET_CLIPPATH, _("Set clipping path"));
2642 else
2643 sp_document_done (doc, SP_VERB_OBJECT_SET_MASK, _("Set mask"));
2644 }
2646 void sp_selection_unset_mask(bool apply_clip_path) {
2647 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2648 if (desktop == NULL)
2649 return;
2651 SPDocument *doc = sp_desktop_document(desktop);
2652 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2653 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2655 // check if something is selected
2656 if (selection->isEmpty()) {
2657 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to remove clippath or mask from."));
2658 return;
2659 }
2661 bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
2662 sp_document_ensure_up_to_date(doc);
2664 gchar const *attributeName = apply_clip_path ? "clip-path" : "mask";
2665 std::map<SPObject*,SPItem*> referenced_objects;
2666 for (GSList const *i = selection->itemList(); NULL != i; i = i->next) {
2667 if (remove_original) {
2668 // remember referenced mask/clippath, so orphaned masks can be moved back to document
2669 SPItem *item = reinterpret_cast<SPItem *>(i->data);
2670 Inkscape::URIReference *uri_ref = NULL;
2672 if (apply_clip_path) {
2673 uri_ref = item->clip_ref;
2674 } else {
2675 uri_ref = item->mask_ref;
2676 }
2678 // collect distinct mask object (and associate with item to apply transform)
2679 if (NULL != uri_ref && NULL != uri_ref->getObject()) {
2680 referenced_objects[uri_ref->getObject()] = item;
2681 }
2682 }
2684 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, "none");
2685 }
2687 // restore mask objects into a document
2688 for ( std::map<SPObject*,SPItem*>::iterator it = referenced_objects.begin() ; it != referenced_objects.end() ; ++it) {
2689 SPObject *obj = (*it).first;
2690 GSList *items_to_move = NULL;
2691 for (SPObject *child = sp_object_first_child(obj) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2692 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate(xml_doc);
2693 items_to_move = g_slist_prepend (items_to_move, copy);
2694 }
2696 if (!obj->isReferenced()) {
2697 // delete from defs if no other object references this mask
2698 obj->deleteObject(false);
2699 }
2701 // remember parent and position of the item to which the clippath/mask was applied
2702 Inkscape::XML::Node *parent = SP_OBJECT_REPR((*it).second)->parent();
2703 gint pos = SP_OBJECT_REPR((*it).second)->position();
2705 for (GSList *i = items_to_move; NULL != i; i = i->next) {
2706 Inkscape::XML::Node *repr = (Inkscape::XML::Node *)i->data;
2708 // insert into parent, restore pos
2709 parent->appendChild(repr);
2710 repr->setPosition((pos + 1) > 0 ? (pos + 1) : 0);
2712 SPItem *mask_item = (SPItem *) sp_desktop_document (desktop)->getObjectByRepr(repr);
2713 selection->add(repr);
2715 // transform mask, so it is moved the same spot where mask was applied
2716 NR::Matrix transform (mask_item->transform);
2717 transform *= (*it).second->transform;
2718 sp_item_write_transform(mask_item, SP_OBJECT_REPR(mask_item), transform);
2719 }
2721 g_slist_free (items_to_move);
2722 }
2724 if (apply_clip_path)
2725 sp_document_done (doc, SP_VERB_OBJECT_UNSET_CLIPPATH, _("Release clipping path"));
2726 else
2727 sp_document_done (doc, SP_VERB_OBJECT_UNSET_MASK, _("Release mask"));
2728 }
2730 void fit_canvas_to_selection(SPDesktop *desktop) {
2731 g_return_if_fail(desktop != NULL);
2732 SPDocument *doc = sp_desktop_document(desktop);
2734 g_return_if_fail(doc != NULL);
2735 g_return_if_fail(desktop->selection != NULL);
2737 if (desktop->selection->isEmpty()) {
2738 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to fit canvas to."));
2739 return;
2740 }
2741 NR::Maybe<NR::Rect> const bbox(desktop->selection->bounds());
2742 if (bbox && !bbox->isEmpty()) {
2743 doc->fitToRect(*bbox);
2744 }
2745 };
2747 void fit_canvas_to_drawing(SPDocument *doc) {
2748 g_return_if_fail(doc != NULL);
2750 sp_document_ensure_up_to_date(doc);
2751 SPItem const *const root = SP_ITEM(doc->root);
2752 NR::Maybe<NR::Rect> const bbox(root->getBounds(sp_item_i2r_affine(root)));
2753 if (bbox && !bbox->isEmpty()) {
2754 doc->fitToRect(*bbox);
2755 }
2756 };
2758 void fit_canvas_to_selection_or_drawing(SPDesktop *desktop) {
2759 g_return_if_fail(desktop != NULL);
2760 SPDocument *doc = sp_desktop_document(desktop);
2762 g_return_if_fail(doc != NULL);
2763 g_return_if_fail(desktop->selection != NULL);
2765 if (desktop->selection->isEmpty()) {
2766 fit_canvas_to_drawing(doc);
2767 } else {
2768 fit_canvas_to_selection(desktop);
2769 }
2771 sp_document_done(doc, SP_VERB_FIT_CANVAS_TO_DRAWING,
2772 _("Fit page to selection"));
2773 };
2775 static void itemtree_map(void (*f)(SPItem *, SPDesktop *), SPObject *root, SPDesktop *desktop) {
2776 // don't operate on layers
2777 if (SP_IS_ITEM(root) && !desktop->isLayer(SP_ITEM(root))) {
2778 f(SP_ITEM(root), desktop);
2779 }
2780 for ( SPObject::SiblingIterator iter = root->firstChild() ; iter ; ++iter ) {
2781 //don't recurse into locked layers
2782 if (!(SP_IS_ITEM(&*iter) && desktop->isLayer(SP_ITEM(&*iter)) && SP_ITEM(&*iter)->isLocked())) {
2783 itemtree_map(f, iter, desktop);
2784 }
2785 }
2786 }
2788 static void unlock(SPItem *item, SPDesktop */*desktop*/) {
2789 if (item->isLocked()) {
2790 item->setLocked(FALSE);
2791 }
2792 }
2794 static void unhide(SPItem *item, SPDesktop *desktop) {
2795 if (desktop->itemIsHidden(item)) {
2796 item->setExplicitlyHidden(FALSE);
2797 }
2798 }
2800 static void process_all(void (*f)(SPItem *, SPDesktop *), SPDesktop *dt, bool layer_only) {
2801 if (!dt) return;
2803 SPObject *root;
2804 if (layer_only) {
2805 root = dt->currentLayer();
2806 } else {
2807 root = dt->currentRoot();
2808 }
2810 itemtree_map(f, root, dt);
2811 }
2813 void unlock_all(SPDesktop *dt) {
2814 process_all(&unlock, dt, true);
2815 }
2817 void unlock_all_in_all_layers(SPDesktop *dt) {
2818 process_all(&unlock, dt, false);
2819 }
2821 void unhide_all(SPDesktop *dt) {
2822 process_all(&unhide, dt, true);
2823 }
2825 void unhide_all_in_all_layers(SPDesktop *dt) {
2826 process_all(&unhide, dt, false);
2827 }
2830 /*
2831 Local Variables:
2832 mode:c++
2833 c-file-style:"stroustrup"
2834 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2835 indent-tabs-mode:nil
2836 fill-column:99
2837 End:
2838 */
2839 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :