Code

OCAL. Fix for Bug #638844 (Errors printed to console if openclipart search fails).
[inkscape.git] / src / sp-polyline.cpp
1 /*
2  * SVG <polyline> implementation
3  *
4  * Authors:
5  *   Lauris Kaplinski <lauris@kaplinski.com>
6  *   Abhishek Sharma
7  *   Jon A. Cruz <jon@joncruz.org>
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 SPShapeClass * SPPolyLineClass::static_parent_class=0;
25 GType SPPolyLine::sp_polyline_get_type(void)
26 {
27     static GType polyline_type = 0;
29     if (!polyline_type) {
30         GTypeInfo polyline_info = {
31             sizeof (SPPolyLineClass),
32             NULL,   /* base_init */
33             NULL,   /* base_finalize */
34             (GClassInitFunc) SPPolyLineClass::sp_polyline_class_init,
35             NULL,   /* klass_finalize */
36             NULL,   /* klass_data */
37             sizeof (SPPolyLine),
38             16,     /* n_preallocs */
39             (GInstanceInitFunc) SPPolyLine::init,
40             NULL,   /* value_table */
41         };
42         polyline_type = g_type_register_static (SP_TYPE_SHAPE, "SPPolyLine", &polyline_info, (GTypeFlags)0);
43     }
44     return polyline_type;
45 }
47 void SPPolyLineClass::sp_polyline_class_init(SPPolyLineClass *klass)
48 {
49     GObjectClass * gobject_class = (GObjectClass *) klass;
50     SPObjectClass * sp_object_class = (SPObjectClass *) klass;
51     SPItemClass * item_class = (SPItemClass *) klass;
53     static_parent_class = (SPShapeClass *)g_type_class_ref(SP_TYPE_SHAPE);
55     sp_object_class->build = SPPolyLine::build;
56     sp_object_class->set = SPPolyLine::set;
57     sp_object_class->write = SPPolyLine::write;
59     item_class->description = SPPolyLine::getDescription;
60 }
62 void SPPolyLine::init(SPPolyLine * /*polyline*/)
63 {
64     /* Nothing here */
65 }
67 void SPPolyLine::build(SPObject * object, SPDocument * document, Inkscape::XML::Node * repr)
68 {
70     if (((SPObjectClass *) SPPolyLineClass::static_parent_class)->build) {
71         ((SPObjectClass *) SPPolyLineClass::static_parent_class)->build (object, document, repr);
72     }
74     object->readAttr( "points" );
75 }
77 void SPPolyLine::set(SPObject *object, unsigned int key, const gchar *value)
78 {
79     SPPolyLine *polyline = SP_POLYLINE(object);
81     switch (key) {
82         case SP_ATTR_POINTS: {
83             SPCurve * curve;
84             const gchar * cptr;
85             char * eptr;
86             gboolean hascpt;
88             if (!value) break;
89             curve = new SPCurve ();
90             hascpt = FALSE;
92             cptr = value;
93             eptr = NULL;
95             while (TRUE) {
96                 gdouble x, y;
98                 while (*cptr != '\0' && (*cptr == ',' || *cptr == '\x20' || *cptr == '\x9' || *cptr == '\xD' || *cptr == '\xA')) {
99                     cptr++;
100                 }
101                 if (!*cptr) break;
103                 x = g_ascii_strtod (cptr, &eptr);
104                 if (eptr == cptr) break;
105                 cptr = eptr;
107                 while (*cptr != '\0' && (*cptr == ',' || *cptr == '\x20' || *cptr == '\x9' || *cptr == '\xD' || *cptr == '\xA')) {
108                     cptr++;
109                 }
110                 if (!*cptr) break;
112                 y = g_ascii_strtod (cptr, &eptr);
113                 if (eptr == cptr) break;
114                 cptr = eptr;
115                 if (hascpt) {
116                     curve->lineto(x, y);
117                 } else {
118                     curve->moveto(x, y);
119                     hascpt = TRUE;
120                 }
121             }
122                 
123             (SP_SHAPE (polyline))->setCurve (curve, TRUE);
124             curve->unref();
125             break;
126         }
127         default:
128             if (((SPObjectClass *) SPPolyLineClass::static_parent_class)->set) {
129                 ((SPObjectClass *) SPPolyLineClass::static_parent_class)->set (object, key, value);
130             }
131             break;
132     }
135 Inkscape::XML::Node *SPPolyLine::write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
137     SPPolyLine *polyline = SP_POLYLINE (object);
139     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
140         repr = xml_doc->createElement("svg:polyline");
141     }
143     if (repr != SP_OBJECT_REPR (object)) {
144         repr->mergeFrom(SP_OBJECT_REPR (object), "id");
145     }
147     if (((SPObjectClass *) (SPPolyLineClass::static_parent_class))->write) {
148         ((SPObjectClass *) (SPPolyLineClass::static_parent_class))->write (object, xml_doc, repr, flags);
149     }
151     return repr;
154 gchar *SPPolyLine::getDescription(SPItem * /*item*/)
156     return g_strdup(_("<b>Polyline</b>"));
160 /*
161   Local Variables:
162   mode:c++
163   c-file-style:"stroustrup"
164   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
165   indent-tabs-mode:nil
166   fill-column:99
167   End:
168 */
169 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :