From 3e8961d05f2ffeb2184c2b0688b2e2c57678b12a Mon Sep 17 00:00:00 2001
From: Marco Antognini <antognini.marco@gmail.com>
Date: Thu, 19 Apr 2012 19:50:13 +0200
Subject: [PATCH] Fixed view resizing with Cocoa (close #207)

---
 src/SFML/Window/OSX/SFViewController.mm       | 97 ++++++++++---------
 src/SFML/Window/OSX/SFWindowController.mm     |  3 +
 .../Window/OSX/WindowImplDelegateProtocol.h   |  7 +-
 3 files changed, 62 insertions(+), 45 deletions(-)

diff --git a/src/SFML/Window/OSX/SFViewController.mm b/src/SFML/Window/OSX/SFViewController.mm
index 527748b5..81e94152 100644
--- a/src/SFML/Window/OSX/SFViewController.mm
+++ b/src/SFML/Window/OSX/SFViewController.mm
@@ -71,6 +71,8 @@
         
         // Set the (OGL) view to the view as its "content" view.
         [m_view addSubview:m_oglView];
+        
+        [m_oglView setAutoresizingMask:[m_view autoresizingMask]];
     }
     
     return self;
@@ -105,27 +107,6 @@
 }
 
 
-////////////////////////////////////////////////////////
--(void)changeTitle:(NSString *)title
-{
-    sf::err() << "Cannot change the title of the SFML area when SFML is integrated in a NSView." << std::endl;
-}
-
-
-////////////////////////////////////////////////////////
--(void)enableKeyRepeat
-{
-    [m_oglView enableKeyRepeat];
-}
-
-
-////////////////////////////////////////////////////////
--(void)disableKeyRepeat
-{
-    [m_oglView disableKeyRepeat];
-}
-
-
 ////////////////////////////////////////////////////////
 -(void)hideMouseCursor
 {
@@ -140,31 +121,11 @@
 }
 
 
-////////////////////////////////////////////////////////
--(void)hideWindow
-{
-    [m_view setHidden:YES];
-}
-
-
-////////////////////////////////////////////////////////
--(void)showWindow
-{
-    [m_view setHidden:NO];
-}
-
-
-////////////////////////////////////////////////////////
--(void)closeWindow
-{
-    sf::err() << "Cannot close SFML area when SFML is integrated in a NSView." << std::endl;
-    [self setRequesterTo:0];
-}
-
-
 ////////////////////////////////////////////////////////
 -(void)setCursorPositionToX:(unsigned int)x Y:(unsigned int)y
 {
+    // TODO remove this ? use ogl view selector instead ?
+    
     if (m_requester == 0) return;
     
     // Create a SFML event.
@@ -203,6 +164,7 @@
     return [m_view convertPoint:NSMakePoint(0, 0) toView:nil]; // nil means window
 }
 
+
 ////////////////////////////////////////////////////////.
 -(void)setWindowPositionToX:(unsigned int)x Y:(unsigned int)y
 {
@@ -213,18 +175,65 @@
 ////////////////////////////////////////////////////////////
 -(NSSize)size
 {
-    return [m_view frame].size;
+    // TODO scaleUnitSquareToSize: ?
+    return [m_oglView frame].size;
 }
 
+
 ////////////////////////////////////////////////////////
 -(void)resizeTo:(unsigned int)width by:(unsigned int)height
 {
+    // TODO scaleUnitSquareToSize: ?
+    
     NSRect frame = NSMakeRect([m_view frame].origin.x,
                               [m_view frame].origin.y,
                               width,
                               height);
     
     [m_view setFrame:frame];
+    [m_oglView setFrame:frame];
+}
+
+
+////////////////////////////////////////////////////////
+-(void)changeTitle:(NSString *)title
+{
+    sf::err() << "Cannot change the title of the SFML area when SFML is integrated in a NSView." << std::endl;
+}
+
+
+////////////////////////////////////////////////////////
+-(void)hideWindow
+{
+    [m_view setHidden:YES];
+}
+
+
+////////////////////////////////////////////////////////
+-(void)showWindow
+{
+    [m_view setHidden:NO];
+}
+
+
+////////////////////////////////////////////////////////
+-(void)closeWindow
+{
+    sf::err() << "Cannot close SFML area when SFML is integrated in a NSView." << std::endl;
+}
+
+
+////////////////////////////////////////////////////////
+-(void)enableKeyRepeat
+{
+    [m_oglView enableKeyRepeat];
+}
+
+
+////////////////////////////////////////////////////////
+-(void)disableKeyRepeat
+{
+    [m_oglView disableKeyRepeat];
 }
 
 
diff --git a/src/SFML/Window/OSX/SFWindowController.mm b/src/SFML/Window/OSX/SFWindowController.mm
index dc0c2fe4..2bffb9a4 100644
--- a/src/SFML/Window/OSX/SFWindowController.mm
+++ b/src/SFML/Window/OSX/SFWindowController.mm
@@ -317,6 +317,7 @@
 ////////////////////////////////////////////////////////
 -(NSSize)size
 {
+    // TODO scaleUnitSquareToSize: ?
     return [m_oglView frame].size;
 }
 
@@ -324,6 +325,8 @@
 ////////////////////////////////////////////////////////
 -(void)resizeTo:(unsigned int)width by:(unsigned int)height
 {
+    // TODO scaleUnitSquareToSize: ?
+    
     // Add titlebar height.
     NSRect frame = NSMakeRect([m_window frame].origin.x,
                               [m_window frame].origin.y,
diff --git a/src/SFML/Window/OSX/WindowImplDelegateProtocol.h b/src/SFML/Window/OSX/WindowImplDelegateProtocol.h
index 184ea7ca..d0bf3415 100644
--- a/src/SFML/Window/OSX/WindowImplDelegateProtocol.h
+++ b/src/SFML/Window/OSX/WindowImplDelegateProtocol.h
@@ -41,6 +41,11 @@ namespace sf {
 /// This protocol defines the interface of the delegate of 
 /// the window implementation.
 ///
+/// We don't create an interface here because Obj-C doesn't allow
+/// multiple inheritance (SFViewController and SFWindowController
+/// don't have the same parent classes). Unfortunately this means
+/// we have to duplicate some code.
+///
 /// Everything is done via a class that implement this protocol.
 /// There are two of these classes : 
 ///
@@ -56,7 +61,7 @@ namespace sf {
 ///
 /// keyDown, keyUp, textEntered
 ///
-/// Note : Joystick are not bound to a view or window 
+/// Note : Joysticks are not bound to a view or window 
 /// thus they're not managed by a class implementing this protocol.
 ///
 ////////////////////////////////////////////////////////////