Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTextfield + NSMenu and first responder

I'm trying to implement my own autocomplemention system (result is pull from an sqlite database)

I've set up a NSTextField and the appropriate delegate. Each time the text in the NSTextField change, it call - (void)controlTextDidChange:(NSNotification *)aNotification method

It work fine, in this method I build a menu programtically and finally I call/show it with that code:

 NSRect frame = [address frame];
NSPoint menuOrigin = [[address superview] convertPoint:NSMakePoint(frame.origin.x, frame.origin.y+frame.size.height-25)
                                                toView:nil];

NSEvent *event =  [NSEvent mouseEventWithType:NSLeftMouseDown
                                     location:menuOrigin
                                modifierFlags:NSLeftMouseDownMask // 0x100
                                    timestamp:0
                                 windowNumber:[[address window] windowNumber]
                                      context:[[address window] graphicsContext]
                                  eventNumber:0
                                   clickCount:1
                                     pressure:1]; 
[NSMenu popUpContextMenu:menu withEvent:event forView:address];

Where address is my NSTextField and menu is my NSMenu. The problem is that the menu take the focus, so you can type only 1 letter in the text field and then you can't type text anymore because the menu is now the first responder.

My questions is how to show the menu and keep the text field as first responder so you can type in it while the menu is reloaded at each text change in the field.

It should be like in Safari or chrome address bar in fact for example.

like image 461
Dimillian Avatar asked May 20 '11 10:05

Dimillian


2 Answers

I don't believe this is possible with NSMenu. NSMenu implementation is controlled by the system at a quite low level, and it is designed to take keyboard focus. What you need is to create your own view, or window, that looks somewhat like a menu, but is not using NSMenu. Notice for example that that the menu in the chrome address bar does not look like a standard NSMenu. You need to create a view that will appear and draw, and receive callbacks or notifications to update as the user types, but will not take keyboard focus. There are methods on NSView (NSResponder actually) that control whether a view accepts first responder status.

like image 77
Michael Gorbach Avatar answered Nov 03 '22 23:11

Michael Gorbach


As mgorbach stated it is not really possible with NSMenu. I've switched to NSTableView and customized my textfield. The textfield forward the up and down arrow to the Table view and that work fine !

like image 37
Dimillian Avatar answered Nov 04 '22 01:11

Dimillian