more tranzport lowlevel fixes and rebinding
[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 }
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 static void
192 gnome_canvas_simpleline_bounds_world (GnomeCanvasItem *item, int* ix1, int* iy1, int* ix2, int* iy2)
193 {
194         double x1, x2, y1, y2;
195         ArtPoint i1, i2;
196         ArtPoint w1, w2;
197         double i2w[6];
198         GnomeCanvasSimpleLine *simpleline = GNOME_CANVAS_SIMPLELINE(item);
199
200         gnome_canvas_simpleline_bounds (item, &x1, &y1, &x2, &y2);
201
202         i1.x = x1;
203         i1.y = y1;
204         i2.x = x2;
205         i2.y = y2;
206         
207         gnome_canvas_item_i2w_affine (item, i2w);
208         art_affine_point (&w1, &i1, i2w);
209         art_affine_point (&w2, &i2, i2w);
210
211         *ix1 = (int) rint(w1.x);
212         *ix2 = (int) rint(w2.x);
213         *iy1 = (int) rint(w1.y);
214         *iy2 = (int) rint(w2.y);
215
216         /* the update rect has to be of non-zero width and height */
217
218         if (x1 == x2) {
219                 simpleline->horizontal = FALSE;
220                 *ix2 += 1;
221         } else {
222                 simpleline->horizontal = TRUE;
223                 *iy2 += 1;
224         }
225 }
226
227 static void 
228 gnome_canvas_simpleline_reset_bounds (GnomeCanvasItem *item)
229 {
230         int Ix1, Ix2, Iy1, Iy2;
231
232         gnome_canvas_simpleline_bounds_world (item, &Ix1, &Iy1, &Ix2, &Iy2);
233         gnome_canvas_update_bbox (item, Ix1, Iy1, Ix2, Iy2);
234 }
235
236 /* 
237  * CANVAS CALLBACKS 
238  */
239
240 static void
241 gnome_canvas_simpleline_set_property (GObject      *object,
242                                       guint         prop_id,
243                                       const GValue *value,
244                                       GParamSpec   *pspec)
245
246 {
247         GnomeCanvasSimpleLine *simpleline;
248         int update = FALSE;
249         int bounds_changed = FALSE;
250
251         g_return_if_fail (object != NULL);
252         g_return_if_fail (GNOME_IS_CANVAS_SIMPLELINE (object));
253
254         simpleline = GNOME_CANVAS_SIMPLELINE (object);
255
256         switch (prop_id) {
257         case PROP_X1:
258                 if (simpleline->x1 != g_value_get_double (value)) {
259                         simpleline->x1 = g_value_get_double (value);
260                         bounds_changed = TRUE;
261                 }
262                 break;
263
264         case PROP_Y1:
265                 if (simpleline->y1 != g_value_get_double (value)) {
266                         simpleline->y1 = g_value_get_double (value);
267                         bounds_changed = TRUE;
268                 }
269                 break;
270
271         case PROP_X2:
272                 if (simpleline->x2 != g_value_get_double (value)) {
273                         simpleline->x2 = g_value_get_double (value);
274                         bounds_changed = TRUE;
275                 }
276                 break;
277
278         case PROP_Y2:
279                 if (simpleline->y2 != g_value_get_double (value)) {
280                         simpleline->y2 = g_value_get_double (value);
281                         bounds_changed = TRUE;
282                 }
283                 break;
284                 
285         case PROP_COLOR_RGBA:
286                 if (simpleline->color != g_value_get_uint(value)) {
287                         simpleline->color = g_value_get_uint(value);
288                         UINT_TO_RGBA (simpleline->color, &simpleline->r, &simpleline->g, &simpleline->b, &simpleline->a);
289                         update = TRUE;
290                 }
291                 break;
292         default:
293                 break;
294         }
295
296         if (update || bounds_changed) {
297                 gnome_canvas_item_request_update (GNOME_CANVAS_ITEM(object));
298         }
299 }
300
301 static void
302 gnome_canvas_simpleline_get_property (GObject      *object,
303                                       guint         prop_id,
304                                       GValue       *value,
305                                       GParamSpec   *pspec)
306 {
307         g_return_if_fail (object != NULL);
308         g_return_if_fail (GNOME_IS_CANVAS_SIMPLELINE (object));
309         
310         GnomeCanvasSimpleLine *line = GNOME_CANVAS_SIMPLELINE (object);
311
312         switch (prop_id) {
313         case PROP_X1:
314                 g_value_set_double (value, line->x1);
315                 break;
316         case PROP_X2:
317                 g_value_set_double (value, line->x2);
318                 break;
319         case PROP_Y1:
320                 g_value_set_double (value, line->y1);
321                 break;
322         case PROP_Y2:
323                 g_value_set_double (value, line->y2);
324                 break;
325         case PROP_COLOR_RGBA:
326                 g_value_set_uint (value, line->color);
327                 break;
328         default:
329                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
330                 break;
331         }
332 }
333
334 static void
335 gnome_canvas_simpleline_update (GnomeCanvasItem *item, double *affine, ArtSVP *clip_path, int flags)
336 {
337         GnomeCanvasSimpleLine *simpleline;
338         double x;
339         double y;
340
341         simpleline = GNOME_CANVAS_SIMPLELINE (item);
342
343         if (parent_class->update)
344                 (* parent_class->update) (item, affine, clip_path, flags);
345
346         gnome_canvas_simpleline_reset_bounds (item);
347
348         x = simpleline->x1;
349         y = simpleline->y1;
350
351         gnome_canvas_item_i2w (item, &x, &y);
352         gnome_canvas_w2c (GNOME_CANVAS(item->canvas), x, y, &simpleline->bbox_ulx, &simpleline->bbox_uly);
353
354         x = simpleline->x2;
355         y = simpleline->y2;
356
357         gnome_canvas_item_i2w (item, &x, &y);
358         gnome_canvas_w2c (GNOME_CANVAS(item->canvas), x, y, &simpleline->bbox_lrx, &simpleline->bbox_lry);
359 }
360
361 static void
362 gnome_canvas_simpleline_render (GnomeCanvasItem *item,
363                               GnomeCanvasBuf *buf)
364 {
365         GnomeCanvasSimpleLine *simpleline;
366         int end, begin;
367
368         simpleline = GNOME_CANVAS_SIMPLELINE (item);
369
370         if (parent_class->render) {
371                 (*parent_class->render) (item, buf);
372         }
373
374         if (buf->is_bg) {
375                 gnome_canvas_buf_ensure_buf (buf);
376                 buf->is_bg = FALSE;
377         }
378
379         //begin = MAX(simpleline->bbox_ulx,buf->rect.x0);
380         //end = MIN(simpleline->bbox_lrx,buf->rect.x1);
381         
382         begin = simpleline->bbox_ulx;
383         end = simpleline->bbox_lrx;
384
385         if (simpleline->color != 0) {
386                 if (simpleline->horizontal) {
387                         PAINT_HORIZA(buf, simpleline->r, simpleline->g, simpleline->b, simpleline->a, 
388                                      begin, end, simpleline->bbox_uly);
389                 } else {
390                         PAINT_VERTA(buf, simpleline->r, simpleline->g, simpleline->b, simpleline->a,
391                                     begin, simpleline->bbox_uly, simpleline->bbox_lry);
392                 }
393         }
394 }
395
396 static void
397 gnome_canvas_simpleline_draw (GnomeCanvasItem *item,
398                             GdkDrawable *drawable,
399                             int x, int y,
400                             int width, int height)
401 {
402         GnomeCanvasSimpleLine *simpleline;
403
404         simpleline = GNOME_CANVAS_SIMPLELINE (item);
405
406         if (parent_class->draw) {
407                 (* parent_class->draw) (item, drawable, x, y, width, height);
408         }
409
410         fprintf (stderr, "please don't use the CanvasSimpleLine item in a non-aa Canvas\n");
411         abort ();
412 }
413
414 static void
415 gnome_canvas_simpleline_bounds (GnomeCanvasItem *item, double *x1, double *y1, double *x2, double *y2)
416 {
417         GnomeCanvasSimpleLine *simpleline = GNOME_CANVAS_SIMPLELINE (item);
418
419         *x1 = simpleline->x1;
420         *y1 = simpleline->y1;
421         *x2 = simpleline->x2;
422         *y2 = simpleline->y2;
423 }
424
425 static double
426 gnome_canvas_simpleline_point (GnomeCanvasItem *item, double x, double y, int cx, int cy, GnomeCanvasItem **actual_item)
427 {
428         GnomeCanvasSimpleLine *simpleline;
429         double x1, y1, x2, y2;
430         double dx, dy;
431
432         simpleline = GNOME_CANVAS_SIMPLELINE (item);
433
434         *actual_item = item;
435
436         /* Find the bounds for the rectangle plus its outline width */
437
438         gnome_canvas_simpleline_bounds (item, &x1, &y1, &x2, &y2);
439
440         /* Is point inside rectangle */
441         
442         if ((x >= x1) && (y >= y1) && (x <= x2) && (y <= y2)) {
443                 return 0.0;
444         }
445
446         /* Point is outside rectangle */
447
448         if (x < x1)
449                 dx = x1 - x;
450         else if (x > x2)
451                 dx = x - x2;
452         else
453                 dx = 0.0;
454
455         if (y < y1)
456                 dy = y1 - y;
457         else if (y > y2)
458                 dy = y - y2;
459         else
460                 dy = 0.0;
461
462         return sqrt (dx * dx + dy * dy);
463 }