]> git.sur5r.net Git - glabels/blob - src/view-line.c
Organized master branch to be top-level directory for glabels, instead of
[glabels] / src / view-line.c
1 /*
2  *  view-line.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-line.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_line.xbm"
33 #include "pixmaps/cursor_line_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 _glViewLinePrivate {
48 };
49
50
51 /*========================================================*/
52 /* Private globals.                                       */
53 /*========================================================*/
54
55
56 /*========================================================*/
57 /* Private function prototypes.                           */
58 /*========================================================*/
59
60 static void       gl_view_line_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 (glViewLine, gl_view_line, GL_TYPE_VIEW_OBJECT);
85
86
87 static void
88 gl_view_line_class_init (glViewLineClass *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_line_parent_class = g_type_class_peek_parent (class);
96
97         object_class->finalize = gl_view_line_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_line_init (glViewLine *view_line)
108 {
109         gl_debug (DEBUG_VIEW, "START");
110
111         view_line->priv = g_new0 (glViewLinePrivate, 1);
112
113         gl_debug (DEBUG_VIEW, "END");
114 }
115
116
117 static void
118 gl_view_line_finalize (GObject *object)
119 {
120         glViewLine *view_line = GL_VIEW_LINE (object);
121
122         gl_debug (DEBUG_VIEW, "START");
123
124         g_return_if_fail (object && GL_IS_VIEW_LINE (object));
125
126         g_free (view_line->priv);
127
128         G_OBJECT_CLASS (gl_view_line_parent_class)->finalize (object);
129
130         gl_debug (DEBUG_VIEW, "END");
131 }
132
133
134 /*****************************************************************************/
135 /* NEW line object view.                                                  */
136 /*****************************************************************************/
137 glViewObject *
138 gl_view_line_new (glLabelLine *object,
139                   glView     *view)
140 {
141         glViewLine         *view_line;
142
143         gl_debug (DEBUG_VIEW, "START");
144
145         g_return_val_if_fail (object && GL_IS_LABEL_LINE (object), NULL);
146         g_return_val_if_fail (view && GL_IS_VIEW (view), NULL);
147         
148         view_line = g_object_new (gl_view_line_get_type(), NULL);
149
150         gl_view_object_set_object (GL_VIEW_OBJECT(view_line),
151                                    GL_LABEL_OBJECT(object),
152                                    GL_VIEW_OBJECT_HANDLES_LINE);
153         gl_view_object_set_view (GL_VIEW_OBJECT(view_line), view);
154
155         gl_debug (DEBUG_VIEW, "END");
156
157         return GL_VIEW_OBJECT (view_line);
158 }
159
160
161 /*****************************************************************************/
162 /* Create a properties dialog for a line object.                             */
163 /*****************************************************************************/
164 static GtkWidget *
165 construct_properties_editor (glViewObject *view_object)
166 {
167         GtkWidget          *editor;
168         glViewLine         *view_line = (glViewLine *)view_object;
169         glLabelObject      *object;
170
171         gl_debug (DEBUG_VIEW, "START");
172
173         object = gl_view_object_get_object (GL_VIEW_OBJECT(view_line));
174
175         /* Build editor. */
176         editor = gl_object_editor_new (GL_STOCK_LINE, _("Line object properties"),
177                                        object->parent,
178                                        GL_OBJECT_EDITOR_SHADOW_PAGE,
179                                        GL_OBJECT_EDITOR_POSITION_PAGE,
180                                        GL_OBJECT_EDITOR_SIZE_LINE_PAGE,
181                                        GL_OBJECT_EDITOR_LINE_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         glColorNode       *line_color_node;
211         gdouble            line_width;
212         gboolean           shadow_state;
213         gdouble            shadow_x, shadow_y;
214         glColorNode       *shadow_color_node;
215         gdouble            shadow_opacity;
216         
217
218         gl_debug (DEBUG_VIEW, "START");
219
220         g_signal_handlers_block_by_func (G_OBJECT(object),
221                                          update_editor_from_object_cb,
222                                          editor);
223         g_signal_handlers_block_by_func (G_OBJECT(object),
224                                          update_editor_from_move_cb,
225                                          editor);
226
227         gl_object_editor_get_position (editor, &x, &y);
228         gl_label_object_set_position (object, x, y);
229
230         gl_object_editor_get_lsize (editor, &w, &h);
231         gl_label_object_set_size (object, w, h);
232
233         line_color_node = gl_object_editor_get_line_color (editor);
234         gl_label_object_set_line_color (object, line_color_node);
235         gl_color_node_free (&line_color_node);
236
237         line_width = gl_object_editor_get_line_width (editor);
238         gl_label_object_set_line_width (object, line_width);
239
240         shadow_state = gl_object_editor_get_shadow_state (editor);
241         gl_label_object_set_shadow_state (object, shadow_state);
242
243         gl_object_editor_get_shadow_offset (editor, &shadow_x, &shadow_y);
244         gl_label_object_set_shadow_offset (object, shadow_x, shadow_y);
245
246         shadow_color_node = gl_object_editor_get_shadow_color (editor);
247         gl_label_object_set_shadow_color (object, shadow_color_node);
248         gl_color_node_free (&shadow_color_node);
249
250         shadow_opacity = gl_object_editor_get_shadow_opacity (editor);
251         gl_label_object_set_shadow_opacity (object, shadow_opacity);
252
253         g_signal_handlers_unblock_by_func (G_OBJECT(object),
254                                            update_editor_from_object_cb,
255                                            editor);
256         g_signal_handlers_unblock_by_func (G_OBJECT(object),
257                                            update_editor_from_move_cb,
258                                            editor);
259
260         gl_debug (DEBUG_VIEW, "END");
261 }
262
263
264 /*---------------------------------------------------------------------------*/
265 /* PRIVATE. label object "changed" callback.                                 */
266 /*---------------------------------------------------------------------------*/
267 static void
268 update_editor_from_object_cb (glLabelObject  *object,
269                               glObjectEditor *editor)
270 {
271         gdouble            w, h;
272         glColorNode       *line_color_node;
273         gdouble            line_width;
274         gboolean           shadow_state;
275         gdouble            shadow_x, shadow_y;
276         glColorNode       *shadow_color_node;
277         gdouble            shadow_opacity;
278         glMerge           *merge;
279
280         gl_debug (DEBUG_VIEW, "START");
281
282         gl_label_object_get_size (object, &w, &h);
283         gl_object_editor_set_lsize (editor, w, h);
284         merge = gl_label_get_merge (GL_LABEL(object->parent));
285         
286         line_color_node = gl_label_object_get_line_color (GL_LABEL_OBJECT(object));
287         gl_object_editor_set_line_color (editor, (merge != NULL), line_color_node);
288         gl_color_node_free (&line_color_node);
289
290         line_width = gl_label_object_get_line_width (GL_LABEL_OBJECT(object));
291         gl_object_editor_set_line_width (editor, line_width);
292
293         shadow_state = gl_label_object_get_shadow_state (object);
294         gl_object_editor_set_shadow_state (editor, shadow_state);
295
296         gl_label_object_get_shadow_offset (object, &shadow_x, &shadow_y);
297         gl_object_editor_set_shadow_offset (editor, shadow_x, shadow_y);
298
299         shadow_color_node = gl_label_object_get_shadow_color (object);
300         gl_object_editor_set_shadow_color (editor, (merge != NULL), shadow_color_node);
301         gl_color_node_free (&shadow_color_node);
302
303         shadow_opacity = gl_label_object_get_shadow_opacity (object);
304         gl_object_editor_set_shadow_opacity (editor, shadow_opacity);
305
306         gl_debug (DEBUG_VIEW, "END");
307 }
308
309
310 /*---------------------------------------------------------------------------*/
311 /* PRIVATE. label object "moved" callback.                                   */
312 /*---------------------------------------------------------------------------*/
313 static void
314 update_editor_from_move_cb (glLabelObject    *object,
315                             gdouble           dx,
316                             gdouble           dy,
317                             glObjectEditor   *editor)
318 {
319         gdouble            x, y;
320
321         gl_debug (DEBUG_VIEW, "START");
322
323         gl_label_object_get_position (object, &x, &y);
324         gl_object_editor_set_position (editor, x, y);
325
326         gl_debug (DEBUG_VIEW, "END");
327 }
328
329
330 /*****************************************************************************/
331 /* Is object at (x,y)?                                                       */
332 /*****************************************************************************/
333 static gboolean
334 object_at (glViewObject  *view_object,
335            cairo_t       *cr,
336            gdouble        x,
337            gdouble        y)
338 {
339         glLabelObject    *object;
340         gdouble           w, h;
341         gdouble           line_width;
342
343         object = gl_view_object_get_object (view_object);
344
345         gl_label_object_get_size (object, &w, &h);
346
347         cairo_move_to (cr, 0.0, 0.0);
348         cairo_line_to (cr, w, h);
349
350         line_width = gl_label_object_get_line_width (object);
351         cairo_set_line_width (cr, line_width);
352         if (cairo_in_stroke (cr, x, y))
353         {
354                 return TRUE;
355         }
356
357         return FALSE;
358 }
359
360
361 /*****************************************************************************/
362 /* Return apropos cursor for create object mode.                             */
363 /*****************************************************************************/
364 GdkCursor *
365 gl_view_line_get_create_cursor (void)
366 {
367         GdkCursor       *cursor = NULL;
368         GdkPixmap       *pixmap_data, *pixmap_mask;
369         GdkColor         fg = { 0, 0, 0, 0 };
370         GdkColor         bg = { 0, 65535, 65535, 65535 };
371
372         gl_debug (DEBUG_VIEW, "START");
373
374         pixmap_data = gdk_bitmap_create_from_data (NULL,
375                                                    (gchar *)cursor_line_bits,
376                                                    cursor_line_width,
377                                                    cursor_line_height);
378         pixmap_mask = gdk_bitmap_create_from_data (NULL,
379                                                    (gchar *)cursor_line_mask_bits,
380                                                    cursor_line_mask_width,
381                                                    cursor_line_mask_height);
382         cursor = gdk_cursor_new_from_pixmap (pixmap_data, pixmap_mask, &fg,
383                                              &bg, cursor_line_x_hot,
384                                              cursor_line_y_hot);
385
386         gl_debug (DEBUG_VIEW, "END");
387
388         return cursor;
389 }
390
391
392 /*****************************************************************************/
393 /* Object creation handler: button press event.                              */
394 /*****************************************************************************/
395 void
396 gl_view_line_create_button_press_event   (glView *view,
397                                           gdouble x,
398                                           gdouble y)
399 {
400         GObject             *object;
401         glColorNode         *line_color_node;
402
403         gl_view_unselect_all (view);
404
405         line_color_node = gl_color_node_new_default ();
406                 
407         object = gl_label_line_new (view->label);
408         gl_label_object_set_position (GL_LABEL_OBJECT(object), x, y);
409         gl_label_object_set_size (GL_LABEL_OBJECT(object), 0.0, 0.0);
410         line_color_node->color = gl_color_set_opacity (gl_view_get_default_line_color(view), 0.5);
411         gl_label_object_set_line_width (GL_LABEL_OBJECT(object),
412                                         gl_view_get_default_line_width(view));
413         gl_label_object_set_line_color (GL_LABEL_OBJECT(object),
414                                         line_color_node);
415
416         gl_color_node_free (&line_color_node);
417
418         view->create_object = GL_LABEL_OBJECT (object);
419         view->create_x0 = x;
420         view->create_y0 = y;
421 }
422
423
424 /*****************************************************************************/
425 /* Object creation handler: motion event.                                    */
426 /*****************************************************************************/
427 void
428 gl_view_line_create_motion_event     (glView *view,
429                                       gdouble x,
430                                       gdouble y)
431 {
432         gdouble w, h;
433
434         w = x - view->create_x0;
435         h = y - view->create_y0;
436         gl_label_object_set_size (GL_LABEL_OBJECT(view->create_object), w, h);
437 }
438
439
440 /*****************************************************************************/
441 /* Object creation handler: button relesase event.                           */
442 /*****************************************************************************/
443 void
444 gl_view_line_create_button_release_event (glView *view,
445                                           gdouble x,
446                                           gdouble y)
447 {
448         glColorNode         *line_color_node;
449         gdouble              w, h;
450
451         line_color_node = gl_color_node_new_default ();
452                 
453         if ((view->create_x0 == x) && (view->create_y0 == y)) {
454                 x = view->create_x0 + 36.0;
455                 y = view->create_y0 + 36.0;
456         }
457         w = x - view->create_x0;
458         h = y - view->create_y0;
459         gl_label_object_set_size (GL_LABEL_OBJECT(view->create_object), w, h);
460         line_color_node->color = gl_view_get_default_line_color(view);
461         gl_label_object_set_line_color (GL_LABEL_OBJECT(view->create_object), line_color_node);
462         gl_color_node_free (&line_color_node);
463 }
464
465
466
467 /*
468  * Local Variables:       -- emacs
469  * mode: C                -- emacs
470  * c-basic-offset: 8      -- emacs
471  * tab-width: 8           -- emacs
472  * indent-tabs-mode: nil  -- emacs
473  * End:                   -- emacs
474  */