62f5c57e2e80c1be0677597912b01ec41609a871
1 #define __SP_SELECTION_CHEMISTRY_C__
3 /*
4 * Miscellanous operations on selected items
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * Frank Felfe <innerspace@iname.com>
9 * MenTaLguY <mental@rydia.net>
10 * bulia byak <buliabyak@users.sf.net>
11 * Andrius R. <knutux@gmail.com>
12 *
13 * Copyright (C) 1999-2006 authors
14 * Copyright (C) 2001-2002 Ximian, Inc.
15 *
16 * Released under GNU GPL, read the file 'COPYING' for more information
17 */
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
23 #include "selection-chemistry.h"
25 #include <gtkmm/clipboard.h>
27 #include "svg/svg.h"
28 #include "inkscape.h"
29 #include "desktop.h"
30 #include "desktop-style.h"
31 #include "selection.h"
32 #include "tools-switch.h"
33 #include "desktop-handles.h"
34 #include "message-stack.h"
35 #include "sp-item-transform.h"
36 #include "marker.h"
37 #include "sp-use.h"
38 #include "sp-textpath.h"
39 #include "sp-tspan.h"
40 #include "sp-tref.h"
41 #include "sp-flowtext.h"
42 #include "sp-flowregion.h"
43 #include "text-editing.h"
44 #include "text-context.h"
45 #include "connector-context.h"
46 #include "sp-path.h"
47 #include "sp-conn-end.h"
48 #include "dropper-context.h"
49 #include <glibmm/i18n.h>
50 #include "libnr/nr-matrix-rotate-ops.h"
51 #include "libnr/nr-matrix-translate-ops.h"
52 #include "libnr/nr-rotate-fns.h"
53 #include "libnr/nr-scale-ops.h"
54 #include "libnr/nr-scale-translate-ops.h"
55 #include "libnr/nr-translate-matrix-ops.h"
56 #include "libnr/nr-translate-scale-ops.h"
57 #include "xml/repr.h"
58 #include "style.h"
59 #include "document-private.h"
60 #include "sp-gradient.h"
61 #include "sp-gradient-reference.h"
62 #include "sp-linear-gradient-fns.h"
63 #include "sp-pattern.h"
64 #include "sp-radial-gradient-fns.h"
65 #include "sp-namedview.h"
66 #include "prefs-utils.h"
67 #include "sp-offset.h"
68 #include "sp-clippath.h"
69 #include "sp-mask.h"
70 #include "file.h"
71 #include "helper/png-write.h"
72 #include "layer-fns.h"
73 #include "context-fns.h"
74 #include <map>
75 #include <cstring>
76 #include <string>
77 #include "helper/units.h"
78 #include "sp-item.h"
79 #include "box3d.h"
80 #include "unit-constants.h"
81 #include "xml/simple-document.h"
82 #include "sp-filter-reference.h"
83 #include "gradient-drag.h"
84 #include "uri-references.h"
85 #include "live_effects/lpeobject.h"
86 #include "live_effects/parameter/path.h"
87 #include "libnr/nr-convert2geom.h"
89 // For clippath editing
90 #include "tools-switch.h"
91 #include "shape-editor.h"
92 #include "node-context.h"
93 #include "nodepath.h"
95 using NR::X;
96 using NR::Y;
98 /* fixme: find a better place */
99 Inkscape::XML::Document *clipboard_document = NULL;
100 GSList *clipboard = NULL;
101 GSList *defs_clipboard = NULL;
102 SPCSSAttr *style_clipboard = NULL;
103 NR::Maybe<NR::Rect> size_clipboard;
105 static void sp_copy_stuff_used_by_item(GSList **defs_clip, SPItem *item, GSList const *items, Inkscape::XML::Document* xml_doc);
107 /**
108 * Copies repr and its inherited css style elements, along with the accumulated transform 'full_t',
109 * then prepends the copy to 'clip'.
110 */
111 void sp_selection_copy_one (Inkscape::XML::Node *repr, NR::Matrix full_t, GSList **clip, Inkscape::XML::Document* xml_doc)
112 {
113 Inkscape::XML::Node *copy = repr->duplicate(xml_doc);
115 // copy complete inherited style
116 SPCSSAttr *css = sp_repr_css_attr_inherited(repr, "style");
117 sp_repr_css_set(copy, css, "style");
118 sp_repr_css_attr_unref(css);
120 // write the complete accumulated transform passed to us
121 // (we're dealing with unattached repr, so we write to its attr
122 // instead of using sp_item_set_transform)
123 gchar *affinestr=sp_svg_transform_write(full_t);
124 copy->setAttribute("transform", affinestr);
125 g_free(affinestr);
127 *clip = g_slist_prepend(*clip, copy);
128 }
130 void sp_selection_copy_impl (GSList const *items, GSList **clip, GSList **defs_clip, SPCSSAttr **style_clip, Inkscape::XML::Document* xml_doc)
131 {
133 // Copy stuff referenced by all items to defs_clip:
134 if (defs_clip) {
135 for (GSList *i = (GSList *) items; i != NULL; i = i->next) {
136 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (i->data), items, xml_doc);
137 }
138 *defs_clip = g_slist_reverse(*defs_clip);
139 }
141 // Store style:
142 if (style_clip) {
143 SPItem *item = SP_ITEM (items->data); // take from the first selected item
144 *style_clip = take_style_from_item (item);
145 }
147 if (clip) {
148 // Sort items:
149 GSList *sorted_items = g_slist_copy ((GSList *) items);
150 sorted_items = g_slist_sort((GSList *) sorted_items, (GCompareFunc) sp_object_compare_position);
152 // Copy item reprs:
153 for (GSList *i = (GSList *) sorted_items; i != NULL; i = i->next) {
154 sp_selection_copy_one (SP_OBJECT_REPR (i->data), sp_item_i2doc_affine(SP_ITEM (i->data)), clip, xml_doc);
155 }
157 *clip = g_slist_reverse(*clip);
158 g_slist_free ((GSList *) sorted_items);
159 }
160 }
162 /**
163 * Add gradients/patterns/markers referenced by copied objects to defs.
164 * Iterates through 'defs_clip', and for each item it adds the data
165 * repr into the global defs.
166 */
167 void
168 paste_defs (GSList **defs_clip, SPDocument *doc)
169 {
170 if (!defs_clip)
171 return;
173 for (GSList *gl = *defs_clip; gl != NULL; gl = gl->next) {
174 SPDefs *defs= (SPDefs *) SP_DOCUMENT_DEFS(doc);
175 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) gl->data;
176 gchar const *id = repr->attribute("id");
177 if (!id || !doc->getObjectById(id)) {
178 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
179 Inkscape::XML::Node *copy = repr->duplicate(xml_doc);
180 SP_OBJECT_REPR(defs)->addChild(copy, NULL);
181 Inkscape::GC::release(copy);
182 }
183 }
184 }
186 GSList *sp_selection_paste_impl (SPDocument *doc, SPObject *parent, GSList **clip, GSList **defs_clip)
187 {
188 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
189 paste_defs (defs_clip, doc);
191 GSList *copied = NULL;
192 // add objects to document
193 for (GSList *l = *clip; l != NULL; l = l->next) {
194 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
195 Inkscape::XML::Node *copy = repr->duplicate(xml_doc);
197 // premultiply the item transform by the accumulated parent transform in the paste layer
198 NR::Matrix local = sp_item_i2doc_affine(SP_ITEM(parent));
199 if (!local.test_identity()) {
200 gchar const *t_str = copy->attribute("transform");
201 NR::Matrix item_t (NR::identity());
202 if (t_str)
203 sp_svg_transform_read(t_str, &item_t);
204 item_t *= local.inverse();
205 // (we're dealing with unattached repr, so we write to its attr instead of using sp_item_set_transform)
206 gchar *affinestr=sp_svg_transform_write(item_t);
207 copy->setAttribute("transform", affinestr);
208 g_free(affinestr);
209 }
211 parent->appendChildRepr(copy);
212 copied = g_slist_prepend(copied, copy);
213 Inkscape::GC::release(copy);
214 }
215 return copied;
216 }
218 void sp_selection_delete_impl(GSList const *items, bool propagate = true, bool propagate_descendants = true)
219 {
220 for (GSList const *i = items ; i ; i = i->next ) {
221 sp_object_ref((SPObject *)i->data, NULL);
222 }
223 for (GSList const *i = items; i != NULL; i = i->next) {
224 SPItem *item = (SPItem *) i->data;
225 SP_OBJECT(item)->deleteObject(propagate, propagate_descendants);
226 sp_object_unref((SPObject *)item, NULL);
227 }
228 }
231 void sp_selection_delete()
232 {
233 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
234 if (desktop == NULL) {
235 return;
236 }
238 if (tools_isactive (desktop, TOOLS_TEXT))
239 if (sp_text_delete_selection(desktop->event_context)) {
240 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_TEXT,
241 _("Delete text"));
242 return;
243 }
245 Inkscape::Selection *selection = sp_desktop_selection(desktop);
247 // check if something is selected
248 if (selection->isEmpty()) {
249 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Nothing</b> was deleted."));
250 return;
251 }
253 GSList const *selected = g_slist_copy(const_cast<GSList *>(selection->itemList()));
254 selection->clear();
255 sp_selection_delete_impl (selected);
256 g_slist_free ((GSList *) selected);
258 /* a tool may have set up private information in it's selection context
259 * that depends on desktop items. I think the only sane way to deal with
260 * this currently is to reset the current tool, which will reset it's
261 * associated selection context. For example: deleting an object
262 * while moving it around the canvas.
263 */
264 tools_switch ( desktop, tools_active ( desktop ) );
266 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_DELETE,
267 _("Delete"));
268 }
270 /* fixme: sequencing */
271 void sp_selection_duplicate()
272 {
273 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
274 if (desktop == NULL)
275 return;
277 Inkscape::XML::Document* xml_doc = sp_document_repr_doc(desktop->doc());
278 Inkscape::Selection *selection = sp_desktop_selection(desktop);
280 // check if something is selected
281 if (selection->isEmpty()) {
282 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to duplicate."));
283 return;
284 }
286 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
288 selection->clear();
290 // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
291 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
293 GSList *newsel = NULL;
295 while (reprs) {
296 Inkscape::XML::Node *parent = ((Inkscape::XML::Node *) reprs->data)->parent();
297 Inkscape::XML::Node *copy = ((Inkscape::XML::Node *) reprs->data)->duplicate(xml_doc);
299 parent->appendChild(copy);
301 newsel = g_slist_prepend(newsel, copy);
302 reprs = g_slist_remove(reprs, reprs->data);
303 Inkscape::GC::release(copy);
304 }
306 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_DUPLICATE,
307 _("Duplicate"));
309 selection->setReprList(newsel);
311 g_slist_free(newsel);
312 }
314 void sp_edit_clear_all()
315 {
316 SPDesktop *dt = SP_ACTIVE_DESKTOP;
317 if (!dt)
318 return;
320 SPDocument *doc = sp_desktop_document(dt);
321 sp_desktop_selection(dt)->clear();
323 g_return_if_fail(SP_IS_GROUP(dt->currentLayer()));
324 GSList *items = sp_item_group_item_list(SP_GROUP(dt->currentLayer()));
326 while (items) {
327 SP_OBJECT (items->data)->deleteObject();
328 items = g_slist_remove(items, items->data);
329 }
331 sp_document_done(doc, SP_VERB_EDIT_CLEAR_ALL,
332 _("Delete all"));
333 }
335 GSList *
336 get_all_items (GSList *list, SPObject *from, SPDesktop *desktop, bool onlyvisible, bool onlysensitive, GSList const *exclude)
337 {
338 for (SPObject *child = sp_object_first_child(SP_OBJECT(from)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
339 if (SP_IS_ITEM(child) &&
340 !desktop->isLayer(SP_ITEM(child)) &&
341 (!onlysensitive || !SP_ITEM(child)->isLocked()) &&
342 (!onlyvisible || !desktop->itemIsHidden(SP_ITEM(child))) &&
343 (!exclude || !g_slist_find ((GSList *) exclude, child))
344 )
345 {
346 list = g_slist_prepend (list, SP_ITEM(child));
347 }
349 if (SP_IS_ITEM(child) && desktop->isLayer(SP_ITEM(child))) {
350 list = get_all_items (list, child, desktop, onlyvisible, onlysensitive, exclude);
351 }
352 }
354 return list;
355 }
357 void sp_edit_select_all_full (bool force_all_layers, bool invert)
358 {
359 SPDesktop *dt = SP_ACTIVE_DESKTOP;
360 if (!dt)
361 return;
363 Inkscape::Selection *selection = sp_desktop_selection(dt);
365 g_return_if_fail(SP_IS_GROUP(dt->currentLayer()));
367 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
368 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
369 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
371 GSList *items = NULL;
373 GSList const *exclude = NULL;
374 if (invert) {
375 exclude = selection->itemList();
376 }
378 if (force_all_layers)
379 inlayer = PREFS_SELECTION_ALL;
381 switch (inlayer) {
382 case PREFS_SELECTION_LAYER: {
383 if ( (onlysensitive && SP_ITEM(dt->currentLayer())->isLocked()) ||
384 (onlyvisible && dt->itemIsHidden(SP_ITEM(dt->currentLayer()))) )
385 return;
387 GSList *all_items = sp_item_group_item_list(SP_GROUP(dt->currentLayer()));
389 for (GSList *i = all_items; i; i = i->next) {
390 SPItem *item = SP_ITEM (i->data);
392 if (item && (!onlysensitive || !item->isLocked())) {
393 if (!onlyvisible || !dt->itemIsHidden(item)) {
394 if (!dt->isLayer(item)) {
395 if (!invert || !g_slist_find ((GSList *) exclude, item)) {
396 items = g_slist_prepend (items, item); // leave it in the list
397 }
398 }
399 }
400 }
401 }
403 g_slist_free (all_items);
404 break;
405 }
406 case PREFS_SELECTION_LAYER_RECURSIVE: {
407 items = get_all_items (NULL, dt->currentLayer(), dt, onlyvisible, onlysensitive, exclude);
408 break;
409 }
410 default: {
411 items = get_all_items (NULL, dt->currentRoot(), dt, onlyvisible, onlysensitive, exclude);
412 break;
413 }
414 }
416 selection->setList (items);
418 if (items) {
419 g_slist_free (items);
420 }
421 }
423 void sp_edit_select_all ()
424 {
425 sp_edit_select_all_full (false, false);
426 }
428 void sp_edit_select_all_in_all_layers ()
429 {
430 sp_edit_select_all_full (true, false);
431 }
433 void sp_edit_invert ()
434 {
435 sp_edit_select_all_full (false, true);
436 }
438 void sp_edit_invert_in_all_layers ()
439 {
440 sp_edit_select_all_full (true, true);
441 }
443 void sp_selection_group()
444 {
445 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
446 if (desktop == NULL)
447 return;
449 SPDocument *doc = sp_desktop_document (desktop);
450 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
452 Inkscape::Selection *selection = sp_desktop_selection(desktop);
454 // Check if something is selected.
455 if (selection->isEmpty()) {
456 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>some objects</b> to group."));
457 return;
458 }
460 GSList const *l = (GSList *) selection->reprList();
462 GSList *p = g_slist_copy((GSList *) l);
464 selection->clear();
466 p = g_slist_sort(p, (GCompareFunc) sp_repr_compare_position);
468 // Remember the position and parent of the topmost object.
469 gint topmost = ((Inkscape::XML::Node *) g_slist_last(p)->data)->position();
470 Inkscape::XML::Node *topmost_parent = ((Inkscape::XML::Node *) g_slist_last(p)->data)->parent();
472 Inkscape::XML::Node *group = xml_doc->createElement("svg:g");
474 while (p) {
475 Inkscape::XML::Node *current = (Inkscape::XML::Node *) p->data;
477 if (current->parent() == topmost_parent) {
478 Inkscape::XML::Node *spnew = current->duplicate(xml_doc);
479 sp_repr_unparent(current);
480 group->appendChild(spnew);
481 Inkscape::GC::release(spnew);
482 topmost --; // only reduce count for those items deleted from topmost_parent
483 } else { // move it to topmost_parent first
484 GSList *temp_clip = NULL;
486 // At this point, current may already have no item, due to its being a clone whose original is already moved away
487 // So we copy it artificially calculating the transform from its repr->attr("transform") and the parent transform
488 gchar const *t_str = current->attribute("transform");
489 NR::Matrix item_t (NR::identity());
490 if (t_str)
491 sp_svg_transform_read(t_str, &item_t);
492 item_t *= sp_item_i2doc_affine(SP_ITEM(doc->getObjectByRepr(current->parent())));
493 //FIXME: when moving both clone and original from a transformed group (either by
494 //grouping into another parent, or by cut/paste) the transform from the original's
495 //parent becomes embedded into original itself, and this affects its clones. Fix
496 //this by remembering the transform diffs we write to each item into an array and
497 //then, if this is clone, looking up its original in that array and pre-multiplying
498 //it by the inverse of that original's transform diff.
500 sp_selection_copy_one (current, item_t, &temp_clip, xml_doc);
501 sp_repr_unparent(current);
503 // paste into topmost_parent (temporarily)
504 GSList *copied = sp_selection_paste_impl (doc, doc->getObjectByRepr(topmost_parent), &temp_clip, NULL);
505 if (temp_clip) g_slist_free (temp_clip);
506 if (copied) { // if success,
507 // take pasted object (now in topmost_parent)
508 Inkscape::XML::Node *in_topmost = (Inkscape::XML::Node *) copied->data;
509 // make a copy
510 Inkscape::XML::Node *spnew = in_topmost->duplicate(xml_doc);
511 // remove pasted
512 sp_repr_unparent(in_topmost);
513 // put its copy into group
514 group->appendChild(spnew);
515 Inkscape::GC::release(spnew);
516 g_slist_free (copied);
517 }
518 }
519 p = g_slist_remove(p, current);
520 }
522 // Add the new group to the topmost members' parent
523 topmost_parent->appendChild(group);
525 // Move to the position of the topmost, reduced by the number of items deleted from topmost_parent
526 group->setPosition(topmost + 1);
528 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_GROUP,
529 _("Group"));
531 selection->set(group);
532 Inkscape::GC::release(group);
533 }
535 void sp_selection_ungroup()
536 {
537 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
538 if (desktop == NULL)
539 return;
541 Inkscape::Selection *selection = sp_desktop_selection(desktop);
543 if (selection->isEmpty()) {
544 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>group</b> to ungroup."));
545 return;
546 }
548 GSList *items = g_slist_copy((GSList *) selection->itemList());
549 selection->clear();
551 // Get a copy of current selection.
552 GSList *new_select = NULL;
553 bool ungrouped = false;
554 for (GSList *i = items;
555 i != NULL;
556 i = i->next)
557 {
558 SPItem *group = (SPItem *) i->data;
560 // when ungrouping cloned groups with their originals, some objects that were selected may no more exist due to unlinking
561 if (!SP_IS_OBJECT(group)) {
562 continue;
563 }
565 /* We do not allow ungrouping <svg> etc. (lauris) */
566 if (strcmp(SP_OBJECT_REPR(group)->name(), "svg:g") && strcmp(SP_OBJECT_REPR(group)->name(), "svg:switch")) {
567 // keep the non-group item in the new selection
568 selection->add(group);
569 continue;
570 }
572 GSList *children = NULL;
573 /* This is not strictly required, but is nicer to rely on group ::destroy (lauris) */
574 sp_item_group_ungroup(SP_GROUP(group), &children, false);
575 ungrouped = true;
576 // Add ungrouped items to the new selection.
577 new_select = g_slist_concat(new_select, children);
578 }
580 if (new_select) { // Set new selection.
581 selection->addList(new_select);
582 g_slist_free(new_select);
583 }
584 if (!ungrouped) {
585 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No groups</b> to ungroup in the selection."));
586 }
588 g_slist_free(items);
590 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_UNGROUP,
591 _("Ungroup"));
592 }
594 static SPGroup *
595 sp_item_list_common_parent_group(GSList const *items)
596 {
597 if (!items) {
598 return NULL;
599 }
600 SPObject *parent = SP_OBJECT_PARENT(items->data);
601 /* Strictly speaking this CAN happen, if user selects <svg> from Inkscape::XML editor */
602 if (!SP_IS_GROUP(parent)) {
603 return NULL;
604 }
605 for (items = items->next; items; items = items->next) {
606 if (SP_OBJECT_PARENT(items->data) != parent) {
607 return NULL;
608 }
609 }
611 return SP_GROUP(parent);
612 }
614 /** Finds out the minimum common bbox of the selected items. */
615 static NR::Maybe<NR::Rect>
616 enclose_items(GSList const *items)
617 {
618 g_assert(items != NULL);
620 NR::Maybe<NR::Rect> r = NR::Nothing();
621 for (GSList const *i = items; i; i = i->next) {
622 r = NR::union_bounds(r, sp_item_bbox_desktop((SPItem *) i->data));
623 }
624 return r;
625 }
627 SPObject *
628 prev_sibling(SPObject *child)
629 {
630 SPObject *parent = SP_OBJECT_PARENT(child);
631 if (!SP_IS_GROUP(parent)) {
632 return NULL;
633 }
634 for ( SPObject *i = sp_object_first_child(parent) ; i; i = SP_OBJECT_NEXT(i) ) {
635 if (i->next == child)
636 return i;
637 }
638 return NULL;
639 }
641 void
642 sp_selection_raise()
643 {
644 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
645 if (!desktop)
646 return;
648 Inkscape::Selection *selection = sp_desktop_selection(desktop);
650 GSList const *items = (GSList *) selection->itemList();
651 if (!items) {
652 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise."));
653 return;
654 }
656 SPGroup const *group = sp_item_list_common_parent_group(items);
657 if (!group) {
658 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
659 return;
660 }
662 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
664 /* Construct reverse-ordered list of selected children. */
665 GSList *rev = g_slist_copy((GSList *) items);
666 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
668 // Determine the common bbox of the selected items.
669 NR::Maybe<NR::Rect> selected = enclose_items(items);
671 // Iterate over all objects in the selection (starting from top).
672 if (selected) {
673 while (rev) {
674 SPObject *child = SP_OBJECT(rev->data);
675 // for each selected object, find the next sibling
676 for (SPObject *newref = child->next; newref; newref = newref->next) {
677 // if the sibling is an item AND overlaps our selection,
678 if (SP_IS_ITEM(newref)) {
679 NR::Maybe<NR::Rect> newref_bbox = sp_item_bbox_desktop(SP_ITEM(newref));
680 if ( newref_bbox && selected->intersects(*newref_bbox) ) {
681 // AND if it's not one of our selected objects,
682 if (!g_slist_find((GSList *) items, newref)) {
683 // move the selected object after that sibling
684 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(newref));
685 }
686 break;
687 }
688 }
689 }
690 rev = g_slist_remove(rev, child);
691 }
692 } else {
693 g_slist_free(rev);
694 }
696 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_RAISE,
697 _("Raise"));
698 }
700 void sp_selection_raise_to_top()
701 {
702 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
703 if (desktop == NULL)
704 return;
706 SPDocument *document = sp_desktop_document(desktop);
707 Inkscape::Selection *selection = sp_desktop_selection(desktop);
709 if (selection->isEmpty()) {
710 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise to top."));
711 return;
712 }
714 GSList const *items = (GSList *) selection->itemList();
716 SPGroup const *group = sp_item_list_common_parent_group(items);
717 if (!group) {
718 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
719 return;
720 }
722 GSList *rl = g_slist_copy((GSList *) selection->reprList());
723 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
725 for (GSList *l = rl; l != NULL; l = l->next) {
726 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
727 repr->setPosition(-1);
728 }
730 g_slist_free(rl);
732 sp_document_done(document, SP_VERB_SELECTION_TO_FRONT,
733 _("Raise to top"));
734 }
736 void
737 sp_selection_lower()
738 {
739 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
740 if (desktop == NULL)
741 return;
743 Inkscape::Selection *selection = sp_desktop_selection(desktop);
745 GSList const *items = (GSList *) selection->itemList();
746 if (!items) {
747 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower."));
748 return;
749 }
751 SPGroup const *group = sp_item_list_common_parent_group(items);
752 if (!group) {
753 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
754 return;
755 }
757 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
759 // Determine the common bbox of the selected items.
760 NR::Maybe<NR::Rect> selected = enclose_items(items);
762 /* Construct direct-ordered list of selected children. */
763 GSList *rev = g_slist_copy((GSList *) items);
764 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
765 rev = g_slist_reverse(rev);
767 // Iterate over all objects in the selection (starting from top).
768 if (selected) {
769 while (rev) {
770 SPObject *child = SP_OBJECT(rev->data);
771 // for each selected object, find the prev sibling
772 for (SPObject *newref = prev_sibling(child); newref; newref = prev_sibling(newref)) {
773 // if the sibling is an item AND overlaps our selection,
774 if (SP_IS_ITEM(newref)) {
775 NR::Maybe<NR::Rect> ref_bbox = sp_item_bbox_desktop(SP_ITEM(newref));
776 if ( ref_bbox && selected->intersects(*ref_bbox) ) {
777 // AND if it's not one of our selected objects,
778 if (!g_slist_find((GSList *) items, newref)) {
779 // move the selected object before that sibling
780 SPObject *put_after = prev_sibling(newref);
781 if (put_after)
782 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(put_after));
783 else
784 SP_OBJECT_REPR(child)->setPosition(0);
785 }
786 break;
787 }
788 }
789 }
790 rev = g_slist_remove(rev, child);
791 }
792 } else {
793 g_slist_free(rev);
794 }
796 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_LOWER,
797 _("Lower"));
798 }
800 void sp_selection_lower_to_bottom()
801 {
802 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
803 if (desktop == NULL)
804 return;
806 SPDocument *document = sp_desktop_document(desktop);
807 Inkscape::Selection *selection = sp_desktop_selection(desktop);
809 if (selection->isEmpty()) {
810 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower to bottom."));
811 return;
812 }
814 GSList const *items = (GSList *) selection->itemList();
816 SPGroup const *group = sp_item_list_common_parent_group(items);
817 if (!group) {
818 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
819 return;
820 }
822 GSList *rl;
823 rl = g_slist_copy((GSList *) selection->reprList());
824 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
825 rl = g_slist_reverse(rl);
827 for (GSList *l = rl; l != NULL; l = l->next) {
828 gint minpos;
829 SPObject *pp, *pc;
830 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
831 pp = document->getObjectByRepr(sp_repr_parent(repr));
832 minpos = 0;
833 g_assert(SP_IS_GROUP(pp));
834 pc = sp_object_first_child(pp);
835 while (!SP_IS_ITEM(pc)) {
836 minpos += 1;
837 pc = pc->next;
838 }
839 repr->setPosition(minpos);
840 }
842 g_slist_free(rl);
844 sp_document_done(document, SP_VERB_SELECTION_TO_BACK,
845 _("Lower to bottom"));
846 }
848 void
849 sp_undo(SPDesktop *desktop, SPDocument *)
850 {
851 if (!sp_document_undo(sp_desktop_document(desktop)))
852 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to undo."));
853 }
855 void
856 sp_redo(SPDesktop *desktop, SPDocument *)
857 {
858 if (!sp_document_redo(sp_desktop_document(desktop)))
859 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to redo."));
860 }
862 void sp_selection_cut()
863 {
864 sp_selection_copy();
865 sp_selection_delete();
866 }
868 void sp_copy_gradient (GSList **defs_clip, SPGradient *gradient, Inkscape::XML::Document* xml_doc)
869 {
870 SPGradient *ref = gradient;
872 while (ref) {
873 // climb up the refs, copying each one in the chain
874 Inkscape::XML::Node *grad_repr = SP_OBJECT_REPR(ref)->duplicate(xml_doc);
875 *defs_clip = g_slist_prepend (*defs_clip, grad_repr);
877 ref = ref->ref->getObject();
878 }
879 }
881 void sp_copy_pattern (GSList **defs_clip, SPPattern *pattern, Inkscape::XML::Document* xml_doc)
882 {
883 SPPattern *ref = pattern;
885 while (ref) {
886 // climb up the refs, copying each one in the chain
887 Inkscape::XML::Node *pattern_repr = SP_OBJECT_REPR(ref)->duplicate(xml_doc);
888 *defs_clip = g_slist_prepend (*defs_clip, pattern_repr);
890 // items in the pattern may also use gradients and other patterns, so we need to recurse here as well
891 for (SPObject *child = sp_object_first_child(SP_OBJECT(ref)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
892 if (!SP_IS_ITEM (child))
893 continue;
894 sp_copy_stuff_used_by_item (defs_clip, (SPItem *) child, NULL, xml_doc);
895 }
897 ref = ref->ref->getObject();
898 }
899 }
901 void sp_copy_single (GSList **defs_clip, SPObject *thing, Inkscape::XML::Document* xml_doc)
902 {
903 Inkscape::XML::Node *duplicate_repr = SP_OBJECT_REPR(thing)->duplicate(xml_doc);
904 *defs_clip = g_slist_prepend (*defs_clip, duplicate_repr);
905 }
908 void sp_copy_textpath_path (GSList **defs_clip, SPTextPath *tp, GSList const *items, Inkscape::XML::Document* xml_doc)
909 {
910 SPItem *path = sp_textpath_get_path_item (tp);
911 if (!path)
912 return;
913 if (items && g_slist_find ((GSList *) items, path)) // do not copy it to defs if it is already in the list of items copied
914 return;
915 Inkscape::XML::Node *repr = SP_OBJECT_REPR(path)->duplicate(xml_doc);
916 *defs_clip = g_slist_prepend (*defs_clip, repr);
917 }
919 /**
920 * Copies things like patterns, markers, gradients, etc.
921 */
922 void sp_copy_stuff_used_by_item (GSList **defs_clip, SPItem *item, GSList const *items, Inkscape::XML::Document* xml_doc)
923 {
924 SPStyle *style = SP_OBJECT_STYLE (item);
926 if (style && (style->fill.isPaintserver())) {
927 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
928 if (SP_IS_LINEARGRADIENT (server) || SP_IS_RADIALGRADIENT (server))
929 sp_copy_gradient (defs_clip, SP_GRADIENT(server), xml_doc);
930 if (SP_IS_PATTERN (server))
931 sp_copy_pattern (defs_clip, SP_PATTERN(server), xml_doc);
932 }
934 if (style && (style->stroke.isPaintserver())) {
935 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
936 if (SP_IS_LINEARGRADIENT (server) || SP_IS_RADIALGRADIENT (server))
937 sp_copy_gradient (defs_clip, SP_GRADIENT(server), xml_doc);
938 if (SP_IS_PATTERN (server))
939 sp_copy_pattern (defs_clip, SP_PATTERN(server), xml_doc);
940 }
942 // For shapes, copy all of the shape's markers into defs_clip
943 if (SP_IS_SHAPE (item)) {
944 SPShape *shape = SP_SHAPE (item);
945 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
946 if (shape->marker[i]) {
947 sp_copy_single (defs_clip, SP_OBJECT (shape->marker[i]), xml_doc);
948 }
949 }
951 // For shapes, also copy liveeffect if applicable
952 if (sp_shape_has_path_effect(shape)) {
953 sp_copy_single (defs_clip, SP_OBJECT(sp_shape_get_livepatheffectobject(shape)), xml_doc);
954 }
955 }
957 // For 3D boxes copy perspectives
958 if (SP_IS_BOX3D(item)) {
959 sp_copy_single (defs_clip, SP_OBJECT(box3d_get_perspective(SP_BOX3D(item))), xml_doc);
960 }
962 if (SP_IS_TEXT_TEXTPATH (item)) {
963 sp_copy_textpath_path (defs_clip, SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))), items, xml_doc);
964 }
966 if (item->clip_ref->getObject()) {
967 sp_copy_single (defs_clip, item->clip_ref->getObject(), xml_doc);
968 }
970 if (item->mask_ref->getObject()) {
971 SPObject *mask = item->mask_ref->getObject();
972 sp_copy_single (defs_clip, mask, xml_doc);
973 // recurse into the mask for its gradients etc.
974 for (SPObject *o = SP_OBJECT(mask)->children; o != NULL; o = o->next) {
975 if (SP_IS_ITEM(o))
976 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (o), items, xml_doc);
977 }
978 }
980 if (style->getFilter()) {
981 SPObject *filter = style->getFilter();
982 if (SP_IS_FILTER(filter)) {
983 sp_copy_single (defs_clip, filter, xml_doc);
984 }
985 }
987 // recurse
988 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
989 if (SP_IS_ITEM(o))
990 sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (o), items, xml_doc);
991 }
992 }
994 void
995 sp_set_style_clipboard (SPCSSAttr *css)
996 {
997 if (css != NULL) {
998 // clear style clipboard
999 if (style_clipboard) {
1000 sp_repr_css_attr_unref (style_clipboard);
1001 style_clipboard = NULL;
1002 }
1003 //sp_repr_css_print (css);
1004 style_clipboard = css;
1005 }
1006 }
1008 /**
1009 * \pre item != NULL
1010 */
1011 SPCSSAttr *
1012 take_style_from_item (SPItem *item)
1013 {
1014 // write the complete cascaded style, context-free
1015 SPCSSAttr *css = sp_css_attr_from_object (SP_OBJECT(item), SP_STYLE_FLAG_ALWAYS);
1016 if (css == NULL)
1017 return NULL;
1019 if ((SP_IS_GROUP(item) && SP_OBJECT(item)->children) ||
1020 (SP_IS_TEXT (item) && SP_OBJECT(item)->children && SP_OBJECT(item)->children->next == NULL)) {
1021 // if this is a text with exactly one tspan child, merge the style of that tspan as well
1022 // If this is a group, merge the style of its topmost (last) child with style
1023 for (SPObject *last_element = item->lastChild(); last_element != NULL; last_element = SP_OBJECT_PREV (last_element)) {
1024 if (SP_OBJECT_STYLE (last_element) != NULL) {
1025 SPCSSAttr *temp = sp_css_attr_from_object (last_element, SP_STYLE_FLAG_IFSET);
1026 if (temp) {
1027 sp_repr_css_merge (css, temp);
1028 sp_repr_css_attr_unref (temp);
1029 }
1030 break;
1031 }
1032 }
1033 }
1034 if (!(SP_IS_TEXT (item) || SP_IS_TSPAN (item) || SP_IS_TREF(item) || SP_IS_STRING (item))) {
1035 // do not copy text properties from non-text objects, it's confusing
1036 css = sp_css_attr_unset_text (css);
1037 }
1039 // FIXME: also transform gradient/pattern fills, by forking? NO, this must be nondestructive
1040 double ex = NR::expansion(sp_item_i2doc_affine(item));
1041 if (ex != 1.0) {
1042 css = sp_css_attr_scale (css, ex);
1043 }
1045 return css;
1046 }
1049 void sp_selection_copy()
1050 {
1051 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1052 if (desktop == NULL)
1053 return;
1055 if (!clipboard_document) {
1056 clipboard_document = new Inkscape::XML::SimpleDocument();
1057 }
1059 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1061 if (tools_isactive (desktop, TOOLS_DROPPER)) {
1062 sp_dropper_context_copy(desktop->event_context);
1063 return; // copied color under cursor, nothing else to do
1064 }
1066 if (desktop->event_context->get_drag() && desktop->event_context->get_drag()->copy()) {
1067 return; // copied selected stop(s), nothing else to do
1068 }
1070 // check if something is selected
1071 if (selection->isEmpty()) {
1072 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing was copied."));
1073 return;
1074 }
1076 GSList const *items = g_slist_copy ((GSList *) selection->itemList());
1078 // 0. Copy text to system clipboard
1079 // FIXME: for non-texts, put serialized Inkscape::XML as text to the clipboard;
1080 //for this sp_repr_write_stream needs to be rewritten with iostream instead of FILE
1081 Glib::ustring text;
1082 if (tools_isactive (desktop, TOOLS_TEXT)) {
1083 text = sp_text_get_selected_text(desktop->event_context);
1084 }
1086 if (text.empty()) {
1087 guint texts = 0;
1088 for (GSList *i = (GSList *) items; i; i = i->next) {
1089 SPItem *item = SP_ITEM (i->data);
1090 if (SP_IS_TEXT (item) || SP_IS_FLOWTEXT(item)) {
1091 if (texts > 0) // if more than one text object is copied, separate them by spaces
1092 text += " ";
1093 gchar *this_text = sp_te_get_string_multiline (item);
1094 if (this_text) {
1095 text += this_text;
1096 g_free(this_text);
1097 }
1098 texts++;
1099 }
1100 }
1101 }
1102 if (!text.empty()) {
1103 Glib::RefPtr<Gtk::Clipboard> refClipboard = Gtk::Clipboard::get();
1104 refClipboard->set_text(text);
1105 }
1107 // clear old defs clipboard
1108 while (defs_clipboard) {
1109 Inkscape::GC::release((Inkscape::XML::Node *) defs_clipboard->data);
1110 defs_clipboard = g_slist_remove (defs_clipboard, defs_clipboard->data);
1111 }
1113 // clear style clipboard
1114 if (style_clipboard) {
1115 sp_repr_css_attr_unref (style_clipboard);
1116 style_clipboard = NULL;
1117 }
1119 //clear main clipboard
1120 while (clipboard) {
1121 Inkscape::GC::release((Inkscape::XML::Node *) clipboard->data);
1122 clipboard = g_slist_remove(clipboard, clipboard->data);
1123 }
1125 sp_selection_copy_impl (items, &clipboard, &defs_clipboard, &style_clipboard, clipboard_document);
1127 if (tools_isactive (desktop, TOOLS_TEXT)) { // take style from cursor/text selection, overwriting the style just set by copy_impl
1128 SPStyle *const query = sp_style_new(SP_ACTIVE_DOCUMENT);
1129 if (sp_desktop_query_style_all (desktop, query)) {
1130 SPCSSAttr *css = sp_css_attr_from_style (query, SP_STYLE_FLAG_ALWAYS);
1131 sp_set_style_clipboard (css);
1132 }
1133 sp_style_unref(query);
1134 }
1136 size_clipboard = selection->bounds();
1138 g_slist_free ((GSList *) items);
1139 }
1142 void sp_selection_copy_lpe_pathparam(Inkscape::LivePathEffect::PathParam * pathparam)
1143 {
1144 if (pathparam == NULL)
1145 return;
1147 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1148 if (desktop == NULL)
1149 return;
1151 if (!clipboard_document) {
1152 clipboard_document = new Inkscape::XML::SimpleDocument();
1153 }
1155 // clear old defs clipboard
1156 while (defs_clipboard) {
1157 Inkscape::GC::release((Inkscape::XML::Node *) defs_clipboard->data);
1158 defs_clipboard = g_slist_remove (defs_clipboard, defs_clipboard->data);
1159 }
1161 // clear style clipboard
1162 if (style_clipboard) {
1163 sp_repr_css_attr_unref (style_clipboard);
1164 style_clipboard = NULL;
1165 }
1167 //clear main clipboard
1168 while (clipboard) {
1169 Inkscape::GC::release((Inkscape::XML::Node *) clipboard->data);
1170 clipboard = g_slist_remove(clipboard, clipboard->data);
1171 }
1173 // make new path node and put svgd as 'd' attribute
1174 Inkscape::XML::Node *newnode = clipboard_document->createElement("svg:path");
1175 gchar * svgd = pathparam->param_writeSVGValue();
1176 newnode->setAttribute("d", svgd);
1177 g_free(svgd);
1179 clipboard = g_slist_prepend(clipboard, newnode);
1181 Geom::Rect bnds = Geom::bounds_exact(*pathparam);
1182 size_clipboard = from_2geom(bnds);
1183 }
1186 //____________________________________________________________________________
1188 /** Paste the bitmap in the clipboard if one is in there.
1189 The bitmap is saved to a PNG file then imported into the document
1191 @return true if a bitmap was detected and pasted; false if no bitmap
1192 */
1193 static bool pastedPicFromClipboard()
1194 {
1195 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1196 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1197 if ( desktop == NULL || doc == NULL)
1198 return false;
1200 Glib::RefPtr<Gtk::Clipboard> refClipboard = Gtk::Clipboard::get();
1201 Glib::RefPtr<Gdk::Pixbuf> pic = refClipboard->wait_for_image();
1203 // Stop if the system clipboard doesn't have a bitmap.
1204 if ( pic == 0 )
1205 {
1206 return false;
1207 } //if
1208 else
1209 {
1210 // Write into a file, then import the file into the document.
1211 // Make a file name based on current time; use the current working dir.
1212 time_t rawtime;
1213 char filename[50];
1214 const char* path;
1216 time ( &rawtime );
1217 strftime (filename,50,"pastedpic_%m%d%Y_%H%M%S.png",localtime( &rawtime ));
1218 path = (char *)prefs_get_string_attribute("dialogs.save_as", "path");
1219 Glib::ustring finalPath = path;
1220 finalPath.append(G_DIR_SEPARATOR_S).append(filename);
1221 pic->save( finalPath, "png" );
1222 file_import(doc, finalPath, NULL);
1224 // Clear the clipboard so that the bitmap in there won't always over
1225 // ride the normal inkscape clipboard.This isn't the ideal solution.
1226 refClipboard->set_text("");
1227 return true;
1228 } //else
1230 return false;
1231 } //pastedPicFromClipboard
1233 //____________________________________________________________________________
1235 void sp_selection_paste(bool in_place)
1236 {
1237 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1239 if (desktop == NULL) {
1240 return;
1241 }
1243 SPDocument *document = sp_desktop_document(desktop);
1245 if (Inkscape::have_viable_layer(desktop, desktop->messageStack()) == false) {
1246 return;
1247 }
1249 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1251 if (tools_isactive (desktop, TOOLS_TEXT)) {
1252 if (sp_text_paste_inline(desktop->event_context))
1253 return; // pasted from system clipboard into text, nothing else to do
1254 }
1256 // check if something is in the clipboard
1258 // Stop if successfully pasted a clipboard bitmap.
1259 if ( pastedPicFromClipboard() )
1260 return;
1263 if (clipboard == NULL) {
1264 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing in the clipboard."));
1265 return;
1266 }
1268 GSList *copied = sp_selection_paste_impl(document, desktop->currentLayer(), &clipboard, &defs_clipboard);
1269 // add pasted objects to selection
1270 selection->setReprList((GSList const *) copied);
1271 g_slist_free (copied);
1273 if (!in_place) {
1274 sp_document_ensure_up_to_date(document);
1276 NR::Maybe<NR::Rect> sel_bbox = selection->bounds();
1277 NR::Point m( desktop->point() );
1278 if (sel_bbox) {
1279 m -= sel_bbox->midpoint();
1280 }
1282 sp_selection_move_relative(selection, m);
1283 }
1285 sp_document_done(document, SP_VERB_EDIT_PASTE, _("Paste"));
1286 }
1288 void sp_selection_paste_style()
1289 {
1290 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1291 if (desktop == NULL) return;
1293 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1295 // check if something is in the clipboard
1296 if (style_clipboard == NULL) {
1297 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the style clipboard."));
1298 return;
1299 }
1301 // check if something is selected
1302 if (selection->isEmpty()) {
1303 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste style to."));
1304 return;
1305 }
1307 paste_defs (&defs_clipboard, sp_desktop_document(desktop));
1309 sp_desktop_set_style (desktop, style_clipboard);
1311 sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_STYLE,
1312 _("Paste style"));
1313 }
1315 void sp_selection_paste_livepatheffect()
1316 {
1317 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1318 if (desktop == NULL) return;
1320 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1322 // check if something is in the clipboard
1323 if (clipboard == NULL) {
1324 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1325 return;
1326 }
1328 // check if something is selected
1329 if (selection->isEmpty()) {
1330 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste live path effect to."));
1331 return;
1332 }
1334 SPDocument *doc = sp_desktop_document(desktop);
1335 paste_defs (&defs_clipboard, doc);
1337 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) clipboard->data;
1338 char const *effecturi = repr->attribute("inkscape:path-effect");
1339 if (!effecturi) {
1340 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Clipboard does not contain a live path effect."));
1341 return;
1342 }
1344 for ( GSList const *itemlist = selection->itemList(); itemlist != NULL; itemlist = g_slist_next(itemlist) ) {
1345 SPItem *item = reinterpret_cast<SPItem*>(itemlist->data);
1346 if ( item && SP_IS_SHAPE(item) ) {
1347 SPShape * shape = SP_SHAPE(item);
1349 // create a private LPE object!
1350 SPObject * obj = sp_uri_reference_resolve(doc, effecturi);
1351 LivePathEffectObject * lpeobj = LIVEPATHEFFECT(obj)->fork_private_if_necessary(0);
1353 sp_shape_set_path_effect(shape, lpeobj);
1355 // set inkscape:original-d for paths. the other shapes don't need this.
1356 if ( SP_IS_PATH(item) ) {
1357 Inkscape::XML::Node *pathrepr = SP_OBJECT_REPR(item);
1358 if ( ! pathrepr->attribute("inkscape:original-d") ) {
1359 pathrepr->setAttribute("inkscape:original-d", pathrepr->attribute("d"));
1360 }
1361 }
1362 }
1363 }
1365 sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_LIVEPATHEFFECT,
1366 _("Paste live path effect"));
1367 }
1369 void sp_selection_paste_size (bool apply_x, bool apply_y)
1370 {
1371 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1372 if (desktop == NULL) return;
1374 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1376 // check if something is in the clipboard
1377 if (!size_clipboard) {
1378 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1379 return;
1380 }
1382 // check if something is selected
1383 if (selection->isEmpty()) {
1384 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste size to."));
1385 return;
1386 }
1388 NR::Maybe<NR::Rect> current = selection->bounds();
1389 if ( !current || current->isEmpty() ) {
1390 return;
1391 }
1393 double scale_x = size_clipboard->extent(NR::X) / current->extent(NR::X);
1394 double scale_y = size_clipboard->extent(NR::Y) / current->extent(NR::Y);
1396 sp_selection_scale_relative (selection, current->midpoint(),
1397 NR::scale(
1398 apply_x? scale_x : (desktop->isToolboxButtonActive ("lock")? scale_y : 1.0),
1399 apply_y? scale_y : (desktop->isToolboxButtonActive ("lock")? scale_x : 1.0)));
1401 sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_SIZE,
1402 _("Paste size"));
1403 }
1405 void sp_selection_paste_size_separately (bool apply_x, bool apply_y)
1406 {
1407 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1408 if (desktop == NULL) return;
1410 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1412 // check if something is in the clipboard
1413 if ( !size_clipboard ) {
1414 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
1415 return;
1416 }
1418 // check if something is selected
1419 if (selection->isEmpty()) {
1420 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to paste size to."));
1421 return;
1422 }
1424 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1425 SPItem *item = SP_ITEM(l->data);
1427 NR::Maybe<NR::Rect> current = sp_item_bbox_desktop(item);
1428 if ( !current || current->isEmpty() ) {
1429 continue;
1430 }
1432 double scale_x = size_clipboard->extent(NR::X) / current->extent(NR::X);
1433 double scale_y = size_clipboard->extent(NR::Y) / current->extent(NR::Y);
1435 sp_item_scale_rel (item,
1436 NR::scale(
1437 apply_x? scale_x : (desktop->isToolboxButtonActive ("lock")? scale_y : 1.0),
1438 apply_y? scale_y : (desktop->isToolboxButtonActive ("lock")? scale_x : 1.0)));
1440 }
1442 sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_SIZE_SEPARATELY,
1443 _("Paste size separately"));
1444 }
1446 void sp_selection_to_next_layer ()
1447 {
1448 SPDesktop *dt = SP_ACTIVE_DESKTOP;
1450 Inkscape::Selection *selection = sp_desktop_selection(dt);
1452 // check if something is selected
1453 if (selection->isEmpty()) {
1454 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer above."));
1455 return;
1456 }
1458 GSList const *items = g_slist_copy ((GSList *) selection->itemList());
1460 bool no_more = false; // Set to true, if no more layers above
1461 SPObject *next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer());
1462 if (next) {
1463 GSList *temp_clip = NULL;
1464 sp_selection_copy_impl (items, &temp_clip, NULL, NULL, sp_document_repr_doc(dt->doc())); // we're in the same doc, so no need to copy defs
1465 sp_selection_delete_impl (items, false, false);
1466 next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
1467 GSList *copied;
1468 if(next) {
1469 copied = sp_selection_paste_impl (sp_desktop_document (dt), next, &temp_clip, NULL);
1470 } else {
1471 copied = sp_selection_paste_impl (sp_desktop_document (dt), dt->currentLayer(), &temp_clip, NULL);
1472 no_more = true;
1473 }
1474 selection->setReprList((GSList const *) copied);
1475 g_slist_free (copied);
1476 if (temp_clip) g_slist_free (temp_clip);
1477 if (next) dt->setCurrentLayer(next);
1478 sp_document_done(sp_desktop_document (dt), SP_VERB_LAYER_MOVE_TO_NEXT,
1479 _("Raise to next layer"));
1480 } else {
1481 no_more = true;
1482 }
1484 if (no_more) {
1485 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers above."));
1486 }
1488 g_slist_free ((GSList *) items);
1489 }
1491 void sp_selection_to_prev_layer ()
1492 {
1493 SPDesktop *dt = SP_ACTIVE_DESKTOP;
1495 Inkscape::Selection *selection = sp_desktop_selection(dt);
1497 // check if something is selected
1498 if (selection->isEmpty()) {
1499 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer below."));
1500 return;
1501 }
1503 GSList const *items = g_slist_copy ((GSList *) selection->itemList());
1505 bool no_more = false; // Set to true, if no more layers below
1506 SPObject *next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer());
1507 if (next) {
1508 GSList *temp_clip = NULL;
1509 sp_selection_copy_impl (items, &temp_clip, NULL, NULL, sp_document_repr_doc(dt->doc())); // we're in the same doc, so no need to copy defs
1510 sp_selection_delete_impl (items, false, false);
1511 next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
1512 GSList *copied;
1513 if(next) {
1514 copied = sp_selection_paste_impl (sp_desktop_document (dt), next, &temp_clip, NULL);
1515 } else {
1516 copied = sp_selection_paste_impl (sp_desktop_document (dt), dt->currentLayer(), &temp_clip, NULL);
1517 no_more = true;
1518 }
1519 selection->setReprList((GSList const *) copied);
1520 g_slist_free (copied);
1521 if (temp_clip) g_slist_free (temp_clip);
1522 if (next) dt->setCurrentLayer(next);
1523 sp_document_done(sp_desktop_document (dt), SP_VERB_LAYER_MOVE_TO_PREV,
1524 _("Lower to previous layer"));
1525 } else {
1526 no_more = true;
1527 }
1529 if (no_more) {
1530 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers below."));
1531 }
1533 g_slist_free ((GSList *) items);
1534 }
1536 bool
1537 selection_contains_original (SPItem *item, Inkscape::Selection *selection)
1538 {
1539 bool contains_original = false;
1541 bool is_use = SP_IS_USE(item);
1542 SPItem *item_use = item;
1543 SPItem *item_use_first = item;
1544 while (is_use && item_use && !contains_original)
1545 {
1546 item_use = sp_use_get_original (SP_USE(item_use));
1547 contains_original |= selection->includes(item_use);
1548 if (item_use == item_use_first)
1549 break;
1550 is_use = SP_IS_USE(item_use);
1551 }
1553 // If it's a tref, check whether the object containing the character
1554 // data is part of the selection
1555 if (!contains_original && SP_IS_TREF(item)) {
1556 contains_original = selection->includes(SP_TREF(item)->getObjectReferredTo());
1557 }
1559 return contains_original;
1560 }
1563 bool
1564 selection_contains_both_clone_and_original (Inkscape::Selection *selection)
1565 {
1566 bool clone_with_original = false;
1567 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1568 SPItem *item = SP_ITEM(l->data);
1569 clone_with_original |= selection_contains_original(item, selection);
1570 if (clone_with_original)
1571 break;
1572 }
1573 return clone_with_original;
1574 }
1577 /** Apply matrix to the selection. \a set_i2d is normally true, which means objects are in the
1578 original transform, synced with their reprs, and need to jump to the new transform in one go. A
1579 value of set_i2d==false is only used by seltrans when it's dragging objects live (not outlines); in
1580 that case, items are already in the new position, but the repr is in the old, and this function
1581 then simply updates the repr from item->transform.
1582 */
1583 void sp_selection_apply_affine(Inkscape::Selection *selection, NR::Matrix const &affine, bool set_i2d)
1584 {
1585 if (selection->isEmpty())
1586 return;
1588 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1589 SPItem *item = SP_ITEM(l->data);
1591 NR::Point old_center(0,0);
1592 if (set_i2d && item->isCenterSet())
1593 old_center = item->getCenter();
1595 #if 0 /* Re-enable this once persistent guides have a graphical indication.
1596 At the time of writing, this is the only place to re-enable. */
1597 sp_item_update_cns(*item, selection->desktop());
1598 #endif
1600 // we're moving both a clone and its original or any ancestor in clone chain?
1601 bool transform_clone_with_original = selection_contains_original(item, selection);
1602 // ...both a text-on-path and its path?
1603 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)))) ));
1604 // ...both a flowtext and its frame?
1605 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)
1606 // ...both an offset and its source?
1607 bool transform_offset_with_source = (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) && selection->includes( sp_offset_get_source (SP_OFFSET(item)) );
1609 // If we're moving a connector, we want to detach it
1610 // from shapes that aren't part of the selection, but
1611 // leave it attached if they are
1612 if (cc_item_is_connector(item)) {
1613 SPItem *attItem[2];
1614 SP_PATH(item)->connEndPair.getAttachedItems(attItem);
1616 for (int n = 0; n < 2; ++n) {
1617 if (!selection->includes(attItem[n])) {
1618 sp_conn_end_detach(item, n);
1619 }
1620 }
1621 }
1623 // "clones are unmoved when original is moved" preference
1624 int compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
1625 bool prefs_unmoved = (compensation == SP_CLONE_COMPENSATION_UNMOVED);
1626 bool prefs_parallel = (compensation == SP_CLONE_COMPENSATION_PARALLEL);
1628 // If this is a clone and it's selected along with its original, do not move it; it will feel the
1629 // transform of its original and respond to it itself. Without this, a clone is doubly
1630 // transformed, very unintuitive.
1631 // Same for textpath if we are also doing ANY transform to its path: do not touch textpath,
1632 // letters cannot be squeezed or rotated anyway, they only refill the changed path.
1633 // Same for linked offset if we are also moving its source: do not move it.
1634 if (transform_textpath_with_path || transform_offset_with_source) {
1635 // restore item->transform field from the repr, in case it was changed by seltrans
1636 sp_object_read_attr (SP_OBJECT (item), "transform");
1638 } else if (transform_flowtext_with_frame) {
1639 // apply the inverse of the region's transform to the <use> so that the flow remains
1640 // the same (even though the output itself gets transformed)
1641 for (SPObject *region = item->firstChild() ; region ; region = SP_OBJECT_NEXT(region)) {
1642 if (!SP_IS_FLOWREGION(region) && !SP_IS_FLOWREGIONEXCLUDE(region))
1643 continue;
1644 for (SPObject *use = region->firstChild() ; use ; use = SP_OBJECT_NEXT(use)) {
1645 if (!SP_IS_USE(use)) continue;
1646 sp_item_write_transform(SP_USE(use), SP_OBJECT_REPR(use), item->transform.inverse(), NULL);
1647 }
1648 }
1649 } else if (transform_clone_with_original) {
1650 // We are transforming a clone along with its original. The below matrix juggling is
1651 // necessary to ensure that they transform as a whole, i.e. the clone's induced
1652 // transform and its move compensation are both cancelled out.
1654 // restore item->transform field from the repr, in case it was changed by seltrans
1655 sp_object_read_attr (SP_OBJECT (item), "transform");
1657 // calculate the matrix we need to apply to the clone to cancel its induced transform from its original
1658 NR::Matrix parent_transform = sp_item_i2root_affine(SP_ITEM(SP_OBJECT_PARENT (item)));
1659 NR::Matrix t = parent_transform * matrix_to_desktop (matrix_from_desktop (affine, item), item) * parent_transform.inverse();
1660 NR::Matrix t_inv =parent_transform * matrix_to_desktop (matrix_from_desktop (affine.inverse(), item), item) * parent_transform.inverse();
1661 NR::Matrix result = t_inv * item->transform * t;
1663 if ((prefs_parallel || prefs_unmoved) && affine.is_translation()) {
1664 // we need to cancel out the move compensation, too
1666 // find out the clone move, same as in sp_use_move_compensate
1667 NR::Matrix parent = sp_use_get_parent_transform (SP_USE(item));
1668 NR::Matrix clone_move = parent.inverse() * t * parent;
1670 if (prefs_parallel) {
1671 NR::Matrix move = result * clone_move * t_inv;
1672 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &move);
1674 } else if (prefs_unmoved) {
1675 //if (SP_IS_USE(sp_use_get_original(SP_USE(item))))
1676 // clone_move = NR::identity();
1677 NR::Matrix move = result * clone_move;
1678 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &t);
1679 }
1681 } else {
1682 // just apply the result
1683 sp_item_write_transform(item, SP_OBJECT_REPR(item), result, &t);
1684 }
1686 } else {
1687 if (set_i2d) {
1688 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * affine);
1689 }
1690 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, NULL);
1691 }
1693 // if we're moving the actual object, not just updating the repr, we can transform the
1694 // center by the same matrix (only necessary for non-translations)
1695 if (set_i2d && item->isCenterSet() && !affine.is_translation()) {
1696 item->setCenter(old_center * affine);
1697 SP_OBJECT(item)->updateRepr();
1698 }
1699 }
1700 }
1702 void sp_selection_remove_transform()
1703 {
1704 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1705 if (desktop == NULL)
1706 return;
1708 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1710 GSList const *l = (GSList *) selection->reprList();
1711 while (l != NULL) {
1712 sp_repr_set_attr((Inkscape::XML::Node*)l->data, "transform", NULL);
1713 l = l->next;
1714 }
1716 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_FLATTEN,
1717 _("Remove transform"));
1718 }
1720 void
1721 sp_selection_scale_absolute(Inkscape::Selection *selection,
1722 double const x0, double const x1,
1723 double const y0, double const y1)
1724 {
1725 if (selection->isEmpty())
1726 return;
1728 NR::Maybe<NR::Rect> const bbox(selection->bounds());
1729 if ( !bbox || bbox->isEmpty() ) {
1730 return;
1731 }
1733 NR::translate const p2o(-bbox->min());
1735 NR::scale const newSize(x1 - x0,
1736 y1 - y0);
1737 NR::scale const scale( newSize / NR::scale(bbox->dimensions()) );
1738 NR::translate const o2n(x0, y0);
1739 NR::Matrix const final( p2o * scale * o2n );
1741 sp_selection_apply_affine(selection, final);
1742 }
1745 void sp_selection_scale_relative(Inkscape::Selection *selection, NR::Point const &align, NR::scale const &scale)
1746 {
1747 if (selection->isEmpty())
1748 return;
1750 NR::Maybe<NR::Rect> const bbox(selection->bounds());
1752 if ( !bbox || bbox->isEmpty() ) {
1753 return;
1754 }
1756 // FIXME: ARBITRARY LIMIT: don't try to scale above 1 Mpx, it won't display properly and will crash sooner or later anyway
1757 if ( bbox->extent(NR::X) * scale[NR::X] > 1e6 ||
1758 bbox->extent(NR::Y) * scale[NR::Y] > 1e6 )
1759 {
1760 return;
1761 }
1763 NR::translate const n2d(-align);
1764 NR::translate const d2n(align);
1765 NR::Matrix const final( n2d * scale * d2n );
1766 sp_selection_apply_affine(selection, final);
1767 }
1769 void
1770 sp_selection_rotate_relative(Inkscape::Selection *selection, NR::Point const ¢er, gdouble const angle_degrees)
1771 {
1772 NR::translate const d2n(center);
1773 NR::translate const n2d(-center);
1774 NR::rotate const rotate(rotate_degrees(angle_degrees));
1775 NR::Matrix const final( NR::Matrix(n2d) * rotate * d2n );
1776 sp_selection_apply_affine(selection, final);
1777 }
1779 void
1780 sp_selection_skew_relative(Inkscape::Selection *selection, NR::Point const &align, double dx, double dy)
1781 {
1782 NR::translate const d2n(align);
1783 NR::translate const n2d(-align);
1784 NR::Matrix const skew(1, dy,
1785 dx, 1,
1786 0, 0);
1787 NR::Matrix const final( n2d * skew * d2n );
1788 sp_selection_apply_affine(selection, final);
1789 }
1791 void sp_selection_move_relative(Inkscape::Selection *selection, NR::Point const &move)
1792 {
1793 sp_selection_apply_affine(selection, NR::Matrix(NR::translate(move)));
1794 }
1796 void sp_selection_move_relative(Inkscape::Selection *selection, double dx, double dy)
1797 {
1798 sp_selection_apply_affine(selection, NR::Matrix(NR::translate(dx, dy)));
1799 }
1802 /**
1803 * \brief sp_selection_rotate_90
1804 *
1805 * This function rotates selected objects 90 degrees clockwise.
1806 *
1807 */
1809 void sp_selection_rotate_90_cw()
1810 {
1811 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1813 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1815 if (selection->isEmpty())
1816 return;
1818 GSList const *l = selection->itemList();
1819 NR::rotate const rot_neg_90(NR::Point(0, -1));
1820 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1821 SPItem *item = SP_ITEM(l2->data);
1822 sp_item_rotate_rel(item, rot_neg_90);
1823 }
1825 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_ROTATE_90_CCW,
1826 _("Rotate 90° CW"));
1827 }
1830 /**
1831 * \brief sp_selection_rotate_90_ccw
1832 *
1833 * This function rotates selected objects 90 degrees counter-clockwise.
1834 *
1835 */
1837 void sp_selection_rotate_90_ccw()
1838 {
1839 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1841 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1843 if (selection->isEmpty())
1844 return;
1846 GSList const *l = selection->itemList();
1847 NR::rotate const rot_neg_90(NR::Point(0, 1));
1848 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1849 SPItem *item = SP_ITEM(l2->data);
1850 sp_item_rotate_rel(item, rot_neg_90);
1851 }
1853 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_ROTATE_90_CW,
1854 _("Rotate 90° CCW"));
1855 }
1857 void
1858 sp_selection_rotate(Inkscape::Selection *selection, gdouble const angle_degrees)
1859 {
1860 if (selection->isEmpty())
1861 return;
1863 NR::Maybe<NR::Point> center = selection->center();
1864 if (!center) {
1865 return;
1866 }
1868 sp_selection_rotate_relative(selection, *center, angle_degrees);
1870 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1871 ( ( angle_degrees > 0 )
1872 ? "selector:rotate:ccw"
1873 : "selector:rotate:cw" ),
1874 SP_VERB_CONTEXT_SELECT,
1875 _("Rotate"));
1876 }
1878 /**
1879 \param angle the angle in "angular pixels", i.e. how many visible pixels must move the outermost point of the rotated object
1880 */
1881 void
1882 sp_selection_rotate_screen(Inkscape::Selection *selection, gdouble angle)
1883 {
1884 if (selection->isEmpty())
1885 return;
1887 NR::Maybe<NR::Rect> const bbox(selection->bounds());
1888 NR::Maybe<NR::Point> center = selection->center();
1890 if ( !bbox || !center ) {
1891 return;
1892 }
1894 gdouble const zoom = selection->desktop()->current_zoom();
1895 gdouble const zmove = angle / zoom;
1896 gdouble const r = NR::L2(bbox->cornerFarthestFrom(*center) - *center);
1898 gdouble const zangle = 180 * atan2(zmove, r) / M_PI;
1900 sp_selection_rotate_relative(selection, *center, zangle);
1902 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1903 ( (angle > 0)
1904 ? "selector:rotate:ccw"
1905 : "selector:rotate:cw" ),
1906 SP_VERB_CONTEXT_SELECT,
1907 _("Rotate by pixels"));
1908 }
1910 void
1911 sp_selection_scale(Inkscape::Selection *selection, gdouble grow)
1912 {
1913 if (selection->isEmpty())
1914 return;
1916 NR::Maybe<NR::Rect> const bbox(selection->bounds());
1917 if (!bbox) {
1918 return;
1919 }
1921 NR::Point const center(bbox->midpoint());
1923 // you can't scale "do nizhe pola" (below zero)
1924 double const max_len = bbox->maxExtent();
1925 if ( max_len + grow <= 1e-3 ) {
1926 return;
1927 }
1929 double const times = 1.0 + grow / max_len;
1930 sp_selection_scale_relative(selection, center, NR::scale(times, times));
1932 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1933 ( (grow > 0)
1934 ? "selector:scale:larger"
1935 : "selector:scale:smaller" ),
1936 SP_VERB_CONTEXT_SELECT,
1937 _("Scale"));
1938 }
1940 void
1941 sp_selection_scale_screen(Inkscape::Selection *selection, gdouble grow_pixels)
1942 {
1943 sp_selection_scale(selection,
1944 grow_pixels / selection->desktop()->current_zoom());
1945 }
1947 void
1948 sp_selection_scale_times(Inkscape::Selection *selection, gdouble times)
1949 {
1950 if (selection->isEmpty())
1951 return;
1953 NR::Maybe<NR::Rect> sel_bbox = selection->bounds();
1955 if (!sel_bbox) {
1956 return;
1957 }
1959 NR::Point const center(sel_bbox->midpoint());
1960 sp_selection_scale_relative(selection, center, NR::scale(times, times));
1961 sp_document_done(sp_desktop_document(selection->desktop()), SP_VERB_CONTEXT_SELECT,
1962 _("Scale by whole factor"));
1963 }
1965 void
1966 sp_selection_move(gdouble dx, gdouble dy)
1967 {
1968 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1969 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1970 if (selection->isEmpty()) {
1971 return;
1972 }
1974 sp_selection_move_relative(selection, dx, dy);
1976 if (dx == 0) {
1977 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical", SP_VERB_CONTEXT_SELECT,
1978 _("Move vertically"));
1979 } else if (dy == 0) {
1980 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal", SP_VERB_CONTEXT_SELECT,
1981 _("Move horizontally"));
1982 } else {
1983 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SELECT,
1984 _("Move"));
1985 }
1986 }
1988 void
1989 sp_selection_move_screen(gdouble dx, gdouble dy)
1990 {
1991 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1993 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1994 if (selection->isEmpty()) {
1995 return;
1996 }
1998 // same as sp_selection_move but divide deltas by zoom factor
1999 gdouble const zoom = desktop->current_zoom();
2000 gdouble const zdx = dx / zoom;
2001 gdouble const zdy = dy / zoom;
2002 sp_selection_move_relative(selection, zdx, zdy);
2004 if (dx == 0) {
2005 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical", SP_VERB_CONTEXT_SELECT,
2006 _("Move vertically by pixels"));
2007 } else if (dy == 0) {
2008 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal", SP_VERB_CONTEXT_SELECT,
2009 _("Move horizontally by pixels"));
2010 } else {
2011 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SELECT,
2012 _("Move"));
2013 }
2014 }
2016 namespace {
2018 template <typename D>
2019 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
2020 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
2022 template <typename D>
2023 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items, SPObject *root,
2024 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
2026 struct Forward {
2027 typedef SPObject *Iterator;
2029 static Iterator children(SPObject *o) { return sp_object_first_child(o); }
2030 static Iterator siblings_after(SPObject *o) { return SP_OBJECT_NEXT(o); }
2031 static void dispose(Iterator /*i*/) {}
2033 static SPObject *object(Iterator i) { return i; }
2034 static Iterator next(Iterator i) { return SP_OBJECT_NEXT(i); }
2035 };
2037 struct Reverse {
2038 typedef GSList *Iterator;
2040 static Iterator children(SPObject *o) {
2041 return make_list(o->firstChild(), NULL);
2042 }
2043 static Iterator siblings_after(SPObject *o) {
2044 return make_list(SP_OBJECT_PARENT(o)->firstChild(), o);
2045 }
2046 static void dispose(Iterator i) {
2047 g_slist_free(i);
2048 }
2050 static SPObject *object(Iterator i) {
2051 return reinterpret_cast<SPObject *>(i->data);
2052 }
2053 static Iterator next(Iterator i) { return i->next; }
2055 private:
2056 static GSList *make_list(SPObject *object, SPObject *limit) {
2057 GSList *list=NULL;
2058 while ( object != limit ) {
2059 list = g_slist_prepend(list, object);
2060 object = SP_OBJECT_NEXT(object);
2061 }
2062 return list;
2063 }
2064 };
2066 }
2068 void
2069 sp_selection_item_next(void)
2070 {
2071 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2072 g_return_if_fail(desktop != NULL);
2073 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2075 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
2076 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
2077 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
2079 SPObject *root;
2080 if (PREFS_SELECTION_ALL != inlayer) {
2081 root = selection->activeContext();
2082 } else {
2083 root = desktop->currentRoot();
2084 }
2086 SPItem *item=next_item_from_list<Forward>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
2088 if (item) {
2089 selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
2090 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
2091 scroll_to_show_item(desktop, item);
2092 }
2093 }
2094 }
2096 void
2097 sp_selection_item_prev(void)
2098 {
2099 SPDocument *document = SP_ACTIVE_DOCUMENT;
2100 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2101 g_return_if_fail(document != NULL);
2102 g_return_if_fail(desktop != NULL);
2103 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2105 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
2106 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
2107 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
2109 SPObject *root;
2110 if (PREFS_SELECTION_ALL != inlayer) {
2111 root = selection->activeContext();
2112 } else {
2113 root = desktop->currentRoot();
2114 }
2116 SPItem *item=next_item_from_list<Reverse>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
2118 if (item) {
2119 selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
2120 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
2121 scroll_to_show_item(desktop, item);
2122 }
2123 }
2124 }
2126 void sp_selection_next_patheffect_param(SPDesktop * dt)
2127 {
2128 if (!dt) return;
2130 Inkscape::Selection *selection = sp_desktop_selection(dt);
2131 if ( selection && !selection->isEmpty() ) {
2132 SPItem *item = selection->singleItem();
2133 if ( item && SP_IS_SHAPE(item)) {
2134 SPShape *shape = SP_SHAPE(item);
2135 if (sp_shape_has_path_effect(shape)) {
2136 sp_shape_edit_next_param_oncanvas(shape, dt);
2137 } else {
2138 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("The selection has no applied path effect."));
2139 }
2140 }
2141 }
2142 }
2144 void sp_selection_edit_clip_or_mask(SPDesktop * dt, bool clip)
2145 {
2146 if (!dt) return;
2148 Inkscape::Selection *selection = sp_desktop_selection(dt);
2149 if ( selection && !selection->isEmpty() ) {
2150 SPItem *item = selection->singleItem();
2151 if ( item ) {
2152 SPObject *obj = NULL;
2153 if (clip)
2154 obj = item->clip_ref ? SP_OBJECT(item->clip_ref->getObject()) : NULL;
2155 else
2156 obj = item->mask_ref ? SP_OBJECT(item->mask_ref->getObject()) : NULL;
2158 if (obj) {
2159 // obj is a group object, the children are the actual clippers
2160 for ( SPObject *child = obj->children ; child ; child = child->next ) {
2161 if ( SP_IS_ITEM(child) ) {
2162 // If not already in nodecontext, goto it!
2163 if (!tools_isactive(dt, TOOLS_NODES)) {
2164 tools_switch_current(TOOLS_NODES);
2165 }
2167 ShapeEditor * shape_editor = SP_NODE_CONTEXT( dt->event_context )->shape_editor;
2168 shape_editor->set_item(SP_ITEM(child));
2169 Inkscape::NodePath::Path *np = shape_editor->get_nodepath();
2170 if (np) {
2171 np->helperpath_rgba = clip ? 0x0000ffff : 0x800080ff;
2172 np->helperpath_width = 1.0;
2173 sp_nodepath_show_helperpath(np, true);
2174 }
2175 break; // break out of for loop after 1st encountered item
2176 }
2177 }
2178 } else if (clip) {
2179 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("The selection has no applied clip path."));
2180 } else {
2181 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("The selection has no applied mask."));
2182 }
2183 }
2184 }
2185 }
2188 namespace {
2190 template <typename D>
2191 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items,
2192 SPObject *root, bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
2193 {
2194 SPObject *current=root;
2195 while (items) {
2196 SPItem *item=SP_ITEM(items->data);
2197 if ( root->isAncestorOf(item) &&
2198 ( !only_in_viewport || desktop->isWithinViewport(item) ) )
2199 {
2200 current = item;
2201 break;
2202 }
2203 items = items->next;
2204 }
2206 GSList *path=NULL;
2207 while ( current != root ) {
2208 path = g_slist_prepend(path, current);
2209 current = SP_OBJECT_PARENT(current);
2210 }
2212 SPItem *next;
2213 // first, try from the current object
2214 next = next_item<D>(desktop, path, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
2215 g_slist_free(path);
2217 if (!next) { // if we ran out of objects, start over at the root
2218 next = next_item<D>(desktop, NULL, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
2219 }
2221 return next;
2222 }
2224 template <typename D>
2225 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
2226 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
2227 {
2228 typename D::Iterator children;
2229 typename D::Iterator iter;
2231 SPItem *found=NULL;
2233 if (path) {
2234 SPObject *object=reinterpret_cast<SPObject *>(path->data);
2235 g_assert(SP_OBJECT_PARENT(object) == root);
2236 if (desktop->isLayer(object)) {
2237 found = next_item<D>(desktop, path->next, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
2238 }
2239 iter = children = D::siblings_after(object);
2240 } else {
2241 iter = children = D::children(root);
2242 }
2244 while ( iter && !found ) {
2245 SPObject *object=D::object(iter);
2246 if (desktop->isLayer(object)) {
2247 if (PREFS_SELECTION_LAYER != inlayer) { // recurse into sublayers
2248 found = next_item<D>(desktop, NULL, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
2249 }
2250 } else if ( SP_IS_ITEM(object) &&
2251 ( !only_in_viewport || desktop->isWithinViewport(SP_ITEM(object)) ) &&
2252 ( !onlyvisible || !desktop->itemIsHidden(SP_ITEM(object))) &&
2253 ( !onlysensitive || !SP_ITEM(object)->isLocked()) &&
2254 !desktop->isLayer(SP_ITEM(object)) )
2255 {
2256 found = SP_ITEM(object);
2257 }
2258 iter = D::next(iter);
2259 }
2261 D::dispose(children);
2263 return found;
2264 }
2266 }
2268 /**
2269 * If \a item is not entirely visible then adjust visible area to centre on the centre on of
2270 * \a item.
2271 */
2272 void scroll_to_show_item(SPDesktop *desktop, SPItem *item)
2273 {
2274 NR::Rect dbox = desktop->get_display_area();
2275 NR::Maybe<NR::Rect> sbox = sp_item_bbox_desktop(item);
2277 if ( sbox && dbox.contains(*sbox) == false ) {
2278 NR::Point const s_dt = sbox->midpoint();
2279 NR::Point const s_w = desktop->d2w(s_dt);
2280 NR::Point const d_dt = dbox.midpoint();
2281 NR::Point const d_w = desktop->d2w(d_dt);
2282 NR::Point const moved_w( d_w - s_w );
2283 gint const dx = (gint) moved_w[X];
2284 gint const dy = (gint) moved_w[Y];
2285 desktop->scroll_world(dx, dy);
2286 }
2287 }
2290 void
2291 sp_selection_clone()
2292 {
2293 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2294 if (desktop == NULL)
2295 return;
2297 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2299 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
2301 // check if something is selected
2302 if (selection->isEmpty()) {
2303 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object</b> to clone."));
2304 return;
2305 }
2307 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
2309 selection->clear();
2311 // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
2312 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
2314 GSList *newsel = NULL;
2316 while (reprs) {
2317 Inkscape::XML::Node *sel_repr = (Inkscape::XML::Node *) reprs->data;
2318 Inkscape::XML::Node *parent = sp_repr_parent(sel_repr);
2320 Inkscape::XML::Node *clone = xml_doc->createElement("svg:use");
2321 sp_repr_set_attr(clone, "x", "0");
2322 sp_repr_set_attr(clone, "y", "0");
2323 sp_repr_set_attr(clone, "xlink:href", g_strdup_printf("#%s", sel_repr->attribute("id")));
2325 sp_repr_set_attr(clone, "inkscape:transform-center-x", sel_repr->attribute("inkscape:transform-center-x"));
2326 sp_repr_set_attr(clone, "inkscape:transform-center-y", sel_repr->attribute("inkscape:transform-center-y"));
2328 // add the new clone to the top of the original's parent
2329 parent->appendChild(clone);
2331 newsel = g_slist_prepend(newsel, clone);
2332 reprs = g_slist_remove(reprs, sel_repr);
2333 Inkscape::GC::release(clone);
2334 }
2336 // TRANSLATORS: only translate "string" in "context|string".
2337 // For more details, see http://developer.gnome.org/doc/API/2.0/glib/glib-I18N.html#Q-:CAPS
2338 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_CLONE,
2339 Q_("action|Clone"));
2341 selection->setReprList(newsel);
2343 g_slist_free(newsel);
2344 }
2346 void
2347 sp_selection_unlink()
2348 {
2349 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2350 if (!desktop)
2351 return;
2353 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2355 if (selection->isEmpty()) {
2356 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>clone</b> to unlink."));
2357 return;
2358 }
2360 // Get a copy of current selection.
2361 GSList *new_select = NULL;
2362 bool unlinked = false;
2363 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2364 items != NULL;
2365 items = items->next)
2366 {
2367 SPItem *item = (SPItem *) items->data;
2369 if (SP_IS_TEXT(item)) {
2370 SPObject *tspan = sp_tref_convert_to_tspan(SP_OBJECT(item));
2372 if (tspan) {
2373 SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
2374 }
2376 // Set unlink to true, and fall into the next if which
2377 // will include this text item in the new selection
2378 unlinked = true;
2379 }
2381 if (!(SP_IS_USE(item) || SP_IS_TREF(item))) {
2382 // keep the non-use item in the new selection
2383 new_select = g_slist_prepend(new_select, item);
2384 continue;
2385 }
2387 SPItem *unlink;
2388 if (SP_IS_USE(item)) {
2389 unlink = sp_use_unlink(SP_USE(item));
2390 } else /*if (SP_IS_TREF(use))*/ {
2391 unlink = SP_ITEM(sp_tref_convert_to_tspan(SP_OBJECT(item)));
2392 }
2394 unlinked = true;
2395 // Add ungrouped items to the new selection.
2396 new_select = g_slist_prepend(new_select, unlink);
2397 }
2399 if (new_select) { // set new selection
2400 selection->clear();
2401 selection->setList(new_select);
2402 g_slist_free(new_select);
2403 }
2404 if (!unlinked) {
2405 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No clones to unlink</b> in the selection."));
2406 }
2408 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNLINK_CLONE,
2409 _("Unlink clone"));
2410 }
2412 void
2413 sp_select_clone_original()
2414 {
2415 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2416 if (desktop == NULL)
2417 return;
2419 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2421 SPItem *item = selection->singleItem();
2423 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.");
2425 // Check if other than two objects are selected
2426 if (g_slist_length((GSList *) selection->itemList()) != 1 || !item) {
2427 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
2428 return;
2429 }
2431 SPItem *original = NULL;
2432 if (SP_IS_USE(item)) {
2433 original = sp_use_get_original (SP_USE(item));
2434 } else if (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) {
2435 original = sp_offset_get_source (SP_OFFSET(item));
2436 } else if (SP_IS_TEXT_TEXTPATH(item)) {
2437 original = sp_textpath_get_path_item (SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))));
2438 } else if (SP_IS_FLOWTEXT(item)) {
2439 original = SP_FLOWTEXT(item)->get_frame (NULL); // first frame only
2440 } else { // it's an object that we don't know what to do with
2441 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
2442 return;
2443 }
2445 if (!original) {
2446 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>Cannot find</b> the object to select (orphaned clone, offset, textpath, flowed text?)"));
2447 return;
2448 }
2450 for (SPObject *o = original; o && !SP_IS_ROOT(o); o = SP_OBJECT_PARENT (o)) {
2451 if (SP_IS_DEFS (o)) {
2452 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("The object you're trying to select is <b>not visible</b> (it is in <defs>)"));
2453 return;
2454 }
2455 }
2457 if (original) {
2458 selection->clear();
2459 selection->set(original);
2460 if (SP_CYCLING == SP_CYCLE_FOCUS) {
2461 scroll_to_show_item(desktop, original);
2462 }
2463 }
2464 }
2467 void sp_selection_to_marker(bool apply)
2468 {
2469 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2470 if (desktop == NULL)
2471 return;
2473 SPDocument *doc = sp_desktop_document(desktop);
2474 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2476 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2478 // check if something is selected
2479 if (selection->isEmpty()) {
2480 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to marker."));
2481 return;
2482 }
2484 sp_document_ensure_up_to_date(doc);
2485 NR::Maybe<NR::Rect> r = selection->bounds();
2486 NR::Maybe<NR::Point> c = selection->center();
2487 if ( !r || !c || r->isEmpty() ) {
2488 return;
2489 }
2491 // calculate the transform to be applied to objects to move them to 0,0
2492 NR::Point move_p = NR::Point(0, sp_document_height(doc)) - *c;
2493 move_p[NR::Y] = -move_p[NR::Y];
2494 NR::Matrix move = NR::Matrix (NR::translate (move_p));
2496 GSList *items = g_slist_copy((GSList *) selection->itemList());
2498 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2500 // bottommost object, after sorting
2501 SPObject *parent = SP_OBJECT_PARENT (items->data);
2503 NR::Matrix parent_transform = sp_item_i2root_affine(SP_ITEM(parent));
2505 // remember the position of the first item
2506 gint pos = SP_OBJECT_REPR (items->data)->position();
2507 (void)pos; // TODO check why this was remembered
2509 // create a list of duplicates
2510 GSList *repr_copies = NULL;
2511 for (GSList *i = items; i != NULL; i = i->next) {
2512 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2513 repr_copies = g_slist_prepend (repr_copies, dup);
2514 }
2516 NR::Rect bounds(desktop->dt2doc(r->min()), desktop->dt2doc(r->max()));
2518 if (apply) {
2519 // delete objects so that their clones don't get alerted; this object will be restored shortly
2520 for (GSList *i = items; i != NULL; i = i->next) {
2521 SPObject *item = SP_OBJECT (i->data);
2522 item->deleteObject (false);
2523 }
2524 }
2526 // Hack: Temporarily set clone compensation to unmoved, so that we can move clone-originals
2527 // without disturbing clones.
2528 // See ActorAlign::on_button_click() in src/ui/dialog/align-and-distribute.cpp
2529 int saved_compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2530 prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2532 gchar const *mark_id = generate_marker(repr_copies, bounds, doc,
2533 ( NR::Matrix(NR::translate(desktop->dt2doc(NR::Point(r->min()[NR::X],
2534 r->max()[NR::Y]))))
2535 * parent_transform.inverse() ),
2536 parent_transform * move);
2537 (void)mark_id;
2539 // restore compensation setting
2540 prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
2543 g_slist_free (items);
2545 sp_document_done (doc, SP_VERB_EDIT_SELECTION_2_MARKER,
2546 _("Objects to marker"));
2547 }
2549 static void sp_selection_to_guides_recursive(SPItem *item, bool deleteitem) {
2550 if (SP_IS_GROUP(item) && !SP_IS_BOX3D(item)) {
2551 for (GSList *i = sp_item_group_item_list (SP_GROUP(item)); i != NULL; i = i->next) {
2552 sp_selection_to_guides_recursive(SP_ITEM(i->data), deleteitem);
2553 }
2554 } else {
2555 sp_item_convert_item_to_guides(item);
2557 if (deleteitem) {
2558 SP_OBJECT(item)->deleteObject(true);
2559 }
2560 }
2561 }
2563 void sp_selection_to_guides()
2564 {
2565 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2566 if (desktop == NULL)
2567 return;
2569 SPDocument *doc = sp_desktop_document(desktop);
2570 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2571 // we need to copy the list because it gets reset when objects are deleted
2572 GSList *items = g_slist_copy((GSList *) selection->itemList());
2574 if (!items) {
2575 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to guides."));
2576 return;
2577 }
2579 bool deleteitem = (prefs_get_int_attribute("tools", "cvg_keep_objects", 0) == 0);
2581 for (GSList const *i = items; i != NULL; i = i->next) {
2582 sp_selection_to_guides_recursive(SP_ITEM(i->data), deleteitem);
2583 }
2585 sp_document_done (doc, SP_VERB_EDIT_SELECTION_2_GUIDES, _("Objects to guides"));
2586 }
2588 void
2589 sp_selection_tile(bool apply)
2590 {
2591 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2592 if (desktop == NULL)
2593 return;
2595 SPDocument *doc = sp_desktop_document(desktop);
2596 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2598 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2600 // check if something is selected
2601 if (selection->isEmpty()) {
2602 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to pattern."));
2603 return;
2604 }
2606 sp_document_ensure_up_to_date(doc);
2607 NR::Maybe<NR::Rect> r = selection->bounds();
2608 if ( !r || r->isEmpty() ) {
2609 return;
2610 }
2612 // calculate the transform to be applied to objects to move them to 0,0
2613 NR::Point move_p = NR::Point(0, sp_document_height(doc)) - (r->min() + NR::Point (0, r->extent(NR::Y)));
2614 move_p[NR::Y] = -move_p[NR::Y];
2615 NR::Matrix move = NR::Matrix (NR::translate (move_p));
2617 GSList *items = g_slist_copy((GSList *) selection->itemList());
2619 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2621 // bottommost object, after sorting
2622 SPObject *parent = SP_OBJECT_PARENT (items->data);
2624 NR::Matrix parent_transform = sp_item_i2root_affine(SP_ITEM(parent));
2626 // remember the position of the first item
2627 gint pos = SP_OBJECT_REPR (items->data)->position();
2629 // create a list of duplicates
2630 GSList *repr_copies = NULL;
2631 for (GSList *i = items; i != NULL; i = i->next) {
2632 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2633 repr_copies = g_slist_prepend (repr_copies, dup);
2634 }
2636 NR::Rect bounds(desktop->dt2doc(r->min()), desktop->dt2doc(r->max()));
2638 if (apply) {
2639 // delete objects so that their clones don't get alerted; this object will be restored shortly
2640 for (GSList *i = items; i != NULL; i = i->next) {
2641 SPObject *item = SP_OBJECT (i->data);
2642 item->deleteObject (false);
2643 }
2644 }
2646 // Hack: Temporarily set clone compensation to unmoved, so that we can move clone-originals
2647 // without disturbing clones.
2648 // See ActorAlign::on_button_click() in src/ui/dialog/align-and-distribute.cpp
2649 int saved_compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2650 prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2652 gchar const *pat_id = pattern_tile(repr_copies, bounds, doc,
2653 ( NR::Matrix(NR::translate(desktop->dt2doc(NR::Point(r->min()[NR::X],
2654 r->max()[NR::Y]))))
2655 * parent_transform.inverse() ),
2656 parent_transform * move);
2658 // restore compensation setting
2659 prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
2661 if (apply) {
2662 Inkscape::XML::Node *rect = xml_doc->createElement("svg:rect");
2663 rect->setAttribute("style", g_strdup_printf("stroke:none;fill:url(#%s)", pat_id));
2665 NR::Point min = bounds.min() * parent_transform.inverse();
2666 NR::Point max = bounds.max() * parent_transform.inverse();
2668 sp_repr_set_svg_double(rect, "width", max[NR::X] - min[NR::X]);
2669 sp_repr_set_svg_double(rect, "height", max[NR::Y] - min[NR::Y]);
2670 sp_repr_set_svg_double(rect, "x", min[NR::X]);
2671 sp_repr_set_svg_double(rect, "y", min[NR::Y]);
2673 // restore parent and position
2674 SP_OBJECT_REPR (parent)->appendChild(rect);
2675 rect->setPosition(pos > 0 ? pos : 0);
2676 SPItem *rectangle = (SPItem *) sp_desktop_document (desktop)->getObjectByRepr(rect);
2678 Inkscape::GC::release(rect);
2680 selection->clear();
2681 selection->set(rectangle);
2682 }
2684 g_slist_free (items);
2686 sp_document_done (doc, SP_VERB_EDIT_TILE,
2687 _("Objects to pattern"));
2688 }
2690 void
2691 sp_selection_untile()
2692 {
2693 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2694 if (desktop == NULL)
2695 return;
2697 SPDocument *doc = sp_desktop_document(desktop);
2698 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2700 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2702 // check if something is selected
2703 if (selection->isEmpty()) {
2704 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object with pattern fill</b> to extract objects from."));
2705 return;
2706 }
2708 GSList *new_select = NULL;
2710 bool did = false;
2712 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2713 items != NULL;
2714 items = items->next) {
2716 SPItem *item = (SPItem *) items->data;
2718 SPStyle *style = SP_OBJECT_STYLE (item);
2720 if (!style || !style->fill.isPaintserver())
2721 continue;
2723 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
2725 if (!SP_IS_PATTERN(server))
2726 continue;
2728 did = true;
2730 SPPattern *pattern = pattern_getroot (SP_PATTERN (server));
2732 NR::Matrix pat_transform = pattern_patternTransform (SP_PATTERN (server));
2733 pat_transform *= item->transform;
2735 for (SPObject *child = sp_object_first_child(SP_OBJECT(pattern)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2736 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate(xml_doc);
2737 SPItem *i = SP_ITEM (desktop->currentLayer()->appendChildRepr(copy));
2739 // FIXME: relink clones to the new canvas objects
2740 // use SPObject::setid when mental finishes it to steal ids of
2742 // this is needed to make sure the new item has curve (simply requestDisplayUpdate does not work)
2743 sp_document_ensure_up_to_date (doc);
2745 NR::Matrix transform( i->transform * pat_transform );
2746 sp_item_write_transform(i, SP_OBJECT_REPR(i), transform);
2748 new_select = g_slist_prepend(new_select, i);
2749 }
2751 SPCSSAttr *css = sp_repr_css_attr_new ();
2752 sp_repr_css_set_property (css, "fill", "none");
2753 sp_repr_css_change (SP_OBJECT_REPR (item), css, "style");
2754 }
2756 if (!did) {
2757 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No pattern fills</b> in the selection."));
2758 } else {
2759 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNTILE,
2760 _("Pattern to objects"));
2761 selection->setList(new_select);
2762 }
2763 }
2765 void
2766 sp_selection_get_export_hints (Inkscape::Selection *selection, char const **filename, float *xdpi, float *ydpi)
2767 {
2768 if (selection->isEmpty()) {
2769 return;
2770 }
2772 GSList const *reprlst = selection->reprList();
2773 bool filename_search = TRUE;
2774 bool xdpi_search = TRUE;
2775 bool ydpi_search = TRUE;
2777 for(; reprlst != NULL &&
2778 filename_search &&
2779 xdpi_search &&
2780 ydpi_search;
2781 reprlst = reprlst->next) {
2782 gchar const *dpi_string;
2783 Inkscape::XML::Node * repr = (Inkscape::XML::Node *)reprlst->data;
2785 if (filename_search) {
2786 *filename = repr->attribute("inkscape:export-filename");
2787 if (*filename != NULL)
2788 filename_search = FALSE;
2789 }
2791 if (xdpi_search) {
2792 dpi_string = NULL;
2793 dpi_string = repr->attribute("inkscape:export-xdpi");
2794 if (dpi_string != NULL) {
2795 *xdpi = atof(dpi_string);
2796 xdpi_search = FALSE;
2797 }
2798 }
2800 if (ydpi_search) {
2801 dpi_string = NULL;
2802 dpi_string = repr->attribute("inkscape:export-ydpi");
2803 if (dpi_string != NULL) {
2804 *ydpi = atof(dpi_string);
2805 ydpi_search = FALSE;
2806 }
2807 }
2808 }
2809 }
2811 void
2812 sp_document_get_export_hints (SPDocument *doc, char const **filename, float *xdpi, float *ydpi)
2813 {
2814 Inkscape::XML::Node * repr = sp_document_repr_root(doc);
2815 gchar const *dpi_string;
2817 *filename = repr->attribute("inkscape:export-filename");
2819 dpi_string = NULL;
2820 dpi_string = repr->attribute("inkscape:export-xdpi");
2821 if (dpi_string != NULL) {
2822 *xdpi = atof(dpi_string);
2823 }
2825 dpi_string = NULL;
2826 dpi_string = repr->attribute("inkscape:export-ydpi");
2827 if (dpi_string != NULL) {
2828 *ydpi = atof(dpi_string);
2829 }
2830 }
2832 void
2833 sp_selection_create_bitmap_copy ()
2834 {
2835 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2836 if (desktop == NULL)
2837 return;
2839 SPDocument *document = sp_desktop_document(desktop);
2840 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
2842 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2844 // check if something is selected
2845 if (selection->isEmpty()) {
2846 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to make a bitmap copy."));
2847 return;
2848 }
2850 // Get the bounding box of the selection
2851 NRRect bbox;
2852 sp_document_ensure_up_to_date (document);
2853 selection->bounds(&bbox);
2854 if (NR_RECT_DFLS_TEST_EMPTY(&bbox)) {
2855 return; // exceptional situation, so not bother with a translatable error message, just quit quietly
2856 }
2858 // List of the items to show; all others will be hidden
2859 GSList *items = g_slist_copy ((GSList *) selection->itemList());
2861 // Sort items so that the topmost comes last
2862 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
2864 // Generate a random value from the current time (you may create bitmap from the same object(s)
2865 // multiple times, and this is done so that they don't clash)
2866 GTimeVal cu;
2867 g_get_current_time (&cu);
2868 guint current = (int) (cu.tv_sec * 1000000 + cu.tv_usec) % 1024;
2870 // Create the filename
2871 gchar *filename = g_strdup_printf ("%s-%s-%u.png", document->name, SP_OBJECT_REPR(items->data)->attribute("id"), current);
2872 // Imagemagick is known not to handle spaces in filenames, so we replace anything but letters,
2873 // digits, and a few other chars, with "_"
2874 filename = g_strcanon (filename, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.=+~$#@^&!?", '_');
2875 // Build the complete path by adding document->base if set
2876 gchar *filepath = g_build_filename (document->base?document->base:"", filename, NULL);
2878 //g_print ("%s\n", filepath);
2880 // Remember parent and z-order of the topmost one
2881 gint pos = SP_OBJECT_REPR(g_slist_last(items)->data)->position();
2882 SPObject *parent_object = SP_OBJECT_PARENT(g_slist_last(items)->data);
2883 Inkscape::XML::Node *parent = SP_OBJECT_REPR(parent_object);
2885 // Calculate resolution
2886 double res;
2887 int const prefs_res = prefs_get_int_attribute ("options.createbitmap", "resolution", 0);
2888 int const prefs_min = prefs_get_int_attribute ("options.createbitmap", "minsize", 0);
2889 if (0 < prefs_res) {
2890 // If it's given explicitly in prefs, take it
2891 res = prefs_res;
2892 } else if (0 < prefs_min) {
2893 // If minsize is given, look up minimum bitmap size (default 250 pixels) and calculate resolution from it
2894 res = PX_PER_IN * prefs_min / MIN ((bbox.x1 - bbox.x0), (bbox.y1 - bbox.y0));
2895 } else {
2896 float hint_xdpi = 0, hint_ydpi = 0;
2897 char const *hint_filename;
2898 // take resolution hint from the selected objects
2899 sp_selection_get_export_hints (selection, &hint_filename, &hint_xdpi, &hint_ydpi);
2900 if (hint_xdpi != 0) {
2901 res = hint_xdpi;
2902 } else {
2903 // take resolution hint from the document
2904 sp_document_get_export_hints (document, &hint_filename, &hint_xdpi, &hint_ydpi);
2905 if (hint_xdpi != 0) {
2906 res = hint_xdpi;
2907 } else {
2908 // if all else fails, take the default 90 dpi
2909 res = PX_PER_IN;
2910 }
2911 }
2912 }
2914 // The width and height of the bitmap in pixels
2915 unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * res / PX_PER_IN);
2916 unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * res / PX_PER_IN);
2918 // Find out if we have to run a filter
2919 gchar const *run = NULL;
2920 gchar const *filter = prefs_get_string_attribute ("options.createbitmap", "filter");
2921 if (filter) {
2922 // filter command is given;
2923 // see if we have a parameter to pass to it
2924 gchar const *param1 = prefs_get_string_attribute ("options.createbitmap", "filter_param1");
2925 if (param1) {
2926 if (param1[strlen(param1) - 1] == '%') {
2927 // if the param string ends with %, interpret it as a percentage of the image's max dimension
2928 gchar p1[256];
2929 g_ascii_dtostr (p1, 256, ceil (g_ascii_strtod (param1, NULL) * MAX(width, height) / 100));
2930 // the first param is always the image filename, the second is param1
2931 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, p1);
2932 } else {
2933 // otherwise pass the param1 unchanged
2934 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, param1);
2935 }
2936 } else {
2937 // run without extra parameter
2938 run = g_strdup_printf ("%s \"%s\"", filter, filepath);
2939 }
2940 }
2942 // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
2943 NR::Matrix eek = sp_item_i2d_affine (SP_ITEM(parent_object));
2944 NR::Matrix t;
2946 double shift_x = bbox.x0;
2947 double shift_y = bbox.y1;
2948 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
2949 shift_x = round (shift_x);
2950 shift_y = -round (-shift_y); // this gets correct rounding despite coordinate inversion, remove the negations when the inversion is gone
2951 }
2952 t = NR::scale(1, -1) * NR::translate (shift_x, shift_y) * eek.inverse();
2954 // Do the export
2955 sp_export_png_file(document, filepath,
2956 bbox.x0, bbox.y0, bbox.x1, bbox.y1,
2957 width, height, res, res,
2958 (guint32) 0xffffff00,
2959 NULL, NULL,
2960 true, /*bool force_overwrite,*/
2961 items);
2963 g_slist_free (items);
2965 // Run filter, if any
2966 if (run) {
2967 g_print ("Running external filter: %s\n", run);
2968 system (run);
2969 }
2971 // Import the image back
2972 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filepath, NULL);
2973 if (pb) {
2974 // Create the repr for the image
2975 Inkscape::XML::Node * repr = xml_doc->createElement("svg:image");
2976 repr->setAttribute("xlink:href", filename);
2977 repr->setAttribute("sodipodi:absref", filepath);
2978 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
2979 sp_repr_set_svg_double(repr, "width", width);
2980 sp_repr_set_svg_double(repr, "height", height);
2981 } else {
2982 sp_repr_set_svg_double(repr, "width", (bbox.x1 - bbox.x0));
2983 sp_repr_set_svg_double(repr, "height", (bbox.y1 - bbox.y0));
2984 }
2986 // Write transform
2987 gchar *c=sp_svg_transform_write(t);
2988 repr->setAttribute("transform", c);
2989 g_free(c);
2991 // add the new repr to the parent
2992 parent->appendChild(repr);
2994 // move to the saved position
2995 repr->setPosition(pos > 0 ? pos + 1 : 1);
2997 // Set selection to the new image
2998 selection->clear();
2999 selection->add(repr);
3001 // Clean up
3002 Inkscape::GC::release(repr);
3003 gdk_pixbuf_unref (pb);
3005 // Complete undoable transaction
3006 sp_document_done (document, SP_VERB_SELECTION_CREATE_BITMAP,
3007 _("Create bitmap"));
3008 }
3010 g_free (filename);
3011 g_free (filepath);
3012 }
3014 /**
3015 * \brief sp_selection_set_mask
3016 *
3017 * This function creates a mask or clipPath from selection
3018 * Two different modes:
3019 * if applyToLayer, all selection is moved to DEFS as mask/clippath
3020 * and is applied to current layer
3021 * otherwise, topmost object is used as mask for other objects
3022 * If \a apply_clip_path parameter is true, clipPath is created, otherwise mask
3023 *
3024 */
3025 void
3026 sp_selection_set_mask(bool apply_clip_path, bool apply_to_layer)
3027 {
3028 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
3029 if (desktop == NULL)
3030 return;
3032 SPDocument *doc = sp_desktop_document(desktop);
3033 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
3035 Inkscape::Selection *selection = sp_desktop_selection(desktop);
3037 // check if something is selected
3038 bool is_empty = selection->isEmpty();
3039 if ( apply_to_layer && is_empty) {
3040 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to create clippath or mask from."));
3041 return;
3042 } else if (!apply_to_layer && ( is_empty || NULL == selection->itemList()->next )) {
3043 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select mask object and <b>object(s)</b> to apply clippath or mask to."));
3044 return;
3045 }
3047 // FIXME: temporary patch to prevent crash!
3048 // Remove this when bboxes are fixed to not blow up on an item clipped/masked with its own clone
3049 bool clone_with_original = selection_contains_both_clone_and_original (selection);
3050 if (clone_with_original) {
3051 return; // in this version, you cannot clip/mask an object with its own clone
3052 }
3053 // /END FIXME
3055 sp_document_ensure_up_to_date(doc);
3057 GSList *items = g_slist_copy((GSList *) selection->itemList());
3059 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
3061 // create a list of duplicates
3062 GSList *mask_items = NULL;
3063 GSList *apply_to_items = NULL;
3064 GSList *items_to_delete = NULL;
3065 bool topmost = prefs_get_int_attribute ("options.maskobject", "topmost", 1);
3066 bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
3068 if (apply_to_layer) {
3069 // all selected items are used for mask, which is applied to a layer
3070 apply_to_items = g_slist_prepend (apply_to_items, desktop->currentLayer());
3072 for (GSList *i = items; i != NULL; i = i->next) {
3073 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
3074 mask_items = g_slist_prepend (mask_items, dup);
3076 if (remove_original) {
3077 SPObject *item = SP_OBJECT (i->data);
3078 items_to_delete = g_slist_prepend (items_to_delete, item);
3079 }
3080 }
3081 } else if (!topmost) {
3082 // topmost item is used as a mask, which is applied to other items in a selection
3083 GSList *i = items;
3084 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
3085 mask_items = g_slist_prepend (mask_items, dup);
3087 if (remove_original) {
3088 SPObject *item = SP_OBJECT (i->data);
3089 items_to_delete = g_slist_prepend (items_to_delete, item);
3090 }
3092 for (i = i->next; i != NULL; i = i->next) {
3093 apply_to_items = g_slist_prepend (apply_to_items, i->data);
3094 }
3095 } else {
3096 GSList *i = NULL;
3097 for (i = items; NULL != i->next; i = i->next) {
3098 apply_to_items = g_slist_prepend (apply_to_items, i->data);
3099 }
3101 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
3102 mask_items = g_slist_prepend (mask_items, dup);
3104 if (remove_original) {
3105 SPObject *item = SP_OBJECT (i->data);
3106 items_to_delete = g_slist_prepend (items_to_delete, item);
3107 }
3108 }
3110 g_slist_free (items);
3111 items = NULL;
3113 gchar const *attributeName = apply_clip_path ? "clip-path" : "mask";
3114 for (GSList *i = apply_to_items; NULL != i; i = i->next) {
3115 SPItem *item = reinterpret_cast<SPItem *>(i->data);
3116 // inverted object transform should be applied to a mask object,
3117 // as mask is calculated in user space (after applying transform)
3118 NR::Matrix maskTransform (item->transform.inverse());
3120 GSList *mask_items_dup = NULL;
3121 for (GSList *mask_item = mask_items; NULL != mask_item; mask_item = mask_item->next) {
3122 Inkscape::XML::Node *dup = reinterpret_cast<Inkscape::XML::Node *>(mask_item->data)->duplicate(xml_doc);
3123 mask_items_dup = g_slist_prepend (mask_items_dup, dup);
3124 }
3126 gchar const *mask_id = NULL;
3127 if (apply_clip_path) {
3128 mask_id = sp_clippath_create(mask_items_dup, doc, &maskTransform);
3129 } else {
3130 mask_id = sp_mask_create(mask_items_dup, doc, &maskTransform);
3131 }
3133 g_slist_free (mask_items_dup);
3134 mask_items_dup = NULL;
3136 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, g_strdup_printf("url(#%s)", mask_id));
3137 }
3139 g_slist_free (mask_items);
3140 g_slist_free (apply_to_items);
3142 for (GSList *i = items_to_delete; NULL != i; i = i->next) {
3143 SPObject *item = SP_OBJECT (i->data);
3144 item->deleteObject (false);
3145 }
3146 g_slist_free (items_to_delete);
3148 if (apply_clip_path)
3149 sp_document_done (doc, SP_VERB_OBJECT_SET_CLIPPATH, _("Set clipping path"));
3150 else
3151 sp_document_done (doc, SP_VERB_OBJECT_SET_MASK, _("Set mask"));
3152 }
3154 void sp_selection_unset_mask(bool apply_clip_path) {
3155 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
3156 if (desktop == NULL)
3157 return;
3159 SPDocument *doc = sp_desktop_document(desktop);
3160 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
3161 Inkscape::Selection *selection = sp_desktop_selection(desktop);
3163 // check if something is selected
3164 if (selection->isEmpty()) {
3165 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to remove clippath or mask from."));
3166 return;
3167 }
3169 bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
3170 sp_document_ensure_up_to_date(doc);
3172 gchar const *attributeName = apply_clip_path ? "clip-path" : "mask";
3173 std::map<SPObject*,SPItem*> referenced_objects;
3174 for (GSList const *i = selection->itemList(); NULL != i; i = i->next) {
3175 if (remove_original) {
3176 // remember referenced mask/clippath, so orphaned masks can be moved back to document
3177 SPItem *item = reinterpret_cast<SPItem *>(i->data);
3178 Inkscape::URIReference *uri_ref = NULL;
3180 if (apply_clip_path) {
3181 uri_ref = item->clip_ref;
3182 } else {
3183 uri_ref = item->mask_ref;
3184 }
3186 // collect distinct mask object (and associate with item to apply transform)
3187 if (NULL != uri_ref && NULL != uri_ref->getObject()) {
3188 referenced_objects[uri_ref->getObject()] = item;
3189 }
3190 }
3192 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, "none");
3193 }
3195 // restore mask objects into a document
3196 for ( std::map<SPObject*,SPItem*>::iterator it = referenced_objects.begin() ; it != referenced_objects.end() ; ++it) {
3197 SPObject *obj = (*it).first;
3198 GSList *items_to_move = NULL;
3199 for (SPObject *child = sp_object_first_child(obj) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
3200 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate(xml_doc);
3201 items_to_move = g_slist_prepend (items_to_move, copy);
3202 }
3204 if (!obj->isReferenced()) {
3205 // delete from defs if no other object references this mask
3206 obj->deleteObject(false);
3207 }
3209 // remember parent and position of the item to which the clippath/mask was applied
3210 Inkscape::XML::Node *parent = SP_OBJECT_REPR((*it).second)->parent();
3211 gint pos = SP_OBJECT_REPR((*it).second)->position();
3213 for (GSList *i = items_to_move; NULL != i; i = i->next) {
3214 Inkscape::XML::Node *repr = (Inkscape::XML::Node *)i->data;
3216 // insert into parent, restore pos
3217 parent->appendChild(repr);
3218 repr->setPosition((pos + 1) > 0 ? (pos + 1) : 0);
3220 SPItem *mask_item = (SPItem *) sp_desktop_document (desktop)->getObjectByRepr(repr);
3221 selection->add(repr);
3223 // transform mask, so it is moved the same spot where mask was applied
3224 NR::Matrix transform (mask_item->transform);
3225 transform *= (*it).second->transform;
3226 sp_item_write_transform(mask_item, SP_OBJECT_REPR(mask_item), transform);
3227 }
3229 g_slist_free (items_to_move);
3230 }
3232 if (apply_clip_path)
3233 sp_document_done (doc, SP_VERB_OBJECT_UNSET_CLIPPATH, _("Release clipping path"));
3234 else
3235 sp_document_done (doc, SP_VERB_OBJECT_UNSET_MASK, _("Release mask"));
3236 }
3238 void fit_canvas_to_selection(SPDesktop *desktop) {
3239 g_return_if_fail(desktop != NULL);
3240 SPDocument *doc = sp_desktop_document(desktop);
3242 g_return_if_fail(doc != NULL);
3243 g_return_if_fail(desktop->selection != NULL);
3245 if (desktop->selection->isEmpty()) {
3246 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to fit canvas to."));
3247 return;
3248 }
3249 NR::Maybe<NR::Rect> const bbox(desktop->selection->bounds());
3250 if (bbox && !bbox->isEmpty()) {
3251 doc->fitToRect(*bbox);
3252 }
3253 };
3255 void fit_canvas_to_drawing(SPDocument *doc) {
3256 g_return_if_fail(doc != NULL);
3258 sp_document_ensure_up_to_date(doc);
3259 SPItem const *const root = SP_ITEM(doc->root);
3260 NR::Maybe<NR::Rect> const bbox(root->getBounds(sp_item_i2r_affine(root)));
3261 if (bbox && !bbox->isEmpty()) {
3262 doc->fitToRect(*bbox);
3263 }
3264 };
3266 void fit_canvas_to_selection_or_drawing(SPDesktop *desktop) {
3267 g_return_if_fail(desktop != NULL);
3268 SPDocument *doc = sp_desktop_document(desktop);
3270 g_return_if_fail(doc != NULL);
3271 g_return_if_fail(desktop->selection != NULL);
3273 if (desktop->selection->isEmpty()) {
3274 fit_canvas_to_drawing(doc);
3275 } else {
3276 fit_canvas_to_selection(desktop);
3277 }
3279 sp_document_done(doc, SP_VERB_FIT_CANVAS_TO_DRAWING,
3280 _("Fit page to selection"));
3281 };
3283 static void itemtree_map(void (*f)(SPItem *, SPDesktop *), SPObject *root, SPDesktop *desktop) {
3284 // don't operate on layers
3285 if (SP_IS_ITEM(root) && !desktop->isLayer(SP_ITEM(root))) {
3286 f(SP_ITEM(root), desktop);
3287 }
3288 for ( SPObject::SiblingIterator iter = root->firstChild() ; iter ; ++iter ) {
3289 //don't recurse into locked layers
3290 if (!(SP_IS_ITEM(&*iter) && desktop->isLayer(SP_ITEM(&*iter)) && SP_ITEM(&*iter)->isLocked())) {
3291 itemtree_map(f, iter, desktop);
3292 }
3293 }
3294 }
3296 static void unlock(SPItem *item, SPDesktop */*desktop*/) {
3297 if (item->isLocked()) {
3298 item->setLocked(FALSE);
3299 }
3300 }
3302 static void unhide(SPItem *item, SPDesktop *desktop) {
3303 if (desktop->itemIsHidden(item)) {
3304 item->setExplicitlyHidden(FALSE);
3305 }
3306 }
3308 static void process_all(void (*f)(SPItem *, SPDesktop *), SPDesktop *dt, bool layer_only) {
3309 if (!dt) return;
3311 SPObject *root;
3312 if (layer_only) {
3313 root = dt->currentLayer();
3314 } else {
3315 root = dt->currentRoot();
3316 }
3318 itemtree_map(f, root, dt);
3319 }
3321 void unlock_all(SPDesktop *dt) {
3322 process_all(&unlock, dt, true);
3323 }
3325 void unlock_all_in_all_layers(SPDesktop *dt) {
3326 process_all(&unlock, dt, false);
3327 }
3329 void unhide_all(SPDesktop *dt) {
3330 process_all(&unhide, dt, true);
3331 }
3333 void unhide_all_in_all_layers(SPDesktop *dt) {
3334 process_all(&unhide, dt, false);
3335 }
3338 GSList * sp_selection_get_clipboard() {
3339 return clipboard;
3340 }
3343 /*
3344 Local Variables:
3345 mode:c++
3346 c-file-style:"stroustrup"
3347 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
3348 indent-tabs-mode:nil
3349 fill-column:99
3350 End:
3351 */
3352 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :