summaryrefslogtreecommitdiff
path: root/gtk2_ardour/canvas-simpleline.c
blob: efff5034f95f4ca7070b8b337dec98e8404aca99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include <stdio.h>
#include <math.h>
#include <cairo.h>
#include <libgnomecanvas/libgnomecanvas.h>

#include "canvas-simpleline.h"
#include "rgb_macros.h"
#include "gettext.h"
#define _(Text)  dgettext (PACKAGE,Text)

enum {
	PROP_0,
	PROP_X1,
	PROP_Y1,
	PROP_X2,
	PROP_Y2,
	PROP_COLOR_RGBA
};

static void gnome_canvas_simpleline_class_init   (GnomeCanvasSimpleLineClass *class);

static void gnome_canvas_simpleline_init         (GnomeCanvasSimpleLine      *simpleline);

static void gnome_canvas_simpleline_destroy      (GtkObject            *object);

static void gnome_canvas_simpleline_set_property (GObject        *object,
						  guint           prop_id,
						  const GValue   *value,
						  GParamSpec     *pspec);
static void gnome_canvas_simpleline_get_property (GObject        *object,
						  guint           prop_id,
						  GValue         *value,
						  GParamSpec     *pspec);

static void   gnome_canvas_simpleline_update     (GnomeCanvasItem *item,
						  double          *affine,
						  ArtSVP          *clip_path,
						  int flags);

static void   gnome_canvas_simpleline_bounds     (GnomeCanvasItem *item,
						  double          *x1,
						  double          *y1,
						  double          *x2,
						  double          *y2);

static double gnome_canvas_simpleline_point      (GnomeCanvasItem  *item,
						  double            x,
						  double            y,
						  int               cx,
						  int               cy,
						  GnomeCanvasItem **actual_item);

static void   gnome_canvas_simpleline_render     (GnomeCanvasItem *item,
						  GnomeCanvasBuf  *buf);

static void   gnome_canvas_simpleline_draw       (GnomeCanvasItem *item,
						  GdkDrawable     *drawable,
						  int              x,
						  int              y,
						  int              w,
						  int              h);

static GnomeCanvasItemClass *parent_class;


GType
gnome_canvas_simpleline_get_type (void)
{
	static GType simpleline_type;

	if (!simpleline_type) {
		static const GTypeInfo object_info = {
			sizeof (GnomeCanvasSimpleLineClass),
			(GBaseInitFunc) NULL,
			(GBaseFinalizeFunc) NULL,
			(GClassInitFunc) gnome_canvas_simpleline_class_init,
			(GClassFinalizeFunc) NULL,
			NULL,			/* class_data */
			sizeof (GnomeCanvasSimpleLine),
			0,			/* n_preallocs */
			(GInstanceInitFunc) gnome_canvas_simpleline_init,
			NULL			/* value_table */
		};

		simpleline_type = g_type_register_static (GNOME_TYPE_CANVAS_ITEM, "GnomeCanvasSimpleLine",
							  &object_info, 0);
	}

	return simpleline_type;
}

static void
gnome_canvas_simpleline_class_init (GnomeCanvasSimpleLineClass *class)
{
        GObjectClass *gobject_class;
	GtkObjectClass *object_class;
	GnomeCanvasItemClass *item_class;

	gobject_class = (GObjectClass *) class;
	object_class = (GtkObjectClass *) class;
	item_class = (GnomeCanvasItemClass *) class;

	parent_class = g_type_class_peek_parent (class);

	gobject_class->set_property = gnome_canvas_simpleline_set_property;
	gobject_class->get_property = gnome_canvas_simpleline_get_property;

	g_object_class_install_property (gobject_class,
					 PROP_X1,
					 g_param_spec_double ("x1",
							      _("x1"),
							      _("x coordinate of upper left corner of rect"),
							      -G_MAXDOUBLE,
							      G_MAXDOUBLE,
							      0.0,
							      G_PARAM_READWRITE));

	g_object_class_install_property (gobject_class,
					 PROP_Y1,
					 g_param_spec_double ("y1",
							      _("y1"),
							      _("y coordinate of upper left corner of rect "),
							      -G_MAXDOUBLE,
							      G_MAXDOUBLE,
							      0.0,
							      G_PARAM_READWRITE));


	g_object_class_install_property (gobject_class,
					 PROP_X2,
					 g_param_spec_double ("x2",
							      _("x2"),
							      _("x coordinate of lower right corner of rect"),
							      -G_MAXDOUBLE,
							      G_MAXDOUBLE,
							      0.0,
							      G_PARAM_READWRITE));

	g_object_class_install_property (gobject_class,
					 PROP_Y2,
					 g_param_spec_double ("y2",
							      _("y2"),
							      _("y coordinate of lower right corner of rect "),
							      -G_MAXDOUBLE,
							      G_MAXDOUBLE,
							      0.0,
							      G_PARAM_READWRITE));
	g_object_class_install_property (gobject_class,
					 PROP_COLOR_RGBA,
					 g_param_spec_uint ("color_rgba",
							    _("color rgba"),
							    _("color of line"),
							    0,
							    G_MAXUINT,
							    0,
							    G_PARAM_READWRITE));

	object_class->destroy = gnome_canvas_simpleline_destroy;

	item_class->update = gnome_canvas_simpleline_update;
	item_class->bounds = gnome_canvas_simpleline_bounds;
	item_class->point = gnome_canvas_simpleline_point;
	item_class->render = gnome_canvas_simpleline_render;
	item_class->draw = gnome_canvas_simpleline_draw;
}

static void
gnome_canvas_simpleline_init (GnomeCanvasSimpleLine *simpleline)
{
	simpleline->x1 = 0.0;
	simpleline->y1 = 0.0;
	simpleline->x2 = 0.0;
	simpleline->y2 = 0.0;
	simpleline->color = RGBA_TO_UINT(98,123,174,241);
}

static void
gnome_canvas_simpleline_destroy (GtkObject *object)
{
	g_return_if_fail (object != NULL);
	g_return_if_fail (GNOME_IS_CANVAS_SIMPLELINE (object));

	if (GTK_OBJECT_CLASS (parent_class)->destroy)
		(* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
}

/*
 * CANVAS CALLBACKS
 */

static void
gnome_canvas_simpleline_set_property (GObject      *object,
				      guint         prop_id,
				      const GValue *value,
				      GParamSpec   *pspec)

{
	(void) pspec;

	GnomeCanvasSimpleLine *simpleline;
	int update = FALSE;
	int bounds_changed = FALSE;
        double d;

	g_return_if_fail (object != NULL);
	g_return_if_fail (GNOME_IS_CANVAS_SIMPLELINE (object));

	simpleline = GNOME_CANVAS_SIMPLELINE (object);

	switch (prop_id) {
	case PROP_X1:
                d = g_value_get_double (value);
	        if (simpleline->x1 != d) {
		        simpleline->x1 = d;
			bounds_changed = TRUE;
		}
		break;

	case PROP_Y1:
                d = g_value_get_double (value);
	        if (simpleline->y1 != d) {
		        simpleline->y1 = d;
			bounds_changed = TRUE;
		}
		break;

	case PROP_X2:
                d = g_value_get_double (value);
	        if (simpleline->x2 != d) {
		        simpleline->x2 = d;
			bounds_changed = TRUE;
		}
		break;

	case PROP_Y2:
                d = g_value_get_double (value);
	        if (simpleline->y2 != d) {
		        simpleline->y2 = d;
			bounds_changed = TRUE;
		}
		break;

	case PROP_COLOR_RGBA:
		if (simpleline->color != g_value_get_uint(value)) {
		        simpleline->color = g_value_get_uint(value);
			UINT_TO_RGBA (simpleline->color, &simpleline->r, &simpleline->g, &simpleline->b, &simpleline->a);
		        update = TRUE;
		}
		break;
	default:
		break;
	}

	if (update || bounds_changed) {
		gnome_canvas_item_request_update (GNOME_CANVAS_ITEM(object));
	}
}

static void
gnome_canvas_simpleline_get_property (GObject      *object,
				      guint         prop_id,
				      GValue       *value,
				      GParamSpec   *pspec)
{
        g_return_if_fail (object != NULL);
        g_return_if_fail (GNOME_IS_CANVAS_SIMPLELINE (object));

	GnomeCanvasSimpleLine *line = GNOME_CANVAS_SIMPLELINE (object);

	switch (prop_id) {
	case PROP_X1:
		g_value_set_double (value, line->x1);
		break;
	case PROP_X2:
		g_value_set_double (value, line->x2);
		break;
	case PROP_Y1:
		g_value_set_double (value, line->y1);
		break;
	case PROP_Y2:
		g_value_set_double (value, line->y2);
		break;
	case PROP_COLOR_RGBA:
		g_value_set_uint (value, line->color);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
gnome_canvas_simpleline_update (GnomeCanvasItem *item, double *affine, ArtSVP *clip_path, int flags)
{
	GnomeCanvasSimpleLine *simpleline;
        double x1, x2, y1, y2;

	simpleline = GNOME_CANVAS_SIMPLELINE (item);

	if (parent_class->update)
		(* parent_class->update) (item, affine, clip_path, flags);

        /* redraw old location */

        gnome_canvas_request_redraw (item->canvas, item->x1, item->y1, item->x2, item->y2);

        /* get current bounding box in parent-relative world coordinates */

        gnome_canvas_simpleline_bounds (item, &x1, &y1, &x2, &y2);

        /* convert parent-relative item coordinates to world coordinates */

        gnome_canvas_item_i2w (item, &x1, &y1);
        gnome_canvas_item_i2w (item, &x2, &y2);

        /* don't suffer from rounding errors */

        x1 = floor (x1);
        y1 = floor (y1);
        x2 = ceil (x2);
        y2 = ceil (y2);

        /* force non-zero dimensionality for both axes */

        if (x1 == x2) {
                x2 += 1.0;
        }

        if (y1 == y2) {
                y2 += 1.0;
        }

        /* reset item bounding box (canvas coordinates, so integral. but stored in doubles) */

        gnome_canvas_w2c_d (GNOME_CANVAS(item->canvas), x1, y1, &item->x1, &item->y1);
        gnome_canvas_w2c_d (GNOME_CANVAS(item->canvas), x2, y2, &item->x2, &item->y2);

        /* redraw new location */

        gnome_canvas_request_redraw (item->canvas, item->x1, item->y1, item->x2, item->y2);

        /* store actual line coords as canvas coordinates for use in render() */

        x1 = simpleline->x1;
        y1 = simpleline->y1;
        x2 = simpleline->x2;
        y2 = simpleline->y2;
        /* convert to world */
        gnome_canvas_item_i2w (item, &x1, &y1);
        gnome_canvas_item_i2w (item, &x2, &y2);
        /* avoid rounding errors */
        x1 = (int) floor (item->x1);
        y1 = (int) floor (item->y1);
        x2 = (int) ceil (item->x2);
        y2 = (int) ceil (item->y2);
        /* convert to canvas coordinates, integral, stored in integers */
        gnome_canvas_w2c (GNOME_CANVAS(item->canvas), x1, y1, &simpleline->cx1, &simpleline->cy1);
        gnome_canvas_w2c (GNOME_CANVAS(item->canvas), x2, y2, &simpleline->cx2, &simpleline->cy2);
}

static void
gnome_canvas_simpleline_render (GnomeCanvasItem *item,
                                GnomeCanvasBuf *buf)
{
	GnomeCanvasSimpleLine *simpleline;
	int x1, x2;
        int y1, y2;

	simpleline = GNOME_CANVAS_SIMPLELINE (item);

	x1 = simpleline->cx1;
	x2 = simpleline->cx2;
        y1 = simpleline->cy1;

	if (buf->is_bg) {
		gnome_canvas_buf_ensure_buf (buf);
		buf->is_bg = FALSE;
	}

        if (simpleline->x1 != simpleline->x2) {
                PAINT_HORIZA(buf, simpleline->r, simpleline->g, simpleline->b, simpleline->a,
                             x1, x2, y1);
        } else {
                y2 = simpleline->cy2;
                PAINT_VERTA (buf, simpleline->r, simpleline->g, simpleline->b, simpleline->a,
                             x1, y1, y2);

        }
}

static void
gnome_canvas_simpleline_draw (GnomeCanvasItem* canvas,
                              GdkDrawable* drawable,
                              int x, int y,
                              int width, int height)
{
        /* XXX not implemented */
}

static void
gnome_canvas_simpleline_bounds (GnomeCanvasItem *item, double *x1, double *y1, double *x2, double *y2)
{
	GnomeCanvasSimpleLine *simpleline = GNOME_CANVAS_SIMPLELINE (item);

        *x1 = simpleline->x1;
        *y1 = simpleline->y1;
        *x2 = simpleline->x1;
        *y2 = simpleline->y2;
}

static double
gnome_canvas_simpleline_point (GnomeCanvasItem *item, double x, double y, int cx, int cy, GnomeCanvasItem **actual_item)
{
	(void) cx;
	(void) cy;

	double x1, y1, x2, y2;
	double dx, dy;

	*actual_item = item;

	/* Find the bounds for the line */

	gnome_canvas_simpleline_bounds (item, &x1, &y1, &x2, &y2);

	/* Is point inside line */

	if ((x >= x1) && (y >= y1) && (x <= x2) && (y <= y2)) {
		return 0.0;
	}
	/* Point is outside line */

	if (x < x1)
		dx = x1 - x;
	else if (x > x2)
		dx = x - x2;
	else
		dx = 0.0;

	if (y < y1)
		dy = y1 - y;
	else if (y > y2)
		dy = y - y2;
	else
		dy = 0.0;

	return sqrt (dx * dx + dy * dy);
}