[cairo] Hit detect surface?

Bill Spitzak spitzak at d2.com
Wed Mar 1 15:03:34 PST 2006

Gustavo J. A. M. Carneiro wrote:

>   But there is a stronger argument against this approach: some
> applications are more interested in "how far is the mouse pointer from
> every object", or "what is the object nearest to the pointer" than "give
> me the object under the mouse pointer".  Because most times it is not
> comfortable for the user to have to place the pointer exactly over an
> pixel that happens to be painted by some object.  For instance, I
> remember how early nautilus used a very precise (to the pixel) hit
> detection for file icons; guess what, usability studies later showed
> that users were annoyed by the fact that clicking on a CD icon wouldn't
> work in some cases, particularly when the pointer is exactly over the
> hole :)

What I am proposing *is* to do this "nearest the pointer" type hit 

There is only one "pixel" in the surface, and the caller has to set the 
transformation so that the region of interest is mapped to the area of 
this pixel. You can thus hit-detect anything that draws into any 
parallelogram-shaped region, though typically a square around the mouse 
cursor will be used. If the square is 20 pixels on a side, this will 
pick up everything within 10 pixels of the mouse.

To find the nearest, you must draw several times, with smaller and 
smaller squares, until either you reach a minimum size, or only one 
object is detected (or it goes to zero, in which case you use the 
previous pass). (actually now that I think about it, it may be more 
efficient to start at the minimum size and go up until you hit something.)

More information about the cairo mailing list