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"
31 #include <sigc++/functors/mem_fun.h>
33 #define SP_SELECTION_UPDATE_PRIORITY (G_PRIORITY_HIGH_IDLE + 1)
35 namespace Inkscape {
37 Selection::Selection(SPDesktop *desktop) :
38 _objs(NULL),
39 _reprs(NULL),
40 _items(NULL),
41 _desktop(desktop),
42 _selection_context(NULL),
43 _flags(0),
44 _idle(0)
45 {
46 }
48 Selection::~Selection() {
49 _clear();
50 _desktop = NULL;
51 if (_idle) {
52 g_source_remove(_idle);
53 _idle = 0;
54 }
55 }
57 /* Handler for selected objects "modified" signal */
59 void Selection::_schedule_modified(SPObject *obj, guint flags) {
60 if (!this->_idle) {
61 /* Request handling to be run in _idle loop */
62 this->_idle = g_idle_add_full(SP_SELECTION_UPDATE_PRIORITY, GSourceFunc(&Selection::_emit_modified), this, NULL);
63 }
65 /* Collect all flags */
66 this->_flags |= flags;
67 }
69 gboolean
70 Selection::_emit_modified(Selection *selection)
71 {
72 /* force new handler to be created if requested before we return */
73 selection->_idle = 0;
74 guint flags = selection->_flags;
75 selection->_flags = 0;
77 selection->_emitModified(flags);
79 /* drop this handler */
80 return FALSE;
81 }
83 void Selection::_emitModified(guint flags) {
84 inkscape_selection_modified(this, flags);
85 _modified_signal.emit(this, flags);
86 }
88 void Selection::_emitChanged(bool persist_selection_context/* = false */) {
89 if (persist_selection_context) {
90 if (NULL == _selection_context) {
91 _selection_context = desktop()->currentLayer();
92 sp_object_ref(_selection_context, NULL);
93 _context_release_connection = _selection_context->connectRelease(sigc::mem_fun(*this, &Selection::_releaseContext));
94 }
95 } else {
96 _releaseContext(_selection_context);
97 }
99 inkscape_selection_changed(this);
100 _changed_signal.emit(this);
101 }
103 void
104 Selection::_releaseContext(SPObject *obj)
105 {
106 if (NULL == _selection_context || _selection_context != obj)
107 return;
109 _context_release_connection.disconnect();
111 sp_object_unref(_selection_context, NULL);
112 _selection_context = NULL;
113 }
115 void Selection::_invalidateCachedLists() {
116 g_slist_free(_items);
117 _items = NULL;
119 g_slist_free(_reprs);
120 _reprs = NULL;
121 }
123 void Selection::_clear() {
124 _invalidateCachedLists();
125 while (_objs) {
126 SPObject *obj=reinterpret_cast<SPObject *>(_objs->data);
127 _remove(obj);
128 }
129 }
131 SPObject *Selection::activeContext() {
132 if (NULL != _selection_context)
133 return _selection_context;
134 return desktop()->currentLayer();
135 }
137 bool Selection::includes(SPObject *obj) const {
138 if (obj == NULL)
139 return FALSE;
141 g_return_val_if_fail(SP_IS_OBJECT(obj), FALSE);
143 return ( g_slist_find(_objs, obj) != NULL );
144 }
146 void Selection::add(SPObject *obj, bool persist_selection_context/* = false */) {
147 g_return_if_fail(obj != NULL);
148 g_return_if_fail(SP_IS_OBJECT(obj));
150 if (includes(obj)) {
151 return;
152 }
154 _invalidateCachedLists();
155 _add(obj);
156 _emitChanged(persist_selection_context);
157 }
159 void Selection::_add(SPObject *obj) {
160 // unselect any of the item's ancestors and descendants which may be selected
161 // (to prevent double-selection)
162 _removeObjectDescendants(obj);
163 _removeObjectAncestors(obj);
165 _objs = g_slist_prepend(_objs, obj);
167 _release_connections[obj] = obj->connectRelease(sigc::mem_fun(*this, (void (Selection::*)(SPObject *))&Selection::remove));
168 _modified_connections[obj] = obj->connectModified(sigc::mem_fun(*this, &Selection::_schedule_modified));
169 }
171 void Selection::set(SPObject *object, bool persist_selection_context) {
172 _clear();
173 add(object, persist_selection_context);
174 }
176 void Selection::toggle(SPObject *obj) {
177 if (includes (obj)) {
178 remove (obj);
179 } else {
180 add(obj);
181 }
182 }
184 void Selection::remove(SPObject *obj) {
185 g_return_if_fail(obj != NULL);
186 g_return_if_fail(SP_IS_OBJECT(obj));
187 g_return_if_fail(includes(obj));
189 _invalidateCachedLists();
190 _remove(obj);
191 _emitChanged();
192 }
194 void Selection::_remove(SPObject *obj) {
195 _modified_connections[obj].disconnect();
196 _modified_connections.erase(obj);
198 _release_connections[obj].disconnect();
199 _release_connections.erase(obj);
201 _objs = g_slist_remove(_objs, obj);
202 }
204 void Selection::setList(GSList const *list) {
205 _clear();
207 if ( list != NULL ) {
208 for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
209 _add(reinterpret_cast<SPObject *>(iter->data));
210 }
211 }
213 _emitChanged();
214 }
216 void Selection::addList(GSList const *list) {
218 if (list == NULL)
219 return;
221 _invalidateCachedLists();
223 for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
224 SPObject *obj = reinterpret_cast<SPObject *>(iter->data);
225 if (includes(obj)) {
226 continue;
227 }
228 _add (obj);
229 }
231 _emitChanged();
232 }
234 void Selection::setReprList(GSList const *list) {
235 _clear();
237 for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
238 SPObject *obj=_objectForXMLNode(reinterpret_cast<Inkscape::XML::Node *>(iter->data));
239 if (obj) {
240 _add(obj);
241 }
242 }
244 _emitChanged();
245 }
247 void Selection::clear() {
248 _clear();
249 _emitChanged();
250 }
252 GSList const *Selection::list() {
253 return _objs;
254 }
256 GSList const *Selection::itemList() {
257 if (_items) {
258 return _items;
259 }
261 for ( GSList const *iter=_objs ; iter != NULL ; iter = iter->next ) {
262 SPObject *obj=reinterpret_cast<SPObject *>(iter->data);
263 if (SP_IS_ITEM(obj)) {
264 _items = g_slist_prepend(_items, SP_ITEM(obj));
265 }
266 }
267 _items = g_slist_reverse(_items);
269 return _items;
270 }
272 GSList const *Selection::reprList() {
273 if (_reprs) { return _reprs; }
275 for ( GSList const *iter=itemList() ; iter != NULL ; iter = iter->next ) {
276 SPObject *obj=reinterpret_cast<SPObject *>(iter->data);
277 _reprs = g_slist_prepend(_reprs, SP_OBJECT_REPR(obj));
278 }
279 _reprs = g_slist_reverse(_reprs);
281 return _reprs;
282 }
284 SPObject *Selection::single() {
285 if ( _objs != NULL && _objs->next == NULL ) {
286 return reinterpret_cast<SPObject *>(_objs->data);
287 } else {
288 return NULL;
289 }
290 }
292 SPItem *Selection::singleItem() {
293 GSList const *items=itemList();
294 if ( items != NULL && items->next == NULL ) {
295 return reinterpret_cast<SPItem *>(items->data);
296 } else {
297 return NULL;
298 }
299 }
301 Inkscape::XML::Node *Selection::singleRepr() {
302 SPObject *obj=single();
303 return obj ? SP_OBJECT_REPR(obj) : NULL;
304 }
306 NRRect *Selection::bounds(NRRect *bbox) const
307 {
308 g_return_val_if_fail (bbox != NULL, NULL);
309 NR::Rect const b = bounds();
310 bbox->x0 = b.min()[NR::X];
311 bbox->y0 = b.min()[NR::Y];
312 bbox->x1 = b.max()[NR::X];
313 bbox->y1 = b.max()[NR::Y];
314 return bbox;
315 }
317 NR::Rect Selection::bounds() const
318 {
319 GSList const *items = const_cast<Selection *>(this)->itemList();
321 NR::Maybe<NR::Rect> bbox = NR::Nothing();
322 for ( GSList const *i = items ; i != NULL ; i = i->next ) {
323 bbox = NR::Rect::union_bounds(bbox, sp_item_bbox_desktop(SP_ITEM(i->data)));
324 }
326 // TODO: return NR::Maybe<NR::Rect>
327 if (bbox) {
328 return *bbox;
329 } else {
330 return NR::Rect(NR::Point(0, 0), NR::Point(0, 0));
331 }
332 }
334 NRRect *Selection::boundsInDocument(NRRect *bbox) const {
335 g_return_val_if_fail (bbox != NULL, NULL);
337 GSList const *items=const_cast<Selection *>(this)->itemList();
338 if (!items) {
339 bbox->x0 = bbox->y0 = bbox->x1 = bbox->y1 = 0.0;
340 return bbox;
341 }
343 bbox->x0 = bbox->y0 = 1e18;
344 bbox->x1 = bbox->y1 = -1e18;
346 for ( GSList const *iter=items ; iter != NULL ; iter = iter->next ) {
347 SPItem *item=SP_ITEM(iter->data);
348 NR::Matrix const i2doc(sp_item_i2doc_affine(item));
349 sp_item_invoke_bbox(item, bbox, i2doc, FALSE);
350 }
352 return bbox;
353 }
355 NR::Rect Selection::boundsInDocument() const {
356 NRRect r;
357 NR::Maybe<NR::Rect> rect(boundsInDocument(&r)->upgrade());
358 if (rect) {
359 return *rect;
360 } else {
361 // FIXME
362 return NR::Rect(NR::Point(0, 0), NR::Point(0, 0));
363 }
364 }
366 /** Extract the position of the center from the first selected object */
367 NR::Point Selection::center() const {
368 GSList *items = (GSList *) const_cast<Selection *>(this)->itemList();
369 NR::Point center;
370 if (items) {
371 SPItem *first = reinterpret_cast<SPItem*>(g_slist_last(items)->data); // from the first item in selection
372 if (first->isCenterSet()) { // only if set explicitly
373 center = first->getCenter();
374 } else {
375 center = bounds().midpoint();
376 }
377 } else {
378 center = bounds().midpoint();
379 }
380 return center;
381 }
383 /**
384 * Compute the list of points in the selection that are to be considered for snapping.
385 */
386 std::vector<NR::Point> Selection::getSnapPoints() const {
387 GSList const *items = const_cast<Selection *>(this)->itemList();
388 std::vector<NR::Point> p;
389 for (GSList const *iter = items; iter != NULL; iter = iter->next) {
390 sp_item_snappoints(SP_ITEM(iter->data), SnapPointsIter(p));
391 }
393 return p;
394 }
396 std::vector<NR::Point> Selection::getSnapPointsConvexHull() const {
397 GSList const *items = const_cast<Selection *>(this)->itemList();
398 std::vector<NR::Point> p;
399 for (GSList const *iter = items; iter != NULL; iter = iter->next) {
400 sp_item_snappoints(SP_ITEM(iter->data), SnapPointsIter(p));
401 }
403 std::vector<NR::Point>::iterator i;
404 NR::ConvexHull cvh(*(p.begin()));
405 for (i = p.begin(); i != p.end(); i++) {
406 // these are the points we get back
407 cvh.add(*i);
408 }
410 NR::Rect rHull = cvh.bounds();
411 std::vector<NR::Point> pHull(4);
412 pHull[0] = rHull.corner(0);
413 pHull[1] = rHull.corner(1);
414 pHull[2] = rHull.corner(2);
415 pHull[3] = rHull.corner(3);
417 return pHull;
418 }
420 std::vector<NR::Point> Selection::getBBoxPoints() const {
421 GSList const *items = const_cast<Selection *>(this)->itemList();
422 std::vector<NR::Point> p;
423 for (GSList const *iter = items; iter != NULL; iter = iter->next) {
424 NR::Rect b = sp_item_bbox_desktop(SP_ITEM(iter->data));
425 p.push_back(b.min());
426 p.push_back(b.max());
427 }
429 return p;
430 }
432 std::vector<NR::Point> Selection::getBBoxPointsOuter() const {
433 std::vector<NR::Point> p;
434 NR::Rect bbox = bounds();
435 p.push_back(bbox.min());
436 p.push_back(bbox.max());
437 return p;
438 }
440 void Selection::_removeObjectDescendants(SPObject *obj) {
441 GSList *iter, *next;
442 for ( iter = _objs ; iter ; iter = next ) {
443 next = iter->next;
444 SPObject *sel_obj=reinterpret_cast<SPObject *>(iter->data);
445 SPObject *parent=SP_OBJECT_PARENT(sel_obj);
446 while (parent) {
447 if ( parent == obj ) {
448 _remove(sel_obj);
449 break;
450 }
451 parent = SP_OBJECT_PARENT(parent);
452 }
453 }
454 }
456 void Selection::_removeObjectAncestors(SPObject *obj) {
457 SPObject *parent=SP_OBJECT_PARENT(obj);
458 while (parent) {
459 if (includes(parent)) {
460 _remove(parent);
461 }
462 parent = SP_OBJECT_PARENT(parent);
463 }
464 }
466 SPObject *Selection::_objectForXMLNode(Inkscape::XML::Node *repr) const {
467 g_return_val_if_fail(repr != NULL, NULL);
468 gchar const *id = repr->attribute("id");
469 g_return_val_if_fail(id != NULL, NULL);
470 SPObject *object=sp_desktop_document(_desktop)->getObjectById(id);
471 g_return_val_if_fail(object != NULL, NULL);
472 return object;
473 }
475 guint Selection::numberOfLayers() {
476 GSList const *items = const_cast<Selection *>(this)->itemList();
477 GSList *layers = NULL;
478 for (GSList const *iter = items; iter != NULL; iter = iter->next) {
479 SPObject *layer = desktop()->layerForObject(SP_OBJECT(iter->data));
480 if (g_slist_find (layers, layer) == NULL) {
481 layers = g_slist_prepend (layers, layer);
482 }
483 }
484 guint ret = g_slist_length (layers);
485 g_slist_free (layers);
486 return ret;
487 }
489 guint Selection::numberOfParents() {
490 GSList const *items = const_cast<Selection *>(this)->itemList();
491 GSList *parents = NULL;
492 for (GSList const *iter = items; iter != NULL; iter = iter->next) {
493 SPObject *parent = SP_OBJECT_PARENT(iter->data);
494 if (g_slist_find (parents, parent) == NULL) {
495 parents = g_slist_prepend (parents, parent);
496 }
497 }
498 guint ret = g_slist_length (parents);
499 g_slist_free (parents);
500 return ret;
501 }
503 }
505 /*
506 Local Variables:
507 mode:c++
508 c-file-style:"stroustrup"
509 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
510 indent-tabs-mode:nil
511 fill-column:99
512 End:
513 */
514 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :