1 /*
2 * SVG <g> implementation
3 *
4 * Authors:
5 * Lauris Kaplinski <lauris@kaplinski.com>
6 * bulia byak <buliabyak@users.sf.net>
7 * Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
8 * Jon A. Cruz <jon@joncruz.org>
9 * Abhishek Sharma
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/effect.h"
47 #include "live_effects/lpeobject.h"
48 #include "live_effects/lpeobject-reference.h"
49 #include "sp-title.h"
50 #include "sp-desc.h"
51 #include "sp-switch.h"
53 using Inkscape::DocumentUndo;
55 static void sp_group_class_init (SPGroupClass *klass);
56 static void sp_group_init (SPGroup *group);
57 static void sp_group_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
58 static void sp_group_release(SPObject *object);
59 static void sp_group_dispose(GObject *object);
61 static void sp_group_child_added (SPObject * object, Inkscape::XML::Node * child, Inkscape::XML::Node * ref);
62 static void sp_group_remove_child (SPObject * object, Inkscape::XML::Node * child);
63 static void sp_group_order_changed (SPObject * object, Inkscape::XML::Node * child, Inkscape::XML::Node * old_ref, Inkscape::XML::Node * new_ref);
64 static void sp_group_update (SPObject *object, SPCtx *ctx, guint flags);
65 static void sp_group_modified (SPObject *object, guint flags);
66 static Inkscape::XML::Node *sp_group_write (SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
67 static void sp_group_set(SPObject *object, unsigned key, char const *value);
69 static void sp_group_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags);
70 static void sp_group_print (SPItem * item, SPPrintContext *ctx);
71 static gchar * sp_group_description (SPItem * item);
72 static NRArenaItem *sp_group_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags);
73 static void sp_group_hide (SPItem * item, unsigned int key);
74 static void sp_group_snappoints (SPItem const *item, std::vector<Inkscape::SnapCandidatePoint> &p, Inkscape::SnapPreferences const *snapprefs);
76 static void sp_group_update_patheffect(SPLPEItem *lpeitem, bool write);
77 static void sp_group_perform_patheffect(SPGroup *group, SPGroup *topgroup, bool write);
79 static SPLPEItemClass * parent_class;
81 GType
82 sp_group_get_type (void)
83 {
84 static GType group_type = 0;
85 if (!group_type) {
86 GTypeInfo group_info = {
87 sizeof (SPGroupClass),
88 NULL, /* base_init */
89 NULL, /* base_finalize */
90 (GClassInitFunc) sp_group_class_init,
91 NULL, /* class_finalize */
92 NULL, /* class_data */
93 sizeof (SPGroup),
94 16, /* n_preallocs */
95 (GInstanceInitFunc) sp_group_init,
96 NULL, /* value_table */
97 };
98 group_type = g_type_register_static (SP_TYPE_LPE_ITEM, "SPGroup", &group_info, (GTypeFlags)0);
99 }
100 return group_type;
101 }
103 static void
104 sp_group_class_init (SPGroupClass *klass)
105 {
106 GObjectClass * object_class;
107 SPObjectClass * sp_object_class;
108 SPItemClass * item_class;
109 SPLPEItemClass * lpe_item_class;
111 object_class = (GObjectClass *) klass;
112 sp_object_class = (SPObjectClass *) klass;
113 item_class = (SPItemClass *) klass;
114 lpe_item_class = (SPLPEItemClass *) klass;
116 parent_class = (SPLPEItemClass *)g_type_class_ref (SP_TYPE_LPE_ITEM);
118 object_class->dispose = sp_group_dispose;
120 sp_object_class->child_added = sp_group_child_added;
121 sp_object_class->remove_child = sp_group_remove_child;
122 sp_object_class->order_changed = sp_group_order_changed;
123 sp_object_class->update = sp_group_update;
124 sp_object_class->modified = sp_group_modified;
125 sp_object_class->set = sp_group_set;
126 sp_object_class->write = sp_group_write;
127 sp_object_class->release = sp_group_release;
128 sp_object_class->build = sp_group_build;
130 item_class->bbox = sp_group_bbox;
131 item_class->print = sp_group_print;
132 item_class->description = sp_group_description;
133 item_class->show = sp_group_show;
134 item_class->hide = sp_group_hide;
135 item_class->snappoints = sp_group_snappoints;
137 lpe_item_class->update_patheffect = sp_group_update_patheffect;
138 }
140 static void
141 sp_group_init (SPGroup *group)
142 {
143 group->_layer_mode = SPGroup::GROUP;
144 group->group = new CGroup(group);
145 new (&group->_display_modes) std::map<unsigned int, SPGroup::LayerMode>();
146 }
148 static void sp_group_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
149 {
150 object->readAttr( "inkscape:groupmode" );
152 if (((SPObjectClass *)parent_class)->build) {
153 ((SPObjectClass *)parent_class)->build(object, document, repr);
154 }
155 }
157 static void sp_group_release(SPObject *object) {
158 if ( SP_GROUP(object)->_layer_mode == SPGroup::LAYER ) {
159 SP_OBJECT_DOCUMENT(object)->removeResource("layer", object);
160 }
161 if (((SPObjectClass *)parent_class)->release) {
162 ((SPObjectClass *)parent_class)->release(object);
163 }
164 }
166 static void
167 sp_group_dispose(GObject *object)
168 {
169 SP_GROUP(object)->_display_modes.~map();
170 delete SP_GROUP(object)->group;
171 }
173 static void
174 sp_group_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
175 {
176 SPItem *item;
178 item = SP_ITEM (object);
180 if (((SPObjectClass *) (parent_class))->child_added)
181 (* ((SPObjectClass *) (parent_class))->child_added) (object, child, ref);
183 SP_GROUP(object)->group->onChildAdded(child);
184 }
186 /* fixme: hide (Lauris) */
188 static void
189 sp_group_remove_child (SPObject * object, Inkscape::XML::Node * child)
190 {
191 if (((SPObjectClass *) (parent_class))->remove_child)
192 (* ((SPObjectClass *) (parent_class))->remove_child) (object, child);
194 SP_GROUP(object)->group->onChildRemoved(child);
195 }
197 static void
198 sp_group_order_changed (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref)
199 {
200 if (((SPObjectClass *) (parent_class))->order_changed)
201 (* ((SPObjectClass *) (parent_class))->order_changed) (object, child, old_ref, new_ref);
203 SP_GROUP(object)->group->onOrderChanged(child, old_ref, new_ref);
204 }
206 static void
207 sp_group_update (SPObject *object, SPCtx *ctx, unsigned int flags)
208 {
209 if (((SPObjectClass *) (parent_class))->update)
210 ((SPObjectClass *) (parent_class))->update (object, ctx, flags);
212 SP_GROUP(object)->group->onUpdate(ctx, flags);
213 }
215 static void
216 sp_group_modified (SPObject *object, guint flags)
217 {
218 if (((SPObjectClass *) (parent_class))->modified)
219 ((SPObjectClass *) (parent_class))->modified (object, flags);
221 SP_GROUP(object)->group->onModified(flags);
222 }
224 static Inkscape::XML::Node * sp_group_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
225 {
226 SPGroup *group = SP_GROUP(object);
228 if (flags & SP_OBJECT_WRITE_BUILD) {
229 GSList *l;
230 if (!repr) {
231 if (SP_IS_SWITCH(object)) {
232 repr = xml_doc->createElement("svg:switch");
233 } else {
234 repr = xml_doc->createElement("svg:g");
235 }
236 }
237 l = NULL;
238 for (SPObject *child = object->firstChild(); child; child = child->getNext() ) {
239 if ( !SP_IS_TITLE(child) && !SP_IS_DESC(child) ) {
240 Inkscape::XML::Node *crepr = child->updateRepr(xml_doc, NULL, flags);
241 if (crepr) {
242 l = g_slist_prepend (l, crepr);
243 }
244 }
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 (SPObject *child = object->firstChild() ; child ; child = child->getNext() ) {
253 if ( !SP_IS_TITLE(child) && !SP_IS_DESC(child) ) {
254 child->updateRepr(flags);
255 }
256 }
257 }
259 if ( flags & SP_OBJECT_WRITE_EXT ) {
260 const char *value;
261 if ( group->_layer_mode == SPGroup::LAYER ) {
262 value = "layer";
263 } else if ( group->_layer_mode == SPGroup::MASK_HELPER ) {
264 value = "maskhelper";
265 } else if ( flags & SP_OBJECT_WRITE_ALL ) {
266 value = "group";
267 } else {
268 value = NULL;
269 }
270 repr->setAttribute("inkscape:groupmode", value);
271 }
273 if (((SPObjectClass *) (parent_class))->write) {
274 ((SPObjectClass *) (parent_class))->write (object, xml_doc, repr, flags);
275 }
277 return repr;
278 }
280 static void
281 sp_group_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags)
282 {
283 SP_GROUP(item)->group->calculateBBox(bbox, transform, flags);
284 }
286 static void
287 sp_group_print (SPItem * item, SPPrintContext *ctx)
288 {
289 SP_GROUP(item)->group->onPrint(ctx);
290 }
292 static gchar * sp_group_description (SPItem * item)
293 {
294 return SP_GROUP(item)->group->getDescription();
295 }
297 static void sp_group_set(SPObject *object, unsigned key, char const *value) {
298 SPGroup *group = SP_GROUP(object);
300 switch (key) {
301 case SP_ATTR_INKSCAPE_GROUPMODE:
302 if ( value && !strcmp(value, "layer") ) {
303 group->setLayerMode(SPGroup::LAYER);
304 } else if ( value && !strcmp(value, "maskhelper") ) {
305 group->setLayerMode(SPGroup::MASK_HELPER);
306 } else {
307 group->setLayerMode(SPGroup::GROUP);
308 }
309 break;
310 default: {
311 if (((SPObjectClass *) (parent_class))->set) {
312 (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
313 }
314 }
315 }
316 }
318 static NRArenaItem *
319 sp_group_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags)
320 {
321 return SP_GROUP(item)->group->show(arena, key, flags);
322 }
324 static void
325 sp_group_hide (SPItem *item, unsigned int key)
326 {
327 SP_GROUP(item)->group->hide(key);
328 }
330 static void sp_group_snappoints(SPItem const *item, std::vector<Inkscape::SnapCandidatePoint> &p, Inkscape::SnapPreferences const *snapprefs)
331 {
332 for ( SPObject const *o = item->firstChild(); o; o = o->getNext() )
333 {
334 if (SP_IS_ITEM(o)) {
335 SP_ITEM(o)->getSnappoints(p, snapprefs);
336 }
337 }
338 }
341 void
342 sp_item_group_ungroup (SPGroup *group, GSList **children, bool do_done)
343 {
344 g_return_if_fail (group != NULL);
345 g_return_if_fail (SP_IS_GROUP (group));
347 SPDocument *doc = SP_OBJECT_DOCUMENT (group);
348 SPObject *root = doc->getRoot();
349 SPObject *defs = SP_OBJECT (SP_ROOT (root)->defs);
351 SPItem *gitem = SP_ITEM (group);
352 Inkscape::XML::Node *grepr = SP_OBJECT_REPR (gitem);
354 g_return_if_fail (!strcmp (grepr->name(), "svg:g") || !strcmp (grepr->name(), "svg:a") || !strcmp (grepr->name(), "svg:switch"));
356 // this converts the gradient/pattern fill/stroke on the group, if any, to userSpaceOnUse
357 gitem->adjust_paint_recursive (Geom::identity(), Geom::identity(), false);
359 SPItem *pitem = SP_ITEM (SP_OBJECT_PARENT (gitem));
360 Inkscape::XML::Node *prepr = SP_OBJECT_REPR (pitem);
362 if (SP_IS_BOX3D(gitem)) {
363 group = box3d_convert_to_group(SP_BOX3D(gitem));
364 gitem = SP_ITEM(group);
365 }
367 sp_lpe_item_remove_all_path_effects(SP_LPE_ITEM(group), false);
369 /* Step 1 - generate lists of children objects */
370 GSList *items = NULL;
371 GSList *objects = NULL;
372 for (SPObject *child = group->firstChild() ; child; child = child->getNext() ) {
374 if (SP_IS_ITEM (child)) {
376 SPItem *citem = SP_ITEM (child);
378 /* Merging of style */
379 // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
380 // it here _before_ the new transform is set, so as to use the pre-transform bbox
381 citem->adjust_paint_recursive (Geom::identity(), Geom::identity(), false);
383 sp_style_merge_from_dying_parent(SP_OBJECT_STYLE(child), SP_OBJECT_STYLE(gitem));
384 /*
385 * fixme: We currently make no allowance for the case where child is cloned
386 * and the group has any style settings.
387 *
388 * (This should never occur with documents created solely with the current
389 * version of inkscape without using the XML editor: we usually apply group
390 * style changes to children rather than to the group itself.)
391 *
392 * If the group has no style settings, then
393 * sp_style_merge_from_dying_parent should be a no-op. Otherwise (i.e. if
394 * we change the child's style to compensate for its parent going away)
395 * then those changes will typically be reflected in any clones of child,
396 * whereas we'd prefer for Ungroup not to affect the visual appearance.
397 *
398 * The only way of preserving styling appearance in general is for child to
399 * be put into a new group -- a somewhat surprising response to an Ungroup
400 * command. We could add a new groupmode:transparent that would mostly
401 * hide the existence of such groups from the user (i.e. editing behaves as
402 * if the transparent group's children weren't in a group), though that's
403 * extra complication & maintenance burden and this case is rare.
404 */
406 child->updateRepr();
408 Inkscape::XML::Node *nrepr = SP_OBJECT_REPR (child)->duplicate(prepr->document());
410 // Merging transform
411 Geom::Matrix ctrans;
412 Geom::Matrix const g(gitem->transform);
413 if (SP_IS_USE(citem) && sp_use_get_original (SP_USE(citem)) &&
414 SP_OBJECT_PARENT (sp_use_get_original (SP_USE(citem))) == SP_OBJECT(group)) {
415 // make sure a clone's effective transform is the same as was under group
416 ctrans = g.inverse() * citem->transform * g;
417 } else {
418 // We should not apply the group's transformation to both a linked offset AND to its source
419 if (SP_IS_OFFSET(citem)) { // Do we have an offset at hand (whether it's dynamic or linked)?
420 SPItem *source = sp_offset_get_source(SP_OFFSET(citem));
421 // When dealing with a chain of linked offsets, the transformation of an offset will be
422 // tied to the transformation of the top-most source, not to any of the intermediate
423 // offsets. So let's find the top-most source
424 while (source != NULL && SP_IS_OFFSET(source)) {
425 source = sp_offset_get_source(SP_OFFSET(source));
426 }
427 if (source != NULL && // If true then we must be dealing with a linked offset ...
428 SP_OBJECT(group)->isAncestorOf(SP_OBJECT(source)) == false) { // ... of which the source is not in the same group
429 ctrans = citem->transform * g; // then we should apply the transformation of the group to the offset
430 } else {
431 ctrans = citem->transform;
432 }
433 } else {
434 ctrans = citem->transform * g;
435 }
436 }
438 // FIXME: constructing a transform that would fully preserve the appearance of a
439 // textpath if it is ungrouped with its path seems to be impossible in general
440 // case. E.g. if the group was squeezed, to keep the ungrouped textpath squeezed
441 // as well, we'll need to relink it to some "virtual" path which is inversely
442 // stretched relative to the actual path, and then squeeze the textpath back so it
443 // would both fit the actual path _and_ be squeezed as before. It's a bummer.
445 // This is just a way to temporarily remember the transform in repr. When repr is
446 // reattached outside of the group, the transform will be written more properly
447 // (i.e. optimized into the object if the corresponding preference is set)
448 gchar *affinestr=sp_svg_transform_write(ctrans);
449 nrepr->setAttribute("transform", affinestr);
450 g_free(affinestr);
452 items = g_slist_prepend (items, nrepr);
454 } else {
455 Inkscape::XML::Node *nrepr = SP_OBJECT_REPR (child)->duplicate(prepr->document());
456 objects = g_slist_prepend (objects, nrepr);
457 }
458 }
460 /* Step 2 - clear group */
461 // remember the position of the group
462 gint pos = SP_OBJECT_REPR(group)->position();
464 // the group is leaving forever, no heir, clones should take note; its children however are going to reemerge
465 SP_OBJECT (group)->deleteObject(true, false);
467 /* Step 3 - add nonitems */
468 if (objects) {
469 Inkscape::XML::Node *last_def = SP_OBJECT_REPR(defs)->lastChild();
470 while (objects) {
471 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) objects->data;
472 if (!sp_repr_is_meta_element(repr))
473 SP_OBJECT_REPR(defs)->addChild(repr, last_def);
474 Inkscape::GC::release(repr);
475 objects = g_slist_remove (objects, objects->data);
476 }
477 }
479 /* Step 4 - add items */
480 while (items) {
481 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) items->data;
482 // add item
483 prepr->appendChild(repr);
484 // restore position; since the items list was prepended (i.e. reverse), we now add
485 // all children at the same pos, which inverts the order once again
486 repr->setPosition(pos > 0 ? pos : 0);
488 // fill in the children list if non-null
489 SPItem *item = (SPItem *) doc->getObjectByRepr(repr);
491 item->doWriteTransform(repr, item->transform, NULL, false);
493 Inkscape::GC::release(repr);
494 if (children && SP_IS_ITEM (item))
495 *children = g_slist_prepend (*children, item);
497 items = g_slist_remove (items, items->data);
498 }
500 if (do_done) {
501 DocumentUndo::done(doc, SP_VERB_NONE, _("Ungroup"));
502 }
503 }
505 /*
506 * some API for list aspect of SPGroup
507 */
509 GSList *sp_item_group_item_list(SPGroup * group)
510 {
511 g_return_val_if_fail(group != NULL, NULL);
512 g_return_val_if_fail(SP_IS_GROUP(group), NULL);
514 GSList *s = NULL;
516 for (SPObject *o = group->firstChild() ; o ; o = o->getNext() ) {
517 if ( SP_IS_ITEM(o) ) {
518 s = g_slist_prepend(s, o);
519 }
520 }
522 return g_slist_reverse (s);
523 }
525 SPObject *sp_item_group_get_child_by_name(SPGroup *group, SPObject *ref, const gchar *name)
526 {
527 SPObject *child = (ref) ? ref->getNext() : group->firstChild();
528 while ( child && strcmp(child->getRepr()->name(), name) ) {
529 child = child->getNext();
530 }
531 return child;
532 }
534 void SPGroup::setLayerMode(LayerMode mode) {
535 if ( _layer_mode != mode ) {
536 if ( mode == LAYER ) {
537 SP_OBJECT_DOCUMENT(this)->addResource("layer", this);
538 } else if ( _layer_mode == LAYER ) {
539 SP_OBJECT_DOCUMENT(this)->removeResource("layer", this);
540 }
541 _layer_mode = mode;
542 _updateLayerMode();
543 }
544 }
546 SPGroup::LayerMode SPGroup::layerDisplayMode(unsigned int dkey) const {
547 std::map<unsigned int, LayerMode>::const_iterator iter;
548 iter = _display_modes.find(dkey);
549 if ( iter != _display_modes.end() ) {
550 return (*iter).second;
551 } else {
552 return GROUP;
553 }
554 }
556 void SPGroup::setLayerDisplayMode(unsigned int dkey, SPGroup::LayerMode mode) {
557 if ( layerDisplayMode(dkey) != mode ) {
558 _display_modes[dkey] = mode;
559 _updateLayerMode(dkey);
560 }
561 }
563 void SPGroup::_updateLayerMode(unsigned int display_key) {
564 SPItemView *view;
565 for ( view = this->display ; view ; view = view->next ) {
566 if ( !display_key || view->key == display_key ) {
567 NRArenaGroup *arena_group=NR_ARENA_GROUP(view->arenaitem);
568 if (arena_group) {
569 nr_arena_group_set_transparent(arena_group, effectiveLayerMode(view->key) == SPGroup::LAYER);
570 }
571 }
572 }
573 }
575 void SPGroup::translateChildItems(Geom::Translate const &tr)
576 {
577 if ( hasChildren() ) {
578 for (SPObject *o = firstChild() ; o ; o = o->getNext() ) {
579 if ( SP_IS_ITEM(o) ) {
580 sp_item_move_rel(reinterpret_cast<SPItem *>(o), tr);
581 }
582 }
583 }
584 }
586 CGroup::CGroup(SPGroup *group) {
587 _group = group;
588 }
590 CGroup::~CGroup() {
591 }
593 void CGroup::onChildAdded(Inkscape::XML::Node *child) {
594 SPObject *last_child = _group->lastChild();
595 if (last_child && SP_OBJECT_REPR(last_child) == child) {
596 // optimization for the common special case where the child is being added at the end
597 SPObject *ochild = last_child;
598 if ( SP_IS_ITEM(ochild) ) {
599 /* TODO: this should be moved into SPItem somehow */
600 SPItemView *v;
601 NRArenaItem *ac;
603 for (v = _group->display; v != NULL; v = v->next) {
604 ac = SP_ITEM (ochild)->invoke_show (NR_ARENA_ITEM_ARENA (v->arenaitem), v->key, v->flags);
606 if (ac) {
607 nr_arena_item_append_child (v->arenaitem, ac);
608 }
609 }
610 }
611 } else { // general case
612 SPObject *ochild = _group->get_child_by_repr(child);
613 if ( ochild && SP_IS_ITEM(ochild) ) {
614 /* TODO: this should be moved into SPItem somehow */
615 SPItemView *v;
616 NRArenaItem *ac;
618 unsigned position = SP_ITEM(ochild)->pos_in_parent();
620 for (v = _group->display; v != NULL; v = v->next) {
621 ac = SP_ITEM (ochild)->invoke_show (NR_ARENA_ITEM_ARENA (v->arenaitem), v->key, v->flags);
623 if (ac) {
624 nr_arena_item_add_child (v->arenaitem, ac, NULL);
625 nr_arena_item_set_order (ac, position);
626 }
627 }
628 }
629 }
631 _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
632 }
634 void CGroup::onChildRemoved(Inkscape::XML::Node */*child*/) {
635 _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
636 }
638 void CGroup::onUpdate(SPCtx *ctx, unsigned int flags) {
639 SPItemCtx *ictx, cctx;
641 ictx = (SPItemCtx *) ctx;
642 cctx = *ictx;
644 if (flags & SP_OBJECT_MODIFIED_FLAG) {
645 flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
646 }
648 flags &= SP_OBJECT_MODIFIED_CASCADE;
650 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
651 SPObject *object = SP_OBJECT(_group);
652 for (SPItemView *v = SP_ITEM(_group)->display; v != NULL; v = v->next) {
653 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
654 }
655 }
657 GSList *l = g_slist_reverse(_group->childList(true, SPObject::ActionUpdate));
658 while (l) {
659 SPObject *child = SP_OBJECT (l->data);
660 l = g_slist_remove (l, child);
661 if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
662 if (SP_IS_ITEM (child)) {
663 SPItem const &chi = *SP_ITEM(child);
664 cctx.i2doc = chi.transform * ictx->i2doc;
665 cctx.i2vp = chi.transform * ictx->i2vp;
666 child->updateDisplay((SPCtx *)&cctx, flags);
667 } else {
668 child->updateDisplay(ctx, flags);
669 }
670 }
671 g_object_unref (G_OBJECT (child));
672 }
673 }
675 void CGroup::onModified(guint flags) {
676 SPObject *child;
678 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
679 flags &= SP_OBJECT_MODIFIED_CASCADE;
681 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
682 SPObject *object = SP_OBJECT(_group);
683 for (SPItemView *v = SP_ITEM(_group)->display; v != NULL; v = v->next) {
684 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
685 }
686 }
688 GSList *l = g_slist_reverse(_group->childList(true));
689 while (l) {
690 child = SP_OBJECT (l->data);
691 l = g_slist_remove (l, child);
692 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
693 child->emitModified(flags);
694 }
695 g_object_unref (G_OBJECT (child));
696 }
697 }
699 void CGroup::calculateBBox(NRRect *bbox, Geom::Matrix const &transform, unsigned const flags) {
701 Geom::OptRect dummy_bbox;
703 GSList *l = _group->childList(false, SPObject::ActionBBox);
704 while (l) {
705 SPObject *o = SP_OBJECT (l->data);
706 if (SP_IS_ITEM(o) && !SP_ITEM(o)->isHidden()) {
707 SPItem *child = SP_ITEM(o);
708 Geom::Matrix const ct(to_2geom(child->transform) * transform);
709 child->invoke_bbox_full( dummy_bbox, ct, flags, FALSE);
710 }
711 l = g_slist_remove (l, o);
712 }
714 *bbox = NRRect(dummy_bbox);
715 }
717 void CGroup::onPrint(SPPrintContext *ctx) {
718 GSList *l = g_slist_reverse(_group->childList(false));
719 while (l) {
720 SPObject *o = SP_OBJECT (l->data);
721 if (SP_IS_ITEM(o)) {
722 SP_ITEM(o)->invoke_print (ctx);
723 }
724 l = g_slist_remove (l, o);
725 }
726 }
728 gint CGroup::getItemCount() {
729 gint len = 0;
730 for (SPObject *o = _group->firstChild() ; o ; o = o->getNext() ) {
731 if (SP_IS_ITEM(o)) {
732 len++;
733 }
734 }
736 return len;
737 }
739 gchar *CGroup::getDescription() {
740 gint len = getItemCount();
741 return g_strdup_printf(
742 ngettext("<b>Group</b> of <b>%d</b> object",
743 "<b>Group</b> of <b>%d</b> objects",
744 len), len);
745 }
747 NRArenaItem *CGroup::show (NRArena *arena, unsigned int key, unsigned int flags) {
748 NRArenaItem *ai;
749 SPObject *object = SP_OBJECT(_group);
751 ai = NRArenaGroup::create(arena);
753 nr_arena_group_set_transparent(NR_ARENA_GROUP (ai),
754 _group->effectiveLayerMode(key) ==
755 SPGroup::LAYER);
756 nr_arena_group_set_style(NR_ARENA_GROUP(ai), SP_OBJECT_STYLE(object));
758 _showChildren(arena, ai, key, flags);
759 return ai;
760 }
762 void CGroup::_showChildren (NRArena *arena, NRArenaItem *ai, unsigned int key, unsigned int flags) {
763 NRArenaItem *ac = NULL;
764 NRArenaItem *ar = NULL;
765 SPItem * child = NULL;
766 GSList *l = g_slist_reverse(_group->childList(false, SPObject::ActionShow));
767 while (l) {
768 SPObject *o = SP_OBJECT (l->data);
769 if (SP_IS_ITEM (o)) {
770 child = SP_ITEM (o);
771 ac = child->invoke_show (arena, key, flags);
772 if (ac) {
773 nr_arena_item_add_child (ai, ac, ar);
774 ar = ac;
775 }
776 }
777 l = g_slist_remove (l, o);
778 }
779 }
781 void CGroup::hide (unsigned int key) {
782 SPItem * child;
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 child->invoke_hide (key);
790 }
791 l = g_slist_remove (l, o);
792 }
794 if (((SPItemClass *) parent_class)->hide)
795 ((SPItemClass *) parent_class)->hide (_group, key);
796 }
798 void CGroup::onOrderChanged (Inkscape::XML::Node *child, Inkscape::XML::Node *, Inkscape::XML::Node *)
799 {
800 SPObject *ochild = _group->get_child_by_repr(child);
801 if ( ochild && SP_IS_ITEM(ochild) ) {
802 /* TODO: this should be moved into SPItem somehow */
803 SPItemView *v;
804 unsigned position = SP_ITEM(ochild)->pos_in_parent();
805 for ( v = SP_ITEM (ochild)->display ; v != NULL ; v = v->next ) {
806 nr_arena_item_set_order (v->arenaitem, position);
807 }
808 }
810 _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
811 }
813 static void
814 sp_group_update_patheffect (SPLPEItem *lpeitem, bool write)
815 {
816 #ifdef GROUP_VERBOSE
817 g_message("sp_group_update_patheffect: %p\n", lpeitem);
818 #endif
819 g_return_if_fail (lpeitem != NULL);
820 g_return_if_fail (SP_IS_GROUP (lpeitem));
822 GSList const *item_list = sp_item_group_item_list(SP_GROUP(lpeitem));
823 for ( GSList const *iter = item_list; iter; iter = iter->next ) {
824 SPObject *subitem = static_cast<SPObject *>(iter->data);
825 if (SP_IS_LPE_ITEM(subitem)) {
826 if (SP_LPE_ITEM_CLASS (G_OBJECT_GET_CLASS (subitem))->update_patheffect) {
827 SP_LPE_ITEM_CLASS (G_OBJECT_GET_CLASS (subitem))->update_patheffect (SP_LPE_ITEM(subitem), write);
828 }
829 }
830 }
832 if (sp_lpe_item_has_path_effect(lpeitem) && sp_lpe_item_path_effects_enabled(lpeitem)) {
833 for (PathEffectList::iterator it = lpeitem->path_effect_list->begin(); it != lpeitem->path_effect_list->end(); it++)
834 {
835 LivePathEffectObject *lpeobj = (*it)->lpeobject;
836 if (lpeobj && lpeobj->get_lpe()) {
837 lpeobj->get_lpe()->doBeforeEffect(lpeitem);
838 }
839 }
841 sp_group_perform_patheffect(SP_GROUP(lpeitem), SP_GROUP(lpeitem), write);
842 }
843 }
845 static void
846 sp_group_perform_patheffect(SPGroup *group, SPGroup *topgroup, bool write)
847 {
848 GSList const *item_list = sp_item_group_item_list(SP_GROUP(group));
849 for ( GSList const *iter = item_list; iter; iter = iter->next ) {
850 SPObject *subitem = static_cast<SPObject *>(iter->data);
851 if (SP_IS_GROUP(subitem)) {
852 sp_group_perform_patheffect(SP_GROUP(subitem), topgroup, write);
853 } else if (SP_IS_SHAPE(subitem)) {
854 SPCurve * c = NULL;
855 if (SP_IS_PATH(subitem)) {
856 c = sp_path_get_original_curve(SP_PATH(subitem));
857 } else {
858 c = SP_SHAPE(subitem)->getCurve();
859 }
860 // only run LPEs when the shape has a curve defined
861 if (c) {
862 sp_lpe_item_perform_path_effect(SP_LPE_ITEM(topgroup), c);
863 SP_SHAPE(subitem)->setCurve(c, TRUE);
865 if (write) {
866 Inkscape::XML::Node *repr = SP_OBJECT_REPR(subitem);
867 gchar *str = sp_svg_write_path(c->get_pathvector());
868 repr->setAttribute("d", str);
869 #ifdef GROUP_VERBOSE
870 g_message("sp_group_perform_patheffect writes 'd' attribute");
871 #endif
872 g_free(str);
873 }
875 c->unref();
876 }
877 }
878 }
879 }
881 /*
882 Local Variables:
883 mode:c++
884 c-file-style:"stroustrup"
885 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
886 indent-tabs-mode:nil
887 fill-column:99
888 End:
889 */
890 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :