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;
233 gnome_canvas_simplerect_destroy (GtkObject *object)
235 GnomeCanvasSimpleRect *rect;
237 g_return_if_fail (object != NULL);
238 g_return_if_fail (GNOME_IS_CANVAS_SIMPLERECT (object));
240 rect = GNOME_CANVAS_SIMPLERECT (object);
242 /* remember, destroy can be run multiple times! */
244 if (GTK_OBJECT_CLASS (parent_class)->destroy)
245 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
249 gnome_canvas_simplerect_bounds (GnomeCanvasItem *item, double *x1, double *y1, double *x2, double *y2)
251 GnomeCanvasSimpleRect *simplerect = GNOME_CANVAS_SIMPLERECT (item);
253 *x1 = simplerect->x1;
254 *y1 = simplerect->y1;
255 *x2 = simplerect->x2 + 1;
256 *y2 = simplerect->y2 + 1;
261 gnome_canvas_simplerect_reset_bounds (GnomeCanvasItem *item)
263 GnomeCanvasSimpleRect* simplerect;
264 double x1, x2, y1, y2;
265 double old_x1, old_x2, old_y1, old_y2;
273 gnome_canvas_simplerect_bounds (item, &x1, &y1, &x2, &y2);
274 gnome_canvas_item_i2w (item, &x1, &y1);
275 gnome_canvas_item_i2w (item, &x2, &y2);
282 /* now compute bounding box in canvas units */
284 simplerect = GNOME_CANVAS_SIMPLERECT (item);
286 gnome_canvas_w2c (GNOME_CANVAS(item->canvas), x1, y1, &simplerect->bbox_ulx, &simplerect->bbox_uly);
287 gnome_canvas_w2c (GNOME_CANVAS(item->canvas), x2, y2, &simplerect->bbox_lrx, &simplerect->bbox_lry);
289 /* now queue redraws for changed areas */
291 if (item->x1 != old_x1) {
293 /* left edge changed. redraw the area that altered */
295 a = MIN(item->x1, old_x1);
296 b = MAX(item->x1, old_x1);
297 gnome_canvas_request_redraw (item->canvas, a - 1, item->y1, b + 1, item->y2);
300 if (item->x2 != old_x2) {
302 /* right edge changed. redraw the area that altered */
304 a = MIN(item->x2, old_x2);
305 b = MAX(item->x2, old_x2);
306 gnome_canvas_request_redraw (item->canvas, a - 1, item->y1, b + 1, item->y2);
309 if (item->y1 != old_y1) {
311 /* top edge changed. redraw the area that altered */
313 a = MIN(item->y1, old_y1);
314 b = MAX(item->y1, old_y1);
315 gnome_canvas_request_redraw (item->canvas, item->x1, a - 1, item->x2, b + 1);
318 if (item->y2 != old_y2) {
320 /* lower edge changed. redraw the area that altered */
322 a = MIN(item->y2, old_y2);
323 b = MAX(item->y2, old_y2);
324 gnome_canvas_request_redraw (item->canvas, item->x1, a - 1, item->x2, b + 1);
333 gnome_canvas_simplerect_set_property (GObject *object,
339 GnomeCanvasSimpleRect *simplerect;
341 int bounds_changed = FALSE;
342 g_return_if_fail (object != NULL);
343 g_return_if_fail (GNOME_IS_CANVAS_SIMPLERECT (object));
345 simplerect = GNOME_CANVAS_SIMPLERECT (object);
349 if (simplerect->x1 != g_value_get_double (value)) {
350 simplerect->x1 = g_value_get_double (value);
351 bounds_changed = TRUE;
356 if (simplerect->y1 != g_value_get_double (value)) {
357 simplerect->y1 = g_value_get_double (value);
358 bounds_changed = TRUE;
363 if (simplerect->x2 != g_value_get_double (value)) {
364 simplerect->x2 = g_value_get_double (value);
365 bounds_changed = TRUE;
370 if (simplerect->y2 != g_value_get_double (value)) {
371 simplerect->y2 = g_value_get_double (value);
372 bounds_changed = TRUE;
377 if (simplerect->draw != g_value_get_boolean (value)) {
378 simplerect->draw = g_value_get_boolean (value);
385 if (simplerect->fill != g_value_get_boolean (value)) {
386 simplerect->fill = g_value_get_boolean (value);
391 case PROP_FILL_COLOR_RGBA:
392 if (simplerect->fill_color != g_value_get_uint(value)) {
393 simplerect->fill_color = g_value_get_uint(value);
398 case PROP_OUTLINE_COLOR_RGBA:
399 if (simplerect->outline_color != g_value_get_uint(value)) {
400 simplerect->outline_color = g_value_get_uint(value);
405 case PROP_OUTLINE_PIXELS:
406 if (simplerect->outline_pixels != g_value_get_uint(value)) {
407 simplerect->outline_pixels = g_value_get_uint(value);
412 case PROP_OUTLINE_WHAT:
413 if (simplerect->outline_what != g_value_get_uint(value)) {
414 simplerect->outline_what = g_value_get_uint(value);
423 simplerect->full_draw_on_update = update;
425 if (update || bounds_changed) {
426 gnome_canvas_item_request_update (GNOME_CANVAS_ITEM(object));
431 gnome_canvas_simplerect_get_property (GObject *object,
436 GnomeCanvasSimpleRect *rect = GNOME_CANVAS_SIMPLERECT (object);
438 g_return_if_fail (object != NULL);
439 g_return_if_fail (GNOME_IS_CANVAS_SIMPLERECT (object));
443 g_value_set_double (value, rect->x1);
446 g_value_set_double (value, rect->x2);
449 g_value_set_double (value, rect->y1);
452 g_value_set_double (value, rect->y2);
454 case PROP_OUTLINE_WHAT:
455 g_value_set_uint (value, rect->outline_what);
458 g_value_set_boolean (value, rect->fill);
460 case PROP_OUTLINE_PIXELS:
461 g_value_set_uint (value, rect->outline_pixels);
463 case PROP_FILL_COLOR_RGBA:
464 g_value_set_uint (value, rect->fill_color);
466 case PROP_OUTLINE_COLOR_RGBA:
467 g_value_set_uint (value, rect->outline_color);
470 g_value_set_boolean (value, rect->draw);
474 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
481 gnome_canvas_simplerect_update (GnomeCanvasItem *item, double *affine, ArtSVP *clip_path, int flags)
483 GnomeCanvasSimpleRect *simplerect;
486 simplerect = GNOME_CANVAS_SIMPLERECT (item);
488 if (parent_class->update)
489 (* parent_class->update) (item, affine, clip_path, flags);
491 gnome_canvas_simplerect_reset_bounds (item);
493 if (simplerect->full_draw_on_update) {
494 gnome_canvas_request_redraw (item->canvas,
495 simplerect->bbox_ulx,
496 simplerect->bbox_uly,
497 simplerect->bbox_lrx+1,
498 simplerect->bbox_lry+1);
499 simplerect->full_draw_on_update = FALSE;
502 UINT_TO_RGBA (simplerect->fill_color, &simplerect->fill_r, &simplerect->fill_g, &simplerect->fill_b, &simplerect->fill_a);
503 UINT_TO_RGBA (simplerect->outline_color, &simplerect->outline_r, &simplerect->outline_g, &simplerect->outline_b, &foo);
506 // this can be useful for debugging/understanding how the canvas redraws
509 #undef HARLEQUIN_DEBUGGING
511 #undef SIMPLERECT_FAST_RENDERER
512 #ifdef SIMPLERECT_FAST_RENDERER
515 gnome_canvas_simplerect_render (GnomeCanvasItem *item,
518 GnomeCanvasSimpleRect *simplerect;
522 ArtIRect intersection;
525 simplerect = GNOME_CANVAS_SIMPLERECT (item);
527 if (parent_class->render) {
528 (*parent_class->render) (item, buf);
533 #ifdef HARLEQUIN_DEBUGGING
534 gint randr, randg, randb;
535 randr = random() % 255;
536 randg = random() % 255;
537 randb = random() % 255;
538 PAINT_BOX(buf, randr, randg, randb, 255, buf->rect.x0, buf->rect.y0, buf->rect.x1, buf->rect.y1);
540 gnome_canvas_buf_ensure_buf (buf);
544 if (!simplerect->draw) {
548 self.x0 = simplerect->bbox_ulx;
549 self.y0 = simplerect->bbox_uly;
550 self.x1 = simplerect->bbox_lrx;
551 self.y1 = simplerect->bbox_lry;
553 art_irect_intersect (&intersection, &self, &buf->rect);
555 begin = MAX(simplerect->bbox_ulx, buf->rect.x0);
556 end = MIN((simplerect->bbox_lrx-1), buf->rect.x1);
558 sy = simplerect->bbox_uly;
559 ey = simplerect->bbox_lry-1;
561 if (simplerect->fill) {
563 // this can be useful for debugging/understanding how the canvas redraws
566 #ifdef HARLEQUIN_DEBUGGING
567 gint randr, randg, randb;
568 randr = random() % 255;
569 randg = random() % 255;
570 randb = random() % 255;
571 PAINT_BOX(buf, randr, randg, randb, simplerect->fill_a, begin, sy, end, ey);
573 PAINT_BOX (buf, simplerect->fill_r, simplerect->fill_g, simplerect->fill_b, simplerect->fill_a,
574 intersection.x0, intersection.y0,
575 intersection.x1, intersection.y1);
580 for (i = 0; i < simplerect->outline_pixels; ++i) {
582 if (simplerect->outline_what & 0x1) {
583 if (begin == simplerect->bbox_ulx) {
584 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin + i, sy, ey);
588 if (simplerect->outline_what & 0x2) {
589 if (end == (simplerect->bbox_lrx - 1)) {
590 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, end - i, sy, ey + 1);
594 if (simplerect->outline_what & 0x4) {
595 PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end, sy+i);
598 if (simplerect->outline_what & 0x8) {
599 PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end + 1, ey-i);
604 #else /* SIMPLERECT_FAST_RENDERER */
607 gnome_canvas_simplerect_render (GnomeCanvasItem *item,
610 GnomeCanvasSimpleRect *simplerect;
615 simplerect = GNOME_CANVAS_SIMPLERECT (item);
617 if (parent_class->render) {
618 (*parent_class->render) (item, buf);
623 #ifdef HARLEQUIN_DEBUGGING
624 gint randr, randg, randb;
625 randr = random() % 255;
626 randg = random() % 255;
627 randb = random() % 255;
628 PAINT_BOX(buf, randr, randg, randb, 255, buf->rect.x0, buf->rect.y0, buf->rect.x1, buf->rect.y1);
630 gnome_canvas_buf_ensure_buf (buf);
634 if (!simplerect->draw) {
638 begin = MAX(simplerect->bbox_ulx,buf->rect.x0);
639 end = MIN((simplerect->bbox_lrx-1),buf->rect.x1);
641 sy = simplerect->bbox_uly;
642 ey = simplerect->bbox_lry-1;
644 if (simplerect->fill) {
646 #ifdef HARLEQUIN_DEBUGGING
647 gint randr, randg, randb;
648 randr = random() % 255;
649 randg = random() % 255;
650 randb = random() % 255;
651 PAINT_BOX(buf, randr, randg, randb, simplerect->fill_a, begin, sy, end, ey);
653 PAINT_BOX(buf, simplerect->fill_r, simplerect->fill_g, simplerect->fill_b, simplerect->fill_a, begin, sy, end, ey);
657 for (i = 0; i < simplerect->outline_pixels; ++i) {
659 if (simplerect->outline_what & 0x1) {
660 if (begin == simplerect->bbox_ulx) {
661 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin + i, sy, ey);
665 if (simplerect->outline_what & 0x2) {
666 if (end == (simplerect->bbox_lrx - 1)) {
667 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, end - i, sy, ey + 1);
671 if (simplerect->outline_what & 0x4) {
672 PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end, sy+i);
675 if (simplerect->outline_what & 0x8) {
676 PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end + 1, ey-i);
680 #endif /* SIMPLERECT_FAST_RENDERER */
683 gnome_canvas_simplerect_draw (GnomeCanvasItem *item,
684 GdkDrawable *drawable,
686 int width, int height)
688 fprintf (stderr, "please don't use the CanvasSimpleRect item in a non-aa Canvas\n");
693 gnome_canvas_simplerect_point (GnomeCanvasItem *item, double x, double y, int cx, int cy, GnomeCanvasItem **actual_item)
695 GnomeCanvasSimpleRect *simplerect;
696 double x1, y1, x2, y2;
699 simplerect = GNOME_CANVAS_SIMPLERECT (item);
703 /* Find the bounds for the rectangle plus its outline width */
705 gnome_canvas_simplerect_bounds (item, &x1, &y1, &x2, &y2);
707 /* Is point inside rectangle */
709 if ((x >= x1) && (y >= y1) && (x <= x2) && (y <= y2)) {
713 /* Point is outside rectangle */
729 return sqrt (dx * dx + dy * dy);