a reverb is a reverb is a reverb
[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         g_return_if_fail (object != NULL);
181         g_return_if_fail (GNOME_IS_CANVAS_SIMPLELINE (object));
182
183         if (GTK_OBJECT_CLASS (parent_class)->destroy)
184                 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
185 }
186
187 /*
188  * CANVAS CALLBACKS
189  */
190
191 static void
192 gnome_canvas_simpleline_set_property (GObject      *object,
193                                       guint         prop_id,
194                                       const GValue *value,
195                                       GParamSpec   *pspec)
196
197 {
198         (void) pspec;
199
200         GnomeCanvasSimpleLine *simpleline;
201         int update = FALSE;
202         int bounds_changed = FALSE;
203         double d;
204
205         g_return_if_fail (object != NULL);
206         g_return_if_fail (GNOME_IS_CANVAS_SIMPLELINE (object));
207
208         simpleline = GNOME_CANVAS_SIMPLELINE (object);
209
210         switch (prop_id) {
211         case PROP_X1:
212                 d = g_value_get_double (value);
213                 if (simpleline->x1 != d) {
214                         simpleline->x1 = d;
215                         bounds_changed = TRUE;
216                 }
217                 break;
218
219         case PROP_Y1:
220                 d = g_value_get_double (value);
221                 if (simpleline->y1 != d) {
222                         simpleline->y1 = d;
223                         bounds_changed = TRUE;
224                 }
225                 break;
226
227         case PROP_X2:
228                 d = g_value_get_double (value);
229                 if (simpleline->x2 != d) {
230                         simpleline->x2 = d;
231                         bounds_changed = TRUE;
232                 }
233                 break;
234
235         case PROP_Y2:
236                 d = g_value_get_double (value);
237                 if (simpleline->y2 != d) {
238                         simpleline->y2 = d;
239                         bounds_changed = TRUE;
240                 }
241                 break;
242
243         case PROP_COLOR_RGBA:
244                 if (simpleline->color != g_value_get_uint(value)) {
245                         simpleline->color = g_value_get_uint(value);
246                         UINT_TO_RGBA (simpleline->color, &simpleline->r, &simpleline->g, &simpleline->b, &simpleline->a);
247                         update = TRUE;
248                 }
249                 break;
250         default:
251                 break;
252         }
253
254         if (update || bounds_changed) {
255                 gnome_canvas_item_request_update (GNOME_CANVAS_ITEM(object));
256         }
257 }
258
259 static void
260 gnome_canvas_simpleline_get_property (GObject      *object,
261                                       guint         prop_id,
262                                       GValue       *value,
263                                       GParamSpec   *pspec)
264 {
265         g_return_if_fail (object != NULL);
266         g_return_if_fail (GNOME_IS_CANVAS_SIMPLELINE (object));
267
268         GnomeCanvasSimpleLine *line = GNOME_CANVAS_SIMPLELINE (object);
269
270         switch (prop_id) {
271         case PROP_X1:
272                 g_value_set_double (value, line->x1);
273                 break;
274         case PROP_X2:
275                 g_value_set_double (value, line->x2);
276                 break;
277         case PROP_Y1:
278                 g_value_set_double (value, line->y1);
279                 break;
280         case PROP_Y2:
281                 g_value_set_double (value, line->y2);
282                 break;
283         case PROP_COLOR_RGBA:
284                 g_value_set_uint (value, line->color);
285                 break;
286         default:
287                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
288                 break;
289         }
290 }
291
292 static void
293 gnome_canvas_simpleline_update (GnomeCanvasItem *item, double *affine, ArtSVP *clip_path, int flags)
294 {
295         GnomeCanvasSimpleLine *simpleline;
296         double x1, x2, y1, y2;
297
298         simpleline = GNOME_CANVAS_SIMPLELINE (item);
299
300         if (parent_class->update)
301                 (* parent_class->update) (item, affine, clip_path, flags);
302
303         /* redraw old location */
304
305         gnome_canvas_request_redraw (item->canvas, item->x1, item->y1, item->x2, item->y2);
306
307         /* get current bounding box in parent-relative world coordinates */
308
309         gnome_canvas_simpleline_bounds (item, &x1, &y1, &x2, &y2);
310
311         /* convert parent-relative item coordinates to world coordinates */
312
313         gnome_canvas_item_i2w (item, &x1, &y1);
314         gnome_canvas_item_i2w (item, &x2, &y2);
315
316         /* don't suffer from rounding errors */
317
318         x1 = floor (x1);
319         y1 = floor (y1);
320         x2 = ceil (x2);
321         y2 = ceil (y2);
322
323         /* force non-zero dimensionality for both axes */
324
325         if (x1 == x2) {
326                 x2 += 1.0;
327         }
328
329         if (y1 == y2) {
330                 y2 += 1.0;
331         }
332
333         /* reset item bounding box (canvas coordinates, so integral. but stored in doubles) */
334
335         gnome_canvas_w2c_d (GNOME_CANVAS(item->canvas), x1, y1, &item->x1, &item->y1);
336         gnome_canvas_w2c_d (GNOME_CANVAS(item->canvas), x2, y2, &item->x2, &item->y2);
337
338         /* redraw new location */
339
340         gnome_canvas_request_redraw (item->canvas, item->x1, item->y1, item->x2, item->y2);
341
342         /* store actual line coords as canvas coordinates for use in render() */
343
344         x1 = simpleline->x1;
345         y1 = simpleline->y1;
346         x2 = simpleline->x2;
347         y2 = simpleline->y2;
348         /* convert to world */
349         gnome_canvas_item_i2w (item, &x1, &y1);
350         gnome_canvas_item_i2w (item, &x2, &y2);
351         /* avoid rounding errors */
352         x1 = (int) floor (item->x1);
353         y1 = (int) floor (item->y1);
354         x2 = (int) ceil (item->x2);
355         y2 = (int) ceil (item->y2);
356         /* convert to canvas coordinates, integral, stored in integers */
357         gnome_canvas_w2c (GNOME_CANVAS(item->canvas), x1, y1, &simpleline->cx1, &simpleline->cy1);
358         gnome_canvas_w2c (GNOME_CANVAS(item->canvas), x2, y2, &simpleline->cx2, &simpleline->cy2);
359 }
360
361 static void
362 gnome_canvas_simpleline_render (GnomeCanvasItem *item,
363                                 GnomeCanvasBuf *buf)
364 {
365         GnomeCanvasSimpleLine *simpleline;
366         int x1, x2;
367         int y1, y2;
368
369         simpleline = GNOME_CANVAS_SIMPLELINE (item);
370
371         x1 = simpleline->cx1;
372         x2 = simpleline->cx2;
373         y1 = simpleline->cy1;
374
375         if (buf->is_bg) {
376                 gnome_canvas_buf_ensure_buf (buf);
377                 buf->is_bg = FALSE;
378         }
379
380         if (simpleline->x1 != simpleline->x2) {
381                 PAINT_HORIZA(buf, simpleline->r, simpleline->g, simpleline->b, simpleline->a,
382                              x1, x2, y1);
383         } else {
384                 y2 = simpleline->cy2;
385                 PAINT_VERTA (buf, simpleline->r, simpleline->g, simpleline->b, simpleline->a,
386                              x1, y1, y2);
387
388         }
389 }
390
391 static void
392 gnome_canvas_simpleline_draw (GnomeCanvasItem* canvas,
393                               GdkDrawable* drawable,
394                               int x, int y,
395                               int width, int height)
396 {
397         /* XXX not implemented */
398 }
399
400 static void
401 gnome_canvas_simpleline_bounds (GnomeCanvasItem *item, double *x1, double *y1, double *x2, double *y2)
402 {
403         GnomeCanvasSimpleLine *simpleline = GNOME_CANVAS_SIMPLELINE (item);
404
405         *x1 = simpleline->x1;
406         *y1 = simpleline->y1;
407         *x2 = simpleline->x1;
408         *y2 = simpleline->y2;
409 }
410
411 static double
412 gnome_canvas_simpleline_point (GnomeCanvasItem *item, double x, double y, int cx, int cy, GnomeCanvasItem **actual_item)
413 {
414         (void) cx;
415         (void) cy;
416
417         double x1, y1, x2, y2;
418         double dx, dy;
419
420         *actual_item = item;
421
422         /* Find the bounds for the line */
423
424         gnome_canvas_simpleline_bounds (item, &x1, &y1, &x2, &y2);
425
426         /* Is point inside line */
427
428         if ((x >= x1) && (y >= y1) && (x <= x2) && (y <= y2)) {
429                 return 0.0;
430         }
431         /* Point is outside line */
432
433         if (x < x1)
434                 dx = x1 - x;
435         else if (x > x2)
436                 dx = x - x2;
437         else
438                 dx = 0.0;
439
440         if (y < y1)
441                 dy = y1 - y;
442         else if (y > y2)
443                 dy = y - y2;
444         else
445                 dy = 0.0;
446
447         return sqrt (dx * dx + dy * dy);
448 }