+ struct state *st = (struct state *) closure;
+
+ if (event->xany.type == ButtonPress)
+ {
+ int i;
+ if (st->mouse_ball != -1) /* second down-click? drop the ball. */
+ {
+ st->mouse_ball = -1;
+ return True;
+ }
+ else
+ {
+ /* When trying to pick up a ball, first look for a click directly
+ inside the ball; but if we don't find it, expand the radius
+ outward until we find something nearby.
+ */
+ int x = event->xbutton.x;
+ int y = event->xbutton.y;
+ float max = 10 * (st->global_size ? st->global_size : MAX_SIZE);
+ float step = max / 100;
+ float r2;
+ for (r2 = step; r2 < max; r2 += step)
+ {
+ for (i = 0; i < st->npoints; i++)
+ {
+ float d = ((st->balls[i].x - x) * (st->balls[i].x - x) +
+ (st->balls[i].y - y) * (st->balls[i].y - y));
+ float r = st->balls[i].size;
+ if (r2 > r) r = r2;
+ if (d < r*r)
+ {
+ st->mouse_ball = i;
+ return True;
+ }
+ }
+ }
+ }
+ return True;
+ }
+ else if (event->xany.type == ButtonRelease) /* drop the ball */
+ {
+ st->mouse_ball = -1;
+ return True;
+ }
+
+
+