Created
January 31, 2016 14:27
-
-
Save uucidl/f593811e4a3a43f2c4fa to your computer and use it in GitHub Desktop.
Minimal Cocoa App
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define BUILD(__os,...) | |
#define DOC(...) | |
#define TAG(...) | |
BUILD(osx,"clang++ -std=c++11 cocoa.mm -o cocoa -framework AppKit") | |
#import "AppKit/AppKit.h" | |
int main(int argc, char **argv) | |
{ | |
[NSApplication sharedApplication]; | |
[NSApp setActivationPolicy: NSApplicationActivationPolicyRegular]; | |
auto const app_name = [[NSProcessInfo processInfo] processName]; | |
DOC("create menubar") { | |
auto menubar = [[NSMenu new] autorelease]; | |
auto menubar_app_item = [[NSMenuItem new] autorelease]; | |
{ | |
auto app_menu = [[NSMenu new] autorelease]; | |
auto quit_title TAG(userstring) = [@"Quit " stringByAppendingString:app_name]; | |
auto quit_item = | |
[[[NSMenuItem alloc] | |
initWithTitle: quit_title | |
action:@selector(terminate:) | |
keyEquivalent:@"q"] | |
autorelease]; | |
[app_menu addItem: quit_item]; | |
[menubar_app_item setSubmenu: app_menu]; | |
} | |
[menubar addItem: menubar_app_item]; | |
[NSApp setMainMenu: menubar]; | |
} | |
DOC("create main window") { | |
auto window_style_mask = NSTitledWindowMask| | |
NSClosableWindowMask| | |
NSMiniaturizableWindowMask| | |
NSTexturedBackgroundWindowMask; | |
auto window = [[[NSWindow alloc] | |
initWithContentRect: NSMakeRect(0, 0, 640, 480) | |
styleMask: window_style_mask | |
backing: NSBackingStoreBuffered | |
defer: NO] autorelease]; | |
[window cascadeTopLeftFromPoint: NSMakePoint(20, 20)]; | |
[window setTitle: app_name]; | |
// NOTE(nicolas): a key window is the one that receives input | |
// events; it also becomes our main window: | |
[window makeKeyAndOrderFront: nil]; | |
} | |
[NSApp activateIgnoringOtherApps: YES]; | |
[NSApp run]; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment