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