

# HG changeset patch
# User Gellule Xg <gellule.xg@gmail.com>
# Date 2011-09-25 18:37:20 -1000
# Node ID edce50d78ba55363f75965fad6e62008f8c45f9a
# Parent  2e0577dbcfbe80dcf42c611cb9270f32d054885f
Better fullscreen mode on OSX, fixing issue: http://dev.ryzom.com/issues/1005.

diff --git a/code/nel/src/3d/driver/opengl/driver_opengl.h b/code/nel/src/3d/driver/opengl/driver_opengl.h
--- a/code/nel/src/3d/driver/opengl/driver_opengl.h
+++ b/code/nel/src/3d/driver/opengl/driver_opengl.h
@@ -793,8 +793,10 @@
 	NSAutoreleasePool*         _autoreleasePool;
 	uint16                     _backBufferHeight;
 	uint16                     _backBufferWidth;
+    NSWindow*                  _cocoaWindow;
 
 	NSView* containerView() { return (NSView*)_win; }
+    NSWindow* cocoaWindow() { return _cocoaWindow; }
 	void setupApplicationMenu();
 
 #elif defined (NL_OS_UNIX)
diff --git a/code/nel/src/3d/driver/opengl/driver_opengl_window.cpp b/code/nel/src/3d/driver/opengl/driver_opengl_window.cpp
--- a/code/nel/src/3d/driver/opengl/driver_opengl_window.cpp
+++ b/code/nel/src/3d/driver/opengl/driver_opengl_window.cpp
@@ -1507,6 +1507,8 @@
 		nldebug("cannot create cocoa view for cocoa window");
 		return false;
 	}
+    
+    _cocoaWindow = cocoa_window;
 
 #elif defined (NL_OS_UNIX)
 
@@ -1688,54 +1690,37 @@
 #elif defined(NL_OS_MAC)
 
 	// leave fullscreen mode, enter windowed mode
-	if(windowStyle == EWSWindowed && [containerView() isInFullScreenMode])
+	if(windowStyle == EWSWindowed && !_CurrentMode.Windowed)
 	{
-		// disable manual setting of back buffer size, cocoa handles this
-		// automatically as soon as the view gets resized
-		CGLError error = CGLDisable((CGLContextObj)[_ctx CGLContextObj],
-			kCGLCESurfaceBackingSize);
-
-		if(error != kCGLNoError)
-			nlerror("cannot disable kCGLCESurfaceBackingSize (%s)",
-				CGLErrorString(error));
-
-		// pull the view back from fullscreen restoring window options
-		[containerView() exitFullScreenModeWithOptions:nil];
-
+        // disable manual setting of back buffer size, cocoa handles this
+        // automatically as soon as the view gets resized
+        CGLError error = CGLDisable((CGLContextObj)[_ctx CGLContextObj], kCGLCESurfaceBackingSize);
+        if(error != kCGLNoError)
+            nlerror("cannot disable kCGLCESurfaceBackingSize (%s)", CGLErrorString(error));
+        
+        // Makes the cocoaWindow a regular window
+        [cocoaWindow() setStyleMask:(NSTitledWindowMask | NSClosableWindowMask |
+                                     NSMiniaturizableWindowMask | NSResizableWindowMask)];
+        [cocoaWindow() setFrame:[[NSScreen mainScreen] visibleFrame] display:true];
+        [cocoaWindow() setLevel:NSNormalWindowLevel];
+                
 		// let the gl view receive key events
 		[[containerView() window] makeFirstResponder:_glView];
-
-		// bring the window containing the gl view to the front
-		[[containerView() window] makeKeyAndOrderFront:nil];
 	}
 
 	// enter fullscreen, leave windowed mode
-	else if(windowStyle == EWSFullscreen && ![containerView() isInFullScreenMode])
+	else if(windowStyle == EWSFullscreen && _CurrentMode.Windowed)
 	{
-		// enable manual back buffer size for mode setting in fullscreen
-		CGLError error = CGLEnable((CGLContextObj)[_ctx CGLContextObj],
-			kCGLCESurfaceBackingSize);
-
-		if(error != kCGLNoError)
-			nlerror("cannot enable kCGLCESurfaceBackingSize (%s)",
-				CGLErrorString(error));
-
-		// put the view in fullscreen mode, hiding the dock but enabling the menubar
-		// to pop up if the mouse hits the top screen border.
-		// NOTE: withOptions:nil disables <CMD>+<Tab> application switching!
-#if defined(MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
-		[containerView() enterFullScreenMode:[NSScreen mainScreen] withOptions:
-			[NSDictionary dictionaryWithObjectsAndKeys:
-				[NSNumber numberWithInt:
-					NSApplicationPresentationHideDock |
-					NSApplicationPresentationAutoHideMenuBar],
-				NSFullScreenModeApplicationPresentationOptions, nil]];
-#endif
-		/*
-			TODO check if simply using NSView enterFullScreenMode is a good idea.
-			 the context can be set to full screen as well, performance differences?
-		*/
-
+        // enable manual back buffer size for mode setting in fullscreen
+        CGLError error = CGLEnable((CGLContextObj)[_ctx CGLContextObj], kCGLCESurfaceBackingSize);
+        if(error != kCGLNoError)
+            nlerror("cannot enable kCGLCESurfaceBackingSize (%s)", CGLErrorString(error));
+
+        // Makes the cocoaWindow borderless, fullscreen, and one level above the menu bar
+        [cocoaWindow() setStyleMask:NSBorderlessWindowMask];
+        [cocoaWindow() setFrame:[[NSScreen mainScreen] frame] display:true];
+        [cocoaWindow() setLevel:NSMainMenuWindowLevel+1];
+        
 		// let the gl view receive key events
 		[[containerView() window] makeFirstResponder:_glView];
 	}
@@ -2072,7 +2057,7 @@
 	mode.Depth     = NSBitsPerPixelFromDepth([screen depth]);
 
 	// in fullscreen mode
-	if([containerView() isInFullScreenMode])
+	if(!_CurrentMode.Windowed)
 	{
 		// return the size of the back buffer (like having switched monitor mode)
 		mode.Windowed  = false;
@@ -2396,7 +2381,7 @@
 	else
 	{
 #ifdef NL_OS_MAC
-		if([containerView() isInFullScreenMode])
+		if(!_CurrentMode.Windowed)
 		{
 			width = _backBufferWidth;
 			height = _backBufferHeight;
@@ -2439,7 +2424,7 @@
 #elif defined(NL_OS_MAC)
 
 	// for fullscreen mode, adjust the back buffer size to desired resolution
-	if([containerView() isInFullScreenMode])
+	if(!_CurrentMode.Windowed)
 	{
 		// disable and re-enable fullscreen
 		// fixes #1062 (http://dev.ryzom.com/issues/1062)
