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;
262 gnome_canvas_simplerect_reset_bounds (GnomeCanvasItem *item)
264 GnomeCanvasSimpleRect* simplerect;
265 double x1, x2, y1, y2;
266 double old_x1, old_x2, old_y1, old_y2;
267 ArtDRect unionrect, old, new;
274 gnome_canvas_simplerect_bounds (item, &x1, &y1, &x2, &y2);
275 gnome_canvas_item_i2w (item, &x1, &y1);
276 gnome_canvas_item_i2w (item, &x2, &y2);
283 /* now compute bounding box in canvas units */
285 simplerect = GNOME_CANVAS_SIMPLERECT (item);
287 gnome_canvas_w2c (GNOME_CANVAS(item->canvas), x1, y1, &simplerect->bbox_ulx, &simplerect->bbox_uly);
288 gnome_canvas_w2c (GNOME_CANVAS(item->canvas), x2, y2, &simplerect->bbox_lrx, &simplerect->bbox_lry);
290 /* now queue redraws for changed areas */
292 if (item->x1 == old_x1 && item->x2 == old_x2) {
294 /* no change in x-axis position */
296 if (item->y1 == old_y1) {
297 /* top didn't change, so just draw bottom */
299 double start_y = MIN (item->y2, old_y2);
300 double end_y = MAX (item->y2, old_y2);
302 gnome_canvas_request_redraw (item->canvas, item->x1, start_y - 0.5, item->x2, end_y + 1.5);
305 } else if (item->y2 == old_y2) {
307 /* bottom didn't change, just draw top */
309 double start_y = MIN (item->y1, old_y1);
310 double end_y = MAX (item->y1, old_y1);
312 gnome_canvas_request_redraw (item->canvas, item->x1, start_y - 0.5, item->x2, end_y + 1.5);
317 } else if (item->y1 == old_y1 && item->y2 == old_y2) {
319 /* no change in y-axis position */
321 if (item->x1 == old_x1) {
322 /* start didn't change, so just draw at the end */
324 double start_x = MIN (item->x2, old_x2);
325 double end_x = MAX (item->x2, old_x2);
327 gnome_canvas_request_redraw (item->canvas, start_x - 0.5, item->y1, end_x + 1.5, item->y2);
330 } else if (item->x2 == old_x2) {
332 /* end didn't change, so just draw at the start */
334 double start_x = MIN (item->x1, old_x1);
335 double end_x = MAX (item->x1, old_x1);
337 gnome_canvas_request_redraw (item->canvas, start_x - 0.5, item->y1, end_x + 1.5, item->y2 + 0.5);
353 art_drect_union (&unionrect, &old, &new);
354 gnome_canvas_request_redraw (item->canvas,
366 gnome_canvas_simplerect_set_property (GObject *object,
372 GnomeCanvasSimpleRect *simplerect;
374 int bounds_changed = FALSE;
375 g_return_if_fail (object != NULL);
376 g_return_if_fail (GNOME_IS_CANVAS_SIMPLERECT (object));
378 simplerect = GNOME_CANVAS_SIMPLERECT (object);
382 if (simplerect->x1 != g_value_get_double (value)) {
383 simplerect->x1 = g_value_get_double (value);
384 bounds_changed = TRUE;
389 if (simplerect->y1 != g_value_get_double (value)) {
390 simplerect->y1 = g_value_get_double (value);
391 bounds_changed = TRUE;
396 if (simplerect->x2 != g_value_get_double (value)) {
397 simplerect->x2 = g_value_get_double (value);
398 bounds_changed = TRUE;
403 if (simplerect->y2 != g_value_get_double (value)) {
404 simplerect->y2 = g_value_get_double (value);
405 bounds_changed = TRUE;
410 if (simplerect->draw != g_value_get_boolean (value)) {
411 simplerect->draw = g_value_get_boolean (value);
418 if (simplerect->fill != g_value_get_boolean (value)) {
419 simplerect->fill = g_value_get_boolean (value);
424 case PROP_FILL_COLOR_RGBA:
425 if (simplerect->fill_color != g_value_get_uint(value)) {
426 simplerect->fill_color = g_value_get_uint(value);
431 case PROP_OUTLINE_COLOR_RGBA:
432 if (simplerect->outline_color != g_value_get_uint(value)) {
433 simplerect->outline_color = g_value_get_uint(value);
438 case PROP_OUTLINE_PIXELS:
439 if (simplerect->outline_pixels != g_value_get_uint(value)) {
440 simplerect->outline_pixels = g_value_get_uint(value);
445 case PROP_OUTLINE_WHAT:
446 if (simplerect->outline_what != g_value_get_uint(value)) {
447 simplerect->outline_what = g_value_get_uint(value);
456 simplerect->full_draw_on_update = update;
458 if (update || bounds_changed) {
459 gnome_canvas_item_request_update (GNOME_CANVAS_ITEM(object));
464 gnome_canvas_simplerect_get_property (GObject *object,
469 GnomeCanvasSimpleRect *rect = GNOME_CANVAS_SIMPLERECT (object);
471 g_return_if_fail (object != NULL);
472 g_return_if_fail (GNOME_IS_CANVAS_SIMPLERECT (object));
476 g_value_set_double (value, rect->x1);
479 g_value_set_double (value, rect->x2);
482 g_value_set_double (value, rect->y1);
485 g_value_set_double (value, rect->y2);
487 case PROP_OUTLINE_WHAT:
488 g_value_set_uint (value, rect->outline_what);
491 g_value_set_boolean (value, rect->fill);
493 case PROP_OUTLINE_PIXELS:
494 g_value_set_uint (value, rect->outline_pixels);
496 case PROP_FILL_COLOR_RGBA:
497 g_value_set_uint (value, rect->fill_color);
499 case PROP_OUTLINE_COLOR_RGBA:
500 g_value_set_uint (value, rect->outline_color);
503 g_value_set_boolean (value, rect->draw);
507 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
514 gnome_canvas_simplerect_update (GnomeCanvasItem *item, double *affine, ArtSVP *clip_path, int flags)
516 GnomeCanvasSimpleRect *simplerect;
519 simplerect = GNOME_CANVAS_SIMPLERECT (item);
521 if (parent_class->update)
522 (* parent_class->update) (item, affine, clip_path, flags);
524 gnome_canvas_simplerect_reset_bounds (item);
526 if (simplerect->full_draw_on_update) {
527 gnome_canvas_request_redraw (item->canvas,
528 simplerect->bbox_ulx,
529 simplerect->bbox_uly,
530 simplerect->bbox_lrx+0.5,
531 simplerect->bbox_lry+0.5);
532 simplerect->full_draw_on_update = FALSE;
535 UINT_TO_RGBA (simplerect->fill_color, &simplerect->fill_r, &simplerect->fill_g, &simplerect->fill_b, &simplerect->fill_a);
536 UINT_TO_RGBA (simplerect->outline_color, &simplerect->outline_r, &simplerect->outline_g, &simplerect->outline_b, &foo);
539 // this can be useful for debugging/understanding how the canvas redraws
542 #undef HARLEQUIN_DEBUGGING
544 #undef SIMPLERECT_FAST_RENDERER
545 #ifdef SIMPLERECT_FAST_RENDERER
548 gnome_canvas_simplerect_render (GnomeCanvasItem *item,
551 GnomeCanvasSimpleRect *simplerect;
555 ArtIRect intersection;
558 simplerect = GNOME_CANVAS_SIMPLERECT (item);
560 if (parent_class->render) {
561 (*parent_class->render) (item, buf);
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, 255, buf->rect.x0, buf->rect.y0, buf->rect.x1, buf->rect.y1);
573 gnome_canvas_buf_ensure_buf (buf);
577 if (!simplerect->draw) {
581 self.x0 = simplerect->bbox_ulx;
582 self.y0 = simplerect->bbox_uly;
583 self.x1 = simplerect->bbox_lrx;
584 self.y1 = simplerect->bbox_lry;
586 art_irect_intersect (&intersection, &self, &buf->rect);
588 begin = MAX(simplerect->bbox_ulx, buf->rect.x0);
589 end = MIN((simplerect->bbox_lrx-1), buf->rect.x1);
591 sy = simplerect->bbox_uly;
592 ey = simplerect->bbox_lry-1;
594 if (simplerect->fill) {
596 // this can be useful for debugging/understanding how the canvas redraws
599 #ifdef HARLEQUIN_DEBUGGING
600 gint randr, randg, randb;
601 randr = random() % 255;
602 randg = random() % 255;
603 randb = random() % 255;
604 PAINT_BOX(buf, randr, randg, randb, simplerect->fill_a, begin, sy, end, ey);
606 PAINT_BOX (buf, simplerect->fill_r, simplerect->fill_g, simplerect->fill_b, simplerect->fill_a,
607 intersection.x0, intersection.y0,
608 intersection.x1, intersection.y1);
613 for (i = 0; i < simplerect->outline_pixels; ++i) {
615 if (simplerect->outline_what & 0x1) {
616 if (begin == simplerect->bbox_ulx) {
617 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin + i, sy, ey);
621 if (simplerect->outline_what & 0x2) {
622 if (end == (simplerect->bbox_lrx - 1)) {
623 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, end - i, sy, ey + 1);
627 if (simplerect->outline_what & 0x4) {
628 PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end, sy+i);
631 if (simplerect->outline_what & 0x8) {
632 PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end + 1, ey-i);
637 #else /* SIMPLERECT_FAST_RENDERER */
640 gnome_canvas_simplerect_render (GnomeCanvasItem *item,
643 GnomeCanvasSimpleRect *simplerect;
648 simplerect = GNOME_CANVAS_SIMPLERECT (item);
650 if (parent_class->render) {
651 (*parent_class->render) (item, buf);
656 #ifdef HARLEQUIN_DEBUGGING
657 gint randr, randg, randb;
658 randr = random() % 255;
659 randg = random() % 255;
660 randb = random() % 255;
661 PAINT_BOX(buf, randr, randg, randb, 255, buf->rect.x0, buf->rect.y0, buf->rect.x1, buf->rect.y1);
663 gnome_canvas_buf_ensure_buf (buf);
667 if (!simplerect->draw) {
671 begin = MAX(simplerect->bbox_ulx,buf->rect.x0);
672 end = MIN((simplerect->bbox_lrx-1),buf->rect.x1);
674 sy = simplerect->bbox_uly;
675 ey = simplerect->bbox_lry-1;
677 if (simplerect->fill) {
679 #ifdef HARLEQUIN_DEBUGGING
680 gint randr, randg, randb;
681 randr = random() % 255;
682 randg = random() % 255;
683 randb = random() % 255;
684 PAINT_BOX(buf, randr, randg, randb, simplerect->fill_a, begin, sy, end, ey);
686 PAINT_BOX(buf, simplerect->fill_r, simplerect->fill_g, simplerect->fill_b, simplerect->fill_a, begin, sy, end, ey);
690 for (i = 0; i < simplerect->outline_pixels; ++i) {
692 if (simplerect->outline_what & 0x1) {
693 if (begin == simplerect->bbox_ulx) {
694 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin + i, sy, ey);
698 if (simplerect->outline_what & 0x2) {
699 if (end == (simplerect->bbox_lrx - 1)) {
700 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, end - i, sy, ey + 1);
704 if (simplerect->outline_what & 0x4) {
705 PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end, sy+i);
708 if (simplerect->outline_what & 0x8) {
709 PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end + 1, ey-i);
713 #endif /* SIMPLERECT_FAST_RENDERER */
716 gnome_canvas_simplerect_draw (GnomeCanvasItem *item,
717 GdkDrawable *drawable,
719 int width, int height)
721 fprintf (stderr, "please don't use the CanvasSimpleRect item in a non-aa Canvas\n");
726 gnome_canvas_simplerect_point (GnomeCanvasItem *item, double x, double y, int cx, int cy, GnomeCanvasItem **actual_item)
728 GnomeCanvasSimpleRect *simplerect;
729 double x1, y1, x2, y2;
732 simplerect = GNOME_CANVAS_SIMPLERECT (item);
736 /* Find the bounds for the rectangle plus its outline width */
738 gnome_canvas_simplerect_bounds (item, &x1, &y1, &x2, &y2);
740 /* Is point inside rectangle */
742 if ((x >= x1) && (y >= y1) && (x <= x2) && (y <= y2)) {
746 /* Point is outside rectangle */
762 return sqrt (dx * dx + dy * dy);