Add wx.App.DisplayAvailable() which can be used to determine if a GUI

can be created in the current environment.  (Still need an
implementation for wxMSW...)


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@40828 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robin Dunn
2006-08-25 21:53:12 +00:00
parent 086ee959df
commit 4d9de11075
5 changed files with 104 additions and 18 deletions

View File

@@ -162,8 +162,9 @@ Added wx.HyperlinkCtrl.
Added battery and power related functions and events (wxMSW only so Added battery and power related functions and events (wxMSW only so
far.) See wx.PowerEvent, wx.GetPowerType and wx.GetBatteryState. far.) See wx.PowerEvent, wx.GetPowerType and wx.GetBatteryState.
Added wx.ListCtrl.HitTestSubItem which returns the sub-item that was Added wx.ListCtrl.HitTestSubItem which returns the sub-item (i.e. the
hit (if any) in addition to the item and flags. column in report mode) that was hit (if any) in addition to the item
and flags.
Added wrappers for wx.ColourPickerCtrl, wx.DirPickerCtrl, Added wrappers for wx.ColourPickerCtrl, wx.DirPickerCtrl,
wx.FilePickerCtrl, and wx.FontPickerCtrl. wx.FilePickerCtrl, and wx.FontPickerCtrl.
@@ -195,6 +196,9 @@ wx.BitmapFromBufferRGBA factory functions. They enable loading of am
image or bitmap directly from a Python object that implements the image or bitmap directly from a Python object that implements the
buffer interface, such as strings, arrays, etc. buffer interface, such as strings, arrays, etc.
Added wx.App.DisplayAvailable() which can be used to determine if a
GUI can be created in the current environment. (Still need an
implementation for wxMSW...)

View File

@@ -251,10 +251,11 @@ bool wxPoint2D_helper(PyObject* source, wxPoint2D** obj);
bool wxPySimple_typecheck(PyObject* source, const wxChar* classname, int seqLen); bool wxPySimple_typecheck(PyObject* source, const wxChar* classname, int seqLen);
bool wxColour_typecheck(PyObject* source); bool wxColour_typecheck(PyObject* source);
bool wxPyCheckForApp();
// Other helpful stuff // Other helpful stuff
bool wxPyCheckForApp();
bool wxPyTestDisplayAvailable();
bool wxPy2int_seq_helper(PyObject* source, int* i1, int* i2); bool wxPy2int_seq_helper(PyObject* source, int* i1, int* i2);
bool wxPy4int_seq_helper(PyObject* source, int* i1, int* i2, int* i3, int* i4); bool wxPy4int_seq_helper(PyObject* source, int* i1, int* i2, int* i3, int* i4);

View File

@@ -290,6 +290,27 @@ it wasn't found at all. Raises an exception on non-Windows platforms.", "");
{ wxPyRaiseNotImplemented(); return 0; } { wxPyRaiseNotImplemented(); return 0; }
} }
#endif #endif
%extend {
DocStr(DisplayAvailable,
"Tests if it is possible to create a GUI in the current environment.
This will mean different things on the different platforms.
* On X Windows systems this function will return ``False`` if it is
not able to open a connection to the X display, which can happen
if $DISPLAY is not set, or is not set correctly.
* On Mac OS X a ``False`` return value will mean that wx is not
able to access the window manager, which can happen if logged in
remotely or if running from the normal version of python instead
of the framework version, (i.e., pythonw.)
* On MS Windows...
", "");
static bool DisplayAvailable() {
return wxPyTestDisplayAvailable();
}
}
}; };

View File

@@ -69,7 +69,7 @@ class PyOnDemandOutputWindow:
#---------------------------------------------------------------------- #----------------------------------------------------------------------
_defRedirect = (wx.Platform == '__WXMSW__' or wx.Platform == '__WXMAC__') _defRedirect = (wx.Platform == '__WXMSW__' or wx.Platform == '__WXMAC__')
class App(wx.PyApp): class App(wx.PyApp):
""" """
The ``wx.App`` class represents the application and is used to: The ``wx.App`` class represents the application and is used to:
@@ -127,22 +127,26 @@ class App(wx.PyApp):
initialization to ensure that the system, toolkit and initialization to ensure that the system, toolkit and
wxWidgets are fully initialized. wxWidgets are fully initialized.
""" """
wx.PyApp.__init__(self) wx.PyApp.__init__(self)
if wx.Platform == "__WXMAC__": # make sure we can create a GUI
try: if not self.DisplayAvailable():
import MacOS
if not MacOS.WMAvailable(): if wx.Platform == "__WXMAC__":
print """\ msg = """This program needs access to the screen.
This program needs access to the screen. Please run with 'pythonw', Please run with 'pythonw', not 'python', and only when you are logged
not 'python', and only when you are logged in on the main display of in on the main display of your Mac."""
your Mac."""
_sys.exit(1) elif wx.Platform == "__WXGTK__":
except SystemExit: msg ="Unable to access the X Display, is $DISPLAY set properly?"
raise
except:
pass
else:
msg = "Unable to create GUI"
# TODO: more description is needed for wxMSW...
raise SystemExit(msg)
# This has to be done before OnInit # This has to be done before OnInit
self.SetUseBestVisual(useBestVisual) self.SetUseBestVisual(useBestVisual)

View File

@@ -2753,6 +2753,62 @@ int wxPyImageHandler::GetImageCount( wxInputStream& stream ) {
} }
//----------------------------------------------------------------------
// Function to test if the Display (or whatever is the platform equivallent)
// can be connected to. This is accessable from wxPython as a staticmethod of
// wx.App called DisplayAvailable().
bool wxPyTestDisplayAvailable()
{
#ifdef __WXGTK__
Display* display;
display = XOpenDisplay(NULL);
if (display == NULL)
return false;
XCloseDisplay(display);
return true;
#endif
#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
#ifdef __WXMSW__
// TODO...
return true;
#endif
}
//---------------------------------------------------------------------- //----------------------------------------------------------------------
//---------------------------------------------------------------------- //----------------------------------------------------------------------