]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/src/_core_ex.py
1 #----------------------------------------------------------------------------
3 # Use Python's bool constants if available, make some if not
7 __builtins__
.True = 1==1
8 __builtins__
.False = 1==0
9 def bool(value
): return not not value
10 __builtins__
.bool = bool
14 # workarounds for bad wxRTTI names
15 __wxPyPtrTypeMap
['wxGauge95'] = 'wxGauge'
16 __wxPyPtrTypeMap
['wxSlider95'] = 'wxSlider'
17 __wxPyPtrTypeMap
['wxStatusBar95'] = 'wxStatusBar'
20 #----------------------------------------------------------------------------
21 # Load version numbers from __version__... Ensure that major and minor
22 # versions are the same for both wxPython and wxWidgets.
24 from __version__
import *
25 __version__
= VERSION_STRING
27 assert MAJOR_VERSION
== _core_
.MAJOR_VERSION
, "wxPython/wxWidgets version mismatch"
28 assert MINOR_VERSION
== _core_
.MINOR_VERSION
, "wxPython/wxWidgets version mismatch"
29 if RELEASE_VERSION
!= _core_
.RELEASE_VERSION
:
31 warnings
.warn("wxPython/wxWidgets release number mismatch")
33 #----------------------------------------------------------------------------
35 # Set wxPython's default string<-->unicode conversion encoding from
36 # the locale, but only if Python's default hasn't been changed. (We
37 # assume that if the user has customized it already then that is the
38 # encoding we need to use as well.)
40 # The encoding selected here is used when string or unicode objects
41 # need to be converted in order to pass them to wxWidgets. Please be
42 # aware that the default encoding within the same locale may be
43 # slightly different on different platforms. For example, please see
44 # http://www.alanwood.net/demos/charsetdiffs.html for differences
45 # between the common latin/roman encodings.
47 default
= _sys
.getdefaultencoding()
48 if default
== 'ascii':
52 default
= locale
.getdefaultlocale()[1]
53 codecs
.lookup(default
)
54 except (ValueError, LookupError, TypeError):
55 default
= _sys
.getdefaultencoding()
59 wx
.SetDefaultPyEncoding(default
)
62 #----------------------------------------------------------------------------
64 class PyDeadObjectError(AttributeError):
67 class _wxPyDeadObject(object):
69 Instances of wx objects that are OOR capable will have their __class__
70 changed to this class when the C++ object is deleted. This should help
71 prevent crashes due to referencing a bogus C++ pointer.
73 reprStr
= "wxPython wrapper for DELETED %s object! (The C++ object no longer exists.)"
74 attrStr
= "The C++ part of the %s object has been deleted, attribute access no longer allowed."
77 if not hasattr(self
, "_name"):
78 self
._name
= "[unknown]"
79 return self
.reprStr
% self
._name
81 def __getattr__(self
, *args
):
82 if not hasattr(self
, "_name"):
83 self
._name
= "[unknown]"
84 raise PyDeadObjectError(self
.attrStr
% self
._name
)
86 def __nonzero__(self
):
91 class PyUnbornObjectError(AttributeError):
94 class _wxPyUnbornObject(object):
96 Some stock objects are created when the wx._core module is
97 imported, but their C++ instance is not created until the wx.App
98 object is created and initialized. These object instances will
99 temporarily have their __class__ changed to this class so an
100 exception will be raised if they are used before the C++ instance
104 reprStr
= "wxPython wrapper for UNBORN object! (The C++ object is not initialized yet.)"
105 attrStr
= "The C++ part of this object has not been initialized, attribute access not allowed."
108 #if not hasattr(self, "_name"):
109 # self._name = "[unknown]"
110 return self
.reprStr
#% self._name
112 def __getattr__(self
, *args
):
113 #if not hasattr(self, "_name"):
114 # self._name = "[unknown]"
115 raise PyUnbornObjectError(self
.attrStr
) # % self._name )
117 def __nonzero__(self
):
121 #----------------------------------------------------------------------------
123 def CallAfter(callable, *args
, **kw
):
125 Call the specified function after the current and pending event
126 handlers have been completed. This is also good for making GUI
127 method calls from non-GUI threads. Any extra positional or
128 keyword args are passed on to the callable when it is called.
130 :see: `wx.FutureCall`
133 assert app
is not None, 'No wx.App created yet'
135 if not hasattr(app
, "_CallAfterId"):
136 app
._CallAfterId
= wx
.NewEventType()
137 app
.Connect(-1, -1, app
._CallAfterId
,
138 lambda event
: event
.callable(*event
.args
, **event
.kw
) )
140 evt
.SetEventType(app
._CallAfterId
)
141 evt
.callable = callable
144 wx
.PostEvent(app
, evt
)
146 #----------------------------------------------------------------------------
151 A convenience class for `wx.Timer`, that calls the given callable
152 object once after the given amount of milliseconds, passing any
153 positional or keyword args. The return value of the callable is
154 availbale after it has been run with the `GetResult` method.
156 If you don't need to get the return value or restart the timer
157 then there is no need to hold a reference to this object. It will
158 hold a reference to itself while the timer is running (the timer
159 has a reference to self.Notify) but the cycle will be broken when
160 the timer completes, automatically cleaning up the wx.FutureCall
165 def __init__(self
, millis
, callable, *args
, **kwargs
):
167 self
.callable = callable
168 self
.SetArgs(*args
, **kwargs
)
180 def Start(self
, millis
=None, *args
, **kwargs
):
185 if millis
is not None:
188 self
.SetArgs(*args
, **kwargs
)
190 self
.timer
= wx
.PyTimer(self
.Notify
)
191 self
.timer
.Start(self
.millis
, wx
.TIMER_ONE_SHOT
)
198 Stop and destroy the timer.
200 if self
.timer
is not None:
205 def GetInterval(self
):
206 if self
.timer
is not None:
207 return self
.timer
.GetInterval()
213 return self
.timer
is not None and self
.timer
.IsRunning()
216 def SetArgs(self
, *args
, **kwargs
):
218 (Re)set the args passed to the callable object. This is
219 useful in conjunction with Restart if you want to schedule a
220 new call to the same callable object but with different
235 The timer has expired so call the callable.
237 if self
.callable and getattr(self
.callable, 'im_self', True):
240 self
.result
= self
.callable(*self
.args
, **self
.kwargs
)
243 # if it wasn't restarted, then cleanup
244 wx
.CallAfter(self
.Stop
)
248 #----------------------------------------------------------------------------
249 # Control which items in this module should be documented by epydoc.
250 # We allow only classes and functions, which will help reduce the size
251 # of the docs by filtering out the zillions of constants, EVT objects,
252 # and etc that don't make much sense by themselves, but are instead
253 # documented (or will be) as part of the classes/functions/methods
254 # where they should be used.
258 A filter for epydoc that only allows non-Ptr classes and
259 fucntions, in order to reduce the clutter in the API docs.
261 def __init__(self
, globals):
262 self
._globals
= globals
264 def __call__(self
, name
):
266 obj
= self
._globals
.get(name
, None)
267 if type(obj
) not in [type, types
.ClassType
, types
.FunctionType
, types
.BuiltinFunctionType
]:
269 if name
.startswith('_') or name
.startswith('EVT') or name
.endswith('_swigregister') or name
.endswith('Ptr') :
273 #----------------------------------------------------------------------------
274 #----------------------------------------------------------------------------
276 # Import other modules in this package that should show up in the
277 # "core" wx namespace
279 from _windows
import *
280 from _controls
import *
284 # Fixup the stock objects since they can't be used yet. (They will be
285 # restored in wx.PyApp.OnInit.)
286 _core_
._wxPyFixStockObjects
()
288 #----------------------------------------------------------------------------
289 #----------------------------------------------------------------------------