38a9cbced2e65c6d634102568454a1aa3b94c76b
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 "prefs-utils.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, NR::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 NR::Matrix sp_group_set_transform(SPItem *item, NR::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, SnapPointsIter p);
74 static void sp_group_update_patheffect(SPLPEItem *lpeitem, bool write);
75 static void sp_group_perform_patheffect(SPGroup *group, SPGroup *topgroup);
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, NR::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 NR::Matrix
294 sp_group_set_transform(SPItem *item, NR::Matrix const &xform)
295 {
296 Inkscape::Selection *selection = sp_desktop_selection(inkscape_active_desktop());
297 persp3d_split_perspectives_according_to_selection(selection);
299 NR::Matrix last_trans;
300 sp_svg_transform_read(SP_OBJECT_REPR(item)->attribute("transform"), &last_trans);
301 NR::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, SnapPointsIter p)
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), false, p);
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, NR::identity(), NR::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, NR::identity(), NR::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 NR::Matrix ctrans;
426 NR::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 ctrans = citem->transform * g;
433 }
435 // FIXME: constructing a transform that would fully preserve the appearance of a
436 // textpath if it is ungrouped with its path seems to be impossible in general
437 // case. E.g. if the group was squeezed, to keep the ungrouped textpath squeezed
438 // as well, we'll need to relink it to some "virtual" path which is inversely
439 // stretched relative to the actual path, and then squeeze the textpath back so it
440 // would both fit the actual path _and_ be squeezed as before. It's a bummer.
442 // This is just a way to temporarily remember the transform in repr. When repr is
443 // reattached outside of the group, the transform will be written more properly
444 // (i.e. optimized into the object if the corresponding preference is set)
445 gchar *affinestr=sp_svg_transform_write(ctrans);
446 nrepr->setAttribute("transform", affinestr);
447 g_free(affinestr);
449 items = g_slist_prepend (items, nrepr);
451 } else {
452 Inkscape::XML::Node *nrepr = SP_OBJECT_REPR (child)->duplicate(prepr->document());
453 objects = g_slist_prepend (objects, nrepr);
454 }
455 }
457 /* Step 2 - clear group */
458 // remember the position of the group
459 gint pos = SP_OBJECT_REPR(group)->position();
461 // the group is leaving forever, no heir, clones should take note; its children however are going to reemerge
462 SP_OBJECT (group)->deleteObject(true, false);
464 /* Step 3 - add nonitems */
465 if (objects) {
466 Inkscape::XML::Node *last_def = SP_OBJECT_REPR(defs)->lastChild();
467 while (objects) {
468 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) objects->data;
469 if (!sp_repr_is_meta_element(repr))
470 SP_OBJECT_REPR(defs)->addChild(repr, last_def);
471 Inkscape::GC::release(repr);
472 objects = g_slist_remove (objects, objects->data);
473 }
474 }
476 /* Step 4 - add items */
477 while (items) {
478 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) items->data;
479 // add item
480 prepr->appendChild(repr);
481 // restore position; since the items list was prepended (i.e. reverse), we now add
482 // all children at the same pos, which inverts the order once again
483 repr->setPosition(pos > 0 ? pos : 0);
485 // fill in the children list if non-null
486 SPItem *item = (SPItem *) doc->getObjectByRepr(repr);
488 sp_item_write_transform(item, repr, item->transform, NULL, false);
490 Inkscape::GC::release(repr);
491 if (children && SP_IS_ITEM (item))
492 *children = g_slist_prepend (*children, item);
494 items = g_slist_remove (items, items->data);
495 }
497 if (do_done)
498 sp_document_done (doc, SP_VERB_NONE, _("Ungroup"));
499 }
501 /*
502 * some API for list aspect of SPGroup
503 */
505 GSList *
506 sp_item_group_item_list (SPGroup * group)
507 {
508 GSList *s;
509 SPObject *o;
511 g_return_val_if_fail (group != NULL, NULL);
512 g_return_val_if_fail (SP_IS_GROUP (group), NULL);
514 s = NULL;
516 for ( o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
517 if (SP_IS_ITEM (o)) {
518 s = g_slist_prepend (s, o);
519 }
520 }
522 return g_slist_reverse (s);
523 }
525 SPObject *
526 sp_item_group_get_child_by_name (SPGroup *group, SPObject *ref, const gchar *name)
527 {
528 SPObject *child;
529 child = (ref) ? SP_OBJECT_NEXT(ref) : sp_object_first_child(SP_OBJECT(group));
530 while ( child && strcmp (SP_OBJECT_REPR(child)->name(), name) ) {
531 child = SP_OBJECT_NEXT(child);
532 }
533 return child;
534 }
536 void SPGroup::setLayerMode(LayerMode mode) {
537 if ( _layer_mode != mode ) {
538 if ( mode == LAYER ) {
539 sp_document_add_resource(SP_OBJECT_DOCUMENT(this), "layer", this);
540 } else {
541 sp_document_remove_resource(SP_OBJECT_DOCUMENT(this), "layer", this);
542 }
543 _layer_mode = mode;
544 _updateLayerMode();
545 }
546 }
548 SPGroup::LayerMode SPGroup::layerDisplayMode(unsigned int dkey) const {
549 std::map<unsigned int, LayerMode>::const_iterator iter;
550 iter = _display_modes.find(dkey);
551 if ( iter != _display_modes.end() ) {
552 return (*iter).second;
553 } else {
554 return GROUP;
555 }
556 }
558 void SPGroup::setLayerDisplayMode(unsigned int dkey, SPGroup::LayerMode mode) {
559 if ( layerDisplayMode(dkey) != mode ) {
560 _display_modes[dkey] = mode;
561 _updateLayerMode(dkey);
562 }
563 }
565 void SPGroup::_updateLayerMode(unsigned int display_key) {
566 SPItemView *view;
567 for ( view = this->display ; view ; view = view->next ) {
568 if ( !display_key || view->key == display_key ) {
569 NRArenaGroup *arena_group=NR_ARENA_GROUP(view->arenaitem);
570 if (arena_group) {
571 nr_arena_group_set_transparent(arena_group, effectiveLayerMode(view->key) == SPGroup::LAYER);
572 }
573 }
574 }
575 }
577 void SPGroup::translateChildItems(NR::translate const &tr)
578 {
579 if (this->hasChildren())
580 {
581 SPObject *o = NULL;
582 for (o = sp_object_first_child(SP_OBJECT(this)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
583 if (SP_IS_ITEM (o)) {
584 sp_item_move_rel(static_cast<SPItem *>(o), tr);
585 }
586 }
587 }
588 }
590 CGroup::CGroup(SPGroup *group) {
591 _group = group;
592 }
594 CGroup::~CGroup() {
595 }
597 void CGroup::onChildAdded(Inkscape::XML::Node *child) {
598 SPObject *last_child = _group->lastChild();
599 if (last_child && SP_OBJECT_REPR(last_child) == child) {
600 // optimization for the common special case where the child is being added at the end
601 SPObject *ochild = last_child;
602 if ( SP_IS_ITEM(ochild) ) {
603 /* TODO: this should be moved into SPItem somehow */
604 SPItemView *v;
605 NRArenaItem *ac;
607 for (v = _group->display; v != NULL; v = v->next) {
608 ac = sp_item_invoke_show (SP_ITEM (ochild), NR_ARENA_ITEM_ARENA (v->arenaitem), v->key, v->flags);
610 if (ac) {
611 nr_arena_item_append_child (v->arenaitem, ac);
612 }
613 }
614 }
615 } else { // general case
616 SPObject *ochild = sp_object_get_child_by_repr(_group, child);
617 if ( ochild && SP_IS_ITEM(ochild) ) {
618 /* TODO: this should be moved into SPItem somehow */
619 SPItemView *v;
620 NRArenaItem *ac;
622 unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
624 for (v = _group->display; v != NULL; v = v->next) {
625 ac = sp_item_invoke_show (SP_ITEM (ochild), NR_ARENA_ITEM_ARENA (v->arenaitem), v->key, v->flags);
627 if (ac) {
628 nr_arena_item_add_child (v->arenaitem, ac, NULL);
629 nr_arena_item_set_order (ac, position);
630 }
631 }
632 }
633 }
635 _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
636 }
638 void CGroup::onChildRemoved(Inkscape::XML::Node */*child*/) {
639 _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
640 }
642 void CGroup::onUpdate(SPCtx *ctx, unsigned int flags) {
643 SPItemCtx *ictx, cctx;
645 ictx = (SPItemCtx *) ctx;
646 cctx = *ictx;
648 if (flags & SP_OBJECT_MODIFIED_FLAG) {
649 flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
650 }
652 flags &= SP_OBJECT_MODIFIED_CASCADE;
654 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
655 SPObject *object = SP_OBJECT(_group);
656 for (SPItemView *v = SP_ITEM(_group)->display; v != NULL; v = v->next) {
657 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
658 }
659 }
661 GSList *l = g_slist_reverse(_group->childList(true, SPObject::ActionUpdate));
662 while (l) {
663 SPObject *child = SP_OBJECT (l->data);
664 l = g_slist_remove (l, child);
665 if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
666 if (SP_IS_ITEM (child)) {
667 SPItem const &chi = *SP_ITEM(child);
668 cctx.i2doc = chi.transform * ictx->i2doc;
669 cctx.i2vp = chi.transform * ictx->i2vp;
670 child->updateDisplay((SPCtx *)&cctx, flags);
671 } else {
672 child->updateDisplay(ctx, flags);
673 }
674 }
675 g_object_unref (G_OBJECT (child));
676 }
677 }
679 void CGroup::onModified(guint flags) {
680 SPObject *child;
682 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
683 flags &= SP_OBJECT_MODIFIED_CASCADE;
685 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
686 SPObject *object = SP_OBJECT(_group);
687 for (SPItemView *v = SP_ITEM(_group)->display; v != NULL; v = v->next) {
688 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
689 }
690 }
692 GSList *l = g_slist_reverse(_group->childList(true));
693 while (l) {
694 child = SP_OBJECT (l->data);
695 l = g_slist_remove (l, child);
696 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
697 child->emitModified(flags);
698 }
699 g_object_unref (G_OBJECT (child));
700 }
701 }
703 void CGroup::calculateBBox(NRRect *bbox, NR::Matrix const &transform, unsigned const flags) {
705 boost::optional<NR::Rect> dummy_bbox;
707 GSList *l = _group->childList(false, SPObject::ActionBBox);
708 while (l) {
709 SPObject *o = SP_OBJECT (l->data);
710 if (SP_IS_ITEM(o) && !SP_ITEM(o)->isHidden()) {
711 SPItem *child = SP_ITEM(o);
712 NR::Matrix const ct(child->transform * transform);
713 sp_item_invoke_bbox_full(child, dummy_bbox, ct, flags, FALSE);
714 }
715 l = g_slist_remove (l, o);
716 }
718 *bbox = NRRect(dummy_bbox);
719 }
721 void CGroup::onPrint(SPPrintContext *ctx) {
722 GSList *l = g_slist_reverse(_group->childList(false));
723 while (l) {
724 SPObject *o = SP_OBJECT (l->data);
725 if (SP_IS_ITEM(o)) {
726 sp_item_invoke_print (SP_ITEM (o), ctx);
727 }
728 l = g_slist_remove (l, o);
729 }
730 }
732 gint CGroup::getItemCount() {
733 gint len = 0;
734 for (SPObject *o = sp_object_first_child(SP_OBJECT(_group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
735 if (SP_IS_ITEM(o)) {
736 len++;
737 }
738 }
740 return len;
741 }
743 gchar *CGroup::getDescription() {
744 gint len = getItemCount();
745 return g_strdup_printf(
746 ngettext("<b>Group</b> of <b>%d</b> object",
747 "<b>Group</b> of <b>%d</b> objects",
748 len), len);
749 }
751 NRArenaItem *CGroup::show (NRArena *arena, unsigned int key, unsigned int flags) {
752 NRArenaItem *ai;
753 SPObject *object = SP_OBJECT(_group);
755 ai = NRArenaGroup::create(arena);
757 nr_arena_group_set_transparent(NR_ARENA_GROUP (ai),
758 _group->effectiveLayerMode(key) ==
759 SPGroup::LAYER);
760 nr_arena_group_set_style(NR_ARENA_GROUP(ai), SP_OBJECT_STYLE(object));
762 _showChildren(arena, ai, key, flags);
763 return ai;
764 }
766 void CGroup::_showChildren (NRArena *arena, NRArenaItem *ai, unsigned int key, unsigned int flags) {
767 NRArenaItem *ac = NULL;
768 NRArenaItem *ar = NULL;
769 SPItem * child = NULL;
770 GSList *l = g_slist_reverse(_group->childList(false, SPObject::ActionShow));
771 while (l) {
772 SPObject *o = SP_OBJECT (l->data);
773 if (SP_IS_ITEM (o)) {
774 child = SP_ITEM (o);
775 ac = sp_item_invoke_show (child, arena, key, flags);
776 if (ac) {
777 nr_arena_item_add_child (ai, ac, ar);
778 ar = ac;
779 }
780 }
781 l = g_slist_remove (l, o);
782 }
783 }
785 void CGroup::hide (unsigned int key) {
786 SPItem * child;
788 GSList *l = g_slist_reverse(_group->childList(false, SPObject::ActionShow));
789 while (l) {
790 SPObject *o = SP_OBJECT (l->data);
791 if (SP_IS_ITEM (o)) {
792 child = SP_ITEM (o);
793 sp_item_invoke_hide (child, key);
794 }
795 l = g_slist_remove (l, o);
796 }
798 if (((SPItemClass *) parent_class)->hide)
799 ((SPItemClass *) parent_class)->hide (_group, key);
800 }
802 void CGroup::onOrderChanged (Inkscape::XML::Node *child, Inkscape::XML::Node *, Inkscape::XML::Node *)
803 {
804 SPObject *ochild = sp_object_get_child_by_repr(_group, child);
805 if ( ochild && SP_IS_ITEM(ochild) ) {
806 /* TODO: this should be moved into SPItem somehow */
807 SPItemView *v;
808 unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
809 for ( v = SP_ITEM (ochild)->display ; v != NULL ; v = v->next ) {
810 nr_arena_item_set_order (v->arenaitem, position);
811 }
812 }
814 _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
815 }
817 static void
818 sp_group_update_patheffect (SPLPEItem *lpeitem, bool write)
819 {
820 #ifdef GROUP_VERBOSE
821 g_message("sp_group_update_patheffect: %p\n", lpeitem);
822 #endif
823 g_return_if_fail (lpeitem != NULL);
824 g_return_if_fail (SP_IS_GROUP (lpeitem));
826 GSList const *item_list = sp_item_group_item_list(SP_GROUP(lpeitem));
827 for ( GSList const *iter = item_list; iter; iter = iter->next ) {
828 SPObject *subitem = static_cast<SPObject *>(iter->data);
829 if (SP_IS_LPE_ITEM(subitem)) {
830 if (SP_LPE_ITEM_CLASS (G_OBJECT_GET_CLASS (subitem))->update_patheffect) {
831 SP_LPE_ITEM_CLASS (G_OBJECT_GET_CLASS (subitem))->update_patheffect (SP_LPE_ITEM(subitem), write);
832 }
833 }
834 }
836 if (sp_lpe_item_has_path_effect(lpeitem) && sp_lpe_item_path_effects_enabled(lpeitem)) {
837 for (PathEffectList::iterator it = lpeitem->path_effect_list->begin(); it != lpeitem->path_effect_list->end(); it++)
838 {
839 LivePathEffectObject *lpeobj = (*it)->lpeobject;
840 lpeobj->lpe->doBeforeEffect(lpeitem);
841 }
843 sp_group_perform_patheffect(SP_GROUP(lpeitem), SP_GROUP(lpeitem));
844 }
845 }
847 static void
848 sp_group_perform_patheffect(SPGroup *group, SPGroup *topgroup)
849 {
850 GSList const *item_list = sp_item_group_item_list(SP_GROUP(group));
851 for ( GSList const *iter = item_list; iter; iter = iter->next ) {
852 SPObject *subitem = static_cast<SPObject *>(iter->data);
853 if (SP_IS_GROUP(subitem)) {
854 sp_group_perform_patheffect(SP_GROUP(subitem), topgroup);
855 } else if (SP_IS_SHAPE(subitem)) {
856 SPCurve * c = sp_shape_get_curve(SP_SHAPE(subitem));
857 sp_lpe_item_perform_path_effect(SP_LPE_ITEM(topgroup), c);
858 sp_shape_set_curve(SP_SHAPE(subitem), c, TRUE);
860 Inkscape::XML::Node *repr = SP_OBJECT_REPR(subitem);
862 gchar *str = sp_svg_write_path(c->get_pathvector());
863 repr->setAttribute("d", str);
864 g_free(str);
866 c->unref();
867 }
868 }
869 }
871 /*
872 Local Variables:
873 mode:c++
874 c-file-style:"stroustrup"
875 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
876 indent-tabs-mode:nil
877 fill-column:99
878 End:
879 */
880 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :