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 "display/curve.h"
27 #include "libnr/nr-matrix-ops.h"
28 #include "libnr/nr-matrix-fns.h"
29 #include "xml/repr.h"
30 #include "svg/svg.h"
31 #include "document.h"
32 #include "style.h"
33 #include "attributes.h"
34 #include "sp-item-transform.h"
35 #include "sp-root.h"
36 #include "sp-use.h"
37 #include "sp-offset.h"
38 #include "sp-clippath.h"
39 #include "sp-mask.h"
40 #include "sp-path.h"
41 #include "box3d.h"
42 #include "persp3d.h"
43 #include "inkscape.h"
44 #include "desktop-handles.h"
45 #include "selection.h"
46 #include "live_effects/lpeobject.h"
47 #include "live_effects/lpeobject-reference.h"
48 #include "sp-title.h"
49 #include "sp-desc.h"
50 #include "sp-switch.h"
52 static void sp_group_class_init (SPGroupClass *klass);
53 static void sp_group_init (SPGroup *group);
54 static void sp_group_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
55 static void sp_group_release(SPObject *object);
56 static void sp_group_dispose(GObject *object);
58 static void sp_group_child_added (SPObject * object, Inkscape::XML::Node * child, Inkscape::XML::Node * ref);
59 static void sp_group_remove_child (SPObject * object, Inkscape::XML::Node * child);
60 static void sp_group_order_changed (SPObject * object, Inkscape::XML::Node * child, Inkscape::XML::Node * old_ref, Inkscape::XML::Node * new_ref);
61 static void sp_group_update (SPObject *object, SPCtx *ctx, guint flags);
62 static void sp_group_modified (SPObject *object, guint flags);
63 static Inkscape::XML::Node *sp_group_write (SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
64 static void sp_group_set(SPObject *object, unsigned key, char const *value);
66 static void sp_group_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags);
67 static void sp_group_print (SPItem * item, SPPrintContext *ctx);
68 static gchar * sp_group_description (SPItem * item);
69 static Geom::Matrix sp_group_set_transform(SPItem *item, Geom::Matrix const &xform);
70 static NRArenaItem *sp_group_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags);
71 static void sp_group_hide (SPItem * item, unsigned int key);
72 static void sp_group_snappoints (SPItem const *item, bool const target, SnapPointsWithType &p, Inkscape::SnapPreferences const *snapprefs);
74 static void sp_group_update_patheffect(SPLPEItem *lpeitem, bool write);
75 static void sp_group_perform_patheffect(SPGroup *group, SPGroup *topgroup, bool write);
77 static SPLPEItemClass * parent_class;
79 GType
80 sp_group_get_type (void)
81 {
82 static GType group_type = 0;
83 if (!group_type) {
84 GTypeInfo group_info = {
85 sizeof (SPGroupClass),
86 NULL, /* base_init */
87 NULL, /* base_finalize */
88 (GClassInitFunc) sp_group_class_init,
89 NULL, /* class_finalize */
90 NULL, /* class_data */
91 sizeof (SPGroup),
92 16, /* n_preallocs */
93 (GInstanceInitFunc) sp_group_init,
94 NULL, /* value_table */
95 };
96 group_type = g_type_register_static (SP_TYPE_LPE_ITEM, "SPGroup", &group_info, (GTypeFlags)0);
97 }
98 return group_type;
99 }
101 static void
102 sp_group_class_init (SPGroupClass *klass)
103 {
104 GObjectClass * object_class;
105 SPObjectClass * sp_object_class;
106 SPItemClass * item_class;
107 SPLPEItemClass * lpe_item_class;
109 object_class = (GObjectClass *) klass;
110 sp_object_class = (SPObjectClass *) klass;
111 item_class = (SPItemClass *) klass;
112 lpe_item_class = (SPLPEItemClass *) klass;
114 parent_class = (SPLPEItemClass *)g_type_class_ref (SP_TYPE_LPE_ITEM);
116 object_class->dispose = sp_group_dispose;
118 sp_object_class->child_added = sp_group_child_added;
119 sp_object_class->remove_child = sp_group_remove_child;
120 sp_object_class->order_changed = sp_group_order_changed;
121 sp_object_class->update = sp_group_update;
122 sp_object_class->modified = sp_group_modified;
123 sp_object_class->set = sp_group_set;
124 sp_object_class->write = sp_group_write;
125 sp_object_class->release = sp_group_release;
126 sp_object_class->build = sp_group_build;
128 item_class->bbox = sp_group_bbox;
129 item_class->print = sp_group_print;
130 item_class->description = sp_group_description;
131 item_class->set_transform = sp_group_set_transform;
132 item_class->show = sp_group_show;
133 item_class->hide = sp_group_hide;
134 item_class->snappoints = sp_group_snappoints;
136 lpe_item_class->update_patheffect = sp_group_update_patheffect;
137 }
139 static void
140 sp_group_init (SPGroup *group)
141 {
142 group->_layer_mode = SPGroup::GROUP;
143 group->group = new CGroup(group);
144 new (&group->_display_modes) std::map<unsigned int, SPGroup::LayerMode>();
145 }
147 static void sp_group_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
148 {
149 sp_object_read_attr(object, "inkscape:groupmode");
151 if (((SPObjectClass *)parent_class)->build) {
152 ((SPObjectClass *)parent_class)->build(object, document, repr);
153 }
154 }
156 static void sp_group_release(SPObject *object) {
157 if ( SP_GROUP(object)->_layer_mode == SPGroup::LAYER ) {
158 sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "layer", object);
159 }
160 if (((SPObjectClass *)parent_class)->release) {
161 ((SPObjectClass *)parent_class)->release(object);
162 }
163 }
165 static void
166 sp_group_dispose(GObject *object)
167 {
168 SP_GROUP(object)->_display_modes.~map();
169 delete SP_GROUP(object)->group;
170 }
172 static void
173 sp_group_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
174 {
175 SPItem *item;
177 item = SP_ITEM (object);
179 if (((SPObjectClass *) (parent_class))->child_added)
180 (* ((SPObjectClass *) (parent_class))->child_added) (object, child, ref);
182 SP_GROUP(object)->group->onChildAdded(child);
183 }
185 /* fixme: hide (Lauris) */
187 static void
188 sp_group_remove_child (SPObject * object, Inkscape::XML::Node * child)
189 {
190 if (((SPObjectClass *) (parent_class))->remove_child)
191 (* ((SPObjectClass *) (parent_class))->remove_child) (object, child);
193 SP_GROUP(object)->group->onChildRemoved(child);
194 }
196 static void
197 sp_group_order_changed (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref)
198 {
199 if (((SPObjectClass *) (parent_class))->order_changed)
200 (* ((SPObjectClass *) (parent_class))->order_changed) (object, child, old_ref, new_ref);
202 SP_GROUP(object)->group->onOrderChanged(child, old_ref, new_ref);
203 }
205 static void
206 sp_group_update (SPObject *object, SPCtx *ctx, unsigned int flags)
207 {
208 if (((SPObjectClass *) (parent_class))->update)
209 ((SPObjectClass *) (parent_class))->update (object, ctx, flags);
211 SP_GROUP(object)->group->onUpdate(ctx, flags);
212 }
214 static void
215 sp_group_modified (SPObject *object, guint flags)
216 {
217 if (((SPObjectClass *) (parent_class))->modified)
218 ((SPObjectClass *) (parent_class))->modified (object, flags);
220 SP_GROUP(object)->group->onModified(flags);
221 }
223 static Inkscape::XML::Node *
224 sp_group_write (SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
225 {
226 SPGroup *group;
227 SPObject *child;
228 Inkscape::XML::Node *crepr;
230 group = SP_GROUP (object);
232 if (flags & SP_OBJECT_WRITE_BUILD) {
233 GSList *l;
234 if (!repr) {
235 if (SP_IS_SWITCH(object))
236 repr = xml_doc->createElement("svg:switch");
237 else
238 repr = xml_doc->createElement("svg:g");
239 }
240 l = NULL;
241 for (child = sp_object_first_child(object); child != NULL; child = SP_OBJECT_NEXT(child) ) {
242 if (SP_IS_TITLE(child) || SP_IS_DESC(child)) continue;
243 crepr = child->updateRepr(xml_doc, NULL, flags);
244 if (crepr) l = g_slist_prepend (l, crepr);
245 }
246 while (l) {
247 repr->addChild((Inkscape::XML::Node *) l->data, NULL);
248 Inkscape::GC::release((Inkscape::XML::Node *) l->data);
249 l = g_slist_remove (l, l->data);
250 }
251 } else {
252 for (child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
253 if (SP_IS_TITLE(child) || SP_IS_DESC(child)) continue;
254 child->updateRepr(flags);
255 }
256 }
258 if ( flags & SP_OBJECT_WRITE_EXT ) {
259 const char *value;
260 if ( group->_layer_mode == SPGroup::LAYER ) {
261 value = "layer";
262 } else if ( flags & SP_OBJECT_WRITE_ALL ) {
263 value = "group";
264 } else {
265 value = NULL;
266 }
267 repr->setAttribute("inkscape:groupmode", value);
268 }
270 if (((SPObjectClass *) (parent_class))->write)
271 ((SPObjectClass *) (parent_class))->write (object, xml_doc, repr, flags);
273 return repr;
274 }
276 static void
277 sp_group_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags)
278 {
279 SP_GROUP(item)->group->calculateBBox(bbox, transform, flags);
280 }
282 static void
283 sp_group_print (SPItem * item, SPPrintContext *ctx)
284 {
285 SP_GROUP(item)->group->onPrint(ctx);
286 }
288 static gchar * sp_group_description (SPItem * item)
289 {
290 return SP_GROUP(item)->group->getDescription();
291 }
293 static Geom::Matrix
294 sp_group_set_transform(SPItem *item, Geom::Matrix const &xform)
295 {
296 Inkscape::Selection *selection = sp_desktop_selection(inkscape_active_desktop());
297 persp3d_split_perspectives_according_to_selection(selection);
299 Geom::Matrix last_trans;
300 sp_svg_transform_read(SP_OBJECT_REPR(item)->attribute("transform"), &last_trans);
301 Geom::Matrix inc_trans = last_trans.inverse()*xform;
303 std::list<Persp3D *> plist = selection->perspList();
304 for (std::list<Persp3D *>::iterator i = plist.begin(); i != plist.end(); ++i) {
305 persp3d_apply_affine_transformation(*i, inc_trans);
306 }
308 return xform;
309 }
311 static void sp_group_set(SPObject *object, unsigned key, char const *value) {
312 SPGroup *group = SP_GROUP(object);
314 switch (key) {
315 case SP_ATTR_INKSCAPE_GROUPMODE:
316 if ( value && !strcmp(value, "layer") ) {
317 group->setLayerMode(SPGroup::LAYER);
318 } else {
319 group->setLayerMode(SPGroup::GROUP);
320 }
321 break;
322 default: {
323 if (((SPObjectClass *) (parent_class))->set) {
324 (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
325 }
326 }
327 }
328 }
330 static NRArenaItem *
331 sp_group_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags)
332 {
333 return SP_GROUP(item)->group->show(arena, key, flags);
334 }
336 static void
337 sp_group_hide (SPItem *item, unsigned int key)
338 {
339 SP_GROUP(item)->group->hide(key);
340 }
342 static void sp_group_snappoints (SPItem const *item, bool const target, SnapPointsWithType &p, Inkscape::SnapPreferences const *snapprefs)
343 {
344 for (SPObject const *o = sp_object_first_child(SP_OBJECT(item));
345 o != NULL;
346 o = SP_OBJECT_NEXT(o))
347 {
348 if (SP_IS_ITEM(o)) {
349 sp_item_snappoints(SP_ITEM(o), target, p, snapprefs);
350 }
351 }
352 }
355 void
356 sp_item_group_ungroup (SPGroup *group, GSList **children, bool do_done)
357 {
358 g_return_if_fail (group != NULL);
359 g_return_if_fail (SP_IS_GROUP (group));
361 SPDocument *doc = SP_OBJECT_DOCUMENT (group);
362 SPObject *root = SP_DOCUMENT_ROOT (doc);
363 SPObject *defs = SP_OBJECT (SP_ROOT (root)->defs);
365 SPItem *gitem = SP_ITEM (group);
366 Inkscape::XML::Node *grepr = SP_OBJECT_REPR (gitem);
368 g_return_if_fail (!strcmp (grepr->name(), "svg:g") || !strcmp (grepr->name(), "svg:a") || !strcmp (grepr->name(), "svg:switch"));
370 // this converts the gradient/pattern fill/stroke on the group, if any, to userSpaceOnUse
371 sp_item_adjust_paint_recursive (gitem, Geom::identity(), Geom::identity(), false);
373 SPItem *pitem = SP_ITEM (SP_OBJECT_PARENT (gitem));
374 Inkscape::XML::Node *prepr = SP_OBJECT_REPR (pitem);
376 if (SP_IS_BOX3D(gitem)) {
377 group = box3d_convert_to_group(SP_BOX3D(gitem));
378 gitem = SP_ITEM(group);
379 }
381 sp_lpe_item_remove_all_path_effects(SP_LPE_ITEM(group), false);
383 /* Step 1 - generate lists of children objects */
384 GSList *items = NULL;
385 GSList *objects = NULL;
386 for (SPObject *child = sp_object_first_child(SP_OBJECT(group)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
388 if (SP_IS_ITEM (child)) {
390 SPItem *citem = SP_ITEM (child);
392 /* Merging of style */
393 // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
394 // it here _before_ the new transform is set, so as to use the pre-transform bbox
395 sp_item_adjust_paint_recursive (citem, Geom::identity(), Geom::identity(), false);
397 sp_style_merge_from_dying_parent(SP_OBJECT_STYLE(child), SP_OBJECT_STYLE(gitem));
398 /*
399 * fixme: We currently make no allowance for the case where child is cloned
400 * and the group has any style settings.
401 *
402 * (This should never occur with documents created solely with the current
403 * version of inkscape without using the XML editor: we usually apply group
404 * style changes to children rather than to the group itself.)
405 *
406 * If the group has no style settings, then
407 * sp_style_merge_from_dying_parent should be a no-op. Otherwise (i.e. if
408 * we change the child's style to compensate for its parent going away)
409 * then those changes will typically be reflected in any clones of child,
410 * whereas we'd prefer for Ungroup not to affect the visual appearance.
411 *
412 * The only way of preserving styling appearance in general is for child to
413 * be put into a new group -- a somewhat surprising response to an Ungroup
414 * command. We could add a new groupmode:transparent that would mostly
415 * hide the existence of such groups from the user (i.e. editing behaves as
416 * if the transparent group's children weren't in a group), though that's
417 * extra complication & maintenance burden and this case is rare.
418 */
420 child->updateRepr();
422 Inkscape::XML::Node *nrepr = SP_OBJECT_REPR (child)->duplicate(prepr->document());
424 // Merging transform
425 Geom::Matrix ctrans;
426 Geom::Matrix const g(gitem->transform);
427 if (SP_IS_USE(citem) && sp_use_get_original (SP_USE(citem)) &&
428 SP_OBJECT_PARENT (sp_use_get_original (SP_USE(citem))) == SP_OBJECT(group)) {
429 // make sure a clone's effective transform is the same as was under group
430 ctrans = g.inverse() * citem->transform * g;
431 } else {
432 // We should not apply the group's transformation to both a linked offset AND to its source
433 if (SP_IS_OFFSET(citem)) { // Do we have an offset at hand (whether it's dynamic or linked)?
434 SPItem *source = sp_offset_get_source(SP_OFFSET(citem));
435 // When dealing with a chain of linked offsets, the transformation of an offset will be
436 // tied to the transformation of the top-most source, not to any of the intermediate
437 // offsets. So let's find the top-most source
438 while (source != NULL && SP_IS_OFFSET(source)) {
439 source = sp_offset_get_source(SP_OFFSET(source));
440 }
441 if (source != NULL && // If true then we must be dealing with a linked offset ...
442 SP_OBJECT(group)->isAncestorOf(SP_OBJECT(source)) == false) { // ... of which the source is not in the same group
443 ctrans = citem->transform * g; // then we should apply the transformation of the group to the offset
444 } else {
445 ctrans = citem->transform;
446 }
447 } else {
448 ctrans = citem->transform * g;
449 }
450 }
452 // FIXME: constructing a transform that would fully preserve the appearance of a
453 // textpath if it is ungrouped with its path seems to be impossible in general
454 // case. E.g. if the group was squeezed, to keep the ungrouped textpath squeezed
455 // as well, we'll need to relink it to some "virtual" path which is inversely
456 // stretched relative to the actual path, and then squeeze the textpath back so it
457 // would both fit the actual path _and_ be squeezed as before. It's a bummer.
459 // This is just a way to temporarily remember the transform in repr. When repr is
460 // reattached outside of the group, the transform will be written more properly
461 // (i.e. optimized into the object if the corresponding preference is set)
462 gchar *affinestr=sp_svg_transform_write(ctrans);
463 nrepr->setAttribute("transform", affinestr);
464 g_free(affinestr);
466 items = g_slist_prepend (items, nrepr);
468 } else {
469 Inkscape::XML::Node *nrepr = SP_OBJECT_REPR (child)->duplicate(prepr->document());
470 objects = g_slist_prepend (objects, nrepr);
471 }
472 }
474 /* Step 2 - clear group */
475 // remember the position of the group
476 gint pos = SP_OBJECT_REPR(group)->position();
478 // the group is leaving forever, no heir, clones should take note; its children however are going to reemerge
479 SP_OBJECT (group)->deleteObject(true, false);
481 /* Step 3 - add nonitems */
482 if (objects) {
483 Inkscape::XML::Node *last_def = SP_OBJECT_REPR(defs)->lastChild();
484 while (objects) {
485 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) objects->data;
486 if (!sp_repr_is_meta_element(repr))
487 SP_OBJECT_REPR(defs)->addChild(repr, last_def);
488 Inkscape::GC::release(repr);
489 objects = g_slist_remove (objects, objects->data);
490 }
491 }
493 /* Step 4 - add items */
494 while (items) {
495 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) items->data;
496 // add item
497 prepr->appendChild(repr);
498 // restore position; since the items list was prepended (i.e. reverse), we now add
499 // all children at the same pos, which inverts the order once again
500 repr->setPosition(pos > 0 ? pos : 0);
502 // fill in the children list if non-null
503 SPItem *item = (SPItem *) doc->getObjectByRepr(repr);
505 sp_item_write_transform(item, repr, item->transform, NULL, false);
507 Inkscape::GC::release(repr);
508 if (children && SP_IS_ITEM (item))
509 *children = g_slist_prepend (*children, item);
511 items = g_slist_remove (items, items->data);
512 }
514 if (do_done)
515 sp_document_done (doc, SP_VERB_NONE, _("Ungroup"));
516 }
518 /*
519 * some API for list aspect of SPGroup
520 */
522 GSList *
523 sp_item_group_item_list (SPGroup * group)
524 {
525 g_return_val_if_fail (group != NULL, NULL);
526 g_return_val_if_fail (SP_IS_GROUP (group), NULL);
528 GSList *s = NULL;
530 for (SPObject *o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
531 if (SP_IS_ITEM (o)) {
532 s = g_slist_prepend (s, o);
533 }
534 }
536 return g_slist_reverse (s);
537 }
539 SPObject *
540 sp_item_group_get_child_by_name (SPGroup *group, SPObject *ref, const gchar *name)
541 {
542 SPObject *child;
543 child = (ref) ? SP_OBJECT_NEXT(ref) : sp_object_first_child(SP_OBJECT(group));
544 while ( child && strcmp (SP_OBJECT_REPR(child)->name(), name) ) {
545 child = SP_OBJECT_NEXT(child);
546 }
547 return child;
548 }
550 void SPGroup::setLayerMode(LayerMode mode) {
551 if ( _layer_mode != mode ) {
552 if ( mode == LAYER ) {
553 sp_document_add_resource(SP_OBJECT_DOCUMENT(this), "layer", this);
554 } else {
555 sp_document_remove_resource(SP_OBJECT_DOCUMENT(this), "layer", this);
556 }
557 _layer_mode = mode;
558 _updateLayerMode();
559 }
560 }
562 SPGroup::LayerMode SPGroup::layerDisplayMode(unsigned int dkey) const {
563 std::map<unsigned int, LayerMode>::const_iterator iter;
564 iter = _display_modes.find(dkey);
565 if ( iter != _display_modes.end() ) {
566 return (*iter).second;
567 } else {
568 return GROUP;
569 }
570 }
572 void SPGroup::setLayerDisplayMode(unsigned int dkey, SPGroup::LayerMode mode) {
573 if ( layerDisplayMode(dkey) != mode ) {
574 _display_modes[dkey] = mode;
575 _updateLayerMode(dkey);
576 }
577 }
579 void SPGroup::_updateLayerMode(unsigned int display_key) {
580 SPItemView *view;
581 for ( view = this->display ; view ; view = view->next ) {
582 if ( !display_key || view->key == display_key ) {
583 NRArenaGroup *arena_group=NR_ARENA_GROUP(view->arenaitem);
584 if (arena_group) {
585 nr_arena_group_set_transparent(arena_group, effectiveLayerMode(view->key) == SPGroup::LAYER);
586 }
587 }
588 }
589 }
591 void SPGroup::translateChildItems(Geom::Translate const &tr)
592 {
593 if (this->hasChildren())
594 {
595 SPObject *o = NULL;
596 for (o = sp_object_first_child(SP_OBJECT(this)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
597 if (SP_IS_ITEM (o)) {
598 sp_item_move_rel(static_cast<SPItem *>(o), tr);
599 }
600 }
601 }
602 }
604 CGroup::CGroup(SPGroup *group) {
605 _group = group;
606 }
608 CGroup::~CGroup() {
609 }
611 void CGroup::onChildAdded(Inkscape::XML::Node *child) {
612 SPObject *last_child = _group->lastChild();
613 if (last_child && SP_OBJECT_REPR(last_child) == child) {
614 // optimization for the common special case where the child is being added at the end
615 SPObject *ochild = last_child;
616 if ( SP_IS_ITEM(ochild) ) {
617 /* TODO: this should be moved into SPItem somehow */
618 SPItemView *v;
619 NRArenaItem *ac;
621 for (v = _group->display; v != NULL; v = v->next) {
622 ac = sp_item_invoke_show (SP_ITEM (ochild), NR_ARENA_ITEM_ARENA (v->arenaitem), v->key, v->flags);
624 if (ac) {
625 nr_arena_item_append_child (v->arenaitem, ac);
626 }
627 }
628 }
629 } else { // general case
630 SPObject *ochild = sp_object_get_child_by_repr(_group, child);
631 if ( ochild && SP_IS_ITEM(ochild) ) {
632 /* TODO: this should be moved into SPItem somehow */
633 SPItemView *v;
634 NRArenaItem *ac;
636 unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
638 for (v = _group->display; v != NULL; v = v->next) {
639 ac = sp_item_invoke_show (SP_ITEM (ochild), NR_ARENA_ITEM_ARENA (v->arenaitem), v->key, v->flags);
641 if (ac) {
642 nr_arena_item_add_child (v->arenaitem, ac, NULL);
643 nr_arena_item_set_order (ac, position);
644 }
645 }
646 }
647 }
649 _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
650 }
652 void CGroup::onChildRemoved(Inkscape::XML::Node */*child*/) {
653 _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
654 }
656 void CGroup::onUpdate(SPCtx *ctx, unsigned int flags) {
657 SPItemCtx *ictx, cctx;
659 ictx = (SPItemCtx *) ctx;
660 cctx = *ictx;
662 if (flags & SP_OBJECT_MODIFIED_FLAG) {
663 flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
664 }
666 flags &= SP_OBJECT_MODIFIED_CASCADE;
668 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
669 SPObject *object = SP_OBJECT(_group);
670 for (SPItemView *v = SP_ITEM(_group)->display; v != NULL; v = v->next) {
671 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
672 }
673 }
675 GSList *l = g_slist_reverse(_group->childList(true, SPObject::ActionUpdate));
676 while (l) {
677 SPObject *child = SP_OBJECT (l->data);
678 l = g_slist_remove (l, child);
679 if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
680 if (SP_IS_ITEM (child)) {
681 SPItem const &chi = *SP_ITEM(child);
682 cctx.i2doc = chi.transform * ictx->i2doc;
683 cctx.i2vp = chi.transform * ictx->i2vp;
684 child->updateDisplay((SPCtx *)&cctx, flags);
685 } else {
686 child->updateDisplay(ctx, flags);
687 }
688 }
689 g_object_unref (G_OBJECT (child));
690 }
691 }
693 void CGroup::onModified(guint flags) {
694 SPObject *child;
696 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
697 flags &= SP_OBJECT_MODIFIED_CASCADE;
699 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
700 SPObject *object = SP_OBJECT(_group);
701 for (SPItemView *v = SP_ITEM(_group)->display; v != NULL; v = v->next) {
702 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
703 }
704 }
706 GSList *l = g_slist_reverse(_group->childList(true));
707 while (l) {
708 child = SP_OBJECT (l->data);
709 l = g_slist_remove (l, child);
710 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
711 child->emitModified(flags);
712 }
713 g_object_unref (G_OBJECT (child));
714 }
715 }
717 void CGroup::calculateBBox(NRRect *bbox, Geom::Matrix const &transform, unsigned const flags) {
719 Geom::OptRect dummy_bbox;
721 GSList *l = _group->childList(false, SPObject::ActionBBox);
722 while (l) {
723 SPObject *o = SP_OBJECT (l->data);
724 if (SP_IS_ITEM(o) && !SP_ITEM(o)->isHidden()) {
725 SPItem *child = SP_ITEM(o);
726 Geom::Matrix const ct(to_2geom(child->transform) * transform);
727 sp_item_invoke_bbox_full(child, dummy_bbox, ct, flags, FALSE);
728 }
729 l = g_slist_remove (l, o);
730 }
732 *bbox = NRRect(dummy_bbox);
733 }
735 void CGroup::onPrint(SPPrintContext *ctx) {
736 GSList *l = g_slist_reverse(_group->childList(false));
737 while (l) {
738 SPObject *o = SP_OBJECT (l->data);
739 if (SP_IS_ITEM(o)) {
740 sp_item_invoke_print (SP_ITEM (o), ctx);
741 }
742 l = g_slist_remove (l, o);
743 }
744 }
746 gint CGroup::getItemCount() {
747 gint len = 0;
748 for (SPObject *o = sp_object_first_child(SP_OBJECT(_group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
749 if (SP_IS_ITEM(o)) {
750 len++;
751 }
752 }
754 return len;
755 }
757 gchar *CGroup::getDescription() {
758 gint len = getItemCount();
759 return g_strdup_printf(
760 ngettext("<b>Group</b> of <b>%d</b> object",
761 "<b>Group</b> of <b>%d</b> objects",
762 len), len);
763 }
765 NRArenaItem *CGroup::show (NRArena *arena, unsigned int key, unsigned int flags) {
766 NRArenaItem *ai;
767 SPObject *object = SP_OBJECT(_group);
769 ai = NRArenaGroup::create(arena);
771 nr_arena_group_set_transparent(NR_ARENA_GROUP (ai),
772 _group->effectiveLayerMode(key) ==
773 SPGroup::LAYER);
774 nr_arena_group_set_style(NR_ARENA_GROUP(ai), SP_OBJECT_STYLE(object));
776 _showChildren(arena, ai, key, flags);
777 return ai;
778 }
780 void CGroup::_showChildren (NRArena *arena, NRArenaItem *ai, unsigned int key, unsigned int flags) {
781 NRArenaItem *ac = NULL;
782 NRArenaItem *ar = NULL;
783 SPItem * child = NULL;
784 GSList *l = g_slist_reverse(_group->childList(false, SPObject::ActionShow));
785 while (l) {
786 SPObject *o = SP_OBJECT (l->data);
787 if (SP_IS_ITEM (o)) {
788 child = SP_ITEM (o);
789 ac = sp_item_invoke_show (child, arena, key, flags);
790 if (ac) {
791 nr_arena_item_add_child (ai, ac, ar);
792 ar = ac;
793 }
794 }
795 l = g_slist_remove (l, o);
796 }
797 }
799 void CGroup::hide (unsigned int key) {
800 SPItem * child;
802 GSList *l = g_slist_reverse(_group->childList(false, SPObject::ActionShow));
803 while (l) {
804 SPObject *o = SP_OBJECT (l->data);
805 if (SP_IS_ITEM (o)) {
806 child = SP_ITEM (o);
807 sp_item_invoke_hide (child, key);
808 }
809 l = g_slist_remove (l, o);
810 }
812 if (((SPItemClass *) parent_class)->hide)
813 ((SPItemClass *) parent_class)->hide (_group, key);
814 }
816 void CGroup::onOrderChanged (Inkscape::XML::Node *child, Inkscape::XML::Node *, Inkscape::XML::Node *)
817 {
818 SPObject *ochild = sp_object_get_child_by_repr(_group, child);
819 if ( ochild && SP_IS_ITEM(ochild) ) {
820 /* TODO: this should be moved into SPItem somehow */
821 SPItemView *v;
822 unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
823 for ( v = SP_ITEM (ochild)->display ; v != NULL ; v = v->next ) {
824 nr_arena_item_set_order (v->arenaitem, position);
825 }
826 }
828 _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
829 }
831 static void
832 sp_group_update_patheffect (SPLPEItem *lpeitem, bool write)
833 {
834 #ifdef GROUP_VERBOSE
835 g_message("sp_group_update_patheffect: %p\n", lpeitem);
836 #endif
837 g_return_if_fail (lpeitem != NULL);
838 g_return_if_fail (SP_IS_GROUP (lpeitem));
840 GSList const *item_list = sp_item_group_item_list(SP_GROUP(lpeitem));
841 for ( GSList const *iter = item_list; iter; iter = iter->next ) {
842 SPObject *subitem = static_cast<SPObject *>(iter->data);
843 if (SP_IS_LPE_ITEM(subitem)) {
844 if (SP_LPE_ITEM_CLASS (G_OBJECT_GET_CLASS (subitem))->update_patheffect) {
845 SP_LPE_ITEM_CLASS (G_OBJECT_GET_CLASS (subitem))->update_patheffect (SP_LPE_ITEM(subitem), write);
846 }
847 }
848 }
850 if (sp_lpe_item_has_path_effect(lpeitem) && sp_lpe_item_path_effects_enabled(lpeitem)) {
851 for (PathEffectList::iterator it = lpeitem->path_effect_list->begin(); it != lpeitem->path_effect_list->end(); it++)
852 {
853 LivePathEffectObject *lpeobj = (*it)->lpeobject;
854 if (lpeobj && lpeobj->get_lpe()) {
855 lpeobj->get_lpe()->doBeforeEffect(lpeitem);
856 }
857 }
859 sp_group_perform_patheffect(SP_GROUP(lpeitem), SP_GROUP(lpeitem), write);
860 }
861 }
863 static void
864 sp_group_perform_patheffect(SPGroup *group, SPGroup *topgroup, bool write)
865 {
866 GSList const *item_list = sp_item_group_item_list(SP_GROUP(group));
867 for ( GSList const *iter = item_list; iter; iter = iter->next ) {
868 SPObject *subitem = static_cast<SPObject *>(iter->data);
869 if (SP_IS_GROUP(subitem)) {
870 sp_group_perform_patheffect(SP_GROUP(subitem), topgroup, write);
871 } else if (SP_IS_SHAPE(subitem)) {
872 SPCurve * c = NULL;
873 if (SP_IS_PATH(subitem)) {
874 c = sp_path_get_curve_for_edit(SP_PATH(subitem));
875 } else {
876 c = sp_shape_get_curve(SP_SHAPE(subitem));
877 }
878 // only run LPEs when the shape has a curve defined
879 if (c) {
880 sp_lpe_item_perform_path_effect(SP_LPE_ITEM(topgroup), c);
881 sp_shape_set_curve(SP_SHAPE(subitem), c, TRUE);
883 if (write) {
884 Inkscape::XML::Node *repr = SP_OBJECT_REPR(subitem);
885 gchar *str = sp_svg_write_path(c->get_pathvector());
886 repr->setAttribute("d", str);
887 g_free(str);
888 }
890 c->unref();
891 }
892 }
893 }
894 }
896 /*
897 Local Variables:
898 mode:c++
899 c-file-style:"stroustrup"
900 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
901 indent-tabs-mode:nil
902 fill-column:99
903 End:
904 */
905 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :