• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Current mouse screen?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Current mouse screen?


  • Subject: Re: Current mouse screen?
  • From: "Louis C. Sacha" <email@hidden>
  • Date: Mon, 19 Apr 2004 22:45:38 -0700

Hello...

John Randolph wrote:
On Apr 18, 2004, at 2:42 AM, M. Uli Kusterer wrote:
...
That should be NSPointInRect() instead of NSMouseInRect(), AFAIK.

Actually, NSMouseInRect() still exists.. It accounts for flipped coordinate spaces, which NSPointInRect() doesn't.

-jcr

This is something which makes absolutely no sense to me (and in fact is one of the documentation bugs that has been on my bug reporting to do list for awhile). In terms of the way NSRect is defined and used in Cocoa, isn't it physically impossible for a specific point and a specific rect within the same two dimensional coordinate system to have a different relationship depending on whether the view is flipped or unflipped?

If the view is unflipped, the origin of the view is at the bottom left corner. Likewise, the origin of an NSRect in this coordinate system is at the bottom left, and the rectangle extends upward and to the right. By definition, the point {170.0,90.0) is inside the rect {{50.0,50.0}.{250.0,100.0}}.

If the view is flipped, the origin of the view is at the top left corner. Likewise, the origin of an NSRect in this view's coordinate system is at the top right, and the rectangle extends downward and to the right. Again, the point {170.0,90.0) is inside the rect {{50.0,50.0}.{250.0,100.0}}.


If you don't believe me, or prefer a visual example, try the following code in a test app:

- - - - - - - - - - - - - - - - - - - - - - - - -
@interface TestView : NSView {}
@end

@implementation TestView

- (BOOL)isFlipped {return FALSE;}

- (void)drawRect:(NSRect)rect
// Drawing code here.
{
[[NSColor whiteColor] set];
NSRectFill([self bounds]);

// rect
NSRect filled = NSMakeRect(50.0f,50.0f,250.0f,100.0f);
[[NSColor greenColor] set];
NSRectFill(filled);
// origin
filled = NSMakeRect(50.0f,50.0f,10.0f,10.0f);
[[NSColor redColor] set];
NSRectFill(filled);

// point
filled = NSMakeRect(170.0f,90.0f,10.0f,10.0f);
[[NSColor blueColor] set];
NSRectFill(filled);
}

@end

@interface FlippedTestView : TestView {}
@end

@implementation FlippedTestView

- (BOOL)isFlipped {return TRUE;}

@end
- - - - - - - - - - - - - - - - - - - -- - - - -

In InterfaceBuilder, in the main window, add two NSView "custom views" next to each other and make one an instance of TestView and the other an instance of FlippedTestView. When the app is run, the blue dot will be inside the green rect, in both views.


In fact, all three implementations that "deal" with this issue return identical results for a particular point, regardless of which function/method is used and whether the view is flipped or the value of the equivalent argument is TRUE/FALSE.

For example:

Given:
NSPoint point01 = {170.0f,90.0f};
NSPoint point02 = {75.0f,45.0f};
NSRect rect = {{50.0f,50.0f},{250.0f,100.0f}};
TestView *aTestView; /* as implemented above in example, unflipped */
FlippedTestView *aFlippedTestView; /* as implemented above, flipped */

Tests:
NSPointInRect(point01,rect) returns TRUE
NSMouseInRect(point01,rect,TRUE) returns TRUE /*isFlipped*/
NSMouseInRect(point01,rect,FALSE) returns TRUE /*not Flipped*/
[aTestView mouse:point01 inRect:rect] returns TRUE /*not Flipped*/
[aFlippedTestView mouse:point01 inRect:rect] returns TRUE /*isFlipped*/

NSPointInRect(point02,rect) returns FALSE
NSMouseInRect(point02,rect,TRUE) returns FALSE /*isFlipped*/
NSMouseInRect(point02,rect,FALSE) returns FALSE /*not Flipped*/
[aTestView mouse:point02 inRect:rect] returns FALSE /*not Flipped*/
[aFlippedTestView mouse:point02 inRect:rect] returns FALSE /*isFlipped*/


It might make sense that different methods would be needed if the point and the rect were supposed to be in different coordinate systems (for example if the mouse point was assumed to be in screen or window coordinates), but the NSView method mouse:inRect: is specifically documented as requiring both the NSPoint and NSRect to be in the view's coordinate system, and the two functions obviously make the same assumption since otherwise the arguments wouldn't provide enough information to determine the answer.


So this whole issue of needing to know if the view uses a flipped coordinate system to determine if a given point is inside a given rect would definitely seem to be some sort of urban legend? Maybe it was some kind of legacy functionality, where at some point the definition of NSRect was different and was always defined with the origin in the bottom left regardless of the view's coordinate system?

Or maybe it's just me :) If anyone knows the answer, I'd appreciate it if you would explain it, since this is something that lots of people seem to quote, but has never made any sense at all to me.

Anyway, since I already ended up spending the time to write all this, I've gone ahead and filed a bug with this info (bug# 3627693).

Louis
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


References: 
 >Current mouse screen? (From: Ondra Cada <email@hidden>)
 >Re: Current mouse screen? (From: Shaun Wexler <email@hidden>)
 >Re: Current mouse screen? (From: "M. Uli Kusterer" <email@hidden>)
 >Re: Current mouse screen? (From: John Randolph <email@hidden>)

  • Prev by Date: Re: cocoa-dev digest, Vol 2 #4042 - 4 msgs
  • Next by Date: Re: Receiving modifer-clicks on open menus
  • Previous by thread: Re: Current mouse screen?
  • Next by thread: Using POSIX threads with Cocoa?
  • Index(es):
    • Date
    • Thread