Code

bulk trailing spaces removal. consistency through MD5 of binary
[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  *
9  * Copyright (C) 2004-2005 MenTaLguY
10  * Copyright (C) 1999-2002 Lauris Kaplinski
11  * Copyright (C) 2001-2002 Ximian, Inc.
12  *
13  * Released under GNU GPL, read the file 'COPYING' for more information
14  */
16 #ifdef HAVE_CONFIG_H
17 # include <config.h>
18 #endif
19 #include "macros.h"
20 #include "inkscape-private.h"
21 #include "desktop.h"
22 #include "desktop-handles.h"
23 #include "document.h"
24 #include "selection.h"
25 #include "xml/repr.h"
27 #include "sp-shape.h"
30 #define SP_SELECTION_UPDATE_PRIORITY (G_PRIORITY_HIGH_IDLE + 1)
32 namespace Inkscape {
34 Selection::Selection(SPDesktop *desktop) :
35     _objs(NULL),
36     _reprs(NULL),
37     _items(NULL),
38     _desktop(desktop),
39     _flags(0),
40     _idle(0)
41 {
42     clearOnceInaccessible(&_desktop);
43 }
45 Selection::~Selection() {
46     _clear();
47     _desktop = NULL;
48     if (_idle) {
49         g_source_remove(_idle);
50         _idle = 0;
51     }
52 }
54 void
55 Selection::_release(SPObject *obj, Selection *selection)
56 {
57     selection->remove(obj);
58 }
60 /* Handler for selected objects "modified" signal */
62 void
63 Selection::_schedule_modified(SPObject *obj, guint flags, Selection *selection)
64 {
65     if (!selection->_idle) {
66         /* Request handling to be run in _idle loop */
67         selection->_idle = g_idle_add_full(SP_SELECTION_UPDATE_PRIORITY, GSourceFunc(&Selection::_emit_modified), selection, NULL);
68     }
70     /* Collect all flags */
71     selection->_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() {
94     inkscape_selection_changed(this);
95     _changed_signal.emit(this);
96 }
98 void Selection::_invalidateCachedLists() {
99     g_slist_free(_items);
100     _items = NULL;
102     g_slist_free(_reprs);
103     _reprs = NULL;
106 void Selection::_clear() {
107     _invalidateCachedLists();
108     while (_objs) {
109         SPObject *obj=reinterpret_cast<SPObject *>(_objs->data);
110         sp_signal_disconnect_by_data(obj, this);
111         _objs = g_slist_remove(_objs, obj);
112     }
115 bool Selection::includes(SPObject *obj) const {
116     if (obj == NULL)
117         return FALSE;
119     g_return_val_if_fail(SP_IS_OBJECT(obj), FALSE);
121     return ( g_slist_find(_objs, obj) != NULL );
124 void Selection::add(SPObject *obj) {
125     g_return_if_fail(obj != NULL);
126     g_return_if_fail(SP_IS_OBJECT(obj));
128     if (includes(obj)) {
129         return;
130     }
132     _invalidateCachedLists();
133     _add(obj);
134     _emitChanged();
137 void Selection::_add(SPObject *obj) {
138     // unselect any of the item's ancestors and descendants which may be selected
139     // (to prevent double-selection)
140     _removeObjectDescendants(obj);
141     _removeObjectAncestors(obj);
143     _objs = g_slist_prepend(_objs, obj);
144     g_signal_connect(G_OBJECT(obj), "release",
145                      G_CALLBACK(&Selection::_release), this);
146     g_signal_connect(G_OBJECT(obj), "modified",
147                      G_CALLBACK(&Selection::_schedule_modified), this);
149     /*
150     if (!SP_IS_SHAPE(obj)) {
151         printf("This is not a shape\n");
152     }
153     */
156 void Selection::set(SPObject *object) {
157     _clear();
158     add(object);
161 void Selection::toggle(SPObject *obj) {
162     if (includes (obj)) {
163         remove (obj);
164     } else {
165         add(obj);
166     }
169 void Selection::remove(SPObject *obj) {
170     g_return_if_fail(obj != NULL);
171     g_return_if_fail(SP_IS_OBJECT(obj));
172     g_return_if_fail(includes(obj));
174     _invalidateCachedLists();
175     _remove(obj);
176     _emitChanged();
179 void Selection::_remove(SPObject *obj) {
180     sp_signal_disconnect_by_data(obj, this);
181     _objs = g_slist_remove(_objs, obj);
184 void Selection::setList(GSList const *list) {
185     _clear();
187     if ( list != NULL ) {
188         for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
189             _add(reinterpret_cast<SPObject *>(iter->data));
190         }
191     }
193     _emitChanged();
196 void Selection::addList(GSList const *list) {
198     if (list == NULL)
199         return;
201     _invalidateCachedLists();
203     for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
204         SPObject *obj = reinterpret_cast<SPObject *>(iter->data);
205         if (includes(obj)) {
206             continue;
207         }
208         _add (obj);
209     }
211     _emitChanged();
214 void Selection::setReprList(GSList const *list) {
215     _clear();
217     for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
218         SPObject *obj=_objectForXMLNode(reinterpret_cast<Inkscape::XML::Node *>(iter->data));
219         if (obj) {
220             _add(obj);
221         }
222     }
224     _emitChanged();
227 void Selection::clear() {
228     _clear();
229     _emitChanged();
232 GSList const *Selection::list() {
233     return _objs;
236 GSList const *Selection::itemList() {
237     if (_items) {
238         return _items;
239     }
241     for ( GSList const *iter=_objs ; iter != NULL ; iter = iter->next ) {
242         SPObject *obj=reinterpret_cast<SPObject *>(iter->data);
243         if (SP_IS_ITEM(obj)) {
244             _items = g_slist_prepend(_items, SP_ITEM(obj));
245         }
246     }
247     _items = g_slist_reverse(_items);
249     return _items;
252 GSList const *Selection::reprList() {
253     if (_reprs) { return _reprs; }
255     for ( GSList const *iter=itemList() ; iter != NULL ; iter = iter->next ) {
256         SPObject *obj=reinterpret_cast<SPObject *>(iter->data);
257         _reprs = g_slist_prepend(_reprs, SP_OBJECT_REPR(obj));
258     }
259     _reprs = g_slist_reverse(_reprs);
261     return _reprs;
264 SPObject *Selection::single() {
265     if ( _objs != NULL && _objs->next == NULL ) {
266         return reinterpret_cast<SPObject *>(_objs->data);
267     } else {
268         return NULL;
269     }
272 SPItem *Selection::singleItem() {
273     GSList const *items=itemList();
274     if ( items != NULL && items->next == NULL ) {
275         return reinterpret_cast<SPItem *>(items->data);
276     } else {
277         return NULL;
278     }
281 Inkscape::XML::Node *Selection::singleRepr() {
282     SPObject *obj=single();
283     return obj ? SP_OBJECT_REPR(obj) : NULL;
286 NRRect *Selection::bounds(NRRect *bbox) const
288     g_return_val_if_fail (bbox != NULL, NULL);
289     NR::Rect const b = bounds();
290     bbox->x0 = b.min()[NR::X];
291     bbox->y0 = b.min()[NR::Y];
292     bbox->x1 = b.max()[NR::X];
293     bbox->y1 = b.max()[NR::Y];
294     return bbox;
297 NR::Rect Selection::bounds() const
299     GSList const *items = const_cast<Selection *>(this)->itemList();
300     if (!items) {
301         return NR::Rect(NR::Point(0, 0), NR::Point(0, 0));
302     }
304     GSList const *i = items;
305     NR::Rect bbox = sp_item_bbox_desktop(SP_ITEM(i->data));
307     while (i != NULL) {
308         bbox = NR::Rect::union_bounds(bbox, sp_item_bbox_desktop(SP_ITEM(i->data)));
309         i = i->next;
310     }
312     return bbox;
315 NRRect *Selection::boundsInDocument(NRRect *bbox) const {
316     g_return_val_if_fail (bbox != NULL, NULL);
318     GSList const *items=const_cast<Selection *>(this)->itemList();
319     if (!items) {
320         bbox->x0 = bbox->y0 = bbox->x1 = bbox->y1 = 0.0;
321         return bbox;
322     }
324     bbox->x0 = bbox->y0 = 1e18;
325     bbox->x1 = bbox->y1 = -1e18;
327     for ( GSList const *iter=items ; iter != NULL ; iter = iter->next ) {
328         SPItem *item=SP_ITEM(iter->data);
329         NR::Matrix const i2doc(sp_item_i2doc_affine(item));
330         sp_item_invoke_bbox(item, bbox, i2doc, FALSE);
331     }
333     return bbox;
336 NR::Rect Selection::boundsInDocument() const {
337     NRRect r;
338     return NR::Rect(*boundsInDocument(&r));
341 /**
342  * Compute the list of points in the selection that are to be considered for snapping.
343  */
344 std::vector<NR::Point> Selection::getSnapPoints() const {
345     GSList const *items = const_cast<Selection *>(this)->itemList();
346     std::vector<NR::Point> p;
347     for (GSList const *iter = items; iter != NULL; iter = iter->next) {
348         sp_item_snappoints(SP_ITEM(iter->data), SnapPointsIter(p));
349     }
351     return p;
354 std::vector<NR::Point> Selection::getSnapPointsConvexHull() const {
355     GSList const *items = const_cast<Selection *>(this)->itemList();
356     std::vector<NR::Point> p;
357     for (GSList const *iter = items; iter != NULL; iter = iter->next) {
358         sp_item_snappoints(SP_ITEM(iter->data), SnapPointsIter(p));
359     }
361     std::vector<NR::Point>::iterator i;
362     NR::ConvexHull cvh(*(p.begin()));
363     for (i = p.begin(); i != p.end(); i++) {
364         // these are the points we get back
365         cvh.add(*i);
366     }
368     NR::Rect rHull = cvh.bounds();
369     std::vector<NR::Point> pHull(4);
370     pHull[0] = rHull.corner(0);
371     pHull[1] = rHull.corner(1);
372     pHull[2] = rHull.corner(2);
373     pHull[3] = rHull.corner(3);
375     return pHull;
378 std::vector<NR::Point> Selection::getBBoxPoints() const {
379     GSList const *items = const_cast<Selection *>(this)->itemList();
380     std::vector<NR::Point> p;
381     for (GSList const *iter = items; iter != NULL; iter = iter->next) {
382         NR::Rect b = sp_item_bbox_desktop(SP_ITEM(iter->data));
383         p.push_back(b.min());
384         p.push_back(b.max());
385     }
387     return p;
390 void Selection::_removeObjectDescendants(SPObject *obj) {
391     GSList *iter, *next;
392     for ( iter = _objs ; iter ; iter = next ) {
393         next = iter->next;
394         SPObject *sel_obj=reinterpret_cast<SPObject *>(iter->data);
395         SPObject *parent=SP_OBJECT_PARENT(sel_obj);
396         while (parent) {
397             if ( parent == obj ) {
398                 _remove(sel_obj);
399                 break;
400             }
401             parent = SP_OBJECT_PARENT(parent);
402         }
403     }
406 void Selection::_removeObjectAncestors(SPObject *obj) {
407         SPObject *parent=SP_OBJECT_PARENT(obj);
408         while (parent) {
409             if (includes(parent)) {
410                 _remove(parent);
411             }
412             parent = SP_OBJECT_PARENT(parent);
413         }
416 SPObject *Selection::_objectForXMLNode(Inkscape::XML::Node *repr) const {
417     g_return_val_if_fail(repr != NULL, NULL);
418     gchar const *id = repr->attribute("id");
419     g_return_val_if_fail(id != NULL, NULL);
420     SPObject *object=SP_DT_DOCUMENT(_desktop)->getObjectById(id);
421     g_return_val_if_fail(object != NULL, NULL);
422     return object;
425 guint Selection::numberOfLayers() {
426       GSList const *items = const_cast<Selection *>(this)->itemList();
427         GSList *layers = NULL;
428         for (GSList const *iter = items; iter != NULL; iter = iter->next) {
429                 SPObject *layer = desktop()->layerForObject(SP_OBJECT(iter->data));
430                 if (g_slist_find (layers, layer) == NULL) {
431                         layers = g_slist_prepend (layers, layer);
432                 }
433         }
434         guint ret = g_slist_length (layers);
435         g_slist_free (layers);
436         return ret;
441 /*
442   Local Variables:
443   mode:c++
444   c-file-style:"stroustrup"
445   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
446   indent-tabs-mode:nil
447   fill-column:99
448   End:
449 */
450 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :