]>
Commit | Line | Data |
---|---|---|
7bf85405 RD |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: helpers.h | |
3 | // Purpose: Helper functions/classes for the wxPython extenaion module | |
4 | // | |
5 | // Author: Robin Dunn | |
6 | // | |
7 | // Created: 7/1/97 | |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) 1998 by Total Control Software | |
10 | // Licence: wxWindows license | |
11 | ///////////////////////////////////////////////////////////////////////////// | |
12 | ||
13 | #ifndef __wxp_helpers__ | |
14 | #define __wxp_helpers__ | |
15 | ||
16 | #include <wx/wx.h> | |
17 | ||
18 | ||
cf694132 RD |
19 | //---------------------------------------------------------------------- |
20 | ||
21 | // if we want to handle threads and Python threads are available... | |
22 | #if defined(WXP_USE_THREAD) && defined(WITH_THREAD) | |
23 | ||
24 | #define WXP_WITH_THREAD | |
25 | #define wxPy_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS | |
26 | #define wxPy_END_ALLOW_THREADS Py_END_ALLOW_THREADS | |
27 | ||
28 | #else // no Python threads... | |
29 | #undef WXP_WITH_THREAD | |
30 | #define wxPy_BEGIN_ALLOW_THREADS | |
31 | #define wxPy_END_ALLOW_THREADS | |
32 | #endif | |
33 | ||
bb0054cd RD |
34 | #ifdef WXP_WITH_THREAD |
35 | extern PyThreadState* wxPyEventThreadState; | |
36 | extern bool wxPyInEvent; | |
37 | #endif | |
cf694132 | 38 | |
efc5f224 RD |
39 | //--------------------------------------------------------------------------- |
40 | ||
41 | #if defined(__WXMSW__) | |
42 | # define HELPEREXPORT __declspec(dllexport) | |
43 | #else | |
44 | # define HELPEREXPORT | |
45 | #endif | |
46 | ||
7bf85405 RD |
47 | //---------------------------------------------------------------------- |
48 | ||
49 | class wxPyApp: public wxApp | |
50 | { | |
51 | public: | |
cf694132 RD |
52 | wxPyApp(); |
53 | ~wxPyApp(); | |
7bf85405 RD |
54 | int MainLoop(void); |
55 | bool OnInit(void); | |
8bf5d46e | 56 | //# void AfterMainLoop(void); |
7bf85405 RD |
57 | }; |
58 | ||
59 | extern wxPyApp *wxPythonApp; | |
60 | ||
61 | //---------------------------------------------------------------------- | |
62 | ||
0d6f9504 | 63 | void __wxPreStart(); |
7bf85405 RD |
64 | PyObject* __wxStart(PyObject*, PyObject* args); |
65 | ||
66 | extern PyObject* wxPython_dict; | |
67 | PyObject* __wxSetDictionary(PyObject*, PyObject* args); | |
68 | ||
7bf85405 | 69 | void wxPyEventThunker(wxObject*, wxEvent& event); |
efc5f224 RD |
70 | |
71 | HELPEREXPORT PyObject* wxPyConstructObject(void* ptr, char* className); | |
7bf85405 RD |
72 | |
73 | //---------------------------------------------------------------------- | |
74 | ||
75 | ||
76 | #ifndef SWIGCODE | |
77 | extern "C" void SWIG_MakePtr(char *, void *, char *); | |
78 | extern "C" char *SWIG_GetPtr(char *, void **, char *); | |
79 | #endif | |
80 | ||
81 | ||
82 | #ifdef _MSC_VER | |
83 | # pragma warning(disable:4800) | |
84 | #endif | |
85 | ||
b639c3c5 RD |
86 | typedef unsigned char byte; |
87 | ||
7bf85405 RD |
88 | |
89 | // Non-const versions to keep SWIG happy. | |
90 | extern wxPoint wxPyDefaultPosition; | |
91 | extern wxSize wxPyDefaultSize; | |
7bf85405 RD |
92 | extern wxString wxPyEmptyStr; |
93 | ||
94 | //---------------------------------------------------------------------- | |
95 | ||
96 | class wxPyCallback : public wxObject { | |
97 | public: | |
cf694132 RD |
98 | wxPyCallback(PyObject* func); |
99 | ~wxPyCallback(); | |
7bf85405 RD |
100 | |
101 | void EventThunker(wxEvent& event); | |
102 | ||
103 | PyObject* m_func; | |
104 | }; | |
105 | ||
106 | //--------------------------------------------------------------------------- | |
107 | ||
8bf5d46e RD |
108 | // class wxPyMenu : public wxMenu { |
109 | // public: | |
110 | // wxPyMenu(const wxString& title = "", PyObject* func=NULL); | |
111 | // ~wxPyMenu(); | |
7bf85405 | 112 | |
8bf5d46e RD |
113 | // private: |
114 | // static void MenuCallback(wxMenu& menu, wxCommandEvent& evt); | |
115 | // PyObject* func; | |
116 | // }; | |
714e6a9e | 117 | |
7bf85405 RD |
118 | |
119 | //--------------------------------------------------------------------------- | |
120 | ||
121 | class wxPyTimer : public wxTimer { | |
122 | public: | |
123 | wxPyTimer(PyObject* callback); | |
124 | ~wxPyTimer(); | |
125 | ||
126 | void Notify(); | |
127 | ||
128 | private: | |
129 | PyObject* func; | |
130 | }; | |
131 | ||
cf694132 RD |
132 | //--------------------------------------------------------------------------- |
133 | ||
134 | class wxPyEvent : public wxCommandEvent { | |
135 | DECLARE_DYNAMIC_CLASS(wxPyEvent) | |
136 | public: | |
137 | wxPyEvent(wxEventType commandType = wxEVT_NULL, PyObject* userData = Py_None); | |
138 | ~wxPyEvent(); | |
139 | ||
140 | void SetUserData(PyObject* userData); | |
141 | PyObject* GetUserData(); | |
142 | ||
143 | private: | |
144 | PyObject* m_userData; | |
145 | }; | |
146 | ||
bb0054cd RD |
147 | |
148 | ||
149 | ||
150 | ||
151 | //--------------------------------------------------------------------------- | |
152 | // This class holds an instance of a Python Shadow Class object and assists | |
153 | // with looking up and invoking Python callback methods from C++ virtual | |
154 | // method redirections. For all classes which have virtuals which should be | |
155 | // overridable in wxPython, a new subclass is created that contains a | |
a08cbc01 | 156 | // wxPyCallbackHelper. |
bb0054cd RD |
157 | //--------------------------------------------------------------------------- |
158 | ||
efc5f224 | 159 | class HELPEREXPORT wxPyCallbackHelper { |
bb0054cd RD |
160 | public: |
161 | wxPyCallbackHelper(); | |
162 | ~wxPyCallbackHelper(); | |
163 | ||
164 | void setSelf(PyObject* self); | |
165 | ||
166 | bool findCallback(const wxString& name); | |
167 | int callCallback(PyObject* argTuple); | |
168 | PyObject* callCallbackObj(PyObject* argTuple); | |
169 | ||
170 | private: | |
171 | PyObject* m_self; | |
172 | PyObject* m_lastFound; | |
173 | }; | |
174 | ||
175 | ||
176 | ||
177 | //--------------------------------------------------------------------------- | |
178 | // These macros are used to implement the virtual methods that should | |
179 | // redirect to a Python method if one exists. The names designate the | |
180 | // return type, if any as well as any parameter types. | |
181 | //--------------------------------------------------------------------------- | |
182 | ||
efc5f224 RD |
183 | #define PYCALLBACK__(PCLASS, CBNAME) \ |
184 | void CBNAME() { \ | |
185 | if (m_myInst.findCallback(#CBNAME)) \ | |
186 | m_myInst.callCallback(Py_BuildValue("()")); \ | |
187 | else \ | |
188 | PCLASS::CBNAME(); \ | |
189 | } \ | |
190 | void base_##CBNAME() { \ | |
191 | PCLASS::CBNAME(); \ | |
192 | } | |
193 | ||
194 | //--------------------------------------------------------------------------- | |
195 | ||
196 | #define PYPRIVATE \ | |
197 | void _setSelf(PyObject* self) { \ | |
198 | m_myInst.setSelf(self); \ | |
199 | } \ | |
200 | private: wxPyCallbackHelper m_myInst; | |
201 | ||
202 | //--------------------------------------------------------------------------- | |
203 | ||
bb0054cd RD |
204 | #define PYCALLBACK_BOOL_INTINT(PCLASS, CBNAME) \ |
205 | bool CBNAME(int a, int b) { \ | |
206 | if (m_myInst.findCallback(#CBNAME)) \ | |
207 | return m_myInst.callCallback(Py_BuildValue("(ii)",a,b)); \ | |
208 | else \ | |
209 | return PCLASS::CBNAME(a,b); \ | |
210 | } \ | |
211 | bool base_##CBNAME(int a, int b) { \ | |
212 | return PCLASS::CBNAME(a,b); \ | |
213 | } | |
214 | ||
215 | //--------------------------------------------------------------------------- | |
216 | ||
217 | #define PYCALLBACK_BOOL_INT(PCLASS, CBNAME) \ | |
218 | bool CBNAME(int a) { \ | |
219 | if (m_myInst.findCallback(#CBNAME)) \ | |
220 | return m_myInst.callCallback(Py_BuildValue("(i)",a)); \ | |
221 | else \ | |
222 | return PCLASS::CBNAME(a); \ | |
223 | } \ | |
224 | bool base_##CBNAME(int a) { \ | |
225 | return PCLASS::CBNAME(a); \ | |
226 | } | |
227 | ||
efc5f224 RD |
228 | //--------------------------------------------------------------------------- |
229 | ||
bb0054cd RD |
230 | #define PYCALLBACK_BOOL_INT_pure(PCLASS, CBNAME) \ |
231 | bool CBNAME(int a) { \ | |
232 | if (m_myInst.findCallback(#CBNAME)) \ | |
233 | return m_myInst.callCallback(Py_BuildValue("(i)",a)); \ | |
234 | else return false; \ | |
235 | } | |
236 | ||
237 | ||
238 | //--------------------------------------------------------------------------- | |
239 | ||
efc5f224 RD |
240 | #define PYCALLBACK__DC(PCLASS, CBNAME) \ |
241 | void CBNAME(wxDC& a) { \ | |
bb0054cd | 242 | if (m_myInst.findCallback(#CBNAME)) \ |
efc5f224 RD |
243 | m_myInst.callCallback(Py_BuildValue("(O)", \ |
244 | wxPyConstructObject(&a, "wxDC"))); \ | |
bb0054cd | 245 | else \ |
efc5f224 | 246 | PCLASS::CBNAME(a); \ |
bb0054cd | 247 | } \ |
efc5f224 RD |
248 | void base_##CBNAME(wxDC& a) { \ |
249 | PCLASS::CBNAME(a); \ | |
bb0054cd RD |
250 | } |
251 | ||
efc5f224 RD |
252 | |
253 | ||
bb0054cd RD |
254 | //--------------------------------------------------------------------------- |
255 | ||
efc5f224 RD |
256 | #define PYCALLBACK__DCBOOL(PCLASS, CBNAME) \ |
257 | void CBNAME(wxDC& a, bool b) { \ | |
258 | if (m_myInst.findCallback(#CBNAME)) \ | |
259 | m_myInst.callCallback(Py_BuildValue("(Oi)", \ | |
260 | wxPyConstructObject(&a, "wxDC"), (int)b)); \ | |
261 | else \ | |
262 | PCLASS::CBNAME(a, b); \ | |
263 | } \ | |
264 | void base_##CBNAME(wxDC& a, bool b) { \ | |
265 | PCLASS::CBNAME(a, b); \ | |
266 | } | |
267 | ||
268 | //--------------------------------------------------------------------------- | |
269 | ||
270 | #define PYCALLBACK__DCBOOL(PCLASS, CBNAME) \ | |
271 | void CBNAME(wxDC& a, bool b) { \ | |
272 | if (m_myInst.findCallback(#CBNAME)) \ | |
273 | m_myInst.callCallback(Py_BuildValue("(Oi)", \ | |
274 | wxPyConstructObject(&a, "wxDC"), (int)b)); \ | |
275 | else \ | |
276 | PCLASS::CBNAME(a, b); \ | |
277 | } \ | |
278 | void base_##CBNAME(wxDC& a, bool b) { \ | |
279 | PCLASS::CBNAME(a, b); \ | |
280 | } | |
281 | ||
282 | //--------------------------------------------------------------------------- | |
283 | ||
284 | #define PYCALLBACK__2DBL(PCLASS, CBNAME) \ | |
285 | void CBNAME(double a, double b) { \ | |
286 | if (m_myInst.findCallback(#CBNAME)) \ | |
287 | m_myInst.callCallback(Py_BuildValue("(dd)",a,b)); \ | |
288 | else \ | |
289 | PCLASS::CBNAME(a, b); \ | |
290 | } \ | |
291 | void base_##CBNAME(double a, double b) { \ | |
292 | PCLASS::CBNAME(a, b); \ | |
293 | } | |
294 | ||
295 | //--------------------------------------------------------------------------- | |
296 | ||
297 | #define PYCALLBACK__2DBL2INT(PCLASS, CBNAME) \ | |
298 | void CBNAME(double a, double b, int c, int d) { \ | |
299 | if (m_myInst.findCallback(#CBNAME)) \ | |
300 | m_myInst.callCallback(Py_BuildValue("(ddii)", \ | |
301 | a,b,c,d)); \ | |
302 | else \ | |
303 | PCLASS::CBNAME(a, b, c, d); \ | |
304 | } \ | |
305 | void base_##CBNAME(double a, double b, int c, int d) { \ | |
306 | PCLASS::CBNAME(a, b, c, d); \ | |
307 | } | |
308 | ||
309 | //--------------------------------------------------------------------------- | |
310 | ||
311 | #define PYCALLBACK__DC4DBLBOOL(PCLASS, CBNAME) \ | |
312 | void CBNAME(wxDC& a, double b, double c, double d, double e, bool f) {\ | |
313 | if (m_myInst.findCallback(#CBNAME)) \ | |
314 | m_myInst.callCallback(Py_BuildValue("(Oddddi)", \ | |
315 | wxPyConstructObject(&a, "wxDC"), \ | |
316 | b, c, d, e, (int)f)); \ | |
317 | else \ | |
318 | PCLASS::CBNAME(a, b, c, d, e, f); \ | |
319 | } \ | |
320 | void base_##CBNAME(wxDC& a, double b, double c, double d, double e, bool f) {\ | |
321 | PCLASS::CBNAME(a, b, c, d, e, f); \ | |
322 | } | |
323 | ||
324 | //--------------------------------------------------------------------------- | |
325 | ||
326 | #define PYCALLBACK_BOOL_DC4DBLBOOL(PCLASS, CBNAME) \ | |
327 | bool CBNAME(wxDC& a, double b, double c, double d, double e, bool f) {\ | |
328 | if (m_myInst.findCallback(#CBNAME)) \ | |
329 | return m_myInst.callCallback(Py_BuildValue("(Oddddi)", \ | |
330 | wxPyConstructObject(&a, "wxDC"), \ | |
331 | b, c, d, e, (int)f)); \ | |
332 | else \ | |
333 | return PCLASS::CBNAME(a, b, c, d, e, f); \ | |
334 | } \ | |
335 | bool base_##CBNAME(wxDC& a, double b, double c, double d, double e, bool f) {\ | |
336 | return PCLASS::CBNAME(a, b, c, d, e, f); \ | |
337 | } | |
338 | ||
339 | //--------------------------------------------------------------------------- | |
340 | ||
341 | #define PYCALLBACK__BOOL2DBL2INT(PCLASS, CBNAME) \ | |
342 | void CBNAME(bool a, double b, double c, int d, int e) { \ | |
343 | if (m_myInst.findCallback(#CBNAME)) \ | |
344 | m_myInst.callCallback(Py_BuildValue("(idii)", \ | |
345 | (int)a,b,c,d,e)); \ | |
346 | else \ | |
347 | PCLASS::CBNAME(a, b, c, d, e); \ | |
348 | } \ | |
349 | void base_##CBNAME(bool a, double b, double c, int d, int e) { \ | |
350 | PCLASS::CBNAME(a, b, c, d, e); \ | |
351 | } | |
352 | ||
353 | //--------------------------------------------------------------------------- | |
354 | ||
355 | #define PYCALLBACK__DC4DBL(PCLASS, CBNAME) \ | |
356 | void CBNAME(wxDC& a, double b, double c, double d, double e) { \ | |
357 | if (m_myInst.findCallback(#CBNAME)) \ | |
358 | m_myInst.callCallback(Py_BuildValue("(Odddd)", \ | |
359 | wxPyConstructObject(&a, "wxDC"), \ | |
360 | b, c, d, e)); \ | |
361 | else \ | |
362 | PCLASS::CBNAME(a, b, c, d, e); \ | |
363 | } \ | |
364 | void base_##CBNAME(wxDC& a, double b, double c, double d, double e) {\ | |
365 | PCLASS::CBNAME(a, b, c, d, e); \ | |
366 | } | |
367 | ||
368 | //--------------------------------------------------------------------------- | |
369 | ||
370 | #define PYCALLBACK__DCBOOL(PCLASS, CBNAME) \ | |
371 | void CBNAME(wxDC& a, bool b) { \ | |
372 | if (m_myInst.findCallback(#CBNAME)) \ | |
373 | m_myInst.callCallback(Py_BuildValue("(Oi)", \ | |
374 | wxPyConstructObject(&a, "wxDC"), \ | |
375 | (int)b)); \ | |
376 | else \ | |
377 | PCLASS::CBNAME(a, b); \ | |
378 | } \ | |
379 | void base_##CBNAME(wxDC& a, bool b) { \ | |
380 | PCLASS::CBNAME(a, b); \ | |
381 | } | |
bb0054cd | 382 | |
7bf85405 | 383 | //--------------------------------------------------------------------------- |
7bf85405 | 384 | |
efc5f224 RD |
385 | #define PYCALLBACK__WXCPBOOL2DBL2INT(PCLASS, CBNAME) \ |
386 | void CBNAME(wxControlPoint* a, bool b, double c, double d, \ | |
387 | int e, int f) { \ | |
388 | if (m_myInst.findCallback(#CBNAME)) \ | |
389 | m_myInst.callCallback(Py_BuildValue("(Oiddii)", \ | |
390 | wxPyConstructObject(a, "wxControlPoint"),\ | |
391 | (int)b, c, d, e, f)); \ | |
392 | else \ | |
393 | PCLASS::CBNAME(a, b, c, d, e, f); \ | |
394 | } \ | |
395 | void base_##CBNAME(wxControlPoint* a, bool b, double c, double d, \ | |
396 | int e, int f) { \ | |
397 | PCLASS::CBNAME(a, b, c, d, e, f); \ | |
398 | } | |
399 | ||
400 | //--------------------------------------------------------------------------- | |
401 | ||
402 | #define PYCALLBACK__WXCP2DBL2INT(PCLASS, CBNAME) \ | |
403 | void CBNAME(wxControlPoint* a, double b, double c, int d, int e) { \ | |
404 | if (m_myInst.findCallback(#CBNAME)) \ | |
405 | m_myInst.callCallback(Py_BuildValue("(Oddii)", \ | |
406 | wxPyConstructObject(a, "wxControlPoint"),\ | |
407 | b, c, d, e)); \ | |
408 | else \ | |
409 | PCLASS::CBNAME(a, b, c, d, e); \ | |
410 | } \ | |
411 | void base_##CBNAME(wxControlPoint* a, double b, double c, \ | |
412 | int d, int e) { \ | |
413 | PCLASS::CBNAME(a, b, c, d, e); \ | |
414 | } | |
415 | ||
416 | //--------------------------------------------------------------------------- | |
417 | ||
418 | #define PYCALLBACK__2DBLINT(PCLASS, CBNAME) \ | |
419 | void CBNAME(double a, double b, int c) { \ | |
420 | if (m_myInst.findCallback(#CBNAME)) \ | |
421 | m_myInst.callCallback(Py_BuildValue("(ddi)", a,b,c)); \ | |
422 | else \ | |
423 | PCLASS::CBNAME(a, b, c); \ | |
424 | } \ | |
425 | void base_##CBNAME(double a, double b, int c) { \ | |
426 | PCLASS::CBNAME(a, b, c); \ | |
427 | } | |
428 | ||
429 | //--------------------------------------------------------------------------- | |
430 | ||
431 | #define PYCALLBACK__BOOL2DBLINT(PCLASS, CBNAME) \ | |
432 | void CBNAME(bool a, double b, double c, int d) { \ | |
433 | if (m_myInst.findCallback(#CBNAME)) \ | |
434 | m_myInst.callCallback(Py_BuildValue("(iddi)", (int)a,b,c,d));\ | |
435 | else \ | |
436 | PCLASS::CBNAME(a, b, c, d); \ | |
437 | } \ | |
438 | void base_##CBNAME(bool a, double b, double c, int d) { \ | |
439 | PCLASS::CBNAME(a, b, c, d); \ | |
440 | } | |
441 | ||
442 | //--------------------------------------------------------------------------- | |
443 | //--------------------------------------------------------------------------- | |
444 | //--------------------------------------------------------------------------- | |
445 | //--------------------------------------------------------------------------- | |
446 | ||
7bf85405 RD |
447 | #endif |
448 | ||
efc5f224 RD |
449 | |
450 |