]>
Commit | Line | Data |
---|---|---|
d14a1e28 RD |
1 | #---------------------------------------------------------------------------- |
2 | ||
3 | # Use Python's bool constants if available, make aliases if not | |
4 | try: | |
5 | True | |
6 | except NameError: | |
7 | True = 1==1 | |
8 | False = 1==0 | |
9 | ||
10 | # Backwards compaatibility | |
11 | TRUE = true = True | |
12 | FALSE = false = False | |
13 | ||
14 | ||
15 | # workarounds for bad wxRTTI names | |
16 | __wxPyPtrTypeMap['wxGauge95'] = 'wxGauge' | |
17 | __wxPyPtrTypeMap['wxSlider95'] = 'wxSlider' | |
18 | __wxPyPtrTypeMap['wxStatusBar95'] = 'wxStatusBar' | |
19 | ||
20 | ||
21 | #---------------------------------------------------------------------------- | |
22 | # Load version numbers from __version__... Ensure that major and minor | |
23 | # versions are the same for both wxPython and wxWindows. | |
24 | ||
25 | from __version__ import * | |
26 | __version__ = VERSION_STRING | |
27 | ||
28 | assert MAJOR_VERSION == _core.MAJOR_VERSION, "wxPython/wxWindows version mismatch" | |
29 | assert MINOR_VERSION == _core.MINOR_VERSION, "wxPython/wxWindows version mismatch" | |
30 | if RELEASE_VERSION != _core.RELEASE_VERSION: | |
31 | import warnings | |
32 | warnings.warn("wxPython/wxWindows release number mismatch") | |
33 | ||
34 | #---------------------------------------------------------------------------- | |
35 | ||
36 | class PyDeadObjectError(AttributeError): | |
37 | pass | |
38 | ||
39 | ||
40 | class _wxPyDeadObject(object): | |
41 | """ | |
42 | Instances of wx objects that are OOR capable will have their __class__ | |
43 | changed to this class when the C++ object is deleted. This should help | |
44 | prevent crashes due to referencing a bogus C++ pointer. | |
45 | """ | |
46 | reprStr = "wxPython wrapper for DELETED %s object! (The C++ object no longer exists.)" | |
47 | attrStr = "The C++ part of the %s object has been deleted, attribute access no longer allowed." | |
48 | ||
49 | def __repr__( self ): | |
50 | if not hasattr(self, "_name"): | |
51 | self._name = "[unknown]" | |
52 | return self.reprStr % self._name | |
53 | ||
54 | def __getattr__( self, *args ): | |
55 | if not hasattr(self, "_name"): | |
56 | self._name = "[unknown]" | |
57 | raise PyDeadObjectError( self.attrStr % self._name ) | |
58 | ||
59 | def __nonzero__(self): | |
60 | return 0 | |
61 | ||
62 | ||
63 | #---------------------------------------------------------------------------- | |
64 | _wxPyCallAfterId = None | |
65 | ||
66 | def CallAfter(callable, *args, **kw): | |
67 | """ | |
68 | Call the specified function after the current and pending event | |
69 | handlers have been completed. This is also good for making GUI | |
70 | method calls from non-GUI threads. | |
71 | """ | |
72 | app = wx.GetApp() | |
73 | assert app, 'No wxApp created yet' | |
74 | ||
75 | global _wxPyCallAfterId | |
76 | if _wxPyCallAfterId is None: | |
77 | _wxPyCallAfterId = wx.NewEventType() | |
78 | app.Connect(-1, -1, _wxPyCallAfterId, | |
79 | lambda event: event.callable(*event.args, **event.kw) ) | |
80 | evt = wx.PyEvent() | |
81 | evt.SetEventType(_wxPyCallAfterId) | |
82 | evt.callable = callable | |
83 | evt.args = args | |
84 | evt.kw = kw | |
85 | wx.PostEvent(app, evt) | |
86 | ||
87 | ||
88 | #---------------------------------------------------------------------------- | |
89 | ||
90 | ||
91 | class FutureCall: | |
92 | """ | |
93 | A convenience class for wxTimer, that calls the given callable | |
94 | object once after the given amount of milliseconds, passing any | |
95 | positional or keyword args. The return value of the callable is | |
96 | availbale after it has been run with the GetResult method. | |
97 | ||
98 | If you don't need to get the return value or restart the timer | |
99 | then there is no need to hold a reference to this object. It will | |
100 | hold a reference to itself while the timer is running (the timer | |
101 | has a reference to self.Notify) but the cycle will be broken when | |
102 | the timer completes, automatically cleaning up the wx.FutureCall | |
103 | object. | |
104 | """ | |
105 | def __init__(self, millis, callable, *args, **kwargs): | |
106 | self.millis = millis | |
107 | self.callable = callable | |
108 | self.SetArgs(*args, **kwargs) | |
109 | self.runCount = 0 | |
110 | self.hasRun = False | |
111 | self.result = None | |
112 | self.timer = None | |
113 | self.Start() | |
114 | ||
115 | def __del__(self): | |
116 | self.Stop() | |
117 | ||
118 | ||
119 | def Start(self, millis=None, *args, **kwargs): | |
120 | """ | |
121 | (Re)start the timer | |
122 | """ | |
123 | self.hasRun = False | |
124 | if millis is not None: | |
125 | self.millis = millis | |
126 | if args or kwargs: | |
127 | self.SetArgs(*args, **kwargs) | |
128 | self.Stop() | |
129 | self.timer = wx.PyTimer(self.Notify) | |
130 | self.timer.Start(self.millis, wx.TIMER_ONE_SHOT) | |
131 | Restart = Start | |
132 | ||
133 | ||
134 | def Stop(self): | |
135 | """ | |
136 | Stop and destroy the timer. | |
137 | """ | |
138 | if self.timer is not None: | |
139 | self.timer.Stop() | |
140 | self.timer = None | |
141 | ||
142 | ||
143 | def GetInterval(self): | |
144 | if self.timer is not None: | |
145 | return self.timer.GetInterval() | |
146 | else: | |
147 | return 0 | |
148 | ||
149 | ||
150 | def IsRunning(self): | |
151 | return self.timer is not None and self.timer.IsRunning() | |
152 | ||
153 | ||
154 | def SetArgs(self, *args, **kwargs): | |
155 | """ | |
156 | (Re)set the args passed to the callable object. This is | |
157 | useful in conjunction with Restart if you want to schedule a | |
158 | new call to the same callable object but with different | |
159 | parameters. | |
160 | """ | |
161 | self.args = args | |
162 | self.kwargs = kwargs | |
163 | ||
164 | ||
165 | def HasRun(self): | |
166 | return self.hasRun | |
167 | ||
168 | def GetResult(self): | |
169 | return self.result | |
170 | ||
171 | def Notify(self): | |
172 | """ | |
173 | The timer has expired so call the callable. | |
174 | """ | |
175 | if self.callable and getattr(self.callable, 'im_self', True): | |
176 | self.runCount += 1 | |
177 | self.result = self.callable(*self.args, **self.kwargs) | |
178 | self.hasRun = True | |
179 | wx.CallAfter(self.Stop) | |
180 | ||
181 | ||
182 | #---------------------------------------------------------------------------- | |
183 | #---------------------------------------------------------------------------- | |
184 | ||
185 | # Import other modules in this package that should show up in the | |
186 | # "core" wx namespace | |
187 | from gdi import * | |
188 | from windows import * | |
189 | from controls import * | |
190 | from misc import * | |
191 | ||
192 | #---------------------------------------------------------------------------- | |
193 | #---------------------------------------------------------------------------- |