Skip to content

Instantly share code, notes, and snippets.

@uucidl uucidl/

Created Jan 31, 2016
What would you like to do?
Minimal Cocoa App
#define BUILD(__os,...)
#define DOC(...)
#define TAG(...)
BUILD(osx,"clang++ -std=c++11 -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
[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|
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
You can’t perform that action at this time.