4 #include <libgnomecanvas/libgnomecanvas.h>
6 #include "canvas-simplerect.h"
7 #include "rgb_macros.h"
9 #define _(Text) dgettext (PACKAGE,Text)
21 PROP_OUTLINE_COLOR_RGBA,
26 static void gnome_canvas_simplerect_class_init (GnomeCanvasSimpleRectClass *class);
28 static void gnome_canvas_simplerect_init (GnomeCanvasSimpleRect *simplerect);
30 static void gnome_canvas_simplerect_destroy (GtkObject *object);
32 static void gnome_canvas_simplerect_set_property (GObject *object,
37 static void gnome_canvas_simplerect_get_property (GObject *object,
42 static void gnome_canvas_simplerect_update (GnomeCanvasItem *item,
47 static void gnome_canvas_simplerect_bounds (GnomeCanvasItem *item,
53 static double gnome_canvas_simplerect_point (GnomeCanvasItem *item, double x, double y, int cx, int cy, GnomeCanvasItem **actual_item);
55 static void gnome_canvas_simplerect_render (GnomeCanvasItem *item, GnomeCanvasBuf *buf);
57 static void gnome_canvas_simplerect_draw (GnomeCanvasItem *item, GdkDrawable *drawable, int x, int y, int w, int h);
59 static GnomeCanvasItemClass *parent_class;
63 gnome_canvas_simplerect_get_type (void)
65 static GType simplerect_type;
67 if (!simplerect_type) {
68 static const GTypeInfo object_info = {
69 sizeof (GnomeCanvasSimpleRectClass),
71 (GBaseFinalizeFunc) NULL,
72 (GClassInitFunc) gnome_canvas_simplerect_class_init,
73 (GClassFinalizeFunc) NULL,
74 NULL, /* class_data */
75 sizeof (GnomeCanvasSimpleRect),
77 (GInstanceInitFunc) gnome_canvas_simplerect_init,
78 NULL /* value_table */
81 simplerect_type = g_type_register_static (GNOME_TYPE_CANVAS_ITEM, "GnomeCanvasSimpleRect",
85 return simplerect_type;
89 gnome_canvas_simplerect_class_init (GnomeCanvasSimpleRectClass *class)
91 GObjectClass *gobject_class;
92 GtkObjectClass *object_class;
93 GnomeCanvasItemClass *item_class;
95 gobject_class = (GObjectClass *) class;
96 object_class = (GtkObjectClass *) class;
97 item_class = (GnomeCanvasItemClass *) class;
99 parent_class = g_type_class_peek_parent (class);
101 gobject_class->set_property = gnome_canvas_simplerect_set_property;
102 gobject_class->get_property = gnome_canvas_simplerect_get_property;
104 g_object_class_install_property (gobject_class,
106 g_param_spec_double ("x1",
108 _("x coordinate of upper left corner of rect"),
114 g_object_class_install_property (gobject_class,
116 g_param_spec_double ("y1",
118 _("y coordinate of upper left corner of rect "),
125 g_object_class_install_property (gobject_class,
127 g_param_spec_double ("x2",
129 _("x coordinate of lower right corner of rect"),
135 g_object_class_install_property (gobject_class,
137 g_param_spec_double ("y2",
139 _("y coordinate of lower right corner of rect "),
146 g_object_class_install_property (gobject_class,
148 g_param_spec_uint ("outline_pixels",
150 _("width in pixels of outline"),
157 g_object_class_install_property (gobject_class,
159 g_param_spec_uint ("outline_what",
161 _("which boundaries to outline (mask)"),
169 g_object_class_install_property (gobject_class,
171 g_param_spec_boolean ("fill",
177 g_object_class_install_property (gobject_class,
179 g_param_spec_boolean ("draw",
186 g_object_class_install_property (gobject_class,
187 PROP_OUTLINE_COLOR_RGBA,
188 g_param_spec_uint ("outline_color_rgba",
189 _("outline color rgba"),
190 _("color of outline"),
197 g_object_class_install_property (gobject_class,
198 PROP_FILL_COLOR_RGBA,
199 g_param_spec_uint ("fill_color_rgba",
200 _("fill color rgba"),
207 object_class->destroy = gnome_canvas_simplerect_destroy;
209 item_class->update = gnome_canvas_simplerect_update;
210 item_class->draw = gnome_canvas_simplerect_draw;
211 item_class->bounds = gnome_canvas_simplerect_bounds;
212 item_class->point = gnome_canvas_simplerect_point;
213 item_class->render = gnome_canvas_simplerect_render;
218 gnome_canvas_simplerect_init (GnomeCanvasSimpleRect *simplerect)
220 simplerect->x1 = 0.0;
221 simplerect->y1 = 0.0;
222 simplerect->x2 = 0.0;
223 simplerect->y2 = 0.0;
224 simplerect->fill = TRUE;
225 simplerect->draw = TRUE;
226 simplerect->full_draw_on_update = TRUE;
227 simplerect->fill_color = 0;
228 simplerect->outline_color = 0;
229 simplerect->outline_pixels = 1;
230 simplerect->outline_what = 0xf;
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;
263 gnome_canvas_simplerect_reset_bounds (GnomeCanvasItem *item)
265 GnomeCanvasSimpleRect* simplerect;
266 double x1, x2, y1, y2;
267 double old_x1, old_x2, old_y1, old_y2;
268 ArtDRect unionrect, old, new;
275 gnome_canvas_simplerect_bounds (item, &x1, &y1, &x2, &y2);
276 gnome_canvas_item_i2w (item, &x1, &y1);
277 gnome_canvas_item_i2w (item, &x2, &y2);
284 /* now compute bounding box in canvas units */
286 simplerect = GNOME_CANVAS_SIMPLERECT (item);
288 gnome_canvas_w2c (GNOME_CANVAS(item->canvas), x1, y1, &simplerect->bbox_ulx, &simplerect->bbox_uly);
289 gnome_canvas_w2c (GNOME_CANVAS(item->canvas), x2, y2, &simplerect->bbox_lrx, &simplerect->bbox_lry);
291 /* now queue redraws for changed areas */
293 if (item->x1 == old_x1 && item->x2 == old_x2) {
295 /* no change in x-axis position */
297 if (item->y1 == old_y1) {
298 /* top didn't change, so just draw bottom */
300 double start_y = MIN (item->y2, old_y2);
301 double end_y = MAX (item->y2, old_y2);
303 gnome_canvas_request_redraw (item->canvas, item->x1, start_y - 0.5, item->x2, end_y + 1.5);
306 } else if (item->y2 == old_y2) {
308 /* bottom didn't change, just draw top */
310 double start_y = MIN (item->y1, old_y1);
311 double end_y = MAX (item->y1, old_y1);
313 gnome_canvas_request_redraw (item->canvas, item->x1, start_y - 0.5, item->x2, end_y + 1.5);
318 } else if (item->y1 == old_y1 && item->y2 == old_y2) {
320 /* no change in y-axis position */
322 if (item->x1 == old_x1) {
323 /* start didn't change, so just draw at the end */
325 double start_x = MIN (item->x2, old_x2);
326 double end_x = MAX (item->x2, old_x2);
328 gnome_canvas_request_redraw (item->canvas, start_x - 0.5, item->y1, end_x + 1.5, item->y2);
331 } else if (item->x2 == old_x2) {
333 /* end didn't change, so just draw at the start */
335 double start_x = MIN (item->x1, old_x1);
336 double end_x = MAX (item->x1, old_x1);
338 gnome_canvas_request_redraw (item->canvas, start_x - 0.5, item->y1, end_x + 1.5, item->y2 + 0.5);
354 art_drect_union (&unionrect, &old, &new);
355 gnome_canvas_request_redraw (item->canvas,
367 gnome_canvas_simplerect_set_property (GObject *object,
375 GnomeCanvasSimpleRect *simplerect;
377 int bounds_changed = FALSE;
378 g_return_if_fail (object != NULL);
379 g_return_if_fail (GNOME_IS_CANVAS_SIMPLERECT (object));
381 simplerect = GNOME_CANVAS_SIMPLERECT (object);
385 if (simplerect->x1 != g_value_get_double (value)) {
386 simplerect->x1 = g_value_get_double (value);
387 bounds_changed = TRUE;
392 if (simplerect->y1 != g_value_get_double (value)) {
393 simplerect->y1 = g_value_get_double (value);
394 bounds_changed = TRUE;
399 if (simplerect->x2 != g_value_get_double (value)) {
400 simplerect->x2 = g_value_get_double (value);
401 bounds_changed = TRUE;
406 if (simplerect->y2 != g_value_get_double (value)) {
407 simplerect->y2 = g_value_get_double (value);
408 bounds_changed = TRUE;
413 if (simplerect->draw != g_value_get_boolean (value)) {
414 simplerect->draw = g_value_get_boolean (value);
421 if (simplerect->fill != g_value_get_boolean (value)) {
422 simplerect->fill = g_value_get_boolean (value);
427 case PROP_FILL_COLOR_RGBA:
428 if (simplerect->fill_color != g_value_get_uint(value)) {
429 simplerect->fill_color = g_value_get_uint(value);
434 case PROP_OUTLINE_COLOR_RGBA:
435 if (simplerect->outline_color != g_value_get_uint(value)) {
436 simplerect->outline_color = g_value_get_uint(value);
441 case PROP_OUTLINE_PIXELS:
442 if (simplerect->outline_pixels != g_value_get_uint(value)) {
443 simplerect->outline_pixels = g_value_get_uint(value);
448 case PROP_OUTLINE_WHAT:
449 if (simplerect->outline_what != g_value_get_uint(value)) {
450 simplerect->outline_what = g_value_get_uint(value);
459 simplerect->full_draw_on_update = update;
461 if (update || bounds_changed) {
462 gnome_canvas_item_request_update (GNOME_CANVAS_ITEM(object));
467 gnome_canvas_simplerect_get_property (GObject *object,
472 GnomeCanvasSimpleRect *rect = GNOME_CANVAS_SIMPLERECT (object);
474 g_return_if_fail (object != NULL);
475 g_return_if_fail (GNOME_IS_CANVAS_SIMPLERECT (object));
479 g_value_set_double (value, rect->x1);
482 g_value_set_double (value, rect->x2);
485 g_value_set_double (value, rect->y1);
488 g_value_set_double (value, rect->y2);
490 case PROP_OUTLINE_WHAT:
491 g_value_set_uint (value, rect->outline_what);
494 g_value_set_boolean (value, rect->fill);
496 case PROP_OUTLINE_PIXELS:
497 g_value_set_uint (value, rect->outline_pixels);
499 case PROP_FILL_COLOR_RGBA:
500 g_value_set_uint (value, rect->fill_color);
502 case PROP_OUTLINE_COLOR_RGBA:
503 g_value_set_uint (value, rect->outline_color);
506 g_value_set_boolean (value, rect->draw);
510 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
517 gnome_canvas_simplerect_update (GnomeCanvasItem *item, double *affine, ArtSVP *clip_path, int flags)
519 GnomeCanvasSimpleRect *simplerect;
522 simplerect = GNOME_CANVAS_SIMPLERECT (item);
524 if (parent_class->update)
525 (* parent_class->update) (item, affine, clip_path, flags);
527 gnome_canvas_simplerect_reset_bounds (item);
529 if (simplerect->full_draw_on_update) {
530 gnome_canvas_request_redraw (item->canvas,
531 simplerect->bbox_ulx,
532 simplerect->bbox_uly,
533 simplerect->bbox_lrx+0.5,
534 simplerect->bbox_lry+0.5);
535 simplerect->full_draw_on_update = FALSE;
538 UINT_TO_RGBA (simplerect->fill_color, &simplerect->fill_r, &simplerect->fill_g, &simplerect->fill_b, &simplerect->fill_a);
539 UINT_TO_RGBA (simplerect->outline_color, &simplerect->outline_r, &simplerect->outline_g, &simplerect->outline_b, &foo);
542 // this can be useful for debugging/understanding how the canvas redraws
545 #undef HARLEQUIN_DEBUGGING
547 #undef SIMPLERECT_FAST_RENDERER
548 #ifdef SIMPLERECT_FAST_RENDERER
551 gnome_canvas_simplerect_render (GnomeCanvasItem *item,
554 GnomeCanvasSimpleRect *simplerect;
558 ArtIRect intersection;
561 simplerect = GNOME_CANVAS_SIMPLERECT (item);
563 if (parent_class->render) {
564 (*parent_class->render) (item, buf);
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, 255, buf->rect.x0, buf->rect.y0, buf->rect.x1, buf->rect.y1);
576 gnome_canvas_buf_ensure_buf (buf);
580 if (!simplerect->draw) {
584 self.x0 = simplerect->bbox_ulx;
585 self.y0 = simplerect->bbox_uly;
586 self.x1 = simplerect->bbox_lrx;
587 self.y1 = simplerect->bbox_lry;
589 art_irect_intersect (&intersection, &self, &buf->rect);
591 begin = MAX(simplerect->bbox_ulx, buf->rect.x0);
592 end = MIN((simplerect->bbox_lrx-1), buf->rect.x1);
594 sy = simplerect->bbox_uly;
595 ey = simplerect->bbox_lry-1;
597 if (simplerect->fill) {
599 // this can be useful for debugging/understanding how the canvas redraws
602 #ifdef HARLEQUIN_DEBUGGING
603 gint randr, randg, randb;
604 randr = random() % 255;
605 randg = random() % 255;
606 randb = random() % 255;
607 PAINT_BOX(buf, randr, randg, randb, simplerect->fill_a, begin, sy, end, ey);
609 PAINT_BOX (buf, simplerect->fill_r, simplerect->fill_g, simplerect->fill_b, simplerect->fill_a,
610 intersection.x0, intersection.y0,
611 intersection.x1, intersection.y1);
616 for (i = 0; i < simplerect->outline_pixels; ++i) {
618 if (simplerect->outline_what & 0x1) {
619 if (begin == simplerect->bbox_ulx) {
620 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin + i, sy, ey);
624 if (simplerect->outline_what & 0x2) {
625 if (end == (simplerect->bbox_lrx - 1)) {
626 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, end - i, sy, ey + 1);
630 if (simplerect->outline_what & 0x4) {
631 PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end, sy+i);
634 if (simplerect->outline_what & 0x8) {
635 PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end + 1, ey-i);
640 #else /* SIMPLERECT_FAST_RENDERER */
643 gnome_canvas_simplerect_render (GnomeCanvasItem *item,
646 GnomeCanvasSimpleRect *simplerect;
651 simplerect = GNOME_CANVAS_SIMPLERECT (item);
653 if (parent_class->render) {
654 (*parent_class->render) (item, buf);
659 #ifdef HARLEQUIN_DEBUGGING
660 gint randr, randg, randb;
661 randr = random() % 255;
662 randg = random() % 255;
663 randb = random() % 255;
664 PAINT_BOX(buf, randr, randg, randb, 255, buf->rect.x0, buf->rect.y0, buf->rect.x1, buf->rect.y1);
666 gnome_canvas_buf_ensure_buf (buf);
670 if (!simplerect->draw) {
674 begin = MAX(simplerect->bbox_ulx,buf->rect.x0);
675 end = MIN((simplerect->bbox_lrx-1),buf->rect.x1);
677 sy = simplerect->bbox_uly;
678 ey = simplerect->bbox_lry-1;
680 if (simplerect->fill) {
682 #ifdef HARLEQUIN_DEBUGGING
683 gint randr, randg, randb;
684 randr = random() % 255;
685 randg = random() % 255;
686 randb = random() % 255;
687 PAINT_BOX(buf, randr, randg, randb, simplerect->fill_a, begin, sy, end, ey);
689 PAINT_BOX(buf, simplerect->fill_r, simplerect->fill_g, simplerect->fill_b, simplerect->fill_a, begin, sy, end, ey);
693 for (i = 0; i < simplerect->outline_pixels; ++i) {
695 if (simplerect->outline_what & 0x1) {
696 if (begin == simplerect->bbox_ulx) {
697 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin + i, sy, ey);
701 if (simplerect->outline_what & 0x2) {
702 if (end == (simplerect->bbox_lrx - 1)) {
703 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, end - i, sy, ey + 1);
707 if (simplerect->outline_what & 0x4) {
708 PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end, sy+i);
711 if (simplerect->outline_what & 0x8) {
712 PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end + 1, ey-i);
716 #endif /* SIMPLERECT_FAST_RENDERER */
719 gnome_canvas_simplerect_draw (GnomeCanvasItem *item,
720 GdkDrawable *drawable,
722 int width, int height)
724 GnomeCanvasSimpleRect *simplerect;
731 simplerect = GNOME_CANVAS_SIMPLERECT (item);
733 cr = gdk_cairo_create (drawable);
735 if (x > simplerect->bbox_ulx) {
738 ulx = simplerect->bbox_ulx;
741 if (y > simplerect->bbox_uly) {
744 uly = simplerect->bbox_uly;
747 if (x + width > simplerect->bbox_lrx) {
748 lrx = simplerect->bbox_lrx;
753 if (y + height > simplerect->bbox_lry) {
754 lry = simplerect->bbox_lry;
764 cairo_rectangle (cr, ulx, uly, lrx - ulx, lry - uly);
766 if (simplerect->fill) {
767 cairo_set_source_rgba (cr,
768 simplerect->fill_r/255.0,
769 simplerect->fill_g/255.0,
770 simplerect->fill_b/255.0,
771 simplerect->fill_a/255.0);
775 if (simplerect->outline_what && simplerect->outline_pixels) {
777 #define x_in_range(a) (x <= (a) && (a) < x + width)
778 #define y_in_range(a) (y <= (a) && (a) < y + height)
780 cairo_set_line_width (cr, simplerect->outline_pixels);
782 cairo_set_source_rgb (cr,
783 simplerect->outline_r/255.0,
784 simplerect->outline_g/255.0,
785 simplerect->outline_b/255.0);
787 if (simplerect->outline_what & 0x1) {
788 /* left edge, if visible */
789 if (x_in_range (simplerect->bbox_ulx)) {
790 cairo_move_to (cr, ulx+0.5, uly+0.5);
791 cairo_line_to (cr, ulx+0.5, lry+0.5);
796 if (simplerect->outline_what & 0x2) {
797 /* right edge, if visible */
798 if (x_in_range (simplerect->bbox_lrx)) {
799 cairo_move_to (cr, lrx+0.5, uly+0.5);
800 cairo_line_to (cr, lrx+0.5, lry+0.5);
805 if (simplerect->outline_what & 0x4) {
807 if (y_in_range (simplerect->bbox_uly)) {
808 cairo_move_to (cr, ulx+0.5, uly+0.5);
809 cairo_line_to (cr, lrx+0.5, uly+0.5);
814 if (simplerect->outline_what & 0x8) {
816 if (y_in_range (simplerect->bbox_lry)) {
817 cairo_move_to (cr, ulx+0.5, lry+0.5);
818 cairo_line_to (cr, lrx+0.5, lry+0.5);
828 gnome_canvas_simplerect_point (GnomeCanvasItem *item, double x, double y, int cx, int cy, GnomeCanvasItem **actual_item)
833 GnomeCanvasSimpleRect *simplerect;
834 double x1, y1, x2, y2;
837 simplerect = GNOME_CANVAS_SIMPLERECT (item);
841 /* Find the bounds for the rectangle plus its outline width */
843 gnome_canvas_simplerect_bounds (item, &x1, &y1, &x2, &y2);
845 /* Is point inside rectangle */
847 if ((x >= x1) && (y >= y1) && (x <= x2) && (y <= y2)) {
851 /* Point is outside rectangle */
867 return sqrt (dx * dx + dy * dy);