Code

Applied patch #1503864
[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"
32 #define SP_SELECTION_UPDATE_PRIORITY (G_PRIORITY_HIGH_IDLE + 1)
34 namespace Inkscape {
36 Selection::Selection(SPDesktop *desktop) :
37     _objs(NULL),
38     _reprs(NULL),
39     _items(NULL),
40     _desktop(desktop),
41     _selection_context(NULL),
42     _context_release_handler_id(0),
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 void
58 Selection::_release(SPObject *obj, Selection *selection)
59 {
60     selection->remove(obj);
61 }
63 /* Handler for selected objects "modified" signal */
65 void
66 Selection::_schedule_modified(SPObject *obj, guint flags, Selection *selection)
67 {
68     if (!selection->_idle) {
69         /* Request handling to be run in _idle loop */
70         selection->_idle = g_idle_add_full(SP_SELECTION_UPDATE_PRIORITY, GSourceFunc(&Selection::_emit_modified), selection, NULL);
71     }
73     /* Collect all flags */
74     selection->_flags |= flags;
75 }
77 gboolean
78 Selection::_emit_modified(Selection *selection)
79 {
80     /* force new handler to be created if requested before we return */
81     selection->_idle = 0;
82     guint flags = selection->_flags;
83     selection->_flags = 0;
85     selection->_emitModified(flags);
87     /* drop this handler */
88     return FALSE;
89 }
91 void Selection::_emitModified(guint flags) {
92     inkscape_selection_modified(this, flags);
93     _modified_signal.emit(this, flags);
94 }
96 void Selection::_emitChanged(bool persist_selection_context/* = false */) {
97     if (persist_selection_context) {
98         if (NULL == _selection_context) {
99             _selection_context = desktop()->currentLayer();
100             sp_object_ref(_selection_context, NULL);
101             _context_release_handler_id = g_signal_connect(
102                                             G_OBJECT(_selection_context), "release",
103                                             G_CALLBACK(&Selection::_releaseSelectionContext),
104                                             this);
105         }
106     } else {
107         _releaseContext(_selection_context);
108     }
110     inkscape_selection_changed(this);
111     _changed_signal.emit(this);
114 void
115 Selection::_releaseSelectionContext(SPObject *obj, Selection *selection)
117     selection->_releaseContext(obj);
120 void
121 Selection::_releaseContext(SPObject *obj)
123     if (NULL == _selection_context || _selection_context != obj)
124         return;
126     g_signal_handler_disconnect(G_OBJECT(_selection_context), _context_release_handler_id);
127     sp_object_unref(_selection_context, NULL);
128     _context_release_handler_id = 0;
129     _selection_context = NULL;
132 void Selection::_invalidateCachedLists() {
133     g_slist_free(_items);
134     _items = NULL;
136     g_slist_free(_reprs);
137     _reprs = NULL;
140 void Selection::_clear() {
141     _invalidateCachedLists();
142     while (_objs) {
143         SPObject *obj=reinterpret_cast<SPObject *>(_objs->data);
144         sp_signal_disconnect_by_data(obj, this);
145         _objs = g_slist_remove(_objs, obj);
146     }
149 SPObject *Selection::activeContext() {
150     if (NULL != _selection_context)
151         return _selection_context;
152     return desktop()->currentLayer();
153     }
155 bool Selection::includes(SPObject *obj) const {
156     if (obj == NULL)
157         return FALSE;
159     g_return_val_if_fail(SP_IS_OBJECT(obj), FALSE);
161     return ( g_slist_find(_objs, obj) != NULL );
164 void Selection::add(SPObject *obj, bool persist_selection_context/* = false */) {
165     g_return_if_fail(obj != NULL);
166     g_return_if_fail(SP_IS_OBJECT(obj));
168     if (includes(obj)) {
169         return;
170     }
172     _invalidateCachedLists();
173     _add(obj);
174     _emitChanged(persist_selection_context);
177 void Selection::_add(SPObject *obj) {
178     // unselect any of the item's ancestors and descendants which may be selected
179     // (to prevent double-selection)
180     _removeObjectDescendants(obj);
181     _removeObjectAncestors(obj);
183     _objs = g_slist_prepend(_objs, obj);
184     g_signal_connect(G_OBJECT(obj), "release",
185                      G_CALLBACK(&Selection::_release), this);
186     g_signal_connect(G_OBJECT(obj), "modified",
187                      G_CALLBACK(&Selection::_schedule_modified), this);
189     /*
190     if (!SP_IS_SHAPE(obj)) {
191         printf("This is not a shape\n");
192     }
193     */
196 void Selection::set(SPObject *object, bool persist_selection_context) {
197     _clear();
198     add(object, persist_selection_context);
201 void Selection::toggle(SPObject *obj) {
202     if (includes (obj)) {
203         remove (obj);
204     } else {
205         add(obj);
206     }
209 void Selection::remove(SPObject *obj) {
210     g_return_if_fail(obj != NULL);
211     g_return_if_fail(SP_IS_OBJECT(obj));
212     g_return_if_fail(includes(obj));
214     _invalidateCachedLists();
215     _remove(obj);
216     _emitChanged();
219 void Selection::_remove(SPObject *obj) {
220     sp_signal_disconnect_by_data(obj, this);
221     _objs = g_slist_remove(_objs, obj);
224 void Selection::setList(GSList const *list) {
225     _clear();
227     if ( list != NULL ) {
228         for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
229             _add(reinterpret_cast<SPObject *>(iter->data));
230         }
231     }
233     _emitChanged();
236 void Selection::addList(GSList const *list) {
238     if (list == NULL)
239         return;
241     _invalidateCachedLists();
243     for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
244         SPObject *obj = reinterpret_cast<SPObject *>(iter->data);
245         if (includes(obj)) {
246             continue;
247         }
248         _add (obj);
249     }
251     _emitChanged();
254 void Selection::setReprList(GSList const *list) {
255     _clear();
257     for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
258         SPObject *obj=_objectForXMLNode(reinterpret_cast<Inkscape::XML::Node *>(iter->data));
259         if (obj) {
260             _add(obj);
261         }
262     }
264     _emitChanged();
267 void Selection::clear() {
268     _clear();
269     _emitChanged();
272 GSList const *Selection::list() {
273     return _objs;
276 GSList const *Selection::itemList() {
277     if (_items) {
278         return _items;
279     }
281     for ( GSList const *iter=_objs ; iter != NULL ; iter = iter->next ) {
282         SPObject *obj=reinterpret_cast<SPObject *>(iter->data);
283         if (SP_IS_ITEM(obj)) {
284             _items = g_slist_prepend(_items, SP_ITEM(obj));
285         }
286     }
287     _items = g_slist_reverse(_items);
289     return _items;
292 GSList const *Selection::reprList() {
293     if (_reprs) { return _reprs; }
295     for ( GSList const *iter=itemList() ; iter != NULL ; iter = iter->next ) {
296         SPObject *obj=reinterpret_cast<SPObject *>(iter->data);
297         _reprs = g_slist_prepend(_reprs, SP_OBJECT_REPR(obj));
298     }
299     _reprs = g_slist_reverse(_reprs);
301     return _reprs;
304 SPObject *Selection::single() {
305     if ( _objs != NULL && _objs->next == NULL ) {
306         return reinterpret_cast<SPObject *>(_objs->data);
307     } else {
308         return NULL;
309     }
312 SPItem *Selection::singleItem() {
313     GSList const *items=itemList();
314     if ( items != NULL && items->next == NULL ) {
315         return reinterpret_cast<SPItem *>(items->data);
316     } else {
317         return NULL;
318     }
321 Inkscape::XML::Node *Selection::singleRepr() {
322     SPObject *obj=single();
323     return obj ? SP_OBJECT_REPR(obj) : NULL;
326 NRRect *Selection::bounds(NRRect *bbox) const
328     g_return_val_if_fail (bbox != NULL, NULL);
329     NR::Rect const b = bounds();
330     bbox->x0 = b.min()[NR::X];
331     bbox->y0 = b.min()[NR::Y];
332     bbox->x1 = b.max()[NR::X];
333     bbox->y1 = b.max()[NR::Y];
334     return bbox;
337 NR::Rect Selection::bounds() const
339     GSList const *items = const_cast<Selection *>(this)->itemList();
340     if (!items) {
341         return NR::Rect(NR::Point(0, 0), NR::Point(0, 0));
342     }
344     GSList const *i = items;
345     NR::Rect bbox = sp_item_bbox_desktop(SP_ITEM(i->data));
347     GSList const *i_start = i;
348     while (i != NULL) {
349         if (i != i_start)
350             bbox = NR::Rect::union_bounds(bbox, sp_item_bbox_desktop(SP_ITEM(i->data)));
351         i = i->next;
352     }
354     return bbox;
357 NRRect *Selection::boundsInDocument(NRRect *bbox) const {
358     g_return_val_if_fail (bbox != NULL, NULL);
360     GSList const *items=const_cast<Selection *>(this)->itemList();
361     if (!items) {
362         bbox->x0 = bbox->y0 = bbox->x1 = bbox->y1 = 0.0;
363         return bbox;
364     }
366     bbox->x0 = bbox->y0 = 1e18;
367     bbox->x1 = bbox->y1 = -1e18;
369     for ( GSList const *iter=items ; iter != NULL ; iter = iter->next ) {
370         SPItem *item=SP_ITEM(iter->data);
371         NR::Matrix const i2doc(sp_item_i2doc_affine(item));
372         sp_item_invoke_bbox(item, bbox, i2doc, FALSE);
373     }
375     return bbox;
378 NR::Rect Selection::boundsInDocument() const {
379     NRRect r;
380     return NR::Rect(*boundsInDocument(&r));
383 /** Extract the position of the center from the first selected object */
384 NR::Point Selection::center() const {
385     GSList *items = (GSList *) const_cast<Selection *>(this)->itemList();
386     NR::Point center;
387     if (items) {
388         SPItem *first = reinterpret_cast<SPItem*>(g_slist_last(items)->data); // from the first item in selection
389         if (first->isCenterSet()) { // only if set explicitly
390             center = first->getCenter();
391         } else {
392             center = bounds().midpoint();
393         }
394     } else {
395         center = bounds().midpoint();
396     }
397     return center;
400 /**
401  * Compute the list of points in the selection that are to be considered for snapping.
402  */
403 std::vector<NR::Point> Selection::getSnapPoints() const {
404     GSList const *items = const_cast<Selection *>(this)->itemList();
405     std::vector<NR::Point> p;
406     for (GSList const *iter = items; iter != NULL; iter = iter->next) {
407         sp_item_snappoints(SP_ITEM(iter->data), SnapPointsIter(p));
408     }
410     return p;
413 std::vector<NR::Point> Selection::getSnapPointsConvexHull() const {
414     GSList const *items = const_cast<Selection *>(this)->itemList();
415     std::vector<NR::Point> p;
416     for (GSList const *iter = items; iter != NULL; iter = iter->next) {
417         sp_item_snappoints(SP_ITEM(iter->data), SnapPointsIter(p));
418     }
420     std::vector<NR::Point>::iterator i;
421     NR::ConvexHull cvh(*(p.begin()));
422     for (i = p.begin(); i != p.end(); i++) {
423         // these are the points we get back
424         cvh.add(*i);
425     }
427     NR::Rect rHull = cvh.bounds();
428     std::vector<NR::Point> pHull(4);
429     pHull[0] = rHull.corner(0);
430     pHull[1] = rHull.corner(1);
431     pHull[2] = rHull.corner(2);
432     pHull[3] = rHull.corner(3);
434     return pHull;
437 std::vector<NR::Point> Selection::getBBoxPoints() const {
438     GSList const *items = const_cast<Selection *>(this)->itemList();
439     std::vector<NR::Point> p;
440     for (GSList const *iter = items; iter != NULL; iter = iter->next) {
441         NR::Rect b = sp_item_bbox_desktop(SP_ITEM(iter->data));
442         p.push_back(b.min());
443         p.push_back(b.max());
444     }
446     return p;
449 std::vector<NR::Point> Selection::getBBoxPointsOuter() const {
450     std::vector<NR::Point> p;
451     NR::Rect bbox = bounds();
452     p.push_back(bbox.min());
453     p.push_back(bbox.max());
454     return p;
457 void Selection::_removeObjectDescendants(SPObject *obj) {
458     GSList *iter, *next;
459     for ( iter = _objs ; iter ; iter = next ) {
460         next = iter->next;
461         SPObject *sel_obj=reinterpret_cast<SPObject *>(iter->data);
462         SPObject *parent=SP_OBJECT_PARENT(sel_obj);
463         while (parent) {
464             if ( parent == obj ) {
465                 _remove(sel_obj);
466                 break;
467             }
468             parent = SP_OBJECT_PARENT(parent);
469         }
470     }
473 void Selection::_removeObjectAncestors(SPObject *obj) {
474         SPObject *parent=SP_OBJECT_PARENT(obj);
475         while (parent) {
476             if (includes(parent)) {
477                 _remove(parent);
478             }
479             parent = SP_OBJECT_PARENT(parent);
480         }
483 SPObject *Selection::_objectForXMLNode(Inkscape::XML::Node *repr) const {
484     g_return_val_if_fail(repr != NULL, NULL);
485     gchar const *id = repr->attribute("id");
486     g_return_val_if_fail(id != NULL, NULL);
487     SPObject *object=sp_desktop_document(_desktop)->getObjectById(id);
488     g_return_val_if_fail(object != NULL, NULL);
489     return object;
492 guint Selection::numberOfLayers() {
493       GSList const *items = const_cast<Selection *>(this)->itemList();
494         GSList *layers = NULL;
495         for (GSList const *iter = items; iter != NULL; iter = iter->next) {
496                 SPObject *layer = desktop()->layerForObject(SP_OBJECT(iter->data));
497                 if (g_slist_find (layers, layer) == NULL) {
498                         layers = g_slist_prepend (layers, layer);
499                 }
500         }
501         guint ret = g_slist_length (layers);
502         g_slist_free (layers);
503         return ret;
506 guint Selection::numberOfParents() {
507       GSList const *items = const_cast<Selection *>(this)->itemList();
508         GSList *parents = NULL;
509         for (GSList const *iter = items; iter != NULL; iter = iter->next) {
510                 SPObject *parent = SP_OBJECT_PARENT(iter->data);
511                 if (g_slist_find (parents, parent) == NULL) {
512                         parents = g_slist_prepend (parents, parent);
513                 }
514         }
515         guint ret = g_slist_length (parents);
516         g_slist_free (parents);
517         return ret;
522 /*
523   Local Variables:
524   mode:c++
525   c-file-style:"stroustrup"
526   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
527   indent-tabs-mode:nil
528   fill-column:99
529   End:
530 */
531 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :