Add a couple of missing attach_buffers() calls after _ports has been changed. I...
[ardour2.git] / gtk2_ardour / canvas-simpleline.c
blob887f71b411e240de6a74c4003b7131ef21e76079
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);
177 static void
178 gnome_canvas_simpleline_destroy (GtkObject *object)
180 g_return_if_fail (object != NULL);
181 g_return_if_fail (GNOME_IS_CANVAS_SIMPLELINE (object));
183 if (GTK_OBJECT_CLASS (parent_class)->destroy)
184 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
188 * CANVAS CALLBACKS
191 static void
192 gnome_canvas_simpleline_set_property (GObject *object,
193 guint prop_id,
194 const GValue *value,
195 GParamSpec *pspec)
198 (void) pspec;
200 GnomeCanvasSimpleLine *simpleline;
201 int update = FALSE;
202 int bounds_changed = FALSE;
203 double d;
205 g_return_if_fail (object != NULL);
206 g_return_if_fail (GNOME_IS_CANVAS_SIMPLELINE (object));
208 simpleline = GNOME_CANVAS_SIMPLELINE (object);
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;
217 break;
219 case PROP_Y1:
220 d = g_value_get_double (value);
221 if (simpleline->y1 != d) {
222 simpleline->y1 = d;
223 bounds_changed = TRUE;
225 break;
227 case PROP_X2:
228 d = g_value_get_double (value);
229 if (simpleline->x2 != d) {
230 simpleline->x2 = d;
231 bounds_changed = TRUE;
233 break;
235 case PROP_Y2:
236 d = g_value_get_double (value);
237 if (simpleline->y2 != d) {
238 simpleline->y2 = d;
239 bounds_changed = TRUE;
241 break;
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;
249 break;
250 default:
251 break;
254 if (update || bounds_changed) {
255 gnome_canvas_item_request_update (GNOME_CANVAS_ITEM(object));
259 static void
260 gnome_canvas_simpleline_get_property (GObject *object,
261 guint prop_id,
262 GValue *value,
263 GParamSpec *pspec)
265 g_return_if_fail (object != NULL);
266 g_return_if_fail (GNOME_IS_CANVAS_SIMPLELINE (object));
268 GnomeCanvasSimpleLine *line = GNOME_CANVAS_SIMPLELINE (object);
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;
292 static void
293 gnome_canvas_simpleline_update (GnomeCanvasItem *item, double *affine, ArtSVP *clip_path, int flags)
295 GnomeCanvasSimpleLine *simpleline;
296 double x1, x2, y1, y2;
298 simpleline = GNOME_CANVAS_SIMPLELINE (item);
300 if (parent_class->update)
301 (* parent_class->update) (item, affine, clip_path, flags);
303 /* redraw old location */
305 gnome_canvas_request_redraw (item->canvas, item->x1, item->y1, item->x2, item->y2);
307 /* get current bounding box in parent-relative world coordinates */
309 gnome_canvas_simpleline_bounds (item, &x1, &y1, &x2, &y2);
311 /* convert parent-relative item coordinates to world coordinates */
313 gnome_canvas_item_i2w (item, &x1, &y1);
314 gnome_canvas_item_i2w (item, &x2, &y2);
316 /* don't suffer from rounding errors */
318 x1 = floor (x1);
319 y1 = floor (y1);
320 x2 = ceil (x2);
321 y2 = ceil (y2);
323 /* force non-zero dimensionality for both axes */
325 if (x1 == x2) {
326 x2 += 1.0;
329 if (y1 == y2) {
330 y2 += 1.0;
333 /* reset item bounding box (canvas coordinates, so integral. but stored in doubles) */
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);
338 /* redraw new location */
340 gnome_canvas_request_redraw (item->canvas, item->x1, item->y1, item->x2, item->y2);
342 /* store actual line coords as canvas coordinates for use in render() */
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);
361 static void
362 gnome_canvas_simpleline_render (GnomeCanvasItem *item,
363 GnomeCanvasBuf *buf)
365 GnomeCanvasSimpleLine *simpleline;
366 int x1, x2;
367 int y1, y2;
369 simpleline = GNOME_CANVAS_SIMPLELINE (item);
371 x1 = simpleline->cx1;
372 x2 = simpleline->cx2;
373 y1 = simpleline->cy1;
375 if (buf->is_bg) {
376 gnome_canvas_buf_ensure_buf (buf);
377 buf->is_bg = FALSE;
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);
391 static void
392 gnome_canvas_simpleline_draw (GnomeCanvasItem *item,
393 GdkDrawable *drawable,
394 int x, int y,
395 int width, int height)
397 /* XXX not implemented */
400 static void
401 gnome_canvas_simpleline_bounds (GnomeCanvasItem *item, double *x1, double *y1, double *x2, double *y2)
403 GnomeCanvasSimpleLine *simpleline = GNOME_CANVAS_SIMPLELINE (item);
405 *x1 = simpleline->x1;
406 *y1 = simpleline->y1;
407 *x2 = simpleline->x1;
408 *y2 = simpleline->y2;
411 static double
412 gnome_canvas_simpleline_point (GnomeCanvasItem *item, double x, double y, int cx, int cy, GnomeCanvasItem **actual_item)
414 (void) cx;
415 (void) cy;
417 GnomeCanvasSimpleLine *simpleline;
418 double x1, y1, x2, y2;
419 double dx, dy;
421 *actual_item = item;
423 /* Find the bounds for the line */
425 gnome_canvas_simpleline_bounds (item, &x1, &y1, &x2, &y2);
427 /* Is point inside line */
429 if ((x >= x1) && (y >= y1) && (x <= x2) && (y <= y2)) {
430 return 0.0;
432 /* Point is outside line */
434 if (x < x1)
435 dx = x1 - x;
436 else if (x > x2)
437 dx = x - x2;
438 else
439 dx = 0.0;
441 if (y < y1)
442 dy = y1 - y;
443 else if (y > y2)
444 dy = y - y2;
445 else
446 dy = 0.0;
448 return sqrt (dx * dx + dy * dy);