fix keyboard event handling for host-provided plugin GUIs
[ardour2.git] / gtk2_ardour / canvas-simpleline.c
blob576eedc369855e7e4de9423080360ebcbcc16f96
1 #include <stdio.h>
2 #include <math.h>
3 #include <cairo.h>
4 #include <libgnomecanvas/libgnomecanvas.h>
6 #include "canvas-simpleline.h"
7 #include "rgb_macros.h"
8 #include "gettext.h"
9 #define _(Text) dgettext (PACKAGE,Text)
11 enum {
12 PROP_0,
13 PROP_X1,
14 PROP_Y1,
15 PROP_X2,
16 PROP_Y2,
17 PROP_COLOR_RGBA
20 static void gnome_canvas_simpleline_class_init (GnomeCanvasSimpleLineClass *class);
22 static void gnome_canvas_simpleline_init (GnomeCanvasSimpleLine *simpleline);
24 static void gnome_canvas_simpleline_destroy (GtkObject *object);
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);
35 static void gnome_canvas_simpleline_update (GnomeCanvasItem *item,
36 double *affine,
37 ArtSVP *clip_path,
38 int flags);
40 static void gnome_canvas_simpleline_bounds (GnomeCanvasItem *item,
41 double *x1,
42 double *y1,
43 double *x2,
44 double *y2);
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);
53 static void gnome_canvas_simpleline_render (GnomeCanvasItem *item,
54 GnomeCanvasBuf *buf);
56 static void gnome_canvas_simpleline_draw (GnomeCanvasItem *item,
57 GdkDrawable *drawable,
58 int x,
59 int y,
60 int w,
61 int h);
63 static GnomeCanvasItemClass *parent_class;
66 GType
67 gnome_canvas_simpleline_get_type (void)
69 static GType simpleline_type;
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 */
85 simpleline_type = g_type_register_static (GNOME_TYPE_CANVAS_ITEM, "GnomeCanvasSimpleLine",
86 &object_info, 0);
89 return simpleline_type;
92 static void
93 gnome_canvas_simpleline_class_init (GnomeCanvasSimpleLineClass *class)
95 GObjectClass *gobject_class;
96 GtkObjectClass *object_class;
97 GnomeCanvasItemClass *item_class;
99 gobject_class = (GObjectClass *) class;
100 object_class = (GtkObjectClass *) class;
101 item_class = (GnomeCanvasItemClass *) class;
103 parent_class = g_type_class_peek_parent (class);
105 gobject_class->set_property = gnome_canvas_simpleline_set_property;
106 gobject_class->get_property = gnome_canvas_simpleline_get_property;
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));
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));
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));
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"),
154 G_MAXUINT,
156 G_PARAM_READWRITE));
158 object_class->destroy = gnome_canvas_simpleline_destroy;
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;
167 static void
168 gnome_canvas_simpleline_init (GnomeCanvasSimpleLine *simpleline)
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 simpleline->horizontal = TRUE; /* reset in the _update() method */
178 static void
179 gnome_canvas_simpleline_destroy (GtkObject *object)
181 GnomeCanvasSimpleLine *line;
183 g_return_if_fail (object != NULL);
184 g_return_if_fail (GNOME_IS_CANVAS_SIMPLELINE (object));
186 line = GNOME_CANVAS_SIMPLELINE (object);
188 if (GTK_OBJECT_CLASS (parent_class)->destroy)
189 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
192 static void
193 gnome_canvas_simpleline_bounds_world (GnomeCanvasItem *item, int* ix1, int* iy1, int* ix2, int* iy2)
195 double x1, x2, y1, y2;
196 ArtPoint i1, i2;
197 ArtPoint w1, w2;
198 double i2w[6];
199 GnomeCanvasSimpleLine *simpleline = GNOME_CANVAS_SIMPLELINE(item);
201 gnome_canvas_simpleline_bounds (item, &x1, &y1, &x2, &y2);
203 i1.x = x1;
204 i1.y = y1;
205 i2.x = x2;
206 i2.y = y2;
208 gnome_canvas_item_i2w_affine (item, i2w);
209 art_affine_point (&w1, &i1, i2w);
210 art_affine_point (&w2, &i2, i2w);
212 *ix1 = (int) rint(w1.x);
213 *ix2 = (int) rint(w2.x);
214 *iy1 = (int) rint(w1.y);
215 *iy2 = (int) rint(w2.y);
217 /* the update rect has to be of non-zero width and height */
219 if (x1 == x2) {
220 simpleline->horizontal = FALSE;
221 *ix2 += 1;
222 } else {
223 simpleline->horizontal = TRUE;
224 *iy2 += 1;
228 static void
229 gnome_canvas_simpleline_reset_bounds (GnomeCanvasItem *item)
231 int Ix1, Ix2, Iy1, Iy2;
233 gnome_canvas_simpleline_bounds_world (item, &Ix1, &Iy1, &Ix2, &Iy2);
234 gnome_canvas_update_bbox (item, Ix1, Iy1, Ix2, Iy2);
238 * CANVAS CALLBACKS
241 static void
242 gnome_canvas_simpleline_set_property (GObject *object,
243 guint prop_id,
244 const GValue *value,
245 GParamSpec *pspec)
248 GnomeCanvasSimpleLine *simpleline;
249 int update = FALSE;
250 int bounds_changed = FALSE;
252 g_return_if_fail (object != NULL);
253 g_return_if_fail (GNOME_IS_CANVAS_SIMPLELINE (object));
255 simpleline = GNOME_CANVAS_SIMPLELINE (object);
257 switch (prop_id) {
258 case PROP_X1:
259 if (simpleline->x1 != g_value_get_double (value)) {
260 simpleline->x1 = g_value_get_double (value);
261 bounds_changed = TRUE;
263 break;
265 case PROP_Y1:
266 if (simpleline->y1 != g_value_get_double (value)) {
267 simpleline->y1 = g_value_get_double (value);
268 bounds_changed = TRUE;
270 break;
272 case PROP_X2:
273 if (simpleline->x2 != g_value_get_double (value)) {
274 simpleline->x2 = g_value_get_double (value);
275 bounds_changed = TRUE;
277 break;
279 case PROP_Y2:
280 if (simpleline->y2 != g_value_get_double (value)) {
281 simpleline->y2 = g_value_get_double (value);
282 bounds_changed = TRUE;
284 break;
286 case PROP_COLOR_RGBA:
287 if (simpleline->color != g_value_get_uint(value)) {
288 simpleline->color = g_value_get_uint(value);
289 UINT_TO_RGBA (simpleline->color, &simpleline->r, &simpleline->g, &simpleline->b, &simpleline->a);
290 update = TRUE;
292 break;
293 default:
294 break;
297 if (update || bounds_changed) {
298 gnome_canvas_item_request_update (GNOME_CANVAS_ITEM(object));
302 static void
303 gnome_canvas_simpleline_get_property (GObject *object,
304 guint prop_id,
305 GValue *value,
306 GParamSpec *pspec)
308 g_return_if_fail (object != NULL);
309 g_return_if_fail (GNOME_IS_CANVAS_SIMPLELINE (object));
311 GnomeCanvasSimpleLine *line = GNOME_CANVAS_SIMPLELINE (object);
313 switch (prop_id) {
314 case PROP_X1:
315 g_value_set_double (value, line->x1);
316 break;
317 case PROP_X2:
318 g_value_set_double (value, line->x2);
319 break;
320 case PROP_Y1:
321 g_value_set_double (value, line->y1);
322 break;
323 case PROP_Y2:
324 g_value_set_double (value, line->y2);
325 break;
326 case PROP_COLOR_RGBA:
327 g_value_set_uint (value, line->color);
328 break;
329 default:
330 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
331 break;
335 static void
336 gnome_canvas_simpleline_update (GnomeCanvasItem *item, double *affine, ArtSVP *clip_path, int flags)
338 GnomeCanvasSimpleLine *simpleline;
339 double x;
340 double y;
342 simpleline = GNOME_CANVAS_SIMPLELINE (item);
344 if (parent_class->update)
345 (* parent_class->update) (item, affine, clip_path, flags);
347 gnome_canvas_simpleline_reset_bounds (item);
349 x = simpleline->x1;
350 y = simpleline->y1;
352 gnome_canvas_item_i2w (item, &x, &y);
353 gnome_canvas_w2c (GNOME_CANVAS(item->canvas), x, y, &simpleline->bbox_ulx, &simpleline->bbox_uly);
355 x = simpleline->x2;
356 y = simpleline->y2;
358 gnome_canvas_item_i2w (item, &x, &y);
359 gnome_canvas_w2c (GNOME_CANVAS(item->canvas), x, y, &simpleline->bbox_lrx, &simpleline->bbox_lry);
362 static void
363 gnome_canvas_simpleline_render (GnomeCanvasItem *item,
364 GnomeCanvasBuf *buf)
366 GnomeCanvasSimpleLine *simpleline;
367 int end, begin;
369 simpleline = GNOME_CANVAS_SIMPLELINE (item);
371 if (parent_class->render) {
372 (*parent_class->render) (item, buf);
375 if (buf->is_bg) {
376 gnome_canvas_buf_ensure_buf (buf);
377 buf->is_bg = FALSE;
380 //begin = MAX(simpleline->bbox_ulx,buf->rect.x0);
381 //end = MIN(simpleline->bbox_lrx,buf->rect.x1);
383 begin = simpleline->bbox_ulx;
384 end = simpleline->bbox_lrx;
386 if (simpleline->color != 0) {
387 if (simpleline->horizontal) {
388 PAINT_HORIZA(buf, simpleline->r, simpleline->g, simpleline->b, simpleline->a,
389 begin, end, simpleline->bbox_uly);
390 } else {
391 PAINT_VERTA(buf, simpleline->r, simpleline->g, simpleline->b, simpleline->a,
392 begin, simpleline->bbox_uly, simpleline->bbox_lry);
397 static void
398 gnome_canvas_simpleline_draw (GnomeCanvasItem *item,
399 GdkDrawable *drawable,
400 int x, int y,
401 int width, int height)
403 GnomeCanvasSimpleLine *simpleline;
404 cairo_t* cr;
405 double ulx;
406 double uly;
407 double lrx;
408 double lry;
410 simpleline = GNOME_CANVAS_SIMPLELINE (item);
412 cr = gdk_cairo_create (drawable);
414 if (x > simpleline->bbox_ulx) {
415 ulx = x;
416 } else {
417 ulx = simpleline->bbox_ulx;
420 if (y > simpleline->bbox_uly) {
421 uly = y;
422 } else {
423 uly = simpleline->bbox_uly;
426 if (x + width > simpleline->bbox_lrx) {
427 lrx = simpleline->bbox_lrx;
428 } else {
429 lrx = x + width;
432 if (y + height > simpleline->bbox_lry) {
433 lry = simpleline->bbox_lry;
434 } else {
435 lry = y + height;
438 ulx -= x;
439 uly -= y;
440 lrx -= x;
441 lry -= y;
443 cairo_set_source_rgba (cr,
444 simpleline->r/255.0,
445 simpleline->g/255.0,
446 simpleline->b/255.0,
447 simpleline->a/255.0);
448 cairo_set_line_width (cr, 1);
449 cairo_move_to (cr, ulx+0.5, uly+0.5);
450 cairo_line_to (cr, lrx+0.5, lry+0.5);
451 cairo_stroke (cr);
454 static void
455 gnome_canvas_simpleline_bounds (GnomeCanvasItem *item, double *x1, double *y1, double *x2, double *y2)
457 GnomeCanvasSimpleLine *simpleline = GNOME_CANVAS_SIMPLELINE (item);
459 *x1 = simpleline->x1;
460 *y1 = simpleline->y1;
461 *x2 = simpleline->x2;
462 *y2 = simpleline->y2;
465 static double
466 gnome_canvas_simpleline_point (GnomeCanvasItem *item, double x, double y, int cx, int cy, GnomeCanvasItem **actual_item)
468 GnomeCanvasSimpleLine *simpleline;
469 double x1, y1, x2, y2;
470 double dx, dy;
472 simpleline = GNOME_CANVAS_SIMPLELINE (item);
474 *actual_item = item;
476 /* Find the bounds for the line plus its outline width */
478 gnome_canvas_simpleline_bounds (item, &x1, &y1, &x2, &y2);
480 /* Is point inside line */
482 if ((x >= x1) && (y >= y1) && (x <= x2) && (y <= y2)) {
483 return 0.0;
486 /* Point is outside line */
488 if (x < x1)
489 dx = x1 - x;
490 else if (x > x2)
491 dx = x - x2;
492 else
493 dx = 0.0;
495 if (y < y1)
496 dy = y1 - y;
497 else if (y > y2)
498 dy = y - y2;
499 else
500 dy = 0.0;
502 return sqrt (dx * dx + dy * dy);