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 void wxPyEventThunker(wxObject
*, wxEvent
& event
);
62 PyObject
* wxPyConstructObject(void* ptr
, char* className
);
64 //----------------------------------------------------------------------
68 extern "C" void SWIG_MakePtr(char *, void *, char *);
69 extern "C" char *SWIG_GetPtr(char *, void **, char *);
74 # pragma warning(disable:4800)
77 typedef unsigned char byte
;
80 // Non-const versions to keep SWIG happy.
81 extern wxPoint wxPyDefaultPosition
;
82 extern wxSize wxPyDefaultSize
;
83 extern wxString wxPyEmptyStr
;
85 //----------------------------------------------------------------------
87 class wxPyCallback
: public wxObject
{
89 wxPyCallback(PyObject
* func
);
92 void EventThunker(wxEvent
& event
);
97 //---------------------------------------------------------------------------
99 // class wxPyMenu : public wxMenu {
101 // wxPyMenu(const wxString& title = "", PyObject* func=NULL);
105 // static void MenuCallback(wxMenu& menu, wxCommandEvent& evt);
110 //---------------------------------------------------------------------------
112 class wxPyTimer
: public wxTimer
{
114 wxPyTimer(PyObject
* callback
);
123 //---------------------------------------------------------------------------
125 class wxPyEvent
: public wxCommandEvent
{
126 DECLARE_DYNAMIC_CLASS(wxPyEvent
)
128 wxPyEvent(wxEventType commandType
= wxEVT_NULL
, PyObject
* userData
= Py_None
);
131 void SetUserData(PyObject
* userData
);
132 PyObject
* GetUserData();
135 PyObject
* m_userData
;
142 //---------------------------------------------------------------------------
143 // This class holds an instance of a Python Shadow Class object and assists
144 // with looking up and invoking Python callback methods from C++ virtual
145 // method redirections. For all classes which have virtuals which should be
146 // overridable in wxPython, a new subclass is created that contains a
147 // wxPyCallbackHelper.
148 //---------------------------------------------------------------------------
150 class wxPyCallbackHelper
{
152 wxPyCallbackHelper();
153 ~wxPyCallbackHelper();
155 void setSelf(PyObject
* self
);
157 bool findCallback(const wxString
& name
);
158 int callCallback(PyObject
* argTuple
);
159 PyObject
* callCallbackObj(PyObject
* argTuple
);
163 PyObject
* m_lastFound
;
168 //---------------------------------------------------------------------------
169 // These macros are used to implement the virtual methods that should
170 // redirect to a Python method if one exists. The names designate the
171 // return type, if any as well as any parameter types.
172 //---------------------------------------------------------------------------
174 #define PYCALLBACK_BOOL_INTINT(PCLASS, CBNAME) \
175 bool CBNAME(int a, int b) { \
176 if (m_myInst.findCallback(#CBNAME)) \
177 return m_myInst.callCallback(Py_BuildValue("(ii)",a,b)); \
179 return PCLASS::CBNAME(a,b); \
181 bool base_##CBNAME(int a, int b) { \
182 return PCLASS::CBNAME(a,b); \
185 //---------------------------------------------------------------------------
187 #define PYCALLBACK_BOOL_INT(PCLASS, CBNAME) \
188 bool CBNAME(int a) { \
189 if (m_myInst.findCallback(#CBNAME)) \
190 return m_myInst.callCallback(Py_BuildValue("(i)",a)); \
192 return PCLASS::CBNAME(a); \
194 bool base_##CBNAME(int a) { \
195 return PCLASS::CBNAME(a); \
198 #define PYCALLBACK_BOOL_INT_pure(PCLASS, CBNAME) \
199 bool CBNAME(int a) { \
200 if (m_myInst.findCallback(#CBNAME)) \
201 return m_myInst.callCallback(Py_BuildValue("(i)",a)); \
206 //---------------------------------------------------------------------------
208 #define PYCALLBACK__(PCLASS, CBNAME) \
210 if (m_myInst.findCallback(#CBNAME)) \
211 m_myInst.callCallback(Py_BuildValue("()")); \
215 void base_##CBNAME() { \
219 //---------------------------------------------------------------------------
222 void _setSelf(PyObject* self) { \
223 m_myInst.setSelf(self); \
225 private: wxPyCallbackHelper m_myInst;
227 //---------------------------------------------------------------------------