Code

merge gsoc2008_johan_path2geom into trunk
[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 "display/curve.h"
27 #include "libnr/nr-matrix-ops.h"
28 #include "libnr/nr-matrix-fns.h"
29 #include "xml/repr.h"
30 #include "svg/svg.h"
31 #include "document.h"
32 #include "style.h"
33 #include "attributes.h"
34 #include "sp-item-transform.h"
35 #include "sp-root.h"
36 #include "sp-use.h"
37 #include "prefs-utils.h"
38 #include "sp-clippath.h"
39 #include "sp-mask.h"
40 #include "sp-path.h"
41 #include "box3d.h"
42 #include "persp3d.h"
43 #include "inkscape.h"
44 #include "desktop-handles.h"
45 #include "selection.h"
46 #include "live_effects/lpeobject.h"
47 #include "live_effects/lpeobject-reference.h"
49 static void sp_group_class_init (SPGroupClass *klass);
50 static void sp_group_init (SPGroup *group);
51 static void sp_group_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
52 static void sp_group_release(SPObject *object);
53 static void sp_group_dispose(GObject *object);
55 static void sp_group_child_added (SPObject * object, Inkscape::XML::Node * child, Inkscape::XML::Node * ref);
56 static void sp_group_remove_child (SPObject * object, Inkscape::XML::Node * child);
57 static void sp_group_order_changed (SPObject * object, Inkscape::XML::Node * child, Inkscape::XML::Node * old_ref, Inkscape::XML::Node * new_ref);
58 static void sp_group_update (SPObject *object, SPCtx *ctx, guint flags);
59 static void sp_group_modified (SPObject *object, guint flags);
60 static Inkscape::XML::Node *sp_group_write (SPObject *object, Inkscape::XML::Node *repr, guint flags);
61 static void sp_group_set(SPObject *object, unsigned key, char const *value);
63 static void sp_group_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
64 static void sp_group_print (SPItem * item, SPPrintContext *ctx);
65 static gchar * sp_group_description (SPItem * item);
66 static NR::Matrix sp_group_set_transform(SPItem *item, NR::Matrix const &xform);
67 static NRArenaItem *sp_group_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags);
68 static void sp_group_hide (SPItem * item, unsigned int key);
69 static void sp_group_snappoints (SPItem const *item, SnapPointsIter p);
71 static void sp_group_update_patheffect(SPLPEItem *lpeitem, bool write);
72 static void sp_group_perform_patheffect(SPGroup *group, SPGroup *topgroup);
74 static SPLPEItemClass * parent_class;
76 GType
77 sp_group_get_type (void)
78 {
79         static GType group_type = 0;
80         if (!group_type) {
81                 GTypeInfo group_info = {
82                         sizeof (SPGroupClass),
83                         NULL,   /* base_init */
84                         NULL,   /* base_finalize */
85                         (GClassInitFunc) sp_group_class_init,
86                         NULL,   /* class_finalize */
87                         NULL,   /* class_data */
88                         sizeof (SPGroup),
89                         16,     /* n_preallocs */
90                         (GInstanceInitFunc) sp_group_init,
91                         NULL,   /* value_table */
92                 };
93                 group_type = g_type_register_static (SP_TYPE_LPE_ITEM, "SPGroup", &group_info, (GTypeFlags)0);
94         }
95         return group_type;
96 }
98 static void
99 sp_group_class_init (SPGroupClass *klass)
101         GObjectClass * object_class;
102         SPObjectClass * sp_object_class;
103         SPItemClass * item_class;
104         SPLPEItemClass * lpe_item_class;
106         object_class = (GObjectClass *) klass;
107         sp_object_class = (SPObjectClass *) klass;
108         item_class = (SPItemClass *) klass;
109         lpe_item_class = (SPLPEItemClass *) klass;
111         parent_class = (SPLPEItemClass *)g_type_class_ref (SP_TYPE_LPE_ITEM);
113         object_class->dispose = sp_group_dispose;
115         sp_object_class->child_added = sp_group_child_added;
116         sp_object_class->remove_child = sp_group_remove_child;
117         sp_object_class->order_changed = sp_group_order_changed;
118         sp_object_class->update = sp_group_update;
119         sp_object_class->modified = sp_group_modified;
120         sp_object_class->set = sp_group_set;
121         sp_object_class->write = sp_group_write;
122         sp_object_class->release = sp_group_release;
123         sp_object_class->build = sp_group_build;
125         item_class->bbox = sp_group_bbox;
126         item_class->print = sp_group_print;
127         item_class->description = sp_group_description;
128         item_class->set_transform = sp_group_set_transform;
129         item_class->show = sp_group_show;
130         item_class->hide = sp_group_hide;
131         item_class->snappoints = sp_group_snappoints;
133         lpe_item_class->update_patheffect = sp_group_update_patheffect;
136 static void
137 sp_group_init (SPGroup *group)
139         group->_layer_mode = SPGroup::GROUP;
140     group->group = new CGroup(group);
141         new (&group->_display_modes) std::map<unsigned int, SPGroup::LayerMode>();
144 static void sp_group_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
146         sp_object_read_attr(object, "inkscape:groupmode");
148         if (((SPObjectClass *)parent_class)->build) {
149                 ((SPObjectClass *)parent_class)->build(object, document, repr);
150         }
153 static void sp_group_release(SPObject *object) {
154         if ( SP_GROUP(object)->_layer_mode == SPGroup::LAYER ) {
155                 sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "layer", object);
156         }
157         if (((SPObjectClass *)parent_class)->release) {
158                 ((SPObjectClass *)parent_class)->release(object);
159         }
162 static void
163 sp_group_dispose(GObject *object)
165         SP_GROUP(object)->_display_modes.~map();
166     delete SP_GROUP(object)->group;
169 static void
170 sp_group_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
172         SPItem *item;
174         item = SP_ITEM (object);
176         if (((SPObjectClass *) (parent_class))->child_added)
177                 (* ((SPObjectClass *) (parent_class))->child_added) (object, child, ref);
179     SP_GROUP(object)->group->onChildAdded(child);
182 /* fixme: hide (Lauris) */
184 static void
185 sp_group_remove_child (SPObject * object, Inkscape::XML::Node * child)
187         if (((SPObjectClass *) (parent_class))->remove_child)
188                 (* ((SPObjectClass *) (parent_class))->remove_child) (object, child);
190     SP_GROUP(object)->group->onChildRemoved(child);
193 static void
194 sp_group_order_changed (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref)
196         if (((SPObjectClass *) (parent_class))->order_changed)
197                 (* ((SPObjectClass *) (parent_class))->order_changed) (object, child, old_ref, new_ref);
199     SP_GROUP(object)->group->onOrderChanged(child, old_ref, new_ref);
202 static void
203 sp_group_update (SPObject *object, SPCtx *ctx, unsigned int flags)
205     if (((SPObjectClass *) (parent_class))->update)
206         ((SPObjectClass *) (parent_class))->update (object, ctx, flags);
208     SP_GROUP(object)->group->onUpdate(ctx, flags);
211 static void
212 sp_group_modified (SPObject *object, guint flags)
214     if (((SPObjectClass *) (parent_class))->modified)
215         ((SPObjectClass *) (parent_class))->modified (object, flags);
217     SP_GROUP(object)->group->onModified(flags);
220 static Inkscape::XML::Node *
221 sp_group_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
223         SPGroup *group;
224         SPObject *child;
225         Inkscape::XML::Node *crepr;
227         group = SP_GROUP (object);
229         if (flags & SP_OBJECT_WRITE_BUILD) {
230                 GSList *l;
231                 if (!repr) {
232                     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
233                     repr = xml_doc->createElement("svg:g");
234                 }
235                 l = NULL;
236                 for (child = sp_object_first_child(object); child != NULL; child = SP_OBJECT_NEXT(child) ) {
237                         crepr = child->updateRepr(NULL, flags);
238                         if (crepr) l = g_slist_prepend (l, crepr);
239                 }
240                 while (l) {
241                         repr->addChild((Inkscape::XML::Node *) l->data, NULL);
242                         Inkscape::GC::release((Inkscape::XML::Node *) l->data);
243                         l = g_slist_remove (l, l->data);
244                 }
245         } else {
246                 for (child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
247                         child->updateRepr(flags);
248                 }
249         }
251         if ( flags & SP_OBJECT_WRITE_EXT ) {
252                 const char *value;
253                 if ( group->_layer_mode == SPGroup::LAYER ) {
254                         value = "layer";
255                 } else if ( flags & SP_OBJECT_WRITE_ALL ) {
256                         value = "group";
257                 } else {
258                         value = NULL;
259                 }
260                 repr->setAttribute("inkscape:groupmode", value);
261         }
263         if (((SPObjectClass *) (parent_class))->write)
264                 ((SPObjectClass *) (parent_class))->write (object, repr, flags);
266         return repr;
269 static void
270 sp_group_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
272     SP_GROUP(item)->group->calculateBBox(bbox, transform, flags);
275 static void
276 sp_group_print (SPItem * item, SPPrintContext *ctx)
278     SP_GROUP(item)->group->onPrint(ctx);
281 static gchar * sp_group_description (SPItem * item)
283     return SP_GROUP(item)->group->getDescription();
286 static NR::Matrix
287 sp_group_set_transform(SPItem *item, NR::Matrix const &xform)
289     Inkscape::Selection *selection = sp_desktop_selection(inkscape_active_desktop());
290     persp3d_split_perspectives_according_to_selection(selection);
292     NR::Matrix last_trans;
293     sp_svg_transform_read(SP_OBJECT_REPR(item)->attribute("transform"), &last_trans);
294     NR::Matrix inc_trans = last_trans.inverse()*xform;
296     std::list<Persp3D *> plist = selection->perspList();
297     for (std::list<Persp3D *>::iterator i = plist.begin(); i != plist.end(); ++i) {
298         persp3d_apply_affine_transformation(*i, inc_trans);
299     }
301     return xform;
304 static void sp_group_set(SPObject *object, unsigned key, char const *value) {
305         SPGroup *group = SP_GROUP(object);
307         switch (key) {
308                 case SP_ATTR_INKSCAPE_GROUPMODE:
309                         if ( value && !strcmp(value, "layer") ) {
310                                 group->setLayerMode(SPGroup::LAYER);
311                         } else {
312                                 group->setLayerMode(SPGroup::GROUP);
313                         }
314                     break;
315                 default: {
316                         if (((SPObjectClass *) (parent_class))->set) {
317                                 (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
318                         }
319                 }
320         }
323 static NRArenaItem *
324 sp_group_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags)
326     return SP_GROUP(item)->group->show(arena, key, flags);
329 static void
330 sp_group_hide (SPItem *item, unsigned int key)
332     SP_GROUP(item)->group->hide(key);
335 static void sp_group_snappoints (SPItem const *item, SnapPointsIter p)
337         for (SPObject const *o = sp_object_first_child(SP_OBJECT(item));
338              o != NULL;
339              o = SP_OBJECT_NEXT(o))
340         {
341                 if (SP_IS_ITEM(o)) {
342             sp_item_snappoints(SP_ITEM(o), false, p);
343                 }
344         }
348 void
349 sp_item_group_ungroup (SPGroup *group, GSList **children, bool do_done)
351         g_return_if_fail (group != NULL);
352         g_return_if_fail (SP_IS_GROUP (group));
354         SPDocument *doc = SP_OBJECT_DOCUMENT (group);
355         SPObject *root = SP_DOCUMENT_ROOT (doc);
356         SPObject *defs = SP_OBJECT (SP_ROOT (root)->defs);
358         SPItem *gitem = SP_ITEM (group);
359         Inkscape::XML::Node *grepr = SP_OBJECT_REPR (gitem);
361         g_return_if_fail (!strcmp (grepr->name(), "svg:g") || !strcmp (grepr->name(), "svg:a") || !strcmp (grepr->name(), "svg:switch"));
363       // this converts the gradient/pattern fill/stroke on the group, if any, to userSpaceOnUse
364       sp_item_adjust_paint_recursive (gitem, NR::identity(), NR::identity(), false);
366         SPItem *pitem = SP_ITEM (SP_OBJECT_PARENT (gitem));
367         Inkscape::XML::Node *prepr = SP_OBJECT_REPR (pitem);
369         if (SP_IS_BOX3D(gitem)) {
370             group = box3d_convert_to_group(SP_BOX3D(gitem));
371             gitem = SP_ITEM(group);
372         }
374         sp_lpe_item_remove_all_path_effects(SP_LPE_ITEM(group), false);
376         /* Step 1 - generate lists of children objects */
377         GSList *items = NULL;
378         GSList *objects = NULL;
379         for (SPObject *child = sp_object_first_child(SP_OBJECT(group)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
381                 if (SP_IS_ITEM (child)) {
383                         SPItem *citem = SP_ITEM (child);
385                         /* Merging of style */
386                         // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
387                         // it here _before_ the new transform is set, so as to use the pre-transform bbox
388                         sp_item_adjust_paint_recursive (citem, NR::identity(), NR::identity(), false);
390                         sp_style_merge_from_dying_parent(SP_OBJECT_STYLE(child), SP_OBJECT_STYLE(gitem));
391                         /*
392                          * fixme: We currently make no allowance for the case where child is cloned
393                          * and the group has any style settings.
394                          *
395                          * (This should never occur with documents created solely with the current
396                          * version of inkscape without using the XML editor: we usually apply group
397                          * style changes to children rather than to the group itself.)
398                          *
399                          * If the group has no style settings, then
400                          * sp_style_merge_from_dying_parent should be a no-op.  Otherwise (i.e. if
401                          * we change the child's style to compensate for its parent going away)
402                          * then those changes will typically be reflected in any clones of child,
403                          * whereas we'd prefer for Ungroup not to affect the visual appearance.
404                          *
405                          * The only way of preserving styling appearance in general is for child to
406                          * be put into a new group -- a somewhat surprising response to an Ungroup
407                          * command.  We could add a new groupmode:transparent that would mostly
408                          * hide the existence of such groups from the user (i.e. editing behaves as
409                          * if the transparent group's children weren't in a group), though that's
410                          * extra complication & maintenance burden and this case is rare.
411                          */
413                         child->updateRepr();
415                         Inkscape::XML::Node *nrepr = SP_OBJECT_REPR (child)->duplicate(prepr->document());
417                         // Merging transform
418                         NR::Matrix ctrans;
419                         NR::Matrix const g(gitem->transform);
420                         if (SP_IS_USE(citem) && sp_use_get_original (SP_USE(citem)) &&
421                                         SP_OBJECT_PARENT (sp_use_get_original (SP_USE(citem))) == SP_OBJECT(group)) {
422                                 // make sure a clone's effective transform is the same as was under group
423                                 ctrans = g.inverse() * citem->transform * g;
424                         } else {
425                                 ctrans = citem->transform * g;
426                         }
428                         // FIXME: constructing a transform that would fully preserve the appearance of a
429                         // textpath if it is ungrouped with its path seems to be impossible in general
430                         // case. E.g. if the group was squeezed, to keep the ungrouped textpath squeezed
431                         // as well, we'll need to relink it to some "virtual" path which is inversely
432                         // stretched relative to the actual path, and then squeeze the textpath back so it
433                         // would both fit the actual path _and_ be squeezed as before. It's a bummer.
435                         // This is just a way to temporarily remember the transform in repr. When repr is
436                         // reattached outside of the group, the transform will be written more properly
437                         // (i.e. optimized into the object if the corresponding preference is set)
438                         gchar *affinestr=sp_svg_transform_write(ctrans);
439                         nrepr->setAttribute("transform", affinestr);
440                         g_free(affinestr);
442                         items = g_slist_prepend (items, nrepr);
444                 } else {
445                         Inkscape::XML::Node *nrepr = SP_OBJECT_REPR (child)->duplicate(prepr->document());
446                         objects = g_slist_prepend (objects, nrepr);
447                 }
448         }
450         /* Step 2 - clear group */
451         // remember the position of the group
452         gint pos = SP_OBJECT_REPR(group)->position();
454         // the group is leaving forever, no heir, clones should take note; its children however are going to reemerge
455         SP_OBJECT (group)->deleteObject(true, false);
457         /* Step 3 - add nonitems */
458         if (objects) {
459             Inkscape::XML::Node *last_def = SP_OBJECT_REPR(defs)->lastChild();
460             while (objects) {
461                 SP_OBJECT_REPR(defs)->addChild((Inkscape::XML::Node *) objects->data, last_def);
462                 Inkscape::GC::release((Inkscape::XML::Node *) objects->data);
463                 objects = g_slist_remove (objects, objects->data);
464             }
465         }
467         /* Step 4 - add items */
468         while (items) {
469                 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) items->data;
470                 // add item
471                 prepr->appendChild(repr);
472                 // restore position; since the items list was prepended (i.e. reverse), we now add
473                 // all children at the same pos, which inverts the order once again
474                 repr->setPosition(pos > 0 ? pos : 0);
476                 // fill in the children list if non-null
477                 SPItem *item = (SPItem *) doc->getObjectByRepr(repr);
479                 sp_item_write_transform(item, repr, item->transform, NULL, false);
481                 Inkscape::GC::release(repr);
482                 if (children && SP_IS_ITEM (item))
483                         *children = g_slist_prepend (*children, item);
485                 items = g_slist_remove (items, items->data);
486         }
488         if (do_done)
489                 sp_document_done (doc, SP_VERB_NONE, _("Ungroup"));
492 /*
493  * some API for list aspect of SPGroup
494  */
496 GSList *
497 sp_item_group_item_list (SPGroup * group)
499         GSList *s;
500         SPObject *o;
502         g_return_val_if_fail (group != NULL, NULL);
503         g_return_val_if_fail (SP_IS_GROUP (group), NULL);
505         s = NULL;
507         for ( o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
508                 if (SP_IS_ITEM (o)) {
509                         s = g_slist_prepend (s, o);
510                 }
511         }
513         return g_slist_reverse (s);
516 SPObject *
517 sp_item_group_get_child_by_name (SPGroup *group, SPObject *ref, const gchar *name)
519         SPObject *child;
520         child = (ref) ? SP_OBJECT_NEXT(ref) : sp_object_first_child(SP_OBJECT(group));
521         while ( child && strcmp (SP_OBJECT_REPR(child)->name(), name) ) {
522                 child = SP_OBJECT_NEXT(child);
523         }
524         return child;
527 void SPGroup::setLayerMode(LayerMode mode) {
528         if ( _layer_mode != mode ) {
529                 if ( mode == LAYER ) {
530                         sp_document_add_resource(SP_OBJECT_DOCUMENT(this), "layer", this);
531                 } else {
532                         sp_document_remove_resource(SP_OBJECT_DOCUMENT(this), "layer", this);
533                 }
534                 _layer_mode = mode;
535                 _updateLayerMode();
536         }
539 SPGroup::LayerMode SPGroup::layerDisplayMode(unsigned int dkey) const {
540         std::map<unsigned int, LayerMode>::const_iterator iter;
541         iter = _display_modes.find(dkey);
542         if ( iter != _display_modes.end() ) {
543                 return (*iter).second;
544         } else {
545                 return GROUP;
546         }
549 void SPGroup::setLayerDisplayMode(unsigned int dkey, SPGroup::LayerMode mode) {
550         if ( layerDisplayMode(dkey) != mode ) {
551                 _display_modes[dkey] = mode;
552                 _updateLayerMode(dkey);
553         }
556 void SPGroup::_updateLayerMode(unsigned int display_key) {
557         SPItemView *view;
558         for ( view = this->display ; view ; view = view->next ) {
559                 if ( !display_key || view->key == display_key ) {
560                         NRArenaGroup *arena_group=NR_ARENA_GROUP(view->arenaitem);
561                         if (arena_group) {
562                                 nr_arena_group_set_transparent(arena_group, effectiveLayerMode(view->key) == SPGroup::LAYER);
563                         }
564                 }
565         }
568 void SPGroup::translateChildItems(NR::translate const &tr)
570         if (this->hasChildren())
571         {
572                 SPObject *o = NULL;
573                 for (o = sp_object_first_child(SP_OBJECT(this)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
574                         if (SP_IS_ITEM (o)) {
575                                 sp_item_move_rel(static_cast<SPItem *>(o), tr);
576                         }
577                 }
578         }
581 CGroup::CGroup(SPGroup *group) {
582     _group = group;
585 CGroup::~CGroup() {
588 void CGroup::onChildAdded(Inkscape::XML::Node *child) {
589     SPObject *last_child = _group->lastChild();
590     if (last_child && SP_OBJECT_REPR(last_child) == child) {
591         // optimization for the common special case where the child is being added at the end
592         SPObject *ochild = last_child;
593         if ( SP_IS_ITEM(ochild) ) {
594             /* TODO: this should be moved into SPItem somehow */
595             SPItemView *v;
596             NRArenaItem *ac;
598             for (v = _group->display; v != NULL; v = v->next) {
599                 ac = sp_item_invoke_show (SP_ITEM (ochild), NR_ARENA_ITEM_ARENA (v->arenaitem), v->key, v->flags);
601                 if (ac) {
602                     nr_arena_item_append_child (v->arenaitem, ac);
603                     nr_arena_item_unref (ac);
604                 }
605             }
606         }
607     } else {    // general case
608         SPObject *ochild = sp_object_get_child_by_repr(_group, child);
609         if ( ochild && SP_IS_ITEM(ochild) ) {
610             /* TODO: this should be moved into SPItem somehow */
611             SPItemView *v;
612             NRArenaItem *ac;
614             unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
616             for (v = _group->display; v != NULL; v = v->next) {
617                 ac = sp_item_invoke_show (SP_ITEM (ochild), NR_ARENA_ITEM_ARENA (v->arenaitem), v->key, v->flags);
619                 if (ac) {
620                     nr_arena_item_add_child (v->arenaitem, ac, NULL);
621                     nr_arena_item_set_order (ac, position);
622                     nr_arena_item_unref (ac);
623                 }
624             }
625         }
626     }
628     _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
631 void CGroup::onChildRemoved(Inkscape::XML::Node */*child*/) {
632     _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
635 void CGroup::onUpdate(SPCtx *ctx, unsigned int flags) {
636     SPItemCtx *ictx, cctx;
638     ictx = (SPItemCtx *) ctx;
639     cctx = *ictx;
641     if (flags & SP_OBJECT_MODIFIED_FLAG) {
642       flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
643     }
645     flags &= SP_OBJECT_MODIFIED_CASCADE;
647     if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
648       SPObject *object = SP_OBJECT(_group);
649       for (SPItemView *v = SP_ITEM(_group)->display; v != NULL; v = v->next) {
650         nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
651       }
652     }
654     GSList *l = g_slist_reverse(_group->childList(true, SPObject::ActionUpdate));
655     while (l) {
656         SPObject *child = SP_OBJECT (l->data);
657         l = g_slist_remove (l, child);
658         if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
659             if (SP_IS_ITEM (child)) {
660                 SPItem const &chi = *SP_ITEM(child);
661                 cctx.i2doc = chi.transform * ictx->i2doc;
662                 cctx.i2vp = chi.transform * ictx->i2vp;
663                 child->updateDisplay((SPCtx *)&cctx, flags);
664             } else {
665                 child->updateDisplay(ctx, flags);
666             }
667         }
668         g_object_unref (G_OBJECT (child));
669     }
672 void CGroup::onModified(guint flags) {
673     SPObject *child;
675     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
676     flags &= SP_OBJECT_MODIFIED_CASCADE;
678     if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
679       SPObject *object = SP_OBJECT(_group);
680       for (SPItemView *v = SP_ITEM(_group)->display; v != NULL; v = v->next) {
681         nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
682       }
683     }
685     GSList *l = g_slist_reverse(_group->childList(true));
686     while (l) {
687         child = SP_OBJECT (l->data);
688         l = g_slist_remove (l, child);
689         if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
690             child->emitModified(flags);
691         }
692         g_object_unref (G_OBJECT (child));
693     }
696 void CGroup::calculateBBox(NRRect *bbox, NR::Matrix const &transform, unsigned const flags) {
698     NR::Maybe<NR::Rect> dummy_bbox = NR::Nothing();
700     GSList *l = _group->childList(false, SPObject::ActionBBox);
701     while (l) {
702         SPObject *o = SP_OBJECT (l->data);
703         if (SP_IS_ITEM(o)) {
704             SPItem *child = SP_ITEM(o);
705             NR::Matrix const ct(child->transform * transform);
706             sp_item_invoke_bbox_full(child, &dummy_bbox, ct, flags, FALSE);
707         }
708         l = g_slist_remove (l, o);
709     }
711     *bbox = NRRect(dummy_bbox);
714 void CGroup::onPrint(SPPrintContext *ctx) {
715     GSList *l = g_slist_reverse(_group->childList(false));
716     while (l) {
717         SPObject *o = SP_OBJECT (l->data);
718         if (SP_IS_ITEM(o)) {
719             sp_item_invoke_print (SP_ITEM (o), ctx);
720         }
721         l = g_slist_remove (l, o);
722     }
725 gint CGroup::getItemCount() {
726     gint len = 0;
727     for (SPObject *o = sp_object_first_child(SP_OBJECT(_group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
728         if (SP_IS_ITEM(o)) {
729             len++;
730         }
731     }
733     return len;
736 gchar *CGroup::getDescription() {
737     gint len = getItemCount();
738     return g_strdup_printf(
739             ngettext("<b>Group</b> of <b>%d</b> object",
740                  "<b>Group</b> of <b>%d</b> objects",
741                  len), len);
744 NRArenaItem *CGroup::show (NRArena *arena, unsigned int key, unsigned int flags) {
745     NRArenaItem *ai;
746     SPObject *object = SP_OBJECT(_group);
748     ai = NRArenaGroup::create(arena);
750     nr_arena_group_set_transparent(NR_ARENA_GROUP (ai),
751                                    _group->effectiveLayerMode(key) ==
752                          SPGroup::LAYER);
753     nr_arena_group_set_style(NR_ARENA_GROUP(ai), SP_OBJECT_STYLE(object));
755     _showChildren(arena, ai, key, flags);
756     return ai;
759 void CGroup::_showChildren (NRArena *arena, NRArenaItem *ai, unsigned int key, unsigned int flags) {
760     NRArenaItem *ac = NULL;
761     NRArenaItem *ar = NULL;
762     SPItem * child = NULL;
763     GSList *l = g_slist_reverse(_group->childList(false, SPObject::ActionShow));
764     while (l) {
765         SPObject *o = SP_OBJECT (l->data);
766         if (SP_IS_ITEM (o)) {
767             child = SP_ITEM (o);
768             ac = sp_item_invoke_show (child, arena, key, flags);
769             if (ac) {
770                 nr_arena_item_add_child (ai, ac, ar);
771                 ar = ac;
772                 nr_arena_item_unref (ac);
773             }
774         }
775         l = g_slist_remove (l, o);
776     }
779 void CGroup::hide (unsigned int key) {
780     SPItem * child;
782     GSList *l = g_slist_reverse(_group->childList(false, SPObject::ActionShow));
783     while (l) {
784         SPObject *o = SP_OBJECT (l->data);
785         if (SP_IS_ITEM (o)) {
786             child = SP_ITEM (o);
787             sp_item_invoke_hide (child, key);
788         }
789         l = g_slist_remove (l, o);
790     }
792     if (((SPItemClass *) parent_class)->hide)
793         ((SPItemClass *) parent_class)->hide (_group, key);
796 void CGroup::onOrderChanged (Inkscape::XML::Node *child, Inkscape::XML::Node *, Inkscape::XML::Node *)
798     SPObject *ochild = sp_object_get_child_by_repr(_group, child);
799     if ( ochild && SP_IS_ITEM(ochild) ) {
800         /* TODO: this should be moved into SPItem somehow */
801         SPItemView *v;
802         unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
803         for ( v = SP_ITEM (ochild)->display ; v != NULL ; v = v->next ) {
804             nr_arena_item_set_order (v->arenaitem, position);
805         }
806     }
808     _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
811 static void
812 sp_group_update_patheffect (SPLPEItem *lpeitem, bool write)
814 #ifdef GROUP_VERBOSE
815     g_message("sp_group_update_patheffect: %p\n", lpeitem);
816 #endif
817     g_return_if_fail (lpeitem != NULL);
818     g_return_if_fail (SP_IS_GROUP (lpeitem));
820     GSList const *item_list = sp_item_group_item_list(SP_GROUP(lpeitem));
821     for ( GSList const *iter = item_list; iter; iter = iter->next ) {
822         SPObject *subitem = static_cast<SPObject *>(iter->data);
823         if (SP_IS_LPE_ITEM(subitem)) {
824             if (SP_LPE_ITEM_CLASS (G_OBJECT_GET_CLASS (subitem))->update_patheffect) {
825                 SP_LPE_ITEM_CLASS (G_OBJECT_GET_CLASS (subitem))->update_patheffect (SP_LPE_ITEM(subitem), write);
826             }
827         }
828     }
829     
830     if (sp_lpe_item_has_path_effect(lpeitem) && sp_lpe_item_path_effects_enabled(lpeitem)) {
831         for (PathEffectList::iterator it = lpeitem->path_effect_list->begin(); it != lpeitem->path_effect_list->end(); it++)
832         {
833             LivePathEffectObject *lpeobj = (*it)->lpeobject;
834             lpeobj->lpe->doBeforeEffect(lpeitem);
835         }
836         
837         sp_group_perform_patheffect(SP_GROUP(lpeitem), SP_GROUP(lpeitem));
838     }
841 static void
842 sp_group_perform_patheffect(SPGroup *group, SPGroup *topgroup)
844     GSList const *item_list = sp_item_group_item_list(SP_GROUP(group));
845     for ( GSList const *iter = item_list; iter; iter = iter->next ) {
846         SPObject *subitem = static_cast<SPObject *>(iter->data);
847         if (SP_IS_GROUP(subitem)) {
848             sp_group_perform_patheffect(SP_GROUP(subitem), topgroup);
849         } else if (SP_IS_SHAPE(subitem)) {
850             SPCurve * c = sp_shape_get_curve(SP_SHAPE(subitem));
851             sp_lpe_item_perform_path_effect(SP_LPE_ITEM(topgroup), c);
852             sp_shape_set_curve(SP_SHAPE(subitem), c, TRUE);
853             
854             Inkscape::XML::Node *repr = SP_OBJECT_REPR(subitem);
855  
856                 NArtBpath const *abp = c->first_bpath();
857                 if (abp) {
858                     gchar *str = sp_svg_write_path(abp);
859                     repr->setAttribute("d", str);
860                     g_free(str);
861                 } else {
862                    repr->setAttribute("d", "");
863                 }
865                 c->unref();
866         }
867     }
870 /*
871   Local Variables:
872   mode:c++
873   c-file-style:"stroustrup"
874   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
875   indent-tabs-mode:nil
876   fill-column:99
877   End:
878 */
879 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :