1 /** \file
2 * Per-desktop selection container
3 *
4 * Authors:
5 * Lauris Kaplinski <lauris@kaplinski.com>
6 * MenTaLguY <mental@rydia.net>
7 * bulia byak <buliabyak@users.sf.net>
8 * Andrius R. <knutux@gmail.com>
9 *
10 * Copyright (C) 2006 Andrius R.
11 * Copyright (C) 2004-2005 MenTaLguY
12 * Copyright (C) 1999-2002 Lauris Kaplinski
13 * Copyright (C) 2001-2002 Ximian, Inc.
14 *
15 * Released under GNU GPL, read the file 'COPYING' for more information
16 */
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21 #include "macros.h"
22 #include "inkscape-private.h"
23 #include "desktop.h"
24 #include "desktop-handles.h"
25 #include "document.h"
26 #include "selection.h"
27 #include "xml/repr.h"
29 #include "sp-shape.h"
30 #include "sp-path.h"
31 #include "sp-item-group.h"
32 #include "box3d.h"
33 #include "box3d.h"
34 #include "persp3d.h"
36 #include <sigc++/functors/mem_fun.h>
38 #define SP_SELECTION_UPDATE_PRIORITY (G_PRIORITY_HIGH_IDLE + 1)
40 namespace Inkscape {
42 Selection::Selection(SPDesktop *desktop) :
43 _objs(NULL),
44 _reprs(NULL),
45 _items(NULL),
46 _desktop(desktop),
47 _selection_context(NULL),
48 _flags(0),
49 _idle(0)
50 {
51 }
53 Selection::~Selection() {
54 _clear();
55 _desktop = NULL;
56 if (_idle) {
57 g_source_remove(_idle);
58 _idle = 0;
59 }
60 }
62 /* Handler for selected objects "modified" signal */
64 void Selection::_schedule_modified(SPObject */*obj*/, guint flags) {
65 if (!this->_idle) {
66 /* Request handling to be run in _idle loop */
67 this->_idle = g_idle_add_full(SP_SELECTION_UPDATE_PRIORITY, GSourceFunc(&Selection::_emit_modified), this, NULL);
68 }
70 /* Collect all flags */
71 this->_flags |= flags;
72 }
74 gboolean
75 Selection::_emit_modified(Selection *selection)
76 {
77 /* force new handler to be created if requested before we return */
78 selection->_idle = 0;
79 guint flags = selection->_flags;
80 selection->_flags = 0;
82 selection->_emitModified(flags);
84 /* drop this handler */
85 return FALSE;
86 }
88 void Selection::_emitModified(guint flags) {
89 inkscape_selection_modified(this, flags);
90 _modified_signal.emit(this, flags);
91 }
93 void Selection::_emitChanged(bool persist_selection_context/* = false */) {
94 if (persist_selection_context) {
95 if (NULL == _selection_context) {
96 _selection_context = desktop()->currentLayer();
97 sp_object_ref(_selection_context, NULL);
98 _context_release_connection = _selection_context->connectRelease(sigc::mem_fun(*this, &Selection::_releaseContext));
99 }
100 } else {
101 _releaseContext(_selection_context);
102 }
104 inkscape_selection_changed(this);
105 _changed_signal.emit(this);
106 }
108 void
109 Selection::_releaseContext(SPObject *obj)
110 {
111 if (NULL == _selection_context || _selection_context != obj)
112 return;
114 _context_release_connection.disconnect();
116 sp_object_unref(_selection_context, NULL);
117 _selection_context = NULL;
118 }
120 void Selection::_invalidateCachedLists() {
121 g_slist_free(_items);
122 _items = NULL;
124 g_slist_free(_reprs);
125 _reprs = NULL;
126 }
128 void Selection::_clear() {
129 _invalidateCachedLists();
130 while (_objs) {
131 SPObject *obj=reinterpret_cast<SPObject *>(_objs->data);
132 _remove(obj);
133 }
134 }
136 SPObject *Selection::activeContext() {
137 if (NULL != _selection_context)
138 return _selection_context;
139 return desktop()->currentLayer();
140 }
142 bool Selection::includes(SPObject *obj) const {
143 if (obj == NULL)
144 return FALSE;
146 g_return_val_if_fail(SP_IS_OBJECT(obj), FALSE);
148 return ( g_slist_find(_objs, obj) != NULL );
149 }
151 void Selection::add(SPObject *obj, bool persist_selection_context/* = false */) {
152 g_return_if_fail(obj != NULL);
153 g_return_if_fail(SP_IS_OBJECT(obj));
155 if (includes(obj)) {
156 return;
157 }
159 _invalidateCachedLists();
160 _add(obj);
161 _emitChanged(persist_selection_context);
162 }
164 void Selection::add_box_perspective(SPBox3D *box) {
165 Persp3D *persp = box3d_get_perspective(box);
166 std::map<Persp3D *, unsigned int>::iterator p = _persps.find(persp);
167 if (p != _persps.end()) {
168 (*p).second++;
169 } else {
170 _persps[persp] = 1;
171 }
172 }
174 void Selection::add_3D_boxes_recursively(SPObject *obj) {
175 std::list<SPBox3D *> boxes = box3d_extract_boxes(obj);
177 for (std::list<SPBox3D *>::iterator i = boxes.begin(); i != boxes.end(); ++i) {
178 SPBox3D *box = *i;
179 box3d_add_to_selection(box);
180 _3dboxes.push_back(box);
181 add_box_perspective(box);
182 }
183 }
185 void Selection::_add(SPObject *obj) {
186 // unselect any of the item's ancestors and descendants which may be selected
187 // (to prevent double-selection)
188 _removeObjectDescendants(obj);
189 _removeObjectAncestors(obj);
191 _objs = g_slist_prepend(_objs, obj);
193 add_3D_boxes_recursively(obj);
195 _release_connections[obj] = obj->connectRelease(sigc::mem_fun(*this, (void (Selection::*)(SPObject *))&Selection::remove));
196 _modified_connections[obj] = obj->connectModified(sigc::mem_fun(*this, &Selection::_schedule_modified));
197 }
199 void Selection::set(SPObject *object, bool persist_selection_context) {
200 _clear();
201 add(object, persist_selection_context);
202 }
204 void Selection::toggle(SPObject *obj) {
205 if (includes (obj)) {
206 remove (obj);
207 } else {
208 add(obj);
209 }
210 }
212 void Selection::remove(SPObject *obj) {
213 g_return_if_fail(obj != NULL);
214 g_return_if_fail(SP_IS_OBJECT(obj));
215 g_return_if_fail(includes(obj));
217 _invalidateCachedLists();
218 _remove(obj);
219 _emitChanged();
220 }
222 void Selection::remove_box_perspective(SPBox3D *box) {
223 Persp3D *persp = box3d_get_perspective(box);
224 std::map<Persp3D *, unsigned int>::iterator p = _persps.find(persp);
225 if (p == _persps.end()) {
226 g_print ("Warning! Trying to remove unselected perspective from selection!\n");
227 return;
228 }
229 if ((*p).second > 1) {
230 _persps[persp]--;
231 } else {
232 _persps.erase(p);
233 }
234 }
236 void Selection::remove_3D_boxes_recursively(SPObject *obj) {
237 std::list<SPBox3D *> boxes = box3d_extract_boxes(obj);
239 for (std::list<SPBox3D *>::iterator i = boxes.begin(); i != boxes.end(); ++i) {
240 SPBox3D *box = *i;
241 box3d_remove_from_selection(box);
242 std::list<SPBox3D *>::iterator b = std::find(_3dboxes.begin(), _3dboxes.end(), box);
243 if (b == _3dboxes.end()) {
244 g_print ("Warning! Trying to remove unselected box from selection.\n");
245 return;
246 }
247 _3dboxes.erase(b);
248 remove_box_perspective(box);
249 }
250 }
252 void Selection::_remove(SPObject *obj) {
253 _modified_connections[obj].disconnect();
254 _modified_connections.erase(obj);
256 _release_connections[obj].disconnect();
257 _release_connections.erase(obj);
259 remove_3D_boxes_recursively(obj);
261 _objs = g_slist_remove(_objs, obj);
262 }
264 void Selection::setList(GSList const *list) {
265 _clear();
267 if ( list != NULL ) {
268 for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
269 _add(reinterpret_cast<SPObject *>(iter->data));
270 }
271 }
273 _emitChanged();
274 }
276 void Selection::addList(GSList const *list) {
278 if (list == NULL)
279 return;
281 _invalidateCachedLists();
283 for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
284 SPObject *obj = reinterpret_cast<SPObject *>(iter->data);
285 if (includes(obj)) {
286 continue;
287 }
288 _add (obj);
289 }
291 _emitChanged();
292 }
294 void Selection::setReprList(GSList const *list) {
295 _clear();
297 for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
298 SPObject *obj=_objectForXMLNode(reinterpret_cast<Inkscape::XML::Node *>(iter->data));
299 if (obj) {
300 _add(obj);
301 }
302 }
304 _emitChanged();
305 }
307 void Selection::clear() {
308 _clear();
309 _emitChanged();
310 }
312 GSList const *Selection::list() {
313 return _objs;
314 }
316 GSList const *Selection::itemList() {
317 if (_items) {
318 return _items;
319 }
321 for ( GSList const *iter=_objs ; iter != NULL ; iter = iter->next ) {
322 SPObject *obj=reinterpret_cast<SPObject *>(iter->data);
323 if (SP_IS_ITEM(obj)) {
324 _items = g_slist_prepend(_items, SP_ITEM(obj));
325 }
326 }
327 _items = g_slist_reverse(_items);
329 return _items;
330 }
332 GSList const *Selection::reprList() {
333 if (_reprs) { return _reprs; }
335 for ( GSList const *iter=itemList() ; iter != NULL ; iter = iter->next ) {
336 SPObject *obj=reinterpret_cast<SPObject *>(iter->data);
337 _reprs = g_slist_prepend(_reprs, SP_OBJECT_REPR(obj));
338 }
339 _reprs = g_slist_reverse(_reprs);
341 return _reprs;
342 }
344 std::list<Persp3D *> const Selection::perspList() {
345 std::list<Persp3D *> pl;
346 for (std::map<Persp3D *, unsigned int>::iterator p = _persps.begin(); p != _persps.end(); ++p) {
347 pl.push_back((*p).first);
348 }
349 return pl;
350 }
352 std::list<SPBox3D *> const Selection::box3DList() {
353 return _3dboxes;
354 }
356 SPObject *Selection::single() {
357 if ( _objs != NULL && _objs->next == NULL ) {
358 return reinterpret_cast<SPObject *>(_objs->data);
359 } else {
360 return NULL;
361 }
362 }
364 SPItem *Selection::singleItem() {
365 GSList const *items=itemList();
366 if ( items != NULL && items->next == NULL ) {
367 return reinterpret_cast<SPItem *>(items->data);
368 } else {
369 return NULL;
370 }
371 }
373 Inkscape::XML::Node *Selection::singleRepr() {
374 SPObject *obj=single();
375 return obj ? SP_OBJECT_REPR(obj) : NULL;
376 }
378 NRRect *Selection::bounds(NRRect *bbox, SPItem::BBoxType type) const
379 {
380 g_return_val_if_fail (bbox != NULL, NULL);
381 *bbox = NRRect(bounds(type));
382 return bbox;
383 }
385 boost::optional<NR::Rect> Selection::bounds(SPItem::BBoxType type) const
386 {
387 GSList const *items = const_cast<Selection *>(this)->itemList();
389 boost::optional<NR::Rect> bbox;
390 for ( GSList const *i = items ; i != NULL ; i = i->next ) {
391 bbox = NR::union_bounds(bbox, sp_item_bbox_desktop(SP_ITEM(i->data), type));
392 }
393 return bbox;
394 }
396 // TODO: This should be replaces by a proper 2geom function
397 inline Geom::Rect union_bounds_2geom(boost::optional<Geom::Rect> const & a, Geom::Rect const &b) {
398 if (a) {
399 return union_bounds_2geom(*a, b);
400 } else {
401 return b;
402 }
403 }
405 boost::optional<Geom::Rect> Selection::bounds_2geom(SPItem::BBoxType type) const
406 {
407 GSList const *items = const_cast<Selection *>(this)->itemList();
409 boost::optional<NR::Rect> bbox;
410 for ( GSList const *i = items ; i != NULL ; i = i->next ) {
411 bbox = union_bounds(bbox, sp_item_bbox_desktop(SP_ITEM(i->data), type));
412 }
413 // TODO: eliminate this conversion after the switch to 2geom
414 boost::optional<Geom::Rect> bbox_ret;
415 if (bbox) {
416 bbox_ret = to_2geom(*bbox);
417 }
418 return bbox_ret;
419 }
421 NRRect *Selection::boundsInDocument(NRRect *bbox, SPItem::BBoxType type) const {
422 g_return_val_if_fail (bbox != NULL, NULL);
424 GSList const *items=const_cast<Selection *>(this)->itemList();
425 if (!items) {
426 bbox->x0 = bbox->y0 = bbox->x1 = bbox->y1 = 0.0;
427 return bbox;
428 }
430 bbox->x0 = bbox->y0 = 1e18;
431 bbox->x1 = bbox->y1 = -1e18;
433 for ( GSList const *iter=items ; iter != NULL ; iter = iter->next ) {
434 SPItem *item=SP_ITEM(iter->data);
435 Geom::Matrix i2doc(sp_item_i2doc_affine(item));
436 sp_item_invoke_bbox(item, bbox, i2doc, FALSE, type);
437 }
439 return bbox;
440 }
442 boost::optional<NR::Rect> Selection::boundsInDocument(SPItem::BBoxType type) const {
443 NRRect r;
444 return boundsInDocument(&r, type)->upgrade();
445 }
447 /** Extract the position of the center from the first selected object */
448 boost::optional<Geom::Point> Selection::center() const {
449 GSList *items = (GSList *) const_cast<Selection *>(this)->itemList();
450 Geom::Point center;
451 if (items) {
452 SPItem *first = reinterpret_cast<SPItem*>(g_slist_last(items)->data); // from the first item in selection
453 if (first->isCenterSet()) { // only if set explicitly
454 return first->getCenter();
455 }
456 }
457 boost::optional<NR::Rect> bbox = bounds();
458 if (bbox) {
459 return to_2geom(bounds()->midpoint());
460 } else {
461 return boost::optional<Geom::Point>();
462 }
463 }
465 /**
466 * Compute the list of points in the selection that are to be considered for snapping.
467 */
468 std::vector<NR::Point> Selection::getSnapPoints(bool includeItemCenter) const {
469 GSList const *items = const_cast<Selection *>(this)->itemList();
470 std::vector<NR::Point> p;
471 for (GSList const *iter = items; iter != NULL; iter = iter->next) {
472 SPItem *this_item = SP_ITEM(iter->data);
473 sp_item_snappoints(this_item, false, SnapPointsIter(p));
474 //Include the transformation origin for snapping
475 //For a group only the group's origin is considered
476 if (includeItemCenter) {
477 p.push_back(this_item->getCenter());
478 }
479 }
481 return p;
482 }
484 std::vector<NR::Point> Selection::getSnapPointsConvexHull() const {
485 GSList const *items = const_cast<Selection *>(this)->itemList();
487 std::vector<NR::Point> p;
488 for (GSList const *iter = items; iter != NULL; iter = iter->next) {
489 sp_item_snappoints(SP_ITEM(iter->data), false, SnapPointsIter(p));
490 }
492 std::vector<NR::Point> pHull;
493 if (!p.empty()) {
494 std::vector<NR::Point>::iterator i;
495 NR::ConvexHull cvh(p.front());
496 for (i = p.begin(); i != p.end(); i++) {
497 // these are the points we get back
498 cvh.add(*i);
499 }
501 boost::optional<NR::Rect> rHull = cvh.bounds();
502 if (rHull) {
503 for ( unsigned i = 0 ; i < 4 ; ++i ) {
504 pHull.push_back(rHull->corner(i));
505 }
506 }
507 }
509 return pHull;
510 }
512 void Selection::_removeObjectDescendants(SPObject *obj) {
513 GSList *iter, *next;
514 for ( iter = _objs ; iter ; iter = next ) {
515 next = iter->next;
516 SPObject *sel_obj=reinterpret_cast<SPObject *>(iter->data);
517 SPObject *parent=SP_OBJECT_PARENT(sel_obj);
518 while (parent) {
519 if ( parent == obj ) {
520 _remove(sel_obj);
521 break;
522 }
523 parent = SP_OBJECT_PARENT(parent);
524 }
525 }
526 }
528 void Selection::_removeObjectAncestors(SPObject *obj) {
529 SPObject *parent=SP_OBJECT_PARENT(obj);
530 while (parent) {
531 if (includes(parent)) {
532 _remove(parent);
533 }
534 parent = SP_OBJECT_PARENT(parent);
535 }
536 }
538 SPObject *Selection::_objectForXMLNode(Inkscape::XML::Node *repr) const {
539 g_return_val_if_fail(repr != NULL, NULL);
540 gchar const *id = repr->attribute("id");
541 g_return_val_if_fail(id != NULL, NULL);
542 SPObject *object=sp_desktop_document(_desktop)->getObjectById(id);
543 g_return_val_if_fail(object != NULL, NULL);
544 return object;
545 }
547 guint Selection::numberOfLayers() {
548 GSList const *items = const_cast<Selection *>(this)->itemList();
549 GSList *layers = NULL;
550 for (GSList const *iter = items; iter != NULL; iter = iter->next) {
551 SPObject *layer = desktop()->layerForObject(SP_OBJECT(iter->data));
552 if (g_slist_find (layers, layer) == NULL) {
553 layers = g_slist_prepend (layers, layer);
554 }
555 }
556 guint ret = g_slist_length (layers);
557 g_slist_free (layers);
558 return ret;
559 }
561 guint Selection::numberOfParents() {
562 GSList const *items = const_cast<Selection *>(this)->itemList();
563 GSList *parents = NULL;
564 for (GSList const *iter = items; iter != NULL; iter = iter->next) {
565 SPObject *parent = SP_OBJECT_PARENT(iter->data);
566 if (g_slist_find (parents, parent) == NULL) {
567 parents = g_slist_prepend (parents, parent);
568 }
569 }
570 guint ret = g_slist_length (parents);
571 g_slist_free (parents);
572 return ret;
573 }
575 }
577 /*
578 Local Variables:
579 mode:c++
580 c-file-style:"stroustrup"
581 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
582 indent-tabs-mode:nil
583 fill-column:99
584 End:
585 */
586 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :