Code

Remove INKSCAPE_VERSION from menus-skeleton.h
[inkscape.git] / src / sp-line.cpp
1 #define __SP_LINE_C__
3 /*
4  * SVG <line> implementation
5  *
6  * Authors:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *
9  * Copyright (C) 1999-2002 Lauris Kaplinski
10  *
11  * Released under GNU GPL, read the file 'COPYING' for more information
12  */
14 #ifdef HAVE_CONFIG_H
15 # include <config.h>
16 #endif
17 #include "attributes.h"
18 #include "style.h"
19 #include "sp-line.h"
20 #include "display/curve.h"
21 #include <glibmm/i18n.h>
22 #include <libnr/nr-matrix-fns.h>
23 #include <xml/repr.h>
24 #include "document.h"
26 static void sp_line_class_init (SPLineClass *klass);
27 static void sp_line_init (SPLine *line);
29 static void sp_line_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
30 static void sp_line_set (SPObject *object, unsigned int key, const gchar *value);
31 static Inkscape::XML::Node *sp_line_write (SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
33 static gchar *sp_line_description (SPItem * item);
34 static Geom::Matrix sp_line_set_transform(SPItem *item, Geom::Matrix const &xform);
36 static void sp_line_update (SPObject *object, SPCtx *ctx, guint flags);
37 static void sp_line_set_shape (SPShape *shape);
39 static SPShapeClass *parent_class;
41 GType
42 sp_line_get_type (void)
43 {
44         static GType line_type = 0;
46         if (!line_type) {
47                 GTypeInfo line_info = {
48                         sizeof (SPLineClass),
49                         NULL,   /* base_init */
50                         NULL,   /* base_finalize */
51                         (GClassInitFunc) sp_line_class_init,
52                         NULL,   /* klass_finalize */
53                         NULL,   /* klass_data */
54                         sizeof (SPLine),
55                         16,     /* n_preallocs */
56                         (GInstanceInitFunc) sp_line_init,
57                         NULL,   /* value_table */
58                 };
59                 line_type = g_type_register_static (SP_TYPE_SHAPE, "SPLine", &line_info, (GTypeFlags)0);
60         }
61         return line_type;
62 }
64 static void
65 sp_line_class_init (SPLineClass *klass)
66 {
67         parent_class = (SPShapeClass *) g_type_class_ref (SP_TYPE_SHAPE);
69         SPObjectClass *sp_object_class = (SPObjectClass *) klass;
70         sp_object_class->build = sp_line_build;
71         sp_object_class->set = sp_line_set;
72         sp_object_class->write = sp_line_write;
74         SPItemClass *item_class = (SPItemClass *) klass;
75         item_class->description = sp_line_description;
76         item_class->set_transform = sp_line_set_transform;
78         sp_object_class->update = sp_line_update;
80         SPShapeClass *shape_class = (SPShapeClass *) klass;
81         shape_class->set_shape = sp_line_set_shape;
82 }
84 static void
85 sp_line_init (SPLine * line)
86 {
87         line->x1.unset();
88         line->y1.unset();
89         line->x2.unset();
90         line->y2.unset();
91 }
94 static void
95 sp_line_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr)
96 {
97         if (((SPObjectClass *) parent_class)->build) {
98                 ((SPObjectClass *) parent_class)->build (object, document, repr);
99         }
101         sp_object_read_attr (object, "x1");
102         sp_object_read_attr (object, "y1");
103         sp_object_read_attr (object, "x2");
104         sp_object_read_attr (object, "y2");
107 static void
108 sp_line_set (SPObject *object, unsigned int key, const gchar *value)
110         SPLine * line = SP_LINE (object);
112         /* fixme: we should really collect updates */
114         switch (key) {
115         case SP_ATTR_X1:
116                 line->x1.readOrUnset(value);
117                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
118                 break;
119         case SP_ATTR_Y1:
120                 line->y1.readOrUnset(value);
121                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
122                 break;
123         case SP_ATTR_X2:
124                 line->x2.readOrUnset(value);
125                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
126                 break;
127         case SP_ATTR_Y2:
128                 line->y2.readOrUnset(value);
129                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
130                 break;
131         default:
132                 if (((SPObjectClass *) parent_class)->set)
133                         ((SPObjectClass *) parent_class)->set (object, key, value);
134                 break;
135         }
138 static void
139 sp_line_update (SPObject *object, SPCtx *ctx, guint flags)
141         if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
142                 SPLine *line = SP_LINE (object);
144                 SPStyle const *style = object->style;
145                 double const d = 1.0 / NR::expansion(((SPItemCtx const *) ctx)->i2vp);
146                 double const em = style->font_size.computed;
147                 double const ex = em * 0.5;  // fixme: get from pango or libnrtype.
148                 line->x1.update(em, ex, d);
149                 line->x2.update(em, ex, d);
150                 line->y1.update(em, ex, d);
151                 line->y2.update(em, ex, d);
153                 sp_shape_set_shape ((SPShape *) object);
154         }
156         if (((SPObjectClass *) parent_class)->update)
157                 ((SPObjectClass *) parent_class)->update (object, ctx, flags);
161 static Inkscape::XML::Node *
162 sp_line_write (SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
164         SPLine *line  = SP_LINE (object);
166         if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
167                 repr = xml_doc->createElement("svg:line");
168         }
170         if (repr != SP_OBJECT_REPR (object)) {
171                 repr->mergeFrom(SP_OBJECT_REPR (object), "id");
172         }
174         sp_repr_set_svg_double(repr, "x1", line->x1.computed);
175         sp_repr_set_svg_double(repr, "y1", line->y1.computed);
176         sp_repr_set_svg_double(repr, "x2", line->x2.computed);
177         sp_repr_set_svg_double(repr, "y2", line->y2.computed);
179         if (((SPObjectClass *) (parent_class))->write)
180                 ((SPObjectClass *) (parent_class))->write (object, xml_doc, repr, flags);
182         return repr;
185 static gchar *
186 sp_line_description(SPItem */*item*/)
188     return g_strdup(_("<b>Line</b>"));
191 static Geom::Matrix
192 sp_line_set_transform (SPItem *item, Geom::Matrix const &xform)
194         SPLine *line = SP_LINE (item);
195         Geom::Point points[2];
197         points[0] = Geom::Point(line->x1.computed, line->y1.computed);
198         points[1] = Geom::Point(line->x2.computed, line->y2.computed);
200         points[0] *= xform;
201         points[1] *= xform;
203         line->x1.computed = points[0][Geom::X];
204         line->y1.computed = points[0][Geom::Y];
205         line->x2.computed = points[1][Geom::X];
206         line->y2.computed = points[1][Geom::Y];
208         sp_item_adjust_stroke(item, xform.descrim());
210         SP_OBJECT (item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
212         return Geom::identity();
215 static void
216 sp_line_set_shape (SPShape *shape)
218         SPLine *line = SP_LINE (shape);
220         SPCurve *c = new SPCurve ();
222         c->moveto(line->x1.computed, line->y1.computed);
223         c->lineto(line->x2.computed, line->y2.computed);
225         sp_shape_set_curve_insync (shape, c, TRUE); // *_insync does not call update, avoiding infinite recursion when set_shape is called by update
227         c->unref();