Added Quit menu item + shortcut (Cmd+Q).

This commit is contained in:
Olle Håkansson 2014-04-27 10:52:28 +02:00
parent a24ce9ba11
commit 160e6ecb31
2 changed files with 18 additions and 3 deletions

View file

@ -61,6 +61,16 @@ namespace OpenTK.Platform.MacOS
Cocoa.SendIntPtr(menubar, Selector.Get("addItem:"), menuItem); Cocoa.SendIntPtr(menubar, Selector.Get("addItem:"), menuItem);
Cocoa.SendIntPtr(Handle, Selector.Get("setMainMenu:"), menubar); Cocoa.SendIntPtr(Handle, Selector.Get("setMainMenu:"), menubar);
// Add a "Quit" menu item and bind the button.
var appMenu = Cocoa.SendIntPtr(Cocoa.SendIntPtr(Class.Get("NSMenu"), Selector.Alloc),
Selector.Autorelease);
var quitMenuItem = Cocoa.SendIntPtr(Cocoa.SendIntPtr(Cocoa.SendIntPtr(Class.Get("NSMenuItem"), Selector.Alloc),
Selector.Get("initWithTitle:action:keyEquivalent:"), Cocoa.ToNSString("Quit"), Selector.Get("terminate:"), Cocoa.ToNSString("q")),
Selector.Autorelease);
Cocoa.SendIntPtr(appMenu, Selector.Get("addItem:"), quitMenuItem);
Cocoa.SendIntPtr(menuItem, Selector.Get("setSubmenu:"), appMenu);
// Tell cocoa we're ready to run the application (usually called by [NSApp run]). // Tell cocoa we're ready to run the application (usually called by [NSApp run]).
Cocoa.SendVoid(Handle, Selector.Get("finishLaunching")); Cocoa.SendVoid(Handle, Selector.Get("finishLaunching"));
} }

View file

@ -151,6 +151,7 @@ namespace OpenTK.Platform.MacOS
// Create the window class // Create the window class
Interlocked.Increment(ref UniqueId); Interlocked.Increment(ref UniqueId);
windowClass = Class.AllocateClass("OpenTK_GameWindow" + UniqueId, "NSWindow"); windowClass = Class.AllocateClass("OpenTK_GameWindow" + UniqueId, "NSWindow");
Class.RegisterMethod(windowClass, new WindowKeyDownDelegate(WindowKeyDown), "keyDown:", "v@:@");
Class.RegisterMethod(windowClass, new WindowDidResizeDelegate(WindowDidResize), "windowDidResize:", "v@:@"); Class.RegisterMethod(windowClass, new WindowDidResizeDelegate(WindowDidResize), "windowDidResize:", "v@:@");
Class.RegisterMethod(windowClass, new WindowDidMoveDelegate(WindowDidMove), "windowDidMove:", "v@:@"); Class.RegisterMethod(windowClass, new WindowDidMoveDelegate(WindowDidMove), "windowDidMove:", "v@:@");
Class.RegisterMethod(windowClass, new WindowDidBecomeKeyDelegate(WindowDidBecomeKey), "windowDidBecomeKey:", "v@:@"); Class.RegisterMethod(windowClass, new WindowDidBecomeKeyDelegate(WindowDidBecomeKey), "windowDidBecomeKey:", "v@:@");
@ -185,6 +186,7 @@ namespace OpenTK.Platform.MacOS
ResetTrackingArea(); ResetTrackingArea();
} }
delegate void WindowKeyDownDelegate(IntPtr self, IntPtr cmd, IntPtr notification);
delegate void WindowDidResizeDelegate(IntPtr self, IntPtr cmd, IntPtr notification); delegate void WindowDidResizeDelegate(IntPtr self, IntPtr cmd, IntPtr notification);
delegate void WindowDidMoveDelegate(IntPtr self, IntPtr cmd, IntPtr notification); delegate void WindowDidMoveDelegate(IntPtr self, IntPtr cmd, IntPtr notification);
delegate void WindowDidBecomeKeyDelegate(IntPtr self, IntPtr cmd, IntPtr notification); delegate void WindowDidBecomeKeyDelegate(IntPtr self, IntPtr cmd, IntPtr notification);
@ -198,6 +200,11 @@ namespace OpenTK.Platform.MacOS
delegate bool CanBecomeKeyWindowDelegate(IntPtr self, IntPtr cmd); delegate bool CanBecomeKeyWindowDelegate(IntPtr self, IntPtr cmd);
delegate bool CanBecomeMainWindowDelegate(IntPtr self, IntPtr cmd); delegate bool CanBecomeMainWindowDelegate(IntPtr self, IntPtr cmd);
private void WindowKeyDown(IntPtr self, IntPtr cmd, IntPtr notification)
{
// Steal the event to remove the "beep" sound that is normally played for unhandled key events.
}
private void WindowDidResize(IntPtr self, IntPtr cmd, IntPtr notification) private void WindowDidResize(IntPtr self, IntPtr cmd, IntPtr notification)
{ {
OnResize(true); OnResize(true);
@ -389,10 +396,8 @@ namespace OpenTK.Platform.MacOS
KeyPress(this, keyPressArgs); KeyPress(this, keyPressArgs);
} }
} }
// Steal all keydown events to avoid the annoying "bleep" sound.
return;
} }
break;
case NSEventType.KeyUp: case NSEventType.KeyUp:
{ {