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"
39 static void sp_group_class_init (SPGroupClass *klass);
40 static void sp_group_init (SPGroup *group);
41 static void sp_group_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
42 static void sp_group_release(SPObject *object);
43 static void sp_group_dispose(GObject *object);
45 static void sp_group_child_added (SPObject * object, Inkscape::XML::Node * child, Inkscape::XML::Node * ref);
46 static void sp_group_remove_child (SPObject * object, Inkscape::XML::Node * child);
47 static void sp_group_order_changed (SPObject * object, Inkscape::XML::Node * child, Inkscape::XML::Node * old_ref, Inkscape::XML::Node * new_ref);
48 static void sp_group_update (SPObject *object, SPCtx *ctx, guint flags);
49 static void sp_group_modified (SPObject *object, guint flags);
50 static Inkscape::XML::Node *sp_group_write (SPObject *object, Inkscape::XML::Node *repr, guint flags);
51 static void sp_group_set(SPObject *object, unsigned key, char const *value);
53 static void sp_group_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
54 static void sp_group_print (SPItem * item, SPPrintContext *ctx);
55 static gchar * sp_group_description (SPItem * item);
56 static NRArenaItem *sp_group_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags);
57 static void sp_group_hide (SPItem * item, unsigned int key);
58 static void sp_group_snappoints (SPItem const *item, SnapPointsIter p);
60 static SPItemClass * parent_class;
62 GType
63 sp_group_get_type (void)
64 {
65 static GType group_type = 0;
66 if (!group_type) {
67 GTypeInfo group_info = {
68 sizeof (SPGroupClass),
69 NULL, /* base_init */
70 NULL, /* base_finalize */
71 (GClassInitFunc) sp_group_class_init,
72 NULL, /* class_finalize */
73 NULL, /* class_data */
74 sizeof (SPGroup),
75 16, /* n_preallocs */
76 (GInstanceInitFunc) sp_group_init,
77 NULL, /* value_table */
78 };
79 group_type = g_type_register_static (SP_TYPE_ITEM, "SPGroup", &group_info, (GTypeFlags)0);
80 }
81 return group_type;
82 }
84 static void
85 sp_group_class_init (SPGroupClass *klass)
86 {
87 GObjectClass * object_class;
88 SPObjectClass * sp_object_class;
89 SPItemClass * item_class;
91 object_class = (GObjectClass *) klass;
92 sp_object_class = (SPObjectClass *) klass;
93 item_class = (SPItemClass *) klass;
95 parent_class = (SPItemClass *)g_type_class_ref (SP_TYPE_ITEM);
97 object_class->dispose = sp_group_dispose;
99 sp_object_class->child_added = sp_group_child_added;
100 sp_object_class->remove_child = sp_group_remove_child;
101 sp_object_class->order_changed = sp_group_order_changed;
102 sp_object_class->update = sp_group_update;
103 sp_object_class->modified = sp_group_modified;
104 sp_object_class->set = sp_group_set;
105 sp_object_class->write = sp_group_write;
106 sp_object_class->release = sp_group_release;
107 sp_object_class->build = sp_group_build;
109 item_class->bbox = sp_group_bbox;
110 item_class->print = sp_group_print;
111 item_class->description = sp_group_description;
112 item_class->show = sp_group_show;
113 item_class->hide = sp_group_hide;
114 item_class->snappoints = sp_group_snappoints;
115 }
117 static void
118 sp_group_init (SPGroup *group)
119 {
120 group->_layer_mode = SPGroup::GROUP;
121 group->group = new CGroup(group);
122 new (&group->_display_modes) std::map<unsigned int, SPGroup::LayerMode>();
123 }
125 static void sp_group_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
126 {
127 sp_object_read_attr(object, "inkscape:groupmode");
129 if (((SPObjectClass *)parent_class)->build) {
130 ((SPObjectClass *)parent_class)->build(object, document, repr);
131 }
132 }
134 static void sp_group_release(SPObject *object) {
135 if ( SP_GROUP(object)->_layer_mode == SPGroup::LAYER ) {
136 sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "layer", object);
137 }
138 if (((SPObjectClass *)parent_class)->release) {
139 ((SPObjectClass *)parent_class)->release(object);
140 }
141 }
143 static void
144 sp_group_dispose(GObject *object)
145 {
146 SP_GROUP(object)->_display_modes.~map();
147 delete SP_GROUP(object)->group;
148 }
150 static void
151 sp_group_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
152 {
153 SPItem *item;
155 item = SP_ITEM (object);
157 if (((SPObjectClass *) (parent_class))->child_added)
158 (* ((SPObjectClass *) (parent_class))->child_added) (object, child, ref);
160 SP_GROUP(object)->group->onChildAdded(child);
161 }
163 /* fixme: hide (Lauris) */
165 static void
166 sp_group_remove_child (SPObject * object, Inkscape::XML::Node * child)
167 {
168 if (((SPObjectClass *) (parent_class))->remove_child)
169 (* ((SPObjectClass *) (parent_class))->remove_child) (object, child);
171 SP_GROUP(object)->group->onChildRemoved(child);
172 }
174 static void
175 sp_group_order_changed (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref)
176 {
177 if (((SPObjectClass *) (parent_class))->order_changed)
178 (* ((SPObjectClass *) (parent_class))->order_changed) (object, child, old_ref, new_ref);
180 SP_GROUP(object)->group->onOrderChanged(child, old_ref, new_ref);
181 }
183 static void
184 sp_group_update (SPObject *object, SPCtx *ctx, unsigned int flags)
185 {
186 if (((SPObjectClass *) (parent_class))->update)
187 ((SPObjectClass *) (parent_class))->update (object, ctx, flags);
189 SP_GROUP(object)->group->onUpdate(ctx, flags);
190 }
192 static void
193 sp_group_modified (SPObject *object, guint flags)
194 {
195 SP_GROUP(object)->group->onModified(flags);
196 }
198 static Inkscape::XML::Node *
199 sp_group_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
200 {
201 SPGroup *group;
202 SPObject *child;
203 Inkscape::XML::Node *crepr;
205 group = SP_GROUP (object);
207 if (flags & SP_OBJECT_WRITE_BUILD) {
208 GSList *l;
209 if (!repr) {
210 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
211 repr = xml_doc->createElement("svg:g");
212 }
213 l = NULL;
214 for (child = sp_object_first_child(object); child != NULL; child = SP_OBJECT_NEXT(child) ) {
215 crepr = child->updateRepr(NULL, flags);
216 if (crepr) l = g_slist_prepend (l, crepr);
217 }
218 while (l) {
219 repr->addChild((Inkscape::XML::Node *) l->data, NULL);
220 Inkscape::GC::release((Inkscape::XML::Node *) l->data);
221 l = g_slist_remove (l, l->data);
222 }
223 } else {
224 for (child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
225 child->updateRepr(flags);
226 }
227 }
229 if ( flags & SP_OBJECT_WRITE_EXT ) {
230 const char *value;
231 if ( group->_layer_mode == SPGroup::LAYER ) {
232 value = "layer";
233 } else if ( flags & SP_OBJECT_WRITE_ALL ) {
234 value = "group";
235 } else {
236 value = NULL;
237 }
238 repr->setAttribute("inkscape:groupmode", value);
239 }
241 if (((SPObjectClass *) (parent_class))->write)
242 ((SPObjectClass *) (parent_class))->write (object, repr, flags);
244 return repr;
245 }
247 static void
248 sp_group_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
249 {
250 SP_GROUP(item)->group->calculateBBox(bbox, transform, flags);
251 }
253 static void
254 sp_group_print (SPItem * item, SPPrintContext *ctx)
255 {
256 SP_GROUP(item)->group->onPrint(ctx);
257 }
259 static gchar * sp_group_description (SPItem * item)
260 {
261 return SP_GROUP(item)->group->getDescription();
262 }
264 static void sp_group_set(SPObject *object, unsigned key, char const *value) {
265 SPGroup *group=SP_GROUP(object);
267 switch (key) {
268 case SP_ATTR_INKSCAPE_GROUPMODE: {
269 if ( value && !strcmp(value, "layer") ) {
270 group->setLayerMode(SPGroup::LAYER);
271 } else {
272 group->setLayerMode(SPGroup::GROUP);
273 }
274 } break;
275 default: {
276 if (((SPObjectClass *) (parent_class))->set) {
277 (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
278 }
279 }
280 }
281 }
283 static NRArenaItem *
284 sp_group_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags)
285 {
286 return SP_GROUP(item)->group->show(arena, key, flags);
287 }
289 static void
290 sp_group_hide (SPItem *item, unsigned int key)
291 {
292 SP_GROUP(item)->group->hide(key);
293 }
295 static void sp_group_snappoints (SPItem const *item, SnapPointsIter p)
296 {
297 for (SPObject const *o = sp_object_first_child(SP_OBJECT(item));
298 o != NULL;
299 o = SP_OBJECT_NEXT(o))
300 {
301 if (SP_IS_ITEM(o) && !SP_IS_PATH(o)) {
302 // getSnapPoints() and sp_group_snappoints are only being used in the selector tool,
303 // which should not snap path nodes. Only the node tool should snap those.
304 sp_item_snappoints(SP_ITEM(o), p);
305 }
306 }
307 }
310 void
311 sp_item_group_ungroup (SPGroup *group, GSList **children, bool do_done)
312 {
313 g_return_if_fail (group != NULL);
314 g_return_if_fail (SP_IS_GROUP (group));
316 SPDocument *doc = SP_OBJECT_DOCUMENT (group);
317 SPObject *root = SP_DOCUMENT_ROOT (doc);
318 SPObject *defs = SP_OBJECT (SP_ROOT (root)->defs);
320 SPItem *gitem = SP_ITEM (group);
321 Inkscape::XML::Node *grepr = SP_OBJECT_REPR (gitem);
323 g_return_if_fail (!strcmp (grepr->name(), "svg:g") || !strcmp (grepr->name(), "svg:a") || !strcmp (grepr->name(), "svg:switch"));
325 // this converts the gradient/pattern fill/stroke on the group, if any, to userSpaceOnUse
326 sp_item_adjust_paint_recursive (gitem, NR::identity(), NR::identity(), false);
328 SPItem *pitem = SP_ITEM (SP_OBJECT_PARENT (gitem));
329 Inkscape::XML::Node *prepr = SP_OBJECT_REPR (pitem);
331 /* Step 1 - generate lists of children objects */
332 GSList *items = NULL;
333 GSList *objects = NULL;
334 for (SPObject *child = sp_object_first_child(SP_OBJECT(group)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
336 if (SP_IS_ITEM (child)) {
338 SPItem *citem = SP_ITEM (child);
340 /* Merging of style */
341 // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
342 // it here _before_ the new transform is set, so as to use the pre-transform bbox
343 sp_item_adjust_paint_recursive (citem, NR::identity(), NR::identity(), false);
345 sp_style_merge_from_dying_parent(SP_OBJECT_STYLE(child), SP_OBJECT_STYLE(gitem));
346 /*
347 * fixme: We currently make no allowance for the case where child is cloned
348 * and the group has any style settings.
349 *
350 * (This should never occur with documents created solely with the current
351 * version of inkscape without using the XML editor: we usually apply group
352 * style changes to children rather than to the group itself.)
353 *
354 * If the group has no style settings, then
355 * sp_style_merge_from_dying_parent should be a no-op. Otherwise (i.e. if
356 * we change the child's style to compensate for its parent going away)
357 * then those changes will typically be reflected in any clones of child,
358 * whereas we'd prefer for Ungroup not to affect the visual appearance.
359 *
360 * The only way of preserving styling appearance in general is for child to
361 * be put into a new group -- a somewhat surprising response to an Ungroup
362 * command. We could add a new groupmode:transparent that would mostly
363 * hide the existence of such groups from the user (i.e. editing behaves as
364 * if the transparent group's children weren't in a group), though that's
365 * extra complication & maintenance burden and this case is rare.
366 */
368 child->updateRepr();
370 Inkscape::XML::Node *nrepr = SP_OBJECT_REPR (child)->duplicate(prepr->document());
372 // Merging transform
373 NR::Matrix ctrans;
374 NR::Matrix const g(gitem->transform);
375 if (SP_IS_USE(citem) && sp_use_get_original (SP_USE(citem)) &&
376 SP_OBJECT_PARENT (sp_use_get_original (SP_USE(citem))) == SP_OBJECT(group)) {
377 // make sure a clone's effective transform is the same as was under group
378 ctrans = g.inverse() * citem->transform * g;
379 } else {
380 ctrans = citem->transform * g;
381 }
383 // FIXME: constructing a transform that would fully preserve the appearance of a
384 // textpath if it is ungrouped with its path seems to be impossible in general
385 // case. E.g. if the group was squeezed, to keep the ungrouped textpath squeezed
386 // as well, we'll need to relink it to some "virtual" path which is inversely
387 // stretched relative to the actual path, and then squeeze the textpath back so it
388 // would both fit the actual path _and_ be squeezed as before. It's a bummer.
390 // This is just a way to temporarily remember the transform in repr. When repr is
391 // reattached outside of the group, the transform will be written more properly
392 // (i.e. optimized into the object if the corresponding preference is set)
393 gchar *affinestr=sp_svg_transform_write(ctrans);
394 nrepr->setAttribute("transform", affinestr);
395 g_free(affinestr);
397 items = g_slist_prepend (items, nrepr);
399 } else {
400 Inkscape::XML::Node *nrepr = SP_OBJECT_REPR (child)->duplicate(prepr->document());
401 objects = g_slist_prepend (objects, nrepr);
402 }
403 }
405 /* Step 2 - clear group */
406 // remember the position of the group
407 gint pos = SP_OBJECT_REPR(group)->position();
409 // the group is leaving forever, no heir, clones should take note; its children however are going to reemerge
410 SP_OBJECT (group)->deleteObject(true, false);
412 /* Step 3 - add nonitems */
413 if (objects) {
414 Inkscape::XML::Node *last_def = SP_OBJECT_REPR(defs)->lastChild();
415 while (objects) {
416 SP_OBJECT_REPR(defs)->addChild((Inkscape::XML::Node *) objects->data, last_def);
417 Inkscape::GC::release((Inkscape::XML::Node *) objects->data);
418 objects = g_slist_remove (objects, objects->data);
419 }
420 }
422 /* Step 4 - add items */
423 while (items) {
424 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) items->data;
425 // add item
426 prepr->appendChild(repr);
427 // restore position; since the items list was prepended (i.e. reverse), we now add
428 // all children at the same pos, which inverts the order once again
429 repr->setPosition(pos > 0 ? pos : 0);
431 // fill in the children list if non-null
432 SPItem *item = (SPItem *) doc->getObjectByRepr(repr);
434 sp_item_write_transform(item, repr, item->transform, NULL, false);
436 Inkscape::GC::release(repr);
437 if (children && SP_IS_ITEM (item))
438 *children = g_slist_prepend (*children, item);
440 items = g_slist_remove (items, items->data);
441 }
443 if (do_done)
444 sp_document_done (doc, SP_VERB_NONE, _("Ungroup"));
445 }
447 /*
448 * some API for list aspect of SPGroup
449 */
451 GSList *
452 sp_item_group_item_list (SPGroup * group)
453 {
454 GSList *s;
455 SPObject *o;
457 g_return_val_if_fail (group != NULL, NULL);
458 g_return_val_if_fail (SP_IS_GROUP (group), NULL);
460 s = NULL;
462 for ( o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
463 if (SP_IS_ITEM (o)) {
464 s = g_slist_prepend (s, o);
465 }
466 }
468 return g_slist_reverse (s);
469 }
471 SPObject *
472 sp_item_group_get_child_by_name (SPGroup *group, SPObject *ref, const gchar *name)
473 {
474 SPObject *child;
475 child = (ref) ? SP_OBJECT_NEXT(ref) : sp_object_first_child(SP_OBJECT(group));
476 while ( child && strcmp (SP_OBJECT_REPR(child)->name(), name) ) {
477 child = SP_OBJECT_NEXT(child);
478 }
479 return child;
480 }
482 void SPGroup::setLayerMode(LayerMode mode) {
483 if ( _layer_mode != mode ) {
484 if ( mode == LAYER ) {
485 sp_document_add_resource(SP_OBJECT_DOCUMENT(this), "layer", this);
486 } else {
487 sp_document_remove_resource(SP_OBJECT_DOCUMENT(this), "layer", this);
488 }
489 _layer_mode = mode;
490 _updateLayerMode();
491 }
492 }
494 SPGroup::LayerMode SPGroup::layerDisplayMode(unsigned int dkey) const {
495 std::map<unsigned int, LayerMode>::const_iterator iter;
496 iter = _display_modes.find(dkey);
497 if ( iter != _display_modes.end() ) {
498 return (*iter).second;
499 } else {
500 return GROUP;
501 }
502 }
504 void SPGroup::setLayerDisplayMode(unsigned int dkey, SPGroup::LayerMode mode) {
505 if ( layerDisplayMode(dkey) != mode ) {
506 _display_modes[dkey] = mode;
507 _updateLayerMode(dkey);
508 }
509 }
511 void SPGroup::_updateLayerMode(unsigned int display_key) {
512 SPItemView *view;
513 for ( view = this->display ; view ; view = view->next ) {
514 if ( !display_key || view->key == display_key ) {
515 NRArenaGroup *arena_group=NR_ARENA_GROUP(view->arenaitem);
516 if (arena_group) {
517 nr_arena_group_set_transparent(arena_group, effectiveLayerMode(view->key) == SPGroup::LAYER);
518 }
519 }
520 }
521 }
523 void SPGroup::translateChildItems(NR::translate const &tr)
524 {
525 if (this->hasChildren())
526 {
527 SPObject *o = NULL;
528 for (o = sp_object_first_child(SP_OBJECT(this)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
529 if (SP_IS_ITEM (o)) {
530 sp_item_move_rel(static_cast<SPItem *>(o), tr);
531 }
532 }
533 }
534 }
536 CGroup::CGroup(SPGroup *group) {
537 _group = group;
538 }
540 CGroup::~CGroup() {
541 }
543 void CGroup::onChildAdded(Inkscape::XML::Node *child) {
544 SPObject *last_child = _group->lastChild();
545 if (last_child && SP_OBJECT_REPR(last_child) == child) {
546 // optimization for the common special case where the child is being added at the end
547 SPObject *ochild = last_child;
548 if ( SP_IS_ITEM(ochild) ) {
549 /* TODO: this should be moved into SPItem somehow */
550 SPItemView *v;
551 NRArenaItem *ac;
553 for (v = _group->display; v != NULL; v = v->next) {
554 ac = sp_item_invoke_show (SP_ITEM (ochild), NR_ARENA_ITEM_ARENA (v->arenaitem), v->key, v->flags);
556 if (ac) {
557 nr_arena_item_append_child (v->arenaitem, ac);
558 nr_arena_item_unref (ac);
559 }
560 }
561 }
562 } else { // general case
563 SPObject *ochild = sp_object_get_child_by_repr(_group, child);
564 if ( ochild && SP_IS_ITEM(ochild) ) {
565 /* TODO: this should be moved into SPItem somehow */
566 SPItemView *v;
567 NRArenaItem *ac;
569 unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
571 for (v = _group->display; v != NULL; v = v->next) {
572 ac = sp_item_invoke_show (SP_ITEM (ochild), NR_ARENA_ITEM_ARENA (v->arenaitem), v->key, v->flags);
574 if (ac) {
575 nr_arena_item_add_child (v->arenaitem, ac, NULL);
576 nr_arena_item_set_order (ac, position);
577 nr_arena_item_unref (ac);
578 }
579 }
580 }
581 }
583 _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
584 }
586 void CGroup::onChildRemoved(Inkscape::XML::Node */*child*/) {
587 _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
588 }
590 void CGroup::onUpdate(SPCtx *ctx, unsigned int flags) {
591 SPItemCtx *ictx, cctx;
593 ictx = (SPItemCtx *) ctx;
594 cctx = *ictx;
596 if (flags & SP_OBJECT_MODIFIED_FLAG) {
597 flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
598 }
600 flags &= SP_OBJECT_MODIFIED_CASCADE;
602 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
603 SPObject *object = SP_OBJECT(_group);
604 for (SPItemView *v = SP_ITEM(_group)->display; v != NULL; v = v->next) {
605 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
606 }
607 }
609 GSList *l = g_slist_reverse(_group->childList(true, SPObject::ActionUpdate));
610 while (l) {
611 SPObject *child = SP_OBJECT (l->data);
612 l = g_slist_remove (l, child);
613 if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
614 if (SP_IS_ITEM (child)) {
615 SPItem const &chi = *SP_ITEM(child);
616 cctx.i2doc = chi.transform * ictx->i2doc;
617 cctx.i2vp = chi.transform * ictx->i2vp;
618 child->updateDisplay((SPCtx *)&cctx, flags);
619 } else {
620 child->updateDisplay(ctx, flags);
621 }
622 }
623 g_object_unref (G_OBJECT (child));
624 }
625 }
627 void CGroup::onModified(guint flags) {
628 SPObject *child;
630 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
631 flags &= SP_OBJECT_MODIFIED_CASCADE;
633 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
634 SPObject *object = SP_OBJECT(_group);
635 for (SPItemView *v = SP_ITEM(_group)->display; v != NULL; v = v->next) {
636 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
637 }
638 }
640 GSList *l = g_slist_reverse(_group->childList(true));
641 while (l) {
642 child = SP_OBJECT (l->data);
643 l = g_slist_remove (l, child);
644 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
645 child->emitModified(flags);
646 }
647 g_object_unref (G_OBJECT (child));
648 }
649 }
651 void CGroup::calculateBBox(NRRect *bbox, NR::Matrix const &transform, unsigned const flags) {
652 GSList *l = _group->childList(false, SPObject::ActionBBox);
653 while (l) {
654 SPObject *o = SP_OBJECT (l->data);
655 if (SP_IS_ITEM(o)) {
656 SPItem *child = SP_ITEM(o);
657 NR::Matrix const ct(child->transform * transform);
658 sp_item_invoke_bbox_full(child, bbox, ct, flags, FALSE);
659 }
660 l = g_slist_remove (l, o);
661 }
662 }
664 void CGroup::onPrint(SPPrintContext *ctx) {
665 GSList *l = g_slist_reverse(_group->childList(false));
666 while (l) {
667 SPObject *o = SP_OBJECT (l->data);
668 if (SP_IS_ITEM(o)) {
669 sp_item_invoke_print (SP_ITEM (o), ctx);
670 }
671 l = g_slist_remove (l, o);
672 }
673 }
675 gint CGroup::getItemCount() {
676 gint len = 0;
677 for (SPObject *o = sp_object_first_child(SP_OBJECT(_group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
678 if (SP_IS_ITEM(o)) {
679 len++;
680 }
681 }
683 return len;
684 }
686 gchar *CGroup::getDescription() {
687 gint len = getItemCount();
688 return g_strdup_printf(
689 ngettext("<b>Group</b> of <b>%d</b> object",
690 "<b>Group</b> of <b>%d</b> objects",
691 len), len);
692 }
694 NRArenaItem *CGroup::show (NRArena *arena, unsigned int key, unsigned int flags) {
695 NRArenaItem *ai;
696 SPObject *object = SP_OBJECT(_group);
698 ai = NRArenaGroup::create(arena);
700 nr_arena_group_set_transparent(NR_ARENA_GROUP (ai),
701 _group->effectiveLayerMode(key) ==
702 SPGroup::LAYER);
703 nr_arena_group_set_style(NR_ARENA_GROUP(ai), SP_OBJECT_STYLE(object));
705 _showChildren(arena, ai, key, flags);
706 return ai;
707 }
709 void CGroup::_showChildren (NRArena *arena, NRArenaItem *ai, unsigned int key, unsigned int flags) {
710 NRArenaItem *ac = NULL;
711 NRArenaItem *ar = NULL;
712 SPItem * child = NULL;
713 GSList *l = g_slist_reverse(_group->childList(false, SPObject::ActionShow));
714 while (l) {
715 SPObject *o = SP_OBJECT (l->data);
716 if (SP_IS_ITEM (o)) {
717 child = SP_ITEM (o);
718 ac = sp_item_invoke_show (child, arena, key, flags);
719 if (ac) {
720 nr_arena_item_add_child (ai, ac, ar);
721 ar = ac;
722 nr_arena_item_unref (ac);
723 }
724 }
725 l = g_slist_remove (l, o);
726 }
727 }
729 void CGroup::hide (unsigned int key) {
730 SPItem * child;
732 GSList *l = g_slist_reverse(_group->childList(false, SPObject::ActionShow));
733 while (l) {
734 SPObject *o = SP_OBJECT (l->data);
735 if (SP_IS_ITEM (o)) {
736 child = SP_ITEM (o);
737 sp_item_invoke_hide (child, key);
738 }
739 l = g_slist_remove (l, o);
740 }
742 if (((SPItemClass *) parent_class)->hide)
743 ((SPItemClass *) parent_class)->hide (_group, key);
744 }
746 void CGroup::onOrderChanged (Inkscape::XML::Node *child, Inkscape::XML::Node *, Inkscape::XML::Node *)
747 {
748 SPObject *ochild = sp_object_get_child_by_repr(_group, child);
749 if ( ochild && SP_IS_ITEM(ochild) ) {
750 /* TODO: this should be moved into SPItem somehow */
751 SPItemView *v;
752 unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
753 for ( v = SP_ITEM (ochild)->display ; v != NULL ; v = v->next ) {
754 nr_arena_item_set_order (v->arenaitem, position);
755 }
756 }
758 _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
759 }
761 /*
762 Local Variables:
763 mode:c++
764 c-file-style:"stroustrup"
765 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
766 indent-tabs-mode:nil
767 fill-column:99
768 End:
769 */
770 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :