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