]> git.sur5r.net Git - glabels/blob - glabels2/src/label-barcode.c
2007-02-14 Jim Evins <evins@snaught.com>
[glabels] / glabels2 / src / label-barcode.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2
3 /*
4  *  (GLABELS) Label and Business Card Creation program for GNOME
5  *
6  *  label_barcode.c:  GLabels label text object
7  *
8  *  Copyright (C) 2001-2002  Jim Evins <evins@snaught.com>.
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23  */
24
25 #include "label-barcode.h"
26
27 #include <glib/gmem.h>
28 #include <glib/gstrfuncs.h>
29 #include <glib/gmessages.h>
30
31 #include "pixmaps/checkerboard.xpm"
32
33 #include "debug.h"
34
35 /*========================================================*/
36 /* Private macros and constants.                          */
37 /*========================================================*/
38
39 /*========================================================*/
40 /* Private types.                                         */
41 /*========================================================*/
42
43 struct _glLabelBarcodePrivate {
44         glTextNode     *text_node;
45         gchar          *id;
46         glColorNode    *color_node;
47         gboolean        text_flag;
48         gboolean        checksum_flag;
49         guint           format_digits;
50 };
51
52 /*========================================================*/
53 /* Private globals.                                       */
54 /*========================================================*/
55
56 static guint instance = 0;
57
58 /*========================================================*/
59 /* Private function prototypes.                           */
60 /*========================================================*/
61
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 G_DEFINE_TYPE (glLabelBarcode, gl_label_barcode, GL_TYPE_LABEL_OBJECT);
82
83 static void
84 gl_label_barcode_class_init (glLabelBarcodeClass *class)
85 {
86         GObjectClass       *object_class       = G_OBJECT_CLASS (class);
87         glLabelObjectClass *label_object_class = GL_LABEL_OBJECT_CLASS (class);
88
89         gl_label_barcode_parent_class = g_type_class_peek_parent (class);
90
91         label_object_class->copy           = copy;
92         label_object_class->get_size       = get_size;
93         label_object_class->set_line_color = set_line_color;
94         label_object_class->get_line_color = get_line_color;
95
96         object_class->finalize = gl_label_barcode_finalize;
97 }
98
99 static void
100 gl_label_barcode_init (glLabelBarcode *lbc)
101 {
102         lbc->priv = g_new0 (glLabelBarcodePrivate, 1);
103         lbc->priv->color_node = gl_color_node_new_default ();
104 }
105
106 static void
107 gl_label_barcode_finalize (GObject *object)
108 {
109         glLabelBarcode *lbc = GL_LABEL_BARCODE (object);
110
111         g_return_if_fail (object && GL_IS_LABEL_BARCODE (object));
112
113         gl_text_node_free (&lbc->priv->text_node);
114         g_free (lbc->priv->id);
115         gl_color_node_free (&(lbc->priv->color_node));
116         g_free (lbc->priv);
117
118         G_OBJECT_CLASS (gl_label_barcode_parent_class)->finalize (object);
119 }
120
121 /*****************************************************************************/
122 /* NEW label "text" object.                                               */
123 /*****************************************************************************/
124 GObject *
125 gl_label_barcode_new (glLabel *label)
126 {
127         glLabelBarcode *lbc;
128
129         lbc = g_object_new (gl_label_barcode_get_type(), NULL);
130
131         gl_label_object_set_parent (GL_LABEL_OBJECT(lbc), label);
132
133         return G_OBJECT (lbc);
134 }
135
136 /*****************************************************************************/
137 /* Copy object contents.                                                     */
138 /*****************************************************************************/
139 static void
140 copy (glLabelObject *dst_object,
141       glLabelObject *src_object)
142 {
143         glLabelBarcode      *lbc     = (glLabelBarcode *)src_object;
144         glLabelBarcode      *new_lbc = (glLabelBarcode *)dst_object;
145         glTextNode          *text_node;
146         gchar               *id;
147         gboolean             text_flag;
148         gboolean             checksum_flag;
149         glColorNode         *color_node;
150         guint                format_digits;
151
152         gl_debug (DEBUG_LABEL, "START");
153
154         g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
155         g_return_if_fail (new_lbc && GL_IS_LABEL_BARCODE (new_lbc));
156
157         text_node = gl_label_barcode_get_data (lbc);
158         gl_label_barcode_get_props (lbc, &id, &text_flag, &checksum_flag, &format_digits);
159         color_node = get_line_color (src_object);
160
161         gl_label_barcode_set_data (new_lbc, text_node);
162         gl_label_barcode_set_props (new_lbc, id, text_flag, checksum_flag, format_digits);
163         set_line_color (dst_object, color_node);
164
165         gl_color_node_free (&color_node);
166         gl_text_node_free (&text_node);
167         g_free (id);
168
169         gl_debug (DEBUG_LABEL, "END");
170 }
171
172
173 /*****************************************************************************/
174 /* Set object params.                                                        */
175 /*****************************************************************************/
176 void
177 gl_label_barcode_set_data (glLabelBarcode *lbc,
178                            glTextNode     *text_node)
179 {
180         gl_debug (DEBUG_LABEL, "START");
181
182         g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
183
184         if (!gl_text_node_equal (lbc->priv->text_node, text_node)) {
185
186                 gl_text_node_free (&lbc->priv->text_node);
187                 lbc->priv->text_node = gl_text_node_dup (text_node);
188
189                 gl_label_object_emit_changed (GL_LABEL_OBJECT(lbc));
190
191         }
192
193         gl_debug (DEBUG_LABEL, "END");
194 }
195
196 void
197 gl_label_barcode_set_props (glLabelBarcode *lbc,
198                             gchar          *id,
199                             gboolean        text_flag,
200                             gboolean        checksum_flag,
201                             guint           format_digits)
202 {
203         gl_debug (DEBUG_LABEL, "START");
204
205         g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
206
207         if ( ((lbc->priv->id == NULL) && (id != NULL))
208              || (g_strcasecmp (lbc->priv->id, id) != 0)
209              || (lbc->priv->text_flag != text_flag)
210              || (lbc->priv->checksum_flag != checksum_flag)
211              || (lbc->priv->format_digits != format_digits)) {
212
213                 lbc->priv->id               = g_strdup (id);
214                 lbc->priv->text_flag        = text_flag;
215                 lbc->priv->checksum_flag    = checksum_flag;
216                 lbc->priv->format_digits    = format_digits;
217
218                 gl_label_object_emit_changed (GL_LABEL_OBJECT(lbc));
219
220         }
221
222         gl_debug (DEBUG_LABEL, "END");
223 }
224
225
226 /*****************************************************************************/
227 /* Get object params.                                                        */
228 /*****************************************************************************/
229 glTextNode *
230 gl_label_barcode_get_data (glLabelBarcode *lbc)
231 {
232         g_return_val_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc), NULL);
233
234         return gl_text_node_dup (lbc->priv->text_node);
235 }
236
237 void
238 gl_label_barcode_get_props (glLabelBarcode *lbc,
239                             gchar          **id,
240                             gboolean       *text_flag,
241                             gboolean       *checksum_flag,
242                             guint          *format_digits)
243 {
244         g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
245
246         *id               = g_strdup (lbc->priv->id);
247         *text_flag        = lbc->priv->text_flag;
248         *checksum_flag    = lbc->priv->checksum_flag;
249         *format_digits    = lbc->priv->format_digits;
250 }
251
252 /*---------------------------------------------------------------------------*/
253 /* PRIVATE.  Get object size method.                                         */
254 /*---------------------------------------------------------------------------*/
255 static void
256 get_size (glLabelObject *object,
257           gdouble       *w,
258           gdouble       *h)
259 {
260         glLabelBarcode      *lbc = (glLabelBarcode *)object;
261         gchar               *data;
262         gdouble              w_parent, h_parent;
263         glBarcode           *gbc;
264
265         gl_debug (DEBUG_LABEL, "START");
266
267         g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
268
269         (* GL_LABEL_OBJECT_CLASS (gl_label_barcode_parent_class)->get_size) (object, &w_parent, &h_parent);
270
271
272         if (lbc->priv->text_node->field_flag) {
273                 data = gl_barcode_default_digits (lbc->priv->id,
274                                                   lbc->priv->format_digits);
275         } else {
276                 data = gl_text_node_expand (lbc->priv->text_node, NULL);
277         }
278
279         gbc = gl_barcode_new (lbc->priv->id,
280                               lbc->priv->text_flag,
281                               lbc->priv->checksum_flag,
282                               w_parent,
283                               h_parent,
284                               data);
285
286         if ( gbc == NULL ) {
287                 /* Try again with default digits. */
288                 data = gl_barcode_default_digits (lbc->priv->id,
289                                                   lbc->priv->format_digits);
290                 gbc = gl_barcode_new (lbc->priv->id,
291                                       lbc->priv->text_flag,
292                                       lbc->priv->checksum_flag,
293                                       w_parent,
294                                       h_parent,
295                                       data);
296         }
297
298         *w = gbc->width;
299         *h = gbc->height;
300
301         gl_barcode_free (&gbc);
302         g_free (data);
303
304         gl_debug (DEBUG_LABEL, "END");
305 }
306
307 /*---------------------------------------------------------------------------*/
308 /* PRIVATE.  Set line color method.                                          */
309 /*---------------------------------------------------------------------------*/
310 static void
311 set_line_color (glLabelObject *object,
312                 glColorNode   *line_color_node)
313 {
314         glLabelBarcode *lbarcode = (glLabelBarcode *)object;
315
316         g_return_if_fail (lbarcode && GL_IS_LABEL_BARCODE (lbarcode));
317
318         if ( !gl_color_node_equal(lbarcode->priv->color_node, line_color_node) ) {
319                 
320                 gl_color_node_free (&(lbarcode->priv->color_node));
321                 lbarcode->priv->color_node = gl_color_node_dup (line_color_node);
322                 gl_label_object_emit_changed (GL_LABEL_OBJECT(lbarcode));
323         }
324 }
325
326 /*---------------------------------------------------------------------------*/
327 /* PRIVATE.  Get line color method.                                          */
328 /*---------------------------------------------------------------------------*/
329 static glColorNode*
330 get_line_color (glLabelObject *object)
331 {
332         glLabelBarcode *lbarcode = (glLabelBarcode *)object;
333
334         g_return_if_fail (lbarcode && GL_IS_LABEL_BARCODE (lbarcode));
335
336         return gl_color_node_dup (lbarcode->priv->color_node);
337 }