Code

Pot and Dutch translation update
[inkscape.git] / src / sp-item-group.cpp
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/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 static void sp_group_class_init (SPGroupClass *klass);
54 static void sp_group_init (SPGroup *group);
55 static void sp_group_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
56 static void sp_group_release(SPObject *object);
57 static void sp_group_dispose(GObject *object);
59 static void sp_group_child_added (SPObject * object, Inkscape::XML::Node * child, Inkscape::XML::Node * ref);
60 static void sp_group_remove_child (SPObject * object, Inkscape::XML::Node * child);
61 static void sp_group_order_changed (SPObject * object, Inkscape::XML::Node * child, Inkscape::XML::Node * old_ref, Inkscape::XML::Node * new_ref);
62 static void sp_group_update (SPObject *object, SPCtx *ctx, guint flags);
63 static void sp_group_modified (SPObject *object, guint flags);
64 static Inkscape::XML::Node *sp_group_write (SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
65 static void sp_group_set(SPObject *object, unsigned key, char const *value);
67 static void sp_group_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags);
68 static void sp_group_print (SPItem * item, SPPrintContext *ctx);
69 static gchar * sp_group_description (SPItem * item);
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, std::vector<Inkscape::SnapCandidatePoint> &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)
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->show = sp_group_show;
132     item_class->hide = sp_group_hide;
133     item_class->snappoints = sp_group_snappoints;
135     lpe_item_class->update_patheffect = sp_group_update_patheffect;
138 static void
139 sp_group_init (SPGroup *group)
141     group->_layer_mode = SPGroup::GROUP;
142     group->group = new CGroup(group);
143     new (&group->_display_modes) std::map<unsigned int, SPGroup::LayerMode>();
146 static void sp_group_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
148     sp_object_read_attr(object, "inkscape:groupmode");
150     if (((SPObjectClass *)parent_class)->build) {
151         ((SPObjectClass *)parent_class)->build(object, document, repr);
152     }
155 static void sp_group_release(SPObject *object) {
156     if ( SP_GROUP(object)->_layer_mode == SPGroup::LAYER ) {
157         sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "layer", object);
158     }
159     if (((SPObjectClass *)parent_class)->release) {
160         ((SPObjectClass *)parent_class)->release(object);
161     }
164 static void
165 sp_group_dispose(GObject *object)
167     SP_GROUP(object)->_display_modes.~map();
168     delete SP_GROUP(object)->group;
171 static void
172 sp_group_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
174     SPItem *item;
176     item = SP_ITEM (object);
178     if (((SPObjectClass *) (parent_class))->child_added)
179         (* ((SPObjectClass *) (parent_class))->child_added) (object, child, ref);
181     SP_GROUP(object)->group->onChildAdded(child);
184 /* fixme: hide (Lauris) */
186 static void
187 sp_group_remove_child (SPObject * object, Inkscape::XML::Node * child)
189     if (((SPObjectClass *) (parent_class))->remove_child)
190         (* ((SPObjectClass *) (parent_class))->remove_child) (object, child);
192     SP_GROUP(object)->group->onChildRemoved(child);
195 static void
196 sp_group_order_changed (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref)
198     if (((SPObjectClass *) (parent_class))->order_changed)
199         (* ((SPObjectClass *) (parent_class))->order_changed) (object, child, old_ref, new_ref);
201     SP_GROUP(object)->group->onOrderChanged(child, old_ref, new_ref);
204 static void
205 sp_group_update (SPObject *object, SPCtx *ctx, unsigned int flags)
207     if (((SPObjectClass *) (parent_class))->update)
208         ((SPObjectClass *) (parent_class))->update (object, ctx, flags);
210     SP_GROUP(object)->group->onUpdate(ctx, flags);
213 static void
214 sp_group_modified (SPObject *object, guint flags)
216     if (((SPObjectClass *) (parent_class))->modified)
217         ((SPObjectClass *) (parent_class))->modified (object, flags);
219     SP_GROUP(object)->group->onModified(flags);
222 static Inkscape::XML::Node *
223 sp_group_write (SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
225     SPGroup *group;
226     SPObject *child;
227     Inkscape::XML::Node *crepr;
229     group = SP_GROUP (object);
231     if (flags & SP_OBJECT_WRITE_BUILD) {
232         GSList *l;
233         if (!repr) {
234             if (SP_IS_SWITCH(object))
235                 repr = xml_doc->createElement("svg:switch");
236             else
237                 repr = xml_doc->createElement("svg:g");
238         }
239         l = NULL;
240         for (child = sp_object_first_child(object); child != NULL; child = SP_OBJECT_NEXT(child) ) {
241             if (SP_IS_TITLE(child) || SP_IS_DESC(child)) continue;
242             crepr = child->updateRepr(xml_doc, NULL, flags);
243             if (crepr) l = g_slist_prepend (l, crepr);
244         }
245         while (l) {
246             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
247             Inkscape::GC::release((Inkscape::XML::Node *) l->data);
248             l = g_slist_remove (l, l->data);
249         }
250     } else {
251         for (child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
252             if (SP_IS_TITLE(child) || SP_IS_DESC(child)) continue;
253             child->updateRepr(flags);
254         }
255     }
257     if ( flags & SP_OBJECT_WRITE_EXT ) {
258         const char *value;
259         if ( group->_layer_mode == SPGroup::LAYER ) {
260             value = "layer";
261         } else if ( group->_layer_mode == SPGroup::MASK_HELPER ) {
262             value = "maskhelper";
263         } else if ( flags & SP_OBJECT_WRITE_ALL ) {
264             value = "group";
265         } else {
266             value = NULL;
267         }
268         repr->setAttribute("inkscape:groupmode", value);
269     }
271     if (((SPObjectClass *) (parent_class))->write)
272         ((SPObjectClass *) (parent_class))->write (object, xml_doc, repr, flags);
274     return repr;
277 static void
278 sp_group_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags)
280     SP_GROUP(item)->group->calculateBBox(bbox, transform, flags);
283 static void
284 sp_group_print (SPItem * item, SPPrintContext *ctx)
286     SP_GROUP(item)->group->onPrint(ctx);
289 static gchar * sp_group_description (SPItem * item)
291     return SP_GROUP(item)->group->getDescription();
294 static void sp_group_set(SPObject *object, unsigned key, char const *value) {
295     SPGroup *group = SP_GROUP(object);
297     switch (key) {
298         case SP_ATTR_INKSCAPE_GROUPMODE:
299             if ( value && !strcmp(value, "layer") ) {
300                 group->setLayerMode(SPGroup::LAYER);
301             } else if ( value && !strcmp(value, "maskhelper") ) {
302                 group->setLayerMode(SPGroup::MASK_HELPER);
303             } else {
304                 group->setLayerMode(SPGroup::GROUP);
305             }
306             break;
307         default: {
308             if (((SPObjectClass *) (parent_class))->set) {
309                 (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
310             }
311         }
312     }
315 static NRArenaItem *
316 sp_group_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags)
318     return SP_GROUP(item)->group->show(arena, key, flags);
321 static void
322 sp_group_hide (SPItem *item, unsigned int key)
324     SP_GROUP(item)->group->hide(key);
327 static void sp_group_snappoints (SPItem const *item, std::vector<Inkscape::SnapCandidatePoint> &p, Inkscape::SnapPreferences const *snapprefs)
329     for (SPObject const *o = sp_object_first_child(SP_OBJECT(item));
330          o != NULL;
331          o = SP_OBJECT_NEXT(o))
332     {
333         if (SP_IS_ITEM(o)) {
334             sp_item_snappoints(SP_ITEM(o), p, snapprefs);
335         }
336     }
340 void
341 sp_item_group_ungroup (SPGroup *group, GSList **children, bool do_done)
343     g_return_if_fail (group != NULL);
344     g_return_if_fail (SP_IS_GROUP (group));
346     SPDocument *doc = SP_OBJECT_DOCUMENT (group);
347     SPObject *root = SP_DOCUMENT_ROOT (doc);
348     SPObject *defs = SP_OBJECT (SP_ROOT (root)->defs);
350     SPItem *gitem = SP_ITEM (group);
351     Inkscape::XML::Node *grepr = SP_OBJECT_REPR (gitem);
353     g_return_if_fail (!strcmp (grepr->name(), "svg:g") || !strcmp (grepr->name(), "svg:a") || !strcmp (grepr->name(), "svg:switch"));
355     // this converts the gradient/pattern fill/stroke on the group, if any, to userSpaceOnUse
356     sp_item_adjust_paint_recursive (gitem, Geom::identity(), Geom::identity(), false);
358     SPItem *pitem = SP_ITEM (SP_OBJECT_PARENT (gitem));
359     Inkscape::XML::Node *prepr = SP_OBJECT_REPR (pitem);
361         if (SP_IS_BOX3D(gitem)) {
362                 group = box3d_convert_to_group(SP_BOX3D(gitem));
363                 gitem = SP_ITEM(group);
364         }
366         sp_lpe_item_remove_all_path_effects(SP_LPE_ITEM(group), false);
368     /* Step 1 - generate lists of children objects */
369     GSList *items = NULL;
370     GSList *objects = NULL;
371     for (SPObject *child = sp_object_first_child(SP_OBJECT(group)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
373         if (SP_IS_ITEM (child)) {
375             SPItem *citem = SP_ITEM (child);
377             /* Merging of style */
378             // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
379             // it here _before_ the new transform is set, so as to use the pre-transform bbox
380             sp_item_adjust_paint_recursive (citem, Geom::identity(), Geom::identity(), false);
382             sp_style_merge_from_dying_parent(SP_OBJECT_STYLE(child), SP_OBJECT_STYLE(gitem));
383             /*
384              * fixme: We currently make no allowance for the case where child is cloned
385              * and the group has any style settings.
386              *
387              * (This should never occur with documents created solely with the current
388              * version of inkscape without using the XML editor: we usually apply group
389              * style changes to children rather than to the group itself.)
390              *
391              * If the group has no style settings, then
392              * sp_style_merge_from_dying_parent should be a no-op.  Otherwise (i.e. if
393              * we change the child's style to compensate for its parent going away)
394              * then those changes will typically be reflected in any clones of child,
395              * whereas we'd prefer for Ungroup not to affect the visual appearance.
396              *
397              * The only way of preserving styling appearance in general is for child to
398              * be put into a new group -- a somewhat surprising response to an Ungroup
399              * command.  We could add a new groupmode:transparent that would mostly
400              * hide the existence of such groups from the user (i.e. editing behaves as
401              * if the transparent group's children weren't in a group), though that's
402              * extra complication & maintenance burden and this case is rare.
403              */
405             child->updateRepr();
407             Inkscape::XML::Node *nrepr = SP_OBJECT_REPR (child)->duplicate(prepr->document());
409             // Merging transform
410             Geom::Matrix ctrans;
411             Geom::Matrix const g(gitem->transform);
412             if (SP_IS_USE(citem) && sp_use_get_original (SP_USE(citem)) &&
413                     SP_OBJECT_PARENT (sp_use_get_original (SP_USE(citem))) == SP_OBJECT(group)) {
414                 // make sure a clone's effective transform is the same as was under group
415                 ctrans = g.inverse() * citem->transform * g;
416             } else {
417                 // We should not apply the group's transformation to both a linked offset AND to its source
418                 if (SP_IS_OFFSET(citem)) { // Do we have an offset at hand (whether it's dynamic or linked)?
419                         SPItem *source = sp_offset_get_source(SP_OFFSET(citem));
420                         // When dealing with a chain of linked offsets, the transformation of an offset will be
421                         // tied to the transformation of the top-most source, not to any of the intermediate
422                         // offsets. So let's find the top-most source
423                         while (source != NULL && SP_IS_OFFSET(source)) {
424                                 source = sp_offset_get_source(SP_OFFSET(source));
425                         }
426                         if (source != NULL && // If true then we must be dealing with a linked offset ...
427                                         SP_OBJECT(group)->isAncestorOf(SP_OBJECT(source)) == false) { // ... of which the source is not in the same group
428                                 ctrans = citem->transform * g; // then we should apply the transformation of the group to the offset
429                         } else {
430                                 ctrans = citem->transform;
431                         }
432                 } else {
433                         ctrans = citem->transform * g;
434                 }
435             }
437             // FIXME: constructing a transform that would fully preserve the appearance of a
438             // textpath if it is ungrouped with its path seems to be impossible in general
439             // case. E.g. if the group was squeezed, to keep the ungrouped textpath squeezed
440             // as well, we'll need to relink it to some "virtual" path which is inversely
441             // stretched relative to the actual path, and then squeeze the textpath back so it
442             // would both fit the actual path _and_ be squeezed as before. It's a bummer.
444             // This is just a way to temporarily remember the transform in repr. When repr is
445             // reattached outside of the group, the transform will be written more properly
446             // (i.e. optimized into the object if the corresponding preference is set)
447             gchar *affinestr=sp_svg_transform_write(ctrans);
448             nrepr->setAttribute("transform", affinestr);
449             g_free(affinestr);
451             items = g_slist_prepend (items, nrepr);
453         } else {
454             Inkscape::XML::Node *nrepr = SP_OBJECT_REPR (child)->duplicate(prepr->document());
455             objects = g_slist_prepend (objects, nrepr);
456         }
457     }
459     /* Step 2 - clear group */
460     // remember the position of the group
461     gint pos = SP_OBJECT_REPR(group)->position();
463     // the group is leaving forever, no heir, clones should take note; its children however are going to reemerge
464     SP_OBJECT (group)->deleteObject(true, false);
466     /* Step 3 - add nonitems */
467     if (objects) {
468         Inkscape::XML::Node *last_def = SP_OBJECT_REPR(defs)->lastChild();
469         while (objects) {
470                         Inkscape::XML::Node *repr = (Inkscape::XML::Node *) objects->data;
471                         if (!sp_repr_is_meta_element(repr))
472                                 SP_OBJECT_REPR(defs)->addChild(repr, last_def);
473                         Inkscape::GC::release(repr);
474                         objects = g_slist_remove (objects, objects->data);
475         }
476     }
478     /* Step 4 - add items */
479     while (items) {
480         Inkscape::XML::Node *repr = (Inkscape::XML::Node *) items->data;
481         // add item
482         prepr->appendChild(repr);
483         // restore position; since the items list was prepended (i.e. reverse), we now add
484         // all children at the same pos, which inverts the order once again
485         repr->setPosition(pos > 0 ? pos : 0);
487         // fill in the children list if non-null
488         SPItem *item = (SPItem *) doc->getObjectByRepr(repr);
490         sp_item_write_transform(item, repr, item->transform, NULL, false);
492         Inkscape::GC::release(repr);
493         if (children && SP_IS_ITEM (item))
494             *children = g_slist_prepend (*children, item);
496         items = g_slist_remove (items, items->data);
497     }
499     if (do_done)
500         sp_document_done (doc, SP_VERB_NONE, _("Ungroup"));
503 /*
504  * some API for list aspect of SPGroup
505  */
507 GSList *
508 sp_item_group_item_list (SPGroup * group)
510     g_return_val_if_fail (group != NULL, NULL);
511     g_return_val_if_fail (SP_IS_GROUP (group), NULL);
513     GSList *s = NULL;
515     for (SPObject *o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
516         if (SP_IS_ITEM (o)) {
517             s = g_slist_prepend (s, o);
518         }
519     }
521     return g_slist_reverse (s);
524 SPObject *
525 sp_item_group_get_child_by_name (SPGroup *group, SPObject *ref, const gchar *name)
527     SPObject *child;
528     child = (ref) ? SP_OBJECT_NEXT(ref) : sp_object_first_child(SP_OBJECT(group));
529     while ( child && strcmp (SP_OBJECT_REPR(child)->name(), name) ) {
530         child = SP_OBJECT_NEXT(child);
531     }
532     return child;
535 void SPGroup::setLayerMode(LayerMode mode) {
536     if ( _layer_mode != mode ) {
537         if ( mode == LAYER ) {
538             sp_document_add_resource(SP_OBJECT_DOCUMENT(this), "layer", this);
539         } else if ( _layer_mode == LAYER ) {
540             sp_document_remove_resource(SP_OBJECT_DOCUMENT(this), "layer", this);
541         }
542         _layer_mode = mode;
543         _updateLayerMode();
544     }
547 SPGroup::LayerMode SPGroup::layerDisplayMode(unsigned int dkey) const {
548     std::map<unsigned int, LayerMode>::const_iterator iter;
549     iter = _display_modes.find(dkey);
550     if ( iter != _display_modes.end() ) {
551         return (*iter).second;
552     } else {
553         return GROUP;
554     }
557 void SPGroup::setLayerDisplayMode(unsigned int dkey, SPGroup::LayerMode mode) {
558     if ( layerDisplayMode(dkey) != mode ) {
559         _display_modes[dkey] = mode;
560         _updateLayerMode(dkey);
561     }
564 void SPGroup::_updateLayerMode(unsigned int display_key) {
565     SPItemView *view;
566     for ( view = this->display ; view ; view = view->next ) {
567         if ( !display_key || view->key == display_key ) {
568             NRArenaGroup *arena_group=NR_ARENA_GROUP(view->arenaitem);
569             if (arena_group) {
570                 nr_arena_group_set_transparent(arena_group, effectiveLayerMode(view->key) == SPGroup::LAYER);
571             }
572         }
573     }
576 void SPGroup::translateChildItems(Geom::Translate const &tr)
578     if (this->hasChildren())
579     {
580         SPObject *o = NULL;
581         for (o = sp_object_first_child(SP_OBJECT(this)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
582             if (SP_IS_ITEM (o)) {
583                 sp_item_move_rel(static_cast<SPItem *>(o), tr);
584             }
585         }
586     }
589 CGroup::CGroup(SPGroup *group) {
590     _group = group;
593 CGroup::~CGroup() {
596 void CGroup::onChildAdded(Inkscape::XML::Node *child) {
597     SPObject *last_child = _group->lastChild();
598     if (last_child && SP_OBJECT_REPR(last_child) == child) {
599         // optimization for the common special case where the child is being added at the end
600         SPObject *ochild = last_child;
601         if ( SP_IS_ITEM(ochild) ) {
602             /* TODO: this should be moved into SPItem somehow */
603             SPItemView *v;
604             NRArenaItem *ac;
606             for (v = _group->display; v != NULL; v = v->next) {
607                 ac = sp_item_invoke_show (SP_ITEM (ochild), NR_ARENA_ITEM_ARENA (v->arenaitem), v->key, v->flags);
609                 if (ac) {
610                     nr_arena_item_append_child (v->arenaitem, ac);
611                 }
612             }
613         }
614     } else {    // general case
615         SPObject *ochild = sp_object_get_child_by_repr(_group, child);
616         if ( ochild && SP_IS_ITEM(ochild) ) {
617             /* TODO: this should be moved into SPItem somehow */
618             SPItemView *v;
619             NRArenaItem *ac;
621             unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
623             for (v = _group->display; v != NULL; v = v->next) {
624                 ac = sp_item_invoke_show (SP_ITEM (ochild), NR_ARENA_ITEM_ARENA (v->arenaitem), v->key, v->flags);
626                 if (ac) {
627                     nr_arena_item_add_child (v->arenaitem, ac, NULL);
628                     nr_arena_item_set_order (ac, position);
629                 }
630             }
631         }
632     }
634     _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
637 void CGroup::onChildRemoved(Inkscape::XML::Node */*child*/) {
638     _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
641 void CGroup::onUpdate(SPCtx *ctx, unsigned int flags) {
642     SPItemCtx *ictx, cctx;
644     ictx = (SPItemCtx *) ctx;
645     cctx = *ictx;
647     if (flags & SP_OBJECT_MODIFIED_FLAG) {
648       flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
649     }
651     flags &= SP_OBJECT_MODIFIED_CASCADE;
653     if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
654       SPObject *object = SP_OBJECT(_group);
655       for (SPItemView *v = SP_ITEM(_group)->display; v != NULL; v = v->next) {
656     nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
657       }
658     }
660     GSList *l = g_slist_reverse(_group->childList(true, SPObject::ActionUpdate));
661     while (l) {
662         SPObject *child = SP_OBJECT (l->data);
663         l = g_slist_remove (l, child);
664         if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
665             if (SP_IS_ITEM (child)) {
666                 SPItem const &chi = *SP_ITEM(child);
667                 cctx.i2doc = chi.transform * ictx->i2doc;
668                 cctx.i2vp = chi.transform * ictx->i2vp;
669                 child->updateDisplay((SPCtx *)&cctx, flags);
670             } else {
671                 child->updateDisplay(ctx, flags);
672             }
673         }
674         g_object_unref (G_OBJECT (child));
675     }
678 void CGroup::onModified(guint flags) {
679     SPObject *child;
681     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
682     flags &= SP_OBJECT_MODIFIED_CASCADE;
684     if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
685       SPObject *object = SP_OBJECT(_group);
686       for (SPItemView *v = SP_ITEM(_group)->display; v != NULL; v = v->next) {
687     nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
688       }
689     }
691     GSList *l = g_slist_reverse(_group->childList(true));
692     while (l) {
693         child = SP_OBJECT (l->data);
694         l = g_slist_remove (l, child);
695         if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
696             child->emitModified(flags);
697         }
698         g_object_unref (G_OBJECT (child));
699     }
702 void CGroup::calculateBBox(NRRect *bbox, Geom::Matrix const &transform, unsigned const flags) {
704     Geom::OptRect dummy_bbox;
706     GSList *l = _group->childList(false, SPObject::ActionBBox);
707     while (l) {
708         SPObject *o = SP_OBJECT (l->data);
709         if (SP_IS_ITEM(o) && !SP_ITEM(o)->isHidden()) {
710             SPItem *child = SP_ITEM(o);
711             Geom::Matrix const ct(to_2geom(child->transform) * transform);
712             sp_item_invoke_bbox_full(child, dummy_bbox, ct, flags, FALSE);
713         }
714         l = g_slist_remove (l, o);
715     }
717     *bbox = NRRect(dummy_bbox);
720 void CGroup::onPrint(SPPrintContext *ctx) {
721     GSList *l = g_slist_reverse(_group->childList(false));
722     while (l) {
723         SPObject *o = SP_OBJECT (l->data);
724         if (SP_IS_ITEM(o)) {
725             sp_item_invoke_print (SP_ITEM (o), ctx);
726         }
727         l = g_slist_remove (l, o);
728     }
731 gint CGroup::getItemCount() {
732     gint len = 0;
733     for (SPObject *o = sp_object_first_child(SP_OBJECT(_group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
734         if (SP_IS_ITEM(o)) {
735             len++;
736         }
737     }
739     return len;
742 gchar *CGroup::getDescription() {
743     gint len = getItemCount();
744     return g_strdup_printf(
745             ngettext("<b>Group</b> of <b>%d</b> object",
746                  "<b>Group</b> of <b>%d</b> objects",
747                  len), len);
750 NRArenaItem *CGroup::show (NRArena *arena, unsigned int key, unsigned int flags) {
751     NRArenaItem *ai;
752     SPObject *object = SP_OBJECT(_group);
754     ai = NRArenaGroup::create(arena);
756     nr_arena_group_set_transparent(NR_ARENA_GROUP (ai),
757                                    _group->effectiveLayerMode(key) ==
758                          SPGroup::LAYER);
759     nr_arena_group_set_style(NR_ARENA_GROUP(ai), SP_OBJECT_STYLE(object));
761     _showChildren(arena, ai, key, flags);
762     return ai;
765 void CGroup::_showChildren (NRArena *arena, NRArenaItem *ai, unsigned int key, unsigned int flags) {
766     NRArenaItem *ac = NULL;
767     NRArenaItem *ar = NULL;
768     SPItem * child = NULL;
769     GSList *l = g_slist_reverse(_group->childList(false, SPObject::ActionShow));
770     while (l) {
771         SPObject *o = SP_OBJECT (l->data);
772         if (SP_IS_ITEM (o)) {
773             child = SP_ITEM (o);
774             ac = sp_item_invoke_show (child, arena, key, flags);
775             if (ac) {
776                 nr_arena_item_add_child (ai, ac, ar);
777                 ar = ac;
778             }
779         }
780         l = g_slist_remove (l, o);
781     }
784 void CGroup::hide (unsigned int key) {
785     SPItem * child;
787     GSList *l = g_slist_reverse(_group->childList(false, SPObject::ActionShow));
788     while (l) {
789         SPObject *o = SP_OBJECT (l->data);
790         if (SP_IS_ITEM (o)) {
791             child = SP_ITEM (o);
792             sp_item_invoke_hide (child, key);
793         }
794         l = g_slist_remove (l, o);
795     }
797     if (((SPItemClass *) parent_class)->hide)
798         ((SPItemClass *) parent_class)->hide (_group, key);
801 void CGroup::onOrderChanged (Inkscape::XML::Node *child, Inkscape::XML::Node *, Inkscape::XML::Node *)
803     SPObject *ochild = sp_object_get_child_by_repr(_group, child);
804     if ( ochild && SP_IS_ITEM(ochild) ) {
805         /* TODO: this should be moved into SPItem somehow */
806         SPItemView *v;
807         unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
808         for ( v = SP_ITEM (ochild)->display ; v != NULL ; v = v->next ) {
809             nr_arena_item_set_order (v->arenaitem, position);
810         }
811     }
813     _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
816 static void
817 sp_group_update_patheffect (SPLPEItem *lpeitem, bool write)
819 #ifdef GROUP_VERBOSE
820     g_message("sp_group_update_patheffect: %p\n", lpeitem);
821 #endif
822     g_return_if_fail (lpeitem != NULL);
823     g_return_if_fail (SP_IS_GROUP (lpeitem));
825     GSList const *item_list = sp_item_group_item_list(SP_GROUP(lpeitem));
826     for ( GSList const *iter = item_list; iter; iter = iter->next ) {
827         SPObject *subitem = static_cast<SPObject *>(iter->data);
828         if (SP_IS_LPE_ITEM(subitem)) {
829             if (SP_LPE_ITEM_CLASS (G_OBJECT_GET_CLASS (subitem))->update_patheffect) {
830                 SP_LPE_ITEM_CLASS (G_OBJECT_GET_CLASS (subitem))->update_patheffect (SP_LPE_ITEM(subitem), write);
831             }
832         }
833     }
835     if (sp_lpe_item_has_path_effect(lpeitem) && sp_lpe_item_path_effects_enabled(lpeitem)) {
836         for (PathEffectList::iterator it = lpeitem->path_effect_list->begin(); it != lpeitem->path_effect_list->end(); it++)
837         {
838             LivePathEffectObject *lpeobj = (*it)->lpeobject;
839             if (lpeobj && lpeobj->get_lpe()) {
840                 lpeobj->get_lpe()->doBeforeEffect(lpeitem);
841             }
842         }
844         sp_group_perform_patheffect(SP_GROUP(lpeitem), SP_GROUP(lpeitem), write);
845     }
848 static void
849 sp_group_perform_patheffect(SPGroup *group, SPGroup *topgroup, bool write)
851     GSList const *item_list = sp_item_group_item_list(SP_GROUP(group));
852     for ( GSList const *iter = item_list; iter; iter = iter->next ) {
853         SPObject *subitem = static_cast<SPObject *>(iter->data);
854         if (SP_IS_GROUP(subitem)) {
855             sp_group_perform_patheffect(SP_GROUP(subitem), topgroup, write);
856         } else if (SP_IS_SHAPE(subitem)) {
857             SPCurve * c = NULL;
858             if (SP_IS_PATH(subitem)) {
859                 c = sp_path_get_original_curve(SP_PATH(subitem));
860             } else {
861                 c = sp_shape_get_curve(SP_SHAPE(subitem));
862             }
863             // only run LPEs when the shape has a curve defined
864             if (c) {
865                 sp_lpe_item_perform_path_effect(SP_LPE_ITEM(topgroup), c);
866                 sp_shape_set_curve(SP_SHAPE(subitem), c, TRUE);
868                 if (write) {
869                     Inkscape::XML::Node *repr = SP_OBJECT_REPR(subitem);
870                     gchar *str = sp_svg_write_path(c->get_pathvector());
871                     repr->setAttribute("d", str);
872 #ifdef GROUP_VERBOSE
873 g_message("sp_group_perform_patheffect writes 'd' attribute");
874 #endif
875                     g_free(str);
876                 }
878                 c->unref();
879             }
880         }
881     }
884 /*
885   Local Variables:
886   mode:c++
887   c-file-style:"stroustrup"
888   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
889   indent-tabs-mode:nil
890   fill-column:99
891   End:
892 */
893 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :