fix up file renaming code a little bit
[ArdourMidi.git] / gtk2_ardour / canvas-simpleline.c
blob7bff05487e9c8121654a8dc8e4dd9247c345849a
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 (void) pspec;
250 GnomeCanvasSimpleLine *simpleline;
251 int update = FALSE;
252 int bounds_changed = FALSE;
254 g_return_if_fail (object != NULL);
255 g_return_if_fail (GNOME_IS_CANVAS_SIMPLELINE (object));
257 simpleline = GNOME_CANVAS_SIMPLELINE (object);
259 switch (prop_id) {
260 case PROP_X1:
261 if (simpleline->x1 != g_value_get_double (value)) {
262 simpleline->x1 = g_value_get_double (value);
263 bounds_changed = TRUE;
265 break;
267 case PROP_Y1:
268 if (simpleline->y1 != g_value_get_double (value)) {
269 simpleline->y1 = g_value_get_double (value);
270 bounds_changed = TRUE;
272 break;
274 case PROP_X2:
275 if (simpleline->x2 != g_value_get_double (value)) {
276 simpleline->x2 = g_value_get_double (value);
277 bounds_changed = TRUE;
279 break;
281 case PROP_Y2:
282 if (simpleline->y2 != g_value_get_double (value)) {
283 simpleline->y2 = g_value_get_double (value);
284 bounds_changed = TRUE;
286 break;
288 case PROP_COLOR_RGBA:
289 if (simpleline->color != g_value_get_uint(value)) {
290 simpleline->color = g_value_get_uint(value);
291 UINT_TO_RGBA (simpleline->color, &simpleline->r, &simpleline->g, &simpleline->b, &simpleline->a);
292 update = TRUE;
294 break;
295 default:
296 break;
299 if (update || bounds_changed) {
300 gnome_canvas_item_request_update (GNOME_CANVAS_ITEM(object));
304 static void
305 gnome_canvas_simpleline_get_property (GObject *object,
306 guint prop_id,
307 GValue *value,
308 GParamSpec *pspec)
310 g_return_if_fail (object != NULL);
311 g_return_if_fail (GNOME_IS_CANVAS_SIMPLELINE (object));
313 GnomeCanvasSimpleLine *line = GNOME_CANVAS_SIMPLELINE (object);
315 switch (prop_id) {
316 case PROP_X1:
317 g_value_set_double (value, line->x1);
318 break;
319 case PROP_X2:
320 g_value_set_double (value, line->x2);
321 break;
322 case PROP_Y1:
323 g_value_set_double (value, line->y1);
324 break;
325 case PROP_Y2:
326 g_value_set_double (value, line->y2);
327 break;
328 case PROP_COLOR_RGBA:
329 g_value_set_uint (value, line->color);
330 break;
331 default:
332 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
333 break;
337 static void
338 gnome_canvas_simpleline_update (GnomeCanvasItem *item, double *affine, ArtSVP *clip_path, int flags)
340 GnomeCanvasSimpleLine *simpleline;
341 double x;
342 double y;
344 simpleline = GNOME_CANVAS_SIMPLELINE (item);
346 if (parent_class->update)
347 (* parent_class->update) (item, affine, clip_path, flags);
349 gnome_canvas_simpleline_reset_bounds (item);
351 x = simpleline->x1;
352 y = simpleline->y1;
354 gnome_canvas_item_i2w (item, &x, &y);
355 gnome_canvas_w2c (GNOME_CANVAS(item->canvas), x, y, &simpleline->bbox_ulx, &simpleline->bbox_uly);
357 x = simpleline->x2;
358 y = simpleline->y2;
360 gnome_canvas_item_i2w (item, &x, &y);
361 gnome_canvas_w2c (GNOME_CANVAS(item->canvas), x, y, &simpleline->bbox_lrx, &simpleline->bbox_lry);
364 static void
365 gnome_canvas_simpleline_render (GnomeCanvasItem *item,
366 GnomeCanvasBuf *buf)
368 GnomeCanvasSimpleLine *simpleline;
369 int end, begin;
371 simpleline = GNOME_CANVAS_SIMPLELINE (item);
373 if (parent_class->render) {
374 (*parent_class->render) (item, buf);
377 if (buf->is_bg) {
378 gnome_canvas_buf_ensure_buf (buf);
379 buf->is_bg = FALSE;
382 //begin = MAX(simpleline->bbox_ulx,buf->rect.x0);
383 //end = MIN(simpleline->bbox_lrx,buf->rect.x1);
385 begin = simpleline->bbox_ulx;
386 end = simpleline->bbox_lrx;
388 if (simpleline->color != 0) {
389 if (simpleline->horizontal) {
390 PAINT_HORIZA(buf, simpleline->r, simpleline->g, simpleline->b, simpleline->a,
391 begin, end, simpleline->bbox_uly);
392 } else {
393 PAINT_VERTA(buf, simpleline->r, simpleline->g, simpleline->b, simpleline->a,
394 begin, simpleline->bbox_uly, simpleline->bbox_lry);
399 static void
400 gnome_canvas_simpleline_draw (GnomeCanvasItem *item,
401 GdkDrawable *drawable,
402 int x, int y,
403 int width, int height)
405 GnomeCanvasSimpleLine *simpleline;
406 cairo_t* cr;
407 double ulx;
408 double uly;
409 double lrx;
410 double lry;
412 simpleline = GNOME_CANVAS_SIMPLELINE (item);
414 cr = gdk_cairo_create (drawable);
416 if (x > simpleline->bbox_ulx) {
417 ulx = x;
418 } else {
419 ulx = simpleline->bbox_ulx;
422 if (y > simpleline->bbox_uly) {
423 uly = y;
424 } else {
425 uly = simpleline->bbox_uly;
428 if (x + width > simpleline->bbox_lrx) {
429 lrx = simpleline->bbox_lrx;
430 } else {
431 lrx = x + width;
434 if (y + height > simpleline->bbox_lry) {
435 lry = simpleline->bbox_lry;
436 } else {
437 lry = y + height;
440 ulx -= x;
441 uly -= y;
442 lrx -= x;
443 lry -= y;
445 cairo_set_source_rgba (cr,
446 simpleline->r/255.0,
447 simpleline->g/255.0,
448 simpleline->b/255.0,
449 simpleline->a/255.0);
450 cairo_set_line_width (cr, 1);
451 cairo_move_to (cr, ulx+0.5, uly+0.5);
452 cairo_line_to (cr, lrx+0.5, lry+0.5);
453 cairo_stroke (cr);
456 static void
457 gnome_canvas_simpleline_bounds (GnomeCanvasItem *item, double *x1, double *y1, double *x2, double *y2)
459 GnomeCanvasSimpleLine *simpleline = GNOME_CANVAS_SIMPLELINE (item);
461 *x1 = simpleline->x1;
462 *y1 = simpleline->y1;
463 *x2 = simpleline->x2;
464 *y2 = simpleline->y2;
467 static double
468 gnome_canvas_simpleline_point (GnomeCanvasItem *item, double x, double y, int cx, int cy, GnomeCanvasItem **actual_item)
470 (void) cx;
471 (void) cy;
473 GnomeCanvasSimpleLine *simpleline;
474 double x1, y1, x2, y2;
475 double dx, dy;
477 simpleline = GNOME_CANVAS_SIMPLELINE (item);
479 *actual_item = item;
481 /* Find the bounds for the line plus its outline width */
483 gnome_canvas_simpleline_bounds (item, &x1, &y1, &x2, &y2);
485 /* Is point inside line */
487 if ((x >= x1) && (y >= y1) && (x <= x2) && (y <= y2)) {
488 return 0.0;
491 /* Point is outside line */
493 if (x < x1)
494 dx = x1 - x;
495 else if (x > x2)
496 dx = x - x2;
497 else
498 dx = 0.0;
500 if (y < y1)
501 dy = y1 - y;
502 else if (y > y2)
503 dy = y - y2;
504 else
505 dy = 0.0;
507 return sqrt (dx * dx + dy * dy);