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 grepr = box3d_convert_to_group(SP_BOX3D(gitem));
334 if (grepr) {
335 gitem = SP_ITEM(doc->getObjectByRepr(grepr));
336 group = SP_GROUP(gitem);
337 }
338 }
340 /* Step 1 - generate lists of children objects */
341 GSList *items = NULL;
342 GSList *objects = NULL;
343 for (SPObject *child = sp_object_first_child(SP_OBJECT(group)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
345 if (SP_IS_ITEM (child)) {
347 SPItem *citem = SP_ITEM (child);
349 /* Merging of style */
350 // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
351 // it here _before_ the new transform is set, so as to use the pre-transform bbox
352 sp_item_adjust_paint_recursive (citem, NR::identity(), NR::identity(), false);
354 sp_style_merge_from_dying_parent(SP_OBJECT_STYLE(child), SP_OBJECT_STYLE(gitem));
355 /*
356 * fixme: We currently make no allowance for the case where child is cloned
357 * and the group has any style settings.
358 *
359 * (This should never occur with documents created solely with the current
360 * version of inkscape without using the XML editor: we usually apply group
361 * style changes to children rather than to the group itself.)
362 *
363 * If the group has no style settings, then
364 * sp_style_merge_from_dying_parent should be a no-op. Otherwise (i.e. if
365 * we change the child's style to compensate for its parent going away)
366 * then those changes will typically be reflected in any clones of child,
367 * whereas we'd prefer for Ungroup not to affect the visual appearance.
368 *
369 * The only way of preserving styling appearance in general is for child to
370 * be put into a new group -- a somewhat surprising response to an Ungroup
371 * command. We could add a new groupmode:transparent that would mostly
372 * hide the existence of such groups from the user (i.e. editing behaves as
373 * if the transparent group's children weren't in a group), though that's
374 * extra complication & maintenance burden and this case is rare.
375 */
377 child->updateRepr();
379 Inkscape::XML::Node *nrepr = SP_OBJECT_REPR (child)->duplicate(prepr->document());
381 // Merging transform
382 NR::Matrix ctrans;
383 NR::Matrix const g(gitem->transform);
384 if (SP_IS_USE(citem) && sp_use_get_original (SP_USE(citem)) &&
385 SP_OBJECT_PARENT (sp_use_get_original (SP_USE(citem))) == SP_OBJECT(group)) {
386 // make sure a clone's effective transform is the same as was under group
387 ctrans = g.inverse() * citem->transform * g;
388 } else {
389 ctrans = citem->transform * g;
390 }
392 // FIXME: constructing a transform that would fully preserve the appearance of a
393 // textpath if it is ungrouped with its path seems to be impossible in general
394 // case. E.g. if the group was squeezed, to keep the ungrouped textpath squeezed
395 // as well, we'll need to relink it to some "virtual" path which is inversely
396 // stretched relative to the actual path, and then squeeze the textpath back so it
397 // would both fit the actual path _and_ be squeezed as before. It's a bummer.
399 // This is just a way to temporarily remember the transform in repr. When repr is
400 // reattached outside of the group, the transform will be written more properly
401 // (i.e. optimized into the object if the corresponding preference is set)
402 gchar *affinestr=sp_svg_transform_write(ctrans);
403 nrepr->setAttribute("transform", affinestr);
404 g_free(affinestr);
406 items = g_slist_prepend (items, nrepr);
408 } else {
409 Inkscape::XML::Node *nrepr = SP_OBJECT_REPR (child)->duplicate(prepr->document());
410 objects = g_slist_prepend (objects, nrepr);
411 }
412 }
414 /* Step 2 - clear group */
415 // remember the position of the group
416 gint pos = SP_OBJECT_REPR(group)->position();
418 // the group is leaving forever, no heir, clones should take note; its children however are going to reemerge
419 SP_OBJECT (group)->deleteObject(true, false);
421 /* Step 3 - add nonitems */
422 if (objects) {
423 Inkscape::XML::Node *last_def = SP_OBJECT_REPR(defs)->lastChild();
424 while (objects) {
425 SP_OBJECT_REPR(defs)->addChild((Inkscape::XML::Node *) objects->data, last_def);
426 Inkscape::GC::release((Inkscape::XML::Node *) objects->data);
427 objects = g_slist_remove (objects, objects->data);
428 }
429 }
431 /* Step 4 - add items */
432 while (items) {
433 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) items->data;
434 // add item
435 prepr->appendChild(repr);
436 // restore position; since the items list was prepended (i.e. reverse), we now add
437 // all children at the same pos, which inverts the order once again
438 repr->setPosition(pos > 0 ? pos : 0);
440 // fill in the children list if non-null
441 SPItem *item = (SPItem *) doc->getObjectByRepr(repr);
443 sp_item_write_transform(item, repr, item->transform, NULL, false);
445 Inkscape::GC::release(repr);
446 if (children && SP_IS_ITEM (item))
447 *children = g_slist_prepend (*children, item);
449 items = g_slist_remove (items, items->data);
450 }
452 if (do_done)
453 sp_document_done (doc, SP_VERB_NONE, _("Ungroup"));
454 }
456 /*
457 * some API for list aspect of SPGroup
458 */
460 GSList *
461 sp_item_group_item_list (SPGroup * group)
462 {
463 GSList *s;
464 SPObject *o;
466 g_return_val_if_fail (group != NULL, NULL);
467 g_return_val_if_fail (SP_IS_GROUP (group), NULL);
469 s = NULL;
471 for ( o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
472 if (SP_IS_ITEM (o)) {
473 s = g_slist_prepend (s, o);
474 }
475 }
477 return g_slist_reverse (s);
478 }
480 SPObject *
481 sp_item_group_get_child_by_name (SPGroup *group, SPObject *ref, const gchar *name)
482 {
483 SPObject *child;
484 child = (ref) ? SP_OBJECT_NEXT(ref) : sp_object_first_child(SP_OBJECT(group));
485 while ( child && strcmp (SP_OBJECT_REPR(child)->name(), name) ) {
486 child = SP_OBJECT_NEXT(child);
487 }
488 return child;
489 }
491 void SPGroup::setLayerMode(LayerMode mode) {
492 if ( _layer_mode != mode ) {
493 if ( mode == LAYER ) {
494 sp_document_add_resource(SP_OBJECT_DOCUMENT(this), "layer", this);
495 } else {
496 sp_document_remove_resource(SP_OBJECT_DOCUMENT(this), "layer", this);
497 }
498 _layer_mode = mode;
499 _updateLayerMode();
500 }
501 }
503 SPGroup::LayerMode SPGroup::layerDisplayMode(unsigned int dkey) const {
504 std::map<unsigned int, LayerMode>::const_iterator iter;
505 iter = _display_modes.find(dkey);
506 if ( iter != _display_modes.end() ) {
507 return (*iter).second;
508 } else {
509 return GROUP;
510 }
511 }
513 void SPGroup::setLayerDisplayMode(unsigned int dkey, SPGroup::LayerMode mode) {
514 if ( layerDisplayMode(dkey) != mode ) {
515 _display_modes[dkey] = mode;
516 _updateLayerMode(dkey);
517 }
518 }
520 void SPGroup::_updateLayerMode(unsigned int display_key) {
521 SPItemView *view;
522 for ( view = this->display ; view ; view = view->next ) {
523 if ( !display_key || view->key == display_key ) {
524 NRArenaGroup *arena_group=NR_ARENA_GROUP(view->arenaitem);
525 if (arena_group) {
526 nr_arena_group_set_transparent(arena_group, effectiveLayerMode(view->key) == SPGroup::LAYER);
527 }
528 }
529 }
530 }
532 void SPGroup::translateChildItems(NR::translate const &tr)
533 {
534 if (this->hasChildren())
535 {
536 SPObject *o = NULL;
537 for (o = sp_object_first_child(SP_OBJECT(this)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
538 if (SP_IS_ITEM (o)) {
539 sp_item_move_rel(static_cast<SPItem *>(o), tr);
540 }
541 }
542 }
543 }
545 CGroup::CGroup(SPGroup *group) {
546 _group = group;
547 }
549 CGroup::~CGroup() {
550 }
552 void CGroup::onChildAdded(Inkscape::XML::Node *child) {
553 SPObject *last_child = _group->lastChild();
554 if (last_child && SP_OBJECT_REPR(last_child) == child) {
555 // optimization for the common special case where the child is being added at the end
556 SPObject *ochild = last_child;
557 if ( SP_IS_ITEM(ochild) ) {
558 /* TODO: this should be moved into SPItem somehow */
559 SPItemView *v;
560 NRArenaItem *ac;
562 for (v = _group->display; v != NULL; v = v->next) {
563 ac = sp_item_invoke_show (SP_ITEM (ochild), NR_ARENA_ITEM_ARENA (v->arenaitem), v->key, v->flags);
565 if (ac) {
566 nr_arena_item_append_child (v->arenaitem, ac);
567 nr_arena_item_unref (ac);
568 }
569 }
570 }
571 } else { // general case
572 SPObject *ochild = sp_object_get_child_by_repr(_group, child);
573 if ( ochild && SP_IS_ITEM(ochild) ) {
574 /* TODO: this should be moved into SPItem somehow */
575 SPItemView *v;
576 NRArenaItem *ac;
578 unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
580 for (v = _group->display; v != NULL; v = v->next) {
581 ac = sp_item_invoke_show (SP_ITEM (ochild), NR_ARENA_ITEM_ARENA (v->arenaitem), v->key, v->flags);
583 if (ac) {
584 nr_arena_item_add_child (v->arenaitem, ac, NULL);
585 nr_arena_item_set_order (ac, position);
586 nr_arena_item_unref (ac);
587 }
588 }
589 }
590 }
592 _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
593 }
595 void CGroup::onChildRemoved(Inkscape::XML::Node */*child*/) {
596 _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
597 }
599 void CGroup::onUpdate(SPCtx *ctx, unsigned int flags) {
600 SPItemCtx *ictx, cctx;
602 ictx = (SPItemCtx *) ctx;
603 cctx = *ictx;
605 if (flags & SP_OBJECT_MODIFIED_FLAG) {
606 flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
607 }
609 flags &= SP_OBJECT_MODIFIED_CASCADE;
611 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
612 SPObject *object = SP_OBJECT(_group);
613 for (SPItemView *v = SP_ITEM(_group)->display; v != NULL; v = v->next) {
614 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
615 }
616 }
618 GSList *l = g_slist_reverse(_group->childList(true, SPObject::ActionUpdate));
619 while (l) {
620 SPObject *child = SP_OBJECT (l->data);
621 l = g_slist_remove (l, child);
622 if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
623 if (SP_IS_ITEM (child)) {
624 SPItem const &chi = *SP_ITEM(child);
625 cctx.i2doc = chi.transform * ictx->i2doc;
626 cctx.i2vp = chi.transform * ictx->i2vp;
627 child->updateDisplay((SPCtx *)&cctx, flags);
628 } else {
629 child->updateDisplay(ctx, flags);
630 }
631 }
632 g_object_unref (G_OBJECT (child));
633 }
634 }
636 void CGroup::onModified(guint flags) {
637 SPObject *child;
639 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
640 flags &= SP_OBJECT_MODIFIED_CASCADE;
642 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
643 SPObject *object = SP_OBJECT(_group);
644 for (SPItemView *v = SP_ITEM(_group)->display; v != NULL; v = v->next) {
645 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
646 }
647 }
649 GSList *l = g_slist_reverse(_group->childList(true));
650 while (l) {
651 child = SP_OBJECT (l->data);
652 l = g_slist_remove (l, child);
653 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
654 child->emitModified(flags);
655 }
656 g_object_unref (G_OBJECT (child));
657 }
658 }
660 void CGroup::calculateBBox(NRRect *bbox, NR::Matrix const &transform, unsigned const flags) {
661 GSList *l = _group->childList(false, SPObject::ActionBBox);
662 while (l) {
663 SPObject *o = SP_OBJECT (l->data);
664 if (SP_IS_ITEM(o)) {
665 SPItem *child = SP_ITEM(o);
666 NR::Matrix const ct(child->transform * transform);
667 sp_item_invoke_bbox_full(child, bbox, ct, flags, FALSE);
668 }
669 l = g_slist_remove (l, o);
670 }
671 }
673 void CGroup::onPrint(SPPrintContext *ctx) {
674 GSList *l = g_slist_reverse(_group->childList(false));
675 while (l) {
676 SPObject *o = SP_OBJECT (l->data);
677 if (SP_IS_ITEM(o)) {
678 sp_item_invoke_print (SP_ITEM (o), ctx);
679 }
680 l = g_slist_remove (l, o);
681 }
682 }
684 gint CGroup::getItemCount() {
685 gint len = 0;
686 for (SPObject *o = sp_object_first_child(SP_OBJECT(_group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
687 if (SP_IS_ITEM(o)) {
688 len++;
689 }
690 }
692 return len;
693 }
695 gchar *CGroup::getDescription() {
696 gint len = getItemCount();
697 return g_strdup_printf(
698 ngettext("<b>Group</b> of <b>%d</b> object",
699 "<b>Group</b> of <b>%d</b> objects",
700 len), len);
701 }
703 NRArenaItem *CGroup::show (NRArena *arena, unsigned int key, unsigned int flags) {
704 NRArenaItem *ai;
705 SPObject *object = SP_OBJECT(_group);
707 ai = NRArenaGroup::create(arena);
709 nr_arena_group_set_transparent(NR_ARENA_GROUP (ai),
710 _group->effectiveLayerMode(key) ==
711 SPGroup::LAYER);
712 nr_arena_group_set_style(NR_ARENA_GROUP(ai), SP_OBJECT_STYLE(object));
714 _showChildren(arena, ai, key, flags);
715 return ai;
716 }
718 void CGroup::_showChildren (NRArena *arena, NRArenaItem *ai, unsigned int key, unsigned int flags) {
719 NRArenaItem *ac = NULL;
720 NRArenaItem *ar = NULL;
721 SPItem * child = NULL;
722 GSList *l = g_slist_reverse(_group->childList(false, SPObject::ActionShow));
723 while (l) {
724 SPObject *o = SP_OBJECT (l->data);
725 if (SP_IS_ITEM (o)) {
726 child = SP_ITEM (o);
727 ac = sp_item_invoke_show (child, arena, key, flags);
728 if (ac) {
729 nr_arena_item_add_child (ai, ac, ar);
730 ar = ac;
731 nr_arena_item_unref (ac);
732 }
733 }
734 l = g_slist_remove (l, o);
735 }
736 }
738 void CGroup::hide (unsigned int key) {
739 SPItem * child;
741 GSList *l = g_slist_reverse(_group->childList(false, SPObject::ActionShow));
742 while (l) {
743 SPObject *o = SP_OBJECT (l->data);
744 if (SP_IS_ITEM (o)) {
745 child = SP_ITEM (o);
746 sp_item_invoke_hide (child, key);
747 }
748 l = g_slist_remove (l, o);
749 }
751 if (((SPItemClass *) parent_class)->hide)
752 ((SPItemClass *) parent_class)->hide (_group, key);
753 }
755 void CGroup::onOrderChanged (Inkscape::XML::Node *child, Inkscape::XML::Node *, Inkscape::XML::Node *)
756 {
757 SPObject *ochild = sp_object_get_child_by_repr(_group, child);
758 if ( ochild && SP_IS_ITEM(ochild) ) {
759 /* TODO: this should be moved into SPItem somehow */
760 SPItemView *v;
761 unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
762 for ( v = SP_ITEM (ochild)->display ; v != NULL ; v = v->next ) {
763 nr_arena_item_set_order (v->arenaitem, position);
764 }
765 }
767 _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
768 }
770 /*
771 Local Variables:
772 mode:c++
773 c-file-style:"stroustrup"
774 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
775 indent-tabs-mode:nil
776 fill-column:99
777 End:
778 */
779 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :