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);
26 static void gnome_canvas_simplerect_init (GnomeCanvasSimpleRect *simplerect);
27 static void gnome_canvas_simplerect_set_property (GObject *object,
31 static void gnome_canvas_simplerect_get_property (GObject *object,
35 static void gnome_canvas_simplerect_update (GnomeCanvasItem *item, double *affine, ArtSVP *clip_path, int flags);
36 static void gnome_canvas_simplerect_bounds (GnomeCanvasItem *item, double *x1, double *y1, double *x2, double *y2);
37 static double gnome_canvas_simplerect_point (GnomeCanvasItem *item, double x, double y, int cx, int cy, GnomeCanvasItem **actual_item);
38 static void gnome_canvas_simplerect_render (GnomeCanvasItem *item, GnomeCanvasBuf *buf);
39 static void gnome_canvas_simplerect_draw (GnomeCanvasItem *item, GdkDrawable *drawable, int x, int y, int w, int h);
41 static GnomeCanvasItemClass *parent_class;
45 gnome_canvas_simplerect_get_type (void)
47 static GtkType simplerect_type = 0;
49 if (!simplerect_type) {
50 GtkTypeInfo simplerect_info = {
51 "GnomeCanvasSimpleRect",
52 sizeof (GnomeCanvasSimpleRect),
53 sizeof (GnomeCanvasSimpleRectClass),
54 (GtkClassInitFunc) gnome_canvas_simplerect_class_init,
55 (GtkObjectInitFunc) gnome_canvas_simplerect_init,
56 NULL, /* reserved_1 */
57 NULL, /* reserved_2 */
58 (GtkClassInitFunc) NULL
61 simplerect_type = gtk_type_unique (gnome_canvas_item_get_type (), &simplerect_info);
64 return simplerect_type;
68 gnome_canvas_simplerect_class_init (GnomeCanvasSimpleRectClass *class)
70 GObjectClass *object_class;
71 GnomeCanvasItemClass *item_class;
73 object_class = G_OBJECT_CLASS (class);
74 item_class = (GnomeCanvasItemClass *) class;
76 parent_class = gtk_type_class (gnome_canvas_item_get_type ());
78 object_class->set_property = gnome_canvas_simplerect_set_property;
79 object_class->get_property = gnome_canvas_simplerect_get_property;
81 g_object_class_install_property (object_class,
83 g_param_spec_double ("x1",
85 _("x coordinate of upper left corner of rect"),
91 g_object_class_install_property (object_class,
93 g_param_spec_double ("y1",
95 _("y coordinate of upper left corner of rect "),
102 g_object_class_install_property (object_class,
104 g_param_spec_double ("x2",
106 _("x coordinate of lower right corner of rect"),
112 g_object_class_install_property (object_class,
114 g_param_spec_double ("y2",
116 _("y coordinate of lower right corner of rect "),
123 g_object_class_install_property (object_class,
125 g_param_spec_uint ("outline_pixels",
127 _("width in pixels of outline"),
134 g_object_class_install_property (object_class,
136 g_param_spec_uint ("outline_what",
138 _("which boundaries to outline (mask)"),
146 g_object_class_install_property (object_class,
148 g_param_spec_boolean ("fill",
154 g_object_class_install_property (object_class,
156 g_param_spec_boolean ("draw",
163 g_object_class_install_property (object_class,
164 PROP_OUTLINE_COLOR_RGBA,
165 g_param_spec_uint ("outline_color_rgba",
166 _("outline color rgba"),
167 _("color of outline"),
174 g_object_class_install_property (object_class,
175 PROP_FILL_COLOR_RGBA,
176 g_param_spec_uint ("fill_color_rgba",
177 _("fill color rgba"),
184 item_class->update = gnome_canvas_simplerect_update;
185 item_class->bounds = gnome_canvas_simplerect_bounds;
186 item_class->point = gnome_canvas_simplerect_point;
187 item_class->render = gnome_canvas_simplerect_render;
188 item_class->draw = gnome_canvas_simplerect_draw;
192 gnome_canvas_simplerect_init (GnomeCanvasSimpleRect *simplerect)
194 simplerect->x1 = 0.0;
195 simplerect->y1 = 0.0;
196 simplerect->x2 = 0.0;
197 simplerect->y2 = 0.0;
198 simplerect->fill = TRUE;
199 simplerect->draw = TRUE;
200 simplerect->full_draw_on_update = TRUE;
201 simplerect->fill_color = 0;
202 simplerect->outline_color = 0;
203 simplerect->outline_pixels = 1;
204 simplerect->outline_what = 0xf;
207 // GNOME_CANVAS_ITEM(simplerect)->object.flags |= GNOME_CANVAS_ITEM_NO_AUTO_REDRAW;
211 gnome_canvas_simplerect_bounds (GnomeCanvasItem *item, double *x1, double *y1, double *x2, double *y2)
213 GnomeCanvasSimpleRect *simplerect = GNOME_CANVAS_SIMPLERECT (item);
215 *x1 = simplerect->x1;
216 *y1 = simplerect->y1;
217 *x2 = simplerect->x2 + 1;
218 *y2 = simplerect->y2 + 1;
223 gnome_canvas_simplerect_reset_bounds (GnomeCanvasItem *item)
225 GnomeCanvasSimpleRect* simplerect;
226 double x1, x2, y1, y2;
227 double old_x1, old_x2, old_y1, old_y2;
235 gnome_canvas_simplerect_bounds (item, &x1, &y1, &x2, &y2);
236 gnome_canvas_item_i2w (item, &x1, &y1);
237 gnome_canvas_item_i2w (item, &x2, &y2);
244 /* now compute bounding box in canvas units */
246 simplerect = GNOME_CANVAS_SIMPLERECT (item);
248 gnome_canvas_w2c (GNOME_CANVAS(item->canvas), x1, y1, &simplerect->bbox_ulx, &simplerect->bbox_uly);
249 gnome_canvas_w2c (GNOME_CANVAS(item->canvas), x2, y2, &simplerect->bbox_lrx, &simplerect->bbox_lry);
251 /* now queue redraws for changed areas */
253 if (item->x1 != old_x1) {
255 /* left edge changed. redraw the area that altered */
257 a = MIN(item->x1, old_x1);
258 b = MAX(item->x1, old_x1);
259 gnome_canvas_request_redraw (item->canvas, a - 1, item->y1, b + 1, item->y2);
262 if (item->x2 != old_x2) {
264 /* right edge changed. redraw the area that altered */
266 a = MIN(item->x2, old_x2);
267 b = MAX(item->x2, old_x2);
268 gnome_canvas_request_redraw (item->canvas, a - 1, item->y1, b + 1, item->y2);
271 if (item->y1 != old_y1) {
273 /* top edge changed. redraw the area that altered */
275 a = MIN(item->y1, old_y1);
276 b = MAX(item->y1, old_y1);
277 gnome_canvas_request_redraw (item->canvas, item->x1, a - 1, item->x2, b + 1);
280 if (item->y2 != old_y2) {
282 /* lower edge changed. redraw the area that altered */
284 a = MIN(item->y2, old_y2);
285 b = MAX(item->y2, old_y2);
286 gnome_canvas_request_redraw (item->canvas, item->x1, a - 1, item->x2, b + 1);
295 gnome_canvas_simplerect_set_property (GObject *object,
301 GnomeCanvasSimpleRect *simplerect;
303 int bounds_changed = FALSE;
305 simplerect = GNOME_CANVAS_SIMPLERECT (object);
309 if (simplerect->x1 != g_value_get_double (value)) {
310 simplerect->x1 = g_value_get_double (value);
311 bounds_changed = TRUE;
316 if (simplerect->y1 != g_value_get_double (value)) {
317 simplerect->y1 = g_value_get_double (value);
318 bounds_changed = TRUE;
323 if (simplerect->x2 != g_value_get_double (value)) {
324 simplerect->x2 = g_value_get_double (value);
325 bounds_changed = TRUE;
330 if (simplerect->y2 != g_value_get_double (value)) {
331 simplerect->y2 = g_value_get_double (value);
332 bounds_changed = TRUE;
337 if (simplerect->draw != g_value_get_boolean (value)) {
338 simplerect->draw = g_value_get_boolean (value);
345 if (simplerect->fill != g_value_get_boolean (value)) {
346 simplerect->fill = g_value_get_boolean (value);
351 case PROP_FILL_COLOR_RGBA:
352 if (simplerect->fill_color != g_value_get_uint(value)) {
353 simplerect->fill_color = g_value_get_uint(value);
358 case PROP_OUTLINE_COLOR_RGBA:
359 if (simplerect->outline_color != g_value_get_uint(value)) {
360 simplerect->outline_color = g_value_get_uint(value);
365 case PROP_OUTLINE_PIXELS:
366 if (simplerect->outline_pixels != g_value_get_uint(value)) {
367 simplerect->outline_pixels = g_value_get_uint(value);
372 case PROP_OUTLINE_WHAT:
373 if (simplerect->outline_what != g_value_get_uint(value)) {
374 simplerect->outline_what = g_value_get_uint(value);
383 simplerect->full_draw_on_update = update;
385 if (update || bounds_changed) {
386 gnome_canvas_item_request_update (GNOME_CANVAS_ITEM(object));
391 gnome_canvas_simplerect_get_property (GObject *object,
396 GnomeCanvasSimpleRect *rect = GNOME_CANVAS_SIMPLERECT (object);
400 g_value_set_double (value, rect->x1);
403 g_value_set_double (value, rect->x2);
406 g_value_set_double (value, rect->y1);
409 g_value_set_double (value, rect->y2);
411 case PROP_OUTLINE_WHAT:
412 g_value_set_uint (value, rect->outline_what);
415 g_value_set_boolean (value, rect->fill);
417 case PROP_OUTLINE_PIXELS:
418 g_value_set_uint (value, rect->outline_pixels);
420 case PROP_FILL_COLOR_RGBA:
421 g_value_set_uint (value, rect->fill_color);
423 case PROP_OUTLINE_COLOR_RGBA:
424 g_value_set_uint (value, rect->outline_color);
427 g_value_set_boolean (value, rect->draw);
431 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
438 gnome_canvas_simplerect_update (GnomeCanvasItem *item, double *affine, ArtSVP *clip_path, int flags)
440 GnomeCanvasSimpleRect *simplerect;
443 simplerect = GNOME_CANVAS_SIMPLERECT (item);
445 if (parent_class->update)
446 (* parent_class->update) (item, affine, clip_path, flags);
448 gnome_canvas_simplerect_reset_bounds (item);
450 if (simplerect->full_draw_on_update) {
451 gnome_canvas_request_redraw (item->canvas,
452 simplerect->bbox_ulx,
453 simplerect->bbox_uly,
454 simplerect->bbox_lrx+1,
455 simplerect->bbox_lry+1);
456 simplerect->full_draw_on_update = FALSE;
459 UINT_TO_RGBA (simplerect->fill_color, &simplerect->fill_r, &simplerect->fill_g, &simplerect->fill_b, &simplerect->fill_a);
460 UINT_TO_RGBA (simplerect->outline_color, &simplerect->outline_r, &simplerect->outline_g, &simplerect->outline_b, &foo);
463 #define SIMPLERECT_FAST_RENDERER
464 #ifdef SIMPLERECT_FAST_RENDERER
467 gnome_canvas_simplerect_render (GnomeCanvasItem *item,
470 GnomeCanvasSimpleRect *simplerect;
474 ArtIRect intersection;
477 simplerect = GNOME_CANVAS_SIMPLERECT (item);
479 if (parent_class->render) {
480 (*parent_class->render) (item, buf);
485 // this can be useful for debugging/understanding how the canvas redraws
488 // gint randr, randg, randb;
489 // randr = random() % 255;
490 // randg = random() % 255;
491 // randb = random() % 255;
492 // PAINT_BOX(buf, randr, randg, randb, 255, buf->rect.x0, buf->rect.y0, buf->rect.x1, buf->rect.y1);
494 gnome_canvas_buf_ensure_buf (buf);
498 if (!simplerect->draw) {
502 self.x0 = simplerect->bbox_ulx;
503 self.y0 = simplerect->bbox_uly;
504 self.x1 = simplerect->bbox_lrx;
505 self.y1 = simplerect->bbox_lry;
507 art_irect_intersect (&intersection, &self, &buf->rect);
509 begin = MAX(simplerect->bbox_ulx, buf->rect.x0);
510 end = MIN((simplerect->bbox_lrx-1), buf->rect.x1);
512 sy = simplerect->bbox_uly;
513 ey = simplerect->bbox_lry-1;
515 if (simplerect->fill) {
517 // this can be useful for debugging/understanding how the canvas redraws
520 // gint randr, randg, randb;
521 // randr = random() % 255;
522 // randg = random() % 255;
523 // randb = random() % 255;
524 // PAINT_BOX(buf, randr, randg, randb, simplerect->fill_a, begin, sy, end, ey);
526 FAST_PAINT_BOX (buf, simplerect->fill_r, simplerect->fill_g, simplerect->fill_b, simplerect->fill_a,
527 intersection.x0, intersection.y0,
528 intersection.x1, intersection.y1);
532 for (i = 0; i < simplerect->outline_pixels; ++i) {
534 if (simplerect->outline_what & 0x1) {
535 if (begin == simplerect->bbox_ulx) {
536 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin + i, sy, ey);
540 if (simplerect->outline_what & 0x2) {
541 if (end == (simplerect->bbox_lrx - 1)) {
542 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, end - i, sy, ey + 1);
546 if (simplerect->outline_what & 0x4) {
547 PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end, sy+i);
550 if (simplerect->outline_what & 0x8) {
551 PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end + 1, ey-i);
556 #else /* SIMPLERECT_FAST_RENDERER */
559 gnome_canvas_simplerect_render (GnomeCanvasItem *item,
562 GnomeCanvasSimpleRect *simplerect;
567 simplerect = GNOME_CANVAS_SIMPLERECT (item);
569 if (parent_class->render) {
570 (*parent_class->render) (item, buf);
575 // this can be useful for debugging/understanding how the canvas redraws
578 // gint randr, randg, randb;
579 // randr = random() % 255;
580 // randg = random() % 255;
581 // randb = random() % 255;
582 // PAINT_BOX(buf, randr, randg, randb, 255, buf->rect.x0, buf->rect.y0, buf->rect.x1, buf->rect.y1);
584 gnome_canvas_buf_ensure_buf (buf);
588 if (!simplerect->draw) {
592 begin = MAX(simplerect->bbox_ulx,buf->rect.x0);
593 end = MIN((simplerect->bbox_lrx-1),buf->rect.x1);
595 sy = simplerect->bbox_uly;
596 ey = simplerect->bbox_lry-1;
598 if (simplerect->fill) {
600 // this can be useful for debugging/understanding how the canvas redraws
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, begin, sy, end, ey);
612 for (i = 0; i < simplerect->outline_pixels; ++i) {
614 if (simplerect->outline_what & 0x1) {
615 if (begin == simplerect->bbox_ulx) {
616 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin + i, sy, ey);
620 if (simplerect->outline_what & 0x2) {
621 if (end == (simplerect->bbox_lrx - 1)) {
622 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, end - i, sy, ey + 1);
626 if (simplerect->outline_what & 0x4) {
627 PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end, sy+i);
630 if (simplerect->outline_what & 0x8) {
631 PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end + 1, ey-i);
635 #endif /* SIMPLERECT_FAST_RENDERER */
638 gnome_canvas_simplerect_draw (GnomeCanvasItem *item,
639 GdkDrawable *drawable,
641 int width, int height)
643 fprintf (stderr, "please don't use the CanvasSimpleRect item in a non-aa Canvas\n");
648 gnome_canvas_simplerect_point (GnomeCanvasItem *item, double x, double y, int cx, int cy, GnomeCanvasItem **actual_item)
650 GnomeCanvasSimpleRect *simplerect;
651 double x1, y1, x2, y2;
654 simplerect = GNOME_CANVAS_SIMPLERECT (item);
658 /* Find the bounds for the rectangle plus its outline width */
660 gnome_canvas_simplerect_bounds (item, &x1, &y1, &x2, &y2);
662 /* Is point inside rectangle */
664 if ((x >= x1) && (y >= y1) && (x <= x2) && (y <= y2)) {
668 /* Point is outside rectangle */
684 return sqrt (dx * dx + dy * dy);