]> git.sur5r.net Git - glabels/blob - glabels2/src/view-barcode.c
2009-09-22 Jim Evins <evins@snaught.com>
[glabels] / glabels2 / src / view-barcode.c
1 /*
2  *  view-barcode.c
3  *  Copyright (C) 2001-2009  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of gLabels.
6  *
7  *  gLabels is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  gLabels is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with gLabels.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22
23 #include "view-barcode.h"
24
25 #include <glib/gi18n.h>
26 #include <glib.h>
27
28 #include "color.h"
29 #include "object-editor.h"
30 #include "stock.h"
31
32 #include "pixmaps/cursor_barcode.xbm"
33 #include "pixmaps/cursor_barcode_mask.xbm"
34
35 #include "debug.h"
36
37
38 /*========================================================*/
39 /* Private macros and constants.                          */
40 /*========================================================*/
41
42
43 /*========================================================*/
44 /* Private types.                                         */
45 /*========================================================*/
46
47 struct _glViewBarcodePrivate {
48 };
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 /* Boilerplate object stuff.                                                 */
83 /*****************************************************************************/
84 G_DEFINE_TYPE (glViewBarcode, gl_view_barcode, GL_TYPE_VIEW_OBJECT);
85
86
87 static void
88 gl_view_barcode_class_init (glViewBarcodeClass *class)
89 {
90         GObjectClass      *object_class      = G_OBJECT_CLASS (class);
91         glViewObjectClass *view_object_class = GL_VIEW_OBJECT_CLASS (class);
92
93         gl_debug (DEBUG_VIEW, "START");
94
95         gl_view_barcode_parent_class = g_type_class_peek_parent (class);
96
97         object_class->finalize = gl_view_barcode_finalize;
98
99         view_object_class->construct_editor = construct_properties_editor;
100         view_object_class->object_at        = object_at;
101
102         gl_debug (DEBUG_VIEW, "END");
103 }
104
105
106 static void
107 gl_view_barcode_init (glViewBarcode *view_barcode)
108 {
109         gl_debug (DEBUG_VIEW, "START");
110
111         view_barcode->priv = g_new0 (glViewBarcodePrivate, 1);
112
113         gl_debug (DEBUG_VIEW, "END");
114 }
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 /*****************************************************************************/
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 /*****************************************************************************/
162 /* Create a properties dialog for a barcode object.                          */
163 /*****************************************************************************/
164 static GtkWidget *
165 construct_properties_editor (glViewObject *view_object)
166 {
167         GtkWidget          *editor;
168         glViewBarcode      *view_bc = (glViewBarcode *)view_object;
169         glLabelObject      *object;
170
171         gl_debug (DEBUG_VIEW, "START");
172
173         object = gl_view_object_get_object (GL_VIEW_OBJECT(view_bc));
174
175         /* Build editor. */
176         editor = gl_object_editor_new (GL_STOCK_BARCODE, _("Barcode object properties"),
177                                        object->parent,
178                                        GL_OBJECT_EDITOR_POSITION_PAGE,
179                                        GL_OBJECT_EDITOR_SIZE_PAGE,
180                                        GL_OBJECT_EDITOR_BC_PAGE,
181                                        GL_OBJECT_EDITOR_DATA_PAGE,
182                                        0);
183
184         /* Update */
185         update_editor_from_object_cb (object, GL_OBJECT_EDITOR(editor));
186         update_editor_from_move_cb (object, 0, 0, GL_OBJECT_EDITOR(editor));
187
188         /* Connect signals. */
189         g_signal_connect (G_OBJECT (editor), "changed",
190                           G_CALLBACK(update_object_from_editor_cb), object);
191         g_signal_connect (G_OBJECT (object), "changed",
192                           G_CALLBACK (update_editor_from_object_cb), editor);
193         g_signal_connect (G_OBJECT (object), "moved",
194                           G_CALLBACK (update_editor_from_move_cb), editor);
195
196         gl_debug (DEBUG_VIEW, "END");
197
198         return editor;
199 }
200
201
202 /*---------------------------------------------------------------------------*/
203 /* PRIVATE.  editor "changed" callback.                                      */
204 /*---------------------------------------------------------------------------*/
205 static void
206 update_object_from_editor_cb (glObjectEditor *editor,
207                               glLabelObject  *object)
208 {
209         gdouble            x, y, w, h;
210         glTextNode        *text_node;
211         gchar             *id;
212         gboolean           text_flag, cs_flag;
213         glColorNode       *color_node;
214         guint              format_digits;
215
216         gl_debug (DEBUG_VIEW, "START");
217
218         g_signal_handlers_block_by_func (G_OBJECT(object),
219                                          update_editor_from_object_cb,
220                                          editor);
221         g_signal_handlers_block_by_func (G_OBJECT(object),
222                                          update_editor_from_move_cb,
223                                          editor);
224
225
226         gl_object_editor_get_position (editor, &x, &y);
227         gl_label_object_set_position (object, x, y);
228
229         gl_object_editor_get_size (editor, &w, &h);
230         gl_label_object_set_size (object, w, h);
231
232         text_node = gl_object_editor_get_data (editor);
233         gl_label_barcode_set_data (GL_LABEL_BARCODE(object), text_node);
234         gl_text_node_free (&text_node);
235
236         gl_object_editor_get_bc_style (editor, &id, &text_flag, &cs_flag, &format_digits);
237         color_node = gl_object_editor_get_bc_color (editor);
238         gl_label_barcode_set_props (GL_LABEL_BARCODE(object),
239                                     id, text_flag, cs_flag, format_digits);
240         gl_label_object_set_line_color (object, color_node);
241         gl_color_node_free (&color_node);
242         g_free (id);
243
244         g_signal_handlers_unblock_by_func (G_OBJECT(object),
245                                            update_editor_from_object_cb,
246                                            editor);
247         g_signal_handlers_unblock_by_func (G_OBJECT(object),
248                                            update_editor_from_move_cb,
249                                            editor);
250
251         gl_debug (DEBUG_VIEW, "END");
252 }
253
254
255 /*---------------------------------------------------------------------------*/
256 /* PRIVATE. label object "changed" callback.                                 */
257 /*---------------------------------------------------------------------------*/
258 static void
259 update_editor_from_object_cb (glLabelObject  *object,
260                               glObjectEditor *editor)
261 {
262         gdouble            w, h;
263         glTextNode        *text_node;
264         gchar             *id;
265         gboolean           text_flag, cs_flag;
266         glColorNode       *color_node;
267         glMerge           *merge;
268         guint              format_digits;
269
270         gl_debug (DEBUG_VIEW, "START");
271
272         gl_label_object_get_size (object, &w, &h);
273         gl_object_editor_set_size (editor, w, h);
274         
275         merge = gl_label_get_merge (GL_LABEL(object->parent));
276
277         gl_label_barcode_get_props (GL_LABEL_BARCODE(object),
278                                     &id, &text_flag, &cs_flag, &format_digits);
279         color_node = gl_label_object_get_line_color (object);
280         gl_object_editor_set_bc_style (editor, id, text_flag, cs_flag, format_digits);
281         gl_object_editor_set_bc_color (editor, (merge != NULL), color_node);
282         gl_color_node_free (&color_node);
283         g_free (id);
284
285         text_node = gl_label_barcode_get_data (GL_LABEL_BARCODE(object));
286         gl_object_editor_set_data (editor, (merge != NULL), text_node);
287         gl_text_node_free (&text_node);
288
289
290         gl_debug (DEBUG_VIEW, "END");
291 }
292
293
294 /*---------------------------------------------------------------------------*/
295 /* PRIVATE. label object "moved" callback.                                   */
296 /*---------------------------------------------------------------------------*/
297 static void
298 update_editor_from_move_cb (glLabelObject    *object,
299                             gdouble           dx,
300                             gdouble           dy,
301                             glObjectEditor   *editor)
302 {
303         gdouble            x, y;
304
305         gl_debug (DEBUG_VIEW, "START");
306
307         gl_label_object_get_position (object, &x, &y);
308         gl_object_editor_set_position (editor, x, y);
309
310         gl_debug (DEBUG_VIEW, "END");
311 }
312
313
314 /*****************************************************************************/
315 /* Is object at (x,y)?                                                       */
316 /*****************************************************************************/
317 static gboolean
318 object_at (glViewObject  *view_object,
319            cairo_t       *cr,
320            gdouble        x,
321            gdouble        y)
322 {
323         glLabelObject    *object;
324         gdouble           w, h;
325
326         object = gl_view_object_get_object (view_object);
327
328         gl_label_object_get_size (object, &w, &h);
329
330         cairo_rectangle (cr, 0.0, 0.0, w, h);
331
332         if (cairo_in_fill (cr, x, y))
333         {
334                 return TRUE;
335         }
336
337         return FALSE;
338 }
339
340
341 /*****************************************************************************/
342 /* Return apropos cursor for create object mode.                             */
343 /*****************************************************************************/
344 GdkCursor *
345 gl_view_barcode_get_create_cursor (void)
346 {
347         GdkCursor       *cursor = NULL;
348         GdkPixmap       *pixmap_data, *pixmap_mask;
349         GdkColor         fg = { 0, 0, 0, 0 };
350         GdkColor         bg = { 0, 65535, 65535, 65535 };
351
352         gl_debug (DEBUG_VIEW, "START");
353
354         pixmap_data = gdk_bitmap_create_from_data (NULL,
355                                                    (gchar *)cursor_barcode_bits,
356                                                    cursor_barcode_width,
357                                                    cursor_barcode_height);
358         pixmap_mask = gdk_bitmap_create_from_data (NULL,
359                                                    (gchar *)cursor_barcode_mask_bits,
360                                                    cursor_barcode_mask_width,
361                                                    cursor_barcode_mask_height);
362         cursor = gdk_cursor_new_from_pixmap (pixmap_data, pixmap_mask, &fg,
363                                              &bg, cursor_barcode_x_hot,
364                                              cursor_barcode_y_hot);
365
366         gl_debug (DEBUG_VIEW, "END");
367
368         return cursor;
369 }
370
371
372 /*****************************************************************************/
373 /* Object creation handler: button press event.                              */
374 /*****************************************************************************/
375 void
376 gl_view_barcode_create_button_press_event   (glView *view,
377                                              gdouble x,
378                                              gdouble y)
379 {
380         GObject             *object;
381         glTextNode          *text_node;
382         glColorNode         *line_color_node;
383
384         gl_view_unselect_all (view);
385
386         line_color_node = gl_color_node_new_default ();
387                 
388         object = gl_label_barcode_new (view->label);
389         gl_label_object_set_position (GL_LABEL_OBJECT(object), x, y);
390         text_node = gl_text_node_new_from_text ("123456789");
391         gl_label_barcode_set_data (GL_LABEL_BARCODE(object), text_node);
392         line_color_node->color = gl_color_set_opacity (gl_view_get_default_line_color(view), 0.5);
393         gl_label_object_set_line_color (GL_LABEL_OBJECT(object),
394                                         line_color_node);
395
396         gl_color_node_free (&line_color_node);
397
398         view->create_object = GL_LABEL_OBJECT (object);
399         view->create_x0 = x;
400         view->create_y0 = y;
401 }
402
403
404 /*****************************************************************************/
405 /* Object creation handler: motion event.                                    */
406 /*****************************************************************************/
407 void
408 gl_view_barcode_create_motion_event         (glView *view,
409                                              gdouble x,
410                                              gdouble y)
411 {
412         gl_label_object_set_position (GL_LABEL_OBJECT(view->create_object), x, y);
413 }
414
415
416 /*****************************************************************************/
417 /* Object creation handler: button relesase event.                           */
418 /*****************************************************************************/
419 void
420 gl_view_barcode_create_button_release_event (glView *view,
421                                              gdouble x,
422                                              gdouble y)
423 {
424         glColorNode         *line_color_node;
425
426         line_color_node = gl_color_node_new_default ();
427                 
428         gl_label_object_set_position (GL_LABEL_OBJECT(view->create_object), x, y);
429         line_color_node->color = gl_view_get_default_line_color(view);
430         gl_label_object_set_line_color (GL_LABEL_OBJECT(view->create_object), line_color_node);
431         gl_color_node_free (&line_color_node);
432 }
433
434
435
436 /*
437  * Local Variables:       -- emacs
438  * mode: C                -- emacs
439  * c-basic-offset: 8      -- emacs
440  * tab-width: 8           -- emacs
441  * indent-tabs-mode: nil  -- emacs
442  * End:                   -- emacs
443  */