From 284bd09709e609c1911fc75001198b934a9cffc6 Mon Sep 17 00:00:00 2001 From: Marco Antognini Date: Mon, 28 Mar 2011 14:39:14 +0200 Subject: [PATCH] Add support for external window handling (NSView + NSWindow) Minor improvement of SFContext. --- src/SFML/Window/OSX/SFViewController.h | 49 +++++ src/SFML/Window/OSX/SFViewController.mm | 241 ++++++++++++++++++++++++ 2 files changed, 290 insertions(+) create mode 100644 src/SFML/Window/OSX/SFViewController.h create mode 100644 src/SFML/Window/OSX/SFViewController.mm diff --git a/src/SFML/Window/OSX/SFViewController.h b/src/SFML/Window/OSX/SFViewController.h new file mode 100644 index 000000000..4ce6a85c6 --- /dev/null +++ b/src/SFML/Window/OSX/SFViewController.h @@ -0,0 +1,49 @@ +//////////////////////////////////////////////////////////// +// +// SFML - Simple and Fast Multimedia Library +// Copyright (C) 2007-2011 Marco Antognini (antognini.marco@gmail.com), +// Laurent Gomila (laurent.gom@gmail.com), +// +// This software is provided 'as-is', without any express or implied warranty. +// In no event will the authors be held liable for any damages arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it freely, +// subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; +// you must not claim that you wrote the original software. +// If you use this software in a product, an acknowledgment +// in the product documentation would be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, +// and must not be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source distribution. +// +//////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////// +// Headers +//////////////////////////////////////////////////////////// +#import + +//////////////////////////////////////////////////////////// +/// Predefine some classes +//////////////////////////////////////////////////////////// +@class SFOpenGLView; + +//////////////////////////////////////////////////////////// +/// Implementation of WindowImplDelegateProtocol for view managment. +/// +//////////////////////////////////////////////////////////// + +@interface SFViewController : NSObject { + NSView* myView; + SFOpenGLView* myOGLView; + sf::priv::WindowImplCocoa* myRequester; +} + +-(id)initWithView:(NSView *)view; + +@end diff --git a/src/SFML/Window/OSX/SFViewController.mm b/src/SFML/Window/OSX/SFViewController.mm new file mode 100644 index 000000000..6047fa46e --- /dev/null +++ b/src/SFML/Window/OSX/SFViewController.mm @@ -0,0 +1,241 @@ +//////////////////////////////////////////////////////////// +// +// SFML - Simple and Fast Multimedia Library +// Copyright (C) 2007-2011 Marco Antognini (antognini.marco@gmail.com), +// Laurent Gomila (laurent.gom@gmail.com), +// +// This software is provided 'as-is', without any express or implied warranty. +// In no event will the authors be held liable for any damages arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it freely, +// subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; +// you must not claim that you wrote the original software. +// If you use this software in a product, an acknowledgment +// in the product documentation would be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, +// and must not be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source distribution. +// +//////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////// +// Headers +//////////////////////////////////////////////////////////// +#import +#import +#include +#include + +@implementation SFViewController + + +//////////////////////////////////////////////////////// +-(id)initWithView:(NSView *)view +{ + if ((self = [super init])) { + myRequester = 0; + + // Retain the view for our own use. + myView = [view retain]; + + if (myView == nil) { + + sf::Err() + << "No view was given to initWithWindow:." + << std::endl; + + return self; + } + + // Create the view. + NSRect frame = [myView frame]; + frame.origin.x = 0; + frame.origin.y = 0; + myOGLView = [[SFOpenGLView alloc] initWithFrame:frame]; + + if (myOGLView == nil) { + + sf::Err() + << "Could not create an instance of NSOpenGLView " + << "in (SFViewController -initWithView:)." + << std::endl; + + return self; + } + + // Set the (OGL) view to the view as its "content" view. + [myView addSubview:myOGLView]; + } + + return self; +} + + +//////////////////////////////////////////////////////// +-(void)dealloc +{ + [self closeWindow]; + + [myView release]; + [myOGLView release]; + + [super dealloc]; +} + + +//////////////////////////////////////////////////////// +-(void)setRequesterTo:(sf::priv::WindowImplCocoa *)requester +{ + // Forward to the view. + [myOGLView setRequesterTo:requester]; + myRequester = requester; +} + + +//////////////////////////////////////////////////////// +-(sf::WindowHandle)getSystemHandle +{ + return myView; +} + + +//////////////////////////////////////////////////////// +-(void)changeTitle:(NSString *)title +{ + sf::Err() << "Cannot change the title of the view." << std::endl; +} + + +//////////////////////////////////////////////////////// +-(void)enableKeyRepeat +{ + [myOGLView enableKeyRepeat]; +} + + +//////////////////////////////////////////////////////// +-(void)disableKeyRepeat +{ + [myOGLView disableKeyRepeat]; +} + + +//////////////////////////////////////////////////////// +-(void)hideMouseCursor +{ + [NSCursor hide]; +} + + +//////////////////////////////////////////////////////// +-(void)showMouseCursor +{ + [NSCursor unhide]; +} + + +//////////////////////////////////////////////////////// +-(void)hideWindow +{ + [myView setHidden:YES]; +} + + +//////////////////////////////////////////////////////// +-(void)showWindow +{ + [myView setHidden:NO]; +} + + +//////////////////////////////////////////////////////// +-(void)closeWindow +{ + sf::Err() << "Cannot close the view." << std::endl; + [self setRequesterTo:0]; +} + + +//////////////////////////////////////////////////////// +-(void)setCursorPositionToX:(unsigned int)x Y:(unsigned int)y +{ + if (myRequester == 0) return; + + // Create a SFML event. + myRequester->MouseMovedAt(x, y); + + // Flip for SFML window coordinate system + y = NSHeight([[myView window] frame]) - y; + + // Adjust for view reference instead of window + y -= NSHeight([[myView window] frame]) - NSHeight([myOGLView frame]); + + // Convert to screen coordinates + NSPoint screenCoord = [[myView window] convertBaseToScreen:NSMakePoint(x, y)]; + + // Flip screen coodinates + float const screenHeight = NSHeight([[[myView window] screen] frame]); + screenCoord.y = screenHeight - screenCoord.y; + + CGDirectDisplayID screenNumber = (CGDirectDisplayID)[[[[[myView window] screen] deviceDescription] valueForKey:@"NSScreenNumber"] intValue]; + + // Place the cursor. + CGDisplayMoveCursorToPoint(screenNumber, CGPointMake(screenCoord.x, screenCoord.y)); + /* + CGDisplayMoveCursorToPoint -- Discussion : + + No events are generated as a result of this move. + Points that lie outside the desktop are clipped to the desktop. + */ +} + + +////////////////////////////////////////////////////////. +-(void)setWindowPositionToX:(unsigned int)x Y:(unsigned int)y +{ + sf::Err() << "Cannot move the view." << std::endl; +} + + +//////////////////////////////////////////////////////// +-(void)resizeTo:(unsigned int)width by:(unsigned int)height +{ + NSRect frame = NSMakeRect([myView frame].origin.x, + [myView frame].origin.y, + width, + height); + + [myView setFrame:frame]; +} + + +//////////////////////////////////////////////////////// +-(void)setIconTo:(unsigned int)width + by:(unsigned int)height + with:(sf::Uint8 const *)pixels +{ + sf::Err() << "Cannot set an icon to the view." << std::endl; +} + + +//////////////////////////////////////////////////////// +-(void)processEventWithBlockingMode:(BOOL)block +{ + sf::Err() << "Cannot process event from the view." << std::endl; +} + + +//////////////////////////////////////////////////////// +-(void)applyContext:(NSOpenGLContext *)context +{ + [myOGLView setOpenGLContext:context]; + [context setView:myOGLView]; +} + + +@end