Code

adapt code to new Maybe/bbox regime
[inkscape.git] / src / selection.cpp
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);
103 void
104 Selection::_releaseContext(SPObject *obj)
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;
115 void Selection::_invalidateCachedLists() {
116     g_slist_free(_items);
117     _items = NULL;
119     g_slist_free(_reprs);
120     _reprs = NULL;
123 void Selection::_clear() {
124     _invalidateCachedLists();
125     while (_objs) {
126         SPObject *obj=reinterpret_cast<SPObject *>(_objs->data);
127         _remove(obj);
128     }
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 );
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);
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));
171 void Selection::set(SPObject *object, bool persist_selection_context) {
172     _clear();
173     add(object, persist_selection_context);
176 void Selection::toggle(SPObject *obj) {
177     if (includes (obj)) {
178         remove (obj);
179     } else {
180         add(obj);
181     }
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();
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);
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();
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();
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();
247 void Selection::clear() {
248     _clear();
249     _emitChanged();
252 GSList const *Selection::list() {
253     return _objs;
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;
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;
284 SPObject *Selection::single() {
285     if ( _objs != NULL && _objs->next == NULL ) {
286         return reinterpret_cast<SPObject *>(_objs->data);
287     } else {
288         return NULL;
289     }
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     }
301 Inkscape::XML::Node *Selection::singleRepr() {
302     SPObject *obj=single();
303     return obj ? SP_OBJECT_REPR(obj) : NULL;
306 NRRect *Selection::bounds(NRRect *bbox) const
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;
317 NR::Rect Selection::bounds() const
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     }
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;
355 NR::Rect Selection::boundsInDocument() const {
356     NRRect r;
357     return NR::Rect(*boundsInDocument(&r));
360 /** Extract the position of the center from the first selected object */
361 NR::Point Selection::center() const {
362     GSList *items = (GSList *) const_cast<Selection *>(this)->itemList();
363     NR::Point center;
364     if (items) {
365         SPItem *first = reinterpret_cast<SPItem*>(g_slist_last(items)->data); // from the first item in selection
366         if (first->isCenterSet()) { // only if set explicitly
367             center = first->getCenter();
368         } else {
369             center = bounds().midpoint();
370         }
371     } else {
372         center = bounds().midpoint();
373     }
374     return center;
377 /**
378  * Compute the list of points in the selection that are to be considered for snapping.
379  */
380 std::vector<NR::Point> Selection::getSnapPoints() const {
381     GSList const *items = const_cast<Selection *>(this)->itemList();
382     std::vector<NR::Point> p;
383     for (GSList const *iter = items; iter != NULL; iter = iter->next) {
384         sp_item_snappoints(SP_ITEM(iter->data), SnapPointsIter(p));
385     }
387     return p;
390 std::vector<NR::Point> Selection::getSnapPointsConvexHull() const {
391     GSList const *items = const_cast<Selection *>(this)->itemList();
392     std::vector<NR::Point> p;
393     for (GSList const *iter = items; iter != NULL; iter = iter->next) {
394         sp_item_snappoints(SP_ITEM(iter->data), SnapPointsIter(p));
395     }
397     std::vector<NR::Point>::iterator i;
398     NR::ConvexHull cvh(*(p.begin()));
399     for (i = p.begin(); i != p.end(); i++) {
400         // these are the points we get back
401         cvh.add(*i);
402     }
404     NR::Rect rHull = cvh.bounds();
405     std::vector<NR::Point> pHull(4);
406     pHull[0] = rHull.corner(0);
407     pHull[1] = rHull.corner(1);
408     pHull[2] = rHull.corner(2);
409     pHull[3] = rHull.corner(3);
411     return pHull;
414 std::vector<NR::Point> Selection::getBBoxPoints() const {
415     GSList const *items = const_cast<Selection *>(this)->itemList();
416     std::vector<NR::Point> p;
417     for (GSList const *iter = items; iter != NULL; iter = iter->next) {
418         NR::Rect b = sp_item_bbox_desktop(SP_ITEM(iter->data));
419         p.push_back(b.min());
420         p.push_back(b.max());
421     }
423     return p;
426 std::vector<NR::Point> Selection::getBBoxPointsOuter() const {
427     std::vector<NR::Point> p;
428     NR::Rect bbox = bounds();
429     p.push_back(bbox.min());
430     p.push_back(bbox.max());
431     return p;
434 void Selection::_removeObjectDescendants(SPObject *obj) {
435     GSList *iter, *next;
436     for ( iter = _objs ; iter ; iter = next ) {
437         next = iter->next;
438         SPObject *sel_obj=reinterpret_cast<SPObject *>(iter->data);
439         SPObject *parent=SP_OBJECT_PARENT(sel_obj);
440         while (parent) {
441             if ( parent == obj ) {
442                 _remove(sel_obj);
443                 break;
444             }
445             parent = SP_OBJECT_PARENT(parent);
446         }
447     }
450 void Selection::_removeObjectAncestors(SPObject *obj) {
451         SPObject *parent=SP_OBJECT_PARENT(obj);
452         while (parent) {
453             if (includes(parent)) {
454                 _remove(parent);
455             }
456             parent = SP_OBJECT_PARENT(parent);
457         }
460 SPObject *Selection::_objectForXMLNode(Inkscape::XML::Node *repr) const {
461     g_return_val_if_fail(repr != NULL, NULL);
462     gchar const *id = repr->attribute("id");
463     g_return_val_if_fail(id != NULL, NULL);
464     SPObject *object=sp_desktop_document(_desktop)->getObjectById(id);
465     g_return_val_if_fail(object != NULL, NULL);
466     return object;
469 guint Selection::numberOfLayers() {
470       GSList const *items = const_cast<Selection *>(this)->itemList();
471         GSList *layers = NULL;
472         for (GSList const *iter = items; iter != NULL; iter = iter->next) {
473                 SPObject *layer = desktop()->layerForObject(SP_OBJECT(iter->data));
474                 if (g_slist_find (layers, layer) == NULL) {
475                         layers = g_slist_prepend (layers, layer);
476                 }
477         }
478         guint ret = g_slist_length (layers);
479         g_slist_free (layers);
480         return ret;
483 guint Selection::numberOfParents() {
484       GSList const *items = const_cast<Selection *>(this)->itemList();
485         GSList *parents = NULL;
486         for (GSList const *iter = items; iter != NULL; iter = iter->next) {
487                 SPObject *parent = SP_OBJECT_PARENT(iter->data);
488                 if (g_slist_find (parents, parent) == NULL) {
489                         parents = g_slist_prepend (parents, parent);
490                 }
491         }
492         guint ret = g_slist_length (parents);
493         g_slist_free (parents);
494         return ret;
499 /*
500   Local Variables:
501   mode:c++
502   c-file-style:"stroustrup"
503   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
504   indent-tabs-mode:nil
505   fill-column:99
506   End:
507 */
508 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :