]> git.sur5r.net Git - glabels/blob - glabels2/src/label-barcode.c
2005-04-17 Jim Evins <evins@snaught.com>
[glabels] / glabels2 / src / label-barcode.c
1 /*
2  *  (GLABELS) Label and Business Card Creation program for GNOME
3  *
4  *  label_barcode.c:  GLabels label text object
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 "label-barcode.h"
24
25 #include <glib/gmem.h>
26 #include <glib/gstrfuncs.h>
27 #include <glib/gmessages.h>
28
29 #include "pixmaps/checkerboard.xpm"
30
31 #include "debug.h"
32
33 /*========================================================*/
34 /* Private macros and constants.                          */
35 /*========================================================*/
36
37 /*========================================================*/
38 /* Private types.                                         */
39 /*========================================================*/
40
41 struct _glLabelBarcodePrivate {
42         glTextNode     *text_node;
43         gchar          *id;
44         glColorNode    *color_node;
45         gboolean        text_flag;
46         gboolean        checksum_flag;
47         guint           format_digits;
48 };
49
50 /*========================================================*/
51 /* Private globals.                                       */
52 /*========================================================*/
53
54 static glLabelObjectClass *parent_class = NULL;
55
56 static guint instance = 0;
57
58 /*========================================================*/
59 /* Private function prototypes.                           */
60 /*========================================================*/
61
62 static void  gl_label_barcode_class_init    (glLabelBarcodeClass *klass);
63 static void  gl_label_barcode_instance_init (glLabelBarcode      *lbc);
64 static void  gl_label_barcode_finalize      (GObject             *object);
65
66 static void  copy                           (glLabelObject       *dst_object,
67                                              glLabelObject       *src_object);
68
69 static void  get_size                       (glLabelObject       *object,
70                                              gdouble             *w,
71                                              gdouble             *h);
72
73 static void  set_line_color                 (glLabelObject       *object,
74                                              glColorNode         *line_color);
75
76 static glColorNode *get_line_color           (glLabelObject       *object);
77
78
79 \f
80 /*****************************************************************************/
81 /* Boilerplate object stuff.                                                 */
82 /*****************************************************************************/
83 GType
84 gl_label_barcode_get_type (void)
85 {
86         static GType type = 0;
87
88         if (!type) {
89                 static const GTypeInfo info = {
90                         sizeof (glLabelBarcodeClass),
91                         NULL,
92                         NULL,
93                         (GClassInitFunc) gl_label_barcode_class_init,
94                         NULL,
95                         NULL,
96                         sizeof (glLabelBarcode),
97                         0,
98                         (GInstanceInitFunc) gl_label_barcode_instance_init,
99                         NULL
100                 };
101
102                 type = g_type_register_static (GL_TYPE_LABEL_OBJECT,
103                                                "glLabelBarcode", &info, 0);
104         }
105
106         return type;
107 }
108
109 static void
110 gl_label_barcode_class_init (glLabelBarcodeClass *klass)
111 {
112         GObjectClass       *object_class       = (GObjectClass *) klass;
113         glLabelObjectClass *label_object_class = (glLabelObjectClass *) klass;
114
115         parent_class = g_type_class_peek_parent (klass);
116
117         label_object_class->copy           = copy;
118         label_object_class->get_size       = get_size;
119         label_object_class->set_line_color = set_line_color;
120         label_object_class->get_line_color = get_line_color;
121
122         object_class->finalize = gl_label_barcode_finalize;
123 }
124
125 static void
126 gl_label_barcode_instance_init (glLabelBarcode *lbc)
127 {
128         lbc->private = g_new0 (glLabelBarcodePrivate, 1);
129         lbc->private->color_node = gl_color_node_new_default ();
130 }
131
132 static void
133 gl_label_barcode_finalize (GObject *object)
134 {
135         glLabelBarcode *lbc;
136
137         g_return_if_fail (object && GL_IS_LABEL_BARCODE (object));
138
139         lbc = GL_LABEL_BARCODE (object);
140
141         gl_color_node_free (&(lbc->private->color_node));
142         gl_text_node_free (&lbc->private->text_node);
143         g_free (lbc->private);
144
145         G_OBJECT_CLASS (parent_class)->finalize (object);
146 }
147
148 /*****************************************************************************/
149 /* NEW label "text" object.                                               */
150 /*****************************************************************************/
151 GObject *
152 gl_label_barcode_new (glLabel *label)
153 {
154         glLabelBarcode *lbc;
155
156         lbc = g_object_new (gl_label_barcode_get_type(), NULL);
157
158         gl_label_object_set_parent (GL_LABEL_OBJECT(lbc), label);
159
160         return G_OBJECT (lbc);
161 }
162
163 /*****************************************************************************/
164 /* Copy object contents.                                                     */
165 /*****************************************************************************/
166 static void
167 copy (glLabelObject *dst_object,
168       glLabelObject *src_object)
169 {
170         glLabelBarcode      *lbc     = (glLabelBarcode *)src_object;
171         glLabelBarcode      *new_lbc = (glLabelBarcode *)dst_object;
172         glTextNode          *text_node;
173         gchar               *id;
174         gboolean             text_flag;
175         gboolean             checksum_flag;
176         glColorNode         *color_node;
177         guint                format_digits;
178
179         gl_debug (DEBUG_LABEL, "START");
180
181         g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
182         g_return_if_fail (new_lbc && GL_IS_LABEL_BARCODE (new_lbc));
183
184         text_node = gl_label_barcode_get_data (lbc);
185         gl_label_barcode_get_props (lbc, &id, &text_flag, &checksum_flag, &format_digits);
186         color_node = get_line_color (src_object);
187
188         gl_label_barcode_set_data (new_lbc, text_node);
189         gl_label_barcode_set_props (new_lbc, id, text_flag, checksum_flag, format_digits);
190         set_line_color (dst_object, color_node);
191
192         gl_color_node_free (&color_node);
193         gl_text_node_free (&text_node);
194         g_free (id);
195
196         gl_debug (DEBUG_LABEL, "END");
197 }
198
199
200 /*****************************************************************************/
201 /* Set object params.                                                        */
202 /*****************************************************************************/
203 void
204 gl_label_barcode_set_data (glLabelBarcode *lbc,
205                            glTextNode     *text_node)
206 {
207         gl_debug (DEBUG_LABEL, "START");
208
209         g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
210
211         if (!gl_text_node_equal (lbc->private->text_node, text_node)) {
212
213                 gl_text_node_free (&lbc->private->text_node);
214                 lbc->private->text_node = gl_text_node_dup (text_node);
215
216                 gl_label_object_emit_changed (GL_LABEL_OBJECT(lbc));
217
218         }
219
220         gl_debug (DEBUG_LABEL, "END");
221 }
222
223 void
224 gl_label_barcode_set_props (glLabelBarcode *lbc,
225                             gchar          *id,
226                             gboolean        text_flag,
227                             gboolean        checksum_flag,
228                             guint           format_digits)
229 {
230         gl_debug (DEBUG_LABEL, "START");
231
232         g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
233
234         if ( ((lbc->private->id == NULL) && (id != NULL))
235              || (g_strcasecmp (lbc->private->id, id) != 0)
236              || (lbc->private->text_flag != text_flag)
237              || (lbc->private->checksum_flag != checksum_flag)
238              || (lbc->private->format_digits != format_digits)) {
239
240                 lbc->private->id               = g_strdup (id);
241                 lbc->private->text_flag        = text_flag;
242                 lbc->private->checksum_flag    = checksum_flag;
243                 lbc->private->format_digits    = format_digits;
244
245                 gl_label_object_emit_changed (GL_LABEL_OBJECT(lbc));
246
247         }
248
249         gl_debug (DEBUG_LABEL, "END");
250 }
251
252
253 /*****************************************************************************/
254 /* Get object params.                                                        */
255 /*****************************************************************************/
256 glTextNode *
257 gl_label_barcode_get_data (glLabelBarcode *lbc)
258 {
259         g_return_val_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc), NULL);
260
261         return gl_text_node_dup (lbc->private->text_node);
262 }
263
264 void
265 gl_label_barcode_get_props (glLabelBarcode *lbc,
266                             gchar          **id,
267                             gboolean       *text_flag,
268                             gboolean       *checksum_flag,
269                             guint          *format_digits)
270 {
271         g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
272
273         *id               = g_strdup (lbc->private->id);
274         *text_flag        = lbc->private->text_flag;
275         *checksum_flag    = lbc->private->checksum_flag;
276         *format_digits    = lbc->private->format_digits;
277 }
278
279 /*---------------------------------------------------------------------------*/
280 /* PRIVATE.  Get object size method.                                         */
281 /*---------------------------------------------------------------------------*/
282 static void
283 get_size (glLabelObject *object,
284           gdouble       *w,
285           gdouble       *h)
286 {
287         glLabelBarcode      *lbc = (glLabelBarcode *)object;
288         gchar               *data;
289         gdouble              w_parent, h_parent;
290         glBarcode           *gbc;
291
292         gl_debug (DEBUG_LABEL, "START");
293
294         g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
295
296         (* parent_class->get_size) (object, &w_parent, &h_parent);
297
298
299         if (lbc->private->text_node->field_flag) {
300                 data = gl_barcode_default_digits (lbc->private->id,
301                                                   lbc->private->format_digits);
302         } else {
303                 data = gl_text_node_expand (lbc->private->text_node, NULL);
304         }
305
306         gbc = gl_barcode_new (lbc->private->id,
307                               lbc->private->text_flag,
308                               lbc->private->checksum_flag,
309                               w_parent,
310                               h_parent,
311                               data);
312
313         if ( gbc == NULL ) {
314                 /* Try again with default digits. */
315                 data = gl_barcode_default_digits (lbc->private->id,
316                                                   lbc->private->format_digits);
317                 gbc = gl_barcode_new (lbc->private->id,
318                                       lbc->private->text_flag,
319                                       lbc->private->checksum_flag,
320                                       w_parent,
321                                       h_parent,
322                                       data);
323         }
324
325         *w = gbc->width;
326         *h = gbc->height;
327
328         gl_barcode_free (&gbc);
329         g_free (data);
330
331         gl_debug (DEBUG_LABEL, "END");
332 }
333
334 /*---------------------------------------------------------------------------*/
335 /* PRIVATE.  Set line color method.                                          */
336 /*---------------------------------------------------------------------------*/
337 static void
338 set_line_color (glLabelObject *object,
339                 glColorNode   *line_color_node)
340 {
341         glLabelBarcode *lbarcode = (glLabelBarcode *)object;
342
343         g_return_if_fail (lbarcode && GL_IS_LABEL_BARCODE (lbarcode));
344
345         if ( !gl_color_node_equal(lbarcode->private->color_node, line_color_node) ) {
346                 
347                 gl_color_node_free (&(lbarcode->private->color_node));
348                 lbarcode->private->color_node = gl_color_node_dup (line_color_node);
349                 gl_label_object_emit_changed (GL_LABEL_OBJECT(lbarcode));
350         }
351 }
352
353 /*---------------------------------------------------------------------------*/
354 /* PRIVATE.  Get line color method.                                          */
355 /*---------------------------------------------------------------------------*/
356 static glColorNode*
357 get_line_color (glLabelObject *object)
358 {
359         glLabelBarcode *lbarcode = (glLabelBarcode *)object;
360
361         g_return_if_fail (lbarcode && GL_IS_LABEL_BARCODE (lbarcode));
362
363         return gl_color_node_dup (lbarcode->private->color_node);
364 }