From e4ba0d2878c516aade93cef726fe6888b84ed9de Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Sun, 4 Feb 2018 11:42:54 -0500 Subject: Add Quit menu item to gfcocoa --- bin/gfcocoa.m | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/bin/gfcocoa.m b/bin/gfcocoa.m index 6f8b0e5e..280a26ef 100644 --- a/bin/gfcocoa.m +++ b/bin/gfcocoa.m @@ -61,19 +61,21 @@ static uint32_t buf[WIDTH * HEIGHT]; - (void) keyDown: (NSEvent *) event { char in; - [ + BOOL converted = [ [event characters] getBytes: &in maxLength: 1 usedLength: NULL encoding: NSASCIIStringEncoding - options: NSStringEncodingConversionAllowLossy + options: 0 range: NSMakeRange(0, 1) remainingRange: NULL ]; - input(in); - draw(buf, WIDTH, HEIGHT); - [self setNeedsDisplay: YES]; + if (converted) { + input(in); + draw(buf, WIDTH, HEIGHT); + [self setNeedsDisplay: YES]; + } } @end @@ -90,25 +92,43 @@ int main(int argc, char *argv[]) { int error = init(argc, argv); if (error) return error; - draw(buf, WIDTH, HEIGHT); - [NSApplication sharedApplication]; [NSApp setActivationPolicy: NSApplicationActivationPolicyRegular]; [NSApp setDelegate: [Delegate new]]; + NSString *name = [[NSProcessInfo processInfo] processName]; + NSMenu *menu = [NSMenu new]; + NSMenuItem *quit = [ + [NSMenuItem alloc] + initWithTitle: [@"Quit " stringByAppendingString: name] + action: @selector(terminate:) + keyEquivalent: @"q" + ]; + [menu addItem: quit]; + NSMenuItem *menuItem = [NSMenuItem new]; + [menuItem setSubmenu: menu]; + [NSApp setMainMenu: [NSMenu new]]; + [[NSApp mainMenu] addItem: menuItem]; + + NSUInteger style = NSTitledWindowMask + | NSClosableWindowMask + | NSMiniaturizableWindowMask + | NSResizableWindowMask; NSWindow *window = [ [NSWindow alloc] initWithContentRect: NSMakeRect(0, 0, WIDTH, HEIGHT) - styleMask: NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask + styleMask: style backing: NSBackingStoreBuffered defer: YES ]; + [window setTitle: name]; + [window center]; BufferView *view = [[BufferView alloc] initWithFrame: [window frame]]; [window setContentView: view]; - [window setTitle: [NSString stringWithUTF8String: argv[0]]]; - [window center]; + draw(buf, WIDTH, HEIGHT); + [window makeKeyAndOrderFront: nil]; [NSApp activateIgnoringOtherApps: YES]; [NSApp run]; -- cgit 1.4.1