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 void sp_group_update_patheffect(SPLPEItem *lpeitem, bool write);
70 static SPLPEItemClass * parent_class;
72 GType
73 sp_group_get_type (void)
74 {
75 static GType group_type = 0;
76 if (!group_type) {
77 GTypeInfo group_info = {
78 sizeof (SPGroupClass),
79 NULL, /* base_init */
80 NULL, /* base_finalize */
81 (GClassInitFunc) sp_group_class_init,
82 NULL, /* class_finalize */
83 NULL, /* class_data */
84 sizeof (SPGroup),
85 16, /* n_preallocs */
86 (GInstanceInitFunc) sp_group_init,
87 NULL, /* value_table */
88 };
89 group_type = g_type_register_static (SP_TYPE_LPE_ITEM, "SPGroup", &group_info, (GTypeFlags)0);
90 }
91 return group_type;
92 }
94 static void
95 sp_group_class_init (SPGroupClass *klass)
96 {
97 GObjectClass * object_class;
98 SPObjectClass * sp_object_class;
99 SPItemClass * item_class;
100 SPLPEItemClass * lpe_item_class;
102 object_class = (GObjectClass *) klass;
103 sp_object_class = (SPObjectClass *) klass;
104 item_class = (SPItemClass *) klass;
105 lpe_item_class = (SPLPEItemClass *) klass;
107 parent_class = (SPLPEItemClass *)g_type_class_ref (SP_TYPE_LPE_ITEM);
109 object_class->dispose = sp_group_dispose;
111 sp_object_class->child_added = sp_group_child_added;
112 sp_object_class->remove_child = sp_group_remove_child;
113 sp_object_class->order_changed = sp_group_order_changed;
114 sp_object_class->update = sp_group_update;
115 sp_object_class->modified = sp_group_modified;
116 sp_object_class->set = sp_group_set;
117 sp_object_class->write = sp_group_write;
118 sp_object_class->release = sp_group_release;
119 sp_object_class->build = sp_group_build;
121 item_class->bbox = sp_group_bbox;
122 item_class->print = sp_group_print;
123 item_class->description = sp_group_description;
124 item_class->set_transform = sp_group_set_transform;
125 item_class->show = sp_group_show;
126 item_class->hide = sp_group_hide;
127 item_class->snappoints = sp_group_snappoints;
129 lpe_item_class->update_patheffect = sp_group_update_patheffect;
130 }
132 static void
133 sp_group_init (SPGroup *group)
134 {
135 group->_layer_mode = SPGroup::GROUP;
136 group->group = new CGroup(group);
137 new (&group->_display_modes) std::map<unsigned int, SPGroup::LayerMode>();
138 }
140 static void sp_group_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
141 {
142 sp_object_read_attr(object, "inkscape:groupmode");
144 if (((SPObjectClass *)parent_class)->build) {
145 ((SPObjectClass *)parent_class)->build(object, document, repr);
146 }
147 }
149 static void sp_group_release(SPObject *object) {
150 if ( SP_GROUP(object)->_layer_mode == SPGroup::LAYER ) {
151 sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "layer", object);
152 }
153 if (((SPObjectClass *)parent_class)->release) {
154 ((SPObjectClass *)parent_class)->release(object);
155 }
156 }
158 static void
159 sp_group_dispose(GObject *object)
160 {
161 SP_GROUP(object)->_display_modes.~map();
162 delete SP_GROUP(object)->group;
163 }
165 static void
166 sp_group_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
167 {
168 SPItem *item;
170 item = SP_ITEM (object);
172 if (((SPObjectClass *) (parent_class))->child_added)
173 (* ((SPObjectClass *) (parent_class))->child_added) (object, child, ref);
175 SP_GROUP(object)->group->onChildAdded(child);
176 }
178 /* fixme: hide (Lauris) */
180 static void
181 sp_group_remove_child (SPObject * object, Inkscape::XML::Node * child)
182 {
183 if (((SPObjectClass *) (parent_class))->remove_child)
184 (* ((SPObjectClass *) (parent_class))->remove_child) (object, child);
186 SP_GROUP(object)->group->onChildRemoved(child);
187 }
189 static void
190 sp_group_order_changed (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref)
191 {
192 if (((SPObjectClass *) (parent_class))->order_changed)
193 (* ((SPObjectClass *) (parent_class))->order_changed) (object, child, old_ref, new_ref);
195 SP_GROUP(object)->group->onOrderChanged(child, old_ref, new_ref);
196 }
198 static void
199 sp_group_update (SPObject *object, SPCtx *ctx, unsigned int flags)
200 {
201 if (((SPObjectClass *) (parent_class))->update)
202 ((SPObjectClass *) (parent_class))->update (object, ctx, flags);
204 SP_GROUP(object)->group->onUpdate(ctx, flags);
205 }
207 static void
208 sp_group_modified (SPObject *object, guint flags)
209 {
210 SP_GROUP(object)->group->onModified(flags);
211 }
213 static Inkscape::XML::Node *
214 sp_group_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
215 {
216 SPGroup *group;
217 SPObject *child;
218 Inkscape::XML::Node *crepr;
220 group = SP_GROUP (object);
222 if (flags & SP_OBJECT_WRITE_BUILD) {
223 GSList *l;
224 if (!repr) {
225 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
226 repr = xml_doc->createElement("svg:g");
227 }
228 l = NULL;
229 for (child = sp_object_first_child(object); child != NULL; child = SP_OBJECT_NEXT(child) ) {
230 crepr = child->updateRepr(NULL, flags);
231 if (crepr) l = g_slist_prepend (l, crepr);
232 }
233 while (l) {
234 repr->addChild((Inkscape::XML::Node *) l->data, NULL);
235 Inkscape::GC::release((Inkscape::XML::Node *) l->data);
236 l = g_slist_remove (l, l->data);
237 }
238 } else {
239 for (child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
240 child->updateRepr(flags);
241 }
242 }
244 if ( flags & SP_OBJECT_WRITE_EXT ) {
245 const char *value;
246 if ( group->_layer_mode == SPGroup::LAYER ) {
247 value = "layer";
248 } else if ( flags & SP_OBJECT_WRITE_ALL ) {
249 value = "group";
250 } else {
251 value = NULL;
252 }
253 repr->setAttribute("inkscape:groupmode", value);
254 }
256 if (((SPObjectClass *) (parent_class))->write)
257 ((SPObjectClass *) (parent_class))->write (object, repr, flags);
259 return repr;
260 }
262 static void
263 sp_group_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
264 {
265 SP_GROUP(item)->group->calculateBBox(bbox, transform, flags);
266 }
268 static void
269 sp_group_print (SPItem * item, SPPrintContext *ctx)
270 {
271 SP_GROUP(item)->group->onPrint(ctx);
272 }
274 static gchar * sp_group_description (SPItem * item)
275 {
276 return SP_GROUP(item)->group->getDescription();
277 }
279 static NR::Matrix
280 sp_group_set_transform(SPItem *item, NR::Matrix const &xform)
281 {
282 Inkscape::Selection *selection = sp_desktop_selection(inkscape_active_desktop());
283 persp3d_split_perspectives_according_to_selection(selection);
285 NR::Matrix last_trans;
286 sp_svg_transform_read(SP_OBJECT_REPR(item)->attribute("transform"), &last_trans);
287 NR::Matrix inc_trans = last_trans.inverse()*xform;
289 std::list<Persp3D *> plist = selection->perspList();
290 for (std::list<Persp3D *>::iterator i = plist.begin(); i != plist.end(); ++i) {
291 persp3d_apply_affine_transformation(*i, inc_trans);
292 }
294 return xform;
295 }
297 static void sp_group_set(SPObject *object, unsigned key, char const *value) {
298 SPGroup *group = SP_GROUP(object);
300 switch (key) {
301 case SP_ATTR_INKSCAPE_GROUPMODE:
302 if ( value && !strcmp(value, "layer") ) {
303 group->setLayerMode(SPGroup::LAYER);
304 } else {
305 group->setLayerMode(SPGroup::GROUP);
306 }
307 break;
308 default: {
309 if (((SPObjectClass *) (parent_class))->set) {
310 (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
311 }
312 }
313 }
314 }
316 static NRArenaItem *
317 sp_group_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags)
318 {
319 return SP_GROUP(item)->group->show(arena, key, flags);
320 }
322 static void
323 sp_group_hide (SPItem *item, unsigned int key)
324 {
325 SP_GROUP(item)->group->hide(key);
326 }
328 static void sp_group_snappoints (SPItem const *item, SnapPointsIter p)
329 {
330 for (SPObject const *o = sp_object_first_child(SP_OBJECT(item));
331 o != NULL;
332 o = SP_OBJECT_NEXT(o))
333 {
334 if (SP_IS_ITEM(o)) {
335 sp_item_snappoints(SP_ITEM(o), false, p);
336 }
337 }
338 }
341 void
342 sp_item_group_ungroup (SPGroup *group, GSList **children, bool do_done)
343 {
344 g_return_if_fail (group != NULL);
345 g_return_if_fail (SP_IS_GROUP (group));
347 SPDocument *doc = SP_OBJECT_DOCUMENT (group);
348 SPObject *root = SP_DOCUMENT_ROOT (doc);
349 SPObject *defs = SP_OBJECT (SP_ROOT (root)->defs);
351 SPItem *gitem = SP_ITEM (group);
352 Inkscape::XML::Node *grepr = SP_OBJECT_REPR (gitem);
354 g_return_if_fail (!strcmp (grepr->name(), "svg:g") || !strcmp (grepr->name(), "svg:a") || !strcmp (grepr->name(), "svg:switch"));
356 // this converts the gradient/pattern fill/stroke on the group, if any, to userSpaceOnUse
357 sp_item_adjust_paint_recursive (gitem, NR::identity(), NR::identity(), false);
359 SPItem *pitem = SP_ITEM (SP_OBJECT_PARENT (gitem));
360 Inkscape::XML::Node *prepr = SP_OBJECT_REPR (pitem);
362 if (SP_IS_BOX3D(gitem)) {
363 group = box3d_convert_to_group(SP_BOX3D(gitem));
364 gitem = SP_ITEM(group);
365 }
367 sp_lpe_item_remove_path_effect(SP_LPE_ITEM(group), false);
369 /* Step 1 - generate lists of children objects */
370 GSList *items = NULL;
371 GSList *objects = NULL;
372 for (SPObject *child = sp_object_first_child(SP_OBJECT(group)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
374 if (SP_IS_ITEM (child)) {
376 SPItem *citem = SP_ITEM (child);
378 /* Merging of style */
379 // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
380 // it here _before_ the new transform is set, so as to use the pre-transform bbox
381 sp_item_adjust_paint_recursive (citem, NR::identity(), NR::identity(), false);
383 sp_style_merge_from_dying_parent(SP_OBJECT_STYLE(child), SP_OBJECT_STYLE(gitem));
384 /*
385 * fixme: We currently make no allowance for the case where child is cloned
386 * and the group has any style settings.
387 *
388 * (This should never occur with documents created solely with the current
389 * version of inkscape without using the XML editor: we usually apply group
390 * style changes to children rather than to the group itself.)
391 *
392 * If the group has no style settings, then
393 * sp_style_merge_from_dying_parent should be a no-op. Otherwise (i.e. if
394 * we change the child's style to compensate for its parent going away)
395 * then those changes will typically be reflected in any clones of child,
396 * whereas we'd prefer for Ungroup not to affect the visual appearance.
397 *
398 * The only way of preserving styling appearance in general is for child to
399 * be put into a new group -- a somewhat surprising response to an Ungroup
400 * command. We could add a new groupmode:transparent that would mostly
401 * hide the existence of such groups from the user (i.e. editing behaves as
402 * if the transparent group's children weren't in a group), though that's
403 * extra complication & maintenance burden and this case is rare.
404 */
406 child->updateRepr();
408 Inkscape::XML::Node *nrepr = SP_OBJECT_REPR (child)->duplicate(prepr->document());
410 // Merging transform
411 NR::Matrix ctrans;
412 NR::Matrix const g(gitem->transform);
413 if (SP_IS_USE(citem) && sp_use_get_original (SP_USE(citem)) &&
414 SP_OBJECT_PARENT (sp_use_get_original (SP_USE(citem))) == SP_OBJECT(group)) {
415 // make sure a clone's effective transform is the same as was under group
416 ctrans = g.inverse() * citem->transform * g;
417 } else {
418 ctrans = citem->transform * g;
419 }
421 // FIXME: constructing a transform that would fully preserve the appearance of a
422 // textpath if it is ungrouped with its path seems to be impossible in general
423 // case. E.g. if the group was squeezed, to keep the ungrouped textpath squeezed
424 // as well, we'll need to relink it to some "virtual" path which is inversely
425 // stretched relative to the actual path, and then squeeze the textpath back so it
426 // would both fit the actual path _and_ be squeezed as before. It's a bummer.
428 // This is just a way to temporarily remember the transform in repr. When repr is
429 // reattached outside of the group, the transform will be written more properly
430 // (i.e. optimized into the object if the corresponding preference is set)
431 gchar *affinestr=sp_svg_transform_write(ctrans);
432 nrepr->setAttribute("transform", affinestr);
433 g_free(affinestr);
435 items = g_slist_prepend (items, nrepr);
437 } else {
438 Inkscape::XML::Node *nrepr = SP_OBJECT_REPR (child)->duplicate(prepr->document());
439 objects = g_slist_prepend (objects, nrepr);
440 }
441 }
443 /* Step 2 - clear group */
444 // remember the position of the group
445 gint pos = SP_OBJECT_REPR(group)->position();
447 // the group is leaving forever, no heir, clones should take note; its children however are going to reemerge
448 SP_OBJECT (group)->deleteObject(true, false);
450 /* Step 3 - add nonitems */
451 if (objects) {
452 Inkscape::XML::Node *last_def = SP_OBJECT_REPR(defs)->lastChild();
453 while (objects) {
454 SP_OBJECT_REPR(defs)->addChild((Inkscape::XML::Node *) objects->data, last_def);
455 Inkscape::GC::release((Inkscape::XML::Node *) objects->data);
456 objects = g_slist_remove (objects, objects->data);
457 }
458 }
460 /* Step 4 - add items */
461 while (items) {
462 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) items->data;
463 // add item
464 prepr->appendChild(repr);
465 // restore position; since the items list was prepended (i.e. reverse), we now add
466 // all children at the same pos, which inverts the order once again
467 repr->setPosition(pos > 0 ? pos : 0);
469 // fill in the children list if non-null
470 SPItem *item = (SPItem *) doc->getObjectByRepr(repr);
472 sp_item_write_transform(item, repr, item->transform, NULL, false);
474 Inkscape::GC::release(repr);
475 if (children && SP_IS_ITEM (item))
476 *children = g_slist_prepend (*children, item);
478 items = g_slist_remove (items, items->data);
479 }
481 if (do_done)
482 sp_document_done (doc, SP_VERB_NONE, _("Ungroup"));
483 }
485 /*
486 * some API for list aspect of SPGroup
487 */
489 GSList *
490 sp_item_group_item_list (SPGroup * group)
491 {
492 GSList *s;
493 SPObject *o;
495 g_return_val_if_fail (group != NULL, NULL);
496 g_return_val_if_fail (SP_IS_GROUP (group), NULL);
498 s = NULL;
500 for ( o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
501 if (SP_IS_ITEM (o)) {
502 s = g_slist_prepend (s, o);
503 }
504 }
506 return g_slist_reverse (s);
507 }
509 SPObject *
510 sp_item_group_get_child_by_name (SPGroup *group, SPObject *ref, const gchar *name)
511 {
512 SPObject *child;
513 child = (ref) ? SP_OBJECT_NEXT(ref) : sp_object_first_child(SP_OBJECT(group));
514 while ( child && strcmp (SP_OBJECT_REPR(child)->name(), name) ) {
515 child = SP_OBJECT_NEXT(child);
516 }
517 return child;
518 }
520 void SPGroup::setLayerMode(LayerMode mode) {
521 if ( _layer_mode != mode ) {
522 if ( mode == LAYER ) {
523 sp_document_add_resource(SP_OBJECT_DOCUMENT(this), "layer", this);
524 } else {
525 sp_document_remove_resource(SP_OBJECT_DOCUMENT(this), "layer", this);
526 }
527 _layer_mode = mode;
528 _updateLayerMode();
529 }
530 }
532 SPGroup::LayerMode SPGroup::layerDisplayMode(unsigned int dkey) const {
533 std::map<unsigned int, LayerMode>::const_iterator iter;
534 iter = _display_modes.find(dkey);
535 if ( iter != _display_modes.end() ) {
536 return (*iter).second;
537 } else {
538 return GROUP;
539 }
540 }
542 void SPGroup::setLayerDisplayMode(unsigned int dkey, SPGroup::LayerMode mode) {
543 if ( layerDisplayMode(dkey) != mode ) {
544 _display_modes[dkey] = mode;
545 _updateLayerMode(dkey);
546 }
547 }
549 void SPGroup::_updateLayerMode(unsigned int display_key) {
550 SPItemView *view;
551 for ( view = this->display ; view ; view = view->next ) {
552 if ( !display_key || view->key == display_key ) {
553 NRArenaGroup *arena_group=NR_ARENA_GROUP(view->arenaitem);
554 if (arena_group) {
555 nr_arena_group_set_transparent(arena_group, effectiveLayerMode(view->key) == SPGroup::LAYER);
556 }
557 }
558 }
559 }
561 void SPGroup::translateChildItems(NR::translate const &tr)
562 {
563 if (this->hasChildren())
564 {
565 SPObject *o = NULL;
566 for (o = sp_object_first_child(SP_OBJECT(this)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
567 if (SP_IS_ITEM (o)) {
568 sp_item_move_rel(static_cast<SPItem *>(o), tr);
569 }
570 }
571 }
572 }
574 CGroup::CGroup(SPGroup *group) {
575 _group = group;
576 }
578 CGroup::~CGroup() {
579 }
581 void CGroup::onChildAdded(Inkscape::XML::Node *child) {
582 SPObject *last_child = _group->lastChild();
583 if (last_child && SP_OBJECT_REPR(last_child) == child) {
584 // optimization for the common special case where the child is being added at the end
585 SPObject *ochild = last_child;
586 if ( SP_IS_ITEM(ochild) ) {
587 /* TODO: this should be moved into SPItem somehow */
588 SPItemView *v;
589 NRArenaItem *ac;
591 for (v = _group->display; v != NULL; v = v->next) {
592 ac = sp_item_invoke_show (SP_ITEM (ochild), NR_ARENA_ITEM_ARENA (v->arenaitem), v->key, v->flags);
594 if (ac) {
595 nr_arena_item_append_child (v->arenaitem, ac);
596 nr_arena_item_unref (ac);
597 }
598 }
599 }
600 } else { // general case
601 SPObject *ochild = sp_object_get_child_by_repr(_group, child);
602 if ( ochild && SP_IS_ITEM(ochild) ) {
603 /* TODO: this should be moved into SPItem somehow */
604 SPItemView *v;
605 NRArenaItem *ac;
607 unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
609 for (v = _group->display; v != NULL; v = v->next) {
610 ac = sp_item_invoke_show (SP_ITEM (ochild), NR_ARENA_ITEM_ARENA (v->arenaitem), v->key, v->flags);
612 if (ac) {
613 nr_arena_item_add_child (v->arenaitem, ac, NULL);
614 nr_arena_item_set_order (ac, position);
615 nr_arena_item_unref (ac);
616 }
617 }
618 }
619 }
621 _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
622 }
624 void CGroup::onChildRemoved(Inkscape::XML::Node */*child*/) {
625 _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
626 }
628 void CGroup::onUpdate(SPCtx *ctx, unsigned int flags) {
629 SPItemCtx *ictx, cctx;
631 ictx = (SPItemCtx *) ctx;
632 cctx = *ictx;
634 if (flags & SP_OBJECT_MODIFIED_FLAG) {
635 flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
636 }
638 flags &= SP_OBJECT_MODIFIED_CASCADE;
640 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
641 SPObject *object = SP_OBJECT(_group);
642 for (SPItemView *v = SP_ITEM(_group)->display; v != NULL; v = v->next) {
643 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
644 }
645 }
647 GSList *l = g_slist_reverse(_group->childList(true, SPObject::ActionUpdate));
648 while (l) {
649 SPObject *child = SP_OBJECT (l->data);
650 l = g_slist_remove (l, child);
651 if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
652 if (SP_IS_ITEM (child)) {
653 SPItem const &chi = *SP_ITEM(child);
654 cctx.i2doc = chi.transform * ictx->i2doc;
655 cctx.i2vp = chi.transform * ictx->i2vp;
656 child->updateDisplay((SPCtx *)&cctx, flags);
657 } else {
658 child->updateDisplay(ctx, flags);
659 }
660 }
661 g_object_unref (G_OBJECT (child));
662 }
663 }
665 void CGroup::onModified(guint flags) {
666 SPObject *child;
668 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
669 flags &= SP_OBJECT_MODIFIED_CASCADE;
671 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
672 SPObject *object = SP_OBJECT(_group);
673 for (SPItemView *v = SP_ITEM(_group)->display; v != NULL; v = v->next) {
674 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
675 }
676 }
678 GSList *l = g_slist_reverse(_group->childList(true));
679 while (l) {
680 child = SP_OBJECT (l->data);
681 l = g_slist_remove (l, child);
682 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
683 child->emitModified(flags);
684 }
685 g_object_unref (G_OBJECT (child));
686 }
687 }
689 void CGroup::calculateBBox(NRRect *bbox, NR::Matrix const &transform, unsigned const flags) {
691 NR::Maybe<NR::Rect> dummy_bbox = NR::Nothing();
693 GSList *l = _group->childList(false, SPObject::ActionBBox);
694 while (l) {
695 SPObject *o = SP_OBJECT (l->data);
696 if (SP_IS_ITEM(o)) {
697 SPItem *child = SP_ITEM(o);
698 NR::Matrix const ct(child->transform * transform);
699 sp_item_invoke_bbox_full(child, &dummy_bbox, ct, flags, FALSE);
700 }
701 l = g_slist_remove (l, o);
702 }
704 *bbox = NRRect(dummy_bbox);
705 }
707 void CGroup::onPrint(SPPrintContext *ctx) {
708 GSList *l = g_slist_reverse(_group->childList(false));
709 while (l) {
710 SPObject *o = SP_OBJECT (l->data);
711 if (SP_IS_ITEM(o)) {
712 sp_item_invoke_print (SP_ITEM (o), ctx);
713 }
714 l = g_slist_remove (l, o);
715 }
716 }
718 gint CGroup::getItemCount() {
719 gint len = 0;
720 for (SPObject *o = sp_object_first_child(SP_OBJECT(_group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
721 if (SP_IS_ITEM(o)) {
722 len++;
723 }
724 }
726 return len;
727 }
729 gchar *CGroup::getDescription() {
730 gint len = getItemCount();
731 return g_strdup_printf(
732 ngettext("<b>Group</b> of <b>%d</b> object",
733 "<b>Group</b> of <b>%d</b> objects",
734 len), len);
735 }
737 NRArenaItem *CGroup::show (NRArena *arena, unsigned int key, unsigned int flags) {
738 NRArenaItem *ai;
739 SPObject *object = SP_OBJECT(_group);
741 ai = NRArenaGroup::create(arena);
743 nr_arena_group_set_transparent(NR_ARENA_GROUP (ai),
744 _group->effectiveLayerMode(key) ==
745 SPGroup::LAYER);
746 nr_arena_group_set_style(NR_ARENA_GROUP(ai), SP_OBJECT_STYLE(object));
748 _showChildren(arena, ai, key, flags);
749 return ai;
750 }
752 void CGroup::_showChildren (NRArena *arena, NRArenaItem *ai, unsigned int key, unsigned int flags) {
753 NRArenaItem *ac = NULL;
754 NRArenaItem *ar = NULL;
755 SPItem * child = NULL;
756 GSList *l = g_slist_reverse(_group->childList(false, SPObject::ActionShow));
757 while (l) {
758 SPObject *o = SP_OBJECT (l->data);
759 if (SP_IS_ITEM (o)) {
760 child = SP_ITEM (o);
761 ac = sp_item_invoke_show (child, arena, key, flags);
762 if (ac) {
763 nr_arena_item_add_child (ai, ac, ar);
764 ar = ac;
765 nr_arena_item_unref (ac);
766 }
767 }
768 l = g_slist_remove (l, o);
769 }
770 }
772 void CGroup::hide (unsigned int key) {
773 SPItem * child;
775 GSList *l = g_slist_reverse(_group->childList(false, SPObject::ActionShow));
776 while (l) {
777 SPObject *o = SP_OBJECT (l->data);
778 if (SP_IS_ITEM (o)) {
779 child = SP_ITEM (o);
780 sp_item_invoke_hide (child, key);
781 }
782 l = g_slist_remove (l, o);
783 }
785 if (((SPItemClass *) parent_class)->hide)
786 ((SPItemClass *) parent_class)->hide (_group, key);
787 }
789 void CGroup::onOrderChanged (Inkscape::XML::Node *child, Inkscape::XML::Node *, Inkscape::XML::Node *)
790 {
791 SPObject *ochild = sp_object_get_child_by_repr(_group, child);
792 if ( ochild && SP_IS_ITEM(ochild) ) {
793 /* TODO: this should be moved into SPItem somehow */
794 SPItemView *v;
795 unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
796 for ( v = SP_ITEM (ochild)->display ; v != NULL ; v = v->next ) {
797 nr_arena_item_set_order (v->arenaitem, position);
798 }
799 }
801 _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
802 }
804 static void
805 sp_group_update_patheffect (SPLPEItem *lpeitem, bool /*write*/)
806 {
807 #ifdef GROUP_VERBOSE
808 g_message("sp_group_update_patheffect: %p\n", lpeitem);
809 #endif
810 g_return_if_fail (lpeitem != NULL);
811 g_return_if_fail (SP_IS_GROUP (lpeitem));
813 GSList const *item_list = sp_item_group_item_list(SP_GROUP(lpeitem));
814 for ( GSList const *iter = item_list; iter; iter = iter->next ) {
815 SPObject *subitem = static_cast<SPObject *>(iter->data);
816 if (SP_IS_LPE_ITEM(subitem)) {
817 sp_lpe_item_update_patheffect(SP_LPE_ITEM(subitem), true);
818 }
819 }
820 }
823 /*
824 Local Variables:
825 mode:c++
826 c-file-style:"stroustrup"
827 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
828 indent-tabs-mode:nil
829 fill-column:99
830 End:
831 */
832 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :