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