[wx-dev] [MacOS] How to force an app to foreground?

Robin Dunn robin at alldunn.com
Sun Feb 3 18:07:41 PST 2008


Stefan.Neis at t-online.de wrote:

> It's working nicely on Linux and _almost_ as nice on MacOS. The only
> problem is that on MacOS, my application is always opened in the
> background, i.e. partially hidden "behind" the terminal window. 

> I've been trying to use wxWindow::Raise function but it didn't help.
> I've been trying to use OSX' "open", but I didn't manage to actually
> pass the question I wanted to get answered by the user to it, so that
> seems useless. 

I have this code in wxPython in a function that tests if the "Display" 
is available (which varies based on platform as to what exactly the 
display is of course) and the code I copied it from also raised the 
application to the front so I did that too.

#ifdef __WXMAC__
     // This is adapted from Python's Mac/Modules/MacOS.c in the
     // MacOS_WMAvailable function.
     bool rv;
     ProcessSerialNumber psn;
		
     /*
     ** This is a fairly innocuous call to make if we don't have a window
     ** manager, or if we have no permission to talk to it. It will print
     ** a message on stderr, but at least it won't abort the process.
     ** It appears the function caches the result itself, and it's cheap, so
     ** no need for us to cache.
     */
#ifdef kCGNullDirectDisplay
     /* On 10.1 CGMainDisplayID() isn't available, and
     ** kCGNullDirectDisplay isn't defined.
     */
     if (CGMainDisplayID() == 0) {
         rv = false;
     } else
#endif
     {
         // Also foreground the application on the first call as a 
side-effect.
         if (GetCurrentProcess(&psn) < 0 || SetFrontProcess(&psn) < 0) {
             rv = false;
         } else {
             rv = true;
         }
     }
     return rv;
#endif


This fixes the problem of the app showing up behind the terminal when 
launching a wxPython script that hasn't been put into an application 
bundle.  I've been thinking of moving this to wxMac's wxApp, or 
someplace else suitable, but I haven't looked into doing that yet.  Then 
it could also do the same for other times you can't use "open" on the 
bundle.  For example I've configured the SVN GUI tool I use to use a wx 
app called DiffMerge by giving it the full command-line to the binary 
inside the app bundle, but it always starts up behind the SVN GUI 
because of that.


-- 
Robin Dunn
Software Craftsman
http://wxPython.org  Java give you jitters?  Relax with wxPython!





More information about the wx-dev mailing list