Split out application delegate into separate class, minor formatting.

This commit is contained in:
Camilla Berglund 2010-09-16 03:05:55 +02:00
parent 33b2195fb9
commit 6ab8cd62d5
3 changed files with 94 additions and 39 deletions

View File

@ -232,6 +232,10 @@ int _glfwPlatformTerminate( void )
{ {
// TODO: Probably other cleanup // TODO: Probably other cleanup
[NSApp setDelegate:nil];
[_glfwLibrary.NS.delegate release];
_glfwLibrary.NS.delegate = nil;
[_glfwLibrary.NS.autoreleasePool release]; [_glfwLibrary.NS.autoreleasePool release];
_glfwLibrary.NS.autoreleasePool = nil; _glfwLibrary.NS.autoreleasePool = nil;

View File

@ -29,9 +29,9 @@
#include "internal.h" #include "internal.h"
//======================================================================== //========================================================================
// Delegate for window related notifications // Delegate for window related notifications
// (but also used as an application delegate)
//======================================================================== //========================================================================
@interface GLFWWindowDelegate : NSObject @interface GLFWWindowDelegate : NSObject
@ -39,13 +39,13 @@
_GLFWwindow* window; _GLFWwindow* window;
} }
- (id)initWithRats:(_GLFWwindow*)initWndow; - (id)initWithGlfwWindow:(_GLFWwindow*)initWndow;
@end @end
@implementation GLFWWindowDelegate @implementation GLFWWindowDelegate
- (id)initWithRats:(_GLFWwindow*)initWindow - (id)initWithGlfwWindow:(_GLFWwindow*)initWindow
{ {
self = [super init]; self = [super init];
if (self != nil) if (self != nil)
@ -56,13 +56,8 @@
- (BOOL)windowShouldClose:(id)sender - (BOOL)windowShouldClose:(id)sender
{ {
if (window->windowCloseCallback)
{
if (!window->windowCloseCallback(window))
return NO;
}
window->closed = GL_TRUE; window->closed = GL_TRUE;
return NO; return NO;
} }
@ -79,20 +74,56 @@
window->windowSizeCallback(window, window->width, window->height); window->windowSizeCallback(window, window->width, window->height);
} }
- (void)windowDidMiniaturize:(NSNotification*)notification
{
window->iconified = GL_TRUE;
}
- (void)windowDidDeminiaturize:(NSNotification*)notification
{
window->iconified = GL_FALSE;
}
- (void)windowDidBecomeKey:(NSNotification*)notification
{
_glfwLibrary.activeWindow = window;
}
- (void)windowDidResignKey:(NSNotification*)notification
{
if (window == _glfwLibrary.activeWindow)
_glfwLibrary.activeWindow = NULL;
_glfwClearInput(window);
}
@end
//========================================================================
// Delegate for application related notifications
//========================================================================
@interface GLFWApplicationDelegate : NSObject
@end
@implementation GLFWApplicationDelegate
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{ {
if (window->windowCloseCallback) _GLFWwindow* window;
{
if (!window->windowCloseCallback(window))
return NSTerminateCancel;
}
for (window = _glfwLibrary.windowListHead; window; window = window->next)
window->closed = GL_TRUE; window->closed = GL_TRUE;
return NSTerminateCancel; return NSTerminateCancel;
} }
@end @end
//========================================================================
// Keyboard symbol translation table
//========================================================================
// TODO: Need to find mappings for F13-F15, volume down/up/mute, and eject. // TODO: Need to find mappings for F13-F15, volume down/up/mute, and eject.
static const unsigned int MAC_TO_GLFW_KEYCODE_MAPPING[128] = static const unsigned int MAC_TO_GLFW_KEYCODE_MAPPING[128] =
{ {
@ -250,13 +281,13 @@ static int convertMacKeyCode(unsigned int macKeyCode)
_GLFWwindow* window; _GLFWwindow* window;
} }
- (id)initWithRats:(_GLFWwindow*)initWindow; - (id)initWithGlfwWindow:(_GLFWwindow*)initWindow;
@end @end
@implementation GLFWContentView @implementation GLFWContentView
- (id)initWithRats:(_GLFWwindow*)initWindow - (id)initWithGlfwWindow:(_GLFWwindow*)initWindow
{ {
self = [super init]; self = [super init];
if (self != nil) if (self != nil)
@ -429,14 +460,26 @@ int _glfwPlatformOpenWindow(_GLFWwindow* window,
return GL_FALSE; return GL_FALSE;
} }
window->NS.delegate = [[GLFWWindowDelegate alloc] initWithRats:window]; // We can only have one application delegate, but we only allocate it the
if (window->NS.delegate == nil) // first time we create a window to keep all window code in this file
if (_glfwLibrary.NS.delegate == nil)
{
_glfwLibrary.NS.delegate = [[GLFWApplicationDelegate alloc] init];
if (_glfwLibrary.NS.delegate == nil)
{ {
_glfwSetError(GLFW_INTERNAL_ERROR); _glfwSetError(GLFW_INTERNAL_ERROR);
return GL_FALSE; return GL_FALSE;
} }
[NSApp setDelegate:window->NS.delegate]; [NSApp setDelegate:_glfwLibrary.NS.delegate];
}
window->NS.delegate = [[GLFWWindowDelegate alloc] initWithGlfwWindow:window];
if (window->NS.delegate == nil)
{
_glfwSetError(GLFW_INTERNAL_ERROR);
return GL_FALSE;
}
// Mac OS X needs non-zero color size, so set resonable values // Mac OS X needs non-zero color size, so set resonable values
int colorBits = fbconfig->redBits + fbconfig->greenBits + fbconfig->blueBits; int colorBits = fbconfig->redBits + fbconfig->greenBits + fbconfig->blueBits;
@ -470,15 +513,18 @@ int _glfwPlatformOpenWindow(_GLFWwindow* window,
kCGDisplayModeIsSafeForHardware, kCGDisplayModeIsSafeForHardware,
NULL); NULL);
window->width = [[(id)fullscreenMode objectForKey:(id)kCGDisplayWidth] intValue]; window->width =
window->height = [[(id)fullscreenMode objectForKey:(id)kCGDisplayHeight] intValue]; [[(id)fullscreenMode objectForKey:(id)kCGDisplayWidth] intValue];
window->height =
[[(id)fullscreenMode objectForKey:(id)kCGDisplayHeight] intValue];
} }
unsigned int styleMask = 0; unsigned int styleMask = 0;
if (wndconfig->mode == GLFW_WINDOWED) if (wndconfig->mode == GLFW_WINDOWED)
{ {
styleMask = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask; styleMask = NSTitledWindowMask | NSClosableWindowMask |
NSMiniaturizableWindowMask;
if (!wndconfig->windowNoResize) if (!wndconfig->windowNoResize)
styleMask |= NSResizableWindowMask; styleMask |= NSResizableWindowMask;
@ -495,7 +541,7 @@ int _glfwPlatformOpenWindow(_GLFWwindow* window,
[window->NS.window setTitle:[NSString stringWithCString:wndconfig->title [window->NS.window setTitle:[NSString stringWithCString:wndconfig->title
encoding:NSISOLatin1StringEncoding]]; encoding:NSISOLatin1StringEncoding]];
[window->NS.window setContentView:[[GLFWContentView alloc] initWithRats:window]]; [window->NS.window setContentView:[[GLFWContentView alloc] initWithGlfwWindow:window]];
[window->NS.window setDelegate:window->NS.delegate]; [window->NS.window setDelegate:window->NS.delegate];
[window->NS.window setAcceptsMouseMovedEvents:YES]; [window->NS.window setAcceptsMouseMovedEvents:YES];
[window->NS.window center]; [window->NS.window center];
@ -507,10 +553,12 @@ int _glfwPlatformOpenWindow(_GLFWwindow* window,
} }
unsigned int attribute_count = 0; unsigned int attribute_count = 0;
#define ADD_ATTR(x) attributes[attribute_count++] = x #define ADD_ATTR(x) attributes[attribute_count++] = x
#define ADD_ATTR2(x, y) (void)({ ADD_ATTR(x); ADD_ATTR(y); }) #define ADD_ATTR2(x, y) { ADD_ATTR(x); ADD_ATTR(y); }
#define MAX_ATTRS 24 // urgh
NSOpenGLPixelFormatAttribute attributes[MAX_ATTRS]; // Arbitrary array size here
NSOpenGLPixelFormatAttribute attributes[24];
ADD_ATTR(NSOpenGLPFADoubleBuffer); ADD_ATTR(NSOpenGLPFADoubleBuffer);
@ -553,14 +601,19 @@ int _glfwPlatformOpenWindow(_GLFWwindow* window,
ADD_ATTR(0); ADD_ATTR(0);
window->NSGL.pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes]; #undef ADD_ATTR
#undef ADD_ATTR2
window->NSGL.pixelFormat =
[[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
if (window->NSGL.pixelFormat == nil) if (window->NSGL.pixelFormat == nil)
{ {
_glfwSetError(GLFW_NO_PIXEL_FORMAT); _glfwSetError(GLFW_NO_PIXEL_FORMAT);
return GL_FALSE; return GL_FALSE;
} }
window->NSGL.context = [[NSOpenGLContext alloc] initWithFormat:window->NSGL.pixelFormat window->NSGL.context =
[[NSOpenGLContext alloc] initWithFormat:window->NSGL.pixelFormat
shareContext:nil]; shareContext:nil];
if (window->NSGL.context == nil) if (window->NSGL.context == nil)
{ {
@ -660,7 +713,8 @@ void _glfwPlatformSetWindowSize(_GLFWwindow* window, int width, int height)
void _glfwPlatformSetWindowPos(_GLFWwindow* window, int x, int y) void _glfwPlatformSetWindowPos(_GLFWwindow* window, int x, int y)
{ {
NSRect contentRect = [window->NS.window contentRectForFrameRect:[window->NS.window frame]]; NSRect contentRect =
[window->NS.window contentRectForFrameRect:[window->NS.window frame]];
// We assume here that the client code wants to position the window within the // We assume here that the client code wants to position the window within the
// screen the window currently occupies // screen the window currently occupies
@ -875,7 +929,8 @@ void _glfwPlatformSetMouseCursorPos(_GLFWwindow* window, int x, int y)
CGPoint mainScreenOrigin = CGDisplayBounds( CGMainDisplayID() ).origin; CGPoint mainScreenOrigin = CGDisplayBounds( CGMainDisplayID() ).origin;
double mainScreenHeight = CGDisplayBounds( CGMainDisplayID() ).size.height; double mainScreenHeight = CGDisplayBounds( CGMainDisplayID() ).size.height;
CGPoint targetPoint = CGPointMake( globalPoint.x - mainScreenOrigin.x, CGPoint targetPoint = CGPointMake( globalPoint.x - mainScreenOrigin.x,
mainScreenHeight - globalPoint.y - mainScreenOrigin.y ); mainScreenHeight - globalPoint.y -
mainScreenOrigin.y );
CGDisplayMoveCursorToPoint( CGMainDisplayID(), targetPoint ); CGDisplayMoveCursorToPoint( CGMainDisplayID(), targetPoint );
} }

View File

@ -94,20 +94,16 @@ typedef struct _GLFWwindowNS
//------------------------------------------------------------------------ //------------------------------------------------------------------------
typedef struct _GLFWlibraryNS typedef struct _GLFWlibraryNS
{ {
// Timer data
struct { struct {
double t0; double t0;
} timer; } timer;
// dlopen handle for dynamically-loading extension function pointers // dlopen handle for dynamically loading OpenGL extension entry points
void* OpenGLFramework; void* OpenGLFramework;
int unbundled; int unbundled;
id desktopMode; id desktopMode;
id delegate;
id autoreleasePool; id autoreleasePool;
} _GLFWlibraryNS; } _GLFWlibraryNS;