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;
16 __strong WindowDelegate *delegate;
20 CFMutableDictionaryRef windows = NULL;
21 CFMutableArrayRef event_queue = NULL;
23 CocoaWindow *create_window(CocoaDisplay *display, unsigned width, unsigned height, bool fullscreen, bool resizable)
25 NSRect rect = NSMakeRect(0, 0, width, height);
26 NSUInteger style = NSTitledWindowMask|NSClosableWindowMask;
28 style |= NSResizableWindowMask;
29 NSWindow *window = [NSWindow alloc];
30 window = [window initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:NO];
31 [window setAcceptsMouseMovedEvents:YES];
35 CocoaWindow *wrapper = (CocoaWindow *)malloc(sizeof(CocoaWindow));
36 wrapper->display = display;
37 wrapper->window = window;
38 wrapper->style = style;
40 wrapper->delegate = [[WindowDelegate alloc] init];
41 [window setDelegate:wrapper->delegate];
44 windows = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
45 CFDictionaryAddValue(windows, window, wrapper);
50 void destroy_window(CocoaWindow *window)
52 CFDictionaryRemoveValue(windows, window->window);
53 if(!CFDictionaryGetCount(windows))
59 [window->window release];
60 [window->delegate release];
64 NSWindow *get_native_window(CocoaWindow *window)
66 return window->window;
69 CocoaWindow *lookup_window(NSWindow *window)
71 return (CocoaWindow *)CFDictionaryGetValue(windows, window);
74 void set_window_title(CocoaWindow *window, const char *title)
76 [window->window setTitle:[NSString stringWithCString:title encoding:NSUTF8StringEncoding]];
79 void set_window_size(CocoaWindow *window, unsigned width, unsigned height)
81 NSRect rect = NSMakeRect(0, 0, width, height);
82 rect = [NSWindow frameRectForContentRect:rect styleMask:window->style];
83 [window->window setFrame:rect display:YES];
86 void show_window(CocoaWindow *window)
88 [window->window makeKeyAndOrderFront:nil];
89 [NSApp activateIgnoringOtherApps:NO];
92 void hide_window(CocoaWindow *window)
97 @implementation WindowDelegate
99 - (void)windowWillClose:(NSNotification *)notification
101 CocoaWindow *window = lookup_window((NSWindow *)[notification object]);
105 event.any.type = WINDOW_CLOSED;
106 event.any.window = window;
107 queue_event(window->display, &event);