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 class wxPyApp
: public wxApp
48 void AfterMainLoop(void);
51 extern wxPyApp
*wxPythonApp
;
53 //----------------------------------------------------------------------
56 PyObject
* __wxStart(PyObject
*, PyObject
* args
);
58 extern PyObject
* wxPython_dict
;
59 PyObject
* __wxSetDictionary(PyObject
*, PyObject
* args
);
61 extern wxHashTable
* wxPyWindows
; // keep track of all windows so we
62 // don't accidentally delete them twice.
64 void wxPyEventThunker(wxObject
*, wxEvent
& event
);
66 //----------------------------------------------------------------------
70 extern "C" void SWIG_MakePtr(char *, void *, char *);
71 extern "C" char *SWIG_GetPtr(char *, void **, char *);
76 # pragma warning(disable:4800)
79 typedef unsigned char byte
;
82 // Non-const versions to keep SWIG happy.
83 extern wxPoint wxPyDefaultPosition
;
84 extern wxSize wxPyDefaultSize
;
85 extern wxString wxPyEmptyStr
;
87 //----------------------------------------------------------------------
89 class wxPyCallback
: public wxObject
{
91 wxPyCallback(PyObject
* func
);
94 void EventThunker(wxEvent
& event
);
99 //---------------------------------------------------------------------------
101 class wxPyMenu
: public wxMenu
{
103 wxPyMenu(const wxString
& title
= "", PyObject
* func
=NULL
);
107 static void MenuCallback(wxMenu
& menu
, wxCommandEvent
& evt
);
112 //---------------------------------------------------------------------------
114 class wxPyTimer
: public wxTimer
{
116 wxPyTimer(PyObject
* callback
);
125 //---------------------------------------------------------------------------
127 class wxPyEvent
: public wxCommandEvent
{
128 DECLARE_DYNAMIC_CLASS(wxPyEvent
)
130 wxPyEvent(wxEventType commandType
= wxEVT_NULL
, PyObject
* userData
= Py_None
);
133 void SetUserData(PyObject
* userData
);
134 PyObject
* GetUserData();
137 PyObject
* m_userData
;
144 //---------------------------------------------------------------------------
145 // This class holds an instance of a Python Shadow Class object and assists
146 // with looking up and invoking Python callback methods from C++ virtual
147 // method redirections. For all classes which have virtuals which should be
148 // overridable in wxPython, a new subclass is created that contains a
149 // wxPyCallbackList. This list is used to hold references to the Python
151 //---------------------------------------------------------------------------
153 class wxPyCallbackHelper
{
155 wxPyCallbackHelper();
156 ~wxPyCallbackHelper();
158 void setSelf(PyObject
* self
);
160 bool findCallback(const wxString
& name
);
161 int callCallback(PyObject
* argTuple
);
162 PyObject
* callCallbackObj(PyObject
* argTuple
);
166 PyObject
* m_lastFound
;
171 //---------------------------------------------------------------------------
172 // These macros are used to implement the virtual methods that should
173 // redirect to a Python method if one exists. The names designate the
174 // return type, if any as well as any parameter types.
175 //---------------------------------------------------------------------------
177 #define PYCALLBACK_BOOL_INTINT(PCLASS, CBNAME) \
178 bool CBNAME(int a, int b) { \
179 if (m_myInst.findCallback(#CBNAME)) \
180 return m_myInst.callCallback(Py_BuildValue("(ii)",a,b)); \
182 return PCLASS::CBNAME(a,b); \
184 bool base_##CBNAME(int a, int b) { \
185 return PCLASS::CBNAME(a,b); \
188 //---------------------------------------------------------------------------
190 #define PYCALLBACK_BOOL_INT(PCLASS, CBNAME) \
191 bool CBNAME(int a) { \
192 if (m_myInst.findCallback(#CBNAME)) \
193 return m_myInst.callCallback(Py_BuildValue("(i)",a)); \
195 return PCLASS::CBNAME(a); \
197 bool base_##CBNAME(int a) { \
198 return PCLASS::CBNAME(a); \
201 #define PYCALLBACK_BOOL_INT_pure(PCLASS, CBNAME) \
202 bool CBNAME(int a) { \
203 if (m_myInst.findCallback(#CBNAME)) \
204 return m_myInst.callCallback(Py_BuildValue("(i)",a)); \
209 //---------------------------------------------------------------------------
211 #define PYCALLBACK__(PCLASS, CBNAME) \
213 if (m_myInst.findCallback(#CBNAME)) \
214 m_myInst.callCallback(Py_BuildValue("()")); \
218 void base_##CBNAME() { \
222 //---------------------------------------------------------------------------
225 void _setSelf(PyObject* self) { \
226 m_myInst.setSelf(self); \
228 private: wxPyCallbackHelper m_myInst;
230 //---------------------------------------------------------------------------