tags:

views:

30

answers:

2

I've got a simple test app with a custom view (set up in Interface Builder) with its origin at (20, 20). When I get a mouse event at the lower left most point in the view, the event's location is being reported as (20, 21) and the converted point at (0, 1). I'm using pixie to make sure I'm clicking right at the lower left corner. (If I move one pixel down, I get nothing, indicating I'm outside the view.)

Here's the mouseDown code:

- (void)mouseDown:(NSEvent *)e
{
    NSPoint pt = [e locationInWindow];
    NSLog(@"Location in Window: %.0f, %.0f", pt.x, pt.y);

    pt = [self convertPoint:pt fromView:nil];

    NSLog(@"Converted Point: %.0f, %.0f", pt.x, pt.y);
}

Can anyone explain why the y position appears to be off by one?

A: 

Fencepost error? In your code, or the library or your use of the library? Rounding errors as you are doing integral pixel reporting of floating point data?

msw
Possibly a fencepost error, but I don't know where. Perhaps it's some artifact of the coordinate system that I'm not understanding. The rounding of the x/y values in the code above was just for convenience. If I report the full floating point value, it's an integral value (as I'd expect for mouse positions).
Scott
See my answer - event y values are 1-based.
Joshua Nozzi
+2  A: 

This is correct behavior. From the Cocoa Drawing Guide:

Important: Cocoa event objects return y coordinate values that are 1-based instead of 0-based. Thus, a mouse click on the bottom left corner of a window or view would yield the point (0, 1) in Cocoa and not (0, 0). Only y-coordinates are 1-based.

Joshua Nozzi
Thank you! I'd love to know why it's done this way, but at least it's intentional, documented behavior.
Scott