]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/cocoa/cocoawindow.m
Remove an unused global variable
[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
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         wrapper->delegate = [[WindowDelegate alloc] init];
40         [window setDelegate:wrapper->delegate];
41
42         if(!windows)
43                 windows = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
44         CFDictionaryAddValue(windows, window, wrapper);
45
46         return wrapper;
47 }
48
49 void destroy_window(CocoaWindow *window)
50 {
51         CFDictionaryRemoveValue(windows, window->window);
52         if(!CFDictionaryGetCount(windows))
53         {
54                 CFRelease(windows);
55                 windows = NULL;
56         }
57
58         [window->window release];
59         [window->delegate release];
60         free(window);
61 }
62
63 NSWindow *get_native_window(CocoaWindow *window)
64 {
65         return window->window;
66 }
67
68 CocoaWindow *lookup_window(NSWindow *window)
69 {
70         return (CocoaWindow *)CFDictionaryGetValue(windows, window);
71 }
72
73 void set_window_title(CocoaWindow *window, const char *title)
74 {
75         [window->window setTitle:[NSString stringWithCString:title encoding:NSUTF8StringEncoding]];
76 }
77
78 void set_window_size(CocoaWindow *window, unsigned width, unsigned height)
79 {
80         NSRect rect = NSMakeRect(0, 0, width, height);
81         rect = [NSWindow frameRectForContentRect:rect styleMask:window->style];
82         [window->window setFrame:rect display:YES];
83 }
84
85 void show_window(CocoaWindow *window)
86 {
87         [window->window makeKeyAndOrderFront:nil];
88         [NSApp activateIgnoringOtherApps:NO];
89 }
90
91 void hide_window(CocoaWindow *window)
92 {
93         (void)window;
94 }
95
96 @implementation WindowDelegate
97
98 - (BOOL)windowShouldClose:(id)sender
99 {
100         CocoaWindow *window = lookup_window((NSWindow *)sender);
101         if(window)
102         {
103                 CocoaEvent event;
104                 event.any.type = WINDOW_CLOSED;
105                 event.any.window = window;
106                 queue_event(window->display, &event);
107                 return NO;
108         }
109         return YES;
110 }
111
112 @end