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 } else if (SP_IS_OFFSET(old_clone)) {
419 for (guint j = 0; j < old_ids.size(); j++) {
420 gchar *source_href = SP_OFFSET(old_clone)->sourceHref;
421 if (source_href && source_href[0]=='#' && !strcmp(source_href+1, old_ids[j])) {
422 gchar *newref = g_strdup_printf("#%s", new_ids[j]);
423 doc->getObjectById(new_ids[i])->repr->setAttribute("xlink:href", newref);
424 g_free(newref);
425 }
426 }
427 }
428 }
429 }
432 if ( !suppressDone ) {
433 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_DUPLICATE,
434 _("Duplicate"));
435 }
437 selection->setReprList(newsel);
439 g_slist_free(newsel);
440 }
442 void sp_edit_clear_all(SPDesktop *dt)
443 {
444 if (!dt)
445 return;
447 SPDocument *doc = sp_desktop_document(dt);
448 sp_desktop_selection(dt)->clear();
450 g_return_if_fail(SP_IS_GROUP(dt->currentLayer()));
451 GSList *items = sp_item_group_item_list(SP_GROUP(dt->currentLayer()));
453 while (items) {
454 reinterpret_cast<SPObject*>(items->data)->deleteObject();
455 items = g_slist_remove(items, items->data);
456 }
458 sp_document_done(doc, SP_VERB_EDIT_CLEAR_ALL,
459 _("Delete all"));
460 }
462 GSList *
463 get_all_items(GSList *list, SPObject *from, SPDesktop *desktop, bool onlyvisible, bool onlysensitive, GSList const *exclude)
464 {
465 for (SPObject *child = sp_object_first_child(from) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
466 if (SP_IS_ITEM(child) &&
467 !desktop->isLayer(SP_ITEM(child)) &&
468 (!onlysensitive || !SP_ITEM(child)->isLocked()) &&
469 (!onlyvisible || !desktop->itemIsHidden(SP_ITEM(child))) &&
470 (!exclude || !g_slist_find((GSList *) exclude, child))
471 )
472 {
473 list = g_slist_prepend(list, SP_ITEM(child));
474 }
476 if (SP_IS_ITEM(child) && desktop->isLayer(SP_ITEM(child))) {
477 list = get_all_items(list, child, desktop, onlyvisible, onlysensitive, exclude);
478 }
479 }
481 return list;
482 }
484 void sp_edit_select_all_full(SPDesktop *dt, bool force_all_layers, bool invert)
485 {
486 if (!dt)
487 return;
489 Inkscape::Selection *selection = sp_desktop_selection(dt);
491 g_return_if_fail(SP_IS_GROUP(dt->currentLayer()));
493 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
494 PrefsSelectionContext inlayer = (PrefsSelectionContext) prefs->getInt("/options/kbselection/inlayer", PREFS_SELECTION_LAYER);
495 bool onlyvisible = prefs->getBool("/options/kbselection/onlyvisible", true);
496 bool onlysensitive = prefs->getBool("/options/kbselection/onlysensitive", true);
498 GSList *items = NULL;
500 GSList const *exclude = NULL;
501 if (invert) {
502 exclude = selection->itemList();
503 }
505 if (force_all_layers)
506 inlayer = PREFS_SELECTION_ALL;
508 switch (inlayer) {
509 case PREFS_SELECTION_LAYER: {
510 if ( (onlysensitive && SP_ITEM(dt->currentLayer())->isLocked()) ||
511 (onlyvisible && dt->itemIsHidden(SP_ITEM(dt->currentLayer()))) )
512 return;
514 GSList *all_items = sp_item_group_item_list(SP_GROUP(dt->currentLayer()));
516 for (GSList *i = all_items; i; i = i->next) {
517 SPItem *item = SP_ITEM(i->data);
519 if (item && (!onlysensitive || !item->isLocked())) {
520 if (!onlyvisible || !dt->itemIsHidden(item)) {
521 if (!dt->isLayer(item)) {
522 if (!invert || !g_slist_find((GSList *) exclude, item)) {
523 items = g_slist_prepend(items, item); // leave it in the list
524 }
525 }
526 }
527 }
528 }
530 g_slist_free(all_items);
531 break;
532 }
533 case PREFS_SELECTION_LAYER_RECURSIVE: {
534 items = get_all_items(NULL, dt->currentLayer(), dt, onlyvisible, onlysensitive, exclude);
535 break;
536 }
537 default: {
538 items = get_all_items(NULL, dt->currentRoot(), dt, onlyvisible, onlysensitive, exclude);
539 break;
540 }
541 }
543 selection->setList(items);
545 if (items) {
546 g_slist_free(items);
547 }
548 }
550 void sp_edit_select_all(SPDesktop *desktop)
551 {
552 sp_edit_select_all_full(desktop, false, false);
553 }
555 void sp_edit_select_all_in_all_layers(SPDesktop *desktop)
556 {
557 sp_edit_select_all_full(desktop, true, false);
558 }
560 void sp_edit_invert(SPDesktop *desktop)
561 {
562 sp_edit_select_all_full(desktop, false, true);
563 }
565 void sp_edit_invert_in_all_layers(SPDesktop *desktop)
566 {
567 sp_edit_select_all_full(desktop, true, true);
568 }
570 void sp_selection_group_impl(GSList *p, Inkscape::XML::Node *group, Inkscape::XML::Document *xml_doc, SPDocument *doc) {
572 p = g_slist_sort(p, (GCompareFunc) sp_repr_compare_position);
574 // Remember the position and parent of the topmost object.
575 gint topmost = ((Inkscape::XML::Node *) g_slist_last(p)->data)->position();
576 Inkscape::XML::Node *topmost_parent = ((Inkscape::XML::Node *) g_slist_last(p)->data)->parent();
578 while (p) {
579 Inkscape::XML::Node *current = (Inkscape::XML::Node *) p->data;
581 if (current->parent() == topmost_parent) {
582 Inkscape::XML::Node *spnew = current->duplicate(xml_doc);
583 sp_repr_unparent(current);
584 group->appendChild(spnew);
585 Inkscape::GC::release(spnew);
586 topmost --; // only reduce count for those items deleted from topmost_parent
587 } else { // move it to topmost_parent first
588 GSList *temp_clip = NULL;
590 // At this point, current may already have no item, due to its being a clone whose original is already moved away
591 // So we copy it artificially calculating the transform from its repr->attr("transform") and the parent transform
592 gchar const *t_str = current->attribute("transform");
593 Geom::Matrix item_t(Geom::identity());
594 if (t_str)
595 sp_svg_transform_read(t_str, &item_t);
596 item_t *= sp_item_i2doc_affine(SP_ITEM(doc->getObjectByRepr(current->parent())));
597 // FIXME: when moving both clone and original from a transformed group (either by
598 // grouping into another parent, or by cut/paste) the transform from the original's
599 // parent becomes embedded into original itself, and this affects its clones. Fix
600 // this by remembering the transform diffs we write to each item into an array and
601 // then, if this is clone, looking up its original in that array and pre-multiplying
602 // it by the inverse of that original's transform diff.
604 sp_selection_copy_one(current, item_t, &temp_clip, xml_doc);
605 sp_repr_unparent(current);
607 // paste into topmost_parent (temporarily)
608 GSList *copied = sp_selection_paste_impl(doc, doc->getObjectByRepr(topmost_parent), &temp_clip);
609 if (temp_clip) g_slist_free(temp_clip);
610 if (copied) { // if success,
611 // take pasted object (now in topmost_parent)
612 Inkscape::XML::Node *in_topmost = (Inkscape::XML::Node *) copied->data;
613 // make a copy
614 Inkscape::XML::Node *spnew = in_topmost->duplicate(xml_doc);
615 // remove pasted
616 sp_repr_unparent(in_topmost);
617 // put its copy into group
618 group->appendChild(spnew);
619 Inkscape::GC::release(spnew);
620 g_slist_free(copied);
621 }
622 }
623 p = g_slist_remove(p, current);
624 }
626 // Add the new group to the topmost members' parent
627 topmost_parent->appendChild(group);
629 // Move to the position of the topmost, reduced by the number of items deleted from topmost_parent
630 group->setPosition(topmost + 1);
631 }
633 void sp_selection_group(SPDesktop *desktop)
634 {
635 if (desktop == NULL)
636 return;
638 SPDocument *doc = sp_desktop_document(desktop);
639 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
641 Inkscape::Selection *selection = sp_desktop_selection(desktop);
643 // Check if something is selected.
644 if (selection->isEmpty()) {
645 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>some objects</b> to group."));
646 return;
647 }
649 GSList const *l = (GSList *) selection->reprList();
651 GSList *p = g_slist_copy((GSList *) l);
653 selection->clear();
655 Inkscape::XML::Node *group = xml_doc->createElement("svg:g");
657 sp_selection_group_impl(p, group, xml_doc, doc);
659 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_GROUP,
660 _("Group"));
662 selection->set(group);
663 Inkscape::GC::release(group);
664 }
666 void sp_selection_ungroup(SPDesktop *desktop)
667 {
668 if (desktop == NULL)
669 return;
671 Inkscape::Selection *selection = sp_desktop_selection(desktop);
673 if (selection->isEmpty()) {
674 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>group</b> to ungroup."));
675 return;
676 }
678 GSList *items = g_slist_copy((GSList *) selection->itemList());
679 selection->clear();
681 // Get a copy of current selection.
682 GSList *new_select = NULL;
683 bool ungrouped = false;
684 for (GSList *i = items;
685 i != NULL;
686 i = i->next)
687 {
688 SPItem *group = (SPItem *) i->data;
690 // when ungrouping cloned groups with their originals, some objects that were selected may no more exist due to unlinking
691 if (!SP_IS_OBJECT(group)) {
692 continue;
693 }
695 /* We do not allow ungrouping <svg> etc. (lauris) */
696 if (strcmp(SP_OBJECT_REPR(group)->name(), "svg:g") && strcmp(SP_OBJECT_REPR(group)->name(), "svg:switch")) {
697 // keep the non-group item in the new selection
698 new_select = g_slist_append(new_select, group);
699 continue;
700 }
702 GSList *children = NULL;
703 /* This is not strictly required, but is nicer to rely on group ::destroy (lauris) */
704 sp_item_group_ungroup(SP_GROUP(group), &children, false);
705 ungrouped = true;
706 // Add ungrouped items to the new selection.
707 new_select = g_slist_concat(new_select, children);
708 }
710 if (new_select) { // Set new selection.
711 selection->addList(new_select);
712 g_slist_free(new_select);
713 }
714 if (!ungrouped) {
715 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No groups</b> to ungroup in the selection."));
716 }
718 g_slist_free(items);
720 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_UNGROUP,
721 _("Ungroup"));
722 }
724 /** Replace all groups in the list with their member objects, recursively; returns a new list, frees old */
725 GSList *
726 sp_degroup_list(GSList *items)
727 {
728 GSList *out = NULL;
729 bool has_groups = false;
730 for (GSList *item = items; item; item = item->next) {
731 if (!SP_IS_GROUP(item->data)) {
732 out = g_slist_prepend(out, item->data);
733 } else {
734 has_groups = true;
735 GSList *members = sp_item_group_item_list(SP_GROUP(item->data));
736 for (GSList *member = members; member; member = member->next) {
737 out = g_slist_prepend(out, member->data);
738 }
739 g_slist_free(members);
740 }
741 }
742 out = g_slist_reverse(out);
743 g_slist_free(items);
745 if (has_groups) { // recurse if we unwrapped a group - it may have contained others
746 out = sp_degroup_list(out);
747 }
749 return out;
750 }
753 /** If items in the list have a common parent, return it, otherwise return NULL */
754 static SPGroup *
755 sp_item_list_common_parent_group(GSList const *items)
756 {
757 if (!items) {
758 return NULL;
759 }
760 SPObject *parent = SP_OBJECT_PARENT(items->data);
761 /* Strictly speaking this CAN happen, if user selects <svg> from Inkscape::XML editor */
762 if (!SP_IS_GROUP(parent)) {
763 return NULL;
764 }
765 for (items = items->next; items; items = items->next) {
766 if (SP_OBJECT_PARENT(items->data) != parent) {
767 return NULL;
768 }
769 }
771 return SP_GROUP(parent);
772 }
774 /** Finds out the minimum common bbox of the selected items. */
775 static Geom::OptRect
776 enclose_items(GSList const *items)
777 {
778 g_assert(items != NULL);
780 Geom::OptRect r;
781 for (GSList const *i = items; i; i = i->next) {
782 r = Geom::unify(r, sp_item_bbox_desktop((SPItem *) i->data));
783 }
784 return r;
785 }
787 SPObject *
788 prev_sibling(SPObject *child)
789 {
790 SPObject *parent = SP_OBJECT_PARENT(child);
791 if (!SP_IS_GROUP(parent)) {
792 return NULL;
793 }
794 for ( SPObject *i = sp_object_first_child(parent) ; i; i = SP_OBJECT_NEXT(i) ) {
795 if (i->next == child)
796 return i;
797 }
798 return NULL;
799 }
801 void
802 sp_selection_raise(SPDesktop *desktop)
803 {
804 if (!desktop)
805 return;
807 Inkscape::Selection *selection = sp_desktop_selection(desktop);
809 GSList const *items = (GSList *) selection->itemList();
810 if (!items) {
811 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise."));
812 return;
813 }
815 SPGroup const *group = sp_item_list_common_parent_group(items);
816 if (!group) {
817 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
818 return;
819 }
821 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
823 /* Construct reverse-ordered list of selected children. */
824 GSList *rev = g_slist_copy((GSList *) items);
825 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
827 // Determine the common bbox of the selected items.
828 Geom::OptRect selected = enclose_items(items);
830 // Iterate over all objects in the selection (starting from top).
831 if (selected) {
832 while (rev) {
833 SPObject *child = reinterpret_cast<SPObject*>(rev->data);
834 // for each selected object, find the next sibling
835 for (SPObject *newref = child->next; newref; newref = newref->next) {
836 // if the sibling is an item AND overlaps our selection,
837 if (SP_IS_ITEM(newref)) {
838 Geom::OptRect newref_bbox = sp_item_bbox_desktop(SP_ITEM(newref));
839 if ( newref_bbox && selected->intersects(*newref_bbox) ) {
840 // AND if it's not one of our selected objects,
841 if (!g_slist_find((GSList *) items, newref)) {
842 // move the selected object after that sibling
843 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(newref));
844 }
845 break;
846 }
847 }
848 }
849 rev = g_slist_remove(rev, child);
850 }
851 } else {
852 g_slist_free(rev);
853 }
855 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_RAISE,
856 //TRANSLATORS: "Raise" means "to raise an object" in the undo history
857 C_("Undo action", "Raise"));
858 }
860 void sp_selection_raise_to_top(SPDesktop *desktop)
861 {
862 if (desktop == NULL)
863 return;
865 SPDocument *document = sp_desktop_document(desktop);
866 Inkscape::Selection *selection = sp_desktop_selection(desktop);
868 if (selection->isEmpty()) {
869 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise to top."));
870 return;
871 }
873 GSList const *items = (GSList *) selection->itemList();
875 SPGroup const *group = sp_item_list_common_parent_group(items);
876 if (!group) {
877 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
878 return;
879 }
881 GSList *rl = g_slist_copy((GSList *) selection->reprList());
882 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
884 for (GSList *l = rl; l != NULL; l = l->next) {
885 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
886 repr->setPosition(-1);
887 }
889 g_slist_free(rl);
891 sp_document_done(document, SP_VERB_SELECTION_TO_FRONT,
892 _("Raise to top"));
893 }
895 void
896 sp_selection_lower(SPDesktop *desktop)
897 {
898 if (desktop == NULL)
899 return;
901 Inkscape::Selection *selection = sp_desktop_selection(desktop);
903 GSList const *items = (GSList *) selection->itemList();
904 if (!items) {
905 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower."));
906 return;
907 }
909 SPGroup const *group = sp_item_list_common_parent_group(items);
910 if (!group) {
911 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
912 return;
913 }
915 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
917 // Determine the common bbox of the selected items.
918 Geom::OptRect selected = enclose_items(items);
920 /* Construct direct-ordered list of selected children. */
921 GSList *rev = g_slist_copy((GSList *) items);
922 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
923 rev = g_slist_reverse(rev);
925 // Iterate over all objects in the selection (starting from top).
926 if (selected) {
927 while (rev) {
928 SPObject *child = reinterpret_cast<SPObject*>(rev->data);
929 // for each selected object, find the prev sibling
930 for (SPObject *newref = prev_sibling(child); newref; newref = prev_sibling(newref)) {
931 // if the sibling is an item AND overlaps our selection,
932 if (SP_IS_ITEM(newref)) {
933 Geom::OptRect ref_bbox = sp_item_bbox_desktop(SP_ITEM(newref));
934 if ( ref_bbox && selected->intersects(*ref_bbox) ) {
935 // AND if it's not one of our selected objects,
936 if (!g_slist_find((GSList *) items, newref)) {
937 // move the selected object before that sibling
938 SPObject *put_after = prev_sibling(newref);
939 if (put_after)
940 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(put_after));
941 else
942 SP_OBJECT_REPR(child)->setPosition(0);
943 }
944 break;
945 }
946 }
947 }
948 rev = g_slist_remove(rev, child);
949 }
950 } else {
951 g_slist_free(rev);
952 }
954 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_LOWER,
955 _("Lower"));
956 }
958 void sp_selection_lower_to_bottom(SPDesktop *desktop)
959 {
960 if (desktop == NULL)
961 return;
963 SPDocument *document = sp_desktop_document(desktop);
964 Inkscape::Selection *selection = sp_desktop_selection(desktop);
966 if (selection->isEmpty()) {
967 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower to bottom."));
968 return;
969 }
971 GSList const *items = (GSList *) selection->itemList();
973 SPGroup const *group = sp_item_list_common_parent_group(items);
974 if (!group) {
975 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
976 return;
977 }
979 GSList *rl;
980 rl = g_slist_copy((GSList *) selection->reprList());
981 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
982 rl = g_slist_reverse(rl);
984 for (GSList *l = rl; l != NULL; l = l->next) {
985 gint minpos;
986 SPObject *pp, *pc;
987 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
988 pp = document->getObjectByRepr(sp_repr_parent(repr));
989 minpos = 0;
990 g_assert(SP_IS_GROUP(pp));
991 pc = sp_object_first_child(pp);
992 while (!SP_IS_ITEM(pc)) {
993 minpos += 1;
994 pc = pc->next;
995 }
996 repr->setPosition(minpos);
997 }
999 g_slist_free(rl);
1001 sp_document_done(document, SP_VERB_SELECTION_TO_BACK,
1002 _("Lower to bottom"));
1003 }
1005 void
1006 sp_undo(SPDesktop *desktop, SPDocument *)
1007 {
1008 if (!sp_document_undo(sp_desktop_document(desktop)))
1009 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to undo."));
1010 }
1012 void
1013 sp_redo(SPDesktop *desktop, SPDocument *)
1014 {
1015 if (!sp_document_redo(sp_desktop_document(desktop)))
1016 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to redo."));
1017 }
1019 void sp_selection_cut(SPDesktop *desktop)
1020 {
1021 sp_selection_copy(desktop);
1022 sp_selection_delete(desktop);
1023 }
1025 /**
1026 * \pre item != NULL
1027 */
1028 SPCSSAttr *
1029 take_style_from_item(SPItem *item)
1030 {
1031 // write the complete cascaded style, context-free
1032 SPCSSAttr *css = sp_css_attr_from_object(item, SP_STYLE_FLAG_ALWAYS);
1033 if (css == NULL)
1034 return NULL;
1036 if ((SP_IS_GROUP(item) && item->children) ||
1037 (SP_IS_TEXT(item) && item->children && item->children->next == NULL)) {
1038 // if this is a text with exactly one tspan child, merge the style of that tspan as well
1039 // If this is a group, merge the style of its topmost (last) child with style
1040 for (SPObject *last_element = item->lastChild(); last_element != NULL; last_element = SP_OBJECT_PREV(last_element)) {
1041 if ( last_element->style ) {
1042 SPCSSAttr *temp = sp_css_attr_from_object(last_element, SP_STYLE_FLAG_IFSET);
1043 if (temp) {
1044 sp_repr_css_merge(css, temp);
1045 sp_repr_css_attr_unref(temp);
1046 }
1047 break;
1048 }
1049 }
1050 }
1051 if (!(SP_IS_TEXT(item) || SP_IS_TSPAN(item) || SP_IS_TREF(item) || SP_IS_STRING(item))) {
1052 // do not copy text properties from non-text objects, it's confusing
1053 css = sp_css_attr_unset_text(css);
1054 }
1056 // FIXME: also transform gradient/pattern fills, by forking? NO, this must be nondestructive
1057 double ex = to_2geom(sp_item_i2doc_affine(item)).descrim();
1058 if (ex != 1.0) {
1059 css = sp_css_attr_scale(css, ex);
1060 }
1062 return css;
1063 }
1066 void sp_selection_copy(SPDesktop *desktop)
1067 {
1068 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
1069 cm->copy(desktop);
1070 }
1072 void sp_selection_paste(SPDesktop *desktop, bool in_place)
1073 {
1074 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
1075 if (cm->paste(desktop, in_place)) {
1076 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_PASTE, _("Paste"));
1077 }
1078 }
1080 void sp_selection_paste_style(SPDesktop *desktop)
1081 {
1082 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
1083 if (cm->pasteStyle(desktop)) {
1084 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_PASTE_STYLE, _("Paste style"));
1085 }
1086 }
1089 void sp_selection_paste_livepatheffect(SPDesktop *desktop)
1090 {
1091 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
1092 if (cm->pastePathEffect(desktop)) {
1093 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_PASTE_LIVEPATHEFFECT,
1094 _("Paste live path effect"));
1095 }
1096 }
1099 void sp_selection_remove_livepatheffect_impl(SPItem *item)
1100 {
1101 if ( item && SP_IS_LPE_ITEM(item) &&
1102 sp_lpe_item_has_path_effect(SP_LPE_ITEM(item))) {
1103 sp_lpe_item_remove_all_path_effects(SP_LPE_ITEM(item), false);
1104 }
1105 }
1107 void sp_selection_remove_livepatheffect(SPDesktop *desktop)
1108 {
1109 if (desktop == NULL) return;
1111 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1113 // check if something is selected
1114 if (selection->isEmpty()) {
1115 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to remove live path effects from."));
1116 return;
1117 }
1119 for ( GSList const *itemlist = selection->itemList(); itemlist != NULL; itemlist = g_slist_next(itemlist) ) {
1120 SPItem *item = reinterpret_cast<SPItem*>(itemlist->data);
1122 sp_selection_remove_livepatheffect_impl(item);
1124 }
1126 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_REMOVE_LIVEPATHEFFECT,
1127 _("Remove live path effect"));
1128 }
1130 void sp_selection_remove_filter(SPDesktop *desktop)
1131 {
1132 if (desktop == NULL) return;
1134 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1136 // check if something is selected
1137 if (selection->isEmpty()) {
1138 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to remove filters from."));
1139 return;
1140 }
1142 SPCSSAttr *css = sp_repr_css_attr_new();
1143 sp_repr_css_unset_property(css, "filter");
1144 sp_desktop_set_style(desktop, css);
1145 sp_repr_css_attr_unref(css);
1147 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_REMOVE_FILTER,
1148 _("Remove filter"));
1149 }
1152 void sp_selection_paste_size(SPDesktop *desktop, bool apply_x, bool apply_y)
1153 {
1154 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
1155 if (cm->pasteSize(desktop, false, apply_x, apply_y)) {
1156 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_PASTE_SIZE,
1157 _("Paste size"));
1158 }
1159 }
1161 void sp_selection_paste_size_separately(SPDesktop *desktop, bool apply_x, bool apply_y)
1162 {
1163 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
1164 if (cm->pasteSize(desktop, true, apply_x, apply_y)) {
1165 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_PASTE_SIZE_SEPARATELY,
1166 _("Paste size separately"));
1167 }
1168 }
1170 void sp_selection_to_next_layer(SPDesktop *dt, bool suppressDone)
1171 {
1172 Inkscape::Selection *selection = sp_desktop_selection(dt);
1174 // check if something is selected
1175 if (selection->isEmpty()) {
1176 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer above."));
1177 return;
1178 }
1180 GSList const *items = g_slist_copy((GSList *) selection->itemList());
1182 bool no_more = false; // Set to true, if no more layers above
1183 SPObject *next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer());
1184 if (next) {
1185 GSList *temp_clip = NULL;
1186 sp_selection_copy_impl(items, &temp_clip, sp_document_repr_doc(dt->doc()));
1187 sp_selection_delete_impl(items, false, false);
1188 next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
1189 GSList *copied;
1190 if (next) {
1191 copied = sp_selection_paste_impl(sp_desktop_document(dt), next, &temp_clip);
1192 } else {
1193 copied = sp_selection_paste_impl(sp_desktop_document(dt), dt->currentLayer(), &temp_clip);
1194 no_more = true;
1195 }
1196 selection->setReprList((GSList const *) copied);
1197 g_slist_free(copied);
1198 if (temp_clip) g_slist_free(temp_clip);
1199 if (next) dt->setCurrentLayer(next);
1200 if ( !suppressDone ) {
1201 sp_document_done(sp_desktop_document(dt), SP_VERB_LAYER_MOVE_TO_NEXT,
1202 _("Raise to next layer"));
1203 }
1204 } else {
1205 no_more = true;
1206 }
1208 if (no_more) {
1209 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers above."));
1210 }
1212 g_slist_free((GSList *) items);
1213 }
1215 void sp_selection_to_prev_layer(SPDesktop *dt, bool suppressDone)
1216 {
1217 Inkscape::Selection *selection = sp_desktop_selection(dt);
1219 // check if something is selected
1220 if (selection->isEmpty()) {
1221 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer below."));
1222 return;
1223 }
1225 GSList const *items = g_slist_copy((GSList *) selection->itemList());
1227 bool no_more = false; // Set to true, if no more layers below
1228 SPObject *next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer());
1229 if (next) {
1230 GSList *temp_clip = NULL;
1231 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
1232 sp_selection_delete_impl(items, false, false);
1233 next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
1234 GSList *copied;
1235 if (next) {
1236 copied = sp_selection_paste_impl(sp_desktop_document(dt), next, &temp_clip);
1237 } else {
1238 copied = sp_selection_paste_impl(sp_desktop_document(dt), dt->currentLayer(), &temp_clip);
1239 no_more = true;
1240 }
1241 selection->setReprList((GSList const *) copied);
1242 g_slist_free(copied);
1243 if (temp_clip) g_slist_free(temp_clip);
1244 if (next) dt->setCurrentLayer(next);
1245 if ( !suppressDone ) {
1246 sp_document_done(sp_desktop_document(dt), SP_VERB_LAYER_MOVE_TO_PREV,
1247 _("Lower to previous layer"));
1248 }
1249 } else {
1250 no_more = true;
1251 }
1253 if (no_more) {
1254 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers below."));
1255 }
1257 g_slist_free((GSList *) items);
1258 }
1260 bool
1261 selection_contains_original(SPItem *item, Inkscape::Selection *selection)
1262 {
1263 bool contains_original = false;
1265 bool is_use = SP_IS_USE(item);
1266 SPItem *item_use = item;
1267 SPItem *item_use_first = item;
1268 while (is_use && item_use && !contains_original)
1269 {
1270 item_use = sp_use_get_original(SP_USE(item_use));
1271 contains_original |= selection->includes(item_use);
1272 if (item_use == item_use_first)
1273 break;
1274 is_use = SP_IS_USE(item_use);
1275 }
1277 // If it's a tref, check whether the object containing the character
1278 // data is part of the selection
1279 if (!contains_original && SP_IS_TREF(item)) {
1280 contains_original = selection->includes(SP_TREF(item)->getObjectReferredTo());
1281 }
1283 return contains_original;
1284 }
1287 bool
1288 selection_contains_both_clone_and_original(Inkscape::Selection *selection)
1289 {
1290 bool clone_with_original = false;
1291 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1292 SPItem *item = SP_ITEM(l->data);
1293 clone_with_original |= selection_contains_original(item, selection);
1294 if (clone_with_original)
1295 break;
1296 }
1297 return clone_with_original;
1298 }
1300 /** Apply matrix to the selection. \a set_i2d is normally true, which means objects are in the
1301 original transform, synced with their reprs, and need to jump to the new transform in one go. A
1302 value of set_i2d==false is only used by seltrans when it's dragging objects live (not outlines); in
1303 that case, items are already in the new position, but the repr is in the old, and this function
1304 then simply updates the repr from item->transform.
1305 */
1306 void sp_selection_apply_affine(Inkscape::Selection *selection, Geom::Matrix const &affine, bool set_i2d, bool compensate)
1307 {
1308 if (selection->isEmpty())
1309 return;
1311 // For each perspective with a box in selection, check whether all boxes are selected and
1312 // unlink all non-selected boxes.
1313 Persp3D *persp;
1314 Persp3D *transf_persp;
1315 std::list<Persp3D *> plist = selection->perspList();
1316 for (std::list<Persp3D *>::iterator i = plist.begin(); i != plist.end(); ++i) {
1317 persp = (Persp3D *) (*i);
1319 if (!persp3d_has_all_boxes_in_selection (persp, selection)) {
1320 std::list<SPBox3D *> selboxes = selection->box3DList(persp);
1322 // create a new perspective as a copy of the current one and link the selected boxes to it
1323 transf_persp = persp3d_create_xml_element (SP_OBJECT_DOCUMENT(persp), persp->perspective_impl);
1325 for (std::list<SPBox3D *>::iterator b = selboxes.begin(); b != selboxes.end(); ++b)
1326 box3d_switch_perspectives(*b, persp, transf_persp);
1327 } else {
1328 transf_persp = persp;
1329 }
1331 persp3d_apply_affine_transformation(transf_persp, affine);
1332 }
1334 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1335 SPItem *item = SP_ITEM(l->data);
1337 Geom::Point old_center(0,0);
1338 if (set_i2d && item->isCenterSet())
1339 old_center = item->getCenter();
1341 #if 0 /* Re-enable this once persistent guides have a graphical indication.
1342 At the time of writing, this is the only place to re-enable. */
1343 sp_item_update_cns(*item, selection->desktop());
1344 #endif
1346 // we're moving both a clone and its original or any ancestor in clone chain?
1347 bool transform_clone_with_original = selection_contains_original(item, selection);
1348 // ...both a text-on-path and its path?
1349 bool transform_textpath_with_path = (SP_IS_TEXT_TEXTPATH(item) && selection->includes( sp_textpath_get_path_item(SP_TEXTPATH(sp_object_first_child(item))) ));
1350 // ...both a flowtext and its frame?
1351 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)
1352 // ...both an offset and its source?
1353 bool transform_offset_with_source = (SP_IS_OFFSET(item) && SP_OFFSET(item)->sourceHref) && selection->includes( sp_offset_get_source(SP_OFFSET(item)) );
1355 // If we're moving a connector, we want to detach it
1356 // from shapes that aren't part of the selection, but
1357 // leave it attached if they are
1358 if (cc_item_is_connector(item)) {
1359 SPItem *attItem[2];
1360 SP_PATH(item)->connEndPair.getAttachedItems(attItem);
1362 for (int n = 0; n < 2; ++n) {
1363 if (!selection->includes(attItem[n])) {
1364 sp_conn_end_detach(item, n);
1365 }
1366 }
1367 }
1369 // "clones are unmoved when original is moved" preference
1370 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1371 int compensation = prefs->getInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
1372 bool prefs_unmoved = (compensation == SP_CLONE_COMPENSATION_UNMOVED);
1373 bool prefs_parallel = (compensation == SP_CLONE_COMPENSATION_PARALLEL);
1375 /* If this is a clone and it's selected along with its original, do not move it;
1376 * it will feel the transform of its original and respond to it itself.
1377 * Without this, a clone is doubly transformed, very unintuitive.
1378 *
1379 * Same for textpath if we are also doing ANY transform to its path: do not touch textpath,
1380 * letters cannot be squeezed or rotated anyway, they only refill the changed path.
1381 * Same for linked offset if we are also moving its source: do not move it. */
1382 if (transform_textpath_with_path || transform_offset_with_source) {
1383 // Restore item->transform field from the repr, in case it was changed by seltrans.
1384 sp_object_read_attr(item, "transform");
1385 } else if (transform_flowtext_with_frame) {
1386 // apply the inverse of the region's transform to the <use> so that the flow remains
1387 // the same (even though the output itself gets transformed)
1388 for (SPObject *region = item->firstChild() ; region ; region = SP_OBJECT_NEXT(region)) {
1389 if (!SP_IS_FLOWREGION(region) && !SP_IS_FLOWREGIONEXCLUDE(region))
1390 continue;
1391 for (SPObject *use = region->firstChild() ; use ; use = SP_OBJECT_NEXT(use)) {
1392 if (!SP_IS_USE(use)) continue;
1393 sp_item_write_transform(SP_USE(use), SP_OBJECT_REPR(use), item->transform.inverse(), NULL, compensate);
1394 }
1395 }
1396 } else if (transform_clone_with_original) {
1397 // We are transforming a clone along with its original. The below matrix juggling is
1398 // necessary to ensure that they transform as a whole, i.e. the clone's induced
1399 // transform and its move compensation are both cancelled out.
1401 // restore item->transform field from the repr, in case it was changed by seltrans
1402 sp_object_read_attr(item, "transform");
1404 // calculate the matrix we need to apply to the clone to cancel its induced transform from its original
1405 Geom::Matrix parent2dt = sp_item_i2d_affine(SP_ITEM(SP_OBJECT_PARENT(item)));
1406 Geom::Matrix t = parent2dt * affine * parent2dt.inverse();
1407 Geom::Matrix t_inv = t.inverse();
1408 Geom::Matrix result = t_inv * item->transform * t;
1410 if ((prefs_parallel || prefs_unmoved) && affine.isTranslation()) {
1411 // we need to cancel out the move compensation, too
1413 // find out the clone move, same as in sp_use_move_compensate
1414 Geom::Matrix parent = sp_use_get_parent_transform(SP_USE(item));
1415 Geom::Matrix clone_move = parent.inverse() * t * parent;
1417 if (prefs_parallel) {
1418 Geom::Matrix move = result * clone_move * t_inv;
1419 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &move, compensate);
1421 } else if (prefs_unmoved) {
1422 //if (SP_IS_USE(sp_use_get_original(SP_USE(item))))
1423 // clone_move = Geom::identity();
1424 Geom::Matrix move = result * clone_move;
1425 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &t, compensate);
1426 }
1428 } else {
1429 // just apply the result
1430 sp_item_write_transform(item, SP_OBJECT_REPR(item), result, &t, compensate);
1431 }
1433 } else {
1434 if (set_i2d) {
1435 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * (Geom::Matrix)affine);
1436 }
1437 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, NULL, compensate);
1438 }
1440 // if we're moving the actual object, not just updating the repr, we can transform the
1441 // center by the same matrix (only necessary for non-translations)
1442 if (set_i2d && item->isCenterSet() && !(affine.isTranslation() || affine.isIdentity())) {
1443 item->setCenter(old_center * affine);
1444 item->updateRepr();
1445 }
1446 }
1447 }
1449 void sp_selection_remove_transform(SPDesktop *desktop)
1450 {
1451 if (desktop == NULL)
1452 return;
1454 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1456 GSList const *l = (GSList *) selection->reprList();
1457 while (l != NULL) {
1458 ((Inkscape::XML::Node*)l->data)->setAttribute("transform", NULL, false);
1459 l = l->next;
1460 }
1462 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_FLATTEN,
1463 _("Remove transform"));
1464 }
1466 void
1467 sp_selection_scale_absolute(Inkscape::Selection *selection,
1468 double const x0, double const x1,
1469 double const y0, double const y1)
1470 {
1471 if (selection->isEmpty())
1472 return;
1474 Geom::OptRect const bbox(selection->bounds());
1475 if ( !bbox ) {
1476 return;
1477 }
1479 Geom::Translate const p2o(-bbox->min());
1481 Geom::Scale const newSize(x1 - x0,
1482 y1 - y0);
1483 Geom::Scale const scale( newSize * Geom::Scale(bbox->dimensions()).inverse() );
1484 Geom::Translate const o2n(x0, y0);
1485 Geom::Matrix const final( p2o * scale * o2n );
1487 sp_selection_apply_affine(selection, final);
1488 }
1491 void sp_selection_scale_relative(Inkscape::Selection *selection, Geom::Point const &align, Geom::Scale const &scale)
1492 {
1493 if (selection->isEmpty())
1494 return;
1496 Geom::OptRect const bbox(selection->bounds());
1498 if ( !bbox ) {
1499 return;
1500 }
1502 // FIXME: ARBITRARY LIMIT: don't try to scale above 1 Mpx, it won't display properly and will crash sooner or later anyway
1503 if ( bbox->dimensions()[Geom::X] * scale[Geom::X] > 1e6 ||
1504 bbox->dimensions()[Geom::Y] * scale[Geom::Y] > 1e6 )
1505 {
1506 return;
1507 }
1509 Geom::Translate const n2d(-align);
1510 Geom::Translate const d2n(align);
1511 Geom::Matrix const final( n2d * scale * d2n );
1512 sp_selection_apply_affine(selection, final);
1513 }
1515 void
1516 sp_selection_rotate_relative(Inkscape::Selection *selection, Geom::Point const ¢er, gdouble const angle_degrees)
1517 {
1518 Geom::Translate const d2n(center);
1519 Geom::Translate const n2d(-center);
1520 Geom::Rotate const rotate(Geom::Rotate::from_degrees(angle_degrees));
1521 Geom::Matrix const final( Geom::Matrix(n2d) * rotate * d2n );
1522 sp_selection_apply_affine(selection, final);
1523 }
1525 void
1526 sp_selection_skew_relative(Inkscape::Selection *selection, Geom::Point const &align, double dx, double dy)
1527 {
1528 Geom::Translate const d2n(align);
1529 Geom::Translate const n2d(-align);
1530 Geom::Matrix const skew(1, dy,
1531 dx, 1,
1532 0, 0);
1533 Geom::Matrix const final( n2d * skew * d2n );
1534 sp_selection_apply_affine(selection, final);
1535 }
1537 void sp_selection_move_relative(Inkscape::Selection *selection, Geom::Point const &move, bool compensate)
1538 {
1539 sp_selection_apply_affine(selection, Geom::Matrix(Geom::Translate(move)), true, compensate);
1540 }
1542 void sp_selection_move_relative(Inkscape::Selection *selection, double dx, double dy)
1543 {
1544 sp_selection_apply_affine(selection, Geom::Matrix(Geom::Translate(dx, dy)));
1545 }
1547 /**
1548 * @brief Rotates selected objects 90 degrees, either clock-wise or counter-clockwise, depending on the value of ccw
1549 */
1550 void sp_selection_rotate_90(SPDesktop *desktop, bool ccw)
1551 {
1552 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1554 if (selection->isEmpty())
1555 return;
1557 GSList const *l = selection->itemList();
1558 Geom::Rotate const rot_90(Geom::Point(0, ccw ? 1 : -1)); // pos. or neg. rotation, depending on the value of ccw
1559 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1560 SPItem *item = SP_ITEM(l2->data);
1561 sp_item_rotate_rel(item, rot_90);
1562 }
1564 sp_document_done(sp_desktop_document(desktop),
1565 ccw ? SP_VERB_OBJECT_ROTATE_90_CCW : SP_VERB_OBJECT_ROTATE_90_CW,
1566 ccw ? _("Rotate 90° CCW") : _("Rotate 90° CW"));
1567 }
1569 void
1570 sp_selection_rotate(Inkscape::Selection *selection, gdouble const angle_degrees)
1571 {
1572 if (selection->isEmpty())
1573 return;
1575 boost::optional<Geom::Point> center = selection->center();
1576 if (!center) {
1577 return;
1578 }
1580 sp_selection_rotate_relative(selection, *center, angle_degrees);
1582 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1583 ( ( angle_degrees > 0 )
1584 ? "selector:rotate:ccw"
1585 : "selector:rotate:cw" ),
1586 SP_VERB_CONTEXT_SELECT,
1587 _("Rotate"));
1588 }
1590 // helper function:
1591 static
1592 Geom::Point
1593 cornerFarthestFrom(Geom::Rect const &r, Geom::Point const &p){
1594 Geom::Point m = r.midpoint();
1595 unsigned i = 0;
1596 if (p[X] < m[X]) {
1597 i = 1;
1598 }
1599 if (p[Y] < m[Y]) {
1600 i = 3 - i;
1601 }
1602 return r.corner(i);
1603 }
1605 /**
1606 \param angle the angle in "angular pixels", i.e. how many visible pixels must move the outermost point of the rotated object
1607 */
1608 void
1609 sp_selection_rotate_screen(Inkscape::Selection *selection, gdouble angle)
1610 {
1611 if (selection->isEmpty())
1612 return;
1614 Geom::OptRect const bbox(selection->bounds());
1615 boost::optional<Geom::Point> center = selection->center();
1617 if ( !bbox || !center ) {
1618 return;
1619 }
1621 gdouble const zoom = selection->desktop()->current_zoom();
1622 gdouble const zmove = angle / zoom;
1623 gdouble const r = Geom::L2(cornerFarthestFrom(*bbox, *center) - *center);
1625 gdouble const zangle = 180 * atan2(zmove, r) / M_PI;
1627 sp_selection_rotate_relative(selection, *center, zangle);
1629 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1630 ( (angle > 0)
1631 ? "selector:rotate:ccw"
1632 : "selector:rotate:cw" ),
1633 SP_VERB_CONTEXT_SELECT,
1634 _("Rotate by pixels"));
1635 }
1637 void
1638 sp_selection_scale(Inkscape::Selection *selection, gdouble grow)
1639 {
1640 if (selection->isEmpty())
1641 return;
1643 Geom::OptRect const bbox(selection->bounds());
1644 if (!bbox) {
1645 return;
1646 }
1648 Geom::Point const center(bbox->midpoint());
1650 // you can't scale "do nizhe pola" (below zero)
1651 double const max_len = bbox->maxExtent();
1652 if ( max_len + grow <= 1e-3 ) {
1653 return;
1654 }
1656 double const times = 1.0 + grow / max_len;
1657 sp_selection_scale_relative(selection, center, Geom::Scale(times, times));
1659 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1660 ( (grow > 0)
1661 ? "selector:scale:larger"
1662 : "selector:scale:smaller" ),
1663 SP_VERB_CONTEXT_SELECT,
1664 _("Scale"));
1665 }
1667 void
1668 sp_selection_scale_screen(Inkscape::Selection *selection, gdouble grow_pixels)
1669 {
1670 sp_selection_scale(selection,
1671 grow_pixels / selection->desktop()->current_zoom());
1672 }
1674 void
1675 sp_selection_scale_times(Inkscape::Selection *selection, gdouble times)
1676 {
1677 if (selection->isEmpty())
1678 return;
1680 Geom::OptRect sel_bbox = selection->bounds();
1682 if (!sel_bbox) {
1683 return;
1684 }
1686 Geom::Point const center(sel_bbox->midpoint());
1687 sp_selection_scale_relative(selection, center, Geom::Scale(times, times));
1688 sp_document_done(sp_desktop_document(selection->desktop()), SP_VERB_CONTEXT_SELECT,
1689 _("Scale by whole factor"));
1690 }
1692 void
1693 sp_selection_move(SPDesktop *desktop, gdouble dx, gdouble dy)
1694 {
1695 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1696 if (selection->isEmpty()) {
1697 return;
1698 }
1700 sp_selection_move_relative(selection, dx, dy);
1702 if (dx == 0) {
1703 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical", SP_VERB_CONTEXT_SELECT,
1704 _("Move vertically"));
1705 } else if (dy == 0) {
1706 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal", SP_VERB_CONTEXT_SELECT,
1707 _("Move horizontally"));
1708 } else {
1709 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SELECT,
1710 _("Move"));
1711 }
1712 }
1714 void
1715 sp_selection_move_screen(SPDesktop *desktop, gdouble dx, gdouble dy)
1716 {
1717 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1718 if (selection->isEmpty()) {
1719 return;
1720 }
1722 // same as sp_selection_move but divide deltas by zoom factor
1723 gdouble const zoom = desktop->current_zoom();
1724 gdouble const zdx = dx / zoom;
1725 gdouble const zdy = dy / zoom;
1726 sp_selection_move_relative(selection, zdx, zdy);
1728 if (dx == 0) {
1729 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical", SP_VERB_CONTEXT_SELECT,
1730 _("Move vertically by pixels"));
1731 } else if (dy == 0) {
1732 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal", SP_VERB_CONTEXT_SELECT,
1733 _("Move horizontally by pixels"));
1734 } else {
1735 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SELECT,
1736 _("Move"));
1737 }
1738 }
1740 namespace {
1742 template <typename D>
1743 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
1744 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
1746 template <typename D>
1747 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items, SPObject *root,
1748 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
1750 struct Forward {
1751 typedef SPObject *Iterator;
1753 static Iterator children(SPObject *o) { return sp_object_first_child(o); }
1754 static Iterator siblings_after(SPObject *o) { return SP_OBJECT_NEXT(o); }
1755 static void dispose(Iterator /*i*/) {}
1757 static SPObject *object(Iterator i) { return i; }
1758 static Iterator next(Iterator i) { return SP_OBJECT_NEXT(i); }
1759 };
1761 struct Reverse {
1762 typedef GSList *Iterator;
1764 static Iterator children(SPObject *o) {
1765 return make_list(o->firstChild(), NULL);
1766 }
1767 static Iterator siblings_after(SPObject *o) {
1768 return make_list(SP_OBJECT_PARENT(o)->firstChild(), o);
1769 }
1770 static void dispose(Iterator i) {
1771 g_slist_free(i);
1772 }
1774 static SPObject *object(Iterator i) {
1775 return reinterpret_cast<SPObject *>(i->data);
1776 }
1777 static Iterator next(Iterator i) { return i->next; }
1779 private:
1780 static GSList *make_list(SPObject *object, SPObject *limit) {
1781 GSList *list=NULL;
1782 while ( object != limit ) {
1783 list = g_slist_prepend(list, object);
1784 object = SP_OBJECT_NEXT(object);
1785 }
1786 return list;
1787 }
1788 };
1790 }
1792 void
1793 sp_selection_item_next(SPDesktop *desktop)
1794 {
1795 g_return_if_fail(desktop != NULL);
1796 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1798 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1799 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs->getInt("/options/kbselection/inlayer", PREFS_SELECTION_LAYER);
1800 bool onlyvisible = prefs->getBool("/options/kbselection/onlyvisible", true);
1801 bool onlysensitive = prefs->getBool("/options/kbselection/onlysensitive", true);
1803 SPObject *root;
1804 if (PREFS_SELECTION_ALL != inlayer) {
1805 root = selection->activeContext();
1806 } else {
1807 root = desktop->currentRoot();
1808 }
1810 SPItem *item=next_item_from_list<Forward>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1812 if (item) {
1813 selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
1814 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1815 scroll_to_show_item(desktop, item);
1816 }
1817 }
1818 }
1820 void
1821 sp_selection_item_prev(SPDesktop *desktop)
1822 {
1823 SPDocument *document = sp_desktop_document(desktop);
1824 g_return_if_fail(document != NULL);
1825 g_return_if_fail(desktop != NULL);
1826 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1828 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1829 PrefsSelectionContext inlayer = (PrefsSelectionContext) prefs->getInt("/options/kbselection/inlayer", PREFS_SELECTION_LAYER);
1830 bool onlyvisible = prefs->getBool("/options/kbselection/onlyvisible", true);
1831 bool onlysensitive = prefs->getBool("/options/kbselection/onlysensitive", true);
1833 SPObject *root;
1834 if (PREFS_SELECTION_ALL != inlayer) {
1835 root = selection->activeContext();
1836 } else {
1837 root = desktop->currentRoot();
1838 }
1840 SPItem *item=next_item_from_list<Reverse>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1842 if (item) {
1843 selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
1844 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1845 scroll_to_show_item(desktop, item);
1846 }
1847 }
1848 }
1850 void sp_selection_next_patheffect_param(SPDesktop * dt)
1851 {
1852 if (!dt) return;
1854 Inkscape::Selection *selection = sp_desktop_selection(dt);
1855 if ( selection && !selection->isEmpty() ) {
1856 SPItem *item = selection->singleItem();
1857 if ( item && SP_IS_SHAPE(item)) {
1858 if (sp_lpe_item_has_path_effect(SP_LPE_ITEM(item))) {
1859 sp_lpe_item_edit_next_param_oncanvas(SP_LPE_ITEM(item), dt);
1860 } else {
1861 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("The selection has no applied path effect."));
1862 }
1863 }
1864 }
1865 }
1867 /*bool has_path_recursive(SPObject *obj)
1868 {
1869 if (!obj) return false;
1870 if (SP_IS_PATH(obj)) {
1871 return true;
1872 }
1873 if (SP_IS_GROUP(obj) || SP_IS_OBJECTGROUP(obj)) {
1874 for (SPObject *c = obj->children; c; c = c->next) {
1875 if (has_path_recursive(c)) return true;
1876 }
1877 }
1878 return false;
1879 }*/
1881 void sp_selection_edit_clip_or_mask(SPDesktop * /*dt*/, bool /*clip*/)
1882 {
1883 return;
1884 /*if (!dt) return;
1885 using namespace Inkscape::UI;
1887 Inkscape::Selection *selection = sp_desktop_selection(dt);
1888 if (!selection || selection->isEmpty()) return;
1890 GSList const *items = selection->itemList();
1891 bool has_path = false;
1892 for (GSList *i = const_cast<GSList*>(items); i; i= i->next) {
1893 SPItem *item = SP_ITEM(i->data);
1894 SPObject *search = clip
1895 ? SP_OBJECT(item->clip_ref ? item->clip_ref->getObject() : NULL)
1896 : SP_OBJECT(item->mask_ref ? item->mask_ref->getObject() : NULL);
1897 has_path |= has_path_recursive(search);
1898 if (has_path) break;
1899 }
1900 if (has_path) {
1901 if (!tools_isactive(dt, TOOLS_NODES)) {
1902 tools_switch(dt, TOOLS_NODES);
1903 }
1904 ink_node_tool_set_mode(INK_NODE_TOOL(dt->event_context),
1905 clip ? NODE_TOOL_EDIT_CLIPPING_PATHS : NODE_TOOL_EDIT_MASKS);
1906 } else if (clip) {
1907 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1908 _("The selection has no applied clip path."));
1909 } else {
1910 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1911 _("The selection has no applied mask."));
1912 }*/
1913 }
1916 namespace {
1918 template <typename D>
1919 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items,
1920 SPObject *root, bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
1921 {
1922 SPObject *current=root;
1923 while (items) {
1924 SPItem *item=SP_ITEM(items->data);
1925 if ( root->isAncestorOf(item) &&
1926 ( !only_in_viewport || desktop->isWithinViewport(item) ) )
1927 {
1928 current = item;
1929 break;
1930 }
1931 items = items->next;
1932 }
1934 GSList *path=NULL;
1935 while ( current != root ) {
1936 path = g_slist_prepend(path, current);
1937 current = SP_OBJECT_PARENT(current);
1938 }
1940 SPItem *next;
1941 // first, try from the current object
1942 next = next_item<D>(desktop, path, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1943 g_slist_free(path);
1945 if (!next) { // if we ran out of objects, start over at the root
1946 next = next_item<D>(desktop, NULL, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1947 }
1949 return next;
1950 }
1952 template <typename D>
1953 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
1954 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
1955 {
1956 typename D::Iterator children;
1957 typename D::Iterator iter;
1959 SPItem *found=NULL;
1961 if (path) {
1962 SPObject *object=reinterpret_cast<SPObject *>(path->data);
1963 g_assert(SP_OBJECT_PARENT(object) == root);
1964 if (desktop->isLayer(object)) {
1965 found = next_item<D>(desktop, path->next, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1966 }
1967 iter = children = D::siblings_after(object);
1968 } else {
1969 iter = children = D::children(root);
1970 }
1972 while ( iter && !found ) {
1973 SPObject *object=D::object(iter);
1974 if (desktop->isLayer(object)) {
1975 if (PREFS_SELECTION_LAYER != inlayer) { // recurse into sublayers
1976 found = next_item<D>(desktop, NULL, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1977 }
1978 } else if ( SP_IS_ITEM(object) &&
1979 ( !only_in_viewport || desktop->isWithinViewport(SP_ITEM(object)) ) &&
1980 ( !onlyvisible || !desktop->itemIsHidden(SP_ITEM(object))) &&
1981 ( !onlysensitive || !SP_ITEM(object)->isLocked()) &&
1982 !desktop->isLayer(SP_ITEM(object)) )
1983 {
1984 found = SP_ITEM(object);
1985 }
1986 iter = D::next(iter);
1987 }
1989 D::dispose(children);
1991 return found;
1992 }
1994 }
1996 /**
1997 * If \a item is not entirely visible then adjust visible area to centre on the centre on of
1998 * \a item.
1999 */
2000 void scroll_to_show_item(SPDesktop *desktop, SPItem *item)
2001 {
2002 Geom::Rect dbox = desktop->get_display_area();
2003 Geom::OptRect sbox = sp_item_bbox_desktop(item);
2005 if ( sbox && dbox.contains(*sbox) == false ) {
2006 Geom::Point const s_dt = sbox->midpoint();
2007 Geom::Point const s_w = desktop->d2w(s_dt);
2008 Geom::Point const d_dt = dbox.midpoint();
2009 Geom::Point const d_w = desktop->d2w(d_dt);
2010 Geom::Point const moved_w( d_w - s_w );
2011 gint const dx = (gint) moved_w[X];
2012 gint const dy = (gint) moved_w[Y];
2013 desktop->scroll_world(dx, dy);
2014 }
2015 }
2018 void
2019 sp_selection_clone(SPDesktop *desktop)
2020 {
2021 if (desktop == NULL)
2022 return;
2024 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2026 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
2028 // check if something is selected
2029 if (selection->isEmpty()) {
2030 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object</b> to clone."));
2031 return;
2032 }
2034 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
2036 selection->clear();
2038 // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
2039 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
2041 GSList *newsel = NULL;
2043 while (reprs) {
2044 Inkscape::XML::Node *sel_repr = (Inkscape::XML::Node *) reprs->data;
2045 Inkscape::XML::Node *parent = sp_repr_parent(sel_repr);
2047 Inkscape::XML::Node *clone = xml_doc->createElement("svg:use");
2048 clone->setAttribute("x", "0", false);
2049 clone->setAttribute("y", "0", false);
2050 clone->setAttribute("xlink:href", g_strdup_printf("#%s", sel_repr->attribute("id")), false);
2052 clone->setAttribute("inkscape:transform-center-x", sel_repr->attribute("inkscape:transform-center-x"), false);
2053 clone->setAttribute("inkscape:transform-center-y", sel_repr->attribute("inkscape:transform-center-y"), false);
2055 // add the new clone to the top of the original's parent
2056 parent->appendChild(clone);
2058 newsel = g_slist_prepend(newsel, clone);
2059 reprs = g_slist_remove(reprs, sel_repr);
2060 Inkscape::GC::release(clone);
2061 }
2063 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_CLONE,
2064 C_("Action","Clone"));
2066 selection->setReprList(newsel);
2068 g_slist_free(newsel);
2069 }
2071 void
2072 sp_selection_relink(SPDesktop *desktop)
2073 {
2074 if (!desktop)
2075 return;
2077 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2079 if (selection->isEmpty()) {
2080 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>clones</b> to relink."));
2081 return;
2082 }
2084 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
2085 const gchar *newid = cm->getFirstObjectID();
2086 if (!newid) {
2087 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Copy an <b>object</b> to clipboard to relink clones to."));
2088 return;
2089 }
2090 gchar *newref = g_strdup_printf("#%s", newid);
2092 // Get a copy of current selection.
2093 bool relinked = false;
2094 for (GSList *items = (GSList *) selection->itemList();
2095 items != NULL;
2096 items = items->next)
2097 {
2098 SPItem *item = (SPItem *) items->data;
2100 if (!SP_IS_USE(item))
2101 continue;
2103 SP_OBJECT_REPR(item)->setAttribute("xlink:href", newref);
2104 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
2105 relinked = true;
2106 }
2108 g_free(newref);
2110 if (!relinked) {
2111 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No clones to relink</b> in the selection."));
2112 } else {
2113 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNLINK_CLONE,
2114 _("Relink clone"));
2115 }
2116 }
2119 void
2120 sp_selection_unlink(SPDesktop *desktop)
2121 {
2122 if (!desktop)
2123 return;
2125 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2127 if (selection->isEmpty()) {
2128 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>clones</b> to unlink."));
2129 return;
2130 }
2132 // Get a copy of current selection.
2133 GSList *new_select = NULL;
2134 bool unlinked = false;
2135 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2136 items != NULL;
2137 items = items->next)
2138 {
2139 SPItem *item = (SPItem *) items->data;
2141 if (SP_IS_TEXT(item)) {
2142 SPObject *tspan = sp_tref_convert_to_tspan(item);
2144 if (tspan) {
2145 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
2146 }
2148 // Set unlink to true, and fall into the next if which
2149 // will include this text item in the new selection
2150 unlinked = true;
2151 }
2153 if (!(SP_IS_USE(item) || SP_IS_TREF(item))) {
2154 // keep the non-use item in the new selection
2155 new_select = g_slist_prepend(new_select, item);
2156 continue;
2157 }
2159 SPItem *unlink;
2160 if (SP_IS_USE(item)) {
2161 unlink = sp_use_unlink(SP_USE(item));
2162 // Unable to unlink use (external or invalid href?)
2163 if (!unlink) {
2164 new_select = g_slist_prepend(new_select, item);
2165 continue;
2166 }
2167 } else /*if (SP_IS_TREF(use))*/ {
2168 unlink = SP_ITEM(sp_tref_convert_to_tspan(item));
2169 }
2171 unlinked = true;
2172 // Add ungrouped items to the new selection.
2173 new_select = g_slist_prepend(new_select, unlink);
2174 }
2176 if (new_select) { // set new selection
2177 selection->clear();
2178 selection->setList(new_select);
2179 g_slist_free(new_select);
2180 }
2181 if (!unlinked) {
2182 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No clones to unlink</b> in the selection."));
2183 }
2185 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNLINK_CLONE,
2186 _("Unlink clone"));
2187 }
2189 void
2190 sp_select_clone_original(SPDesktop *desktop)
2191 {
2192 if (desktop == NULL)
2193 return;
2195 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2197 SPItem *item = selection->singleItem();
2199 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.");
2201 // Check if other than two objects are selected
2202 if (g_slist_length((GSList *) selection->itemList()) != 1 || !item) {
2203 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
2204 return;
2205 }
2207 SPItem *original = NULL;
2208 if (SP_IS_USE(item)) {
2209 original = sp_use_get_original(SP_USE(item));
2210 } else if (SP_IS_OFFSET(item) && SP_OFFSET(item)->sourceHref) {
2211 original = sp_offset_get_source(SP_OFFSET(item));
2212 } else if (SP_IS_TEXT_TEXTPATH(item)) {
2213 original = sp_textpath_get_path_item(SP_TEXTPATH(sp_object_first_child(item)));
2214 } else if (SP_IS_FLOWTEXT(item)) {
2215 original = SP_FLOWTEXT(item)->get_frame(NULL); // first frame only
2216 } else { // it's an object that we don't know what to do with
2217 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
2218 return;
2219 }
2221 if (!original) {
2222 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>Cannot find</b> the object to select (orphaned clone, offset, textpath, flowed text?)"));
2223 return;
2224 }
2226 for (SPObject *o = original; o && !SP_IS_ROOT(o); o = SP_OBJECT_PARENT(o)) {
2227 if (SP_IS_DEFS(o)) {
2228 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("The object you're trying to select is <b>not visible</b> (it is in <defs>)"));
2229 return;
2230 }
2231 }
2233 if (original) {
2234 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
2235 bool highlight = prefs->getBool("/options/highlightoriginal/value");
2236 if (highlight) {
2237 Geom::OptRect a = item->getBounds(sp_item_i2d_affine(item));
2238 Geom::OptRect b = original->getBounds(sp_item_i2d_affine(original));
2239 if ( a && b ) {
2240 // draw a flashing line between the objects
2241 SPCurve *curve = new SPCurve();
2242 curve->moveto(a->midpoint());
2243 curve->lineto(b->midpoint());
2245 SPCanvasItem * canvasitem = sp_canvas_bpath_new(sp_desktop_tempgroup(desktop), curve);
2246 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(canvasitem), 0x0000ddff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT, 5, 3);
2247 sp_canvas_item_show(canvasitem);
2248 curve->unref();
2249 desktop->add_temporary_canvasitem(canvasitem, 1000);
2250 }
2251 }
2253 selection->clear();
2254 selection->set(original);
2255 if (SP_CYCLING == SP_CYCLE_FOCUS) {
2256 scroll_to_show_item(desktop, original);
2257 }
2258 }
2259 }
2262 void sp_selection_to_marker(SPDesktop *desktop, bool apply)
2263 {
2264 if (desktop == NULL)
2265 return;
2267 SPDocument *doc = sp_desktop_document(desktop);
2268 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2270 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2272 // check if something is selected
2273 if (selection->isEmpty()) {
2274 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to marker."));
2275 return;
2276 }
2278 sp_document_ensure_up_to_date(doc);
2279 Geom::OptRect r = selection->bounds(SPItem::RENDERING_BBOX);
2280 boost::optional<Geom::Point> c = selection->center();
2281 if ( !r || !c ) {
2282 return;
2283 }
2285 // calculate the transform to be applied to objects to move them to 0,0
2286 Geom::Point move_p = Geom::Point(0, sp_document_height(doc)) - *c;
2287 move_p[Geom::Y] = -move_p[Geom::Y];
2288 Geom::Matrix move = Geom::Matrix(Geom::Translate(move_p));
2290 GSList *items = g_slist_copy((GSList *) selection->itemList());
2292 items = g_slist_sort(items, (GCompareFunc) sp_object_compare_position);
2294 // bottommost object, after sorting
2295 SPObject *parent = SP_OBJECT_PARENT(items->data);
2297 Geom::Matrix parent_transform(sp_item_i2doc_affine(SP_ITEM(parent)));
2299 // remember the position of the first item
2300 gint pos = SP_OBJECT_REPR(items->data)->position();
2301 (void)pos; // TODO check why this was remembered
2303 // create a list of duplicates
2304 GSList *repr_copies = NULL;
2305 for (GSList *i = items; i != NULL; i = i->next) {
2306 Inkscape::XML::Node *dup = (SP_OBJECT_REPR(i->data))->duplicate(xml_doc);
2307 repr_copies = g_slist_prepend(repr_copies, dup);
2308 }
2310 Geom::Rect bounds(desktop->dt2doc(r->min()), desktop->dt2doc(r->max()));
2312 if (apply) {
2313 // delete objects so that their clones don't get alerted; this object will be restored shortly
2314 for (GSList *i = items; i != NULL; i = i->next) {
2315 SPObject *item = reinterpret_cast<SPObject*>(i->data);
2316 item->deleteObject(false);
2317 }
2318 }
2320 // Hack: Temporarily set clone compensation to unmoved, so that we can move clone-originals
2321 // without disturbing clones.
2322 // See ActorAlign::on_button_click() in src/ui/dialog/align-and-distribute.cpp
2323 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
2324 int saved_compensation = prefs->getInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
2325 prefs->setInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
2327 gchar const *mark_id = generate_marker(repr_copies, bounds, doc,
2328 ( Geom::Matrix(Geom::Translate(desktop->dt2doc(
2329 Geom::Point(r->min()[Geom::X],
2330 r->max()[Geom::Y]))))
2331 * parent_transform.inverse() ),
2332 parent_transform * move);
2333 (void)mark_id;
2335 // restore compensation setting
2336 prefs->setInt("/options/clonecompensation/value", saved_compensation);
2339 g_slist_free(items);
2341 sp_document_done(doc, SP_VERB_EDIT_SELECTION_2_MARKER,
2342 _("Objects to marker"));
2343 }
2345 static void sp_selection_to_guides_recursive(SPItem *item, bool deleteitem, bool wholegroups) {
2346 if (SP_IS_GROUP(item) && !SP_IS_BOX3D(item) && !wholegroups) {
2347 for (GSList *i = sp_item_group_item_list(SP_GROUP(item)); i != NULL; i = i->next) {
2348 sp_selection_to_guides_recursive(SP_ITEM(i->data), deleteitem, wholegroups);
2349 }
2350 } else {
2351 sp_item_convert_item_to_guides(item);
2353 if (deleteitem) {
2354 item->deleteObject(true);
2355 }
2356 }
2357 }
2359 void sp_selection_to_guides(SPDesktop *desktop)
2360 {
2361 if (desktop == NULL)
2362 return;
2364 SPDocument *doc = sp_desktop_document(desktop);
2365 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2366 // we need to copy the list because it gets reset when objects are deleted
2367 GSList *items = g_slist_copy((GSList *) selection->itemList());
2369 if (!items) {
2370 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to guides."));
2371 return;
2372 }
2374 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
2375 bool deleteitem = !prefs->getBool("/tools/cvg_keep_objects", 0);
2376 bool wholegroups = prefs->getBool("/tools/cvg_convert_whole_groups", 0);
2378 for (GSList const *i = items; i != NULL; i = i->next) {
2379 sp_selection_to_guides_recursive(SP_ITEM(i->data), deleteitem, wholegroups);
2380 }
2382 sp_document_done(doc, SP_VERB_EDIT_SELECTION_2_GUIDES, _("Objects to guides"));
2383 }
2385 void
2386 sp_selection_tile(SPDesktop *desktop, bool apply)
2387 {
2388 if (desktop == NULL)
2389 return;
2391 SPDocument *doc = sp_desktop_document(desktop);
2392 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2394 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2396 // check if something is selected
2397 if (selection->isEmpty()) {
2398 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to pattern."));
2399 return;
2400 }
2402 sp_document_ensure_up_to_date(doc);
2403 Geom::OptRect r = selection->bounds(SPItem::RENDERING_BBOX);
2404 if ( !r ) {
2405 return;
2406 }
2408 // calculate the transform to be applied to objects to move them to 0,0
2409 Geom::Point move_p = Geom::Point(0, sp_document_height(doc)) - (r->min() + Geom::Point(0, r->dimensions()[Geom::Y]));
2410 move_p[Geom::Y] = -move_p[Geom::Y];
2411 Geom::Matrix move = Geom::Matrix(Geom::Translate(move_p));
2413 GSList *items = g_slist_copy((GSList *) selection->itemList());
2415 items = g_slist_sort(items, (GCompareFunc) sp_object_compare_position);
2417 // bottommost object, after sorting
2418 SPObject *parent = SP_OBJECT_PARENT(items->data);
2420 Geom::Matrix parent_transform(sp_item_i2doc_affine(SP_ITEM(parent)));
2422 // remember the position of the first item
2423 gint pos = SP_OBJECT_REPR(items->data)->position();
2425 // create a list of duplicates
2426 GSList *repr_copies = NULL;
2427 for (GSList *i = items; i != NULL; i = i->next) {
2428 Inkscape::XML::Node *dup = (SP_OBJECT_REPR(i->data))->duplicate(xml_doc);
2429 repr_copies = g_slist_prepend(repr_copies, dup);
2430 }
2431 // restore the z-order after prepends
2432 repr_copies = g_slist_reverse(repr_copies);
2434 Geom::Rect bounds(desktop->dt2doc(r->min()), desktop->dt2doc(r->max()));
2436 if (apply) {
2437 // delete objects so that their clones don't get alerted; this object will be restored shortly
2438 for (GSList *i = items; i != NULL; i = i->next) {
2439 SPObject *item = reinterpret_cast<SPObject*>(i->data);
2440 item->deleteObject(false);
2441 }
2442 }
2444 // Hack: Temporarily set clone compensation to unmoved, so that we can move clone-originals
2445 // without disturbing clones.
2446 // See ActorAlign::on_button_click() in src/ui/dialog/align-and-distribute.cpp
2447 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
2448 int saved_compensation = prefs->getInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
2449 prefs->setInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
2451 gchar const *pat_id = pattern_tile(repr_copies, bounds, doc,
2452 ( Geom::Matrix(Geom::Translate(desktop->dt2doc(Geom::Point(r->min()[Geom::X],
2453 r->max()[Geom::Y]))))
2454 * to_2geom(parent_transform.inverse()) ),
2455 parent_transform * move);
2457 // restore compensation setting
2458 prefs->setInt("/options/clonecompensation/value", saved_compensation);
2460 if (apply) {
2461 Inkscape::XML::Node *rect = xml_doc->createElement("svg:rect");
2462 rect->setAttribute("style", g_strdup_printf("stroke:none;fill:url(#%s)", pat_id));
2464 Geom::Point min = bounds.min() * to_2geom(parent_transform.inverse());
2465 Geom::Point max = bounds.max() * to_2geom(parent_transform.inverse());
2467 sp_repr_set_svg_double(rect, "width", max[Geom::X] - min[Geom::X]);
2468 sp_repr_set_svg_double(rect, "height", max[Geom::Y] - min[Geom::Y]);
2469 sp_repr_set_svg_double(rect, "x", min[Geom::X]);
2470 sp_repr_set_svg_double(rect, "y", min[Geom::Y]);
2472 // restore parent and position
2473 SP_OBJECT_REPR(parent)->appendChild(rect);
2474 rect->setPosition(pos > 0 ? pos : 0);
2475 SPItem *rectangle = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(rect);
2477 Inkscape::GC::release(rect);
2479 selection->clear();
2480 selection->set(rectangle);
2481 }
2483 g_slist_free(items);
2485 sp_document_done(doc, SP_VERB_EDIT_TILE,
2486 _("Objects to pattern"));
2487 }
2489 void
2490 sp_selection_untile(SPDesktop *desktop)
2491 {
2492 if (desktop == NULL)
2493 return;
2495 SPDocument *doc = sp_desktop_document(desktop);
2496 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2498 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2500 // check if something is selected
2501 if (selection->isEmpty()) {
2502 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object with pattern fill</b> to extract objects from."));
2503 return;
2504 }
2506 GSList *new_select = NULL;
2508 bool did = false;
2510 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2511 items != NULL;
2512 items = items->next) {
2514 SPItem *item = (SPItem *) items->data;
2516 SPStyle *style = item->style;
2518 if (!style || !style->fill.isPaintserver())
2519 continue;
2521 SPPaintServer *server = item->style->getFillPaintServer();
2523 if (!SP_IS_PATTERN(server))
2524 continue;
2526 did = true;
2528 SPPattern *pattern = pattern_getroot(SP_PATTERN(server));
2530 Geom::Matrix pat_transform = to_2geom(pattern_patternTransform(SP_PATTERN(server)));
2531 pat_transform *= item->transform;
2533 for (SPObject *child = sp_object_first_child(pattern) ; child != NULL; child = child->next ) {
2534 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate(xml_doc);
2535 SPItem *i = SP_ITEM(desktop->currentLayer()->appendChildRepr(copy));
2537 // FIXME: relink clones to the new canvas objects
2538 // use SPObject::setid when mental finishes it to steal ids of
2540 // this is needed to make sure the new item has curve (simply requestDisplayUpdate does not work)
2541 sp_document_ensure_up_to_date(doc);
2543 Geom::Matrix transform( i->transform * pat_transform );
2544 sp_item_write_transform(i, SP_OBJECT_REPR(i), transform);
2546 new_select = g_slist_prepend(new_select, i);
2547 }
2549 SPCSSAttr *css = sp_repr_css_attr_new();
2550 sp_repr_css_set_property(css, "fill", "none");
2551 sp_repr_css_change(SP_OBJECT_REPR(item), css, "style");
2552 }
2554 if (!did) {
2555 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No pattern fills</b> in the selection."));
2556 } else {
2557 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNTILE,
2558 _("Pattern to objects"));
2559 selection->setList(new_select);
2560 }
2561 }
2563 void
2564 sp_selection_get_export_hints(Inkscape::Selection *selection, char const **filename, float *xdpi, float *ydpi)
2565 {
2566 if (selection->isEmpty()) {
2567 return;
2568 }
2570 GSList const *reprlst = selection->reprList();
2571 bool filename_search = TRUE;
2572 bool xdpi_search = TRUE;
2573 bool ydpi_search = TRUE;
2575 for (; reprlst != NULL &&
2576 filename_search &&
2577 xdpi_search &&
2578 ydpi_search;
2579 reprlst = reprlst->next) {
2580 gchar const *dpi_string;
2581 Inkscape::XML::Node * repr = (Inkscape::XML::Node *)reprlst->data;
2583 if (filename_search) {
2584 *filename = repr->attribute("inkscape:export-filename");
2585 if (*filename != NULL)
2586 filename_search = FALSE;
2587 }
2589 if (xdpi_search) {
2590 dpi_string = NULL;
2591 dpi_string = repr->attribute("inkscape:export-xdpi");
2592 if (dpi_string != NULL) {
2593 *xdpi = atof(dpi_string);
2594 xdpi_search = FALSE;
2595 }
2596 }
2598 if (ydpi_search) {
2599 dpi_string = NULL;
2600 dpi_string = repr->attribute("inkscape:export-ydpi");
2601 if (dpi_string != NULL) {
2602 *ydpi = atof(dpi_string);
2603 ydpi_search = FALSE;
2604 }
2605 }
2606 }
2607 }
2609 void
2610 sp_document_get_export_hints(SPDocument *doc, char const **filename, float *xdpi, float *ydpi)
2611 {
2612 Inkscape::XML::Node * repr = sp_document_repr_root(doc);
2613 gchar const *dpi_string;
2615 *filename = repr->attribute("inkscape:export-filename");
2617 dpi_string = NULL;
2618 dpi_string = repr->attribute("inkscape:export-xdpi");
2619 if (dpi_string != NULL) {
2620 *xdpi = atof(dpi_string);
2621 }
2623 dpi_string = NULL;
2624 dpi_string = repr->attribute("inkscape:export-ydpi");
2625 if (dpi_string != NULL) {
2626 *ydpi = atof(dpi_string);
2627 }
2628 }
2630 void
2631 sp_selection_create_bitmap_copy(SPDesktop *desktop)
2632 {
2633 if (desktop == NULL)
2634 return;
2636 SPDocument *document = sp_desktop_document(desktop);
2637 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
2639 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2641 // check if something is selected
2642 if (selection->isEmpty()) {
2643 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to make a bitmap copy."));
2644 return;
2645 }
2647 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Rendering bitmap..."));
2648 // set "busy" cursor
2649 desktop->setWaitingCursor();
2651 // Get the bounding box of the selection
2652 NRRect bbox;
2653 sp_document_ensure_up_to_date(document);
2654 selection->bounds(&bbox);
2655 if (NR_RECT_DFLS_TEST_EMPTY(&bbox)) {
2656 desktop->clearWaitingCursor();
2657 return; // exceptional situation, so not bother with a translatable error message, just quit quietly
2658 }
2660 // List of the items to show; all others will be hidden
2661 GSList *items = g_slist_copy((GSList *) selection->itemList());
2663 // Sort items so that the topmost comes last
2664 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
2666 // Generate a random value from the current time (you may create bitmap from the same object(s)
2667 // multiple times, and this is done so that they don't clash)
2668 GTimeVal cu;
2669 g_get_current_time(&cu);
2670 guint current = (int) (cu.tv_sec * 1000000 + cu.tv_usec) % 1024;
2672 // Create the filename.
2673 gchar *const basename = g_strdup_printf("%s-%s-%u.png",
2674 document->name,
2675 SP_OBJECT_REPR(items->data)->attribute("id"),
2676 current);
2677 // Imagemagick is known not to handle spaces in filenames, so we replace anything but letters,
2678 // digits, and a few other chars, with "_"
2679 g_strcanon(basename, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.=+~$#@^&!?", '_');
2681 // Build the complete path by adding document base dir, if set, otherwise home dir
2682 gchar * directory = NULL;
2683 if (SP_DOCUMENT_URI(document)) {
2684 directory = g_dirname(SP_DOCUMENT_URI(document));
2685 }
2686 if (directory == NULL) {
2687 directory = homedir_path(NULL);
2688 }
2689 gchar *filepath = g_build_filename(directory, basename, NULL);
2691 //g_print("%s\n", filepath);
2693 // Remember parent and z-order of the topmost one
2694 gint pos = SP_OBJECT_REPR(g_slist_last(items)->data)->position();
2695 SPObject *parent_object = SP_OBJECT_PARENT(g_slist_last(items)->data);
2696 Inkscape::XML::Node *parent = SP_OBJECT_REPR(parent_object);
2698 // Calculate resolution
2699 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
2700 double res;
2701 int const prefs_res = prefs->getInt("/options/createbitmap/resolution", 0);
2702 int const prefs_min = prefs->getInt("/options/createbitmap/minsize", 0);
2703 if (0 < prefs_res) {
2704 // If it's given explicitly in prefs, take it
2705 res = prefs_res;
2706 } else if (0 < prefs_min) {
2707 // If minsize is given, look up minimum bitmap size (default 250 pixels) and calculate resolution from it
2708 res = PX_PER_IN * prefs_min / MIN((bbox.x1 - bbox.x0), (bbox.y1 - bbox.y0));
2709 } else {
2710 float hint_xdpi = 0, hint_ydpi = 0;
2711 char const *hint_filename;
2712 // take resolution hint from the selected objects
2713 sp_selection_get_export_hints(selection, &hint_filename, &hint_xdpi, &hint_ydpi);
2714 if (hint_xdpi != 0) {
2715 res = hint_xdpi;
2716 } else {
2717 // take resolution hint from the document
2718 sp_document_get_export_hints(document, &hint_filename, &hint_xdpi, &hint_ydpi);
2719 if (hint_xdpi != 0) {
2720 res = hint_xdpi;
2721 } else {
2722 // if all else fails, take the default 90 dpi
2723 res = PX_PER_IN;
2724 }
2725 }
2726 }
2728 // The width and height of the bitmap in pixels
2729 unsigned width = (unsigned) floor((bbox.x1 - bbox.x0) * res / PX_PER_IN);
2730 unsigned height =(unsigned) floor((bbox.y1 - bbox.y0) * res / PX_PER_IN);
2732 // Find out if we have to run an external filter
2733 gchar const *run = NULL;
2734 Glib::ustring filter = prefs->getString("/options/createbitmap/filter");
2735 if (!filter.empty()) {
2736 // filter command is given;
2737 // see if we have a parameter to pass to it
2738 Glib::ustring param1 = prefs->getString("/options/createbitmap/filter_param1");
2739 if (!param1.empty()) {
2740 if (param1[param1.length() - 1] == '%') {
2741 // if the param string ends with %, interpret it as a percentage of the image's max dimension
2742 gchar p1[256];
2743 g_ascii_dtostr(p1, 256, ceil(g_ascii_strtod(param1.data(), NULL) * MAX(width, height) / 100));
2744 // the first param is always the image filename, the second is param1
2745 run = g_strdup_printf("%s \"%s\" %s", filter.data(), filepath, p1);
2746 } else {
2747 // otherwise pass the param1 unchanged
2748 run = g_strdup_printf("%s \"%s\" %s", filter.data(), filepath, param1.data());
2749 }
2750 } else {
2751 // run without extra parameter
2752 run = g_strdup_printf("%s \"%s\"", filter.data(), filepath);
2753 }
2754 }
2756 // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
2757 Geom::Matrix eek(sp_item_i2d_affine(SP_ITEM(parent_object)));
2758 Geom::Matrix t;
2760 double shift_x = bbox.x0;
2761 double shift_y = bbox.y1;
2762 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
2763 shift_x = round(shift_x);
2764 shift_y = -round(-shift_y); // this gets correct rounding despite coordinate inversion, remove the negations when the inversion is gone
2765 }
2766 t = Geom::Scale(1, -1) * Geom::Translate(shift_x, shift_y) * eek.inverse();
2768 // Do the export
2769 sp_export_png_file(document, filepath,
2770 bbox.x0, bbox.y0, bbox.x1, bbox.y1,
2771 width, height, res, res,
2772 (guint32) 0xffffff00,
2773 NULL, NULL,
2774 true, /*bool force_overwrite,*/
2775 items);
2777 g_slist_free(items);
2779 // Run filter, if any
2780 if (run) {
2781 g_print("Running external filter: %s\n", run);
2782 int retval;
2783 retval = system(run);
2784 }
2786 // Import the image back
2787 GdkPixbuf *pb = gdk_pixbuf_new_from_file(filepath, NULL);
2788 if (pb) {
2789 // Create the repr for the image
2790 Inkscape::XML::Node * repr = xml_doc->createElement("svg:image");
2791 sp_embed_image(repr, pb, "image/png");
2792 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
2793 sp_repr_set_svg_double(repr, "width", width);
2794 sp_repr_set_svg_double(repr, "height", height);
2795 } else {
2796 sp_repr_set_svg_double(repr, "width", (bbox.x1 - bbox.x0));
2797 sp_repr_set_svg_double(repr, "height", (bbox.y1 - bbox.y0));
2798 }
2800 // Write transform
2801 gchar *c=sp_svg_transform_write(t);
2802 repr->setAttribute("transform", c);
2803 g_free(c);
2805 // add the new repr to the parent
2806 parent->appendChild(repr);
2808 // move to the saved position
2809 repr->setPosition(pos > 0 ? pos + 1 : 1);
2811 // Set selection to the new image
2812 selection->clear();
2813 selection->add(repr);
2815 // Clean up
2816 Inkscape::GC::release(repr);
2817 gdk_pixbuf_unref(pb);
2819 // Complete undoable transaction
2820 sp_document_done(document, SP_VERB_SELECTION_CREATE_BITMAP,
2821 _("Create bitmap"));
2822 }
2824 desktop->clearWaitingCursor();
2826 g_free(basename);
2827 g_free(filepath);
2828 }
2830 /**
2831 * \brief Creates a mask or clipPath from selection
2832 * Two different modes:
2833 * if applyToLayer, all selection is moved to DEFS as mask/clippath
2834 * and is applied to current layer
2835 * otherwise, topmost object is used as mask for other objects
2836 * If \a apply_clip_path parameter is true, clipPath is created, otherwise mask
2837 *
2838 */
2839 void
2840 sp_selection_set_mask(SPDesktop *desktop, bool apply_clip_path, bool apply_to_layer)
2841 {
2842 if (desktop == NULL)
2843 return;
2845 SPDocument *doc = sp_desktop_document(desktop);
2846 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2848 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2850 // check if something is selected
2851 bool is_empty = selection->isEmpty();
2852 if ( apply_to_layer && is_empty) {
2853 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to create clippath or mask from."));
2854 return;
2855 } else if (!apply_to_layer && ( is_empty || NULL == selection->itemList()->next )) {
2856 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select mask object and <b>object(s)</b> to apply clippath or mask to."));
2857 return;
2858 }
2860 // FIXME: temporary patch to prevent crash!
2861 // Remove this when bboxes are fixed to not blow up on an item clipped/masked with its own clone
2862 bool clone_with_original = selection_contains_both_clone_and_original(selection);
2863 if (clone_with_original) {
2864 return; // in this version, you cannot clip/mask an object with its own clone
2865 }
2866 // /END FIXME
2868 sp_document_ensure_up_to_date(doc);
2870 GSList *items = g_slist_copy((GSList *) selection->itemList());
2872 items = g_slist_sort(items, (GCompareFunc) sp_object_compare_position);
2874 // See lp bug #542004
2875 selection->clear();
2877 // create a list of duplicates
2878 GSList *mask_items = NULL;
2879 GSList *apply_to_items = NULL;
2880 GSList *items_to_delete = NULL;
2881 GSList *items_to_select = NULL;
2883 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
2884 bool topmost = prefs->getBool("/options/maskobject/topmost", true);
2885 bool remove_original = prefs->getBool("/options/maskobject/remove", true);
2886 int grouping = prefs->getInt("/options/maskobject/grouping", PREFS_MASKOBJECT_GROUPING_NONE);
2888 if (apply_to_layer) {
2889 // all selected items are used for mask, which is applied to a layer
2890 apply_to_items = g_slist_prepend(apply_to_items, desktop->currentLayer());
2892 for (GSList *i = items; i != NULL; i = i->next) {
2893 Inkscape::XML::Node *dup = (SP_OBJECT_REPR(i->data))->duplicate(xml_doc);
2894 mask_items = g_slist_prepend(mask_items, dup);
2896 SPObject *item = reinterpret_cast<SPObject*>(i->data);
2897 if (remove_original) {
2898 items_to_delete = g_slist_prepend(items_to_delete, item);
2899 }
2900 else {
2901 items_to_select = g_slist_prepend(items_to_select, item);
2902 }
2903 }
2904 } else if (!topmost) {
2905 // topmost item is used as a mask, which is applied to other items in a selection
2906 GSList *i = items;
2907 Inkscape::XML::Node *dup = (SP_OBJECT_REPR(i->data))->duplicate(xml_doc);
2908 mask_items = g_slist_prepend(mask_items, dup);
2910 if (remove_original) {
2911 SPObject *item = reinterpret_cast<SPObject*>(i->data);
2912 items_to_delete = g_slist_prepend(items_to_delete, item);
2913 }
2915 for (i = i->next; i != NULL; i = i->next) {
2916 apply_to_items = g_slist_prepend(apply_to_items, i->data);
2917 items_to_select = g_slist_prepend(items_to_select, i->data);
2918 }
2919 } else {
2920 GSList *i = NULL;
2921 for (i = items; NULL != i->next; i = i->next) {
2922 apply_to_items = g_slist_prepend(apply_to_items, i->data);
2923 items_to_select = g_slist_prepend(items_to_select, i->data);
2924 }
2926 Inkscape::XML::Node *dup = (SP_OBJECT_REPR(i->data))->duplicate(xml_doc);
2927 mask_items = g_slist_prepend(mask_items, dup);
2929 if (remove_original) {
2930 SPObject *item = reinterpret_cast<SPObject*>(i->data);
2931 items_to_delete = g_slist_prepend(items_to_delete, item);
2932 }
2933 }
2935 g_slist_free(items);
2936 items = NULL;
2938 if (apply_to_items && grouping == PREFS_MASKOBJECT_GROUPING_ALL) {
2939 // group all those objects into one group
2940 // and apply mask to that
2941 Inkscape::XML::Node *group = xml_doc->createElement("svg:g");
2943 // make a note we should ungroup this when unsetting mask
2944 group->setAttribute("inkscape:groupmode", "maskhelper");
2946 GSList *reprs_to_group = NULL;
2948 for (GSList *i = apply_to_items ; NULL != i ; i = i->next) {
2949 reprs_to_group = g_slist_prepend(reprs_to_group, SP_OBJECT_REPR(i->data));
2950 items_to_select = g_slist_remove(items_to_select, i->data);
2951 }
2952 reprs_to_group = g_slist_reverse(reprs_to_group);
2954 sp_selection_group_impl(reprs_to_group, group, xml_doc, doc);
2956 reprs_to_group = NULL;
2958 // apply clip/mask only to newly created group
2959 g_slist_free(apply_to_items);
2960 apply_to_items = NULL;
2961 apply_to_items = g_slist_prepend(apply_to_items, doc->getObjectByRepr(group));
2963 items_to_select = g_slist_prepend(items_to_select, doc->getObjectByRepr(group));
2965 Inkscape::GC::release(group);
2966 }
2968 gchar const *attributeName = apply_clip_path ? "clip-path" : "mask";
2969 for (GSList *i = apply_to_items; NULL != i; i = i->next) {
2970 SPItem *item = reinterpret_cast<SPItem *>(i->data);
2971 // inverted object transform should be applied to a mask object,
2972 // as mask is calculated in user space (after applying transform)
2973 Geom::Matrix maskTransform(item->transform.inverse());
2975 GSList *mask_items_dup = NULL;
2976 for (GSList *mask_item = mask_items; NULL != mask_item; mask_item = mask_item->next) {
2977 Inkscape::XML::Node *dup = reinterpret_cast<Inkscape::XML::Node *>(mask_item->data)->duplicate(xml_doc);
2978 mask_items_dup = g_slist_prepend(mask_items_dup, dup);
2979 }
2981 gchar const *mask_id = NULL;
2982 if (apply_clip_path) {
2983 mask_id = sp_clippath_create(mask_items_dup, doc, &maskTransform);
2984 } else {
2985 mask_id = sp_mask_create(mask_items_dup, doc, &maskTransform);
2986 }
2988 g_slist_free(mask_items_dup);
2989 mask_items_dup = NULL;
2991 Inkscape::XML::Node *current = SP_OBJECT_REPR(i->data);
2992 // Node to apply mask to
2993 Inkscape::XML::Node *apply_mask_to = current;
2995 if (grouping == PREFS_MASKOBJECT_GROUPING_SEPARATE) {
2996 // enclose current node in group, and apply crop/mask on that
2997 Inkscape::XML::Node *group = xml_doc->createElement("svg:g");
2998 // make a note we should ungroup this when unsetting mask
2999 group->setAttribute("inkscape:groupmode", "maskhelper");
3001 Inkscape::XML::Node *spnew = current->duplicate(xml_doc);
3002 gint position = current->position();
3003 items_to_select = g_slist_remove(items_to_select, item);
3004 current->parent()->appendChild(group);
3005 sp_repr_unparent(current);
3006 group->appendChild(spnew);
3007 group->setPosition(position);
3009 // Apply clip/mask to group instead
3010 apply_mask_to = group;
3012 items_to_select = g_slist_prepend(items_to_select, doc->getObjectByRepr(group));
3013 Inkscape::GC::release(spnew);
3014 Inkscape::GC::release(group);
3015 }
3017 apply_mask_to->setAttribute(attributeName, g_strdup_printf("url(#%s)", mask_id));
3019 }
3021 g_slist_free(mask_items);
3022 g_slist_free(apply_to_items);
3024 for (GSList *i = items_to_delete; NULL != i; i = i->next) {
3025 SPObject *item = reinterpret_cast<SPObject*>(i->data);
3026 item->deleteObject(false);
3027 items_to_select = g_slist_remove(items_to_select, item);
3028 }
3029 g_slist_free(items_to_delete);
3031 items_to_select = g_slist_reverse(items_to_select);
3033 selection->addList(items_to_select);
3034 g_slist_free(items_to_select);
3036 if (apply_clip_path)
3037 sp_document_done(doc, SP_VERB_OBJECT_SET_CLIPPATH, _("Set clipping path"));
3038 else
3039 sp_document_done(doc, SP_VERB_OBJECT_SET_MASK, _("Set mask"));
3040 }
3042 void sp_selection_unset_mask(SPDesktop *desktop, bool apply_clip_path) {
3043 if (desktop == NULL)
3044 return;
3046 SPDocument *doc = sp_desktop_document(desktop);
3047 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
3048 Inkscape::Selection *selection = sp_desktop_selection(desktop);
3050 // check if something is selected
3051 if (selection->isEmpty()) {
3052 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to remove clippath or mask from."));
3053 return;
3054 }
3056 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
3057 bool remove_original = prefs->getBool("/options/maskobject/remove", true);
3058 bool ungroup_masked = prefs->getBool("/options/maskobject/ungrouping", true);
3059 sp_document_ensure_up_to_date(doc);
3061 gchar const *attributeName = apply_clip_path ? "clip-path" : "mask";
3062 std::map<SPObject*,SPItem*> referenced_objects;
3064 GSList *items = g_slist_copy((GSList *) selection->itemList());
3065 selection->clear();
3067 GSList *items_to_ungroup = NULL;
3068 GSList *items_to_select = g_slist_copy(items);
3069 items_to_select = g_slist_reverse(items_to_select);
3072 // SPObject* refers to a group containing the clipped path or mask itself,
3073 // whereas SPItem* refers to the item being clipped or masked
3074 for (GSList const *i = items; NULL != i; i = i->next) {
3075 if (remove_original) {
3076 // remember referenced mask/clippath, so orphaned masks can be moved back to document
3077 SPItem *item = reinterpret_cast<SPItem *>(i->data);
3078 Inkscape::URIReference *uri_ref = NULL;
3080 if (apply_clip_path) {
3081 uri_ref = item->clip_ref;
3082 } else {
3083 uri_ref = item->mask_ref;
3084 }
3086 // collect distinct mask object (and associate with item to apply transform)
3087 if (NULL != uri_ref && NULL != uri_ref->getObject()) {
3088 referenced_objects[uri_ref->getObject()] = item;
3089 }
3090 }
3092 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, "none");
3094 if (ungroup_masked && SP_IS_GROUP(i->data)) {
3095 // if we had previously enclosed masked object in group,
3096 // add it to list so we can ungroup it later
3097 SPGroup *item = SP_GROUP(i->data);
3099 // ungroup only groups we created when setting clip/mask
3100 if (item->layerMode() == SPGroup::MASK_HELPER) {
3101 items_to_ungroup = g_slist_prepend(items_to_ungroup, item);
3102 }
3104 }
3105 }
3106 g_slist_free(items);
3108 // restore mask objects into a document
3109 for ( std::map<SPObject*,SPItem*>::iterator it = referenced_objects.begin() ; it != referenced_objects.end() ; ++it) {
3110 SPObject *obj = (*it).first; // Group containing the clipped paths or masks
3111 GSList *items_to_move = NULL;
3112 for (SPObject *child = sp_object_first_child(obj) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
3113 // Collect all clipped paths and masks within a single group
3114 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate(xml_doc);
3115 items_to_move = g_slist_prepend(items_to_move, copy);
3116 }
3118 if (!obj->isReferenced()) {
3119 // delete from defs if no other object references this mask
3120 obj->deleteObject(false);
3121 }
3123 // remember parent and position of the item to which the clippath/mask was applied
3124 Inkscape::XML::Node *parent = SP_OBJECT_REPR((*it).second)->parent();
3125 gint pos = SP_OBJECT_REPR((*it).second)->position();
3127 // Iterate through all clipped paths / masks
3128 for (GSList *i = items_to_move; NULL != i; i = i->next) {
3129 Inkscape::XML::Node *repr = (Inkscape::XML::Node *)i->data;
3131 // insert into parent, restore pos
3132 parent->appendChild(repr);
3133 repr->setPosition((pos + 1) > 0 ? (pos + 1) : 0);
3135 SPItem *mask_item = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
3136 items_to_select = g_slist_prepend(items_to_select, mask_item);
3138 // transform mask, so it is moved the same spot where mask was applied
3139 Geom::Matrix transform(mask_item->transform);
3140 transform *= (*it).second->transform;
3141 sp_item_write_transform(mask_item, SP_OBJECT_REPR(mask_item), transform);
3142 }
3144 g_slist_free(items_to_move);
3145 }
3147 // ungroup marked groups added when setting mask
3148 for (GSList *i = items_to_ungroup ; NULL != i ; i = i->next) {
3149 items_to_select = g_slist_remove(items_to_select, SP_GROUP(i->data));
3150 GSList *children = NULL;
3151 sp_item_group_ungroup(SP_GROUP(i->data), &children, false);
3152 items_to_select = g_slist_concat(children, items_to_select);
3153 }
3155 g_slist_free(items_to_ungroup);
3157 // rebuild selection
3158 items_to_select = g_slist_reverse(items_to_select);
3159 selection->addList(items_to_select);
3160 g_slist_free(items_to_select);
3162 if (apply_clip_path)
3163 sp_document_done(doc, SP_VERB_OBJECT_UNSET_CLIPPATH, _("Release clipping path"));
3164 else
3165 sp_document_done(doc, SP_VERB_OBJECT_UNSET_MASK, _("Release mask"));
3166 }
3168 /**
3169 * \param with_margins margins defined in the xml under <sodipodi:namedview>
3170 * "fit-margin-..." attributes. See SPDocument::fitToRect.
3171 * \return true if an undoable change should be recorded.
3172 */
3173 bool
3174 fit_canvas_to_selection(SPDesktop *desktop, bool with_margins)
3175 {
3176 g_return_val_if_fail(desktop != NULL, false);
3177 SPDocument *doc = sp_desktop_document(desktop);
3179 g_return_val_if_fail(doc != NULL, false);
3180 g_return_val_if_fail(desktop->selection != NULL, false);
3182 if (desktop->selection->isEmpty()) {
3183 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to fit canvas to."));
3184 return false;
3185 }
3186 Geom::OptRect const bbox(desktop->selection->bounds(SPItem::RENDERING_BBOX));
3187 if (bbox) {
3188 doc->fitToRect(*bbox, with_margins);
3189 return true;
3190 } else {
3191 return false;
3192 }
3193 }
3195 /**
3196 * Fit canvas to the bounding box of the selection, as an undoable action.
3197 */
3198 void
3199 verb_fit_canvas_to_selection(SPDesktop *const desktop)
3200 {
3201 if (fit_canvas_to_selection(desktop)) {
3202 sp_document_done(sp_desktop_document(desktop), SP_VERB_FIT_CANVAS_TO_SELECTION,
3203 _("Fit Page to Selection"));
3204 }
3205 }
3207 /**
3208 * \param with_margins margins defined in the xml under <sodipodi:namedview>
3209 * "fit-margin-..." attributes. See SPDocument::fitToRect.
3210 */
3211 bool
3212 fit_canvas_to_drawing(SPDocument *doc, bool with_margins)
3213 {
3214 g_return_val_if_fail(doc != NULL, false);
3216 sp_document_ensure_up_to_date(doc);
3217 SPItem const *const root = SP_ITEM(doc->root);
3218 Geom::OptRect const bbox(root->getBounds(sp_item_i2d_affine(root)));
3219 if (bbox) {
3220 doc->fitToRect(*bbox, with_margins);
3221 return true;
3222 } else {
3223 return false;
3224 }
3225 }
3227 void
3228 verb_fit_canvas_to_drawing(SPDesktop *desktop)
3229 {
3230 if (fit_canvas_to_drawing(sp_desktop_document(desktop))) {
3231 sp_document_done(sp_desktop_document(desktop), SP_VERB_FIT_CANVAS_TO_DRAWING,
3232 _("Fit Page to Drawing"));
3233 }
3234 }
3236 /**
3237 * Fits canvas to selection or drawing with margins from <sodipodi:namedview>
3238 * "fit-margin-..." attributes. See SPDocument::fitToRect and
3239 * ui/dialog/page-sizer.
3240 */
3241 void fit_canvas_to_selection_or_drawing(SPDesktop *desktop) {
3242 g_return_if_fail(desktop != NULL);
3243 SPDocument *doc = sp_desktop_document(desktop);
3245 g_return_if_fail(doc != NULL);
3246 g_return_if_fail(desktop->selection != NULL);
3248 bool const changed = ( desktop->selection->isEmpty()
3249 ? fit_canvas_to_drawing(doc, true)
3250 : fit_canvas_to_selection(desktop, true) );
3251 if (changed) {
3252 sp_document_done(sp_desktop_document(desktop), SP_VERB_FIT_CANVAS_TO_SELECTION_OR_DRAWING,
3253 _("Fit Page to Selection or Drawing"));
3254 }
3255 };
3257 static void itemtree_map(void (*f)(SPItem *, SPDesktop *), SPObject *root, SPDesktop *desktop) {
3258 // don't operate on layers
3259 if (SP_IS_ITEM(root) && !desktop->isLayer(SP_ITEM(root))) {
3260 f(SP_ITEM(root), desktop);
3261 }
3262 for ( SPObject::SiblingIterator iter = root->firstChild() ; iter ; ++iter ) {
3263 //don't recurse into locked layers
3264 if (!(SP_IS_ITEM(&*iter) && desktop->isLayer(SP_ITEM(&*iter)) && SP_ITEM(&*iter)->isLocked())) {
3265 itemtree_map(f, iter, desktop);
3266 }
3267 }
3268 }
3270 static void unlock(SPItem *item, SPDesktop */*desktop*/) {
3271 if (item->isLocked()) {
3272 item->setLocked(FALSE);
3273 }
3274 }
3276 static void unhide(SPItem *item, SPDesktop *desktop) {
3277 if (desktop->itemIsHidden(item)) {
3278 item->setExplicitlyHidden(FALSE);
3279 }
3280 }
3282 static void process_all(void (*f)(SPItem *, SPDesktop *), SPDesktop *dt, bool layer_only) {
3283 if (!dt) return;
3285 SPObject *root;
3286 if (layer_only) {
3287 root = dt->currentLayer();
3288 } else {
3289 root = dt->currentRoot();
3290 }
3292 itemtree_map(f, root, dt);
3293 }
3295 void unlock_all(SPDesktop *dt) {
3296 process_all(&unlock, dt, true);
3297 }
3299 void unlock_all_in_all_layers(SPDesktop *dt) {
3300 process_all(&unlock, dt, false);
3301 }
3303 void unhide_all(SPDesktop *dt) {
3304 process_all(&unhide, dt, true);
3305 }
3307 void unhide_all_in_all_layers(SPDesktop *dt) {
3308 process_all(&unhide, dt, false);
3309 }
3312 /*
3313 Local Variables:
3314 mode:c++
3315 c-file-style:"stroustrup"
3316 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
3317 indent-tabs-mode:nil
3318 fill-column:99
3319 End:
3320 */
3321 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :