make autoscroll a bit easier to use, fix missing measure lines when moving the editor...
[ardour.git] / gtk2_ardour / canvas-simplerect.c
1 #include <stdio.h>
2 #include <math.h>
3 #include <libgnomecanvas/libgnomecanvas.h>
4
5 #include "canvas-simplerect.h"
6 #include "rgb_macros.h"
7 #include "gettext.h"
8 #define _(Text)  dgettext (PACKAGE,Text)
9
10 enum {
11         PROP_0,
12         PROP_X1,
13         PROP_Y1,
14         PROP_X2,
15         PROP_Y2,
16         PROP_OUTLINE_PIXELS,
17         PROP_OUTLINE_WHAT,
18         PROP_FILL,
19         PROP_FILL_COLOR_RGBA,
20         PROP_OUTLINE_COLOR_RGBA,
21         PROP_DRAW
22         
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         double a, b, c, d;
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                 a = MIN(item->x1, old_x1); 
292                 b = MAX(item->x1, old_x1);
293
294                 a = MIN(a, item->x2);
295                 a = MIN(a, old_x2);
296                 b = MAX(b, item->x2);
297                 b = MAX(b, old_x2);
298
299                 c = MIN(item->y1, old_y1);
300                 d = MAX(item->y1, old_y1);
301
302                 c = MIN(c,item->y2);
303                 c = MIN(c, old_y2);
304                 d = MAX(d,item->y2);
305                 d = MAX(d, old_y2);
306
307                 gnome_canvas_request_redraw (item->canvas, a, c, b + 0.5, d + 0.5);
308 }
309
310 /* 
311  * CANVAS CALLBACKS 
312  */
313
314 static void
315 gnome_canvas_simplerect_set_property (GObject      *object,
316                                       guint         prop_id,
317                                       const GValue *value,
318                                       GParamSpec   *pspec)
319
320 {
321         GnomeCanvasSimpleRect *simplerect;
322         int update = FALSE;
323         int bounds_changed = FALSE;
324         g_return_if_fail (object != NULL);
325         g_return_if_fail (GNOME_IS_CANVAS_SIMPLERECT (object));
326
327         simplerect = GNOME_CANVAS_SIMPLERECT (object);
328
329         switch (prop_id) {
330         case PROP_X1:
331                 if (simplerect->x1 != g_value_get_double (value)) {
332                         simplerect->x1 = g_value_get_double (value);
333                         bounds_changed = TRUE;
334                 }
335                 break;
336
337         case PROP_Y1:
338                 if (simplerect->y1 != g_value_get_double (value)) {
339                         simplerect->y1 = g_value_get_double (value);
340                         bounds_changed = TRUE;
341                 }
342                 break;
343
344         case PROP_X2:
345                 if (simplerect->x2 != g_value_get_double (value)) {
346                         simplerect->x2 = g_value_get_double (value);
347                         bounds_changed = TRUE;
348                 }
349                 break;
350
351         case PROP_Y2:
352                 if (simplerect->y2 != g_value_get_double (value)) {
353                         simplerect->y2 = g_value_get_double (value);
354                         bounds_changed = TRUE;
355                 }
356                 break;
357
358         case PROP_DRAW:
359                 if (simplerect->draw != g_value_get_boolean (value)) {
360                         simplerect->draw = g_value_get_boolean (value);
361                         update = TRUE;
362                 }
363                 break;
364
365
366         case PROP_FILL:
367                 if (simplerect->fill != g_value_get_boolean (value)) {
368                         simplerect->fill = g_value_get_boolean (value);
369                         update = TRUE;
370                 }
371                 break;
372
373         case PROP_FILL_COLOR_RGBA:
374                 if (simplerect->fill_color != g_value_get_uint(value)) {
375                         simplerect->fill_color = g_value_get_uint(value);
376                         update = TRUE;
377                 }
378                 break;
379
380         case PROP_OUTLINE_COLOR_RGBA:
381                 if (simplerect->outline_color != g_value_get_uint(value)) {
382                         simplerect->outline_color = g_value_get_uint(value);
383                         update = TRUE;
384                 }
385                 break;
386
387         case PROP_OUTLINE_PIXELS:
388                 if (simplerect->outline_pixels != g_value_get_uint(value)) {
389                         simplerect->outline_pixels = g_value_get_uint(value);
390                         update = TRUE;
391                 }
392                 break;
393
394         case PROP_OUTLINE_WHAT:
395                 if (simplerect->outline_what != g_value_get_uint(value)) {
396                         simplerect->outline_what = g_value_get_uint(value);
397                         update = TRUE;
398                 }
399                 break;
400
401         default:
402                 break;
403         }
404
405         simplerect->full_draw_on_update = update;
406
407         if (update || bounds_changed) {
408                 gnome_canvas_item_request_update (GNOME_CANVAS_ITEM(object));
409         }
410 }
411
412 static void
413 gnome_canvas_simplerect_get_property (GObject      *object,
414                                       guint         prop_id,
415                                       GValue       *value,
416                                       GParamSpec   *pspec)
417 {
418         GnomeCanvasSimpleRect *rect = GNOME_CANVAS_SIMPLERECT (object);
419         
420         g_return_if_fail (object != NULL);
421         g_return_if_fail (GNOME_IS_CANVAS_SIMPLERECT (object));
422
423         switch (prop_id) {
424         case PROP_X1:
425                 g_value_set_double (value, rect->x1);
426                 break;
427         case PROP_X2:
428                 g_value_set_double (value, rect->x2);
429                 break;
430         case PROP_Y1:
431                 g_value_set_double (value, rect->y1);
432                 break;
433         case PROP_Y2:
434                 g_value_set_double (value, rect->y2);
435                 break;
436         case PROP_OUTLINE_WHAT:
437                 g_value_set_uint (value, rect->outline_what);
438                 break;
439         case PROP_FILL:
440                 g_value_set_boolean (value, rect->fill);
441                 break;
442         case PROP_OUTLINE_PIXELS:
443                 g_value_set_uint (value, rect->outline_pixels);
444                 break;
445         case PROP_FILL_COLOR_RGBA:
446                 g_value_set_uint (value, rect->fill_color);
447                 break;
448         case PROP_OUTLINE_COLOR_RGBA:
449                 g_value_set_uint (value, rect->outline_color);
450                 break;
451         case PROP_DRAW:
452                 g_value_set_boolean (value, rect->draw);
453                 break;
454                 
455         default:
456                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
457                 break;
458         }
459 }
460
461
462 static void
463 gnome_canvas_simplerect_update (GnomeCanvasItem *item, double *affine, ArtSVP *clip_path, int flags)
464 {
465         GnomeCanvasSimpleRect *simplerect;
466         unsigned char foo;
467
468         simplerect = GNOME_CANVAS_SIMPLERECT (item);
469
470         if (parent_class->update)
471                 (* parent_class->update) (item, affine, clip_path, flags);
472
473         gnome_canvas_simplerect_reset_bounds (item);
474
475         if (simplerect->full_draw_on_update) {
476                 gnome_canvas_request_redraw (item->canvas, 
477                                            simplerect->bbox_ulx,
478                                            simplerect->bbox_uly,
479                                            simplerect->bbox_lrx+0.5,
480                                            simplerect->bbox_lry+0.5);
481                 simplerect->full_draw_on_update = FALSE;
482         }
483
484         UINT_TO_RGBA (simplerect->fill_color, &simplerect->fill_r, &simplerect->fill_g, &simplerect->fill_b, &simplerect->fill_a);
485         UINT_TO_RGBA (simplerect->outline_color, &simplerect->outline_r, &simplerect->outline_g, &simplerect->outline_b, &foo);
486 }
487
488 // this can be useful for debugging/understanding how the canvas redraws
489 // stuff.
490
491 #undef HARLEQUIN_DEBUGGING
492
493 #undef SIMPLERECT_FAST_RENDERER
494 #ifdef SIMPLERECT_FAST_RENDERER
495
496 static void
497 gnome_canvas_simplerect_render (GnomeCanvasItem *item,
498                               GnomeCanvasBuf *buf)
499 {
500         GnomeCanvasSimpleRect *simplerect;
501         int end, begin;
502         int ey, sy;
503         unsigned int i;
504         ArtIRect intersection;
505         ArtIRect self;
506
507         simplerect = GNOME_CANVAS_SIMPLERECT (item);
508
509         if (parent_class->render) {
510                 (*parent_class->render) (item, buf);
511         }
512
513         if (buf->is_bg) {
514
515 #ifdef HARLEQUIN_DEBUGGING
516                 gint randr, randg, randb;
517                 randr = random() % 255;
518                 randg = random() % 255;
519                 randb = random() % 255;
520                 PAINT_BOX(buf, randr, randg, randb, 255, buf->rect.x0, buf->rect.y0, buf->rect.x1, buf->rect.y1);
521 #endif
522                 gnome_canvas_buf_ensure_buf (buf);
523                 buf->is_bg = FALSE;
524         }
525
526         if (!simplerect->draw) {
527                 return;
528         }
529         
530         self.x0 = simplerect->bbox_ulx;
531         self.y0 = simplerect->bbox_uly;
532         self.x1 = simplerect->bbox_lrx;
533         self.y1 = simplerect->bbox_lry;
534
535         art_irect_intersect (&intersection, &self, &buf->rect);
536
537         begin = MAX(simplerect->bbox_ulx, buf->rect.x0);
538         end = MIN((simplerect->bbox_lrx-1), buf->rect.x1);
539
540         sy = simplerect->bbox_uly;
541         ey = simplerect->bbox_lry-1;
542
543         if (simplerect->fill) {
544                 
545                 // this can be useful for debugging/understanding how the canvas redraws
546                 // stuff.
547
548 #ifdef HARLEQUIN_DEBUGGING
549                 gint randr, randg, randb;
550                 randr = random() % 255;
551                 randg = random() % 255;
552                 randb = random() % 255;
553                 PAINT_BOX(buf, randr, randg, randb, simplerect->fill_a, begin, sy, end, ey);
554 #else           
555                 PAINT_BOX (buf, simplerect->fill_r, simplerect->fill_g, simplerect->fill_b, simplerect->fill_a, 
556                            intersection.x0, intersection.y0,
557                            intersection.x1, intersection.y1);
558 #endif
559                 
560         }
561
562         for (i = 0; i < simplerect->outline_pixels; ++i) {
563
564                 if (simplerect->outline_what & 0x1) {
565                         if (begin == simplerect->bbox_ulx) {
566                                 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin + i, sy, ey);
567                         }
568                 }
569
570                 if (simplerect->outline_what & 0x2) {
571                         if (end == (simplerect->bbox_lrx - 1)) {
572                                 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, end - i, sy, ey + 1);
573                         }
574                 }
575
576                 if (simplerect->outline_what & 0x4) {
577                         PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end, sy+i);
578                 }
579         
580                 if (simplerect->outline_what & 0x8) {
581                         PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end + 1, ey-i);
582                 }
583         }
584 }
585
586 #else /* SIMPLERECT_FAST_RENDERER */
587
588 static void
589 gnome_canvas_simplerect_render (GnomeCanvasItem *item,
590                               GnomeCanvasBuf *buf)
591 {
592         GnomeCanvasSimpleRect *simplerect;
593         int end, begin;
594         int ey, sy;
595         unsigned int i;
596
597         simplerect = GNOME_CANVAS_SIMPLERECT (item);
598
599         if (parent_class->render) {
600                 (*parent_class->render) (item, buf);
601         }
602
603         if (buf->is_bg) {
604
605 #ifdef HARLEQUIN_DEBUGGING
606                 gint randr, randg, randb;
607                 randr = random() % 255;
608                 randg = random() % 255;
609                 randb = random() % 255;
610                 PAINT_BOX(buf, randr, randg, randb, 255, buf->rect.x0, buf->rect.y0, buf->rect.x1, buf->rect.y1);
611 #endif
612                 gnome_canvas_buf_ensure_buf (buf);
613                 buf->is_bg = FALSE;
614         }
615
616         if (!simplerect->draw) {
617                 return;
618         }
619
620         begin = MAX(simplerect->bbox_ulx,buf->rect.x0);
621         end = MIN((simplerect->bbox_lrx-1),buf->rect.x1);
622
623         sy = simplerect->bbox_uly;
624         ey = simplerect->bbox_lry-1;
625
626         if (simplerect->fill) {
627                 
628 #ifdef HARLEQUIN_DEBUGGING
629                 gint randr, randg, randb;
630                 randr = random() % 255;
631                 randg = random() % 255;
632                 randb = random() % 255;
633                 PAINT_BOX(buf, randr, randg, randb, simplerect->fill_a, begin, sy, end, ey);
634 #else           
635                 PAINT_BOX(buf, simplerect->fill_r, simplerect->fill_g, simplerect->fill_b, simplerect->fill_a, begin, sy, end, ey);
636 #endif
637         }
638
639         for (i = 0; i < simplerect->outline_pixels; ++i) {
640
641                 if (simplerect->outline_what & 0x1) {
642                         if (begin == simplerect->bbox_ulx) {
643                                 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin + i, sy, ey);
644                         }
645                 }
646
647                 if (simplerect->outline_what & 0x2) {
648                         if (end == (simplerect->bbox_lrx - 1)) {
649                                 PAINT_VERT(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, end - i, sy, ey + 1);
650                         }
651                 }
652
653                 if (simplerect->outline_what & 0x4) {
654                         PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end, sy+i);
655                 }
656         
657                 if (simplerect->outline_what & 0x8) {
658                         PAINT_HORIZ(buf, simplerect->outline_r, simplerect->outline_g, simplerect->outline_b, begin, end + 1, ey-i);
659                 }
660         }
661 }
662 #endif /* SIMPLERECT_FAST_RENDERER */
663
664 static void
665 gnome_canvas_simplerect_draw (GnomeCanvasItem *item,
666                             GdkDrawable *drawable,
667                             int x, int y,
668                             int width, int height)
669 {
670         fprintf (stderr, "please don't use the CanvasSimpleRect item in a non-aa Canvas\n");
671         abort ();
672 }
673
674 static double
675 gnome_canvas_simplerect_point (GnomeCanvasItem *item, double x, double y, int cx, int cy, GnomeCanvasItem **actual_item)
676 {
677         GnomeCanvasSimpleRect *simplerect;
678         double x1, y1, x2, y2;
679         double dx, dy;
680
681         simplerect = GNOME_CANVAS_SIMPLERECT (item);
682
683         *actual_item = item;
684
685         /* Find the bounds for the rectangle plus its outline width */
686
687         gnome_canvas_simplerect_bounds (item, &x1, &y1, &x2, &y2);
688
689         /* Is point inside rectangle */
690         
691         if ((x >= x1) && (y >= y1) && (x <= x2) && (y <= y2)) {
692                 return 0.0;
693         }
694
695         /* Point is outside rectangle */
696
697         if (x < x1)
698                 dx = x1 - x;
699         else if (x > x2)
700                 dx = x - x2;
701         else
702                 dx = 0.0;
703
704         if (y < y1)
705                 dy = y1 - y;
706         else if (y > y2)
707                 dy = y - y2;
708         else
709                 dy = 0.0;
710
711         return sqrt (dx * dx + dy * dy);
712 }