1// license:BSD-3-Clause
2// copyright-holders:R. Belmont
3//============================================================
4//
5//  windowcontroller.mm - our window/fullscreen manager
6//
7//  Mac OSD by R. Belmont
8//
9//============================================================
10
11#import "windowcontroller.h"
12#import "mamefswindow.h"
13
14@interface MAMEWindowController ()
15{
16	MAMEFSWindow *_fullscreenWindow;
17	NSWindow* _standardWindow;
18}
19@end
20
21@implementation MAMEWindowController
22
23- (instancetype)initWithWindow:(NSWindow *)window
24{
25	self = [super initWithWindow:window];
26
27	if (self)
28	{
29		_fullscreenWindow = nil;
30		_standardWindow = window;
31	}
32
33	return self;
34}
35
36- (void) goFullscreen
37{
38	if(_fullscreenWindow)
39	{
40		return;
41	}
42
43	_fullscreenWindow = [[MAMEFSWindow alloc] init];
44
45	NSRect viewRect = [_fullscreenWindow frame];
46	[self.window.contentView setFrameSize: viewRect.size];
47	[_fullscreenWindow setContentView:self.window.contentView];
48
49	_standardWindow = [self window];
50	[_standardWindow orderOut:self];
51
52	[self setWindow:_fullscreenWindow];
53	[_fullscreenWindow makeKeyAndOrderFront:self];
54}
55
56- (void) goWindow
57{
58	if(_fullscreenWindow == nil)
59	{
60		return;
61	}
62
63	NSRect viewRect = [_standardWindow frame];
64	[self.window.contentView setFrame:viewRect];
65
66	[self setWindow:_standardWindow];
67	[[self window] setContentView:_fullscreenWindow.contentView];
68	[[self window] makeKeyAndOrderFront:self];
69
70	_fullscreenWindow = nil;
71}
72
73
74- (void) keyDown:(NSEvent *)event
75{
76//  unichar c = [[event charactersIgnoringModifiers] characterAtIndex:0];
77	[super keyDown:event];
78}
79
80- (NSWindow *) getWindow
81{
82	if(_fullscreenWindow == nil)
83	{
84		return _standardWindow;
85	}
86
87	return _fullscreenWindow;
88}
89@end
90
91void MacPollInputs()
92{
93	NSEvent* event = [NSApp nextEventMatchingMask:NSEventMaskAny
94			untilDate:[NSDate distantPast] // do not wait for event
95			inMode:NSDefaultRunLoopMode
96			dequeue:YES];
97
98	if (event)
99	{
100		[NSApp sendEvent:event];
101		[NSApp updateWindows];
102	}
103}
104
105void *GetOSWindow(void *wincontroller)
106{
107	MAMEWindowController *wc = (MAMEWindowController *)wincontroller;
108
109	return [wc getWindow];
110}
111
112void *CreateMAMEWindow(char *title, int x, int y, int w, int h, bool isFullscreen)
113{
114	NSRect bounds = NSMakeRect(x, y, w, h);
115	NSUInteger style = NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask;
116	NSWindow *window = [NSWindow alloc];
117	MAMEWindowController *controller = [MAMEWindowController alloc];
118
119	[window initWithContentRect:bounds
120		styleMask:style
121		backing:NSBackingStoreBuffered
122		defer:NO];
123	[controller initWithWindow:window];
124	NSString *nstitle = [[NSString alloc] initWithUTF8String:title];
125	[window setTitle:nstitle];
126	[nstitle release];
127	if (isFullscreen)
128	{
129		[controller goFullscreen];
130	}
131	else
132	{
133		[window makeKeyAndOrderFront:nil];
134	}
135
136	return (void *)controller;
137}
138