Code

Applied patch #1501709
[inkscape.git] / src / sp-item-group.cpp
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;
114 static void
115 sp_group_init (SPGroup *group)
117         group->_layer_mode = SPGroup::GROUP;
118     group->group = new CGroup(group);
119         new (&group->_display_modes) std::map<unsigned int, SPGroup::LayerMode>();
122 static void sp_group_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
124         sp_object_read_attr(object, "inkscape:groupmode");
126         if (((SPObjectClass *)parent_class)->build) {
127                 ((SPObjectClass *)parent_class)->build(object, document, repr);
128         }
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         }
140 static void
141 sp_group_dispose(GObject *object)
143         SP_GROUP(object)->_display_modes.~map();
144     delete SP_GROUP(object)->group;
147 static void
148 sp_group_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
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);
160 /* fixme: hide (Lauris) */
162 static void
163 sp_group_remove_child (SPObject * object, Inkscape::XML::Node * child)
165         if (((SPObjectClass *) (parent_class))->remove_child)
166                 (* ((SPObjectClass *) (parent_class))->remove_child) (object, child);
168     SP_GROUP(object)->group->onChildRemoved(child);
171 static void
172 sp_group_order_changed (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref)
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);
180 static void
181 sp_group_update (SPObject *object, SPCtx *ctx, unsigned int flags)
183     if (((SPObjectClass *) (parent_class))->update)
184         ((SPObjectClass *) (parent_class))->update (object, ctx, flags);
186     SP_GROUP(object)->group->onUpdate(ctx, flags);
189 static void
190 sp_group_modified (SPObject *object, guint flags)
192     SP_GROUP(object)->group->onModified(flags);
195 static Inkscape::XML::Node *
196 sp_group_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
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;
241 static void
242 sp_group_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
244     SP_GROUP(item)->group->calculateBBox(bbox, transform, flags);
247 static void
248 sp_group_print (SPItem * item, SPPrintContext *ctx)
250     SP_GROUP(item)->group->onPrint(ctx);
253 static gchar * sp_group_description (SPItem * item)
255     return SP_GROUP(item)->group->getDescription();
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         }
277 static NRArenaItem *
278 sp_group_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags)
280     return SP_GROUP(item)->group->show(arena, key, flags);
283 static void
284 sp_group_hide (SPItem *item, unsigned int key)
286     SP_GROUP(item)->group->hide(key);
289 static void sp_group_snappoints (SPItem const *item, SnapPointsIter p)
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         }
302 void
303 sp_item_group_ungroup (SPGroup *group, GSList **children, bool do_done)
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);
450 /*
451  * some API for list aspect of SPGroup
452  */
454 GSList *
455 sp_item_group_item_list (SPGroup * group)
457         GSList *s;
458         SPObject *o;
460         g_return_val_if_fail (group != NULL, NULL);
461         g_return_val_if_fail (SP_IS_GROUP (group), NULL);
463         s = NULL;
465         for ( o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
466                 if (SP_IS_ITEM (o)) {
467                         s = g_slist_prepend (s, o);
468                 }
469         }
471         return g_slist_reverse (s);
474 SPObject *
475 sp_item_group_get_child_by_name (SPGroup *group, SPObject *ref, const gchar *name)
477         SPObject *child;
478         child = (ref) ? SP_OBJECT_NEXT(ref) : sp_object_first_child(SP_OBJECT(group));
479         while ( child && strcmp (SP_OBJECT_REPR(child)->name(), name) ) {
480                 child = SP_OBJECT_NEXT(child);
481         }
482         return child;
485 void SPGroup::setLayerMode(LayerMode mode) {
486         if ( _layer_mode != mode ) {
487                 if ( mode == LAYER ) {
488                         sp_document_add_resource(SP_OBJECT_DOCUMENT(this), "layer", this);
489                 } else {
490                         sp_document_remove_resource(SP_OBJECT_DOCUMENT(this), "layer", this);
491                 }
492                 _layer_mode = mode;
493                 _updateLayerMode();
494         }
497 SPGroup::LayerMode SPGroup::layerDisplayMode(unsigned int dkey) const {
498         std::map<unsigned int, LayerMode>::const_iterator iter;
499         iter = _display_modes.find(dkey);
500         if ( iter != _display_modes.end() ) {
501                 return (*iter).second;
502         } else {
503                 return GROUP;
504         }
507 void SPGroup::setLayerDisplayMode(unsigned int dkey, SPGroup::LayerMode mode) {
508         if ( layerDisplayMode(dkey) != mode ) {
509                 _display_modes[dkey] = mode;
510                 _updateLayerMode(dkey);
511         }
514 void SPGroup::_updateLayerMode(unsigned int display_key) {
515         SPItemView *view;
516         for ( view = this->display ; view ; view = view->next ) {
517                 if ( !display_key || view->key == display_key ) {
518                         NRArenaGroup *arena_group=NR_ARENA_GROUP(view->arenaitem);
519                         if (arena_group) {
520                                 nr_arena_group_set_transparent(arena_group, effectiveLayerMode(view->key) == SPGroup::LAYER);
521                         }
522                 }
523         }
526 void SPGroup::translateChildItems(NR::translate const &tr)
528         if (this->hasChildren())
529         {
530                 SPObject *o = NULL;
531                 for (o = sp_object_first_child(SP_OBJECT(this)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
532                         if (SP_IS_ITEM (o)) {
533                                 sp_item_move_rel(static_cast<SPItem *>(o), tr);
534                         }
535                 }
536         }
539 CGroup::CGroup(SPGroup *group) {
540     _group = group;
543 CGroup::~CGroup() {
546 void CGroup::onChildAdded(Inkscape::XML::Node *child) {
547     SPObject *last_child = _group->lastChild();
548     if (last_child && SP_OBJECT_REPR(last_child) == child) {
549         // optimization for the common special case where the child is being added at the end
550         SPObject *ochild = last_child;
551         if ( SP_IS_ITEM(ochild) ) {
552             /* TODO: this should be moved into SPItem somehow */
553             SPItemView *v;
554             NRArenaItem *ac;
556             for (v = _group->display; v != NULL; v = v->next) {
557                 ac = sp_item_invoke_show (SP_ITEM (ochild), NR_ARENA_ITEM_ARENA (v->arenaitem), v->key, v->flags);
559                 if (ac) {
560                     nr_arena_item_append_child (v->arenaitem, ac);
561                     nr_arena_item_unref (ac);
562                 }
563             }
564         }
565     } else {    // general case
566         SPObject *ochild = sp_object_get_child_by_repr(_group, child);
567         if ( ochild && SP_IS_ITEM(ochild) ) {
568             /* TODO: this should be moved into SPItem somehow */
569             SPItemView *v;
570             NRArenaItem *ac;
572             unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
574             for (v = _group->display; v != NULL; v = v->next) {
575                 ac = sp_item_invoke_show (SP_ITEM (ochild), NR_ARENA_ITEM_ARENA (v->arenaitem), v->key, v->flags);
577                 if (ac) {
578                     nr_arena_item_add_child (v->arenaitem, ac, NULL);
579                     nr_arena_item_set_order (ac, position);
580                     nr_arena_item_unref (ac);
581                 }
582             }
583         }
584     }
586     _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
589 void CGroup::onChildRemoved(Inkscape::XML::Node */*child*/) {
590     _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
593 void CGroup::onUpdate(SPCtx *ctx, unsigned int flags) {    
594     SPObject *child;
595     SPItemCtx *ictx, cctx;
597     ictx = (SPItemCtx *) ctx;
598     cctx = *ictx;
600     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
601     flags &= SP_OBJECT_MODIFIED_CASCADE;
603     GSList *l = g_slist_reverse(_childList(true, ActionUpdate));
604     while (l) {
605         child = SP_OBJECT (l->data);
606         l = g_slist_remove (l, child);
607         if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
608             if (SP_IS_ITEM (child)) {
609                 SPItem const &chi = *SP_ITEM(child);
610                 cctx.i2doc = chi.transform * ictx->i2doc;
611                 cctx.i2vp = chi.transform * ictx->i2vp;
612                 child->updateDisplay((SPCtx *)&cctx, flags);
613             } else {
614                 child->updateDisplay(ctx, flags);
615             }
616         }
617         g_object_unref (G_OBJECT (child));
618     }
621 GSList *CGroup::_childList(bool add_ref, Action) {
622     GSList *l = NULL;
623     for (SPObject *child = sp_object_first_child(_group) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
624         if (add_ref)
625             g_object_ref (G_OBJECT (child));
627         l = g_slist_prepend (l, child);
628     }
629     return l;
632 void CGroup::onModified(guint flags) {
633     SPObject *child;
635     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
636     flags &= SP_OBJECT_MODIFIED_CASCADE;
638     GSList *l = g_slist_reverse(_childList(true));
639     while (l) {
640         child = SP_OBJECT (l->data);
641         l = g_slist_remove (l, child);
642         if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
643             child->emitModified(flags);
644         }
645         g_object_unref (G_OBJECT (child));
646     }
649 void CGroup::calculateBBox(NRRect *bbox, NR::Matrix const &transform, unsigned const flags) {
650     GSList *l = _childList(false, ActionBBox);
651     while (l) {
652         SPObject *o = SP_OBJECT (l->data);
653         if (SP_IS_ITEM(o)) {
654             SPItem *child = SP_ITEM(o);
655             NR::Matrix const ct(child->transform * transform);
656             sp_item_invoke_bbox_full(child, bbox, ct, flags, FALSE);
657         }        
658         l = g_slist_remove (l, o);
659     }
662 void CGroup::onPrint(SPPrintContext *ctx) {
663     GSList *l = g_slist_reverse(_childList(false));
664     while (l) {
665         SPObject *o = SP_OBJECT (l->data);
666         if (SP_IS_ITEM(o)) {
667             sp_item_invoke_print (SP_ITEM (o), ctx);
668         }        
669         l = g_slist_remove (l, o);
670     }
673 gint CGroup::getItemCount() {
674     gint len = 0;
675     for (SPObject *o = sp_object_first_child(SP_OBJECT(_group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
676         if (SP_IS_ITEM(o)) {
677             len++;
678         }
679     }
680     
681     return len;
684 gchar *CGroup::getDescription() {
685     gint len = getItemCount();
686     return g_strdup_printf(
687             ngettext("<b>Group</b> of <b>%d</b> object",
688                  "<b>Group</b> of <b>%d</b> objects",
689                  len), len);
692 NRArenaItem *CGroup::show (NRArena *arena, unsigned int key, unsigned int flags) {
693     NRArenaItem *ai;
695     ai = NRArenaGroup::create(arena);
696     nr_arena_group_set_transparent(NR_ARENA_GROUP (ai),
697                                    _group->effectiveLayerMode(key) ==
698                          SPGroup::LAYER);
700     _showChildren(arena, ai, key, flags);
701     return ai;
704 void CGroup::_showChildren (NRArena *arena, NRArenaItem *ai, unsigned int key, unsigned int flags) {
705     NRArenaItem *ac = NULL;
706     NRArenaItem *ar = NULL;
707     SPItem * child = NULL;
708     GSList *l = g_slist_reverse(_childList(false, ActionShow));
709     while (l) {
710         SPObject *o = SP_OBJECT (l->data);
711         if (SP_IS_ITEM (o)) {
712             child = SP_ITEM (o);
713             ac = sp_item_invoke_show (child, arena, key, flags);
714             if (ac) {
715                 nr_arena_item_add_child (ai, ac, ar);
716                 ar = ac;
717                 nr_arena_item_unref (ac);
718             }
719         }
720         l = g_slist_remove (l, o);
721     }
724 void CGroup::hide (unsigned int key) {
725     SPItem * child;
727     GSList *l = g_slist_reverse(_childList(false, ActionShow));
728     while (l) {
729         SPObject *o = SP_OBJECT (l->data);
730         if (SP_IS_ITEM (o)) {
731             child = SP_ITEM (o);
732             sp_item_invoke_hide (child, key);
733         }
734         l = g_slist_remove (l, o);
735     }
737     if (((SPItemClass *) parent_class)->hide)
738         ((SPItemClass *) parent_class)->hide (_group, key);
741 void CGroup::onOrderChanged (Inkscape::XML::Node *child, Inkscape::XML::Node *, Inkscape::XML::Node *)
743     SPObject *ochild = sp_object_get_child_by_repr(_group, child);
744     if ( ochild && SP_IS_ITEM(ochild) ) {
745         /* TODO: this should be moved into SPItem somehow */
746         SPItemView *v;
747         unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
748         for ( v = SP_ITEM (ochild)->display ; v != NULL ; v = v->next ) {
749             nr_arena_item_set_order (v->arenaitem, position);
750         }
751     }
753     _group->requestModified(SP_OBJECT_MODIFIED_FLAG);