Code

continue switching sp_repr_new* over to XML::Document::create*
[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::Node *repr, guint flags);
33 static gchar *sp_line_description (SPItem * item);
34 static NR::Matrix sp_line_set_transform(SPItem *item, NR::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::Node *repr, guint flags)
164         SPLine *line  = SP_LINE (object);
166         if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
167                 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
168                 repr = xml_doc->createElement("svg:line");
169         }
171         if (repr != SP_OBJECT_REPR (object)) {
172                 repr->mergeFrom(SP_OBJECT_REPR (object), "id");
173         }
175         sp_repr_set_svg_double(repr, "x1", line->x1.computed);
176         sp_repr_set_svg_double(repr, "y1", line->y1.computed);
177         sp_repr_set_svg_double(repr, "x2", line->x2.computed);
178         sp_repr_set_svg_double(repr, "y2", line->y2.computed);
180         if (((SPObjectClass *) (parent_class))->write)
181                 ((SPObjectClass *) (parent_class))->write (object, repr, flags);
183         return repr;
186 static gchar *
187 sp_line_description(SPItem *item)
189         return g_strdup(_("<b>Line</b>"));
192 static NR::Matrix
193 sp_line_set_transform (SPItem *item, NR::Matrix const &xform)
195         SPLine *line = SP_LINE (item);
196         NR::Point points[2];
198         points[0] = NR::Point(line->x1.computed, line->y1.computed);
199         points[1] = NR::Point(line->x2.computed, line->y2.computed);
201         points[0] *= xform;
202         points[1] *= xform;
204         line->x1.computed = points[0][NR::X];
205         line->y1.computed = points[0][NR::Y];
206         line->x2.computed = points[1][NR::X];
207         line->y2.computed = points[1][NR::Y];
209         sp_item_adjust_stroke(item, NR::expansion(xform));
211         SP_OBJECT (item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
213         return NR::identity();
216 static void
217 sp_line_set_shape (SPShape *shape)
219         SPLine *line = SP_LINE (shape);
221         SPCurve *c = sp_curve_new ();
223         sp_curve_moveto (c, line->x1.computed, line->y1.computed);
224         sp_curve_lineto (c, line->x2.computed, line->y2.computed);
226         sp_shape_set_curve_insync (shape, c, TRUE); // *_insync does not call update, avoiding infinite recursion when set_shape is called by update
228         sp_curve_unref (c);