Need help with my Mac GetActiveWindow code

BlitzMax Forums/BlitzMax Programming/Need help with my Mac GetActiveWindow code

Grey Alien(Posted 2007) [#1]
OK, with the help of some other forum members, I've got some code which gets a handle to the current window on Mac OSX so that it can be passed to other functions that do stuff like return the window position.

This is great, but it only works if I create the app in Windowed mode first. If I create it in Full-screen mode then change to Windowed mode it fails. It seems that KeyWindow may not be correctly set to the window after the full-screen one is destroyed. Any idea how this could be sorted? It's pretty important as it's very likely that a user will start in full-screen and the switch to windowed mode later.

Btw, this code works if you start in windowed mode then flip to fullscreen and back to windowed mode. It only fails if you start in windowed mode. I noticed a very similar problem a little while back where the window was not centred if you started full-screen and then changed to windowed mode. Mark gave me a workaround where I call EndGraphics() then PollInput() before I call Graphics w,h,0. That worked for that problem but doesn't fix this problem.

Clearly there is something funny going on when an app starts in full-screen. Something to do with NSApplication and KeyWindow not being validly set for the window (perhaps KeyWindow becomes null or is still attached to the now destroyed full-screen window??)

To be saved as GAGMacLib.m
#include <AppKit/AppKit.h>

NSWindow* macGetActiveWindow(){
    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
    NSApplication *myApp;
    myApp=[NSApplication sharedApplication];
    NSWindow *myWindow = [myApp keyWindow];
    [pool release];
	return myWindow;
}

void macGetCursorPos(int *x,int *y){
	NSPoint mouseLoc = [NSEvent mouseLocation];
	*x = mouseLoc.x;
	*y = mouseLoc.y;
}

void macGetDesktopSize(int *width,int *height) {
	NSRect rect;
	rect = [[NSScreen deepestScreen] frame];
	*width = rect.size.width;
	*height = rect.size.height;
}

void macGetWindowOrigin(NSWindow *myWindow, int *x, int *y){
    NSRect myFrame = [myWindow frame];
    *x = myFrame.origin.x;
    *y = myFrame.origin.y;
}


Strict

Import "GAGMacLib.m"

Extern
	Function macGetCursorPos(x:Int Ptr, y:Int Ptr)
	Function macGetWindowOrigin(handle: Int, x:Int Ptr, y:Int Ptr)
	Function macGetActiveWindow:Int()
End Extern



Type TPoint
	Field X
	Field Y
End Type

Function GetScreenPointerPos(x:Int Var, y:Int Var)
	macGetCursorPos(Varptr x, Varptr y)
End Function

Graphics 640,480, 32 'comment this line out and it works
Graphics 640,480, 0

Global WindowHandle: Int = macGetActiveWindow()
Print WindowHandle

While Not KeyDown(KEY_ESCAPE)

    Cls

    Local x:Int, y:Int
    GetScreenPointerPos(x, y)

    DrawText "Mouse Pos  = " + x + ", " + y, 50, 50
	Local p:TPoint = New TPoint
	macGetWindowOrigin(WindowHandle, Varptr p.x,Varptr p.y)
    DrawText "Window Pos = " + p.x + ", " + p.y, 50, 70
    Flip

Wend

End



Grey Alien(Posted 2007) [#2]
still need help with this. Anyone got any ideas? thx.