Code

whitespace
[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"
31 #include "sp-root.h"
32 #include "sp-use.h"
33 #include "prefs-utils.h"
34 #include "sp-clippath.h"
35 #include "sp-mask.h"
37 static void sp_group_class_init (SPGroupClass *klass);
38 static void sp_group_init (SPGroup *group);
39 static void sp_group_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
40 static void sp_group_release(SPObject *object);
41 static void sp_group_dispose(GObject *object);
43 static void sp_group_child_added (SPObject * object, Inkscape::XML::Node * child, Inkscape::XML::Node * ref);
44 static void sp_group_remove_child (SPObject * object, Inkscape::XML::Node * child);
45 static void sp_group_order_changed (SPObject * object, Inkscape::XML::Node * child, Inkscape::XML::Node * old_ref, Inkscape::XML::Node * new_ref);
46 static void sp_group_update (SPObject *object, SPCtx *ctx, guint flags);
47 static void sp_group_modified (SPObject *object, guint flags);
48 static Inkscape::XML::Node *sp_group_write (SPObject *object, Inkscape::XML::Node *repr, guint flags);
49 static void sp_group_set(SPObject *object, unsigned key, char const *value);
51 static void sp_group_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
52 static void sp_group_print (SPItem * item, SPPrintContext *ctx);
53 static gchar * sp_group_description (SPItem * item);
54 static NRArenaItem *sp_group_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags);
55 static void sp_group_hide (SPItem * item, unsigned int key);
56 static void sp_group_snappoints (SPItem const *item, SnapPointsIter p);
58 static SPItemClass * parent_class;
60 GType
61 sp_group_get_type (void)
62 {
63         static GType group_type = 0;
64         if (!group_type) {
65                 GTypeInfo group_info = {
66                         sizeof (SPGroupClass),
67                         NULL,   /* base_init */
68                         NULL,   /* base_finalize */
69                         (GClassInitFunc) sp_group_class_init,
70                         NULL,   /* class_finalize */
71                         NULL,   /* class_data */
72                         sizeof (SPGroup),
73                         16,     /* n_preallocs */
74                         (GInstanceInitFunc) sp_group_init,
75                         NULL,   /* value_table */
76                 };
77                 group_type = g_type_register_static (SP_TYPE_ITEM, "SPGroup", &group_info, (GTypeFlags)0);
78         }
79         return group_type;
80 }
82 static void
83 sp_group_class_init (SPGroupClass *klass)
84 {
85         GObjectClass * object_class;
86         SPObjectClass * sp_object_class;
87         SPItemClass * item_class;
89         object_class = (GObjectClass *) klass;
90         sp_object_class = (SPObjectClass *) klass;
91         item_class = (SPItemClass *) klass;
93         parent_class = (SPItemClass *)g_type_class_ref (SP_TYPE_ITEM);
95         object_class->dispose = sp_group_dispose;
97         sp_object_class->child_added = sp_group_child_added;
98         sp_object_class->remove_child = sp_group_remove_child;
99         sp_object_class->order_changed = sp_group_order_changed;
100         sp_object_class->update = sp_group_update;
101         sp_object_class->modified = sp_group_modified;
102         sp_object_class->set = sp_group_set;
103         sp_object_class->write = sp_group_write;
104         sp_object_class->release = sp_group_release;
105         sp_object_class->build = sp_group_build;
107         item_class->bbox = sp_group_bbox;
108         item_class->print = sp_group_print;
109         item_class->description = sp_group_description;
110         item_class->show = sp_group_show;
111         item_class->hide = sp_group_hide;
112         item_class->snappoints = sp_group_snappoints;
115 static void
116 sp_group_init (SPGroup *group)
118         group->_layer_mode = SPGroup::GROUP;
119     group->group = new CGroup(group);
120         new (&group->_display_modes) std::map<unsigned int, SPGroup::LayerMode>();
123 static void sp_group_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
125         sp_object_read_attr(object, "inkscape:groupmode");
127         if (((SPObjectClass *)parent_class)->build) {
128                 ((SPObjectClass *)parent_class)->build(object, document, repr);
129         }
132 static void sp_group_release(SPObject *object) {
133         if ( SP_GROUP(object)->_layer_mode == SPGroup::LAYER ) {
134                 sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "layer", object);
135         }
136         if (((SPObjectClass *)parent_class)->release) {
137                 ((SPObjectClass *)parent_class)->release(object);
138         }
141 static void
142 sp_group_dispose(GObject *object)
144         SP_GROUP(object)->_display_modes.~map();
145     delete SP_GROUP(object)->group;
148 static void
149 sp_group_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
151         SPItem *item;
153         item = SP_ITEM (object);
155         if (((SPObjectClass *) (parent_class))->child_added)
156                 (* ((SPObjectClass *) (parent_class))->child_added) (object, child, ref);
158     SP_GROUP(object)->group->onChildAdded(child);
161 /* fixme: hide (Lauris) */
163 static void
164 sp_group_remove_child (SPObject * object, Inkscape::XML::Node * child)
166         if (((SPObjectClass *) (parent_class))->remove_child)
167                 (* ((SPObjectClass *) (parent_class))->remove_child) (object, child);
169     SP_GROUP(object)->group->onChildRemoved(child);
172 static void
173 sp_group_order_changed (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref)
175         if (((SPObjectClass *) (parent_class))->order_changed)
176                 (* ((SPObjectClass *) (parent_class))->order_changed) (object, child, old_ref, new_ref);
178     SP_GROUP(object)->group->onOrderChanged(child, old_ref, new_ref);
181 static void
182 sp_group_update (SPObject *object, SPCtx *ctx, unsigned int flags)
184     if (((SPObjectClass *) (parent_class))->update)
185         ((SPObjectClass *) (parent_class))->update (object, ctx, flags);
187     SP_GROUP(object)->group->onUpdate(ctx, flags);
190 static void
191 sp_group_modified (SPObject *object, guint flags)
193     SP_GROUP(object)->group->onModified(flags);
196 static Inkscape::XML::Node *
197 sp_group_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
199         SPGroup *group;
200         SPObject *child;
201         Inkscape::XML::Node *crepr;
203         group = SP_GROUP (object);
205         if (flags & SP_OBJECT_WRITE_BUILD) {
206                 GSList *l;
207                 if (!repr) repr = sp_repr_new ("svg:g");
208                 l = NULL;
209                 for (child = sp_object_first_child(object); child != NULL; child = SP_OBJECT_NEXT(child) ) {
210                         crepr = child->updateRepr(NULL, flags);
211                         if (crepr) l = g_slist_prepend (l, crepr);
212                 }
213                 while (l) {
214                         repr->addChild((Inkscape::XML::Node *) l->data, NULL);
215                         Inkscape::GC::release((Inkscape::XML::Node *) l->data);
216                         l = g_slist_remove (l, l->data);
217                 }
218         } else {
219                 for (child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
220                         child->updateRepr(flags);
221                 }
222         }
224         if ( flags & SP_OBJECT_WRITE_EXT ) {
225                 const char *value;
226                 if ( group->_layer_mode == SPGroup::LAYER ) {
227                         value = "layer";
228                 } else if ( flags & SP_OBJECT_WRITE_ALL ) {
229                         value = "group";
230                 } else {
231                         value = NULL;
232                 }
233                 repr->setAttribute("inkscape:groupmode", value);
234         }
236         if (((SPObjectClass *) (parent_class))->write)
237                 ((SPObjectClass *) (parent_class))->write (object, repr, flags);
239         return repr;
242 static void
243 sp_group_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
245     SP_GROUP(item)->group->calculateBBox(bbox, transform, flags);
248 static void
249 sp_group_print (SPItem * item, SPPrintContext *ctx)
251     SP_GROUP(item)->group->onPrint(ctx);
254 static gchar * sp_group_description (SPItem * item)
256     return SP_GROUP(item)->group->getDescription();
259 static void sp_group_set(SPObject *object, unsigned key, char const *value) {
260         SPGroup *group=SP_GROUP(object);
262         switch (key) {
263                 case SP_ATTR_INKSCAPE_GROUPMODE: {
264                         if ( value && !strcmp(value, "layer") ) {
265                                 group->setLayerMode(SPGroup::LAYER);
266                         } else {
267                                 group->setLayerMode(SPGroup::GROUP);
268                         }
269                 } break;
270                 default: {
271                         if (((SPObjectClass *) (parent_class))->set) {
272                                 (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
273                         }
274                 }
275         }
278 static NRArenaItem *
279 sp_group_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags)
281     return SP_GROUP(item)->group->show(arena, key, flags);
284 static void
285 sp_group_hide (SPItem *item, unsigned int key)
287     SP_GROUP(item)->group->hide(key);
290 static void sp_group_snappoints (SPItem const *item, SnapPointsIter p)
292         for (SPObject const *o = sp_object_first_child(SP_OBJECT(item));
293              o != NULL;
294              o = SP_OBJECT_NEXT(o))
295         {
296                 if (SP_IS_ITEM(o)) {
297                         sp_item_snappoints(SP_ITEM(o), p);
298                 }
299         }
303 void
304 sp_item_group_ungroup (SPGroup *group, GSList **children, bool do_done)
306         g_return_if_fail (group != NULL);
307         g_return_if_fail (SP_IS_GROUP (group));
309         SPDocument *doc = SP_OBJECT_DOCUMENT (group);
310         SPObject *root = SP_DOCUMENT_ROOT (doc);
311         SPObject *defs = SP_OBJECT (SP_ROOT (root)->defs);
313         SPItem *gitem = SP_ITEM (group);
314         Inkscape::XML::Node *grepr = SP_OBJECT_REPR (gitem);
316         g_return_if_fail (!strcmp (grepr->name(), "svg:g") || !strcmp (grepr->name(), "svg:a") || !strcmp (grepr->name(), "svg:switch"));
318       // this converts the gradient/pattern fill/stroke on the group, if any, to userSpaceOnUse
319       sp_item_adjust_paint_recursive (gitem, NR::identity(), NR::identity(), false);
321         SPItem *pitem = SP_ITEM (SP_OBJECT_PARENT (gitem));
322         Inkscape::XML::Node *prepr = SP_OBJECT_REPR (pitem);
324         /* Step 1 - generate lists of children objects */
325         GSList *items = NULL;
326         GSList *objects = NULL;
327         for (SPObject *child = sp_object_first_child(SP_OBJECT(group)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
329                 if (SP_IS_ITEM (child)) {
331                         SPItem *citem = SP_ITEM (child);
333                         /* Merging of style */
334                         // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
335                         // it here _before_ the new transform is set, so as to use the pre-transform bbox
336                         sp_item_adjust_paint_recursive (citem, NR::identity(), NR::identity(), false);
338                         sp_style_merge_from_dying_parent(SP_OBJECT_STYLE(child), SP_OBJECT_STYLE(gitem));
339                         /*
340                          * fixme: We currently make no allowance for the case where child is cloned
341                          * and the group has any style settings.
342                          *
343                          * (This should never occur with documents created solely with the current
344                          * version of inkscape without using the XML editor: we usually apply group
345                          * style changes to children rather than to the group itself.)
346                          *
347                          * If the group has no style settings, then
348                          * sp_style_merge_from_dying_parent should be a no-op.  Otherwise (i.e. if
349                          * we change the child's style to compensate for its parent going away)
350                          * then those changes will typically be reflected in any clones of child,
351                          * whereas we'd prefer for Ungroup not to affect the visual appearance.
352                          *
353                          * The only way of preserving styling appearance in general is for child to
354                          * be put into a new group -- a somewhat surprising response to an Ungroup
355                          * command.  We could add a new groupmode:transparent that would mostly
356                          * hide the existence of such groups from the user (i.e. editing behaves as
357                          * if the transparent group's children weren't in a group), though that's
358                          * extra complication & maintenance burden and this case is rare.
359                          */
361                         child->updateRepr();
363                         Inkscape::XML::Node *nrepr = SP_OBJECT_REPR (child)->duplicate();
365                         // Merging transform
366                         NR::Matrix ctrans;
367                         NR::Matrix const g(gitem->transform);
368                         if (SP_IS_USE(citem) && sp_use_get_original (SP_USE(citem)) &&
369                                         SP_OBJECT_PARENT (sp_use_get_original (SP_USE(citem))) == SP_OBJECT(group)) {
370                                 // make sure a clone's effective transform is the same as was under group
371                                 ctrans = g.inverse() * citem->transform * g;
372                         } else {
373                                 ctrans = citem->transform * g;
374                         }
376                         // FIXME: constructing a transform that would fully preserve the appearance of a
377                         // textpath if it is ungrouped with its path seems to be impossible in general
378                         // case. E.g. if the group was squeezed, to keep the ungrouped textpath squeezed
379                         // as well, we'll need to relink it to some "virtual" path which is inversely
380                         // stretched relative to the actual path, and then squeeze the textpath back so it
381                         // would both fit the actual path _and_ be squeezed as before. It's a bummer.
383                         // This is just a way to temporarily remember the transform in repr. When repr is
384                         // reattached outside of the group, the transform will be written more properly
385                         // (i.e. optimized into the object if the corresponding preference is set)
386                         gchar affinestr[80];
387                         if (sp_svg_transform_write(affinestr, 79, ctrans)) {
388                                 nrepr->setAttribute("transform", affinestr);
389                         } else {
390                                 nrepr->setAttribute("transform", NULL);
391                         }
393                         items = g_slist_prepend (items, nrepr);
395                 } else {
396                         Inkscape::XML::Node *nrepr = SP_OBJECT_REPR (child)->duplicate();
397                         objects = g_slist_prepend (objects, nrepr);
398                 }
399         }
401         /* Step 2 - clear group */
402         // remember the position of the group
403         gint pos = SP_OBJECT_REPR(group)->position();
405         // the group is leaving forever, no heir, clones should take note; its children however are going to reemerge
406         SP_OBJECT (group)->deleteObject(true, false);
408         /* Step 3 - add nonitems */
409         if (objects) {
410             Inkscape::XML::Node *last_def = SP_OBJECT_REPR(defs)->lastChild();
411             while (objects) {
412                 SP_OBJECT_REPR(defs)->addChild((Inkscape::XML::Node *) objects->data, last_def);
413                 Inkscape::GC::release((Inkscape::XML::Node *) objects->data);
414                 objects = g_slist_remove (objects, objects->data);
415             }
416         }
418         /* Step 4 - add items */
419         gint const preserve = prefs_get_int_attribute("options.preservetransform", "value", 0);
420         while (items) {
421                 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) items->data;
422                 // add item
423                 prepr->appendChild(repr);
424                 // restore position; since the items list was prepended (i.e. reverse), we now add
425                 // all children at the same pos, which inverts the order once again
426                 repr->setPosition(pos > 0 ? pos : 0);
428                 // fill in the children list if non-null
429                 SPItem *item = (SPItem *) doc->getObjectByRepr(repr);
431                 /* Optimize the transform matrix if requested. */
432                 // No compensations are required because this is supposed to be a non-transformation visually.
433                 // FIXME: this if is wholly lifted from sp_item_write_transform and should stay in sync
434                 NR::Matrix transform_attr (item->transform);
435                 if ( // run the object's set_transform (i.e. embed transform) only if:
436                                 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform && // it does have a set_transform method
437                                 !preserve && // user did not chose to preserve all transforms
438                                 !item->clip_ref->getObject() && // the object does not have a clippath
439                                 !item->mask_ref->getObject() && // the object does not have a mask
440                                 !(!transform_attr.is_translation() && SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->filter.filter) 
441                                 // the object does not have a filter, or the transform is translation (which is supposed to not affect filters)
442                                 ) {
443                         transform_attr = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform(item, item->transform);
444                 }
445                 sp_item_set_item_transform(item, transform_attr);
447                 Inkscape::GC::release(repr);
448                 if (children && SP_IS_ITEM (item))
449                         *children = g_slist_prepend (*children, item);
451                 items = g_slist_remove (items, items->data);
452         }
454         if (do_done) 
455                 sp_document_done (doc, SP_VERB_NONE, _("Ungroup"));
458 /*
459  * some API for list aspect of SPGroup
460  */
462 GSList *
463 sp_item_group_item_list (SPGroup * group)
465         GSList *s;
466         SPObject *o;
468         g_return_val_if_fail (group != NULL, NULL);
469         g_return_val_if_fail (SP_IS_GROUP (group), NULL);
471         s = NULL;
473         for ( o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
474                 if (SP_IS_ITEM (o)) {
475                         s = g_slist_prepend (s, o);
476                 }
477         }
479         return g_slist_reverse (s);
482 SPObject *
483 sp_item_group_get_child_by_name (SPGroup *group, SPObject *ref, const gchar *name)
485         SPObject *child;
486         child = (ref) ? SP_OBJECT_NEXT(ref) : sp_object_first_child(SP_OBJECT(group));
487         while ( child && strcmp (SP_OBJECT_REPR(child)->name(), name) ) {
488                 child = SP_OBJECT_NEXT(child);
489         }
490         return child;
493 void SPGroup::setLayerMode(LayerMode mode) {
494         if ( _layer_mode != mode ) {
495                 if ( mode == LAYER ) {
496                         sp_document_add_resource(SP_OBJECT_DOCUMENT(this), "layer", this);
497                 } else {
498                         sp_document_remove_resource(SP_OBJECT_DOCUMENT(this), "layer", this);
499                 }
500                 _layer_mode = mode;
501                 _updateLayerMode();
502         }
505 SPGroup::LayerMode SPGroup::layerDisplayMode(unsigned int dkey) const {
506         std::map<unsigned int, LayerMode>::const_iterator iter;
507         iter = _display_modes.find(dkey);
508         if ( iter != _display_modes.end() ) {
509                 return (*iter).second;
510         } else {
511                 return GROUP;
512         }
515 void SPGroup::setLayerDisplayMode(unsigned int dkey, SPGroup::LayerMode mode) {
516         if ( layerDisplayMode(dkey) != mode ) {
517                 _display_modes[dkey] = mode;
518                 _updateLayerMode(dkey);
519         }
522 void SPGroup::_updateLayerMode(unsigned int display_key) {
523         SPItemView *view;
524         for ( view = this->display ; view ; view = view->next ) {
525                 if ( !display_key || view->key == display_key ) {
526                         NRArenaGroup *arena_group=NR_ARENA_GROUP(view->arenaitem);
527                         if (arena_group) {
528                                 nr_arena_group_set_transparent(arena_group, effectiveLayerMode(view->key) == SPGroup::LAYER);
529                         }
530                 }
531         }
534 void SPGroup::translateChildItems(NR::translate const &tr)
536         if (this->hasChildren())
537         {
538                 SPObject *o = NULL;
539                 for (o = sp_object_first_child(SP_OBJECT(this)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
540                         if (SP_IS_ITEM (o)) {
541                                 sp_item_move_rel(static_cast<SPItem *>(o), tr);
542                         }
543                 }
544         }
547 CGroup::CGroup(SPGroup *group) {
548     _group = group;
551 CGroup::~CGroup() {
554 void CGroup::onChildAdded(Inkscape::XML::Node *child) {
555     SPObject *last_child = _group->lastChild();
556     if (last_child && SP_OBJECT_REPR(last_child) == child) {
557         // optimization for the common special case where the child is being added at the end
558         SPObject *ochild = last_child;
559         if ( SP_IS_ITEM(ochild) ) {
560             /* TODO: this should be moved into SPItem somehow */
561             SPItemView *v;
562             NRArenaItem *ac;
564             for (v = _group->display; v != NULL; v = v->next) {
565                 ac = sp_item_invoke_show (SP_ITEM (ochild), NR_ARENA_ITEM_ARENA (v->arenaitem), v->key, v->flags);
567                 if (ac) {
568                     nr_arena_item_append_child (v->arenaitem, ac);
569                     nr_arena_item_unref (ac);
570                 }
571             }
572         }
573     } else {    // general case
574         SPObject *ochild = sp_object_get_child_by_repr(_group, child);
575         if ( ochild && SP_IS_ITEM(ochild) ) {
576             /* TODO: this should be moved into SPItem somehow */
577             SPItemView *v;
578             NRArenaItem *ac;
580             unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
582             for (v = _group->display; v != NULL; v = v->next) {
583                 ac = sp_item_invoke_show (SP_ITEM (ochild), NR_ARENA_ITEM_ARENA (v->arenaitem), v->key, v->flags);
585                 if (ac) {
586                     nr_arena_item_add_child (v->arenaitem, ac, NULL);
587                     nr_arena_item_set_order (ac, position);
588                     nr_arena_item_unref (ac);
589                 }
590             }
591         }
592     }
594     _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
597 void CGroup::onChildRemoved(Inkscape::XML::Node */*child*/) {
598     _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
601 void CGroup::onUpdate(SPCtx *ctx, unsigned int flags) {    
602     SPItemCtx *ictx, cctx;
604     ictx = (SPItemCtx *) ctx;
605     cctx = *ictx;
607     if (flags & SP_OBJECT_MODIFIED_FLAG) {
608       flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
609     }
611     flags &= SP_OBJECT_MODIFIED_CASCADE;
613     if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
614       SPObject *object = SP_OBJECT(_group);
615       for (SPItemView *v = SP_ITEM(_group)->display; v != NULL; v = v->next) {
616         nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
617       }
618     }
620     GSList *l = g_slist_reverse(_group->childList(true, SPObject::ActionUpdate));
621     while (l) {
622         SPObject *child = SP_OBJECT (l->data);
623         l = g_slist_remove (l, child);
624         if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
625             if (SP_IS_ITEM (child)) {
626                 SPItem const &chi = *SP_ITEM(child);
627                 cctx.i2doc = chi.transform * ictx->i2doc;
628                 cctx.i2vp = chi.transform * ictx->i2vp;
629                 child->updateDisplay((SPCtx *)&cctx, flags);
630             } else {
631                 child->updateDisplay(ctx, flags);
632             }
633         }
634         g_object_unref (G_OBJECT (child));
635     }
638 void CGroup::onModified(guint flags) {
639     SPObject *child;
641     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
642     flags &= SP_OBJECT_MODIFIED_CASCADE;
644     if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
645       SPObject *object = SP_OBJECT(_group);
646       for (SPItemView *v = SP_ITEM(_group)->display; v != NULL; v = v->next) {
647         nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
648       }
649     }
651     GSList *l = g_slist_reverse(_group->childList(true));
652     while (l) {
653         child = SP_OBJECT (l->data);
654         l = g_slist_remove (l, child);
655         if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
656             child->emitModified(flags);
657         }
658         g_object_unref (G_OBJECT (child));
659     }
662 void CGroup::calculateBBox(NRRect *bbox, NR::Matrix const &transform, unsigned const flags) {
663     GSList *l = _group->childList(false, SPObject::ActionBBox);
664     while (l) {
665         SPObject *o = SP_OBJECT (l->data);
666         if (SP_IS_ITEM(o)) {
667             SPItem *child = SP_ITEM(o);
668             NR::Matrix const ct(child->transform * transform);
669             sp_item_invoke_bbox_full(child, bbox, ct, flags, FALSE);
670         }        
671         l = g_slist_remove (l, o);
672     }
675 void CGroup::onPrint(SPPrintContext *ctx) {
676     GSList *l = g_slist_reverse(_group->childList(false));
677     while (l) {
678         SPObject *o = SP_OBJECT (l->data);
679         if (SP_IS_ITEM(o)) {
680             sp_item_invoke_print (SP_ITEM (o), ctx);
681         }        
682         l = g_slist_remove (l, o);
683     }
686 gint CGroup::getItemCount() {
687     gint len = 0;
688     for (SPObject *o = sp_object_first_child(SP_OBJECT(_group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
689         if (SP_IS_ITEM(o)) {
690             len++;
691         }
692     }
693     
694     return len;
697 gchar *CGroup::getDescription() {
698     gint len = getItemCount();
699     return g_strdup_printf(
700             ngettext("<b>Group</b> of <b>%d</b> object",
701                  "<b>Group</b> of <b>%d</b> objects",
702                  len), len);
705 NRArenaItem *CGroup::show (NRArena *arena, unsigned int key, unsigned int flags) {
706     NRArenaItem *ai;
707     SPObject *object = SP_OBJECT(_group);
709     ai = NRArenaGroup::create(arena);
711     nr_arena_group_set_transparent(NR_ARENA_GROUP (ai),
712                                    _group->effectiveLayerMode(key) ==
713                          SPGroup::LAYER);
714     nr_arena_group_set_style(NR_ARENA_GROUP(ai), SP_OBJECT_STYLE(object));
716     _showChildren(arena, ai, key, flags);
717     return ai;
720 void CGroup::_showChildren (NRArena *arena, NRArenaItem *ai, unsigned int key, unsigned int flags) {
721     NRArenaItem *ac = NULL;
722     NRArenaItem *ar = NULL;
723     SPItem * child = NULL;
724     GSList *l = g_slist_reverse(_group->childList(false, SPObject::ActionShow));
725     while (l) {
726         SPObject *o = SP_OBJECT (l->data);
727         if (SP_IS_ITEM (o)) {
728             child = SP_ITEM (o);
729             ac = sp_item_invoke_show (child, arena, key, flags);
730             if (ac) {
731                 nr_arena_item_add_child (ai, ac, ar);
732                 ar = ac;
733                 nr_arena_item_unref (ac);
734             }
735         }
736         l = g_slist_remove (l, o);
737     }
740 void CGroup::hide (unsigned int key) {
741     SPItem * child;
743     GSList *l = g_slist_reverse(_group->childList(false, SPObject::ActionShow));
744     while (l) {
745         SPObject *o = SP_OBJECT (l->data);
746         if (SP_IS_ITEM (o)) {
747             child = SP_ITEM (o);
748             sp_item_invoke_hide (child, key);
749         }
750         l = g_slist_remove (l, o);
751     }
753     if (((SPItemClass *) parent_class)->hide)
754         ((SPItemClass *) parent_class)->hide (_group, key);
757 void CGroup::onOrderChanged (Inkscape::XML::Node *child, Inkscape::XML::Node *, Inkscape::XML::Node *)
759     SPObject *ochild = sp_object_get_child_by_repr(_group, child);
760     if ( ochild && SP_IS_ITEM(ochild) ) {
761         /* TODO: this should be moved into SPItem somehow */
762         SPItemView *v;
763         unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
764         for ( v = SP_ITEM (ochild)->display ; v != NULL ; v = v->next ) {
765             nr_arena_item_set_order (v->arenaitem, position);
766         }
767     }
769     _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
772 /*
773   Local Variables:
774   mode:c++
775   c-file-style:"stroustrup"
776   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
777   indent-tabs-mode:nil
778   fill-column:99
779   End:
780 */
781 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :