Delete trailing whitespace
[ardour.git] / gtk2_ardour / canvas-simplerect.c
1 #include <stdio.h>
2 #include <math.h>
3 #include <cairo.h>
4 #include <libgnomecanvas/libgnomecanvas.h>
5
6 #include "canvas-simplerect.h"
7 #include "rgb_macros.h"
8 #include "gettext.h"
9 #define _(Text)  dgettext (PACKAGE,Text)
10
11 enum {
12         PROP_0,
13         PROP_X1,
14         PROP_Y1,
15         PROP_X2,
16         PROP_Y2,
17         PROP_OUTLINE_PIXELS,
18         PROP_OUTLINE_WHAT,
19         PROP_FILL,
20         PROP_FILL_COLOR_RGBA,
21         PROP_OUTLINE_COLOR_RGBA,
22         PROP_DRAW
23 };
24
25 static void   gnome_canvas_simplerect_class_init    (GnomeCanvasSimpleRectClass *class);
26
27 static void   gnome_canvas_simplerect_init          (GnomeCanvasSimpleRect      *simplerect);
28
29 static void   gnome_canvas_simplerect_destroy       (GtkObject                  *object);
30
31 static void   gnome_canvas_simplerect_set_property  (GObject        *object,
32                                                      guint            prop_id,
33                                                      const GValue   *value,
34                                                      GParamSpec     *pspec);
35
36 static void   gnome_canvas_simplerect_get_property  (GObject        *object,
37                                                      guint           prop_id,
38                                                      GValue         *value,
39                                                      GParamSpec     *pspec);
40
41 static void   gnome_canvas_simplerect_update        (GnomeCanvasItem *item,
42                                                      double          *affine,
43                                                      ArtSVP          *clip_path,
44                                                      int              flags);
45
46 static void   gnome_canvas_simplerect_bounds        (GnomeCanvasItem *item,
47                                                      double          *x1,
48                                                      double          *y1,
49                                                      double          *x2,
50                                                      double          *y2);
51
52 static double gnome_canvas_simplerect_point         (GnomeCanvasItem *item, double x, double y, int cx, int cy, GnomeCanvasItem **actual_item);
53
54 static void   gnome_canvas_simplerect_render        (GnomeCanvasItem *item, GnomeCanvasBuf *buf);
55
56 static void   gnome_canvas_simplerect_draw          (GnomeCanvasItem *item, GdkDrawable *drawable, int x, int y, int w, int h);
57
58 static GnomeCanvasItemClass *parent_class;
59
60
61 GType
62 gnome_canvas_simplerect_get_type (void)
63 {
64         static GType simplerect_type;
65
66         if (!simplerect_type) {
67                 static const GTypeInfo object_info = {
68                         sizeof (GnomeCanvasSimpleRectClass),
69                         (GBaseInitFunc) NULL,
70                         (GBaseFinalizeFunc) NULL,
71                         (GClassInitFunc) gnome_canvas_simplerect_class_init,
72                         (GClassFinalizeFunc) NULL,
73                         NULL,                   /* class_data */
74                         sizeof (GnomeCanvasSimpleRect),
75                         0,                      /* n_preallocs */
76                         (GInstanceInitFunc) gnome_canvas_simplerect_init,
77                         NULL                    /* value_table */
78                 };
79
80                 simplerect_type = g_type_register_static (GNOME_TYPE_CANVAS_ITEM, "GnomeCanvasSimpleRect",
81                                                           &object_info, 0);
82         }
83
84         return simplerect_type;
85 }
86
87 static void
88 gnome_canvas_simplerect_class_init (GnomeCanvasSimpleRectClass *class)
89 {
90         GObjectClass *gobject_class;
91         GtkObjectClass *object_class;
92         GnomeCanvasItemClass *item_class;
93
94         gobject_class = (GObjectClass *) class;
95         object_class = (GtkObjectClass *) class;
96         item_class = (GnomeCanvasItemClass *) class;
97
98         parent_class = g_type_class_peek_parent (class);
99
100         gobject_class->set_property = gnome_canvas_simplerect_set_property;
101         gobject_class->get_property = gnome_canvas_simplerect_get_property;
102
103         g_object_class_install_property (gobject_class,
104                                          PROP_X1,
105                                          g_param_spec_double ("x1",
106                                                               _("x1"),
107                                                               _("x coordinate of upper left corner of rect"),
108                                                               -G_MAXDOUBLE,
109                                                               G_MAXDOUBLE,
110                                                               0.0,
111                                                               G_PARAM_READWRITE));
112
113         g_object_class_install_property (gobject_class,
114                                          PROP_Y1,
115                                          g_param_spec_double ("y1",
116                                                               _("y1"),
117                                                               _("y coordinate of upper left corner of rect "),
118                                                               -G_MAXDOUBLE,
119                                                               G_MAXDOUBLE,
120                                                               0.0,
121                                                               G_PARAM_READWRITE));
122
123
124         g_object_class_install_property (gobject_class,
125                                          PROP_X2,
126                                          g_param_spec_double ("x2",
127                                                               _("x2"),
128                                                               _("x coordinate of lower right corner of rect"),
129                                                               -G_MAXDOUBLE,
130                                                               G_MAXDOUBLE,
131                                                               0.0,
132                                                               G_PARAM_READWRITE));
133
134         g_object_class_install_property (gobject_class,
135                                          PROP_Y2,
136                                          g_param_spec_double ("y2",
137                                                               _("y2"),
138                                                               _("y coordinate of lower right corner of rect "),
139                                                               -G_MAXDOUBLE,
140                                                               G_MAXDOUBLE,
141                                                               0.0,
142                                                               G_PARAM_READWRITE));
143
144
145         g_object_class_install_property (gobject_class,
146                                          PROP_OUTLINE_PIXELS,
147                                          g_param_spec_uint ("outline_pixels",
148                                                               _("outline pixels"),
149                                                               _("width in pixels of outline"),
150                                                               0,
151                                                               G_MAXUINT,
152                                                               0,
153                                                               G_PARAM_READWRITE));
154
155
156         g_object_class_install_property (gobject_class,
157                                          PROP_OUTLINE_WHAT,
158                                          g_param_spec_uint ("outline_what",
159                                                               _("outline what"),
160                                                               _("which boundaries to outline (mask)"),
161                                                               0,
162                                                               G_MAXUINT,
163                                                               0,
164                                                               G_PARAM_READWRITE));
165
166
167
168         g_object_class_install_property (gobject_class,
169                                          PROP_FILL,
170                                          g_param_spec_boolean ("fill",
171                                                                _("fill"),
172                                                                _("fill rectangle"),
173                                                                TRUE,
174                                                                G_PARAM_READWRITE));
175
176         g_object_class_install_property (gobject_class,
177                                          PROP_DRAW,
178                                          g_param_spec_boolean ("draw",
179                                                                _("draw"),
180                                                                _("draw rectangle"),
181                                                                TRUE,
182                                                                G_PARAM_READWRITE));
183
184
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"),
190                                                             0,
191                                                             G_MAXUINT,
192                                                             0,
193                                                             G_PARAM_READWRITE));
194
195
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"),
200                                                             _("color of fill"),
201                                                             0,
202                                                             G_MAXUINT,
203                                                             0,
204                                                             G_PARAM_READWRITE));
205
206         object_class->destroy = gnome_canvas_simplerect_destroy;
207
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;
213
214 }
215
216 static void
217 gnome_canvas_simplerect_init (GnomeCanvasSimpleRect *simplerect)
218 {
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;
230 }
231
232 static void
233 gnome_canvas_simplerect_destroy (GtkObject *object)
234 {
235         GnomeCanvasSimpleRect *rect;
236
237         g_return_if_fail (object != NULL);
238         g_return_if_fail (GNOME_IS_CANVAS_SIMPLERECT (object));
239
240         rect = GNOME_CANVAS_SIMPLERECT (object);
241
242         /* remember, destroy can be run multiple times! */
243
244         if (GTK_OBJECT_CLASS (parent_class)->destroy)
245               (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
246 }
247
248 static void
249 gnome_canvas_simplerect_bounds (GnomeCanvasItem *item, double *x1, double *y1, double *x2, double *y2)
250 {
251         GnomeCanvasSimpleRect *simplerect = GNOME_CANVAS_SIMPLERECT (item);
252
253         *x1 = simplerect->x1;
254         *y1 = simplerect->y1;
255         *x2 = simplerect->x2 + 1;
256         *y2 = simplerect->y2 + 1;
257 }
258
259
260 static void
261 gnome_canvas_simplerect_reset_bounds (GnomeCanvasItem *item)
262 {
263         GnomeCanvasSimpleRect* simplerect;
264         double x1, x2, y1, y2;
265         double old_x1, old_x2, old_y1, old_y2;
266         ArtDRect unionrect, old, new;
267
268         old_x1 = item->x1;
269         old_y1 = item->y1;
270         old_x2 = item->x2;
271         old_y2 = item->y2;
272
273         gnome_canvas_simplerect_bounds (item, &x1, &y1, &x2, &y2);
274         gnome_canvas_item_i2w (item, &x1, &y1);
275         gnome_canvas_item_i2w (item, &x2, &y2);
276
277         item->x1 = x1;
278         item->y1 = y1;
279         item->x2 = x2;
280         item->y2 = y2;
281
282         /* now compute bounding box in canvas units */
283
284         simplerect = GNOME_CANVAS_SIMPLERECT (item);
285
286         gnome_canvas_w2c (GNOME_CANVAS(item->canvas), x1, y1, &simplerect->bbox_ulx, &simplerect->bbox_uly);
287         gnome_canvas_w2c (GNOME_CANVAS(item->canvas), x2, y2, &simplerect->bbox_lrx, &simplerect->bbox_lry);
288
289         /* now queue redraws for changed areas */
290
291         if (item->x1 == old_x1 && item->x2 == old_x2) {
292
293                 /* no change in x-axis position */
294
295                 if (item->y1 == old_y1) {
296                         /* top didn't change, so just draw bottom */
297
298                         double start_y = MIN (item->y2, old_y2);
299                         double end_y = MAX (item->y2, old_y2);
300
301                         gnome_canvas_request_redraw (item->canvas, item->x1, start_y - 0.5, item->x2, end_y + 1.5);
302                         return;
303
304                 } else if (item->y2 == old_y2) {
305
306                         /* bottom didn't change, just draw top */
307
308                         double start_y = MIN (item->y1, old_y1);
309                         double end_y = MAX (item->y1, old_y1);
310
311                         gnome_canvas_request_redraw (item->canvas, item->x1, start_y - 0.5, item->x2, end_y + 1.5);
312                         return;
313
314                 }
315
316         } else if (item->y1 == old_y1 && item->y2 == old_y2) {
317
318                 /* no change in y-axis position */
319
320                 if (item->x1 == old_x1) {
321                         /* start didn't change, so just draw at the end */
322
323                         double start_x = MIN (item->x2, old_x2);
324                         double end_x = MAX (item->x2, old_x2);
325
326                         gnome_canvas_request_redraw (item->canvas, start_x - 0.5, item->y1, end_x + 1.5, item->y2 + 0.5);
327                         return;
328
329                 } else if (item->x2 == old_x2) {
330
331                         /* end didn't change, so just draw at the start */
332
333                         double start_x = MIN (item->x1, old_x1);
334                         double end_x = MAX (item->x1, old_x1);
335
336                         gnome_canvas_request_redraw (item->canvas, start_x - 0.5, item->y1, end_x + 1.5, item->y2 + 0.5);
337                         return;
338
339                 }
340         }
341
342         new.x0 = x1;
343         new.y0 = y1;
344         new.x1 = x2;
345         new.y1 = y2;
346
347         old.x0 = old_x1;
348         old.y0 = old_y1;
349         old.x1 = old_x2;
350         old.y1 = old_y2;
351
352         art_drect_union (&unionrect, &old, &new);
353         gnome_canvas_request_redraw (item->canvas,
354                                      unionrect.x0 - 0.5,
355                                      unionrect.y0 - 0.5,
356                                      unionrect.x1 + 1.5,
357                                      unionrect.y1 + 1.5);
358 }
359
360 /*
361  * CANVAS CALLBACKS
362  */
363
364 static void
365 gnome_canvas_simplerect_set_property (GObject      *object,
366                                       guint         prop_id,
367                                       const GValue *value,
368                                       GParamSpec   *pspec)
369
370 {
371         (void) pspec;
372
373         GnomeCanvasSimpleRect *simplerect;
374         int update = FALSE;
375         int bounds_changed = FALSE;
376         g_return_if_fail (object != NULL);
377         g_return_if_fail (GNOME_IS_CANVAS_SIMPLERECT (object));
378
379         simplerect = GNOME_CANVAS_SIMPLERECT (object);
380
381         switch (prop_id) {
382         case PROP_X1:
383                 if (simplerect->x1 != g_value_get_double (value)) {
384                         simplerect->x1 = g_value_get_double (value);
385                         bounds_changed = TRUE;
386                 }
387                 break;
388
389         case PROP_Y1:
390                 if (simplerect->y1 != g_value_get_double (value)) {
391                         simplerect->y1 = g_value_get_double (value);
392                         bounds_changed = TRUE;
393                 }
394                 break;
395
396         case PROP_X2:
397                 if (simplerect->x2 != g_value_get_double (value)) {
398                         simplerect->x2 = g_value_get_double (value);
399                         bounds_changed = TRUE;
400                 }
401                 break;
402
403         case PROP_Y2:
404                 if (simplerect->y2 != g_value_get_double (value)) {
405                         simplerect->y2 = g_value_get_double (value);
406                         bounds_changed = TRUE;
407                 }
408                 break;
409
410         case PROP_DRAW:
411                 if (simplerect->draw != g_value_get_boolean (value)) {
412                         simplerect->draw = g_value_get_boolean (value);
413                         update = TRUE;
414                 }
415                 break;
416
417
418         case PROP_FILL:
419                 if (simplerect->fill != g_value_get_boolean (value)) {
420                         simplerect->fill = g_value_get_boolean (value);
421                         update = TRUE;
422                 }
423                 break;
424
425         case PROP_FILL_COLOR_RGBA:
426                 if (simplerect->fill_color != g_value_get_uint(value)) {
427                         simplerect->fill_color = g_value_get_uint(value);
428                         update = TRUE;
429                 }
430                 break;
431
432         case PROP_OUTLINE_COLOR_RGBA:
433                 if (simplerect->outline_color != g_value_get_uint(value)) {
434                         simplerect->outline_color = g_value_get_uint(value);
435                         update = TRUE;
436                 }
437                 break;
438
439         case PROP_OUTLINE_PIXELS:
440                 if (simplerect->outline_pixels != g_value_get_uint(value)) {
441                         simplerect->outline_pixels = g_value_get_uint(value);
442                         update = TRUE;
443                 }
444                 break;
445
446         case PROP_OUTLINE_WHAT:
447                 if (simplerect->outline_what != g_value_get_uint(value)) {
448                         simplerect->outline_what = g_value_get_uint(value);
449                         update = TRUE;
450                 }
451                 break;
452
453         default:
454                 break;
455         }
456
457         if (!simplerect->full_draw_on_update) {
458                 /* XXX: not sure about this;
459                  *
460                  * I changed the next line to be conditional, rather than always
461                  * being executed.  Without the condition, the following bug occurs:
462                  *
463                  * caller sets a property (e.g. outline colour); this sets update = TRUE and hence full_draw_on_update = TRUE
464                  * update is requested (and it is intended, I suppose, that during this update, full_draw_on_update is noted)
465                  * ... update does not occur before ...
466                  * caller sets the same property again to the same value; this sets update = FALSE and hence full_draw_on_update = FALSE
467                  * update now occurs, but full_draw_on_update is FALSE, so the full redraw does not happen,
468                  * which results in graphical glitches.
469                  *
470                  * (Carl, 2/1/2010)
471                  */
472                 simplerect->full_draw_on_update = update;
473         }
474
475         if (update || bounds_changed) {
476                 gnome_canvas_item_request_update (GNOME_CANVAS_ITEM(object));
477         }
478 }
479
480 static void
481 gnome_canvas_simplerect_get_property (GObject      *object,
482                                       guint         prop_id,
483                                       GValue       *value,
484                                       GParamSpec   *pspec)
485 {
486         GnomeCanvasSimpleRect *rect = GNOME_CANVAS_SIMPLERECT (object);
487
488         g_return_if_fail (object != NULL);
489         g_return_if_fail (GNOME_IS_CANVAS_SIMPLERECT (object));
490
491         switch (prop_id) {
492         case PROP_X1:
493                 g_value_set_double (value, rect->x1);
494                 break;
495         case PROP_X2:
496                 g_value_set_double (value, rect->x2);
497                 break;
498         case PROP_Y1:
499                 g_value_set_double (value, rect->y1);
500                 break;
501         case PROP_Y2:
502                 g_value_set_double (value, rect->y2);
503                 break;
504         case PROP_OUTLINE_WHAT:
505                 g_value_set_uint (value, rect->outline_what);
506                 break;
507         case PROP_FILL:
508                 g_value_set_boolean (value, rect->fill);
509                 break;
510         case PROP_OUTLINE_PIXELS:
511                 g_value_set_uint (value, rect->outline_pixels);
512                 break;
513         case PROP_FILL_COLOR_RGBA:
514                 g_value_set_uint (value, rect->fill_color);
515                 break;
516         case PROP_OUTLINE_COLOR_RGBA:
517                 g_value_set_uint (value, rect->outline_color);
518                 break;
519         case PROP_DRAW:
520                 g_value_set_boolean (value, rect->draw);
521                 break;
522
523         default:
524                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
525                 break;
526         }
527 }
528
529
530 static void
531 gnome_canvas_simplerect_update (GnomeCanvasItem *item, double *affine, ArtSVP *clip_path, int flags)
532 {
533         GnomeCanvasSimpleRect *simplerect;
534
535         simplerect = GNOME_CANVAS_SIMPLERECT (item);
536
537         if (parent_class->update)
538                 (* parent_class->update) (item, affine, clip_path, flags);
539
540         gnome_canvas_simplerect_reset_bounds (item);
541
542         if (simplerect->full_draw_on_update) {
543                 gnome_canvas_request_redraw (item->canvas,
544                                              simplerect->bbox_ulx,
545                                              simplerect->bbox_uly,
546                                              simplerect->bbox_lrx+0.5,
547                                              simplerect->bbox_lry+0.5);
548                 simplerect->full_draw_on_update = FALSE;
549         }
550
551         UINT_TO_RGBA (simplerect->fill_color, &simplerect->fill_r, &simplerect->fill_g, &simplerect->fill_b, &simplerect->fill_a);
552         UINT_TO_RGBA (simplerect->outline_color, &simplerect->outline_r, &simplerect->outline_g, &simplerect->outline_b, &simplerect->outline_a);
553 }
554
555 // this can be useful for debugging/understanding how the canvas redraws
556 // stuff.
557
558 #undef HARLEQUIN_DEBUGGING
559
560 #undef SIMPLERECT_FAST_RENDERER
561 #ifdef SIMPLERECT_FAST_RENDERER
562
563 static void
564 gnome_canvas_simplerect_render (GnomeCanvasItem *item,
565                               GnomeCanvasBuf *buf)
566 {
567         GnomeCanvasSimpleRect *simplerect;
568         int end, begin;
569         int ey, sy;
570         unsigned int i;
571         ArtIRect intersection;
572         ArtIRect self;
573
574         simplerect = GNOME_CANVAS_SIMPLERECT (item);
575
576         if (parent_class->render) {
577                 (*parent_class->render) (item, buf);
578         }
579
580         if (buf->is_bg) {
581
582 #ifdef HARLEQUIN_DEBUGGING
583                 gint randr, randg, randb;
584                 randr = random() % 255;
585                 randg = random() % 255;
586                 randb = random() % 255;
587                 PAINT_BOX(buf, randr, randg, randb, 255, buf->rect.x0, buf->rect.y0, buf->rect.x1, buf->rect.y1);
588 #endif
589                 gnome_canvas_buf_ensure_buf (buf);
590                 buf->is_bg = FALSE;
591         }
592
593         if (!simplerect->draw) {
594                 return;
595         }
596
597         self.x0 = simplerect->bbox_ulx;
598         self.y0 = simplerect->bbox_uly;
599         self.x1 = simplerect->bbox_lrx;
600         self.y1 = simplerect->bbox_lry;
601
602         art_irect_intersect (&intersection, &self, &buf->rect);
603
604         begin = MAX(simplerect->bbox_ulx, buf->rect.x0);
605         end = MIN((simplerect->bbox_lrx-1), buf->rect.x1);
606
607         sy = simplerect->bbox_uly;
608         ey = simplerect->bbox_lry-1;
609
610         if (simplerect->fill) {
611
612                 // this can be useful for debugging/understanding how the canvas redraws
613                 // stuff.
614
615 #ifdef HARLEQUIN_DEBUGGING
616                 gint randr, randg, randb;
617                 randr = random() % 255;
618                 randg = random() % 255;
619                 randb = random() % 255;
620                 PAINT_BOX(buf, randr, randg, randb, simplerect->fill_a, begin, sy, end, ey);
621 #else
622                 PAINT_BOX (buf, simplerect->fill_r, simplerect->fill_g, simplerect->fill_b, simplerect->fill_a,
623                            intersection.x0, intersection.y0,
624                            intersection.x1, intersection.y1);
625 #endif
626
627         }
628
629         if (simplerect->outline_a > 0) {
630                 for (i = 0; i < simplerect->outline_pixels; ++i) {
631
632                         if (simplerect->outline_what & 0x1) {
633                                 if (begin == simplerect->bbox_ulx) {
634                                         PAINT_VERTA(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, simplerect->outline_a, begin + i, sy, ey);
635                                 }
636                         }
637
638                         if (simplerect->outline_what & 0x2) {
639                                 if (end == (simplerect->bbox_lrx - 1)) {
640                                         PAINT_VERTA(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, simplerect->outline_a, end - i, sy, ey + 1);
641                                 }
642                         }
643
644                         if (simplerect->outline_what & 0x4) {
645                                 PAINT_HORIZA(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, simplerect->outline_a, begin, end, sy+i);
646                         }
647
648                         if (simplerect->outline_what & 0x8) {
649                                 PAINT_HORIZA(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, simplerect->outline_a, begin, end + 1, ey-i);
650                         }
651                 }
652         }
653 }
654
655 #else /* SIMPLERECT_FAST_RENDERER */
656
657 static void
658 gnome_canvas_simplerect_render (GnomeCanvasItem *item,
659                               GnomeCanvasBuf *buf)
660 {
661         GnomeCanvasSimpleRect *simplerect;
662         int end, begin;
663         int ey, sy;
664         int i;
665
666         simplerect = GNOME_CANVAS_SIMPLERECT (item);
667
668         if (parent_class->render) {
669                 (*parent_class->render) (item, buf);
670         }
671
672         if (buf->is_bg) {
673
674 #ifdef HARLEQUIN_DEBUGGING
675                 gint randr, randg, randb;
676                 randr = random() % 255;
677                 randg = random() % 255;
678                 randb = random() % 255;
679                 PAINT_BOX(buf, randr, randg, randb, 255, buf->rect.x0, buf->rect.y0, buf->rect.x1, buf->rect.y1);
680 #endif
681                 gnome_canvas_buf_ensure_buf (buf);
682                 buf->is_bg = FALSE;
683         }
684
685         if (!simplerect->draw) {
686                 return;
687         }
688
689         begin = MAX(simplerect->bbox_ulx,buf->rect.x0);
690         end = MIN((simplerect->bbox_lrx-1),buf->rect.x1);
691
692         sy = simplerect->bbox_uly;
693         ey = simplerect->bbox_lry-1;
694
695         if (simplerect->fill) {
696
697 #ifdef HARLEQUIN_DEBUGGING
698                 gint randr, randg, randb;
699                 randr = random() % 255;
700                 randg = random() % 255;
701                 randb = random() % 255;
702                 PAINT_BOX(buf, randr, randg, randb, simplerect->fill_a, begin, sy, end, ey);
703 #else
704                 PAINT_BOX(buf, simplerect->fill_r, simplerect->fill_g, simplerect->fill_b, simplerect->fill_a, begin, sy, end, ey);
705 #endif
706         }
707
708         if (simplerect->outline_a) {
709                 for (i = 0; i < (int) simplerect->outline_pixels; ++i) {
710
711                         if (simplerect->outline_what & 0x1) {
712                                 if (begin == simplerect->bbox_ulx) {
713                                         PAINT_VERTA(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, simplerect->outline_a, begin + i, sy, ey);
714                                 }
715                         }
716
717                         if (simplerect->outline_what & 0x2) {
718                                 if (end == (simplerect->bbox_lrx - 1)) {
719                                         PAINT_VERTA(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, simplerect->outline_a, end - i, sy, ey + 1);
720                                 }
721                         }
722
723                         if (simplerect->outline_what & 0x4) {
724                                 PAINT_HORIZA(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, simplerect->outline_a, begin, end, sy+i);
725                         }
726
727                         if (simplerect->outline_what & 0x8) {
728                                 PAINT_HORIZA(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, simplerect->outline_a, begin, end + 1, ey-i);
729                         }
730                 }
731         }
732 }
733 #endif /* SIMPLERECT_FAST_RENDERER */
734
735 static void
736 gnome_canvas_simplerect_draw (GnomeCanvasItem *item,
737                               GdkDrawable *drawable,
738                               int x, int y,
739                               int width, int height)
740 {
741         GnomeCanvasSimpleRect *simplerect;
742         cairo_t* cr;
743         double ulx;
744         double uly;
745         double lrx;
746         double lry;
747
748         simplerect = GNOME_CANVAS_SIMPLERECT (item);
749
750         cr = gdk_cairo_create (drawable);
751
752         if (x > simplerect->bbox_ulx) {
753                 ulx = x;
754         } else {
755                 ulx = simplerect->bbox_ulx;
756         }
757
758         if (y > simplerect->bbox_uly) {
759                 uly = y;
760         } else {
761                 uly = simplerect->bbox_uly;
762         }
763
764         if (x + width > simplerect->bbox_lrx) {
765                 lrx = simplerect->bbox_lrx;
766         } else {
767                 lrx = x + width;
768         }
769
770         if (y + height > simplerect->bbox_lry) {
771                 lry = simplerect->bbox_lry;
772         } else {
773                 lry = y + height;
774         }
775
776         ulx -= x;
777         uly -= y;
778         lrx -= x;
779         lry -= y;
780
781         cairo_rectangle (cr, ulx, uly, lrx - ulx, lry - uly);
782
783         if (simplerect->fill) {
784                 cairo_set_source_rgba (cr,
785                                        simplerect->fill_r/255.0,
786                                        simplerect->fill_g/255.0,
787                                        simplerect->fill_b/255.0,
788                                        simplerect->fill_a/255.0);
789                 cairo_fill (cr);
790         }
791
792         if (simplerect->outline_what && simplerect->outline_pixels) {
793
794 #define x_in_range(a) (x <= (a) && (a) < x + width)
795 #define y_in_range(a) (y <= (a) && (a) < y + height)
796
797                 cairo_set_line_width (cr, simplerect->outline_pixels);
798
799                 cairo_set_source_rgb (cr,
800                                       simplerect->outline_r/255.0,
801                                       simplerect->outline_g/255.0,
802                                       simplerect->outline_b/255.0);
803
804                 if (simplerect->outline_what & 0x1) {
805                         /* left edge, if visible */
806                         if (x_in_range (simplerect->bbox_ulx)) {
807                                 cairo_move_to (cr, ulx+0.5, uly+0.5);
808                                 cairo_line_to (cr, ulx+0.5, lry+0.5);
809                                 cairo_stroke (cr);
810                         }
811                 }
812
813                 if (simplerect->outline_what & 0x2) {
814                         /* right edge, if visible */
815                         if (x_in_range (simplerect->bbox_lrx)) {
816                                 cairo_move_to (cr, lrx+0.5, uly+0.5);
817                                 cairo_line_to (cr, lrx+0.5, lry+0.5);
818                                 cairo_stroke (cr);
819                         }
820                 }
821
822                 if (simplerect->outline_what & 0x4) {
823                         /* top edge */
824                         if (y_in_range (simplerect->bbox_uly)) {
825                                 cairo_move_to (cr, ulx+0.5, uly+0.5);
826                                 cairo_line_to (cr, lrx+0.5, uly+0.5);
827                                 cairo_stroke (cr);
828                         }
829                 }
830
831                 if (simplerect->outline_what & 0x8) {
832                         /* bottom edge */
833                         if (y_in_range (simplerect->bbox_lry)) {
834                                 cairo_move_to (cr, ulx+0.5, lry+0.5);
835                                 cairo_line_to (cr, lrx+0.5, lry+0.5);
836                                 cairo_stroke (cr);
837                         }
838                 }
839         }
840
841         cairo_destroy (cr);
842 }
843
844 static double
845 gnome_canvas_simplerect_point (GnomeCanvasItem *item, double x, double y, int cx, int cy, GnomeCanvasItem **actual_item)
846 {
847         (void) cx;
848         (void) cy;
849
850         GnomeCanvasSimpleRect *simplerect;
851         double x1, y1, x2, y2;
852         double dx, dy;
853
854         simplerect = GNOME_CANVAS_SIMPLERECT (item);
855
856         *actual_item = item;
857
858         /* Find the bounds for the rectangle plus its outline width */
859
860         gnome_canvas_simplerect_bounds (item, &x1, &y1, &x2, &y2);
861
862         /* Is point inside rectangle */
863
864         if ((x >= x1) && (y >= y1) && (x <= x2) && (y <= y2)) {
865                 return 0.0;
866         }
867
868         /* Point is outside rectangle */
869
870         if (x < x1)
871                 dx = x1 - x;
872         else if (x > x2)
873                 dx = x - x2;
874         else
875                 dx = 0.0;
876
877         if (y < y1)
878                 dy = y1 - y;
879         else if (y > y2)
880                 dy = y - y2;
881         else
882                 dy = 0.0;
883
884         return sqrt (dx * dx + dy * dy);
885 }