aafd489669c27b36a64b7fb6ec7985d66a169cde
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>
23 #include "display/nr-arena-group.h"
24 #include "libnr/nr-matrix-ops.h"
25 #include "libnr/nr-matrix-fns.h"
26 #include "xml/repr.h"
27 #include "svg/svg.h"
28 #include "document.h"
29 #include "style.h"
30 #include "attributes.h"
31 #include "sp-item-transform.h"
32 #include "sp-root.h"
33 #include "sp-use.h"
34 #include "prefs-utils.h"
35 #include "sp-clippath.h"
36 #include "sp-mask.h"
37 #include "sp-path.h"
38 #include "box3d.h"
40 static void sp_group_class_init (SPGroupClass *klass);
41 static void sp_group_init (SPGroup *group);
42 static void sp_group_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
43 static void sp_group_release(SPObject *object);
44 static void sp_group_dispose(GObject *object);
46 static void sp_group_child_added (SPObject * object, Inkscape::XML::Node * child, Inkscape::XML::Node * ref);
47 static void sp_group_remove_child (SPObject * object, Inkscape::XML::Node * child);
48 static void sp_group_order_changed (SPObject * object, Inkscape::XML::Node * child, Inkscape::XML::Node * old_ref, Inkscape::XML::Node * new_ref);
49 static void sp_group_update (SPObject *object, SPCtx *ctx, guint flags);
50 static void sp_group_modified (SPObject *object, guint flags);
51 static Inkscape::XML::Node *sp_group_write (SPObject *object, Inkscape::XML::Node *repr, guint flags);
52 static void sp_group_set(SPObject *object, unsigned key, char const *value);
54 static void sp_group_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
55 static void sp_group_print (SPItem * item, SPPrintContext *ctx);
56 static gchar * sp_group_description (SPItem * item);
57 static NRArenaItem *sp_group_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags);
58 static void sp_group_hide (SPItem * item, unsigned int key);
59 static void sp_group_snappoints (SPItem const *item, SnapPointsIter p);
61 static SPItemClass * parent_class;
63 GType
64 sp_group_get_type (void)
65 {
66 static GType group_type = 0;
67 if (!group_type) {
68 GTypeInfo group_info = {
69 sizeof (SPGroupClass),
70 NULL, /* base_init */
71 NULL, /* base_finalize */
72 (GClassInitFunc) sp_group_class_init,
73 NULL, /* class_finalize */
74 NULL, /* class_data */
75 sizeof (SPGroup),
76 16, /* n_preallocs */
77 (GInstanceInitFunc) sp_group_init,
78 NULL, /* value_table */
79 };
80 group_type = g_type_register_static (SP_TYPE_ITEM, "SPGroup", &group_info, (GTypeFlags)0);
81 }
82 return group_type;
83 }
85 static void
86 sp_group_class_init (SPGroupClass *klass)
87 {
88 GObjectClass * object_class;
89 SPObjectClass * sp_object_class;
90 SPItemClass * item_class;
92 object_class = (GObjectClass *) klass;
93 sp_object_class = (SPObjectClass *) klass;
94 item_class = (SPItemClass *) klass;
96 parent_class = (SPItemClass *)g_type_class_ref (SP_TYPE_ITEM);
98 object_class->dispose = sp_group_dispose;
100 sp_object_class->child_added = sp_group_child_added;
101 sp_object_class->remove_child = sp_group_remove_child;
102 sp_object_class->order_changed = sp_group_order_changed;
103 sp_object_class->update = sp_group_update;
104 sp_object_class->modified = sp_group_modified;
105 sp_object_class->set = sp_group_set;
106 sp_object_class->write = sp_group_write;
107 sp_object_class->release = sp_group_release;
108 sp_object_class->build = sp_group_build;
110 item_class->bbox = sp_group_bbox;
111 item_class->print = sp_group_print;
112 item_class->description = sp_group_description;
113 item_class->show = sp_group_show;
114 item_class->hide = sp_group_hide;
115 item_class->snappoints = sp_group_snappoints;
116 }
118 static void
119 sp_group_init (SPGroup *group)
120 {
121 group->_layer_mode = SPGroup::GROUP;
122 group->group = new CGroup(group);
123 new (&group->_display_modes) std::map<unsigned int, SPGroup::LayerMode>();
124 }
126 static void sp_group_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
127 {
128 sp_object_read_attr(object, "inkscape:groupmode");
130 if (((SPObjectClass *)parent_class)->build) {
131 ((SPObjectClass *)parent_class)->build(object, document, repr);
132 }
133 }
135 static void sp_group_release(SPObject *object) {
136 if ( SP_GROUP(object)->_layer_mode == SPGroup::LAYER ) {
137 sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "layer", object);
138 }
139 if (((SPObjectClass *)parent_class)->release) {
140 ((SPObjectClass *)parent_class)->release(object);
141 }
142 }
144 static void
145 sp_group_dispose(GObject *object)
146 {
147 SP_GROUP(object)->_display_modes.~map();
148 delete SP_GROUP(object)->group;
149 }
151 static void
152 sp_group_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
153 {
154 SPItem *item;
156 item = SP_ITEM (object);
158 if (((SPObjectClass *) (parent_class))->child_added)
159 (* ((SPObjectClass *) (parent_class))->child_added) (object, child, ref);
161 SP_GROUP(object)->group->onChildAdded(child);
162 }
164 /* fixme: hide (Lauris) */
166 static void
167 sp_group_remove_child (SPObject * object, Inkscape::XML::Node * child)
168 {
169 if (((SPObjectClass *) (parent_class))->remove_child)
170 (* ((SPObjectClass *) (parent_class))->remove_child) (object, child);
172 SP_GROUP(object)->group->onChildRemoved(child);
173 }
175 static void
176 sp_group_order_changed (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref)
177 {
178 if (((SPObjectClass *) (parent_class))->order_changed)
179 (* ((SPObjectClass *) (parent_class))->order_changed) (object, child, old_ref, new_ref);
181 SP_GROUP(object)->group->onOrderChanged(child, old_ref, new_ref);
182 }
184 static void
185 sp_group_update (SPObject *object, SPCtx *ctx, unsigned int flags)
186 {
187 if (((SPObjectClass *) (parent_class))->update)
188 ((SPObjectClass *) (parent_class))->update (object, ctx, flags);
190 SP_GROUP(object)->group->onUpdate(ctx, flags);
191 }
193 static void
194 sp_group_modified (SPObject *object, guint flags)
195 {
196 SP_GROUP(object)->group->onModified(flags);
197 }
199 static Inkscape::XML::Node *
200 sp_group_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
201 {
202 SPGroup *group;
203 SPObject *child;
204 Inkscape::XML::Node *crepr;
206 group = SP_GROUP (object);
208 if (flags & SP_OBJECT_WRITE_BUILD) {
209 GSList *l;
210 if (!repr) {
211 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
212 repr = xml_doc->createElement("svg:g");
213 }
214 l = NULL;
215 for (child = sp_object_first_child(object); child != NULL; child = SP_OBJECT_NEXT(child) ) {
216 crepr = child->updateRepr(NULL, flags);
217 if (crepr) l = g_slist_prepend (l, crepr);
218 }
219 while (l) {
220 repr->addChild((Inkscape::XML::Node *) l->data, NULL);
221 Inkscape::GC::release((Inkscape::XML::Node *) l->data);
222 l = g_slist_remove (l, l->data);
223 }
224 } else {
225 for (child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
226 child->updateRepr(flags);
227 }
228 }
230 if ( flags & SP_OBJECT_WRITE_EXT ) {
231 const char *value;
232 if ( group->_layer_mode == SPGroup::LAYER ) {
233 value = "layer";
234 } else if ( flags & SP_OBJECT_WRITE_ALL ) {
235 value = "group";
236 } else {
237 value = NULL;
238 }
239 repr->setAttribute("inkscape:groupmode", value);
240 }
242 if (((SPObjectClass *) (parent_class))->write)
243 ((SPObjectClass *) (parent_class))->write (object, repr, flags);
245 return repr;
246 }
248 static void
249 sp_group_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
250 {
251 SP_GROUP(item)->group->calculateBBox(bbox, transform, flags);
252 }
254 static void
255 sp_group_print (SPItem * item, SPPrintContext *ctx)
256 {
257 SP_GROUP(item)->group->onPrint(ctx);
258 }
260 static gchar * sp_group_description (SPItem * item)
261 {
262 return SP_GROUP(item)->group->getDescription();
263 }
265 static void sp_group_set(SPObject *object, unsigned key, char const *value) {
266 SPGroup *group=SP_GROUP(object);
268 switch (key) {
269 case SP_ATTR_INKSCAPE_GROUPMODE: {
270 if ( value && !strcmp(value, "layer") ) {
271 group->setLayerMode(SPGroup::LAYER);
272 } else {
273 group->setLayerMode(SPGroup::GROUP);
274 }
275 } break;
276 default: {
277 if (((SPObjectClass *) (parent_class))->set) {
278 (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
279 }
280 }
281 }
282 }
284 static NRArenaItem *
285 sp_group_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags)
286 {
287 return SP_GROUP(item)->group->show(arena, key, flags);
288 }
290 static void
291 sp_group_hide (SPItem *item, unsigned int key)
292 {
293 SP_GROUP(item)->group->hide(key);
294 }
296 static void sp_group_snappoints (SPItem const *item, SnapPointsIter p)
297 {
298 for (SPObject const *o = sp_object_first_child(SP_OBJECT(item));
299 o != NULL;
300 o = SP_OBJECT_NEXT(o))
301 {
302 if (SP_IS_ITEM(o)) {
303 sp_item_snappoints(SP_ITEM(o), false, p);
304 }
305 }
306 }
309 void
310 sp_item_group_ungroup (SPGroup *group, GSList **children, bool do_done)
311 {
312 g_return_if_fail (group != NULL);
313 g_return_if_fail (SP_IS_GROUP (group));
315 SPDocument *doc = SP_OBJECT_DOCUMENT (group);
316 SPObject *root = SP_DOCUMENT_ROOT (doc);
317 SPObject *defs = SP_OBJECT (SP_ROOT (root)->defs);
319 SPItem *gitem = SP_ITEM (group);
320 Inkscape::XML::Node *grepr = SP_OBJECT_REPR (gitem);
322 g_return_if_fail (!strcmp (grepr->name(), "svg:g") || !strcmp (grepr->name(), "svg:a") || !strcmp (grepr->name(), "svg:switch"));
324 // this converts the gradient/pattern fill/stroke on the group, if any, to userSpaceOnUse
325 sp_item_adjust_paint_recursive (gitem, NR::identity(), NR::identity(), false);
327 SPItem *pitem = SP_ITEM (SP_OBJECT_PARENT (gitem));
328 Inkscape::XML::Node *prepr = SP_OBJECT_REPR (pitem);
330 if (SP_IS_BOX3D(gitem)) {
331 group = box3d_convert_to_group(SP_BOX3D(gitem));
332 gitem = SP_ITEM(group);
333 grepr = SP_OBJECT_REPR(gitem);
334 }
336 /* Step 1 - generate lists of children objects */
337 GSList *items = NULL;
338 GSList *objects = NULL;
339 for (SPObject *child = sp_object_first_child(SP_OBJECT(group)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
341 if (SP_IS_ITEM (child)) {
343 SPItem *citem = SP_ITEM (child);
345 /* Merging of style */
346 // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
347 // it here _before_ the new transform is set, so as to use the pre-transform bbox
348 sp_item_adjust_paint_recursive (citem, NR::identity(), NR::identity(), false);
350 sp_style_merge_from_dying_parent(SP_OBJECT_STYLE(child), SP_OBJECT_STYLE(gitem));
351 /*
352 * fixme: We currently make no allowance for the case where child is cloned
353 * and the group has any style settings.
354 *
355 * (This should never occur with documents created solely with the current
356 * version of inkscape without using the XML editor: we usually apply group
357 * style changes to children rather than to the group itself.)
358 *
359 * If the group has no style settings, then
360 * sp_style_merge_from_dying_parent should be a no-op. Otherwise (i.e. if
361 * we change the child's style to compensate for its parent going away)
362 * then those changes will typically be reflected in any clones of child,
363 * whereas we'd prefer for Ungroup not to affect the visual appearance.
364 *
365 * The only way of preserving styling appearance in general is for child to
366 * be put into a new group -- a somewhat surprising response to an Ungroup
367 * command. We could add a new groupmode:transparent that would mostly
368 * hide the existence of such groups from the user (i.e. editing behaves as
369 * if the transparent group's children weren't in a group), though that's
370 * extra complication & maintenance burden and this case is rare.
371 */
373 child->updateRepr();
375 Inkscape::XML::Node *nrepr = SP_OBJECT_REPR (child)->duplicate(prepr->document());
377 // Merging transform
378 NR::Matrix ctrans;
379 NR::Matrix const g(gitem->transform);
380 if (SP_IS_USE(citem) && sp_use_get_original (SP_USE(citem)) &&
381 SP_OBJECT_PARENT (sp_use_get_original (SP_USE(citem))) == SP_OBJECT(group)) {
382 // make sure a clone's effective transform is the same as was under group
383 ctrans = g.inverse() * citem->transform * g;
384 } else {
385 ctrans = citem->transform * g;
386 }
388 // FIXME: constructing a transform that would fully preserve the appearance of a
389 // textpath if it is ungrouped with its path seems to be impossible in general
390 // case. E.g. if the group was squeezed, to keep the ungrouped textpath squeezed
391 // as well, we'll need to relink it to some "virtual" path which is inversely
392 // stretched relative to the actual path, and then squeeze the textpath back so it
393 // would both fit the actual path _and_ be squeezed as before. It's a bummer.
395 // This is just a way to temporarily remember the transform in repr. When repr is
396 // reattached outside of the group, the transform will be written more properly
397 // (i.e. optimized into the object if the corresponding preference is set)
398 gchar *affinestr=sp_svg_transform_write(ctrans);
399 nrepr->setAttribute("transform", affinestr);
400 g_free(affinestr);
402 items = g_slist_prepend (items, nrepr);
404 } else {
405 Inkscape::XML::Node *nrepr = SP_OBJECT_REPR (child)->duplicate(prepr->document());
406 objects = g_slist_prepend (objects, nrepr);
407 }
408 }
410 /* Step 2 - clear group */
411 // remember the position of the group
412 gint pos = SP_OBJECT_REPR(group)->position();
414 // the group is leaving forever, no heir, clones should take note; its children however are going to reemerge
415 SP_OBJECT (group)->deleteObject(true, false);
417 /* Step 3 - add nonitems */
418 if (objects) {
419 Inkscape::XML::Node *last_def = SP_OBJECT_REPR(defs)->lastChild();
420 while (objects) {
421 SP_OBJECT_REPR(defs)->addChild((Inkscape::XML::Node *) objects->data, last_def);
422 Inkscape::GC::release((Inkscape::XML::Node *) objects->data);
423 objects = g_slist_remove (objects, objects->data);
424 }
425 }
427 /* Step 4 - add items */
428 while (items) {
429 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) items->data;
430 // add item
431 prepr->appendChild(repr);
432 // restore position; since the items list was prepended (i.e. reverse), we now add
433 // all children at the same pos, which inverts the order once again
434 repr->setPosition(pos > 0 ? pos : 0);
436 // fill in the children list if non-null
437 SPItem *item = (SPItem *) doc->getObjectByRepr(repr);
439 sp_item_write_transform(item, repr, item->transform, NULL, false);
441 Inkscape::GC::release(repr);
442 if (children && SP_IS_ITEM (item))
443 *children = g_slist_prepend (*children, item);
445 items = g_slist_remove (items, items->data);
446 }
448 if (do_done)
449 sp_document_done (doc, SP_VERB_NONE, _("Ungroup"));
450 }
452 /*
453 * some API for list aspect of SPGroup
454 */
456 GSList *
457 sp_item_group_item_list (SPGroup * group)
458 {
459 GSList *s;
460 SPObject *o;
462 g_return_val_if_fail (group != NULL, NULL);
463 g_return_val_if_fail (SP_IS_GROUP (group), NULL);
465 s = NULL;
467 for ( o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
468 if (SP_IS_ITEM (o)) {
469 s = g_slist_prepend (s, o);
470 }
471 }
473 return g_slist_reverse (s);
474 }
476 SPObject *
477 sp_item_group_get_child_by_name (SPGroup *group, SPObject *ref, const gchar *name)
478 {
479 SPObject *child;
480 child = (ref) ? SP_OBJECT_NEXT(ref) : sp_object_first_child(SP_OBJECT(group));
481 while ( child && strcmp (SP_OBJECT_REPR(child)->name(), name) ) {
482 child = SP_OBJECT_NEXT(child);
483 }
484 return child;
485 }
487 void SPGroup::setLayerMode(LayerMode mode) {
488 if ( _layer_mode != mode ) {
489 if ( mode == LAYER ) {
490 sp_document_add_resource(SP_OBJECT_DOCUMENT(this), "layer", this);
491 } else {
492 sp_document_remove_resource(SP_OBJECT_DOCUMENT(this), "layer", this);
493 }
494 _layer_mode = mode;
495 _updateLayerMode();
496 }
497 }
499 SPGroup::LayerMode SPGroup::layerDisplayMode(unsigned int dkey) const {
500 std::map<unsigned int, LayerMode>::const_iterator iter;
501 iter = _display_modes.find(dkey);
502 if ( iter != _display_modes.end() ) {
503 return (*iter).second;
504 } else {
505 return GROUP;
506 }
507 }
509 void SPGroup::setLayerDisplayMode(unsigned int dkey, SPGroup::LayerMode mode) {
510 if ( layerDisplayMode(dkey) != mode ) {
511 _display_modes[dkey] = mode;
512 _updateLayerMode(dkey);
513 }
514 }
516 void SPGroup::_updateLayerMode(unsigned int display_key) {
517 SPItemView *view;
518 for ( view = this->display ; view ; view = view->next ) {
519 if ( !display_key || view->key == display_key ) {
520 NRArenaGroup *arena_group=NR_ARENA_GROUP(view->arenaitem);
521 if (arena_group) {
522 nr_arena_group_set_transparent(arena_group, effectiveLayerMode(view->key) == SPGroup::LAYER);
523 }
524 }
525 }
526 }
528 void SPGroup::translateChildItems(NR::translate const &tr)
529 {
530 if (this->hasChildren())
531 {
532 SPObject *o = NULL;
533 for (o = sp_object_first_child(SP_OBJECT(this)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
534 if (SP_IS_ITEM (o)) {
535 sp_item_move_rel(static_cast<SPItem *>(o), tr);
536 }
537 }
538 }
539 }
541 CGroup::CGroup(SPGroup *group) {
542 _group = group;
543 }
545 CGroup::~CGroup() {
546 }
548 void CGroup::onChildAdded(Inkscape::XML::Node *child) {
549 SPObject *last_child = _group->lastChild();
550 if (last_child && SP_OBJECT_REPR(last_child) == child) {
551 // optimization for the common special case where the child is being added at the end
552 SPObject *ochild = last_child;
553 if ( SP_IS_ITEM(ochild) ) {
554 /* TODO: this should be moved into SPItem somehow */
555 SPItemView *v;
556 NRArenaItem *ac;
558 for (v = _group->display; v != NULL; v = v->next) {
559 ac = sp_item_invoke_show (SP_ITEM (ochild), NR_ARENA_ITEM_ARENA (v->arenaitem), v->key, v->flags);
561 if (ac) {
562 nr_arena_item_append_child (v->arenaitem, ac);
563 nr_arena_item_unref (ac);
564 }
565 }
566 }
567 } else { // general case
568 SPObject *ochild = sp_object_get_child_by_repr(_group, child);
569 if ( ochild && SP_IS_ITEM(ochild) ) {
570 /* TODO: this should be moved into SPItem somehow */
571 SPItemView *v;
572 NRArenaItem *ac;
574 unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
576 for (v = _group->display; v != NULL; v = v->next) {
577 ac = sp_item_invoke_show (SP_ITEM (ochild), NR_ARENA_ITEM_ARENA (v->arenaitem), v->key, v->flags);
579 if (ac) {
580 nr_arena_item_add_child (v->arenaitem, ac, NULL);
581 nr_arena_item_set_order (ac, position);
582 nr_arena_item_unref (ac);
583 }
584 }
585 }
586 }
588 _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
589 }
591 void CGroup::onChildRemoved(Inkscape::XML::Node */*child*/) {
592 _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
593 }
595 void CGroup::onUpdate(SPCtx *ctx, unsigned int flags) {
596 SPItemCtx *ictx, cctx;
598 ictx = (SPItemCtx *) ctx;
599 cctx = *ictx;
601 if (flags & SP_OBJECT_MODIFIED_FLAG) {
602 flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
603 }
605 flags &= SP_OBJECT_MODIFIED_CASCADE;
607 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
608 SPObject *object = SP_OBJECT(_group);
609 for (SPItemView *v = SP_ITEM(_group)->display; v != NULL; v = v->next) {
610 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
611 }
612 }
614 GSList *l = g_slist_reverse(_group->childList(true, SPObject::ActionUpdate));
615 while (l) {
616 SPObject *child = SP_OBJECT (l->data);
617 l = g_slist_remove (l, child);
618 if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
619 if (SP_IS_ITEM (child)) {
620 SPItem const &chi = *SP_ITEM(child);
621 cctx.i2doc = chi.transform * ictx->i2doc;
622 cctx.i2vp = chi.transform * ictx->i2vp;
623 child->updateDisplay((SPCtx *)&cctx, flags);
624 } else {
625 child->updateDisplay(ctx, flags);
626 }
627 }
628 g_object_unref (G_OBJECT (child));
629 }
630 }
632 void CGroup::onModified(guint flags) {
633 SPObject *child;
635 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
636 flags &= SP_OBJECT_MODIFIED_CASCADE;
638 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
639 SPObject *object = SP_OBJECT(_group);
640 for (SPItemView *v = SP_ITEM(_group)->display; v != NULL; v = v->next) {
641 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
642 }
643 }
645 GSList *l = g_slist_reverse(_group->childList(true));
646 while (l) {
647 child = SP_OBJECT (l->data);
648 l = g_slist_remove (l, child);
649 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
650 child->emitModified(flags);
651 }
652 g_object_unref (G_OBJECT (child));
653 }
654 }
656 void CGroup::calculateBBox(NRRect *bbox, NR::Matrix const &transform, unsigned const flags) {
657 GSList *l = _group->childList(false, SPObject::ActionBBox);
658 while (l) {
659 SPObject *o = SP_OBJECT (l->data);
660 if (SP_IS_ITEM(o)) {
661 SPItem *child = SP_ITEM(o);
662 NR::Matrix const ct(child->transform * transform);
663 sp_item_invoke_bbox_full(child, bbox, ct, flags, FALSE);
664 }
665 l = g_slist_remove (l, o);
666 }
667 }
669 void CGroup::onPrint(SPPrintContext *ctx) {
670 GSList *l = g_slist_reverse(_group->childList(false));
671 while (l) {
672 SPObject *o = SP_OBJECT (l->data);
673 if (SP_IS_ITEM(o)) {
674 sp_item_invoke_print (SP_ITEM (o), ctx);
675 }
676 l = g_slist_remove (l, o);
677 }
678 }
680 gint CGroup::getItemCount() {
681 gint len = 0;
682 for (SPObject *o = sp_object_first_child(SP_OBJECT(_group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
683 if (SP_IS_ITEM(o)) {
684 len++;
685 }
686 }
688 return len;
689 }
691 gchar *CGroup::getDescription() {
692 gint len = getItemCount();
693 return g_strdup_printf(
694 ngettext("<b>Group</b> of <b>%d</b> object",
695 "<b>Group</b> of <b>%d</b> objects",
696 len), len);
697 }
699 NRArenaItem *CGroup::show (NRArena *arena, unsigned int key, unsigned int flags) {
700 NRArenaItem *ai;
701 SPObject *object = SP_OBJECT(_group);
703 ai = NRArenaGroup::create(arena);
705 nr_arena_group_set_transparent(NR_ARENA_GROUP (ai),
706 _group->effectiveLayerMode(key) ==
707 SPGroup::LAYER);
708 nr_arena_group_set_style(NR_ARENA_GROUP(ai), SP_OBJECT_STYLE(object));
710 _showChildren(arena, ai, key, flags);
711 return ai;
712 }
714 void CGroup::_showChildren (NRArena *arena, NRArenaItem *ai, unsigned int key, unsigned int flags) {
715 NRArenaItem *ac = NULL;
716 NRArenaItem *ar = NULL;
717 SPItem * child = NULL;
718 GSList *l = g_slist_reverse(_group->childList(false, SPObject::ActionShow));
719 while (l) {
720 SPObject *o = SP_OBJECT (l->data);
721 if (SP_IS_ITEM (o)) {
722 child = SP_ITEM (o);
723 ac = sp_item_invoke_show (child, arena, key, flags);
724 if (ac) {
725 nr_arena_item_add_child (ai, ac, ar);
726 ar = ac;
727 nr_arena_item_unref (ac);
728 }
729 }
730 l = g_slist_remove (l, o);
731 }
732 }
734 void CGroup::hide (unsigned int key) {
735 SPItem * child;
737 GSList *l = g_slist_reverse(_group->childList(false, SPObject::ActionShow));
738 while (l) {
739 SPObject *o = SP_OBJECT (l->data);
740 if (SP_IS_ITEM (o)) {
741 child = SP_ITEM (o);
742 sp_item_invoke_hide (child, key);
743 }
744 l = g_slist_remove (l, o);
745 }
747 if (((SPItemClass *) parent_class)->hide)
748 ((SPItemClass *) parent_class)->hide (_group, key);
749 }
751 void CGroup::onOrderChanged (Inkscape::XML::Node *child, Inkscape::XML::Node *, Inkscape::XML::Node *)
752 {
753 SPObject *ochild = sp_object_get_child_by_repr(_group, child);
754 if ( ochild && SP_IS_ITEM(ochild) ) {
755 /* TODO: this should be moved into SPItem somehow */
756 SPItemView *v;
757 unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
758 for ( v = SP_ITEM (ochild)->display ; v != NULL ; v = v->next ) {
759 nr_arena_item_set_order (v->arenaitem, position);
760 }
761 }
763 _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
764 }
766 /*
767 Local Variables:
768 mode:c++
769 c-file-style:"stroustrup"
770 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
771 indent-tabs-mode:nil
772 fill-column:99
773 End:
774 */
775 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :