Screen Coordinates of Menu. Use CTM? (was: Give menu keyboard focus)
Screen Coordinates of Menu. Use CTM? (was: Give menu keyboard focus)
- Subject: Screen Coordinates of Menu. Use CTM? (was: Give menu keyboard focus)
- From: Jerry Krinock <email@hidden>
- Date: Sun, 09 Oct 2011 19:26:25 -0700
I changed the subject line because if I could just get the screen coordinates of my Status Item, my kludge for giving it keyboard focus looks like it will work.
NSMenu inherits from NSObject, and as far as I can see give no clue regarding its location on the screen. NSStatusItem and NSStatusBar are likewise mum.
However, the status item has an image. I subclassed NSImage, found that -drawInRect:fromRect:operation:fraction:respectFlipped:hints: is invoked. From reading the Cocoa Drawing Guide, it seems that the tx value of the current transformation matrix when this method is running is the x-coordinate that I'm looking for. So I subclassed NSImageā¦
@implementation MyLocatableImage
- (void)drawInRect:(NSRect)dstSpacePortionRect
fromRect:(NSRect)srcSpacePortionRect
operation:(NSCompositingOperation)op
fraction:(CGFloat)requestedAlpha
respectFlipped:(BOOL)respectContextIsFlipped
hints:(NSDictionary *)hints {
NSGraphicsContext* nsGraphicsContext = [NSGraphicsContext currentContext] ;
CGContextRef aContext = [nsGraphicsContext graphicsPort] ;
CGAffineTransform ctm = CGContextGetCTM(aContext) ;
NSLog(@"The ctm: %f %f %f %f %f %f", ctm.a , ctm.b , ctm.c , ctm.d , ctm.tx , ctm.ty) ;
[super drawInRect:dstSpacePortionRect
fromRect:srcSpacePortionRect
operation:op
fraction:requestedAlpha
respectFlipped:respectContextIsFlipped
hints:hints] ;
}
@end
Unfortunately, the matrix elements are NSLogged as:
1.000000 0.000000 -0.000000 1.000000 0.000000 22.000000
which is obviously not what I'm looking for, since the the x-coordinate of my status item is about 850.
The two 'Rect' parameters of that method give similar useless local coordinates.
Is there any way to get the global/screen coordinates of a menu, an NSStatusItem in particular?
Thanks,
Jerry
On 2011 Oct 09, at 14:41, Jerry Krinock wrote:
> OK, the CGEventCreateMouseEvent() kludge I described in my previous message seems to work, except for one detail. How do I find the location of, in particular, a Status Menu item, in CG "global" coordinates? It looks like that's the hardest part!
>
> Jerry
>
> On 2011 Oct 09, at 14:16, Jerry Krinock wrote:
>
>> Update: I'm "borrowing" the mouse using CGEventCreateMouseEvent(). It seems like it might work, but sure seems like a kludge, and many lines of code to do something very simple.
>>
>> Any better ideas would be appreciated.
>>
>> On 2011 Oct 09, at 12:31, Jerry Krinock wrote:
>>
>>> Is there any code which can *click* a menu, to give it keyboard focus, so that the user may then select an item in the menu using arrow keys?
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden