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