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