1 #import <AppKit/NSApplication.h>
2 #import <AppKit/NSWindow.h>
3 #import <CoreFoundation/CFArray.h>
4 #include "cocoadisplay.h"
5 #include "cocoawindow.h"
7 @interface MspWindow: NSWindow {
11 - (BOOL)windowShouldClose:(id)sender;
12 - (void)keyDown:(NSEvent *)event;
18 CocoaDisplay *display;
19 __strong NSWindow *window;
23 CFMutableDictionaryRef windows = NULL;
25 CocoaWindow *create_window(CocoaDisplay *display, unsigned width, unsigned height, bool fullscreen, bool resizable)
27 NSRect rect = NSMakeRect(0, 0, width, height);
28 NSUInteger style = NSTitledWindowMask|NSClosableWindowMask;
30 style |= NSResizableWindowMask;
31 NSWindow *window = [MspWindow alloc];
32 window = [window initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:NO];
33 [window setAcceptsMouseMovedEvents:YES];
37 CocoaWindow *wrapper = (CocoaWindow *)malloc(sizeof(CocoaWindow));
38 wrapper->display = display;
39 wrapper->window = window;
40 wrapper->style = style;
43 windows = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
44 CFDictionaryAddValue(windows, window, wrapper);
49 void destroy_window(CocoaWindow *window)
51 CFDictionaryRemoveValue(windows, window->window);
52 if(!CFDictionaryGetCount(windows))
58 [window->window release];
62 NSWindow *get_native_window(CocoaWindow *window)
64 return window->window;
67 CocoaWindow *lookup_window(NSWindow *window)
69 return (CocoaWindow *)CFDictionaryGetValue(windows, window);
72 void set_window_title(CocoaWindow *window, const char *title)
74 [window->window setTitle:[NSString stringWithCString:title encoding:NSUTF8StringEncoding]];
77 void set_window_size(CocoaWindow *window, unsigned width, unsigned height)
79 NSRect rect = NSMakeRect(0, 0, width, height);
80 rect = [NSWindow frameRectForContentRect:rect styleMask:window->style];
81 [window->window setFrame:rect display:YES];
84 void show_window(CocoaWindow *window)
86 [window->window makeKeyAndOrderFront:nil];
87 [NSApp activateIgnoringOtherApps:NO];
90 void hide_window(CocoaWindow *window)
95 @implementation MspWindow
97 - (BOOL)windowShouldClose:(id)sender
99 CocoaWindow *window = lookup_window((NSWindow *)sender);
103 event.any.type = WINDOW_CLOSED;
104 event.any.window = window;
105 queue_event(window->display, &event);
111 - (void)keyDown:(NSEvent *)event
113 // Eat the event to avoid beeping