Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 4th, 2012  |  syntax: None  |  size: 1.33 KB  |  hits: 32  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Is it possible to focus an NSTextField in a NSMenuItem?
  2. #import <Cocoa/Cocoa.h>
  3.  
  4. @class App;
  5. @interface App : NSObject <NSApplicationDelegate>
  6. -(void) menuWillOpen: (NSMenu*) menu;
  7. @end
  8.  
  9. // ---
  10.  
  11. int main() {
  12.   [NSApplication sharedApplication];
  13.   NSString* name = [[NSProcessInfo processInfo] processName];
  14.  
  15.   // menubar item
  16.   NSStatusItem* menuBarItem = [[NSStatusBar systemStatusBar]
  17.     statusItemWithLength: NSVariableStatusItemLength];
  18.   [menuBarItem setTitle: name];
  19.  
  20.   // menu
  21.   NSMenu* menu = [NSMenu new];
  22.   menuBarItem.menu = menu;
  23.  
  24.   // delegate for menu
  25.   id app = [App new];
  26.   menu.delegate = app;
  27.  
  28.   // first item - the one with the custom view
  29.   NSMenuItem* item = [NSMenuItem new];
  30.   [menu addItem: item];
  31.  
  32.   // custom view for first item
  33.   NSView* view = [[NSView new] initWithFrame: NSMakeRect(0,0,200,20)];
  34.   item.view = view;
  35.  
  36.   // single text field in view
  37.   NSTextField* textField = [[NSTextField alloc] initWithFrame: NSMakeRect(5,0,190,20)];
  38.   [view addSubview: textField];
  39.  
  40.   // another item
  41.   NSMenuItem* quit = [NSMenuItem new];
  42.   quit.title = @"Quit";
  43.   quit.action = @selector(terminate:);
  44.   [menu addItem: quit];
  45.  
  46.   [NSApp run];
  47. }
  48.  
  49. @implementation App
  50. - (void) menuWillOpen: (NSMenu*) menu {
  51.   NSLog(@"menu opened");
  52. }
  53.  
  54. @end
  55.        
  56. clang -g -framework Cocoa menu-with-textfield.m -o menu-with-textfield
  57. ./menu-with-textfield