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