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();
320 if (!items) {
321 return NR::Rect(NR::Point(0, 0), NR::Point(0, 0));
322 }
324 GSList const *i = items;
325 NR::Rect bbox = sp_item_bbox_desktop(SP_ITEM(i->data));
327 GSList const *i_start = i;
328 while (i != NULL) {
329 if (i != i_start)
330 bbox = NR::Rect::union_bounds(bbox, sp_item_bbox_desktop(SP_ITEM(i->data)));
331 i = i->next;
332 }
334 return bbox;
335 }
337 NRRect *Selection::boundsInDocument(NRRect *bbox) const {
338 g_return_val_if_fail (bbox != NULL, NULL);
340 GSList const *items=const_cast<Selection *>(this)->itemList();
341 if (!items) {
342 bbox->x0 = bbox->y0 = bbox->x1 = bbox->y1 = 0.0;
343 return bbox;
344 }
346 bbox->x0 = bbox->y0 = 1e18;
347 bbox->x1 = bbox->y1 = -1e18;
349 for ( GSList const *iter=items ; iter != NULL ; iter = iter->next ) {
350 SPItem *item=SP_ITEM(iter->data);
351 NR::Matrix const i2doc(sp_item_i2doc_affine(item));
352 sp_item_invoke_bbox(item, bbox, i2doc, FALSE);
353 }
355 return bbox;
356 }
358 NR::Rect Selection::boundsInDocument() const {
359 NRRect r;
360 return NR::Rect(*boundsInDocument(&r));
361 }
363 /** Extract the position of the center from the first selected object */
364 NR::Point Selection::center() const {
365 GSList *items = (GSList *) const_cast<Selection *>(this)->itemList();
366 NR::Point center;
367 if (items) {
368 SPItem *first = reinterpret_cast<SPItem*>(g_slist_last(items)->data); // from the first item in selection
369 if (first->isCenterSet()) { // only if set explicitly
370 center = first->getCenter();
371 } else {
372 center = bounds().midpoint();
373 }
374 } else {
375 center = bounds().midpoint();
376 }
377 return center;
378 }
380 /**
381 * Compute the list of points in the selection that are to be considered for snapping.
382 */
383 std::vector<NR::Point> Selection::getSnapPoints() const {
384 GSList const *items = const_cast<Selection *>(this)->itemList();
385 std::vector<NR::Point> p;
386 for (GSList const *iter = items; iter != NULL; iter = iter->next) {
387 sp_item_snappoints(SP_ITEM(iter->data), SnapPointsIter(p));
388 }
390 return p;
391 }
393 std::vector<NR::Point> Selection::getSnapPointsConvexHull() const {
394 GSList const *items = const_cast<Selection *>(this)->itemList();
395 std::vector<NR::Point> p;
396 for (GSList const *iter = items; iter != NULL; iter = iter->next) {
397 sp_item_snappoints(SP_ITEM(iter->data), SnapPointsIter(p));
398 }
400 std::vector<NR::Point>::iterator i;
401 NR::ConvexHull cvh(*(p.begin()));
402 for (i = p.begin(); i != p.end(); i++) {
403 // these are the points we get back
404 cvh.add(*i);
405 }
407 NR::Rect rHull = cvh.bounds();
408 std::vector<NR::Point> pHull(4);
409 pHull[0] = rHull.corner(0);
410 pHull[1] = rHull.corner(1);
411 pHull[2] = rHull.corner(2);
412 pHull[3] = rHull.corner(3);
414 return pHull;
415 }
417 std::vector<NR::Point> Selection::getBBoxPoints() const {
418 GSList const *items = const_cast<Selection *>(this)->itemList();
419 std::vector<NR::Point> p;
420 for (GSList const *iter = items; iter != NULL; iter = iter->next) {
421 NR::Rect b = sp_item_bbox_desktop(SP_ITEM(iter->data));
422 p.push_back(b.min());
423 p.push_back(b.max());
424 }
426 return p;
427 }
429 std::vector<NR::Point> Selection::getBBoxPointsOuter() const {
430 std::vector<NR::Point> p;
431 NR::Rect bbox = bounds();
432 p.push_back(bbox.min());
433 p.push_back(bbox.max());
434 return p;
435 }
437 void Selection::_removeObjectDescendants(SPObject *obj) {
438 GSList *iter, *next;
439 for ( iter = _objs ; iter ; iter = next ) {
440 next = iter->next;
441 SPObject *sel_obj=reinterpret_cast<SPObject *>(iter->data);
442 SPObject *parent=SP_OBJECT_PARENT(sel_obj);
443 while (parent) {
444 if ( parent == obj ) {
445 _remove(sel_obj);
446 break;
447 }
448 parent = SP_OBJECT_PARENT(parent);
449 }
450 }
451 }
453 void Selection::_removeObjectAncestors(SPObject *obj) {
454 SPObject *parent=SP_OBJECT_PARENT(obj);
455 while (parent) {
456 if (includes(parent)) {
457 _remove(parent);
458 }
459 parent = SP_OBJECT_PARENT(parent);
460 }
461 }
463 SPObject *Selection::_objectForXMLNode(Inkscape::XML::Node *repr) const {
464 g_return_val_if_fail(repr != NULL, NULL);
465 gchar const *id = repr->attribute("id");
466 g_return_val_if_fail(id != NULL, NULL);
467 SPObject *object=sp_desktop_document(_desktop)->getObjectById(id);
468 g_return_val_if_fail(object != NULL, NULL);
469 return object;
470 }
472 guint Selection::numberOfLayers() {
473 GSList const *items = const_cast<Selection *>(this)->itemList();
474 GSList *layers = NULL;
475 for (GSList const *iter = items; iter != NULL; iter = iter->next) {
476 SPObject *layer = desktop()->layerForObject(SP_OBJECT(iter->data));
477 if (g_slist_find (layers, layer) == NULL) {
478 layers = g_slist_prepend (layers, layer);
479 }
480 }
481 guint ret = g_slist_length (layers);
482 g_slist_free (layers);
483 return ret;
484 }
486 guint Selection::numberOfParents() {
487 GSList const *items = const_cast<Selection *>(this)->itemList();
488 GSList *parents = NULL;
489 for (GSList const *iter = items; iter != NULL; iter = iter->next) {
490 SPObject *parent = SP_OBJECT_PARENT(iter->data);
491 if (g_slist_find (parents, parent) == NULL) {
492 parents = g_slist_prepend (parents, parent);
493 }
494 }
495 guint ret = g_slist_length (parents);
496 g_slist_free (parents);
497 return ret;
498 }
500 }
502 /*
503 Local Variables:
504 mode:c++
505 c-file-style:"stroustrup"
506 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
507 indent-tabs-mode:nil
508 fill-column:99
509 End:
510 */
511 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :