]> git.sur5r.net Git - glabels/blob - glabels2/src/label-barcode.c
2004-02-15 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 #include <libgnomeprint/gnome-glyphlist.h>
25
26 #include "label-barcode.h"
27
28 #include "pixmaps/checkerboard.xpm"
29
30 #include "debug.h"
31
32 /*========================================================*/
33 /* Private macros and constants.                          */
34 /*========================================================*/
35
36 /*========================================================*/
37 /* Private types.                                         */
38 /*========================================================*/
39
40 struct _glLabelBarcodePrivate {
41         glTextNode     *text_node;
42         gchar          *id;
43         guint           color;
44         gboolean        text_flag;
45         gboolean        checksum_flag;
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                                              guint                line_color);
73
74 static guint 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                 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                 };
98
99                 type = g_type_register_static (GL_TYPE_LABEL_OBJECT,
100                                                "glLabelBarcode", &info, 0);
101         }
102
103         return type;
104 }
105
106 static void
107 gl_label_barcode_class_init (glLabelBarcodeClass *klass)
108 {
109         GObjectClass       *object_class       = (GObjectClass *) klass;
110         glLabelObjectClass *label_object_class = (glLabelObjectClass *) klass;
111
112         parent_class = g_type_class_peek_parent (klass);
113
114         label_object_class->copy           = copy;
115         label_object_class->get_size       = get_size;
116         label_object_class->set_line_color = set_line_color;
117         label_object_class->get_line_color = get_line_color;
118
119         object_class->finalize = gl_label_barcode_finalize;
120 }
121
122 static void
123 gl_label_barcode_instance_init (glLabelBarcode *lbc)
124 {
125         lbc->private = g_new0 (glLabelBarcodePrivate, 1);
126 }
127
128 static void
129 gl_label_barcode_finalize (GObject *object)
130 {
131         glLabelBarcode *lbc;
132
133         g_return_if_fail (object && GL_IS_LABEL_BARCODE (object));
134
135         lbc = GL_LABEL_BARCODE (object);
136
137         gl_text_node_free (&lbc->private->text_node);
138         g_free (lbc->private);
139
140         G_OBJECT_CLASS (parent_class)->finalize (object);
141 }
142
143 /*****************************************************************************/
144 /* NEW label "text" object.                                               */
145 /*****************************************************************************/
146 GObject *
147 gl_label_barcode_new (glLabel *label)
148 {
149         glLabelBarcode *lbc;
150
151         lbc = g_object_new (gl_label_barcode_get_type(), NULL);
152
153         gl_label_object_set_parent (GL_LABEL_OBJECT(lbc), label);
154
155         return G_OBJECT (lbc);
156 }
157
158 /*****************************************************************************/
159 /* Copy object contents.                                                     */
160 /*****************************************************************************/
161 static void
162 copy (glLabelObject *dst_object,
163       glLabelObject *src_object)
164 {
165         glLabelBarcode      *lbc     = (glLabelBarcode *)src_object;
166         glLabelBarcode      *new_lbc = (glLabelBarcode *)dst_object;
167         glTextNode          *text_node;
168         gchar               *id;
169         gboolean             text_flag;
170         gboolean             checksum_flag;
171         guint                color;
172
173         gl_debug (DEBUG_LABEL, "START");
174
175         g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
176         g_return_if_fail (new_lbc && GL_IS_LABEL_BARCODE (new_lbc));
177
178         text_node = gl_label_barcode_get_data (lbc);
179         gl_label_barcode_get_props (lbc, &id, &text_flag, &checksum_flag);
180         color = get_line_color (src_object);
181
182         gl_label_barcode_set_data (new_lbc, text_node);
183         gl_label_barcode_set_props (new_lbc, id, text_flag, checksum_flag);
184         set_line_color (dst_object, color);
185
186         gl_text_node_free (&text_node);
187         g_free (id);
188
189         gl_debug (DEBUG_LABEL, "END");
190 }
191
192
193 /*****************************************************************************/
194 /* Set object params.                                                        */
195 /*****************************************************************************/
196 void
197 gl_label_barcode_set_data (glLabelBarcode *lbc,
198                            glTextNode     *text_node)
199 {
200         gl_debug (DEBUG_LABEL, "START");
201
202         g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
203
204         gl_text_node_free (&lbc->private->text_node);
205         lbc->private->text_node = gl_text_node_dup (text_node);
206
207         gl_label_object_emit_changed (GL_LABEL_OBJECT(lbc));
208
209         gl_debug (DEBUG_LABEL, "END");
210 }
211
212 void
213 gl_label_barcode_set_props (glLabelBarcode *lbc,
214                             gchar          *id,
215                             gboolean        text_flag,
216                             gboolean        checksum_flag)
217 {
218         gl_debug (DEBUG_LABEL, "START");
219
220         g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
221
222         lbc->private->id               = g_strdup (id);
223         lbc->private->text_flag        = text_flag;
224         lbc->private->checksum_flag    = checksum_flag;
225
226         gl_label_object_emit_changed (GL_LABEL_OBJECT(lbc));
227
228         gl_debug (DEBUG_LABEL, "END");
229 }
230
231
232 /*****************************************************************************/
233 /* Get object params.                                                        */
234 /*****************************************************************************/
235 glTextNode *
236 gl_label_barcode_get_data (glLabelBarcode *lbc)
237 {
238         g_return_val_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc), NULL);
239
240         return gl_text_node_dup (lbc->private->text_node);
241 }
242
243 void
244 gl_label_barcode_get_props (glLabelBarcode *lbc,
245                             gchar          **id,
246                             gboolean       *text_flag,
247                             gboolean       *checksum_flag)
248 {
249         g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
250
251         *id               = g_strdup (lbc->private->id);
252         *text_flag        = lbc->private->text_flag;
253         *checksum_flag    = lbc->private->checksum_flag;
254 }
255
256 /*---------------------------------------------------------------------------*/
257 /* PRIVATE.  Get object size method.                                         */
258 /*---------------------------------------------------------------------------*/
259 static void
260 get_size (glLabelObject *object,
261           gdouble       *w,
262           gdouble       *h)
263 {
264         glLabelBarcode      *lbc = (glLabelBarcode *)object;
265         gchar               *data;
266         gdouble              w_parent, h_parent;
267         glBarcode           *gbc;
268
269         gl_debug (DEBUG_LABEL, "START");
270
271         g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
272
273         (* parent_class->get_size) (object, &w_parent, &h_parent);
274
275
276         if (lbc->private->text_node->field_flag) {
277                 data = gl_barcode_default_digits (lbc->private->id);
278         } else {
279                 data = gl_text_node_expand (lbc->private->text_node, NULL);
280         }
281
282         gbc = gl_barcode_new (lbc->private->id,
283                               lbc->private->text_flag,
284                               lbc->private->checksum_flag,
285                               w_parent,
286                               h_parent,
287                               data);
288
289         if ( gbc == NULL ) {
290                 /* Try again with default digits. */
291                 data = gl_barcode_default_digits (lbc->private->id);
292                 gbc = gl_barcode_new (lbc->private->id,
293                                       lbc->private->text_flag,
294                                       lbc->private->checksum_flag,
295                                       w_parent,
296                                       h_parent,
297                                       data);
298         }
299
300         *w = gbc->width;
301         *h = gbc->height;
302
303         gl_barcode_free (&gbc);
304         g_free (data);
305
306         gl_debug (DEBUG_LABEL, "END");
307 }
308
309 /*---------------------------------------------------------------------------*/
310 /* PRIVATE.  Set line color method.                                          */
311 /*---------------------------------------------------------------------------*/
312 static void
313 set_line_color (glLabelObject *object,
314                 guint          line_color)
315 {
316         glLabelBarcode *lbarcode = (glLabelBarcode *)object;
317
318         g_return_if_fail (lbarcode && GL_IS_LABEL_BARCODE (lbarcode));
319
320         if ( lbarcode->private->color != line_color ) {
321                 lbarcode->private->color = line_color;
322                 gl_label_object_emit_changed (GL_LABEL_OBJECT(lbarcode));
323         }
324 }
325
326 /*---------------------------------------------------------------------------*/
327 /* PRIVATE.  Get line color method.                                          */
328 /*---------------------------------------------------------------------------*/
329 static guint
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 lbarcode->private->color;
337 }
338