Code

Pot and Dutch translation update
[inkscape.git] / src / sp-item.cpp
1 /** \file
2  * Base class for visual SVG elements
3  */
4 /*
5  * Authors:
6  *   Lauris Kaplinski <lauris@kaplinski.com>
7  *   bulia byak <buliabyak@users.sf.net>
8  *   Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
9  *
10  * Copyright (C) 2001-2006 authors
11  * Copyright (C) 2001 Ximian, Inc.
12  *
13  * Released under GNU GPL, read the file 'COPYING' for more information
14  */
16 /** \class SPItem
17  *
18  * SPItem is an abstract base class for all graphic (visible) SVG nodes. It
19  * is a subclass of SPObject, with great deal of specific functionality.
20  */
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
27 #include "sp-item.h"
28 #include "svg/svg.h"
29 #include "print.h"
30 #include "display/nr-arena.h"
31 #include "display/nr-arena-item.h"
32 #include "attributes.h"
33 #include "document.h"
34 #include "uri.h"
35 #include "inkscape.h"
36 #include "desktop.h"
37 #include "desktop-handles.h"
39 #include "style.h"
40 #include <glibmm/i18n.h>
41 #include "sp-root.h"
42 #include "sp-clippath.h"
43 #include "sp-mask.h"
44 #include "sp-rect.h"
45 #include "sp-use.h"
46 #include "sp-text.h"
47 #include "sp-item-rm-unsatisfied-cns.h"
48 #include "sp-pattern.h"
49 #include "sp-switch.h"
50 #include "sp-guide-constraint.h"
51 #include "gradient-chemistry.h"
52 #include "preferences.h"
53 #include "conn-avoid-ref.h"
54 #include "conditions.h"
55 #include "sp-filter-reference.h"
56 #include "filter-chemistry.h"
57 #include "sp-guide.h"
58 #include "sp-title.h"
59 #include "sp-desc.h"
61 #include "libnr/nr-matrix-fns.h"
62 #include "libnr/nr-matrix-scale-ops.h"
63 #include "libnr/nr-matrix-translate-ops.h"
64 #include "libnr/nr-scale-translate-ops.h"
65 #include "libnr/nr-translate-scale-ops.h"
66 #include "libnr/nr-convert2geom.h"
67 #include "util/find-last-if.h"
68 #include "util/reverse-list.h"
69 #include <2geom/rect.h>
70 #include <2geom/matrix.h>
71 #include <2geom/transforms.h>
73 #include "xml/repr.h"
74 #include "extract-uri.h"
75 #include "helper/geom.h"
77 #include "live_effects/lpeobject.h"
78 #include "live_effects/effect.h"
79 #include "live_effects/lpeobject-reference.h"
81 #define noSP_ITEM_DEBUG_IDLE
83 static void sp_item_class_init(SPItemClass *klass);
84 static void sp_item_init(SPItem *item);
86 static void sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
87 static void sp_item_release(SPObject *object);
88 static void sp_item_set(SPObject *object, unsigned key, gchar const *value);
89 static void sp_item_update(SPObject *object, SPCtx *ctx, guint flags);
90 static Inkscape::XML::Node *sp_item_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
92 static gchar *sp_item_private_description(SPItem *item);
93 static void sp_item_private_snappoints(SPItem const *item, std::vector<Inkscape::SnapCandidatePoint> &p, Inkscape::SnapPreferences const *snapprefs);
95 static SPItemView *sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem);
96 static SPItemView *sp_item_view_list_remove(SPItemView *list, SPItemView *view);
98 static SPObjectClass *parent_class;
100 static void clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
101 static void mask_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
103 /**
104  * Registers SPItem class and returns its type number.
105  */
106 GType
107 sp_item_get_type(void)
109     static GType type = 0;
110     if (!type) {
111         GTypeInfo info = {
112             sizeof(SPItemClass),
113             NULL, NULL,
114             (GClassInitFunc) sp_item_class_init,
115             NULL, NULL,
116             sizeof(SPItem),
117             16,
118             (GInstanceInitFunc) sp_item_init,
119             NULL,   /* value_table */
120         };
121         type = g_type_register_static(SP_TYPE_OBJECT, "SPItem", &info, (GTypeFlags)0);
122     }
123     return type;
126 /**
127  * SPItem vtable initialization.
128  */
129 static void
130 sp_item_class_init(SPItemClass *klass)
132     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
134     parent_class = (SPObjectClass *)g_type_class_ref(SP_TYPE_OBJECT);
136     sp_object_class->build = sp_item_build;
137     sp_object_class->release = sp_item_release;
138     sp_object_class->set = sp_item_set;
139     sp_object_class->update = sp_item_update;
140     sp_object_class->write = sp_item_write;
142     klass->description = sp_item_private_description;
143     klass->snappoints = sp_item_private_snappoints;
146 /**
147  * Callback for SPItem object initialization.
148  */
149 static void
150 sp_item_init(SPItem *item)
152     item->init();
155 void SPItem::init() {
156     this->sensitive = TRUE;
158     this->transform_center_x = 0;
159     this->transform_center_y = 0;
161     this->_is_evaluated = true;
162     this->_evaluated_status = StatusUnknown;
164     this->transform = Geom::identity();
166     this->display = NULL;
168     this->clip_ref = new SPClipPathReference(this);
169     sigc::signal<void, SPObject *, SPObject *> cs1=this->clip_ref->changedSignal();
170     sigc::slot2<void,SPObject*, SPObject *> sl1=sigc::bind(sigc::ptr_fun(clip_ref_changed), this);
171     _clip_ref_connection = cs1.connect(sl1);
173     this->mask_ref = new SPMaskReference(this);
174     sigc::signal<void, SPObject *, SPObject *> cs2=this->mask_ref->changedSignal();
175     sigc::slot2<void,SPObject*, SPObject *> sl2=sigc::bind(sigc::ptr_fun(mask_ref_changed), this);
176     _mask_ref_connection = cs2.connect(sl2);
178     this->avoidRef = new SPAvoidRef(this);
180     new (&this->constraints) std::vector<SPGuideConstraint>();
182     new (&this->_transformed_signal) sigc::signal<void, Geom::Matrix const *, SPItem *>();
185 bool SPItem::isVisibleAndUnlocked() const {
186     return (!isHidden() && !isLocked());
189 bool SPItem::isVisibleAndUnlocked(unsigned display_key) const {
190     return (!isHidden(display_key) && !isLocked());
193 bool SPItem::isLocked() const {
194     for (SPObject const *o = this; o != NULL; o = o->parent) {
195         if (SP_IS_ITEM(o) && !(SP_ITEM(o)->sensitive)) {
196             return true;
197         }
198     }
199     return false;
202 void SPItem::setLocked(bool locked) {
203     this->repr->setAttribute("sodipodi:insensitive",
204                              ( locked ? "1" : NULL ));
205     updateRepr();
208 bool SPItem::isHidden() const {
209     if (!isEvaluated())
210         return true;
211     return style->display.computed == SP_CSS_DISPLAY_NONE;
214 void SPItem::setHidden(bool hide) {
215     style->display.set = TRUE;
216     style->display.value = ( hide ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
217     style->display.computed = style->display.value;
218     style->display.inherit = FALSE;
219     updateRepr();
222 bool SPItem::isHidden(unsigned display_key) const {
223     if (!isEvaluated())
224         return true;
225     for ( SPItemView *view(display) ; view ; view = view->next ) {
226         if ( view->key == display_key ) {
227             g_assert(view->arenaitem != NULL);
228             for ( NRArenaItem *arenaitem = view->arenaitem ;
229                   arenaitem ; arenaitem = arenaitem->parent )
230             {
231                 if (!arenaitem->visible) {
232                     return true;
233                 }
234             }
235             return false;
236         }
237     }
238     return true;
241 void SPItem::setEvaluated(bool evaluated) {
242     _is_evaluated = evaluated;
243     _evaluated_status = StatusSet;
246 void SPItem::resetEvaluated() {
247     if ( StatusCalculated == _evaluated_status ) {
248         _evaluated_status = StatusUnknown;
249         bool oldValue = _is_evaluated;
250         if ( oldValue != isEvaluated() ) {
251             requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
252         }
253     } if ( StatusSet == _evaluated_status ) {
254         SPObject const *const parent = this->parent;
255         if (SP_IS_SWITCH(parent)) {
256             SP_SWITCH(parent)->resetChildEvaluated();
257         }
258     }
261 bool SPItem::isEvaluated() const {
262     if ( StatusUnknown == _evaluated_status ) {
263         _is_evaluated = sp_item_evaluate(this);
264         _evaluated_status = StatusCalculated;
265     }
266     return _is_evaluated;
269 /**
270  * Returns something suitable for the `Hide' checkbox in the Object Properties dialog box.
271  *  Corresponds to setExplicitlyHidden.
272  */
273 bool
274 SPItem::isExplicitlyHidden() const
276     return (this->style->display.set
277             && this->style->display.value == SP_CSS_DISPLAY_NONE);
280 /**
281  * Sets the display CSS property to `hidden' if \a val is true,
282  * otherwise makes it unset
283  */
284 void
285 SPItem::setExplicitlyHidden(bool const val) {
286     this->style->display.set = val;
287     this->style->display.value = ( val ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
288     this->style->display.computed = this->style->display.value;
289     this->updateRepr();
292 /**
293  * Sets the transform_center_x and transform_center_y properties to retain the rotation centre
294  */
295 void
296 SPItem::setCenter(Geom::Point object_centre) {
297     // for getBounds() to work
298     sp_document_ensure_up_to_date( this->document );
300     Geom::OptRect bbox = getBounds(sp_item_i2d_affine(this));
301     if (bbox) {
302         transform_center_x = object_centre[Geom::X] - bbox->midpoint()[Geom::X];
303         if (fabs(transform_center_x) < 1e-5) // rounding error
304             transform_center_x = 0;
305         transform_center_y = object_centre[Geom::Y] - bbox->midpoint()[Geom::Y];
306         if (fabs(transform_center_y) < 1e-5) // rounding error
307             transform_center_y = 0;
308     }
311 void
312 SPItem::unsetCenter() {
313     transform_center_x = 0;
314     transform_center_y = 0;
317 bool SPItem::isCenterSet() {
318     return (transform_center_x != 0 || transform_center_y != 0);
321 Geom::Point SPItem::getCenter() const {
322     // for getBounds() to work
323     sp_document_ensure_up_to_date( this->document );
325     Geom::OptRect bbox = getBounds(sp_item_i2d_affine(this));
326     if (bbox) {
327         return to_2geom(bbox->midpoint()) + Geom::Point (this->transform_center_x, this->transform_center_y);
328     } else {
329         return Geom::Point (0, 0); // something's wrong!
330     }
334 namespace {
336 bool is_item(SPObject const &object) {
337     return SP_IS_ITEM(&object);
342 void SPItem::raiseToTop() {
343     using Inkscape::Algorithms::find_last_if;
345     SPObject *topmost=find_last_if<SPObject::SiblingIterator>(
346         this->next, NULL, &is_item
347     );
348     if (topmost) {
349         Inkscape::XML::Node *repr = this->repr;
350         sp_repr_parent(repr)->changeOrder( repr, topmost->repr );
351     }
354 void SPItem::raiseOne() {
355     SPObject *next_higher=std::find_if<SPObject::SiblingIterator>(
356         this->next, NULL, &is_item
357     );
358     if (next_higher) {
359         Inkscape::XML::Node *repr = this->repr;
360         Inkscape::XML::Node *ref = next_higher->repr;
361         sp_repr_parent(repr)->changeOrder(repr, ref);
362     }
365 void SPItem::lowerOne() {
366     using Inkscape::Util::MutableList;
367     using Inkscape::Util::reverse_list;
369     MutableList<SPObject &> next_lower=std::find_if(
370         reverse_list<SPObject::SiblingIterator>(
371             this->parent->firstChild(), this
372         ),
373         MutableList<SPObject &>(),
374         &is_item
375     );
376     if (next_lower) {
377         ++next_lower;
378         Inkscape::XML::Node *repr = this->repr;
379         Inkscape::XML::Node *ref = ( next_lower ? next_lower->repr : NULL );
380         sp_repr_parent(repr)->changeOrder(repr, ref);
381     }
384 void SPItem::lowerToBottom() {
385     using Inkscape::Algorithms::find_last_if;
386     using Inkscape::Util::MutableList;
387     using Inkscape::Util::reverse_list;
389     MutableList<SPObject &> bottom=find_last_if(
390         reverse_list<SPObject::SiblingIterator>(
391             this->parent->firstChild(), this
392         ),
393         MutableList<SPObject &>(),
394         &is_item
395     );
396     if (bottom) {
397         ++bottom;
398         Inkscape::XML::Node *repr = this->repr;
399         Inkscape::XML::Node *ref = ( bottom ? bottom->repr : NULL );
400         sp_repr_parent(repr)->changeOrder(repr, ref);
401     }
404 static void
405 sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
407     sp_object_read_attr(object, "style");
408     sp_object_read_attr(object, "transform");
409     sp_object_read_attr(object, "clip-path");
410     sp_object_read_attr(object, "mask");
411     sp_object_read_attr(object, "sodipodi:insensitive");
412     sp_object_read_attr(object, "sodipodi:nonprintable");
413     sp_object_read_attr(object, "inkscape:transform-center-x");
414     sp_object_read_attr(object, "inkscape:transform-center-y");
415     sp_object_read_attr(object, "inkscape:connector-avoid");
416     sp_object_read_attr(object, "inkscape:connection-points");
418     if (((SPObjectClass *) (parent_class))->build) {
419         (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
420     }
423 static void
424 sp_item_release(SPObject *object)
426     SPItem *item = (SPItem *) object;
428     item->_clip_ref_connection.disconnect();
429     item->_mask_ref_connection.disconnect();
431     // Note: do this here before the clip_ref is deleted, since calling
432     // sp_document_ensure_up_to_date for triggered routing may reference
433     // the deleted clip_ref.
434     if (item->avoidRef) {
435         delete item->avoidRef;
436         item->avoidRef = NULL;
437     }
439     if (item->clip_ref) {
440         item->clip_ref->detach();
441         delete item->clip_ref;
442         item->clip_ref = NULL;
443     }
445     if (item->mask_ref) {
446         item->mask_ref->detach();
447         delete item->mask_ref;
448         item->mask_ref = NULL;
449     }
451     if (((SPObjectClass *) (parent_class))->release) {
452         ((SPObjectClass *) parent_class)->release(object);
453     }
455     while (item->display) {
456         nr_arena_item_unparent(item->display->arenaitem);
457         item->display = sp_item_view_list_remove(item->display, item->display);
458     }
460     item->_transformed_signal.~signal();
463 static void
464 sp_item_set(SPObject *object, unsigned key, gchar const *value)
466     SPItem *item = (SPItem *) object;
468     switch (key) {
469         case SP_ATTR_TRANSFORM: {
470             Geom::Matrix t;
471             if (value && sp_svg_transform_read(value, &t)) {
472                 sp_item_set_item_transform(item, t);
473             } else {
474                 sp_item_set_item_transform(item, Geom::identity());
475             }
476             break;
477         }
478         case SP_PROP_CLIP_PATH: {
479             gchar *uri = extract_uri(value);
480             if (uri) {
481                 try {
482                     item->clip_ref->attach(Inkscape::URI(uri));
483                 } catch (Inkscape::BadURIException &e) {
484                     g_warning("%s", e.what());
485                     item->clip_ref->detach();
486                 }
487                 g_free(uri);
488             } else {
489                 item->clip_ref->detach();
490             }
492             break;
493         }
494         case SP_PROP_MASK: {
495             gchar *uri = extract_uri(value);
496             if (uri) {
497                 try {
498                     item->mask_ref->attach(Inkscape::URI(uri));
499                 } catch (Inkscape::BadURIException &e) {
500                     g_warning("%s", e.what());
501                     item->mask_ref->detach();
502                 }
503                 g_free(uri);
504             } else {
505                 item->mask_ref->detach();
506             }
508             break;
509         }
510         case SP_ATTR_SODIPODI_INSENSITIVE:
511             item->sensitive = !value;
512             for (SPItemView *v = item->display; v != NULL; v = v->next) {
513                 nr_arena_item_set_sensitive(v->arenaitem, item->sensitive);
514             }
515             break;
516         case SP_ATTR_CONNECTOR_AVOID:
517             item->avoidRef->setAvoid(value);
518             break;
519         case SP_ATTR_CONNECTION_POINTS:
520             item->avoidRef->setConnectionPoints(value);
521             break;
522         case SP_ATTR_TRANSFORM_CENTER_X:
523             if (value) {
524                 item->transform_center_x = g_strtod(value, NULL);
525             } else {
526                 item->transform_center_x = 0;
527             }
528             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
529             break;
530         case SP_ATTR_TRANSFORM_CENTER_Y:
531             if (value) {
532                 item->transform_center_y = g_strtod(value, NULL);
533             } else {
534                 item->transform_center_y = 0;
535             }
536             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
537             break;
538         case SP_PROP_SYSTEM_LANGUAGE:
539         case SP_PROP_REQUIRED_FEATURES:
540         case SP_PROP_REQUIRED_EXTENSIONS:
541             {
542                 item->resetEvaluated();
543                 // pass to default handler
544             }
545         default:
546             if (SP_ATTRIBUTE_IS_CSS(key)) {
547                 sp_style_read_from_object(object->style, object);
548                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
549             } else {
550                 if (((SPObjectClass *) (parent_class))->set) {
551                     (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
552                 }
553             }
554             break;
555     }
558 static void
559 clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item)
561     if (old_clip) {
562         SPItemView *v;
563         /* Hide clippath */
564         for (v = item->display; v != NULL; v = v->next) {
565             sp_clippath_hide(SP_CLIPPATH(old_clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
566             nr_arena_item_set_clip(v->arenaitem, NULL);
567         }
568     }
569     if (SP_IS_CLIPPATH(clip)) {
570         NRRect bbox;
571         sp_item_invoke_bbox(item, &bbox, Geom::identity(), TRUE);
572         for (SPItemView *v = item->display; v != NULL; v = v->next) {
573             if (!v->arenaitem->key) {
574                 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
575             }
576             NRArenaItem *ai = sp_clippath_show(SP_CLIPPATH(clip),
577                                                NR_ARENA_ITEM_ARENA(v->arenaitem),
578                                                NR_ARENA_ITEM_GET_KEY(v->arenaitem));
579             nr_arena_item_set_clip(v->arenaitem, ai);
580             nr_arena_item_unref(ai);
581             sp_clippath_set_bbox(SP_CLIPPATH(clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
582             clip->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
583         }
584     }
587 static void
588 mask_ref_changed(SPObject *old_mask, SPObject *mask, SPItem *item)
590     if (old_mask) {
591         /* Hide mask */
592         for (SPItemView *v = item->display; v != NULL; v = v->next) {
593             sp_mask_hide(SP_MASK(old_mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
594             nr_arena_item_set_mask(v->arenaitem, NULL);
595         }
596     }
597     if (SP_IS_MASK(mask)) {
598         NRRect bbox;
599         sp_item_invoke_bbox(item, &bbox, Geom::identity(), TRUE);
600         for (SPItemView *v = item->display; v != NULL; v = v->next) {
601             if (!v->arenaitem->key) {
602                 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
603             }
604             NRArenaItem *ai = sp_mask_show(SP_MASK(mask),
605                                            NR_ARENA_ITEM_ARENA(v->arenaitem),
606                                            NR_ARENA_ITEM_GET_KEY(v->arenaitem));
607             nr_arena_item_set_mask(v->arenaitem, ai);
608             nr_arena_item_unref(ai);
609             sp_mask_set_bbox(SP_MASK(mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
610             mask->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
611         }
612     }
615 static void
616 sp_item_update(SPObject *object, SPCtx *ctx, guint flags)
618     SPItem *item = SP_ITEM(object);
620     if (((SPObjectClass *) (parent_class))->update)
621         (* ((SPObjectClass *) (parent_class))->update)(object, ctx, flags);
623     if (flags & (SP_OBJECT_CHILD_MODIFIED_FLAG | SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG)) {
624         if (flags & SP_OBJECT_MODIFIED_FLAG) {
625             for (SPItemView *v = item->display; v != NULL; v = v->next) {
626                 nr_arena_item_set_transform(v->arenaitem, item->transform);
627             }
628         }
630         SPClipPath *clip_path = item->clip_ref ? item->clip_ref->getObject() : NULL;
631         SPMask *mask = item->mask_ref ? item->mask_ref->getObject() : NULL;
633         if ( clip_path || mask ) {
634             NRRect bbox;
635             sp_item_invoke_bbox(item, &bbox, Geom::identity(), TRUE);
636             if (clip_path) {
637                 for (SPItemView *v = item->display; v != NULL; v = v->next) {
638                     sp_clippath_set_bbox(clip_path, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
639                 }
640             }
641             if (mask) {
642                 for (SPItemView *v = item->display; v != NULL; v = v->next) {
643                     sp_mask_set_bbox(mask, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
644                 }
645             }
646         }
648         if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
649             for (SPItemView *v = item->display; v != NULL; v = v->next) {
650                 nr_arena_item_set_opacity(v->arenaitem, SP_SCALE24_TO_FLOAT(object->style->opacity.value));
651                 nr_arena_item_set_visible(v->arenaitem, !item->isHidden());
652             }
653         }
654     }
656     /* Update bounding box data used by filters */
657     if (item->style->filter.set && item->display) {
658         Geom::OptRect item_bbox;
659         sp_item_invoke_bbox(item, item_bbox, Geom::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
661         SPItemView *itemview = item->display;
662         do {
663             if (itemview->arenaitem)
664                 nr_arena_item_set_item_bbox(itemview->arenaitem, item_bbox);
665         } while ( (itemview = itemview->next) );
666     }
668     // Update libavoid with item geometry (for connector routing).
669     if (item->avoidRef)
670         item->avoidRef->handleSettingChange();
673 static Inkscape::XML::Node *
674 sp_item_write(SPObject *const object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
676     SPObject *child;
677     SPItem *item = SP_ITEM(object);
679     // in the case of SP_OBJECT_WRITE_BUILD, the item should always be newly created,
680     // so we need to add any children from the underlying object to the new repr
681     if (flags & SP_OBJECT_WRITE_BUILD) {
682         Inkscape::XML::Node *crepr;
683         GSList *l;
684         l = NULL;
685         for (child = sp_object_first_child(object); child != NULL; child = child->next ) {
686             if (!SP_IS_TITLE(child) && !SP_IS_DESC(child)) continue;
687             crepr = child->updateRepr(xml_doc, NULL, flags);
688             if (crepr) l = g_slist_prepend (l, crepr);
689         }
690         while (l) {
691             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
692             Inkscape::GC::release((Inkscape::XML::Node *) l->data);
693             l = g_slist_remove (l, l->data);
694         }
695     } else {
696         for (child = sp_object_first_child(object) ; child != NULL; child = child->next ) {
697             if (!SP_IS_TITLE(child) && !SP_IS_DESC(child)) continue;
698             child->updateRepr(flags);
699         }
700     }
702     gchar *c = sp_svg_transform_write(item->transform);
703     repr->setAttribute("transform", c);
704     g_free(c);
706     if (flags & SP_OBJECT_WRITE_EXT) {
707         repr->setAttribute("sodipodi:insensitive", ( item->sensitive ? NULL : "true" ));
708         if (item->transform_center_x != 0)
709             sp_repr_set_svg_double (repr, "inkscape:transform-center-x", item->transform_center_x);
710         else
711             repr->setAttribute ("inkscape:transform-center-x", NULL);
712         if (item->transform_center_y != 0)
713             sp_repr_set_svg_double (repr, "inkscape:transform-center-y", item->transform_center_y);
714         else
715             repr->setAttribute ("inkscape:transform-center-y", NULL);
716     }
718     if (item->clip_ref->getObject()) {
719         const gchar *value = g_strdup_printf ("url(%s)", item->clip_ref->getURI()->toString());
720         repr->setAttribute ("clip-path", value);
721         g_free ((void *) value);
722     }
723     if (item->mask_ref->getObject()) {
724         const gchar *value = g_strdup_printf ("url(%s)", item->mask_ref->getURI()->toString());
725         repr->setAttribute ("mask", value);
726         g_free ((void *) value);
727     }
729     if (((SPObjectClass *) (parent_class))->write) {
730         ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
731     }
733     return repr;
736 /**
737  * \return  There is no guarantee that the return value will contain a rectangle.
738             If this item does not have a boundingbox, it might well be empty.
739  */
740 Geom::OptRect SPItem::getBounds(Geom::Matrix const &transform,
741                                       SPItem::BBoxType type,
742                                       unsigned int /*dkey*/) const
744     Geom::OptRect r;
745     sp_item_invoke_bbox_full(this, r, transform, type, TRUE);
746     return r;
749 void
750 sp_item_invoke_bbox(SPItem const *item, Geom::OptRect &bbox, Geom::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
752     sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
755 // DEPRECATED to phase out the use of NRRect in favor of Geom::OptRect
756 void
757 sp_item_invoke_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
759     sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
762 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
763  * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
764  * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
765  * clones), in turn, call this function in their bbox methods.
766  * \retval bbox  Note that there is no guarantee that bbox will contain a rectangle when the
767  *               function returns. If this item does not have a boundingbox, this might well be empty.
768  */
769 void
770 sp_item_invoke_bbox_full(SPItem const *item, Geom::OptRect &bbox, Geom::Matrix const &transform, unsigned const flags, unsigned const clear)
772     g_assert(item != NULL);
773     g_assert(SP_IS_ITEM(item));
775     if (clear) {
776         bbox = Geom::OptRect();
777     }
779     // TODO: replace NRRect by Geom::Rect, for all SPItemClasses, and for SP_CLIPPATH
781     NRRect temp_bbox;
782     temp_bbox.x0 = temp_bbox.y0 = NR_HUGE;
783     temp_bbox.x1 = temp_bbox.y1 = -NR_HUGE;
785     // call the subclass method
786     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
787         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &temp_bbox, transform, flags);
788     }
790     // unless this is geometric bbox, extend by filter area and crop the bbox by clip path, if any
791     if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX) {
792         if ( item->style && item->style->filter.href ) {
793             SPObject *filter = item->style->getFilter();
794             if (filter && SP_IS_FILTER(filter)) {
795                 // default filer area per the SVG spec:
796                 double x = -0.1;
797                 double y = -0.1;
798                 double w = 1.2;
799                 double h = 1.2;
801                 // if area is explicitly set, override:
802                 if (SP_FILTER(filter)->x._set)
803                     x = SP_FILTER(filter)->x.computed;
804                 if (SP_FILTER(filter)->y._set)
805                     y = SP_FILTER(filter)->y.computed;
806                 if (SP_FILTER(filter)->width._set)
807                     w = SP_FILTER(filter)->width.computed;
808                 if (SP_FILTER(filter)->height._set)
809                     h = SP_FILTER(filter)->height.computed;
811                 double dx0 = 0;
812                 double dx1 = 0;
813                 double dy0 = 0;
814                 double dy1 = 0;
815                 if (filter_is_single_gaussian_blur(SP_FILTER(filter))) {
816                     // if this is a single blur, use 2.4*radius
817                     // which may be smaller than the default area;
818                     // see set_filter_area for why it's 2.4
819                     double r = get_single_gaussian_blur_radius (SP_FILTER(filter));
820                     dx0 = -2.4 * r;
821                     dx1 = 2.4 * r;
822                     dy0 = -2.4 * r;
823                     dy1 = 2.4 * r;
824                 } else {
825                     // otherwise, calculate expansion from relative to absolute units:
826                     dx0 = x * (temp_bbox.x1 - temp_bbox.x0);
827                     dx1 = (w + x - 1) * (temp_bbox.x1 - temp_bbox.x0);
828                     dy0 = y * (temp_bbox.y1 - temp_bbox.y0);
829                     dy1 = (h + y - 1) * (temp_bbox.y1 - temp_bbox.y0);
830                 }
832                 // transform the expansions by the item's transform:
833                 Geom::Matrix i2d(sp_item_i2d_affine (item));
834                 dx0 *= i2d.expansionX();
835                 dx1 *= i2d.expansionX();
836                 dy0 *= i2d.expansionY();
837                 dy1 *= i2d.expansionY();
839                 // expand the bbox
840                 temp_bbox.x0 += dx0;
841                 temp_bbox.x1 += dx1;
842                 temp_bbox.y0 += dy0;
843                 temp_bbox.y1 += dy1;
844             }
845         }
846         if (item->clip_ref->getObject()) {
847             NRRect b;
848             sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
849             nr_rect_d_intersect (&temp_bbox, &temp_bbox, &b);
850         }
851     }
853     if (temp_bbox.x0 > temp_bbox.x1 || temp_bbox.y0 > temp_bbox.y1) {
854         // Either the bbox hasn't been touched by the SPItemClass' bbox method
855         // (it still has its initial values, see above: x0 = y0 = NR_HUGE and x1 = y1 = -NR_HUGE)
856         // or it has explicitely been set to be like this (e.g. in sp_shape_bbox)
858         // When x0 > x1 or y0 > y1, the bbox is considered to be "nothing", although it has not been
859         // explicitely defined this way for NRRects (as opposed to Geom::OptRect)
860         // So union bbox with nothing = do nothing, just return
861         return;
862     }
864     // Do not use temp_bbox.upgrade() here, because it uses a test that returns an empty Geom::OptRect()
865     // for any rectangle with zero area. The geometrical bbox of for example a vertical line
866     // would therefore be translated into empty Geom::OptRect() (see bug https://bugs.launchpad.net/inkscape/+bug/168684)
867     Geom::OptRect temp_bbox_new = Geom::Rect(Geom::Point(temp_bbox.x0, temp_bbox.y0), Geom::Point(temp_bbox.x1, temp_bbox.y1));
869     bbox = Geom::unify(bbox, temp_bbox_new);
872 // DEPRECATED to phase out the use of NRRect in favor of Geom::OptRect
873 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
874  * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
875  * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
876  * clones), in turn, call this function in their bbox methods. */
877 void
878 sp_item_invoke_bbox_full(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags, unsigned const clear)
880     g_assert(item != NULL);
881     g_assert(SP_IS_ITEM(item));
882     g_assert(bbox != NULL);
884     if (clear) {
885         bbox->x0 = bbox->y0 = 1e18;
886         bbox->x1 = bbox->y1 = -1e18;
887     }
889     NRRect this_bbox;
890     this_bbox.x0 = this_bbox.y0 = 1e18;
891     this_bbox.x1 = this_bbox.y1 = -1e18;
893     // call the subclass method
894     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
895         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &this_bbox, transform, flags);
896     }
898     // unless this is geometric bbox, crop the bbox by clip path, if any
899     if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX && item->clip_ref->getObject()) {
900         NRRect b;
901         sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
902         nr_rect_d_intersect (&this_bbox, &this_bbox, &b);
903     }
905     // if non-empty (with some tolerance - ?) union this_bbox with the bbox we've got passed
906     if ( fabs(this_bbox.x1-this_bbox.x0) > -0.00001 && fabs(this_bbox.y1-this_bbox.y0) > -0.00001 ) {
907         nr_rect_d_union (bbox, bbox, &this_bbox);
908     }
911 unsigned sp_item_pos_in_parent(SPItem *item)
913     g_assert(item != NULL);
914     g_assert(SP_IS_ITEM(item));
916     SPObject *parent = item->parent;
917     g_assert(parent != NULL);
918     g_assert(SP_IS_OBJECT(parent));
920     SPObject *object = item;
922     unsigned pos = 0;
923     for ( SPObject *iter = sp_object_first_child(parent) ; iter ; iter = iter->next) {
924         if ( iter == object ) {
925             return pos;
926         }
927         if (SP_IS_ITEM(iter)) {
928             pos++;
929         }
930     }
932     g_assert_not_reached();
933     return 0;
936 void
937 sp_item_bbox_desktop(SPItem *item, NRRect *bbox, SPItem::BBoxType type)
939     g_assert(item != NULL);
940     g_assert(SP_IS_ITEM(item));
941     g_assert(bbox != NULL);
943     sp_item_invoke_bbox(item, bbox, sp_item_i2d_affine(item), TRUE, type);
946 Geom::OptRect sp_item_bbox_desktop(SPItem *item, SPItem::BBoxType type)
948     Geom::OptRect rect = Geom::OptRect();
949     sp_item_invoke_bbox(item, rect, sp_item_i2d_affine(item), TRUE, type);
950     return rect;
953 static void sp_item_private_snappoints(SPItem const *item, std::vector<Inkscape::SnapCandidatePoint> &p, Inkscape::SnapPreferences const */*snapprefs*/)
955     /* This will only be called if the derived class doesn't override this.
956      * see for example sp_genericellipse_snappoints in sp-ellipse.cpp
957      * We don't know what shape we could be dealing with here, so we'll just
958      * return the corners of the bounding box */
960     Geom::OptRect bbox = item->getBounds(sp_item_i2d_affine(item));
962     if (bbox) {
963         Geom::Point p1, p2;
964         p1 = bbox->min();
965         p2 = bbox->max();
966         p.push_back(Inkscape::SnapCandidatePoint(p1, Inkscape::SNAPSOURCE_BBOX_CORNER, Inkscape::SNAPTARGET_BBOX_CORNER));
967         p.push_back(Inkscape::SnapCandidatePoint(Geom::Point(p1[Geom::X], p2[Geom::Y]), Inkscape::SNAPSOURCE_BBOX_CORNER, Inkscape::SNAPTARGET_BBOX_CORNER));
968         p.push_back(Inkscape::SnapCandidatePoint(p2, Inkscape::SNAPSOURCE_BBOX_CORNER, Inkscape::SNAPTARGET_BBOX_CORNER));
969         p.push_back(Inkscape::SnapCandidatePoint(Geom::Point(p2[Geom::X], p1[Geom::Y]), Inkscape::SNAPSOURCE_BBOX_CORNER, Inkscape::SNAPTARGET_BBOX_CORNER));
970     }
974 void sp_item_snappoints(SPItem const *item, std::vector<Inkscape::SnapCandidatePoint> &p, Inkscape::SnapPreferences const *snapprefs)
976     if (item == NULL) {
977         g_warning("sp_item_snappoints: cannot snap because no item is being provided");
978         return;
979     }
981     if (!SP_IS_ITEM(item)) {
982         g_warning("sp_item_snappoints: cannot snap because this is not a SP_ITEM");
983         return;
984     }
986     // Get the snappoints of the item
987     SPItemClass const &item_class = *(SPItemClass const *) G_OBJECT_GET_CLASS(item);
988     if (item_class.snappoints) {
989         item_class.snappoints(item, p, snapprefs);
990     }
992     // Get the snappoints at the item's center
993     if (snapprefs != NULL && snapprefs->getIncludeItemCenter()) {
994         p.push_back(Inkscape::SnapCandidatePoint(item->getCenter(), Inkscape::SNAPSOURCE_ROTATION_CENTER, Inkscape::SNAPTARGET_ROTATION_CENTER));
995     }
997     // Get the snappoints of clipping paths and mask, if any
998     std::list<SPObject const *> clips_and_masks;
1000     clips_and_masks.push_back(item->clip_ref->getObject());
1001     clips_and_masks.push_back(item->mask_ref->getObject());
1003     SPDesktop *desktop = inkscape_active_desktop();
1004     for (std::list<SPObject const *>::const_iterator o = clips_and_masks.begin(); o != clips_and_masks.end(); o++) {
1005         if (*o) {
1006             // obj is a group object, the children are the actual clippers
1007             for (SPObject *child = (*o)->children ; child ; child = child->next) {
1008                 if (SP_IS_ITEM(child)) {
1009                     std::vector<Inkscape::SnapCandidatePoint> p_clip_or_mask;
1010                     // Please note the recursive call here!
1011                     sp_item_snappoints(SP_ITEM(child), p_clip_or_mask, snapprefs);
1012                     // Take into account the transformation of the item being clipped or masked
1013                     for (std::vector<Inkscape::SnapCandidatePoint>::const_iterator p_orig = p_clip_or_mask.begin(); p_orig != p_clip_or_mask.end(); p_orig++) {
1014                         // All snappoints are in desktop coordinates, but the item's transformation is
1015                         // in document coordinates. Hence the awkward construction below
1016                         Geom::Point pt = desktop->dt2doc((*p_orig).getPoint()) * sp_item_i2d_affine(item);
1017                         p.push_back(Inkscape::SnapCandidatePoint(pt, (*p_orig).getSourceType(), (*p_orig).getTargetType()));
1018                     }
1019                 }
1020             }
1021         }
1022     }
1025 void
1026 sp_item_invoke_print(SPItem *item, SPPrintContext *ctx)
1028     if (!item->isHidden()) {
1029         if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->print) {
1030             if (!item->transform.isIdentity()
1031                 || item->style->opacity.value != SP_SCALE24_MAX)
1032             {
1033                 sp_print_bind(ctx, item->transform, SP_SCALE24_TO_FLOAT(item->style->opacity.value));
1034                 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
1035                 sp_print_release(ctx);
1036             } else {
1037                 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
1038             }
1039         }
1040     }
1043 static gchar *
1044 sp_item_private_description(SPItem */*item*/)
1046     return g_strdup(_("Object"));
1049 /**
1050  * Returns a string suitable for status bar, formatted in pango markup language.
1051  *
1052  * Must be freed by caller.
1053  */
1054 gchar *
1055 sp_item_description(SPItem *item)
1057     g_assert(item != NULL);
1058     g_assert(SP_IS_ITEM(item));
1060     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->description) {
1061         gchar *s = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->description(item);
1062         if (s && item->clip_ref->getObject()) {
1063             gchar *snew = g_strdup_printf (_("%s; <i>clipped</i>"), s);
1064             g_free (s);
1065             s = snew;
1066         }
1067         if (s && item->mask_ref->getObject()) {
1068             gchar *snew = g_strdup_printf (_("%s; <i>masked</i>"), s);
1069             g_free (s);
1070             s = snew;
1071         }
1072         if ( item->style && item->style->filter.href && item->style->filter.href->getObject() ) {
1073             const gchar *label = item->style->filter.href->getObject()->label();
1074             gchar *snew;
1075             if (label) {
1076                 snew = g_strdup_printf (_("%s; <i>filtered (%s)</i>"), s, _(label));
1077             } else {
1078                 snew = g_strdup_printf (_("%s; <i>filtered</i>"), s);
1079             }
1080             g_free (s);
1081             s = snew;
1082         }
1083         return s;
1084     }
1086     g_assert_not_reached();
1087     return NULL;
1090 /**
1091  * Allocates unique integer keys.
1092  * \param numkeys Number of keys required.
1093  * \return First allocated key; hence if the returned key is n
1094  * you can use n, n + 1, ..., n + (numkeys - 1)
1095  */
1096 unsigned
1097 sp_item_display_key_new(unsigned numkeys)
1099     static unsigned dkey = 0;
1101     dkey += numkeys;
1103     return dkey - numkeys;
1106 NRArenaItem *
1107 sp_item_invoke_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
1109     g_assert(item != NULL);
1110     g_assert(SP_IS_ITEM(item));
1111     g_assert(arena != NULL);
1112     g_assert(NR_IS_ARENA(arena));
1114     NRArenaItem *ai = NULL;
1115     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->show) {
1116         ai = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->show(item, arena, key, flags);
1117     }
1119     if (ai != NULL) {
1120         item->display = sp_item_view_new_prepend(item->display, item, flags, key, ai);
1121         nr_arena_item_set_transform(ai, item->transform);
1122         nr_arena_item_set_opacity(ai, SP_SCALE24_TO_FLOAT(item->style->opacity.value));
1123         nr_arena_item_set_visible(ai, !item->isHidden());
1124         nr_arena_item_set_sensitive(ai, item->sensitive);
1125         if (item->clip_ref->getObject()) {
1126             SPClipPath *cp = item->clip_ref->getObject();
1128             if (!item->display->arenaitem->key) {
1129                 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
1130             }
1131             int clip_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
1133             // Show and set clip
1134             NRArenaItem *ac = sp_clippath_show(cp, arena, clip_key);
1135             nr_arena_item_set_clip(ai, ac);
1136             nr_arena_item_unref(ac);
1138             // Update bbox, in case the clip uses bbox units
1139             NRRect bbox;
1140             sp_item_invoke_bbox(item, &bbox, Geom::identity(), TRUE);
1141             sp_clippath_set_bbox(SP_CLIPPATH(cp), clip_key, &bbox);
1142             cp->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1143         }
1144         if (item->mask_ref->getObject()) {
1145             SPMask *mask = item->mask_ref->getObject();
1147             if (!item->display->arenaitem->key) {
1148                 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
1149             }
1150             int mask_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
1152             // Show and set mask
1153             NRArenaItem *ac = sp_mask_show(mask, arena, mask_key);
1154             nr_arena_item_set_mask(ai, ac);
1155             nr_arena_item_unref(ac);
1157             // Update bbox, in case the mask uses bbox units
1158             NRRect bbox;
1159             sp_item_invoke_bbox(item, &bbox, Geom::identity(), TRUE);
1160             sp_mask_set_bbox(SP_MASK(mask), mask_key, &bbox);
1161             mask->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1162         }
1163         NR_ARENA_ITEM_SET_DATA(ai, item);
1164         Geom::OptRect item_bbox;
1165         sp_item_invoke_bbox(item, item_bbox, Geom::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
1166         nr_arena_item_set_item_bbox(ai, item_bbox);
1167     }
1169     return ai;
1172 void
1173 sp_item_invoke_hide(SPItem *item, unsigned key)
1175     g_assert(item != NULL);
1176     g_assert(SP_IS_ITEM(item));
1178     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide) {
1179         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide(item, key);
1180     }
1182     SPItemView *ref = NULL;
1183     SPItemView *v = item->display;
1184     while (v != NULL) {
1185         SPItemView *next = v->next;
1186         if (v->key == key) {
1187             if (item->clip_ref->getObject()) {
1188                 sp_clippath_hide(item->clip_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
1189                 nr_arena_item_set_clip(v->arenaitem, NULL);
1190             }
1191             if (item->mask_ref->getObject()) {
1192                 sp_mask_hide(item->mask_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
1193                 nr_arena_item_set_mask(v->arenaitem, NULL);
1194             }
1195             if (!ref) {
1196                 item->display = v->next;
1197             } else {
1198                 ref->next = v->next;
1199             }
1200             nr_arena_item_unparent(v->arenaitem);
1201             nr_arena_item_unref(v->arenaitem);
1202             g_free(v);
1203         } else {
1204             ref = v;
1205         }
1206         v = next;
1207     }
1210 // Adjusters
1212 void
1213 sp_item_adjust_pattern (SPItem *item, Geom::Matrix const &postmul, bool set)
1215     SPStyle *style = item->style;
1217     if ( style && style->fill.isPaintserver() ) {
1218         SPPaintServer *server = item->style->getFillPaintServer();
1219         if ( SP_IS_PATTERN(server) ) {
1220             SPPattern *pattern = sp_pattern_clone_if_necessary(item, SP_PATTERN(server), "fill");
1221             sp_pattern_transform_multiply(pattern, postmul, set);
1222         }
1223     }
1225     if ( style && style->stroke.isPaintserver() ) {
1226         SPPaintServer *server = item->style->getStrokePaintServer();
1227         if ( SP_IS_PATTERN(server) ) {
1228             SPPattern *pattern = sp_pattern_clone_if_necessary(item, SP_PATTERN(server), "stroke");
1229             sp_pattern_transform_multiply(pattern, postmul, set);
1230         }
1231     }
1234 void sp_item_adjust_gradient( SPItem *item, Geom::Matrix const &postmul, bool set )
1236     SPStyle *style = item->style;
1238     if ( style && style->fill.isPaintserver() ) {
1239         SPPaintServer *server = item->style->getFillPaintServer();
1240         if ( SP_IS_GRADIENT(server) ) {
1242             /**
1243              * \note Bbox units for a gradient are generally a bad idea because
1244              * with them, you cannot preserve the relative position of the
1245              * object and its gradient after rotation or skew. So now we
1246              * convert them to userspace units which are easy to keep in sync
1247              * just by adding the object's transform to gradientTransform.
1248              * \todo FIXME: convert back to bbox units after transforming with
1249              * the item, so as to preserve the original units.
1250              */
1251             SPGradient *gradient = sp_gradient_convert_to_userspace( SP_GRADIENT(server), item, "fill" );
1253             sp_gradient_transform_multiply( gradient, postmul, set );
1254         }
1255     }
1257     if ( style && style->stroke.isPaintserver() ) {
1258         SPPaintServer *server = item->style->getStrokePaintServer();
1259         if ( SP_IS_GRADIENT(server) ) {
1260             SPGradient *gradient = sp_gradient_convert_to_userspace( SP_GRADIENT(server), item, "stroke" );
1261             sp_gradient_transform_multiply( gradient, postmul, set );
1262         }
1263     }
1266 void sp_item_adjust_stroke( SPItem *item, gdouble ex )
1268     SPStyle *style = item->style;
1270     if ( style && !style->stroke.isNone() && !NR_DF_TEST_CLOSE(ex, 1.0, NR_EPSILON) ) {
1271         style->stroke_width.computed *= ex;
1272         style->stroke_width.set = TRUE;
1274         if ( style->stroke_dash.n_dash != 0 ) {
1275             for (int i = 0; i < style->stroke_dash.n_dash; i++) {
1276                 style->stroke_dash.dash[i] *= ex;
1277             }
1278             style->stroke_dash.offset *= ex;
1279         }
1281         item->updateRepr();
1282     }
1285 /**
1286  * Find out the inverse of previous transform of an item (from its repr)
1287  */
1288 Geom::Matrix
1289 sp_item_transform_repr (SPItem *item)
1291     Geom::Matrix t_old(Geom::identity());
1292     gchar const *t_attr = item->repr->attribute("transform");
1293     if (t_attr) {
1294         Geom::Matrix t;
1295         if (sp_svg_transform_read(t_attr, &t)) {
1296             t_old = t;
1297         }
1298     }
1300     return t_old;
1304 /**
1305  * Recursively scale stroke width in \a item and its children by \a expansion.
1306  */
1307 void
1308 sp_item_adjust_stroke_width_recursive(SPItem *item, double expansion)
1310     sp_item_adjust_stroke (item, expansion);
1312 // A clone's child is the ghost of its original - we must not touch it, skip recursion
1313     if (item && SP_IS_USE(item))
1314         return;
1316     for (SPObject *o = item->children; o != NULL; o = o->next) {
1317         if (SP_IS_ITEM(o))
1318             sp_item_adjust_stroke_width_recursive(SP_ITEM(o), expansion);
1319     }
1322 /**
1323  * Recursively adjust rx and ry of rects.
1324  */
1325 void
1326 sp_item_adjust_rects_recursive(SPItem *item, Geom::Matrix advertized_transform)
1328     if (SP_IS_RECT (item)) {
1329         sp_rect_compensate_rxry (SP_RECT(item), advertized_transform);
1330     }
1332     for (SPObject *o = item->children; o != NULL; o = o->next) {
1333         if (SP_IS_ITEM(o))
1334             sp_item_adjust_rects_recursive(SP_ITEM(o), advertized_transform);
1335     }
1338 /**
1339  * Recursively compensate pattern or gradient transform.
1340  */
1341 void
1342 sp_item_adjust_paint_recursive (SPItem *item, Geom::Matrix advertized_transform, Geom::Matrix t_ancestors, bool is_pattern)
1344 // _Before_ full pattern/gradient transform: t_paint * t_item * t_ancestors
1345 // _After_ full pattern/gradient transform: t_paint_new * t_item * t_ancestors * advertised_transform
1346 // By equating these two expressions we get t_paint_new = t_paint * paint_delta, where:
1347     Geom::Matrix t_item = sp_item_transform_repr (item);
1348     Geom::Matrix paint_delta = t_item * t_ancestors * advertized_transform * t_ancestors.inverse() * t_item.inverse();
1350 // Within text, we do not fork gradients, and so must not recurse to avoid double compensation;
1351 // also we do not recurse into clones, because a clone's child is the ghost of its original -
1352 // we must not touch it
1353     if (!(item && (SP_IS_TEXT(item) || SP_IS_USE(item)))) {
1354         for (SPObject *o = item->children; o != NULL; o = o->next) {
1355             if (SP_IS_ITEM(o)) {
1356 // At the level of the transformed item, t_ancestors is identity;
1357 // below it, it is the accmmulated chain of transforms from this level to the top level
1358                 sp_item_adjust_paint_recursive (SP_ITEM(o), advertized_transform, t_item * t_ancestors, is_pattern);
1359             }
1360         }
1361     }
1363 // We recursed into children first, and are now adjusting this object second;
1364 // this is so that adjustments in a tree are done from leaves up to the root,
1365 // and paintservers on leaves inheriting their values from ancestors could adjust themselves properly
1366 // before ancestors themselves are adjusted, probably differently (bug 1286535)
1368     if (is_pattern)
1369         sp_item_adjust_pattern (item, paint_delta);
1370     else
1371         sp_item_adjust_gradient (item, paint_delta);
1375 void
1376 sp_item_adjust_livepatheffect (SPItem *item, Geom::Matrix const &postmul, bool set)
1378     if ( !SP_IS_LPE_ITEM(item) )
1379         return;
1381     SPLPEItem *lpeitem = SP_LPE_ITEM (item);
1382     if ( sp_lpe_item_has_path_effect(lpeitem) ) {
1383         sp_lpe_item_fork_path_effects_if_necessary(lpeitem);
1385         // now that all LPEs are forked_if_necessary, we can apply the transform
1386         PathEffectList effect_list =  sp_lpe_item_get_effect_list(lpeitem);
1387         for (PathEffectList::iterator it = effect_list.begin(); it != effect_list.end(); it++)
1388         {
1389             LivePathEffectObject *lpeobj = (*it)->lpeobject;
1390             if (lpeobj && lpeobj->get_lpe()) {
1391                 Inkscape::LivePathEffect::Effect * effect = lpeobj->get_lpe();
1392                 effect->transform_multiply(postmul, set);
1393             }
1394         }
1395     }
1398 /**
1399  * Set a new transform on an object.
1400  *
1401  * Compensate for stroke scaling and gradient/pattern fill transform, if
1402  * necessary. Call the object's set_transform method if transforms are
1403  * stored optimized. Send _transformed_signal. Invoke _write method so that
1404  * the repr is updated with the new transform.
1405  */
1406 void
1407 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, Geom::Matrix const &transform, Geom::Matrix const *adv, bool compensate)
1409     g_return_if_fail(item != NULL);
1410     g_return_if_fail(SP_IS_ITEM(item));
1411     g_return_if_fail(repr != NULL);
1413     // calculate the relative transform, if not given by the adv attribute
1414     Geom::Matrix advertized_transform;
1415     if (adv != NULL) {
1416         advertized_transform = *adv;
1417     } else {
1418         advertized_transform = sp_item_transform_repr (item).inverse() * transform;
1419     }
1421     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1422     if (compensate) {
1424          // recursively compensate for stroke scaling, depending on user preference
1425         if (!prefs->getBool("/options/transform/stroke", true)) {
1426             double const expansion = 1. / advertized_transform.descrim();
1427             sp_item_adjust_stroke_width_recursive(item, expansion);
1428         }
1430         // recursively compensate rx/ry of a rect if requested
1431         if (!prefs->getBool("/options/transform/rectcorners", true)) {
1432             sp_item_adjust_rects_recursive(item, advertized_transform);
1433         }
1435         // recursively compensate pattern fill if it's not to be transformed
1436         if (!prefs->getBool("/options/transform/pattern", true)) {
1437             sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), Geom::identity(), true);
1438         }
1439         /// \todo FIXME: add the same else branch as for gradients below, to convert patterns to userSpaceOnUse as well
1440         /// recursively compensate gradient fill if it's not to be transformed
1441         if (!prefs->getBool("/options/transform/gradient", true)) {
1442             sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), Geom::identity(), false);
1443         } else {
1444             // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
1445             // it here _before_ the new transform is set, so as to use the pre-transform bbox
1446             sp_item_adjust_paint_recursive (item, Geom::identity(), Geom::identity(), false);
1447         }
1449     } // endif(compensate)
1451     gint preserve = prefs->getBool("/options/preservetransform/value", 0);
1452     Geom::Matrix transform_attr (transform);
1453     if ( // run the object's set_transform (i.e. embed transform) only if:
1454          ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform && // it does have a set_transform method
1455              !preserve && // user did not chose to preserve all transforms
1456              !item->clip_ref->getObject() && // the object does not have a clippath
1457              !item->mask_ref->getObject() && // the object does not have a mask
1458          !(!transform.isTranslation() && item->style && item->style->getFilter())
1459              // the object does not have a filter, or the transform is translation (which is supposed to not affect filters)
1460         ) {
1461         transform_attr = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform(item, transform);
1462     }
1463     sp_item_set_item_transform(item, transform_attr);
1465     // Note: updateRepr comes before emitting the transformed signal since
1466     // it causes clone SPUse's copy of the original object to brought up to
1467     // date with the original.  Otherwise, sp_use_bbox returns incorrect
1468     // values if called in code handling the transformed signal.
1469     item->updateRepr();
1471     // send the relative transform with a _transformed_signal
1472     item->_transformed_signal.emit(&advertized_transform, item);
1475 gint
1476 sp_item_event(SPItem *item, SPEvent *event)
1478     g_return_val_if_fail(item != NULL, FALSE);
1479     g_return_val_if_fail(SP_IS_ITEM(item), FALSE);
1480     g_return_val_if_fail(event != NULL, FALSE);
1482     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->event)
1483         return ((SPItemClass *) G_OBJECT_GET_CLASS(item))->event(item, event);
1485     return FALSE;
1488 /**
1489  * Sets item private transform (not propagated to repr), without compensating stroke widths,
1490  * gradients, patterns as sp_item_write_transform does.
1491  */
1492 void
1493 sp_item_set_item_transform(SPItem *item, Geom::Matrix const &transform)
1495     g_return_if_fail(item != NULL);
1496     g_return_if_fail(SP_IS_ITEM(item));
1498     if (!matrix_equalp(transform, item->transform, NR_EPSILON)) {
1499         item->transform = transform;
1500         /* The SP_OBJECT_USER_MODIFIED_FLAG_B is used to mark the fact that it's only a
1501            transformation.  It's apparently not used anywhere else. */
1502         item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_USER_MODIFIED_FLAG_B);
1503         sp_item_rm_unsatisfied_cns(*item);
1504     }
1507 void
1508 sp_item_convert_item_to_guides(SPItem *item) {
1509     g_return_if_fail(item != NULL);
1510     g_return_if_fail(SP_IS_ITEM(item));
1512     /* Use derived method if present ... */
1513     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->convert_to_guides) {
1514         (*((SPItemClass *) G_OBJECT_GET_CLASS(item))->convert_to_guides)(item);
1515         return;
1516     }
1518     /* .. otherwise simply place the guides around the item's bounding box */
1520     sp_item_convert_to_guides(item);
1524 /**
1525  * \pre \a ancestor really is an ancestor (\>=) of \a object, or NULL.
1526  *   ("Ancestor (\>=)" here includes as far as \a object itself.)
1527  */
1528 Geom::Matrix
1529 i2anc_affine(SPObject const *object, SPObject const *const ancestor) {
1530     Geom::Matrix ret(Geom::identity());
1531     g_return_val_if_fail(object != NULL, ret);
1533     /* stop at first non-renderable ancestor */
1534     while ( object != ancestor && SP_IS_ITEM(object) ) {
1535         if (SP_IS_ROOT(object)) {
1536             ret *= SP_ROOT(object)->c2p;
1537         } else {
1538             ret *= SP_ITEM(object)->transform;
1539         }
1540         object = object->parent;
1541     }
1542     return ret;
1545 Geom::Matrix
1546 i2i_affine(SPObject const *src, SPObject const *dest) {
1547     g_return_val_if_fail(src != NULL && dest != NULL, Geom::identity());
1548     SPObject const *ancestor = src->nearestCommonAncestor(dest);
1549     return i2anc_affine(src, ancestor) * i2anc_affine(dest, ancestor).inverse();
1552 Geom::Matrix SPItem::getRelativeTransform(SPObject const *dest) const {
1553     return i2i_affine(this, dest);
1556 /**
1557  * Returns the accumulated transformation of the item and all its ancestors, including root's viewport.
1558  * \pre (item != NULL) and SP_IS_ITEM(item).
1559  */
1560 Geom::Matrix sp_item_i2doc_affine(SPItem const *item)
1562     return i2anc_affine(item, NULL);
1565 /**
1566  * Returns the transformation from item to desktop coords
1567  */
1568 Geom::Matrix sp_item_i2d_affine(SPItem const *item)
1570     g_assert(item != NULL);
1571     g_assert(SP_IS_ITEM(item));
1573     Geom::Matrix const ret( sp_item_i2doc_affine(item)
1574                           * Geom::Scale(1, -1)
1575                           * Geom::Translate(0, sp_document_height(item->document)) );
1576     return ret;
1579 void sp_item_set_i2d_affine(SPItem *item, Geom::Matrix const &i2dt)
1581     g_return_if_fail( item != NULL );
1582     g_return_if_fail( SP_IS_ITEM(item) );
1584     Geom::Matrix dt2p; /* desktop to item parent transform */
1585     if (item->parent) {
1586         dt2p = sp_item_i2d_affine(static_cast<SPItem *>(item->parent)).inverse();
1587     } else {
1588         dt2p = ( Geom::Translate(0, -sp_document_height(item->document))
1589                  * Geom::Scale(1, -1) );
1590     }
1592     Geom::Matrix const i2p( i2dt * dt2p );
1593     sp_item_set_item_transform(item, i2p);
1597 /**
1598  * should rather be named "sp_item_d2i_affine" to match "sp_item_i2d_affine" (or vice versa)
1599  */
1600 Geom::Matrix
1601 sp_item_dt2i_affine(SPItem const *item)
1603     /* fixme: Implement the right way (Lauris) */
1604     return sp_item_i2d_affine(item).inverse();
1607 /* Item views */
1609 static SPItemView *
1610 sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem)
1612     SPItemView *new_view;
1614     g_assert(item != NULL);
1615     g_assert(SP_IS_ITEM(item));
1616     g_assert(arenaitem != NULL);
1617     g_assert(NR_IS_ARENA_ITEM(arenaitem));
1619     new_view = g_new(SPItemView, 1);
1621     new_view->next = list;
1622     new_view->flags = flags;
1623     new_view->key = key;
1624     new_view->arenaitem = arenaitem;
1626     return new_view;
1629 static SPItemView *
1630 sp_item_view_list_remove(SPItemView *list, SPItemView *view)
1632     if (view == list) {
1633         list = list->next;
1634     } else {
1635         SPItemView *prev;
1636         prev = list;
1637         while (prev->next != view) prev = prev->next;
1638         prev->next = view->next;
1639     }
1641     nr_arena_item_unref(view->arenaitem);
1642     g_free(view);
1644     return list;
1647 /**
1648  * Return the arenaitem corresponding to the given item in the display
1649  * with the given key
1650  */
1651 NRArenaItem *
1652 sp_item_get_arenaitem(SPItem *item, unsigned key)
1654     for ( SPItemView *iv = item->display ; iv ; iv = iv->next ) {
1655         if ( iv->key == key ) {
1656             return iv->arenaitem;
1657         }
1658     }
1660     return NULL;
1663 int
1664 sp_item_repr_compare_position(SPItem *first, SPItem *second)
1666     return sp_repr_compare_position(first->repr,
1667                                     second->repr);
1670 SPItem *
1671 sp_item_first_item_child (SPObject *obj)
1673     for ( SPObject *iter = sp_object_first_child(obj) ; iter ; iter = iter->next) {
1674         if ( SP_IS_ITEM(iter) ) {
1675             return SP_ITEM (iter);
1676         }
1677     }
1678     return NULL;
1681 void
1682 sp_item_convert_to_guides(SPItem *item) {
1683     SPDesktop *dt = inkscape_active_desktop();
1684     SPNamedView *nv = sp_desktop_namedview(dt);
1685     (void)nv;
1687     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1688     int prefs_bbox = prefs->getInt("/tools/bounding_box", 0);
1689     SPItem::BBoxType bbox_type = (prefs_bbox ==0)?
1690         SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
1692     Geom::OptRect bbox = sp_item_bbox_desktop(item, bbox_type);
1693     if (!bbox) {
1694         g_warning ("Cannot determine item's bounding box during conversion to guides.\n");
1695         return;
1696     }
1698     std::list<std::pair<Geom::Point, Geom::Point> > pts;
1700     Geom::Point A((*bbox).min());
1701     Geom::Point C((*bbox).max());
1702     Geom::Point B(A[Geom::X], C[Geom::Y]);
1703     Geom::Point D(C[Geom::X], A[Geom::Y]);
1705     pts.push_back(std::make_pair(A, B));
1706     pts.push_back(std::make_pair(B, C));
1707     pts.push_back(std::make_pair(C, D));
1708     pts.push_back(std::make_pair(D, A));
1710     sp_guide_pt_pairs_to_guides(dt, pts);
1713 /*
1714   Local Variables:
1715   mode:c++
1716   c-file-style:"stroustrup"
1717   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1718   indent-tabs-mode:nil
1719   fill-column:99
1720   End:
1721 */
1722 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :