Code

continue switching sp_repr_new* over to XML::Document::create*
[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  *   Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
10  *
11  * Copyright (C) 1999-2006 authors
12  * Copyright (C) 2000-2001 Ximian, Inc.
13  *
14  * Released under GNU GPL, read the file 'COPYING' for more information
15  */
17 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif
21 #include <glibmm/i18n.h>
23 #include "display/nr-arena-group.h"
24 #include "libnr/nr-matrix-ops.h"
25 #include "libnr/nr-matrix-fns.h"
26 #include "xml/repr.h"
27 #include "svg/svg.h"
28 #include "document.h"
29 #include "style.h"
30 #include "attributes.h"
31 #include "sp-item-transform.h"
32 #include "sp-root.h"
33 #include "sp-use.h"
34 #include "prefs-utils.h"
35 #include "sp-clippath.h"
36 #include "sp-mask.h"
38 static void sp_group_class_init (SPGroupClass *klass);
39 static void sp_group_init (SPGroup *group);
40 static void sp_group_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
41 static void sp_group_release(SPObject *object);
42 static void sp_group_dispose(GObject *object);
44 static void sp_group_child_added (SPObject * object, Inkscape::XML::Node * child, Inkscape::XML::Node * ref);
45 static void sp_group_remove_child (SPObject * object, Inkscape::XML::Node * child);
46 static void sp_group_order_changed (SPObject * object, Inkscape::XML::Node * child, Inkscape::XML::Node * old_ref, Inkscape::XML::Node * new_ref);
47 static void sp_group_update (SPObject *object, SPCtx *ctx, guint flags);
48 static void sp_group_modified (SPObject *object, guint flags);
49 static Inkscape::XML::Node *sp_group_write (SPObject *object, Inkscape::XML::Node *repr, guint flags);
50 static void sp_group_set(SPObject *object, unsigned key, char const *value);
52 static void sp_group_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
53 static void sp_group_print (SPItem * item, SPPrintContext *ctx);
54 static gchar * sp_group_description (SPItem * item);
55 static NRArenaItem *sp_group_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags);
56 static void sp_group_hide (SPItem * item, unsigned int key);
57 static void sp_group_snappoints (SPItem const *item, SnapPointsIter p);
59 static SPItemClass * parent_class;
61 GType
62 sp_group_get_type (void)
63 {
64         static GType group_type = 0;
65         if (!group_type) {
66                 GTypeInfo group_info = {
67                         sizeof (SPGroupClass),
68                         NULL,   /* base_init */
69                         NULL,   /* base_finalize */
70                         (GClassInitFunc) sp_group_class_init,
71                         NULL,   /* class_finalize */
72                         NULL,   /* class_data */
73                         sizeof (SPGroup),
74                         16,     /* n_preallocs */
75                         (GInstanceInitFunc) sp_group_init,
76                         NULL,   /* value_table */
77                 };
78                 group_type = g_type_register_static (SP_TYPE_ITEM, "SPGroup", &group_info, (GTypeFlags)0);
79         }
80         return group_type;
81 }
83 static void
84 sp_group_class_init (SPGroupClass *klass)
85 {
86         GObjectClass * object_class;
87         SPObjectClass * sp_object_class;
88         SPItemClass * item_class;
90         object_class = (GObjectClass *) klass;
91         sp_object_class = (SPObjectClass *) klass;
92         item_class = (SPItemClass *) klass;
94         parent_class = (SPItemClass *)g_type_class_ref (SP_TYPE_ITEM);
96         object_class->dispose = sp_group_dispose;
98         sp_object_class->child_added = sp_group_child_added;
99         sp_object_class->remove_child = sp_group_remove_child;
100         sp_object_class->order_changed = sp_group_order_changed;
101         sp_object_class->update = sp_group_update;
102         sp_object_class->modified = sp_group_modified;
103         sp_object_class->set = sp_group_set;
104         sp_object_class->write = sp_group_write;
105         sp_object_class->release = sp_group_release;
106         sp_object_class->build = sp_group_build;
108         item_class->bbox = sp_group_bbox;
109         item_class->print = sp_group_print;
110         item_class->description = sp_group_description;
111         item_class->show = sp_group_show;
112         item_class->hide = sp_group_hide;
113         item_class->snappoints = sp_group_snappoints;
116 static void
117 sp_group_init (SPGroup *group)
119         group->_layer_mode = SPGroup::GROUP;
120     group->group = new CGroup(group);
121         new (&group->_display_modes) std::map<unsigned int, SPGroup::LayerMode>();
124 static void sp_group_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
126         sp_object_read_attr(object, "inkscape:groupmode");
128         if (((SPObjectClass *)parent_class)->build) {
129                 ((SPObjectClass *)parent_class)->build(object, document, repr);
130         }
133 static void sp_group_release(SPObject *object) {
134         if ( SP_GROUP(object)->_layer_mode == SPGroup::LAYER ) {
135                 sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "layer", object);
136         }
137         if (((SPObjectClass *)parent_class)->release) {
138                 ((SPObjectClass *)parent_class)->release(object);
139         }
142 static void
143 sp_group_dispose(GObject *object)
145         SP_GROUP(object)->_display_modes.~map();
146     delete SP_GROUP(object)->group;
149 static void
150 sp_group_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
152         SPItem *item;
154         item = SP_ITEM (object);
156         if (((SPObjectClass *) (parent_class))->child_added)
157                 (* ((SPObjectClass *) (parent_class))->child_added) (object, child, ref);
159     SP_GROUP(object)->group->onChildAdded(child);
162 /* fixme: hide (Lauris) */
164 static void
165 sp_group_remove_child (SPObject * object, Inkscape::XML::Node * child)
167         if (((SPObjectClass *) (parent_class))->remove_child)
168                 (* ((SPObjectClass *) (parent_class))->remove_child) (object, child);
170     SP_GROUP(object)->group->onChildRemoved(child);
173 static void
174 sp_group_order_changed (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref)
176         if (((SPObjectClass *) (parent_class))->order_changed)
177                 (* ((SPObjectClass *) (parent_class))->order_changed) (object, child, old_ref, new_ref);
179     SP_GROUP(object)->group->onOrderChanged(child, old_ref, new_ref);
182 static void
183 sp_group_update (SPObject *object, SPCtx *ctx, unsigned int flags)
185     if (((SPObjectClass *) (parent_class))->update)
186         ((SPObjectClass *) (parent_class))->update (object, ctx, flags);
188     SP_GROUP(object)->group->onUpdate(ctx, flags);
191 static void
192 sp_group_modified (SPObject *object, guint flags)
194     SP_GROUP(object)->group->onModified(flags);
197 static Inkscape::XML::Node *
198 sp_group_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
200         SPGroup *group;
201         SPObject *child;
202         Inkscape::XML::Node *crepr;
204         group = SP_GROUP (object);
206         if (flags & SP_OBJECT_WRITE_BUILD) {
207                 GSList *l;
208                 if (!repr) {
209                     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
210                     repr = xml_doc->createElement("svg:g");
211                 }
212                 l = NULL;
213                 for (child = sp_object_first_child(object); child != NULL; child = SP_OBJECT_NEXT(child) ) {
214                         crepr = child->updateRepr(NULL, flags);
215                         if (crepr) l = g_slist_prepend (l, crepr);
216                 }
217                 while (l) {
218                         repr->addChild((Inkscape::XML::Node *) l->data, NULL);
219                         Inkscape::GC::release((Inkscape::XML::Node *) l->data);
220                         l = g_slist_remove (l, l->data);
221                 }
222         } else {
223                 for (child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
224                         child->updateRepr(flags);
225                 }
226         }
228         if ( flags & SP_OBJECT_WRITE_EXT ) {
229                 const char *value;
230                 if ( group->_layer_mode == SPGroup::LAYER ) {
231                         value = "layer";
232                 } else if ( flags & SP_OBJECT_WRITE_ALL ) {
233                         value = "group";
234                 } else {
235                         value = NULL;
236                 }
237                 repr->setAttribute("inkscape:groupmode", value);
238         }
240         if (((SPObjectClass *) (parent_class))->write)
241                 ((SPObjectClass *) (parent_class))->write (object, repr, flags);
243         return repr;
246 static void
247 sp_group_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
249     SP_GROUP(item)->group->calculateBBox(bbox, transform, flags);
252 static void
253 sp_group_print (SPItem * item, SPPrintContext *ctx)
255     SP_GROUP(item)->group->onPrint(ctx);
258 static gchar * sp_group_description (SPItem * item)
260     return SP_GROUP(item)->group->getDescription();
263 static void sp_group_set(SPObject *object, unsigned key, char const *value) {
264         SPGroup *group=SP_GROUP(object);
266         switch (key) {
267                 case SP_ATTR_INKSCAPE_GROUPMODE: {
268                         if ( value && !strcmp(value, "layer") ) {
269                                 group->setLayerMode(SPGroup::LAYER);
270                         } else {
271                                 group->setLayerMode(SPGroup::GROUP);
272                         }
273                 } break;
274                 default: {
275                         if (((SPObjectClass *) (parent_class))->set) {
276                                 (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
277                         }
278                 }
279         }
282 static NRArenaItem *
283 sp_group_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags)
285     return SP_GROUP(item)->group->show(arena, key, flags);
288 static void
289 sp_group_hide (SPItem *item, unsigned int key)
291     SP_GROUP(item)->group->hide(key);
294 static void sp_group_snappoints (SPItem const *item, SnapPointsIter p)
296         for (SPObject const *o = sp_object_first_child(SP_OBJECT(item));
297              o != NULL;
298              o = SP_OBJECT_NEXT(o))
299         {
300                 if (SP_IS_ITEM(o)) {
301                         sp_item_snappoints(SP_ITEM(o), p);
302                 }
303         }
307 void
308 sp_item_group_ungroup (SPGroup *group, GSList **children, bool do_done)
310         g_return_if_fail (group != NULL);
311         g_return_if_fail (SP_IS_GROUP (group));
313         SPDocument *doc = SP_OBJECT_DOCUMENT (group);
314         SPObject *root = SP_DOCUMENT_ROOT (doc);
315         SPObject *defs = SP_OBJECT (SP_ROOT (root)->defs);
317         SPItem *gitem = SP_ITEM (group);
318         Inkscape::XML::Node *grepr = SP_OBJECT_REPR (gitem);
320         g_return_if_fail (!strcmp (grepr->name(), "svg:g") || !strcmp (grepr->name(), "svg:a") || !strcmp (grepr->name(), "svg:switch"));
322       // this converts the gradient/pattern fill/stroke on the group, if any, to userSpaceOnUse
323       sp_item_adjust_paint_recursive (gitem, NR::identity(), NR::identity(), false);
325         SPItem *pitem = SP_ITEM (SP_OBJECT_PARENT (gitem));
326         Inkscape::XML::Node *prepr = SP_OBJECT_REPR (pitem);
328         /* Step 1 - generate lists of children objects */
329         GSList *items = NULL;
330         GSList *objects = NULL;
331         for (SPObject *child = sp_object_first_child(SP_OBJECT(group)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
333                 if (SP_IS_ITEM (child)) {
335                         SPItem *citem = SP_ITEM (child);
337                         /* Merging of style */
338                         // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
339                         // it here _before_ the new transform is set, so as to use the pre-transform bbox
340                         sp_item_adjust_paint_recursive (citem, NR::identity(), NR::identity(), false);
342                         sp_style_merge_from_dying_parent(SP_OBJECT_STYLE(child), SP_OBJECT_STYLE(gitem));
343                         /*
344                          * fixme: We currently make no allowance for the case where child is cloned
345                          * and the group has any style settings.
346                          *
347                          * (This should never occur with documents created solely with the current
348                          * version of inkscape without using the XML editor: we usually apply group
349                          * style changes to children rather than to the group itself.)
350                          *
351                          * If the group has no style settings, then
352                          * sp_style_merge_from_dying_parent should be a no-op.  Otherwise (i.e. if
353                          * we change the child's style to compensate for its parent going away)
354                          * then those changes will typically be reflected in any clones of child,
355                          * whereas we'd prefer for Ungroup not to affect the visual appearance.
356                          *
357                          * The only way of preserving styling appearance in general is for child to
358                          * be put into a new group -- a somewhat surprising response to an Ungroup
359                          * command.  We could add a new groupmode:transparent that would mostly
360                          * hide the existence of such groups from the user (i.e. editing behaves as
361                          * if the transparent group's children weren't in a group), though that's
362                          * extra complication & maintenance burden and this case is rare.
363                          */
365                         child->updateRepr();
367                         Inkscape::XML::Node *nrepr = SP_OBJECT_REPR (child)->duplicate();
369                         // Merging transform
370                         NR::Matrix ctrans;
371                         NR::Matrix const g(gitem->transform);
372                         if (SP_IS_USE(citem) && sp_use_get_original (SP_USE(citem)) &&
373                                         SP_OBJECT_PARENT (sp_use_get_original (SP_USE(citem))) == SP_OBJECT(group)) {
374                                 // make sure a clone's effective transform is the same as was under group
375                                 ctrans = g.inverse() * citem->transform * g;
376                         } else {
377                                 ctrans = citem->transform * g;
378                         }
380                         // FIXME: constructing a transform that would fully preserve the appearance of a
381                         // textpath if it is ungrouped with its path seems to be impossible in general
382                         // case. E.g. if the group was squeezed, to keep the ungrouped textpath squeezed
383                         // as well, we'll need to relink it to some "virtual" path which is inversely
384                         // stretched relative to the actual path, and then squeeze the textpath back so it
385                         // would both fit the actual path _and_ be squeezed as before. It's a bummer.
387                         // This is just a way to temporarily remember the transform in repr. When repr is
388                         // reattached outside of the group, the transform will be written more properly
389                         // (i.e. optimized into the object if the corresponding preference is set)
390                         gchar affinestr[80];
391                         if (sp_svg_transform_write(affinestr, 79, ctrans)) {
392                                 nrepr->setAttribute("transform", affinestr);
393                         } else {
394                                 nrepr->setAttribute("transform", NULL);
395                         }
397                         items = g_slist_prepend (items, nrepr);
399                 } else {
400                         Inkscape::XML::Node *nrepr = SP_OBJECT_REPR (child)->duplicate();
401                         objects = g_slist_prepend (objects, nrepr);
402                 }
403         }
405         /* Step 2 - clear group */
406         // remember the position of the group
407         gint pos = SP_OBJECT_REPR(group)->position();
409         // the group is leaving forever, no heir, clones should take note; its children however are going to reemerge
410         SP_OBJECT (group)->deleteObject(true, false);
412         /* Step 3 - add nonitems */
413         if (objects) {
414             Inkscape::XML::Node *last_def = SP_OBJECT_REPR(defs)->lastChild();
415             while (objects) {
416                 SP_OBJECT_REPR(defs)->addChild((Inkscape::XML::Node *) objects->data, last_def);
417                 Inkscape::GC::release((Inkscape::XML::Node *) objects->data);
418                 objects = g_slist_remove (objects, objects->data);
419             }
420         }
422         /* Step 4 - add items */
423         gint const preserve = prefs_get_int_attribute("options.preservetransform", "value", 0);
424         while (items) {
425                 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) items->data;
426                 // add item
427                 prepr->appendChild(repr);
428                 // restore position; since the items list was prepended (i.e. reverse), we now add
429                 // all children at the same pos, which inverts the order once again
430                 repr->setPosition(pos > 0 ? pos : 0);
432                 // fill in the children list if non-null
433                 SPItem *item = (SPItem *) doc->getObjectByRepr(repr);
435                 sp_item_write_transform(item, repr, item->transform, NULL, false);
436                 
437                 Inkscape::GC::release(repr);
438                 if (children && SP_IS_ITEM (item))
439                         *children = g_slist_prepend (*children, item);
441                 items = g_slist_remove (items, items->data);
442         }
444         if (do_done) 
445                 sp_document_done (doc, SP_VERB_NONE, _("Ungroup"));
448 /*
449  * some API for list aspect of SPGroup
450  */
452 GSList *
453 sp_item_group_item_list (SPGroup * group)
455         GSList *s;
456         SPObject *o;
458         g_return_val_if_fail (group != NULL, NULL);
459         g_return_val_if_fail (SP_IS_GROUP (group), NULL);
461         s = NULL;
463         for ( o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
464                 if (SP_IS_ITEM (o)) {
465                         s = g_slist_prepend (s, o);
466                 }
467         }
469         return g_slist_reverse (s);
472 SPObject *
473 sp_item_group_get_child_by_name (SPGroup *group, SPObject *ref, const gchar *name)
475         SPObject *child;
476         child = (ref) ? SP_OBJECT_NEXT(ref) : sp_object_first_child(SP_OBJECT(group));
477         while ( child && strcmp (SP_OBJECT_REPR(child)->name(), name) ) {
478                 child = SP_OBJECT_NEXT(child);
479         }
480         return child;
483 void SPGroup::setLayerMode(LayerMode mode) {
484         if ( _layer_mode != mode ) {
485                 if ( mode == LAYER ) {
486                         sp_document_add_resource(SP_OBJECT_DOCUMENT(this), "layer", this);
487                 } else {
488                         sp_document_remove_resource(SP_OBJECT_DOCUMENT(this), "layer", this);
489                 }
490                 _layer_mode = mode;
491                 _updateLayerMode();
492         }
495 SPGroup::LayerMode SPGroup::layerDisplayMode(unsigned int dkey) const {
496         std::map<unsigned int, LayerMode>::const_iterator iter;
497         iter = _display_modes.find(dkey);
498         if ( iter != _display_modes.end() ) {
499                 return (*iter).second;
500         } else {
501                 return GROUP;
502         }
505 void SPGroup::setLayerDisplayMode(unsigned int dkey, SPGroup::LayerMode mode) {
506         if ( layerDisplayMode(dkey) != mode ) {
507                 _display_modes[dkey] = mode;
508                 _updateLayerMode(dkey);
509         }
512 void SPGroup::_updateLayerMode(unsigned int display_key) {
513         SPItemView *view;
514         for ( view = this->display ; view ; view = view->next ) {
515                 if ( !display_key || view->key == display_key ) {
516                         NRArenaGroup *arena_group=NR_ARENA_GROUP(view->arenaitem);
517                         if (arena_group) {
518                                 nr_arena_group_set_transparent(arena_group, effectiveLayerMode(view->key) == SPGroup::LAYER);
519                         }
520                 }
521         }
524 void SPGroup::translateChildItems(NR::translate const &tr)
526         if (this->hasChildren())
527         {
528                 SPObject *o = NULL;
529                 for (o = sp_object_first_child(SP_OBJECT(this)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
530                         if (SP_IS_ITEM (o)) {
531                                 sp_item_move_rel(static_cast<SPItem *>(o), tr);
532                         }
533                 }
534         }
537 CGroup::CGroup(SPGroup *group) {
538     _group = group;
541 CGroup::~CGroup() {
544 void CGroup::onChildAdded(Inkscape::XML::Node *child) {
545     SPObject *last_child = _group->lastChild();
546     if (last_child && SP_OBJECT_REPR(last_child) == child) {
547         // optimization for the common special case where the child is being added at the end
548         SPObject *ochild = last_child;
549         if ( SP_IS_ITEM(ochild) ) {
550             /* TODO: this should be moved into SPItem somehow */
551             SPItemView *v;
552             NRArenaItem *ac;
554             for (v = _group->display; v != NULL; v = v->next) {
555                 ac = sp_item_invoke_show (SP_ITEM (ochild), NR_ARENA_ITEM_ARENA (v->arenaitem), v->key, v->flags);
557                 if (ac) {
558                     nr_arena_item_append_child (v->arenaitem, ac);
559                     nr_arena_item_unref (ac);
560                 }
561             }
562         }
563     } else {    // general case
564         SPObject *ochild = sp_object_get_child_by_repr(_group, child);
565         if ( ochild && SP_IS_ITEM(ochild) ) {
566             /* TODO: this should be moved into SPItem somehow */
567             SPItemView *v;
568             NRArenaItem *ac;
570             unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
572             for (v = _group->display; v != NULL; v = v->next) {
573                 ac = sp_item_invoke_show (SP_ITEM (ochild), NR_ARENA_ITEM_ARENA (v->arenaitem), v->key, v->flags);
575                 if (ac) {
576                     nr_arena_item_add_child (v->arenaitem, ac, NULL);
577                     nr_arena_item_set_order (ac, position);
578                     nr_arena_item_unref (ac);
579                 }
580             }
581         }
582     }
584     _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
587 void CGroup::onChildRemoved(Inkscape::XML::Node */*child*/) {
588     _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
591 void CGroup::onUpdate(SPCtx *ctx, unsigned int flags) {    
592     SPItemCtx *ictx, cctx;
594     ictx = (SPItemCtx *) ctx;
595     cctx = *ictx;
597     if (flags & SP_OBJECT_MODIFIED_FLAG) {
598       flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
599     }
601     flags &= SP_OBJECT_MODIFIED_CASCADE;
603     if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
604       SPObject *object = SP_OBJECT(_group);
605       for (SPItemView *v = SP_ITEM(_group)->display; v != NULL; v = v->next) {
606         nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
607       }
608     }
610     GSList *l = g_slist_reverse(_group->childList(true, SPObject::ActionUpdate));
611     while (l) {
612         SPObject *child = SP_OBJECT (l->data);
613         l = g_slist_remove (l, child);
614         if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
615             if (SP_IS_ITEM (child)) {
616                 SPItem const &chi = *SP_ITEM(child);
617                 cctx.i2doc = chi.transform * ictx->i2doc;
618                 cctx.i2vp = chi.transform * ictx->i2vp;
619                 child->updateDisplay((SPCtx *)&cctx, flags);
620             } else {
621                 child->updateDisplay(ctx, flags);
622             }
623         }
624         g_object_unref (G_OBJECT (child));
625     }
628 void CGroup::onModified(guint flags) {
629     SPObject *child;
631     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
632     flags &= SP_OBJECT_MODIFIED_CASCADE;
634     if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
635       SPObject *object = SP_OBJECT(_group);
636       for (SPItemView *v = SP_ITEM(_group)->display; v != NULL; v = v->next) {
637         nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
638       }
639     }
641     GSList *l = g_slist_reverse(_group->childList(true));
642     while (l) {
643         child = SP_OBJECT (l->data);
644         l = g_slist_remove (l, child);
645         if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
646             child->emitModified(flags);
647         }
648         g_object_unref (G_OBJECT (child));
649     }
652 void CGroup::calculateBBox(NRRect *bbox, NR::Matrix const &transform, unsigned const flags) {
653     GSList *l = _group->childList(false, SPObject::ActionBBox);
654     while (l) {
655         SPObject *o = SP_OBJECT (l->data);
656         if (SP_IS_ITEM(o)) {
657             SPItem *child = SP_ITEM(o);
658             NR::Matrix const ct(child->transform * transform);
659             sp_item_invoke_bbox_full(child, bbox, ct, flags, FALSE);
660         }        
661         l = g_slist_remove (l, o);
662     }
665 void CGroup::onPrint(SPPrintContext *ctx) {
666     GSList *l = g_slist_reverse(_group->childList(false));
667     while (l) {
668         SPObject *o = SP_OBJECT (l->data);
669         if (SP_IS_ITEM(o)) {
670             sp_item_invoke_print (SP_ITEM (o), ctx);
671         }        
672         l = g_slist_remove (l, o);
673     }
676 gint CGroup::getItemCount() {
677     gint len = 0;
678     for (SPObject *o = sp_object_first_child(SP_OBJECT(_group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
679         if (SP_IS_ITEM(o)) {
680             len++;
681         }
682     }
683     
684     return len;
687 gchar *CGroup::getDescription() {
688     gint len = getItemCount();
689     return g_strdup_printf(
690             ngettext("<b>Group</b> of <b>%d</b> object",
691                  "<b>Group</b> of <b>%d</b> objects",
692                  len), len);
695 NRArenaItem *CGroup::show (NRArena *arena, unsigned int key, unsigned int flags) {
696     NRArenaItem *ai;
697     SPObject *object = SP_OBJECT(_group);
699     ai = NRArenaGroup::create(arena);
701     nr_arena_group_set_transparent(NR_ARENA_GROUP (ai),
702                                    _group->effectiveLayerMode(key) ==
703                          SPGroup::LAYER);
704     nr_arena_group_set_style(NR_ARENA_GROUP(ai), SP_OBJECT_STYLE(object));
706     _showChildren(arena, ai, key, flags);
707     return ai;
710 void CGroup::_showChildren (NRArena *arena, NRArenaItem *ai, unsigned int key, unsigned int flags) {
711     NRArenaItem *ac = NULL;
712     NRArenaItem *ar = NULL;
713     SPItem * child = NULL;
714     GSList *l = g_slist_reverse(_group->childList(false, SPObject::ActionShow));
715     while (l) {
716         SPObject *o = SP_OBJECT (l->data);
717         if (SP_IS_ITEM (o)) {
718             child = SP_ITEM (o);
719             ac = sp_item_invoke_show (child, arena, key, flags);
720             if (ac) {
721                 nr_arena_item_add_child (ai, ac, ar);
722                 ar = ac;
723                 nr_arena_item_unref (ac);
724             }
725         }
726         l = g_slist_remove (l, o);
727     }
730 void CGroup::hide (unsigned int key) {
731     SPItem * child;
733     GSList *l = g_slist_reverse(_group->childList(false, SPObject::ActionShow));
734     while (l) {
735         SPObject *o = SP_OBJECT (l->data);
736         if (SP_IS_ITEM (o)) {
737             child = SP_ITEM (o);
738             sp_item_invoke_hide (child, key);
739         }
740         l = g_slist_remove (l, o);
741     }
743     if (((SPItemClass *) parent_class)->hide)
744         ((SPItemClass *) parent_class)->hide (_group, key);
747 void CGroup::onOrderChanged (Inkscape::XML::Node *child, Inkscape::XML::Node *, Inkscape::XML::Node *)
749     SPObject *ochild = sp_object_get_child_by_repr(_group, child);
750     if ( ochild && SP_IS_ITEM(ochild) ) {
751         /* TODO: this should be moved into SPItem somehow */
752         SPItemView *v;
753         unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
754         for ( v = SP_ITEM (ochild)->display ; v != NULL ; v = v->next ) {
755             nr_arena_item_set_order (v->arenaitem, position);
756         }
757     }
759     _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
762 /*
763   Local Variables:
764   mode:c++
765   c-file-style:"stroustrup"
766   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
767   indent-tabs-mode:nil
768   fill-column:99
769   End:
770 */
771 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :