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;
305 simplerect = GNOME_CANVAS_SIMPLERECT (object);
308 bounds_changed = FALSE;
312 if (simplerect->x1 != g_value_get_double (value)) {
313 simplerect->x1 = g_value_get_double (value);
314 bounds_changed = TRUE;
319 if (simplerect->y1 != g_value_get_double (value)) {
320 simplerect->y1 = g_value_get_double (value);
321 bounds_changed = TRUE;
326 if (simplerect->x2 != g_value_get_double (value)) {
327 simplerect->x2 = g_value_get_double (value);
328 bounds_changed = TRUE;
333 if (simplerect->y2 != g_value_get_double (value)) {
334 simplerect->y2 = g_value_get_double (value);
335 bounds_changed = TRUE;
340 if (simplerect->draw != g_value_get_boolean (value)) {
341 simplerect->draw = g_value_get_boolean (value);
348 if (simplerect->fill != g_value_get_boolean (value)) {
349 simplerect->fill = g_value_get_boolean (value);
354 case PROP_FILL_COLOR_RGBA:
355 if (simplerect->fill_color != g_value_get_uint(value)) {
356 simplerect->fill_color = g_value_get_uint(value);
361 case PROP_OUTLINE_COLOR_RGBA:
362 if (simplerect->outline_color != g_value_get_uint(value)) {
363 simplerect->outline_color = g_value_get_uint(value);
368 case PROP_OUTLINE_PIXELS:
369 if (simplerect->outline_pixels != g_value_get_uint(value)) {
370 simplerect->outline_pixels = g_value_get_uint(value);
375 case PROP_OUTLINE_WHAT:
376 if (simplerect->outline_what != g_value_get_uint(value)) {
377 simplerect->outline_what = g_value_get_uint(value);
386 simplerect->full_draw_on_update = update;
388 if (update || bounds_changed) {
389 gnome_canvas_item_request_update (GNOME_CANVAS_ITEM(object));
394 gnome_canvas_simplerect_get_property (GObject *object,
399 GnomeCanvasSimpleRect *rect = GNOME_CANVAS_SIMPLERECT (object);
403 g_value_set_double (value, rect->x1);
406 g_value_set_double (value, rect->x2);
409 g_value_set_double (value, rect->y1);
412 g_value_set_double (value, rect->y2);
414 case PROP_OUTLINE_WHAT:
415 g_value_set_uint (value, rect->outline_what);
418 g_value_set_boolean (value, rect->fill);
420 case PROP_OUTLINE_PIXELS:
421 g_value_set_uint (value, rect->outline_pixels);
423 case PROP_FILL_COLOR_RGBA:
424 g_value_set_uint (value, rect->fill_color);
426 case PROP_OUTLINE_COLOR_RGBA:
427 g_value_set_uint (value, rect->outline_color);
430 g_value_set_boolean (value, rect->draw);
434 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
441 gnome_canvas_simplerect_update (GnomeCanvasItem *item, double *affine, ArtSVP *clip_path, int flags)
443 GnomeCanvasSimpleRect *simplerect;
446 simplerect = GNOME_CANVAS_SIMPLERECT (item);
448 if (parent_class->update)
449 (* parent_class->update) (item, affine, clip_path, flags);
451 gnome_canvas_simplerect_reset_bounds (item);
453 if (simplerect->full_draw_on_update) {
454 gnome_canvas_request_redraw (item->canvas,
455 simplerect->bbox_ulx,
456 simplerect->bbox_uly,
457 simplerect->bbox_lrx+1,
458 simplerect->bbox_lry+1);
459 simplerect->full_draw_on_update = FALSE;
462 UINT_TO_RGBA (simplerect->fill_color, &simplerect->fill_r, &simplerect->fill_g, &simplerect->fill_b, &simplerect->fill_a);
463 UINT_TO_RGBA (simplerect->outline_color, &simplerect->outline_r, &simplerect->outline_g, &simplerect->outline_b, &foo);
466 #define SIMPLERECT_FAST_RENDERER
467 #ifdef SIMPLERECT_FAST_RENDERER
470 gnome_canvas_simplerect_render (GnomeCanvasItem *item,
473 GnomeCanvasSimpleRect *simplerect;
477 ArtIRect intersection;
480 simplerect = GNOME_CANVAS_SIMPLERECT (item);
482 if (parent_class->render) {
483 (*parent_class->render) (item, buf);
488 // this can be useful for debugging/understanding how the canvas redraws
491 // gint randr, randg, randb;
492 // randr = random() % 255;
493 // randg = random() % 255;
494 // randb = random() % 255;
495 // PAINT_BOX(buf, randr, randg, randb, 255, buf->rect.x0, buf->rect.y0, buf->rect.x1, buf->rect.y1);
497 gnome_canvas_buf_ensure_buf (buf);
501 if (!simplerect->draw) {
505 self.x0 = simplerect->bbox_ulx;
506 self.y0 = simplerect->bbox_uly;
507 self.x1 = simplerect->bbox_lrx;
508 self.y1 = simplerect->bbox_lry;
510 art_irect_intersect (&intersection, &self, &buf->rect);
512 begin = MAX(simplerect->bbox_ulx, buf->rect.x0);
513 end = MIN((simplerect->bbox_lrx-1), buf->rect.x1);
515 sy = simplerect->bbox_uly;
516 ey = simplerect->bbox_lry-1;
518 if (simplerect->fill) {
520 // this can be useful for debugging/understanding how the canvas redraws
523 // gint randr, randg, randb;
524 // randr = random() % 255;
525 // randg = random() % 255;
526 // randb = random() % 255;
527 // PAINT_BOX(buf, randr, randg, randb, simplerect->fill_a, begin, sy, end, ey);
529 FAST_PAINT_BOX (buf, simplerect->fill_r, simplerect->fill_g, simplerect->fill_b, simplerect->fill_a,
530 intersection.x0, intersection.y0,
531 intersection.x1, intersection.y1);
535 for (i = 0; i < simplerect->outline_pixels; ++i) {
537 if (simplerect->outline_what & 0x1) {
538 if (begin == simplerect->bbox_ulx) {
539 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin + i, sy, ey);
543 if (simplerect->outline_what & 0x2) {
544 if (end == (simplerect->bbox_lrx - 1)) {
545 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, end - i, sy, ey + 1);
549 if (simplerect->outline_what & 0x4) {
550 PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end, sy+i);
553 if (simplerect->outline_what & 0x8) {
554 PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end + 1, ey-i);
559 #else /* SIMPLERECT_FAST_RENDERER */
562 gnome_canvas_simplerect_render (GnomeCanvasItem *item,
565 GnomeCanvasSimpleRect *simplerect;
570 simplerect = GNOME_CANVAS_SIMPLERECT (item);
572 if (parent_class->render) {
573 (*parent_class->render) (item, buf);
578 // this can be useful for debugging/understanding how the canvas redraws
581 // gint randr, randg, randb;
582 // randr = random() % 255;
583 // randg = random() % 255;
584 // randb = random() % 255;
585 // PAINT_BOX(buf, randr, randg, randb, 255, buf->rect.x0, buf->rect.y0, buf->rect.x1, buf->rect.y1);
587 gnome_canvas_buf_ensure_buf (buf);
591 if (!simplerect->draw) {
595 begin = MAX(simplerect->bbox_ulx,buf->rect.x0);
596 end = MIN((simplerect->bbox_lrx-1),buf->rect.x1);
598 sy = simplerect->bbox_uly;
599 ey = simplerect->bbox_lry-1;
601 if (simplerect->fill) {
603 // this can be useful for debugging/understanding how the canvas redraws
606 // gint randr, randg, randb;
607 // randr = random() % 255;
608 // randg = random() % 255;
609 // randb = random() % 255;
610 // PAINT_BOX(buf, randr, randg, randb, simplerect->fill_a, begin, sy, end, ey);
612 PAINT_BOX(buf, simplerect->fill_r, simplerect->fill_g, simplerect->fill_b, simplerect->fill_a, begin, sy, end, ey);
615 for (i = 0; i < simplerect->outline_pixels; ++i) {
617 if (simplerect->outline_what & 0x1) {
618 if (begin == simplerect->bbox_ulx) {
619 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin + i, sy, ey);
623 if (simplerect->outline_what & 0x2) {
624 if (end == (simplerect->bbox_lrx - 1)) {
625 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, end - i, sy, ey + 1);
629 if (simplerect->outline_what & 0x4) {
630 PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end, sy+i);
633 if (simplerect->outline_what & 0x8) {
634 PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end + 1, ey-i);
638 #endif /* SIMPLERECT_FAST_RENDERER */
641 gnome_canvas_simplerect_draw (GnomeCanvasItem *item,
642 GdkDrawable *drawable,
644 int width, int height)
646 fprintf (stderr, "please don't use the CanvasSimpleRect item in a non-aa Canvas\n");
651 gnome_canvas_simplerect_point (GnomeCanvasItem *item, double x, double y, int cx, int cy, GnomeCanvasItem **actual_item)
653 GnomeCanvasSimpleRect *simplerect;
654 double x1, y1, x2, y2;
657 simplerect = GNOME_CANVAS_SIMPLERECT (item);
661 /* Find the bounds for the rectangle plus its outline width */
663 gnome_canvas_simplerect_bounds (item, &x1, &y1, &x2, &y2);
665 /* Is point inside rectangle */
667 if ((x >= x1) && (y >= y1) && (x <= x2) && (y <= y2)) {
671 /* Point is outside rectangle */
687 return sqrt (dx * dx + dy * dy);