Code

Make curvature work again by fixing a minor omission
[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 "sp-offset.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/effect.h"
47 #include "live_effects/lpeobject.h"
48 #include "live_effects/lpeobject-reference.h"
49 #include "sp-title.h"
50 #include "sp-desc.h"
51 #include "sp-switch.h"
53 static void sp_group_class_init (SPGroupClass *klass);
54 static void sp_group_init (SPGroup *group);
55 static void sp_group_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
56 static void sp_group_release(SPObject *object);
57 static void sp_group_dispose(GObject *object);
59 static void sp_group_child_added (SPObject * object, Inkscape::XML::Node * child, Inkscape::XML::Node * ref);
60 static void sp_group_remove_child (SPObject * object, Inkscape::XML::Node * child);
61 static void sp_group_order_changed (SPObject * object, Inkscape::XML::Node * child, Inkscape::XML::Node * old_ref, Inkscape::XML::Node * new_ref);
62 static void sp_group_update (SPObject *object, SPCtx *ctx, guint flags);
63 static void sp_group_modified (SPObject *object, guint flags);
64 static Inkscape::XML::Node *sp_group_write (SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
65 static void sp_group_set(SPObject *object, unsigned key, char const *value);
67 static void sp_group_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags);
68 static void sp_group_print (SPItem * item, SPPrintContext *ctx);
69 static gchar * sp_group_description (SPItem * item);
70 static Geom::Matrix sp_group_set_transform(SPItem *item, Geom::Matrix const &xform);
71 static NRArenaItem *sp_group_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags);
72 static void sp_group_hide (SPItem * item, unsigned int key);
73 static void sp_group_snappoints (SPItem const *item, bool const target, SnapPointsWithType &p, Inkscape::SnapPreferences const *snapprefs);
75 static void sp_group_update_patheffect(SPLPEItem *lpeitem, bool write);
76 static void sp_group_perform_patheffect(SPGroup *group, SPGroup *topgroup, bool write);
78 static SPLPEItemClass * parent_class;
80 GType
81 sp_group_get_type (void)
82 {
83     static GType group_type = 0;
84     if (!group_type) {
85         GTypeInfo group_info = {
86             sizeof (SPGroupClass),
87             NULL,       /* base_init */
88             NULL,       /* base_finalize */
89             (GClassInitFunc) sp_group_class_init,
90             NULL,       /* class_finalize */
91             NULL,       /* class_data */
92             sizeof (SPGroup),
93             16, /* n_preallocs */
94             (GInstanceInitFunc) sp_group_init,
95             NULL,       /* value_table */
96         };
97         group_type = g_type_register_static (SP_TYPE_LPE_ITEM, "SPGroup", &group_info, (GTypeFlags)0);
98     }
99     return group_type;
102 static void
103 sp_group_class_init (SPGroupClass *klass)
105     GObjectClass * object_class;
106     SPObjectClass * sp_object_class;
107     SPItemClass * item_class;
108     SPLPEItemClass * lpe_item_class;
110     object_class = (GObjectClass *) klass;
111     sp_object_class = (SPObjectClass *) klass;
112     item_class = (SPItemClass *) klass;
113     lpe_item_class = (SPLPEItemClass *) klass;
115     parent_class = (SPLPEItemClass *)g_type_class_ref (SP_TYPE_LPE_ITEM);
117     object_class->dispose = sp_group_dispose;
119     sp_object_class->child_added = sp_group_child_added;
120     sp_object_class->remove_child = sp_group_remove_child;
121     sp_object_class->order_changed = sp_group_order_changed;
122     sp_object_class->update = sp_group_update;
123     sp_object_class->modified = sp_group_modified;
124     sp_object_class->set = sp_group_set;
125     sp_object_class->write = sp_group_write;
126     sp_object_class->release = sp_group_release;
127     sp_object_class->build = sp_group_build;
129     item_class->bbox = sp_group_bbox;
130     item_class->print = sp_group_print;
131     item_class->description = sp_group_description;
132         item_class->set_transform = sp_group_set_transform;
133     item_class->show = sp_group_show;
134     item_class->hide = sp_group_hide;
135     item_class->snappoints = sp_group_snappoints;
137     lpe_item_class->update_patheffect = sp_group_update_patheffect;
140 static void
141 sp_group_init (SPGroup *group)
143     group->_layer_mode = SPGroup::GROUP;
144     group->group = new CGroup(group);
145     new (&group->_display_modes) std::map<unsigned int, SPGroup::LayerMode>();
148 static void sp_group_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
150     sp_object_read_attr(object, "inkscape:groupmode");
152     if (((SPObjectClass *)parent_class)->build) {
153         ((SPObjectClass *)parent_class)->build(object, document, repr);
154     }
157 static void sp_group_release(SPObject *object) {
158     if ( SP_GROUP(object)->_layer_mode == SPGroup::LAYER ) {
159         sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "layer", object);
160     }
161     if (((SPObjectClass *)parent_class)->release) {
162         ((SPObjectClass *)parent_class)->release(object);
163     }
166 static void
167 sp_group_dispose(GObject *object)
169     SP_GROUP(object)->_display_modes.~map();
170     delete SP_GROUP(object)->group;
173 static void
174 sp_group_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
176     SPItem *item;
178     item = SP_ITEM (object);
180     if (((SPObjectClass *) (parent_class))->child_added)
181         (* ((SPObjectClass *) (parent_class))->child_added) (object, child, ref);
183     SP_GROUP(object)->group->onChildAdded(child);
186 /* fixme: hide (Lauris) */
188 static void
189 sp_group_remove_child (SPObject * object, Inkscape::XML::Node * child)
191     if (((SPObjectClass *) (parent_class))->remove_child)
192         (* ((SPObjectClass *) (parent_class))->remove_child) (object, child);
194     SP_GROUP(object)->group->onChildRemoved(child);
197 static void
198 sp_group_order_changed (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref)
200     if (((SPObjectClass *) (parent_class))->order_changed)
201         (* ((SPObjectClass *) (parent_class))->order_changed) (object, child, old_ref, new_ref);
203     SP_GROUP(object)->group->onOrderChanged(child, old_ref, new_ref);
206 static void
207 sp_group_update (SPObject *object, SPCtx *ctx, unsigned int flags)
209     if (((SPObjectClass *) (parent_class))->update)
210         ((SPObjectClass *) (parent_class))->update (object, ctx, flags);
212     SP_GROUP(object)->group->onUpdate(ctx, flags);
215 static void
216 sp_group_modified (SPObject *object, guint flags)
218     if (((SPObjectClass *) (parent_class))->modified)
219         ((SPObjectClass *) (parent_class))->modified (object, flags);
221     SP_GROUP(object)->group->onModified(flags);
224 static Inkscape::XML::Node *
225 sp_group_write (SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
227     SPGroup *group;
228     SPObject *child;
229     Inkscape::XML::Node *crepr;
231     group = SP_GROUP (object);
233     if (flags & SP_OBJECT_WRITE_BUILD) {
234         GSList *l;
235         if (!repr) {
236             if (SP_IS_SWITCH(object))
237                 repr = xml_doc->createElement("svg:switch");
238             else
239                 repr = xml_doc->createElement("svg:g");
240         }
241         l = NULL;
242         for (child = sp_object_first_child(object); child != NULL; child = SP_OBJECT_NEXT(child) ) {
243             if (SP_IS_TITLE(child) || SP_IS_DESC(child)) continue;
244             crepr = child->updateRepr(xml_doc, NULL, flags);
245             if (crepr) l = g_slist_prepend (l, crepr);
246         }
247         while (l) {
248             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
249             Inkscape::GC::release((Inkscape::XML::Node *) l->data);
250             l = g_slist_remove (l, l->data);
251         }
252     } else {
253         for (child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
254             if (SP_IS_TITLE(child) || SP_IS_DESC(child)) continue;
255             child->updateRepr(flags);
256         }
257     }
259     if ( flags & SP_OBJECT_WRITE_EXT ) {
260         const char *value;
261         if ( group->_layer_mode == SPGroup::LAYER ) {
262             value = "layer";
263         } else if ( flags & SP_OBJECT_WRITE_ALL ) {
264             value = "group";
265         } else {
266             value = NULL;
267         }
268         repr->setAttribute("inkscape:groupmode", value);
269     }
271     if (((SPObjectClass *) (parent_class))->write)
272         ((SPObjectClass *) (parent_class))->write (object, xml_doc, repr, flags);
274     return repr;
277 static void
278 sp_group_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags)
280     SP_GROUP(item)->group->calculateBBox(bbox, transform, flags);
283 static void
284 sp_group_print (SPItem * item, SPPrintContext *ctx)
286     SP_GROUP(item)->group->onPrint(ctx);
289 static gchar * sp_group_description (SPItem * item)
291     return SP_GROUP(item)->group->getDescription();
294 static Geom::Matrix
295 sp_group_set_transform(SPItem *item, Geom::Matrix const &xform)
297     Inkscape::Selection *selection = sp_desktop_selection(inkscape_active_desktop());
298     persp3d_split_perspectives_according_to_selection(selection);
300     Geom::Matrix last_trans;
301     sp_svg_transform_read(SP_OBJECT_REPR(item)->attribute("transform"), &last_trans);
302     Geom::Matrix inc_trans = last_trans.inverse()*xform;
304     std::list<Persp3D *> plist = selection->perspList();
305     for (std::list<Persp3D *>::iterator i = plist.begin(); i != plist.end(); ++i) {
306         persp3d_apply_affine_transformation(*i, inc_trans);
307     }
309     return xform;
312 static void sp_group_set(SPObject *object, unsigned key, char const *value) {
313     SPGroup *group = SP_GROUP(object);
315     switch (key) {
316         case SP_ATTR_INKSCAPE_GROUPMODE:
317             if ( value && !strcmp(value, "layer") ) {
318                 group->setLayerMode(SPGroup::LAYER);
319             } else {
320                 group->setLayerMode(SPGroup::GROUP);
321             }
322             break;
323         default: {
324             if (((SPObjectClass *) (parent_class))->set) {
325                 (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
326             }
327         }
328     }
331 static NRArenaItem *
332 sp_group_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags)
334     return SP_GROUP(item)->group->show(arena, key, flags);
337 static void
338 sp_group_hide (SPItem *item, unsigned int key)
340     SP_GROUP(item)->group->hide(key);
343 static void sp_group_snappoints (SPItem const *item, bool const target, SnapPointsWithType &p, Inkscape::SnapPreferences const *snapprefs)
345     for (SPObject const *o = sp_object_first_child(SP_OBJECT(item));
346          o != NULL;
347          o = SP_OBJECT_NEXT(o))
348     {
349         if (SP_IS_ITEM(o)) {
350             sp_item_snappoints(SP_ITEM(o), target, p, snapprefs);
351         }
352     }
356 void
357 sp_item_group_ungroup (SPGroup *group, GSList **children, bool do_done)
359     g_return_if_fail (group != NULL);
360     g_return_if_fail (SP_IS_GROUP (group));
362     SPDocument *doc = SP_OBJECT_DOCUMENT (group);
363     SPObject *root = SP_DOCUMENT_ROOT (doc);
364     SPObject *defs = SP_OBJECT (SP_ROOT (root)->defs);
366     SPItem *gitem = SP_ITEM (group);
367     Inkscape::XML::Node *grepr = SP_OBJECT_REPR (gitem);
369     g_return_if_fail (!strcmp (grepr->name(), "svg:g") || !strcmp (grepr->name(), "svg:a") || !strcmp (grepr->name(), "svg:switch"));
371     // this converts the gradient/pattern fill/stroke on the group, if any, to userSpaceOnUse
372     sp_item_adjust_paint_recursive (gitem, Geom::identity(), Geom::identity(), false);
374     SPItem *pitem = SP_ITEM (SP_OBJECT_PARENT (gitem));
375     Inkscape::XML::Node *prepr = SP_OBJECT_REPR (pitem);
377         if (SP_IS_BOX3D(gitem)) {
378                 group = box3d_convert_to_group(SP_BOX3D(gitem));
379                 gitem = SP_ITEM(group);
380         }
382         sp_lpe_item_remove_all_path_effects(SP_LPE_ITEM(group), false);
384     /* Step 1 - generate lists of children objects */
385     GSList *items = NULL;
386     GSList *objects = NULL;
387     for (SPObject *child = sp_object_first_child(SP_OBJECT(group)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
389         if (SP_IS_ITEM (child)) {
391             SPItem *citem = SP_ITEM (child);
393             /* Merging of style */
394             // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
395             // it here _before_ the new transform is set, so as to use the pre-transform bbox
396             sp_item_adjust_paint_recursive (citem, Geom::identity(), Geom::identity(), false);
398             sp_style_merge_from_dying_parent(SP_OBJECT_STYLE(child), SP_OBJECT_STYLE(gitem));
399             /*
400              * fixme: We currently make no allowance for the case where child is cloned
401              * and the group has any style settings.
402              *
403              * (This should never occur with documents created solely with the current
404              * version of inkscape without using the XML editor: we usually apply group
405              * style changes to children rather than to the group itself.)
406              *
407              * If the group has no style settings, then
408              * sp_style_merge_from_dying_parent should be a no-op.  Otherwise (i.e. if
409              * we change the child's style to compensate for its parent going away)
410              * then those changes will typically be reflected in any clones of child,
411              * whereas we'd prefer for Ungroup not to affect the visual appearance.
412              *
413              * The only way of preserving styling appearance in general is for child to
414              * be put into a new group -- a somewhat surprising response to an Ungroup
415              * command.  We could add a new groupmode:transparent that would mostly
416              * hide the existence of such groups from the user (i.e. editing behaves as
417              * if the transparent group's children weren't in a group), though that's
418              * extra complication & maintenance burden and this case is rare.
419              */
421             child->updateRepr();
423             Inkscape::XML::Node *nrepr = SP_OBJECT_REPR (child)->duplicate(prepr->document());
425             // Merging transform
426             Geom::Matrix ctrans;
427             Geom::Matrix const g(gitem->transform);
428             if (SP_IS_USE(citem) && sp_use_get_original (SP_USE(citem)) &&
429                     SP_OBJECT_PARENT (sp_use_get_original (SP_USE(citem))) == SP_OBJECT(group)) {
430                 // make sure a clone's effective transform is the same as was under group
431                 ctrans = g.inverse() * citem->transform * g;
432             } else {
433                 // We should not apply the group's transformation to both a linked offset AND to its source
434                 if (SP_IS_OFFSET(citem)) { // Do we have an offset at hand (whether it's dynamic or linked)?
435                         SPItem *source = sp_offset_get_source(SP_OFFSET(citem));
436                         // When dealing with a chain of linked offsets, the transformation of an offset will be
437                         // tied to the transformation of the top-most source, not to any of the intermediate
438                         // offsets. So let's find the top-most source
439                         while (source != NULL && SP_IS_OFFSET(source)) {
440                                 source = sp_offset_get_source(SP_OFFSET(source));
441                         }
442                         if (source != NULL && // If true then we must be dealing with a linked offset ...
443                                         SP_OBJECT(group)->isAncestorOf(SP_OBJECT(source)) == false) { // ... of which the source is not in the same group
444                                 ctrans = citem->transform * g; // then we should apply the transformation of the group to the offset
445                         } else {
446                                 ctrans = citem->transform;
447                         }
448                 } else {
449                         ctrans = citem->transform * g;
450                 }
451             }
453             // FIXME: constructing a transform that would fully preserve the appearance of a
454             // textpath if it is ungrouped with its path seems to be impossible in general
455             // case. E.g. if the group was squeezed, to keep the ungrouped textpath squeezed
456             // as well, we'll need to relink it to some "virtual" path which is inversely
457             // stretched relative to the actual path, and then squeeze the textpath back so it
458             // would both fit the actual path _and_ be squeezed as before. It's a bummer.
460             // This is just a way to temporarily remember the transform in repr. When repr is
461             // reattached outside of the group, the transform will be written more properly
462             // (i.e. optimized into the object if the corresponding preference is set)
463             gchar *affinestr=sp_svg_transform_write(ctrans);
464             nrepr->setAttribute("transform", affinestr);
465             g_free(affinestr);
467             items = g_slist_prepend (items, nrepr);
469         } else {
470             Inkscape::XML::Node *nrepr = SP_OBJECT_REPR (child)->duplicate(prepr->document());
471             objects = g_slist_prepend (objects, nrepr);
472         }
473     }
475     /* Step 2 - clear group */
476     // remember the position of the group
477     gint pos = SP_OBJECT_REPR(group)->position();
479     // the group is leaving forever, no heir, clones should take note; its children however are going to reemerge
480     SP_OBJECT (group)->deleteObject(true, false);
482     /* Step 3 - add nonitems */
483     if (objects) {
484         Inkscape::XML::Node *last_def = SP_OBJECT_REPR(defs)->lastChild();
485         while (objects) {
486                         Inkscape::XML::Node *repr = (Inkscape::XML::Node *) objects->data;
487                         if (!sp_repr_is_meta_element(repr))
488                                 SP_OBJECT_REPR(defs)->addChild(repr, last_def);
489                         Inkscape::GC::release(repr);
490                         objects = g_slist_remove (objects, objects->data);
491         }
492     }
494     /* Step 4 - add items */
495     while (items) {
496         Inkscape::XML::Node *repr = (Inkscape::XML::Node *) items->data;
497         // add item
498         prepr->appendChild(repr);
499         // restore position; since the items list was prepended (i.e. reverse), we now add
500         // all children at the same pos, which inverts the order once again
501         repr->setPosition(pos > 0 ? pos : 0);
503         // fill in the children list if non-null
504         SPItem *item = (SPItem *) doc->getObjectByRepr(repr);
506         sp_item_write_transform(item, repr, item->transform, NULL, false);
508         Inkscape::GC::release(repr);
509         if (children && SP_IS_ITEM (item))
510             *children = g_slist_prepend (*children, item);
512         items = g_slist_remove (items, items->data);
513     }
515     if (do_done)
516         sp_document_done (doc, SP_VERB_NONE, _("Ungroup"));
519 /*
520  * some API for list aspect of SPGroup
521  */
523 GSList *
524 sp_item_group_item_list (SPGroup * group)
526     g_return_val_if_fail (group != NULL, NULL);
527     g_return_val_if_fail (SP_IS_GROUP (group), NULL);
529     GSList *s = NULL;
531     for (SPObject *o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
532         if (SP_IS_ITEM (o)) {
533             s = g_slist_prepend (s, o);
534         }
535     }
537     return g_slist_reverse (s);
540 SPObject *
541 sp_item_group_get_child_by_name (SPGroup *group, SPObject *ref, const gchar *name)
543     SPObject *child;
544     child = (ref) ? SP_OBJECT_NEXT(ref) : sp_object_first_child(SP_OBJECT(group));
545     while ( child && strcmp (SP_OBJECT_REPR(child)->name(), name) ) {
546         child = SP_OBJECT_NEXT(child);
547     }
548     return child;
551 void SPGroup::setLayerMode(LayerMode mode) {
552     if ( _layer_mode != mode ) {
553         if ( mode == LAYER ) {
554             sp_document_add_resource(SP_OBJECT_DOCUMENT(this), "layer", this);
555         } else {
556             sp_document_remove_resource(SP_OBJECT_DOCUMENT(this), "layer", this);
557         }
558         _layer_mode = mode;
559         _updateLayerMode();
560     }
563 SPGroup::LayerMode SPGroup::layerDisplayMode(unsigned int dkey) const {
564     std::map<unsigned int, LayerMode>::const_iterator iter;
565     iter = _display_modes.find(dkey);
566     if ( iter != _display_modes.end() ) {
567         return (*iter).second;
568     } else {
569         return GROUP;
570     }
573 void SPGroup::setLayerDisplayMode(unsigned int dkey, SPGroup::LayerMode mode) {
574     if ( layerDisplayMode(dkey) != mode ) {
575         _display_modes[dkey] = mode;
576         _updateLayerMode(dkey);
577     }
580 void SPGroup::_updateLayerMode(unsigned int display_key) {
581     SPItemView *view;
582     for ( view = this->display ; view ; view = view->next ) {
583         if ( !display_key || view->key == display_key ) {
584             NRArenaGroup *arena_group=NR_ARENA_GROUP(view->arenaitem);
585             if (arena_group) {
586                 nr_arena_group_set_transparent(arena_group, effectiveLayerMode(view->key) == SPGroup::LAYER);
587             }
588         }
589     }
592 void SPGroup::translateChildItems(Geom::Translate const &tr)
594     if (this->hasChildren())
595     {
596         SPObject *o = NULL;
597         for (o = sp_object_first_child(SP_OBJECT(this)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
598             if (SP_IS_ITEM (o)) {
599                 sp_item_move_rel(static_cast<SPItem *>(o), tr);
600             }
601         }
602     }
605 CGroup::CGroup(SPGroup *group) {
606     _group = group;
609 CGroup::~CGroup() {
612 void CGroup::onChildAdded(Inkscape::XML::Node *child) {
613     SPObject *last_child = _group->lastChild();
614     if (last_child && SP_OBJECT_REPR(last_child) == child) {
615         // optimization for the common special case where the child is being added at the end
616         SPObject *ochild = last_child;
617         if ( SP_IS_ITEM(ochild) ) {
618             /* TODO: this should be moved into SPItem somehow */
619             SPItemView *v;
620             NRArenaItem *ac;
622             for (v = _group->display; v != NULL; v = v->next) {
623                 ac = sp_item_invoke_show (SP_ITEM (ochild), NR_ARENA_ITEM_ARENA (v->arenaitem), v->key, v->flags);
625                 if (ac) {
626                     nr_arena_item_append_child (v->arenaitem, ac);
627                 }
628             }
629         }
630     } else {    // general case
631         SPObject *ochild = sp_object_get_child_by_repr(_group, child);
632         if ( ochild && SP_IS_ITEM(ochild) ) {
633             /* TODO: this should be moved into SPItem somehow */
634             SPItemView *v;
635             NRArenaItem *ac;
637             unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
639             for (v = _group->display; v != NULL; v = v->next) {
640                 ac = sp_item_invoke_show (SP_ITEM (ochild), NR_ARENA_ITEM_ARENA (v->arenaitem), v->key, v->flags);
642                 if (ac) {
643                     nr_arena_item_add_child (v->arenaitem, ac, NULL);
644                     nr_arena_item_set_order (ac, position);
645                 }
646             }
647         }
648     }
650     _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
653 void CGroup::onChildRemoved(Inkscape::XML::Node */*child*/) {
654     _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
657 void CGroup::onUpdate(SPCtx *ctx, unsigned int flags) {
658     SPItemCtx *ictx, cctx;
660     ictx = (SPItemCtx *) ctx;
661     cctx = *ictx;
663     if (flags & SP_OBJECT_MODIFIED_FLAG) {
664       flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
665     }
667     flags &= SP_OBJECT_MODIFIED_CASCADE;
669     if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
670       SPObject *object = SP_OBJECT(_group);
671       for (SPItemView *v = SP_ITEM(_group)->display; v != NULL; v = v->next) {
672     nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
673       }
674     }
676     GSList *l = g_slist_reverse(_group->childList(true, SPObject::ActionUpdate));
677     while (l) {
678         SPObject *child = SP_OBJECT (l->data);
679         l = g_slist_remove (l, child);
680         if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
681             if (SP_IS_ITEM (child)) {
682                 SPItem const &chi = *SP_ITEM(child);
683                 cctx.i2doc = chi.transform * ictx->i2doc;
684                 cctx.i2vp = chi.transform * ictx->i2vp;
685                 child->updateDisplay((SPCtx *)&cctx, flags);
686             } else {
687                 child->updateDisplay(ctx, flags);
688             }
689         }
690         g_object_unref (G_OBJECT (child));
691     }
694 void CGroup::onModified(guint flags) {
695     SPObject *child;
697     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
698     flags &= SP_OBJECT_MODIFIED_CASCADE;
700     if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
701       SPObject *object = SP_OBJECT(_group);
702       for (SPItemView *v = SP_ITEM(_group)->display; v != NULL; v = v->next) {
703     nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
704       }
705     }
707     GSList *l = g_slist_reverse(_group->childList(true));
708     while (l) {
709         child = SP_OBJECT (l->data);
710         l = g_slist_remove (l, child);
711         if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
712             child->emitModified(flags);
713         }
714         g_object_unref (G_OBJECT (child));
715     }
718 void CGroup::calculateBBox(NRRect *bbox, Geom::Matrix const &transform, unsigned const flags) {
720     Geom::OptRect dummy_bbox;
722     GSList *l = _group->childList(false, SPObject::ActionBBox);
723     while (l) {
724         SPObject *o = SP_OBJECT (l->data);
725         if (SP_IS_ITEM(o) && !SP_ITEM(o)->isHidden()) {
726             SPItem *child = SP_ITEM(o);
727             Geom::Matrix const ct(to_2geom(child->transform) * transform);
728             sp_item_invoke_bbox_full(child, dummy_bbox, ct, flags, FALSE);
729         }
730         l = g_slist_remove (l, o);
731     }
733     *bbox = NRRect(dummy_bbox);
736 void CGroup::onPrint(SPPrintContext *ctx) {
737     GSList *l = g_slist_reverse(_group->childList(false));
738     while (l) {
739         SPObject *o = SP_OBJECT (l->data);
740         if (SP_IS_ITEM(o)) {
741             sp_item_invoke_print (SP_ITEM (o), ctx);
742         }
743         l = g_slist_remove (l, o);
744     }
747 gint CGroup::getItemCount() {
748     gint len = 0;
749     for (SPObject *o = sp_object_first_child(SP_OBJECT(_group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
750         if (SP_IS_ITEM(o)) {
751             len++;
752         }
753     }
755     return len;
758 gchar *CGroup::getDescription() {
759     gint len = getItemCount();
760     return g_strdup_printf(
761             ngettext("<b>Group</b> of <b>%d</b> object",
762                  "<b>Group</b> of <b>%d</b> objects",
763                  len), len);
766 NRArenaItem *CGroup::show (NRArena *arena, unsigned int key, unsigned int flags) {
767     NRArenaItem *ai;
768     SPObject *object = SP_OBJECT(_group);
770     ai = NRArenaGroup::create(arena);
772     nr_arena_group_set_transparent(NR_ARENA_GROUP (ai),
773                                    _group->effectiveLayerMode(key) ==
774                          SPGroup::LAYER);
775     nr_arena_group_set_style(NR_ARENA_GROUP(ai), SP_OBJECT_STYLE(object));
777     _showChildren(arena, ai, key, flags);
778     return ai;
781 void CGroup::_showChildren (NRArena *arena, NRArenaItem *ai, unsigned int key, unsigned int flags) {
782     NRArenaItem *ac = NULL;
783     NRArenaItem *ar = NULL;
784     SPItem * child = NULL;
785     GSList *l = g_slist_reverse(_group->childList(false, SPObject::ActionShow));
786     while (l) {
787         SPObject *o = SP_OBJECT (l->data);
788         if (SP_IS_ITEM (o)) {
789             child = SP_ITEM (o);
790             ac = sp_item_invoke_show (child, arena, key, flags);
791             if (ac) {
792                 nr_arena_item_add_child (ai, ac, ar);
793                 ar = ac;
794             }
795         }
796         l = g_slist_remove (l, o);
797     }
800 void CGroup::hide (unsigned int key) {
801     SPItem * child;
803     GSList *l = g_slist_reverse(_group->childList(false, SPObject::ActionShow));
804     while (l) {
805         SPObject *o = SP_OBJECT (l->data);
806         if (SP_IS_ITEM (o)) {
807             child = SP_ITEM (o);
808             sp_item_invoke_hide (child, key);
809         }
810         l = g_slist_remove (l, o);
811     }
813     if (((SPItemClass *) parent_class)->hide)
814         ((SPItemClass *) parent_class)->hide (_group, key);
817 void CGroup::onOrderChanged (Inkscape::XML::Node *child, Inkscape::XML::Node *, Inkscape::XML::Node *)
819     SPObject *ochild = sp_object_get_child_by_repr(_group, child);
820     if ( ochild && SP_IS_ITEM(ochild) ) {
821         /* TODO: this should be moved into SPItem somehow */
822         SPItemView *v;
823         unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
824         for ( v = SP_ITEM (ochild)->display ; v != NULL ; v = v->next ) {
825             nr_arena_item_set_order (v->arenaitem, position);
826         }
827     }
829     _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
832 static void
833 sp_group_update_patheffect (SPLPEItem *lpeitem, bool write)
835 #ifdef GROUP_VERBOSE
836     g_message("sp_group_update_patheffect: %p\n", lpeitem);
837 #endif
838     g_return_if_fail (lpeitem != NULL);
839     g_return_if_fail (SP_IS_GROUP (lpeitem));
841     GSList const *item_list = sp_item_group_item_list(SP_GROUP(lpeitem));
842     for ( GSList const *iter = item_list; iter; iter = iter->next ) {
843         SPObject *subitem = static_cast<SPObject *>(iter->data);
844         if (SP_IS_LPE_ITEM(subitem)) {
845             if (SP_LPE_ITEM_CLASS (G_OBJECT_GET_CLASS (subitem))->update_patheffect) {
846                 SP_LPE_ITEM_CLASS (G_OBJECT_GET_CLASS (subitem))->update_patheffect (SP_LPE_ITEM(subitem), write);
847             }
848         }
849     }
851     if (sp_lpe_item_has_path_effect(lpeitem) && sp_lpe_item_path_effects_enabled(lpeitem)) {
852         for (PathEffectList::iterator it = lpeitem->path_effect_list->begin(); it != lpeitem->path_effect_list->end(); it++)
853         {
854             LivePathEffectObject *lpeobj = (*it)->lpeobject;
855             if (lpeobj && lpeobj->get_lpe()) {
856                 lpeobj->get_lpe()->doBeforeEffect(lpeitem);
857             }
858         }
860         sp_group_perform_patheffect(SP_GROUP(lpeitem), SP_GROUP(lpeitem), write);
861     }
864 static void
865 sp_group_perform_patheffect(SPGroup *group, SPGroup *topgroup, bool write)
867     GSList const *item_list = sp_item_group_item_list(SP_GROUP(group));
868     for ( GSList const *iter = item_list; iter; iter = iter->next ) {
869         SPObject *subitem = static_cast<SPObject *>(iter->data);
870         if (SP_IS_GROUP(subitem)) {
871             sp_group_perform_patheffect(SP_GROUP(subitem), topgroup, write);
872         } else if (SP_IS_SHAPE(subitem)) {
873             SPCurve * c = NULL;
874             if (SP_IS_PATH(subitem)) {
875                 c = sp_path_get_original_curve(SP_PATH(subitem));
876             } else {
877                 c = sp_shape_get_curve(SP_SHAPE(subitem));
878             }
879             // only run LPEs when the shape has a curve defined
880             if (c) {
881                 sp_lpe_item_perform_path_effect(SP_LPE_ITEM(topgroup), c);
882                 sp_shape_set_curve(SP_SHAPE(subitem), c, TRUE);
884                 if (write) {
885                     Inkscape::XML::Node *repr = SP_OBJECT_REPR(subitem);
886                     gchar *str = sp_svg_write_path(c->get_pathvector());
887                     repr->setAttribute("d", str);
888 #ifdef GROUP_VERBOSE
889 g_message("sp_group_perform_patheffect writes 'd' attribute");
890 #endif
891                     g_free(str);
892                 }
894                 c->unref();
895             }
896         }
897     }
900 /*
901   Local Variables:
902   mode:c++
903   c-file-style:"stroustrup"
904   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
905   indent-tabs-mode:nil
906   fill-column:99
907   End:
908 */
909 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :