]> git.sur5r.net Git - glabels/blob - glabels2/src/view-barcode.c
Initial revision
[glabels] / glabels2 / src / view-barcode.c
1 /*
2  *  (GLABELS) Label and Business Card Creation program for GNOME
3  *
4  *  view_barcode.c:  GLabels label barcode object widget
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
25 #include <libgnomeprint/gnome-glyphlist.h>
26
27 #include "view-barcode.h"
28 #include "canvas-hacktext.h"
29
30 #include "view-highlight.h"
31
32 #include "glabels.h"
33 #include "wdgt-bc-data.h"
34 #include "wdgt-bc-props.h"
35 #include "wdgt-bc-style.h"
36 #include "wdgt-position.h"
37
38 #include "pixmaps/cursor_barcode.xbm"
39 #include "pixmaps/cursor_barcode_mask.xbm"
40
41 #include "debug.h"
42
43 /*========================================================*/
44 /* Private macros and constants.                          */
45 /*========================================================*/
46
47 /*========================================================*/
48 /* Private types.                                         */
49 /*========================================================*/
50
51 struct _glViewBarcodePrivate {
52
53         GList     *item_list;
54
55         /* Page 0 widgets */
56         GtkWidget *bc_data;
57
58         /* Page 1 widgets */
59         GtkWidget *bc_props;
60         GtkWidget *bc_style;
61
62         /* Page 2 widgets */
63         GtkWidget *position;
64 };
65
66 /*========================================================*/
67 /* Private globals.                                       */
68 /*========================================================*/
69
70 static glViewObjectClass *parent_class = NULL;
71
72
73 /*========================================================*/
74 /* Private function prototypes.                           */
75 /*========================================================*/
76
77 static void      gl_view_barcode_class_init       (glViewBarcodeClass *klass);
78 static void      gl_view_barcode_instance_init    (glViewBarcode *view_barcode);
79 static void      gl_view_barcode_finalize         (GObject *object);
80
81 static void      update_view_barcode_cb           (glLabelObject *object,
82                                                 glViewBarcode *view_barcode);
83
84 static GtkWidget *construct_properties_dialog  (glViewBarcode *view_barcode);
85
86 static void      response_cb                   (GtkDialog *dialog,
87                                                 gint response,
88                                                 glViewBarcode *view_barcode);
89
90 static void      bc_data_changed_cb            (glWdgtBCData *bc_data,
91                                                 glViewBarcode *view_barcode);
92
93 static void      bc_props_changed_cb           (glWdgtBCProps *bc_props,
94                                                 glViewBarcode *view_barcode);
95
96 static void      bc_style_changed_cb           (glWdgtBCStyle *bc_style,
97                                                 glViewBarcode *view_barcode);
98
99 static void      position_changed_cb           (glWdgtPosition *position,
100                                                 glViewBarcode *view_barcode);
101
102 static void      update_dialog_cb              (glLabelObject *object,
103                                                 glViewBarcode *view_barcode);
104
105 static void      draw_barcode                  (glViewBarcode *view_barcode);
106
107 \f
108 /*****************************************************************************/
109 /* Boilerplate object stuff.                                                 */
110 /*****************************************************************************/
111 GType
112 gl_view_barcode_get_type (void)
113 {
114         static GType type = 0;
115
116         if (!type) {
117                 GTypeInfo info = {
118                         sizeof (glViewBarcodeClass),
119                         NULL,
120                         NULL,
121                         (GClassInitFunc) gl_view_barcode_class_init,
122                         NULL,
123                         NULL,
124                         sizeof (glViewBarcode),
125                         0,
126                         (GInstanceInitFunc) gl_view_barcode_instance_init,
127                 };
128
129                 type = g_type_register_static (GL_TYPE_VIEW_OBJECT,
130                                                "glViewBarcode", &info, 0);
131         }
132
133         return type;
134 }
135
136 static void
137 gl_view_barcode_class_init (glViewBarcodeClass *klass)
138 {
139         GObjectClass *object_class = (GObjectClass *) klass;
140
141         gl_debug (DEBUG_VIEW, "START");
142
143         parent_class = g_type_class_peek_parent (klass);
144
145         object_class->finalize = gl_view_barcode_finalize;
146
147         gl_debug (DEBUG_VIEW, "END");
148 }
149
150 static void
151 gl_view_barcode_instance_init (glViewBarcode *view_barcode)
152 {
153         gl_debug (DEBUG_VIEW, "START");
154
155         view_barcode->private = g_new0 (glViewBarcodePrivate, 1);
156
157         gl_debug (DEBUG_VIEW, "END");
158 }
159
160 static void
161 gl_view_barcode_finalize (GObject *object)
162 {
163         glLabel       *parent;
164
165         gl_debug (DEBUG_VIEW, "START");
166
167         g_return_if_fail (object && GL_IS_VIEW_BARCODE (object));
168
169         G_OBJECT_CLASS (parent_class)->finalize (object);
170
171         gl_debug (DEBUG_VIEW, "END");
172 }
173
174 /*****************************************************************************/
175 /* NEW barcode object view.                                                  */
176 /*****************************************************************************/
177 glViewObject *
178 gl_view_barcode_new (glLabelBarcode *object,
179                      glView         *view)
180 {
181         glViewBarcode      *view_barcode;
182         GtkMenu            *menu;
183         GtkWidget          *dialog;
184
185         gl_debug (DEBUG_VIEW, "START");
186         g_return_if_fail (object && GL_IS_LABEL_BARCODE (object));
187         g_return_if_fail (view && GL_IS_VIEW (view));
188         
189         view_barcode = g_object_new (gl_view_barcode_get_type(), NULL);
190
191         gl_view_object_set_view (GL_VIEW_OBJECT(view_barcode), view);
192         gl_view_object_set_object (GL_VIEW_OBJECT(view_barcode),
193                                    GL_LABEL_OBJECT(object),
194                                    GL_VIEW_HIGHLIGHT_SIMPLE);
195
196         /* Create analogous canvas items. */
197         draw_barcode (view_barcode);
198
199         g_signal_connect (G_OBJECT (object), "changed",
200                           G_CALLBACK (update_view_barcode_cb), view_barcode);
201
202         /* Create a dialog for controlling/viewing object properties. */
203         dialog = construct_properties_dialog (view_barcode);
204         gl_view_object_set_dialog     (GL_VIEW_OBJECT(view_barcode), dialog);
205
206         gl_debug (DEBUG_VIEW, "END");
207
208         return GL_VIEW_OBJECT (view_barcode);
209 }
210
211 /*---------------------------------------------------------------------------*/
212 /* PRIVATE. label object "changed" callback.                                 */
213 /*---------------------------------------------------------------------------*/
214 static void
215 update_view_barcode_cb (glLabelObject *object,
216                         glViewBarcode *view_barcode)
217 {
218         glView             *view;
219         GnomeCanvasItem    *group;
220
221         gl_debug (DEBUG_VIEW, "START");
222
223         view = gl_view_object_get_view (GL_VIEW_OBJECT(view_barcode));
224
225         /* Adjust appearance of analogous canvas items. */
226         draw_barcode (view_barcode);
227
228         /* Adjust highlight */
229         gl_view_object_update_highlight (GL_VIEW_OBJECT(view_barcode));
230
231         gl_debug (DEBUG_VIEW, "END");
232 }
233
234 /*****************************************************************************/
235 /* Create a properties dialog for a barcode object.                          */
236 /*****************************************************************************/
237 static GtkWidget *
238 construct_properties_dialog (glViewBarcode *view_barcode)
239 {
240         GtkWidget          *dialog, *notebook, *wvbox, *wbutton;
241         BonoboWindow       *win = glabels_get_active_window ();
242         glLabelObject      *object;
243         gdouble            x, y, w, h, label_width, label_height;
244         glTextNode         *text_node;
245         glBarcodeStyle     style;
246         gboolean           text_flag;
247         guint              color;
248         gdouble            scale;
249         glMerge            *merge;
250
251         gl_debug (DEBUG_VIEW, "START");
252
253         /* retrieve object and query parameters */
254         object = gl_view_object_get_object (GL_VIEW_OBJECT(view_barcode));
255         gl_label_object_get_position (GL_LABEL_OBJECT(object), &x, &y);
256         text_node = gl_label_barcode_get_data(GL_LABEL_BARCODE(object));
257         gl_label_barcode_get_props (GL_LABEL_BARCODE(object),
258                                     &style, &text_flag, &color, &scale);
259         gl_label_get_size (GL_LABEL(object->parent),
260                            &label_width, &label_height);
261         merge = gl_label_get_merge (GL_LABEL(object->parent));
262
263         /*-----------------------------------------------------------------*/
264         /* Build dialog with notebook.                                     */
265         /*-----------------------------------------------------------------*/
266         gl_debug (DEBUG_VIEW, "Creating dialog...");
267         dialog = gtk_dialog_new_with_buttons ( _("Edit barcode object properties"),
268                                                GTK_WINDOW (win),
269                                                GTK_DIALOG_DESTROY_WITH_PARENT,
270                                                GTK_STOCK_CLOSE,
271                                                            GTK_RESPONSE_CLOSE,
272                                                NULL );
273         g_signal_connect (G_OBJECT (dialog), "response",
274                           G_CALLBACK (response_cb), view_barcode);
275
276         notebook = gtk_notebook_new ();
277         gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox),
278                             notebook, TRUE, TRUE, 0);
279
280         /*---------------------------*/
281         /* Data Notebook Tab         */
282         /*---------------------------*/
283         gl_debug (DEBUG_VIEW, "Creating data tab...");
284         wvbox = gtk_vbox_new (FALSE, GNOME_PAD);
285         gtk_container_set_border_width (GTK_CONTAINER (wvbox), 10);
286         gtk_notebook_append_page (GTK_NOTEBOOK (notebook), wvbox,
287                                   gtk_label_new (_("Data")));
288
289         /* barcode data */
290         gl_debug (DEBUG_VIEW, "Creating data entry...");
291         view_barcode->private->bc_data =
292                 gl_wdgt_bc_data_new (_("Barcode data"), merge->field_defs);
293         gl_debug (DEBUG_VIEW, "1");
294         gl_wdgt_bc_data_set_data (GL_WDGT_BC_DATA(view_barcode->private->bc_data),
295                                   (merge->type != GL_MERGE_NONE),
296                                   text_node);
297         gl_debug (DEBUG_VIEW, "2");
298         gtk_box_pack_start (GTK_BOX (wvbox), view_barcode->private->bc_data,
299                             FALSE, FALSE, 0);
300         gl_debug (DEBUG_VIEW, "3");
301         g_signal_connect ( G_OBJECT(view_barcode->private->bc_data),
302                            "changed", G_CALLBACK (bc_data_changed_cb),
303                            view_barcode);
304
305
306         /*---------------------------*/
307         /* Appearance Notebook Tab   */
308         /*---------------------------*/
309         gl_debug (DEBUG_VIEW, "Creating props tab...");
310         wvbox = gtk_vbox_new (FALSE, GNOME_PAD);
311         gtk_container_set_border_width (GTK_CONTAINER (wvbox), 10);
312         gtk_notebook_append_page (GTK_NOTEBOOK (notebook), wvbox,
313                                   gtk_label_new (_("Appearance")));
314
315         /* barcode props entry */
316         gl_debug (DEBUG_VIEW, "Creating props entry...");
317         view_barcode->private->bc_props =
318                 gl_wdgt_bc_props_new (_("Barcode Properties"));
319         gl_wdgt_bc_props_set_params (GL_WDGT_BC_PROPS(view_barcode->private->bc_props),
320                                      scale, color);
321         gtk_box_pack_start (GTK_BOX (wvbox), view_barcode->private->bc_props,
322                             FALSE, FALSE, 0);
323         g_signal_connect ( G_OBJECT(view_barcode->private->bc_props),
324                            "changed", G_CALLBACK (bc_props_changed_cb),
325                            view_barcode);
326
327         /* ------ Barcode Style Frame ------ */
328         view_barcode->private->bc_style = gl_wdgt_bc_style_new (_("Style"));
329         gl_wdgt_bc_style_set_params (GL_WDGT_BC_STYLE (view_barcode->private->bc_style),
330                                      style, text_flag);
331         gtk_box_pack_start (GTK_BOX (wvbox), view_barcode->private->bc_style,
332                             FALSE, FALSE, 0);
333         g_signal_connect (G_OBJECT (view_barcode->private->bc_style),
334                           "changed", G_CALLBACK (bc_style_changed_cb),
335                           view_barcode);
336
337
338         /*----------------------------*/
339         /* Position/Size Notebook Tab */
340         /*----------------------------*/
341         gl_debug (DEBUG_VIEW, "Creating position tab...");
342         wvbox = gtk_vbox_new (FALSE, GNOME_PAD);
343         gtk_container_set_border_width (GTK_CONTAINER (wvbox), 10);
344         gtk_notebook_append_page (GTK_NOTEBOOK (notebook), wvbox,
345                                   gtk_label_new (_("Position")));
346
347         /* ------ Position Frame ------ */
348         gl_debug (DEBUG_VIEW, "Creating position entry...");
349         view_barcode->private->position = gl_wdgt_position_new (_("Position"));
350         gl_wdgt_position_set_params (GL_WDGT_POSITION (view_barcode->private->position),
351                                      x, y,
352                                      label_width, label_height);
353         gtk_box_pack_start (GTK_BOX (wvbox),
354                                 view_barcode->private->position,
355                             FALSE, FALSE, 0);
356         g_signal_connect (G_OBJECT (view_barcode->private->position),
357                           "changed",
358                           G_CALLBACK(position_changed_cb), view_barcode);
359
360
361         /*----------------------------*/
362         /* Track object changes.      */
363         /*----------------------------*/
364         g_signal_connect (G_OBJECT (object), "changed",
365                           G_CALLBACK (update_dialog_cb), view_barcode);
366
367         gl_debug (DEBUG_VIEW, "END");
368
369         return dialog;
370 }
371
372 /*---------------------------------------------------------------------------*/
373 /* PRIVATE.  "Response" callback.                                            */
374 /*---------------------------------------------------------------------------*/
375 static void
376 response_cb (GtkDialog     *dialog,
377              gint          response,
378              glViewBarcode   *view_barcode)
379 {
380         gl_debug (DEBUG_VIEW, "START");
381
382         g_return_if_fail(dialog != NULL);
383         g_return_if_fail(GTK_IS_DIALOG(dialog));
384
385         switch(response) {
386         case GTK_RESPONSE_CLOSE:
387                 gtk_widget_hide (GTK_WIDGET(dialog));
388                 break;
389         default:
390                 g_assert_not_reached();
391         }
392
393         gl_debug (DEBUG_VIEW, "END");
394 }
395
396 /*---------------------------------------------------------------------------*/
397 /* PRIVATE.  barcode data "changed" callback.                                */
398 /*---------------------------------------------------------------------------*/
399 static void
400 bc_data_changed_cb (glWdgtBCData        *bc_data,
401                     glViewBarcode       *view_barcode)
402 {
403         glLabelObject    *object;
404         glTextNode       *text_node;
405
406         gl_debug (DEBUG_VIEW, "START");
407
408         object = gl_view_object_get_object (GL_VIEW_OBJECT(view_barcode));
409
410         text_node = gl_wdgt_bc_data_get_data (bc_data);
411
412         g_signal_handlers_block_by_func (G_OBJECT(object),
413                                          update_dialog_cb, view_barcode);
414         gl_label_barcode_set_data (GL_LABEL_BARCODE(object), text_node);
415         g_signal_handlers_unblock_by_func (G_OBJECT(object),
416                                            update_dialog_cb, view_barcode);
417
418         gl_text_node_free (&text_node);
419
420         gl_debug (DEBUG_VIEW, "END");
421 }
422
423 /*---------------------------------------------------------------------------*/
424 /* PRIVATE.  barcode props "changed" callback.                               */
425 /*---------------------------------------------------------------------------*/
426 static void
427 bc_props_changed_cb (glWdgtBCProps  *text_props,
428                      glViewBarcode  *view_barcode)
429 {
430         glLabelObject      *object;
431         glBarcodeStyle     style;
432         gboolean           text_flag;
433         guint              color;
434         gdouble            scale;
435
436
437         gl_debug (DEBUG_VIEW, "START");
438
439         object = gl_view_object_get_object (GL_VIEW_OBJECT(view_barcode));
440
441         gl_label_barcode_get_props (GL_LABEL_BARCODE(object),
442                                     &style, &text_flag, &color, &scale);
443         gl_wdgt_bc_props_get_params (text_props, &scale, &color);
444
445         g_signal_handlers_block_by_func (G_OBJECT(object),
446                                          update_dialog_cb, view_barcode);
447         gl_label_barcode_set_props (GL_LABEL_BARCODE(object),
448                                     style, text_flag, color, scale);
449         g_signal_handlers_unblock_by_func (G_OBJECT(object),
450                                            update_dialog_cb, view_barcode);
451
452         gl_debug (DEBUG_VIEW, "END");
453 }
454
455 /*---------------------------------------------------------------------------*/
456 /* PRIVATE.  barcode style "changed" callback.                               */
457 /*---------------------------------------------------------------------------*/
458 static void
459 bc_style_changed_cb (glWdgtBCStyle  *bc_style,
460                      glViewBarcode  *view_barcode)
461 {
462         glLabelObject      *object;
463         glBarcodeStyle     style;
464         gboolean           text_flag;
465         guint              color;
466         gdouble            scale;
467
468
469         gl_debug (DEBUG_VIEW, "START");
470
471         object = gl_view_object_get_object (GL_VIEW_OBJECT(view_barcode));
472
473         gl_label_barcode_get_props (GL_LABEL_BARCODE(object),
474                                     &style, &text_flag, &color, &scale);
475         gl_wdgt_bc_style_get_params (bc_style, &style, &text_flag);
476
477         g_signal_handlers_block_by_func (G_OBJECT(object),
478                                          update_dialog_cb, view_barcode);
479         gl_label_barcode_set_props (GL_LABEL_BARCODE(object),
480                                     style, text_flag, color, scale);
481         g_signal_handlers_unblock_by_func (G_OBJECT(object),
482                                            update_dialog_cb, view_barcode);
483
484         gl_debug (DEBUG_VIEW, "END");
485 }
486
487 /*---------------------------------------------------------------------------*/
488 /* PRIVATE.  position "changed" callback.                                    */
489 /*---------------------------------------------------------------------------*/
490 static void
491 position_changed_cb (glWdgtPosition     *position,
492                      glViewBarcode         *view_barcode)
493 {
494         glLabelObject      *object;
495         gdouble            x, y;
496
497         gl_debug (DEBUG_VIEW, "START");
498
499         gl_wdgt_position_get_position (GL_WDGT_POSITION (position), &x, &y);
500
501         object = gl_view_object_get_object (GL_VIEW_OBJECT(view_barcode));
502
503         g_signal_handlers_block_by_func (G_OBJECT(object),
504                                          update_dialog_cb, view_barcode);
505         gl_label_object_set_position (GL_LABEL_OBJECT(object), x, y);
506         g_signal_handlers_unblock_by_func (G_OBJECT(object),
507                                            update_dialog_cb, view_barcode);
508
509         gl_debug (DEBUG_VIEW, "END");
510 }
511
512 /*---------------------------------------------------------------------------*/
513 /* PRIVATE. label object "changed" callback.                                 */
514 /*---------------------------------------------------------------------------*/
515 static void
516 update_dialog_cb (glLabelObject  *object,
517                   glViewBarcode     *view_barcode)
518 {
519         gdouble            x, y;
520         glTextNode         *text_node;
521         glBarcodeStyle     style;
522         gboolean           text_flag;
523         guint              color;
524         gdouble            scale;
525         glMerge            *merge;
526
527         gl_debug (DEBUG_VIEW, "START");
528
529         /* Query properties of object. */
530         text_node = gl_label_barcode_get_data(GL_LABEL_BARCODE(object));
531         gl_label_barcode_get_props (GL_LABEL_BARCODE(object),
532                                     &style, &text_flag, &color, &scale);
533         gl_label_object_get_position (GL_LABEL_OBJECT(object), &x, &y);
534         merge = gl_label_get_merge (GL_LABEL(object->parent));
535
536         /* Block widget handlers to prevent recursion */
537         g_signal_handlers_block_by_func (G_OBJECT(view_barcode->private->bc_data),
538                                          bc_data_changed_cb, view_barcode);
539         g_signal_handlers_block_by_func (G_OBJECT(view_barcode->private->bc_props),
540                                          bc_props_changed_cb, view_barcode);
541         g_signal_handlers_block_by_func (G_OBJECT(view_barcode->private->bc_style),
542                                          bc_style_changed_cb, view_barcode);
543         g_signal_handlers_block_by_func (G_OBJECT(view_barcode->private->position),
544                                          position_changed_cb, view_barcode);
545
546         /* Update widgets in property dialog */
547
548         gl_wdgt_bc_data_set_data (GL_WDGT_BC_DATA(view_barcode->private->bc_data),
549                                   (merge->type != GL_MERGE_NONE),
550                                   text_node);
551         gl_wdgt_bc_props_set_params (GL_WDGT_BC_PROPS(view_barcode->private->bc_props),
552                                      scale, color);
553         gl_wdgt_bc_style_set_params (GL_WDGT_BC_STYLE(view_barcode->private->bc_style),
554                                      style, text_flag);
555         gl_wdgt_position_set_position (GL_WDGT_POSITION(view_barcode->private->position),
556                                        x, y);
557
558         /* Unblock widget handlers */
559         g_signal_handlers_unblock_by_func (G_OBJECT(view_barcode->private->bc_data),
560                                            bc_data_changed_cb, view_barcode);
561         g_signal_handlers_unblock_by_func (G_OBJECT(view_barcode->private->bc_props),
562                                            bc_props_changed_cb, view_barcode);
563         g_signal_handlers_unblock_by_func (G_OBJECT(view_barcode->private->bc_style),
564                                            bc_style_changed_cb, view_barcode);
565         g_signal_handlers_unblock_by_func (G_OBJECT(view_barcode->private->position),
566                                            position_changed_cb, view_barcode);
567
568         gl_text_node_free (&text_node);
569
570         gl_debug (DEBUG_VIEW, "END");
571 }
572
573 /*****************************************************************************/
574 /* Return apropos cursor for create object mode.                             */
575 /*****************************************************************************/
576 GdkCursor *
577 gl_view_barcode_get_create_cursor (void)
578 {
579         static GdkCursor *cursor = NULL;
580         GdkPixmap        *pixmap_data, *pixmap_mask;
581         GdkColor         fg = { 0, 0, 0, 0 };
582         GdkColor         bg = { 0, 65535, 65535, 65535 };
583
584         gl_debug (DEBUG_VIEW, "START");
585
586         if (!cursor) {
587                 pixmap_data = gdk_bitmap_create_from_data (NULL,
588                                                            cursor_barcode_bits,
589                                                            cursor_barcode_width,
590                                                            cursor_barcode_height);
591                 pixmap_mask = gdk_bitmap_create_from_data (NULL,
592                                                            cursor_barcode_mask_bits,
593                                                            cursor_barcode_mask_width,
594                                                            cursor_barcode_mask_height);
595                 cursor =
596                     gdk_cursor_new_from_pixmap (pixmap_data, pixmap_mask, &fg,
597                                                 &bg, cursor_barcode_x_hot,
598                                                 cursor_barcode_y_hot);
599         }
600
601         gl_debug (DEBUG_VIEW, "END");
602
603         return cursor;
604 }
605
606 /*****************************************************************************/
607 /* Canvas event handler for creating barcode objects.                            */
608 /*****************************************************************************/
609 int
610 gl_view_barcode_create_event_handler (GnomeCanvas *canvas,
611                                       GdkEvent    *event,
612                                       glView      *view)
613 {
614         static gdouble      x0, y0;
615         static gboolean     dragging = FALSE;
616         static glViewObject *view_barcode;
617         static GObject      *object;
618         gdouble             x, y;
619         glTextNode          *text_node;
620
621         gl_debug (DEBUG_VIEW, "");
622
623         switch (event->type) {
624
625         case GDK_BUTTON_PRESS:
626                 gl_debug (DEBUG_VIEW, "BUTTON_PRESS");
627                 switch (event->button.button) {
628                 case 1:
629                         dragging = TRUE;
630                         gdk_pointer_grab (GTK_WIDGET (view->canvas)->window,
631                                           FALSE,
632                                           GDK_POINTER_MOTION_MASK |
633                                           GDK_BUTTON_RELEASE_MASK |
634                                           GDK_BUTTON_PRESS_MASK,
635                                           NULL, NULL, event->button.time);
636                         gnome_canvas_window_to_world (canvas,
637                                                       event->button.x,
638                                                       event->button.y, &x, &y);
639                         object = gl_label_barcode_new (view->label);
640                         gl_label_object_set_position (GL_LABEL_OBJECT(object),
641                                                      x, y);
642                         text_node = gl_text_node_new_from_text ("123456789");
643                         gl_label_barcode_set_data (GL_LABEL_BARCODE(object),
644                                                    text_node);
645                         gl_label_barcode_set_props (GL_LABEL_BARCODE(object),
646                                                     GL_BARCODE_STYLE_POSTNET,
647                                                     FALSE,
648                                                     GNOME_CANVAS_COLOR_A (0,0,0,255),
649                                                     1.0);
650                         view_barcode = gl_view_barcode_new (GL_LABEL_BARCODE(object),
651                                                             view);
652                         x0 = x;
653                         y0 = y;
654                         return TRUE;
655
656                 default:
657                         return FALSE;
658                 }
659
660         case GDK_BUTTON_RELEASE:
661                 gl_debug (DEBUG_VIEW, "BUTTON_RELEASE");
662                 switch (event->button.button) {
663                 case 1:
664                         dragging = FALSE;
665                         gdk_pointer_ungrab (event->button.time);
666                         gnome_canvas_window_to_world (canvas,
667                                                       event->button.x,
668                                                       event->button.y, &x, &y);
669                         gl_label_object_set_position (GL_LABEL_OBJECT(object),
670                                                       x, y);
671                         gl_view_unselect_all (view);
672                         gl_view_object_select (GL_VIEW_OBJECT(view_barcode));
673                         gl_view_arrow_mode (view);
674                         return TRUE;
675
676                 default:
677                         return FALSE;
678                 }
679
680         case GDK_MOTION_NOTIFY:
681                 gl_debug (DEBUG_VIEW, "MOTION_NOTIFY");
682                 if (dragging && (event->motion.state & GDK_BUTTON1_MASK)) {
683                         gnome_canvas_window_to_world (canvas,
684                                                       event->button.x,
685                                                       event->button.y, &x, &y);
686                         gl_label_object_set_position (GL_LABEL_OBJECT(object),
687                                                       x, y);
688                         return TRUE;
689                 } else {
690                         return FALSE;
691                 }
692
693         default:
694                 return FALSE;
695         }
696
697 }
698
699 /*--------------------------------------------------------------------------*/
700 /* PRIVATE.  Draw barcode to item (group).                                  */
701 /*--------------------------------------------------------------------------*/
702 static void
703 draw_barcode (glViewBarcode *view_barcode)
704 {
705         glLabelObject    *object;
706         GnomeCanvasItem  *group, *item;
707         glTextNode *text_node;
708         glBarcodeStyle style;
709         gboolean text_flag;
710         guint color;
711         gdouble scale;
712         glBarcodeLine *line;
713         glBarcodeChar *bchar;
714         glBarcode *gbc;
715         GList *li;
716         GList *item_list = NULL;
717         GnomeCanvasPoints *points;
718         gchar *digits, *cstring;
719         GnomeFont *font;
720         GnomeGlyphList *glyphlist;
721         gdouble y_offset;
722
723         gl_debug (DEBUG_VIEW, "START");
724
725         /* Query label object and properties */
726         object = gl_view_object_get_object (GL_VIEW_OBJECT(view_barcode));
727         gl_label_barcode_get_props (GL_LABEL_BARCODE(object),
728                                     &style, &text_flag, &color, &scale);
729         text_node = gl_label_barcode_get_data(GL_LABEL_BARCODE(object));
730         if (text_node->field_flag) {
731                 digits = gl_barcode_default_digits (style);
732         } else {
733                 digits = gl_text_node_expand (text_node, NULL);
734         }
735
736         /* get parent item/group to render to. */
737         group = gl_view_object_get_group (GL_VIEW_OBJECT(view_barcode));
738
739         /* remove previous items from group. */
740         for (li = view_barcode->private->item_list; li!=NULL; li = li->next) {
741                 gl_debug (DEBUG_VIEW, "in loop");
742                 gtk_object_destroy (GTK_OBJECT (li->data));
743         }
744         gl_debug (DEBUG_VIEW, "1");
745         g_list_free (view_barcode->private->item_list);
746         view_barcode->private->item_list = NULL;
747         gl_debug (DEBUG_VIEW, "2");
748
749         /* get Gnome Font */
750         font = gnome_font_find_closest_from_weight_slant (GL_BARCODE_FONT_FAMILY,
751                                                           GL_BARCODE_FONT_WEIGHT,
752                                                           FALSE,
753                                                           10.0);
754
755         gbc = gl_barcode_new (style, text_flag, scale, digits);
756         if (gbc == NULL) {
757
758                 cstring = _("Invalid barcode");
759                 glyphlist = gnome_glyphlist_from_text_sized_dumb (font,
760                                                                   color,
761                                                                   0.0, 0.0,
762                                                                   cstring,
763                                                                   strlen
764                                                                   (cstring));
765                 y_offset = 10.0 - gnome_font_get_descender (font);
766                 item = gnome_canvas_item_new (GNOME_CANVAS_GROUP (group),
767                                               gl_canvas_hacktext_get_type (),
768                                               "x", 0.0,
769                                               "y", y_offset,
770                                               "glyphlist", glyphlist, NULL);
771
772                 view_barcode->private->item_list =
773                         g_list_prepend (view_barcode->private->item_list, item);
774         } else {
775
776                 points = gnome_canvas_points_new (2);
777                 for (li = gbc->lines; li != NULL; li = li->next) {
778                         line = (glBarcodeLine *) li->data;
779
780                         points->coords[0] = line->x;
781                         points->coords[1] = line->y;
782                         points->coords[2] = line->x;
783                         points->coords[3] = line->y + line->length;
784
785                         item =
786                             gnome_canvas_item_new (GNOME_CANVAS_GROUP (group),
787                                                    gnome_canvas_line_get_type
788                                                    (), "points", points,
789                                                    "width_units", line->width,
790                                                    "fill_color_rgba", color,
791                                                    NULL);
792                         view_barcode->private->item_list =
793                                 g_list_prepend (view_barcode->private->item_list, item);
794                 }
795                 gnome_canvas_points_free (points);
796
797                 for (li = gbc->chars; li != NULL; li = li->next) {
798                         bchar = (glBarcodeChar *) li->data;
799
800                         font = gnome_font_find_closest_from_weight_slant (
801                                                        GL_BARCODE_FONT_FAMILY,
802                                                        GL_BARCODE_FONT_WEIGHT,
803                                                        FALSE, bchar->fsize);
804                         glyphlist = gnome_glyphlist_from_text_sized_dumb (font,
805                                                                           color,
806                                                                           0.0,
807                                                                           0.0,
808                                                                           &
809                                                                           (bchar->
810                                                                            c),
811                                                                           1);
812                         y_offset =
813                             bchar->fsize - gnome_font_get_descender (font);
814                         item =
815                             gnome_canvas_item_new (GNOME_CANVAS_GROUP (group),
816                                                    gnome_canvas_hacktext_get_type
817                                                    (), "x", bchar->x, "y",
818                                                    bchar->y + y_offset,
819                                                    "glyphlist", glyphlist,
820                                                    NULL);
821
822                         view_barcode->private->item_list =
823                                 g_list_prepend (view_barcode->private->item_list, item);
824
825                 }
826
827         }
828
829         /* clean up */
830         gl_barcode_free (&gbc);
831         g_free (digits);
832
833         gl_debug (DEBUG_VIEW, "END");
834 }
835