Code

remove registerEditWidget, move this to SPDesktop::init instead; a noop change but...
[inkscape.git] / src / sp-path.cpp
1 #define __SP_PATH_C__
3 /*
4  * SVG <path> implementation
5  *
6  * Authors:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   David Turner <novalis@gnu.org>
9  *
10  * Copyright (C) 2004 David Turner
11  * Copyright (C) 1999-2002 Lauris Kaplinski
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>
23 #include "live_effects/effect.h"
24 #include "live_effects/lpeobject.h"
25 #include "live_effects/lpeobject-reference.h"
26 #include "sp-lpe-item.h"
28 #include <display/curve.h>
29 #include <libnr/nr-matrix-fns.h>
30 #include <2geom/pathvector.h>
31 #include <2geom/bezier-curve.h>
32 #include <2geom/hvlinesegment.h>
33 #include "helper/geom-curves.h"
35 #include "svg/svg.h"
36 #include "xml/repr.h"
37 #include "attributes.h"
39 #include "sp-path.h"
40 #include "sp-guide.h"
42 #include "document.h"
43 #include "desktop.h"
44 #include "desktop-handles.h"
45 #include "desktop-style.h"
46 #include "event-context.h"
47 #include "inkscape.h"
48 #include "style.h"
49 #include "message-stack.h"
50 #include "selection.h"
52 #define noPATH_VERBOSE
54 static void sp_path_class_init(SPPathClass *klass);
55 static void sp_path_init(SPPath *path);
56 static void sp_path_finalize(GObject *obj);
57 static void sp_path_release(SPObject *object);
59 static void sp_path_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
60 static void sp_path_set(SPObject *object, unsigned key, gchar const *value);
62 static Inkscape::XML::Node *sp_path_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
63 static Geom::Matrix sp_path_set_transform(SPItem *item, Geom::Matrix const &xform);
64 static gchar * sp_path_description(SPItem *item);
65 static void sp_path_convert_to_guides(SPItem *item);
67 static void sp_path_update(SPObject *object, SPCtx *ctx, guint flags);
68 static void sp_path_update_patheffect(SPLPEItem *lpeitem, bool write);
70 static SPShapeClass *parent_class;
72 /**
73  * Gets the GType object for SPPathClass
74  */
75 GType
76 sp_path_get_type(void)
77 {
78     static GType type = 0;
80     if (!type) {
81         GTypeInfo info = {
82             sizeof(SPPathClass),
83             NULL, NULL,
84             (GClassInitFunc) sp_path_class_init,
85             NULL, NULL,
86             sizeof(SPPath),
87             16,
88             (GInstanceInitFunc) sp_path_init,
89             NULL,   /* value_table */
90         };
91         type = g_type_register_static(SP_TYPE_SHAPE, "SPPath", &info, (GTypeFlags)0);
92     }
93     return type;
94 }
96 /**
97  *  Does the object-oriented work of initializing the class structure
98  *  including parent class, and registers function pointers for
99  *  the functions build, set, write, and set_transform.
100  */
101 static void
102 sp_path_class_init(SPPathClass * klass)
104     GObjectClass *gobject_class = (GObjectClass *) klass;
105     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
106     SPItemClass *item_class = (SPItemClass *) klass;
107     SPLPEItemClass *lpe_item_class = (SPLPEItemClass *) klass;
109     parent_class = (SPShapeClass *)g_type_class_peek_parent(klass);
111     gobject_class->finalize = sp_path_finalize;
113     sp_object_class->build = sp_path_build;
114     sp_object_class->release = sp_path_release;
115     sp_object_class->set = sp_path_set;
116     sp_object_class->write = sp_path_write;
117     sp_object_class->update = sp_path_update;
119     item_class->description = sp_path_description;
120     item_class->set_transform = sp_path_set_transform;
121     item_class->convert_to_guides = sp_path_convert_to_guides;
123     lpe_item_class->update_patheffect = sp_path_update_patheffect;
127 gint
128 sp_nodes_in_path(SPPath *path)
130     SPCurve *curve = SP_SHAPE(path)->curve;
131     if (!curve)
132         return 0;
133     return curve->nodes_in_path();
136 static gchar *
137 sp_path_description(SPItem * item)
139     int count = sp_nodes_in_path(SP_PATH(item));
140     if (SP_IS_LPE_ITEM(item) && sp_lpe_item_has_path_effect(SP_LPE_ITEM(item))) {
142         Glib::ustring s;
144         PathEffectList effect_list =  sp_lpe_item_get_effect_list(SP_LPE_ITEM(item));
145         for (PathEffectList::iterator it = effect_list.begin(); it != effect_list.end(); it++)
146         {
147             LivePathEffectObject *lpeobj = (*it)->lpeobject;
148             if (!lpeobj || !lpeobj->get_lpe())
149                 break;
150             if (s.empty())
151                 s = lpeobj->get_lpe()->getName();
152             else
153                 s = s + ", " + lpeobj->get_lpe()->getName();
154         }
156         return g_strdup_printf(ngettext("<b>Path</b> (%i node, path effect: %s)",
157                                         "<b>Path</b> (%i nodes, path effect: %s)",count), count, s.c_str());
158     } else {
159         return g_strdup_printf(ngettext("<b>Path</b> (%i node)",
160                                         "<b>Path</b> (%i nodes)",count), count);
161     }
164 static void
165 sp_path_convert_to_guides(SPItem *item)
167     SPPath *path = SP_PATH(item);
169     SPCurve *curve = SP_SHAPE(path)->curve;
170     if (!curve) return;
172     std::list<std::pair<Geom::Point, Geom::Point> > pts;
174     Geom::Matrix const i2d (sp_item_i2d_affine(SP_ITEM(path)));
176     Geom::PathVector const & pv = curve->get_pathvector();
177     for(Geom::PathVector::const_iterator pit = pv.begin(); pit != pv.end(); ++pit) {
178         for(Geom::Path::const_iterator cit = pit->begin(); cit != pit->end_default(); ++cit) {
179             // only add curves for straight line segments
180             if( is_straight_curve(*cit) )
181             {
182                 pts.push_back(std::make_pair(cit->initialPoint() * i2d, cit->finalPoint() * i2d));
183             }
184         }
185     }
187     sp_guide_pt_pairs_to_guides(inkscape_active_desktop(), pts);
190 /**
191  * Initializes an SPPath.
192  */
193 static void
194 sp_path_init(SPPath *path)
196     new (&path->connEndPair) SPConnEndPair(path);
198     path->original_curve = NULL;
201 static void
202 sp_path_finalize(GObject *obj)
204     SPPath *path = (SPPath *) obj;
206     path->connEndPair.~SPConnEndPair();
209 /**
210  *  Given a repr, this sets the data items in the path object such as
211  *  fill & style attributes, markers, and CSS properties.
212  */
213 static void
214 sp_path_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
216     /* Are these calls actually necessary? */
217     sp_object_read_attr(object, "marker");
218     sp_object_read_attr(object, "marker-start");
219     sp_object_read_attr(object, "marker-mid");
220     sp_object_read_attr(object, "marker-end");
222     sp_conn_end_pair_build(object);
224     if (((SPObjectClass *) parent_class)->build) {
225         ((SPObjectClass *) parent_class)->build(object, document, repr);
226     }
228     sp_object_read_attr(object, "inkscape:original-d");
229     sp_object_read_attr(object, "d");
231     /* d is a required attribute */
232     gchar const *d = sp_object_getAttribute(object, "d", NULL);
233     if (d == NULL) {
234         sp_object_set(object, sp_attribute_lookup("d"), "");
235     }
238 static void
239 sp_path_release(SPObject *object)
241     SPPath *path = SP_PATH(object);
243     path->connEndPair.release();
245     if (path->original_curve) {
246         path->original_curve = path->original_curve->unref();
247     }
249     if (((SPObjectClass *) parent_class)->release) {
250         ((SPObjectClass *) parent_class)->release(object);
251     }
254 /**
255  *  Sets a value in the path object given by 'key', to 'value'.  This is used
256  *  for setting attributes and markers on a path object.
257  */
258 static void
259 sp_path_set(SPObject *object, unsigned int key, gchar const *value)
261     SPPath *path = (SPPath *) object;
263     switch (key) {
264         case SP_ATTR_INKSCAPE_ORIGINAL_D:
265                 if (value) {
266                     Geom::PathVector pv = sp_svg_read_pathv(value);
267                     SPCurve *curve = new SPCurve(pv);
268                     if (curve) {
269                         sp_path_set_original_curve(path, curve, TRUE, true);
270                         curve->unref();
271                     }
272                 } else {
273                     sp_path_set_original_curve(path, NULL, TRUE, true);
274                 }
275                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
276             break;
277        case SP_ATTR_D:
278                 if (value) {
279                     Geom::PathVector pv = sp_svg_read_pathv(value);
280                     SPCurve *curve = new SPCurve(pv);
281                     if (curve) {
282                         sp_shape_set_curve((SPShape *) path, curve, TRUE);
283                         curve->unref();
284                     }
285                 } else {
286                     sp_shape_set_curve((SPShape *) path, NULL, TRUE);
287                 }
288                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
289             break;
290         case SP_PROP_MARKER:
291         case SP_PROP_MARKER_START:
292         case SP_PROP_MARKER_MID:
293         case SP_PROP_MARKER_END:
294             sp_shape_set_marker(object, key, value);
295             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
296             break;
297         case SP_ATTR_CONNECTOR_TYPE:
298         case SP_ATTR_CONNECTION_START:
299         case SP_ATTR_CONNECTION_END:
300             path->connEndPair.setAttr(key, value);
301             break;
302         default:
303             if (((SPObjectClass *) parent_class)->set) {
304                 ((SPObjectClass *) parent_class)->set(object, key, value);
305             }
306             break;
307     }
310 /**
311  *
312  * Writes the path object into a Inkscape::XML::Node
313  */
314 static Inkscape::XML::Node *
315 sp_path_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
317     SPShape *shape = (SPShape *) object;
319     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
320         repr = xml_doc->createElement("svg:path");
321     }
323     if ( shape->curve != NULL ) {
324         gchar *str = sp_svg_write_path(shape->curve->get_pathvector());
325         repr->setAttribute("d", str);
326         g_free(str);
327     } else {
328         repr->setAttribute("d", NULL);
329     }
331     if (flags & SP_OBJECT_WRITE_EXT) {
332         SPPath *path = (SPPath *) object;
333         if ( path->original_curve != NULL ) {
334             gchar *str = sp_svg_write_path(path->original_curve->get_pathvector());
335             repr->setAttribute("inkscape:original-d", str);
336             g_free(str);
337         } else {
338             repr->setAttribute("inkscape:original-d", NULL);
339         }
340     }
342     SP_PATH(shape)->connEndPair.writeRepr(repr);
344     if (((SPObjectClass *)(parent_class))->write) {
345         ((SPObjectClass *)(parent_class))->write(object, xml_doc, repr, flags);
346     }
348     return repr;
351 static void
352 sp_path_update(SPObject *object, SPCtx *ctx, guint flags)
354     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
355         flags &= ~SP_OBJECT_USER_MODIFIED_FLAG_B; // since we change the description, it's not a "just translation" anymore
356     }
358     if (((SPObjectClass *) parent_class)->update) {
359         ((SPObjectClass *) parent_class)->update(object, ctx, flags);
360     }
362     SPPath *path = SP_PATH(object);
363     path->connEndPair.update();
367 /**
368  * Writes the given transform into the repr for the given item.
369  */
370 static Geom::Matrix
371 sp_path_set_transform(SPItem *item, Geom::Matrix const &xform)
373     SPShape *shape = (SPShape *) item;
374     SPPath *path = (SPPath *) item;
376     if (!shape->curve) { // 0 nodes, nothing to transform
377         return Geom::identity();
378     }
380     // Transform the original-d path if this is a valid LPE item, other else the (ordinary) path
381     if (path->original_curve && SP_IS_LPE_ITEM(item) && 
382                                 sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(item))) {
383         path->original_curve->transform(xform);
384     } else {
385         shape->curve->transform(xform);
386     }
388     // Adjust stroke
389     sp_item_adjust_stroke(item, xform.descrim());
391     // Adjust pattern fill
392     sp_item_adjust_pattern(item, xform);
394     // Adjust gradient fill
395     sp_item_adjust_gradient(item, xform);
397     // Adjust LPE
398     sp_item_adjust_livepatheffect(item, xform);
400     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
402     // nothing remains - we've written all of the transform, so return identity
403     return Geom::identity();
407 static void
408 sp_path_update_patheffect(SPLPEItem *lpeitem, bool write)
410     SPShape * const shape = (SPShape *) lpeitem;
411     SPPath * const path = (SPPath *) lpeitem;
412     Inkscape::XML::Node *repr = SP_OBJECT_REPR(shape);
414     if (path->original_curve && sp_lpe_item_has_path_effect_recursive(lpeitem)) {
415         SPCurve *curve = path->original_curve->copy();
416         /* if a path does not have an lpeitem applied, then reset the curve to the original_curve.
417          * This is very important for LPEs to work properly! (the bbox might be recalculated depending on the curve in shape)*/
418         sp_shape_set_curve_insync(shape, curve, TRUE);
420         bool success = sp_lpe_item_perform_path_effect(SP_LPE_ITEM(shape), curve);
421         if (success && write) {
422             // could also do SP_OBJECT(shape)->updateRepr();  but only the d attribute needs updating.
423             if ( shape->curve != NULL ) {
424                 gchar *str = sp_svg_write_path(shape->curve->get_pathvector());
425                 repr->setAttribute("d", str);
426                 g_free(str);
427             } else {
428                 repr->setAttribute("d", NULL);
429             }
430         } else if (!success) {
431             // LPE was unsuccesfull. Read the old 'd'-attribute.
432             if (gchar const * value = repr->attribute("d")) {
433                 Geom::PathVector pv = sp_svg_read_pathv(value);
434                 SPCurve *oldcurve = new SPCurve(pv);
435                 if (oldcurve) {
436                     sp_shape_set_curve(shape, oldcurve, TRUE);
437                     oldcurve->unref();
438                 }
439             }
440         }
441         SP_OBJECT(shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
442         curve->unref();
443     }
447 /**
448  * Adds a original_curve to the path.  If owner is specified, a reference
449  * will be made, otherwise the curve will be copied into the path.
450  * Any existing curve in the path will be unreferenced first.
451  * This routine triggers reapplication of an effect if present
452  * and also triggers a request to update the display. Does not write
453  * result to XML when write=false.
454  */
455 void
456 sp_path_set_original_curve (SPPath *path, SPCurve *curve, unsigned int owner, bool write)
458     if (path->original_curve) {
459         path->original_curve = path->original_curve->unref();
460     }
461     if (curve) {
462         if (owner) {
463             path->original_curve = curve->ref();
464         } else {
465             path->original_curve = curve->copy();
466         }
467     }
468     sp_lpe_item_update_patheffect(path, true, write);
469     SP_OBJECT(path)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
472 /**
473  * Return duplicate of original_curve (if any exists) or NULL if there is no curve
474  */
475 SPCurve *
476 sp_path_get_original_curve (SPPath *path)
478     if (path->original_curve) {
479         return path->original_curve->copy();
480     }
481     return NULL;
484 /**
485  * Return duplicate of edittable curve which is original_curve if it exists or
486  * shape->curve if not.
487  */
488 SPCurve*
489 sp_path_get_curve_for_edit (SPPath *path)
491     if (path->original_curve && SP_IS_LPE_ITEM(path) && 
492                                 sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path))) {
493         return sp_path_get_original_curve(path);
494     } else {
495         return sp_shape_get_curve( (SPShape *) path );
496     }
499 /**
500  * Return a reference to original_curve if it exists or
501  * shape->curve if not.
502  */
503 const SPCurve*
504 sp_path_get_curve_reference (SPPath *path)
506     if (path->original_curve && SP_IS_LPE_ITEM(path) && 
507                                 sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path))) {
508         return path->original_curve;
509     } else {
510         return path->curve;
511     }
514 /*
515   Local Variables:
516   mode:c++
517   c-file-style:"stroustrup"
518   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
519   indent-tabs-mode:nil
520   fill-column:99
521   End:
522 */
523 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :