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 *
12 * Copyright (C) 1999-2010 authors
13 * Copyright (C) 2001-2002 Ximian, Inc.
14 *
15 * Released under GNU GPL, read the file 'COPYING' for more information
16 */
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
22 #include "selection-chemistry.h"
24 // TOOD fixme: This should be moved into preference repr
25 SPCycleType SP_CYCLING = SP_CYCLE_FOCUS;
28 #include <gtkmm/clipboard.h>
30 #include "svg/svg.h"
31 #include "desktop.h"
32 #include "desktop-style.h"
33 #include "dir-util.h"
34 #include "selection.h"
35 #include "tools-switch.h"
36 #include "desktop-handles.h"
37 #include "message-stack.h"
38 #include "sp-item-transform.h"
39 #include "marker.h"
40 #include "sp-use.h"
41 #include "sp-textpath.h"
42 #include "sp-tspan.h"
43 #include "sp-tref.h"
44 #include "sp-flowtext.h"
45 #include "sp-flowregion.h"
46 #include "sp-image.h"
47 #include "text-editing.h"
48 #include "text-context.h"
49 #include "connector-context.h"
50 #include "sp-path.h"
51 #include "sp-conn-end.h"
52 #include "dropper-context.h"
53 #include <glibmm/i18n.h>
54 #include "libnr/nr-matrix-rotate-ops.h"
55 #include "libnr/nr-matrix-translate-ops.h"
56 #include "libnr/nr-scale-ops.h"
57 #include <libnr/nr-matrix-ops.h>
58 #include <2geom/transforms.h>
59 #include "xml/repr.h"
60 #include "xml/rebase-hrefs.h"
61 #include "style.h"
62 #include "document-private.h"
63 #include "sp-gradient.h"
64 #include "sp-gradient-reference.h"
65 #include "sp-linear-gradient-fns.h"
66 #include "sp-pattern.h"
67 #include "sp-radial-gradient-fns.h"
68 #include "gradient-context.h"
69 #include "sp-namedview.h"
70 #include "preferences.h"
71 #include "sp-offset.h"
72 #include "sp-clippath.h"
73 #include "sp-mask.h"
74 #include "file.h"
75 #include "helper/png-write.h"
76 #include "layer-fns.h"
77 #include "context-fns.h"
78 #include <map>
79 #include <cstring>
80 #include <string>
81 #include "helper/units.h"
82 #include "sp-item.h"
83 #include "box3d.h"
84 #include "persp3d.h"
85 #include "unit-constants.h"
86 #include "xml/simple-document.h"
87 #include "sp-filter-reference.h"
88 #include "gradient-drag.h"
89 #include "uri-references.h"
90 #include "libnr/nr-convert2geom.h"
91 #include "display/curve.h"
92 #include "display/canvas-bpath.h"
93 #include "inkscape-private.h"
94 #include "path-chemistry.h"
95 #include "ui/tool/control-point-selection.h"
96 #include "ui/tool/multi-path-manipulator.h"
98 #include "enums.h"
99 #include "sp-item-group.h"
101 // For clippath editing
102 #include "tools-switch.h"
103 #include "ui/tool/node-tool.h"
105 #include "ui/clipboard.h"
107 using Geom::X;
108 using Geom::Y;
110 /* The clipboard handling is in ui/clipboard.cpp now. There are some legacy functions left here,
111 because the layer manipulation code uses them. It should be rewritten specifically
112 for that purpose. */
116 namespace Inkscape {
118 void SelectionHelper::selectAll(SPDesktop *dt)
119 {
120 if (tools_isactive(dt, TOOLS_NODES)) {
121 InkNodeTool *nt = static_cast<InkNodeTool*>(dt->event_context);
122 if (!nt->_multipath->empty()) {
123 nt->_multipath->selectSubpaths();
124 return;
125 }
126 }
127 sp_edit_select_all(dt);
128 }
130 void SelectionHelper::selectAllInAll(SPDesktop *dt)
131 {
132 if (tools_isactive(dt, TOOLS_NODES)) {
133 InkNodeTool *nt = static_cast<InkNodeTool*>(dt->event_context);
134 nt->_selected_nodes->selectAll();
135 } else {
136 sp_edit_select_all_in_all_layers(dt);
137 }
138 }
140 void SelectionHelper::selectNone(SPDesktop *dt)
141 {
142 if (tools_isactive(dt, TOOLS_NODES)) {
143 InkNodeTool *nt = static_cast<InkNodeTool*>(dt->event_context);
144 nt->_selected_nodes->clear();
145 } else {
146 sp_desktop_selection(dt)->clear();
147 }
148 }
150 void SelectionHelper::invert(SPDesktop *dt)
151 {
152 if (tools_isactive(dt, TOOLS_NODES)) {
153 InkNodeTool *nt = static_cast<InkNodeTool*>(dt->event_context);
154 nt->_multipath->invertSelectionInSubpaths();
155 } else {
156 sp_edit_invert(dt);
157 }
158 }
160 void SelectionHelper::invertAllInAll(SPDesktop *dt)
161 {
162 if (tools_isactive(dt, TOOLS_NODES)) {
163 InkNodeTool *nt = static_cast<InkNodeTool*>(dt->event_context);
164 nt->_selected_nodes->invertSelection();
165 } else {
166 sp_edit_invert_in_all_layers(dt);
167 }
168 }
170 void SelectionHelper::reverse(SPDesktop *dt)
171 {
172 // TODO make this a virtual method of event context!
173 if (tools_isactive(dt, TOOLS_NODES)) {
174 InkNodeTool *nt = static_cast<InkNodeTool*>(dt->event_context);
175 nt->_multipath->reverseSubpaths();
176 } else {
177 sp_selected_path_reverse(dt);
178 }
179 }
181 void SelectionHelper::selectNext(SPDesktop *dt)
182 {
183 SPEventContext *ec = dt->event_context;
184 if (tools_isactive(dt, TOOLS_NODES)) {
185 InkNodeTool *nt = static_cast<InkNodeTool*>(dt->event_context);
186 nt->_multipath->shiftSelection(1);
187 } else if (tools_isactive(dt, TOOLS_GRADIENT)
188 && ec->_grdrag->isNonEmpty()) {
189 sp_gradient_context_select_next(ec);
190 } else {
191 sp_selection_item_next(dt);
192 }
193 }
195 void SelectionHelper::selectPrev(SPDesktop *dt)
196 {
197 SPEventContext *ec = dt->event_context;
198 if (tools_isactive(dt, TOOLS_NODES)) {
199 InkNodeTool *nt = static_cast<InkNodeTool*>(dt->event_context);
200 nt->_multipath->shiftSelection(-1);
201 } else if (tools_isactive(dt, TOOLS_GRADIENT)
202 && ec->_grdrag->isNonEmpty()) {
203 sp_gradient_context_select_prev(ec);
204 } else {
205 sp_selection_item_prev(dt);
206 }
207 }
209 } // namespace Inkscape
212 /**
213 * Copies repr and its inherited css style elements, along with the accumulated transform 'full_t',
214 * then prepends the copy to 'clip'.
215 */
216 void sp_selection_copy_one(Inkscape::XML::Node *repr, Geom::Matrix full_t, GSList **clip, Inkscape::XML::Document* xml_doc)
217 {
218 Inkscape::XML::Node *copy = repr->duplicate(xml_doc);
220 // copy complete inherited style
221 SPCSSAttr *css = sp_repr_css_attr_inherited(repr, "style");
222 sp_repr_css_set(copy, css, "style");
223 sp_repr_css_attr_unref(css);
225 // write the complete accumulated transform passed to us
226 // (we're dealing with unattached repr, so we write to its attr
227 // instead of using sp_item_set_transform)
228 gchar *affinestr=sp_svg_transform_write(full_t);
229 copy->setAttribute("transform", affinestr);
230 g_free(affinestr);
232 *clip = g_slist_prepend(*clip, copy);
233 }
235 void sp_selection_copy_impl(GSList const *items, GSList **clip, Inkscape::XML::Document* xml_doc)
236 {
237 // Sort items:
238 GSList *sorted_items = g_slist_copy((GSList *) items);
239 sorted_items = g_slist_sort((GSList *) sorted_items, (GCompareFunc) sp_object_compare_position);
241 // Copy item reprs:
242 for (GSList *i = (GSList *) sorted_items; i != NULL; i = i->next) {
243 sp_selection_copy_one(SP_OBJECT_REPR(i->data), sp_item_i2doc_affine(SP_ITEM(i->data)), clip, xml_doc);
244 }
246 *clip = g_slist_reverse(*clip);
247 g_slist_free((GSList *) sorted_items);
248 }
250 GSList *sp_selection_paste_impl(SPDocument *doc, SPObject *parent, GSList **clip)
251 {
252 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
254 GSList *copied = NULL;
255 // add objects to document
256 for (GSList *l = *clip; l != NULL; l = l->next) {
257 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
258 Inkscape::XML::Node *copy = repr->duplicate(xml_doc);
260 // premultiply the item transform by the accumulated parent transform in the paste layer
261 Geom::Matrix local(sp_item_i2doc_affine(SP_ITEM(parent)));
262 if (!local.isIdentity()) {
263 gchar const *t_str = copy->attribute("transform");
264 Geom::Matrix item_t(Geom::identity());
265 if (t_str)
266 sp_svg_transform_read(t_str, &item_t);
267 item_t *= local.inverse();
268 // (we're dealing with unattached repr, so we write to its attr instead of using sp_item_set_transform)
269 gchar *affinestr=sp_svg_transform_write(item_t);
270 copy->setAttribute("transform", affinestr);
271 g_free(affinestr);
272 }
274 parent->appendChildRepr(copy);
275 copied = g_slist_prepend(copied, copy);
276 Inkscape::GC::release(copy);
277 }
278 return copied;
279 }
281 void sp_selection_delete_impl(GSList const *items, bool propagate = true, bool propagate_descendants = true)
282 {
283 for (GSList const *i = items ; i ; i = i->next ) {
284 sp_object_ref((SPObject *)i->data, NULL);
285 }
286 for (GSList const *i = items; i != NULL; i = i->next) {
287 SPItem *item = (SPItem *) i->data;
288 SP_OBJECT(item)->deleteObject(propagate, propagate_descendants);
289 sp_object_unref((SPObject *)item, NULL);
290 }
291 }
294 void sp_selection_delete(SPDesktop *desktop)
295 {
296 if (desktop == NULL) {
297 return;
298 }
300 if (tools_isactive(desktop, TOOLS_TEXT))
301 if (sp_text_delete_selection(desktop->event_context)) {
302 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_TEXT,
303 _("Delete text"));
304 return;
305 }
307 Inkscape::Selection *selection = sp_desktop_selection(desktop);
309 // check if something is selected
310 if (selection->isEmpty()) {
311 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Nothing</b> was deleted."));
312 return;
313 }
315 GSList const *selected = g_slist_copy(const_cast<GSList *>(selection->itemList()));
316 selection->clear();
317 sp_selection_delete_impl(selected);
318 g_slist_free((GSList *) selected);
320 /* a tool may have set up private information in it's selection context
321 * that depends on desktop items. I think the only sane way to deal with
322 * this currently is to reset the current tool, which will reset it's
323 * associated selection context. For example: deleting an object
324 * while moving it around the canvas.
325 */
326 tools_switch( desktop, tools_active( desktop ) );
328 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_DELETE,
329 _("Delete"));
330 }
332 void add_ids_recursive(std::vector<const gchar *> &ids, SPObject *obj)
333 {
334 if (!obj)
335 return;
337 ids.push_back(obj->getId());
339 if (SP_IS_GROUP(obj)) {
340 for (SPObject *child = sp_object_first_child(obj) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
341 add_ids_recursive(ids, child);
342 }
343 }
344 }
346 void sp_selection_duplicate(SPDesktop *desktop, bool suppressDone)
347 {
348 if (desktop == NULL)
349 return;
351 SPDocument *doc = desktop->doc();
352 Inkscape::XML::Document* xml_doc = sp_document_repr_doc(doc);
353 Inkscape::Selection *selection = sp_desktop_selection(desktop);
355 // check if something is selected
356 if (selection->isEmpty()) {
357 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to duplicate."));
358 return;
359 }
361 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
363 selection->clear();
365 // sorting items from different parents sorts each parent's subset without possibly mixing
366 // them, just what we need
367 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
369 GSList *newsel = NULL;
371 std::vector<const gchar *> old_ids;
372 std::vector<const gchar *> new_ids;
373 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
374 bool relink_clones = prefs->getBool("/options/relinkclonesonduplicate/value");
376 while (reprs) {
377 Inkscape::XML::Node *old_repr = (Inkscape::XML::Node *) reprs->data;
378 Inkscape::XML::Node *parent = old_repr->parent();
379 Inkscape::XML::Node *copy = old_repr->duplicate(xml_doc);
381 parent->appendChild(copy);
383 if (relink_clones) {
384 SPObject *old_obj = doc->getObjectByRepr(old_repr);
385 SPObject *new_obj = doc->getObjectByRepr(copy);
386 add_ids_recursive(old_ids, old_obj);
387 add_ids_recursive(new_ids, new_obj);
388 }
390 newsel = g_slist_prepend(newsel, copy);
391 reprs = g_slist_remove(reprs, reprs->data);
392 Inkscape::GC::release(copy);
393 }
395 if (relink_clones) {
397 g_assert(old_ids.size() == new_ids.size());
399 for (unsigned int i = 0; i < old_ids.size(); i++) {
400 const gchar *id = old_ids[i];
401 SPObject *old_clone = doc->getObjectById(id);
402 if (SP_IS_USE(old_clone)) {
403 SPItem *orig = sp_use_get_original(SP_USE(old_clone));
404 if (!orig) // orphaned
405 continue;
406 for (unsigned int j = 0; j < old_ids.size(); j++) {
407 if (!strcmp(orig->getId(), old_ids[j])) {
408 // we have both orig and clone in selection, relink
409 // std::cout << id << " old, its ori: " << SP_OBJECT_ID(orig) << "; will relink:" << new_ids[i] << " to " << new_ids[j] << "\n";
410 gchar *newref = g_strdup_printf("#%s", new_ids[j]);
411 SPObject *new_clone = doc->getObjectById(new_ids[i]);
412 SP_OBJECT_REPR(new_clone)->setAttribute("xlink:href", newref);
413 new_clone->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
414 g_free(newref);
415 }
416 }
417 }
418 }
419 }
422 if ( !suppressDone ) {
423 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_DUPLICATE,
424 _("Duplicate"));
425 }
427 selection->setReprList(newsel);
429 g_slist_free(newsel);
430 }
432 void sp_edit_clear_all(SPDesktop *dt)
433 {
434 if (!dt)
435 return;
437 SPDocument *doc = sp_desktop_document(dt);
438 sp_desktop_selection(dt)->clear();
440 g_return_if_fail(SP_IS_GROUP(dt->currentLayer()));
441 GSList *items = sp_item_group_item_list(SP_GROUP(dt->currentLayer()));
443 while (items) {
444 SP_OBJECT(items->data)->deleteObject();
445 items = g_slist_remove(items, items->data);
446 }
448 sp_document_done(doc, SP_VERB_EDIT_CLEAR_ALL,
449 _("Delete all"));
450 }
452 GSList *
453 get_all_items(GSList *list, SPObject *from, SPDesktop *desktop, bool onlyvisible, bool onlysensitive, GSList const *exclude)
454 {
455 for (SPObject *child = sp_object_first_child(SP_OBJECT(from)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
456 if (SP_IS_ITEM(child) &&
457 !desktop->isLayer(SP_ITEM(child)) &&
458 (!onlysensitive || !SP_ITEM(child)->isLocked()) &&
459 (!onlyvisible || !desktop->itemIsHidden(SP_ITEM(child))) &&
460 (!exclude || !g_slist_find((GSList *) exclude, child))
461 )
462 {
463 list = g_slist_prepend(list, SP_ITEM(child));
464 }
466 if (SP_IS_ITEM(child) && desktop->isLayer(SP_ITEM(child))) {
467 list = get_all_items(list, child, desktop, onlyvisible, onlysensitive, exclude);
468 }
469 }
471 return list;
472 }
474 void sp_edit_select_all_full(SPDesktop *dt, bool force_all_layers, bool invert)
475 {
476 if (!dt)
477 return;
479 Inkscape::Selection *selection = sp_desktop_selection(dt);
481 g_return_if_fail(SP_IS_GROUP(dt->currentLayer()));
483 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
484 PrefsSelectionContext inlayer = (PrefsSelectionContext) prefs->getInt("/options/kbselection/inlayer", PREFS_SELECTION_LAYER);
485 bool onlyvisible = prefs->getBool("/options/kbselection/onlyvisible", true);
486 bool onlysensitive = prefs->getBool("/options/kbselection/onlysensitive", true);
488 GSList *items = NULL;
490 GSList const *exclude = NULL;
491 if (invert) {
492 exclude = selection->itemList();
493 }
495 if (force_all_layers)
496 inlayer = PREFS_SELECTION_ALL;
498 switch (inlayer) {
499 case PREFS_SELECTION_LAYER: {
500 if ( (onlysensitive && SP_ITEM(dt->currentLayer())->isLocked()) ||
501 (onlyvisible && dt->itemIsHidden(SP_ITEM(dt->currentLayer()))) )
502 return;
504 GSList *all_items = sp_item_group_item_list(SP_GROUP(dt->currentLayer()));
506 for (GSList *i = all_items; i; i = i->next) {
507 SPItem *item = SP_ITEM(i->data);
509 if (item && (!onlysensitive || !item->isLocked())) {
510 if (!onlyvisible || !dt->itemIsHidden(item)) {
511 if (!dt->isLayer(item)) {
512 if (!invert || !g_slist_find((GSList *) exclude, item)) {
513 items = g_slist_prepend(items, item); // leave it in the list
514 }
515 }
516 }
517 }
518 }
520 g_slist_free(all_items);
521 break;
522 }
523 case PREFS_SELECTION_LAYER_RECURSIVE: {
524 items = get_all_items(NULL, dt->currentLayer(), dt, onlyvisible, onlysensitive, exclude);
525 break;
526 }
527 default: {
528 items = get_all_items(NULL, dt->currentRoot(), dt, onlyvisible, onlysensitive, exclude);
529 break;
530 }
531 }
533 selection->setList(items);
535 if (items) {
536 g_slist_free(items);
537 }
538 }
540 void sp_edit_select_all(SPDesktop *desktop)
541 {
542 sp_edit_select_all_full(desktop, false, false);
543 }
545 void sp_edit_select_all_in_all_layers(SPDesktop *desktop)
546 {
547 sp_edit_select_all_full(desktop, true, false);
548 }
550 void sp_edit_invert(SPDesktop *desktop)
551 {
552 sp_edit_select_all_full(desktop, false, true);
553 }
555 void sp_edit_invert_in_all_layers(SPDesktop *desktop)
556 {
557 sp_edit_select_all_full(desktop, true, true);
558 }
560 void sp_selection_group_impl(GSList const *reprs_to_group, Inkscape::XML::Node *group, Inkscape::XML::Document *xml_doc, SPDocument *doc) {
561 GSList *p = g_slist_copy((GSList *) reprs_to_group);
563 p = g_slist_sort(p, (GCompareFunc) sp_repr_compare_position);
565 // Remember the position and parent of the topmost object.
566 gint topmost = ((Inkscape::XML::Node *) g_slist_last(p)->data)->position();
567 Inkscape::XML::Node *topmost_parent = ((Inkscape::XML::Node *) g_slist_last(p)->data)->parent();
569 while (p) {
570 Inkscape::XML::Node *current = (Inkscape::XML::Node *) p->data;
572 if (current->parent() == topmost_parent) {
573 Inkscape::XML::Node *spnew = current->duplicate(xml_doc);
574 sp_repr_unparent(current);
575 group->appendChild(spnew);
576 Inkscape::GC::release(spnew);
577 topmost --; // only reduce count for those items deleted from topmost_parent
578 } else { // move it to topmost_parent first
579 GSList *temp_clip = NULL;
581 // At this point, current may already have no item, due to its being a clone whose original is already moved away
582 // So we copy it artificially calculating the transform from its repr->attr("transform") and the parent transform
583 gchar const *t_str = current->attribute("transform");
584 Geom::Matrix item_t(Geom::identity());
585 if (t_str)
586 sp_svg_transform_read(t_str, &item_t);
587 item_t *= sp_item_i2doc_affine(SP_ITEM(doc->getObjectByRepr(current->parent())));
588 // FIXME: when moving both clone and original from a transformed group (either by
589 // grouping into another parent, or by cut/paste) the transform from the original's
590 // parent becomes embedded into original itself, and this affects its clones. Fix
591 // this by remembering the transform diffs we write to each item into an array and
592 // then, if this is clone, looking up its original in that array and pre-multiplying
593 // it by the inverse of that original's transform diff.
595 sp_selection_copy_one(current, item_t, &temp_clip, xml_doc);
596 sp_repr_unparent(current);
598 // paste into topmost_parent (temporarily)
599 GSList *copied = sp_selection_paste_impl(doc, doc->getObjectByRepr(topmost_parent), &temp_clip);
600 if (temp_clip) g_slist_free(temp_clip);
601 if (copied) { // if success,
602 // take pasted object (now in topmost_parent)
603 Inkscape::XML::Node *in_topmost = (Inkscape::XML::Node *) copied->data;
604 // make a copy
605 Inkscape::XML::Node *spnew = in_topmost->duplicate(xml_doc);
606 // remove pasted
607 sp_repr_unparent(in_topmost);
608 // put its copy into group
609 group->appendChild(spnew);
610 Inkscape::GC::release(spnew);
611 g_slist_free(copied);
612 }
613 }
614 p = g_slist_remove(p, current);
615 }
617 // Add the new group to the topmost members' parent
618 topmost_parent->appendChild(group);
620 // Move to the position of the topmost, reduced by the number of items deleted from topmost_parent
621 group->setPosition(topmost + 1);
622 }
624 void sp_selection_group(SPDesktop *desktop)
625 {
626 if (desktop == NULL)
627 return;
629 SPDocument *doc = sp_desktop_document(desktop);
630 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
632 Inkscape::Selection *selection = sp_desktop_selection(desktop);
634 // Check if something is selected.
635 if (selection->isEmpty()) {
636 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>some objects</b> to group."));
637 return;
638 }
640 GSList const *l = (GSList *) selection->reprList();
643 Inkscape::XML::Node *group = xml_doc->createElement("svg:g");
645 sp_selection_group_impl(l, group, xml_doc, doc);
647 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_GROUP,
648 _("Group"));
650 selection->clear();
651 selection->set(group);
652 Inkscape::GC::release(group);
653 }
655 void sp_selection_ungroup(SPDesktop *desktop)
656 {
657 if (desktop == NULL)
658 return;
660 Inkscape::Selection *selection = sp_desktop_selection(desktop);
662 if (selection->isEmpty()) {
663 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>group</b> to ungroup."));
664 return;
665 }
667 GSList *items = g_slist_copy((GSList *) selection->itemList());
668 selection->clear();
670 // Get a copy of current selection.
671 GSList *new_select = NULL;
672 bool ungrouped = false;
673 for (GSList *i = items;
674 i != NULL;
675 i = i->next)
676 {
677 SPItem *group = (SPItem *) i->data;
679 // when ungrouping cloned groups with their originals, some objects that were selected may no more exist due to unlinking
680 if (!SP_IS_OBJECT(group)) {
681 continue;
682 }
684 /* We do not allow ungrouping <svg> etc. (lauris) */
685 if (strcmp(SP_OBJECT_REPR(group)->name(), "svg:g") && strcmp(SP_OBJECT_REPR(group)->name(), "svg:switch")) {
686 // keep the non-group item in the new selection
687 selection->add(group);
688 continue;
689 }
691 GSList *children = NULL;
692 /* This is not strictly required, but is nicer to rely on group ::destroy (lauris) */
693 sp_item_group_ungroup(SP_GROUP(group), &children, false);
694 ungrouped = true;
695 // Add ungrouped items to the new selection.
696 new_select = g_slist_concat(new_select, children);
697 }
699 if (new_select) { // Set new selection.
700 selection->addList(new_select);
701 g_slist_free(new_select);
702 }
703 if (!ungrouped) {
704 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No groups</b> to ungroup in the selection."));
705 }
707 g_slist_free(items);
709 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_UNGROUP,
710 _("Ungroup"));
711 }
713 /** Replace all groups in the list with their member objects, recursively; returns a new list, frees old */
714 GSList *
715 sp_degroup_list(GSList *items)
716 {
717 GSList *out = NULL;
718 bool has_groups = false;
719 for (GSList *item = items; item; item = item->next) {
720 if (!SP_IS_GROUP(item->data)) {
721 out = g_slist_prepend(out, item->data);
722 } else {
723 has_groups = true;
724 GSList *members = sp_item_group_item_list(SP_GROUP(item->data));
725 for (GSList *member = members; member; member = member->next) {
726 out = g_slist_prepend(out, member->data);
727 }
728 g_slist_free(members);
729 }
730 }
731 out = g_slist_reverse(out);
732 g_slist_free(items);
734 if (has_groups) { // recurse if we unwrapped a group - it may have contained others
735 out = sp_degroup_list(out);
736 }
738 return out;
739 }
742 /** If items in the list have a common parent, return it, otherwise return NULL */
743 static SPGroup *
744 sp_item_list_common_parent_group(GSList const *items)
745 {
746 if (!items) {
747 return NULL;
748 }
749 SPObject *parent = SP_OBJECT_PARENT(items->data);
750 /* Strictly speaking this CAN happen, if user selects <svg> from Inkscape::XML editor */
751 if (!SP_IS_GROUP(parent)) {
752 return NULL;
753 }
754 for (items = items->next; items; items = items->next) {
755 if (SP_OBJECT_PARENT(items->data) != parent) {
756 return NULL;
757 }
758 }
760 return SP_GROUP(parent);
761 }
763 /** Finds out the minimum common bbox of the selected items. */
764 static Geom::OptRect
765 enclose_items(GSList const *items)
766 {
767 g_assert(items != NULL);
769 Geom::OptRect r;
770 for (GSList const *i = items; i; i = i->next) {
771 r = Geom::unify(r, sp_item_bbox_desktop((SPItem *) i->data));
772 }
773 return r;
774 }
776 SPObject *
777 prev_sibling(SPObject *child)
778 {
779 SPObject *parent = SP_OBJECT_PARENT(child);
780 if (!SP_IS_GROUP(parent)) {
781 return NULL;
782 }
783 for ( SPObject *i = sp_object_first_child(parent) ; i; i = SP_OBJECT_NEXT(i) ) {
784 if (i->next == child)
785 return i;
786 }
787 return NULL;
788 }
790 void
791 sp_selection_raise(SPDesktop *desktop)
792 {
793 if (!desktop)
794 return;
796 Inkscape::Selection *selection = sp_desktop_selection(desktop);
798 GSList const *items = (GSList *) selection->itemList();
799 if (!items) {
800 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise."));
801 return;
802 }
804 SPGroup const *group = sp_item_list_common_parent_group(items);
805 if (!group) {
806 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
807 return;
808 }
810 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
812 /* Construct reverse-ordered list of selected children. */
813 GSList *rev = g_slist_copy((GSList *) items);
814 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
816 // Determine the common bbox of the selected items.
817 Geom::OptRect selected = enclose_items(items);
819 // Iterate over all objects in the selection (starting from top).
820 if (selected) {
821 while (rev) {
822 SPObject *child = SP_OBJECT(rev->data);
823 // for each selected object, find the next sibling
824 for (SPObject *newref = child->next; newref; newref = newref->next) {
825 // if the sibling is an item AND overlaps our selection,
826 if (SP_IS_ITEM(newref)) {
827 Geom::OptRect newref_bbox = sp_item_bbox_desktop(SP_ITEM(newref));
828 if ( newref_bbox && selected->intersects(*newref_bbox) ) {
829 // AND if it's not one of our selected objects,
830 if (!g_slist_find((GSList *) items, newref)) {
831 // move the selected object after that sibling
832 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(newref));
833 }
834 break;
835 }
836 }
837 }
838 rev = g_slist_remove(rev, child);
839 }
840 } else {
841 g_slist_free(rev);
842 }
844 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_RAISE,
845 //TRANSLATORS: only translate "string" in "context|string".
846 // For more details, see http://developer.gnome.org/doc/API/2.0/glib/glib-I18N.html#Q-:CAPS
847 // "Raise" means "to raise an object" in the undo history
848 Q_("undo_action|Raise"));
849 }
851 void sp_selection_raise_to_top(SPDesktop *desktop)
852 {
853 if (desktop == NULL)
854 return;
856 SPDocument *document = sp_desktop_document(desktop);
857 Inkscape::Selection *selection = sp_desktop_selection(desktop);
859 if (selection->isEmpty()) {
860 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise to top."));
861 return;
862 }
864 GSList const *items = (GSList *) selection->itemList();
866 SPGroup const *group = sp_item_list_common_parent_group(items);
867 if (!group) {
868 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
869 return;
870 }
872 GSList *rl = g_slist_copy((GSList *) selection->reprList());
873 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
875 for (GSList *l = rl; l != NULL; l = l->next) {
876 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
877 repr->setPosition(-1);
878 }
880 g_slist_free(rl);
882 sp_document_done(document, SP_VERB_SELECTION_TO_FRONT,
883 _("Raise to top"));
884 }
886 void
887 sp_selection_lower(SPDesktop *desktop)
888 {
889 if (desktop == NULL)
890 return;
892 Inkscape::Selection *selection = sp_desktop_selection(desktop);
894 GSList const *items = (GSList *) selection->itemList();
895 if (!items) {
896 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower."));
897 return;
898 }
900 SPGroup const *group = sp_item_list_common_parent_group(items);
901 if (!group) {
902 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
903 return;
904 }
906 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
908 // Determine the common bbox of the selected items.
909 Geom::OptRect selected = enclose_items(items);
911 /* Construct direct-ordered list of selected children. */
912 GSList *rev = g_slist_copy((GSList *) items);
913 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
914 rev = g_slist_reverse(rev);
916 // Iterate over all objects in the selection (starting from top).
917 if (selected) {
918 while (rev) {
919 SPObject *child = SP_OBJECT(rev->data);
920 // for each selected object, find the prev sibling
921 for (SPObject *newref = prev_sibling(child); newref; newref = prev_sibling(newref)) {
922 // if the sibling is an item AND overlaps our selection,
923 if (SP_IS_ITEM(newref)) {
924 Geom::OptRect ref_bbox = sp_item_bbox_desktop(SP_ITEM(newref));
925 if ( ref_bbox && selected->intersects(*ref_bbox) ) {
926 // AND if it's not one of our selected objects,
927 if (!g_slist_find((GSList *) items, newref)) {
928 // move the selected object before that sibling
929 SPObject *put_after = prev_sibling(newref);
930 if (put_after)
931 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(put_after));
932 else
933 SP_OBJECT_REPR(child)->setPosition(0);
934 }
935 break;
936 }
937 }
938 }
939 rev = g_slist_remove(rev, child);
940 }
941 } else {
942 g_slist_free(rev);
943 }
945 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_LOWER,
946 _("Lower"));
947 }
949 void sp_selection_lower_to_bottom(SPDesktop *desktop)
950 {
951 if (desktop == NULL)
952 return;
954 SPDocument *document = sp_desktop_document(desktop);
955 Inkscape::Selection *selection = sp_desktop_selection(desktop);
957 if (selection->isEmpty()) {
958 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower to bottom."));
959 return;
960 }
962 GSList const *items = (GSList *) selection->itemList();
964 SPGroup const *group = sp_item_list_common_parent_group(items);
965 if (!group) {
966 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
967 return;
968 }
970 GSList *rl;
971 rl = g_slist_copy((GSList *) selection->reprList());
972 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
973 rl = g_slist_reverse(rl);
975 for (GSList *l = rl; l != NULL; l = l->next) {
976 gint minpos;
977 SPObject *pp, *pc;
978 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
979 pp = document->getObjectByRepr(sp_repr_parent(repr));
980 minpos = 0;
981 g_assert(SP_IS_GROUP(pp));
982 pc = sp_object_first_child(pp);
983 while (!SP_IS_ITEM(pc)) {
984 minpos += 1;
985 pc = pc->next;
986 }
987 repr->setPosition(minpos);
988 }
990 g_slist_free(rl);
992 sp_document_done(document, SP_VERB_SELECTION_TO_BACK,
993 _("Lower to bottom"));
994 }
996 void
997 sp_undo(SPDesktop *desktop, SPDocument *)
998 {
999 if (!sp_document_undo(sp_desktop_document(desktop)))
1000 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to undo."));
1001 }
1003 void
1004 sp_redo(SPDesktop *desktop, SPDocument *)
1005 {
1006 if (!sp_document_redo(sp_desktop_document(desktop)))
1007 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to redo."));
1008 }
1010 void sp_selection_cut(SPDesktop *desktop)
1011 {
1012 sp_selection_copy();
1013 sp_selection_delete(desktop);
1014 }
1016 /**
1017 * \pre item != NULL
1018 */
1019 SPCSSAttr *
1020 take_style_from_item(SPItem *item)
1021 {
1022 // write the complete cascaded style, context-free
1023 SPCSSAttr *css = sp_css_attr_from_object(SP_OBJECT(item), SP_STYLE_FLAG_ALWAYS);
1024 if (css == NULL)
1025 return NULL;
1027 if ((SP_IS_GROUP(item) && SP_OBJECT(item)->children) ||
1028 (SP_IS_TEXT(item) && SP_OBJECT(item)->children && SP_OBJECT(item)->children->next == NULL)) {
1029 // if this is a text with exactly one tspan child, merge the style of that tspan as well
1030 // If this is a group, merge the style of its topmost (last) child with style
1031 for (SPObject *last_element = item->lastChild(); last_element != NULL; last_element = SP_OBJECT_PREV(last_element)) {
1032 if (SP_OBJECT_STYLE(last_element) != NULL) {
1033 SPCSSAttr *temp = sp_css_attr_from_object(last_element, SP_STYLE_FLAG_IFSET);
1034 if (temp) {
1035 sp_repr_css_merge(css, temp);
1036 sp_repr_css_attr_unref(temp);
1037 }
1038 break;
1039 }
1040 }
1041 }
1042 if (!(SP_IS_TEXT(item) || SP_IS_TSPAN(item) || SP_IS_TREF(item) || SP_IS_STRING(item))) {
1043 // do not copy text properties from non-text objects, it's confusing
1044 css = sp_css_attr_unset_text(css);
1045 }
1047 // FIXME: also transform gradient/pattern fills, by forking? NO, this must be nondestructive
1048 double ex = to_2geom(sp_item_i2doc_affine(item)).descrim();
1049 if (ex != 1.0) {
1050 css = sp_css_attr_scale(css, ex);
1051 }
1053 return css;
1054 }
1057 void sp_selection_copy()
1058 {
1059 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
1060 cm->copy();
1061 }
1063 void sp_selection_paste(SPDesktop *desktop, bool in_place)
1064 {
1065 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
1066 if (cm->paste(in_place))
1067 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_PASTE, _("Paste"));
1068 }
1070 void sp_selection_paste_style(SPDesktop *desktop)
1071 {
1072 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
1073 if (cm->pasteStyle())
1074 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_PASTE_STYLE, _("Paste style"));
1075 }
1078 void sp_selection_paste_livepatheffect(SPDesktop *desktop)
1079 {
1080 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
1081 if (cm->pastePathEffect())
1082 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_PASTE_LIVEPATHEFFECT,
1083 _("Paste live path effect"));
1084 }
1087 void sp_selection_remove_livepatheffect_impl(SPItem *item)
1088 {
1089 if ( item && SP_IS_LPE_ITEM(item) &&
1090 sp_lpe_item_has_path_effect(SP_LPE_ITEM(item))) {
1091 sp_lpe_item_remove_all_path_effects(SP_LPE_ITEM(item), false);
1092 }
1093 }
1095 void sp_selection_remove_livepatheffect(SPDesktop *desktop)
1096 {
1097 if (desktop == NULL) return;
1099 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1101 // check if something is selected
1102 if (selection->isEmpty()) {
1103 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to remove live path effects from."));
1104 return;
1105 }
1107 for ( GSList const *itemlist = selection->itemList(); itemlist != NULL; itemlist = g_slist_next(itemlist) ) {
1108 SPItem *item = reinterpret_cast<SPItem*>(itemlist->data);
1110 sp_selection_remove_livepatheffect_impl(item);
1112 }
1114 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_REMOVE_LIVEPATHEFFECT,
1115 _("Remove live path effect"));
1116 }
1118 void sp_selection_remove_filter(SPDesktop *desktop)
1119 {
1120 if (desktop == NULL) return;
1122 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1124 // check if something is selected
1125 if (selection->isEmpty()) {
1126 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to remove filters from."));
1127 return;
1128 }
1130 SPCSSAttr *css = sp_repr_css_attr_new();
1131 sp_repr_css_unset_property(css, "filter");
1132 sp_desktop_set_style(desktop, css);
1133 sp_repr_css_attr_unref(css);
1135 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_REMOVE_FILTER,
1136 _("Remove filter"));
1137 }
1140 void sp_selection_paste_size(SPDesktop *desktop, bool apply_x, bool apply_y)
1141 {
1142 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
1143 if (cm->pasteSize(false, apply_x, apply_y))
1144 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_PASTE_SIZE,
1145 _("Paste size"));
1146 }
1148 void sp_selection_paste_size_separately(SPDesktop *desktop, bool apply_x, bool apply_y)
1149 {
1150 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
1151 if (cm->pasteSize(true, apply_x, apply_y))
1152 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_PASTE_SIZE_SEPARATELY,
1153 _("Paste size separately"));
1154 }
1156 void sp_selection_to_next_layer(SPDesktop *dt, bool suppressDone)
1157 {
1158 Inkscape::Selection *selection = sp_desktop_selection(dt);
1160 // check if something is selected
1161 if (selection->isEmpty()) {
1162 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer above."));
1163 return;
1164 }
1166 GSList const *items = g_slist_copy((GSList *) selection->itemList());
1168 bool no_more = false; // Set to true, if no more layers above
1169 SPObject *next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer());
1170 if (next) {
1171 GSList *temp_clip = NULL;
1172 sp_selection_copy_impl(items, &temp_clip, sp_document_repr_doc(dt->doc()));
1173 sp_selection_delete_impl(items, false, false);
1174 next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
1175 GSList *copied;
1176 if (next) {
1177 copied = sp_selection_paste_impl(sp_desktop_document(dt), next, &temp_clip);
1178 } else {
1179 copied = sp_selection_paste_impl(sp_desktop_document(dt), dt->currentLayer(), &temp_clip);
1180 no_more = true;
1181 }
1182 selection->setReprList((GSList const *) copied);
1183 g_slist_free(copied);
1184 if (temp_clip) g_slist_free(temp_clip);
1185 if (next) dt->setCurrentLayer(next);
1186 if ( !suppressDone ) {
1187 sp_document_done(sp_desktop_document(dt), SP_VERB_LAYER_MOVE_TO_NEXT,
1188 _("Raise to next layer"));
1189 }
1190 } else {
1191 no_more = true;
1192 }
1194 if (no_more) {
1195 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers above."));
1196 }
1198 g_slist_free((GSList *) items);
1199 }
1201 void sp_selection_to_prev_layer(SPDesktop *dt, bool suppressDone)
1202 {
1203 Inkscape::Selection *selection = sp_desktop_selection(dt);
1205 // check if something is selected
1206 if (selection->isEmpty()) {
1207 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer below."));
1208 return;
1209 }
1211 GSList const *items = g_slist_copy((GSList *) selection->itemList());
1213 bool no_more = false; // Set to true, if no more layers below
1214 SPObject *next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer());
1215 if (next) {
1216 GSList *temp_clip = NULL;
1217 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
1218 sp_selection_delete_impl(items, false, false);
1219 next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
1220 GSList *copied;
1221 if (next) {
1222 copied = sp_selection_paste_impl(sp_desktop_document(dt), next, &temp_clip);
1223 } else {
1224 copied = sp_selection_paste_impl(sp_desktop_document(dt), dt->currentLayer(), &temp_clip);
1225 no_more = true;
1226 }
1227 selection->setReprList((GSList const *) copied);
1228 g_slist_free(copied);
1229 if (temp_clip) g_slist_free(temp_clip);
1230 if (next) dt->setCurrentLayer(next);
1231 if ( !suppressDone ) {
1232 sp_document_done(sp_desktop_document(dt), SP_VERB_LAYER_MOVE_TO_PREV,
1233 _("Lower to previous layer"));
1234 }
1235 } else {
1236 no_more = true;
1237 }
1239 if (no_more) {
1240 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers below."));
1241 }
1243 g_slist_free((GSList *) items);
1244 }
1246 bool
1247 selection_contains_original(SPItem *item, Inkscape::Selection *selection)
1248 {
1249 bool contains_original = false;
1251 bool is_use = SP_IS_USE(item);
1252 SPItem *item_use = item;
1253 SPItem *item_use_first = item;
1254 while (is_use && item_use && !contains_original)
1255 {
1256 item_use = sp_use_get_original(SP_USE(item_use));
1257 contains_original |= selection->includes(item_use);
1258 if (item_use == item_use_first)
1259 break;
1260 is_use = SP_IS_USE(item_use);
1261 }
1263 // If it's a tref, check whether the object containing the character
1264 // data is part of the selection
1265 if (!contains_original && SP_IS_TREF(item)) {
1266 contains_original = selection->includes(SP_TREF(item)->getObjectReferredTo());
1267 }
1269 return contains_original;
1270 }
1273 bool
1274 selection_contains_both_clone_and_original(Inkscape::Selection *selection)
1275 {
1276 bool clone_with_original = false;
1277 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1278 SPItem *item = SP_ITEM(l->data);
1279 clone_with_original |= selection_contains_original(item, selection);
1280 if (clone_with_original)
1281 break;
1282 }
1283 return clone_with_original;
1284 }
1286 /** Apply matrix to the selection. \a set_i2d is normally true, which means objects are in the
1287 original transform, synced with their reprs, and need to jump to the new transform in one go. A
1288 value of set_i2d==false is only used by seltrans when it's dragging objects live (not outlines); in
1289 that case, items are already in the new position, but the repr is in the old, and this function
1290 then simply updates the repr from item->transform.
1291 */
1292 void sp_selection_apply_affine(Inkscape::Selection *selection, Geom::Matrix const &affine, bool set_i2d, bool compensate)
1293 {
1294 if (selection->isEmpty())
1295 return;
1297 // For each perspective with a box in selection, check whether all boxes are selected and
1298 // unlink all non-selected boxes.
1299 Persp3D *persp;
1300 Persp3D *transf_persp;
1301 std::list<Persp3D *> plist = selection->perspList();
1302 for (std::list<Persp3D *>::iterator i = plist.begin(); i != plist.end(); ++i) {
1303 persp = (Persp3D *) (*i);
1305 if (!persp3d_has_all_boxes_in_selection (persp, selection)) {
1306 std::list<SPBox3D *> selboxes = selection->box3DList(persp);
1308 // create a new perspective as a copy of the current one and link the selected boxes to it
1309 transf_persp = persp3d_create_xml_element (SP_OBJECT_DOCUMENT(persp), persp->perspective_impl);
1311 for (std::list<SPBox3D *>::iterator b = selboxes.begin(); b != selboxes.end(); ++b)
1312 box3d_switch_perspectives(*b, persp, transf_persp);
1313 } else {
1314 transf_persp = persp;
1315 }
1317 persp3d_apply_affine_transformation(transf_persp, affine);
1318 }
1320 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1321 SPItem *item = SP_ITEM(l->data);
1323 Geom::Point old_center(0,0);
1324 if (set_i2d && item->isCenterSet())
1325 old_center = item->getCenter();
1327 #if 0 /* Re-enable this once persistent guides have a graphical indication.
1328 At the time of writing, this is the only place to re-enable. */
1329 sp_item_update_cns(*item, selection->desktop());
1330 #endif
1332 // we're moving both a clone and its original or any ancestor in clone chain?
1333 bool transform_clone_with_original = selection_contains_original(item, selection);
1334 // ...both a text-on-path and its path?
1335 bool transform_textpath_with_path = (SP_IS_TEXT_TEXTPATH(item) && selection->includes( sp_textpath_get_path_item(SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item)))) ));
1336 // ...both a flowtext and its frame?
1337 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)
1338 // ...both an offset and its source?
1339 bool transform_offset_with_source = (SP_IS_OFFSET(item) && SP_OFFSET(item)->sourceHref) && selection->includes( sp_offset_get_source(SP_OFFSET(item)) );
1341 // If we're moving a connector, we want to detach it
1342 // from shapes that aren't part of the selection, but
1343 // leave it attached if they are
1344 if (cc_item_is_connector(item)) {
1345 SPItem *attItem[2];
1346 SP_PATH(item)->connEndPair.getAttachedItems(attItem);
1348 for (int n = 0; n < 2; ++n) {
1349 if (!selection->includes(attItem[n])) {
1350 sp_conn_end_detach(item, n);
1351 }
1352 }
1353 }
1355 // "clones are unmoved when original is moved" preference
1356 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1357 int compensation = prefs->getInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
1358 bool prefs_unmoved = (compensation == SP_CLONE_COMPENSATION_UNMOVED);
1359 bool prefs_parallel = (compensation == SP_CLONE_COMPENSATION_PARALLEL);
1361 /* If this is a clone and it's selected along with its original, do not move it;
1362 * it will feel the transform of its original and respond to it itself.
1363 * Without this, a clone is doubly transformed, very unintuitive.
1364 *
1365 * Same for textpath if we are also doing ANY transform to its path: do not touch textpath,
1366 * letters cannot be squeezed or rotated anyway, they only refill the changed path.
1367 * Same for linked offset if we are also moving its source: do not move it. */
1368 if (transform_textpath_with_path || transform_offset_with_source) {
1369 // Restore item->transform field from the repr, in case it was changed by seltrans.
1370 sp_object_read_attr(SP_OBJECT(item), "transform");
1371 } else if (transform_flowtext_with_frame) {
1372 // apply the inverse of the region's transform to the <use> so that the flow remains
1373 // the same (even though the output itself gets transformed)
1374 for (SPObject *region = item->firstChild() ; region ; region = SP_OBJECT_NEXT(region)) {
1375 if (!SP_IS_FLOWREGION(region) && !SP_IS_FLOWREGIONEXCLUDE(region))
1376 continue;
1377 for (SPObject *use = region->firstChild() ; use ; use = SP_OBJECT_NEXT(use)) {
1378 if (!SP_IS_USE(use)) continue;
1379 sp_item_write_transform(SP_USE(use), SP_OBJECT_REPR(use), item->transform.inverse(), NULL, compensate);
1380 }
1381 }
1382 } else if (transform_clone_with_original) {
1383 // We are transforming a clone along with its original. The below matrix juggling is
1384 // necessary to ensure that they transform as a whole, i.e. the clone's induced
1385 // transform and its move compensation are both cancelled out.
1387 // restore item->transform field from the repr, in case it was changed by seltrans
1388 sp_object_read_attr(SP_OBJECT(item), "transform");
1390 // calculate the matrix we need to apply to the clone to cancel its induced transform from its original
1391 Geom::Matrix parent2dt = sp_item_i2d_affine(SP_ITEM(SP_OBJECT_PARENT(item)));
1392 Geom::Matrix t = parent2dt * affine * parent2dt.inverse();
1393 Geom::Matrix t_inv = t.inverse();
1394 Geom::Matrix result = t_inv * item->transform * t;
1396 if ((prefs_parallel || prefs_unmoved) && affine.isTranslation()) {
1397 // we need to cancel out the move compensation, too
1399 // find out the clone move, same as in sp_use_move_compensate
1400 Geom::Matrix parent = sp_use_get_parent_transform(SP_USE(item));
1401 Geom::Matrix clone_move = parent.inverse() * t * parent;
1403 if (prefs_parallel) {
1404 Geom::Matrix move = result * clone_move * t_inv;
1405 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &move, compensate);
1407 } else if (prefs_unmoved) {
1408 //if (SP_IS_USE(sp_use_get_original(SP_USE(item))))
1409 // clone_move = Geom::identity();
1410 Geom::Matrix move = result * clone_move;
1411 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &t, compensate);
1412 }
1414 } else {
1415 // just apply the result
1416 sp_item_write_transform(item, SP_OBJECT_REPR(item), result, &t, compensate);
1417 }
1419 } else {
1420 if (set_i2d) {
1421 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * (Geom::Matrix)affine);
1422 }
1423 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, NULL, compensate);
1424 }
1426 // if we're moving the actual object, not just updating the repr, we can transform the
1427 // center by the same matrix (only necessary for non-translations)
1428 if (set_i2d && item->isCenterSet() && !(affine.isTranslation() || affine.isIdentity())) {
1429 item->setCenter(old_center * affine);
1430 SP_OBJECT(item)->updateRepr();
1431 }
1432 }
1433 }
1435 void sp_selection_remove_transform(SPDesktop *desktop)
1436 {
1437 if (desktop == NULL)
1438 return;
1440 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1442 GSList const *l = (GSList *) selection->reprList();
1443 while (l != NULL) {
1444 ((Inkscape::XML::Node*)l->data)->setAttribute("transform", NULL, false);
1445 l = l->next;
1446 }
1448 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_FLATTEN,
1449 _("Remove transform"));
1450 }
1452 void
1453 sp_selection_scale_absolute(Inkscape::Selection *selection,
1454 double const x0, double const x1,
1455 double const y0, double const y1)
1456 {
1457 if (selection->isEmpty())
1458 return;
1460 Geom::OptRect const bbox(selection->bounds());
1461 if ( !bbox ) {
1462 return;
1463 }
1465 Geom::Translate const p2o(-bbox->min());
1467 Geom::Scale const newSize(x1 - x0,
1468 y1 - y0);
1469 Geom::Scale const scale( newSize * Geom::Scale(bbox->dimensions()).inverse() );
1470 Geom::Translate const o2n(x0, y0);
1471 Geom::Matrix const final( p2o * scale * o2n );
1473 sp_selection_apply_affine(selection, final);
1474 }
1477 void sp_selection_scale_relative(Inkscape::Selection *selection, Geom::Point const &align, Geom::Scale const &scale)
1478 {
1479 if (selection->isEmpty())
1480 return;
1482 Geom::OptRect const bbox(selection->bounds());
1484 if ( !bbox ) {
1485 return;
1486 }
1488 // FIXME: ARBITRARY LIMIT: don't try to scale above 1 Mpx, it won't display properly and will crash sooner or later anyway
1489 if ( bbox->dimensions()[Geom::X] * scale[Geom::X] > 1e6 ||
1490 bbox->dimensions()[Geom::Y] * scale[Geom::Y] > 1e6 )
1491 {
1492 return;
1493 }
1495 Geom::Translate const n2d(-align);
1496 Geom::Translate const d2n(align);
1497 Geom::Matrix const final( n2d * scale * d2n );
1498 sp_selection_apply_affine(selection, final);
1499 }
1501 void
1502 sp_selection_rotate_relative(Inkscape::Selection *selection, Geom::Point const ¢er, gdouble const angle_degrees)
1503 {
1504 Geom::Translate const d2n(center);
1505 Geom::Translate const n2d(-center);
1506 Geom::Rotate const rotate(Geom::Rotate::from_degrees(angle_degrees));
1507 Geom::Matrix const final( Geom::Matrix(n2d) * rotate * d2n );
1508 sp_selection_apply_affine(selection, final);
1509 }
1511 void
1512 sp_selection_skew_relative(Inkscape::Selection *selection, Geom::Point const &align, double dx, double dy)
1513 {
1514 Geom::Translate const d2n(align);
1515 Geom::Translate const n2d(-align);
1516 Geom::Matrix const skew(1, dy,
1517 dx, 1,
1518 0, 0);
1519 Geom::Matrix const final( n2d * skew * d2n );
1520 sp_selection_apply_affine(selection, final);
1521 }
1523 void sp_selection_move_relative(Inkscape::Selection *selection, Geom::Point const &move, bool compensate)
1524 {
1525 sp_selection_apply_affine(selection, Geom::Matrix(Geom::Translate(move)), true, compensate);
1526 }
1528 void sp_selection_move_relative(Inkscape::Selection *selection, double dx, double dy)
1529 {
1530 sp_selection_apply_affine(selection, Geom::Matrix(Geom::Translate(dx, dy)));
1531 }
1533 /**
1534 * @brief Rotates selected objects 90 degrees, either clock-wise or counter-clockwise, depending on the value of ccw
1535 */
1536 void sp_selection_rotate_90(SPDesktop *desktop, bool ccw)
1537 {
1538 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1540 if (selection->isEmpty())
1541 return;
1543 GSList const *l = selection->itemList();
1544 Geom::Rotate const rot_90(Geom::Point(0, ccw ? 1 : -1)); // pos. or neg. rotation, depending on the value of ccw
1545 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1546 SPItem *item = SP_ITEM(l2->data);
1547 sp_item_rotate_rel(item, rot_90);
1548 }
1550 sp_document_done(sp_desktop_document(desktop),
1551 ccw ? SP_VERB_OBJECT_ROTATE_90_CCW : SP_VERB_OBJECT_ROTATE_90_CW,
1552 ccw ? _("Rotate 90° CCW") : _("Rotate 90° CW"));
1553 }
1555 void
1556 sp_selection_rotate(Inkscape::Selection *selection, gdouble const angle_degrees)
1557 {
1558 if (selection->isEmpty())
1559 return;
1561 boost::optional<Geom::Point> center = selection->center();
1562 if (!center) {
1563 return;
1564 }
1566 sp_selection_rotate_relative(selection, *center, angle_degrees);
1568 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1569 ( ( angle_degrees > 0 )
1570 ? "selector:rotate:ccw"
1571 : "selector:rotate:cw" ),
1572 SP_VERB_CONTEXT_SELECT,
1573 _("Rotate"));
1574 }
1576 // helper function:
1577 static
1578 Geom::Point
1579 cornerFarthestFrom(Geom::Rect const &r, Geom::Point const &p){
1580 Geom::Point m = r.midpoint();
1581 unsigned i = 0;
1582 if (p[X] < m[X]) {
1583 i = 1;
1584 }
1585 if (p[Y] < m[Y]) {
1586 i = 3 - i;
1587 }
1588 return r.corner(i);
1589 }
1591 /**
1592 \param angle the angle in "angular pixels", i.e. how many visible pixels must move the outermost point of the rotated object
1593 */
1594 void
1595 sp_selection_rotate_screen(Inkscape::Selection *selection, gdouble angle)
1596 {
1597 if (selection->isEmpty())
1598 return;
1600 Geom::OptRect const bbox(selection->bounds());
1601 boost::optional<Geom::Point> center = selection->center();
1603 if ( !bbox || !center ) {
1604 return;
1605 }
1607 gdouble const zoom = selection->desktop()->current_zoom();
1608 gdouble const zmove = angle / zoom;
1609 gdouble const r = Geom::L2(cornerFarthestFrom(*bbox, *center) - *center);
1611 gdouble const zangle = 180 * atan2(zmove, r) / M_PI;
1613 sp_selection_rotate_relative(selection, *center, zangle);
1615 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1616 ( (angle > 0)
1617 ? "selector:rotate:ccw"
1618 : "selector:rotate:cw" ),
1619 SP_VERB_CONTEXT_SELECT,
1620 _("Rotate by pixels"));
1621 }
1623 void
1624 sp_selection_scale(Inkscape::Selection *selection, gdouble grow)
1625 {
1626 if (selection->isEmpty())
1627 return;
1629 Geom::OptRect const bbox(selection->bounds());
1630 if (!bbox) {
1631 return;
1632 }
1634 Geom::Point const center(bbox->midpoint());
1636 // you can't scale "do nizhe pola" (below zero)
1637 double const max_len = bbox->maxExtent();
1638 if ( max_len + grow <= 1e-3 ) {
1639 return;
1640 }
1642 double const times = 1.0 + grow / max_len;
1643 sp_selection_scale_relative(selection, center, Geom::Scale(times, times));
1645 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1646 ( (grow > 0)
1647 ? "selector:scale:larger"
1648 : "selector:scale:smaller" ),
1649 SP_VERB_CONTEXT_SELECT,
1650 _("Scale"));
1651 }
1653 void
1654 sp_selection_scale_screen(Inkscape::Selection *selection, gdouble grow_pixels)
1655 {
1656 sp_selection_scale(selection,
1657 grow_pixels / selection->desktop()->current_zoom());
1658 }
1660 void
1661 sp_selection_scale_times(Inkscape::Selection *selection, gdouble times)
1662 {
1663 if (selection->isEmpty())
1664 return;
1666 Geom::OptRect sel_bbox = selection->bounds();
1668 if (!sel_bbox) {
1669 return;
1670 }
1672 Geom::Point const center(sel_bbox->midpoint());
1673 sp_selection_scale_relative(selection, center, Geom::Scale(times, times));
1674 sp_document_done(sp_desktop_document(selection->desktop()), SP_VERB_CONTEXT_SELECT,
1675 _("Scale by whole factor"));
1676 }
1678 void
1679 sp_selection_move(SPDesktop *desktop, gdouble dx, gdouble dy)
1680 {
1681 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1682 if (selection->isEmpty()) {
1683 return;
1684 }
1686 sp_selection_move_relative(selection, dx, dy);
1688 if (dx == 0) {
1689 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical", SP_VERB_CONTEXT_SELECT,
1690 _("Move vertically"));
1691 } else if (dy == 0) {
1692 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal", SP_VERB_CONTEXT_SELECT,
1693 _("Move horizontally"));
1694 } else {
1695 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SELECT,
1696 _("Move"));
1697 }
1698 }
1700 void
1701 sp_selection_move_screen(SPDesktop *desktop, gdouble dx, gdouble dy)
1702 {
1703 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1704 if (selection->isEmpty()) {
1705 return;
1706 }
1708 // same as sp_selection_move but divide deltas by zoom factor
1709 gdouble const zoom = desktop->current_zoom();
1710 gdouble const zdx = dx / zoom;
1711 gdouble const zdy = dy / zoom;
1712 sp_selection_move_relative(selection, zdx, zdy);
1714 if (dx == 0) {
1715 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical", SP_VERB_CONTEXT_SELECT,
1716 _("Move vertically by pixels"));
1717 } else if (dy == 0) {
1718 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal", SP_VERB_CONTEXT_SELECT,
1719 _("Move horizontally by pixels"));
1720 } else {
1721 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SELECT,
1722 _("Move"));
1723 }
1724 }
1726 namespace {
1728 template <typename D>
1729 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
1730 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
1732 template <typename D>
1733 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items, SPObject *root,
1734 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
1736 struct Forward {
1737 typedef SPObject *Iterator;
1739 static Iterator children(SPObject *o) { return sp_object_first_child(o); }
1740 static Iterator siblings_after(SPObject *o) { return SP_OBJECT_NEXT(o); }
1741 static void dispose(Iterator /*i*/) {}
1743 static SPObject *object(Iterator i) { return i; }
1744 static Iterator next(Iterator i) { return SP_OBJECT_NEXT(i); }
1745 };
1747 struct Reverse {
1748 typedef GSList *Iterator;
1750 static Iterator children(SPObject *o) {
1751 return make_list(o->firstChild(), NULL);
1752 }
1753 static Iterator siblings_after(SPObject *o) {
1754 return make_list(SP_OBJECT_PARENT(o)->firstChild(), o);
1755 }
1756 static void dispose(Iterator i) {
1757 g_slist_free(i);
1758 }
1760 static SPObject *object(Iterator i) {
1761 return reinterpret_cast<SPObject *>(i->data);
1762 }
1763 static Iterator next(Iterator i) { return i->next; }
1765 private:
1766 static GSList *make_list(SPObject *object, SPObject *limit) {
1767 GSList *list=NULL;
1768 while ( object != limit ) {
1769 list = g_slist_prepend(list, object);
1770 object = SP_OBJECT_NEXT(object);
1771 }
1772 return list;
1773 }
1774 };
1776 }
1778 void
1779 sp_selection_item_next(SPDesktop *desktop)
1780 {
1781 g_return_if_fail(desktop != NULL);
1782 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1784 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1785 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs->getInt("/options/kbselection/inlayer", PREFS_SELECTION_LAYER);
1786 bool onlyvisible = prefs->getBool("/options/kbselection/onlyvisible", true);
1787 bool onlysensitive = prefs->getBool("/options/kbselection/onlysensitive", true);
1789 SPObject *root;
1790 if (PREFS_SELECTION_ALL != inlayer) {
1791 root = selection->activeContext();
1792 } else {
1793 root = desktop->currentRoot();
1794 }
1796 SPItem *item=next_item_from_list<Forward>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1798 if (item) {
1799 selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
1800 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1801 scroll_to_show_item(desktop, item);
1802 }
1803 }
1804 }
1806 void
1807 sp_selection_item_prev(SPDesktop *desktop)
1808 {
1809 SPDocument *document = sp_desktop_document(desktop);
1810 g_return_if_fail(document != NULL);
1811 g_return_if_fail(desktop != NULL);
1812 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1814 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1815 PrefsSelectionContext inlayer = (PrefsSelectionContext) prefs->getInt("/options/kbselection/inlayer", PREFS_SELECTION_LAYER);
1816 bool onlyvisible = prefs->getBool("/options/kbselection/onlyvisible", true);
1817 bool onlysensitive = prefs->getBool("/options/kbselection/onlysensitive", true);
1819 SPObject *root;
1820 if (PREFS_SELECTION_ALL != inlayer) {
1821 root = selection->activeContext();
1822 } else {
1823 root = desktop->currentRoot();
1824 }
1826 SPItem *item=next_item_from_list<Reverse>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1828 if (item) {
1829 selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
1830 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1831 scroll_to_show_item(desktop, item);
1832 }
1833 }
1834 }
1836 void sp_selection_next_patheffect_param(SPDesktop * dt)
1837 {
1838 if (!dt) return;
1840 Inkscape::Selection *selection = sp_desktop_selection(dt);
1841 if ( selection && !selection->isEmpty() ) {
1842 SPItem *item = selection->singleItem();
1843 if ( item && SP_IS_SHAPE(item)) {
1844 if (sp_lpe_item_has_path_effect(SP_LPE_ITEM(item))) {
1845 sp_lpe_item_edit_next_param_oncanvas(SP_LPE_ITEM(item), dt);
1846 } else {
1847 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("The selection has no applied path effect."));
1848 }
1849 }
1850 }
1851 }
1853 /*bool has_path_recursive(SPObject *obj)
1854 {
1855 if (!obj) return false;
1856 if (SP_IS_PATH(obj)) {
1857 return true;
1858 }
1859 if (SP_IS_GROUP(obj) || SP_IS_OBJECTGROUP(obj)) {
1860 for (SPObject *c = obj->children; c; c = c->next) {
1861 if (has_path_recursive(c)) return true;
1862 }
1863 }
1864 return false;
1865 }*/
1867 void sp_selection_edit_clip_or_mask(SPDesktop * /*dt*/, bool /*clip*/)
1868 {
1869 return;
1870 /*if (!dt) return;
1871 using namespace Inkscape::UI;
1873 Inkscape::Selection *selection = sp_desktop_selection(dt);
1874 if (!selection || selection->isEmpty()) return;
1876 GSList const *items = selection->itemList();
1877 bool has_path = false;
1878 for (GSList *i = const_cast<GSList*>(items); i; i= i->next) {
1879 SPItem *item = SP_ITEM(i->data);
1880 SPObject *search = clip
1881 ? SP_OBJECT(item->clip_ref ? item->clip_ref->getObject() : NULL)
1882 : SP_OBJECT(item->mask_ref ? item->mask_ref->getObject() : NULL);
1883 has_path |= has_path_recursive(search);
1884 if (has_path) break;
1885 }
1886 if (has_path) {
1887 if (!tools_isactive(dt, TOOLS_NODES)) {
1888 tools_switch(dt, TOOLS_NODES);
1889 }
1890 ink_node_tool_set_mode(INK_NODE_TOOL(dt->event_context),
1891 clip ? NODE_TOOL_EDIT_CLIPPING_PATHS : NODE_TOOL_EDIT_MASKS);
1892 } else if (clip) {
1893 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1894 _("The selection has no applied clip path."));
1895 } else {
1896 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1897 _("The selection has no applied mask."));
1898 }*/
1899 }
1902 namespace {
1904 template <typename D>
1905 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items,
1906 SPObject *root, bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
1907 {
1908 SPObject *current=root;
1909 while (items) {
1910 SPItem *item=SP_ITEM(items->data);
1911 if ( root->isAncestorOf(item) &&
1912 ( !only_in_viewport || desktop->isWithinViewport(item) ) )
1913 {
1914 current = item;
1915 break;
1916 }
1917 items = items->next;
1918 }
1920 GSList *path=NULL;
1921 while ( current != root ) {
1922 path = g_slist_prepend(path, current);
1923 current = SP_OBJECT_PARENT(current);
1924 }
1926 SPItem *next;
1927 // first, try from the current object
1928 next = next_item<D>(desktop, path, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1929 g_slist_free(path);
1931 if (!next) { // if we ran out of objects, start over at the root
1932 next = next_item<D>(desktop, NULL, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1933 }
1935 return next;
1936 }
1938 template <typename D>
1939 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
1940 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
1941 {
1942 typename D::Iterator children;
1943 typename D::Iterator iter;
1945 SPItem *found=NULL;
1947 if (path) {
1948 SPObject *object=reinterpret_cast<SPObject *>(path->data);
1949 g_assert(SP_OBJECT_PARENT(object) == root);
1950 if (desktop->isLayer(object)) {
1951 found = next_item<D>(desktop, path->next, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1952 }
1953 iter = children = D::siblings_after(object);
1954 } else {
1955 iter = children = D::children(root);
1956 }
1958 while ( iter && !found ) {
1959 SPObject *object=D::object(iter);
1960 if (desktop->isLayer(object)) {
1961 if (PREFS_SELECTION_LAYER != inlayer) { // recurse into sublayers
1962 found = next_item<D>(desktop, NULL, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1963 }
1964 } else if ( SP_IS_ITEM(object) &&
1965 ( !only_in_viewport || desktop->isWithinViewport(SP_ITEM(object)) ) &&
1966 ( !onlyvisible || !desktop->itemIsHidden(SP_ITEM(object))) &&
1967 ( !onlysensitive || !SP_ITEM(object)->isLocked()) &&
1968 !desktop->isLayer(SP_ITEM(object)) )
1969 {
1970 found = SP_ITEM(object);
1971 }
1972 iter = D::next(iter);
1973 }
1975 D::dispose(children);
1977 return found;
1978 }
1980 }
1982 /**
1983 * If \a item is not entirely visible then adjust visible area to centre on the centre on of
1984 * \a item.
1985 */
1986 void scroll_to_show_item(SPDesktop *desktop, SPItem *item)
1987 {
1988 Geom::Rect dbox = desktop->get_display_area();
1989 Geom::OptRect sbox = sp_item_bbox_desktop(item);
1991 if ( sbox && dbox.contains(*sbox) == false ) {
1992 Geom::Point const s_dt = sbox->midpoint();
1993 Geom::Point const s_w = desktop->d2w(s_dt);
1994 Geom::Point const d_dt = dbox.midpoint();
1995 Geom::Point const d_w = desktop->d2w(d_dt);
1996 Geom::Point const moved_w( d_w - s_w );
1997 gint const dx = (gint) moved_w[X];
1998 gint const dy = (gint) moved_w[Y];
1999 desktop->scroll_world(dx, dy);
2000 }
2001 }
2004 void
2005 sp_selection_clone(SPDesktop *desktop)
2006 {
2007 if (desktop == NULL)
2008 return;
2010 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2012 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
2014 // check if something is selected
2015 if (selection->isEmpty()) {
2016 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object</b> to clone."));
2017 return;
2018 }
2020 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
2022 selection->clear();
2024 // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
2025 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
2027 GSList *newsel = NULL;
2029 while (reprs) {
2030 Inkscape::XML::Node *sel_repr = (Inkscape::XML::Node *) reprs->data;
2031 Inkscape::XML::Node *parent = sp_repr_parent(sel_repr);
2033 Inkscape::XML::Node *clone = xml_doc->createElement("svg:use");
2034 clone->setAttribute("x", "0", false);
2035 clone->setAttribute("y", "0", false);
2036 clone->setAttribute("xlink:href", g_strdup_printf("#%s", sel_repr->attribute("id")), false);
2038 clone->setAttribute("inkscape:transform-center-x", sel_repr->attribute("inkscape:transform-center-x"), false);
2039 clone->setAttribute("inkscape:transform-center-y", sel_repr->attribute("inkscape:transform-center-y"), false);
2041 // add the new clone to the top of the original's parent
2042 parent->appendChild(clone);
2044 newsel = g_slist_prepend(newsel, clone);
2045 reprs = g_slist_remove(reprs, sel_repr);
2046 Inkscape::GC::release(clone);
2047 }
2049 // TRANSLATORS: only translate "string" in "context|string".
2050 // For more details, see http://developer.gnome.org/doc/API/2.0/glib/glib-I18N.html#Q-:CAPS
2051 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_CLONE,
2052 Q_("action|Clone"));
2054 selection->setReprList(newsel);
2056 g_slist_free(newsel);
2057 }
2059 void
2060 sp_selection_relink(SPDesktop *desktop)
2061 {
2062 if (!desktop)
2063 return;
2065 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2067 if (selection->isEmpty()) {
2068 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>clones</b> to relink."));
2069 return;
2070 }
2072 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
2073 const gchar *newid = cm->getFirstObjectID();
2074 if (!newid) {
2075 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Copy an <b>object</b> to clipboard to relink clones to."));
2076 return;
2077 }
2078 gchar *newref = g_strdup_printf("#%s", newid);
2080 // Get a copy of current selection.
2081 bool relinked = false;
2082 for (GSList *items = (GSList *) selection->itemList();
2083 items != NULL;
2084 items = items->next)
2085 {
2086 SPItem *item = (SPItem *) items->data;
2088 if (!SP_IS_USE(item))
2089 continue;
2091 SP_OBJECT_REPR(item)->setAttribute("xlink:href", newref);
2092 SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
2093 relinked = true;
2094 }
2096 g_free(newref);
2098 if (!relinked) {
2099 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No clones to relink</b> in the selection."));
2100 } else {
2101 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNLINK_CLONE,
2102 _("Relink clone"));
2103 }
2104 }
2107 void
2108 sp_selection_unlink(SPDesktop *desktop)
2109 {
2110 if (!desktop)
2111 return;
2113 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2115 if (selection->isEmpty()) {
2116 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>clones</b> to unlink."));
2117 return;
2118 }
2120 // Get a copy of current selection.
2121 GSList *new_select = NULL;
2122 bool unlinked = false;
2123 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2124 items != NULL;
2125 items = items->next)
2126 {
2127 SPItem *item = (SPItem *) items->data;
2129 if (SP_IS_TEXT(item)) {
2130 SPObject *tspan = sp_tref_convert_to_tspan(SP_OBJECT(item));
2132 if (tspan) {
2133 SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
2134 }
2136 // Set unlink to true, and fall into the next if which
2137 // will include this text item in the new selection
2138 unlinked = true;
2139 }
2141 if (!(SP_IS_USE(item) || SP_IS_TREF(item))) {
2142 // keep the non-use item in the new selection
2143 new_select = g_slist_prepend(new_select, item);
2144 continue;
2145 }
2147 SPItem *unlink;
2148 if (SP_IS_USE(item)) {
2149 unlink = sp_use_unlink(SP_USE(item));
2150 } else /*if (SP_IS_TREF(use))*/ {
2151 unlink = SP_ITEM(sp_tref_convert_to_tspan(SP_OBJECT(item)));
2152 }
2154 unlinked = true;
2155 // Add ungrouped items to the new selection.
2156 new_select = g_slist_prepend(new_select, unlink);
2157 }
2159 if (new_select) { // set new selection
2160 selection->clear();
2161 selection->setList(new_select);
2162 g_slist_free(new_select);
2163 }
2164 if (!unlinked) {
2165 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No clones to unlink</b> in the selection."));
2166 }
2168 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNLINK_CLONE,
2169 _("Unlink clone"));
2170 }
2172 void
2173 sp_select_clone_original(SPDesktop *desktop)
2174 {
2175 if (desktop == NULL)
2176 return;
2178 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2180 SPItem *item = selection->singleItem();
2182 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.");
2184 // Check if other than two objects are selected
2185 if (g_slist_length((GSList *) selection->itemList()) != 1 || !item) {
2186 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
2187 return;
2188 }
2190 SPItem *original = NULL;
2191 if (SP_IS_USE(item)) {
2192 original = sp_use_get_original(SP_USE(item));
2193 } else if (SP_IS_OFFSET(item) && SP_OFFSET(item)->sourceHref) {
2194 original = sp_offset_get_source(SP_OFFSET(item));
2195 } else if (SP_IS_TEXT_TEXTPATH(item)) {
2196 original = sp_textpath_get_path_item(SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))));
2197 } else if (SP_IS_FLOWTEXT(item)) {
2198 original = SP_FLOWTEXT(item)->get_frame(NULL); // first frame only
2199 } else { // it's an object that we don't know what to do with
2200 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
2201 return;
2202 }
2204 if (!original) {
2205 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>Cannot find</b> the object to select (orphaned clone, offset, textpath, flowed text?)"));
2206 return;
2207 }
2209 for (SPObject *o = original; o && !SP_IS_ROOT(o); o = SP_OBJECT_PARENT(o)) {
2210 if (SP_IS_DEFS(o)) {
2211 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("The object you're trying to select is <b>not visible</b> (it is in <defs>)"));
2212 return;
2213 }
2214 }
2216 if (original) {
2217 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
2218 bool highlight = prefs->getBool("/options/highlightoriginal/value");
2219 if (highlight) {
2220 Geom::OptRect a = item->getBounds(sp_item_i2d_affine(item));
2221 Geom::OptRect b = original->getBounds(sp_item_i2d_affine(original));
2222 if ( a && b ) {
2223 // draw a flashing line between the objects
2224 SPCurve *curve = new SPCurve();
2225 curve->moveto(a->midpoint());
2226 curve->lineto(b->midpoint());
2228 SPCanvasItem * canvasitem = sp_canvas_bpath_new(sp_desktop_tempgroup(desktop), curve);
2229 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(canvasitem), 0x0000ddff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT, 5, 3);
2230 sp_canvas_item_show(canvasitem);
2231 curve->unref();
2232 desktop->add_temporary_canvasitem(canvasitem, 1000);
2233 }
2234 }
2236 selection->clear();
2237 selection->set(original);
2238 if (SP_CYCLING == SP_CYCLE_FOCUS) {
2239 scroll_to_show_item(desktop, original);
2240 }
2241 }
2242 }
2245 void sp_selection_to_marker(SPDesktop *desktop, bool apply)
2246 {
2247 if (desktop == NULL)
2248 return;
2250 SPDocument *doc = sp_desktop_document(desktop);
2251 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2253 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2255 // check if something is selected
2256 if (selection->isEmpty()) {
2257 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to marker."));
2258 return;
2259 }
2261 sp_document_ensure_up_to_date(doc);
2262 Geom::OptRect r = selection->bounds();
2263 boost::optional<Geom::Point> c = selection->center();
2264 if ( !r || !c ) {
2265 return;
2266 }
2268 // calculate the transform to be applied to objects to move them to 0,0
2269 Geom::Point move_p = Geom::Point(0, sp_document_height(doc)) - *c;
2270 move_p[Geom::Y] = -move_p[Geom::Y];
2271 Geom::Matrix move = Geom::Matrix(Geom::Translate(move_p));
2273 GSList *items = g_slist_copy((GSList *) selection->itemList());
2275 items = g_slist_sort(items, (GCompareFunc) sp_object_compare_position);
2277 // bottommost object, after sorting
2278 SPObject *parent = SP_OBJECT_PARENT(items->data);
2280 Geom::Matrix parent_transform(sp_item_i2doc_affine(SP_ITEM(parent)));
2282 // remember the position of the first item
2283 gint pos = SP_OBJECT_REPR(items->data)->position();
2284 (void)pos; // TODO check why this was remembered
2286 // create a list of duplicates
2287 GSList *repr_copies = NULL;
2288 for (GSList *i = items; i != NULL; i = i->next) {
2289 Inkscape::XML::Node *dup = (SP_OBJECT_REPR(i->data))->duplicate(xml_doc);
2290 repr_copies = g_slist_prepend(repr_copies, dup);
2291 }
2293 Geom::Rect bounds(desktop->dt2doc(r->min()), desktop->dt2doc(r->max()));
2295 if (apply) {
2296 // delete objects so that their clones don't get alerted; this object will be restored shortly
2297 for (GSList *i = items; i != NULL; i = i->next) {
2298 SPObject *item = SP_OBJECT(i->data);
2299 item->deleteObject(false);
2300 }
2301 }
2303 // Hack: Temporarily set clone compensation to unmoved, so that we can move clone-originals
2304 // without disturbing clones.
2305 // See ActorAlign::on_button_click() in src/ui/dialog/align-and-distribute.cpp
2306 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
2307 int saved_compensation = prefs->getInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
2308 prefs->setInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
2310 gchar const *mark_id = generate_marker(repr_copies, bounds, doc,
2311 ( Geom::Matrix(Geom::Translate(desktop->dt2doc(
2312 Geom::Point(r->min()[Geom::X],
2313 r->max()[Geom::Y]))))
2314 * parent_transform.inverse() ),
2315 parent_transform * move);
2316 (void)mark_id;
2318 // restore compensation setting
2319 prefs->setInt("/options/clonecompensation/value", saved_compensation);
2322 g_slist_free(items);
2324 sp_document_done(doc, SP_VERB_EDIT_SELECTION_2_MARKER,
2325 _("Objects to marker"));
2326 }
2328 static void sp_selection_to_guides_recursive(SPItem *item, bool deleteitem, bool wholegroups) {
2329 if (SP_IS_GROUP(item) && !SP_IS_BOX3D(item) && !wholegroups) {
2330 for (GSList *i = sp_item_group_item_list(SP_GROUP(item)); i != NULL; i = i->next) {
2331 sp_selection_to_guides_recursive(SP_ITEM(i->data), deleteitem, wholegroups);
2332 }
2333 } else {
2334 sp_item_convert_item_to_guides(item);
2336 if (deleteitem) {
2337 SP_OBJECT(item)->deleteObject(true);
2338 }
2339 }
2340 }
2342 void sp_selection_to_guides(SPDesktop *desktop)
2343 {
2344 if (desktop == NULL)
2345 return;
2347 SPDocument *doc = sp_desktop_document(desktop);
2348 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2349 // we need to copy the list because it gets reset when objects are deleted
2350 GSList *items = g_slist_copy((GSList *) selection->itemList());
2352 if (!items) {
2353 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to guides."));
2354 return;
2355 }
2357 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
2358 bool deleteitem = !prefs->getBool("/tools/cvg_keep_objects", 0);
2359 bool wholegroups = prefs->getBool("/tools/cvg_convert_whole_groups", 0);
2361 for (GSList const *i = items; i != NULL; i = i->next) {
2362 sp_selection_to_guides_recursive(SP_ITEM(i->data), deleteitem, wholegroups);
2363 }
2365 sp_document_done(doc, SP_VERB_EDIT_SELECTION_2_GUIDES, _("Objects to guides"));
2366 }
2368 void
2369 sp_selection_tile(SPDesktop *desktop, bool apply)
2370 {
2371 if (desktop == NULL)
2372 return;
2374 SPDocument *doc = sp_desktop_document(desktop);
2375 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2377 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2379 // check if something is selected
2380 if (selection->isEmpty()) {
2381 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to pattern."));
2382 return;
2383 }
2385 sp_document_ensure_up_to_date(doc);
2386 Geom::OptRect r = selection->bounds();
2387 if ( !r ) {
2388 return;
2389 }
2391 // calculate the transform to be applied to objects to move them to 0,0
2392 Geom::Point move_p = Geom::Point(0, sp_document_height(doc)) - (r->min() + Geom::Point(0, r->dimensions()[Geom::Y]));
2393 move_p[Geom::Y] = -move_p[Geom::Y];
2394 Geom::Matrix move = Geom::Matrix(Geom::Translate(move_p));
2396 GSList *items = g_slist_copy((GSList *) selection->itemList());
2398 items = g_slist_sort(items, (GCompareFunc) sp_object_compare_position);
2400 // bottommost object, after sorting
2401 SPObject *parent = SP_OBJECT_PARENT(items->data);
2403 Geom::Matrix parent_transform(sp_item_i2doc_affine(SP_ITEM(parent)));
2405 // remember the position of the first item
2406 gint pos = SP_OBJECT_REPR(items->data)->position();
2408 // create a list of duplicates
2409 GSList *repr_copies = NULL;
2410 for (GSList *i = items; i != NULL; i = i->next) {
2411 Inkscape::XML::Node *dup = (SP_OBJECT_REPR(i->data))->duplicate(xml_doc);
2412 repr_copies = g_slist_prepend(repr_copies, dup);
2413 }
2414 // restore the z-order after prepends
2415 repr_copies = g_slist_reverse(repr_copies);
2417 Geom::Rect bounds(desktop->dt2doc(r->min()), desktop->dt2doc(r->max()));
2419 if (apply) {
2420 // delete objects so that their clones don't get alerted; this object will be restored shortly
2421 for (GSList *i = items; i != NULL; i = i->next) {
2422 SPObject *item = SP_OBJECT(i->data);
2423 item->deleteObject(false);
2424 }
2425 }
2427 // Hack: Temporarily set clone compensation to unmoved, so that we can move clone-originals
2428 // without disturbing clones.
2429 // See ActorAlign::on_button_click() in src/ui/dialog/align-and-distribute.cpp
2430 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
2431 int saved_compensation = prefs->getInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
2432 prefs->setInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
2434 gchar const *pat_id = pattern_tile(repr_copies, bounds, doc,
2435 ( Geom::Matrix(Geom::Translate(desktop->dt2doc(Geom::Point(r->min()[Geom::X],
2436 r->max()[Geom::Y]))))
2437 * to_2geom(parent_transform.inverse()) ),
2438 parent_transform * move);
2440 // restore compensation setting
2441 prefs->setInt("/options/clonecompensation/value", saved_compensation);
2443 if (apply) {
2444 Inkscape::XML::Node *rect = xml_doc->createElement("svg:rect");
2445 rect->setAttribute("style", g_strdup_printf("stroke:none;fill:url(#%s)", pat_id));
2447 Geom::Point min = bounds.min() * to_2geom(parent_transform.inverse());
2448 Geom::Point max = bounds.max() * to_2geom(parent_transform.inverse());
2450 sp_repr_set_svg_double(rect, "width", max[Geom::X] - min[Geom::X]);
2451 sp_repr_set_svg_double(rect, "height", max[Geom::Y] - min[Geom::Y]);
2452 sp_repr_set_svg_double(rect, "x", min[Geom::X]);
2453 sp_repr_set_svg_double(rect, "y", min[Geom::Y]);
2455 // restore parent and position
2456 SP_OBJECT_REPR(parent)->appendChild(rect);
2457 rect->setPosition(pos > 0 ? pos : 0);
2458 SPItem *rectangle = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(rect);
2460 Inkscape::GC::release(rect);
2462 selection->clear();
2463 selection->set(rectangle);
2464 }
2466 g_slist_free(items);
2468 sp_document_done(doc, SP_VERB_EDIT_TILE,
2469 _("Objects to pattern"));
2470 }
2472 void
2473 sp_selection_untile(SPDesktop *desktop)
2474 {
2475 if (desktop == NULL)
2476 return;
2478 SPDocument *doc = sp_desktop_document(desktop);
2479 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2481 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2483 // check if something is selected
2484 if (selection->isEmpty()) {
2485 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object with pattern fill</b> to extract objects from."));
2486 return;
2487 }
2489 GSList *new_select = NULL;
2491 bool did = false;
2493 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2494 items != NULL;
2495 items = items->next) {
2497 SPItem *item = (SPItem *) items->data;
2499 SPStyle *style = SP_OBJECT_STYLE(item);
2501 if (!style || !style->fill.isPaintserver())
2502 continue;
2504 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
2506 if (!SP_IS_PATTERN(server))
2507 continue;
2509 did = true;
2511 SPPattern *pattern = pattern_getroot(SP_PATTERN(server));
2513 Geom::Matrix pat_transform = to_2geom(pattern_patternTransform(SP_PATTERN(server)));
2514 pat_transform *= item->transform;
2516 for (SPObject *child = sp_object_first_child(SP_OBJECT(pattern)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2517 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate(xml_doc);
2518 SPItem *i = SP_ITEM(desktop->currentLayer()->appendChildRepr(copy));
2520 // FIXME: relink clones to the new canvas objects
2521 // use SPObject::setid when mental finishes it to steal ids of
2523 // this is needed to make sure the new item has curve (simply requestDisplayUpdate does not work)
2524 sp_document_ensure_up_to_date(doc);
2526 Geom::Matrix transform( i->transform * pat_transform );
2527 sp_item_write_transform(i, SP_OBJECT_REPR(i), transform);
2529 new_select = g_slist_prepend(new_select, i);
2530 }
2532 SPCSSAttr *css = sp_repr_css_attr_new();
2533 sp_repr_css_set_property(css, "fill", "none");
2534 sp_repr_css_change(SP_OBJECT_REPR(item), css, "style");
2535 }
2537 if (!did) {
2538 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No pattern fills</b> in the selection."));
2539 } else {
2540 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNTILE,
2541 _("Pattern to objects"));
2542 selection->setList(new_select);
2543 }
2544 }
2546 void
2547 sp_selection_get_export_hints(Inkscape::Selection *selection, char const **filename, float *xdpi, float *ydpi)
2548 {
2549 if (selection->isEmpty()) {
2550 return;
2551 }
2553 GSList const *reprlst = selection->reprList();
2554 bool filename_search = TRUE;
2555 bool xdpi_search = TRUE;
2556 bool ydpi_search = TRUE;
2558 for (; reprlst != NULL &&
2559 filename_search &&
2560 xdpi_search &&
2561 ydpi_search;
2562 reprlst = reprlst->next) {
2563 gchar const *dpi_string;
2564 Inkscape::XML::Node * repr = (Inkscape::XML::Node *)reprlst->data;
2566 if (filename_search) {
2567 *filename = repr->attribute("inkscape:export-filename");
2568 if (*filename != NULL)
2569 filename_search = FALSE;
2570 }
2572 if (xdpi_search) {
2573 dpi_string = NULL;
2574 dpi_string = repr->attribute("inkscape:export-xdpi");
2575 if (dpi_string != NULL) {
2576 *xdpi = atof(dpi_string);
2577 xdpi_search = FALSE;
2578 }
2579 }
2581 if (ydpi_search) {
2582 dpi_string = NULL;
2583 dpi_string = repr->attribute("inkscape:export-ydpi");
2584 if (dpi_string != NULL) {
2585 *ydpi = atof(dpi_string);
2586 ydpi_search = FALSE;
2587 }
2588 }
2589 }
2590 }
2592 void
2593 sp_document_get_export_hints(SPDocument *doc, char const **filename, float *xdpi, float *ydpi)
2594 {
2595 Inkscape::XML::Node * repr = sp_document_repr_root(doc);
2596 gchar const *dpi_string;
2598 *filename = repr->attribute("inkscape:export-filename");
2600 dpi_string = NULL;
2601 dpi_string = repr->attribute("inkscape:export-xdpi");
2602 if (dpi_string != NULL) {
2603 *xdpi = atof(dpi_string);
2604 }
2606 dpi_string = NULL;
2607 dpi_string = repr->attribute("inkscape:export-ydpi");
2608 if (dpi_string != NULL) {
2609 *ydpi = atof(dpi_string);
2610 }
2611 }
2613 void
2614 sp_selection_create_bitmap_copy(SPDesktop *desktop)
2615 {
2616 if (desktop == NULL)
2617 return;
2619 SPDocument *document = sp_desktop_document(desktop);
2620 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
2622 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2624 // check if something is selected
2625 if (selection->isEmpty()) {
2626 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to make a bitmap copy."));
2627 return;
2628 }
2630 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Rendering bitmap..."));
2631 // set "busy" cursor
2632 desktop->setWaitingCursor();
2634 // Get the bounding box of the selection
2635 NRRect bbox;
2636 sp_document_ensure_up_to_date(document);
2637 selection->bounds(&bbox);
2638 if (NR_RECT_DFLS_TEST_EMPTY(&bbox)) {
2639 desktop->clearWaitingCursor();
2640 return; // exceptional situation, so not bother with a translatable error message, just quit quietly
2641 }
2643 // List of the items to show; all others will be hidden
2644 GSList *items = g_slist_copy((GSList *) selection->itemList());
2646 // Sort items so that the topmost comes last
2647 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
2649 // Generate a random value from the current time (you may create bitmap from the same object(s)
2650 // multiple times, and this is done so that they don't clash)
2651 GTimeVal cu;
2652 g_get_current_time(&cu);
2653 guint current = (int) (cu.tv_sec * 1000000 + cu.tv_usec) % 1024;
2655 // Create the filename.
2656 gchar *const basename = g_strdup_printf("%s-%s-%u.png",
2657 document->name,
2658 SP_OBJECT_REPR(items->data)->attribute("id"),
2659 current);
2660 // Imagemagick is known not to handle spaces in filenames, so we replace anything but letters,
2661 // digits, and a few other chars, with "_"
2662 g_strcanon(basename, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.=+~$#@^&!?", '_');
2664 // Build the complete path by adding document base dir, if set, otherwise home dir
2665 gchar * directory = NULL;
2666 if (SP_DOCUMENT_URI(document)) {
2667 directory = g_dirname(SP_DOCUMENT_URI(document));
2668 }
2669 if (directory == NULL) {
2670 directory = homedir_path(NULL);
2671 }
2672 gchar *filepath = g_build_filename(directory, basename, NULL);
2674 //g_print("%s\n", filepath);
2676 // Remember parent and z-order of the topmost one
2677 gint pos = SP_OBJECT_REPR(g_slist_last(items)->data)->position();
2678 SPObject *parent_object = SP_OBJECT_PARENT(g_slist_last(items)->data);
2679 Inkscape::XML::Node *parent = SP_OBJECT_REPR(parent_object);
2681 // Calculate resolution
2682 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
2683 double res;
2684 int const prefs_res = prefs->getInt("/options/createbitmap/resolution", 0);
2685 int const prefs_min = prefs->getInt("/options/createbitmap/minsize", 0);
2686 if (0 < prefs_res) {
2687 // If it's given explicitly in prefs, take it
2688 res = prefs_res;
2689 } else if (0 < prefs_min) {
2690 // If minsize is given, look up minimum bitmap size (default 250 pixels) and calculate resolution from it
2691 res = PX_PER_IN * prefs_min / MIN((bbox.x1 - bbox.x0), (bbox.y1 - bbox.y0));
2692 } else {
2693 float hint_xdpi = 0, hint_ydpi = 0;
2694 char const *hint_filename;
2695 // take resolution hint from the selected objects
2696 sp_selection_get_export_hints(selection, &hint_filename, &hint_xdpi, &hint_ydpi);
2697 if (hint_xdpi != 0) {
2698 res = hint_xdpi;
2699 } else {
2700 // take resolution hint from the document
2701 sp_document_get_export_hints(document, &hint_filename, &hint_xdpi, &hint_ydpi);
2702 if (hint_xdpi != 0) {
2703 res = hint_xdpi;
2704 } else {
2705 // if all else fails, take the default 90 dpi
2706 res = PX_PER_IN;
2707 }
2708 }
2709 }
2711 // The width and height of the bitmap in pixels
2712 unsigned width = (unsigned) floor((bbox.x1 - bbox.x0) * res / PX_PER_IN);
2713 unsigned height =(unsigned) floor((bbox.y1 - bbox.y0) * res / PX_PER_IN);
2715 // Find out if we have to run an external filter
2716 gchar const *run = NULL;
2717 Glib::ustring filter = prefs->getString("/options/createbitmap/filter");
2718 if (!filter.empty()) {
2719 // filter command is given;
2720 // see if we have a parameter to pass to it
2721 Glib::ustring param1 = prefs->getString("/options/createbitmap/filter_param1");
2722 if (!param1.empty()) {
2723 if (param1[param1.length() - 1] == '%') {
2724 // if the param string ends with %, interpret it as a percentage of the image's max dimension
2725 gchar p1[256];
2726 g_ascii_dtostr(p1, 256, ceil(g_ascii_strtod(param1.data(), NULL) * MAX(width, height) / 100));
2727 // the first param is always the image filename, the second is param1
2728 run = g_strdup_printf("%s \"%s\" %s", filter.data(), filepath, p1);
2729 } else {
2730 // otherwise pass the param1 unchanged
2731 run = g_strdup_printf("%s \"%s\" %s", filter.data(), filepath, param1.data());
2732 }
2733 } else {
2734 // run without extra parameter
2735 run = g_strdup_printf("%s \"%s\"", filter.data(), filepath);
2736 }
2737 }
2739 // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
2740 Geom::Matrix eek(sp_item_i2d_affine(SP_ITEM(parent_object)));
2741 Geom::Matrix t;
2743 double shift_x = bbox.x0;
2744 double shift_y = bbox.y1;
2745 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
2746 shift_x = round(shift_x);
2747 shift_y = -round(-shift_y); // this gets correct rounding despite coordinate inversion, remove the negations when the inversion is gone
2748 }
2749 t = Geom::Scale(1, -1) * Geom::Translate(shift_x, shift_y) * eek.inverse();
2751 // Do the export
2752 sp_export_png_file(document, filepath,
2753 bbox.x0, bbox.y0, bbox.x1, bbox.y1,
2754 width, height, res, res,
2755 (guint32) 0xffffff00,
2756 NULL, NULL,
2757 true, /*bool force_overwrite,*/
2758 items);
2760 g_slist_free(items);
2762 // Run filter, if any
2763 if (run) {
2764 g_print("Running external filter: %s\n", run);
2765 int retval;
2766 retval = system(run);
2767 }
2769 // Import the image back
2770 GdkPixbuf *pb = gdk_pixbuf_new_from_file(filepath, NULL);
2771 if (pb) {
2772 // Create the repr for the image
2773 Inkscape::XML::Node * repr = xml_doc->createElement("svg:image");
2774 sp_embed_image(repr, pb, "image/png");
2775 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
2776 sp_repr_set_svg_double(repr, "width", width);
2777 sp_repr_set_svg_double(repr, "height", height);
2778 } else {
2779 sp_repr_set_svg_double(repr, "width", (bbox.x1 - bbox.x0));
2780 sp_repr_set_svg_double(repr, "height", (bbox.y1 - bbox.y0));
2781 }
2783 // Write transform
2784 gchar *c=sp_svg_transform_write(t);
2785 repr->setAttribute("transform", c);
2786 g_free(c);
2788 // add the new repr to the parent
2789 parent->appendChild(repr);
2791 // move to the saved position
2792 repr->setPosition(pos > 0 ? pos + 1 : 1);
2794 // Set selection to the new image
2795 selection->clear();
2796 selection->add(repr);
2798 // Clean up
2799 Inkscape::GC::release(repr);
2800 gdk_pixbuf_unref(pb);
2802 // Complete undoable transaction
2803 sp_document_done(document, SP_VERB_SELECTION_CREATE_BITMAP,
2804 _("Create bitmap"));
2805 }
2807 desktop->clearWaitingCursor();
2809 g_free(basename);
2810 g_free(filepath);
2811 }
2813 /**
2814 * \brief Creates a mask or clipPath from selection
2815 * Two different modes:
2816 * if applyToLayer, all selection is moved to DEFS as mask/clippath
2817 * and is applied to current layer
2818 * otherwise, topmost object is used as mask for other objects
2819 * If \a apply_clip_path parameter is true, clipPath is created, otherwise mask
2820 *
2821 */
2822 void
2823 sp_selection_set_mask(SPDesktop *desktop, bool apply_clip_path, bool apply_to_layer)
2824 {
2825 if (desktop == NULL)
2826 return;
2828 SPDocument *doc = sp_desktop_document(desktop);
2829 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2831 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2833 // check if something is selected
2834 bool is_empty = selection->isEmpty();
2835 if ( apply_to_layer && is_empty) {
2836 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to create clippath or mask from."));
2837 return;
2838 } else if (!apply_to_layer && ( is_empty || NULL == selection->itemList()->next )) {
2839 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select mask object and <b>object(s)</b> to apply clippath or mask to."));
2840 return;
2841 }
2843 // FIXME: temporary patch to prevent crash!
2844 // Remove this when bboxes are fixed to not blow up on an item clipped/masked with its own clone
2845 bool clone_with_original = selection_contains_both_clone_and_original(selection);
2846 if (clone_with_original) {
2847 return; // in this version, you cannot clip/mask an object with its own clone
2848 }
2849 // /END FIXME
2851 sp_document_ensure_up_to_date(doc);
2853 GSList *items = g_slist_copy((GSList *) selection->itemList());
2855 items = g_slist_sort(items, (GCompareFunc) sp_object_compare_position);
2857 // create a list of duplicates
2858 GSList *mask_items = NULL;
2859 GSList *apply_to_items = NULL;
2860 GSList *items_to_delete = NULL;
2861 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
2862 bool topmost = prefs->getBool("/options/maskobject/topmost", true);
2863 bool remove_original = prefs->getBool("/options/maskobject/remove", true);
2864 int grouping = prefs->getInt("/options/maskobject/grouping", PREFS_MASKOBJECT_GROUPING_NONE);
2866 if (apply_to_layer) {
2867 // all selected items are used for mask, which is applied to a layer
2868 apply_to_items = g_slist_prepend(apply_to_items, desktop->currentLayer());
2870 for (GSList *i = items; i != NULL; i = i->next) {
2871 Inkscape::XML::Node *dup = (SP_OBJECT_REPR(i->data))->duplicate(xml_doc);
2872 mask_items = g_slist_prepend(mask_items, dup);
2874 if (remove_original) {
2875 SPObject *item = SP_OBJECT(i->data);
2876 items_to_delete = g_slist_prepend(items_to_delete, item);
2877 }
2878 }
2879 } else if (!topmost) {
2880 // topmost item is used as a mask, which is applied to other items in a selection
2881 GSList *i = items;
2882 Inkscape::XML::Node *dup = (SP_OBJECT_REPR(i->data))->duplicate(xml_doc);
2883 mask_items = g_slist_prepend(mask_items, dup);
2885 if (remove_original) {
2886 SPObject *item = SP_OBJECT(i->data);
2887 items_to_delete = g_slist_prepend(items_to_delete, item);
2888 }
2890 for (i = i->next; i != NULL; i = i->next) {
2891 apply_to_items = g_slist_prepend(apply_to_items, i->data);
2892 }
2893 } else {
2894 GSList *i = NULL;
2895 for (i = items; NULL != i->next; i = i->next) {
2896 apply_to_items = g_slist_prepend(apply_to_items, i->data);
2897 }
2899 Inkscape::XML::Node *dup = (SP_OBJECT_REPR(i->data))->duplicate(xml_doc);
2900 mask_items = g_slist_prepend(mask_items, dup);
2902 if (remove_original) {
2903 SPObject *item = SP_OBJECT(i->data);
2904 items_to_delete = g_slist_prepend(items_to_delete, item);
2905 }
2906 }
2908 g_slist_free(items);
2909 items = NULL;
2911 if (apply_to_items && grouping == PREFS_MASKOBJECT_GROUPING_ALL) {
2912 // group all those objects into one group
2913 // and apply mask to that
2914 Inkscape::XML::Node *group = xml_doc->createElement("svg:g");
2916 // make a note we should ungroup this when unsetting mask
2917 group->setAttribute("inkscape:groupmode", "maskhelper");
2919 GSList *reprs_to_group = NULL;
2921 for (GSList *i = apply_to_items ; NULL != i ; i = i->next) {
2922 reprs_to_group = g_slist_prepend(reprs_to_group, SP_OBJECT_REPR(i->data));
2923 selection->remove(SP_OBJECT(i->data));
2924 }
2925 reprs_to_group = g_slist_reverse(reprs_to_group);
2927 sp_selection_group_impl(reprs_to_group, group, xml_doc, doc);
2929 g_slist_free(reprs_to_group);
2931 // apply clip/mask only to newly created group
2932 g_slist_free(apply_to_items);
2933 apply_to_items = NULL;
2934 apply_to_items = g_slist_prepend(apply_to_items, doc->getObjectByRepr(group));
2936 selection->add(group);
2938 Inkscape::GC::release(group);
2939 }
2941 gchar const *attributeName = apply_clip_path ? "clip-path" : "mask";
2942 for (GSList *i = apply_to_items; NULL != i; i = i->next) {
2943 SPItem *item = reinterpret_cast<SPItem *>(i->data);
2944 // inverted object transform should be applied to a mask object,
2945 // as mask is calculated in user space (after applying transform)
2946 Geom::Matrix maskTransform(item->transform.inverse());
2948 GSList *mask_items_dup = NULL;
2949 for (GSList *mask_item = mask_items; NULL != mask_item; mask_item = mask_item->next) {
2950 Inkscape::XML::Node *dup = reinterpret_cast<Inkscape::XML::Node *>(mask_item->data)->duplicate(xml_doc);
2951 mask_items_dup = g_slist_prepend(mask_items_dup, dup);
2952 }
2954 gchar const *mask_id = NULL;
2955 if (apply_clip_path) {
2956 mask_id = sp_clippath_create(mask_items_dup, doc, &maskTransform);
2957 } else {
2958 mask_id = sp_mask_create(mask_items_dup, doc, &maskTransform);
2959 }
2961 g_slist_free(mask_items_dup);
2962 mask_items_dup = NULL;
2964 Inkscape::XML::Node *current = SP_OBJECT_REPR(i->data);
2965 // Node to apply mask to
2966 Inkscape::XML::Node *apply_mask_to = current;
2968 if (grouping == PREFS_MASKOBJECT_GROUPING_SEPARATE) {
2969 // enclose current node in group, and apply crop/mask on that
2970 Inkscape::XML::Node *group = xml_doc->createElement("svg:g");
2971 // make a note we should ungroup this when unsetting mask
2972 group->setAttribute("inkscape:groupmode", "maskhelper");
2974 Inkscape::XML::Node *spnew = current->duplicate(xml_doc);
2975 gint position = current->position();
2976 selection->remove(current);
2977 current->parent()->appendChild(group);
2978 sp_repr_unparent(current);
2979 group->appendChild(spnew);
2980 group->setPosition(position);
2982 // Apply clip/mask to group instead
2983 apply_mask_to = group;
2985 selection->add(group);
2986 Inkscape::GC::release(spnew);
2987 Inkscape::GC::release(group);
2988 }
2990 apply_mask_to->setAttribute(attributeName, g_strdup_printf("url(#%s)", mask_id));
2992 }
2994 g_slist_free(mask_items);
2995 g_slist_free(apply_to_items);
2997 for (GSList *i = items_to_delete; NULL != i; i = i->next) {
2998 SPObject *item = SP_OBJECT(i->data);
2999 item->deleteObject(false);
3000 }
3001 g_slist_free(items_to_delete);
3003 if (apply_clip_path)
3004 sp_document_done(doc, SP_VERB_OBJECT_SET_CLIPPATH, _("Set clipping path"));
3005 else
3006 sp_document_done(doc, SP_VERB_OBJECT_SET_MASK, _("Set mask"));
3007 }
3009 void sp_selection_unset_mask(SPDesktop *desktop, bool apply_clip_path) {
3010 if (desktop == NULL)
3011 return;
3013 SPDocument *doc = sp_desktop_document(desktop);
3014 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
3015 Inkscape::Selection *selection = sp_desktop_selection(desktop);
3017 // check if something is selected
3018 if (selection->isEmpty()) {
3019 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to remove clippath or mask from."));
3020 return;
3021 }
3023 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
3024 bool remove_original = prefs->getBool("/options/maskobject/remove", true);
3025 bool ungroup_masked = prefs->getBool("/options/maskobject/ungrouping", true);
3026 sp_document_ensure_up_to_date(doc);
3028 gchar const *attributeName = apply_clip_path ? "clip-path" : "mask";
3029 std::map<SPObject*,SPItem*> referenced_objects;
3031 GSList *items_to_ungroup = NULL;
3033 // SPObject* refers to a group containing the clipped path or mask itself,
3034 // whereas SPItem* refers to the item being clipped or masked
3035 for (GSList const *i = selection->itemList(); NULL != i; i = i->next) {
3036 if (remove_original) {
3037 // remember referenced mask/clippath, so orphaned masks can be moved back to document
3038 SPItem *item = reinterpret_cast<SPItem *>(i->data);
3039 Inkscape::URIReference *uri_ref = NULL;
3041 if (apply_clip_path) {
3042 uri_ref = item->clip_ref;
3043 } else {
3044 uri_ref = item->mask_ref;
3045 }
3047 // collect distinct mask object (and associate with item to apply transform)
3048 if (NULL != uri_ref && NULL != uri_ref->getObject()) {
3049 referenced_objects[uri_ref->getObject()] = item;
3050 }
3051 }
3053 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, "none");
3055 if (ungroup_masked && SP_IS_GROUP(i->data)) {
3056 // if we had previously enclosed masked object in group,
3057 // add it to list so we can ungroup it later
3058 SPGroup *item = SP_GROUP(i->data);
3060 // ungroup only groups we created when setting clip/mask
3061 if (item->layerMode() == SPGroup::MASK_HELPER) {
3062 items_to_ungroup = g_slist_prepend(items_to_ungroup, item);
3063 }
3065 }
3066 }
3068 // restore mask objects into a document
3069 for ( std::map<SPObject*,SPItem*>::iterator it = referenced_objects.begin() ; it != referenced_objects.end() ; ++it) {
3070 SPObject *obj = (*it).first; // Group containing the clipped paths or masks
3071 GSList *items_to_move = NULL;
3072 for (SPObject *child = sp_object_first_child(obj) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
3073 // Collect all clipped paths and masks within a single group
3074 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate(xml_doc);
3075 items_to_move = g_slist_prepend(items_to_move, copy);
3076 }
3078 if (!obj->isReferenced()) {
3079 // delete from defs if no other object references this mask
3080 obj->deleteObject(false);
3081 }
3083 // remember parent and position of the item to which the clippath/mask was applied
3084 Inkscape::XML::Node *parent = SP_OBJECT_REPR((*it).second)->parent();
3085 gint pos = SP_OBJECT_REPR((*it).second)->position();
3087 // Iterate through all clipped paths / masks
3088 for (GSList *i = items_to_move; NULL != i; i = i->next) {
3089 Inkscape::XML::Node *repr = (Inkscape::XML::Node *)i->data;
3091 // insert into parent, restore pos
3092 parent->appendChild(repr);
3093 repr->setPosition((pos + 1) > 0 ? (pos + 1) : 0);
3095 SPItem *mask_item = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
3096 selection->add(repr);
3098 // transform mask, so it is moved the same spot where mask was applied
3099 Geom::Matrix transform(mask_item->transform);
3100 transform *= (*it).second->transform;
3101 sp_item_write_transform(mask_item, SP_OBJECT_REPR(mask_item), transform);
3102 }
3104 g_slist_free(items_to_move);
3105 }
3107 // ungroup marked groups added when setting mask
3108 for (GSList *i = items_to_ungroup ; NULL != i ; i = i->next) {
3109 selection->remove(SP_GROUP(i->data));
3110 GSList *children = NULL;
3111 sp_item_group_ungroup(SP_GROUP(i->data), &children, false);
3112 selection->addList(children);
3113 g_slist_free(children);
3114 }
3116 g_slist_free(items_to_ungroup);
3118 if (apply_clip_path)
3119 sp_document_done(doc, SP_VERB_OBJECT_UNSET_CLIPPATH, _("Release clipping path"));
3120 else
3121 sp_document_done(doc, SP_VERB_OBJECT_UNSET_MASK, _("Release mask"));
3122 }
3124 /**
3125 * \param with_margins margins defined in the xml under <sodipodi:namedview>
3126 * "fit-margin-..." attributes. See SPDocument::fitToRect.
3127 * \return true if an undoable change should be recorded.
3128 */
3129 bool
3130 fit_canvas_to_selection(SPDesktop *desktop, bool with_margins)
3131 {
3132 g_return_val_if_fail(desktop != NULL, false);
3133 SPDocument *doc = sp_desktop_document(desktop);
3135 g_return_val_if_fail(doc != NULL, false);
3136 g_return_val_if_fail(desktop->selection != NULL, false);
3138 if (desktop->selection->isEmpty()) {
3139 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to fit canvas to."));
3140 return false;
3141 }
3142 Geom::OptRect const bbox(desktop->selection->bounds());
3143 if (bbox) {
3144 doc->fitToRect(*bbox, with_margins);
3145 return true;
3146 } else {
3147 return false;
3148 }
3149 }
3151 /**
3152 * Fit canvas to the bounding box of the selection, as an undoable action.
3153 */
3154 void
3155 verb_fit_canvas_to_selection(SPDesktop *const desktop)
3156 {
3157 if (fit_canvas_to_selection(desktop)) {
3158 sp_document_done(sp_desktop_document(desktop), SP_VERB_FIT_CANVAS_TO_SELECTION,
3159 _("Fit Page to Selection"));
3160 }
3161 }
3163 /**
3164 * \param with_margins margins defined in the xml under <sodipodi:namedview>
3165 * "fit-margin-..." attributes. See SPDocument::fitToRect.
3166 */
3167 bool
3168 fit_canvas_to_drawing(SPDocument *doc, bool with_margins)
3169 {
3170 g_return_val_if_fail(doc != NULL, false);
3172 sp_document_ensure_up_to_date(doc);
3173 SPItem const *const root = SP_ITEM(doc->root);
3174 Geom::OptRect const bbox(root->getBounds(sp_item_i2d_affine(root)));
3175 if (bbox) {
3176 doc->fitToRect(*bbox, with_margins);
3177 return true;
3178 } else {
3179 return false;
3180 }
3181 }
3183 void
3184 verb_fit_canvas_to_drawing(SPDesktop *desktop)
3185 {
3186 if (fit_canvas_to_drawing(sp_desktop_document(desktop))) {
3187 sp_document_done(sp_desktop_document(desktop), SP_VERB_FIT_CANVAS_TO_DRAWING,
3188 _("Fit Page to Drawing"));
3189 }
3190 }
3192 /**
3193 * Fits canvas to selection or drawing with margins from <sodipodi:namedview>
3194 * "fit-margin-..." attributes. See SPDocument::fitToRect and
3195 * ui/dialog/page-sizer.
3196 */
3197 void fit_canvas_to_selection_or_drawing(SPDesktop *desktop) {
3198 g_return_if_fail(desktop != NULL);
3199 SPDocument *doc = sp_desktop_document(desktop);
3201 g_return_if_fail(doc != NULL);
3202 g_return_if_fail(desktop->selection != NULL);
3204 bool const changed = ( desktop->selection->isEmpty()
3205 ? fit_canvas_to_drawing(doc, true)
3206 : fit_canvas_to_selection(desktop, true) );
3207 if (changed) {
3208 sp_document_done(sp_desktop_document(desktop), SP_VERB_FIT_CANVAS_TO_SELECTION_OR_DRAWING,
3209 _("Fit Page to Selection or Drawing"));
3210 }
3211 };
3213 static void itemtree_map(void (*f)(SPItem *, SPDesktop *), SPObject *root, SPDesktop *desktop) {
3214 // don't operate on layers
3215 if (SP_IS_ITEM(root) && !desktop->isLayer(SP_ITEM(root))) {
3216 f(SP_ITEM(root), desktop);
3217 }
3218 for ( SPObject::SiblingIterator iter = root->firstChild() ; iter ; ++iter ) {
3219 //don't recurse into locked layers
3220 if (!(SP_IS_ITEM(&*iter) && desktop->isLayer(SP_ITEM(&*iter)) && SP_ITEM(&*iter)->isLocked())) {
3221 itemtree_map(f, iter, desktop);
3222 }
3223 }
3224 }
3226 static void unlock(SPItem *item, SPDesktop */*desktop*/) {
3227 if (item->isLocked()) {
3228 item->setLocked(FALSE);
3229 }
3230 }
3232 static void unhide(SPItem *item, SPDesktop *desktop) {
3233 if (desktop->itemIsHidden(item)) {
3234 item->setExplicitlyHidden(FALSE);
3235 }
3236 }
3238 static void process_all(void (*f)(SPItem *, SPDesktop *), SPDesktop *dt, bool layer_only) {
3239 if (!dt) return;
3241 SPObject *root;
3242 if (layer_only) {
3243 root = dt->currentLayer();
3244 } else {
3245 root = dt->currentRoot();
3246 }
3248 itemtree_map(f, root, dt);
3249 }
3251 void unlock_all(SPDesktop *dt) {
3252 process_all(&unlock, dt, true);
3253 }
3255 void unlock_all_in_all_layers(SPDesktop *dt) {
3256 process_all(&unlock, dt, false);
3257 }
3259 void unhide_all(SPDesktop *dt) {
3260 process_all(&unhide, dt, true);
3261 }
3263 void unhide_all_in_all_layers(SPDesktop *dt) {
3264 process_all(&unhide, dt, false);
3265 }
3268 /*
3269 Local Variables:
3270 mode:c++
3271 c-file-style:"stroustrup"
3272 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
3273 indent-tabs-mode:nil
3274 fill-column:99
3275 End:
3276 */
3277 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :