Code

A simple layout document as to what, why and how is cppification.
[inkscape.git] / src / sp-polyline.cpp
1 #define __SP_POLYLINE_C__
3 /*
4  * SVG <polyline> 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 #include "config.h"
16 #include "attributes.h"
17 #include "sp-polyline.h"
18 #include "display/curve.h"
19 #include <glibmm/i18n.h>
20 #include "xml/repr.h"
21 #include "document.h"
23 //static void sp_polyline_class_init (SPPolyLineClass *klass);
24 //static void sp_polyline_init (SPPolyLine *polyline);
26 //static void sp_polyline_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
27 //static void sp_polyline_set (SPObject *object, unsigned int key, const gchar *value);
28 //static Inkscape::XML::Node *sp_polyline_write (SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
30 //static gchar * sp_polyline_description (SPItem * item);
32 //static SPShapeClass *parent_class;
34 SPShapeClass * SPPolyLineClass::static_parent_class=0;
36 GType
37 SPPolyLine::sp_polyline_get_type (void)
38 {
39         static GType polyline_type = 0;
41         if (!polyline_type) {
42                 GTypeInfo polyline_info = {
43                         sizeof (SPPolyLineClass),
44                         NULL,   /* base_init */
45                         NULL,   /* base_finalize */
46                         (GClassInitFunc) SPPolyLineClass::sp_polyline_class_init,
47                         NULL,   /* klass_finalize */
48                         NULL,   /* klass_data */
49                         sizeof (SPPolyLine),
50                         16,     /* n_preallocs */
51                         (GInstanceInitFunc) sp_polyline_init,
52                         NULL,   /* value_table */
53                 };
54                 polyline_type = g_type_register_static (SP_TYPE_SHAPE, "SPPolyLine", &polyline_info, (GTypeFlags)0);
55         }
56         return polyline_type;
57 }
59 void
60 SPPolyLineClass::sp_polyline_class_init (SPPolyLineClass *klass)
61 {
62         GObjectClass * gobject_class;
63         SPObjectClass * sp_object_class;
64         SPItemClass * item_class;
66         gobject_class = (GObjectClass *) klass;
67         sp_object_class = (SPObjectClass *) klass;
68         item_class = (SPItemClass *) klass;
70         static_parent_class = (SPShapeClass *)g_type_class_ref (SP_TYPE_SHAPE);
72         sp_object_class->build = SPPolyLine::sp_polyline_build;
73         sp_object_class->set = SPPolyLine::sp_polyline_set;
74         sp_object_class->write = SPPolyLine::sp_polyline_write;
76         item_class->description = SPPolyLine::sp_polyline_description;
77 }
79 void
80 SPPolyLine::sp_polyline_init (SPPolyLine * /*polyline*/)
81 {
82     /* Nothing here */
83 }
85 void
86 SPPolyLine::sp_polyline_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr)
87 {
89         if (((SPObjectClass *) SPPolyLineClass::static_parent_class)->build)
90                 ((SPObjectClass *) SPPolyLineClass::static_parent_class)->build (object, document, repr);
92         object->readAttr( "points");
93 }
95 void
96 SPPolyLine::sp_polyline_set (SPObject *object, unsigned int key, const gchar *value)
97 {
98         SPPolyLine *polyline;
100         polyline = SP_POLYLINE (object);
102         switch (key) {
103         case SP_ATTR_POINTS: {
104                 SPCurve * curve;
105                 const gchar * cptr;
106                 char * eptr;
107                 gboolean hascpt;
109                 if (!value) break;
110                 curve = new SPCurve ();
111                 hascpt = FALSE;
113                 cptr = value;
114                 eptr = NULL;
116                 while (TRUE) {
117                         gdouble x, y;
119                         while (*cptr != '\0' && (*cptr == ',' || *cptr == '\x20' || *cptr == '\x9' || *cptr == '\xD' || *cptr == '\xA')) {
120                                 cptr++;
121                         }
122                         if (!*cptr) break;
124                         x = g_ascii_strtod (cptr, &eptr);
125                         if (eptr == cptr) break;
126                         cptr = eptr;
128                         while (*cptr != '\0' && (*cptr == ',' || *cptr == '\x20' || *cptr == '\x9' || *cptr == '\xD' || *cptr == '\xA')) {
129                                 cptr++;
130                         }
131                         if (!*cptr) break;
133                         y = g_ascii_strtod (cptr, &eptr);
134                         if (eptr == cptr) break;
135                         cptr = eptr;
136                         if (hascpt) {
137                                 curve->lineto(x, y);
138                         } else {
139                                 curve->moveto(x, y);
140                                 hascpt = TRUE;
141                         }
142                 }
143                 
144                 (SP_SHAPE (polyline))->setCurve (curve, TRUE);
145                 curve->unref();
146                 break;
147         }
148         default:
149                 if (((SPObjectClass *) SPPolyLineClass::static_parent_class)->set)
150                         ((SPObjectClass *) SPPolyLineClass::static_parent_class)->set (object, key, value);
151                 break;
152         }
155 Inkscape::XML::Node *
156 SPPolyLine::sp_polyline_write (SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
158         SPPolyLine *polyline;
160         polyline = SP_POLYLINE (object);
162         if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
163                 repr = xml_doc->createElement("svg:polyline");
164         }
166         if (repr != SP_OBJECT_REPR (object)) {
167                 repr->mergeFrom(SP_OBJECT_REPR (object), "id");
168         }
170         if (((SPObjectClass *) (SPPolyLineClass::static_parent_class))->write)
171                 ((SPObjectClass *) (SPPolyLineClass::static_parent_class))->write (object, xml_doc, repr, flags);
173         return repr;
176 gchar *
177 SPPolyLine::sp_polyline_description(SPItem */*item*/)
179     return g_strdup(_("<b>Polyline</b>"));