Re: Contextual Menu
Re: Contextual Menu
- Subject: Re: Contextual Menu
- From: Keith Alperin <email@hidden>
- Date: Thu, 28 Apr 2005 07:35:30 -0500
Update: I realized that the menu isn't assigned to the NSTextField view but rather the field editor (which to the uninitiated is an NSTextView shared amongst all NSTextFields in an NSWindow.) Thus, i was able to get the menu to popup via the following:
NSRect windowFrame = [window frame];
NSRect frame = [textField frame];
NSPoint point = NSMakePoint(windowFrame.origin.x+frame.origin.x, windowFrame.origin.y+frame.origin.y);
NSGraphicsContext *context = [NSGraphicsContext currentContext];
NSEvent *event = [NSEvent mouseEventWithType:NSRightMouseDown
location:point
modifierFlags:NSCommandKeyMask | NSDownArrowFunctionKey
timestamp:GetCurrentEventTime()
windowNumber:1
context:context
eventNumber:1
clickCount:1
pressure:0.0];
NSTextView *editor = [window fieldEditor:YES forObject:nil];
NSMenu *menu = [editor menuForEvent:event];
[NSMenu popUpContextMenu: menu withEvent: event forView: editor];
The good news is that the menu functions as expected. The bad news is that the menu appears in the bottom left corner of the screen rather than over my NSTextField where I want it. I tried changing:
[NSMenu popUpContextMenu: menu withEvent: event forView: editor];
to
[NSMenu popUpContextMenu: menu withEvent: event forView: textField];
which yields the menu in the bottom corner. However, the menu is not fully functioning (if there is a misspelled word in the textField, then correctly spelled suggestions are in the popup menu, but they are grayed out.) I also tried changing the location: argument of the method to create the NSEvent, but that had no effect,
Does anyone out there cocoa-land have any knowledge about how to position my menu on a specific spot on the screen?
Many thanks,
Keith
On Apr 12, 2005, at 12:04 AM, Keith Alperin wrote:
Well, i've made some progress. I was able to determine that the 6th argument is not the context, but rather the timeStamp. I used the carbon "GetCurrentEventTime()" function in order to get an NSTimeInterval and now it looks like this and compiles cleanly:
NSEvent *event = [NSEvent mouseEventWithType:NSRightMouseDown
location:point
modifierFlags:NSCommandKeyMask | NSDownArrowFunctionKey
timestamp:GetCurrentEventTime()
windowNumber:1
context:context
eventNumber:1
clickCount:1
pressure:0.0];
Unfortunately, the menu that I get with
NSMenu *menu = [textField menuForEvent:event];
is nil, so I'm not there yet.
Any thoughts on whether this is the correct way to get a contextual menu to show would be greatly appreciated.
Best,
Keith
On Apr 11, 2005, at 7:33 AM, Keith Alperin wrote:
Greetings cocoa-devs! I'm a cocoa newbie and I'm working on a prototype whereby I can make the contextual menu for an NSTextField appear when I click a button. Ultimately, I would like to be able to make the contextual menu appear when i hit a certain key combination. I'm trying to use popUpContextMenu:withEvent:forView: on NSMenu, which necessitates having both an NSEvent and an NSMenu. Currently I'm stuck on creating the event. The following code yields a <x-tad-smaller>"error: incompatible type for argument 6 of indirect function call" </x-tad-smaller>at compile time:<x-tad-smaller>
</x-tad-smaller>
NSEvent *event = [NSEvent mouseEventWithType:NSRightMouseDown
location:point
modifierFlags:NSCommandKeyMask | NSDownArrowFunctionKey
timestamp:[NSEvent timeStamp]
windowNumber:0
context:[NSGraphicsContext currentContext]
eventNumber:1
clickCount:1
pressure:0.0];
Once I have the event, I'll call menuForEvent: on my NSTextField to get the menu and then I can call popUpContextMenu:withEvent:forView
My questions are:
1) Is this the correct way to get the contextual menu to show or should I try something else (such as just posting the right mouse click event)?
2) What is causing my compilation error. It looks to me like the 6th argument (context) is indeed an NSGraphicsContext?
Thank you all so much,
Keith R. Alperin
_______________________________________________
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 _______________________________________________
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
_______________________________________________
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