1 /** @file
2 * @brief Miscellanous operations on selected items
3 */
4 /* Authors:
5 * Lauris Kaplinski <lauris@kaplinski.com>
6 * Frank Felfe <innerspace@iname.com>
7 * MenTaLguY <mental@rydia.net>
8 * bulia byak <buliabyak@users.sf.net>
9 * Andrius R. <knutux@gmail.com>
10 * Jon A. Cruz <jon@joncruz.org>
11 * Martin Sucha <martin.sucha-inkscape@jts-sro.sk>
12 *
13 * Copyright (C) 1999-2010 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 // TOOD fixme: This should be moved into preference repr
26 SPCycleType SP_CYCLING = SP_CYCLE_FOCUS;
29 #include <gtkmm/clipboard.h>
31 #include "svg/svg.h"
32 #include "desktop.h"
33 #include "desktop-style.h"
34 #include "dir-util.h"
35 #include "selection.h"
36 #include "tools-switch.h"
37 #include "desktop-handles.h"
38 #include "message-stack.h"
39 #include "sp-item-transform.h"
40 #include "marker.h"
41 #include "sp-use.h"
42 #include "sp-textpath.h"
43 #include "sp-tspan.h"
44 #include "sp-tref.h"
45 #include "sp-flowtext.h"
46 #include "sp-flowregion.h"
47 #include "sp-image.h"
48 #include "text-editing.h"
49 #include "text-context.h"
50 #include "connector-context.h"
51 #include "sp-path.h"
52 #include "sp-conn-end.h"
53 #include "dropper-context.h"
54 #include <glibmm/i18n.h>
55 #include "libnr/nr-matrix-rotate-ops.h"
56 #include "libnr/nr-matrix-translate-ops.h"
57 #include "libnr/nr-scale-ops.h"
58 #include <libnr/nr-matrix-ops.h>
59 #include <2geom/transforms.h>
60 #include "xml/repr.h"
61 #include "xml/rebase-hrefs.h"
62 #include "style.h"
63 #include "document-private.h"
64 #include "sp-gradient.h"
65 #include "sp-gradient-reference.h"
66 #include "sp-linear-gradient-fns.h"
67 #include "sp-pattern.h"
68 #include "sp-radial-gradient-fns.h"
69 #include "gradient-context.h"
70 #include "sp-namedview.h"
71 #include "preferences.h"
72 #include "sp-offset.h"
73 #include "sp-clippath.h"
74 #include "sp-mask.h"
75 #include "file.h"
76 #include "helper/png-write.h"
77 #include "layer-fns.h"
78 #include "context-fns.h"
79 #include <map>
80 #include <cstring>
81 #include <string>
82 #include "helper/units.h"
83 #include "sp-item.h"
84 #include "box3d.h"
85 #include "persp3d.h"
86 #include "unit-constants.h"
87 #include "xml/simple-document.h"
88 #include "sp-filter-reference.h"
89 #include "gradient-drag.h"
90 #include "uri-references.h"
91 #include "libnr/nr-convert2geom.h"
92 #include "display/curve.h"
93 #include "display/canvas-bpath.h"
94 #include "inkscape-private.h"
95 #include "path-chemistry.h"
96 #include "ui/tool/control-point-selection.h"
97 #include "ui/tool/multi-path-manipulator.h"
99 #include "enums.h"
100 #include "sp-item-group.h"
102 // For clippath editing
103 #include "tools-switch.h"
104 #include "ui/tool/node-tool.h"
106 #include "ui/clipboard.h"
108 using Geom::X;
109 using Geom::Y;
111 /* The clipboard handling is in ui/clipboard.cpp now. There are some legacy functions left here,
112 because the layer manipulation code uses them. It should be rewritten specifically
113 for that purpose. */
117 namespace Inkscape {
119 void SelectionHelper::selectAll(SPDesktop *dt)
120 {
121 if (tools_isactive(dt, TOOLS_NODES)) {
122 InkNodeTool *nt = static_cast<InkNodeTool*>(dt->event_context);
123 if (!nt->_multipath->empty()) {
124 nt->_multipath->selectSubpaths();
125 return;
126 }
127 }
128 sp_edit_select_all(dt);
129 }
131 void SelectionHelper::selectAllInAll(SPDesktop *dt)
132 {
133 if (tools_isactive(dt, TOOLS_NODES)) {
134 InkNodeTool *nt = static_cast<InkNodeTool*>(dt->event_context);
135 nt->_selected_nodes->selectAll();
136 } else {
137 sp_edit_select_all_in_all_layers(dt);
138 }
139 }
141 void SelectionHelper::selectNone(SPDesktop *dt)
142 {
143 if (tools_isactive(dt, TOOLS_NODES)) {
144 InkNodeTool *nt = static_cast<InkNodeTool*>(dt->event_context);
145 nt->_selected_nodes->clear();
146 } else {
147 sp_desktop_selection(dt)->clear();
148 }
149 }
151 void SelectionHelper::invert(SPDesktop *dt)
152 {
153 if (tools_isactive(dt, TOOLS_NODES)) {
154 InkNodeTool *nt = static_cast<InkNodeTool*>(dt->event_context);
155 nt->_multipath->invertSelectionInSubpaths();
156 } else {
157 sp_edit_invert(dt);
158 }
159 }
161 void SelectionHelper::invertAllInAll(SPDesktop *dt)
162 {
163 if (tools_isactive(dt, TOOLS_NODES)) {
164 InkNodeTool *nt = static_cast<InkNodeTool*>(dt->event_context);
165 nt->_selected_nodes->invertSelection();
166 } else {
167 sp_edit_invert_in_all_layers(dt);
168 }
169 }
171 void SelectionHelper::reverse(SPDesktop *dt)
172 {
173 // TODO make this a virtual method of event context!
174 if (tools_isactive(dt, TOOLS_NODES)) {
175 InkNodeTool *nt = static_cast<InkNodeTool*>(dt->event_context);
176 nt->_multipath->reverseSubpaths();
177 } else {
178 sp_selected_path_reverse(dt);
179 }
180 }
182 void SelectionHelper::selectNext(SPDesktop *dt)
183 {
184 SPEventContext *ec = dt->event_context;
185 if (tools_isactive(dt, TOOLS_NODES)) {
186 InkNodeTool *nt = static_cast<InkNodeTool*>(dt->event_context);
187 nt->_multipath->shiftSelection(1);
188 } else if (tools_isactive(dt, TOOLS_GRADIENT)
189 && ec->_grdrag->isNonEmpty()) {
190 sp_gradient_context_select_next(ec);
191 } else {
192 sp_selection_item_next(dt);
193 }
194 }
196 void SelectionHelper::selectPrev(SPDesktop *dt)
197 {
198 SPEventContext *ec = dt->event_context;
199 if (tools_isactive(dt, TOOLS_NODES)) {
200 InkNodeTool *nt = static_cast<InkNodeTool*>(dt->event_context);
201 nt->_multipath->shiftSelection(-1);
202 } else if (tools_isactive(dt, TOOLS_GRADIENT)
203 && ec->_grdrag->isNonEmpty()) {
204 sp_gradient_context_select_prev(ec);
205 } else {
206 sp_selection_item_prev(dt);
207 }
208 }
210 } // namespace Inkscape
213 /**
214 * Copies repr and its inherited css style elements, along with the accumulated transform 'full_t',
215 * then prepends the copy to 'clip'.
216 */
217 void sp_selection_copy_one(Inkscape::XML::Node *repr, Geom::Matrix full_t, GSList **clip, Inkscape::XML::Document* xml_doc)
218 {
219 Inkscape::XML::Node *copy = repr->duplicate(xml_doc);
221 // copy complete inherited style
222 SPCSSAttr *css = sp_repr_css_attr_inherited(repr, "style");
223 sp_repr_css_set(copy, css, "style");
224 sp_repr_css_attr_unref(css);
226 // write the complete accumulated transform passed to us
227 // (we're dealing with unattached repr, so we write to its attr
228 // instead of using sp_item_set_transform)
229 gchar *affinestr=sp_svg_transform_write(full_t);
230 copy->setAttribute("transform", affinestr);
231 g_free(affinestr);
233 *clip = g_slist_prepend(*clip, copy);
234 }
236 void sp_selection_copy_impl(GSList const *items, GSList **clip, Inkscape::XML::Document* xml_doc)
237 {
238 // Sort items:
239 GSList *sorted_items = g_slist_copy((GSList *) items);
240 sorted_items = g_slist_sort((GSList *) sorted_items, (GCompareFunc) sp_object_compare_position);
242 // Copy item reprs:
243 for (GSList *i = (GSList *) sorted_items; i != NULL; i = i->next) {
244 sp_selection_copy_one(SP_OBJECT_REPR(i->data), sp_item_i2doc_affine(SP_ITEM(i->data)), clip, xml_doc);
245 }
247 *clip = g_slist_reverse(*clip);
248 g_slist_free((GSList *) sorted_items);
249 }
251 GSList *sp_selection_paste_impl(SPDocument *doc, SPObject *parent, GSList **clip)
252 {
253 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
255 GSList *copied = NULL;
256 // add objects to document
257 for (GSList *l = *clip; l != NULL; l = l->next) {
258 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
259 Inkscape::XML::Node *copy = repr->duplicate(xml_doc);
261 // premultiply the item transform by the accumulated parent transform in the paste layer
262 Geom::Matrix local(sp_item_i2doc_affine(SP_ITEM(parent)));
263 if (!local.isIdentity()) {
264 gchar const *t_str = copy->attribute("transform");
265 Geom::Matrix item_t(Geom::identity());
266 if (t_str)
267 sp_svg_transform_read(t_str, &item_t);
268 item_t *= local.inverse();
269 // (we're dealing with unattached repr, so we write to its attr instead of using sp_item_set_transform)
270 gchar *affinestr=sp_svg_transform_write(item_t);
271 copy->setAttribute("transform", affinestr);
272 g_free(affinestr);
273 }
275 parent->appendChildRepr(copy);
276 copied = g_slist_prepend(copied, copy);
277 Inkscape::GC::release(copy);
278 }
279 return copied;
280 }
282 void sp_selection_delete_impl(GSList const *items, bool propagate = true, bool propagate_descendants = true)
283 {
284 for (GSList const *i = items ; i ; i = i->next ) {
285 sp_object_ref((SPObject *)i->data, NULL);
286 }
287 for (GSList const *i = items; i != NULL; i = i->next) {
288 SPItem *item = reinterpret_cast<SPItem *>(i->data);
289 item->deleteObject(propagate, propagate_descendants);
290 sp_object_unref(item, NULL);
291 }
292 }
295 void sp_selection_delete(SPDesktop *desktop)
296 {
297 if (desktop == NULL) {
298 return;
299 }
301 if (tools_isactive(desktop, TOOLS_TEXT))
302 if (sp_text_delete_selection(desktop->event_context)) {
303 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_TEXT,
304 _("Delete text"));
305 return;
306 }
308 Inkscape::Selection *selection = sp_desktop_selection(desktop);
310 // check if something is selected
311 if (selection->isEmpty()) {
312 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Nothing</b> was deleted."));
313 return;
314 }
316 GSList const *selected = g_slist_copy(const_cast<GSList *>(selection->itemList()));
317 selection->clear();
318 sp_selection_delete_impl(selected);
319 g_slist_free((GSList *) selected);
321 /* a tool may have set up private information in it's selection context
322 * that depends on desktop items. I think the only sane way to deal with
323 * this currently is to reset the current tool, which will reset it's
324 * associated selection context. For example: deleting an object
325 * while moving it around the canvas.
326 */
327 tools_switch( desktop, tools_active( desktop ) );
329 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_DELETE,
330 _("Delete"));
331 }
333 void add_ids_recursive(std::vector<const gchar *> &ids, SPObject *obj)
334 {
335 if (!obj)
336 return;
338 ids.push_back(obj->getId());
340 if (SP_IS_GROUP(obj)) {
341 for (SPObject *child = sp_object_first_child(obj) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
342 add_ids_recursive(ids, child);
343 }
344 }
345 }
347 void sp_selection_duplicate(SPDesktop *desktop, bool suppressDone)
348 {
349 if (desktop == NULL)
350 return;
352 SPDocument *doc = desktop->doc();
353 Inkscape::XML::Document* xml_doc = sp_document_repr_doc(doc);
354 Inkscape::Selection *selection = sp_desktop_selection(desktop);
356 // check if something is selected
357 if (selection->isEmpty()) {
358 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to duplicate."));
359 return;
360 }
362 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
364 selection->clear();
366 // sorting items from different parents sorts each parent's subset without possibly mixing
367 // them, just what we need
368 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
370 GSList *newsel = NULL;
372 std::vector<const gchar *> old_ids;
373 std::vector<const gchar *> new_ids;
374 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
375 bool relink_clones = prefs->getBool("/options/relinkclonesonduplicate/value");
377 while (reprs) {
378 Inkscape::XML::Node *old_repr = (Inkscape::XML::Node *) reprs->data;
379 Inkscape::XML::Node *parent = old_repr->parent();
380 Inkscape::XML::Node *copy = old_repr->duplicate(xml_doc);
382 parent->appendChild(copy);
384 if (relink_clones) {
385 SPObject *old_obj = doc->getObjectByRepr(old_repr);
386 SPObject *new_obj = doc->getObjectByRepr(copy);
387 add_ids_recursive(old_ids, old_obj);
388 add_ids_recursive(new_ids, new_obj);
389 }
391 newsel = g_slist_prepend(newsel, copy);
392 reprs = g_slist_remove(reprs, reprs->data);
393 Inkscape::GC::release(copy);
394 }
396 if (relink_clones) {
398 g_assert(old_ids.size() == new_ids.size());
400 for (unsigned int i = 0; i < old_ids.size(); i++) {
401 const gchar *id = old_ids[i];
402 SPObject *old_clone = doc->getObjectById(id);
403 if (SP_IS_USE(old_clone)) {
404 SPItem *orig = sp_use_get_original(SP_USE(old_clone));
405 if (!orig) // orphaned
406 continue;
407 for (unsigned int j = 0; j < old_ids.size(); j++) {
408 if (!strcmp(orig->getId(), old_ids[j])) {
409 // we have both orig and clone in selection, relink
410 // std::cout << id << " old, its ori: " << SP_OBJECT_ID(orig) << "; will relink:" << new_ids[i] << " to " << new_ids[j] << "\n";
411 gchar *newref = g_strdup_printf("#%s", new_ids[j]);
412 SPObject *new_clone = doc->getObjectById(new_ids[i]);
413 SP_OBJECT_REPR(new_clone)->setAttribute("xlink:href", newref);
414 new_clone->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
415 g_free(newref);
416 }
417 }
418 }
419 }
420 }
423 if ( !suppressDone ) {
424 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_DUPLICATE,
425 _("Duplicate"));
426 }
428 selection->setReprList(newsel);
430 g_slist_free(newsel);
431 }
433 void sp_edit_clear_all(SPDesktop *dt)
434 {
435 if (!dt)
436 return;
438 SPDocument *doc = sp_desktop_document(dt);
439 sp_desktop_selection(dt)->clear();
441 g_return_if_fail(SP_IS_GROUP(dt->currentLayer()));
442 GSList *items = sp_item_group_item_list(SP_GROUP(dt->currentLayer()));
444 while (items) {
445 reinterpret_cast<SPObject*>(items->data)->deleteObject();
446 items = g_slist_remove(items, items->data);
447 }
449 sp_document_done(doc, SP_VERB_EDIT_CLEAR_ALL,
450 _("Delete all"));
451 }
453 GSList *
454 get_all_items(GSList *list, SPObject *from, SPDesktop *desktop, bool onlyvisible, bool onlysensitive, GSList const *exclude)
455 {
456 for (SPObject *child = sp_object_first_child(from) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
457 if (SP_IS_ITEM(child) &&
458 !desktop->isLayer(SP_ITEM(child)) &&
459 (!onlysensitive || !SP_ITEM(child)->isLocked()) &&
460 (!onlyvisible || !desktop->itemIsHidden(SP_ITEM(child))) &&
461 (!exclude || !g_slist_find((GSList *) exclude, child))
462 )
463 {
464 list = g_slist_prepend(list, SP_ITEM(child));
465 }
467 if (SP_IS_ITEM(child) && desktop->isLayer(SP_ITEM(child))) {
468 list = get_all_items(list, child, desktop, onlyvisible, onlysensitive, exclude);
469 }
470 }
472 return list;
473 }
475 void sp_edit_select_all_full(SPDesktop *dt, bool force_all_layers, bool invert)
476 {
477 if (!dt)
478 return;
480 Inkscape::Selection *selection = sp_desktop_selection(dt);
482 g_return_if_fail(SP_IS_GROUP(dt->currentLayer()));
484 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
485 PrefsSelectionContext inlayer = (PrefsSelectionContext) prefs->getInt("/options/kbselection/inlayer", PREFS_SELECTION_LAYER);
486 bool onlyvisible = prefs->getBool("/options/kbselection/onlyvisible", true);
487 bool onlysensitive = prefs->getBool("/options/kbselection/onlysensitive", true);
489 GSList *items = NULL;
491 GSList const *exclude = NULL;
492 if (invert) {
493 exclude = selection->itemList();
494 }
496 if (force_all_layers)
497 inlayer = PREFS_SELECTION_ALL;
499 switch (inlayer) {
500 case PREFS_SELECTION_LAYER: {
501 if ( (onlysensitive && SP_ITEM(dt->currentLayer())->isLocked()) ||
502 (onlyvisible && dt->itemIsHidden(SP_ITEM(dt->currentLayer()))) )
503 return;
505 GSList *all_items = sp_item_group_item_list(SP_GROUP(dt->currentLayer()));
507 for (GSList *i = all_items; i; i = i->next) {
508 SPItem *item = SP_ITEM(i->data);
510 if (item && (!onlysensitive || !item->isLocked())) {
511 if (!onlyvisible || !dt->itemIsHidden(item)) {
512 if (!dt->isLayer(item)) {
513 if (!invert || !g_slist_find((GSList *) exclude, item)) {
514 items = g_slist_prepend(items, item); // leave it in the list
515 }
516 }
517 }
518 }
519 }
521 g_slist_free(all_items);
522 break;
523 }
524 case PREFS_SELECTION_LAYER_RECURSIVE: {
525 items = get_all_items(NULL, dt->currentLayer(), dt, onlyvisible, onlysensitive, exclude);
526 break;
527 }
528 default: {
529 items = get_all_items(NULL, dt->currentRoot(), dt, onlyvisible, onlysensitive, exclude);
530 break;
531 }
532 }
534 selection->setList(items);
536 if (items) {
537 g_slist_free(items);
538 }
539 }
541 void sp_edit_select_all(SPDesktop *desktop)
542 {
543 sp_edit_select_all_full(desktop, false, false);
544 }
546 void sp_edit_select_all_in_all_layers(SPDesktop *desktop)
547 {
548 sp_edit_select_all_full(desktop, true, false);
549 }
551 void sp_edit_invert(SPDesktop *desktop)
552 {
553 sp_edit_select_all_full(desktop, false, true);
554 }
556 void sp_edit_invert_in_all_layers(SPDesktop *desktop)
557 {
558 sp_edit_select_all_full(desktop, true, true);
559 }
561 void sp_selection_group_impl(GSList *p, Inkscape::XML::Node *group, Inkscape::XML::Document *xml_doc, SPDocument *doc) {
563 p = g_slist_sort(p, (GCompareFunc) sp_repr_compare_position);
565 // Remember the position and parent of the topmost object.
566 gint topmost = ((Inkscape::XML::Node *) g_slist_last(p)->data)->position();
567 Inkscape::XML::Node *topmost_parent = ((Inkscape::XML::Node *) g_slist_last(p)->data)->parent();
569 while (p) {
570 Inkscape::XML::Node *current = (Inkscape::XML::Node *) p->data;
572 if (current->parent() == topmost_parent) {
573 Inkscape::XML::Node *spnew = current->duplicate(xml_doc);
574 sp_repr_unparent(current);
575 group->appendChild(spnew);
576 Inkscape::GC::release(spnew);
577 topmost --; // only reduce count for those items deleted from topmost_parent
578 } else { // move it to topmost_parent first
579 GSList *temp_clip = NULL;
581 // At this point, current may already have no item, due to its being a clone whose original is already moved away
582 // So we copy it artificially calculating the transform from its repr->attr("transform") and the parent transform
583 gchar const *t_str = current->attribute("transform");
584 Geom::Matrix item_t(Geom::identity());
585 if (t_str)
586 sp_svg_transform_read(t_str, &item_t);
587 item_t *= sp_item_i2doc_affine(SP_ITEM(doc->getObjectByRepr(current->parent())));
588 // FIXME: when moving both clone and original from a transformed group (either by
589 // grouping into another parent, or by cut/paste) the transform from the original's
590 // parent becomes embedded into original itself, and this affects its clones. Fix
591 // this by remembering the transform diffs we write to each item into an array and
592 // then, if this is clone, looking up its original in that array and pre-multiplying
593 // it by the inverse of that original's transform diff.
595 sp_selection_copy_one(current, item_t, &temp_clip, xml_doc);
596 sp_repr_unparent(current);
598 // paste into topmost_parent (temporarily)
599 GSList *copied = sp_selection_paste_impl(doc, doc->getObjectByRepr(topmost_parent), &temp_clip);
600 if (temp_clip) g_slist_free(temp_clip);
601 if (copied) { // if success,
602 // take pasted object (now in topmost_parent)
603 Inkscape::XML::Node *in_topmost = (Inkscape::XML::Node *) copied->data;
604 // make a copy
605 Inkscape::XML::Node *spnew = in_topmost->duplicate(xml_doc);
606 // remove pasted
607 sp_repr_unparent(in_topmost);
608 // put its copy into group
609 group->appendChild(spnew);
610 Inkscape::GC::release(spnew);
611 g_slist_free(copied);
612 }
613 }
614 p = g_slist_remove(p, current);
615 }
617 // Add the new group to the topmost members' parent
618 topmost_parent->appendChild(group);
620 // Move to the position of the topmost, reduced by the number of items deleted from topmost_parent
621 group->setPosition(topmost + 1);
622 }
624 void sp_selection_group(SPDesktop *desktop)
625 {
626 if (desktop == NULL)
627 return;
629 SPDocument *doc = sp_desktop_document(desktop);
630 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
632 Inkscape::Selection *selection = sp_desktop_selection(desktop);
634 // Check if something is selected.
635 if (selection->isEmpty()) {
636 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>some objects</b> to group."));
637 return;
638 }
640 GSList const *l = (GSList *) selection->reprList();
642 GSList *p = g_slist_copy((GSList *) l);
644 selection->clear();
646 Inkscape::XML::Node *group = xml_doc->createElement("svg:g");
648 sp_selection_group_impl(p, group, xml_doc, doc);
650 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_GROUP,
651 _("Group"));
653 selection->set(group);
654 Inkscape::GC::release(group);
655 }
657 void sp_selection_ungroup(SPDesktop *desktop)
658 {
659 if (desktop == NULL)
660 return;
662 Inkscape::Selection *selection = sp_desktop_selection(desktop);
664 if (selection->isEmpty()) {
665 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>group</b> to ungroup."));
666 return;
667 }
669 GSList *items = g_slist_copy((GSList *) selection->itemList());
670 selection->clear();
672 // Get a copy of current selection.
673 GSList *new_select = NULL;
674 bool ungrouped = false;
675 for (GSList *i = items;
676 i != NULL;
677 i = i->next)
678 {
679 SPItem *group = (SPItem *) i->data;
681 // when ungrouping cloned groups with their originals, some objects that were selected may no more exist due to unlinking
682 if (!SP_IS_OBJECT(group)) {
683 continue;
684 }
686 /* We do not allow ungrouping <svg> etc. (lauris) */
687 if (strcmp(SP_OBJECT_REPR(group)->name(), "svg:g") && strcmp(SP_OBJECT_REPR(group)->name(), "svg:switch")) {
688 // keep the non-group item in the new selection
689 new_select = g_slist_append(new_select, group);
690 continue;
691 }
693 GSList *children = NULL;
694 /* This is not strictly required, but is nicer to rely on group ::destroy (lauris) */
695 sp_item_group_ungroup(SP_GROUP(group), &children, false);
696 ungrouped = true;
697 // Add ungrouped items to the new selection.
698 new_select = g_slist_concat(new_select, children);
699 }
701 if (new_select) { // Set new selection.
702 selection->addList(new_select);
703 g_slist_free(new_select);
704 }
705 if (!ungrouped) {
706 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No groups</b> to ungroup in the selection."));
707 }
709 g_slist_free(items);
711 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_UNGROUP,
712 _("Ungroup"));
713 }
715 /** Replace all groups in the list with their member objects, recursively; returns a new list, frees old */
716 GSList *
717 sp_degroup_list(GSList *items)
718 {
719 GSList *out = NULL;
720 bool has_groups = false;
721 for (GSList *item = items; item; item = item->next) {
722 if (!SP_IS_GROUP(item->data)) {
723 out = g_slist_prepend(out, item->data);
724 } else {
725 has_groups = true;
726 GSList *members = sp_item_group_item_list(SP_GROUP(item->data));
727 for (GSList *member = members; member; member = member->next) {
728 out = g_slist_prepend(out, member->data);
729 }
730 g_slist_free(members);
731 }
732 }
733 out = g_slist_reverse(out);
734 g_slist_free(items);
736 if (has_groups) { // recurse if we unwrapped a group - it may have contained others
737 out = sp_degroup_list(out);
738 }
740 return out;
741 }
744 /** If items in the list have a common parent, return it, otherwise return NULL */
745 static SPGroup *
746 sp_item_list_common_parent_group(GSList const *items)
747 {
748 if (!items) {
749 return NULL;
750 }
751 SPObject *parent = SP_OBJECT_PARENT(items->data);
752 /* Strictly speaking this CAN happen, if user selects <svg> from Inkscape::XML editor */
753 if (!SP_IS_GROUP(parent)) {
754 return NULL;
755 }
756 for (items = items->next; items; items = items->next) {
757 if (SP_OBJECT_PARENT(items->data) != parent) {
758 return NULL;
759 }
760 }
762 return SP_GROUP(parent);
763 }
765 /** Finds out the minimum common bbox of the selected items. */
766 static Geom::OptRect
767 enclose_items(GSList const *items)
768 {
769 g_assert(items != NULL);
771 Geom::OptRect r;
772 for (GSList const *i = items; i; i = i->next) {
773 r = Geom::unify(r, sp_item_bbox_desktop((SPItem *) i->data));
774 }
775 return r;
776 }
778 SPObject *
779 prev_sibling(SPObject *child)
780 {
781 SPObject *parent = SP_OBJECT_PARENT(child);
782 if (!SP_IS_GROUP(parent)) {
783 return NULL;
784 }
785 for ( SPObject *i = sp_object_first_child(parent) ; i; i = SP_OBJECT_NEXT(i) ) {
786 if (i->next == child)
787 return i;
788 }
789 return NULL;
790 }
792 void
793 sp_selection_raise(SPDesktop *desktop)
794 {
795 if (!desktop)
796 return;
798 Inkscape::Selection *selection = sp_desktop_selection(desktop);
800 GSList const *items = (GSList *) selection->itemList();
801 if (!items) {
802 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise."));
803 return;
804 }
806 SPGroup const *group = sp_item_list_common_parent_group(items);
807 if (!group) {
808 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
809 return;
810 }
812 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
814 /* Construct reverse-ordered list of selected children. */
815 GSList *rev = g_slist_copy((GSList *) items);
816 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
818 // Determine the common bbox of the selected items.
819 Geom::OptRect selected = enclose_items(items);
821 // Iterate over all objects in the selection (starting from top).
822 if (selected) {
823 while (rev) {
824 SPObject *child = reinterpret_cast<SPObject*>(rev->data);
825 // for each selected object, find the next sibling
826 for (SPObject *newref = child->next; newref; newref = newref->next) {
827 // if the sibling is an item AND overlaps our selection,
828 if (SP_IS_ITEM(newref)) {
829 Geom::OptRect newref_bbox = sp_item_bbox_desktop(SP_ITEM(newref));
830 if ( newref_bbox && selected->intersects(*newref_bbox) ) {
831 // AND if it's not one of our selected objects,
832 if (!g_slist_find((GSList *) items, newref)) {
833 // move the selected object after that sibling
834 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(newref));
835 }
836 break;
837 }
838 }
839 }
840 rev = g_slist_remove(rev, child);
841 }
842 } else {
843 g_slist_free(rev);
844 }
846 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_RAISE,
847 //TRANSLATORS: "Raise" means "to raise an object" in the undo history
848 C_("Undo action", "Raise"));
849 }
851 void sp_selection_raise_to_top(SPDesktop *desktop)
852 {
853 if (desktop == NULL)
854 return;
856 SPDocument *document = sp_desktop_document(desktop);
857 Inkscape::Selection *selection = sp_desktop_selection(desktop);
859 if (selection->isEmpty()) {
860 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise to top."));
861 return;
862 }
864 GSList const *items = (GSList *) selection->itemList();
866 SPGroup const *group = sp_item_list_common_parent_group(items);
867 if (!group) {
868 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
869 return;
870 }
872 GSList *rl = g_slist_copy((GSList *) selection->reprList());
873 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
875 for (GSList *l = rl; l != NULL; l = l->next) {
876 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
877 repr->setPosition(-1);
878 }
880 g_slist_free(rl);
882 sp_document_done(document, SP_VERB_SELECTION_TO_FRONT,
883 _("Raise to top"));
884 }
886 void
887 sp_selection_lower(SPDesktop *desktop)
888 {
889 if (desktop == NULL)
890 return;
892 Inkscape::Selection *selection = sp_desktop_selection(desktop);
894 GSList const *items = (GSList *) selection->itemList();
895 if (!items) {
896 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower."));
897 return;
898 }
900 SPGroup const *group = sp_item_list_common_parent_group(items);
901 if (!group) {
902 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
903 return;
904 }
906 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
908 // Determine the common bbox of the selected items.
909 Geom::OptRect selected = enclose_items(items);
911 /* Construct direct-ordered list of selected children. */
912 GSList *rev = g_slist_copy((GSList *) items);
913 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
914 rev = g_slist_reverse(rev);
916 // Iterate over all objects in the selection (starting from top).
917 if (selected) {
918 while (rev) {
919 SPObject *child = reinterpret_cast<SPObject*>(rev->data);
920 // for each selected object, find the prev sibling
921 for (SPObject *newref = prev_sibling(child); newref; newref = prev_sibling(newref)) {
922 // if the sibling is an item AND overlaps our selection,
923 if (SP_IS_ITEM(newref)) {
924 Geom::OptRect ref_bbox = sp_item_bbox_desktop(SP_ITEM(newref));
925 if ( ref_bbox && selected->intersects(*ref_bbox) ) {
926 // AND if it's not one of our selected objects,
927 if (!g_slist_find((GSList *) items, newref)) {
928 // move the selected object before that sibling
929 SPObject *put_after = prev_sibling(newref);
930 if (put_after)
931 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(put_after));
932 else
933 SP_OBJECT_REPR(child)->setPosition(0);
934 }
935 break;
936 }
937 }
938 }
939 rev = g_slist_remove(rev, child);
940 }
941 } else {
942 g_slist_free(rev);
943 }
945 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_LOWER,
946 _("Lower"));
947 }
949 void sp_selection_lower_to_bottom(SPDesktop *desktop)
950 {
951 if (desktop == NULL)
952 return;
954 SPDocument *document = sp_desktop_document(desktop);
955 Inkscape::Selection *selection = sp_desktop_selection(desktop);
957 if (selection->isEmpty()) {
958 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower to bottom."));
959 return;
960 }
962 GSList const *items = (GSList *) selection->itemList();
964 SPGroup const *group = sp_item_list_common_parent_group(items);
965 if (!group) {
966 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
967 return;
968 }
970 GSList *rl;
971 rl = g_slist_copy((GSList *) selection->reprList());
972 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
973 rl = g_slist_reverse(rl);
975 for (GSList *l = rl; l != NULL; l = l->next) {
976 gint minpos;
977 SPObject *pp, *pc;
978 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
979 pp = document->getObjectByRepr(sp_repr_parent(repr));
980 minpos = 0;
981 g_assert(SP_IS_GROUP(pp));
982 pc = sp_object_first_child(pp);
983 while (!SP_IS_ITEM(pc)) {
984 minpos += 1;
985 pc = pc->next;
986 }
987 repr->setPosition(minpos);
988 }
990 g_slist_free(rl);
992 sp_document_done(document, SP_VERB_SELECTION_TO_BACK,
993 _("Lower to bottom"));
994 }
996 void
997 sp_undo(SPDesktop *desktop, SPDocument *)
998 {
999 if (!sp_document_undo(sp_desktop_document(desktop)))
1000 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to undo."));
1001 }
1003 void
1004 sp_redo(SPDesktop *desktop, SPDocument *)
1005 {
1006 if (!sp_document_redo(sp_desktop_document(desktop)))
1007 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to redo."));
1008 }
1010 void sp_selection_cut(SPDesktop *desktop)
1011 {
1012 sp_selection_copy(desktop);
1013 sp_selection_delete(desktop);
1014 }
1016 /**
1017 * \pre item != NULL
1018 */
1019 SPCSSAttr *
1020 take_style_from_item(SPItem *item)
1021 {
1022 // write the complete cascaded style, context-free
1023 SPCSSAttr *css = sp_css_attr_from_object(item, SP_STYLE_FLAG_ALWAYS);
1024 if (css == NULL)
1025 return NULL;
1027 if ((SP_IS_GROUP(item) && item->children) ||
1028 (SP_IS_TEXT(item) && item->children && item->children->next == NULL)) {
1029 // if this is a text with exactly one tspan child, merge the style of that tspan as well
1030 // If this is a group, merge the style of its topmost (last) child with style
1031 for (SPObject *last_element = item->lastChild(); last_element != NULL; last_element = SP_OBJECT_PREV(last_element)) {
1032 if ( last_element->style ) {
1033 SPCSSAttr *temp = sp_css_attr_from_object(last_element, SP_STYLE_FLAG_IFSET);
1034 if (temp) {
1035 sp_repr_css_merge(css, temp);
1036 sp_repr_css_attr_unref(temp);
1037 }
1038 break;
1039 }
1040 }
1041 }
1042 if (!(SP_IS_TEXT(item) || SP_IS_TSPAN(item) || SP_IS_TREF(item) || SP_IS_STRING(item))) {
1043 // do not copy text properties from non-text objects, it's confusing
1044 css = sp_css_attr_unset_text(css);
1045 }
1047 // FIXME: also transform gradient/pattern fills, by forking? NO, this must be nondestructive
1048 double ex = to_2geom(sp_item_i2doc_affine(item)).descrim();
1049 if (ex != 1.0) {
1050 css = sp_css_attr_scale(css, ex);
1051 }
1053 return css;
1054 }
1057 void sp_selection_copy(SPDesktop *desktop)
1058 {
1059 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
1060 cm->copy(desktop);
1061 }
1063 void sp_selection_paste(SPDesktop *desktop, bool in_place)
1064 {
1065 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
1066 if (cm->paste(desktop, in_place)) {
1067 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_PASTE, _("Paste"));
1068 }
1069 }
1071 void sp_selection_paste_style(SPDesktop *desktop)
1072 {
1073 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
1074 if (cm->pasteStyle(desktop)) {
1075 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_PASTE_STYLE, _("Paste style"));
1076 }
1077 }
1080 void sp_selection_paste_livepatheffect(SPDesktop *desktop)
1081 {
1082 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
1083 if (cm->pastePathEffect(desktop)) {
1084 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_PASTE_LIVEPATHEFFECT,
1085 _("Paste live path effect"));
1086 }
1087 }
1090 void sp_selection_remove_livepatheffect_impl(SPItem *item)
1091 {
1092 if ( item && SP_IS_LPE_ITEM(item) &&
1093 sp_lpe_item_has_path_effect(SP_LPE_ITEM(item))) {
1094 sp_lpe_item_remove_all_path_effects(SP_LPE_ITEM(item), false);
1095 }
1096 }
1098 void sp_selection_remove_livepatheffect(SPDesktop *desktop)
1099 {
1100 if (desktop == NULL) return;
1102 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1104 // check if something is selected
1105 if (selection->isEmpty()) {
1106 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to remove live path effects from."));
1107 return;
1108 }
1110 for ( GSList const *itemlist = selection->itemList(); itemlist != NULL; itemlist = g_slist_next(itemlist) ) {
1111 SPItem *item = reinterpret_cast<SPItem*>(itemlist->data);
1113 sp_selection_remove_livepatheffect_impl(item);
1115 }
1117 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_REMOVE_LIVEPATHEFFECT,
1118 _("Remove live path effect"));
1119 }
1121 void sp_selection_remove_filter(SPDesktop *desktop)
1122 {
1123 if (desktop == NULL) return;
1125 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1127 // check if something is selected
1128 if (selection->isEmpty()) {
1129 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to remove filters from."));
1130 return;
1131 }
1133 SPCSSAttr *css = sp_repr_css_attr_new();
1134 sp_repr_css_unset_property(css, "filter");
1135 sp_desktop_set_style(desktop, css);
1136 sp_repr_css_attr_unref(css);
1138 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_REMOVE_FILTER,
1139 _("Remove filter"));
1140 }
1143 void sp_selection_paste_size(SPDesktop *desktop, bool apply_x, bool apply_y)
1144 {
1145 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
1146 if (cm->pasteSize(desktop, false, apply_x, apply_y)) {
1147 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_PASTE_SIZE,
1148 _("Paste size"));
1149 }
1150 }
1152 void sp_selection_paste_size_separately(SPDesktop *desktop, bool apply_x, bool apply_y)
1153 {
1154 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
1155 if (cm->pasteSize(desktop, true, apply_x, apply_y)) {
1156 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_PASTE_SIZE_SEPARATELY,
1157 _("Paste size separately"));
1158 }
1159 }
1161 void sp_selection_to_next_layer(SPDesktop *dt, bool suppressDone)
1162 {
1163 Inkscape::Selection *selection = sp_desktop_selection(dt);
1165 // check if something is selected
1166 if (selection->isEmpty()) {
1167 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer above."));
1168 return;
1169 }
1171 GSList const *items = g_slist_copy((GSList *) selection->itemList());
1173 bool no_more = false; // Set to true, if no more layers above
1174 SPObject *next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer());
1175 if (next) {
1176 GSList *temp_clip = NULL;
1177 sp_selection_copy_impl(items, &temp_clip, sp_document_repr_doc(dt->doc()));
1178 sp_selection_delete_impl(items, false, false);
1179 next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
1180 GSList *copied;
1181 if (next) {
1182 copied = sp_selection_paste_impl(sp_desktop_document(dt), next, &temp_clip);
1183 } else {
1184 copied = sp_selection_paste_impl(sp_desktop_document(dt), dt->currentLayer(), &temp_clip);
1185 no_more = true;
1186 }
1187 selection->setReprList((GSList const *) copied);
1188 g_slist_free(copied);
1189 if (temp_clip) g_slist_free(temp_clip);
1190 if (next) dt->setCurrentLayer(next);
1191 if ( !suppressDone ) {
1192 sp_document_done(sp_desktop_document(dt), SP_VERB_LAYER_MOVE_TO_NEXT,
1193 _("Raise to next layer"));
1194 }
1195 } else {
1196 no_more = true;
1197 }
1199 if (no_more) {
1200 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers above."));
1201 }
1203 g_slist_free((GSList *) items);
1204 }
1206 void sp_selection_to_prev_layer(SPDesktop *dt, bool suppressDone)
1207 {
1208 Inkscape::Selection *selection = sp_desktop_selection(dt);
1210 // check if something is selected
1211 if (selection->isEmpty()) {
1212 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer below."));
1213 return;
1214 }
1216 GSList const *items = g_slist_copy((GSList *) selection->itemList());
1218 bool no_more = false; // Set to true, if no more layers below
1219 SPObject *next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer());
1220 if (next) {
1221 GSList *temp_clip = NULL;
1222 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
1223 sp_selection_delete_impl(items, false, false);
1224 next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
1225 GSList *copied;
1226 if (next) {
1227 copied = sp_selection_paste_impl(sp_desktop_document(dt), next, &temp_clip);
1228 } else {
1229 copied = sp_selection_paste_impl(sp_desktop_document(dt), dt->currentLayer(), &temp_clip);
1230 no_more = true;
1231 }
1232 selection->setReprList((GSList const *) copied);
1233 g_slist_free(copied);
1234 if (temp_clip) g_slist_free(temp_clip);
1235 if (next) dt->setCurrentLayer(next);
1236 if ( !suppressDone ) {
1237 sp_document_done(sp_desktop_document(dt), SP_VERB_LAYER_MOVE_TO_PREV,
1238 _("Lower to previous layer"));
1239 }
1240 } else {
1241 no_more = true;
1242 }
1244 if (no_more) {
1245 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers below."));
1246 }
1248 g_slist_free((GSList *) items);
1249 }
1251 bool
1252 selection_contains_original(SPItem *item, Inkscape::Selection *selection)
1253 {
1254 bool contains_original = false;
1256 bool is_use = SP_IS_USE(item);
1257 SPItem *item_use = item;
1258 SPItem *item_use_first = item;
1259 while (is_use && item_use && !contains_original)
1260 {
1261 item_use = sp_use_get_original(SP_USE(item_use));
1262 contains_original |= selection->includes(item_use);
1263 if (item_use == item_use_first)
1264 break;
1265 is_use = SP_IS_USE(item_use);
1266 }
1268 // If it's a tref, check whether the object containing the character
1269 // data is part of the selection
1270 if (!contains_original && SP_IS_TREF(item)) {
1271 contains_original = selection->includes(SP_TREF(item)->getObjectReferredTo());
1272 }
1274 return contains_original;
1275 }
1278 bool
1279 selection_contains_both_clone_and_original(Inkscape::Selection *selection)
1280 {
1281 bool clone_with_original = false;
1282 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1283 SPItem *item = SP_ITEM(l->data);
1284 clone_with_original |= selection_contains_original(item, selection);
1285 if (clone_with_original)
1286 break;
1287 }
1288 return clone_with_original;
1289 }
1291 /** Apply matrix to the selection. \a set_i2d is normally true, which means objects are in the
1292 original transform, synced with their reprs, and need to jump to the new transform in one go. A
1293 value of set_i2d==false is only used by seltrans when it's dragging objects live (not outlines); in
1294 that case, items are already in the new position, but the repr is in the old, and this function
1295 then simply updates the repr from item->transform.
1296 */
1297 void sp_selection_apply_affine(Inkscape::Selection *selection, Geom::Matrix const &affine, bool set_i2d, bool compensate)
1298 {
1299 if (selection->isEmpty())
1300 return;
1302 // For each perspective with a box in selection, check whether all boxes are selected and
1303 // unlink all non-selected boxes.
1304 Persp3D *persp;
1305 Persp3D *transf_persp;
1306 std::list<Persp3D *> plist = selection->perspList();
1307 for (std::list<Persp3D *>::iterator i = plist.begin(); i != plist.end(); ++i) {
1308 persp = (Persp3D *) (*i);
1310 if (!persp3d_has_all_boxes_in_selection (persp, selection)) {
1311 std::list<SPBox3D *> selboxes = selection->box3DList(persp);
1313 // create a new perspective as a copy of the current one and link the selected boxes to it
1314 transf_persp = persp3d_create_xml_element (SP_OBJECT_DOCUMENT(persp), persp->perspective_impl);
1316 for (std::list<SPBox3D *>::iterator b = selboxes.begin(); b != selboxes.end(); ++b)
1317 box3d_switch_perspectives(*b, persp, transf_persp);
1318 } else {
1319 transf_persp = persp;
1320 }
1322 persp3d_apply_affine_transformation(transf_persp, affine);
1323 }
1325 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1326 SPItem *item = SP_ITEM(l->data);
1328 Geom::Point old_center(0,0);
1329 if (set_i2d && item->isCenterSet())
1330 old_center = item->getCenter();
1332 #if 0 /* Re-enable this once persistent guides have a graphical indication.
1333 At the time of writing, this is the only place to re-enable. */
1334 sp_item_update_cns(*item, selection->desktop());
1335 #endif
1337 // we're moving both a clone and its original or any ancestor in clone chain?
1338 bool transform_clone_with_original = selection_contains_original(item, selection);
1339 // ...both a text-on-path and its path?
1340 bool transform_textpath_with_path = (SP_IS_TEXT_TEXTPATH(item) && selection->includes( sp_textpath_get_path_item(SP_TEXTPATH(sp_object_first_child(item))) ));
1341 // ...both a flowtext and its frame?
1342 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)
1343 // ...both an offset and its source?
1344 bool transform_offset_with_source = (SP_IS_OFFSET(item) && SP_OFFSET(item)->sourceHref) && selection->includes( sp_offset_get_source(SP_OFFSET(item)) );
1346 // If we're moving a connector, we want to detach it
1347 // from shapes that aren't part of the selection, but
1348 // leave it attached if they are
1349 if (cc_item_is_connector(item)) {
1350 SPItem *attItem[2];
1351 SP_PATH(item)->connEndPair.getAttachedItems(attItem);
1353 for (int n = 0; n < 2; ++n) {
1354 if (!selection->includes(attItem[n])) {
1355 sp_conn_end_detach(item, n);
1356 }
1357 }
1358 }
1360 // "clones are unmoved when original is moved" preference
1361 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1362 int compensation = prefs->getInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
1363 bool prefs_unmoved = (compensation == SP_CLONE_COMPENSATION_UNMOVED);
1364 bool prefs_parallel = (compensation == SP_CLONE_COMPENSATION_PARALLEL);
1366 /* If this is a clone and it's selected along with its original, do not move it;
1367 * it will feel the transform of its original and respond to it itself.
1368 * Without this, a clone is doubly transformed, very unintuitive.
1369 *
1370 * Same for textpath if we are also doing ANY transform to its path: do not touch textpath,
1371 * letters cannot be squeezed or rotated anyway, they only refill the changed path.
1372 * Same for linked offset if we are also moving its source: do not move it. */
1373 if (transform_textpath_with_path || transform_offset_with_source) {
1374 // Restore item->transform field from the repr, in case it was changed by seltrans.
1375 sp_object_read_attr(item, "transform");
1376 } else if (transform_flowtext_with_frame) {
1377 // apply the inverse of the region's transform to the <use> so that the flow remains
1378 // the same (even though the output itself gets transformed)
1379 for (SPObject *region = item->firstChild() ; region ; region = SP_OBJECT_NEXT(region)) {
1380 if (!SP_IS_FLOWREGION(region) && !SP_IS_FLOWREGIONEXCLUDE(region))
1381 continue;
1382 for (SPObject *use = region->firstChild() ; use ; use = SP_OBJECT_NEXT(use)) {
1383 if (!SP_IS_USE(use)) continue;
1384 sp_item_write_transform(SP_USE(use), SP_OBJECT_REPR(use), item->transform.inverse(), NULL, compensate);
1385 }
1386 }
1387 } else if (transform_clone_with_original) {
1388 // We are transforming a clone along with its original. The below matrix juggling is
1389 // necessary to ensure that they transform as a whole, i.e. the clone's induced
1390 // transform and its move compensation are both cancelled out.
1392 // restore item->transform field from the repr, in case it was changed by seltrans
1393 sp_object_read_attr(item, "transform");
1395 // calculate the matrix we need to apply to the clone to cancel its induced transform from its original
1396 Geom::Matrix parent2dt = sp_item_i2d_affine(SP_ITEM(SP_OBJECT_PARENT(item)));
1397 Geom::Matrix t = parent2dt * affine * parent2dt.inverse();
1398 Geom::Matrix t_inv = t.inverse();
1399 Geom::Matrix result = t_inv * item->transform * t;
1401 if ((prefs_parallel || prefs_unmoved) && affine.isTranslation()) {
1402 // we need to cancel out the move compensation, too
1404 // find out the clone move, same as in sp_use_move_compensate
1405 Geom::Matrix parent = sp_use_get_parent_transform(SP_USE(item));
1406 Geom::Matrix clone_move = parent.inverse() * t * parent;
1408 if (prefs_parallel) {
1409 Geom::Matrix move = result * clone_move * t_inv;
1410 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &move, compensate);
1412 } else if (prefs_unmoved) {
1413 //if (SP_IS_USE(sp_use_get_original(SP_USE(item))))
1414 // clone_move = Geom::identity();
1415 Geom::Matrix move = result * clone_move;
1416 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &t, compensate);
1417 }
1419 } else {
1420 // just apply the result
1421 sp_item_write_transform(item, SP_OBJECT_REPR(item), result, &t, compensate);
1422 }
1424 } else {
1425 if (set_i2d) {
1426 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * (Geom::Matrix)affine);
1427 }
1428 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, NULL, compensate);
1429 }
1431 // if we're moving the actual object, not just updating the repr, we can transform the
1432 // center by the same matrix (only necessary for non-translations)
1433 if (set_i2d && item->isCenterSet() && !(affine.isTranslation() || affine.isIdentity())) {
1434 item->setCenter(old_center * affine);
1435 item->updateRepr();
1436 }
1437 }
1438 }
1440 void sp_selection_remove_transform(SPDesktop *desktop)
1441 {
1442 if (desktop == NULL)
1443 return;
1445 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1447 GSList const *l = (GSList *) selection->reprList();
1448 while (l != NULL) {
1449 ((Inkscape::XML::Node*)l->data)->setAttribute("transform", NULL, false);
1450 l = l->next;
1451 }
1453 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_FLATTEN,
1454 _("Remove transform"));
1455 }
1457 void
1458 sp_selection_scale_absolute(Inkscape::Selection *selection,
1459 double const x0, double const x1,
1460 double const y0, double const y1)
1461 {
1462 if (selection->isEmpty())
1463 return;
1465 Geom::OptRect const bbox(selection->bounds());
1466 if ( !bbox ) {
1467 return;
1468 }
1470 Geom::Translate const p2o(-bbox->min());
1472 Geom::Scale const newSize(x1 - x0,
1473 y1 - y0);
1474 Geom::Scale const scale( newSize * Geom::Scale(bbox->dimensions()).inverse() );
1475 Geom::Translate const o2n(x0, y0);
1476 Geom::Matrix const final( p2o * scale * o2n );
1478 sp_selection_apply_affine(selection, final);
1479 }
1482 void sp_selection_scale_relative(Inkscape::Selection *selection, Geom::Point const &align, Geom::Scale const &scale)
1483 {
1484 if (selection->isEmpty())
1485 return;
1487 Geom::OptRect const bbox(selection->bounds());
1489 if ( !bbox ) {
1490 return;
1491 }
1493 // FIXME: ARBITRARY LIMIT: don't try to scale above 1 Mpx, it won't display properly and will crash sooner or later anyway
1494 if ( bbox->dimensions()[Geom::X] * scale[Geom::X] > 1e6 ||
1495 bbox->dimensions()[Geom::Y] * scale[Geom::Y] > 1e6 )
1496 {
1497 return;
1498 }
1500 Geom::Translate const n2d(-align);
1501 Geom::Translate const d2n(align);
1502 Geom::Matrix const final( n2d * scale * d2n );
1503 sp_selection_apply_affine(selection, final);
1504 }
1506 void
1507 sp_selection_rotate_relative(Inkscape::Selection *selection, Geom::Point const ¢er, gdouble const angle_degrees)
1508 {
1509 Geom::Translate const d2n(center);
1510 Geom::Translate const n2d(-center);
1511 Geom::Rotate const rotate(Geom::Rotate::from_degrees(angle_degrees));
1512 Geom::Matrix const final( Geom::Matrix(n2d) * rotate * d2n );
1513 sp_selection_apply_affine(selection, final);
1514 }
1516 void
1517 sp_selection_skew_relative(Inkscape::Selection *selection, Geom::Point const &align, double dx, double dy)
1518 {
1519 Geom::Translate const d2n(align);
1520 Geom::Translate const n2d(-align);
1521 Geom::Matrix const skew(1, dy,
1522 dx, 1,
1523 0, 0);
1524 Geom::Matrix const final( n2d * skew * d2n );
1525 sp_selection_apply_affine(selection, final);
1526 }
1528 void sp_selection_move_relative(Inkscape::Selection *selection, Geom::Point const &move, bool compensate)
1529 {
1530 sp_selection_apply_affine(selection, Geom::Matrix(Geom::Translate(move)), true, compensate);
1531 }
1533 void sp_selection_move_relative(Inkscape::Selection *selection, double dx, double dy)
1534 {
1535 sp_selection_apply_affine(selection, Geom::Matrix(Geom::Translate(dx, dy)));
1536 }
1538 /**
1539 * @brief Rotates selected objects 90 degrees, either clock-wise or counter-clockwise, depending on the value of ccw
1540 */
1541 void sp_selection_rotate_90(SPDesktop *desktop, bool ccw)
1542 {
1543 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1545 if (selection->isEmpty())
1546 return;
1548 GSList const *l = selection->itemList();
1549 Geom::Rotate const rot_90(Geom::Point(0, ccw ? 1 : -1)); // pos. or neg. rotation, depending on the value of ccw
1550 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1551 SPItem *item = SP_ITEM(l2->data);
1552 sp_item_rotate_rel(item, rot_90);
1553 }
1555 sp_document_done(sp_desktop_document(desktop),
1556 ccw ? SP_VERB_OBJECT_ROTATE_90_CCW : SP_VERB_OBJECT_ROTATE_90_CW,
1557 ccw ? _("Rotate 90° CCW") : _("Rotate 90° CW"));
1558 }
1560 void
1561 sp_selection_rotate(Inkscape::Selection *selection, gdouble const angle_degrees)
1562 {
1563 if (selection->isEmpty())
1564 return;
1566 boost::optional<Geom::Point> center = selection->center();
1567 if (!center) {
1568 return;
1569 }
1571 sp_selection_rotate_relative(selection, *center, angle_degrees);
1573 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1574 ( ( angle_degrees > 0 )
1575 ? "selector:rotate:ccw"
1576 : "selector:rotate:cw" ),
1577 SP_VERB_CONTEXT_SELECT,
1578 _("Rotate"));
1579 }
1581 // helper function:
1582 static
1583 Geom::Point
1584 cornerFarthestFrom(Geom::Rect const &r, Geom::Point const &p){
1585 Geom::Point m = r.midpoint();
1586 unsigned i = 0;
1587 if (p[X] < m[X]) {
1588 i = 1;
1589 }
1590 if (p[Y] < m[Y]) {
1591 i = 3 - i;
1592 }
1593 return r.corner(i);
1594 }
1596 /**
1597 \param angle the angle in "angular pixels", i.e. how many visible pixels must move the outermost point of the rotated object
1598 */
1599 void
1600 sp_selection_rotate_screen(Inkscape::Selection *selection, gdouble angle)
1601 {
1602 if (selection->isEmpty())
1603 return;
1605 Geom::OptRect const bbox(selection->bounds());
1606 boost::optional<Geom::Point> center = selection->center();
1608 if ( !bbox || !center ) {
1609 return;
1610 }
1612 gdouble const zoom = selection->desktop()->current_zoom();
1613 gdouble const zmove = angle / zoom;
1614 gdouble const r = Geom::L2(cornerFarthestFrom(*bbox, *center) - *center);
1616 gdouble const zangle = 180 * atan2(zmove, r) / M_PI;
1618 sp_selection_rotate_relative(selection, *center, zangle);
1620 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1621 ( (angle > 0)
1622 ? "selector:rotate:ccw"
1623 : "selector:rotate:cw" ),
1624 SP_VERB_CONTEXT_SELECT,
1625 _("Rotate by pixels"));
1626 }
1628 void
1629 sp_selection_scale(Inkscape::Selection *selection, gdouble grow)
1630 {
1631 if (selection->isEmpty())
1632 return;
1634 Geom::OptRect const bbox(selection->bounds());
1635 if (!bbox) {
1636 return;
1637 }
1639 Geom::Point const center(bbox->midpoint());
1641 // you can't scale "do nizhe pola" (below zero)
1642 double const max_len = bbox->maxExtent();
1643 if ( max_len + grow <= 1e-3 ) {
1644 return;
1645 }
1647 double const times = 1.0 + grow / max_len;
1648 sp_selection_scale_relative(selection, center, Geom::Scale(times, times));
1650 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1651 ( (grow > 0)
1652 ? "selector:scale:larger"
1653 : "selector:scale:smaller" ),
1654 SP_VERB_CONTEXT_SELECT,
1655 _("Scale"));
1656 }
1658 void
1659 sp_selection_scale_screen(Inkscape::Selection *selection, gdouble grow_pixels)
1660 {
1661 sp_selection_scale(selection,
1662 grow_pixels / selection->desktop()->current_zoom());
1663 }
1665 void
1666 sp_selection_scale_times(Inkscape::Selection *selection, gdouble times)
1667 {
1668 if (selection->isEmpty())
1669 return;
1671 Geom::OptRect sel_bbox = selection->bounds();
1673 if (!sel_bbox) {
1674 return;
1675 }
1677 Geom::Point const center(sel_bbox->midpoint());
1678 sp_selection_scale_relative(selection, center, Geom::Scale(times, times));
1679 sp_document_done(sp_desktop_document(selection->desktop()), SP_VERB_CONTEXT_SELECT,
1680 _("Scale by whole factor"));
1681 }
1683 void
1684 sp_selection_move(SPDesktop *desktop, gdouble dx, gdouble dy)
1685 {
1686 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1687 if (selection->isEmpty()) {
1688 return;
1689 }
1691 sp_selection_move_relative(selection, dx, dy);
1693 if (dx == 0) {
1694 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical", SP_VERB_CONTEXT_SELECT,
1695 _("Move vertically"));
1696 } else if (dy == 0) {
1697 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal", SP_VERB_CONTEXT_SELECT,
1698 _("Move horizontally"));
1699 } else {
1700 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SELECT,
1701 _("Move"));
1702 }
1703 }
1705 void
1706 sp_selection_move_screen(SPDesktop *desktop, gdouble dx, gdouble dy)
1707 {
1708 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1709 if (selection->isEmpty()) {
1710 return;
1711 }
1713 // same as sp_selection_move but divide deltas by zoom factor
1714 gdouble const zoom = desktop->current_zoom();
1715 gdouble const zdx = dx / zoom;
1716 gdouble const zdy = dy / zoom;
1717 sp_selection_move_relative(selection, zdx, zdy);
1719 if (dx == 0) {
1720 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical", SP_VERB_CONTEXT_SELECT,
1721 _("Move vertically by pixels"));
1722 } else if (dy == 0) {
1723 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal", SP_VERB_CONTEXT_SELECT,
1724 _("Move horizontally by pixels"));
1725 } else {
1726 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SELECT,
1727 _("Move"));
1728 }
1729 }
1731 namespace {
1733 template <typename D>
1734 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
1735 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
1737 template <typename D>
1738 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items, SPObject *root,
1739 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
1741 struct Forward {
1742 typedef SPObject *Iterator;
1744 static Iterator children(SPObject *o) { return sp_object_first_child(o); }
1745 static Iterator siblings_after(SPObject *o) { return SP_OBJECT_NEXT(o); }
1746 static void dispose(Iterator /*i*/) {}
1748 static SPObject *object(Iterator i) { return i; }
1749 static Iterator next(Iterator i) { return SP_OBJECT_NEXT(i); }
1750 };
1752 struct Reverse {
1753 typedef GSList *Iterator;
1755 static Iterator children(SPObject *o) {
1756 return make_list(o->firstChild(), NULL);
1757 }
1758 static Iterator siblings_after(SPObject *o) {
1759 return make_list(SP_OBJECT_PARENT(o)->firstChild(), o);
1760 }
1761 static void dispose(Iterator i) {
1762 g_slist_free(i);
1763 }
1765 static SPObject *object(Iterator i) {
1766 return reinterpret_cast<SPObject *>(i->data);
1767 }
1768 static Iterator next(Iterator i) { return i->next; }
1770 private:
1771 static GSList *make_list(SPObject *object, SPObject *limit) {
1772 GSList *list=NULL;
1773 while ( object != limit ) {
1774 list = g_slist_prepend(list, object);
1775 object = SP_OBJECT_NEXT(object);
1776 }
1777 return list;
1778 }
1779 };
1781 }
1783 void
1784 sp_selection_item_next(SPDesktop *desktop)
1785 {
1786 g_return_if_fail(desktop != NULL);
1787 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1789 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1790 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs->getInt("/options/kbselection/inlayer", PREFS_SELECTION_LAYER);
1791 bool onlyvisible = prefs->getBool("/options/kbselection/onlyvisible", true);
1792 bool onlysensitive = prefs->getBool("/options/kbselection/onlysensitive", true);
1794 SPObject *root;
1795 if (PREFS_SELECTION_ALL != inlayer) {
1796 root = selection->activeContext();
1797 } else {
1798 root = desktop->currentRoot();
1799 }
1801 SPItem *item=next_item_from_list<Forward>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1803 if (item) {
1804 selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
1805 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1806 scroll_to_show_item(desktop, item);
1807 }
1808 }
1809 }
1811 void
1812 sp_selection_item_prev(SPDesktop *desktop)
1813 {
1814 SPDocument *document = sp_desktop_document(desktop);
1815 g_return_if_fail(document != NULL);
1816 g_return_if_fail(desktop != NULL);
1817 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1819 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1820 PrefsSelectionContext inlayer = (PrefsSelectionContext) prefs->getInt("/options/kbselection/inlayer", PREFS_SELECTION_LAYER);
1821 bool onlyvisible = prefs->getBool("/options/kbselection/onlyvisible", true);
1822 bool onlysensitive = prefs->getBool("/options/kbselection/onlysensitive", true);
1824 SPObject *root;
1825 if (PREFS_SELECTION_ALL != inlayer) {
1826 root = selection->activeContext();
1827 } else {
1828 root = desktop->currentRoot();
1829 }
1831 SPItem *item=next_item_from_list<Reverse>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1833 if (item) {
1834 selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
1835 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1836 scroll_to_show_item(desktop, item);
1837 }
1838 }
1839 }
1841 void sp_selection_next_patheffect_param(SPDesktop * dt)
1842 {
1843 if (!dt) return;
1845 Inkscape::Selection *selection = sp_desktop_selection(dt);
1846 if ( selection && !selection->isEmpty() ) {
1847 SPItem *item = selection->singleItem();
1848 if ( item && SP_IS_SHAPE(item)) {
1849 if (sp_lpe_item_has_path_effect(SP_LPE_ITEM(item))) {
1850 sp_lpe_item_edit_next_param_oncanvas(SP_LPE_ITEM(item), dt);
1851 } else {
1852 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("The selection has no applied path effect."));
1853 }
1854 }
1855 }
1856 }
1858 /*bool has_path_recursive(SPObject *obj)
1859 {
1860 if (!obj) return false;
1861 if (SP_IS_PATH(obj)) {
1862 return true;
1863 }
1864 if (SP_IS_GROUP(obj) || SP_IS_OBJECTGROUP(obj)) {
1865 for (SPObject *c = obj->children; c; c = c->next) {
1866 if (has_path_recursive(c)) return true;
1867 }
1868 }
1869 return false;
1870 }*/
1872 void sp_selection_edit_clip_or_mask(SPDesktop * /*dt*/, bool /*clip*/)
1873 {
1874 return;
1875 /*if (!dt) return;
1876 using namespace Inkscape::UI;
1878 Inkscape::Selection *selection = sp_desktop_selection(dt);
1879 if (!selection || selection->isEmpty()) return;
1881 GSList const *items = selection->itemList();
1882 bool has_path = false;
1883 for (GSList *i = const_cast<GSList*>(items); i; i= i->next) {
1884 SPItem *item = SP_ITEM(i->data);
1885 SPObject *search = clip
1886 ? SP_OBJECT(item->clip_ref ? item->clip_ref->getObject() : NULL)
1887 : SP_OBJECT(item->mask_ref ? item->mask_ref->getObject() : NULL);
1888 has_path |= has_path_recursive(search);
1889 if (has_path) break;
1890 }
1891 if (has_path) {
1892 if (!tools_isactive(dt, TOOLS_NODES)) {
1893 tools_switch(dt, TOOLS_NODES);
1894 }
1895 ink_node_tool_set_mode(INK_NODE_TOOL(dt->event_context),
1896 clip ? NODE_TOOL_EDIT_CLIPPING_PATHS : NODE_TOOL_EDIT_MASKS);
1897 } else if (clip) {
1898 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1899 _("The selection has no applied clip path."));
1900 } else {
1901 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1902 _("The selection has no applied mask."));
1903 }*/
1904 }
1907 namespace {
1909 template <typename D>
1910 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items,
1911 SPObject *root, bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
1912 {
1913 SPObject *current=root;
1914 while (items) {
1915 SPItem *item=SP_ITEM(items->data);
1916 if ( root->isAncestorOf(item) &&
1917 ( !only_in_viewport || desktop->isWithinViewport(item) ) )
1918 {
1919 current = item;
1920 break;
1921 }
1922 items = items->next;
1923 }
1925 GSList *path=NULL;
1926 while ( current != root ) {
1927 path = g_slist_prepend(path, current);
1928 current = SP_OBJECT_PARENT(current);
1929 }
1931 SPItem *next;
1932 // first, try from the current object
1933 next = next_item<D>(desktop, path, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1934 g_slist_free(path);
1936 if (!next) { // if we ran out of objects, start over at the root
1937 next = next_item<D>(desktop, NULL, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1938 }
1940 return next;
1941 }
1943 template <typename D>
1944 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
1945 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
1946 {
1947 typename D::Iterator children;
1948 typename D::Iterator iter;
1950 SPItem *found=NULL;
1952 if (path) {
1953 SPObject *object=reinterpret_cast<SPObject *>(path->data);
1954 g_assert(SP_OBJECT_PARENT(object) == root);
1955 if (desktop->isLayer(object)) {
1956 found = next_item<D>(desktop, path->next, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1957 }
1958 iter = children = D::siblings_after(object);
1959 } else {
1960 iter = children = D::children(root);
1961 }
1963 while ( iter && !found ) {
1964 SPObject *object=D::object(iter);
1965 if (desktop->isLayer(object)) {
1966 if (PREFS_SELECTION_LAYER != inlayer) { // recurse into sublayers
1967 found = next_item<D>(desktop, NULL, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1968 }
1969 } else if ( SP_IS_ITEM(object) &&
1970 ( !only_in_viewport || desktop->isWithinViewport(SP_ITEM(object)) ) &&
1971 ( !onlyvisible || !desktop->itemIsHidden(SP_ITEM(object))) &&
1972 ( !onlysensitive || !SP_ITEM(object)->isLocked()) &&
1973 !desktop->isLayer(SP_ITEM(object)) )
1974 {
1975 found = SP_ITEM(object);
1976 }
1977 iter = D::next(iter);
1978 }
1980 D::dispose(children);
1982 return found;
1983 }
1985 }
1987 /**
1988 * If \a item is not entirely visible then adjust visible area to centre on the centre on of
1989 * \a item.
1990 */
1991 void scroll_to_show_item(SPDesktop *desktop, SPItem *item)
1992 {
1993 Geom::Rect dbox = desktop->get_display_area();
1994 Geom::OptRect sbox = sp_item_bbox_desktop(item);
1996 if ( sbox && dbox.contains(*sbox) == false ) {
1997 Geom::Point const s_dt = sbox->midpoint();
1998 Geom::Point const s_w = desktop->d2w(s_dt);
1999 Geom::Point const d_dt = dbox.midpoint();
2000 Geom::Point const d_w = desktop->d2w(d_dt);
2001 Geom::Point const moved_w( d_w - s_w );
2002 gint const dx = (gint) moved_w[X];
2003 gint const dy = (gint) moved_w[Y];
2004 desktop->scroll_world(dx, dy);
2005 }
2006 }
2009 void
2010 sp_selection_clone(SPDesktop *desktop)
2011 {
2012 if (desktop == NULL)
2013 return;
2015 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2017 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
2019 // check if something is selected
2020 if (selection->isEmpty()) {
2021 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object</b> to clone."));
2022 return;
2023 }
2025 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
2027 selection->clear();
2029 // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
2030 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
2032 GSList *newsel = NULL;
2034 while (reprs) {
2035 Inkscape::XML::Node *sel_repr = (Inkscape::XML::Node *) reprs->data;
2036 Inkscape::XML::Node *parent = sp_repr_parent(sel_repr);
2038 Inkscape::XML::Node *clone = xml_doc->createElement("svg:use");
2039 clone->setAttribute("x", "0", false);
2040 clone->setAttribute("y", "0", false);
2041 clone->setAttribute("xlink:href", g_strdup_printf("#%s", sel_repr->attribute("id")), false);
2043 clone->setAttribute("inkscape:transform-center-x", sel_repr->attribute("inkscape:transform-center-x"), false);
2044 clone->setAttribute("inkscape:transform-center-y", sel_repr->attribute("inkscape:transform-center-y"), false);
2046 // add the new clone to the top of the original's parent
2047 parent->appendChild(clone);
2049 newsel = g_slist_prepend(newsel, clone);
2050 reprs = g_slist_remove(reprs, sel_repr);
2051 Inkscape::GC::release(clone);
2052 }
2054 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_CLONE,
2055 C_("Action","Clone"));
2057 selection->setReprList(newsel);
2059 g_slist_free(newsel);
2060 }
2062 void
2063 sp_selection_relink(SPDesktop *desktop)
2064 {
2065 if (!desktop)
2066 return;
2068 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2070 if (selection->isEmpty()) {
2071 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>clones</b> to relink."));
2072 return;
2073 }
2075 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
2076 const gchar *newid = cm->getFirstObjectID();
2077 if (!newid) {
2078 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Copy an <b>object</b> to clipboard to relink clones to."));
2079 return;
2080 }
2081 gchar *newref = g_strdup_printf("#%s", newid);
2083 // Get a copy of current selection.
2084 bool relinked = false;
2085 for (GSList *items = (GSList *) selection->itemList();
2086 items != NULL;
2087 items = items->next)
2088 {
2089 SPItem *item = (SPItem *) items->data;
2091 if (!SP_IS_USE(item))
2092 continue;
2094 SP_OBJECT_REPR(item)->setAttribute("xlink:href", newref);
2095 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
2096 relinked = true;
2097 }
2099 g_free(newref);
2101 if (!relinked) {
2102 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No clones to relink</b> in the selection."));
2103 } else {
2104 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNLINK_CLONE,
2105 _("Relink clone"));
2106 }
2107 }
2110 void
2111 sp_selection_unlink(SPDesktop *desktop)
2112 {
2113 if (!desktop)
2114 return;
2116 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2118 if (selection->isEmpty()) {
2119 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>clones</b> to unlink."));
2120 return;
2121 }
2123 // Get a copy of current selection.
2124 GSList *new_select = NULL;
2125 bool unlinked = false;
2126 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2127 items != NULL;
2128 items = items->next)
2129 {
2130 SPItem *item = (SPItem *) items->data;
2132 if (SP_IS_TEXT(item)) {
2133 SPObject *tspan = sp_tref_convert_to_tspan(item);
2135 if (tspan) {
2136 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
2137 }
2139 // Set unlink to true, and fall into the next if which
2140 // will include this text item in the new selection
2141 unlinked = true;
2142 }
2144 if (!(SP_IS_USE(item) || SP_IS_TREF(item))) {
2145 // keep the non-use item in the new selection
2146 new_select = g_slist_prepend(new_select, item);
2147 continue;
2148 }
2150 SPItem *unlink;
2151 if (SP_IS_USE(item)) {
2152 unlink = sp_use_unlink(SP_USE(item));
2153 } else /*if (SP_IS_TREF(use))*/ {
2154 unlink = SP_ITEM(sp_tref_convert_to_tspan(item));
2155 }
2157 unlinked = true;
2158 // Add ungrouped items to the new selection.
2159 new_select = g_slist_prepend(new_select, unlink);
2160 }
2162 if (new_select) { // set new selection
2163 selection->clear();
2164 selection->setList(new_select);
2165 g_slist_free(new_select);
2166 }
2167 if (!unlinked) {
2168 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No clones to unlink</b> in the selection."));
2169 }
2171 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNLINK_CLONE,
2172 _("Unlink clone"));
2173 }
2175 void
2176 sp_select_clone_original(SPDesktop *desktop)
2177 {
2178 if (desktop == NULL)
2179 return;
2181 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2183 SPItem *item = selection->singleItem();
2185 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.");
2187 // Check if other than two objects are selected
2188 if (g_slist_length((GSList *) selection->itemList()) != 1 || !item) {
2189 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
2190 return;
2191 }
2193 SPItem *original = NULL;
2194 if (SP_IS_USE(item)) {
2195 original = sp_use_get_original(SP_USE(item));
2196 } else if (SP_IS_OFFSET(item) && SP_OFFSET(item)->sourceHref) {
2197 original = sp_offset_get_source(SP_OFFSET(item));
2198 } else if (SP_IS_TEXT_TEXTPATH(item)) {
2199 original = sp_textpath_get_path_item(SP_TEXTPATH(sp_object_first_child(item)));
2200 } else if (SP_IS_FLOWTEXT(item)) {
2201 original = SP_FLOWTEXT(item)->get_frame(NULL); // first frame only
2202 } else { // it's an object that we don't know what to do with
2203 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
2204 return;
2205 }
2207 if (!original) {
2208 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>Cannot find</b> the object to select (orphaned clone, offset, textpath, flowed text?)"));
2209 return;
2210 }
2212 for (SPObject *o = original; o && !SP_IS_ROOT(o); o = SP_OBJECT_PARENT(o)) {
2213 if (SP_IS_DEFS(o)) {
2214 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("The object you're trying to select is <b>not visible</b> (it is in <defs>)"));
2215 return;
2216 }
2217 }
2219 if (original) {
2220 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
2221 bool highlight = prefs->getBool("/options/highlightoriginal/value");
2222 if (highlight) {
2223 Geom::OptRect a = item->getBounds(sp_item_i2d_affine(item));
2224 Geom::OptRect b = original->getBounds(sp_item_i2d_affine(original));
2225 if ( a && b ) {
2226 // draw a flashing line between the objects
2227 SPCurve *curve = new SPCurve();
2228 curve->moveto(a->midpoint());
2229 curve->lineto(b->midpoint());
2231 SPCanvasItem * canvasitem = sp_canvas_bpath_new(sp_desktop_tempgroup(desktop), curve);
2232 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(canvasitem), 0x0000ddff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT, 5, 3);
2233 sp_canvas_item_show(canvasitem);
2234 curve->unref();
2235 desktop->add_temporary_canvasitem(canvasitem, 1000);
2236 }
2237 }
2239 selection->clear();
2240 selection->set(original);
2241 if (SP_CYCLING == SP_CYCLE_FOCUS) {
2242 scroll_to_show_item(desktop, original);
2243 }
2244 }
2245 }
2248 void sp_selection_to_marker(SPDesktop *desktop, bool apply)
2249 {
2250 if (desktop == NULL)
2251 return;
2253 SPDocument *doc = sp_desktop_document(desktop);
2254 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2256 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2258 // check if something is selected
2259 if (selection->isEmpty()) {
2260 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to marker."));
2261 return;
2262 }
2264 sp_document_ensure_up_to_date(doc);
2265 Geom::OptRect r = selection->bounds(SPItem::RENDERING_BBOX);
2266 boost::optional<Geom::Point> c = selection->center();
2267 if ( !r || !c ) {
2268 return;
2269 }
2271 // calculate the transform to be applied to objects to move them to 0,0
2272 Geom::Point move_p = Geom::Point(0, sp_document_height(doc)) - *c;
2273 move_p[Geom::Y] = -move_p[Geom::Y];
2274 Geom::Matrix move = Geom::Matrix(Geom::Translate(move_p));
2276 GSList *items = g_slist_copy((GSList *) selection->itemList());
2278 items = g_slist_sort(items, (GCompareFunc) sp_object_compare_position);
2280 // bottommost object, after sorting
2281 SPObject *parent = SP_OBJECT_PARENT(items->data);
2283 Geom::Matrix parent_transform(sp_item_i2doc_affine(SP_ITEM(parent)));
2285 // remember the position of the first item
2286 gint pos = SP_OBJECT_REPR(items->data)->position();
2287 (void)pos; // TODO check why this was remembered
2289 // create a list of duplicates
2290 GSList *repr_copies = NULL;
2291 for (GSList *i = items; i != NULL; i = i->next) {
2292 Inkscape::XML::Node *dup = (SP_OBJECT_REPR(i->data))->duplicate(xml_doc);
2293 repr_copies = g_slist_prepend(repr_copies, dup);
2294 }
2296 Geom::Rect bounds(desktop->dt2doc(r->min()), desktop->dt2doc(r->max()));
2298 if (apply) {
2299 // delete objects so that their clones don't get alerted; this object will be restored shortly
2300 for (GSList *i = items; i != NULL; i = i->next) {
2301 SPObject *item = reinterpret_cast<SPObject*>(i->data);
2302 item->deleteObject(false);
2303 }
2304 }
2306 // Hack: Temporarily set clone compensation to unmoved, so that we can move clone-originals
2307 // without disturbing clones.
2308 // See ActorAlign::on_button_click() in src/ui/dialog/align-and-distribute.cpp
2309 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
2310 int saved_compensation = prefs->getInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
2311 prefs->setInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
2313 gchar const *mark_id = generate_marker(repr_copies, bounds, doc,
2314 ( Geom::Matrix(Geom::Translate(desktop->dt2doc(
2315 Geom::Point(r->min()[Geom::X],
2316 r->max()[Geom::Y]))))
2317 * parent_transform.inverse() ),
2318 parent_transform * move);
2319 (void)mark_id;
2321 // restore compensation setting
2322 prefs->setInt("/options/clonecompensation/value", saved_compensation);
2325 g_slist_free(items);
2327 sp_document_done(doc, SP_VERB_EDIT_SELECTION_2_MARKER,
2328 _("Objects to marker"));
2329 }
2331 static void sp_selection_to_guides_recursive(SPItem *item, bool deleteitem, bool wholegroups) {
2332 if (SP_IS_GROUP(item) && !SP_IS_BOX3D(item) && !wholegroups) {
2333 for (GSList *i = sp_item_group_item_list(SP_GROUP(item)); i != NULL; i = i->next) {
2334 sp_selection_to_guides_recursive(SP_ITEM(i->data), deleteitem, wholegroups);
2335 }
2336 } else {
2337 sp_item_convert_item_to_guides(item);
2339 if (deleteitem) {
2340 item->deleteObject(true);
2341 }
2342 }
2343 }
2345 void sp_selection_to_guides(SPDesktop *desktop)
2346 {
2347 if (desktop == NULL)
2348 return;
2350 SPDocument *doc = sp_desktop_document(desktop);
2351 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2352 // we need to copy the list because it gets reset when objects are deleted
2353 GSList *items = g_slist_copy((GSList *) selection->itemList());
2355 if (!items) {
2356 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to guides."));
2357 return;
2358 }
2360 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
2361 bool deleteitem = !prefs->getBool("/tools/cvg_keep_objects", 0);
2362 bool wholegroups = prefs->getBool("/tools/cvg_convert_whole_groups", 0);
2364 for (GSList const *i = items; i != NULL; i = i->next) {
2365 sp_selection_to_guides_recursive(SP_ITEM(i->data), deleteitem, wholegroups);
2366 }
2368 sp_document_done(doc, SP_VERB_EDIT_SELECTION_2_GUIDES, _("Objects to guides"));
2369 }
2371 void
2372 sp_selection_tile(SPDesktop *desktop, bool apply)
2373 {
2374 if (desktop == NULL)
2375 return;
2377 SPDocument *doc = sp_desktop_document(desktop);
2378 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2380 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2382 // check if something is selected
2383 if (selection->isEmpty()) {
2384 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to pattern."));
2385 return;
2386 }
2388 sp_document_ensure_up_to_date(doc);
2389 Geom::OptRect r = selection->bounds(SPItem::RENDERING_BBOX);
2390 if ( !r ) {
2391 return;
2392 }
2394 // calculate the transform to be applied to objects to move them to 0,0
2395 Geom::Point move_p = Geom::Point(0, sp_document_height(doc)) - (r->min() + Geom::Point(0, r->dimensions()[Geom::Y]));
2396 move_p[Geom::Y] = -move_p[Geom::Y];
2397 Geom::Matrix move = Geom::Matrix(Geom::Translate(move_p));
2399 GSList *items = g_slist_copy((GSList *) selection->itemList());
2401 items = g_slist_sort(items, (GCompareFunc) sp_object_compare_position);
2403 // bottommost object, after sorting
2404 SPObject *parent = SP_OBJECT_PARENT(items->data);
2406 Geom::Matrix parent_transform(sp_item_i2doc_affine(SP_ITEM(parent)));
2408 // remember the position of the first item
2409 gint pos = SP_OBJECT_REPR(items->data)->position();
2411 // create a list of duplicates
2412 GSList *repr_copies = NULL;
2413 for (GSList *i = items; i != NULL; i = i->next) {
2414 Inkscape::XML::Node *dup = (SP_OBJECT_REPR(i->data))->duplicate(xml_doc);
2415 repr_copies = g_slist_prepend(repr_copies, dup);
2416 }
2417 // restore the z-order after prepends
2418 repr_copies = g_slist_reverse(repr_copies);
2420 Geom::Rect bounds(desktop->dt2doc(r->min()), desktop->dt2doc(r->max()));
2422 if (apply) {
2423 // delete objects so that their clones don't get alerted; this object will be restored shortly
2424 for (GSList *i = items; i != NULL; i = i->next) {
2425 SPObject *item = reinterpret_cast<SPObject*>(i->data);
2426 item->deleteObject(false);
2427 }
2428 }
2430 // Hack: Temporarily set clone compensation to unmoved, so that we can move clone-originals
2431 // without disturbing clones.
2432 // See ActorAlign::on_button_click() in src/ui/dialog/align-and-distribute.cpp
2433 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
2434 int saved_compensation = prefs->getInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
2435 prefs->setInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
2437 gchar const *pat_id = pattern_tile(repr_copies, bounds, doc,
2438 ( Geom::Matrix(Geom::Translate(desktop->dt2doc(Geom::Point(r->min()[Geom::X],
2439 r->max()[Geom::Y]))))
2440 * to_2geom(parent_transform.inverse()) ),
2441 parent_transform * move);
2443 // restore compensation setting
2444 prefs->setInt("/options/clonecompensation/value", saved_compensation);
2446 if (apply) {
2447 Inkscape::XML::Node *rect = xml_doc->createElement("svg:rect");
2448 rect->setAttribute("style", g_strdup_printf("stroke:none;fill:url(#%s)", pat_id));
2450 Geom::Point min = bounds.min() * to_2geom(parent_transform.inverse());
2451 Geom::Point max = bounds.max() * to_2geom(parent_transform.inverse());
2453 sp_repr_set_svg_double(rect, "width", max[Geom::X] - min[Geom::X]);
2454 sp_repr_set_svg_double(rect, "height", max[Geom::Y] - min[Geom::Y]);
2455 sp_repr_set_svg_double(rect, "x", min[Geom::X]);
2456 sp_repr_set_svg_double(rect, "y", min[Geom::Y]);
2458 // restore parent and position
2459 SP_OBJECT_REPR(parent)->appendChild(rect);
2460 rect->setPosition(pos > 0 ? pos : 0);
2461 SPItem *rectangle = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(rect);
2463 Inkscape::GC::release(rect);
2465 selection->clear();
2466 selection->set(rectangle);
2467 }
2469 g_slist_free(items);
2471 sp_document_done(doc, SP_VERB_EDIT_TILE,
2472 _("Objects to pattern"));
2473 }
2475 void
2476 sp_selection_untile(SPDesktop *desktop)
2477 {
2478 if (desktop == NULL)
2479 return;
2481 SPDocument *doc = sp_desktop_document(desktop);
2482 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2484 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2486 // check if something is selected
2487 if (selection->isEmpty()) {
2488 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object with pattern fill</b> to extract objects from."));
2489 return;
2490 }
2492 GSList *new_select = NULL;
2494 bool did = false;
2496 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2497 items != NULL;
2498 items = items->next) {
2500 SPItem *item = (SPItem *) items->data;
2502 SPStyle *style = item->style;
2504 if (!style || !style->fill.isPaintserver())
2505 continue;
2507 SPPaintServer *server = item->style->getFillPaintServer();
2509 if (!SP_IS_PATTERN(server))
2510 continue;
2512 did = true;
2514 SPPattern *pattern = pattern_getroot(SP_PATTERN(server));
2516 Geom::Matrix pat_transform = to_2geom(pattern_patternTransform(SP_PATTERN(server)));
2517 pat_transform *= item->transform;
2519 for (SPObject *child = sp_object_first_child(pattern) ; child != NULL; child = child->next ) {
2520 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate(xml_doc);
2521 SPItem *i = SP_ITEM(desktop->currentLayer()->appendChildRepr(copy));
2523 // FIXME: relink clones to the new canvas objects
2524 // use SPObject::setid when mental finishes it to steal ids of
2526 // this is needed to make sure the new item has curve (simply requestDisplayUpdate does not work)
2527 sp_document_ensure_up_to_date(doc);
2529 Geom::Matrix transform( i->transform * pat_transform );
2530 sp_item_write_transform(i, SP_OBJECT_REPR(i), transform);
2532 new_select = g_slist_prepend(new_select, i);
2533 }
2535 SPCSSAttr *css = sp_repr_css_attr_new();
2536 sp_repr_css_set_property(css, "fill", "none");
2537 sp_repr_css_change(SP_OBJECT_REPR(item), css, "style");
2538 }
2540 if (!did) {
2541 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No pattern fills</b> in the selection."));
2542 } else {
2543 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNTILE,
2544 _("Pattern to objects"));
2545 selection->setList(new_select);
2546 }
2547 }
2549 void
2550 sp_selection_get_export_hints(Inkscape::Selection *selection, char const **filename, float *xdpi, float *ydpi)
2551 {
2552 if (selection->isEmpty()) {
2553 return;
2554 }
2556 GSList const *reprlst = selection->reprList();
2557 bool filename_search = TRUE;
2558 bool xdpi_search = TRUE;
2559 bool ydpi_search = TRUE;
2561 for (; reprlst != NULL &&
2562 filename_search &&
2563 xdpi_search &&
2564 ydpi_search;
2565 reprlst = reprlst->next) {
2566 gchar const *dpi_string;
2567 Inkscape::XML::Node * repr = (Inkscape::XML::Node *)reprlst->data;
2569 if (filename_search) {
2570 *filename = repr->attribute("inkscape:export-filename");
2571 if (*filename != NULL)
2572 filename_search = FALSE;
2573 }
2575 if (xdpi_search) {
2576 dpi_string = NULL;
2577 dpi_string = repr->attribute("inkscape:export-xdpi");
2578 if (dpi_string != NULL) {
2579 *xdpi = atof(dpi_string);
2580 xdpi_search = FALSE;
2581 }
2582 }
2584 if (ydpi_search) {
2585 dpi_string = NULL;
2586 dpi_string = repr->attribute("inkscape:export-ydpi");
2587 if (dpi_string != NULL) {
2588 *ydpi = atof(dpi_string);
2589 ydpi_search = FALSE;
2590 }
2591 }
2592 }
2593 }
2595 void
2596 sp_document_get_export_hints(SPDocument *doc, char const **filename, float *xdpi, float *ydpi)
2597 {
2598 Inkscape::XML::Node * repr = sp_document_repr_root(doc);
2599 gchar const *dpi_string;
2601 *filename = repr->attribute("inkscape:export-filename");
2603 dpi_string = NULL;
2604 dpi_string = repr->attribute("inkscape:export-xdpi");
2605 if (dpi_string != NULL) {
2606 *xdpi = atof(dpi_string);
2607 }
2609 dpi_string = NULL;
2610 dpi_string = repr->attribute("inkscape:export-ydpi");
2611 if (dpi_string != NULL) {
2612 *ydpi = atof(dpi_string);
2613 }
2614 }
2616 void
2617 sp_selection_create_bitmap_copy(SPDesktop *desktop)
2618 {
2619 if (desktop == NULL)
2620 return;
2622 SPDocument *document = sp_desktop_document(desktop);
2623 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
2625 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2627 // check if something is selected
2628 if (selection->isEmpty()) {
2629 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to make a bitmap copy."));
2630 return;
2631 }
2633 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Rendering bitmap..."));
2634 // set "busy" cursor
2635 desktop->setWaitingCursor();
2637 // Get the bounding box of the selection
2638 NRRect bbox;
2639 sp_document_ensure_up_to_date(document);
2640 selection->bounds(&bbox);
2641 if (NR_RECT_DFLS_TEST_EMPTY(&bbox)) {
2642 desktop->clearWaitingCursor();
2643 return; // exceptional situation, so not bother with a translatable error message, just quit quietly
2644 }
2646 // List of the items to show; all others will be hidden
2647 GSList *items = g_slist_copy((GSList *) selection->itemList());
2649 // Sort items so that the topmost comes last
2650 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
2652 // Generate a random value from the current time (you may create bitmap from the same object(s)
2653 // multiple times, and this is done so that they don't clash)
2654 GTimeVal cu;
2655 g_get_current_time(&cu);
2656 guint current = (int) (cu.tv_sec * 1000000 + cu.tv_usec) % 1024;
2658 // Create the filename.
2659 gchar *const basename = g_strdup_printf("%s-%s-%u.png",
2660 document->name,
2661 SP_OBJECT_REPR(items->data)->attribute("id"),
2662 current);
2663 // Imagemagick is known not to handle spaces in filenames, so we replace anything but letters,
2664 // digits, and a few other chars, with "_"
2665 g_strcanon(basename, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.=+~$#@^&!?", '_');
2667 // Build the complete path by adding document base dir, if set, otherwise home dir
2668 gchar * directory = NULL;
2669 if (SP_DOCUMENT_URI(document)) {
2670 directory = g_dirname(SP_DOCUMENT_URI(document));
2671 }
2672 if (directory == NULL) {
2673 directory = homedir_path(NULL);
2674 }
2675 gchar *filepath = g_build_filename(directory, basename, NULL);
2677 //g_print("%s\n", filepath);
2679 // Remember parent and z-order of the topmost one
2680 gint pos = SP_OBJECT_REPR(g_slist_last(items)->data)->position();
2681 SPObject *parent_object = SP_OBJECT_PARENT(g_slist_last(items)->data);
2682 Inkscape::XML::Node *parent = SP_OBJECT_REPR(parent_object);
2684 // Calculate resolution
2685 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
2686 double res;
2687 int const prefs_res = prefs->getInt("/options/createbitmap/resolution", 0);
2688 int const prefs_min = prefs->getInt("/options/createbitmap/minsize", 0);
2689 if (0 < prefs_res) {
2690 // If it's given explicitly in prefs, take it
2691 res = prefs_res;
2692 } else if (0 < prefs_min) {
2693 // If minsize is given, look up minimum bitmap size (default 250 pixels) and calculate resolution from it
2694 res = PX_PER_IN * prefs_min / MIN((bbox.x1 - bbox.x0), (bbox.y1 - bbox.y0));
2695 } else {
2696 float hint_xdpi = 0, hint_ydpi = 0;
2697 char const *hint_filename;
2698 // take resolution hint from the selected objects
2699 sp_selection_get_export_hints(selection, &hint_filename, &hint_xdpi, &hint_ydpi);
2700 if (hint_xdpi != 0) {
2701 res = hint_xdpi;
2702 } else {
2703 // take resolution hint from the document
2704 sp_document_get_export_hints(document, &hint_filename, &hint_xdpi, &hint_ydpi);
2705 if (hint_xdpi != 0) {
2706 res = hint_xdpi;
2707 } else {
2708 // if all else fails, take the default 90 dpi
2709 res = PX_PER_IN;
2710 }
2711 }
2712 }
2714 // The width and height of the bitmap in pixels
2715 unsigned width = (unsigned) floor((bbox.x1 - bbox.x0) * res / PX_PER_IN);
2716 unsigned height =(unsigned) floor((bbox.y1 - bbox.y0) * res / PX_PER_IN);
2718 // Find out if we have to run an external filter
2719 gchar const *run = NULL;
2720 Glib::ustring filter = prefs->getString("/options/createbitmap/filter");
2721 if (!filter.empty()) {
2722 // filter command is given;
2723 // see if we have a parameter to pass to it
2724 Glib::ustring param1 = prefs->getString("/options/createbitmap/filter_param1");
2725 if (!param1.empty()) {
2726 if (param1[param1.length() - 1] == '%') {
2727 // if the param string ends with %, interpret it as a percentage of the image's max dimension
2728 gchar p1[256];
2729 g_ascii_dtostr(p1, 256, ceil(g_ascii_strtod(param1.data(), NULL) * MAX(width, height) / 100));
2730 // the first param is always the image filename, the second is param1
2731 run = g_strdup_printf("%s \"%s\" %s", filter.data(), filepath, p1);
2732 } else {
2733 // otherwise pass the param1 unchanged
2734 run = g_strdup_printf("%s \"%s\" %s", filter.data(), filepath, param1.data());
2735 }
2736 } else {
2737 // run without extra parameter
2738 run = g_strdup_printf("%s \"%s\"", filter.data(), filepath);
2739 }
2740 }
2742 // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
2743 Geom::Matrix eek(sp_item_i2d_affine(SP_ITEM(parent_object)));
2744 Geom::Matrix t;
2746 double shift_x = bbox.x0;
2747 double shift_y = bbox.y1;
2748 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
2749 shift_x = round(shift_x);
2750 shift_y = -round(-shift_y); // this gets correct rounding despite coordinate inversion, remove the negations when the inversion is gone
2751 }
2752 t = Geom::Scale(1, -1) * Geom::Translate(shift_x, shift_y) * eek.inverse();
2754 // Do the export
2755 sp_export_png_file(document, filepath,
2756 bbox.x0, bbox.y0, bbox.x1, bbox.y1,
2757 width, height, res, res,
2758 (guint32) 0xffffff00,
2759 NULL, NULL,
2760 true, /*bool force_overwrite,*/
2761 items);
2763 g_slist_free(items);
2765 // Run filter, if any
2766 if (run) {
2767 g_print("Running external filter: %s\n", run);
2768 int retval;
2769 retval = system(run);
2770 }
2772 // Import the image back
2773 GdkPixbuf *pb = gdk_pixbuf_new_from_file(filepath, NULL);
2774 if (pb) {
2775 // Create the repr for the image
2776 Inkscape::XML::Node * repr = xml_doc->createElement("svg:image");
2777 sp_embed_image(repr, pb, "image/png");
2778 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
2779 sp_repr_set_svg_double(repr, "width", width);
2780 sp_repr_set_svg_double(repr, "height", height);
2781 } else {
2782 sp_repr_set_svg_double(repr, "width", (bbox.x1 - bbox.x0));
2783 sp_repr_set_svg_double(repr, "height", (bbox.y1 - bbox.y0));
2784 }
2786 // Write transform
2787 gchar *c=sp_svg_transform_write(t);
2788 repr->setAttribute("transform", c);
2789 g_free(c);
2791 // add the new repr to the parent
2792 parent->appendChild(repr);
2794 // move to the saved position
2795 repr->setPosition(pos > 0 ? pos + 1 : 1);
2797 // Set selection to the new image
2798 selection->clear();
2799 selection->add(repr);
2801 // Clean up
2802 Inkscape::GC::release(repr);
2803 gdk_pixbuf_unref(pb);
2805 // Complete undoable transaction
2806 sp_document_done(document, SP_VERB_SELECTION_CREATE_BITMAP,
2807 _("Create bitmap"));
2808 }
2810 desktop->clearWaitingCursor();
2812 g_free(basename);
2813 g_free(filepath);
2814 }
2816 /**
2817 * \brief Creates a mask or clipPath from selection
2818 * Two different modes:
2819 * if applyToLayer, all selection is moved to DEFS as mask/clippath
2820 * and is applied to current layer
2821 * otherwise, topmost object is used as mask for other objects
2822 * If \a apply_clip_path parameter is true, clipPath is created, otherwise mask
2823 *
2824 */
2825 void
2826 sp_selection_set_mask(SPDesktop *desktop, bool apply_clip_path, bool apply_to_layer)
2827 {
2828 if (desktop == NULL)
2829 return;
2831 SPDocument *doc = sp_desktop_document(desktop);
2832 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2834 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2836 // check if something is selected
2837 bool is_empty = selection->isEmpty();
2838 if ( apply_to_layer && is_empty) {
2839 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to create clippath or mask from."));
2840 return;
2841 } else if (!apply_to_layer && ( is_empty || NULL == selection->itemList()->next )) {
2842 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select mask object and <b>object(s)</b> to apply clippath or mask to."));
2843 return;
2844 }
2846 // FIXME: temporary patch to prevent crash!
2847 // Remove this when bboxes are fixed to not blow up on an item clipped/masked with its own clone
2848 bool clone_with_original = selection_contains_both_clone_and_original(selection);
2849 if (clone_with_original) {
2850 return; // in this version, you cannot clip/mask an object with its own clone
2851 }
2852 // /END FIXME
2854 sp_document_ensure_up_to_date(doc);
2856 GSList *items = g_slist_copy((GSList *) selection->itemList());
2858 items = g_slist_sort(items, (GCompareFunc) sp_object_compare_position);
2860 // See lp bug #542004
2861 selection->clear();
2863 // create a list of duplicates
2864 GSList *mask_items = NULL;
2865 GSList *apply_to_items = NULL;
2866 GSList *items_to_delete = NULL;
2867 GSList *items_to_select = NULL;
2869 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
2870 bool topmost = prefs->getBool("/options/maskobject/topmost", true);
2871 bool remove_original = prefs->getBool("/options/maskobject/remove", true);
2872 int grouping = prefs->getInt("/options/maskobject/grouping", PREFS_MASKOBJECT_GROUPING_NONE);
2874 if (apply_to_layer) {
2875 // all selected items are used for mask, which is applied to a layer
2876 apply_to_items = g_slist_prepend(apply_to_items, desktop->currentLayer());
2878 for (GSList *i = items; i != NULL; i = i->next) {
2879 Inkscape::XML::Node *dup = (SP_OBJECT_REPR(i->data))->duplicate(xml_doc);
2880 mask_items = g_slist_prepend(mask_items, dup);
2882 SPObject *item = reinterpret_cast<SPObject*>(i->data);
2883 if (remove_original) {
2884 items_to_delete = g_slist_prepend(items_to_delete, item);
2885 }
2886 else {
2887 items_to_select = g_slist_prepend(items_to_select, item);
2888 }
2889 }
2890 } else if (!topmost) {
2891 // topmost item is used as a mask, which is applied to other items in a selection
2892 GSList *i = items;
2893 Inkscape::XML::Node *dup = (SP_OBJECT_REPR(i->data))->duplicate(xml_doc);
2894 mask_items = g_slist_prepend(mask_items, dup);
2896 if (remove_original) {
2897 SPObject *item = reinterpret_cast<SPObject*>(i->data);
2898 items_to_delete = g_slist_prepend(items_to_delete, item);
2899 }
2901 for (i = i->next; i != NULL; i = i->next) {
2902 apply_to_items = g_slist_prepend(apply_to_items, i->data);
2903 items_to_select = g_slist_prepend(items_to_select, i->data);
2904 }
2905 } else {
2906 GSList *i = NULL;
2907 for (i = items; NULL != i->next; i = i->next) {
2908 apply_to_items = g_slist_prepend(apply_to_items, i->data);
2909 items_to_select = g_slist_prepend(items_to_select, i->data);
2910 }
2912 Inkscape::XML::Node *dup = (SP_OBJECT_REPR(i->data))->duplicate(xml_doc);
2913 mask_items = g_slist_prepend(mask_items, dup);
2915 if (remove_original) {
2916 SPObject *item = reinterpret_cast<SPObject*>(i->data);
2917 items_to_delete = g_slist_prepend(items_to_delete, item);
2918 }
2919 }
2921 g_slist_free(items);
2922 items = NULL;
2924 if (apply_to_items && grouping == PREFS_MASKOBJECT_GROUPING_ALL) {
2925 // group all those objects into one group
2926 // and apply mask to that
2927 Inkscape::XML::Node *group = xml_doc->createElement("svg:g");
2929 // make a note we should ungroup this when unsetting mask
2930 group->setAttribute("inkscape:groupmode", "maskhelper");
2932 GSList *reprs_to_group = NULL;
2934 for (GSList *i = apply_to_items ; NULL != i ; i = i->next) {
2935 reprs_to_group = g_slist_prepend(reprs_to_group, SP_OBJECT_REPR(i->data));
2936 items_to_select = g_slist_remove(items_to_select, i->data);
2937 }
2938 reprs_to_group = g_slist_reverse(reprs_to_group);
2940 sp_selection_group_impl(reprs_to_group, group, xml_doc, doc);
2942 reprs_to_group = NULL;
2944 // apply clip/mask only to newly created group
2945 g_slist_free(apply_to_items);
2946 apply_to_items = NULL;
2947 apply_to_items = g_slist_prepend(apply_to_items, doc->getObjectByRepr(group));
2949 items_to_select = g_slist_prepend(items_to_select, doc->getObjectByRepr(group));
2951 Inkscape::GC::release(group);
2952 }
2954 gchar const *attributeName = apply_clip_path ? "clip-path" : "mask";
2955 for (GSList *i = apply_to_items; NULL != i; i = i->next) {
2956 SPItem *item = reinterpret_cast<SPItem *>(i->data);
2957 // inverted object transform should be applied to a mask object,
2958 // as mask is calculated in user space (after applying transform)
2959 Geom::Matrix maskTransform(item->transform.inverse());
2961 GSList *mask_items_dup = NULL;
2962 for (GSList *mask_item = mask_items; NULL != mask_item; mask_item = mask_item->next) {
2963 Inkscape::XML::Node *dup = reinterpret_cast<Inkscape::XML::Node *>(mask_item->data)->duplicate(xml_doc);
2964 mask_items_dup = g_slist_prepend(mask_items_dup, dup);
2965 }
2967 gchar const *mask_id = NULL;
2968 if (apply_clip_path) {
2969 mask_id = sp_clippath_create(mask_items_dup, doc, &maskTransform);
2970 } else {
2971 mask_id = sp_mask_create(mask_items_dup, doc, &maskTransform);
2972 }
2974 g_slist_free(mask_items_dup);
2975 mask_items_dup = NULL;
2977 Inkscape::XML::Node *current = SP_OBJECT_REPR(i->data);
2978 // Node to apply mask to
2979 Inkscape::XML::Node *apply_mask_to = current;
2981 if (grouping == PREFS_MASKOBJECT_GROUPING_SEPARATE) {
2982 // enclose current node in group, and apply crop/mask on that
2983 Inkscape::XML::Node *group = xml_doc->createElement("svg:g");
2984 // make a note we should ungroup this when unsetting mask
2985 group->setAttribute("inkscape:groupmode", "maskhelper");
2987 Inkscape::XML::Node *spnew = current->duplicate(xml_doc);
2988 gint position = current->position();
2989 items_to_select = g_slist_remove(items_to_select, item);
2990 current->parent()->appendChild(group);
2991 sp_repr_unparent(current);
2992 group->appendChild(spnew);
2993 group->setPosition(position);
2995 // Apply clip/mask to group instead
2996 apply_mask_to = group;
2998 items_to_select = g_slist_prepend(items_to_select, doc->getObjectByRepr(group));
2999 Inkscape::GC::release(spnew);
3000 Inkscape::GC::release(group);
3001 }
3003 apply_mask_to->setAttribute(attributeName, g_strdup_printf("url(#%s)", mask_id));
3005 }
3007 g_slist_free(mask_items);
3008 g_slist_free(apply_to_items);
3010 for (GSList *i = items_to_delete; NULL != i; i = i->next) {
3011 SPObject *item = reinterpret_cast<SPObject*>(i->data);
3012 item->deleteObject(false);
3013 items_to_select = g_slist_remove(items_to_select, item);
3014 }
3015 g_slist_free(items_to_delete);
3017 items_to_select = g_slist_reverse(items_to_select);
3019 selection->addList(items_to_select);
3020 g_slist_free(items_to_select);
3022 if (apply_clip_path)
3023 sp_document_done(doc, SP_VERB_OBJECT_SET_CLIPPATH, _("Set clipping path"));
3024 else
3025 sp_document_done(doc, SP_VERB_OBJECT_SET_MASK, _("Set mask"));
3026 }
3028 void sp_selection_unset_mask(SPDesktop *desktop, bool apply_clip_path) {
3029 if (desktop == NULL)
3030 return;
3032 SPDocument *doc = sp_desktop_document(desktop);
3033 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
3034 Inkscape::Selection *selection = sp_desktop_selection(desktop);
3036 // check if something is selected
3037 if (selection->isEmpty()) {
3038 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to remove clippath or mask from."));
3039 return;
3040 }
3042 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
3043 bool remove_original = prefs->getBool("/options/maskobject/remove", true);
3044 bool ungroup_masked = prefs->getBool("/options/maskobject/ungrouping", true);
3045 sp_document_ensure_up_to_date(doc);
3047 gchar const *attributeName = apply_clip_path ? "clip-path" : "mask";
3048 std::map<SPObject*,SPItem*> referenced_objects;
3050 GSList *items = g_slist_copy((GSList *) selection->itemList());
3051 selection->clear();
3053 GSList *items_to_ungroup = NULL;
3054 GSList *items_to_select = g_slist_copy(items);
3055 items_to_select = g_slist_reverse(items_to_select);
3058 // SPObject* refers to a group containing the clipped path or mask itself,
3059 // whereas SPItem* refers to the item being clipped or masked
3060 for (GSList const *i = items; NULL != i; i = i->next) {
3061 if (remove_original) {
3062 // remember referenced mask/clippath, so orphaned masks can be moved back to document
3063 SPItem *item = reinterpret_cast<SPItem *>(i->data);
3064 Inkscape::URIReference *uri_ref = NULL;
3066 if (apply_clip_path) {
3067 uri_ref = item->clip_ref;
3068 } else {
3069 uri_ref = item->mask_ref;
3070 }
3072 // collect distinct mask object (and associate with item to apply transform)
3073 if (NULL != uri_ref && NULL != uri_ref->getObject()) {
3074 referenced_objects[uri_ref->getObject()] = item;
3075 }
3076 }
3078 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, "none");
3080 if (ungroup_masked && SP_IS_GROUP(i->data)) {
3081 // if we had previously enclosed masked object in group,
3082 // add it to list so we can ungroup it later
3083 SPGroup *item = SP_GROUP(i->data);
3085 // ungroup only groups we created when setting clip/mask
3086 if (item->layerMode() == SPGroup::MASK_HELPER) {
3087 items_to_ungroup = g_slist_prepend(items_to_ungroup, item);
3088 }
3090 }
3091 }
3092 g_slist_free(items);
3094 // restore mask objects into a document
3095 for ( std::map<SPObject*,SPItem*>::iterator it = referenced_objects.begin() ; it != referenced_objects.end() ; ++it) {
3096 SPObject *obj = (*it).first; // Group containing the clipped paths or masks
3097 GSList *items_to_move = NULL;
3098 for (SPObject *child = sp_object_first_child(obj) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
3099 // Collect all clipped paths and masks within a single group
3100 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate(xml_doc);
3101 items_to_move = g_slist_prepend(items_to_move, copy);
3102 }
3104 if (!obj->isReferenced()) {
3105 // delete from defs if no other object references this mask
3106 obj->deleteObject(false);
3107 }
3109 // remember parent and position of the item to which the clippath/mask was applied
3110 Inkscape::XML::Node *parent = SP_OBJECT_REPR((*it).second)->parent();
3111 gint pos = SP_OBJECT_REPR((*it).second)->position();
3113 // Iterate through all clipped paths / masks
3114 for (GSList *i = items_to_move; NULL != i; i = i->next) {
3115 Inkscape::XML::Node *repr = (Inkscape::XML::Node *)i->data;
3117 // insert into parent, restore pos
3118 parent->appendChild(repr);
3119 repr->setPosition((pos + 1) > 0 ? (pos + 1) : 0);
3121 SPItem *mask_item = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
3122 items_to_select = g_slist_prepend(items_to_select, mask_item);
3124 // transform mask, so it is moved the same spot where mask was applied
3125 Geom::Matrix transform(mask_item->transform);
3126 transform *= (*it).second->transform;
3127 sp_item_write_transform(mask_item, SP_OBJECT_REPR(mask_item), transform);
3128 }
3130 g_slist_free(items_to_move);
3131 }
3133 // ungroup marked groups added when setting mask
3134 for (GSList *i = items_to_ungroup ; NULL != i ; i = i->next) {
3135 items_to_select = g_slist_remove(items_to_select, SP_GROUP(i->data));
3136 GSList *children = NULL;
3137 sp_item_group_ungroup(SP_GROUP(i->data), &children, false);
3138 items_to_select = g_slist_concat(children, items_to_select);
3139 }
3141 g_slist_free(items_to_ungroup);
3143 // rebuild selection
3144 items_to_select = g_slist_reverse(items_to_select);
3145 selection->addList(items_to_select);
3146 g_slist_free(items_to_select);
3148 if (apply_clip_path)
3149 sp_document_done(doc, SP_VERB_OBJECT_UNSET_CLIPPATH, _("Release clipping path"));
3150 else
3151 sp_document_done(doc, SP_VERB_OBJECT_UNSET_MASK, _("Release mask"));
3152 }
3154 /**
3155 * \param with_margins margins defined in the xml under <sodipodi:namedview>
3156 * "fit-margin-..." attributes. See SPDocument::fitToRect.
3157 * \return true if an undoable change should be recorded.
3158 */
3159 bool
3160 fit_canvas_to_selection(SPDesktop *desktop, bool with_margins)
3161 {
3162 g_return_val_if_fail(desktop != NULL, false);
3163 SPDocument *doc = sp_desktop_document(desktop);
3165 g_return_val_if_fail(doc != NULL, false);
3166 g_return_val_if_fail(desktop->selection != NULL, false);
3168 if (desktop->selection->isEmpty()) {
3169 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to fit canvas to."));
3170 return false;
3171 }
3172 Geom::OptRect const bbox(desktop->selection->bounds(SPItem::RENDERING_BBOX));
3173 if (bbox) {
3174 doc->fitToRect(*bbox, with_margins);
3175 return true;
3176 } else {
3177 return false;
3178 }
3179 }
3181 /**
3182 * Fit canvas to the bounding box of the selection, as an undoable action.
3183 */
3184 void
3185 verb_fit_canvas_to_selection(SPDesktop *const desktop)
3186 {
3187 if (fit_canvas_to_selection(desktop)) {
3188 sp_document_done(sp_desktop_document(desktop), SP_VERB_FIT_CANVAS_TO_SELECTION,
3189 _("Fit Page to Selection"));
3190 }
3191 }
3193 /**
3194 * \param with_margins margins defined in the xml under <sodipodi:namedview>
3195 * "fit-margin-..." attributes. See SPDocument::fitToRect.
3196 */
3197 bool
3198 fit_canvas_to_drawing(SPDocument *doc, bool with_margins)
3199 {
3200 g_return_val_if_fail(doc != NULL, false);
3202 sp_document_ensure_up_to_date(doc);
3203 SPItem const *const root = SP_ITEM(doc->root);
3204 Geom::OptRect const bbox(root->getBounds(sp_item_i2d_affine(root)));
3205 if (bbox) {
3206 doc->fitToRect(*bbox, with_margins);
3207 return true;
3208 } else {
3209 return false;
3210 }
3211 }
3213 void
3214 verb_fit_canvas_to_drawing(SPDesktop *desktop)
3215 {
3216 if (fit_canvas_to_drawing(sp_desktop_document(desktop))) {
3217 sp_document_done(sp_desktop_document(desktop), SP_VERB_FIT_CANVAS_TO_DRAWING,
3218 _("Fit Page to Drawing"));
3219 }
3220 }
3222 /**
3223 * Fits canvas to selection or drawing with margins from <sodipodi:namedview>
3224 * "fit-margin-..." attributes. See SPDocument::fitToRect and
3225 * ui/dialog/page-sizer.
3226 */
3227 void fit_canvas_to_selection_or_drawing(SPDesktop *desktop) {
3228 g_return_if_fail(desktop != NULL);
3229 SPDocument *doc = sp_desktop_document(desktop);
3231 g_return_if_fail(doc != NULL);
3232 g_return_if_fail(desktop->selection != NULL);
3234 bool const changed = ( desktop->selection->isEmpty()
3235 ? fit_canvas_to_drawing(doc, true)
3236 : fit_canvas_to_selection(desktop, true) );
3237 if (changed) {
3238 sp_document_done(sp_desktop_document(desktop), SP_VERB_FIT_CANVAS_TO_SELECTION_OR_DRAWING,
3239 _("Fit Page to Selection or Drawing"));
3240 }
3241 };
3243 static void itemtree_map(void (*f)(SPItem *, SPDesktop *), SPObject *root, SPDesktop *desktop) {
3244 // don't operate on layers
3245 if (SP_IS_ITEM(root) && !desktop->isLayer(SP_ITEM(root))) {
3246 f(SP_ITEM(root), desktop);
3247 }
3248 for ( SPObject::SiblingIterator iter = root->firstChild() ; iter ; ++iter ) {
3249 //don't recurse into locked layers
3250 if (!(SP_IS_ITEM(&*iter) && desktop->isLayer(SP_ITEM(&*iter)) && SP_ITEM(&*iter)->isLocked())) {
3251 itemtree_map(f, iter, desktop);
3252 }
3253 }
3254 }
3256 static void unlock(SPItem *item, SPDesktop */*desktop*/) {
3257 if (item->isLocked()) {
3258 item->setLocked(FALSE);
3259 }
3260 }
3262 static void unhide(SPItem *item, SPDesktop *desktop) {
3263 if (desktop->itemIsHidden(item)) {
3264 item->setExplicitlyHidden(FALSE);
3265 }
3266 }
3268 static void process_all(void (*f)(SPItem *, SPDesktop *), SPDesktop *dt, bool layer_only) {
3269 if (!dt) return;
3271 SPObject *root;
3272 if (layer_only) {
3273 root = dt->currentLayer();
3274 } else {
3275 root = dt->currentRoot();
3276 }
3278 itemtree_map(f, root, dt);
3279 }
3281 void unlock_all(SPDesktop *dt) {
3282 process_all(&unlock, dt, true);
3283 }
3285 void unlock_all_in_all_layers(SPDesktop *dt) {
3286 process_all(&unlock, dt, false);
3287 }
3289 void unhide_all(SPDesktop *dt) {
3290 process_all(&unhide, dt, true);
3291 }
3293 void unhide_all_in_all_layers(SPDesktop *dt) {
3294 process_all(&unhide, dt, false);
3295 }
3298 /*
3299 Local Variables:
3300 mode:c++
3301 c-file-style:"stroustrup"
3302 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
3303 indent-tabs-mode:nil
3304 fill-column:99
3305 End:
3306 */
3307 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :