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
25 #include "label-barcode.h"
27 #include "pixmaps/checkerboard.xpm"
31 /*========================================================*/
32 /* Private macros and constants. */
33 /*========================================================*/
35 /*========================================================*/
37 /*========================================================*/
39 struct _glLabelBarcodePrivate {
40 glTextNode *text_node;
42 glColorNode *color_node;
44 gboolean checksum_flag;
48 /*========================================================*/
49 /* Private globals. */
50 /*========================================================*/
52 static glLabelObjectClass *parent_class = NULL;
54 static guint instance = 0;
56 /*========================================================*/
57 /* Private function prototypes. */
58 /*========================================================*/
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);
64 static void copy (glLabelObject *dst_object,
65 glLabelObject *src_object);
67 static void get_size (glLabelObject *object,
71 static void set_line_color (glLabelObject *object,
72 glColorNode *line_color);
74 static glColorNode *get_line_color (glLabelObject *object);
78 /*****************************************************************************/
79 /* Boilerplate object stuff. */
80 /*****************************************************************************/
82 gl_label_barcode_get_type (void)
84 static GType type = 0;
87 static const GTypeInfo info = {
88 sizeof (glLabelBarcodeClass),
91 (GClassInitFunc) gl_label_barcode_class_init,
94 sizeof (glLabelBarcode),
96 (GInstanceInitFunc) gl_label_barcode_instance_init,
100 type = g_type_register_static (GL_TYPE_LABEL_OBJECT,
101 "glLabelBarcode", &info, 0);
108 gl_label_barcode_class_init (glLabelBarcodeClass *klass)
110 GObjectClass *object_class = (GObjectClass *) klass;
111 glLabelObjectClass *label_object_class = (glLabelObjectClass *) klass;
113 parent_class = g_type_class_peek_parent (klass);
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;
120 object_class->finalize = gl_label_barcode_finalize;
124 gl_label_barcode_instance_init (glLabelBarcode *lbc)
126 lbc->private = g_new0 (glLabelBarcodePrivate, 1);
127 lbc->private->color_node = gl_color_node_new_default ();
131 gl_label_barcode_finalize (GObject *object)
135 g_return_if_fail (object && GL_IS_LABEL_BARCODE (object));
137 lbc = GL_LABEL_BARCODE (object);
139 gl_color_node_free (&(lbc->private->color_node));
140 gl_text_node_free (&lbc->private->text_node);
141 g_free (lbc->private);
143 G_OBJECT_CLASS (parent_class)->finalize (object);
146 /*****************************************************************************/
147 /* NEW label "text" object. */
148 /*****************************************************************************/
150 gl_label_barcode_new (glLabel *label)
154 lbc = g_object_new (gl_label_barcode_get_type(), NULL);
156 gl_label_object_set_parent (GL_LABEL_OBJECT(lbc), label);
158 return G_OBJECT (lbc);
161 /*****************************************************************************/
162 /* Copy object contents. */
163 /*****************************************************************************/
165 copy (glLabelObject *dst_object,
166 glLabelObject *src_object)
168 glLabelBarcode *lbc = (glLabelBarcode *)src_object;
169 glLabelBarcode *new_lbc = (glLabelBarcode *)dst_object;
170 glTextNode *text_node;
173 gboolean checksum_flag;
174 glColorNode *color_node;
177 gl_debug (DEBUG_LABEL, "START");
179 g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
180 g_return_if_fail (new_lbc && GL_IS_LABEL_BARCODE (new_lbc));
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);
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);
190 gl_color_node_free (&color_node);
191 gl_text_node_free (&text_node);
194 gl_debug (DEBUG_LABEL, "END");
198 /*****************************************************************************/
199 /* Set object params. */
200 /*****************************************************************************/
202 gl_label_barcode_set_data (glLabelBarcode *lbc,
203 glTextNode *text_node)
205 gl_debug (DEBUG_LABEL, "START");
207 g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
209 if (!gl_text_node_equal (lbc->private->text_node, text_node)) {
211 gl_text_node_free (&lbc->private->text_node);
212 lbc->private->text_node = gl_text_node_dup (text_node);
214 gl_label_object_emit_changed (GL_LABEL_OBJECT(lbc));
218 gl_debug (DEBUG_LABEL, "END");
222 gl_label_barcode_set_props (glLabelBarcode *lbc,
225 gboolean checksum_flag,
228 gl_debug (DEBUG_LABEL, "START");
230 g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
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)) {
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;
243 gl_label_object_emit_changed (GL_LABEL_OBJECT(lbc));
247 gl_debug (DEBUG_LABEL, "END");
251 /*****************************************************************************/
252 /* Get object params. */
253 /*****************************************************************************/
255 gl_label_barcode_get_data (glLabelBarcode *lbc)
257 g_return_val_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc), NULL);
259 return gl_text_node_dup (lbc->private->text_node);
263 gl_label_barcode_get_props (glLabelBarcode *lbc,
266 gboolean *checksum_flag,
267 guint *format_digits)
269 g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
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;
277 /*---------------------------------------------------------------------------*/
278 /* PRIVATE. Get object size method. */
279 /*---------------------------------------------------------------------------*/
281 get_size (glLabelObject *object,
285 glLabelBarcode *lbc = (glLabelBarcode *)object;
287 gdouble w_parent, h_parent;
290 gl_debug (DEBUG_LABEL, "START");
292 g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
294 (* parent_class->get_size) (object, &w_parent, &h_parent);
297 if (lbc->private->text_node->field_flag) {
298 data = gl_barcode_default_digits (lbc->private->id,
299 lbc->private->format_digits);
301 data = gl_text_node_expand (lbc->private->text_node, NULL);
304 gbc = gl_barcode_new (lbc->private->id,
305 lbc->private->text_flag,
306 lbc->private->checksum_flag,
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,
326 gl_barcode_free (&gbc);
329 gl_debug (DEBUG_LABEL, "END");
332 /*---------------------------------------------------------------------------*/
333 /* PRIVATE. Set line color method. */
334 /*---------------------------------------------------------------------------*/
336 set_line_color (glLabelObject *object,
337 glColorNode *line_color_node)
339 glLabelBarcode *lbarcode = (glLabelBarcode *)object;
341 g_return_if_fail (lbarcode && GL_IS_LABEL_BARCODE (lbarcode));
343 if ( !gl_color_node_equal(lbarcode->private->color_node, line_color_node) ) {
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));
351 /*---------------------------------------------------------------------------*/
352 /* PRIVATE. Get line color method. */
353 /*---------------------------------------------------------------------------*/
355 get_line_color (glLabelObject *object)
357 glLabelBarcode *lbarcode = (glLabelBarcode *)object;
359 g_return_if_fail (lbarcode && GL_IS_LABEL_BARCODE (lbarcode));
361 return gl_color_node_dup (lbarcode->private->color_node);