]> git.tdb.fi Git - libs/gui.git/blobdiff - source/graphics/cocoa/cocoawindow.m
Basic OS X support
[libs/gui.git] / source / graphics / cocoa / cocoawindow.m
diff --git a/source/graphics/cocoa/cocoawindow.m b/source/graphics/cocoa/cocoawindow.m
new file mode 100644 (file)
index 0000000..6929520
--- /dev/null
@@ -0,0 +1,108 @@
+#import <AppKit/NSWindow.h>
+#import <CoreFoundation/CFArray.h>
+#include "cocoadisplay.h"
+#include "cocoawindow.h"
+
+@interface WindowDelegate: NSObject <NSWindowDelegate>
+
+- (void)windowWillClose:(NSNotification *)notification;
+
+@end
+
+struct _CocoaWindow
+{
+       CocoaDisplay *display;
+       __strong NSWindow *window;
+       NSUInteger style;
+};
+
+CFMutableDictionaryRef windows = NULL;
+CFMutableArrayRef event_queue = NULL;
+
+CocoaWindow *create_window(CocoaDisplay *display, unsigned width, unsigned height, bool fullscreen, bool resizable)
+{
+       NSRect rect = NSMakeRect(0, 0, width, height);
+       NSUInteger style = NSTitledWindowMask|NSClosableWindowMask;
+       if(resizable)
+               style |= NSResizableWindowMask;
+       NSWindow *window = [NSWindow alloc];
+       window = [window initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:NO];
+       [window setAcceptsMouseMovedEvents:YES];
+
+       (void)fullscreen;
+
+       CocoaWindow *wrapper = (CocoaWindow *)malloc(sizeof(CocoaWindow));
+       wrapper->display = display;
+       wrapper->window = window;
+       wrapper->style = style;
+
+       [window setDelegate:[[WindowDelegate alloc] init]];
+
+       if(!windows)
+               windows = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
+       CFDictionaryAddValue(windows, window, wrapper);
+
+       return wrapper;
+}
+
+void destroy_window(CocoaWindow *window)
+{
+       CFDictionaryRemoveValue(windows, window->window);
+       if(!CFDictionaryGetCount(windows))
+       {
+               CFRelease(windows);
+               windows = NULL;
+       }
+
+       [window->window release];
+       free(window);
+}
+
+NSWindow *get_native_window(CocoaWindow *window)
+{
+       return window->window;
+}
+
+CocoaWindow *lookup_window(NSWindow *window)
+{
+       return (CocoaWindow *)CFDictionaryGetValue(windows, window);
+}
+
+void set_window_title(CocoaWindow *window, const char *title)
+{
+       [window->window setTitle:[NSString stringWithCString:title encoding:NSUTF8StringEncoding]];
+}
+
+void set_window_size(CocoaWindow *window, unsigned width, unsigned height)
+{
+       NSRect rect = NSMakeRect(0, 0, width, height);
+       rect = [NSWindow frameRectForContentRect:rect styleMask:window->style];
+       [window->window setFrame:rect display:YES];
+}
+
+void show_window(CocoaWindow *window)
+{
+       [window->window makeKeyAndOrderFront:nil];
+       [NSApp activateIgnoringOtherApps:NO];
+}
+
+void hide_window(CocoaWindow *window)
+{
+       (void)window;
+}
+
+@implementation WindowDelegate
+
+- (void)windowWillClose:(NSNotification *)notification
+{
+       CocoaWindow *window = lookup_window((NSWindow *)[notification object]);
+       if(window)
+       {
+               CocoaEvent event;
+               event.any.type = WINDOW_CLOSED;
+               event.any.window = window;
+               queue_event(window->display, &event);
+       }
+}
+
+@end