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 "sp-guide.h"
21 #include "display/curve.h"
22 #include <glibmm/i18n.h>
23 #include <libnr/nr-matrix-fns.h>
24 #include <xml/repr.h>
25 #include "document.h"
26 #include "inkscape.h"
28 //static void sp_line_class_init (SPLineClass *klass);
29 //static void sp_line_init (SPLine *line);
31 //static void sp_line_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
32 //static void sp_line_set (SPObject *object, unsigned int key, const gchar *value);
33 //static Inkscape::XML::Node *sp_line_write (SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
35 //static gchar *sp_line_description (SPItem * item);
36 //static Geom::Matrix sp_line_set_transform(SPItem *item, Geom::Matrix const &xform);
38 //static void sp_line_update (SPObject *object, SPCtx *ctx, guint flags);
39 //static void sp_line_set_shape (SPShape *shape);
40 //static void sp_line_convert_to_guides(SPItem *item);
42 //static SPShapeClass *static_parent_class;
43 SPShapeClass * SPLineClass::static_parent_class=0;
45 GType
46 SPLine::sp_line_get_type (void)
47 {
48 static GType line_type = 0;
50 if (!line_type) {
51 GTypeInfo line_info = {
52 sizeof (SPLineClass),
53 NULL, /* base_init */
54 NULL, /* base_finalize */
55 (GClassInitFunc) SPLineClass::sp_line_class_init,
56 NULL, /* klass_finalize */
57 NULL, /* klass_data */
58 sizeof (SPLine),
59 16, /* n_preallocs */
60 (GInstanceInitFunc) sp_line_init,
61 NULL, /* value_table */
62 };
63 line_type = g_type_register_static (SP_TYPE_SHAPE, "SPLine", &line_info, (GTypeFlags)0);
64 }
65 return line_type;
66 }
69 void
70 SPLineClass::sp_line_class_init (SPLineClass *klass)
71 {
72 SPLineClass::static_parent_class = (SPShapeClass *) g_type_class_ref (SP_TYPE_SHAPE);
74 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
75 sp_object_class->build = SPLine::sp_line_build;
76 sp_object_class->set = SPLine::sp_line_set;
77 sp_object_class->write = SPLine::sp_line_write;
79 SPItemClass *item_class = (SPItemClass *) klass;
80 item_class->description = SPLine::sp_line_description;
81 item_class->set_transform = SPLine::sp_line_set_transform;
82 item_class->convert_to_guides = SPLine::sp_line_convert_to_guides;
84 sp_object_class->update = SPLine::sp_line_update;
86 SPShapeClass *shape_class = (SPShapeClass *) klass;
87 shape_class->set_shape = SPLine::sp_line_set_shape;
88 }
90 void
91 SPLine::sp_line_init (SPLine * line)
92 {
93 line->x1.unset();
94 line->y1.unset();
95 line->x2.unset();
96 line->y2.unset();
97 }
100 void
101 SPLine::sp_line_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr)
102 {
103 if (((SPObjectClass *) SPLineClass::static_parent_class)->build) {
104 ((SPObjectClass *) SPLineClass::static_parent_class)->build (object, document, repr);
105 }
107 object->readAttr( "x1");
108 object->readAttr( "y1");
109 object->readAttr( "x2");
110 object->readAttr( "y2");
111 }
113 void
114 SPLine::sp_line_set (SPObject *object, unsigned int key, const gchar *value)
115 {
116 SPLine * line = SP_LINE (object);
118 /* fixme: we should really collect updates */
120 switch (key) {
121 case SP_ATTR_X1:
122 line->x1.readOrUnset(value);
123 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
124 break;
125 case SP_ATTR_Y1:
126 line->y1.readOrUnset(value);
127 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
128 break;
129 case SP_ATTR_X2:
130 line->x2.readOrUnset(value);
131 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
132 break;
133 case SP_ATTR_Y2:
134 line->y2.readOrUnset(value);
135 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
136 break;
137 default:
138 if (((SPObjectClass *) SPLineClass::static_parent_class)->set)
139 ((SPObjectClass *) SPLineClass::static_parent_class)->set (object, key, value);
140 break;
141 }
142 }
144 void
145 SPLine::sp_line_update (SPObject *object, SPCtx *ctx, guint flags)
146 {
147 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
148 SPLine *line = SP_LINE (object);
150 SPStyle const *style = object->style;
151 SPItemCtx const *ictx = (SPItemCtx const *) ctx;
152 double const w = (ictx->vp.x1 - ictx->vp.x0);
153 double const h = (ictx->vp.y1 - ictx->vp.y0);
154 double const em = style->font_size.computed;
155 double const ex = em * 0.5; // fixme: get from pango or libnrtype.
156 line->x1.update(em, ex, w);
157 line->x2.update(em, ex, w);
158 line->y1.update(em, ex, h);
159 line->y2.update(em, ex, h);
161 ((SPShape *) object)->setShape ();
162 }
164 if (((SPObjectClass *) SPLineClass::static_parent_class)->update)
165 ((SPObjectClass *) SPLineClass::static_parent_class)->update (object, ctx, flags);
166 }
169 Inkscape::XML::Node *
170 SPLine::sp_line_write (SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
171 {
172 SPLine *line = SP_LINE (object);
174 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
175 repr = xml_doc->createElement("svg:line");
176 }
178 if (repr != SP_OBJECT_REPR (object)) {
179 repr->mergeFrom(SP_OBJECT_REPR (object), "id");
180 }
182 sp_repr_set_svg_double(repr, "x1", line->x1.computed);
183 sp_repr_set_svg_double(repr, "y1", line->y1.computed);
184 sp_repr_set_svg_double(repr, "x2", line->x2.computed);
185 sp_repr_set_svg_double(repr, "y2", line->y2.computed);
187 if (((SPObjectClass *) (SPLineClass::static_parent_class))->write)
188 ((SPObjectClass *) (SPLineClass::static_parent_class))->write (object, xml_doc, repr, flags);
190 return repr;
191 }
193 gchar *
194 SPLine::sp_line_description(SPItem */*item*/)
195 {
196 return g_strdup(_("<b>Line</b>"));
197 }
199 void
200 SPLine::sp_line_convert_to_guides(SPItem *item)
201 {
202 SPLine *line = SP_LINE(item);
203 Geom::Point points[2];
205 Geom::Matrix const i2d (item->i2d_affine());
207 points[0] = Geom::Point(line->x1.computed, line->y1.computed)*i2d;
208 points[1] = Geom::Point(line->x2.computed, line->y2.computed)*i2d;
210 SPGuide::createSPGuide(inkscape_active_desktop(), points[0], points[1]);
211 }
213 Geom::Matrix
214 SPLine::sp_line_set_transform (SPItem *item, Geom::Matrix const &xform)
215 {
216 SPLine *line = SP_LINE (item);
217 Geom::Point points[2];
219 points[0] = Geom::Point(line->x1.computed, line->y1.computed);
220 points[1] = Geom::Point(line->x2.computed, line->y2.computed);
222 points[0] *= xform;
223 points[1] *= xform;
225 line->x1.computed = points[0][Geom::X];
226 line->y1.computed = points[0][Geom::Y];
227 line->x2.computed = points[1][Geom::X];
228 line->y2.computed = points[1][Geom::Y];
230 item->adjust_stroke(xform.descrim());
232 SP_OBJECT (item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
234 return Geom::identity();
235 }
237 void
238 SPLine::sp_line_set_shape (SPShape *shape)
239 {
240 SPLine *line = SP_LINE (shape);
242 SPCurve *c = new SPCurve ();
244 c->moveto(line->x1.computed, line->y1.computed);
245 c->lineto(line->x2.computed, line->y2.computed);
247 shape->setCurveInsync (c, TRUE); // *_insync does not call update, avoiding infinite recursion when set_shape is called by update
249 c->unref();
250 }