]> git.sur5r.net Git - glabels/blob - glabels2/src/view-barcode.c
2007-09-27 Jim Evins <evins@snaught.com>
[glabels] / glabels2 / src / view-barcode.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2
3 /*
4  *  (GLABELS) Label and Business Card Creation program for GNOME
5  *
6  *  view_barcode.c:  GLabels label barcode object view
7  *
8  *  Copyright (C) 2001-2007  Jim Evins <evins@snaught.com>.
9  *
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.
14  *
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.
19  *
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
23  */
24 #include <config.h>
25
26 #include "view-barcode.h"
27
28 #include <glib/gi18n.h>
29 #include <glib/gmem.h>
30
31 #include "color.h"
32 #include "object-editor.h"
33 #include "stock.h"
34
35 #include "pixmaps/cursor_barcode.xbm"
36 #include "pixmaps/cursor_barcode_mask.xbm"
37
38 #include "debug.h"
39
40 /*========================================================*/
41 /* Private macros and constants.                          */
42 /*========================================================*/
43
44 /*========================================================*/
45 /* Private types.                                         */
46 /*========================================================*/
47
48 struct _glViewBarcodePrivate {
49 };
50
51 /*========================================================*/
52 /* Private globals.                                       */
53 /*========================================================*/
54
55
56 /*========================================================*/
57 /* Private function prototypes.                           */
58 /*========================================================*/
59
60 static void       gl_view_barcode_finalize          (GObject          *object);
61
62 static GtkWidget *construct_properties_editor       (glViewObject     *view_object);
63
64 static void       update_object_from_editor_cb      (glObjectEditor   *editor,
65                                                      glLabelObject    *object);
66
67 static void       update_editor_from_object_cb      (glLabelObject    *object,
68                                                      glObjectEditor   *editor);
69
70 static void       update_editor_from_move_cb        (glLabelObject    *object,
71                                                      gdouble           dx,
72                                                      gdouble           dy,
73                                                      glObjectEditor   *editor);
74
75 static void       update_editor_from_label_cb       (glLabel          *label,
76                                                      glObjectEditor   *editor);
77
78 static gboolean   object_at                         (glViewObject     *view_object,
79                                                      cairo_t          *cr,
80                                                      gdouble           x,
81                                                      gdouble           y);
82
83
84
85 \f
86 /*****************************************************************************/
87 /* Boilerplate object stuff.                                                 */
88 /*****************************************************************************/
89 G_DEFINE_TYPE (glViewBarcode, gl_view_barcode, GL_TYPE_VIEW_OBJECT);
90
91
92 static void
93 gl_view_barcode_class_init (glViewBarcodeClass *class)
94 {
95         GObjectClass      *object_class      = G_OBJECT_CLASS (class);
96         glViewObjectClass *view_object_class = GL_VIEW_OBJECT_CLASS (class);
97
98         gl_debug (DEBUG_VIEW, "START");
99
100         gl_view_barcode_parent_class = g_type_class_peek_parent (class);
101
102         object_class->finalize = gl_view_barcode_finalize;
103
104         view_object_class->construct_editor = construct_properties_editor;
105         view_object_class->object_at        = object_at;
106
107         gl_debug (DEBUG_VIEW, "END");
108 }
109
110 static void
111 gl_view_barcode_init (glViewBarcode *view_barcode)
112 {
113         gl_debug (DEBUG_VIEW, "START");
114
115         view_barcode->priv = g_new0 (glViewBarcodePrivate, 1);
116
117         gl_debug (DEBUG_VIEW, "END");
118 }
119
120 static void
121 gl_view_barcode_finalize (GObject *object)
122 {
123         glViewBarcode *view_barcode = GL_VIEW_BARCODE (object);
124
125         gl_debug (DEBUG_VIEW, "START");
126
127         g_return_if_fail (object && GL_IS_VIEW_BARCODE (object));
128
129         g_free (view_barcode->priv);
130
131         G_OBJECT_CLASS (gl_view_barcode_parent_class)->finalize (object);
132
133         gl_debug (DEBUG_VIEW, "END");
134 }
135
136 /*****************************************************************************/
137 /* NEW barcode object view.                                                  */
138 /*****************************************************************************/
139 glViewObject *
140 gl_view_barcode_new (glLabelBarcode *object,
141                      glView         *view)
142 {
143         glViewBarcode         *view_barcode;
144
145         gl_debug (DEBUG_VIEW, "START");
146
147         g_return_val_if_fail (object && GL_IS_LABEL_BARCODE (object), NULL);
148         g_return_val_if_fail (view && GL_IS_VIEW (view), NULL);
149         
150         view_barcode = g_object_new (gl_view_barcode_get_type(), NULL);
151
152         gl_view_object_set_object (GL_VIEW_OBJECT(view_barcode),
153                                    GL_LABEL_OBJECT(object),
154                                    GL_VIEW_OBJECT_HANDLES_BOX);
155         gl_view_object_set_view (GL_VIEW_OBJECT(view_barcode), view);
156
157         gl_debug (DEBUG_VIEW, "END");
158
159         return GL_VIEW_OBJECT (view_barcode);
160 }
161
162 /*****************************************************************************/
163 /* Create a properties dialog for a barcode object.                          */
164 /*****************************************************************************/
165 static GtkWidget *
166 construct_properties_editor (glViewObject *view_object)
167 {
168         GtkWidget          *editor;
169         glViewBarcode      *view_bc = (glViewBarcode *)view_object;
170         glLabelObject      *object;
171
172         gl_debug (DEBUG_VIEW, "START");
173
174         object = gl_view_object_get_object (GL_VIEW_OBJECT(view_bc));
175
176         /* Build editor. */
177         editor = gl_object_editor_new (GL_STOCK_BARCODE, _("Barcode object properties"),
178                                        GL_OBJECT_EDITOR_POSITION_PAGE,
179                                        GL_OBJECT_EDITOR_SIZE_PAGE,
180                                        GL_OBJECT_EDITOR_BC_PAGE,
181                                        GL_OBJECT_EDITOR_DATA_PAGE,
182                                        0);
183
184         /* Update */
185         update_editor_from_label_cb (object->parent, GL_OBJECT_EDITOR(editor));
186         update_editor_from_object_cb (object, GL_OBJECT_EDITOR(editor));
187         update_editor_from_move_cb (object, 0, 0, GL_OBJECT_EDITOR(editor));
188
189         /* Connect signals. */
190         g_signal_connect (G_OBJECT (editor), "changed",
191                           G_CALLBACK(update_object_from_editor_cb), object);
192         g_signal_connect (G_OBJECT (object), "changed",
193                           G_CALLBACK (update_editor_from_object_cb), editor);
194         g_signal_connect (G_OBJECT (object), "moved",
195                           G_CALLBACK (update_editor_from_move_cb), editor);
196         g_signal_connect (G_OBJECT (object->parent), "size_changed",
197                           G_CALLBACK (update_editor_from_label_cb), editor);
198         g_signal_connect (G_OBJECT (object->parent), "merge_changed",
199                           G_CALLBACK (update_editor_from_label_cb), editor);
200
201         gl_debug (DEBUG_VIEW, "END");
202
203         return editor;
204 }
205
206 /*---------------------------------------------------------------------------*/
207 /* PRIVATE.  editor "changed" callback.                                      */
208 /*---------------------------------------------------------------------------*/
209 static void
210 update_object_from_editor_cb (glObjectEditor *editor,
211                               glLabelObject  *object)
212 {
213         gdouble            x, y, w, h;
214         glTextNode        *text_node;
215         gchar             *id;
216         gboolean           text_flag, cs_flag;
217         glColorNode       *color_node;
218         guint              format_digits;
219
220         gl_debug (DEBUG_VIEW, "START");
221
222         g_signal_handlers_block_by_func (G_OBJECT(object),
223                                          update_editor_from_object_cb,
224                                          editor);
225         g_signal_handlers_block_by_func (G_OBJECT(object),
226                                          update_editor_from_move_cb,
227                                          editor);
228
229
230         gl_object_editor_get_position (editor, &x, &y);
231         gl_label_object_set_position (object, x, y);
232
233         gl_object_editor_get_size (editor, &w, &h);
234         gl_label_object_set_size (object, w, h);
235
236         text_node = gl_object_editor_get_data (editor);
237         gl_label_barcode_set_data (GL_LABEL_BARCODE(object), text_node);
238         gl_text_node_free (&text_node);
239
240         gl_object_editor_get_bc_style (editor, &id, &text_flag, &cs_flag, &format_digits);
241         color_node = gl_object_editor_get_bc_color (editor);
242         gl_label_barcode_set_props (GL_LABEL_BARCODE(object),
243                                     id, text_flag, cs_flag, format_digits);
244         gl_label_object_set_line_color (object, color_node);
245         gl_color_node_free (&color_node);
246         g_free (id);
247
248         g_signal_handlers_unblock_by_func (G_OBJECT(object),
249                                            update_editor_from_object_cb,
250                                            editor);
251         g_signal_handlers_unblock_by_func (G_OBJECT(object),
252                                            update_editor_from_move_cb,
253                                            editor);
254
255         gl_debug (DEBUG_VIEW, "END");
256 }
257
258 /*---------------------------------------------------------------------------*/
259 /* PRIVATE. label object "changed" callback.                                 */
260 /*---------------------------------------------------------------------------*/
261 static void
262 update_editor_from_object_cb (glLabelObject  *object,
263                               glObjectEditor *editor)
264 {
265         gdouble            w, h;
266         glTextNode        *text_node;
267         gchar             *id;
268         gboolean           text_flag, cs_flag;
269         glColorNode       *color_node;
270         glMerge           *merge;
271         guint              format_digits;
272
273         gl_debug (DEBUG_VIEW, "START");
274
275         gl_label_object_get_size (object, &w, &h);
276         gl_object_editor_set_size (editor, w, h);
277         
278         merge = gl_label_get_merge (GL_LABEL(object->parent));
279
280         gl_label_barcode_get_props (GL_LABEL_BARCODE(object),
281                                     &id, &text_flag, &cs_flag, &format_digits);
282         color_node = gl_label_object_get_line_color (object);
283         gl_object_editor_set_bc_style (editor, id, text_flag, cs_flag, format_digits);
284         gl_object_editor_set_bc_color (editor, (merge != NULL), color_node);
285         gl_color_node_free (&color_node);
286         g_free (id);
287
288         text_node = gl_label_barcode_get_data (GL_LABEL_BARCODE(object));
289         gl_object_editor_set_data (editor, (merge != NULL), text_node);
290         gl_text_node_free (&text_node);
291
292
293         gl_debug (DEBUG_VIEW, "END");
294 }
295
296 /*---------------------------------------------------------------------------*/
297 /* PRIVATE. label object "moved" callback.                                   */
298 /*---------------------------------------------------------------------------*/
299 static void
300 update_editor_from_move_cb (glLabelObject    *object,
301                             gdouble           dx,
302                             gdouble           dy,
303                             glObjectEditor   *editor)
304 {
305         gdouble            x, y;
306
307         gl_debug (DEBUG_VIEW, "START");
308
309         gl_label_object_get_position (object, &x, &y);
310         gl_object_editor_set_position (editor, x, y);
311
312         gl_debug (DEBUG_VIEW, "END");
313 }
314
315 /*---------------------------------------------------------------------------*/
316 /* PRIVATE. label "changed" callback.                                        */
317 /*---------------------------------------------------------------------------*/
318 static void
319 update_editor_from_label_cb (glLabel        *label,
320                              glObjectEditor *editor)
321 {
322         gdouble            label_width, label_height;
323         glMerge                    *merge;
324
325         gl_debug (DEBUG_VIEW, "START");
326
327         gl_label_get_size (label, &label_width, &label_height);
328         gl_object_editor_set_max_position (GL_OBJECT_EDITOR (editor),
329                                            label_width, label_height);
330         gl_object_editor_set_max_size (GL_OBJECT_EDITOR (editor),
331                                        label_width, label_height);
332
333         merge = gl_label_get_merge (label);
334         gl_object_editor_set_key_names (editor, merge);
335
336         gl_debug (DEBUG_VIEW, "END");
337 }
338
339 /*****************************************************************************/
340 /* Is object at (x,y)?                                                       */
341 /*****************************************************************************/
342 static gboolean
343 object_at (glViewObject  *view_object,
344            cairo_t       *cr,
345            gdouble        x,
346            gdouble        y)
347 {
348         glLabelObject    *object;
349         gdouble           w, h;
350
351         object = gl_view_object_get_object (view_object);
352
353         gl_label_object_get_size (object, &w, &h);
354
355         cairo_rectangle (cr, 0.0, 0.0, w, h);
356
357         if (cairo_in_fill (cr, x, y))
358         {
359                 return TRUE;
360         }
361
362         return FALSE;
363 }
364
365
366 /*****************************************************************************/
367 /* Return apropos cursor for create object mode.                             */
368 /*****************************************************************************/
369 GdkCursor *
370 gl_view_barcode_get_create_cursor (void)
371 {
372         GdkCursor       *cursor = NULL;
373         GdkPixmap       *pixmap_data, *pixmap_mask;
374         GdkColor         fg = { 0, 0, 0, 0 };
375         GdkColor         bg = { 0, 65535, 65535, 65535 };
376
377         gl_debug (DEBUG_VIEW, "START");
378
379         pixmap_data = gdk_bitmap_create_from_data (NULL,
380                                                    (gchar *)cursor_barcode_bits,
381                                                    cursor_barcode_width,
382                                                    cursor_barcode_height);
383         pixmap_mask = gdk_bitmap_create_from_data (NULL,
384                                                    (gchar *)cursor_barcode_mask_bits,
385                                                    cursor_barcode_mask_width,
386                                                    cursor_barcode_mask_height);
387         cursor = gdk_cursor_new_from_pixmap (pixmap_data, pixmap_mask, &fg,
388                                              &bg, cursor_barcode_x_hot,
389                                              cursor_barcode_y_hot);
390
391         gl_debug (DEBUG_VIEW, "END");
392
393         return cursor;
394 }
395
396 /*****************************************************************************/
397 /* Object creation handler: button press event.                              */
398 /*****************************************************************************/
399 void
400 gl_view_barcode_create_button_press_event   (glView *view,
401                                              gdouble x,
402                                              gdouble y)
403 {
404         GObject             *object;
405         glTextNode          *text_node;
406         glColorNode         *line_color_node;
407
408         gl_view_unselect_all (view);
409
410         line_color_node = gl_color_node_new_default ();
411                 
412         object = gl_label_barcode_new (view->label);
413         gl_label_object_set_position (GL_LABEL_OBJECT(object), x, y);
414         text_node = gl_text_node_new_from_text ("123456789");
415         gl_label_barcode_set_data (GL_LABEL_BARCODE(object), text_node);
416         line_color_node->color = gl_color_set_opacity (gl_view_get_default_line_color(view), 0.5);
417         gl_label_object_set_line_color (GL_LABEL_OBJECT(object),
418                                         line_color_node);
419
420         gl_color_node_free (&line_color_node);
421
422         view->create_object = GL_LABEL_OBJECT (object);
423         view->create_x0 = x;
424         view->create_y0 = y;
425 }
426
427 /*****************************************************************************/
428 /* Object creation handler: motion event.                                    */
429 /*****************************************************************************/
430 void
431 gl_view_barcode_create_motion_event         (glView *view,
432                                              gdouble x,
433                                              gdouble y)
434 {
435         gl_label_object_set_position (GL_LABEL_OBJECT(view->create_object), x, y);
436 }
437
438 /*****************************************************************************/
439 /* Object creation handler: button relesase event.                           */
440 /*****************************************************************************/
441 void
442 gl_view_barcode_create_button_release_event (glView *view,
443                                              gdouble x,
444                                              gdouble y)
445 {
446         glColorNode         *line_color_node;
447
448         line_color_node = gl_color_node_new_default ();
449                 
450         gl_label_object_set_position (GL_LABEL_OBJECT(view->create_object), x, y);
451         line_color_node->color = gl_view_get_default_line_color(view);
452         gl_label_object_set_line_color (GL_LABEL_OBJECT(view->create_object), line_color_node);
453         gl_color_node_free (&line_color_node);
454 }
455