pcsxr-1.9.92
[pcsx_rearmed.git] / macosx / plugins / DFXVideo / macsrc / PluginWindowController.m
1 /***************************************************************************
2     PluginWindowController.m
3     PeopsSoftGPU
4   
5     Created by Gil Pedersen on Tue April 12 2004.
6     Copyright (c) 2004 Gil Pedersen.
7  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version. See also the license.txt file for *
15  *   additional informations.                                              *
16  *                                                                         *
17  ***************************************************************************/
18
19 #import "PluginWindowController.h"
20 #import "PluginWindow.h"
21 #import "Carbon/Carbon.h"
22 #include "externals.h"
23 #undef BOOL
24
25 NSWindow *gameWindow;
26 PluginWindowController *gameController;
27 NSRect windowFrame;
28
29 @implementation PluginWindowController
30
31 + (id)openGameView
32 {
33         if (gameWindow == nil) {
34                 if (gameController == nil) {
35                         gameController = [[PluginWindowController alloc] initWithWindowNibName:@"NetSfPeopsSoftGPUInterface"];
36                 }
37                 gameWindow = [gameController window];
38         }
39         
40         if (windowFrame.size.width != 0) {
41                 [gameWindow setFrame:windowFrame display:NO];
42         } else {
43                 [gameWindow center];
44         }
45
46         [gameWindow makeKeyAndOrderFront:nil];
47         [gameController showWindow:nil];
48         
49         CGDirectDisplayID display = (CGDirectDisplayID)[[[[gameWindow screen] deviceDescription] objectForKey:@"NSScreenNumber"] longValue];
50         if (CGDisplayIsCaptured(display)) {
51                 [gameController setFullscreen:YES];
52         }
53         
54         return gameController;
55 }
56
57 - (PluginGLView *)getOpenGLView
58 {
59         return (PluginGLView *)glView;
60 }
61
62 - (void)dealloc
63 {
64         if (fullWindow) {
65                 [fullWindow orderOut:self];
66                 [fullWindow autorelease];
67                 fullWindow = nil;
68         }
69         
70         windowFrame = [[self window] frame];
71         
72         [super dealloc];
73 }
74
75 // forget keyDownEvents
76 - (void)keyDown:(NSEvent *)theEvent
77 {
78         // Not required any more
79 }
80
81 - (void)mouseDown:(NSEvent *)theEvent
82 {
83         if ([self fullscreen]) {
84                 [self setFullscreen:NO];
85         }
86 }
87
88 - (BOOL)fullscreen
89 {
90         return (fullWindow!=nil);
91 }
92
93 - (void)setFullscreen:(BOOL)flag
94 {
95         NSWindow *window = [self window];
96         NSScreen *screen = [window screen];
97         CGDirectDisplayID display = (CGDirectDisplayID)[[[screen deviceDescription] objectForKey:@"NSScreenNumber"] longValue];
98         
99         NSDisableScreenUpdates();
100         
101         if (flag) {
102                 if (!CGDisplayIsCaptured(display)) {
103                         CGDisplayCapture(display);
104                         
105                         CGDisplayCount count = 10;
106                         CGDirectDisplayID displays[10];
107                         CGGetActiveDisplayList(10, displays, &count);
108                         if (count == 1) {
109                                 CGDisplayHideCursor(display);
110                                 CGAssociateMouseAndMouseCursorPosition(NO);
111                         }
112                         
113                         //[window orderOut:self];
114                 }
115                 
116                 int width = CGDisplayPixelsWide(display);
117                 int height = CGDisplayPixelsHigh(display);
118                 
119                 // assume square pixel ratio on the monitor
120                 if ((width*3)/4 < height) {
121                         height = (width*3)/4;
122                 } else {
123                         width = (height*4)/3;
124                 }
125
126                 fullWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect((CGDisplayPixelsWide(display)-width)/2, 
127                                                                                                                                                                                   (CGDisplayPixelsHigh(display)-height)/2, 
128                                                                                                                                                                                   width, height)
129                                                         styleMask:NSBorderlessWindowMask
130                                                         backing:NSBackingStoreRetained
131                                                         defer:NO
132                                                         screen:screen];
133                 
134                 //[[glView openGLContext] setFullScreen];
135                 [[glView openGLContext] setView:[fullWindow contentView]];
136                 [glView reshape];
137                 //[[glView openGLContext] update];
138                 //[fullWindow setContentView:glView];
139                 
140                 [fullWindow setBackgroundColor:[NSColor blackColor]];
141                 [fullWindow setHasShadow:NO];
142                 [fullWindow setDelegate:self];
143                 
144                 [fullWindow setLevel:CGShieldingWindowLevel()];
145                 [fullWindow makeKeyAndOrderFront:self];
146
147                 [[self window] makeKeyAndOrderFront:self];
148         } else {
149                 CGDisplayRelease(display);
150                 //CGReleaseAllDisplays();
151
152                 CGAssociateMouseAndMouseCursorPosition(YES);
153                 CGDisplayShowCursor(display);
154
155                 if (fullWindow) {
156                         [fullWindow orderOut:self];
157                         [fullWindow autorelease];
158                         fullWindow = nil;
159                         
160                         [[glView openGLContext] setView:glView];
161                         [glView reshape];
162                         //[window setContentView:glView];
163                 }
164                 
165                 [[self window] makeKeyAndOrderFront:self];
166         }
167         
168         NSEnableScreenUpdates();
169 }
170
171 - (BOOL)windowShouldZoom:(NSWindow *)sender toFrame:(NSRect)newFrame
172 {
173         [self setFullscreen:YES];
174         
175         return NO;
176 }
177
178 - (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)proposedFrameSize
179 {
180         if (!(([sender resizeFlags] & NSShiftKeyMask) == NSShiftKeyMask)) {
181                 NSRect oldSize = [sender frame];
182                 NSRect viewSize = [glView frame];
183                 
184                 float xDiff = NSWidth(oldSize) - NSWidth(viewSize);
185                 float yDiff = NSHeight(oldSize) - NSHeight(viewSize);
186                 
187                 //if ((proposedFrameSize.height / proposedFrameSize.width) < (3.0/4.0))
188                 //      proposedFrameSize.height = ((proposedFrameSize.width - xDiff) * 3.0) / 4.0 + yDiff;
189                 //else
190                         proposedFrameSize.width = ((proposedFrameSize.height - yDiff) * 4.0) / 3.0 + xDiff;
191         }
192         
193         return proposedFrameSize;
194 }
195
196 - (void)windowWillMiniaturize:(NSNotification *)aNotification
197 {
198         [[NSNotificationCenter defaultCenter] postNotificationName:@"emuWindowWantPause" object:self];
199 }
200
201 - (void)windowDidDeminiaturize:(NSNotification *)aNotification
202 {
203         [[NSNotificationCenter defaultCenter] postNotificationName:@"emuWindowWantResume" object:self];
204 }
205
206 //- (void)windowDidBecomeMain:(NSNotification *)aNotification
207 /*- (void)windowDidBecomeKey:(NSNotification *)aNotification
208 {
209         if (iWindowMode==0) {
210                 [self setFullscreen:YES];
211         }
212 }*/
213
214 - (BOOL)windowShouldClose:(id)sender
215 {
216         if (fullWindow) {
217                 return NO;
218         }
219         [[NSNotificationCenter defaultCenter] postNotificationName:@"emuWindowDidClose" object:self];
220         [gameController autorelease];
221         gameController = nil;
222         gameWindow = nil;
223         
224         return YES;
225 }
226
227 @end