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