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