1 #define __SP_GROUP_C__
3 /*
4 * SVG <g> implementation
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * bulia byak <buliabyak@users.sf.net>
9 *
10 * Copyright (C) 1999-2005 authors
11 * Copyright (C) 2000-2001 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
20 #include <glibmm/i18n.h>
22 #include "display/nr-arena-group.h"
23 #include "libnr/nr-matrix-ops.h"
24 #include "libnr/nr-matrix-fns.h"
25 #include "xml/repr.h"
26 #include "svg/svg.h"
27 #include "document.h"
28 #include "style.h"
29 #include "attributes.h"
30 #include "sp-item-transform.h"
32 #include "sp-root.h"
33 #include "sp-use.h"
34 #include "prefs-utils.h"
36 static void sp_group_class_init (SPGroupClass *klass);
37 static void sp_group_init (SPGroup *group);
38 static void sp_group_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
39 static void sp_group_release(SPObject *object);
40 static void sp_group_dispose(GObject *object);
42 static void sp_group_child_added (SPObject * object, Inkscape::XML::Node * child, Inkscape::XML::Node * ref);
43 static void sp_group_remove_child (SPObject * object, Inkscape::XML::Node * child);
44 static void sp_group_order_changed (SPObject * object, Inkscape::XML::Node * child, Inkscape::XML::Node * old_ref, Inkscape::XML::Node * new_ref);
45 static void sp_group_update (SPObject *object, SPCtx *ctx, guint flags);
46 static void sp_group_modified (SPObject *object, guint flags);
47 static Inkscape::XML::Node *sp_group_write (SPObject *object, Inkscape::XML::Node *repr, guint flags);
48 static void sp_group_set(SPObject *object, unsigned key, char const *value);
50 static void sp_group_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
51 static void sp_group_print (SPItem * item, SPPrintContext *ctx);
52 static gchar * sp_group_description (SPItem * item);
53 static NRArenaItem *sp_group_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags);
54 static void sp_group_hide (SPItem * item, unsigned int key);
55 static void sp_group_snappoints (SPItem const *item, SnapPointsIter p);
57 static SPItemClass * parent_class;
59 GType
60 sp_group_get_type (void)
61 {
62 static GType group_type = 0;
63 if (!group_type) {
64 GTypeInfo group_info = {
65 sizeof (SPGroupClass),
66 NULL, /* base_init */
67 NULL, /* base_finalize */
68 (GClassInitFunc) sp_group_class_init,
69 NULL, /* class_finalize */
70 NULL, /* class_data */
71 sizeof (SPGroup),
72 16, /* n_preallocs */
73 (GInstanceInitFunc) sp_group_init,
74 NULL, /* value_table */
75 };
76 group_type = g_type_register_static (SP_TYPE_ITEM, "SPGroup", &group_info, (GTypeFlags)0);
77 }
78 return group_type;
79 }
81 static void
82 sp_group_class_init (SPGroupClass *klass)
83 {
84 GObjectClass * object_class;
85 SPObjectClass * sp_object_class;
86 SPItemClass * item_class;
88 object_class = (GObjectClass *) klass;
89 sp_object_class = (SPObjectClass *) klass;
90 item_class = (SPItemClass *) klass;
92 parent_class = (SPItemClass *)g_type_class_ref (SP_TYPE_ITEM);
94 object_class->dispose = sp_group_dispose;
96 sp_object_class->child_added = sp_group_child_added;
97 sp_object_class->remove_child = sp_group_remove_child;
98 sp_object_class->order_changed = sp_group_order_changed;
99 sp_object_class->update = sp_group_update;
100 sp_object_class->modified = sp_group_modified;
101 sp_object_class->set = sp_group_set;
102 sp_object_class->write = sp_group_write;
103 sp_object_class->release = sp_group_release;
104 sp_object_class->build = sp_group_build;
106 item_class->bbox = sp_group_bbox;
107 item_class->print = sp_group_print;
108 item_class->description = sp_group_description;
109 item_class->show = sp_group_show;
110 item_class->hide = sp_group_hide;
111 item_class->snappoints = sp_group_snappoints;
112 }
114 static void
115 sp_group_init (SPGroup *group)
116 {
117 group->_layer_mode = SPGroup::GROUP;
118 group->group = new CGroup(group);
119 new (&group->_display_modes) std::map<unsigned int, SPGroup::LayerMode>();
120 }
122 static void sp_group_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
123 {
124 sp_object_read_attr(object, "inkscape:groupmode");
126 if (((SPObjectClass *)parent_class)->build) {
127 ((SPObjectClass *)parent_class)->build(object, document, repr);
128 }
129 }
131 static void sp_group_release(SPObject *object) {
132 if ( SP_GROUP(object)->_layer_mode == SPGroup::LAYER ) {
133 sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "layer", object);
134 }
135 if (((SPObjectClass *)parent_class)->release) {
136 ((SPObjectClass *)parent_class)->release(object);
137 }
138 }
140 static void
141 sp_group_dispose(GObject *object)
142 {
143 SP_GROUP(object)->_display_modes.~map();
144 delete SP_GROUP(object)->group;
145 }
147 static void
148 sp_group_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
149 {
150 SPItem *item;
152 item = SP_ITEM (object);
154 if (((SPObjectClass *) (parent_class))->child_added)
155 (* ((SPObjectClass *) (parent_class))->child_added) (object, child, ref);
157 SP_GROUP(object)->group->onChildAdded(child);
158 }
160 /* fixme: hide (Lauris) */
162 static void
163 sp_group_remove_child (SPObject * object, Inkscape::XML::Node * child)
164 {
165 if (((SPObjectClass *) (parent_class))->remove_child)
166 (* ((SPObjectClass *) (parent_class))->remove_child) (object, child);
168 SP_GROUP(object)->group->onChildRemoved(child);
169 }
171 static void
172 sp_group_order_changed (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref)
173 {
174 if (((SPObjectClass *) (parent_class))->order_changed)
175 (* ((SPObjectClass *) (parent_class))->order_changed) (object, child, old_ref, new_ref);
177 SP_GROUP(object)->group->onOrderChanged(child, old_ref, new_ref);
178 }
180 static void
181 sp_group_update (SPObject *object, SPCtx *ctx, unsigned int flags)
182 {
183 if (((SPObjectClass *) (parent_class))->update)
184 ((SPObjectClass *) (parent_class))->update (object, ctx, flags);
186 SP_GROUP(object)->group->onUpdate(ctx, flags);
187 }
189 static void
190 sp_group_modified (SPObject *object, guint flags)
191 {
192 SP_GROUP(object)->group->onModified(flags);
193 }
195 static Inkscape::XML::Node *
196 sp_group_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
197 {
198 SPGroup *group;
199 SPObject *child;
200 Inkscape::XML::Node *crepr;
202 group = SP_GROUP (object);
204 if (flags & SP_OBJECT_WRITE_BUILD) {
205 GSList *l;
206 if (!repr) repr = sp_repr_new ("svg:g");
207 l = NULL;
208 for (child = sp_object_first_child(object); child != NULL; child = SP_OBJECT_NEXT(child) ) {
209 crepr = child->updateRepr(NULL, flags);
210 if (crepr) l = g_slist_prepend (l, crepr);
211 }
212 while (l) {
213 repr->addChild((Inkscape::XML::Node *) l->data, NULL);
214 Inkscape::GC::release((Inkscape::XML::Node *) l->data);
215 l = g_slist_remove (l, l->data);
216 }
217 } else {
218 for (child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
219 child->updateRepr(flags);
220 }
221 }
223 if ( flags & SP_OBJECT_WRITE_EXT ) {
224 const char *value;
225 if ( group->_layer_mode == SPGroup::LAYER ) {
226 value = "layer";
227 } else if ( flags & SP_OBJECT_WRITE_ALL ) {
228 value = "group";
229 } else {
230 value = NULL;
231 }
232 repr->setAttribute("inkscape:groupmode", value);
233 }
235 if (((SPObjectClass *) (parent_class))->write)
236 ((SPObjectClass *) (parent_class))->write (object, repr, flags);
238 return repr;
239 }
241 static void
242 sp_group_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
243 {
244 SP_GROUP(item)->group->calculateBBox(bbox, transform, flags);
245 }
247 static void
248 sp_group_print (SPItem * item, SPPrintContext *ctx)
249 {
250 SP_GROUP(item)->group->onPrint(ctx);
251 }
253 static gchar * sp_group_description (SPItem * item)
254 {
255 return SP_GROUP(item)->group->getDescription();
256 }
258 static void sp_group_set(SPObject *object, unsigned key, char const *value) {
259 SPGroup *group=SP_GROUP(object);
261 switch (key) {
262 case SP_ATTR_INKSCAPE_GROUPMODE: {
263 if ( value && !strcmp(value, "layer") ) {
264 group->setLayerMode(SPGroup::LAYER);
265 } else {
266 group->setLayerMode(SPGroup::GROUP);
267 }
268 } break;
269 default: {
270 if (((SPObjectClass *) (parent_class))->set) {
271 (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
272 }
273 }
274 }
275 }
277 static NRArenaItem *
278 sp_group_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags)
279 {
280 return SP_GROUP(item)->group->show(arena, key, flags);
281 }
283 static void
284 sp_group_hide (SPItem *item, unsigned int key)
285 {
286 SP_GROUP(item)->group->hide(key);
287 }
289 static void sp_group_snappoints (SPItem const *item, SnapPointsIter p)
290 {
291 for (SPObject const *o = sp_object_first_child(SP_OBJECT(item));
292 o != NULL;
293 o = SP_OBJECT_NEXT(o))
294 {
295 if (SP_IS_ITEM(o)) {
296 sp_item_snappoints(SP_ITEM(o), p);
297 }
298 }
299 }
302 void
303 sp_item_group_ungroup (SPGroup *group, GSList **children, bool do_done)
304 {
305 g_return_if_fail (group != NULL);
306 g_return_if_fail (SP_IS_GROUP (group));
308 SPDocument *doc = SP_OBJECT_DOCUMENT (group);
309 SPObject *root = SP_DOCUMENT_ROOT (doc);
310 SPObject *defs = SP_OBJECT (SP_ROOT (root)->defs);
312 SPItem *gitem = SP_ITEM (group);
313 Inkscape::XML::Node *grepr = SP_OBJECT_REPR (gitem);
315 g_return_if_fail (!strcmp (grepr->name(), "svg:g") || !strcmp (grepr->name(), "svg:a") || !strcmp (grepr->name(), "svg:switch"));
317 // this converts the gradient/pattern fill/stroke on the group, if any, to userSpaceOnUse
318 sp_item_adjust_paint_recursive (gitem, NR::identity(), NR::identity(), false);
320 SPItem *pitem = SP_ITEM (SP_OBJECT_PARENT (gitem));
321 Inkscape::XML::Node *prepr = SP_OBJECT_REPR (pitem);
323 /* Step 1 - generate lists of children objects */
324 GSList *items = NULL;
325 GSList *objects = NULL;
326 for (SPObject *child = sp_object_first_child(SP_OBJECT(group)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
328 if (SP_IS_ITEM (child)) {
330 SPItem *citem = SP_ITEM (child);
332 /* Merging of style */
333 // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
334 // it here _before_ the new transform is set, so as to use the pre-transform bbox
335 sp_item_adjust_paint_recursive (citem, NR::identity(), NR::identity(), false);
337 sp_style_merge_from_dying_parent(SP_OBJECT_STYLE(child), SP_OBJECT_STYLE(gitem));
338 /*
339 * fixme: We currently make no allowance for the case where child is cloned
340 * and the group has any style settings.
341 *
342 * (This should never occur with documents created solely with the current
343 * version of inkscape without using the XML editor: we usually apply group
344 * style changes to children rather than to the group itself.)
345 *
346 * If the group has no style settings, then
347 * sp_style_merge_from_dying_parent should be a no-op. Otherwise (i.e. if
348 * we change the child's style to compensate for its parent going away)
349 * then those changes will typically be reflected in any clones of child,
350 * whereas we'd prefer for Ungroup not to affect the visual appearance.
351 *
352 * The only way of preserving styling appearance in general is for child to
353 * be put into a new group -- a somewhat surprising response to an Ungroup
354 * command. We could add a new groupmode:transparent that would mostly
355 * hide the existence of such groups from the user (i.e. editing behaves as
356 * if the transparent group's children weren't in a group), though that's
357 * extra complication & maintenance burden and this case is rare.
358 */
360 child->updateRepr();
362 Inkscape::XML::Node *nrepr = SP_OBJECT_REPR (child)->duplicate();
364 // Merging transform
365 NR::Matrix ctrans;
366 NR::Matrix const g(gitem->transform);
367 if (SP_IS_USE(citem) && sp_use_get_original (SP_USE(citem)) &&
368 SP_OBJECT_PARENT (sp_use_get_original (SP_USE(citem))) == SP_OBJECT(group)) {
369 // make sure a clone's effective transform is the same as was under group
370 ctrans = g.inverse() * citem->transform * g;
371 } else {
372 ctrans = citem->transform * g;
373 }
375 // FIXME: constructing a transform that would fully preserve the appearance of a
376 // textpath if it is ungrouped with its path seems to be impossible in general
377 // case. E.g. if the group was squeezed, to keep the ungrouped textpath squeezed
378 // as well, we'll need to relink it to some "virtual" path which is inversely
379 // stretched relative to the actual path, and then squeeze the textpath back so it
380 // would both fit the actual path _and_ be squeezed as before. It's a bummer.
382 // This is just a way to temporarily remember the transform in repr. When repr is
383 // reattached outside of the group, the transform will be written more properly
384 // (i.e. optimized into the object if the corresponding preference is set)
385 gchar affinestr[80];
386 if (sp_svg_transform_write(affinestr, 79, ctrans)) {
387 nrepr->setAttribute("transform", affinestr);
388 } else {
389 nrepr->setAttribute("transform", NULL);
390 }
392 items = g_slist_prepend (items, nrepr);
394 } else {
395 Inkscape::XML::Node *nrepr = SP_OBJECT_REPR (child)->duplicate();
396 objects = g_slist_prepend (objects, nrepr);
397 }
398 }
400 /* Step 2 - clear group */
401 // remember the position of the group
402 gint pos = SP_OBJECT_REPR(group)->position();
404 // the group is leaving forever, no heir, clones should take note; its children however are going to reemerge
405 SP_OBJECT (group)->deleteObject(true, false);
407 /* Step 3 - add nonitems */
408 if (objects) {
409 Inkscape::XML::Node *last_def = SP_OBJECT_REPR(defs)->lastChild();
410 while (objects) {
411 SP_OBJECT_REPR(defs)->addChild((Inkscape::XML::Node *) objects->data, last_def);
412 Inkscape::GC::release((Inkscape::XML::Node *) objects->data);
413 objects = g_slist_remove (objects, objects->data);
414 }
415 }
417 /* Step 4 - add items */
418 gint const preserve = prefs_get_int_attribute("options.preservetransform", "value", 0);
419 while (items) {
420 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) items->data;
421 // add item
422 prepr->appendChild(repr);
423 // restore position; since the items list was prepended (i.e. reverse), we now add
424 // all children at the same pos, which inverts the order once again
425 repr->setPosition(pos > 0 ? pos : 0);
427 // fill in the children list if non-null
428 SPItem *nitem = (SPItem *) doc->getObjectByRepr(repr);
430 /* Optimize the transform matrix if requested. */
431 // No compensations are required because this is supposed to be a non-transformation visually.
432 if (!preserve) {
433 NR::Matrix (*set_transform)(SPItem *, NR::Matrix const &) = ((SPItemClass *) G_OBJECT_GET_CLASS(nitem))->set_transform;
434 if (set_transform) {
435 sp_item_set_item_transform(nitem, set_transform(nitem, nitem->transform));
436 nitem->updateRepr();
437 }
438 }
440 Inkscape::GC::release(repr);
441 if (children && SP_IS_ITEM (nitem))
442 *children = g_slist_prepend (*children, nitem);
444 items = g_slist_remove (items, items->data);
445 }
447 if (do_done) sp_document_done (doc, SP_VERB_NONE,
448 /* TODO: annotate */ "sp-item-group.cpp:448");
449 }
451 /*
452 * some API for list aspect of SPGroup
453 */
455 GSList *
456 sp_item_group_item_list (SPGroup * group)
457 {
458 GSList *s;
459 SPObject *o;
461 g_return_val_if_fail (group != NULL, NULL);
462 g_return_val_if_fail (SP_IS_GROUP (group), NULL);
464 s = NULL;
466 for ( o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
467 if (SP_IS_ITEM (o)) {
468 s = g_slist_prepend (s, o);
469 }
470 }
472 return g_slist_reverse (s);
473 }
475 SPObject *
476 sp_item_group_get_child_by_name (SPGroup *group, SPObject *ref, const gchar *name)
477 {
478 SPObject *child;
479 child = (ref) ? SP_OBJECT_NEXT(ref) : sp_object_first_child(SP_OBJECT(group));
480 while ( child && strcmp (SP_OBJECT_REPR(child)->name(), name) ) {
481 child = SP_OBJECT_NEXT(child);
482 }
483 return child;
484 }
486 void SPGroup::setLayerMode(LayerMode mode) {
487 if ( _layer_mode != mode ) {
488 if ( mode == LAYER ) {
489 sp_document_add_resource(SP_OBJECT_DOCUMENT(this), "layer", this);
490 } else {
491 sp_document_remove_resource(SP_OBJECT_DOCUMENT(this), "layer", this);
492 }
493 _layer_mode = mode;
494 _updateLayerMode();
495 }
496 }
498 SPGroup::LayerMode SPGroup::layerDisplayMode(unsigned int dkey) const {
499 std::map<unsigned int, LayerMode>::const_iterator iter;
500 iter = _display_modes.find(dkey);
501 if ( iter != _display_modes.end() ) {
502 return (*iter).second;
503 } else {
504 return GROUP;
505 }
506 }
508 void SPGroup::setLayerDisplayMode(unsigned int dkey, SPGroup::LayerMode mode) {
509 if ( layerDisplayMode(dkey) != mode ) {
510 _display_modes[dkey] = mode;
511 _updateLayerMode(dkey);
512 }
513 }
515 void SPGroup::_updateLayerMode(unsigned int display_key) {
516 SPItemView *view;
517 for ( view = this->display ; view ; view = view->next ) {
518 if ( !display_key || view->key == display_key ) {
519 NRArenaGroup *arena_group=NR_ARENA_GROUP(view->arenaitem);
520 if (arena_group) {
521 nr_arena_group_set_transparent(arena_group, effectiveLayerMode(view->key) == SPGroup::LAYER);
522 }
523 }
524 }
525 }
527 void SPGroup::translateChildItems(NR::translate const &tr)
528 {
529 if (this->hasChildren())
530 {
531 SPObject *o = NULL;
532 for (o = sp_object_first_child(SP_OBJECT(this)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
533 if (SP_IS_ITEM (o)) {
534 sp_item_move_rel(static_cast<SPItem *>(o), tr);
535 }
536 }
537 }
538 }
540 CGroup::CGroup(SPGroup *group) {
541 _group = group;
542 }
544 CGroup::~CGroup() {
545 }
547 void CGroup::onChildAdded(Inkscape::XML::Node *child) {
548 SPObject *last_child = _group->lastChild();
549 if (last_child && SP_OBJECT_REPR(last_child) == child) {
550 // optimization for the common special case where the child is being added at the end
551 SPObject *ochild = last_child;
552 if ( SP_IS_ITEM(ochild) ) {
553 /* TODO: this should be moved into SPItem somehow */
554 SPItemView *v;
555 NRArenaItem *ac;
557 for (v = _group->display; v != NULL; v = v->next) {
558 ac = sp_item_invoke_show (SP_ITEM (ochild), NR_ARENA_ITEM_ARENA (v->arenaitem), v->key, v->flags);
560 if (ac) {
561 nr_arena_item_append_child (v->arenaitem, ac);
562 nr_arena_item_unref (ac);
563 }
564 }
565 }
566 } else { // general case
567 SPObject *ochild = sp_object_get_child_by_repr(_group, child);
568 if ( ochild && SP_IS_ITEM(ochild) ) {
569 /* TODO: this should be moved into SPItem somehow */
570 SPItemView *v;
571 NRArenaItem *ac;
573 unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
575 for (v = _group->display; v != NULL; v = v->next) {
576 ac = sp_item_invoke_show (SP_ITEM (ochild), NR_ARENA_ITEM_ARENA (v->arenaitem), v->key, v->flags);
578 if (ac) {
579 nr_arena_item_add_child (v->arenaitem, ac, NULL);
580 nr_arena_item_set_order (ac, position);
581 nr_arena_item_unref (ac);
582 }
583 }
584 }
585 }
587 _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
588 }
590 void CGroup::onChildRemoved(Inkscape::XML::Node */*child*/) {
591 _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
592 }
594 void CGroup::onUpdate(SPCtx *ctx, unsigned int flags) {
595 SPObject *child;
596 SPItemCtx *ictx, cctx;
598 ictx = (SPItemCtx *) ctx;
599 cctx = *ictx;
601 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
602 flags &= SP_OBJECT_MODIFIED_CASCADE;
604 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
605 SPObject *object = SP_OBJECT(_group);
606 for (SPItemView *v = SP_ITEM(_group)->display; v != NULL; v = v->next) {
607 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
608 }
609 }
611 GSList *l = g_slist_reverse(_childList(true, ActionUpdate));
612 while (l) {
613 child = SP_OBJECT (l->data);
614 l = g_slist_remove (l, child);
615 if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
616 if (SP_IS_ITEM (child)) {
617 SPItem const &chi = *SP_ITEM(child);
618 cctx.i2doc = chi.transform * ictx->i2doc;
619 cctx.i2vp = chi.transform * ictx->i2vp;
620 child->updateDisplay((SPCtx *)&cctx, flags);
621 } else {
622 child->updateDisplay(ctx, flags);
623 }
624 }
625 g_object_unref (G_OBJECT (child));
626 }
627 }
629 GSList *CGroup::_childList(bool add_ref, Action) {
630 GSList *l = NULL;
631 for (SPObject *child = sp_object_first_child(_group) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
632 if (add_ref)
633 g_object_ref (G_OBJECT (child));
635 l = g_slist_prepend (l, child);
636 }
637 return l;
638 }
640 void CGroup::onModified(guint flags) {
641 SPObject *child;
643 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
644 flags &= SP_OBJECT_MODIFIED_CASCADE;
646 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
647 SPObject *object = SP_OBJECT(_group);
648 for (SPItemView *v = SP_ITEM(_group)->display; v != NULL; v = v->next) {
649 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
650 }
651 }
653 GSList *l = g_slist_reverse(_childList(true));
654 while (l) {
655 child = SP_OBJECT (l->data);
656 l = g_slist_remove (l, child);
657 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
658 child->emitModified(flags);
659 }
660 g_object_unref (G_OBJECT (child));
661 }
662 }
664 void CGroup::calculateBBox(NRRect *bbox, NR::Matrix const &transform, unsigned const flags) {
665 GSList *l = _childList(false, ActionBBox);
666 while (l) {
667 SPObject *o = SP_OBJECT (l->data);
668 if (SP_IS_ITEM(o)) {
669 SPItem *child = SP_ITEM(o);
670 NR::Matrix const ct(child->transform * transform);
671 sp_item_invoke_bbox_full(child, bbox, ct, flags, FALSE);
672 }
673 l = g_slist_remove (l, o);
674 }
675 }
677 void CGroup::onPrint(SPPrintContext *ctx) {
678 GSList *l = g_slist_reverse(_childList(false));
679 while (l) {
680 SPObject *o = SP_OBJECT (l->data);
681 if (SP_IS_ITEM(o)) {
682 sp_item_invoke_print (SP_ITEM (o), ctx);
683 }
684 l = g_slist_remove (l, o);
685 }
686 }
688 gint CGroup::getItemCount() {
689 gint len = 0;
690 for (SPObject *o = sp_object_first_child(SP_OBJECT(_group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
691 if (SP_IS_ITEM(o)) {
692 len++;
693 }
694 }
696 return len;
697 }
699 gchar *CGroup::getDescription() {
700 gint len = getItemCount();
701 return g_strdup_printf(
702 ngettext("<b>Group</b> of <b>%d</b> object",
703 "<b>Group</b> of <b>%d</b> objects",
704 len), len);
705 }
707 NRArenaItem *CGroup::show (NRArena *arena, unsigned int key, unsigned int flags) {
708 NRArenaItem *ai;
709 SPObject *object = SP_OBJECT(_group);
711 ai = NRArenaGroup::create(arena);
713 nr_arena_group_set_transparent(NR_ARENA_GROUP (ai),
714 _group->effectiveLayerMode(key) ==
715 SPGroup::LAYER);
716 nr_arena_group_set_style(NR_ARENA_GROUP(ai), SP_OBJECT_STYLE(object));
718 _showChildren(arena, ai, key, flags);
719 return ai;
720 }
722 void CGroup::_showChildren (NRArena *arena, NRArenaItem *ai, unsigned int key, unsigned int flags) {
723 NRArenaItem *ac = NULL;
724 NRArenaItem *ar = NULL;
725 SPItem * child = NULL;
726 GSList *l = g_slist_reverse(_childList(false, ActionShow));
727 while (l) {
728 SPObject *o = SP_OBJECT (l->data);
729 if (SP_IS_ITEM (o)) {
730 child = SP_ITEM (o);
731 ac = sp_item_invoke_show (child, arena, key, flags);
732 if (ac) {
733 nr_arena_item_add_child (ai, ac, ar);
734 ar = ac;
735 nr_arena_item_unref (ac);
736 }
737 }
738 l = g_slist_remove (l, o);
739 }
740 }
742 void CGroup::hide (unsigned int key) {
743 SPItem * child;
745 GSList *l = g_slist_reverse(_childList(false, ActionShow));
746 while (l) {
747 SPObject *o = SP_OBJECT (l->data);
748 if (SP_IS_ITEM (o)) {
749 child = SP_ITEM (o);
750 sp_item_invoke_hide (child, key);
751 }
752 l = g_slist_remove (l, o);
753 }
755 if (((SPItemClass *) parent_class)->hide)
756 ((SPItemClass *) parent_class)->hide (_group, key);
757 }
759 void CGroup::onOrderChanged (Inkscape::XML::Node *child, Inkscape::XML::Node *, Inkscape::XML::Node *)
760 {
761 SPObject *ochild = sp_object_get_child_by_repr(_group, child);
762 if ( ochild && SP_IS_ITEM(ochild) ) {
763 /* TODO: this should be moved into SPItem somehow */
764 SPItemView *v;
765 unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
766 for ( v = SP_ITEM (ochild)->display ; v != NULL ; v = v->next ) {
767 nr_arena_item_set_order (v->arenaitem, position);
768 }
769 }
771 _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
772 }