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 gobject_class->set_property = gnome_canvas_simplerect_set_property;
99 gobject_class->get_property = gnome_canvas_simplerect_get_property;
101 g_object_class_install_property (gobject_class,
103 g_param_spec_double ("x1",
105 _("x coordinate of upper left corner of rect"),
111 g_object_class_install_property (gobject_class,
113 g_param_spec_double ("y1",
115 _("y coordinate of upper left corner of rect "),
122 g_object_class_install_property (gobject_class,
124 g_param_spec_double ("x2",
126 _("x coordinate of lower right corner of rect"),
132 g_object_class_install_property (gobject_class,
134 g_param_spec_double ("y2",
136 _("y coordinate of lower right corner of rect "),
143 g_object_class_install_property (gobject_class,
145 g_param_spec_uint ("outline_pixels",
147 _("width in pixels of outline"),
154 g_object_class_install_property (gobject_class,
156 g_param_spec_uint ("outline_what",
158 _("which boundaries to outline (mask)"),
166 g_object_class_install_property (gobject_class,
168 g_param_spec_boolean ("fill",
174 g_object_class_install_property (gobject_class,
176 g_param_spec_boolean ("draw",
183 g_object_class_install_property (gobject_class,
184 PROP_OUTLINE_COLOR_RGBA,
185 g_param_spec_uint ("outline_color_rgba",
186 _("outline color rgba"),
187 _("color of outline"),
194 g_object_class_install_property (gobject_class,
195 PROP_FILL_COLOR_RGBA,
196 g_param_spec_uint ("fill_color_rgba",
197 _("fill color rgba"),
204 object_class->destroy = gnome_canvas_simplerect_destroy;
206 item_class->update = gnome_canvas_simplerect_update;
207 item_class->draw = gnome_canvas_simplerect_draw;
208 item_class->bounds = gnome_canvas_simplerect_bounds;
209 item_class->point = gnome_canvas_simplerect_point;
210 item_class->render = gnome_canvas_simplerect_render;
215 gnome_canvas_simplerect_init (GnomeCanvasSimpleRect *simplerect)
217 simplerect->x1 = 0.0;
218 simplerect->y1 = 0.0;
219 simplerect->x2 = 0.0;
220 simplerect->y2 = 0.0;
221 simplerect->fill = TRUE;
222 simplerect->draw = TRUE;
223 simplerect->full_draw_on_update = TRUE;
224 simplerect->fill_color = 0;
225 simplerect->outline_color = 0;
226 simplerect->outline_pixels = 1;
227 simplerect->outline_what = 0xf;
230 // GNOME_CANVAS_ITEM(simplerect)->object.flags |= GNOME_CANVAS_ITEM_NO_AUTO_REDRAW;
234 gnome_canvas_simplerect_destroy (GtkObject *object)
236 GnomeCanvasSimpleRect *rect;
238 g_return_if_fail (object != NULL);
239 g_return_if_fail (GNOME_IS_CANVAS_SIMPLERECT (object));
241 rect = GNOME_CANVAS_SIMPLERECT (object);
243 /* remember, destroy can be run multiple times! */
245 if (GTK_OBJECT_CLASS (parent_class)->destroy)
246 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
250 gnome_canvas_simplerect_bounds (GnomeCanvasItem *item, double *x1, double *y1, double *x2, double *y2)
252 GnomeCanvasSimpleRect *simplerect = GNOME_CANVAS_SIMPLERECT (item);
254 *x1 = simplerect->x1;
255 *y1 = simplerect->y1;
256 *x2 = simplerect->x2 + 1;
257 *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;
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) {
294 /* left edge changed. redraw the area that altered */
296 a = MIN(item->x1, old_x1);
297 b = MAX(item->x1, old_x1);
298 gnome_canvas_request_redraw (item->canvas, a - 1, item->y1, b + 1, item->y2);
301 if (item->x2 != old_x2) {
303 /* right edge changed. redraw the area that altered */
305 a = MIN(item->x2, old_x2);
306 b = MAX(item->x2, old_x2);
307 gnome_canvas_request_redraw (item->canvas, a - 1, item->y1, b + 1, item->y2);
310 if (item->y1 != old_y1) {
312 /* top edge changed. redraw the area that altered */
314 a = MIN(item->y1, old_y1);
315 b = MAX(item->y1, old_y1);
316 gnome_canvas_request_redraw (item->canvas, item->x1, a - 1, item->x2, b + 1);
319 if (item->y2 != old_y2) {
321 /* lower edge changed. redraw the area that altered */
323 a = MIN(item->y2, old_y2);
324 b = MAX(item->y2, old_y2);
325 gnome_canvas_request_redraw (item->canvas, item->x1, a - 1, item->x2, b + 1);
334 gnome_canvas_simplerect_set_property (GObject *object,
340 GnomeCanvasSimpleRect *simplerect;
342 int bounds_changed = FALSE;
343 g_return_if_fail (object != NULL);
344 g_return_if_fail (GNOME_IS_CANVAS_SIMPLERECT (object));
346 simplerect = GNOME_CANVAS_SIMPLERECT (object);
350 if (simplerect->x1 != g_value_get_double (value)) {
351 simplerect->x1 = g_value_get_double (value);
352 bounds_changed = TRUE;
357 if (simplerect->y1 != g_value_get_double (value)) {
358 simplerect->y1 = g_value_get_double (value);
359 bounds_changed = TRUE;
364 if (simplerect->x2 != g_value_get_double (value)) {
365 simplerect->x2 = g_value_get_double (value);
366 bounds_changed = TRUE;
371 if (simplerect->y2 != g_value_get_double (value)) {
372 simplerect->y2 = g_value_get_double (value);
373 bounds_changed = TRUE;
378 if (simplerect->draw != g_value_get_boolean (value)) {
379 simplerect->draw = g_value_get_boolean (value);
386 if (simplerect->fill != g_value_get_boolean (value)) {
387 simplerect->fill = g_value_get_boolean (value);
392 case PROP_FILL_COLOR_RGBA:
393 if (simplerect->fill_color != g_value_get_uint(value)) {
394 simplerect->fill_color = g_value_get_uint(value);
399 case PROP_OUTLINE_COLOR_RGBA:
400 if (simplerect->outline_color != g_value_get_uint(value)) {
401 simplerect->outline_color = g_value_get_uint(value);
406 case PROP_OUTLINE_PIXELS:
407 if (simplerect->outline_pixels != g_value_get_uint(value)) {
408 simplerect->outline_pixels = g_value_get_uint(value);
413 case PROP_OUTLINE_WHAT:
414 if (simplerect->outline_what != g_value_get_uint(value)) {
415 simplerect->outline_what = g_value_get_uint(value);
424 simplerect->full_draw_on_update = update;
426 if (update || bounds_changed) {
427 gnome_canvas_item_request_update (GNOME_CANVAS_ITEM(object));
432 gnome_canvas_simplerect_get_property (GObject *object,
437 GnomeCanvasSimpleRect *rect = GNOME_CANVAS_SIMPLERECT (object);
439 g_return_if_fail (object != NULL);
440 g_return_if_fail (GNOME_IS_CANVAS_SIMPLERECT (object));
444 g_value_set_double (value, rect->x1);
447 g_value_set_double (value, rect->x2);
450 g_value_set_double (value, rect->y1);
453 g_value_set_double (value, rect->y2);
455 case PROP_OUTLINE_WHAT:
456 g_value_set_uint (value, rect->outline_what);
459 g_value_set_boolean (value, rect->fill);
461 case PROP_OUTLINE_PIXELS:
462 g_value_set_uint (value, rect->outline_pixels);
464 case PROP_FILL_COLOR_RGBA:
465 g_value_set_uint (value, rect->fill_color);
467 case PROP_OUTLINE_COLOR_RGBA:
468 g_value_set_uint (value, rect->outline_color);
471 g_value_set_boolean (value, rect->draw);
475 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
482 gnome_canvas_simplerect_update (GnomeCanvasItem *item, double *affine, ArtSVP *clip_path, int flags)
484 GnomeCanvasSimpleRect *simplerect;
487 simplerect = GNOME_CANVAS_SIMPLERECT (item);
489 if (parent_class->update)
490 (* parent_class->update) (item, affine, clip_path, flags);
492 gnome_canvas_simplerect_reset_bounds (item);
494 if (simplerect->full_draw_on_update) {
495 gnome_canvas_request_redraw (item->canvas,
496 simplerect->bbox_ulx,
497 simplerect->bbox_uly,
498 simplerect->bbox_lrx+1,
499 simplerect->bbox_lry+1);
500 simplerect->full_draw_on_update = FALSE;
503 UINT_TO_RGBA (simplerect->fill_color, &simplerect->fill_r, &simplerect->fill_g, &simplerect->fill_b, &simplerect->fill_a);
504 UINT_TO_RGBA (simplerect->outline_color, &simplerect->outline_r, &simplerect->outline_g, &simplerect->outline_b, &foo);
507 #define SIMPLERECT_FAST_RENDERER
508 #ifdef SIMPLERECT_FAST_RENDERER
511 gnome_canvas_simplerect_render (GnomeCanvasItem *item,
514 GnomeCanvasSimpleRect *simplerect;
518 ArtIRect intersection;
521 simplerect = GNOME_CANVAS_SIMPLERECT (item);
523 if (parent_class->render) {
524 (*parent_class->render) (item, buf);
529 // this can be useful for debugging/understanding how the canvas redraws
532 // gint randr, randg, randb;
533 // randr = random() % 255;
534 // randg = random() % 255;
535 // randb = random() % 255;
536 // PAINT_BOX(buf, randr, randg, randb, 255, buf->rect.x0, buf->rect.y0, buf->rect.x1, buf->rect.y1);
538 gnome_canvas_buf_ensure_buf (buf);
542 if (!simplerect->draw) {
546 self.x0 = simplerect->bbox_ulx;
547 self.y0 = simplerect->bbox_uly;
548 self.x1 = simplerect->bbox_lrx;
549 self.y1 = simplerect->bbox_lry;
551 art_irect_intersect (&intersection, &self, &buf->rect);
553 begin = MAX(simplerect->bbox_ulx, buf->rect.x0);
554 end = MIN((simplerect->bbox_lrx-1), buf->rect.x1);
556 sy = simplerect->bbox_uly;
557 ey = simplerect->bbox_lry-1;
559 if (simplerect->fill) {
561 // this can be useful for debugging/understanding how the canvas redraws
564 // gint randr, randg, randb;
565 // randr = random() % 255;
566 // randg = random() % 255;
567 // randb = random() % 255;
568 // PAINT_BOX(buf, randr, randg, randb, simplerect->fill_a, begin, sy, end, ey);
570 FAST_PAINT_BOX (buf, simplerect->fill_r, simplerect->fill_g, simplerect->fill_b, simplerect->fill_a,
571 intersection.x0, intersection.y0,
572 intersection.x1, intersection.y1);
576 for (i = 0; i < simplerect->outline_pixels; ++i) {
578 if (simplerect->outline_what & 0x1) {
579 if (begin == simplerect->bbox_ulx) {
580 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin + i, sy, ey);
584 if (simplerect->outline_what & 0x2) {
585 if (end == (simplerect->bbox_lrx - 1)) {
586 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, end - i, sy, ey + 1);
590 if (simplerect->outline_what & 0x4) {
591 PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end, sy+i);
594 if (simplerect->outline_what & 0x8) {
595 PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end + 1, ey-i);
600 #else /* SIMPLERECT_FAST_RENDERER */
603 gnome_canvas_simplerect_render (GnomeCanvasItem *item,
606 GnomeCanvasSimpleRect *simplerect;
611 simplerect = GNOME_CANVAS_SIMPLERECT (item);
613 if (parent_class->render) {
614 (*parent_class->render) (item, buf);
619 // this can be useful for debugging/understanding how the canvas redraws
622 // gint randr, randg, randb;
623 // randr = random() % 255;
624 // randg = random() % 255;
625 // randb = random() % 255;
626 // PAINT_BOX(buf, randr, randg, randb, 255, buf->rect.x0, buf->rect.y0, buf->rect.x1, buf->rect.y1);
628 gnome_canvas_buf_ensure_buf (buf);
632 if (!simplerect->draw) {
636 begin = MAX(simplerect->bbox_ulx,buf->rect.x0);
637 end = MIN((simplerect->bbox_lrx-1),buf->rect.x1);
639 sy = simplerect->bbox_uly;
640 ey = simplerect->bbox_lry-1;
642 if (simplerect->fill) {
644 // this can be useful for debugging/understanding how the canvas redraws
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);
656 for (i = 0; i < simplerect->outline_pixels; ++i) {
658 if (simplerect->outline_what & 0x1) {
659 if (begin == simplerect->bbox_ulx) {
660 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin + i, sy, ey);
664 if (simplerect->outline_what & 0x2) {
665 if (end == (simplerect->bbox_lrx - 1)) {
666 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, end - i, sy, ey + 1);
670 if (simplerect->outline_what & 0x4) {
671 PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end, sy+i);
674 if (simplerect->outline_what & 0x8) {
675 PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end + 1, ey-i);
679 #endif /* SIMPLERECT_FAST_RENDERER */
682 gnome_canvas_simplerect_draw (GnomeCanvasItem *item,
683 GdkDrawable *drawable,
685 int width, int height)
687 fprintf (stderr, "please don't use the CanvasSimpleRect item in a non-aa Canvas\n");
692 gnome_canvas_simplerect_point (GnomeCanvasItem *item, double x, double y, int cx, int cy, GnomeCanvasItem **actual_item)
694 GnomeCanvasSimpleRect *simplerect;
695 double x1, y1, x2, y2;
698 simplerect = GNOME_CANVAS_SIMPLERECT (item);
702 /* Find the bounds for the rectangle plus its outline width */
704 gnome_canvas_simplerect_bounds (item, &x1, &y1, &x2, &y2);
706 /* Is point inside rectangle */
708 if ((x >= x1) && (y >= y1) && (x <= x2) && (y <= y2)) {
712 /* Point is outside rectangle */
728 return sqrt (dx * dx + dy * dy);