3 #include <libgnomecanvas/libgnomecanvas.h>
5 #include "canvas-simplerect.h"
6 #include "rgb_macros.h"
8 #define _(Text) dgettext (PACKAGE,Text)
20 PROP_OUTLINE_COLOR_RGBA,
25 static void gnome_canvas_simplerect_class_init (GnomeCanvasSimpleRectClass *class);
27 static void gnome_canvas_simplerect_init (GnomeCanvasSimpleRect *simplerect);
29 static void gnome_canvas_simplerect_destroy (GtkObject *object);
31 static void gnome_canvas_simplerect_set_property (GObject *object,
36 static void gnome_canvas_simplerect_get_property (GObject *object,
41 static void gnome_canvas_simplerect_update (GnomeCanvasItem *item,
46 static void gnome_canvas_simplerect_bounds (GnomeCanvasItem *item,
52 static double gnome_canvas_simplerect_point (GnomeCanvasItem *item, double x, double y, int cx, int cy, GnomeCanvasItem **actual_item);
54 static void gnome_canvas_simplerect_render (GnomeCanvasItem *item, GnomeCanvasBuf *buf);
56 static void gnome_canvas_simplerect_draw (GnomeCanvasItem *item, GdkDrawable *drawable, int x, int y, int w, int h);
58 static GnomeCanvasItemClass *parent_class;
62 gnome_canvas_simplerect_get_type (void)
64 static GType simplerect_type;
66 if (!simplerect_type) {
67 static const GTypeInfo object_info = {
68 sizeof (GnomeCanvasSimpleRectClass),
70 (GBaseFinalizeFunc) NULL,
71 (GClassInitFunc) gnome_canvas_simplerect_class_init,
72 (GClassFinalizeFunc) NULL,
73 NULL, /* class_data */
74 sizeof (GnomeCanvasSimpleRect),
76 (GInstanceInitFunc) gnome_canvas_simplerect_init,
77 NULL /* value_table */
80 simplerect_type = g_type_register_static (GNOME_TYPE_CANVAS_ITEM, "GnomeCanvasSimpleRect",
84 return simplerect_type;
88 gnome_canvas_simplerect_class_init (GnomeCanvasSimpleRectClass *class)
90 GObjectClass *gobject_class;
91 GtkObjectClass *object_class;
92 GnomeCanvasItemClass *item_class;
94 gobject_class = (GObjectClass *) class;
95 object_class = (GtkObjectClass *) class;
96 item_class = (GnomeCanvasItemClass *) class;
98 parent_class = g_type_class_peek_parent (class);
100 gobject_class->set_property = gnome_canvas_simplerect_set_property;
101 gobject_class->get_property = gnome_canvas_simplerect_get_property;
103 g_object_class_install_property (gobject_class,
105 g_param_spec_double ("x1",
107 _("x coordinate of upper left corner of rect"),
113 g_object_class_install_property (gobject_class,
115 g_param_spec_double ("y1",
117 _("y coordinate of upper left corner of rect "),
124 g_object_class_install_property (gobject_class,
126 g_param_spec_double ("x2",
128 _("x coordinate of lower right corner of rect"),
134 g_object_class_install_property (gobject_class,
136 g_param_spec_double ("y2",
138 _("y coordinate of lower right corner of rect "),
145 g_object_class_install_property (gobject_class,
147 g_param_spec_uint ("outline_pixels",
149 _("width in pixels of outline"),
156 g_object_class_install_property (gobject_class,
158 g_param_spec_uint ("outline_what",
160 _("which boundaries to outline (mask)"),
168 g_object_class_install_property (gobject_class,
170 g_param_spec_boolean ("fill",
176 g_object_class_install_property (gobject_class,
178 g_param_spec_boolean ("draw",
185 g_object_class_install_property (gobject_class,
186 PROP_OUTLINE_COLOR_RGBA,
187 g_param_spec_uint ("outline_color_rgba",
188 _("outline color rgba"),
189 _("color of outline"),
196 g_object_class_install_property (gobject_class,
197 PROP_FILL_COLOR_RGBA,
198 g_param_spec_uint ("fill_color_rgba",
199 _("fill color rgba"),
206 object_class->destroy = gnome_canvas_simplerect_destroy;
208 item_class->update = gnome_canvas_simplerect_update;
209 item_class->draw = gnome_canvas_simplerect_draw;
210 item_class->bounds = gnome_canvas_simplerect_bounds;
211 item_class->point = gnome_canvas_simplerect_point;
212 item_class->render = gnome_canvas_simplerect_render;
217 gnome_canvas_simplerect_init (GnomeCanvasSimpleRect *simplerect)
219 simplerect->x1 = 0.0;
220 simplerect->y1 = 0.0;
221 simplerect->x2 = 0.0;
222 simplerect->y2 = 0.0;
223 simplerect->fill = TRUE;
224 simplerect->draw = TRUE;
225 simplerect->full_draw_on_update = TRUE;
226 simplerect->fill_color = 0;
227 simplerect->outline_color = 0;
228 simplerect->outline_pixels = 1;
229 simplerect->outline_what = 0xf;
232 // GNOME_CANVAS_ITEM(simplerect)->object.flags |= GNOME_CANVAS_ITEM_NO_AUTO_REDRAW;
236 gnome_canvas_simplerect_destroy (GtkObject *object)
238 GnomeCanvasSimpleRect *rect;
240 g_return_if_fail (object != NULL);
241 g_return_if_fail (GNOME_IS_CANVAS_SIMPLERECT (object));
243 rect = GNOME_CANVAS_SIMPLERECT (object);
245 /* remember, destroy can be run multiple times! */
247 if (GTK_OBJECT_CLASS (parent_class)->destroy)
248 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
252 gnome_canvas_simplerect_bounds (GnomeCanvasItem *item, double *x1, double *y1, double *x2, double *y2)
254 GnomeCanvasSimpleRect *simplerect = GNOME_CANVAS_SIMPLERECT (item);
256 *x1 = simplerect->x1;
257 *y1 = simplerect->y1;
258 *x2 = simplerect->x2 + 1;
259 *y2 = simplerect->y2 + 1;
264 gnome_canvas_simplerect_reset_bounds (GnomeCanvasItem *item)
266 GnomeCanvasSimpleRect* simplerect;
267 double x1, x2, y1, y2;
268 double old_x1, old_x2, old_y1, old_y2;
276 gnome_canvas_simplerect_bounds (item, &x1, &y1, &x2, &y2);
277 gnome_canvas_item_i2w (item, &x1, &y1);
278 gnome_canvas_item_i2w (item, &x2, &y2);
285 /* now compute bounding box in canvas units */
287 simplerect = GNOME_CANVAS_SIMPLERECT (item);
289 gnome_canvas_w2c (GNOME_CANVAS(item->canvas), x1, y1, &simplerect->bbox_ulx, &simplerect->bbox_uly);
290 gnome_canvas_w2c (GNOME_CANVAS(item->canvas), x2, y2, &simplerect->bbox_lrx, &simplerect->bbox_lry);
292 /* now queue redraws for changed areas */
294 if (item->x1 != old_x1) {
296 /* left edge changed. redraw the area that altered */
298 a = MIN(item->x1, old_x1);
299 b = MAX(item->x1, old_x1);
300 gnome_canvas_request_redraw (item->canvas, a - 1, item->y1, b + 1, item->y2);
303 if (item->x2 != old_x2) {
305 /* right edge changed. redraw the area that altered */
307 a = MIN(item->x2, old_x2);
308 b = MAX(item->x2, old_x2);
309 gnome_canvas_request_redraw (item->canvas, a - 1, item->y1, b + 1, item->y2);
312 if (item->y1 != old_y1) {
314 /* top edge changed. redraw the area that altered */
316 a = MIN(item->y1, old_y1);
317 b = MAX(item->y1, old_y1);
318 gnome_canvas_request_redraw (item->canvas, item->x1, a - 1, item->x2, b + 1);
321 if (item->y2 != old_y2) {
323 /* lower edge changed. redraw the area that altered */
325 a = MIN(item->y2, old_y2);
326 b = MAX(item->y2, old_y2);
327 gnome_canvas_request_redraw (item->canvas, item->x1, a - 1, item->x2, b + 1);
336 gnome_canvas_simplerect_set_property (GObject *object,
342 GnomeCanvasSimpleRect *simplerect;
344 int bounds_changed = FALSE;
345 g_return_if_fail (object != NULL);
346 g_return_if_fail (GNOME_IS_CANVAS_SIMPLERECT (object));
348 simplerect = GNOME_CANVAS_SIMPLERECT (object);
352 if (simplerect->x1 != g_value_get_double (value)) {
353 simplerect->x1 = g_value_get_double (value);
354 bounds_changed = TRUE;
359 if (simplerect->y1 != g_value_get_double (value)) {
360 simplerect->y1 = g_value_get_double (value);
361 bounds_changed = TRUE;
366 if (simplerect->x2 != g_value_get_double (value)) {
367 simplerect->x2 = g_value_get_double (value);
368 bounds_changed = TRUE;
373 if (simplerect->y2 != g_value_get_double (value)) {
374 simplerect->y2 = g_value_get_double (value);
375 bounds_changed = TRUE;
380 if (simplerect->draw != g_value_get_boolean (value)) {
381 simplerect->draw = g_value_get_boolean (value);
388 if (simplerect->fill != g_value_get_boolean (value)) {
389 simplerect->fill = g_value_get_boolean (value);
394 case PROP_FILL_COLOR_RGBA:
395 if (simplerect->fill_color != g_value_get_uint(value)) {
396 simplerect->fill_color = g_value_get_uint(value);
401 case PROP_OUTLINE_COLOR_RGBA:
402 if (simplerect->outline_color != g_value_get_uint(value)) {
403 simplerect->outline_color = g_value_get_uint(value);
408 case PROP_OUTLINE_PIXELS:
409 if (simplerect->outline_pixels != g_value_get_uint(value)) {
410 simplerect->outline_pixels = g_value_get_uint(value);
415 case PROP_OUTLINE_WHAT:
416 if (simplerect->outline_what != g_value_get_uint(value)) {
417 simplerect->outline_what = g_value_get_uint(value);
426 simplerect->full_draw_on_update = update;
428 if (update || bounds_changed) {
429 gnome_canvas_item_request_update (GNOME_CANVAS_ITEM(object));
434 gnome_canvas_simplerect_get_property (GObject *object,
439 GnomeCanvasSimpleRect *rect = GNOME_CANVAS_SIMPLERECT (object);
441 g_return_if_fail (object != NULL);
442 g_return_if_fail (GNOME_IS_CANVAS_SIMPLERECT (object));
446 g_value_set_double (value, rect->x1);
449 g_value_set_double (value, rect->x2);
452 g_value_set_double (value, rect->y1);
455 g_value_set_double (value, rect->y2);
457 case PROP_OUTLINE_WHAT:
458 g_value_set_uint (value, rect->outline_what);
461 g_value_set_boolean (value, rect->fill);
463 case PROP_OUTLINE_PIXELS:
464 g_value_set_uint (value, rect->outline_pixels);
466 case PROP_FILL_COLOR_RGBA:
467 g_value_set_uint (value, rect->fill_color);
469 case PROP_OUTLINE_COLOR_RGBA:
470 g_value_set_uint (value, rect->outline_color);
473 g_value_set_boolean (value, rect->draw);
477 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
484 gnome_canvas_simplerect_update (GnomeCanvasItem *item, double *affine, ArtSVP *clip_path, int flags)
486 GnomeCanvasSimpleRect *simplerect;
489 simplerect = GNOME_CANVAS_SIMPLERECT (item);
491 if (parent_class->update)
492 (* parent_class->update) (item, affine, clip_path, flags);
494 gnome_canvas_simplerect_reset_bounds (item);
496 if (simplerect->full_draw_on_update) {
497 gnome_canvas_request_redraw (item->canvas,
498 simplerect->bbox_ulx,
499 simplerect->bbox_uly,
500 simplerect->bbox_lrx+1,
501 simplerect->bbox_lry+1);
502 simplerect->full_draw_on_update = FALSE;
505 UINT_TO_RGBA (simplerect->fill_color, &simplerect->fill_r, &simplerect->fill_g, &simplerect->fill_b, &simplerect->fill_a);
506 UINT_TO_RGBA (simplerect->outline_color, &simplerect->outline_r, &simplerect->outline_g, &simplerect->outline_b, &foo);
509 // this can be useful for debugging/understanding how the canvas redraws
512 #undef HARLEQUIN_DEBUGGING
514 #undef SIMPLERECT_FAST_RENDERER
515 #ifdef SIMPLERECT_FAST_RENDERER
518 gnome_canvas_simplerect_render (GnomeCanvasItem *item,
521 GnomeCanvasSimpleRect *simplerect;
525 ArtIRect intersection;
528 simplerect = GNOME_CANVAS_SIMPLERECT (item);
530 if (parent_class->render) {
531 (*parent_class->render) (item, buf);
536 #ifdef HARLEQUIN_DEBUGGING
537 gint randr, randg, randb;
538 randr = random() % 255;
539 randg = random() % 255;
540 randb = random() % 255;
541 PAINT_BOX(buf, randr, randg, randb, 255, buf->rect.x0, buf->rect.y0, buf->rect.x1, buf->rect.y1);
543 gnome_canvas_buf_ensure_buf (buf);
547 if (!simplerect->draw) {
551 self.x0 = simplerect->bbox_ulx;
552 self.y0 = simplerect->bbox_uly;
553 self.x1 = simplerect->bbox_lrx;
554 self.y1 = simplerect->bbox_lry;
556 art_irect_intersect (&intersection, &self, &buf->rect);
558 begin = MAX(simplerect->bbox_ulx, buf->rect.x0);
559 end = MIN((simplerect->bbox_lrx-1), buf->rect.x1);
561 sy = simplerect->bbox_uly;
562 ey = simplerect->bbox_lry-1;
564 if (simplerect->fill) {
566 // this can be useful for debugging/understanding how the canvas redraws
569 #ifdef HARLEQUIN_DEBUGGING
570 gint randr, randg, randb;
571 randr = random() % 255;
572 randg = random() % 255;
573 randb = random() % 255;
574 PAINT_BOX(buf, randr, randg, randb, simplerect->fill_a, begin, sy, end, ey);
576 FAST_PAINT_BOX (buf, simplerect->fill_r, simplerect->fill_g, simplerect->fill_b, simplerect->fill_a,
577 intersection.x0, intersection.y0,
578 intersection.x1, intersection.y1);
583 for (i = 0; i < simplerect->outline_pixels; ++i) {
585 if (simplerect->outline_what & 0x1) {
586 if (begin == simplerect->bbox_ulx) {
587 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin + i, sy, ey);
591 if (simplerect->outline_what & 0x2) {
592 if (end == (simplerect->bbox_lrx - 1)) {
593 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, end - i, sy, ey + 1);
597 if (simplerect->outline_what & 0x4) {
598 PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end, sy+i);
601 if (simplerect->outline_what & 0x8) {
602 PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end + 1, ey-i);
607 #else /* SIMPLERECT_FAST_RENDERER */
610 gnome_canvas_simplerect_render (GnomeCanvasItem *item,
613 GnomeCanvasSimpleRect *simplerect;
618 simplerect = GNOME_CANVAS_SIMPLERECT (item);
620 if (parent_class->render) {
621 (*parent_class->render) (item, buf);
626 #ifdef HARLEQUIN_DEBUGGING
627 gint randr, randg, randb;
628 randr = random() % 255;
629 randg = random() % 255;
630 randb = random() % 255;
631 PAINT_BOX(buf, randr, randg, randb, 255, buf->rect.x0, buf->rect.y0, buf->rect.x1, buf->rect.y1);
633 gnome_canvas_buf_ensure_buf (buf);
637 if (!simplerect->draw) {
641 begin = MAX(simplerect->bbox_ulx,buf->rect.x0);
642 end = MIN((simplerect->bbox_lrx-1),buf->rect.x1);
644 sy = simplerect->bbox_uly;
645 ey = simplerect->bbox_lry-1;
647 if (simplerect->fill) {
649 #ifdef HARLEQUIN_DEBUGGING
650 gint randr, randg, randb;
651 randr = random() % 255;
652 randg = random() % 255;
653 randb = random() % 255;
654 PAINT_BOX(buf, randr, randg, randb, simplerect->fill_a, begin, sy, end, ey);
656 PAINT_BOX(buf, simplerect->fill_r, simplerect->fill_g, simplerect->fill_b, simplerect->fill_a, begin, sy, end, ey);
660 for (i = 0; i < simplerect->outline_pixels; ++i) {
662 if (simplerect->outline_what & 0x1) {
663 if (begin == simplerect->bbox_ulx) {
664 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin + i, sy, ey);
668 if (simplerect->outline_what & 0x2) {
669 if (end == (simplerect->bbox_lrx - 1)) {
670 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, end - i, sy, ey + 1);
674 if (simplerect->outline_what & 0x4) {
675 PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end, sy+i);
678 if (simplerect->outline_what & 0x8) {
679 PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end + 1, ey-i);
683 #endif /* SIMPLERECT_FAST_RENDERER */
686 gnome_canvas_simplerect_draw (GnomeCanvasItem *item,
687 GdkDrawable *drawable,
689 int width, int height)
691 fprintf (stderr, "please don't use the CanvasSimpleRect item in a non-aa Canvas\n");
696 gnome_canvas_simplerect_point (GnomeCanvasItem *item, double x, double y, int cx, int cy, GnomeCanvasItem **actual_item)
698 GnomeCanvasSimpleRect *simplerect;
699 double x1, y1, x2, y2;
702 simplerect = GNOME_CANVAS_SIMPLERECT (item);
706 /* Find the bounds for the rectangle plus its outline width */
708 gnome_canvas_simplerect_bounds (item, &x1, &y1, &x2, &y2);
710 /* Is point inside rectangle */
712 if ((x >= x1) && (y >= y1) && (x <= x2) && (y <= y2)) {
716 /* Point is outside rectangle */
732 return sqrt (dx * dx + dy * dy);