summaryrefslogtreecommitdiff
path: root/glabels2/src/label-barcode.c
blob: 9ec54c970480041f84f1050f530c9b63223f77be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
 *  (GLABELS) Label and Business Card Creation program for GNOME
 *
 *  label_barcode.c:  GLabels label text object
 *
 *  Copyright (C) 2001-2002  Jim Evins <evins@snaught.com>.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include "label-barcode.h"

#include <glib/gmem.h>
#include <glib/gstrfuncs.h>
#include <glib/gmessages.h>

#include "pixmaps/checkerboard.xpm"

#include "debug.h"

/*========================================================*/
/* Private macros and constants.                          */
/*========================================================*/

/*========================================================*/
/* Private types.                                         */
/*========================================================*/

struct _glLabelBarcodePrivate {
	glTextNode     *text_node;
	gchar          *id;
	glColorNode    *color_node;
	gboolean        text_flag;
	gboolean        checksum_flag;
	guint           format_digits;
};

/*========================================================*/
/* Private globals.                                       */
/*========================================================*/

static glLabelObjectClass *parent_class = NULL;

static guint instance = 0;

/*========================================================*/
/* Private function prototypes.                           */
/*========================================================*/

static void  gl_label_barcode_class_init    (glLabelBarcodeClass *klass);
static void  gl_label_barcode_instance_init (glLabelBarcode      *lbc);
static void  gl_label_barcode_finalize      (GObject             *object);

static void  copy                           (glLabelObject       *dst_object,
					     glLabelObject       *src_object);

static void  get_size                       (glLabelObject       *object,
					     gdouble             *w,
					     gdouble             *h);

static void  set_line_color                 (glLabelObject       *object,
					     glColorNode         *line_color);

static glColorNode *get_line_color           (glLabelObject       *object);



/*****************************************************************************/
/* Boilerplate object stuff.                                                 */
/*****************************************************************************/
GType
gl_label_barcode_get_type (void)
{
	static GType type = 0;

	if (!type) {
		static const GTypeInfo info = {
			sizeof (glLabelBarcodeClass),
			NULL,
			NULL,
			(GClassInitFunc) gl_label_barcode_class_init,
			NULL,
			NULL,
			sizeof (glLabelBarcode),
			0,
			(GInstanceInitFunc) gl_label_barcode_instance_init,
			NULL
		};

		type = g_type_register_static (GL_TYPE_LABEL_OBJECT,
					       "glLabelBarcode", &info, 0);
	}

	return type;
}

static void
gl_label_barcode_class_init (glLabelBarcodeClass *klass)
{
	GObjectClass       *object_class       = (GObjectClass *) klass;
	glLabelObjectClass *label_object_class = (glLabelObjectClass *) klass;

	parent_class = g_type_class_peek_parent (klass);

	label_object_class->copy           = copy;
	label_object_class->get_size       = get_size;
	label_object_class->set_line_color = set_line_color;
	label_object_class->get_line_color = get_line_color;

	object_class->finalize = gl_label_barcode_finalize;
}

static void
gl_label_barcode_instance_init (glLabelBarcode *lbc)
{
	lbc->private = g_new0 (glLabelBarcodePrivate, 1);
	lbc->private->color_node = gl_color_node_new_default ();
}

static void
gl_label_barcode_finalize (GObject *object)
{
	glLabelBarcode *lbc;

	g_return_if_fail (object && GL_IS_LABEL_BARCODE (object));

	lbc = GL_LABEL_BARCODE (object);

	gl_color_node_free (&(lbc->private->color_node));
	gl_text_node_free (&lbc->private->text_node);
	g_free (lbc->private);

	G_OBJECT_CLASS (parent_class)->finalize (object);
}

/*****************************************************************************/
/* NEW label "text" object.                                               */
/*****************************************************************************/
GObject *
gl_label_barcode_new (glLabel *label)
{
	glLabelBarcode *lbc;

	lbc = g_object_new (gl_label_barcode_get_type(), NULL);

	gl_label_object_set_parent (GL_LABEL_OBJECT(lbc), label);

	return G_OBJECT (lbc);
}

/*****************************************************************************/
/* Copy object contents.                                                     */
/*****************************************************************************/
static void
copy (glLabelObject *dst_object,
      glLabelObject *src_object)
{
	glLabelBarcode      *lbc     = (glLabelBarcode *)src_object;
	glLabelBarcode      *new_lbc = (glLabelBarcode *)dst_object;
	glTextNode          *text_node;
	gchar               *id;
	gboolean             text_flag;
	gboolean             checksum_flag;
	glColorNode         *color_node;
	guint                format_digits;

	gl_debug (DEBUG_LABEL, "START");

	g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
	g_return_if_fail (new_lbc && GL_IS_LABEL_BARCODE (new_lbc));

	text_node = gl_label_barcode_get_data (lbc);
	gl_label_barcode_get_props (lbc, &id, &text_flag, &checksum_flag, &format_digits);
	color_node = get_line_color (src_object);

	gl_label_barcode_set_data (new_lbc, text_node);
	gl_label_barcode_set_props (new_lbc, id, text_flag, checksum_flag, format_digits);
	set_line_color (dst_object, color_node);

	gl_color_node_free (&color_node);
	gl_text_node_free (&text_node);
	g_free (id);

	gl_debug (DEBUG_LABEL, "END");
}


/*****************************************************************************/
/* Set object params.                                                        */
/*****************************************************************************/
void
gl_label_barcode_set_data (glLabelBarcode *lbc,
			   glTextNode     *text_node)
{
	gl_debug (DEBUG_LABEL, "START");

	g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));

	if (!gl_text_node_equal (lbc->private->text_node, text_node)) {

		gl_text_node_free (&lbc->private->text_node);
		lbc->private->text_node = gl_text_node_dup (text_node);

		gl_label_object_emit_changed (GL_LABEL_OBJECT(lbc));

	}

	gl_debug (DEBUG_LABEL, "END");
}

void
gl_label_barcode_set_props (glLabelBarcode *lbc,
			    gchar          *id,
			    gboolean        text_flag,
			    gboolean        checksum_flag,
			    guint           format_digits)
{
	gl_debug (DEBUG_LABEL, "START");

	g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));

	if ( ((lbc->private->id == NULL) && (id != NULL))
	     || (g_strcasecmp (lbc->private->id, id) != 0)
	     || (lbc->private->text_flag != text_flag)
	     || (lbc->private->checksum_flag != checksum_flag)
	     || (lbc->private->format_digits != format_digits)) {

		lbc->private->id               = g_strdup (id);
		lbc->private->text_flag        = text_flag;
		lbc->private->checksum_flag    = checksum_flag;
		lbc->private->format_digits    = format_digits;

		gl_label_object_emit_changed (GL_LABEL_OBJECT(lbc));

	}

	gl_debug (DEBUG_LABEL, "END");
}


/*****************************************************************************/
/* Get object params.                                                        */
/*****************************************************************************/
glTextNode *
gl_label_barcode_get_data (glLabelBarcode *lbc)
{
	g_return_val_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc), NULL);

	return gl_text_node_dup (lbc->private->text_node);
}

void
gl_label_barcode_get_props (glLabelBarcode *lbc,
			    gchar          **id,
			    gboolean       *text_flag,
			    gboolean       *checksum_flag,
			    guint          *format_digits)
{
	g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));

	*id               = g_strdup (lbc->private->id);
	*text_flag        = lbc->private->text_flag;
	*checksum_flag    = lbc->private->checksum_flag;
	*format_digits    = lbc->private->format_digits;
}

/*---------------------------------------------------------------------------*/
/* PRIVATE.  Get object size method.                                         */
/*---------------------------------------------------------------------------*/
static void
get_size (glLabelObject *object,
	  gdouble       *w,
	  gdouble       *h)
{
	glLabelBarcode      *lbc = (glLabelBarcode *)object;
	gchar               *data;
	gdouble              w_parent, h_parent;
	glBarcode           *gbc;

	gl_debug (DEBUG_LABEL, "START");

	g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));

	(* parent_class->get_size) (object, &w_parent, &h_parent);


	if (lbc->private->text_node->field_flag) {
		data = gl_barcode_default_digits (lbc->private->id,
						  lbc->private->format_digits);
	} else {
		data = gl_text_node_expand (lbc->private->text_node, NULL);
	}

	gbc = gl_barcode_new (lbc->private->id,
			      lbc->private->text_flag,
			      lbc->private->checksum_flag,
			      w_parent,
			      h_parent,
			      data);

	if ( gbc == NULL ) {
		/* Try again with default digits. */
		data = gl_barcode_default_digits (lbc->private->id,
						  lbc->private->format_digits);
		gbc = gl_barcode_new (lbc->private->id,
				      lbc->private->text_flag,
				      lbc->private->checksum_flag,
				      w_parent,
				      h_parent,
				      data);
	}

	*w = gbc->width;
	*h = gbc->height;

	gl_barcode_free (&gbc);
	g_free (data);

	gl_debug (DEBUG_LABEL, "END");
}

/*---------------------------------------------------------------------------*/
/* PRIVATE.  Set line color method.                                          */
/*---------------------------------------------------------------------------*/
static void
set_line_color (glLabelObject *object,
		glColorNode   *line_color_node)
{
	glLabelBarcode *lbarcode = (glLabelBarcode *)object;

	g_return_if_fail (lbarcode && GL_IS_LABEL_BARCODE (lbarcode));

	if ( !gl_color_node_equal(lbarcode->private->color_node, line_color_node) ) {
		
		gl_color_node_free (&(lbarcode->private->color_node));
		lbarcode->private->color_node = gl_color_node_dup (line_color_node);
		gl_label_object_emit_changed (GL_LABEL_OBJECT(lbarcode));
	}
}

/*---------------------------------------------------------------------------*/
/* PRIVATE.  Get line color method.                                          */
/*---------------------------------------------------------------------------*/
static glColorNode*
get_line_color (glLabelObject *object)
{
	glLabelBarcode *lbarcode = (glLabelBarcode *)object;

	g_return_if_fail (lbarcode && GL_IS_LABEL_BARCODE (lbarcode));

	return gl_color_node_dup (lbarcode->private->color_node);
}