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 "helper/recthull.h"
28 #include "xml/repr.h"
30 #include "sp-shape.h"
31 #include "sp-path.h"
32 #include "sp-item-group.h"
33 #include "box3d.h"
34 #include "box3d.h"
35 #include "persp3d.h"
37 #include <sigc++/functors/mem_fun.h>
39 #define SP_SELECTION_UPDATE_PRIORITY (G_PRIORITY_HIGH_IDLE + 1)
41 namespace Inkscape {
43 Selection::Selection(SPDesktop *desktop) :
44 _objs(NULL),
45 _reprs(NULL),
46 _items(NULL),
47 _desktop(desktop),
48 _selection_context(NULL),
49 _flags(0),
50 _idle(0)
51 {
52 }
54 Selection::~Selection() {
55 _clear();
56 _desktop = NULL;
57 if (_idle) {
58 g_source_remove(_idle);
59 _idle = 0;
60 }
61 }
63 /* Handler for selected objects "modified" signal */
65 void Selection::_schedule_modified(SPObject */*obj*/, guint flags) {
66 if (!this->_idle) {
67 /* Request handling to be run in _idle loop */
68 this->_idle = g_idle_add_full(SP_SELECTION_UPDATE_PRIORITY, GSourceFunc(&Selection::_emit_modified), this, NULL);
69 }
71 /* Collect all flags */
72 this->_flags |= flags;
73 }
75 gboolean
76 Selection::_emit_modified(Selection *selection)
77 {
78 /* force new handler to be created if requested before we return */
79 selection->_idle = 0;
80 guint flags = selection->_flags;
81 selection->_flags = 0;
83 selection->_emitModified(flags);
85 /* drop this handler */
86 return FALSE;
87 }
89 void Selection::_emitModified(guint flags) {
90 inkscape_selection_modified(this, flags);
91 _modified_signal.emit(this, flags);
92 }
94 void Selection::_emitChanged(bool persist_selection_context/* = false */) {
95 if (persist_selection_context) {
96 if (NULL == _selection_context) {
97 _selection_context = desktop()->currentLayer();
98 sp_object_ref(_selection_context, NULL);
99 _context_release_connection = _selection_context->connectRelease(sigc::mem_fun(*this, &Selection::_releaseContext));
100 }
101 } else {
102 _releaseContext(_selection_context);
103 }
105 inkscape_selection_changed(this);
106 _changed_signal.emit(this);
107 }
109 void
110 Selection::_releaseContext(SPObject *obj)
111 {
112 if (NULL == _selection_context || _selection_context != obj)
113 return;
115 _context_release_connection.disconnect();
117 sp_object_unref(_selection_context, NULL);
118 _selection_context = NULL;
119 }
121 void Selection::_invalidateCachedLists() {
122 g_slist_free(_items);
123 _items = NULL;
125 g_slist_free(_reprs);
126 _reprs = NULL;
127 }
129 void Selection::_clear() {
130 _invalidateCachedLists();
131 while (_objs) {
132 SPObject *obj=reinterpret_cast<SPObject *>(_objs->data);
133 _remove(obj);
134 }
135 }
137 SPObject *Selection::activeContext() {
138 if (NULL != _selection_context)
139 return _selection_context;
140 return desktop()->currentLayer();
141 }
143 bool Selection::includes(SPObject *obj) const {
144 if (obj == NULL)
145 return FALSE;
147 g_return_val_if_fail(SP_IS_OBJECT(obj), FALSE);
149 return ( g_slist_find(_objs, obj) != NULL );
150 }
152 void Selection::add(SPObject *obj, bool persist_selection_context/* = false */) {
153 g_return_if_fail(obj != NULL);
154 g_return_if_fail(SP_IS_OBJECT(obj));
156 if (includes(obj)) {
157 return;
158 }
160 _invalidateCachedLists();
161 _add(obj);
162 _emitChanged(persist_selection_context);
163 }
165 void Selection::add_box_perspective(SPBox3D *box) {
166 Persp3D *persp = box3d_get_perspective(box);
167 std::map<Persp3D *, unsigned int>::iterator p = _persps.find(persp);
168 if (p != _persps.end()) {
169 (*p).second++;
170 } else {
171 _persps[persp] = 1;
172 }
173 }
175 void Selection::add_3D_boxes_recursively(SPObject *obj) {
176 std::list<SPBox3D *> boxes = box3d_extract_boxes(obj);
178 for (std::list<SPBox3D *>::iterator i = boxes.begin(); i != boxes.end(); ++i) {
179 SPBox3D *box = *i;
180 box3d_add_to_selection(box);
181 _3dboxes.push_back(box);
182 add_box_perspective(box);
183 }
184 }
186 void Selection::_add(SPObject *obj) {
187 // unselect any of the item's ancestors and descendants which may be selected
188 // (to prevent double-selection)
189 _removeObjectDescendants(obj);
190 _removeObjectAncestors(obj);
192 _objs = g_slist_prepend(_objs, obj);
194 add_3D_boxes_recursively(obj);
196 _release_connections[obj] = obj->connectRelease(sigc::mem_fun(*this, (void (Selection::*)(SPObject *))&Selection::remove));
197 _modified_connections[obj] = obj->connectModified(sigc::mem_fun(*this, &Selection::_schedule_modified));
198 }
200 void Selection::set(SPObject *object, bool persist_selection_context) {
201 _clear();
202 add(object, persist_selection_context);
203 }
205 void Selection::toggle(SPObject *obj) {
206 if (includes (obj)) {
207 remove (obj);
208 } else {
209 add(obj);
210 }
211 }
213 void Selection::remove(SPObject *obj) {
214 g_return_if_fail(obj != NULL);
215 g_return_if_fail(SP_IS_OBJECT(obj));
216 g_return_if_fail(includes(obj));
218 _invalidateCachedLists();
219 _remove(obj);
220 _emitChanged();
221 }
223 void Selection::remove_box_perspective(SPBox3D *box) {
224 Persp3D *persp = box3d_get_perspective(box);
225 std::map<Persp3D *, unsigned int>::iterator p = _persps.find(persp);
226 if (p == _persps.end()) {
227 g_print ("Warning! Trying to remove unselected perspective from selection!\n");
228 return;
229 }
230 if ((*p).second > 1) {
231 _persps[persp]--;
232 } else {
233 _persps.erase(p);
234 }
235 }
237 void Selection::remove_3D_boxes_recursively(SPObject *obj) {
238 std::list<SPBox3D *> boxes = box3d_extract_boxes(obj);
240 for (std::list<SPBox3D *>::iterator i = boxes.begin(); i != boxes.end(); ++i) {
241 SPBox3D *box = *i;
242 box3d_remove_from_selection(box);
243 std::list<SPBox3D *>::iterator b = std::find(_3dboxes.begin(), _3dboxes.end(), box);
244 if (b == _3dboxes.end()) {
245 g_print ("Warning! Trying to remove unselected box from selection.\n");
246 return;
247 }
248 _3dboxes.erase(b);
249 remove_box_perspective(box);
250 }
251 }
253 void Selection::_remove(SPObject *obj) {
254 _modified_connections[obj].disconnect();
255 _modified_connections.erase(obj);
257 _release_connections[obj].disconnect();
258 _release_connections.erase(obj);
260 remove_3D_boxes_recursively(obj);
262 _objs = g_slist_remove(_objs, obj);
263 }
265 void Selection::setList(GSList const *list) {
266 _clear();
268 if ( list != NULL ) {
269 for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
270 _add(reinterpret_cast<SPObject *>(iter->data));
271 }
272 }
274 _emitChanged();
275 }
277 void Selection::addList(GSList const *list) {
279 if (list == NULL)
280 return;
282 _invalidateCachedLists();
284 for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
285 SPObject *obj = reinterpret_cast<SPObject *>(iter->data);
286 if (includes(obj)) {
287 continue;
288 }
289 _add (obj);
290 }
292 _emitChanged();
293 }
295 void Selection::setReprList(GSList const *list) {
296 _clear();
298 for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
299 SPObject *obj=_objectForXMLNode(reinterpret_cast<Inkscape::XML::Node *>(iter->data));
300 if (obj) {
301 _add(obj);
302 }
303 }
305 _emitChanged();
306 }
308 void Selection::clear() {
309 _clear();
310 _emitChanged();
311 }
313 GSList const *Selection::list() {
314 return _objs;
315 }
317 GSList const *Selection::itemList() {
318 if (_items) {
319 return _items;
320 }
322 for ( GSList const *iter=_objs ; iter != NULL ; iter = iter->next ) {
323 SPObject *obj=reinterpret_cast<SPObject *>(iter->data);
324 if (SP_IS_ITEM(obj)) {
325 _items = g_slist_prepend(_items, SP_ITEM(obj));
326 }
327 }
328 _items = g_slist_reverse(_items);
330 return _items;
331 }
333 GSList const *Selection::reprList() {
334 if (_reprs) { return _reprs; }
336 for ( GSList const *iter=itemList() ; iter != NULL ; iter = iter->next ) {
337 SPObject *obj=reinterpret_cast<SPObject *>(iter->data);
338 _reprs = g_slist_prepend(_reprs, SP_OBJECT_REPR(obj));
339 }
340 _reprs = g_slist_reverse(_reprs);
342 return _reprs;
343 }
345 std::list<Persp3D *> const Selection::perspList() {
346 std::list<Persp3D *> pl;
347 for (std::map<Persp3D *, unsigned int>::iterator p = _persps.begin(); p != _persps.end(); ++p) {
348 pl.push_back((*p).first);
349 }
350 return pl;
351 }
353 std::list<SPBox3D *> const Selection::box3DList() {
354 return _3dboxes;
355 }
357 SPObject *Selection::single() {
358 if ( _objs != NULL && _objs->next == NULL ) {
359 return reinterpret_cast<SPObject *>(_objs->data);
360 } else {
361 return NULL;
362 }
363 }
365 SPItem *Selection::singleItem() {
366 GSList const *items=itemList();
367 if ( items != NULL && items->next == NULL ) {
368 return reinterpret_cast<SPItem *>(items->data);
369 } else {
370 return NULL;
371 }
372 }
374 Inkscape::XML::Node *Selection::singleRepr() {
375 SPObject *obj=single();
376 return obj ? SP_OBJECT_REPR(obj) : NULL;
377 }
379 NRRect *Selection::bounds(NRRect *bbox, SPItem::BBoxType type) const
380 {
381 g_return_val_if_fail (bbox != NULL, NULL);
382 bounds(bbox, type);
383 return bbox;
384 }
386 boost::optional<Geom::Rect> Selection::bounds(SPItem::BBoxType type) const
387 {
388 GSList const *items = const_cast<Selection *>(this)->itemList();
390 boost::optional<Geom::Rect> bbox;
391 for ( GSList const *i = items ; i != NULL ; i = i->next ) {
392 bbox = unify(bbox, sp_item_bbox_desktop(SP_ITEM(i->data), type));
393 }
394 return bbox;
395 }
397 NRRect *Selection::boundsInDocument(NRRect *bbox, SPItem::BBoxType type) const {
398 g_return_val_if_fail (bbox != NULL, NULL);
400 GSList const *items=const_cast<Selection *>(this)->itemList();
401 if (!items) {
402 bbox->x0 = bbox->y0 = bbox->x1 = bbox->y1 = 0.0;
403 return bbox;
404 }
406 bbox->x0 = bbox->y0 = 1e18;
407 bbox->x1 = bbox->y1 = -1e18;
409 for ( GSList const *iter=items ; iter != NULL ; iter = iter->next ) {
410 SPItem *item=SP_ITEM(iter->data);
411 Geom::Matrix i2doc(sp_item_i2doc_affine(item));
412 sp_item_invoke_bbox(item, bbox, i2doc, FALSE, type);
413 }
415 return bbox;
416 }
418 boost::optional<Geom::Rect> Selection::boundsInDocument(SPItem::BBoxType type) const {
419 NRRect r;
420 return to_2geom(boundsInDocument(&r, type)->upgrade());
421 }
423 /** Extract the position of the center from the first selected object */
424 boost::optional<Geom::Point> Selection::center() const {
425 GSList *items = (GSList *) const_cast<Selection *>(this)->itemList();
426 Geom::Point center;
427 if (items) {
428 SPItem *first = reinterpret_cast<SPItem*>(g_slist_last(items)->data); // from the first item in selection
429 if (first->isCenterSet()) { // only if set explicitly
430 return first->getCenter();
431 }
432 }
433 boost::optional<Geom::Rect> bbox = bounds();
434 if (bbox) {
435 return bounds()->midpoint();
436 } else {
437 return boost::optional<Geom::Point>();
438 }
439 }
441 /**
442 * Compute the list of points in the selection that are to be considered for snapping.
443 */
444 std::vector<Geom::Point> Selection::getSnapPoints(bool includeItemCenter) const {
445 GSList const *items = const_cast<Selection *>(this)->itemList();
446 std::vector<Geom::Point> p;
447 for (GSList const *iter = items; iter != NULL; iter = iter->next) {
448 SPItem *this_item = SP_ITEM(iter->data);
449 sp_item_snappoints(this_item, false, SnapPointsIter(p));
450 //Include the transformation origin for snapping
451 //For a group only the group's origin is considered
452 if (includeItemCenter) {
453 p.push_back(this_item->getCenter());
454 }
455 }
457 return p;
458 }
460 std::vector<Geom::Point> Selection::getSnapPointsConvexHull() const {
461 GSList const *items = const_cast<Selection *>(this)->itemList();
463 std::vector<Geom::Point> p;
464 for (GSList const *iter = items; iter != NULL; iter = iter->next) {
465 sp_item_snappoints(SP_ITEM(iter->data), false, SnapPointsIter(p));
466 }
468 std::vector<Geom::Point> pHull;
469 if (!p.empty()) {
470 std::vector<Geom::Point>::iterator i;
471 Geom::RectHull cvh(p.front());
472 for (i = p.begin(); i != p.end(); i++) {
473 // these are the points we get back
474 cvh.add(*i);
475 }
477 boost::optional<Geom::Rect> rHull = cvh.bounds();
478 if (rHull) {
479 for ( unsigned i = 0 ; i < 4 ; ++i ) {
480 pHull.push_back(rHull->corner(i));
481 }
482 }
483 }
485 return pHull;
486 }
488 void Selection::_removeObjectDescendants(SPObject *obj) {
489 GSList *iter, *next;
490 for ( iter = _objs ; iter ; iter = next ) {
491 next = iter->next;
492 SPObject *sel_obj=reinterpret_cast<SPObject *>(iter->data);
493 SPObject *parent=SP_OBJECT_PARENT(sel_obj);
494 while (parent) {
495 if ( parent == obj ) {
496 _remove(sel_obj);
497 break;
498 }
499 parent = SP_OBJECT_PARENT(parent);
500 }
501 }
502 }
504 void Selection::_removeObjectAncestors(SPObject *obj) {
505 SPObject *parent=SP_OBJECT_PARENT(obj);
506 while (parent) {
507 if (includes(parent)) {
508 _remove(parent);
509 }
510 parent = SP_OBJECT_PARENT(parent);
511 }
512 }
514 SPObject *Selection::_objectForXMLNode(Inkscape::XML::Node *repr) const {
515 g_return_val_if_fail(repr != NULL, NULL);
516 gchar const *id = repr->attribute("id");
517 g_return_val_if_fail(id != NULL, NULL);
518 SPObject *object=sp_desktop_document(_desktop)->getObjectById(id);
519 g_return_val_if_fail(object != NULL, NULL);
520 return object;
521 }
523 guint Selection::numberOfLayers() {
524 GSList const *items = const_cast<Selection *>(this)->itemList();
525 GSList *layers = NULL;
526 for (GSList const *iter = items; iter != NULL; iter = iter->next) {
527 SPObject *layer = desktop()->layerForObject(SP_OBJECT(iter->data));
528 if (g_slist_find (layers, layer) == NULL) {
529 layers = g_slist_prepend (layers, layer);
530 }
531 }
532 guint ret = g_slist_length (layers);
533 g_slist_free (layers);
534 return ret;
535 }
537 guint Selection::numberOfParents() {
538 GSList const *items = const_cast<Selection *>(this)->itemList();
539 GSList *parents = NULL;
540 for (GSList const *iter = items; iter != NULL; iter = iter->next) {
541 SPObject *parent = SP_OBJECT_PARENT(iter->data);
542 if (g_slist_find (parents, parent) == NULL) {
543 parents = g_slist_prepend (parents, parent);
544 }
545 }
546 guint ret = g_slist_length (parents);
547 g_slist_free (parents);
548 return ret;
549 }
551 }
553 /*
554 Local Variables:
555 mode:c++
556 c-file-style:"stroustrup"
557 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
558 indent-tabs-mode:nil
559 fill-column:99
560 End:
561 */
562 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :