]> git.sur5r.net Git - glabels/blob - glabels2/src/xml-label-04.c
Moved item event handler to view-object.c.
[glabels] / glabels2 / src / xml-label-04.c
1 /*
2  *  (GLABELS) Label and Business Card Creation program for GNOME
3  *
4  *  xml-label-04.c:  GLabels xml label compat module
5  *
6  *  Copyright (C) 2001-2002  Jim Evins <evins@snaught.com>.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  */
22
23 #include <config.h>
24
25 #include <gnome.h>
26
27 #include "xml-label-04.h"
28 #include "label-text.h"
29 #include "label-box.h"
30 #include "label-line.h"
31 #include "label-ellipse.h"
32 #include "label-image.h"
33 #include "label-barcode.h"
34
35 #include "debug.h"
36
37 static gboolean xml04_parse_media_description (xmlNodePtr node,
38                                                glLabel *label);
39 static void     xml04_parse_object            (xmlNodePtr node,
40                                                glLabelObject *object);
41 static void     xml04_parse_text_props        (xmlNodePtr node,
42                                                glLabelText *object);
43 static void     xml04_parse_box_props         (xmlNodePtr node,
44                                                glLabelBox *object);
45 static void     xml04_parse_line_props        (xmlNodePtr node,
46                                                glLabelLine *object);
47 static void     xml04_parse_ellipse_props     (xmlNodePtr node,
48                                                glLabelEllipse *object);
49 static void     xml04_parse_image_props       (xmlNodePtr node,
50                                                glLabelImage *object);
51 static void     xml04_parse_barcode_props     (xmlNodePtr node,
52                                                glLabelBarcode *object);
53 static void     xml04_parse_merge_properties  (xmlNodePtr node,
54                                                glLabel *label);
55
56 \f
57 /****************************************************************************/
58 /* PRIVATE.  Parse xml doc structure and create label.                      */
59 /****************************************************************************/
60 glLabel      *gl_xml_label_04_parse      (xmlNodePtr       root,
61                                           glXMLLabelStatus *status)
62 {
63         glLabel       *label;
64         xmlNodePtr    node;
65         xmlNsPtr      ns;
66         GObject       *object;
67         gboolean      rotate_flag;
68
69         gl_debug (DEBUG_XML, "START");
70
71         *status = XML_LABEL_OK;
72
73         if (g_strcasecmp (root->name, "Label") != 0) {
74                 g_warning (_("Bad root node = \"%s\""), root->name);
75                 *status = XML_LABEL_ERROR_OPEN_PARSE;
76                 return NULL;
77         }
78
79         label = GL_LABEL (gl_label_new ());
80
81         rotate_flag =
82             !(g_strcasecmp (xmlGetProp (root, "rotate"), "false") == 0);
83         gl_label_set_rotate_flag (label, rotate_flag);
84
85         for (node = root->xmlChildrenNode; node != NULL; node = node->next) {
86
87                 gl_debug (DEBUG_XML, "node name = \"%s\"", node->name);
88
89                 if (!xmlNodeIsText (node)) {
90                         if (g_strcasecmp (node->name, "Media_Type") == 0) {
91                                 if (!xml04_parse_media_description (node, label)) {
92                                         *status = XML_LABEL_UNKNOWN_MEDIA;
93                                 }
94                         } else if (g_strcasecmp (node->name, "Text") == 0) {
95                                 object = gl_label_text_new (label);
96                                 xml04_parse_object (node, GL_LABEL_OBJECT(object));
97                                 xml04_parse_text_props (node, GL_LABEL_TEXT(object));
98                         } else if (g_strcasecmp (node->name, "Box") == 0) {
99                                 object = gl_label_box_new (label);
100                                 xml04_parse_object (node, GL_LABEL_OBJECT(object));
101                                 xml04_parse_box_props (node, GL_LABEL_BOX(object));
102                         } else if (g_strcasecmp (node->name, "Line") == 0) {
103                                 object = gl_label_line_new (label);
104                                 xml04_parse_object (node, GL_LABEL_OBJECT(object));
105                                 xml04_parse_line_props (node, GL_LABEL_LINE(object));
106                         } else if (g_strcasecmp (node->name, "Ellipse") == 0) {
107                                 object = gl_label_ellipse_new (label);
108                                 xml04_parse_object (node, GL_LABEL_OBJECT(object));
109                                 xml04_parse_ellipse_props (node,
110                                                            GL_LABEL_ELLIPSE(object));
111                         } else if (g_strcasecmp (node->name, "Image") == 0) {
112                                 object = gl_label_image_new (label);
113                                 xml04_parse_object (node, GL_LABEL_OBJECT(object));
114                                 xml04_parse_image_props (node, GL_LABEL_IMAGE(object));
115                         } else if (g_strcasecmp (node->name, "Barcode") == 0) {
116                                 object = gl_label_barcode_new (label);
117                                 xml04_parse_object (node, GL_LABEL_OBJECT(object));
118                                 xml04_parse_barcode_props (node,
119                                                            GL_LABEL_BARCODE(object));
120                         } else if (g_strcasecmp (node->name, "Merge_Properties") == 0) {
121                                 xml04_parse_merge_properties (node, label);
122                         } else {
123                                 g_warning (_("bad node =  \"%s\""), node->name);
124                         }
125                 }
126         }
127
128         gl_debug (DEBUG_XML, "END");
129
130         return label;
131 }
132
133 /*--------------------------------------------------------------------------*/
134 /* PRIVATE.  Parse Media Description node.                                  */
135 /*--------------------------------------------------------------------------*/
136 static gboolean
137 xml04_parse_media_description (xmlNodePtr node,
138                                glLabel    *label)
139 {
140         gchar      *template_name;
141         glTemplate *template;
142         gboolean   ret;
143
144         gl_debug (DEBUG_XML, "START");
145
146         template_name = xmlNodeGetContent (node);
147
148         template = gl_template_from_name (template_name);
149         if (template == NULL) {
150                 g_warning ("Undefined template \"%s\"", template_name);
151                 /* Get a default */
152                 template = gl_template_from_name (NULL);
153                 ret = FALSE;
154         } else {
155                 ret = TRUE;
156         }
157
158         gl_label_set_template (label, template);
159
160         gl_template_free (&template);
161         g_free (template_name);
162
163         gl_debug (DEBUG_XML, "END");
164
165         return ret;
166 }
167
168 /*--------------------------------------------------------------------------*/
169 /* PRIVATE.  Parse XML Object Node                                          */
170 /*--------------------------------------------------------------------------*/
171 static void
172 xml04_parse_object (xmlNodePtr    object_node,
173                     glLabelObject *object)
174 {
175         gdouble x, y;
176
177         gl_debug (DEBUG_XML, "START");
178
179         x = g_strtod (xmlGetProp (object_node, "x"), NULL);
180         y = g_strtod (xmlGetProp (object_node, "y"), NULL);
181
182         gl_label_object_set_position (object, x, y);
183
184         gl_debug (DEBUG_XML, "END");
185 }
186
187 /*--------------------------------------------------------------------------*/
188 /* PRIVATE.  Parse XML Label->Text Node Properties                          */
189 /*--------------------------------------------------------------------------*/
190 static void
191 xml04_parse_text_props (xmlNodePtr    object_node,
192                         glLabelText   *object)
193 {
194         gchar            *font_family;
195         gdouble          font_size;
196         GnomeFontWeight  font_weight;
197         gboolean         font_italic_flag;
198         guint            color;
199         GtkJustification just;
200         xmlNodePtr       line_node, text_node;
201         glTextNode       *node_text;
202         GList            *nodes, *lines;
203         gdouble          w, h, x, y;
204
205         gl_debug (DEBUG_XML, "START");
206
207         font_family = xmlGetProp (object_node, "font_family");
208         font_size = g_strtod (xmlGetProp (object_node, "font_size"), NULL);
209         font_weight =
210                 gl_util_string_to_weight (xmlGetProp (object_node, "font_weight"));
211         font_italic_flag =
212                 !(g_strcasecmp (xmlGetProp (object_node, "font_italic"), "false") ==
213                   0);
214
215         just = gl_util_string_to_just (xmlGetProp (object_node, "justify"));
216
217         sscanf (xmlGetProp (object_node, "color"), "%x", &color);
218
219         gl_label_text_set_props (object,
220                                  font_family,
221                                  font_size,
222                                  font_weight,
223                                  font_italic_flag,
224                                  color,
225                                  just);
226
227         lines = NULL;
228         for (line_node = object_node->xmlChildrenNode; line_node != NULL;
229              line_node = line_node->next) {
230
231                 if (g_strcasecmp (line_node->name, "Line") == 0) {
232
233                         nodes = NULL;
234                         for (text_node = line_node->xmlChildrenNode;
235                              text_node != NULL; text_node = text_node->next) {
236
237                                 if (g_strcasecmp (text_node->name, "Field") ==
238                                     0) {
239                                         node_text = g_new0 (glTextNode, 1);
240                                         node_text->field_flag = TRUE;
241                                         node_text->data =
242                                             xmlGetProp (text_node, "name");
243                                         nodes =
244                                             g_list_append (nodes, node_text);
245                                 } else if (xmlNodeIsText (text_node)) {
246                                         node_text = g_new0 (glTextNode, 1);
247                                         node_text->field_flag = FALSE;
248                                         node_text->data =
249                                             xmlNodeGetContent (text_node);
250                                         nodes =
251                                             g_list_append (nodes, node_text);
252                                 } else {
253                                         g_warning ("Unexpected Text Line child: \"%s\"",
254                                                    text_node->name);
255                                 }
256
257                         }
258                         lines = g_list_append (lines, nodes);
259
260                 } else if (!xmlNodeIsText (line_node)) {
261                         g_warning ("Unexpected Text child: \"%s\"",
262                                    line_node->name);
263                 }
264
265         }
266
267         gl_label_text_set_lines (object, lines);
268
269         gl_text_node_lines_free (&lines);
270         g_free (font_family);
271
272         /* Adjust location.  In 0.4.x, text was anchored at x,y */
273         gl_label_object_get_position (GL_LABEL_OBJECT(object), &x, &y);
274         gl_label_object_get_size (GL_LABEL_OBJECT(object), &w, &h);
275         switch (just) {
276         case GTK_JUSTIFY_LEFT:
277                 /* nothing */
278                 break;
279         case GTK_JUSTIFY_CENTER:
280                 x -= w/2.0;
281                 gl_label_object_set_position (GL_LABEL_OBJECT(object), x, y);
282                 break;
283         case GTK_JUSTIFY_RIGHT:
284                 x -= w;
285                 gl_label_object_set_position (GL_LABEL_OBJECT(object), x, y);
286                 break;
287         default:
288                 /* should not happen */
289         }
290
291         gl_debug (DEBUG_XML, "END");
292 }
293
294 /*--------------------------------------------------------------------------*/
295 /* PRIVATE.  Parse XML Label->Box Node Properties                           */
296 /*--------------------------------------------------------------------------*/
297 static void
298 xml04_parse_box_props (xmlNodePtr    node,
299                        glLabelBox    *object)
300 {
301         gdouble w, h, line_width;
302         guint line_color, fill_color;
303
304         gl_debug (DEBUG_XML, "START");
305
306         w = g_strtod (xmlGetProp (node, "w"), NULL);
307         h = g_strtod (xmlGetProp (node, "h"), NULL);
308
309         line_width = g_strtod (xmlGetProp (node, "line_width"), NULL);
310
311         sscanf (xmlGetProp (node, "line_color"), "%x", &line_color);
312         sscanf (xmlGetProp (node, "fill_color"), "%x", &fill_color);
313
314         gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h);
315         gl_label_box_set_line_width (object, line_width);
316         gl_label_box_set_line_color (object, line_color);
317         gl_label_box_set_fill_color (object, fill_color);
318
319         gl_debug (DEBUG_XML, "END");
320 }
321
322 /*--------------------------------------------------------------------------*/
323 /* PRIVATE.  Parse XML Label->Line Node Properties                          */
324 /*--------------------------------------------------------------------------*/
325 static void
326 xml04_parse_line_props (xmlNodePtr    node,
327                         glLabelLine *object)
328 {
329         gdouble w, h, line_width;
330         guint line_color;
331
332         gl_debug (DEBUG_XML, "START");
333
334         w = g_strtod (xmlGetProp (node, "dx"), NULL);
335         h = g_strtod (xmlGetProp (node, "dy"), NULL);
336
337         line_width = g_strtod (xmlGetProp (node, "line_width"), NULL);
338
339         sscanf (xmlGetProp (node, "line_color"), "%x", &line_color);
340
341         gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h);
342         gl_label_line_set_line_width (object, line_width);
343         gl_label_line_set_line_color (object, line_color);
344
345         gl_debug (DEBUG_XML, "END");
346 }
347
348 /*--------------------------------------------------------------------------*/
349 /* PRIVATE.  Parse XML Label->Ellipse Node Properties                       */
350 /*--------------------------------------------------------------------------*/
351 static void
352 xml04_parse_ellipse_props (xmlNodePtr     node,
353                            glLabelEllipse *object)
354 {
355         gdouble w, h, line_width;
356         guint line_color, fill_color;
357
358         gl_debug (DEBUG_XML, "START");
359
360         w = g_strtod (xmlGetProp (node, "w"), NULL);
361         h = g_strtod (xmlGetProp (node, "h"), NULL);
362
363         line_width = g_strtod (xmlGetProp (node, "line_width"), NULL);
364
365         sscanf (xmlGetProp (node, "line_color"), "%x", &line_color);
366         sscanf (xmlGetProp (node, "fill_color"), "%x", &fill_color);
367
368         gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h);
369         gl_label_ellipse_set_line_width (object, line_width);
370         gl_label_ellipse_set_line_color (object, line_color);
371         gl_label_ellipse_set_fill_color (object, fill_color);
372
373         gl_debug (DEBUG_XML, "END");
374 }
375
376 /*--------------------------------------------------------------------------*/
377 /* PRIVATE.  Parse XML Label->Image Node Properties                         */
378 /*--------------------------------------------------------------------------*/
379 static void
380 xml04_parse_image_props (xmlNodePtr    node,
381                          glLabelImage  *object)
382 {
383         gdouble w, h;
384         glTextNode *filename;
385
386         gl_debug (DEBUG_XML, "START");
387
388         w = g_strtod (xmlGetProp (node, "w"), NULL);
389         h = g_strtod (xmlGetProp (node, "h"), NULL);
390         gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h);
391
392         filename = g_new0 (glTextNode, 1);
393         filename->field_flag = FALSE;
394         filename->data = xmlGetProp (node, "filename");
395         gl_label_image_set_filename (object, filename);
396         gl_text_node_free (&filename);
397
398         gl_debug (DEBUG_XML, "END");
399 }
400
401 /*--------------------------------------------------------------------------*/
402 /* PRIVATE.  Parse XML Label->Barcode Node Properties                       */
403 /*--------------------------------------------------------------------------*/
404 static void
405 xml04_parse_barcode_props (xmlNodePtr    node,
406                            glLabelBarcode *object)
407 {
408         glBarcodeStyle style;
409         gboolean       text_flag;
410         guint          color;
411         gdouble        scale;
412         xmlNodePtr     child;
413         glTextNode     *text_node;
414
415         gl_debug (DEBUG_XML, "START");
416
417         sscanf (xmlGetProp (node, "color"), "%x", &color);
418         style = gl_barcode_text_to_style (xmlGetProp (node, "style"));
419         text_flag = !(g_strcasecmp (xmlGetProp (node, "text"), "false") == 0);
420         scale = g_strtod (xmlGetProp (node, "scale"), NULL);
421         if (scale == 0.0) {
422                 scale = 0.5; /* Set to a valid value */
423         }
424         gl_label_barcode_set_props (object, style, text_flag, color, scale);
425
426         child = node->xmlChildrenNode;
427         text_node = g_new0 (glTextNode, 1);
428         if (g_strcasecmp (child->name, "Field") == 0) {
429                 text_node->field_flag = TRUE;
430                 text_node->data       = xmlGetProp (child, "name");
431         } else if (xmlNodeIsText (child)) {
432                 text_node->field_flag = FALSE;
433                 text_node->data       = xmlNodeGetContent (child);
434         } else {
435                 g_warning ("Unexpected Barcode child: \"%s\"", child->name);
436         }
437         gl_label_barcode_set_data (object, text_node);
438
439         gl_text_node_free (&text_node);
440
441         gl_debug (DEBUG_XML, "END");
442 }
443
444 /*--------------------------------------------------------------------------*/
445 /* PRIVATE.  Parse XML merge properties tag.                                */
446 /*--------------------------------------------------------------------------*/
447 static void
448 xml04_parse_merge_properties (xmlNodePtr node,
449                               glLabel    *label)
450 {
451         glMerge                *merge;
452         xmlNodePtr             child;
453
454         gl_debug (DEBUG_XML, "START");
455
456         merge = gl_merge_new (xmlGetProp (node, "type"));
457         gl_merge_set_src (merge, xmlGetProp (node, "src"));
458
459         gl_label_set_merge (label, merge);
460
461         g_object_unref (G_OBJECT(merge));
462
463         gl_debug (DEBUG_XML, "END");
464 }
465