]> git.sur5r.net Git - glabels/blob - glabels2/src/label-barcode.c
Initial revision
[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         glBarcodeStyle  style;
43         guint           color;
44         gboolean        text_flag;
45         gdouble         scale;
46 };
47
48 /*========================================================*/
49 /* Private globals.                                       */
50 /*========================================================*/
51
52 static GObjectClass *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 update_size (glLabelBarcode *lbc);
65
66 \f
67 /*****************************************************************************/
68 /* Boilerplate object stuff.                                                 */
69 /*****************************************************************************/
70 GType
71 gl_label_barcode_get_type (void)
72 {
73         static GType type = 0;
74
75         if (!type) {
76                 GTypeInfo info = {
77                         sizeof (glLabelBarcodeClass),
78                         NULL,
79                         NULL,
80                         (GClassInitFunc) gl_label_barcode_class_init,
81                         NULL,
82                         NULL,
83                         sizeof (glLabelBarcode),
84                         0,
85                         (GInstanceInitFunc) gl_label_barcode_instance_init,
86                 };
87
88                 type = g_type_register_static (GL_TYPE_LABEL_OBJECT,
89                                                "glLabelBarcode", &info, 0);
90         }
91
92         return type;
93 }
94
95 static void
96 gl_label_barcode_class_init (glLabelBarcodeClass *klass)
97 {
98         GObjectClass *object_class = (GObjectClass *) klass;
99
100         parent_class = g_type_class_peek_parent (klass);
101
102         object_class->finalize = gl_label_barcode_finalize;
103 }
104
105 static void
106 gl_label_barcode_instance_init (glLabelBarcode *lbc)
107 {
108         lbc->private = g_new0 (glLabelBarcodePrivate, 1);
109 }
110
111 static void
112 gl_label_barcode_finalize (GObject *object)
113 {
114         glLabelBarcode *lbc;
115
116         g_return_if_fail (object && GL_IS_LABEL_BARCODE (object));
117
118         lbc = GL_LABEL_BARCODE (object);
119
120         gl_text_node_free (&lbc->private->text_node);
121         g_free (lbc->private);
122
123         G_OBJECT_CLASS (parent_class)->finalize (object);
124 }
125
126 /*****************************************************************************/
127 /* NEW label "text" object.                                               */
128 /*****************************************************************************/
129 GObject *
130 gl_label_barcode_new (glLabel *label)
131 {
132         glLabelBarcode *lbc;
133
134         lbc = g_object_new (gl_label_barcode_get_type(), NULL);
135
136         gl_label_object_set_parent (GL_LABEL_OBJECT(lbc), label);
137
138         return G_OBJECT (lbc);
139 }
140
141 /*****************************************************************************/
142 /* Duplicate object.                                                         */
143 /*****************************************************************************/
144 glLabelBarcode *
145 gl_label_barcode_dup (glLabelBarcode *lbc,
146                     glLabel *label)
147 {
148         glLabelBarcode      *new_lbc;
149         glTextNode          *text_node;
150         gdouble             x, y, w, h;
151         glBarcodeStyle      style;
152         gboolean            text_flag;
153         guint               color;
154         gdouble             scale;
155
156         gl_debug (DEBUG_LABEL, "START");
157
158         g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
159         g_return_if_fail (label && GL_IS_LABEL (label));
160
161         new_lbc = GL_LABEL_BARCODE(gl_label_barcode_new (label));
162
163         gl_label_object_get_position (GL_LABEL_OBJECT(lbc), &x, &y);
164         gl_label_object_get_size     (GL_LABEL_OBJECT(lbc), &w, &h);
165
166         gl_label_object_set_position (GL_LABEL_OBJECT(new_lbc),  x,  y);
167         gl_label_object_set_size     (GL_LABEL_OBJECT(new_lbc),  w,  h);
168
169         text_node = gl_label_barcode_get_data (lbc);
170         gl_label_barcode_get_props (lbc, &style, &text_flag, &color, &scale);
171
172         gl_label_barcode_set_data (new_lbc, text_node);
173         gl_label_barcode_set_props (new_lbc,style, text_flag, color, scale);
174
175         gl_text_node_free (&text_node);
176
177         gl_debug (DEBUG_LABEL, "END");
178
179         return new_lbc;
180 }
181
182
183 /*****************************************************************************/
184 /* Set object params.                                                        */
185 /*****************************************************************************/
186 void
187 gl_label_barcode_set_data (glLabelBarcode *lbc,
188                            glTextNode *text_node)
189 {
190         gl_debug (DEBUG_LABEL, "START");
191
192         g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
193
194         gl_text_node_free (&lbc->private->text_node);
195         lbc->private->text_node = gl_text_node_dup (text_node);
196
197         update_size (lbc);
198
199         gl_label_object_emit_changed (GL_LABEL_OBJECT(lbc));
200
201         gl_debug (DEBUG_LABEL, "END");
202 }
203
204 void
205 gl_label_barcode_set_props (glLabelBarcode *lbc,
206                             glBarcodeStyle style,
207                             gboolean text_flag,
208                             guint color,
209                             gdouble scale)
210 {
211         GdkPixbuf *pixbuf;
212
213         gl_debug (DEBUG_LABEL, "START");
214
215         g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
216
217         lbc->private->style            = style;
218         lbc->private->text_flag        = text_flag;
219         lbc->private->color            = color;
220         lbc->private->scale            = scale;
221
222         update_size (lbc);
223
224         gl_label_object_emit_changed (GL_LABEL_OBJECT(lbc));
225
226         gl_debug (DEBUG_LABEL, "END");
227 }
228
229
230 /*****************************************************************************/
231 /* Get object params.                                                        */
232 /*****************************************************************************/
233 glTextNode *
234 gl_label_barcode_get_data (glLabelBarcode *lbc)
235 {
236         g_return_val_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc), NULL);
237
238         return gl_text_node_dup (lbc->private->text_node);
239 }
240
241 void
242 gl_label_barcode_get_props (glLabelBarcode *lbc,
243                             glBarcodeStyle *style,
244                             gboolean *text_flag,
245                             guint *color,
246                             gdouble *scale)
247 {
248         g_return_if_fail (lbc && GL_IS_LABEL_BARCODE (lbc));
249
250         *style            = lbc->private->style;
251         *text_flag        = lbc->private->text_flag;
252         *color            = lbc->private->color;
253         *scale            = lbc->private->scale;
254 }
255
256 /*---------------------------------------------------------------------------*/
257 /* PRIVATE.  Update object size.                                             */
258 /*---------------------------------------------------------------------------*/
259 static void
260 update_size (glLabelBarcode *lbc)
261 {
262         gchar               *data;
263         glBarcode           *gbc;
264
265         gl_debug (DEBUG_LABEL, "START");
266
267         data = gl_barcode_default_digits (lbc->private->style);
268         gbc = gl_barcode_new (lbc->private->style,
269                               lbc->private->text_flag,
270                               lbc->private->scale,
271                               data);
272
273         gl_label_object_set_size (GL_LABEL_OBJECT(lbc),
274                                   gbc->width, gbc->height);
275
276         gl_barcode_free (&gbc);
277
278         gl_debug (DEBUG_LABEL, "END");
279 }
280