Re: How to find a window under the cursor
Re: How to find a window under the cursor
- Subject: Re: How to find a window under the cursor
- From: Manfred Schwind <email@hidden>
- Date: Mon, 5 Dec 2005 19:11:41 +0100
Still I wonder if there is a way how to find a window under the
cursor in Cocoa. Nobody knows?
I would try something like that (attention: untested - directly
written into Mail.app!):
NSPoint screenMouseLocation = ...; // mouse location in screen
coordinates
NSPoint localMouseLocation;
NSEnumerator *windowEnumerator = [[NSApp orderedWindows]
objectEnumerator];
NSWindow *window;
NSView *windowContent;
while ((window = [windowEnumerator nextObject]) != nil) {
windowContent = [window contentView];
// convert screen mouse coordinates to window coordinates:
localMouseLocation = [window convertScreenToBase:screenMouseLocation];
// now convert window coordinates to content view coordinates:
localMouseLocation = [windowContent convertPoint:localMouseLocation
fromView:nil];
// now test if the location is inside some NSView of the whole view
hierarchy of the window:
if ([windowContent hitTest] != nil) {
// yeah, this window is under the mouse!
break;
}
}
I currently don't know if [window contentView] covers the complete
window, or if does not include things like the title bar of the
window. So the above method may not find windows when pointing to
their title bar - I don't know!
But by using hitTest it should also work for non-rectangle windows.
The easier way may be just to use Carbon! But you asked for plain
Cocoa. ;-)
You can always call Carbon functions from a Cocoa app without
problems. (AFAIK Cocoa classes are themselfes very often just
wrappers around Carbon functions.)
The Carbon function seems to be (found in MacWindows.h of the Carbon
framework):
extern WindowPartCode
MacFindWindow(
Point thePoint,
WindowRef * window) AVAILABLE_MAC_OS_X_VERSION_10_0_AND_LATER;
But you also have to loop through all NSWindows (like in the code
example above) and check for [theWindow windowRef] to find the
NSWindow for the given WindowRef.
Regards,
Mani
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden