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