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: Jiri Volejnik <email@hidden>
- Date: Tue, 6 Dec 2005 18:30:25 +0100
Hi Manfred,
thanks for your reply!
On Dec 5, 2005, at 19:11, Manfred Schwind wrote:
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.
Unfortunately, this method does not work with floating panels. The
-orderedWindows message does not return them at all, and -windows
message does not return windows "z-ordered".
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.
It really seems this is currently the only solution.
I asked for a Cocoa way, because it seemed to me to be both unnecessary
and expensive to walk through all windows twice and ask every one to
create it's own WindowRef. I also believed the way exists, and I just
missed it somehow.
Luckily, it turns out that for my application it's sufficient to know
only if the key window is under the cursor. Therefore, I need to create
only the key window's WindowRef and test it against FindWindow result.
FindWindow works well for both windows and panels, even if there is no
WindowRef created but the one tested!
Many thanks guys,
Jiri
_______________________________________________
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