
Untitled
By: a guest on
May 4th, 2012 | syntax:
None | size: 1.33 KB | hits: 32 | expires: Never
Is it possible to focus an NSTextField in a NSMenuItem?
#import <Cocoa/Cocoa.h>
@class App;
@interface App : NSObject <NSApplicationDelegate>
-(void) menuWillOpen: (NSMenu*) menu;
@end
// ---
int main() {
[NSApplication sharedApplication];
NSString* name = [[NSProcessInfo processInfo] processName];
// menubar item
NSStatusItem* menuBarItem = [[NSStatusBar systemStatusBar]
statusItemWithLength: NSVariableStatusItemLength];
[menuBarItem setTitle: name];
// menu
NSMenu* menu = [NSMenu new];
menuBarItem.menu = menu;
// delegate for menu
id app = [App new];
menu.delegate = app;
// first item - the one with the custom view
NSMenuItem* item = [NSMenuItem new];
[menu addItem: item];
// custom view for first item
NSView* view = [[NSView new] initWithFrame: NSMakeRect(0,0,200,20)];
item.view = view;
// single text field in view
NSTextField* textField = [[NSTextField alloc] initWithFrame: NSMakeRect(5,0,190,20)];
[view addSubview: textField];
// another item
NSMenuItem* quit = [NSMenuItem new];
quit.title = @"Quit";
quit.action = @selector(terminate:);
[menu addItem: quit];
[NSApp run];
}
@implementation App
- (void) menuWillOpen: (NSMenu*) menu {
NSLog(@"menu opened");
}
@end
clang -g -framework Cocoa menu-with-textfield.m -o menu-with-textfield
./menu-with-textfield