1 #define __SP_SELECTION_CHEMISTRY_C__
3 /** @file
4 * @brief Miscellanous operations on selected items
5 */
6 /* Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * Frank Felfe <innerspace@iname.com>
9 * MenTaLguY <mental@rydia.net>
10 * bulia byak <buliabyak@users.sf.net>
11 * Andrius R. <knutux@gmail.com>
12 *
13 * Copyright (C) 1999-2006 authors
14 * Copyright (C) 2001-2002 Ximian, Inc.
15 *
16 * Released under GNU GPL, read the file 'COPYING' for more information
17 */
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
23 #include "selection-chemistry.h"
25 #include <gtkmm/clipboard.h>
27 #include "svg/svg.h"
28 #include "inkscape.h"
29 #include "desktop.h"
30 #include "desktop-style.h"
31 #include "selection.h"
32 #include "tools-switch.h"
33 #include "desktop-handles.h"
34 #include "message-stack.h"
35 #include "sp-item-transform.h"
36 #include "marker.h"
37 #include "sp-use.h"
38 #include "sp-textpath.h"
39 #include "sp-tspan.h"
40 #include "sp-tref.h"
41 #include "sp-flowtext.h"
42 #include "sp-flowregion.h"
43 #include "text-editing.h"
44 #include "text-context.h"
45 #include "connector-context.h"
46 #include "sp-path.h"
47 #include "sp-conn-end.h"
48 #include "dropper-context.h"
49 #include <glibmm/i18n.h>
50 #include "libnr/nr-matrix-rotate-ops.h"
51 #include "libnr/nr-matrix-translate-ops.h"
52 #include "libnr/nr-rotate-fns.h"
53 #include "libnr/nr-scale-ops.h"
54 #include "libnr/nr-scale-translate-ops.h"
55 #include "libnr/nr-translate-matrix-ops.h"
56 #include "libnr/nr-translate-scale-ops.h"
57 #include "xml/repr.h"
58 #include "style.h"
59 #include "document-private.h"
60 #include "sp-gradient.h"
61 #include "sp-gradient-reference.h"
62 #include "sp-linear-gradient-fns.h"
63 #include "sp-pattern.h"
64 #include "sp-radial-gradient-fns.h"
65 #include "sp-namedview.h"
66 #include "prefs-utils.h"
67 #include "sp-offset.h"
68 #include "sp-clippath.h"
69 #include "sp-mask.h"
70 #include "file.h"
71 #include "helper/png-write.h"
72 #include "layer-fns.h"
73 #include "context-fns.h"
74 #include <map>
75 #include <cstring>
76 #include <string>
77 #include "helper/units.h"
78 #include "sp-item.h"
79 #include "box3d.h"
80 #include "unit-constants.h"
81 #include "xml/simple-document.h"
82 #include "sp-filter-reference.h"
83 #include "gradient-drag.h"
84 #include "uri-references.h"
85 #include "libnr/nr-convert2geom.h"
87 // For clippath editing
88 #include "tools-switch.h"
89 #include "shape-editor.h"
90 #include "node-context.h"
91 #include "nodepath.h"
93 #include "ui/clipboard.h"
95 using NR::X;
96 using NR::Y;
98 /* The clipboard handling is in ui/clipboard.cpp now. There are some legacy functions left here,
99 because the layer manipulation code uses them. It should be rewritten specifically
100 for that purpose. */
102 /**
103 * Copies repr and its inherited css style elements, along with the accumulated transform 'full_t',
104 * then prepends the copy to 'clip'.
105 */
106 void sp_selection_copy_one (Inkscape::XML::Node *repr, NR::Matrix full_t, GSList **clip, Inkscape::XML::Document* xml_doc)
107 {
108 Inkscape::XML::Node *copy = repr->duplicate(xml_doc);
110 // copy complete inherited style
111 SPCSSAttr *css = sp_repr_css_attr_inherited(repr, "style");
112 sp_repr_css_set(copy, css, "style");
113 sp_repr_css_attr_unref(css);
115 // write the complete accumulated transform passed to us
116 // (we're dealing with unattached repr, so we write to its attr
117 // instead of using sp_item_set_transform)
118 gchar *affinestr=sp_svg_transform_write(full_t);
119 copy->setAttribute("transform", affinestr);
120 g_free(affinestr);
122 *clip = g_slist_prepend(*clip, copy);
123 }
125 void sp_selection_copy_impl (GSList const *items, GSList **clip, Inkscape::XML::Document* xml_doc)
126 {
127 // Sort items:
128 GSList *sorted_items = g_slist_copy ((GSList *) items);
129 sorted_items = g_slist_sort((GSList *) sorted_items, (GCompareFunc) sp_object_compare_position);
131 // Copy item reprs:
132 for (GSList *i = (GSList *) sorted_items; i != NULL; i = i->next) {
133 sp_selection_copy_one (SP_OBJECT_REPR (i->data), from_2geom(sp_item_i2doc_affine(SP_ITEM (i->data))), clip, xml_doc);
134 }
136 *clip = g_slist_reverse(*clip);
137 g_slist_free ((GSList *) sorted_items);
138 }
140 GSList *sp_selection_paste_impl (SPDocument *doc, SPObject *parent, GSList **clip)
141 {
142 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
144 GSList *copied = NULL;
145 // add objects to document
146 for (GSList *l = *clip; l != NULL; l = l->next) {
147 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
148 Inkscape::XML::Node *copy = repr->duplicate(xml_doc);
150 // premultiply the item transform by the accumulated parent transform in the paste layer
151 NR::Matrix local = from_2geom(sp_item_i2doc_affine(SP_ITEM(parent)));
152 if (!local.test_identity()) {
153 gchar const *t_str = copy->attribute("transform");
154 NR::Matrix item_t (NR::identity());
155 if (t_str)
156 sp_svg_transform_read(t_str, &item_t);
157 item_t *= local.inverse();
158 // (we're dealing with unattached repr, so we write to its attr instead of using sp_item_set_transform)
159 gchar *affinestr=sp_svg_transform_write(item_t);
160 copy->setAttribute("transform", affinestr);
161 g_free(affinestr);
162 }
164 parent->appendChildRepr(copy);
165 copied = g_slist_prepend(copied, copy);
166 Inkscape::GC::release(copy);
167 }
168 return copied;
169 }
171 void sp_selection_delete_impl(GSList const *items, bool propagate = true, bool propagate_descendants = true)
172 {
173 for (GSList const *i = items ; i ; i = i->next ) {
174 sp_object_ref((SPObject *)i->data, NULL);
175 }
176 for (GSList const *i = items; i != NULL; i = i->next) {
177 SPItem *item = (SPItem *) i->data;
178 SP_OBJECT(item)->deleteObject(propagate, propagate_descendants);
179 sp_object_unref((SPObject *)item, NULL);
180 }
181 }
184 void sp_selection_delete()
185 {
186 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
187 if (desktop == NULL) {
188 return;
189 }
191 if (tools_isactive (desktop, TOOLS_TEXT))
192 if (sp_text_delete_selection(desktop->event_context)) {
193 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_TEXT,
194 _("Delete text"));
195 return;
196 }
198 Inkscape::Selection *selection = sp_desktop_selection(desktop);
200 // check if something is selected
201 if (selection->isEmpty()) {
202 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Nothing</b> was deleted."));
203 return;
204 }
206 GSList const *selected = g_slist_copy(const_cast<GSList *>(selection->itemList()));
207 selection->clear();
208 sp_selection_delete_impl (selected);
209 g_slist_free ((GSList *) selected);
211 /* a tool may have set up private information in it's selection context
212 * that depends on desktop items. I think the only sane way to deal with
213 * this currently is to reset the current tool, which will reset it's
214 * associated selection context. For example: deleting an object
215 * while moving it around the canvas.
216 */
217 tools_switch ( desktop, tools_active ( desktop ) );
219 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_DELETE,
220 _("Delete"));
221 }
223 /* fixme: sequencing */
224 void sp_selection_duplicate(bool suppressDone)
225 {
226 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
227 if (desktop == NULL)
228 return;
230 Inkscape::XML::Document* xml_doc = sp_document_repr_doc(desktop->doc());
231 Inkscape::Selection *selection = sp_desktop_selection(desktop);
233 // check if something is selected
234 if (selection->isEmpty()) {
235 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to duplicate."));
236 return;
237 }
239 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
241 selection->clear();
243 // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
244 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
246 GSList *newsel = NULL;
248 while (reprs) {
249 Inkscape::XML::Node *parent = ((Inkscape::XML::Node *) reprs->data)->parent();
250 Inkscape::XML::Node *copy = ((Inkscape::XML::Node *) reprs->data)->duplicate(xml_doc);
252 parent->appendChild(copy);
254 newsel = g_slist_prepend(newsel, copy);
255 reprs = g_slist_remove(reprs, reprs->data);
256 Inkscape::GC::release(copy);
257 }
259 if ( !suppressDone ) {
260 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_DUPLICATE,
261 _("Duplicate"));
262 }
264 selection->setReprList(newsel);
266 g_slist_free(newsel);
267 }
269 void sp_edit_clear_all()
270 {
271 SPDesktop *dt = SP_ACTIVE_DESKTOP;
272 if (!dt)
273 return;
275 SPDocument *doc = sp_desktop_document(dt);
276 sp_desktop_selection(dt)->clear();
278 g_return_if_fail(SP_IS_GROUP(dt->currentLayer()));
279 GSList *items = sp_item_group_item_list(SP_GROUP(dt->currentLayer()));
281 while (items) {
282 SP_OBJECT (items->data)->deleteObject();
283 items = g_slist_remove(items, items->data);
284 }
286 sp_document_done(doc, SP_VERB_EDIT_CLEAR_ALL,
287 _("Delete all"));
288 }
290 GSList *
291 get_all_items (GSList *list, SPObject *from, SPDesktop *desktop, bool onlyvisible, bool onlysensitive, GSList const *exclude)
292 {
293 for (SPObject *child = sp_object_first_child(SP_OBJECT(from)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
294 if (SP_IS_ITEM(child) &&
295 !desktop->isLayer(SP_ITEM(child)) &&
296 (!onlysensitive || !SP_ITEM(child)->isLocked()) &&
297 (!onlyvisible || !desktop->itemIsHidden(SP_ITEM(child))) &&
298 (!exclude || !g_slist_find ((GSList *) exclude, child))
299 )
300 {
301 list = g_slist_prepend (list, SP_ITEM(child));
302 }
304 if (SP_IS_ITEM(child) && desktop->isLayer(SP_ITEM(child))) {
305 list = get_all_items (list, child, desktop, onlyvisible, onlysensitive, exclude);
306 }
307 }
309 return list;
310 }
312 void sp_edit_select_all_full (bool force_all_layers, bool invert)
313 {
314 SPDesktop *dt = SP_ACTIVE_DESKTOP;
315 if (!dt)
316 return;
318 Inkscape::Selection *selection = sp_desktop_selection(dt);
320 g_return_if_fail(SP_IS_GROUP(dt->currentLayer()));
322 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
323 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
324 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
326 GSList *items = NULL;
328 GSList const *exclude = NULL;
329 if (invert) {
330 exclude = selection->itemList();
331 }
333 if (force_all_layers)
334 inlayer = PREFS_SELECTION_ALL;
336 switch (inlayer) {
337 case PREFS_SELECTION_LAYER: {
338 if ( (onlysensitive && SP_ITEM(dt->currentLayer())->isLocked()) ||
339 (onlyvisible && dt->itemIsHidden(SP_ITEM(dt->currentLayer()))) )
340 return;
342 GSList *all_items = sp_item_group_item_list(SP_GROUP(dt->currentLayer()));
344 for (GSList *i = all_items; i; i = i->next) {
345 SPItem *item = SP_ITEM (i->data);
347 if (item && (!onlysensitive || !item->isLocked())) {
348 if (!onlyvisible || !dt->itemIsHidden(item)) {
349 if (!dt->isLayer(item)) {
350 if (!invert || !g_slist_find ((GSList *) exclude, item)) {
351 items = g_slist_prepend (items, item); // leave it in the list
352 }
353 }
354 }
355 }
356 }
358 g_slist_free (all_items);
359 break;
360 }
361 case PREFS_SELECTION_LAYER_RECURSIVE: {
362 items = get_all_items (NULL, dt->currentLayer(), dt, onlyvisible, onlysensitive, exclude);
363 break;
364 }
365 default: {
366 items = get_all_items (NULL, dt->currentRoot(), dt, onlyvisible, onlysensitive, exclude);
367 break;
368 }
369 }
371 selection->setList (items);
373 if (items) {
374 g_slist_free (items);
375 }
376 }
378 void sp_edit_select_all ()
379 {
380 sp_edit_select_all_full (false, false);
381 }
383 void sp_edit_select_all_in_all_layers ()
384 {
385 sp_edit_select_all_full (true, false);
386 }
388 void sp_edit_invert ()
389 {
390 sp_edit_select_all_full (false, true);
391 }
393 void sp_edit_invert_in_all_layers ()
394 {
395 sp_edit_select_all_full (true, true);
396 }
398 void sp_selection_group()
399 {
400 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
401 if (desktop == NULL)
402 return;
404 SPDocument *doc = sp_desktop_document (desktop);
405 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
407 Inkscape::Selection *selection = sp_desktop_selection(desktop);
409 // Check if something is selected.
410 if (selection->isEmpty()) {
411 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>some objects</b> to group."));
412 return;
413 }
415 GSList const *l = (GSList *) selection->reprList();
417 GSList *p = g_slist_copy((GSList *) l);
419 selection->clear();
421 p = g_slist_sort(p, (GCompareFunc) sp_repr_compare_position);
423 // Remember the position and parent of the topmost object.
424 gint topmost = ((Inkscape::XML::Node *) g_slist_last(p)->data)->position();
425 Inkscape::XML::Node *topmost_parent = ((Inkscape::XML::Node *) g_slist_last(p)->data)->parent();
427 Inkscape::XML::Node *group = xml_doc->createElement("svg:g");
429 while (p) {
430 Inkscape::XML::Node *current = (Inkscape::XML::Node *) p->data;
432 if (current->parent() == topmost_parent) {
433 Inkscape::XML::Node *spnew = current->duplicate(xml_doc);
434 sp_repr_unparent(current);
435 group->appendChild(spnew);
436 Inkscape::GC::release(spnew);
437 topmost --; // only reduce count for those items deleted from topmost_parent
438 } else { // move it to topmost_parent first
439 GSList *temp_clip = NULL;
441 // At this point, current may already have no item, due to its being a clone whose original is already moved away
442 // So we copy it artificially calculating the transform from its repr->attr("transform") and the parent transform
443 gchar const *t_str = current->attribute("transform");
444 NR::Matrix item_t (NR::identity());
445 if (t_str)
446 sp_svg_transform_read(t_str, &item_t);
447 item_t *= from_2geom(sp_item_i2doc_affine(SP_ITEM(doc->getObjectByRepr(current->parent()))));
448 //FIXME: when moving both clone and original from a transformed group (either by
449 //grouping into another parent, or by cut/paste) the transform from the original's
450 //parent becomes embedded into original itself, and this affects its clones. Fix
451 //this by remembering the transform diffs we write to each item into an array and
452 //then, if this is clone, looking up its original in that array and pre-multiplying
453 //it by the inverse of that original's transform diff.
455 sp_selection_copy_one (current, item_t, &temp_clip, xml_doc);
456 sp_repr_unparent(current);
458 // paste into topmost_parent (temporarily)
459 GSList *copied = sp_selection_paste_impl (doc, doc->getObjectByRepr(topmost_parent), &temp_clip);
460 if (temp_clip) g_slist_free (temp_clip);
461 if (copied) { // if success,
462 // take pasted object (now in topmost_parent)
463 Inkscape::XML::Node *in_topmost = (Inkscape::XML::Node *) copied->data;
464 // make a copy
465 Inkscape::XML::Node *spnew = in_topmost->duplicate(xml_doc);
466 // remove pasted
467 sp_repr_unparent(in_topmost);
468 // put its copy into group
469 group->appendChild(spnew);
470 Inkscape::GC::release(spnew);
471 g_slist_free (copied);
472 }
473 }
474 p = g_slist_remove(p, current);
475 }
477 // Add the new group to the topmost members' parent
478 topmost_parent->appendChild(group);
480 // Move to the position of the topmost, reduced by the number of items deleted from topmost_parent
481 group->setPosition(topmost + 1);
483 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_GROUP,
484 _("Group"));
486 selection->set(group);
487 Inkscape::GC::release(group);
488 }
490 void sp_selection_ungroup()
491 {
492 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
493 if (desktop == NULL)
494 return;
496 Inkscape::Selection *selection = sp_desktop_selection(desktop);
498 if (selection->isEmpty()) {
499 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>group</b> to ungroup."));
500 return;
501 }
503 GSList *items = g_slist_copy((GSList *) selection->itemList());
504 selection->clear();
506 // Get a copy of current selection.
507 GSList *new_select = NULL;
508 bool ungrouped = false;
509 for (GSList *i = items;
510 i != NULL;
511 i = i->next)
512 {
513 SPItem *group = (SPItem *) i->data;
515 // when ungrouping cloned groups with their originals, some objects that were selected may no more exist due to unlinking
516 if (!SP_IS_OBJECT(group)) {
517 continue;
518 }
520 /* We do not allow ungrouping <svg> etc. (lauris) */
521 if (strcmp(SP_OBJECT_REPR(group)->name(), "svg:g") && strcmp(SP_OBJECT_REPR(group)->name(), "svg:switch")) {
522 // keep the non-group item in the new selection
523 selection->add(group);
524 continue;
525 }
527 GSList *children = NULL;
528 /* This is not strictly required, but is nicer to rely on group ::destroy (lauris) */
529 sp_item_group_ungroup(SP_GROUP(group), &children, false);
530 ungrouped = true;
531 // Add ungrouped items to the new selection.
532 new_select = g_slist_concat(new_select, children);
533 }
535 if (new_select) { // Set new selection.
536 selection->addList(new_select);
537 g_slist_free(new_select);
538 }
539 if (!ungrouped) {
540 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No groups</b> to ungroup in the selection."));
541 }
543 g_slist_free(items);
545 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_UNGROUP,
546 _("Ungroup"));
547 }
549 static SPGroup *
550 sp_item_list_common_parent_group(GSList const *items)
551 {
552 if (!items) {
553 return NULL;
554 }
555 SPObject *parent = SP_OBJECT_PARENT(items->data);
556 /* Strictly speaking this CAN happen, if user selects <svg> from Inkscape::XML editor */
557 if (!SP_IS_GROUP(parent)) {
558 return NULL;
559 }
560 for (items = items->next; items; items = items->next) {
561 if (SP_OBJECT_PARENT(items->data) != parent) {
562 return NULL;
563 }
564 }
566 return SP_GROUP(parent);
567 }
569 /** Finds out the minimum common bbox of the selected items. */
570 static NR::Maybe<NR::Rect>
571 enclose_items(GSList const *items)
572 {
573 g_assert(items != NULL);
575 NR::Maybe<NR::Rect> r = NR::Nothing();
576 for (GSList const *i = items; i; i = i->next) {
577 r = NR::union_bounds(r, sp_item_bbox_desktop((SPItem *) i->data));
578 }
579 return r;
580 }
582 SPObject *
583 prev_sibling(SPObject *child)
584 {
585 SPObject *parent = SP_OBJECT_PARENT(child);
586 if (!SP_IS_GROUP(parent)) {
587 return NULL;
588 }
589 for ( SPObject *i = sp_object_first_child(parent) ; i; i = SP_OBJECT_NEXT(i) ) {
590 if (i->next == child)
591 return i;
592 }
593 return NULL;
594 }
596 void
597 sp_selection_raise()
598 {
599 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
600 if (!desktop)
601 return;
603 Inkscape::Selection *selection = sp_desktop_selection(desktop);
605 GSList const *items = (GSList *) selection->itemList();
606 if (!items) {
607 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise."));
608 return;
609 }
611 SPGroup const *group = sp_item_list_common_parent_group(items);
612 if (!group) {
613 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
614 return;
615 }
617 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
619 /* Construct reverse-ordered list of selected children. */
620 GSList *rev = g_slist_copy((GSList *) items);
621 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
623 // Determine the common bbox of the selected items.
624 NR::Maybe<NR::Rect> selected = enclose_items(items);
626 // Iterate over all objects in the selection (starting from top).
627 if (selected) {
628 while (rev) {
629 SPObject *child = SP_OBJECT(rev->data);
630 // for each selected object, find the next sibling
631 for (SPObject *newref = child->next; newref; newref = newref->next) {
632 // if the sibling is an item AND overlaps our selection,
633 if (SP_IS_ITEM(newref)) {
634 NR::Maybe<NR::Rect> newref_bbox = sp_item_bbox_desktop(SP_ITEM(newref));
635 if ( newref_bbox && selected->intersects(*newref_bbox) ) {
636 // AND if it's not one of our selected objects,
637 if (!g_slist_find((GSList *) items, newref)) {
638 // move the selected object after that sibling
639 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(newref));
640 }
641 break;
642 }
643 }
644 }
645 rev = g_slist_remove(rev, child);
646 }
647 } else {
648 g_slist_free(rev);
649 }
651 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_RAISE,
652 //TRANSLATORS: Only put the word "Raise" in the translation. Means "to raise an object" in the undo history
653 Q_("undo_action|Raise"));
654 }
656 void sp_selection_raise_to_top()
657 {
658 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
659 if (desktop == NULL)
660 return;
662 SPDocument *document = sp_desktop_document(desktop);
663 Inkscape::Selection *selection = sp_desktop_selection(desktop);
665 if (selection->isEmpty()) {
666 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to raise to top."));
667 return;
668 }
670 GSList const *items = (GSList *) selection->itemList();
672 SPGroup const *group = sp_item_list_common_parent_group(items);
673 if (!group) {
674 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
675 return;
676 }
678 GSList *rl = g_slist_copy((GSList *) selection->reprList());
679 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
681 for (GSList *l = rl; l != NULL; l = l->next) {
682 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
683 repr->setPosition(-1);
684 }
686 g_slist_free(rl);
688 sp_document_done(document, SP_VERB_SELECTION_TO_FRONT,
689 _("Raise to top"));
690 }
692 void
693 sp_selection_lower()
694 {
695 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
696 if (desktop == NULL)
697 return;
699 Inkscape::Selection *selection = sp_desktop_selection(desktop);
701 GSList const *items = (GSList *) selection->itemList();
702 if (!items) {
703 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower."));
704 return;
705 }
707 SPGroup const *group = sp_item_list_common_parent_group(items);
708 if (!group) {
709 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
710 return;
711 }
713 Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
715 // Determine the common bbox of the selected items.
716 NR::Maybe<NR::Rect> selected = enclose_items(items);
718 /* Construct direct-ordered list of selected children. */
719 GSList *rev = g_slist_copy((GSList *) items);
720 rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
721 rev = g_slist_reverse(rev);
723 // Iterate over all objects in the selection (starting from top).
724 if (selected) {
725 while (rev) {
726 SPObject *child = SP_OBJECT(rev->data);
727 // for each selected object, find the prev sibling
728 for (SPObject *newref = prev_sibling(child); newref; newref = prev_sibling(newref)) {
729 // if the sibling is an item AND overlaps our selection,
730 if (SP_IS_ITEM(newref)) {
731 NR::Maybe<NR::Rect> ref_bbox = sp_item_bbox_desktop(SP_ITEM(newref));
732 if ( ref_bbox && selected->intersects(*ref_bbox) ) {
733 // AND if it's not one of our selected objects,
734 if (!g_slist_find((GSList *) items, newref)) {
735 // move the selected object before that sibling
736 SPObject *put_after = prev_sibling(newref);
737 if (put_after)
738 grepr->changeOrder(SP_OBJECT_REPR(child), SP_OBJECT_REPR(put_after));
739 else
740 SP_OBJECT_REPR(child)->setPosition(0);
741 }
742 break;
743 }
744 }
745 }
746 rev = g_slist_remove(rev, child);
747 }
748 } else {
749 g_slist_free(rev);
750 }
752 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_LOWER,
753 _("Lower"));
754 }
756 void sp_selection_lower_to_bottom()
757 {
758 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
759 if (desktop == NULL)
760 return;
762 SPDocument *document = sp_desktop_document(desktop);
763 Inkscape::Selection *selection = sp_desktop_selection(desktop);
765 if (selection->isEmpty()) {
766 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to lower to bottom."));
767 return;
768 }
770 GSList const *items = (GSList *) selection->itemList();
772 SPGroup const *group = sp_item_list_common_parent_group(items);
773 if (!group) {
774 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("You cannot raise/lower objects from <b>different groups</b> or <b>layers</b>."));
775 return;
776 }
778 GSList *rl;
779 rl = g_slist_copy((GSList *) selection->reprList());
780 rl = g_slist_sort(rl, (GCompareFunc) sp_repr_compare_position);
781 rl = g_slist_reverse(rl);
783 for (GSList *l = rl; l != NULL; l = l->next) {
784 gint minpos;
785 SPObject *pp, *pc;
786 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) l->data;
787 pp = document->getObjectByRepr(sp_repr_parent(repr));
788 minpos = 0;
789 g_assert(SP_IS_GROUP(pp));
790 pc = sp_object_first_child(pp);
791 while (!SP_IS_ITEM(pc)) {
792 minpos += 1;
793 pc = pc->next;
794 }
795 repr->setPosition(minpos);
796 }
798 g_slist_free(rl);
800 sp_document_done(document, SP_VERB_SELECTION_TO_BACK,
801 _("Lower to bottom"));
802 }
804 void
805 sp_undo(SPDesktop *desktop, SPDocument *)
806 {
807 if (!sp_document_undo(sp_desktop_document(desktop)))
808 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to undo."));
809 }
811 void
812 sp_redo(SPDesktop *desktop, SPDocument *)
813 {
814 if (!sp_document_redo(sp_desktop_document(desktop)))
815 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing to redo."));
816 }
818 void sp_selection_cut()
819 {
820 sp_selection_copy();
821 sp_selection_delete();
822 }
824 /**
825 * \pre item != NULL
826 */
827 SPCSSAttr *
828 take_style_from_item (SPItem *item)
829 {
830 // write the complete cascaded style, context-free
831 SPCSSAttr *css = sp_css_attr_from_object (SP_OBJECT(item), SP_STYLE_FLAG_ALWAYS);
832 if (css == NULL)
833 return NULL;
835 if ((SP_IS_GROUP(item) && SP_OBJECT(item)->children) ||
836 (SP_IS_TEXT (item) && SP_OBJECT(item)->children && SP_OBJECT(item)->children->next == NULL)) {
837 // if this is a text with exactly one tspan child, merge the style of that tspan as well
838 // If this is a group, merge the style of its topmost (last) child with style
839 for (SPObject *last_element = item->lastChild(); last_element != NULL; last_element = SP_OBJECT_PREV (last_element)) {
840 if (SP_OBJECT_STYLE (last_element) != NULL) {
841 SPCSSAttr *temp = sp_css_attr_from_object (last_element, SP_STYLE_FLAG_IFSET);
842 if (temp) {
843 sp_repr_css_merge (css, temp);
844 sp_repr_css_attr_unref (temp);
845 }
846 break;
847 }
848 }
849 }
850 if (!(SP_IS_TEXT (item) || SP_IS_TSPAN (item) || SP_IS_TREF(item) || SP_IS_STRING (item))) {
851 // do not copy text properties from non-text objects, it's confusing
852 css = sp_css_attr_unset_text (css);
853 }
855 // FIXME: also transform gradient/pattern fills, by forking? NO, this must be nondestructive
856 double ex = NR::expansion(from_2geom(sp_item_i2doc_affine(item)));
857 if (ex != 1.0) {
858 css = sp_css_attr_scale (css, ex);
859 }
861 return css;
862 }
865 void sp_selection_copy()
866 {
867 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
868 cm->copy();
869 }
871 void sp_selection_paste(bool in_place)
872 {
873 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
874 if(cm->paste(in_place))
875 sp_document_done(SP_ACTIVE_DOCUMENT, SP_VERB_EDIT_PASTE, _("Paste"));
876 }
878 void sp_selection_paste_style()
879 {
880 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
881 if(cm->pasteStyle())
882 sp_document_done(SP_ACTIVE_DOCUMENT, SP_VERB_EDIT_PASTE_STYLE, _("Paste style"));
883 }
886 void sp_selection_paste_livepatheffect()
887 {
888 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
889 if(cm->pastePathEffect())
890 sp_document_done(SP_ACTIVE_DOCUMENT, SP_VERB_EDIT_PASTE_LIVEPATHEFFECT,
891 _("Paste live path effect"));
892 }
895 void sp_selection_remove_livepatheffect_impl(SPItem *item)
896 {
897 if ( item && SP_IS_LPE_ITEM(item) ) {
898 sp_lpe_item_remove_all_path_effects(SP_LPE_ITEM(item), false);
899 }
900 }
902 void sp_selection_remove_livepatheffect()
903 {
904 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
905 if (desktop == NULL) return;
907 Inkscape::Selection *selection = sp_desktop_selection(desktop);
909 // check if something is selected
910 if (selection->isEmpty()) {
911 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to remove live path effects from."));
912 return;
913 }
915 for ( GSList const *itemlist = selection->itemList(); itemlist != NULL; itemlist = g_slist_next(itemlist) ) {
916 SPItem *item = reinterpret_cast<SPItem*>(itemlist->data);
918 sp_selection_remove_livepatheffect_impl(item);
920 }
922 sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_REMOVE_LIVEPATHEFFECT,
923 _("Remove live path effect"));
924 }
926 void sp_selection_remove_filter ()
927 {
928 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
929 if (desktop == NULL) return;
931 Inkscape::Selection *selection = sp_desktop_selection(desktop);
933 // check if something is selected
934 if (selection->isEmpty()) {
935 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to remove filters from."));
936 return;
937 }
939 SPCSSAttr *css = sp_repr_css_attr_new();
940 sp_repr_css_unset_property(css, "filter");
941 sp_desktop_set_style(desktop, css);
942 sp_repr_css_attr_unref(css);
944 sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_REMOVE_FILTER,
945 _("Remove filter"));
946 }
949 void sp_selection_paste_size (bool apply_x, bool apply_y)
950 {
951 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
952 if(cm->pasteSize(false, apply_x, apply_y))
953 sp_document_done(sp_desktop_document(SP_ACTIVE_DESKTOP), SP_VERB_EDIT_PASTE_SIZE,
954 _("Paste size"));
955 }
957 void sp_selection_paste_size_separately (bool apply_x, bool apply_y)
958 {
959 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
960 if(cm->pasteSize(true, apply_x, apply_y))
961 sp_document_done(sp_desktop_document(SP_ACTIVE_DESKTOP), SP_VERB_EDIT_PASTE_SIZE_SEPARATELY,
962 _("Paste size separately"));
963 }
965 void sp_selection_to_next_layer(bool suppressDone)
966 {
967 SPDesktop *dt = SP_ACTIVE_DESKTOP;
969 Inkscape::Selection *selection = sp_desktop_selection(dt);
971 // check if something is selected
972 if (selection->isEmpty()) {
973 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer above."));
974 return;
975 }
977 GSList const *items = g_slist_copy ((GSList *) selection->itemList());
979 bool no_more = false; // Set to true, if no more layers above
980 SPObject *next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer());
981 if (next) {
982 GSList *temp_clip = NULL;
983 sp_selection_copy_impl (items, &temp_clip, sp_document_repr_doc(dt->doc()));
984 sp_selection_delete_impl (items, false, false);
985 next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
986 GSList *copied;
987 if(next) {
988 copied = sp_selection_paste_impl (sp_desktop_document (dt), next, &temp_clip);
989 } else {
990 copied = sp_selection_paste_impl (sp_desktop_document (dt), dt->currentLayer(), &temp_clip);
991 no_more = true;
992 }
993 selection->setReprList((GSList const *) copied);
994 g_slist_free (copied);
995 if (temp_clip) g_slist_free (temp_clip);
996 if (next) dt->setCurrentLayer(next);
997 if ( !suppressDone ) {
998 sp_document_done(sp_desktop_document (dt), SP_VERB_LAYER_MOVE_TO_NEXT,
999 _("Raise to next layer"));
1000 }
1001 } else {
1002 no_more = true;
1003 }
1005 if (no_more) {
1006 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers above."));
1007 }
1009 g_slist_free ((GSList *) items);
1010 }
1012 void sp_selection_to_prev_layer(bool suppressDone)
1013 {
1014 SPDesktop *dt = SP_ACTIVE_DESKTOP;
1016 Inkscape::Selection *selection = sp_desktop_selection(dt);
1018 // check if something is selected
1019 if (selection->isEmpty()) {
1020 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to move to the layer below."));
1021 return;
1022 }
1024 GSList const *items = g_slist_copy ((GSList *) selection->itemList());
1026 bool no_more = false; // Set to true, if no more layers below
1027 SPObject *next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer());
1028 if (next) {
1029 GSList *temp_clip = NULL;
1030 sp_selection_copy_impl (items, &temp_clip, sp_document_repr_doc(dt->doc())); // we're in the same doc, so no need to copy defs
1031 sp_selection_delete_impl (items, false, false);
1032 next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers
1033 GSList *copied;
1034 if(next) {
1035 copied = sp_selection_paste_impl (sp_desktop_document (dt), next, &temp_clip);
1036 } else {
1037 copied = sp_selection_paste_impl (sp_desktop_document (dt), dt->currentLayer(), &temp_clip);
1038 no_more = true;
1039 }
1040 selection->setReprList((GSList const *) copied);
1041 g_slist_free (copied);
1042 if (temp_clip) g_slist_free (temp_clip);
1043 if (next) dt->setCurrentLayer(next);
1044 if ( !suppressDone ) {
1045 sp_document_done(sp_desktop_document (dt), SP_VERB_LAYER_MOVE_TO_PREV,
1046 _("Lower to previous layer"));
1047 }
1048 } else {
1049 no_more = true;
1050 }
1052 if (no_more) {
1053 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No more layers below."));
1054 }
1056 g_slist_free ((GSList *) items);
1057 }
1059 bool
1060 selection_contains_original (SPItem *item, Inkscape::Selection *selection)
1061 {
1062 bool contains_original = false;
1064 bool is_use = SP_IS_USE(item);
1065 SPItem *item_use = item;
1066 SPItem *item_use_first = item;
1067 while (is_use && item_use && !contains_original)
1068 {
1069 item_use = sp_use_get_original (SP_USE(item_use));
1070 contains_original |= selection->includes(item_use);
1071 if (item_use == item_use_first)
1072 break;
1073 is_use = SP_IS_USE(item_use);
1074 }
1076 // If it's a tref, check whether the object containing the character
1077 // data is part of the selection
1078 if (!contains_original && SP_IS_TREF(item)) {
1079 contains_original = selection->includes(SP_TREF(item)->getObjectReferredTo());
1080 }
1082 return contains_original;
1083 }
1086 bool
1087 selection_contains_both_clone_and_original (Inkscape::Selection *selection)
1088 {
1089 bool clone_with_original = false;
1090 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1091 SPItem *item = SP_ITEM(l->data);
1092 clone_with_original |= selection_contains_original(item, selection);
1093 if (clone_with_original)
1094 break;
1095 }
1096 return clone_with_original;
1097 }
1100 /** Apply matrix to the selection. \a set_i2d is normally true, which means objects are in the
1101 original transform, synced with their reprs, and need to jump to the new transform in one go. A
1102 value of set_i2d==false is only used by seltrans when it's dragging objects live (not outlines); in
1103 that case, items are already in the new position, but the repr is in the old, and this function
1104 then simply updates the repr from item->transform.
1105 */
1106 void sp_selection_apply_affine(Inkscape::Selection *selection, NR::Matrix const &affine, bool set_i2d)
1107 {
1108 if (selection->isEmpty())
1109 return;
1111 for (GSList const *l = selection->itemList(); l != NULL; l = l->next) {
1112 SPItem *item = SP_ITEM(l->data);
1114 NR::Point old_center(0,0);
1115 if (set_i2d && item->isCenterSet())
1116 old_center = item->getCenter();
1118 #if 0 /* Re-enable this once persistent guides have a graphical indication.
1119 At the time of writing, this is the only place to re-enable. */
1120 sp_item_update_cns(*item, selection->desktop());
1121 #endif
1123 // we're moving both a clone and its original or any ancestor in clone chain?
1124 bool transform_clone_with_original = selection_contains_original(item, selection);
1125 // ...both a text-on-path and its path?
1126 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)))) ));
1127 // ...both a flowtext and its frame?
1128 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)
1129 // ...both an offset and its source?
1130 bool transform_offset_with_source = (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) && selection->includes( sp_offset_get_source (SP_OFFSET(item)) );
1132 // If we're moving a connector, we want to detach it
1133 // from shapes that aren't part of the selection, but
1134 // leave it attached if they are
1135 if (cc_item_is_connector(item)) {
1136 SPItem *attItem[2];
1137 SP_PATH(item)->connEndPair.getAttachedItems(attItem);
1139 for (int n = 0; n < 2; ++n) {
1140 if (!selection->includes(attItem[n])) {
1141 sp_conn_end_detach(item, n);
1142 }
1143 }
1144 }
1146 // "clones are unmoved when original is moved" preference
1147 int compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
1148 bool prefs_unmoved = (compensation == SP_CLONE_COMPENSATION_UNMOVED);
1149 bool prefs_parallel = (compensation == SP_CLONE_COMPENSATION_PARALLEL);
1151 // If this is a clone and it's selected along with its original, do not move it; it will feel the
1152 // transform of its original and respond to it itself. Without this, a clone is doubly
1153 // transformed, very unintuitive.
1154 // Same for textpath if we are also doing ANY transform to its path: do not touch textpath,
1155 // letters cannot be squeezed or rotated anyway, they only refill the changed path.
1156 // Same for linked offset if we are also moving its source: do not move it.
1157 if (transform_textpath_with_path || transform_offset_with_source) {
1158 // restore item->transform field from the repr, in case it was changed by seltrans
1159 sp_object_read_attr (SP_OBJECT (item), "transform");
1161 } else if (transform_flowtext_with_frame) {
1162 // apply the inverse of the region's transform to the <use> so that the flow remains
1163 // the same (even though the output itself gets transformed)
1164 for (SPObject *region = item->firstChild() ; region ; region = SP_OBJECT_NEXT(region)) {
1165 if (!SP_IS_FLOWREGION(region) && !SP_IS_FLOWREGIONEXCLUDE(region))
1166 continue;
1167 for (SPObject *use = region->firstChild() ; use ; use = SP_OBJECT_NEXT(use)) {
1168 if (!SP_IS_USE(use)) continue;
1169 sp_item_write_transform(SP_USE(use), SP_OBJECT_REPR(use), item->transform.inverse(), NULL);
1170 }
1171 }
1172 } else if (transform_clone_with_original) {
1173 // We are transforming a clone along with its original. The below matrix juggling is
1174 // necessary to ensure that they transform as a whole, i.e. the clone's induced
1175 // transform and its move compensation are both cancelled out.
1177 // restore item->transform field from the repr, in case it was changed by seltrans
1178 sp_object_read_attr (SP_OBJECT (item), "transform");
1180 // calculate the matrix we need to apply to the clone to cancel its induced transform from its original
1181 NR::Matrix parent_transform = from_2geom(sp_item_i2root_affine(SP_ITEM(SP_OBJECT_PARENT (item))));
1182 NR::Matrix t = parent_transform * from_2geom(matrix_to_desktop (matrix_from_desktop (to_2geom(affine), item), item)) * parent_transform.inverse();
1183 NR::Matrix t_inv =parent_transform * from_2geom(matrix_to_desktop (matrix_from_desktop (to_2geom(affine.inverse()), item), item)) * parent_transform.inverse();
1184 NR::Matrix result = t_inv * item->transform * t;
1186 if ((prefs_parallel || prefs_unmoved) && affine.is_translation()) {
1187 // we need to cancel out the move compensation, too
1189 // find out the clone move, same as in sp_use_move_compensate
1190 NR::Matrix parent = sp_use_get_parent_transform (SP_USE(item));
1191 NR::Matrix clone_move = parent.inverse() * t * parent;
1193 if (prefs_parallel) {
1194 NR::Matrix move = result * clone_move * t_inv;
1195 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &move);
1197 } else if (prefs_unmoved) {
1198 //if (SP_IS_USE(sp_use_get_original(SP_USE(item))))
1199 // clone_move = NR::identity();
1200 NR::Matrix move = result * clone_move;
1201 sp_item_write_transform(item, SP_OBJECT_REPR(item), move, &t);
1202 }
1204 } else {
1205 // just apply the result
1206 sp_item_write_transform(item, SP_OBJECT_REPR(item), result, &t);
1207 }
1209 } else {
1210 if (set_i2d) {
1211 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * to_2geom(affine));
1212 }
1213 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, NULL);
1214 }
1216 // if we're moving the actual object, not just updating the repr, we can transform the
1217 // center by the same matrix (only necessary for non-translations)
1218 if (set_i2d && item->isCenterSet() && !affine.is_translation()) {
1219 item->setCenter(old_center * affine);
1220 SP_OBJECT(item)->updateRepr();
1221 }
1222 }
1223 }
1225 void sp_selection_remove_transform()
1226 {
1227 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1228 if (desktop == NULL)
1229 return;
1231 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1233 GSList const *l = (GSList *) selection->reprList();
1234 while (l != NULL) {
1235 ((Inkscape::XML::Node*)l->data)->setAttribute("transform", NULL, false);
1236 l = l->next;
1237 }
1239 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_FLATTEN,
1240 _("Remove transform"));
1241 }
1243 void
1244 sp_selection_scale_absolute(Inkscape::Selection *selection,
1245 double const x0, double const x1,
1246 double const y0, double const y1)
1247 {
1248 if (selection->isEmpty())
1249 return;
1251 NR::Maybe<NR::Rect> const bbox(selection->bounds());
1252 if ( !bbox || bbox->isEmpty() ) {
1253 return;
1254 }
1256 NR::translate const p2o(-bbox->min());
1258 NR::scale const newSize(x1 - x0,
1259 y1 - y0);
1260 NR::scale const scale( newSize / NR::scale(bbox->dimensions()) );
1261 NR::translate const o2n(x0, y0);
1262 NR::Matrix const final( p2o * scale * o2n );
1264 sp_selection_apply_affine(selection, final);
1265 }
1268 void sp_selection_scale_relative(Inkscape::Selection *selection, NR::Point const &align, NR::scale const &scale)
1269 {
1270 if (selection->isEmpty())
1271 return;
1273 NR::Maybe<NR::Rect> const bbox(selection->bounds());
1275 if ( !bbox || bbox->isEmpty() ) {
1276 return;
1277 }
1279 // FIXME: ARBITRARY LIMIT: don't try to scale above 1 Mpx, it won't display properly and will crash sooner or later anyway
1280 if ( bbox->extent(NR::X) * scale[NR::X] > 1e6 ||
1281 bbox->extent(NR::Y) * scale[NR::Y] > 1e6 )
1282 {
1283 return;
1284 }
1286 NR::translate const n2d(-align);
1287 NR::translate const d2n(align);
1288 NR::Matrix const final( n2d * scale * d2n );
1289 sp_selection_apply_affine(selection, final);
1290 }
1292 void
1293 sp_selection_rotate_relative(Inkscape::Selection *selection, NR::Point const ¢er, gdouble const angle_degrees)
1294 {
1295 NR::translate const d2n(center);
1296 NR::translate const n2d(-center);
1297 NR::rotate const rotate(rotate_degrees(angle_degrees));
1298 NR::Matrix const final( NR::Matrix(n2d) * rotate * d2n );
1299 sp_selection_apply_affine(selection, final);
1300 }
1302 void
1303 sp_selection_skew_relative(Inkscape::Selection *selection, NR::Point const &align, double dx, double dy)
1304 {
1305 NR::translate const d2n(align);
1306 NR::translate const n2d(-align);
1307 NR::Matrix const skew(1, dy,
1308 dx, 1,
1309 0, 0);
1310 NR::Matrix const final( n2d * skew * d2n );
1311 sp_selection_apply_affine(selection, final);
1312 }
1314 void sp_selection_move_relative(Inkscape::Selection *selection, NR::Point const &move)
1315 {
1316 sp_selection_apply_affine(selection, NR::Matrix(NR::translate(move)));
1317 }
1319 void sp_selection_move_relative(Inkscape::Selection *selection, double dx, double dy)
1320 {
1321 sp_selection_apply_affine(selection, NR::Matrix(NR::translate(dx, dy)));
1322 }
1325 /**
1326 * \brief sp_selection_rotate_90
1327 *
1328 * This function rotates selected objects 90 degrees clockwise.
1329 *
1330 */
1332 void sp_selection_rotate_90_cw()
1333 {
1334 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1336 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1338 if (selection->isEmpty())
1339 return;
1341 GSList const *l = selection->itemList();
1342 NR::rotate const rot_neg_90(NR::Point(0, -1));
1343 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1344 SPItem *item = SP_ITEM(l2->data);
1345 sp_item_rotate_rel(item, rot_neg_90);
1346 }
1348 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_ROTATE_90_CCW,
1349 _("Rotate 90° CW"));
1350 }
1353 /**
1354 * @brief Rotates selected objects 90 degrees counter-clockwise.
1355 */
1356 void sp_selection_rotate_90_ccw()
1357 {
1358 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1360 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1362 if (selection->isEmpty())
1363 return;
1365 GSList const *l = selection->itemList();
1366 NR::rotate const rot_neg_90(NR::Point(0, 1));
1367 for (GSList const *l2 = l ; l2 != NULL ; l2 = l2->next) {
1368 SPItem *item = SP_ITEM(l2->data);
1369 sp_item_rotate_rel(item, rot_neg_90);
1370 }
1372 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_ROTATE_90_CW,
1373 _("Rotate 90° CCW"));
1374 }
1376 void
1377 sp_selection_rotate(Inkscape::Selection *selection, gdouble const angle_degrees)
1378 {
1379 if (selection->isEmpty())
1380 return;
1382 NR::Maybe<NR::Point> center = selection->center();
1383 if (!center) {
1384 return;
1385 }
1387 sp_selection_rotate_relative(selection, *center, angle_degrees);
1389 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1390 ( ( angle_degrees > 0 )
1391 ? "selector:rotate:ccw"
1392 : "selector:rotate:cw" ),
1393 SP_VERB_CONTEXT_SELECT,
1394 _("Rotate"));
1395 }
1397 /**
1398 \param angle the angle in "angular pixels", i.e. how many visible pixels must move the outermost point of the rotated object
1399 */
1400 void
1401 sp_selection_rotate_screen(Inkscape::Selection *selection, gdouble angle)
1402 {
1403 if (selection->isEmpty())
1404 return;
1406 NR::Maybe<NR::Rect> const bbox(selection->bounds());
1407 NR::Maybe<NR::Point> center = selection->center();
1409 if ( !bbox || !center ) {
1410 return;
1411 }
1413 gdouble const zoom = selection->desktop()->current_zoom();
1414 gdouble const zmove = angle / zoom;
1415 gdouble const r = NR::L2(bbox->cornerFarthestFrom(*center) - *center);
1417 gdouble const zangle = 180 * atan2(zmove, r) / M_PI;
1419 sp_selection_rotate_relative(selection, *center, zangle);
1421 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1422 ( (angle > 0)
1423 ? "selector:rotate:ccw"
1424 : "selector:rotate:cw" ),
1425 SP_VERB_CONTEXT_SELECT,
1426 _("Rotate by pixels"));
1427 }
1429 void
1430 sp_selection_scale(Inkscape::Selection *selection, gdouble grow)
1431 {
1432 if (selection->isEmpty())
1433 return;
1435 NR::Maybe<NR::Rect> const bbox(selection->bounds());
1436 if (!bbox) {
1437 return;
1438 }
1440 NR::Point const center(bbox->midpoint());
1442 // you can't scale "do nizhe pola" (below zero)
1443 double const max_len = bbox->maxExtent();
1444 if ( max_len + grow <= 1e-3 ) {
1445 return;
1446 }
1448 double const times = 1.0 + grow / max_len;
1449 sp_selection_scale_relative(selection, center, NR::scale(times, times));
1451 sp_document_maybe_done(sp_desktop_document(selection->desktop()),
1452 ( (grow > 0)
1453 ? "selector:scale:larger"
1454 : "selector:scale:smaller" ),
1455 SP_VERB_CONTEXT_SELECT,
1456 _("Scale"));
1457 }
1459 void
1460 sp_selection_scale_screen(Inkscape::Selection *selection, gdouble grow_pixels)
1461 {
1462 sp_selection_scale(selection,
1463 grow_pixels / selection->desktop()->current_zoom());
1464 }
1466 void
1467 sp_selection_scale_times(Inkscape::Selection *selection, gdouble times)
1468 {
1469 if (selection->isEmpty())
1470 return;
1472 NR::Maybe<NR::Rect> sel_bbox = selection->bounds();
1474 if (!sel_bbox) {
1475 return;
1476 }
1478 NR::Point const center(sel_bbox->midpoint());
1479 sp_selection_scale_relative(selection, center, NR::scale(times, times));
1480 sp_document_done(sp_desktop_document(selection->desktop()), SP_VERB_CONTEXT_SELECT,
1481 _("Scale by whole factor"));
1482 }
1484 void
1485 sp_selection_move(gdouble dx, gdouble dy)
1486 {
1487 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1488 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1489 if (selection->isEmpty()) {
1490 return;
1491 }
1493 sp_selection_move_relative(selection, dx, dy);
1495 if (dx == 0) {
1496 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical", SP_VERB_CONTEXT_SELECT,
1497 _("Move vertically"));
1498 } else if (dy == 0) {
1499 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal", SP_VERB_CONTEXT_SELECT,
1500 _("Move horizontally"));
1501 } else {
1502 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SELECT,
1503 _("Move"));
1504 }
1505 }
1507 void
1508 sp_selection_move_screen(gdouble dx, gdouble dy)
1509 {
1510 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1512 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1513 if (selection->isEmpty()) {
1514 return;
1515 }
1517 // same as sp_selection_move but divide deltas by zoom factor
1518 gdouble const zoom = desktop->current_zoom();
1519 gdouble const zdx = dx / zoom;
1520 gdouble const zdy = dy / zoom;
1521 sp_selection_move_relative(selection, zdx, zdy);
1523 if (dx == 0) {
1524 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical", SP_VERB_CONTEXT_SELECT,
1525 _("Move vertically by pixels"));
1526 } else if (dy == 0) {
1527 sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal", SP_VERB_CONTEXT_SELECT,
1528 _("Move horizontally by pixels"));
1529 } else {
1530 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SELECT,
1531 _("Move"));
1532 }
1533 }
1535 namespace {
1537 template <typename D>
1538 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
1539 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
1541 template <typename D>
1542 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items, SPObject *root,
1543 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive);
1545 struct Forward {
1546 typedef SPObject *Iterator;
1548 static Iterator children(SPObject *o) { return sp_object_first_child(o); }
1549 static Iterator siblings_after(SPObject *o) { return SP_OBJECT_NEXT(o); }
1550 static void dispose(Iterator /*i*/) {}
1552 static SPObject *object(Iterator i) { return i; }
1553 static Iterator next(Iterator i) { return SP_OBJECT_NEXT(i); }
1554 };
1556 struct Reverse {
1557 typedef GSList *Iterator;
1559 static Iterator children(SPObject *o) {
1560 return make_list(o->firstChild(), NULL);
1561 }
1562 static Iterator siblings_after(SPObject *o) {
1563 return make_list(SP_OBJECT_PARENT(o)->firstChild(), o);
1564 }
1565 static void dispose(Iterator i) {
1566 g_slist_free(i);
1567 }
1569 static SPObject *object(Iterator i) {
1570 return reinterpret_cast<SPObject *>(i->data);
1571 }
1572 static Iterator next(Iterator i) { return i->next; }
1574 private:
1575 static GSList *make_list(SPObject *object, SPObject *limit) {
1576 GSList *list=NULL;
1577 while ( object != limit ) {
1578 list = g_slist_prepend(list, object);
1579 object = SP_OBJECT_NEXT(object);
1580 }
1581 return list;
1582 }
1583 };
1585 }
1587 void
1588 sp_selection_item_next(void)
1589 {
1590 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1591 g_return_if_fail(desktop != NULL);
1592 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1594 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
1595 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
1596 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
1598 SPObject *root;
1599 if (PREFS_SELECTION_ALL != inlayer) {
1600 root = selection->activeContext();
1601 } else {
1602 root = desktop->currentRoot();
1603 }
1605 SPItem *item=next_item_from_list<Forward>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1607 if (item) {
1608 selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
1609 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1610 scroll_to_show_item(desktop, item);
1611 }
1612 }
1613 }
1615 void
1616 sp_selection_item_prev(void)
1617 {
1618 SPDocument *document = SP_ACTIVE_DOCUMENT;
1619 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1620 g_return_if_fail(document != NULL);
1621 g_return_if_fail(desktop != NULL);
1622 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1624 PrefsSelectionContext inlayer = (PrefsSelectionContext)prefs_get_int_attribute ("options.kbselection", "inlayer", PREFS_SELECTION_LAYER);
1625 bool onlyvisible = prefs_get_int_attribute ("options.kbselection", "onlyvisible", 1);
1626 bool onlysensitive = prefs_get_int_attribute ("options.kbselection", "onlysensitive", 1);
1628 SPObject *root;
1629 if (PREFS_SELECTION_ALL != inlayer) {
1630 root = selection->activeContext();
1631 } else {
1632 root = desktop->currentRoot();
1633 }
1635 SPItem *item=next_item_from_list<Reverse>(desktop, selection->itemList(), root, SP_CYCLING == SP_CYCLE_VISIBLE, inlayer, onlyvisible, onlysensitive);
1637 if (item) {
1638 selection->set(item, PREFS_SELECTION_LAYER_RECURSIVE == inlayer);
1639 if ( SP_CYCLING == SP_CYCLE_FOCUS ) {
1640 scroll_to_show_item(desktop, item);
1641 }
1642 }
1643 }
1645 void sp_selection_next_patheffect_param(SPDesktop * dt)
1646 {
1647 if (!dt) return;
1649 Inkscape::Selection *selection = sp_desktop_selection(dt);
1650 if ( selection && !selection->isEmpty() ) {
1651 SPItem *item = selection->singleItem();
1652 if ( item && SP_IS_SHAPE(item)) {
1653 if (sp_lpe_item_has_path_effect(SP_LPE_ITEM(item))) {
1654 sp_lpe_item_edit_next_param_oncanvas(SP_LPE_ITEM(item), dt);
1655 } else {
1656 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("The selection has no applied path effect."));
1657 }
1658 }
1659 }
1660 }
1662 void sp_selection_edit_clip_or_mask(SPDesktop * dt, bool clip)
1663 {
1664 if (!dt) return;
1666 Inkscape::Selection *selection = sp_desktop_selection(dt);
1667 if ( selection && !selection->isEmpty() ) {
1668 SPItem *item = selection->singleItem();
1669 if ( item ) {
1670 SPObject *obj = NULL;
1671 if (clip)
1672 obj = item->clip_ref ? SP_OBJECT(item->clip_ref->getObject()) : NULL;
1673 else
1674 obj = item->mask_ref ? SP_OBJECT(item->mask_ref->getObject()) : NULL;
1676 if (obj) {
1677 // obj is a group object, the children are the actual clippers
1678 for ( SPObject *child = obj->children ; child ; child = child->next ) {
1679 if ( SP_IS_ITEM(child) ) {
1680 // If not already in nodecontext, goto it!
1681 if (!tools_isactive(dt, TOOLS_NODES)) {
1682 tools_switch_current(TOOLS_NODES);
1683 }
1685 ShapeEditor * shape_editor = SP_NODE_CONTEXT( dt->event_context )->shape_editor;
1686 // TODO: should we set the item for nodepath or knotholder or both? seems to work with both.
1687 shape_editor->set_item(SP_ITEM(child), SH_NODEPATH);
1688 shape_editor->set_item(SP_ITEM(child), SH_KNOTHOLDER);
1689 Inkscape::NodePath::Path *np = shape_editor->get_nodepath();
1690 if (np) {
1691 // take colors from prefs (same as used in outline mode)
1692 np->helperpath_rgba = clip ?
1693 prefs_get_int_attribute("options.wireframecolors", "clips", 0x00ff00ff) :
1694 prefs_get_int_attribute("options.wireframecolors", "masks", 0x0000ffff);
1695 np->helperpath_width = 1.0;
1696 sp_nodepath_show_helperpath(np, true);
1697 }
1698 break; // break out of for loop after 1st encountered item
1699 }
1700 }
1701 } else if (clip) {
1702 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("The selection has no applied clip path."));
1703 } else {
1704 dt->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("The selection has no applied mask."));
1705 }
1706 }
1707 }
1708 }
1711 namespace {
1713 template <typename D>
1714 SPItem *next_item_from_list(SPDesktop *desktop, GSList const *items,
1715 SPObject *root, bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
1716 {
1717 SPObject *current=root;
1718 while (items) {
1719 SPItem *item=SP_ITEM(items->data);
1720 if ( root->isAncestorOf(item) &&
1721 ( !only_in_viewport || desktop->isWithinViewport(item) ) )
1722 {
1723 current = item;
1724 break;
1725 }
1726 items = items->next;
1727 }
1729 GSList *path=NULL;
1730 while ( current != root ) {
1731 path = g_slist_prepend(path, current);
1732 current = SP_OBJECT_PARENT(current);
1733 }
1735 SPItem *next;
1736 // first, try from the current object
1737 next = next_item<D>(desktop, path, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1738 g_slist_free(path);
1740 if (!next) { // if we ran out of objects, start over at the root
1741 next = next_item<D>(desktop, NULL, root, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1742 }
1744 return next;
1745 }
1747 template <typename D>
1748 SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
1749 bool only_in_viewport, PrefsSelectionContext inlayer, bool onlyvisible, bool onlysensitive)
1750 {
1751 typename D::Iterator children;
1752 typename D::Iterator iter;
1754 SPItem *found=NULL;
1756 if (path) {
1757 SPObject *object=reinterpret_cast<SPObject *>(path->data);
1758 g_assert(SP_OBJECT_PARENT(object) == root);
1759 if (desktop->isLayer(object)) {
1760 found = next_item<D>(desktop, path->next, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1761 }
1762 iter = children = D::siblings_after(object);
1763 } else {
1764 iter = children = D::children(root);
1765 }
1767 while ( iter && !found ) {
1768 SPObject *object=D::object(iter);
1769 if (desktop->isLayer(object)) {
1770 if (PREFS_SELECTION_LAYER != inlayer) { // recurse into sublayers
1771 found = next_item<D>(desktop, NULL, object, only_in_viewport, inlayer, onlyvisible, onlysensitive);
1772 }
1773 } else if ( SP_IS_ITEM(object) &&
1774 ( !only_in_viewport || desktop->isWithinViewport(SP_ITEM(object)) ) &&
1775 ( !onlyvisible || !desktop->itemIsHidden(SP_ITEM(object))) &&
1776 ( !onlysensitive || !SP_ITEM(object)->isLocked()) &&
1777 !desktop->isLayer(SP_ITEM(object)) )
1778 {
1779 found = SP_ITEM(object);
1780 }
1781 iter = D::next(iter);
1782 }
1784 D::dispose(children);
1786 return found;
1787 }
1789 }
1791 /**
1792 * If \a item is not entirely visible then adjust visible area to centre on the centre on of
1793 * \a item.
1794 */
1795 void scroll_to_show_item(SPDesktop *desktop, SPItem *item)
1796 {
1797 NR::Rect dbox = desktop->get_display_area();
1798 NR::Maybe<NR::Rect> sbox = sp_item_bbox_desktop(item);
1800 if ( sbox && dbox.contains(*sbox) == false ) {
1801 NR::Point const s_dt = sbox->midpoint();
1802 NR::Point const s_w = desktop->d2w(s_dt);
1803 NR::Point const d_dt = dbox.midpoint();
1804 NR::Point const d_w = desktop->d2w(d_dt);
1805 NR::Point const moved_w( d_w - s_w );
1806 gint const dx = (gint) moved_w[X];
1807 gint const dy = (gint) moved_w[Y];
1808 desktop->scroll_world(dx, dy);
1809 }
1810 }
1813 void
1814 sp_selection_clone()
1815 {
1816 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1817 if (desktop == NULL)
1818 return;
1820 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1822 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
1824 // check if something is selected
1825 if (selection->isEmpty()) {
1826 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object</b> to clone."));
1827 return;
1828 }
1830 GSList *reprs = g_slist_copy((GSList *) selection->reprList());
1832 selection->clear();
1834 // sorting items from different parents sorts each parent's subset without possibly mixing them, just what we need
1835 reprs = g_slist_sort(reprs, (GCompareFunc) sp_repr_compare_position);
1837 GSList *newsel = NULL;
1839 while (reprs) {
1840 Inkscape::XML::Node *sel_repr = (Inkscape::XML::Node *) reprs->data;
1841 Inkscape::XML::Node *parent = sp_repr_parent(sel_repr);
1843 Inkscape::XML::Node *clone = xml_doc->createElement("svg:use");
1844 clone->setAttribute("x", "0", false);
1845 clone->setAttribute("y", "0", false);
1846 clone->setAttribute("xlink:href", g_strdup_printf("#%s", sel_repr->attribute("id")), false);
1848 clone->setAttribute("inkscape:transform-center-x", sel_repr->attribute("inkscape:transform-center-x"), false);
1849 clone->setAttribute("inkscape:transform-center-y", sel_repr->attribute("inkscape:transform-center-y"), false);
1851 // add the new clone to the top of the original's parent
1852 parent->appendChild(clone);
1854 newsel = g_slist_prepend(newsel, clone);
1855 reprs = g_slist_remove(reprs, sel_repr);
1856 Inkscape::GC::release(clone);
1857 }
1859 // TRANSLATORS: only translate "string" in "context|string".
1860 // For more details, see http://developer.gnome.org/doc/API/2.0/glib/glib-I18N.html#Q-:CAPS
1861 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_CLONE,
1862 Q_("action|Clone"));
1864 selection->setReprList(newsel);
1866 g_slist_free(newsel);
1867 }
1869 void
1870 sp_selection_relink()
1871 {
1872 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1873 if (!desktop)
1874 return;
1876 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1878 if (selection->isEmpty()) {
1879 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>clones</b> to relink."));
1880 return;
1881 }
1883 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
1884 const gchar *newid = cm->getFirstObjectID();
1885 if (!newid) {
1886 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Copy an <b>object</b> to clipboard to relink clones to."));
1887 return;
1888 }
1889 gchar *newref = g_strdup_printf ("#%s", newid);
1891 // Get a copy of current selection.
1892 bool relinked = false;
1893 for (GSList *items = (GSList *) selection->itemList();
1894 items != NULL;
1895 items = items->next)
1896 {
1897 SPItem *item = (SPItem *) items->data;
1899 if (!SP_IS_USE(item))
1900 continue;
1902 SP_OBJECT_REPR(item)->setAttribute("xlink:href", newref);
1903 SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1904 relinked = true;
1905 }
1907 g_free(newref);
1909 if (!relinked) {
1910 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No clones to relink</b> in the selection."));
1911 } else {
1912 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNLINK_CLONE,
1913 _("Relink clone"));
1914 }
1915 }
1918 void
1919 sp_selection_unlink()
1920 {
1921 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1922 if (!desktop)
1923 return;
1925 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1927 if (selection->isEmpty()) {
1928 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>clones</b> to unlink."));
1929 return;
1930 }
1932 // Get a copy of current selection.
1933 GSList *new_select = NULL;
1934 bool unlinked = false;
1935 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
1936 items != NULL;
1937 items = items->next)
1938 {
1939 SPItem *item = (SPItem *) items->data;
1941 if (SP_IS_TEXT(item)) {
1942 SPObject *tspan = sp_tref_convert_to_tspan(SP_OBJECT(item));
1944 if (tspan) {
1945 SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1946 }
1948 // Set unlink to true, and fall into the next if which
1949 // will include this text item in the new selection
1950 unlinked = true;
1951 }
1953 if (!(SP_IS_USE(item) || SP_IS_TREF(item))) {
1954 // keep the non-use item in the new selection
1955 new_select = g_slist_prepend(new_select, item);
1956 continue;
1957 }
1959 SPItem *unlink;
1960 if (SP_IS_USE(item)) {
1961 unlink = sp_use_unlink(SP_USE(item));
1962 } else /*if (SP_IS_TREF(use))*/ {
1963 unlink = SP_ITEM(sp_tref_convert_to_tspan(SP_OBJECT(item)));
1964 }
1966 unlinked = true;
1967 // Add ungrouped items to the new selection.
1968 new_select = g_slist_prepend(new_select, unlink);
1969 }
1971 if (new_select) { // set new selection
1972 selection->clear();
1973 selection->setList(new_select);
1974 g_slist_free(new_select);
1975 }
1976 if (!unlinked) {
1977 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No clones to unlink</b> in the selection."));
1978 }
1980 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNLINK_CLONE,
1981 _("Unlink clone"));
1982 }
1984 void
1985 sp_select_clone_original()
1986 {
1987 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1988 if (desktop == NULL)
1989 return;
1991 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1993 SPItem *item = selection->singleItem();
1995 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.");
1997 // Check if other than two objects are selected
1998 if (g_slist_length((GSList *) selection->itemList()) != 1 || !item) {
1999 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
2000 return;
2001 }
2003 SPItem *original = NULL;
2004 if (SP_IS_USE(item)) {
2005 original = sp_use_get_original (SP_USE(item));
2006 } else if (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref) {
2007 original = sp_offset_get_source (SP_OFFSET(item));
2008 } else if (SP_IS_TEXT_TEXTPATH(item)) {
2009 original = sp_textpath_get_path_item (SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))));
2010 } else if (SP_IS_FLOWTEXT(item)) {
2011 original = SP_FLOWTEXT(item)->get_frame (NULL); // first frame only
2012 } else { // it's an object that we don't know what to do with
2013 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, error);
2014 return;
2015 }
2017 if (!original) {
2018 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>Cannot find</b> the object to select (orphaned clone, offset, textpath, flowed text?)"));
2019 return;
2020 }
2022 for (SPObject *o = original; o && !SP_IS_ROOT(o); o = SP_OBJECT_PARENT (o)) {
2023 if (SP_IS_DEFS (o)) {
2024 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("The object you're trying to select is <b>not visible</b> (it is in <defs>)"));
2025 return;
2026 }
2027 }
2029 if (original) {
2030 selection->clear();
2031 selection->set(original);
2032 if (SP_CYCLING == SP_CYCLE_FOCUS) {
2033 scroll_to_show_item(desktop, original);
2034 }
2035 }
2036 }
2039 void sp_selection_to_marker(bool apply)
2040 {
2041 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2042 if (desktop == NULL)
2043 return;
2045 SPDocument *doc = sp_desktop_document(desktop);
2046 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2048 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2050 // check if something is selected
2051 if (selection->isEmpty()) {
2052 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to marker."));
2053 return;
2054 }
2056 sp_document_ensure_up_to_date(doc);
2057 NR::Maybe<NR::Rect> r = selection->bounds();
2058 NR::Maybe<NR::Point> c = selection->center();
2059 if ( !r || !c || r->isEmpty() ) {
2060 return;
2061 }
2063 // calculate the transform to be applied to objects to move them to 0,0
2064 NR::Point move_p = NR::Point(0, sp_document_height(doc)) - *c;
2065 move_p[NR::Y] = -move_p[NR::Y];
2066 NR::Matrix move = NR::Matrix (NR::translate (move_p));
2068 GSList *items = g_slist_copy((GSList *) selection->itemList());
2070 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2072 // bottommost object, after sorting
2073 SPObject *parent = SP_OBJECT_PARENT (items->data);
2075 NR::Matrix parent_transform = from_2geom(sp_item_i2root_affine(SP_ITEM(parent)));
2077 // remember the position of the first item
2078 gint pos = SP_OBJECT_REPR (items->data)->position();
2079 (void)pos; // TODO check why this was remembered
2081 // create a list of duplicates
2082 GSList *repr_copies = NULL;
2083 for (GSList *i = items; i != NULL; i = i->next) {
2084 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2085 repr_copies = g_slist_prepend (repr_copies, dup);
2086 }
2088 NR::Rect bounds(desktop->dt2doc(r->min()), desktop->dt2doc(r->max()));
2090 if (apply) {
2091 // delete objects so that their clones don't get alerted; this object will be restored shortly
2092 for (GSList *i = items; i != NULL; i = i->next) {
2093 SPObject *item = SP_OBJECT (i->data);
2094 item->deleteObject (false);
2095 }
2096 }
2098 // Hack: Temporarily set clone compensation to unmoved, so that we can move clone-originals
2099 // without disturbing clones.
2100 // See ActorAlign::on_button_click() in src/ui/dialog/align-and-distribute.cpp
2101 int saved_compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2102 prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2104 gchar const *mark_id = generate_marker(repr_copies, bounds, doc,
2105 ( NR::Matrix(NR::translate(desktop->dt2doc(NR::Point(r->min()[NR::X],
2106 r->max()[NR::Y]))))
2107 * parent_transform.inverse() ),
2108 parent_transform * move);
2109 (void)mark_id;
2111 // restore compensation setting
2112 prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
2115 g_slist_free (items);
2117 sp_document_done (doc, SP_VERB_EDIT_SELECTION_2_MARKER,
2118 _("Objects to marker"));
2119 }
2121 static void sp_selection_to_guides_recursive(SPItem *item, bool deleteitem, bool wholegroups) {
2122 if (SP_IS_GROUP(item) && !SP_IS_BOX3D(item) && !wholegroups) {
2123 for (GSList *i = sp_item_group_item_list (SP_GROUP(item)); i != NULL; i = i->next) {
2124 sp_selection_to_guides_recursive(SP_ITEM(i->data), deleteitem, wholegroups);
2125 }
2126 } else {
2127 sp_item_convert_item_to_guides(item);
2129 if (deleteitem) {
2130 SP_OBJECT(item)->deleteObject(true);
2131 }
2132 }
2133 }
2135 void sp_selection_to_guides()
2136 {
2137 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2138 if (desktop == NULL)
2139 return;
2141 SPDocument *doc = sp_desktop_document(desktop);
2142 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2143 // we need to copy the list because it gets reset when objects are deleted
2144 GSList *items = g_slist_copy((GSList *) selection->itemList());
2146 if (!items) {
2147 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to guides."));
2148 return;
2149 }
2151 bool deleteitem = (prefs_get_int_attribute("tools", "cvg_keep_objects", 0) == 0);
2152 bool wholegroups = (prefs_get_int_attribute("tools", "cvg_convert_whole_groups", 0) != 0);
2154 for (GSList const *i = items; i != NULL; i = i->next) {
2155 sp_selection_to_guides_recursive(SP_ITEM(i->data), deleteitem, wholegroups);
2156 }
2158 sp_document_done (doc, SP_VERB_EDIT_SELECTION_2_GUIDES, _("Objects to guides"));
2159 }
2161 void
2162 sp_selection_tile(bool apply)
2163 {
2164 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2165 if (desktop == NULL)
2166 return;
2168 SPDocument *doc = sp_desktop_document(desktop);
2169 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2171 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2173 // check if something is selected
2174 if (selection->isEmpty()) {
2175 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to pattern."));
2176 return;
2177 }
2179 sp_document_ensure_up_to_date(doc);
2180 NR::Maybe<NR::Rect> r = selection->bounds();
2181 if ( !r || r->isEmpty() ) {
2182 return;
2183 }
2185 // calculate the transform to be applied to objects to move them to 0,0
2186 NR::Point move_p = NR::Point(0, sp_document_height(doc)) - (r->min() + NR::Point (0, r->extent(NR::Y)));
2187 move_p[NR::Y] = -move_p[NR::Y];
2188 NR::Matrix move = NR::Matrix (NR::translate (move_p));
2190 GSList *items = g_slist_copy((GSList *) selection->itemList());
2192 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2194 // bottommost object, after sorting
2195 SPObject *parent = SP_OBJECT_PARENT (items->data);
2197 NR::Matrix parent_transform = from_2geom(sp_item_i2root_affine(SP_ITEM(parent)));
2199 // remember the position of the first item
2200 gint pos = SP_OBJECT_REPR (items->data)->position();
2202 // create a list of duplicates
2203 GSList *repr_copies = NULL;
2204 for (GSList *i = items; i != NULL; i = i->next) {
2205 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2206 repr_copies = g_slist_prepend (repr_copies, dup);
2207 }
2208 // restore the z-order after prepends
2209 repr_copies = g_slist_reverse (repr_copies);
2211 NR::Rect bounds(desktop->dt2doc(r->min()), desktop->dt2doc(r->max()));
2213 if (apply) {
2214 // delete objects so that their clones don't get alerted; this object will be restored shortly
2215 for (GSList *i = items; i != NULL; i = i->next) {
2216 SPObject *item = SP_OBJECT (i->data);
2217 item->deleteObject (false);
2218 }
2219 }
2221 // Hack: Temporarily set clone compensation to unmoved, so that we can move clone-originals
2222 // without disturbing clones.
2223 // See ActorAlign::on_button_click() in src/ui/dialog/align-and-distribute.cpp
2224 int saved_compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2225 prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
2227 gchar const *pat_id = pattern_tile(repr_copies, bounds, doc,
2228 ( NR::Matrix(NR::translate(desktop->dt2doc(NR::Point(r->min()[NR::X],
2229 r->max()[NR::Y]))))
2230 * parent_transform.inverse() ),
2231 parent_transform * move);
2233 // restore compensation setting
2234 prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
2236 if (apply) {
2237 Inkscape::XML::Node *rect = xml_doc->createElement("svg:rect");
2238 rect->setAttribute("style", g_strdup_printf("stroke:none;fill:url(#%s)", pat_id));
2240 NR::Point min = bounds.min() * parent_transform.inverse();
2241 NR::Point max = bounds.max() * parent_transform.inverse();
2243 sp_repr_set_svg_double(rect, "width", max[NR::X] - min[NR::X]);
2244 sp_repr_set_svg_double(rect, "height", max[NR::Y] - min[NR::Y]);
2245 sp_repr_set_svg_double(rect, "x", min[NR::X]);
2246 sp_repr_set_svg_double(rect, "y", min[NR::Y]);
2248 // restore parent and position
2249 SP_OBJECT_REPR (parent)->appendChild(rect);
2250 rect->setPosition(pos > 0 ? pos : 0);
2251 SPItem *rectangle = (SPItem *) sp_desktop_document (desktop)->getObjectByRepr(rect);
2253 Inkscape::GC::release(rect);
2255 selection->clear();
2256 selection->set(rectangle);
2257 }
2259 g_slist_free (items);
2261 sp_document_done (doc, SP_VERB_EDIT_TILE,
2262 _("Objects to pattern"));
2263 }
2265 void
2266 sp_selection_untile()
2267 {
2268 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2269 if (desktop == NULL)
2270 return;
2272 SPDocument *doc = sp_desktop_document(desktop);
2273 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2275 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2277 // check if something is selected
2278 if (selection->isEmpty()) {
2279 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select an <b>object with pattern fill</b> to extract objects from."));
2280 return;
2281 }
2283 GSList *new_select = NULL;
2285 bool did = false;
2287 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
2288 items != NULL;
2289 items = items->next) {
2291 SPItem *item = (SPItem *) items->data;
2293 SPStyle *style = SP_OBJECT_STYLE (item);
2295 if (!style || !style->fill.isPaintserver())
2296 continue;
2298 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
2300 if (!SP_IS_PATTERN(server))
2301 continue;
2303 did = true;
2305 SPPattern *pattern = pattern_getroot (SP_PATTERN (server));
2307 NR::Matrix pat_transform = pattern_patternTransform (SP_PATTERN (server));
2308 pat_transform *= item->transform;
2310 for (SPObject *child = sp_object_first_child(SP_OBJECT(pattern)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2311 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate(xml_doc);
2312 SPItem *i = SP_ITEM (desktop->currentLayer()->appendChildRepr(copy));
2314 // FIXME: relink clones to the new canvas objects
2315 // use SPObject::setid when mental finishes it to steal ids of
2317 // this is needed to make sure the new item has curve (simply requestDisplayUpdate does not work)
2318 sp_document_ensure_up_to_date (doc);
2320 NR::Matrix transform( i->transform * pat_transform );
2321 sp_item_write_transform(i, SP_OBJECT_REPR(i), transform);
2323 new_select = g_slist_prepend(new_select, i);
2324 }
2326 SPCSSAttr *css = sp_repr_css_attr_new ();
2327 sp_repr_css_set_property (css, "fill", "none");
2328 sp_repr_css_change (SP_OBJECT_REPR (item), css, "style");
2329 }
2331 if (!did) {
2332 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No pattern fills</b> in the selection."));
2333 } else {
2334 sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNTILE,
2335 _("Pattern to objects"));
2336 selection->setList(new_select);
2337 }
2338 }
2340 void
2341 sp_selection_get_export_hints (Inkscape::Selection *selection, char const **filename, float *xdpi, float *ydpi)
2342 {
2343 if (selection->isEmpty()) {
2344 return;
2345 }
2347 GSList const *reprlst = selection->reprList();
2348 bool filename_search = TRUE;
2349 bool xdpi_search = TRUE;
2350 bool ydpi_search = TRUE;
2352 for(; reprlst != NULL &&
2353 filename_search &&
2354 xdpi_search &&
2355 ydpi_search;
2356 reprlst = reprlst->next) {
2357 gchar const *dpi_string;
2358 Inkscape::XML::Node * repr = (Inkscape::XML::Node *)reprlst->data;
2360 if (filename_search) {
2361 *filename = repr->attribute("inkscape:export-filename");
2362 if (*filename != NULL)
2363 filename_search = FALSE;
2364 }
2366 if (xdpi_search) {
2367 dpi_string = NULL;
2368 dpi_string = repr->attribute("inkscape:export-xdpi");
2369 if (dpi_string != NULL) {
2370 *xdpi = atof(dpi_string);
2371 xdpi_search = FALSE;
2372 }
2373 }
2375 if (ydpi_search) {
2376 dpi_string = NULL;
2377 dpi_string = repr->attribute("inkscape:export-ydpi");
2378 if (dpi_string != NULL) {
2379 *ydpi = atof(dpi_string);
2380 ydpi_search = FALSE;
2381 }
2382 }
2383 }
2384 }
2386 void
2387 sp_document_get_export_hints (SPDocument *doc, char const **filename, float *xdpi, float *ydpi)
2388 {
2389 Inkscape::XML::Node * repr = sp_document_repr_root(doc);
2390 gchar const *dpi_string;
2392 *filename = repr->attribute("inkscape:export-filename");
2394 dpi_string = NULL;
2395 dpi_string = repr->attribute("inkscape:export-xdpi");
2396 if (dpi_string != NULL) {
2397 *xdpi = atof(dpi_string);
2398 }
2400 dpi_string = NULL;
2401 dpi_string = repr->attribute("inkscape:export-ydpi");
2402 if (dpi_string != NULL) {
2403 *ydpi = atof(dpi_string);
2404 }
2405 }
2407 void
2408 sp_selection_create_bitmap_copy ()
2409 {
2410 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2411 if (desktop == NULL)
2412 return;
2414 SPDocument *document = sp_desktop_document(desktop);
2415 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
2417 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2419 // check if something is selected
2420 if (selection->isEmpty()) {
2421 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to make a bitmap copy."));
2422 return;
2423 }
2425 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Rendering bitmap..."));
2426 // set "busy" cursor
2427 desktop->setWaitingCursor();
2429 // Get the bounding box of the selection
2430 NRRect bbox;
2431 sp_document_ensure_up_to_date (document);
2432 selection->bounds(&bbox);
2433 if (NR_RECT_DFLS_TEST_EMPTY(&bbox)) {
2434 desktop->clearWaitingCursor();
2435 return; // exceptional situation, so not bother with a translatable error message, just quit quietly
2436 }
2438 // List of the items to show; all others will be hidden
2439 GSList *items = g_slist_copy ((GSList *) selection->itemList());
2441 // Sort items so that the topmost comes last
2442 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
2444 // Generate a random value from the current time (you may create bitmap from the same object(s)
2445 // multiple times, and this is done so that they don't clash)
2446 GTimeVal cu;
2447 g_get_current_time (&cu);
2448 guint current = (int) (cu.tv_sec * 1000000 + cu.tv_usec) % 1024;
2450 // Create the filename
2451 gchar *filename = g_strdup_printf ("%s-%s-%u.png", document->name, SP_OBJECT_REPR(items->data)->attribute("id"), current);
2452 // Imagemagick is known not to handle spaces in filenames, so we replace anything but letters,
2453 // digits, and a few other chars, with "_"
2454 filename = g_strcanon (filename, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.=+~$#@^&!?", '_');
2455 // Build the complete path by adding document->base if set
2456 gchar *filepath = g_build_filename (document->base?document->base:"", filename, NULL);
2458 //g_print ("%s\n", filepath);
2460 // Remember parent and z-order of the topmost one
2461 gint pos = SP_OBJECT_REPR(g_slist_last(items)->data)->position();
2462 SPObject *parent_object = SP_OBJECT_PARENT(g_slist_last(items)->data);
2463 Inkscape::XML::Node *parent = SP_OBJECT_REPR(parent_object);
2465 // Calculate resolution
2466 double res;
2467 int const prefs_res = prefs_get_int_attribute ("options.createbitmap", "resolution", 0);
2468 int const prefs_min = prefs_get_int_attribute ("options.createbitmap", "minsize", 0);
2469 if (0 < prefs_res) {
2470 // If it's given explicitly in prefs, take it
2471 res = prefs_res;
2472 } else if (0 < prefs_min) {
2473 // If minsize is given, look up minimum bitmap size (default 250 pixels) and calculate resolution from it
2474 res = PX_PER_IN * prefs_min / MIN ((bbox.x1 - bbox.x0), (bbox.y1 - bbox.y0));
2475 } else {
2476 float hint_xdpi = 0, hint_ydpi = 0;
2477 char const *hint_filename;
2478 // take resolution hint from the selected objects
2479 sp_selection_get_export_hints (selection, &hint_filename, &hint_xdpi, &hint_ydpi);
2480 if (hint_xdpi != 0) {
2481 res = hint_xdpi;
2482 } else {
2483 // take resolution hint from the document
2484 sp_document_get_export_hints (document, &hint_filename, &hint_xdpi, &hint_ydpi);
2485 if (hint_xdpi != 0) {
2486 res = hint_xdpi;
2487 } else {
2488 // if all else fails, take the default 90 dpi
2489 res = PX_PER_IN;
2490 }
2491 }
2492 }
2494 // The width and height of the bitmap in pixels
2495 unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * res / PX_PER_IN);
2496 unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * res / PX_PER_IN);
2498 // Find out if we have to run an external filter
2499 gchar const *run = NULL;
2500 gchar const *filter = prefs_get_string_attribute ("options.createbitmap", "filter");
2501 if (filter) {
2502 // filter command is given;
2503 // see if we have a parameter to pass to it
2504 gchar const *param1 = prefs_get_string_attribute ("options.createbitmap", "filter_param1");
2505 if (param1) {
2506 if (param1[strlen(param1) - 1] == '%') {
2507 // if the param string ends with %, interpret it as a percentage of the image's max dimension
2508 gchar p1[256];
2509 g_ascii_dtostr (p1, 256, ceil (g_ascii_strtod (param1, NULL) * MAX(width, height) / 100));
2510 // the first param is always the image filename, the second is param1
2511 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, p1);
2512 } else {
2513 // otherwise pass the param1 unchanged
2514 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, param1);
2515 }
2516 } else {
2517 // run without extra parameter
2518 run = g_strdup_printf ("%s \"%s\"", filter, filepath);
2519 }
2520 }
2522 // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
2523 NR::Matrix eek = from_2geom(sp_item_i2d_affine (SP_ITEM(parent_object)));
2524 NR::Matrix t;
2526 double shift_x = bbox.x0;
2527 double shift_y = bbox.y1;
2528 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
2529 shift_x = round (shift_x);
2530 shift_y = -round (-shift_y); // this gets correct rounding despite coordinate inversion, remove the negations when the inversion is gone
2531 }
2532 t = NR::scale(1, -1) * NR::translate (shift_x, shift_y) * eek.inverse();
2534 // Do the export
2535 sp_export_png_file(document, filepath,
2536 bbox.x0, bbox.y0, bbox.x1, bbox.y1,
2537 width, height, res, res,
2538 (guint32) 0xffffff00,
2539 NULL, NULL,
2540 true, /*bool force_overwrite,*/
2541 items);
2543 g_slist_free (items);
2545 // Run filter, if any
2546 if (run) {
2547 g_print ("Running external filter: %s\n", run);
2548 int retval;
2549 retval = system (run);
2550 }
2552 // Import the image back
2553 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filepath, NULL);
2554 if (pb) {
2555 // Create the repr for the image
2556 Inkscape::XML::Node * repr = xml_doc->createElement("svg:image");
2557 repr->setAttribute("xlink:href", filename);
2558 repr->setAttribute("sodipodi:absref", filepath);
2559 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
2560 sp_repr_set_svg_double(repr, "width", width);
2561 sp_repr_set_svg_double(repr, "height", height);
2562 } else {
2563 sp_repr_set_svg_double(repr, "width", (bbox.x1 - bbox.x0));
2564 sp_repr_set_svg_double(repr, "height", (bbox.y1 - bbox.y0));
2565 }
2567 // Write transform
2568 gchar *c=sp_svg_transform_write(t);
2569 repr->setAttribute("transform", c);
2570 g_free(c);
2572 // add the new repr to the parent
2573 parent->appendChild(repr);
2575 // move to the saved position
2576 repr->setPosition(pos > 0 ? pos + 1 : 1);
2578 // Set selection to the new image
2579 selection->clear();
2580 selection->add(repr);
2582 // Clean up
2583 Inkscape::GC::release(repr);
2584 gdk_pixbuf_unref (pb);
2586 // Complete undoable transaction
2587 sp_document_done (document, SP_VERB_SELECTION_CREATE_BITMAP,
2588 _("Create bitmap"));
2589 }
2591 desktop->clearWaitingCursor();
2593 g_free (filename);
2594 g_free (filepath);
2595 }
2597 /**
2598 * \brief Creates a mask or clipPath from selection
2599 * Two different modes:
2600 * if applyToLayer, all selection is moved to DEFS as mask/clippath
2601 * and is applied to current layer
2602 * otherwise, topmost object is used as mask for other objects
2603 * If \a apply_clip_path parameter is true, clipPath is created, otherwise mask
2604 *
2605 */
2606 void
2607 sp_selection_set_mask(bool apply_clip_path, bool apply_to_layer)
2608 {
2609 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2610 if (desktop == NULL)
2611 return;
2613 SPDocument *doc = sp_desktop_document(desktop);
2614 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2616 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2618 // check if something is selected
2619 bool is_empty = selection->isEmpty();
2620 if ( apply_to_layer && is_empty) {
2621 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to create clippath or mask from."));
2622 return;
2623 } else if (!apply_to_layer && ( is_empty || NULL == selection->itemList()->next )) {
2624 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select mask object and <b>object(s)</b> to apply clippath or mask to."));
2625 return;
2626 }
2628 // FIXME: temporary patch to prevent crash!
2629 // Remove this when bboxes are fixed to not blow up on an item clipped/masked with its own clone
2630 bool clone_with_original = selection_contains_both_clone_and_original (selection);
2631 if (clone_with_original) {
2632 return; // in this version, you cannot clip/mask an object with its own clone
2633 }
2634 // /END FIXME
2636 sp_document_ensure_up_to_date(doc);
2638 GSList *items = g_slist_copy((GSList *) selection->itemList());
2640 items = g_slist_sort (items, (GCompareFunc) sp_object_compare_position);
2642 // create a list of duplicates
2643 GSList *mask_items = NULL;
2644 GSList *apply_to_items = NULL;
2645 GSList *items_to_delete = NULL;
2646 bool topmost = prefs_get_int_attribute ("options.maskobject", "topmost", 1);
2647 bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
2649 if (apply_to_layer) {
2650 // all selected items are used for mask, which is applied to a layer
2651 apply_to_items = g_slist_prepend (apply_to_items, desktop->currentLayer());
2653 for (GSList *i = items; i != NULL; i = i->next) {
2654 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2655 mask_items = g_slist_prepend (mask_items, dup);
2657 if (remove_original) {
2658 SPObject *item = SP_OBJECT (i->data);
2659 items_to_delete = g_slist_prepend (items_to_delete, item);
2660 }
2661 }
2662 } else if (!topmost) {
2663 // topmost item is used as a mask, which is applied to other items in a selection
2664 GSList *i = items;
2665 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2666 mask_items = g_slist_prepend (mask_items, dup);
2668 if (remove_original) {
2669 SPObject *item = SP_OBJECT (i->data);
2670 items_to_delete = g_slist_prepend (items_to_delete, item);
2671 }
2673 for (i = i->next; i != NULL; i = i->next) {
2674 apply_to_items = g_slist_prepend (apply_to_items, i->data);
2675 }
2676 } else {
2677 GSList *i = NULL;
2678 for (i = items; NULL != i->next; i = i->next) {
2679 apply_to_items = g_slist_prepend (apply_to_items, i->data);
2680 }
2682 Inkscape::XML::Node *dup = (SP_OBJECT_REPR (i->data))->duplicate(xml_doc);
2683 mask_items = g_slist_prepend (mask_items, dup);
2685 if (remove_original) {
2686 SPObject *item = SP_OBJECT (i->data);
2687 items_to_delete = g_slist_prepend (items_to_delete, item);
2688 }
2689 }
2691 g_slist_free (items);
2692 items = NULL;
2694 gchar const *attributeName = apply_clip_path ? "clip-path" : "mask";
2695 for (GSList *i = apply_to_items; NULL != i; i = i->next) {
2696 SPItem *item = reinterpret_cast<SPItem *>(i->data);
2697 // inverted object transform should be applied to a mask object,
2698 // as mask is calculated in user space (after applying transform)
2699 NR::Matrix maskTransform (item->transform.inverse());
2701 GSList *mask_items_dup = NULL;
2702 for (GSList *mask_item = mask_items; NULL != mask_item; mask_item = mask_item->next) {
2703 Inkscape::XML::Node *dup = reinterpret_cast<Inkscape::XML::Node *>(mask_item->data)->duplicate(xml_doc);
2704 mask_items_dup = g_slist_prepend (mask_items_dup, dup);
2705 }
2707 gchar const *mask_id = NULL;
2708 if (apply_clip_path) {
2709 mask_id = sp_clippath_create(mask_items_dup, doc, &maskTransform);
2710 } else {
2711 mask_id = sp_mask_create(mask_items_dup, doc, &maskTransform);
2712 }
2714 g_slist_free (mask_items_dup);
2715 mask_items_dup = NULL;
2717 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, g_strdup_printf("url(#%s)", mask_id));
2718 }
2720 g_slist_free (mask_items);
2721 g_slist_free (apply_to_items);
2723 for (GSList *i = items_to_delete; NULL != i; i = i->next) {
2724 SPObject *item = SP_OBJECT (i->data);
2725 item->deleteObject (false);
2726 }
2727 g_slist_free (items_to_delete);
2729 if (apply_clip_path)
2730 sp_document_done (doc, SP_VERB_OBJECT_SET_CLIPPATH, _("Set clipping path"));
2731 else
2732 sp_document_done (doc, SP_VERB_OBJECT_SET_MASK, _("Set mask"));
2733 }
2735 void sp_selection_unset_mask(bool apply_clip_path) {
2736 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
2737 if (desktop == NULL)
2738 return;
2740 SPDocument *doc = sp_desktop_document(desktop);
2741 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
2742 Inkscape::Selection *selection = sp_desktop_selection(desktop);
2744 // check if something is selected
2745 if (selection->isEmpty()) {
2746 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to remove clippath or mask from."));
2747 return;
2748 }
2750 bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
2751 sp_document_ensure_up_to_date(doc);
2753 gchar const *attributeName = apply_clip_path ? "clip-path" : "mask";
2754 std::map<SPObject*,SPItem*> referenced_objects;
2755 // SPObject* refers to a group containing the clipped path or mask itself,
2756 // whereas SPItem* refers to the item being clipped or masked
2757 for (GSList const *i = selection->itemList(); NULL != i; i = i->next) {
2758 if (remove_original) {
2759 // remember referenced mask/clippath, so orphaned masks can be moved back to document
2760 SPItem *item = reinterpret_cast<SPItem *>(i->data);
2761 Inkscape::URIReference *uri_ref = NULL;
2763 if (apply_clip_path) {
2764 uri_ref = item->clip_ref;
2765 } else {
2766 uri_ref = item->mask_ref;
2767 }
2769 // collect distinct mask object (and associate with item to apply transform)
2770 if (NULL != uri_ref && NULL != uri_ref->getObject()) {
2771 referenced_objects[uri_ref->getObject()] = item;
2772 }
2773 }
2775 SP_OBJECT_REPR(i->data)->setAttribute(attributeName, "none");
2776 }
2778 // restore mask objects into a document
2779 for ( std::map<SPObject*,SPItem*>::iterator it = referenced_objects.begin() ; it != referenced_objects.end() ; ++it) {
2780 SPObject *obj = (*it).first; // Group containing the clipped paths or masks
2781 GSList *items_to_move = NULL;
2782 for (SPObject *child = sp_object_first_child(obj) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
2783 // Collect all clipped paths and masks within a single group
2784 Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate(xml_doc);
2785 items_to_move = g_slist_prepend (items_to_move, copy);
2786 }
2788 if (!obj->isReferenced()) {
2789 // delete from defs if no other object references this mask
2790 obj->deleteObject(false);
2791 }
2793 // remember parent and position of the item to which the clippath/mask was applied
2794 Inkscape::XML::Node *parent = SP_OBJECT_REPR((*it).second)->parent();
2795 gint pos = SP_OBJECT_REPR((*it).second)->position();
2797 // Iterate through all clipped paths / masks
2798 for (GSList *i = items_to_move; NULL != i; i = i->next) {
2799 Inkscape::XML::Node *repr = (Inkscape::XML::Node *)i->data;
2801 // insert into parent, restore pos
2802 parent->appendChild(repr);
2803 repr->setPosition((pos + 1) > 0 ? (pos + 1) : 0);
2805 SPItem *mask_item = (SPItem *) sp_desktop_document (desktop)->getObjectByRepr(repr);
2806 selection->add(repr);
2808 // transform mask, so it is moved the same spot where mask was applied
2809 NR::Matrix transform (mask_item->transform);
2810 transform *= (*it).second->transform;
2811 sp_item_write_transform(mask_item, SP_OBJECT_REPR(mask_item), transform);
2812 }
2814 g_slist_free (items_to_move);
2815 }
2817 if (apply_clip_path)
2818 sp_document_done (doc, SP_VERB_OBJECT_UNSET_CLIPPATH, _("Release clipping path"));
2819 else
2820 sp_document_done (doc, SP_VERB_OBJECT_UNSET_MASK, _("Release mask"));
2821 }
2823 /**
2824 * Returns true if an undoable change should be recorded.
2825 */
2826 bool
2827 fit_canvas_to_selection(SPDesktop *desktop)
2828 {
2829 g_return_val_if_fail(desktop != NULL, false);
2830 SPDocument *doc = sp_desktop_document(desktop);
2832 g_return_val_if_fail(doc != NULL, false);
2833 g_return_val_if_fail(desktop->selection != NULL, false);
2835 if (desktop->selection->isEmpty()) {
2836 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to fit canvas to."));
2837 return false;
2838 }
2839 NR::Maybe<NR::Rect> const bbox(desktop->selection->bounds());
2840 if (bbox && !bbox->isEmpty()) {
2841 doc->fitToRect(*bbox);
2842 return true;
2843 } else {
2844 return false;
2845 }
2846 }
2848 /**
2849 * Fit canvas to the bounding box of the selection, as an undoable action.
2850 */
2851 void
2852 verb_fit_canvas_to_selection(SPDesktop *const desktop)
2853 {
2854 if (fit_canvas_to_selection(desktop)) {
2855 sp_document_done(sp_desktop_document(desktop), SP_VERB_FIT_CANVAS_TO_SELECTION,
2856 _("Fit Page to Selection"));
2857 }
2858 }
2860 bool
2861 fit_canvas_to_drawing(SPDocument *doc)
2862 {
2863 g_return_val_if_fail(doc != NULL, false);
2865 sp_document_ensure_up_to_date(doc);
2866 SPItem const *const root = SP_ITEM(doc->root);
2867 NR::Maybe<NR::Rect> const bbox(root->getBounds(from_2geom(sp_item_i2r_affine(root))));
2868 if (bbox && !bbox->isEmpty()) {
2869 doc->fitToRect(*bbox);
2870 return true;
2871 } else {
2872 return false;
2873 }
2874 }
2876 void
2877 verb_fit_canvas_to_drawing(SPDesktop *desktop)
2878 {
2879 if (fit_canvas_to_drawing(sp_desktop_document(desktop))) {
2880 sp_document_done(sp_desktop_document(desktop), SP_VERB_FIT_CANVAS_TO_DRAWING,
2881 _("Fit Page to Drawing"));
2882 }
2883 }
2885 void fit_canvas_to_selection_or_drawing(SPDesktop *desktop) {
2886 g_return_if_fail(desktop != NULL);
2887 SPDocument *doc = sp_desktop_document(desktop);
2889 g_return_if_fail(doc != NULL);
2890 g_return_if_fail(desktop->selection != NULL);
2892 bool const changed = ( desktop->selection->isEmpty()
2893 ? fit_canvas_to_drawing(doc)
2894 : fit_canvas_to_selection(desktop) );
2895 if (changed) {
2896 sp_document_done(sp_desktop_document(desktop), SP_VERB_FIT_CANVAS_TO_SELECTION_OR_DRAWING,
2897 _("Fit Page to Selection or Drawing"));
2898 }
2899 };
2901 static void itemtree_map(void (*f)(SPItem *, SPDesktop *), SPObject *root, SPDesktop *desktop) {
2902 // don't operate on layers
2903 if (SP_IS_ITEM(root) && !desktop->isLayer(SP_ITEM(root))) {
2904 f(SP_ITEM(root), desktop);
2905 }
2906 for ( SPObject::SiblingIterator iter = root->firstChild() ; iter ; ++iter ) {
2907 //don't recurse into locked layers
2908 if (!(SP_IS_ITEM(&*iter) && desktop->isLayer(SP_ITEM(&*iter)) && SP_ITEM(&*iter)->isLocked())) {
2909 itemtree_map(f, iter, desktop);
2910 }
2911 }
2912 }
2914 static void unlock(SPItem *item, SPDesktop */*desktop*/) {
2915 if (item->isLocked()) {
2916 item->setLocked(FALSE);
2917 }
2918 }
2920 static void unhide(SPItem *item, SPDesktop *desktop) {
2921 if (desktop->itemIsHidden(item)) {
2922 item->setExplicitlyHidden(FALSE);
2923 }
2924 }
2926 static void process_all(void (*f)(SPItem *, SPDesktop *), SPDesktop *dt, bool layer_only) {
2927 if (!dt) return;
2929 SPObject *root;
2930 if (layer_only) {
2931 root = dt->currentLayer();
2932 } else {
2933 root = dt->currentRoot();
2934 }
2936 itemtree_map(f, root, dt);
2937 }
2939 void unlock_all(SPDesktop *dt) {
2940 process_all(&unlock, dt, true);
2941 }
2943 void unlock_all_in_all_layers(SPDesktop *dt) {
2944 process_all(&unlock, dt, false);
2945 }
2947 void unhide_all(SPDesktop *dt) {
2948 process_all(&unhide, dt, true);
2949 }
2951 void unhide_all_in_all_layers(SPDesktop *dt) {
2952 process_all(&unhide, dt, false);
2953 }
2956 /*
2957 Local Variables:
2958 mode:c++
2959 c-file-style:"stroustrup"
2960 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2961 indent-tabs-mode:nil
2962 fill-column:99
2963 End:
2964 */
2965 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :