2 * (GLABELS) Label and Business Card Creation program for GNOME
4 * label_barcode.c: GLabels label text object
6 * Copyright (C) 2001-2002 Jim Evins <evins@snaught.com>.
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.
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.
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
23 #include "label-barcode.h"
25 #include <glib/gmem.h>
26 #include <glib/gstrfuncs.h>
27 #include <glib/gmessages.h>
29 #include "pixmaps/checkerboard.xpm"
33 /*========================================================*/
34 /* Private macros and constants. */
35 /*========================================================*/
37 /*========================================================*/
39 /*========================================================*/
41 struct _glLabelBarcodePrivate {
42 glTextNode *text_node;
44 glColorNode *color_node;
46 gboolean checksum_flag;
50 /*========================================================*/
51 /* Private globals. */
52 /*========================================================*/
54 static glLabelObjectClass *parent_class = NULL;
56 static guint instance = 0;
58 /*========================================================*/
59 /* Private function prototypes. */
60 /*========================================================*/
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);
66 static void copy (glLabelObject *dst_object,
67 glLabelObject *src_object);
69 static void get_size (glLabelObject *object,
73 static void set_line_color (glLabelObject *object,
74 glColorNode *line_color);
76 static glColorNode *get_line_color (glLabelObject *object);
80 /*****************************************************************************/
81 /* Boilerplate object stuff. */
82 /*****************************************************************************/
84 gl_label_barcode_get_type (void)
86 static GType type = 0;
89 static const GTypeInfo info = {
90 sizeof (glLabelBarcodeClass),
93 (GClassInitFunc) gl_label_barcode_class_init,
96 sizeof (glLabelBarcode),
98 (GInstanceInitFunc) gl_label_barcode_instance_init,
102 type = g_type_register_static (GL_TYPE_LABEL_OBJECT,
103 "glLabelBarcode", &info, 0);
110 gl_label_barcode_class_init (glLabelBarcodeClass *klass)
112 GObjectClass *object_class = (GObjectClass *) klass;
113 glLabelObjectClass *label_object_class = (glLabelObjectClass *) klass;
115 parent_class = g_type_class_peek_parent (klass);
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;
122 object_class->finalize = gl_label_barcode_finalize;
126 gl_label_barcode_instance_init (glLabelBarcode *lbc)
128 lbc->private = g_new0 (glLabelBarcodePrivate, 1);
129 lbc->private->color_node = gl_color_node_new_default ();
133 gl_label_barcode_finalize (GObject *object)
137 g_return_if_fail (object && GL_IS_LABEL_BARCODE (object));
139 lbc = GL_LABEL_BARCODE (object);
141 gl_color_node_free (&(lbc->private->color_node));
142 gl_text_node_free (&lbc->private->text_node);
143 g_free (lbc->private);
145 G_OBJECT_CLASS (parent_class)->finalize (object);
148 /*****************************************************************************/
149 /* NEW label "text" object. */
150 /*****************************************************************************/
152 gl_label_barcode_new (glLabel *label)
156 lbc = g_object_new (gl_label_barcode_get_type(), NULL);
158 gl_label_object_set_parent (GL_LABEL_OBJECT(lbc), label);
160 return G_OBJECT (lbc);
163 /*****************************************************************************/
164 /* Copy object contents. */
165 /*****************************************************************************/
167 copy (glLabelObject *dst_object,
168 glLabelObject *src_object)
170 glLabelBarcode *lbc = (glLabelBarcode *)src_object;
171 glLabelBarcode *new_lbc = (glLabelBarcode *)dst_object;
172 glTextNode *text_node;
175 gboolean checksum_flag;
176 glColorNode *color_node;
179 gl_debug (DEBUG_LABEL, "START");
181 g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
182 g_return_if_fail (new_lbc && GL_IS_LABEL_BARCODE (new_lbc));
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);
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);
192 gl_color_node_free (&color_node);
193 gl_text_node_free (&text_node);
196 gl_debug (DEBUG_LABEL, "END");
200 /*****************************************************************************/
201 /* Set object params. */
202 /*****************************************************************************/
204 gl_label_barcode_set_data (glLabelBarcode *lbc,
205 glTextNode *text_node)
207 gl_debug (DEBUG_LABEL, "START");
209 g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
211 if (!gl_text_node_equal (lbc->private->text_node, text_node)) {
213 gl_text_node_free (&lbc->private->text_node);
214 lbc->private->text_node = gl_text_node_dup (text_node);
216 gl_label_object_emit_changed (GL_LABEL_OBJECT(lbc));
220 gl_debug (DEBUG_LABEL, "END");
224 gl_label_barcode_set_props (glLabelBarcode *lbc,
227 gboolean checksum_flag,
230 gl_debug (DEBUG_LABEL, "START");
232 g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
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)) {
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;
245 gl_label_object_emit_changed (GL_LABEL_OBJECT(lbc));
249 gl_debug (DEBUG_LABEL, "END");
253 /*****************************************************************************/
254 /* Get object params. */
255 /*****************************************************************************/
257 gl_label_barcode_get_data (glLabelBarcode *lbc)
259 g_return_val_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc), NULL);
261 return gl_text_node_dup (lbc->private->text_node);
265 gl_label_barcode_get_props (glLabelBarcode *lbc,
268 gboolean *checksum_flag,
269 guint *format_digits)
271 g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
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;
279 /*---------------------------------------------------------------------------*/
280 /* PRIVATE. Get object size method. */
281 /*---------------------------------------------------------------------------*/
283 get_size (glLabelObject *object,
287 glLabelBarcode *lbc = (glLabelBarcode *)object;
289 gdouble w_parent, h_parent;
292 gl_debug (DEBUG_LABEL, "START");
294 g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
296 (* parent_class->get_size) (object, &w_parent, &h_parent);
299 if (lbc->private->text_node->field_flag) {
300 data = gl_barcode_default_digits (lbc->private->id,
301 lbc->private->format_digits);
303 data = gl_text_node_expand (lbc->private->text_node, NULL);
306 gbc = gl_barcode_new (lbc->private->id,
307 lbc->private->text_flag,
308 lbc->private->checksum_flag,
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,
328 gl_barcode_free (&gbc);
331 gl_debug (DEBUG_LABEL, "END");
334 /*---------------------------------------------------------------------------*/
335 /* PRIVATE. Set line color method. */
336 /*---------------------------------------------------------------------------*/
338 set_line_color (glLabelObject *object,
339 glColorNode *line_color_node)
341 glLabelBarcode *lbarcode = (glLabelBarcode *)object;
343 g_return_if_fail (lbarcode && GL_IS_LABEL_BARCODE (lbarcode));
345 if ( !gl_color_node_equal(lbarcode->private->color_node, line_color_node) ) {
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));
353 /*---------------------------------------------------------------------------*/
354 /* PRIVATE. Get line color method. */
355 /*---------------------------------------------------------------------------*/
357 get_line_color (glLabelObject *object)
359 glLabelBarcode *lbarcode = (glLabelBarcode *)object;
361 g_return_if_fail (lbarcode && GL_IS_LABEL_BARCODE (lbarcode));
363 return gl_color_node_dup (lbarcode->private->color_node);