From: Robin Dunn Date: Fri, 25 Aug 2006 21:53:12 +0000 (+0000) Subject: Add wx.App.DisplayAvailable() which can be used to determine if a GUI X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/4d9de11075ad9523c34d7c8401b8eea9d35e57d0 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 --- diff --git a/wxPython/docs/CHANGES.txt b/wxPython/docs/CHANGES.txt index 3704a9283a..924c8d067b 100644 --- a/wxPython/docs/CHANGES.txt +++ b/wxPython/docs/CHANGES.txt @@ -162,8 +162,9 @@ Added wx.HyperlinkCtrl. Added battery and power related functions and events (wxMSW only so far.) See wx.PowerEvent, wx.GetPowerType and wx.GetBatteryState. -Added wx.ListCtrl.HitTestSubItem which returns the sub-item that was -hit (if any) in addition to the item and flags. +Added wx.ListCtrl.HitTestSubItem which returns the sub-item (i.e. the +column in report mode) that was hit (if any) in addition to the item +and flags. Added wrappers for wx.ColourPickerCtrl, wx.DirPickerCtrl, 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 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...) diff --git a/wxPython/include/wx/wxPython/wxPython_int.h b/wxPython/include/wx/wxPython/wxPython_int.h index 4af26ea1fc..460631c1f4 100644 --- a/wxPython/include/wx/wxPython/wxPython_int.h +++ b/wxPython/include/wx/wxPython/wxPython_int.h @@ -251,10 +251,11 @@ bool wxPoint2D_helper(PyObject* source, wxPoint2D** obj); bool wxPySimple_typecheck(PyObject* source, const wxChar* classname, int seqLen); bool wxColour_typecheck(PyObject* source); -bool wxPyCheckForApp(); - // Other helpful stuff +bool wxPyCheckForApp(); +bool wxPyTestDisplayAvailable(); + bool wxPy2int_seq_helper(PyObject* source, int* i1, int* i2); bool wxPy4int_seq_helper(PyObject* source, int* i1, int* i2, int* i3, int* i4); diff --git a/wxPython/src/_app.i b/wxPython/src/_app.i index 5aa20a5d55..4ffdbe075d 100644 --- a/wxPython/src/_app.i +++ b/wxPython/src/_app.i @@ -290,6 +290,27 @@ it wasn't found at all. Raises an exception on non-Windows platforms.", ""); { wxPyRaiseNotImplemented(); return 0; } } #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(); + } + } }; diff --git a/wxPython/src/_app_ex.py b/wxPython/src/_app_ex.py index dc168fbf10..57b90756bd 100644 --- a/wxPython/src/_app_ex.py +++ b/wxPython/src/_app_ex.py @@ -69,7 +69,7 @@ class PyOnDemandOutputWindow: #---------------------------------------------------------------------- _defRedirect = (wx.Platform == '__WXMSW__' or wx.Platform == '__WXMAC__') - + class App(wx.PyApp): """ 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 wxWidgets are fully initialized. """ + wx.PyApp.__init__(self) - if wx.Platform == "__WXMAC__": - try: - import MacOS - if not MacOS.WMAvailable(): - print """\ -This program needs access to the screen. Please run with 'pythonw', -not 'python', and only when you are logged in on the main display of -your Mac.""" - _sys.exit(1) - except SystemExit: - raise - except: - pass + # make sure we can create a GUI + if not self.DisplayAvailable(): + + if wx.Platform == "__WXMAC__": + msg = """This program needs access to the screen. +Please run with 'pythonw', not 'python', and only when you are logged +in on the main display of your Mac.""" + + elif wx.Platform == "__WXGTK__": + msg ="Unable to access the X Display, is $DISPLAY set properly?" + else: + msg = "Unable to create GUI" + # TODO: more description is needed for wxMSW... + + raise SystemExit(msg) + # This has to be done before OnInit self.SetUseBestVisual(useBestVisual) diff --git a/wxPython/src/helpers.cpp b/wxPython/src/helpers.cpp index d4c30cce5f..d562546189 100644 --- a/wxPython/src/helpers.cpp +++ b/wxPython/src/helpers.cpp @@ -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 +} + + //---------------------------------------------------------------------- //----------------------------------------------------------------------