Delete trailing whitespace
[ardour.git] / gtk2_ardour / canvas-simpleline.c
1 #include <stdio.h>
2 #include <math.h>
3 #include <cairo.h>
4 #include <libgnomecanvas/libgnomecanvas.h>
5
6 #include "canvas-simpleline.h"
7 #include "rgb_macros.h"
8 #include "gettext.h"
9 #define _(Text)  dgettext (PACKAGE,Text)
10
11 enum {
12         PROP_0,
13         PROP_X1,
14         PROP_Y1,
15         PROP_X2,
16         PROP_Y2,
17         PROP_COLOR_RGBA
18 };
19
20 static void gnome_canvas_simpleline_class_init   (GnomeCanvasSimpleLineClass *class);
21
22 static void gnome_canvas_simpleline_init         (GnomeCanvasSimpleLine      *simpleline);
23
24 static void gnome_canvas_simpleline_destroy      (GtkObject            *object);
25
26 static void gnome_canvas_simpleline_set_property (GObject        *object,
27                                                   guint           prop_id,
28                                                   const GValue   *value,
29                                                   GParamSpec     *pspec);
30 static void gnome_canvas_simpleline_get_property (GObject        *object,
31                                                   guint           prop_id,
32                                                   GValue         *value,
33                                                   GParamSpec     *pspec);
34
35 static void   gnome_canvas_simpleline_update     (GnomeCanvasItem *item,
36                                                   double          *affine,
37                                                   ArtSVP          *clip_path,
38                                                   int flags);
39
40 static void   gnome_canvas_simpleline_bounds     (GnomeCanvasItem *item,
41                                                   double          *x1,
42                                                   double          *y1,
43                                                   double          *x2,
44                                                   double          *y2);
45
46 static double gnome_canvas_simpleline_point      (GnomeCanvasItem  *item,
47                                                   double            x,
48                                                   double            y,
49                                                   int               cx,
50                                                   int               cy,
51                                                   GnomeCanvasItem **actual_item);
52
53 static void   gnome_canvas_simpleline_render     (GnomeCanvasItem *item,
54                                                   GnomeCanvasBuf  *buf);
55
56 static void   gnome_canvas_simpleline_draw       (GnomeCanvasItem *item,
57                                                   GdkDrawable     *drawable,
58                                                   int              x,
59                                                   int              y,
60                                                   int              w,
61                                                   int              h);
62
63 static GnomeCanvasItemClass *parent_class;
64
65
66 GType
67 gnome_canvas_simpleline_get_type (void)
68 {
69         static GType simpleline_type;
70
71         if (!simpleline_type) {
72                 static const GTypeInfo object_info = {
73                         sizeof (GnomeCanvasSimpleLineClass),
74                         (GBaseInitFunc) NULL,
75                         (GBaseFinalizeFunc) NULL,
76                         (GClassInitFunc) gnome_canvas_simpleline_class_init,
77                         (GClassFinalizeFunc) NULL,
78                         NULL,                   /* class_data */
79                         sizeof (GnomeCanvasSimpleLine),
80                         0,                      /* n_preallocs */
81                         (GInstanceInitFunc) gnome_canvas_simpleline_init,
82                         NULL                    /* value_table */
83                 };
84
85                 simpleline_type = g_type_register_static (GNOME_TYPE_CANVAS_ITEM, "GnomeCanvasSimpleLine",
86                                                           &object_info, 0);
87         }
88
89         return simpleline_type;
90 }
91
92 static void
93 gnome_canvas_simpleline_class_init (GnomeCanvasSimpleLineClass *class)
94 {
95         GObjectClass *gobject_class;
96         GtkObjectClass *object_class;
97         GnomeCanvasItemClass *item_class;
98
99         gobject_class = (GObjectClass *) class;
100         object_class = (GtkObjectClass *) class;
101         item_class = (GnomeCanvasItemClass *) class;
102
103         parent_class = g_type_class_peek_parent (class);
104
105         gobject_class->set_property = gnome_canvas_simpleline_set_property;
106         gobject_class->get_property = gnome_canvas_simpleline_get_property;
107
108         g_object_class_install_property (gobject_class,
109                                          PROP_X1,
110                                          g_param_spec_double ("x1",
111                                                               _("x1"),
112                                                               _("x coordinate of upper left corner of rect"),
113                                                               -G_MAXDOUBLE,
114                                                               G_MAXDOUBLE,
115                                                               0.0,
116                                                               G_PARAM_READWRITE));
117
118         g_object_class_install_property (gobject_class,
119                                          PROP_Y1,
120                                          g_param_spec_double ("y1",
121                                                               _("y1"),
122                                                               _("y coordinate of upper left corner of rect "),
123                                                               -G_MAXDOUBLE,
124                                                               G_MAXDOUBLE,
125                                                               0.0,
126                                                               G_PARAM_READWRITE));
127
128
129         g_object_class_install_property (gobject_class,
130                                          PROP_X2,
131                                          g_param_spec_double ("x2",
132                                                               _("x2"),
133                                                               _("x coordinate of lower right corner of rect"),
134                                                               -G_MAXDOUBLE,
135                                                               G_MAXDOUBLE,
136                                                               0.0,
137                                                               G_PARAM_READWRITE));
138
139         g_object_class_install_property (gobject_class,
140                                          PROP_Y2,
141                                          g_param_spec_double ("y2",
142                                                               _("y2"),
143                                                               _("y coordinate of lower right corner of rect "),
144                                                               -G_MAXDOUBLE,
145                                                               G_MAXDOUBLE,
146                                                               0.0,
147                                                               G_PARAM_READWRITE));
148         g_object_class_install_property (gobject_class,
149                                          PROP_COLOR_RGBA,
150                                          g_param_spec_uint ("color_rgba",
151                                                             _("color rgba"),
152                                                             _("color of line"),
153                                                             0,
154                                                             G_MAXUINT,
155                                                             0,
156                                                             G_PARAM_READWRITE));
157
158         object_class->destroy = gnome_canvas_simpleline_destroy;
159
160         item_class->update = gnome_canvas_simpleline_update;
161         item_class->bounds = gnome_canvas_simpleline_bounds;
162         item_class->point = gnome_canvas_simpleline_point;
163         item_class->render = gnome_canvas_simpleline_render;
164         item_class->draw = gnome_canvas_simpleline_draw;
165 }
166
167 static void
168 gnome_canvas_simpleline_init (GnomeCanvasSimpleLine *simpleline)
169 {
170         simpleline->x1 = 0.0;
171         simpleline->y1 = 0.0;
172         simpleline->x2 = 0.0;
173         simpleline->y2 = 0.0;
174         simpleline->color = RGBA_TO_UINT(98,123,174,241);
175 }
176
177 static void
178 gnome_canvas_simpleline_destroy (GtkObject *object)
179 {
180         GnomeCanvasSimpleLine *line;
181
182         g_return_if_fail (object != NULL);
183         g_return_if_fail (GNOME_IS_CANVAS_SIMPLELINE (object));
184
185         line = GNOME_CANVAS_SIMPLELINE (object);
186
187         if (GTK_OBJECT_CLASS (parent_class)->destroy)
188                 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
189 }
190
191 /*
192  * CANVAS CALLBACKS
193  */
194
195 static void
196 gnome_canvas_simpleline_set_property (GObject      *object,
197                                       guint         prop_id,
198                                       const GValue *value,
199                                       GParamSpec   *pspec)
200
201 {
202         (void) pspec;
203
204         GnomeCanvasSimpleLine *simpleline;
205         int update = FALSE;
206         int bounds_changed = FALSE;
207         double d;
208
209         g_return_if_fail (object != NULL);
210         g_return_if_fail (GNOME_IS_CANVAS_SIMPLELINE (object));
211
212         simpleline = GNOME_CANVAS_SIMPLELINE (object);
213
214         switch (prop_id) {
215         case PROP_X1:
216                 d = g_value_get_double (value);
217                 if (simpleline->x1 != d) {
218                         simpleline->x1 = d;
219                         bounds_changed = TRUE;
220                 }
221                 break;
222
223         case PROP_Y1:
224                 d = g_value_get_double (value);
225                 if (simpleline->y1 != d) {
226                         simpleline->y1 = d;
227                         bounds_changed = TRUE;
228                 }
229                 break;
230
231         case PROP_X2:
232                 d = g_value_get_double (value);
233                 if (simpleline->x2 != d) {
234                         simpleline->x2 = d;
235                         bounds_changed = TRUE;
236                 }
237                 break;
238
239         case PROP_Y2:
240                 d = g_value_get_double (value);
241                 if (simpleline->y2 != d) {
242                         simpleline->y2 = d;
243                         bounds_changed = TRUE;
244                 }
245                 break;
246
247         case PROP_COLOR_RGBA:
248                 if (simpleline->color != g_value_get_uint(value)) {
249                         simpleline->color = g_value_get_uint(value);
250                         UINT_TO_RGBA (simpleline->color, &simpleline->r, &simpleline->g, &simpleline->b, &simpleline->a);
251                         update = TRUE;
252                 }
253                 break;
254         default:
255                 break;
256         }
257
258         if (update || bounds_changed) {
259                 gnome_canvas_item_request_update (GNOME_CANVAS_ITEM(object));
260         }
261 }
262
263 static void
264 gnome_canvas_simpleline_get_property (GObject      *object,
265                                       guint         prop_id,
266                                       GValue       *value,
267                                       GParamSpec   *pspec)
268 {
269         g_return_if_fail (object != NULL);
270         g_return_if_fail (GNOME_IS_CANVAS_SIMPLELINE (object));
271
272         GnomeCanvasSimpleLine *line = GNOME_CANVAS_SIMPLELINE (object);
273
274         switch (prop_id) {
275         case PROP_X1:
276                 g_value_set_double (value, line->x1);
277                 break;
278         case PROP_X2:
279                 g_value_set_double (value, line->x2);
280                 break;
281         case PROP_Y1:
282                 g_value_set_double (value, line->y1);
283                 break;
284         case PROP_Y2:
285                 g_value_set_double (value, line->y2);
286                 break;
287         case PROP_COLOR_RGBA:
288                 g_value_set_uint (value, line->color);
289                 break;
290         default:
291                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
292                 break;
293         }
294 }
295
296 static void
297 gnome_canvas_simpleline_update (GnomeCanvasItem *item, double *affine, ArtSVP *clip_path, int flags)
298 {
299         GnomeCanvasSimpleLine *simpleline;
300         double x1, x2, y1, y2;
301
302         simpleline = GNOME_CANVAS_SIMPLELINE (item);
303
304         if (parent_class->update)
305                 (* parent_class->update) (item, affine, clip_path, flags);
306
307         /* redraw old location */
308
309         gnome_canvas_request_redraw (item->canvas, item->x1, item->y1, item->x2, item->y2);
310
311         /* get current bounding box in parent-relative world coordinates */
312
313         gnome_canvas_simpleline_bounds (item, &x1, &y1, &x2, &y2);
314
315         /* convert parent-relative item coordinates to world coordinates */
316
317         gnome_canvas_item_i2w (item, &x1, &y1);
318         gnome_canvas_item_i2w (item, &x2, &y2);
319
320         /* don't suffer from rounding errors */
321
322         x1 = floor (x1);
323         y1 = floor (y1);
324         x2 = ceil (x2);
325         y2 = ceil (y2);
326
327         /* force non-zero dimensionality for both axes */
328
329         if (x1 == x2) {
330                 x2 += 1.0;
331         }
332
333         if (y1 == y2) {
334                 y2 += 1.0;
335         }
336
337         /* reset item bounding box (canvas coordinates, so integral. but stored in doubles) */
338
339         gnome_canvas_w2c_d (GNOME_CANVAS(item->canvas), x1, y1, &item->x1, &item->y1);
340         gnome_canvas_w2c_d (GNOME_CANVAS(item->canvas), x2, y2, &item->x2, &item->y2);
341
342         /* redraw new location */
343
344         gnome_canvas_request_redraw (item->canvas, item->x1, item->y1, item->x2, item->y2);
345
346         /* store actual line coords as canvas coordinates for use in render() */
347
348         x1 = simpleline->x1;
349         y1 = simpleline->y1;
350         x2 = simpleline->x2;
351         y2 = simpleline->y2;
352         /* convert to world */
353         gnome_canvas_item_i2w (item, &x1, &y1);
354         gnome_canvas_item_i2w (item, &x2, &y2);
355         /* avoid rounding errors */
356         x1 = (int) floor (item->x1);
357         y1 = (int) floor (item->y1);
358         x2 = (int) ceil (item->x2);
359         y2 = (int) ceil (item->y2);
360         /* convert to canvas coordinates, integral, stored in integers */
361         gnome_canvas_w2c (GNOME_CANVAS(item->canvas), x1, y1, &simpleline->cx1, &simpleline->cy1);
362         gnome_canvas_w2c (GNOME_CANVAS(item->canvas), x2, y2, &simpleline->cx2, &simpleline->cy2);
363 }
364
365 static void
366 gnome_canvas_simpleline_render (GnomeCanvasItem *item,
367                                 GnomeCanvasBuf *buf)
368 {
369         GnomeCanvasSimpleLine *simpleline;
370         int x1, x2;
371         int y1, y2;
372
373         simpleline = GNOME_CANVAS_SIMPLELINE (item);
374
375         x1 = simpleline->cx1;
376         x2 = simpleline->cx2;
377         y1 = simpleline->cy1;
378
379         if (buf->is_bg) {
380                 gnome_canvas_buf_ensure_buf (buf);
381                 buf->is_bg = FALSE;
382         }
383
384         if (simpleline->x1 != simpleline->x2) {
385                 PAINT_HORIZA(buf, simpleline->r, simpleline->g, simpleline->b, simpleline->a,
386                              x1, x2, y1);
387         } else {
388                 y2 = simpleline->cy2;
389                 PAINT_VERTA (buf, simpleline->r, simpleline->g, simpleline->b, simpleline->a,
390                              x1, y1, y2);
391
392         }
393 }
394
395 static void
396 gnome_canvas_simpleline_draw (GnomeCanvasItem *item,
397                               GdkDrawable *drawable,
398                               int x, int y,
399                               int width, int height)
400 {
401         GnomeCanvasSimpleLine *simpleline;
402
403         simpleline = GNOME_CANVAS_SIMPLELINE (item);
404
405         /* XXX not implemented */
406 }
407
408 static void
409 gnome_canvas_simpleline_bounds (GnomeCanvasItem *item, double *x1, double *y1, double *x2, double *y2)
410 {
411         GnomeCanvasSimpleLine *simpleline = GNOME_CANVAS_SIMPLELINE (item);
412
413         *x1 = simpleline->x1;
414         *y1 = simpleline->y1;
415         *x2 = simpleline->x1;
416         *y2 = simpleline->y2;
417 }
418
419 static double
420 gnome_canvas_simpleline_point (GnomeCanvasItem *item, double x, double y, int cx, int cy, GnomeCanvasItem **actual_item)
421 {
422         (void) cx;
423         (void) cy;
424
425         GnomeCanvasSimpleLine *simpleline;
426         double x1, y1, x2, y2;
427         double dx, dy;
428
429         simpleline = GNOME_CANVAS_SIMPLELINE (item);
430
431         *actual_item = item;
432
433         /* Find the bounds for the line */
434
435         gnome_canvas_simpleline_bounds (item, &x1, &y1, &x2, &y2);
436
437         /* Is point inside line */
438
439         if ((x >= x1) && (y >= y1) && (x <= x2) && (y <= y2)) {
440                 return 0.0;
441         }
442         /* Point is outside line */
443
444         if (x < x1)
445                 dx = x1 - x;
446         else if (x > x2)
447                 dx = x - x2;
448         else
449                 dx = 0.0;
450
451         if (y < y1)
452                 dy = y1 - y;
453         else if (y > y2)
454                 dy = y - y2;
455         else
456                 dy = 0.0;
457
458         return sqrt (dx * dx + dy * dy);
459 }