1 #import <AppKit/NSWindow.h>
2 #import <CoreFoundation/CFArray.h>
3 #include "cocoadisplay.h"
4 #include "cocoawindow.h"
6 @interface WindowDelegate: NSObject <NSWindowDelegate>
8 - (void)windowWillClose:(NSNotification *)notification;
14 CocoaDisplay *display;
15 __strong NSWindow *window;
19 CFMutableDictionaryRef windows = NULL;
20 CFMutableArrayRef event_queue = NULL;
22 CocoaWindow *create_window(CocoaDisplay *display, unsigned width, unsigned height, bool fullscreen, bool resizable)
24 NSRect rect = NSMakeRect(0, 0, width, height);
25 NSUInteger style = NSTitledWindowMask|NSClosableWindowMask;
27 style |= NSResizableWindowMask;
28 NSWindow *window = [NSWindow alloc];
29 window = [window initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:NO];
30 [window setAcceptsMouseMovedEvents:YES];
34 CocoaWindow *wrapper = (CocoaWindow *)malloc(sizeof(CocoaWindow));
35 wrapper->display = display;
36 wrapper->window = window;
37 wrapper->style = style;
39 [window setDelegate:[[WindowDelegate alloc] init]];
42 windows = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
43 CFDictionaryAddValue(windows, window, wrapper);
48 void destroy_window(CocoaWindow *window)
50 CFDictionaryRemoveValue(windows, window->window);
51 if(!CFDictionaryGetCount(windows))
57 [window->window release];
61 NSWindow *get_native_window(CocoaWindow *window)
63 return window->window;
66 CocoaWindow *lookup_window(NSWindow *window)
68 return (CocoaWindow *)CFDictionaryGetValue(windows, window);
71 void set_window_title(CocoaWindow *window, const char *title)
73 [window->window setTitle:[NSString stringWithCString:title encoding:NSUTF8StringEncoding]];
76 void set_window_size(CocoaWindow *window, unsigned width, unsigned height)
78 NSRect rect = NSMakeRect(0, 0, width, height);
79 rect = [NSWindow frameRectForContentRect:rect styleMask:window->style];
80 [window->window setFrame:rect display:YES];
83 void show_window(CocoaWindow *window)
85 [window->window makeKeyAndOrderFront:nil];
86 [NSApp activateIgnoringOtherApps:NO];
89 void hide_window(CocoaWindow *window)
94 @implementation WindowDelegate
96 - (void)windowWillClose:(NSNotification *)notification
98 CocoaWindow *window = lookup_window((NSWindow *)[notification object]);
102 event.any.type = WINDOW_CLOSED;
103 event.any.window = window;
104 queue_event(window->display, &event);