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_3D_boxes_recursively(SPObject *obj) {
166 std::list<SPBox3D *> boxes = box3d_extract_boxes(obj);
168 for (std::list<SPBox3D *>::iterator i = boxes.begin(); i != boxes.end(); ++i) {
169 SPBox3D *box = *i;
170 _3dboxes.push_back(box);
171 }
172 }
174 void Selection::_add(SPObject *obj) {
175 // unselect any of the item's ancestors and descendants which may be selected
176 // (to prevent double-selection)
177 _removeObjectDescendants(obj);
178 _removeObjectAncestors(obj);
180 _objs = g_slist_prepend(_objs, obj);
182 add_3D_boxes_recursively(obj);
184 _release_connections[obj] = obj->connectRelease(sigc::mem_fun(*this, (void (Selection::*)(SPObject *))&Selection::remove));
185 _modified_connections[obj] = obj->connectModified(sigc::mem_fun(*this, &Selection::_schedule_modified));
186 }
188 void Selection::set(SPObject *object, bool persist_selection_context) {
189 _clear();
190 add(object, persist_selection_context);
191 }
193 void Selection::toggle(SPObject *obj) {
194 if (includes (obj)) {
195 remove (obj);
196 } else {
197 add(obj);
198 }
199 }
201 void Selection::remove(SPObject *obj) {
202 g_return_if_fail(obj != NULL);
203 g_return_if_fail(SP_IS_OBJECT(obj));
204 g_return_if_fail(includes(obj));
206 _invalidateCachedLists();
207 _remove(obj);
208 _emitChanged();
209 }
211 void Selection::remove_3D_boxes_recursively(SPObject *obj) {
212 std::list<SPBox3D *> boxes = box3d_extract_boxes(obj);
214 for (std::list<SPBox3D *>::iterator i = boxes.begin(); i != boxes.end(); ++i) {
215 SPBox3D *box = *i;
216 std::list<SPBox3D *>::iterator b = std::find(_3dboxes.begin(), _3dboxes.end(), box);
217 if (b == _3dboxes.end()) {
218 g_print ("Warning! Trying to remove unselected box from selection.\n");
219 return;
220 }
221 _3dboxes.erase(b);
222 }
223 }
225 void Selection::_remove(SPObject *obj) {
226 _modified_connections[obj].disconnect();
227 _modified_connections.erase(obj);
229 _release_connections[obj].disconnect();
230 _release_connections.erase(obj);
232 remove_3D_boxes_recursively(obj);
234 _objs = g_slist_remove(_objs, obj);
235 }
237 void Selection::setList(GSList const *list) {
238 _clear();
240 if ( list != NULL ) {
241 for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
242 _add(reinterpret_cast<SPObject *>(iter->data));
243 }
244 }
246 _emitChanged();
247 }
249 void Selection::addList(GSList const *list) {
251 if (list == NULL)
252 return;
254 _invalidateCachedLists();
256 for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
257 SPObject *obj = reinterpret_cast<SPObject *>(iter->data);
258 if (includes(obj)) {
259 continue;
260 }
261 _add (obj);
262 }
264 _emitChanged();
265 }
267 void Selection::setReprList(GSList const *list) {
268 _clear();
270 for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
271 SPObject *obj=_objectForXMLNode(reinterpret_cast<Inkscape::XML::Node *>(iter->data));
272 if (obj) {
273 _add(obj);
274 }
275 }
277 _emitChanged();
278 }
280 void Selection::clear() {
281 _clear();
282 _emitChanged();
283 }
285 GSList const *Selection::list() {
286 return _objs;
287 }
289 GSList const *Selection::itemList() {
290 if (_items) {
291 return _items;
292 }
294 for ( GSList const *iter=_objs ; iter != NULL ; iter = iter->next ) {
295 SPObject *obj=reinterpret_cast<SPObject *>(iter->data);
296 if (SP_IS_ITEM(obj)) {
297 _items = g_slist_prepend(_items, SP_ITEM(obj));
298 }
299 }
300 _items = g_slist_reverse(_items);
302 return _items;
303 }
305 GSList const *Selection::reprList() {
306 if (_reprs) { return _reprs; }
308 for ( GSList const *iter=itemList() ; iter != NULL ; iter = iter->next ) {
309 SPObject *obj=reinterpret_cast<SPObject *>(iter->data);
310 _reprs = g_slist_prepend(_reprs, SP_OBJECT_REPR(obj));
311 }
312 _reprs = g_slist_reverse(_reprs);
314 return _reprs;
315 }
317 std::list<Persp3D *> const Selection::perspList() {
318 std::list<Persp3D *> pl;
319 for (std::list<SPBox3D *>::iterator i = _3dboxes.begin(); i != _3dboxes.end(); ++i) {
320 Persp3D *persp = box3d_get_perspective(*i);
321 if (std::find(pl.begin(), pl.end(), persp) == pl.end())
322 pl.push_back(persp);
323 }
324 return pl;
325 }
327 std::list<SPBox3D *> const Selection::box3DList(Persp3D *persp) {
328 std::list<SPBox3D *> boxes;
329 if (persp) {
330 SPBox3D *box;
331 for (std::list<SPBox3D *>::iterator i = _3dboxes.begin(); i != _3dboxes.end(); ++i) {
332 box = *i;
333 if (persp == box3d_get_perspective(box))
334 boxes.push_back(box);
335 }
336 } else {
337 boxes = _3dboxes;
338 }
339 return boxes;
340 }
342 SPObject *Selection::single() {
343 if ( _objs != NULL && _objs->next == NULL ) {
344 return reinterpret_cast<SPObject *>(_objs->data);
345 } else {
346 return NULL;
347 }
348 }
350 SPItem *Selection::singleItem() {
351 GSList const *items=itemList();
352 if ( items != NULL && items->next == NULL ) {
353 return reinterpret_cast<SPItem *>(items->data);
354 } else {
355 return NULL;
356 }
357 }
359 Inkscape::XML::Node *Selection::singleRepr() {
360 SPObject *obj=single();
361 return obj ? SP_OBJECT_REPR(obj) : NULL;
362 }
364 NRRect *Selection::bounds(NRRect *bbox, SPItem::BBoxType type) const
365 {
366 g_return_val_if_fail (bbox != NULL, NULL);
367 *bbox = NRRect(bounds(type));
368 return bbox;
369 }
371 Geom::OptRect Selection::bounds(SPItem::BBoxType type) const
372 {
373 GSList const *items = const_cast<Selection *>(this)->itemList();
375 Geom::OptRect bbox;
376 for ( GSList const *i = items ; i != NULL ; i = i->next ) {
377 bbox = unify(bbox, sp_item_bbox_desktop(SP_ITEM(i->data), type));
378 }
379 return bbox;
380 }
382 NRRect *Selection::boundsInDocument(NRRect *bbox, SPItem::BBoxType type) const {
383 g_return_val_if_fail (bbox != NULL, NULL);
385 GSList const *items=const_cast<Selection *>(this)->itemList();
386 if (!items) {
387 bbox->x0 = bbox->y0 = bbox->x1 = bbox->y1 = 0.0;
388 return bbox;
389 }
391 bbox->x0 = bbox->y0 = 1e18;
392 bbox->x1 = bbox->y1 = -1e18;
394 for ( GSList const *iter=items ; iter != NULL ; iter = iter->next ) {
395 SPItem *item=SP_ITEM(iter->data);
396 Geom::Matrix i2doc(sp_item_i2doc_affine(item));
397 sp_item_invoke_bbox(item, bbox, i2doc, FALSE, type);
398 }
400 return bbox;
401 }
403 Geom::OptRect Selection::boundsInDocument(SPItem::BBoxType type) const {
404 NRRect r;
405 return to_2geom(boundsInDocument(&r, type)->upgrade());
406 }
408 /** Extract the position of the center from the first selected object */
409 boost::optional<Geom::Point> Selection::center() const {
410 GSList *items = (GSList *) const_cast<Selection *>(this)->itemList();
411 Geom::Point center;
412 if (items) {
413 SPItem *first = reinterpret_cast<SPItem*>(g_slist_last(items)->data); // from the first item in selection
414 if (first->isCenterSet()) { // only if set explicitly
415 return first->getCenter();
416 }
417 }
418 Geom::OptRect bbox = bounds();
419 if (bbox) {
420 return bounds()->midpoint();
421 } else {
422 return boost::optional<Geom::Point>();
423 }
424 }
426 /**
427 * Compute the list of points in the selection that are to be considered for snapping.
428 */
429 std::vector<std::pair<Geom::Point, int> > Selection::getSnapPoints(SnapPreferences const *snapprefs) const {
430 GSList const *items = const_cast<Selection *>(this)->itemList();
432 SnapPreferences snapprefs_dummy = *snapprefs; // create a local copy of the snapping prefs
433 snapprefs_dummy.setIncludeItemCenter(false); // locally disable snapping to the item center
435 std::vector<std::pair<Geom::Point, int> > p;
436 for (GSList const *iter = items; iter != NULL; iter = iter->next) {
437 SPItem *this_item = SP_ITEM(iter->data);
438 sp_item_snappoints(this_item, false, p, &snapprefs_dummy);
440 //Include the transformation origin for snapping
441 //For a selection or group only the overall origin is considered
442 if (snapprefs != NULL && snapprefs->getIncludeItemCenter()) {
443 p.push_back(std::make_pair(this_item->getCenter(), SNAPSOURCE_ROTATION_CENTER));
444 }
445 }
447 return p;
448 }
450 std::vector<std::pair<Geom::Point, int> > Selection::getSnapPointsConvexHull(SnapPreferences const *snapprefs) const {
451 GSList const *items = const_cast<Selection *>(this)->itemList();
453 std::vector<std::pair<Geom::Point, int> > p;
454 for (GSList const *iter = items; iter != NULL; iter = iter->next) {
455 sp_item_snappoints(SP_ITEM(iter->data), false, p, snapprefs);
456 }
458 std::vector<std::pair<Geom::Point, int> > pHull;
459 if (!p.empty()) {
460 std::vector<std::pair<Geom::Point, int> >::iterator i;
461 Geom::RectHull cvh((p.front()).first);
462 for (i = p.begin(); i != p.end(); i++) {
463 // these are the points we get back
464 cvh.add((*i).first);
465 }
467 Geom::OptRect rHull = cvh.bounds();
468 if (rHull) {
469 for ( unsigned i = 0 ; i < 4 ; ++i ) {
470 pHull.push_back(std::make_pair(rHull->corner(i), SNAPSOURCE_CONVEX_HULL_CORNER));
471 }
472 }
473 }
475 return pHull;
476 }
478 void Selection::_removeObjectDescendants(SPObject *obj) {
479 GSList *iter, *next;
480 for ( iter = _objs ; iter ; iter = next ) {
481 next = iter->next;
482 SPObject *sel_obj=reinterpret_cast<SPObject *>(iter->data);
483 SPObject *parent=SP_OBJECT_PARENT(sel_obj);
484 while (parent) {
485 if ( parent == obj ) {
486 _remove(sel_obj);
487 break;
488 }
489 parent = SP_OBJECT_PARENT(parent);
490 }
491 }
492 }
494 void Selection::_removeObjectAncestors(SPObject *obj) {
495 SPObject *parent=SP_OBJECT_PARENT(obj);
496 while (parent) {
497 if (includes(parent)) {
498 _remove(parent);
499 }
500 parent = SP_OBJECT_PARENT(parent);
501 }
502 }
504 SPObject *Selection::_objectForXMLNode(Inkscape::XML::Node *repr) const {
505 g_return_val_if_fail(repr != NULL, NULL);
506 gchar const *id = repr->attribute("id");
507 g_return_val_if_fail(id != NULL, NULL);
508 SPObject *object=sp_desktop_document(_desktop)->getObjectById(id);
509 g_return_val_if_fail(object != NULL, NULL);
510 return object;
511 }
513 guint Selection::numberOfLayers() {
514 GSList const *items = const_cast<Selection *>(this)->itemList();
515 GSList *layers = NULL;
516 for (GSList const *iter = items; iter != NULL; iter = iter->next) {
517 SPObject *layer = desktop()->layerForObject(SP_OBJECT(iter->data));
518 if (g_slist_find (layers, layer) == NULL) {
519 layers = g_slist_prepend (layers, layer);
520 }
521 }
522 guint ret = g_slist_length (layers);
523 g_slist_free (layers);
524 return ret;
525 }
527 guint Selection::numberOfParents() {
528 GSList const *items = const_cast<Selection *>(this)->itemList();
529 GSList *parents = NULL;
530 for (GSList const *iter = items; iter != NULL; iter = iter->next) {
531 SPObject *parent = SP_OBJECT_PARENT(iter->data);
532 if (g_slist_find (parents, parent) == NULL) {
533 parents = g_slist_prepend (parents, parent);
534 }
535 }
536 guint ret = g_slist_length (parents);
537 g_slist_free (parents);
538 return ret;
539 }
541 }
543 /*
544 Local Variables:
545 mode:c++
546 c-file-style:"stroustrup"
547 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
548 indent-tabs-mode:nil
549 fill-column:99
550 End:
551 */
552 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :