Code

fix combo enum, to handle enums of all types (not only the ones that range from 0...
[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"
30 #include "sp-path.h"
31 #include "sp-item-group.h"
32 #include "box3d.h"
33 #include "box3d.h"
34 #include "persp3d.h"
36 #include <sigc++/functors/mem_fun.h>
38 #define SP_SELECTION_UPDATE_PRIORITY (G_PRIORITY_HIGH_IDLE + 1)
40 namespace Inkscape {
42 Selection::Selection(SPDesktop *desktop) :
43     _objs(NULL),
44     _reprs(NULL),
45     _items(NULL),
46     _desktop(desktop),
47     _selection_context(NULL),
48     _flags(0),
49     _idle(0)
50 {
51 }
53 Selection::~Selection() {
54     _clear();
55     _desktop = NULL;
56     if (_idle) {
57         g_source_remove(_idle);
58         _idle = 0;
59     }
60 }
62 /* Handler for selected objects "modified" signal */
64 void Selection::_schedule_modified(SPObject */*obj*/, guint flags) {
65     if (!this->_idle) {
66         /* Request handling to be run in _idle loop */
67         this->_idle = g_idle_add_full(SP_SELECTION_UPDATE_PRIORITY, GSourceFunc(&Selection::_emit_modified), this, NULL);
68     }
70     /* Collect all flags */
71     this->_flags |= flags;
72 }
74 gboolean
75 Selection::_emit_modified(Selection *selection)
76 {
77     /* force new handler to be created if requested before we return */
78     selection->_idle = 0;
79     guint flags = selection->_flags;
80     selection->_flags = 0;
82     selection->_emitModified(flags);
84     /* drop this handler */
85     return FALSE;
86 }
88 void Selection::_emitModified(guint flags) {
89     inkscape_selection_modified(this, flags);
90     _modified_signal.emit(this, flags);
91 }
93 void Selection::_emitChanged(bool persist_selection_context/* = false */) {
94     if (persist_selection_context) {
95         if (NULL == _selection_context) {
96             _selection_context = desktop()->currentLayer();
97             sp_object_ref(_selection_context, NULL);
98             _context_release_connection = _selection_context->connectRelease(sigc::mem_fun(*this, &Selection::_releaseContext));
99         }
100     } else {
101         _releaseContext(_selection_context);
102     }
104     inkscape_selection_changed(this);
105     _changed_signal.emit(this);
108 void
109 Selection::_releaseContext(SPObject *obj)
111     if (NULL == _selection_context || _selection_context != obj)
112         return;
114     _context_release_connection.disconnect();
116     sp_object_unref(_selection_context, NULL);
117     _selection_context = NULL;
120 void Selection::_invalidateCachedLists() {
121     g_slist_free(_items);
122     _items = NULL;
124     g_slist_free(_reprs);
125     _reprs = NULL;
128 void Selection::_clear() {
129     _invalidateCachedLists();
130     while (_objs) {
131         SPObject *obj=reinterpret_cast<SPObject *>(_objs->data);
132         _remove(obj);
133     }
136 SPObject *Selection::activeContext() {
137     if (NULL != _selection_context)
138         return _selection_context;
139     return desktop()->currentLayer();
140     }
142 bool Selection::includes(SPObject *obj) const {
143     if (obj == NULL)
144         return FALSE;
146     g_return_val_if_fail(SP_IS_OBJECT(obj), FALSE);
148     return ( g_slist_find(_objs, obj) != NULL );
151 void Selection::add(SPObject *obj, bool persist_selection_context/* = false */) {
152     g_return_if_fail(obj != NULL);
153     g_return_if_fail(SP_IS_OBJECT(obj));
155     if (includes(obj)) {
156         return;
157     }
159     _invalidateCachedLists();
160     _add(obj);
161     _emitChanged(persist_selection_context);
164 void Selection::add_box_perspective(SPBox3D *box) {
165     Persp3D *persp = box3d_get_perspective(box);
166     std::map<Persp3D *, unsigned int>::iterator p = _persps.find(persp);
167     if (p != _persps.end()) {
168         (*p).second++;
169     } else {
170         _persps[persp] = 1;
171     }
174 void Selection::add_3D_boxes_recursively(SPObject *obj) {
175     std::list<SPBox3D *> boxes = box3d_extract_boxes(obj);
177     for (std::list<SPBox3D *>::iterator i = boxes.begin(); i != boxes.end(); ++i) {
178         SPBox3D *box = *i;
179         box3d_add_to_selection(box);
180         _3dboxes.push_back(box);
181         add_box_perspective(box);
182     }
185 void Selection::_add(SPObject *obj) {
186     // unselect any of the item's ancestors and descendants which may be selected
187     // (to prevent double-selection)
188     _removeObjectDescendants(obj);
189     _removeObjectAncestors(obj);
191     _objs = g_slist_prepend(_objs, obj);
193     add_3D_boxes_recursively(obj);
195     if (SP_IS_LPE_ITEM(obj)) {
196         sp_lpe_item_add_temporary_canvasitems(SP_LPE_ITEM(obj), desktop());
197     }
199     _release_connections[obj] = obj->connectRelease(sigc::mem_fun(*this, (void (Selection::*)(SPObject *))&Selection::remove));
200     _modified_connections[obj] = obj->connectModified(sigc::mem_fun(*this, &Selection::_schedule_modified));
203 void Selection::set(SPObject *object, bool persist_selection_context) {
204     _clear();
205     add(object, persist_selection_context);
208 void Selection::toggle(SPObject *obj) {
209     if (includes (obj)) {
210         remove (obj);
211     } else {
212         add(obj);
213     }
216 void Selection::remove(SPObject *obj) {
217     g_return_if_fail(obj != NULL);
218     g_return_if_fail(SP_IS_OBJECT(obj));
219     g_return_if_fail(includes(obj));
221     _invalidateCachedLists();
222     _remove(obj);
223     _emitChanged();
226 void Selection::remove_box_perspective(SPBox3D *box) {
227     Persp3D *persp = box3d_get_perspective(box);
228     std::map<Persp3D *, unsigned int>::iterator p = _persps.find(persp);
229     if (p == _persps.end()) {
230         g_print ("Warning! Trying to remove unselected perspective from selection!\n");
231         return;
232     }
233     if ((*p).second > 1) {
234         _persps[persp]--;
235     } else {
236         _persps.erase(p);
237     }
240 void Selection::remove_3D_boxes_recursively(SPObject *obj) {
241     std::list<SPBox3D *> boxes = box3d_extract_boxes(obj);
243     for (std::list<SPBox3D *>::iterator i = boxes.begin(); i != boxes.end(); ++i) {
244         SPBox3D *box = *i;
245         box3d_remove_from_selection(box);
246         std::list<SPBox3D *>::iterator b = std::find(_3dboxes.begin(), _3dboxes.end(), box);
247         if (b == _3dboxes.end()) {
248             g_print ("Warning! Trying to remove unselected box from selection.\n");
249             return;
250         }
251         _3dboxes.erase(b);
252         remove_box_perspective(box);
253     }
256 void Selection::_remove(SPObject *obj) {
257     _modified_connections[obj].disconnect();
258     _modified_connections.erase(obj);
260     _release_connections[obj].disconnect();
261     _release_connections.erase(obj);
263     remove_3D_boxes_recursively(obj);
265     if (SP_IS_LPE_ITEM(obj)) {
266         sp_lpe_item_remove_temporary_canvasitems(SP_LPE_ITEM(obj), desktop());
267     }
269     _objs = g_slist_remove(_objs, obj);
272 void Selection::setList(GSList const *list) {
273     _clear();
275     if ( list != NULL ) {
276         for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
277             _add(reinterpret_cast<SPObject *>(iter->data));
278         }
279     }
281     _emitChanged();
284 void Selection::addList(GSList const *list) {
286     if (list == NULL)
287         return;
289     _invalidateCachedLists();
291     for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
292         SPObject *obj = reinterpret_cast<SPObject *>(iter->data);
293         if (includes(obj)) {
294             continue;
295         }
296         _add (obj);
297     }
299     _emitChanged();
302 void Selection::setReprList(GSList const *list) {
303     _clear();
305     for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
306         SPObject *obj=_objectForXMLNode(reinterpret_cast<Inkscape::XML::Node *>(iter->data));
307         if (obj) {
308             _add(obj);
309         }
310     }
312     _emitChanged();
315 void Selection::clear() {
316     _clear();
317     _emitChanged();
320 GSList const *Selection::list() {
321     return _objs;
324 GSList const *Selection::itemList() {
325     if (_items) {
326         return _items;
327     }
329     for ( GSList const *iter=_objs ; iter != NULL ; iter = iter->next ) {
330         SPObject *obj=reinterpret_cast<SPObject *>(iter->data);
331         if (SP_IS_ITEM(obj)) {
332             _items = g_slist_prepend(_items, SP_ITEM(obj));
333         }
334     }
335     _items = g_slist_reverse(_items);
337     return _items;
340 GSList const *Selection::reprList() {
341     if (_reprs) { return _reprs; }
343     for ( GSList const *iter=itemList() ; iter != NULL ; iter = iter->next ) {
344         SPObject *obj=reinterpret_cast<SPObject *>(iter->data);
345         _reprs = g_slist_prepend(_reprs, SP_OBJECT_REPR(obj));
346     }
347     _reprs = g_slist_reverse(_reprs);
349     return _reprs;
352 std::list<Persp3D *> const Selection::perspList() {
353     std::list<Persp3D *> pl;
354     for (std::map<Persp3D *, unsigned int>::iterator p = _persps.begin(); p != _persps.end(); ++p) {
355         pl.push_back((*p).first);
356     }
357     return pl;
360 std::list<SPBox3D *> const Selection::box3DList() {
361     return _3dboxes;
364 SPObject *Selection::single() {
365     if ( _objs != NULL && _objs->next == NULL ) {
366         return reinterpret_cast<SPObject *>(_objs->data);
367     } else {
368         return NULL;
369     }
372 SPItem *Selection::singleItem() {
373     GSList const *items=itemList();
374     if ( items != NULL && items->next == NULL ) {
375         return reinterpret_cast<SPItem *>(items->data);
376     } else {
377         return NULL;
378     }
381 Inkscape::XML::Node *Selection::singleRepr() {
382     SPObject *obj=single();
383     return obj ? SP_OBJECT_REPR(obj) : NULL;
386 NRRect *Selection::bounds(NRRect *bbox, SPItem::BBoxType type) const
388     g_return_val_if_fail (bbox != NULL, NULL);
389     *bbox = NRRect(bounds(type));
390     return bbox;
393 NR::Maybe<NR::Rect> Selection::bounds(SPItem::BBoxType type) const
395     GSList const *items = const_cast<Selection *>(this)->itemList();
397     NR::Maybe<NR::Rect> bbox = NR::Nothing();
398     for ( GSList const *i = items ; i != NULL ; i = i->next ) {
399         bbox = NR::union_bounds(bbox, sp_item_bbox_desktop(SP_ITEM(i->data), type));
400     }
401     return bbox;
404 NRRect *Selection::boundsInDocument(NRRect *bbox, SPItem::BBoxType type) const {
405     g_return_val_if_fail (bbox != NULL, NULL);
407     GSList const *items=const_cast<Selection *>(this)->itemList();
408     if (!items) {
409         bbox->x0 = bbox->y0 = bbox->x1 = bbox->y1 = 0.0;
410         return bbox;
411     }
413     bbox->x0 = bbox->y0 = 1e18;
414     bbox->x1 = bbox->y1 = -1e18;
416     for ( GSList const *iter=items ; iter != NULL ; iter = iter->next ) {
417         SPItem *item=SP_ITEM(iter->data);
418         NR::Matrix i2doc(from_2geom(sp_item_i2doc_affine(item)));
419         sp_item_invoke_bbox(item, bbox, i2doc, FALSE, type);
420     }
422     return bbox;
425 NR::Maybe<NR::Rect> Selection::boundsInDocument(SPItem::BBoxType type) const {
426     NRRect r;
427     return boundsInDocument(&r, type)->upgrade();
430 /** Extract the position of the center from the first selected object */
431 NR::Maybe<NR::Point> Selection::center() const {
432     GSList *items = (GSList *) const_cast<Selection *>(this)->itemList();
433     NR::Point center;
434     if (items) {
435         SPItem *first = reinterpret_cast<SPItem*>(g_slist_last(items)->data); // from the first item in selection
436         if (first->isCenterSet()) { // only if set explicitly
437             return first->getCenter();
438         }
439     }
440     NR::Maybe<NR::Rect> bbox = bounds();
441     if (bbox) {
442         return bounds()->midpoint();
443     } else {
444         return NR::Nothing();
445     }
448 /**
449  * Compute the list of points in the selection that are to be considered for snapping.
450  */
451 std::vector<NR::Point> Selection::getSnapPoints(bool includeItemCenter) const {
452     GSList const *items = const_cast<Selection *>(this)->itemList();
453     std::vector<NR::Point> p;
454     for (GSList const *iter = items; iter != NULL; iter = iter->next) {
455         SPItem *this_item = SP_ITEM(iter->data);
456         sp_item_snappoints(this_item, false, SnapPointsIter(p));
457         //Include the transformation origin for snapping
458         //For a group only the group's origin is considered
459         if (includeItemCenter) {
460                 p.push_back(this_item->getCenter());
461         }  
462     }
464     return p;
467 std::vector<NR::Point> Selection::getSnapPointsConvexHull() const {
468     GSList const *items = const_cast<Selection *>(this)->itemList();
470     std::vector<NR::Point> p;
471     for (GSList const *iter = items; iter != NULL; iter = iter->next) {
472                 sp_item_snappoints(SP_ITEM(iter->data), false, SnapPointsIter(p));
473     }
475     std::vector<NR::Point> pHull;
476     if (!p.empty()) {
477         std::vector<NR::Point>::iterator i;
478         NR::ConvexHull cvh(p.front());
479         for (i = p.begin(); i != p.end(); i++) {
480             // these are the points we get back
481             cvh.add(*i);
482         }
484         NR::Maybe<NR::Rect> rHull = cvh.bounds();
485         if (rHull) {
486             for ( unsigned i = 0 ; i < 4 ; ++i ) {
487                 pHull.push_back(rHull->corner(i));
488             }
489         }
490     }
492     return pHull;
495 void Selection::_removeObjectDescendants(SPObject *obj) {
496     GSList *iter, *next;
497     for ( iter = _objs ; iter ; iter = next ) {
498         next = iter->next;
499         SPObject *sel_obj=reinterpret_cast<SPObject *>(iter->data);
500         SPObject *parent=SP_OBJECT_PARENT(sel_obj);
501         while (parent) {
502             if ( parent == obj ) {
503                 _remove(sel_obj);
504                 break;
505             }
506             parent = SP_OBJECT_PARENT(parent);
507         }
508     }
511 void Selection::_removeObjectAncestors(SPObject *obj) {
512         SPObject *parent=SP_OBJECT_PARENT(obj);
513         while (parent) {
514             if (includes(parent)) {
515                 _remove(parent);
516             }
517             parent = SP_OBJECT_PARENT(parent);
518         }
521 SPObject *Selection::_objectForXMLNode(Inkscape::XML::Node *repr) const {
522     g_return_val_if_fail(repr != NULL, NULL);
523     gchar const *id = repr->attribute("id");
524     g_return_val_if_fail(id != NULL, NULL);
525     SPObject *object=sp_desktop_document(_desktop)->getObjectById(id);
526     g_return_val_if_fail(object != NULL, NULL);
527     return object;
530 guint Selection::numberOfLayers() {
531       GSList const *items = const_cast<Selection *>(this)->itemList();
532         GSList *layers = NULL;
533         for (GSList const *iter = items; iter != NULL; iter = iter->next) {
534                 SPObject *layer = desktop()->layerForObject(SP_OBJECT(iter->data));
535                 if (g_slist_find (layers, layer) == NULL) {
536                         layers = g_slist_prepend (layers, layer);
537                 }
538         }
539         guint ret = g_slist_length (layers);
540         g_slist_free (layers);
541         return ret;
544 guint Selection::numberOfParents() {
545       GSList const *items = const_cast<Selection *>(this)->itemList();
546         GSList *parents = NULL;
547         for (GSList const *iter = items; iter != NULL; iter = iter->next) {
548                 SPObject *parent = SP_OBJECT_PARENT(iter->data);
549                 if (g_slist_find (parents, parent) == NULL) {
550                         parents = g_slist_prepend (parents, parent);
551                 }
552         }
553         guint ret = g_slist_length (parents);
554         g_slist_free (parents);
555         return ret;
560 /*
561   Local Variables:
562   mode:c++
563   c-file-style:"stroustrup"
564   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
565   indent-tabs-mode:nil
566   fill-column:99
567   End:
568 */
569 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :