• 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: How to find a window under the cursor
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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
References: 
 >How to find a window under the cursor (From: Jiri Volejnik <email@hidden>)
 >Re: How to find a window under the cursor (From: Cybereer <email@hidden>)
 >Re: How to find a window under the cursor (From: Jiri Volejnik <email@hidden>)
 >Re: How to find a window under the cursor (From: Cybereer <email@hidden>)
 >Re: How to find a window under the cursor (From: Jiri Volejnik <email@hidden>)
 >Re: How to find a window under the cursor (From: Cybereer <email@hidden>)
 >Re: How to find a window under the cursor (From: Jiri Volejnik <email@hidden>)
 >Re: How to find a window under the cursor (From: Manfred Schwind <email@hidden>)

  • Prev by Date: Re: Thread safety of Cocoa Bindings
  • Next by Date: Re: resize image with cocoa
  • Previous by thread: Re: How to find a window under the cursor
  • Next by thread: Re: NSComboBox Coredata bindings
  • Index(es):
    • Date
    • Thread