]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/cocoa/cocoawindow.m
692952002f7c6be6f5d60be10266ba2d5e03b58a
[libs/gui.git] / source / graphics / cocoa / cocoawindow.m
1 #import <AppKit/NSWindow.h>
2 #import <CoreFoundation/CFArray.h>
3 #include "cocoadisplay.h"
4 #include "cocoawindow.h"
5
6 @interface WindowDelegate: NSObject <NSWindowDelegate>
7
8 - (void)windowWillClose:(NSNotification *)notification;
9
10 @end
11
12 struct _CocoaWindow
13 {
14         CocoaDisplay *display;
15         __strong NSWindow *window;
16         NSUInteger style;
17 };
18
19 CFMutableDictionaryRef windows = NULL;
20 CFMutableArrayRef event_queue = NULL;
21
22 CocoaWindow *create_window(CocoaDisplay *display, unsigned width, unsigned height, bool fullscreen, bool resizable)
23 {
24         NSRect rect = NSMakeRect(0, 0, width, height);
25         NSUInteger style = NSTitledWindowMask|NSClosableWindowMask;
26         if(resizable)
27                 style |= NSResizableWindowMask;
28         NSWindow *window = [NSWindow alloc];
29         window = [window initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:NO];
30         [window setAcceptsMouseMovedEvents:YES];
31
32         (void)fullscreen;
33
34         CocoaWindow *wrapper = (CocoaWindow *)malloc(sizeof(CocoaWindow));
35         wrapper->display = display;
36         wrapper->window = window;
37         wrapper->style = style;
38
39         [window setDelegate:[[WindowDelegate alloc] init]];
40
41         if(!windows)
42                 windows = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
43         CFDictionaryAddValue(windows, window, wrapper);
44
45         return wrapper;
46 }
47
48 void destroy_window(CocoaWindow *window)
49 {
50         CFDictionaryRemoveValue(windows, window->window);
51         if(!CFDictionaryGetCount(windows))
52         {
53                 CFRelease(windows);
54                 windows = NULL;
55         }
56
57         [window->window release];
58         free(window);
59 }
60
61 NSWindow *get_native_window(CocoaWindow *window)
62 {
63         return window->window;
64 }
65
66 CocoaWindow *lookup_window(NSWindow *window)
67 {
68         return (CocoaWindow *)CFDictionaryGetValue(windows, window);
69 }
70
71 void set_window_title(CocoaWindow *window, const char *title)
72 {
73         [window->window setTitle:[NSString stringWithCString:title encoding:NSUTF8StringEncoding]];
74 }
75
76 void set_window_size(CocoaWindow *window, unsigned width, unsigned height)
77 {
78         NSRect rect = NSMakeRect(0, 0, width, height);
79         rect = [NSWindow frameRectForContentRect:rect styleMask:window->style];
80         [window->window setFrame:rect display:YES];
81 }
82
83 void show_window(CocoaWindow *window)
84 {
85         [window->window makeKeyAndOrderFront:nil];
86         [NSApp activateIgnoringOtherApps:NO];
87 }
88
89 void hide_window(CocoaWindow *window)
90 {
91         (void)window;
92 }
93
94 @implementation WindowDelegate
95
96 - (void)windowWillClose:(NSNotification *)notification
97 {
98         CocoaWindow *window = lookup_window((NSWindow *)[notification object]);
99         if(window)
100         {
101                 CocoaEvent event;
102                 event.any.type = WINDOW_CLOSED;
103                 event.any.window = window;
104                 queue_event(window->display, &event);
105         }
106 }
107
108 @end