1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
4 * (GLABELS) Label and Business Card Creation program for GNOME
6 * label_barcode.c: GLabels label text object
8 * Copyright (C) 2001-2002 Jim Evins <evins@snaught.com>.
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.
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.
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
25 #include "label-barcode.h"
27 #include <glib/gmem.h>
28 #include <glib/gstrfuncs.h>
29 #include <glib/gmessages.h>
31 #include "pixmaps/checkerboard.xpm"
35 /*========================================================*/
36 /* Private macros and constants. */
37 /*========================================================*/
39 /*========================================================*/
41 /*========================================================*/
43 struct _glLabelBarcodePrivate {
44 glTextNode *text_node;
46 glColorNode *color_node;
48 gboolean checksum_flag;
52 /*========================================================*/
53 /* Private globals. */
54 /*========================================================*/
56 static guint instance = 0;
58 /*========================================================*/
59 /* Private function prototypes. */
60 /*========================================================*/
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 /*****************************************************************************/
81 G_DEFINE_TYPE (glLabelBarcode, gl_label_barcode, GL_TYPE_LABEL_OBJECT);
84 gl_label_barcode_class_init (glLabelBarcodeClass *class)
86 GObjectClass *object_class = G_OBJECT_CLASS (class);
87 glLabelObjectClass *label_object_class = GL_LABEL_OBJECT_CLASS (class);
89 gl_label_barcode_parent_class = g_type_class_peek_parent (class);
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;
96 object_class->finalize = gl_label_barcode_finalize;
100 gl_label_barcode_init (glLabelBarcode *lbc)
102 lbc->priv = g_new0 (glLabelBarcodePrivate, 1);
103 lbc->priv->color_node = gl_color_node_new_default ();
107 gl_label_barcode_finalize (GObject *object)
109 glLabelBarcode *lbc = GL_LABEL_BARCODE (object);
111 g_return_if_fail (object && GL_IS_LABEL_BARCODE (object));
113 gl_text_node_free (&lbc->priv->text_node);
114 g_free (lbc->priv->id);
115 gl_color_node_free (&(lbc->priv->color_node));
118 G_OBJECT_CLASS (gl_label_barcode_parent_class)->finalize (object);
121 /*****************************************************************************/
122 /* NEW label "text" object. */
123 /*****************************************************************************/
125 gl_label_barcode_new (glLabel *label)
129 lbc = g_object_new (gl_label_barcode_get_type(), NULL);
131 gl_label_object_set_parent (GL_LABEL_OBJECT(lbc), label);
133 return G_OBJECT (lbc);
136 /*****************************************************************************/
137 /* Copy object contents. */
138 /*****************************************************************************/
140 copy (glLabelObject *dst_object,
141 glLabelObject *src_object)
143 glLabelBarcode *lbc = (glLabelBarcode *)src_object;
144 glLabelBarcode *new_lbc = (glLabelBarcode *)dst_object;
145 glTextNode *text_node;
148 gboolean checksum_flag;
149 glColorNode *color_node;
152 gl_debug (DEBUG_LABEL, "START");
154 g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
155 g_return_if_fail (new_lbc && GL_IS_LABEL_BARCODE (new_lbc));
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);
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);
165 gl_color_node_free (&color_node);
166 gl_text_node_free (&text_node);
169 gl_debug (DEBUG_LABEL, "END");
173 /*****************************************************************************/
174 /* Set object params. */
175 /*****************************************************************************/
177 gl_label_barcode_set_data (glLabelBarcode *lbc,
178 glTextNode *text_node)
180 gl_debug (DEBUG_LABEL, "START");
182 g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
184 if (!gl_text_node_equal (lbc->priv->text_node, text_node)) {
186 gl_text_node_free (&lbc->priv->text_node);
187 lbc->priv->text_node = gl_text_node_dup (text_node);
189 gl_label_object_emit_changed (GL_LABEL_OBJECT(lbc));
193 gl_debug (DEBUG_LABEL, "END");
197 gl_label_barcode_set_props (glLabelBarcode *lbc,
200 gboolean checksum_flag,
203 gl_debug (DEBUG_LABEL, "START");
205 g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
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)) {
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;
218 gl_label_object_emit_changed (GL_LABEL_OBJECT(lbc));
222 gl_debug (DEBUG_LABEL, "END");
226 /*****************************************************************************/
227 /* Get object params. */
228 /*****************************************************************************/
230 gl_label_barcode_get_data (glLabelBarcode *lbc)
232 g_return_val_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc), NULL);
234 return gl_text_node_dup (lbc->priv->text_node);
238 gl_label_barcode_get_props (glLabelBarcode *lbc,
241 gboolean *checksum_flag,
242 guint *format_digits)
244 g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
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;
252 /*---------------------------------------------------------------------------*/
253 /* PRIVATE. Get object size method. */
254 /*---------------------------------------------------------------------------*/
256 get_size (glLabelObject *object,
260 glLabelBarcode *lbc = (glLabelBarcode *)object;
262 gdouble w_parent, h_parent;
265 gl_debug (DEBUG_LABEL, "START");
267 g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
269 (* GL_LABEL_OBJECT_CLASS (gl_label_barcode_parent_class)->get_size) (object, &w_parent, &h_parent);
272 if (lbc->priv->text_node->field_flag) {
273 data = gl_barcode_default_digits (lbc->priv->id,
274 lbc->priv->format_digits);
276 data = gl_text_node_expand (lbc->priv->text_node, NULL);
279 gbc = gl_barcode_new (lbc->priv->id,
280 lbc->priv->text_flag,
281 lbc->priv->checksum_flag,
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,
301 gl_barcode_free (&gbc);
304 gl_debug (DEBUG_LABEL, "END");
307 /*---------------------------------------------------------------------------*/
308 /* PRIVATE. Set line color method. */
309 /*---------------------------------------------------------------------------*/
311 set_line_color (glLabelObject *object,
312 glColorNode *line_color_node)
314 glLabelBarcode *lbarcode = (glLabelBarcode *)object;
316 g_return_if_fail (lbarcode && GL_IS_LABEL_BARCODE (lbarcode));
318 if ( !gl_color_node_equal(lbarcode->priv->color_node, line_color_node) ) {
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));
326 /*---------------------------------------------------------------------------*/
327 /* PRIVATE. Get line color method. */
328 /*---------------------------------------------------------------------------*/
330 get_line_color (glLabelObject *object)
332 glLabelBarcode *lbarcode = (glLabelBarcode *)object;
334 g_return_if_fail (lbarcode && GL_IS_LABEL_BARCODE (lbarcode));
336 return gl_color_node_dup (lbarcode->priv->color_node);