• 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: 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
  • Follow-Ups:
    • Re: How to find a window under the cursor
      • From: Jiri Volejnik <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>)

  • Prev by Date: Re: Do bindings "pay attention" to visibility of control?
  • Next by Date: Re: Unified instead of Brushed Metal NSDrawer
  • Previous by thread: Re: How to find a window under the cursor
  • Next by thread: Re: How to find a window under the cursor
  • Index(es):
    • Date
    • Thread