]>
Commit | Line | Data |
---|---|---|
d14a1e28 RD |
1 | #---------------------------------------------------------------------------- |
2 | ||
322913ce | 3 | # Use Python's bool constants if available, make some if not |
d14a1e28 RD |
4 | try: |
5 | True | |
6 | except NameError: | |
322913ce RD |
7 | __builtins__.True = 1==1 |
8 | __builtins__.False = 1==0 | |
c19bd0a3 RD |
9 | def bool(value): return not not value |
10 | __builtins__.bool = bool | |
d14a1e28 | 11 | |
d14a1e28 RD |
12 | |
13 | ||
14 | # workarounds for bad wxRTTI names | |
15 | __wxPyPtrTypeMap['wxGauge95'] = 'wxGauge' | |
16 | __wxPyPtrTypeMap['wxSlider95'] = 'wxSlider' | |
17 | __wxPyPtrTypeMap['wxStatusBar95'] = 'wxStatusBar' | |
18 | ||
19 | ||
20 | #---------------------------------------------------------------------------- | |
21 | # Load version numbers from __version__... Ensure that major and minor | |
611190df | 22 | # versions are the same for both wxPython and wxWidgets. |
d14a1e28 RD |
23 | |
24 | from __version__ import * | |
25 | __version__ = VERSION_STRING | |
26 | ||
611190df RD |
27 | assert MAJOR_VERSION == _core_.MAJOR_VERSION, "wxPython/wxWidgets version mismatch" |
28 | assert MINOR_VERSION == _core_.MINOR_VERSION, "wxPython/wxWidgets version mismatch" | |
54f9ee45 | 29 | if RELEASE_VERSION != _core_.RELEASE_VERSION: |
d14a1e28 | 30 | import warnings |
611190df | 31 | warnings.warn("wxPython/wxWidgets release number mismatch") |
d14a1e28 RD |
32 | |
33 | #---------------------------------------------------------------------------- | |
34 | ||
669e06d7 RD |
35 | # Set the default string<-->unicode conversion encoding from the |
36 | # locale. This encoding is used when string or unicode objects need | |
37 | # to be converted in order to pass them to wxWidgets. Please be aware | |
38 | # that the default encoding within the same locale may be slightly | |
39 | # different on different platforms. For example, please see | |
40 | # http://www.alanwood.net/demos/charsetdiffs.html for differences | |
41 | # between the common latin/roman encodings. | |
4293f847 | 42 | import locale |
061d64dd RD |
43 | try: |
44 | default = locale.getdefaultlocale()[1] | |
45 | except ValueError: | |
46 | default = "iso8859-1" | |
4293f847 RD |
47 | if default: |
48 | wx.SetDefaultPyEncoding(default) | |
49 | del default | |
50 | del locale | |
51 | ||
52 | #---------------------------------------------------------------------------- | |
53 | ||
d14a1e28 RD |
54 | class PyDeadObjectError(AttributeError): |
55 | pass | |
56 | ||
d14a1e28 RD |
57 | class _wxPyDeadObject(object): |
58 | """ | |
59 | Instances of wx objects that are OOR capable will have their __class__ | |
60 | changed to this class when the C++ object is deleted. This should help | |
61 | prevent crashes due to referencing a bogus C++ pointer. | |
62 | """ | |
63 | reprStr = "wxPython wrapper for DELETED %s object! (The C++ object no longer exists.)" | |
64 | attrStr = "The C++ part of the %s object has been deleted, attribute access no longer allowed." | |
65 | ||
9e58eb56 | 66 | def __repr__(self): |
d14a1e28 RD |
67 | if not hasattr(self, "_name"): |
68 | self._name = "[unknown]" | |
69 | return self.reprStr % self._name | |
70 | ||
9e58eb56 | 71 | def __getattr__(self, *args): |
d14a1e28 RD |
72 | if not hasattr(self, "_name"): |
73 | self._name = "[unknown]" | |
9e58eb56 RD |
74 | raise PyDeadObjectError(self.attrStr % self._name) |
75 | ||
76 | def __nonzero__(self): | |
77 | return 0 | |
78 | ||
79 | ||
80 | ||
81 | class PyUnbornObjectError(AttributeError): | |
82 | pass | |
83 | ||
84 | class _wxPyUnbornObject(object): | |
85 | """ | |
54f9ee45 | 86 | Some stock objects are created when the wx._core module is |
9e58eb56 RD |
87 | imported, but their C++ instance is not created until the wx.App |
88 | object is created and initialized. These object instances will | |
89 | temporarily have their __class__ changed to this class so an | |
90 | exception will be raised if they are used before the C++ instance | |
91 | is ready. | |
92 | """ | |
93 | ||
94 | reprStr = "wxPython wrapper for UNBORN object! (The C++ object is not initialized yet.)" | |
95 | attrStr = "The C++ part of this object has not been initialized, attribute access not allowed." | |
96 | ||
97 | def __repr__(self): | |
98 | #if not hasattr(self, "_name"): | |
99 | # self._name = "[unknown]" | |
100 | return self.reprStr #% self._name | |
101 | ||
102 | def __getattr__(self, *args): | |
103 | #if not hasattr(self, "_name"): | |
104 | # self._name = "[unknown]" | |
105 | raise PyUnbornObjectError(self.attrStr) # % self._name ) | |
d14a1e28 RD |
106 | |
107 | def __nonzero__(self): | |
108 | return 0 | |
109 | ||
110 | ||
111 | #---------------------------------------------------------------------------- | |
d14a1e28 RD |
112 | |
113 | def CallAfter(callable, *args, **kw): | |
114 | """ | |
115 | Call the specified function after the current and pending event | |
116 | handlers have been completed. This is also good for making GUI | |
61863841 RD |
117 | method calls from non-GUI threads. Any extra positional or |
118 | keyword args are passed on to the callable when it is called. | |
119 | ||
120 | :see: `wx.FutureCall` | |
d14a1e28 RD |
121 | """ |
122 | app = wx.GetApp() | |
2148e701 | 123 | assert app is not None, 'No wx.App created yet' |
d14a1e28 | 124 | |
b0cafb91 RD |
125 | if not hasattr(app, "_CallAfterId"): |
126 | app._CallAfterId = wx.NewEventType() | |
127 | app.Connect(-1, -1, app._CallAfterId, | |
128 | lambda event: event.callable(*event.args, **event.kw) ) | |
d14a1e28 | 129 | evt = wx.PyEvent() |
b0cafb91 | 130 | evt.SetEventType(app._CallAfterId) |
d14a1e28 RD |
131 | evt.callable = callable |
132 | evt.args = args | |
133 | evt.kw = kw | |
134 | wx.PostEvent(app, evt) | |
135 | ||
d14a1e28 RD |
136 | #---------------------------------------------------------------------------- |
137 | ||
138 | ||
139 | class FutureCall: | |
140 | """ | |
a3c9f064 | 141 | A convenience class for `wx.Timer`, that calls the given callable |
d14a1e28 RD |
142 | object once after the given amount of milliseconds, passing any |
143 | positional or keyword args. The return value of the callable is | |
61863841 | 144 | availbale after it has been run with the `GetResult` method. |
d14a1e28 RD |
145 | |
146 | If you don't need to get the return value or restart the timer | |
147 | then there is no need to hold a reference to this object. It will | |
148 | hold a reference to itself while the timer is running (the timer | |
149 | has a reference to self.Notify) but the cycle will be broken when | |
150 | the timer completes, automatically cleaning up the wx.FutureCall | |
151 | object. | |
61863841 RD |
152 | |
153 | :see: `wx.CallAfter` | |
d14a1e28 RD |
154 | """ |
155 | def __init__(self, millis, callable, *args, **kwargs): | |
156 | self.millis = millis | |
157 | self.callable = callable | |
158 | self.SetArgs(*args, **kwargs) | |
159 | self.runCount = 0 | |
66065839 | 160 | self.running = False |
d14a1e28 RD |
161 | self.hasRun = False |
162 | self.result = None | |
163 | self.timer = None | |
164 | self.Start() | |
165 | ||
166 | def __del__(self): | |
167 | self.Stop() | |
168 | ||
169 | ||
170 | def Start(self, millis=None, *args, **kwargs): | |
171 | """ | |
172 | (Re)start the timer | |
173 | """ | |
174 | self.hasRun = False | |
175 | if millis is not None: | |
176 | self.millis = millis | |
177 | if args or kwargs: | |
178 | self.SetArgs(*args, **kwargs) | |
179 | self.Stop() | |
180 | self.timer = wx.PyTimer(self.Notify) | |
181 | self.timer.Start(self.millis, wx.TIMER_ONE_SHOT) | |
66065839 | 182 | self.running = True |
d14a1e28 RD |
183 | Restart = Start |
184 | ||
185 | ||
186 | def Stop(self): | |
187 | """ | |
188 | Stop and destroy the timer. | |
189 | """ | |
190 | if self.timer is not None: | |
191 | self.timer.Stop() | |
192 | self.timer = None | |
193 | ||
194 | ||
195 | def GetInterval(self): | |
196 | if self.timer is not None: | |
197 | return self.timer.GetInterval() | |
198 | else: | |
199 | return 0 | |
200 | ||
201 | ||
202 | def IsRunning(self): | |
203 | return self.timer is not None and self.timer.IsRunning() | |
204 | ||
205 | ||
206 | def SetArgs(self, *args, **kwargs): | |
207 | """ | |
208 | (Re)set the args passed to the callable object. This is | |
209 | useful in conjunction with Restart if you want to schedule a | |
210 | new call to the same callable object but with different | |
211 | parameters. | |
212 | """ | |
213 | self.args = args | |
214 | self.kwargs = kwargs | |
215 | ||
216 | ||
217 | def HasRun(self): | |
218 | return self.hasRun | |
219 | ||
220 | def GetResult(self): | |
221 | return self.result | |
222 | ||
223 | def Notify(self): | |
224 | """ | |
225 | The timer has expired so call the callable. | |
226 | """ | |
227 | if self.callable and getattr(self.callable, 'im_self', True): | |
228 | self.runCount += 1 | |
66065839 | 229 | self.running = False |
d14a1e28 RD |
230 | self.result = self.callable(*self.args, **self.kwargs) |
231 | self.hasRun = True | |
66065839 RD |
232 | if not self.running: |
233 | # if it wasn't restarted, then cleanup | |
234 | wx.CallAfter(self.Stop) | |
d14a1e28 RD |
235 | |
236 | ||
5b29df3f RD |
237 | |
238 | #---------------------------------------------------------------------------- | |
239 | # Control which items in this module should be documented by epydoc. | |
240 | # We allow only classes and functions, which will help reduce the size | |
241 | # of the docs by filtering out the zillions of constants, EVT objects, | |
242 | # and etc that don't make much sense by themselves, but are instead | |
243 | # documented (or will be) as part of the classes/functions/methods | |
244 | # where they should be used. | |
245 | ||
99109c0f RD |
246 | class __DocFilter: |
247 | """ | |
248 | A filter for epydoc that only allows non-Ptr classes and | |
249 | fucntions, in order to reduce the clutter in the API docs. | |
250 | """ | |
251 | def __init__(self, globals): | |
252 | self._globals = globals | |
253 | ||
254 | def __call__(self, name): | |
255 | import types | |
256 | obj = self._globals.get(name, None) | |
257 | if type(obj) not in [type, types.ClassType, types.FunctionType, types.BuiltinFunctionType]: | |
258 | return False | |
259 | if name.startswith('_') or name.endswith('Ptr') or name.startswith('EVT'): | |
260 | return False | |
261 | return True | |
5b29df3f | 262 | |
d14a1e28 RD |
263 | #---------------------------------------------------------------------------- |
264 | #---------------------------------------------------------------------------- | |
265 | ||
266 | # Import other modules in this package that should show up in the | |
267 | # "core" wx namespace | |
54f9ee45 RD |
268 | from _gdi import * |
269 | from _windows import * | |
270 | from _controls import * | |
271 | from _misc import * | |
d14a1e28 | 272 | |
9e58eb56 RD |
273 | |
274 | # Fixup the stock objects since they can't be used yet. (They will be | |
275 | # restored in wx.PyApp.OnInit.) | |
54f9ee45 | 276 | _core_._wxPyFixStockObjects() |
9e58eb56 | 277 | |
d14a1e28 RD |
278 | #---------------------------------------------------------------------------- |
279 | #---------------------------------------------------------------------------- |