3 #include <libgnomecanvas/libgnomecanvas.h>
5 #include "canvas-simpleline.h"
6 #include "rgb_macros.h"
8 #define _(Text) dgettext (PACKAGE,Text)
19 static void gnome_canvas_simpleline_class_init (GnomeCanvasSimpleLineClass *class);
21 static void gnome_canvas_simpleline_init (GnomeCanvasSimpleLine *simpleline);
23 static void gnome_canvas_simpleline_destroy (GtkObject *object);
25 static void gnome_canvas_simpleline_set_property (GObject *object,
29 static void gnome_canvas_simpleline_get_property (GObject *object,
34 static void gnome_canvas_simpleline_update (GnomeCanvasItem *item,
39 static void gnome_canvas_simpleline_bounds (GnomeCanvasItem *item,
45 static double gnome_canvas_simpleline_point (GnomeCanvasItem *item,
50 GnomeCanvasItem **actual_item);
52 static void gnome_canvas_simpleline_render (GnomeCanvasItem *item,
55 static void gnome_canvas_simpleline_draw (GnomeCanvasItem *item,
56 GdkDrawable *drawable,
62 static GnomeCanvasItemClass *parent_class;
66 gnome_canvas_simpleline_get_type (void)
68 static GType simpleline_type;
70 if (!simpleline_type) {
71 static const GTypeInfo object_info = {
72 sizeof (GnomeCanvasSimpleLineClass),
74 (GBaseFinalizeFunc) NULL,
75 (GClassInitFunc) gnome_canvas_simpleline_class_init,
76 (GClassFinalizeFunc) NULL,
77 NULL, /* class_data */
78 sizeof (GnomeCanvasSimpleLine),
80 (GInstanceInitFunc) gnome_canvas_simpleline_init,
81 NULL /* value_table */
84 simpleline_type = g_type_register_static (GNOME_TYPE_CANVAS_ITEM, "GnomeCanvasSimpleLine",
88 return simpleline_type;
92 gnome_canvas_simpleline_class_init (GnomeCanvasSimpleLineClass *class)
94 GObjectClass *gobject_class;
95 GtkObjectClass *object_class;
96 GnomeCanvasItemClass *item_class;
98 gobject_class = (GObjectClass *) class;
99 object_class = (GtkObjectClass *) class;
100 item_class = (GnomeCanvasItemClass *) class;
102 parent_class = g_type_class_peek_parent (class);
104 gobject_class->set_property = gnome_canvas_simpleline_set_property;
105 gobject_class->get_property = gnome_canvas_simpleline_get_property;
107 g_object_class_install_property (gobject_class,
109 g_param_spec_double ("x1",
111 _("x coordinate of upper left corner of rect"),
117 g_object_class_install_property (gobject_class,
119 g_param_spec_double ("y1",
121 _("y coordinate of upper left corner of rect "),
128 g_object_class_install_property (gobject_class,
130 g_param_spec_double ("x2",
132 _("x coordinate of lower right corner of rect"),
138 g_object_class_install_property (gobject_class,
140 g_param_spec_double ("y2",
142 _("y coordinate of lower right corner of rect "),
147 g_object_class_install_property (gobject_class,
149 g_param_spec_uint ("color_rgba",
157 object_class->destroy = gnome_canvas_simpleline_destroy;
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;
167 gnome_canvas_simpleline_init (GnomeCanvasSimpleLine *simpleline)
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 */
176 // GNOME_CANVAS_ITEM(simpleline)->object.flags |= GNOME_CANVAS_ITEM_NO_AUTO_REDRAW;
180 gnome_canvas_simpleline_destroy (GtkObject *object)
182 GnomeCanvasSimpleLine *line;
184 g_return_if_fail (object != NULL);
185 g_return_if_fail (GNOME_IS_CANVAS_LINE (object));
187 line = GNOME_CANVAS_SIMPLELINE (object);
189 if (GTK_OBJECT_CLASS (parent_class)->destroy)
190 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
194 gnome_canvas_simpleline_bounds_world (GnomeCanvasItem *item, int* ix1, int* iy1, int* ix2, int* iy2)
196 double x1, x2, y1, y2;
200 GnomeCanvasSimpleLine *simpleline = GNOME_CANVAS_SIMPLELINE(item);
202 gnome_canvas_simpleline_bounds (item, &x1, &y1, &x2, &y2);
209 gnome_canvas_item_i2w_affine (item, i2w);
210 art_affine_point (&w1, &i1, i2w);
211 art_affine_point (&w2, &i2, i2w);
213 *ix1 = (int) rint(w1.x);
214 *ix2 = (int) rint(w2.x);
215 *iy1 = (int) rint(w1.y);
216 *iy2 = (int) rint(w2.y);
218 /* the update rect has to be of non-zero width and height */
221 simpleline->horizontal = FALSE;
224 simpleline->horizontal = TRUE;
230 gnome_canvas_simpleline_reset_bounds (GnomeCanvasItem *item)
232 int Ix1, Ix2, Iy1, Iy2;
234 gnome_canvas_simpleline_bounds_world (item, &Ix1, &Iy1, &Ix2, &Iy2);
235 gnome_canvas_update_bbox (item, Ix1, Iy1, Ix2, Iy2);
243 gnome_canvas_simpleline_set_property (GObject *object,
249 GnomeCanvasSimpleLine *simpleline;
251 int bounds_changed = FALSE;
253 g_return_if_fail (object != NULL);
254 g_return_if_fail (GNOME_IS_CANVAS_SIMPLELINE (object));
256 simpleline = GNOME_CANVAS_SIMPLELINE (object);
260 if (simpleline->x1 != g_value_get_double (value)) {
261 simpleline->x1 = g_value_get_double (value);
262 bounds_changed = TRUE;
267 if (simpleline->y1 != g_value_get_double (value)) {
268 simpleline->y1 = g_value_get_double (value);
269 bounds_changed = TRUE;
274 if (simpleline->x2 != g_value_get_double (value)) {
275 simpleline->x2 = g_value_get_double (value);
276 bounds_changed = TRUE;
281 if (simpleline->y2 != g_value_get_double (value)) {
282 simpleline->y2 = g_value_get_double (value);
283 bounds_changed = TRUE;
287 case PROP_COLOR_RGBA:
288 if (simpleline->color != g_value_get_uint(value)) {
289 simpleline->color = g_value_get_uint(value);
297 if (update || bounds_changed) {
298 gnome_canvas_item_request_update (GNOME_CANVAS_ITEM(object));
303 gnome_canvas_simpleline_get_property (GObject *object,
308 g_return_if_fail (object != NULL);
309 g_return_if_fail (GNOME_IS_CANVAS_SIMPLELINE (object));
311 GnomeCanvasSimpleLine *line = GNOME_CANVAS_SIMPLELINE (object);
315 g_value_set_double (value, line->x1);
318 g_value_set_double (value, line->x2);
321 g_value_set_double (value, line->y1);
324 g_value_set_double (value, line->y2);
326 case PROP_COLOR_RGBA:
327 g_value_set_uint (value, line->color);
330 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
336 gnome_canvas_simpleline_update (GnomeCanvasItem *item, double *affine, ArtSVP *clip_path, int flags)
338 GnomeCanvasSimpleLine *simpleline;
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);
352 gnome_canvas_item_i2w (item, &x, &y);
353 gnome_canvas_w2c (GNOME_CANVAS(item->canvas), x, y, &simpleline->bbox_ulx, &simpleline->bbox_uly);
358 gnome_canvas_item_i2w (item, &x, &y);
359 gnome_canvas_w2c (GNOME_CANVAS(item->canvas), x, y, &simpleline->bbox_lrx, &simpleline->bbox_lry);
363 gnome_canvas_simpleline_render (GnomeCanvasItem *item,
366 GnomeCanvasSimpleLine *simpleline;
369 simpleline = GNOME_CANVAS_SIMPLELINE (item);
371 if (parent_class->render) {
372 (*parent_class->render) (item, buf);
376 gnome_canvas_buf_ensure_buf (buf);
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);
391 PAINT_VERTA(buf, simpleline->r, simpleline->g, simpleline->b, simpleline->a,
392 begin, simpleline->bbox_uly, simpleline->bbox_lry);
398 gnome_canvas_simpleline_draw (GnomeCanvasItem *item,
399 GdkDrawable *drawable,
401 int width, int height)
403 GnomeCanvasSimpleLine *simpleline;
405 simpleline = GNOME_CANVAS_SIMPLELINE (item);
407 if (parent_class->draw) {
408 (* parent_class->draw) (item, drawable, x, y, width, height);
411 fprintf (stderr, "please don't use the CanvasSimpleLine item in a non-aa Canvas\n");
416 gnome_canvas_simpleline_bounds (GnomeCanvasItem *item, double *x1, double *y1, double *x2, double *y2)
418 GnomeCanvasSimpleLine *simpleline = GNOME_CANVAS_SIMPLELINE (item);
420 *x1 = simpleline->x1;
421 *y1 = simpleline->y1;
422 *x2 = simpleline->x2;
423 *y2 = simpleline->y2;
427 gnome_canvas_simpleline_point (GnomeCanvasItem *item, double x, double y, int cx, int cy, GnomeCanvasItem **actual_item)
429 GnomeCanvasSimpleLine *simpleline;
430 double x1, y1, x2, y2;
433 simpleline = GNOME_CANVAS_SIMPLELINE (item);
437 /* Find the bounds for the rectangle plus its outline width */
439 gnome_canvas_simpleline_bounds (item, &x1, &y1, &x2, &y2);
441 /* Is point inside rectangle */
443 if ((x >= x1) && (y >= y1) && (x <= x2) && (y <= y2)) {
447 /* Point is outside rectangle */
463 return sqrt (dx * dx + dy * dy);