switch to using boost::signals2 instead of sigc++, at least for libardour. not finish...
[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         (void) pspec;
374
375         GnomeCanvasSimpleRect *simplerect;
376         int update = FALSE;
377         int bounds_changed = FALSE;
378         g_return_if_fail (object != NULL);
379         g_return_if_fail (GNOME_IS_CANVAS_SIMPLERECT (object));
380
381         simplerect = GNOME_CANVAS_SIMPLERECT (object);
382
383         switch (prop_id) {
384         case PROP_X1:
385                 if (simplerect->x1 != g_value_get_double (value)) {
386                         simplerect->x1 = g_value_get_double (value);
387                         bounds_changed = TRUE;
388                 }
389                 break;
390
391         case PROP_Y1:
392                 if (simplerect->y1 != g_value_get_double (value)) {
393                         simplerect->y1 = g_value_get_double (value);
394                         bounds_changed = TRUE;
395                 }
396                 break;
397
398         case PROP_X2:
399                 if (simplerect->x2 != g_value_get_double (value)) {
400                         simplerect->x2 = g_value_get_double (value);
401                         bounds_changed = TRUE;
402                 }
403                 break;
404
405         case PROP_Y2:
406                 if (simplerect->y2 != g_value_get_double (value)) {
407                         simplerect->y2 = g_value_get_double (value);
408                         bounds_changed = TRUE;
409                 }
410                 break;
411
412         case PROP_DRAW:
413                 if (simplerect->draw != g_value_get_boolean (value)) {
414                         simplerect->draw = g_value_get_boolean (value);
415                         update = TRUE;
416                 }
417                 break;
418
419
420         case PROP_FILL:
421                 if (simplerect->fill != g_value_get_boolean (value)) {
422                         simplerect->fill = g_value_get_boolean (value);
423                         update = TRUE;
424                 }
425                 break;
426
427         case PROP_FILL_COLOR_RGBA:
428                 if (simplerect->fill_color != g_value_get_uint(value)) {
429                         simplerect->fill_color = g_value_get_uint(value);
430                         update = TRUE;
431                 }
432                 break;
433
434         case PROP_OUTLINE_COLOR_RGBA:
435                 if (simplerect->outline_color != g_value_get_uint(value)) {
436                         simplerect->outline_color = g_value_get_uint(value);
437                         update = TRUE;
438                 }
439                 break;
440
441         case PROP_OUTLINE_PIXELS:
442                 if (simplerect->outline_pixels != g_value_get_uint(value)) {
443                         simplerect->outline_pixels = g_value_get_uint(value);
444                         update = TRUE;
445                 }
446                 break;
447
448         case PROP_OUTLINE_WHAT:
449                 if (simplerect->outline_what != g_value_get_uint(value)) {
450                         simplerect->outline_what = g_value_get_uint(value);
451                         update = TRUE;
452                 }
453                 break;
454
455         default:
456                 break;
457         }
458
459         simplerect->full_draw_on_update = update;
460
461         if (update || bounds_changed) {
462                 gnome_canvas_item_request_update (GNOME_CANVAS_ITEM(object));
463         }
464 }
465
466 static void
467 gnome_canvas_simplerect_get_property (GObject      *object,
468                                       guint         prop_id,
469                                       GValue       *value,
470                                       GParamSpec   *pspec)
471 {
472         GnomeCanvasSimpleRect *rect = GNOME_CANVAS_SIMPLERECT (object);
473
474         g_return_if_fail (object != NULL);
475         g_return_if_fail (GNOME_IS_CANVAS_SIMPLERECT (object));
476
477         switch (prop_id) {
478         case PROP_X1:
479                 g_value_set_double (value, rect->x1);
480                 break;
481         case PROP_X2:
482                 g_value_set_double (value, rect->x2);
483                 break;
484         case PROP_Y1:
485                 g_value_set_double (value, rect->y1);
486                 break;
487         case PROP_Y2:
488                 g_value_set_double (value, rect->y2);
489                 break;
490         case PROP_OUTLINE_WHAT:
491                 g_value_set_uint (value, rect->outline_what);
492                 break;
493         case PROP_FILL:
494                 g_value_set_boolean (value, rect->fill);
495                 break;
496         case PROP_OUTLINE_PIXELS:
497                 g_value_set_uint (value, rect->outline_pixels);
498                 break;
499         case PROP_FILL_COLOR_RGBA:
500                 g_value_set_uint (value, rect->fill_color);
501                 break;
502         case PROP_OUTLINE_COLOR_RGBA:
503                 g_value_set_uint (value, rect->outline_color);
504                 break;
505         case PROP_DRAW:
506                 g_value_set_boolean (value, rect->draw);
507                 break;
508
509         default:
510                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
511                 break;
512         }
513 }
514
515
516 static void
517 gnome_canvas_simplerect_update (GnomeCanvasItem *item, double *affine, ArtSVP *clip_path, int flags)
518 {
519         GnomeCanvasSimpleRect *simplerect;
520         unsigned char foo;
521
522         simplerect = GNOME_CANVAS_SIMPLERECT (item);
523
524         if (parent_class->update)
525                 (* parent_class->update) (item, affine, clip_path, flags);
526
527         gnome_canvas_simplerect_reset_bounds (item);
528
529         if (simplerect->full_draw_on_update) {
530                 gnome_canvas_request_redraw (item->canvas,
531                                            simplerect->bbox_ulx,
532                                            simplerect->bbox_uly,
533                                            simplerect->bbox_lrx+0.5,
534                                            simplerect->bbox_lry+0.5);
535                 simplerect->full_draw_on_update = FALSE;
536         }
537
538         UINT_TO_RGBA (simplerect->fill_color, &simplerect->fill_r, &simplerect->fill_g, &simplerect->fill_b, &simplerect->fill_a);
539         UINT_TO_RGBA (simplerect->outline_color, &simplerect->outline_r, &simplerect->outline_g, &simplerect->outline_b, &foo);
540 }
541
542 // this can be useful for debugging/understanding how the canvas redraws
543 // stuff.
544
545 #undef HARLEQUIN_DEBUGGING
546
547 #undef SIMPLERECT_FAST_RENDERER
548 #ifdef SIMPLERECT_FAST_RENDERER
549
550 static void
551 gnome_canvas_simplerect_render (GnomeCanvasItem *item,
552                               GnomeCanvasBuf *buf)
553 {
554         GnomeCanvasSimpleRect *simplerect;
555         int end, begin;
556         int ey, sy;
557         unsigned int i;
558         ArtIRect intersection;
559         ArtIRect self;
560
561         simplerect = GNOME_CANVAS_SIMPLERECT (item);
562
563         if (parent_class->render) {
564                 (*parent_class->render) (item, buf);
565         }
566
567         if (buf->is_bg) {
568
569 #ifdef HARLEQUIN_DEBUGGING
570                 gint randr, randg, randb;
571                 randr = random() % 255;
572                 randg = random() % 255;
573                 randb = random() % 255;
574                 PAINT_BOX(buf, randr, randg, randb, 255, buf->rect.x0, buf->rect.y0, buf->rect.x1, buf->rect.y1);
575 #endif
576                 gnome_canvas_buf_ensure_buf (buf);
577                 buf->is_bg = FALSE;
578         }
579
580         if (!simplerect->draw) {
581                 return;
582         }
583
584         self.x0 = simplerect->bbox_ulx;
585         self.y0 = simplerect->bbox_uly;
586         self.x1 = simplerect->bbox_lrx;
587         self.y1 = simplerect->bbox_lry;
588
589         art_irect_intersect (&intersection, &self, &buf->rect);
590
591         begin = MAX(simplerect->bbox_ulx, buf->rect.x0);
592         end = MIN((simplerect->bbox_lrx-1), buf->rect.x1);
593
594         sy = simplerect->bbox_uly;
595         ey = simplerect->bbox_lry-1;
596
597         if (simplerect->fill) {
598
599                 // this can be useful for debugging/understanding how the canvas redraws
600                 // stuff.
601
602 #ifdef HARLEQUIN_DEBUGGING
603                 gint randr, randg, randb;
604                 randr = random() % 255;
605                 randg = random() % 255;
606                 randb = random() % 255;
607                 PAINT_BOX(buf, randr, randg, randb, simplerect->fill_a, begin, sy, end, ey);
608 #else
609                 PAINT_BOX (buf, simplerect->fill_r, simplerect->fill_g, simplerect->fill_b, simplerect->fill_a,
610                            intersection.x0, intersection.y0,
611                            intersection.x1, intersection.y1);
612 #endif
613
614         }
615
616         for (i = 0; i < simplerect->outline_pixels; ++i) {
617
618                 if (simplerect->outline_what & 0x1) {
619                         if (begin == simplerect->bbox_ulx) {
620                                 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin + i, sy, ey);
621                         }
622                 }
623
624                 if (simplerect->outline_what & 0x2) {
625                         if (end == (simplerect->bbox_lrx - 1)) {
626                                 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, end - i, sy, ey + 1);
627                         }
628                 }
629
630                 if (simplerect->outline_what & 0x4) {
631                         PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end, sy+i);
632                 }
633
634                 if (simplerect->outline_what & 0x8) {
635                         PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end + 1, ey-i);
636                 }
637         }
638 }
639
640 #else /* SIMPLERECT_FAST_RENDERER */
641
642 static void
643 gnome_canvas_simplerect_render (GnomeCanvasItem *item,
644                               GnomeCanvasBuf *buf)
645 {
646         GnomeCanvasSimpleRect *simplerect;
647         int end, begin;
648         int ey, sy;
649         unsigned int i;
650
651         simplerect = GNOME_CANVAS_SIMPLERECT (item);
652
653         if (parent_class->render) {
654                 (*parent_class->render) (item, buf);
655         }
656
657         if (buf->is_bg) {
658
659 #ifdef HARLEQUIN_DEBUGGING
660                 gint randr, randg, randb;
661                 randr = random() % 255;
662                 randg = random() % 255;
663                 randb = random() % 255;
664                 PAINT_BOX(buf, randr, randg, randb, 255, buf->rect.x0, buf->rect.y0, buf->rect.x1, buf->rect.y1);
665 #endif
666                 gnome_canvas_buf_ensure_buf (buf);
667                 buf->is_bg = FALSE;
668         }
669
670         if (!simplerect->draw) {
671                 return;
672         }
673
674         begin = MAX(simplerect->bbox_ulx,buf->rect.x0);
675         end = MIN((simplerect->bbox_lrx-1),buf->rect.x1);
676
677         sy = simplerect->bbox_uly;
678         ey = simplerect->bbox_lry-1;
679
680         if (simplerect->fill) {
681
682 #ifdef HARLEQUIN_DEBUGGING
683                 gint randr, randg, randb;
684                 randr = random() % 255;
685                 randg = random() % 255;
686                 randb = random() % 255;
687                 PAINT_BOX(buf, randr, randg, randb, simplerect->fill_a, begin, sy, end, ey);
688 #else
689                 PAINT_BOX(buf, simplerect->fill_r, simplerect->fill_g, simplerect->fill_b, simplerect->fill_a, begin, sy, end, ey);
690 #endif
691         }
692
693         for (i = 0; i < simplerect->outline_pixels; ++i) {
694
695                 if (simplerect->outline_what & 0x1) {
696                         if (begin == simplerect->bbox_ulx) {
697                                 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin + i, sy, ey);
698                         }
699                 }
700
701                 if (simplerect->outline_what & 0x2) {
702                         if (end == (simplerect->bbox_lrx - 1)) {
703                                 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, end - i, sy, ey + 1);
704                         }
705                 }
706
707                 if (simplerect->outline_what & 0x4) {
708                         PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end, sy+i);
709                 }
710
711                 if (simplerect->outline_what & 0x8) {
712                         PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end + 1, ey-i);
713                 }
714         }
715 }
716 #endif /* SIMPLERECT_FAST_RENDERER */
717
718 static void
719 gnome_canvas_simplerect_draw (GnomeCanvasItem *item,
720                               GdkDrawable *drawable,
721                               int x, int y,
722                               int width, int height)
723 {
724         GnomeCanvasSimpleRect *simplerect;
725         cairo_t* cr;
726         double ulx;
727         double uly;
728         double lrx;
729         double lry;
730
731         simplerect = GNOME_CANVAS_SIMPLERECT (item);
732
733         cr = gdk_cairo_create (drawable);
734
735         if (x > simplerect->bbox_ulx) {
736                 ulx = x;
737         } else {
738                 ulx = simplerect->bbox_ulx;
739         }
740
741         if (y > simplerect->bbox_uly) {
742                 uly = y;
743         } else {
744                 uly = simplerect->bbox_uly;
745         }
746
747         if (x + width > simplerect->bbox_lrx) {
748                 lrx = simplerect->bbox_lrx;
749         } else {
750                 lrx = x + width;
751         }
752
753         if (y + height > simplerect->bbox_lry) {
754                 lry = simplerect->bbox_lry;
755         } else {
756                 lry = y + height;
757         }
758
759         ulx -= x;
760         uly -= y;
761         lrx -= x;
762         lry -= y;
763
764         cairo_rectangle (cr, ulx, uly, lrx - ulx, lry - uly);
765
766         if (simplerect->fill) {
767                 cairo_set_source_rgba (cr,
768                                        simplerect->fill_r/255.0,
769                                        simplerect->fill_g/255.0,
770                                        simplerect->fill_b/255.0,
771                                        simplerect->fill_a/255.0);
772                 cairo_fill (cr);
773         }
774
775         if (simplerect->outline_what && simplerect->outline_pixels) {
776
777 #define x_in_range(a) (x <= (a) && (a) < x + width)
778 #define y_in_range(a) (y <= (a) && (a) < y + height)
779
780                 cairo_set_line_width (cr, simplerect->outline_pixels);
781
782                 cairo_set_source_rgb (cr,
783                                       simplerect->outline_r/255.0,
784                                       simplerect->outline_g/255.0,
785                                       simplerect->outline_b/255.0);
786
787                 if (simplerect->outline_what & 0x1) {
788                         /* left edge, if visible */
789                         if (x_in_range (simplerect->bbox_ulx)) {
790                                 cairo_move_to (cr, ulx+0.5, uly+0.5);
791                                 cairo_line_to (cr, ulx+0.5, lry+0.5);
792                                 cairo_stroke (cr);
793                         }
794                 }
795
796                 if (simplerect->outline_what & 0x2) {
797                         /* right edge, if visible */
798                         if (x_in_range (simplerect->bbox_lrx)) {
799                                 cairo_move_to (cr, lrx+0.5, uly+0.5);
800                                 cairo_line_to (cr, lrx+0.5, lry+0.5);
801                                 cairo_stroke (cr);
802                         }
803                 }
804
805                 if (simplerect->outline_what & 0x4) {
806                         /* top edge */
807                         if (y_in_range (simplerect->bbox_uly)) {
808                                 cairo_move_to (cr, ulx+0.5, uly+0.5);
809                                 cairo_line_to (cr, lrx+0.5, uly+0.5);
810                                 cairo_stroke (cr);
811                         }
812                 }
813
814                 if (simplerect->outline_what & 0x8) {
815                         /* bottom edge */
816                         if (y_in_range (simplerect->bbox_lry)) {
817                                 cairo_move_to (cr, ulx+0.5, lry+0.5);
818                                 cairo_line_to (cr, lrx+0.5, lry+0.5);
819                                 cairo_stroke (cr);
820                         }
821                 }
822         }
823
824         cairo_destroy (cr);
825 }
826
827 static double
828 gnome_canvas_simplerect_point (GnomeCanvasItem *item, double x, double y, int cx, int cy, GnomeCanvasItem **actual_item)
829 {
830         (void) cx;
831         (void) cy;
832
833         GnomeCanvasSimpleRect *simplerect;
834         double x1, y1, x2, y2;
835         double dx, dy;
836
837         simplerect = GNOME_CANVAS_SIMPLERECT (item);
838
839         *actual_item = item;
840
841         /* Find the bounds for the rectangle plus its outline width */
842
843         gnome_canvas_simplerect_bounds (item, &x1, &y1, &x2, &y2);
844
845         /* Is point inside rectangle */
846
847         if ((x >= x1) && (y >= y1) && (x <= x2) && (y <= y2)) {
848                 return 0.0;
849         }
850
851         /* Point is outside rectangle */
852
853         if (x < x1)
854                 dx = x1 - x;
855         else if (x > x2)
856                 dx = x - x2;
857         else
858                 dx = 0.0;
859
860         if (y < y1)
861                 dy = y1 - y;
862         else if (y > y2)
863                 dy = y - y2;
864         else
865                 dy = 0.0;
866
867         return sqrt (dx * dx + dy * dy);
868 }