GetActiveWindow and GetForegroundWindow on MAC

BlitzMax Forums/BlitzMax Programming/GetActiveWindow and GetForegroundWindow on MAC

Grey Alien(Posted 2007) [#1]
Hi, are there equivalent MacOSX API calls to GetActiveWindow and GetForegroundWindow?

On PC, I use GetActiveWindow as soon as my game's graphics window is created to obtain a Window handle that I then use with other API calls such as GetForegroundWindow.

I use GetForegroundWindow to detect if the App has been suspended so that I can pause it. I normally use PeekEvent to capture EVENT_APPSUSPEND however, this doesn't work if a game is loading and the user clicks on some other app or the desktop meanwhile. When the game finishes loading, there is no EVENT_APPSUSPEND to process which I why I'm checking it with an API call to GetForegroundWindow.

If anyone can help with these I'd be very grateful, thanks!


Perturbatio(Posted 2007) [#2]
I don't know if this will help you:

http://www.blitzbasic.com/Community/posts.php?topic=43341#500806

I'm not up on Cocoa


Grey Alien(Posted 2007) [#3]
Thanks but that module doesn't have a Mac version of GetACtiveWindow, oh well.

I don't even know what Cocoa is but I've seen the name round a lot.


Perturbatio(Posted 2007) [#4]
Well, looking at that code and a little probing (Oo-err), I reckon it should look something vaguely like this:



I'm probably way off the mark, but hey, I don't own a Mac and have never coded on one. :)


Grey Alien(Posted 2007) [#5]
interesting, I shall have to investigate...


Perturbatio(Posted 2007) [#6]
any joy with this?


Grey Alien(Posted 2007) [#7]
Sorry had to go out, will check now.


Grey Alien(Posted 2007) [#8]
OK it nearly works. For a start I changed the return line to:

return NSWindow;


as NWWindow was a typo. But anyway it won't compile, I get "parse error before 'NSWindow'". I'm basically saving the above code into a file with extension .m and then importing it into my BMX. Any ideas? Is the return value the wrong type?


Damien Sturdy(Posted 2007) [#9]
This will likely come in very handy, I have done previous searches and was dissapointed that nobody else had done it!


Bobysait(Posted 2016) [#10]
Pretty old post but as I didn't find other related topic, here is how I managed to get it working (on Mac Os X 10.7.5)

It requires some modification in the maxgui module (cocoa and maxgui header)

It won't enable to make a already existing window pop on top, but it will allow to make the window on top at creation
(by the way, shouldn't it be like this natively ? I had to do it because while running my debugger for "sublime text" under MacOsX the debug window was behind the sublime text interface ...)

* In file "maxgui.mod/maxgui.h" @line 84: add
(above other WINDOW_XXX flags)
#define WINDOW_FOREGROUND 1024



* In file "cocoamaxgui.mod/cocoa.macos_tiger.m" @line 2391 : replace the whole "if else" line with this
(that's actually what makes a new window the active window and set it on top of all).
		if (style&WINDOW_HIDDEN){
			[window orderOut:window];
		}
		else{
			if (style&WINDOW_FOREGROUND)
			{
				[window setLevel:NSFloatingWindowLevel];
				[NSApp activateIgnoringOtherApps:YES];
				[window makeKeyAndOrderFront:nil];
			}
			else
			{
				[window makeKeyAndOrderFront:NSApp];
			}
		}


And finally,

* In file "magui.mod/gadget.bmx" @line 68 : add
(wrap the "foreground" flag to the maxgui sdk)

Const WINDOW_FOREGROUND=1024


And as it's not done for windows or linux, you might want to create your window like this to avoid errors on other target platforms (or tweak win32 and fltk too)
?MacOS
Local win:TGadget = CreateWindow("foreground window", 0,0,600,800,, WINDOW_DEFAULT | WINDOW_FOREGROUND)
?

?Not MacOS
Local win:TGadget = CreateWindow("foreground window", 0,0,600,800,, WINDOW_DEFAULT)
?




ps : But before considering anything, consider this :
That's my second day of my life on a mac ... so ... that's maybe not the best way to do it, but currently it works like a charm for me.