0b17eefe98d0a5814ff0fef502e885165d78a239
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 g_signal_connect(G_OBJECT(_selection_context), "release",
102 G_CALLBACK(&Selection::_releaseSelectionContext), this);
103 }
104 } else {
105 _releaseContext(_selection_context);
106 }
108 inkscape_selection_changed(this);
109 _changed_signal.emit(this);
110 }
112 void
113 Selection::_releaseSelectionContext(SPObject *obj, Selection *selection)
114 {
115 selection->_releaseContext(obj);
116 }
118 void
119 Selection::_releaseContext(SPObject *obj)
120 {
121 if (NULL == _selection_context || _selection_context != obj)
122 return;
124 g_signal_handler_disconnect(G_OBJECT(_selection_context), _context_release_handler_id);
125 sp_object_unref(_selection_context, NULL);
126 _context_release_handler_id = 0;
127 _selection_context = NULL;
128 }
130 void Selection::_invalidateCachedLists() {
131 g_slist_free(_items);
132 _items = NULL;
134 g_slist_free(_reprs);
135 _reprs = NULL;
136 }
138 void Selection::_clear() {
139 _invalidateCachedLists();
140 while (_objs) {
141 SPObject *obj=reinterpret_cast<SPObject *>(_objs->data);
142 sp_signal_disconnect_by_data(obj, this);
143 _objs = g_slist_remove(_objs, obj);
144 }
145 }
147 SPObject *Selection::activeContext() {
148 if (NULL != _selection_context)
149 return _selection_context;
150 return desktop()->currentLayer();
151 }
153 bool Selection::includes(SPObject *obj) const {
154 if (obj == NULL)
155 return FALSE;
157 g_return_val_if_fail(SP_IS_OBJECT(obj), FALSE);
159 return ( g_slist_find(_objs, obj) != NULL );
160 }
162 void Selection::add(SPObject *obj, bool persist_selection_context/* = false */) {
163 g_return_if_fail(obj != NULL);
164 g_return_if_fail(SP_IS_OBJECT(obj));
166 if (includes(obj)) {
167 return;
168 }
170 _invalidateCachedLists();
171 _add(obj);
172 _emitChanged(persist_selection_context);
173 }
175 void Selection::_add(SPObject *obj) {
176 // unselect any of the item's ancestors and descendants which may be selected
177 // (to prevent double-selection)
178 _removeObjectDescendants(obj);
179 _removeObjectAncestors(obj);
181 _objs = g_slist_prepend(_objs, obj);
182 g_signal_connect(G_OBJECT(obj), "release",
183 G_CALLBACK(&Selection::_release), this);
184 g_signal_connect(G_OBJECT(obj), "modified",
185 G_CALLBACK(&Selection::_schedule_modified), this);
187 /*
188 if (!SP_IS_SHAPE(obj)) {
189 printf("This is not a shape\n");
190 }
191 */
192 }
194 void Selection::set(SPObject *object, bool persist_selection_context) {
195 _clear();
196 add(object, persist_selection_context);
197 }
199 void Selection::toggle(SPObject *obj) {
200 if (includes (obj)) {
201 remove (obj);
202 } else {
203 add(obj);
204 }
205 }
207 void Selection::remove(SPObject *obj) {
208 g_return_if_fail(obj != NULL);
209 g_return_if_fail(SP_IS_OBJECT(obj));
210 g_return_if_fail(includes(obj));
212 _invalidateCachedLists();
213 _remove(obj);
214 _emitChanged();
215 }
217 void Selection::_remove(SPObject *obj) {
218 sp_signal_disconnect_by_data(obj, this);
219 _objs = g_slist_remove(_objs, obj);
220 }
222 void Selection::setList(GSList const *list) {
223 _clear();
225 if ( list != NULL ) {
226 for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
227 _add(reinterpret_cast<SPObject *>(iter->data));
228 }
229 }
231 _emitChanged();
232 }
234 void Selection::addList(GSList const *list) {
236 if (list == NULL)
237 return;
239 _invalidateCachedLists();
241 for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
242 SPObject *obj = reinterpret_cast<SPObject *>(iter->data);
243 if (includes(obj)) {
244 continue;
245 }
246 _add (obj);
247 }
249 _emitChanged();
250 }
252 void Selection::setReprList(GSList const *list) {
253 _clear();
255 for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
256 SPObject *obj=_objectForXMLNode(reinterpret_cast<Inkscape::XML::Node *>(iter->data));
257 if (obj) {
258 _add(obj);
259 }
260 }
262 _emitChanged();
263 }
265 void Selection::clear() {
266 _clear();
267 _emitChanged();
268 }
270 GSList const *Selection::list() {
271 return _objs;
272 }
274 GSList const *Selection::itemList() {
275 if (_items) {
276 return _items;
277 }
279 for ( GSList const *iter=_objs ; iter != NULL ; iter = iter->next ) {
280 SPObject *obj=reinterpret_cast<SPObject *>(iter->data);
281 if (SP_IS_ITEM(obj)) {
282 _items = g_slist_prepend(_items, SP_ITEM(obj));
283 }
284 }
285 _items = g_slist_reverse(_items);
287 return _items;
288 }
290 GSList const *Selection::reprList() {
291 if (_reprs) { return _reprs; }
293 for ( GSList const *iter=itemList() ; iter != NULL ; iter = iter->next ) {
294 SPObject *obj=reinterpret_cast<SPObject *>(iter->data);
295 _reprs = g_slist_prepend(_reprs, SP_OBJECT_REPR(obj));
296 }
297 _reprs = g_slist_reverse(_reprs);
299 return _reprs;
300 }
302 SPObject *Selection::single() {
303 if ( _objs != NULL && _objs->next == NULL ) {
304 return reinterpret_cast<SPObject *>(_objs->data);
305 } else {
306 return NULL;
307 }
308 }
310 SPItem *Selection::singleItem() {
311 GSList const *items=itemList();
312 if ( items != NULL && items->next == NULL ) {
313 return reinterpret_cast<SPItem *>(items->data);
314 } else {
315 return NULL;
316 }
317 }
319 Inkscape::XML::Node *Selection::singleRepr() {
320 SPObject *obj=single();
321 return obj ? SP_OBJECT_REPR(obj) : NULL;
322 }
324 NRRect *Selection::bounds(NRRect *bbox) const
325 {
326 g_return_val_if_fail (bbox != NULL, NULL);
327 NR::Rect const b = bounds();
328 bbox->x0 = b.min()[NR::X];
329 bbox->y0 = b.min()[NR::Y];
330 bbox->x1 = b.max()[NR::X];
331 bbox->y1 = b.max()[NR::Y];
332 return bbox;
333 }
335 NR::Rect Selection::bounds() const
336 {
337 GSList const *items = const_cast<Selection *>(this)->itemList();
338 if (!items) {
339 return NR::Rect(NR::Point(0, 0), NR::Point(0, 0));
340 }
342 GSList const *i = items;
343 NR::Rect bbox = sp_item_bbox_desktop(SP_ITEM(i->data));
345 GSList const *i_start = i;
346 while (i != NULL) {
347 if (i != i_start)
348 bbox = NR::Rect::union_bounds(bbox, sp_item_bbox_desktop(SP_ITEM(i->data)));
349 i = i->next;
350 }
352 return bbox;
353 }
355 NRRect *Selection::boundsInDocument(NRRect *bbox) const {
356 g_return_val_if_fail (bbox != NULL, NULL);
358 GSList const *items=const_cast<Selection *>(this)->itemList();
359 if (!items) {
360 bbox->x0 = bbox->y0 = bbox->x1 = bbox->y1 = 0.0;
361 return bbox;
362 }
364 bbox->x0 = bbox->y0 = 1e18;
365 bbox->x1 = bbox->y1 = -1e18;
367 for ( GSList const *iter=items ; iter != NULL ; iter = iter->next ) {
368 SPItem *item=SP_ITEM(iter->data);
369 NR::Matrix const i2doc(sp_item_i2doc_affine(item));
370 sp_item_invoke_bbox(item, bbox, i2doc, FALSE);
371 }
373 return bbox;
374 }
376 NR::Rect Selection::boundsInDocument() const {
377 NRRect r;
378 return NR::Rect(*boundsInDocument(&r));
379 }
381 /** Extract the position of the center from the first selected object */
382 NR::Point Selection::center() const {
383 GSList *items = (GSList *) const_cast<Selection *>(this)->itemList();
384 NR::Point center;
385 if (items) {
386 SPItem *first = reinterpret_cast<SPItem*>(g_slist_last(items)->data); // from the first item in selection
387 if (first->isCenterSet()) { // only if set explicitly
388 center = first->getCenter();
389 } else {
390 center = bounds().midpoint();
391 }
392 } else {
393 center = bounds().midpoint();
394 }
395 return center;
396 }
398 /**
399 * Compute the list of points in the selection that are to be considered for snapping.
400 */
401 std::vector<NR::Point> Selection::getSnapPoints() const {
402 GSList const *items = const_cast<Selection *>(this)->itemList();
403 std::vector<NR::Point> p;
404 for (GSList const *iter = items; iter != NULL; iter = iter->next) {
405 sp_item_snappoints(SP_ITEM(iter->data), SnapPointsIter(p));
406 }
408 return p;
409 }
411 std::vector<NR::Point> Selection::getSnapPointsConvexHull() const {
412 GSList const *items = const_cast<Selection *>(this)->itemList();
413 std::vector<NR::Point> p;
414 for (GSList const *iter = items; iter != NULL; iter = iter->next) {
415 sp_item_snappoints(SP_ITEM(iter->data), SnapPointsIter(p));
416 }
418 std::vector<NR::Point>::iterator i;
419 NR::ConvexHull cvh(*(p.begin()));
420 for (i = p.begin(); i != p.end(); i++) {
421 // these are the points we get back
422 cvh.add(*i);
423 }
425 NR::Rect rHull = cvh.bounds();
426 std::vector<NR::Point> pHull(4);
427 pHull[0] = rHull.corner(0);
428 pHull[1] = rHull.corner(1);
429 pHull[2] = rHull.corner(2);
430 pHull[3] = rHull.corner(3);
432 return pHull;
433 }
435 std::vector<NR::Point> Selection::getBBoxPoints() const {
436 GSList const *items = const_cast<Selection *>(this)->itemList();
437 std::vector<NR::Point> p;
438 for (GSList const *iter = items; iter != NULL; iter = iter->next) {
439 NR::Rect b = sp_item_bbox_desktop(SP_ITEM(iter->data));
440 p.push_back(b.min());
441 p.push_back(b.max());
442 }
444 return p;
445 }
447 void Selection::_removeObjectDescendants(SPObject *obj) {
448 GSList *iter, *next;
449 for ( iter = _objs ; iter ; iter = next ) {
450 next = iter->next;
451 SPObject *sel_obj=reinterpret_cast<SPObject *>(iter->data);
452 SPObject *parent=SP_OBJECT_PARENT(sel_obj);
453 while (parent) {
454 if ( parent == obj ) {
455 _remove(sel_obj);
456 break;
457 }
458 parent = SP_OBJECT_PARENT(parent);
459 }
460 }
461 }
463 void Selection::_removeObjectAncestors(SPObject *obj) {
464 SPObject *parent=SP_OBJECT_PARENT(obj);
465 while (parent) {
466 if (includes(parent)) {
467 _remove(parent);
468 }
469 parent = SP_OBJECT_PARENT(parent);
470 }
471 }
473 SPObject *Selection::_objectForXMLNode(Inkscape::XML::Node *repr) const {
474 g_return_val_if_fail(repr != NULL, NULL);
475 gchar const *id = repr->attribute("id");
476 g_return_val_if_fail(id != NULL, NULL);
477 SPObject *object=SP_DT_DOCUMENT(_desktop)->getObjectById(id);
478 g_return_val_if_fail(object != NULL, NULL);
479 return object;
480 }
482 guint Selection::numberOfLayers() {
483 GSList const *items = const_cast<Selection *>(this)->itemList();
484 GSList *layers = NULL;
485 for (GSList const *iter = items; iter != NULL; iter = iter->next) {
486 SPObject *layer = desktop()->layerForObject(SP_OBJECT(iter->data));
487 if (g_slist_find (layers, layer) == NULL) {
488 layers = g_slist_prepend (layers, layer);
489 }
490 }
491 guint ret = g_slist_length (layers);
492 g_slist_free (layers);
493 return ret;
494 }
496 guint Selection::numberOfParents() {
497 GSList const *items = const_cast<Selection *>(this)->itemList();
498 GSList *parents = NULL;
499 for (GSList const *iter = items; iter != NULL; iter = iter->next) {
500 SPObject *parent = SP_OBJECT_PARENT(iter->data);
501 if (g_slist_find (parents, parent) == NULL) {
502 parents = g_slist_prepend (parents, parent);
503 }
504 }
505 guint ret = g_slist_length (parents);
506 g_slist_free (parents);
507 return ret;
508 }
510 }
512 /*
513 Local Variables:
514 mode:c++
515 c-file-style:"stroustrup"
516 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
517 indent-tabs-mode:nil
518 fill-column:99
519 End:
520 */
521 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :