1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Helper functions/classes for the wxPython extenaion module
9 // Copyright: (c) 1998 by Total Control Software
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
13 #ifndef __wxp_helpers__
14 #define __wxp_helpers__
19 //----------------------------------------------------------------------
21 // if we want to handle threads and Python threads are available...
22 #if defined(WXP_USE_THREAD) && defined(WITH_THREAD)
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
28 #else // no Python threads...
29 #undef WXP_WITH_THREAD
30 #define wxPy_BEGIN_ALLOW_THREADS
31 #define wxPy_END_ALLOW_THREADS
34 #ifdef WXP_WITH_THREAD
35 extern PyThreadState
* wxPyEventThreadState
;
36 extern bool wxPyInEvent
;
39 //---------------------------------------------------------------------------
41 #if defined(__WXMSW__)
42 # define HELPEREXPORT __declspec(dllexport)
47 //----------------------------------------------------------------------
49 class wxPyApp
: public wxApp
56 //# void AfterMainLoop(void);
59 extern wxPyApp
*wxPythonApp
;
61 //----------------------------------------------------------------------
64 PyObject
* __wxStart(PyObject
*, PyObject
* args
);
66 extern PyObject
* wxPython_dict
;
67 PyObject
* __wxSetDictionary(PyObject
*, PyObject
* args
);
69 void wxPyEventThunker(wxObject
*, wxEvent
& event
);
71 HELPEREXPORT PyObject
* wxPyConstructObject(void* ptr
, char* className
);
73 //----------------------------------------------------------------------
77 extern "C" void SWIG_MakePtr(char *, void *, char *);
78 extern "C" char *SWIG_GetPtr(char *, void **, char *);
83 # pragma warning(disable:4800)
86 typedef unsigned char byte
;
89 // Non-const versions to keep SWIG happy.
90 extern wxPoint wxPyDefaultPosition
;
91 extern wxSize wxPyDefaultSize
;
92 extern wxString wxPyEmptyStr
;
94 //----------------------------------------------------------------------
96 class wxPyCallback
: public wxObject
{
98 wxPyCallback(PyObject
* func
);
101 void EventThunker(wxEvent
& event
);
106 //---------------------------------------------------------------------------
108 // class wxPyMenu : public wxMenu {
110 // wxPyMenu(const wxString& title = "", PyObject* func=NULL);
114 // static void MenuCallback(wxMenu& menu, wxCommandEvent& evt);
119 //---------------------------------------------------------------------------
121 class wxPyTimer
: public wxTimer
{
123 wxPyTimer(PyObject
* callback
);
132 //---------------------------------------------------------------------------
134 class wxPyEvent
: public wxCommandEvent
{
135 DECLARE_DYNAMIC_CLASS(wxPyEvent
)
137 wxPyEvent(wxEventType commandType
= wxEVT_NULL
, PyObject
* userData
= Py_None
);
140 void SetUserData(PyObject
* userData
);
141 PyObject
* GetUserData();
144 PyObject
* m_userData
;
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
156 // wxPyCallbackHelper.
157 //---------------------------------------------------------------------------
159 class HELPEREXPORT wxPyCallbackHelper
{
161 wxPyCallbackHelper();
162 ~wxPyCallbackHelper();
164 void setSelf(PyObject
* self
);
166 bool findCallback(const wxString
& name
);
167 int callCallback(PyObject
* argTuple
);
168 PyObject
* callCallbackObj(PyObject
* argTuple
);
172 PyObject
* m_lastFound
;
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 //---------------------------------------------------------------------------
183 #define PYCALLBACK__(PCLASS, CBNAME) \
185 if (m_myInst.findCallback(#CBNAME)) \
186 m_myInst.callCallback(Py_BuildValue("()")); \
190 void base_##CBNAME() { \
194 //---------------------------------------------------------------------------
197 void _setSelf(PyObject* self) { \
198 m_myInst.setSelf(self); \
200 private: wxPyCallbackHelper m_myInst;
202 //---------------------------------------------------------------------------
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)); \
209 return PCLASS::CBNAME(a,b); \
211 bool base_##CBNAME(int a, int b) { \
212 return PCLASS::CBNAME(a,b); \
215 //---------------------------------------------------------------------------
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)); \
222 return PCLASS::CBNAME(a); \
224 bool base_##CBNAME(int a) { \
225 return PCLASS::CBNAME(a); \
228 //---------------------------------------------------------------------------
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)); \
238 //---------------------------------------------------------------------------
240 #define PYCALLBACK__DC(PCLASS, CBNAME) \
241 void CBNAME(wxDC& a) { \
242 if (m_myInst.findCallback(#CBNAME)) \
243 m_myInst.callCallback(Py_BuildValue("(O)", \
244 wxPyConstructObject(&a, "wxDC"))); \
248 void base_##CBNAME(wxDC& a) { \
254 //---------------------------------------------------------------------------
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)); \
262 PCLASS::CBNAME(a, b); \
264 void base_##CBNAME(wxDC& a, bool b) { \
265 PCLASS::CBNAME(a, b); \
268 //---------------------------------------------------------------------------
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)); \
276 PCLASS::CBNAME(a, b); \
278 void base_##CBNAME(wxDC& a, bool b) { \
279 PCLASS::CBNAME(a, b); \
282 //---------------------------------------------------------------------------
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)); \
289 PCLASS::CBNAME(a, b); \
291 void base_##CBNAME(double a, double b) { \
292 PCLASS::CBNAME(a, b); \
295 //---------------------------------------------------------------------------
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)", \
303 PCLASS::CBNAME(a, b, c, d); \
305 void base_##CBNAME(double a, double b, int c, int d) { \
306 PCLASS::CBNAME(a, b, c, d); \
309 //---------------------------------------------------------------------------
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)); \
318 PCLASS::CBNAME(a, b, c, d, e, f); \
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); \
324 //---------------------------------------------------------------------------
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)); \
333 return PCLASS::CBNAME(a, b, c, d, e, f); \
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); \
339 //---------------------------------------------------------------------------
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)", \
347 PCLASS::CBNAME(a, b, c, d, e); \
349 void base_##CBNAME(bool a, double b, double c, int d, int e) { \
350 PCLASS::CBNAME(a, b, c, d, e); \
353 //---------------------------------------------------------------------------
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"), \
362 PCLASS::CBNAME(a, b, c, d, e); \
364 void base_##CBNAME(wxDC& a, double b, double c, double d, double e) {\
365 PCLASS::CBNAME(a, b, c, d, e); \
368 //---------------------------------------------------------------------------
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"), \
377 PCLASS::CBNAME(a, b); \
379 void base_##CBNAME(wxDC& a, bool b) { \
380 PCLASS::CBNAME(a, b); \
383 //---------------------------------------------------------------------------
385 #define PYCALLBACK__WXCPBOOL2DBL2INT(PCLASS, CBNAME) \
386 void CBNAME(wxControlPoint* a, bool b, double c, double d, \
388 if (m_myInst.findCallback(#CBNAME)) \
389 m_myInst.callCallback(Py_BuildValue("(Oiddii)", \
390 wxPyConstructObject(a, "wxControlPoint"),\
391 (int)b, c, d, e, f)); \
393 PCLASS::CBNAME(a, b, c, d, e, f); \
395 void base_##CBNAME(wxControlPoint* a, bool b, double c, double d, \
397 PCLASS::CBNAME(a, b, c, d, e, f); \
400 //---------------------------------------------------------------------------
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"),\
409 PCLASS::CBNAME(a, b, c, d, e); \
411 void base_##CBNAME(wxControlPoint* a, double b, double c, \
413 PCLASS::CBNAME(a, b, c, d, e); \
416 //---------------------------------------------------------------------------
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)); \
423 PCLASS::CBNAME(a, b, c); \
425 void base_##CBNAME(double a, double b, int c) { \
426 PCLASS::CBNAME(a, b, c); \
429 //---------------------------------------------------------------------------
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));\
436 PCLASS::CBNAME(a, b, c, d); \
438 void base_##CBNAME(bool a, double b, double c, int d) { \
439 PCLASS::CBNAME(a, b, c, d); \
442 //---------------------------------------------------------------------------
443 //---------------------------------------------------------------------------
444 //---------------------------------------------------------------------------
445 //---------------------------------------------------------------------------