Code

Make grouped 3D boxes work correctly when transformed (fixes: LP 188991)
[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>
22 #include <cstring>
23 #include <string>
25 #include "display/nr-arena-group.h"
26 #include "libnr/nr-matrix-ops.h"
27 #include "libnr/nr-matrix-fns.h"
28 #include "xml/repr.h"
29 #include "svg/svg.h"
30 #include "document.h"
31 #include "style.h"
32 #include "attributes.h"
33 #include "sp-item-transform.h"
34 #include "sp-root.h"
35 #include "sp-use.h"
36 #include "prefs-utils.h"
37 #include "sp-clippath.h"
38 #include "sp-mask.h"
39 #include "sp-path.h"
40 #include "box3d.h"
41 #include "persp3d.h"
42 #include "inkscape.h"
43 #include "desktop-handles.h"
44 #include "selection.h"
46 static void sp_group_class_init (SPGroupClass *klass);
47 static void sp_group_init (SPGroup *group);
48 static void sp_group_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
49 static void sp_group_release(SPObject *object);
50 static void sp_group_dispose(GObject *object);
52 static void sp_group_child_added (SPObject * object, Inkscape::XML::Node * child, Inkscape::XML::Node * ref);
53 static void sp_group_remove_child (SPObject * object, Inkscape::XML::Node * child);
54 static void sp_group_order_changed (SPObject * object, Inkscape::XML::Node * child, Inkscape::XML::Node * old_ref, Inkscape::XML::Node * new_ref);
55 static void sp_group_update (SPObject *object, SPCtx *ctx, guint flags);
56 static void sp_group_modified (SPObject *object, guint flags);
57 static Inkscape::XML::Node *sp_group_write (SPObject *object, Inkscape::XML::Node *repr, guint flags);
58 static void sp_group_set(SPObject *object, unsigned key, char const *value);
60 static void sp_group_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
61 static void sp_group_print (SPItem * item, SPPrintContext *ctx);
62 static gchar * sp_group_description (SPItem * item);
63 static NR::Matrix sp_group_set_transform(SPItem *item, NR::Matrix const &xform);
64 static NRArenaItem *sp_group_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags);
65 static void sp_group_hide (SPItem * item, unsigned int key);
66 static void sp_group_snappoints (SPItem const *item, SnapPointsIter p);
68 static SPItemClass * parent_class;
70 GType
71 sp_group_get_type (void)
72 {
73         static GType group_type = 0;
74         if (!group_type) {
75                 GTypeInfo group_info = {
76                         sizeof (SPGroupClass),
77                         NULL,   /* base_init */
78                         NULL,   /* base_finalize */
79                         (GClassInitFunc) sp_group_class_init,
80                         NULL,   /* class_finalize */
81                         NULL,   /* class_data */
82                         sizeof (SPGroup),
83                         16,     /* n_preallocs */
84                         (GInstanceInitFunc) sp_group_init,
85                         NULL,   /* value_table */
86                 };
87                 group_type = g_type_register_static (SP_TYPE_ITEM, "SPGroup", &group_info, (GTypeFlags)0);
88         }
89         return group_type;
90 }
92 static void
93 sp_group_class_init (SPGroupClass *klass)
94 {
95         GObjectClass * object_class;
96         SPObjectClass * sp_object_class;
97         SPItemClass * item_class;
99         object_class = (GObjectClass *) klass;
100         sp_object_class = (SPObjectClass *) klass;
101         item_class = (SPItemClass *) klass;
103         parent_class = (SPItemClass *)g_type_class_ref (SP_TYPE_ITEM);
105         object_class->dispose = sp_group_dispose;
107         sp_object_class->child_added = sp_group_child_added;
108         sp_object_class->remove_child = sp_group_remove_child;
109         sp_object_class->order_changed = sp_group_order_changed;
110         sp_object_class->update = sp_group_update;
111         sp_object_class->modified = sp_group_modified;
112         sp_object_class->set = sp_group_set;
113         sp_object_class->write = sp_group_write;
114         sp_object_class->release = sp_group_release;
115         sp_object_class->build = sp_group_build;
117         item_class->bbox = sp_group_bbox;
118         item_class->print = sp_group_print;
119         item_class->description = sp_group_description;
120         item_class->set_transform = sp_group_set_transform;
121         item_class->show = sp_group_show;
122         item_class->hide = sp_group_hide;
123         item_class->snappoints = sp_group_snappoints;
126 static void
127 sp_group_init (SPGroup *group)
129         group->_layer_mode = SPGroup::GROUP;
130     group->group = new CGroup(group);
131         new (&group->_display_modes) std::map<unsigned int, SPGroup::LayerMode>();
134 static void sp_group_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
136         sp_object_read_attr(object, "inkscape:groupmode");
138         if (((SPObjectClass *)parent_class)->build) {
139                 ((SPObjectClass *)parent_class)->build(object, document, repr);
140         }
143 static void sp_group_release(SPObject *object) {
144         if ( SP_GROUP(object)->_layer_mode == SPGroup::LAYER ) {
145                 sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "layer", object);
146         }
147         if (((SPObjectClass *)parent_class)->release) {
148                 ((SPObjectClass *)parent_class)->release(object);
149         }
152 static void
153 sp_group_dispose(GObject *object)
155         SP_GROUP(object)->_display_modes.~map();
156     delete SP_GROUP(object)->group;
159 static void
160 sp_group_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
162         SPItem *item;
164         item = SP_ITEM (object);
166         if (((SPObjectClass *) (parent_class))->child_added)
167                 (* ((SPObjectClass *) (parent_class))->child_added) (object, child, ref);
169     SP_GROUP(object)->group->onChildAdded(child);
172 /* fixme: hide (Lauris) */
174 static void
175 sp_group_remove_child (SPObject * object, Inkscape::XML::Node * child)
177         if (((SPObjectClass *) (parent_class))->remove_child)
178                 (* ((SPObjectClass *) (parent_class))->remove_child) (object, child);
180     SP_GROUP(object)->group->onChildRemoved(child);
183 static void
184 sp_group_order_changed (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref)
186         if (((SPObjectClass *) (parent_class))->order_changed)
187                 (* ((SPObjectClass *) (parent_class))->order_changed) (object, child, old_ref, new_ref);
189     SP_GROUP(object)->group->onOrderChanged(child, old_ref, new_ref);
192 static void
193 sp_group_update (SPObject *object, SPCtx *ctx, unsigned int flags)
195     if (((SPObjectClass *) (parent_class))->update)
196         ((SPObjectClass *) (parent_class))->update (object, ctx, flags);
198     SP_GROUP(object)->group->onUpdate(ctx, flags);
201 static void
202 sp_group_modified (SPObject *object, guint flags)
204     SP_GROUP(object)->group->onModified(flags);
207 static Inkscape::XML::Node *
208 sp_group_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
210         SPGroup *group;
211         SPObject *child;
212         Inkscape::XML::Node *crepr;
214         group = SP_GROUP (object);
216         if (flags & SP_OBJECT_WRITE_BUILD) {
217                 GSList *l;
218                 if (!repr) {
219                     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
220                     repr = xml_doc->createElement("svg:g");
221                 }
222                 l = NULL;
223                 for (child = sp_object_first_child(object); child != NULL; child = SP_OBJECT_NEXT(child) ) {
224                         crepr = child->updateRepr(NULL, flags);
225                         if (crepr) l = g_slist_prepend (l, crepr);
226                 }
227                 while (l) {
228                         repr->addChild((Inkscape::XML::Node *) l->data, NULL);
229                         Inkscape::GC::release((Inkscape::XML::Node *) l->data);
230                         l = g_slist_remove (l, l->data);
231                 }
232         } else {
233                 for (child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
234                         child->updateRepr(flags);
235                 }
236         }
238         if ( flags & SP_OBJECT_WRITE_EXT ) {
239                 const char *value;
240                 if ( group->_layer_mode == SPGroup::LAYER ) {
241                         value = "layer";
242                 } else if ( flags & SP_OBJECT_WRITE_ALL ) {
243                         value = "group";
244                 } else {
245                         value = NULL;
246                 }
247                 repr->setAttribute("inkscape:groupmode", value);
248         }
250         if (((SPObjectClass *) (parent_class))->write)
251                 ((SPObjectClass *) (parent_class))->write (object, repr, flags);
253         return repr;
256 static void
257 sp_group_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
259     SP_GROUP(item)->group->calculateBBox(bbox, transform, flags);
262 static void
263 sp_group_print (SPItem * item, SPPrintContext *ctx)
265     SP_GROUP(item)->group->onPrint(ctx);
268 static gchar * sp_group_description (SPItem * item)
270     return SP_GROUP(item)->group->getDescription();
273 static NR::Matrix
274 sp_group_set_transform(SPItem *item, NR::Matrix const &xform)
276     SPGroup *group = SP_GROUP(item);
278     Inkscape::Selection *selection = sp_desktop_selection(inkscape_active_desktop());
279     persp3d_split_perspectives_according_to_selection(selection);
281     NR::Matrix last_trans;
282     sp_svg_transform_read(SP_OBJECT_REPR(item)->attribute("transform"), &last_trans);
283     NR::Matrix inc_trans = last_trans.inverse()*xform;
285     std::list<Persp3D *> plist = selection->perspList();
286     for (std::list<Persp3D *>::iterator i = plist.begin(); i != plist.end(); ++i) {
287         persp3d_apply_affine_transformation(*i, inc_trans);
288     }
290     return xform;
293 static void sp_group_set(SPObject *object, unsigned key, char const *value) {
294         SPGroup *group=SP_GROUP(object);
296         switch (key) {
297                 case SP_ATTR_INKSCAPE_GROUPMODE: {
298                         if ( value && !strcmp(value, "layer") ) {
299                                 group->setLayerMode(SPGroup::LAYER);
300                         } else {
301                                 group->setLayerMode(SPGroup::GROUP);
302                         }
303                 } break;
304                 default: {
305                         if (((SPObjectClass *) (parent_class))->set) {
306                                 (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
307                         }
308                 }
309         }
312 static NRArenaItem *
313 sp_group_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags)
315     return SP_GROUP(item)->group->show(arena, key, flags);
318 static void
319 sp_group_hide (SPItem *item, unsigned int key)
321     SP_GROUP(item)->group->hide(key);
324 static void sp_group_snappoints (SPItem const *item, SnapPointsIter p)
326         for (SPObject const *o = sp_object_first_child(SP_OBJECT(item));
327              o != NULL;
328              o = SP_OBJECT_NEXT(o))
329         {
330                 if (SP_IS_ITEM(o)) {
331             sp_item_snappoints(SP_ITEM(o), false, p);
332                 }
333         }
337 void
338 sp_item_group_ungroup (SPGroup *group, GSList **children, bool do_done)
340         g_return_if_fail (group != NULL);
341         g_return_if_fail (SP_IS_GROUP (group));
343         SPDocument *doc = SP_OBJECT_DOCUMENT (group);
344         SPObject *root = SP_DOCUMENT_ROOT (doc);
345         SPObject *defs = SP_OBJECT (SP_ROOT (root)->defs);
347         SPItem *gitem = SP_ITEM (group);
348         Inkscape::XML::Node *grepr = SP_OBJECT_REPR (gitem);
350         g_return_if_fail (!strcmp (grepr->name(), "svg:g") || !strcmp (grepr->name(), "svg:a") || !strcmp (grepr->name(), "svg:switch"));
352       // this converts the gradient/pattern fill/stroke on the group, if any, to userSpaceOnUse
353       sp_item_adjust_paint_recursive (gitem, NR::identity(), NR::identity(), false);
355         SPItem *pitem = SP_ITEM (SP_OBJECT_PARENT (gitem));
356         Inkscape::XML::Node *prepr = SP_OBJECT_REPR (pitem);
358   if (SP_IS_BOX3D(gitem)) {
359       grepr = box3d_convert_to_group(SP_BOX3D(gitem));
360       if (grepr) {
361           gitem = SP_ITEM(doc->getObjectByRepr(grepr));
362           group = SP_GROUP(gitem);
363       }
364   }
366         /* Step 1 - generate lists of children objects */
367         GSList *items = NULL;
368         GSList *objects = NULL;
369         for (SPObject *child = sp_object_first_child(SP_OBJECT(group)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
371                 if (SP_IS_ITEM (child)) {
373                         SPItem *citem = SP_ITEM (child);
375                         /* Merging of style */
376                         // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
377                         // it here _before_ the new transform is set, so as to use the pre-transform bbox
378                         sp_item_adjust_paint_recursive (citem, NR::identity(), NR::identity(), false);
380                         sp_style_merge_from_dying_parent(SP_OBJECT_STYLE(child), SP_OBJECT_STYLE(gitem));
381                         /*
382                          * fixme: We currently make no allowance for the case where child is cloned
383                          * and the group has any style settings.
384                          *
385                          * (This should never occur with documents created solely with the current
386                          * version of inkscape without using the XML editor: we usually apply group
387                          * style changes to children rather than to the group itself.)
388                          *
389                          * If the group has no style settings, then
390                          * sp_style_merge_from_dying_parent should be a no-op.  Otherwise (i.e. if
391                          * we change the child's style to compensate for its parent going away)
392                          * then those changes will typically be reflected in any clones of child,
393                          * whereas we'd prefer for Ungroup not to affect the visual appearance.
394                          *
395                          * The only way of preserving styling appearance in general is for child to
396                          * be put into a new group -- a somewhat surprising response to an Ungroup
397                          * command.  We could add a new groupmode:transparent that would mostly
398                          * hide the existence of such groups from the user (i.e. editing behaves as
399                          * if the transparent group's children weren't in a group), though that's
400                          * extra complication & maintenance burden and this case is rare.
401                          */
403                         child->updateRepr();
405                         Inkscape::XML::Node *nrepr = SP_OBJECT_REPR (child)->duplicate(prepr->document());
407                         // Merging transform
408                         NR::Matrix ctrans;
409                         NR::Matrix const g(gitem->transform);
410                         if (SP_IS_USE(citem) && sp_use_get_original (SP_USE(citem)) &&
411                                         SP_OBJECT_PARENT (sp_use_get_original (SP_USE(citem))) == SP_OBJECT(group)) {
412                                 // make sure a clone's effective transform is the same as was under group
413                                 ctrans = g.inverse() * citem->transform * g;
414                         } else {
415                                 ctrans = citem->transform * g;
416                         }
418                         // FIXME: constructing a transform that would fully preserve the appearance of a
419                         // textpath if it is ungrouped with its path seems to be impossible in general
420                         // case. E.g. if the group was squeezed, to keep the ungrouped textpath squeezed
421                         // as well, we'll need to relink it to some "virtual" path which is inversely
422                         // stretched relative to the actual path, and then squeeze the textpath back so it
423                         // would both fit the actual path _and_ be squeezed as before. It's a bummer.
425                         // This is just a way to temporarily remember the transform in repr. When repr is
426                         // reattached outside of the group, the transform will be written more properly
427                         // (i.e. optimized into the object if the corresponding preference is set)
428                         gchar *affinestr=sp_svg_transform_write(ctrans);
429                         nrepr->setAttribute("transform", affinestr);
430                         g_free(affinestr);
432                         items = g_slist_prepend (items, nrepr);
434                 } else {
435                         Inkscape::XML::Node *nrepr = SP_OBJECT_REPR (child)->duplicate(prepr->document());
436                         objects = g_slist_prepend (objects, nrepr);
437                 }
438         }
440         /* Step 2 - clear group */
441         // remember the position of the group
442         gint pos = SP_OBJECT_REPR(group)->position();
444         // the group is leaving forever, no heir, clones should take note; its children however are going to reemerge
445         SP_OBJECT (group)->deleteObject(true, false);
447         /* Step 3 - add nonitems */
448         if (objects) {
449             Inkscape::XML::Node *last_def = SP_OBJECT_REPR(defs)->lastChild();
450             while (objects) {
451                 SP_OBJECT_REPR(defs)->addChild((Inkscape::XML::Node *) objects->data, last_def);
452                 Inkscape::GC::release((Inkscape::XML::Node *) objects->data);
453                 objects = g_slist_remove (objects, objects->data);
454             }
455         }
457         /* Step 4 - add items */
458         while (items) {
459                 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) items->data;
460                 // add item
461                 prepr->appendChild(repr);
462                 // restore position; since the items list was prepended (i.e. reverse), we now add
463                 // all children at the same pos, which inverts the order once again
464                 repr->setPosition(pos > 0 ? pos : 0);
466                 // fill in the children list if non-null
467                 SPItem *item = (SPItem *) doc->getObjectByRepr(repr);
469                 sp_item_write_transform(item, repr, item->transform, NULL, false);
470                 
471                 Inkscape::GC::release(repr);
472                 if (children && SP_IS_ITEM (item))
473                         *children = g_slist_prepend (*children, item);
475                 items = g_slist_remove (items, items->data);
476         }
478         if (do_done) 
479                 sp_document_done (doc, SP_VERB_NONE, _("Ungroup"));
482 /*
483  * some API for list aspect of SPGroup
484  */
486 GSList *
487 sp_item_group_item_list (SPGroup * group)
489         GSList *s;
490         SPObject *o;
492         g_return_val_if_fail (group != NULL, NULL);
493         g_return_val_if_fail (SP_IS_GROUP (group), NULL);
495         s = NULL;
497         for ( o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
498                 if (SP_IS_ITEM (o)) {
499                         s = g_slist_prepend (s, o);
500                 }
501         }
503         return g_slist_reverse (s);
506 SPObject *
507 sp_item_group_get_child_by_name (SPGroup *group, SPObject *ref, const gchar *name)
509         SPObject *child;
510         child = (ref) ? SP_OBJECT_NEXT(ref) : sp_object_first_child(SP_OBJECT(group));
511         while ( child && strcmp (SP_OBJECT_REPR(child)->name(), name) ) {
512                 child = SP_OBJECT_NEXT(child);
513         }
514         return child;
517 void SPGroup::setLayerMode(LayerMode mode) {
518         if ( _layer_mode != mode ) {
519                 if ( mode == LAYER ) {
520                         sp_document_add_resource(SP_OBJECT_DOCUMENT(this), "layer", this);
521                 } else {
522                         sp_document_remove_resource(SP_OBJECT_DOCUMENT(this), "layer", this);
523                 }
524                 _layer_mode = mode;
525                 _updateLayerMode();
526         }
529 SPGroup::LayerMode SPGroup::layerDisplayMode(unsigned int dkey) const {
530         std::map<unsigned int, LayerMode>::const_iterator iter;
531         iter = _display_modes.find(dkey);
532         if ( iter != _display_modes.end() ) {
533                 return (*iter).second;
534         } else {
535                 return GROUP;
536         }
539 void SPGroup::setLayerDisplayMode(unsigned int dkey, SPGroup::LayerMode mode) {
540         if ( layerDisplayMode(dkey) != mode ) {
541                 _display_modes[dkey] = mode;
542                 _updateLayerMode(dkey);
543         }
546 void SPGroup::_updateLayerMode(unsigned int display_key) {
547         SPItemView *view;
548         for ( view = this->display ; view ; view = view->next ) {
549                 if ( !display_key || view->key == display_key ) {
550                         NRArenaGroup *arena_group=NR_ARENA_GROUP(view->arenaitem);
551                         if (arena_group) {
552                                 nr_arena_group_set_transparent(arena_group, effectiveLayerMode(view->key) == SPGroup::LAYER);
553                         }
554                 }
555         }
558 void SPGroup::translateChildItems(NR::translate const &tr)
560         if (this->hasChildren())
561         {
562                 SPObject *o = NULL;
563                 for (o = sp_object_first_child(SP_OBJECT(this)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
564                         if (SP_IS_ITEM (o)) {
565                                 sp_item_move_rel(static_cast<SPItem *>(o), tr);
566                         }
567                 }
568         }
571 CGroup::CGroup(SPGroup *group) {
572     _group = group;
575 CGroup::~CGroup() {
578 void CGroup::onChildAdded(Inkscape::XML::Node *child) {
579     SPObject *last_child = _group->lastChild();
580     if (last_child && SP_OBJECT_REPR(last_child) == child) {
581         // optimization for the common special case where the child is being added at the end
582         SPObject *ochild = last_child;
583         if ( SP_IS_ITEM(ochild) ) {
584             /* TODO: this should be moved into SPItem somehow */
585             SPItemView *v;
586             NRArenaItem *ac;
588             for (v = _group->display; v != NULL; v = v->next) {
589                 ac = sp_item_invoke_show (SP_ITEM (ochild), NR_ARENA_ITEM_ARENA (v->arenaitem), v->key, v->flags);
591                 if (ac) {
592                     nr_arena_item_append_child (v->arenaitem, ac);
593                     nr_arena_item_unref (ac);
594                 }
595             }
596         }
597     } else {    // general case
598         SPObject *ochild = sp_object_get_child_by_repr(_group, child);
599         if ( ochild && SP_IS_ITEM(ochild) ) {
600             /* TODO: this should be moved into SPItem somehow */
601             SPItemView *v;
602             NRArenaItem *ac;
604             unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
606             for (v = _group->display; v != NULL; v = v->next) {
607                 ac = sp_item_invoke_show (SP_ITEM (ochild), NR_ARENA_ITEM_ARENA (v->arenaitem), v->key, v->flags);
609                 if (ac) {
610                     nr_arena_item_add_child (v->arenaitem, ac, NULL);
611                     nr_arena_item_set_order (ac, position);
612                     nr_arena_item_unref (ac);
613                 }
614             }
615         }
616     }
618     _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
621 void CGroup::onChildRemoved(Inkscape::XML::Node */*child*/) {
622     _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
625 void CGroup::onUpdate(SPCtx *ctx, unsigned int flags) {    
626     SPItemCtx *ictx, cctx;
628     ictx = (SPItemCtx *) ctx;
629     cctx = *ictx;
631     if (flags & SP_OBJECT_MODIFIED_FLAG) {
632       flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
633     }
635     flags &= SP_OBJECT_MODIFIED_CASCADE;
637     if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
638       SPObject *object = SP_OBJECT(_group);
639       for (SPItemView *v = SP_ITEM(_group)->display; v != NULL; v = v->next) {
640         nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
641       }
642     }
644     GSList *l = g_slist_reverse(_group->childList(true, SPObject::ActionUpdate));
645     while (l) {
646         SPObject *child = SP_OBJECT (l->data);
647         l = g_slist_remove (l, child);
648         if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
649             if (SP_IS_ITEM (child)) {
650                 SPItem const &chi = *SP_ITEM(child);
651                 cctx.i2doc = chi.transform * ictx->i2doc;
652                 cctx.i2vp = chi.transform * ictx->i2vp;
653                 child->updateDisplay((SPCtx *)&cctx, flags);
654             } else {
655                 child->updateDisplay(ctx, flags);
656             }
657         }
658         g_object_unref (G_OBJECT (child));
659     }
662 void CGroup::onModified(guint flags) {
663     SPObject *child;
665     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
666     flags &= SP_OBJECT_MODIFIED_CASCADE;
668     if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
669       SPObject *object = SP_OBJECT(_group);
670       for (SPItemView *v = SP_ITEM(_group)->display; v != NULL; v = v->next) {
671         nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
672       }
673     }
675     GSList *l = g_slist_reverse(_group->childList(true));
676     while (l) {
677         child = SP_OBJECT (l->data);
678         l = g_slist_remove (l, child);
679         if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
680             child->emitModified(flags);
681         }
682         g_object_unref (G_OBJECT (child));
683     }
686 void CGroup::calculateBBox(NRRect *bbox, NR::Matrix const &transform, unsigned const flags) {
687     GSList *l = _group->childList(false, SPObject::ActionBBox);
688     while (l) {
689         SPObject *o = SP_OBJECT (l->data);
690         if (SP_IS_ITEM(o)) {
691             SPItem *child = SP_ITEM(o);
692             NR::Matrix const ct(child->transform * transform);
693             sp_item_invoke_bbox_full(child, bbox, ct, flags, FALSE);
694         }        
695         l = g_slist_remove (l, o);
696     }
699 void CGroup::onPrint(SPPrintContext *ctx) {
700     GSList *l = g_slist_reverse(_group->childList(false));
701     while (l) {
702         SPObject *o = SP_OBJECT (l->data);
703         if (SP_IS_ITEM(o)) {
704             sp_item_invoke_print (SP_ITEM (o), ctx);
705         }        
706         l = g_slist_remove (l, o);
707     }
710 gint CGroup::getItemCount() {
711     gint len = 0;
712     for (SPObject *o = sp_object_first_child(SP_OBJECT(_group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
713         if (SP_IS_ITEM(o)) {
714             len++;
715         }
716     }
717     
718     return len;
721 gchar *CGroup::getDescription() {
722     gint len = getItemCount();
723     return g_strdup_printf(
724             ngettext("<b>Group</b> of <b>%d</b> object",
725                  "<b>Group</b> of <b>%d</b> objects",
726                  len), len);
729 NRArenaItem *CGroup::show (NRArena *arena, unsigned int key, unsigned int flags) {
730     NRArenaItem *ai;
731     SPObject *object = SP_OBJECT(_group);
733     ai = NRArenaGroup::create(arena);
735     nr_arena_group_set_transparent(NR_ARENA_GROUP (ai),
736                                    _group->effectiveLayerMode(key) ==
737                          SPGroup::LAYER);
738     nr_arena_group_set_style(NR_ARENA_GROUP(ai), SP_OBJECT_STYLE(object));
740     _showChildren(arena, ai, key, flags);
741     return ai;
744 void CGroup::_showChildren (NRArena *arena, NRArenaItem *ai, unsigned int key, unsigned int flags) {
745     NRArenaItem *ac = NULL;
746     NRArenaItem *ar = NULL;
747     SPItem * child = NULL;
748     GSList *l = g_slist_reverse(_group->childList(false, SPObject::ActionShow));
749     while (l) {
750         SPObject *o = SP_OBJECT (l->data);
751         if (SP_IS_ITEM (o)) {
752             child = SP_ITEM (o);
753             ac = sp_item_invoke_show (child, arena, key, flags);
754             if (ac) {
755                 nr_arena_item_add_child (ai, ac, ar);
756                 ar = ac;
757                 nr_arena_item_unref (ac);
758             }
759         }
760         l = g_slist_remove (l, o);
761     }
764 void CGroup::hide (unsigned int key) {
765     SPItem * child;
767     GSList *l = g_slist_reverse(_group->childList(false, SPObject::ActionShow));
768     while (l) {
769         SPObject *o = SP_OBJECT (l->data);
770         if (SP_IS_ITEM (o)) {
771             child = SP_ITEM (o);
772             sp_item_invoke_hide (child, key);
773         }
774         l = g_slist_remove (l, o);
775     }
777     if (((SPItemClass *) parent_class)->hide)
778         ((SPItemClass *) parent_class)->hide (_group, key);
781 void CGroup::onOrderChanged (Inkscape::XML::Node *child, Inkscape::XML::Node *, Inkscape::XML::Node *)
783     SPObject *ochild = sp_object_get_child_by_repr(_group, child);
784     if ( ochild && SP_IS_ITEM(ochild) ) {
785         /* TODO: this should be moved into SPItem somehow */
786         SPItemView *v;
787         unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
788         for ( v = SP_ITEM (ochild)->display ; v != NULL ; v = v->next ) {
789             nr_arena_item_set_order (v->arenaitem, position);
790         }
791     }
793     _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
796 /*
797   Local Variables:
798   mode:c++
799   c-file-style:"stroustrup"
800   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
801   indent-tabs-mode:nil
802   fill-column:99
803   End:
804 */
805 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :