1 #import <AppKit/NSApplication.h>
2 #import <AppKit/NSWindow.h>
3 #import <CoreFoundation/CFArray.h>
4 #include "cocoadisplay.h"
5 #include "cocoawindow.h"
7 #if __MAC_OS_X_VERSION_MAX_ALLOWED < 1060
8 @protocol NSWindowDelegate <NSObject> @end
11 @interface WindowDelegate: NSObject <NSWindowDelegate> {
15 - (BOOL)windowShouldClose:(id)sender;
21 CocoaDisplay *display;
22 __strong NSWindow *window;
23 __strong WindowDelegate *delegate;
27 CFMutableDictionaryRef windows = NULL;
29 CocoaWindow *create_window(CocoaDisplay *display, unsigned width, unsigned height, bool fullscreen, bool resizable)
31 NSRect rect = NSMakeRect(0, 0, width, height);
32 NSUInteger style = NSTitledWindowMask|NSClosableWindowMask;
34 style |= NSResizableWindowMask;
35 NSWindow *window = [NSWindow alloc];
36 window = [window initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:NO];
37 [window setAcceptsMouseMovedEvents:YES];
41 CocoaWindow *wrapper = (CocoaWindow *)malloc(sizeof(CocoaWindow));
42 wrapper->display = display;
43 wrapper->window = window;
44 wrapper->style = style;
46 wrapper->delegate = [[WindowDelegate alloc] init];
47 [window setDelegate:wrapper->delegate];
50 windows = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
51 CFDictionaryAddValue(windows, window, wrapper);
56 void destroy_window(CocoaWindow *window)
58 CFDictionaryRemoveValue(windows, window->window);
59 if(!CFDictionaryGetCount(windows))
65 [window->window release];
66 [window->delegate release];
70 NSWindow *get_native_window(CocoaWindow *window)
72 return window->window;
75 CocoaWindow *lookup_window(NSWindow *window)
77 return (CocoaWindow *)CFDictionaryGetValue(windows, window);
80 void set_window_title(CocoaWindow *window, const char *title)
82 [window->window setTitle:[NSString stringWithCString:title encoding:NSUTF8StringEncoding]];
85 void set_window_size(CocoaWindow *window, unsigned width, unsigned height)
87 NSRect rect = NSMakeRect(0, 0, width, height);
88 rect = [NSWindow frameRectForContentRect:rect styleMask:window->style];
89 [window->window setFrame:rect display:YES];
92 void show_window(CocoaWindow *window)
94 [window->window makeKeyAndOrderFront:nil];
95 [NSApp activateIgnoringOtherApps:NO];
98 void hide_window(CocoaWindow *window)
103 @implementation WindowDelegate
105 - (BOOL)windowShouldClose:(id)sender
107 CocoaWindow *window = lookup_window((NSWindow *)sender);
111 event.any.type = WINDOW_CLOSED;
112 event.any.window = window;
113 queue_event(window->display, &event);