]>
Commit | Line | Data |
---|---|---|
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 | ||
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 | ||
34 | #ifdef WXP_WITH_THREAD | |
35 | extern PyThreadState* wxPyEventThreadState; | |
36 | extern bool wxPyInEvent; | |
37 | #endif | |
38 | ||
39 | //---------------------------------------------------------------------- | |
40 | ||
41 | class wxPyApp: public wxApp | |
42 | { | |
43 | public: | |
44 | wxPyApp(); | |
45 | ~wxPyApp(); | |
46 | int MainLoop(void); | |
47 | bool OnInit(void); | |
48 | //# void AfterMainLoop(void); | |
49 | }; | |
50 | ||
51 | extern wxPyApp *wxPythonApp; | |
52 | ||
53 | //---------------------------------------------------------------------- | |
54 | ||
55 | void __wxPreStart(); | |
56 | PyObject* __wxStart(PyObject*, PyObject* args); | |
57 | ||
58 | extern PyObject* wxPython_dict; | |
59 | PyObject* __wxSetDictionary(PyObject*, PyObject* args); | |
60 | ||
61 | void wxPyEventThunker(wxObject*, wxEvent& event); | |
62 | PyObject* wxPyConstructObject(void* ptr, char* className); | |
63 | ||
64 | //---------------------------------------------------------------------- | |
65 | ||
66 | ||
67 | #ifndef SWIGCODE | |
68 | extern "C" void SWIG_MakePtr(char *, void *, char *); | |
69 | extern "C" char *SWIG_GetPtr(char *, void **, char *); | |
70 | #endif | |
71 | ||
72 | ||
73 | #ifdef _MSC_VER | |
74 | # pragma warning(disable:4800) | |
75 | #endif | |
76 | ||
77 | typedef unsigned char byte; | |
78 | ||
79 | ||
80 | // Non-const versions to keep SWIG happy. | |
81 | extern wxPoint wxPyDefaultPosition; | |
82 | extern wxSize wxPyDefaultSize; | |
83 | extern wxString wxPyEmptyStr; | |
84 | ||
85 | //---------------------------------------------------------------------- | |
86 | ||
87 | class wxPyCallback : public wxObject { | |
88 | public: | |
89 | wxPyCallback(PyObject* func); | |
90 | ~wxPyCallback(); | |
91 | ||
92 | void EventThunker(wxEvent& event); | |
93 | ||
94 | PyObject* m_func; | |
95 | }; | |
96 | ||
97 | //--------------------------------------------------------------------------- | |
98 | ||
99 | // class wxPyMenu : public wxMenu { | |
100 | // public: | |
101 | // wxPyMenu(const wxString& title = "", PyObject* func=NULL); | |
102 | // ~wxPyMenu(); | |
103 | ||
104 | // private: | |
105 | // static void MenuCallback(wxMenu& menu, wxCommandEvent& evt); | |
106 | // PyObject* func; | |
107 | // }; | |
108 | ||
109 | ||
110 | //--------------------------------------------------------------------------- | |
111 | ||
112 | class wxPyTimer : public wxTimer { | |
113 | public: | |
114 | wxPyTimer(PyObject* callback); | |
115 | ~wxPyTimer(); | |
116 | ||
117 | void Notify(); | |
118 | ||
119 | private: | |
120 | PyObject* func; | |
121 | }; | |
122 | ||
123 | //--------------------------------------------------------------------------- | |
124 | ||
125 | class wxPyEvent : public wxCommandEvent { | |
126 | DECLARE_DYNAMIC_CLASS(wxPyEvent) | |
127 | public: | |
128 | wxPyEvent(wxEventType commandType = wxEVT_NULL, PyObject* userData = Py_None); | |
129 | ~wxPyEvent(); | |
130 | ||
131 | void SetUserData(PyObject* userData); | |
132 | PyObject* GetUserData(); | |
133 | ||
134 | private: | |
135 | PyObject* m_userData; | |
136 | }; | |
137 | ||
138 | ||
139 | ||
140 | ||
141 | ||
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 | //--------------------------------------------------------------------------- | |
149 | ||
150 | class wxPyCallbackHelper { | |
151 | public: | |
152 | wxPyCallbackHelper(); | |
153 | ~wxPyCallbackHelper(); | |
154 | ||
155 | void setSelf(PyObject* self); | |
156 | ||
157 | bool findCallback(const wxString& name); | |
158 | int callCallback(PyObject* argTuple); | |
159 | PyObject* callCallbackObj(PyObject* argTuple); | |
160 | ||
161 | private: | |
162 | PyObject* m_self; | |
163 | PyObject* m_lastFound; | |
164 | }; | |
165 | ||
166 | ||
167 | ||
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 | //--------------------------------------------------------------------------- | |
173 | ||
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)); \ | |
178 | else \ | |
179 | return PCLASS::CBNAME(a,b); \ | |
180 | } \ | |
181 | bool base_##CBNAME(int a, int b) { \ | |
182 | return PCLASS::CBNAME(a,b); \ | |
183 | } | |
184 | ||
185 | //--------------------------------------------------------------------------- | |
186 | ||
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)); \ | |
191 | else \ | |
192 | return PCLASS::CBNAME(a); \ | |
193 | } \ | |
194 | bool base_##CBNAME(int a) { \ | |
195 | return PCLASS::CBNAME(a); \ | |
196 | } | |
197 | ||
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)); \ | |
202 | else return false; \ | |
203 | } | |
204 | ||
205 | ||
206 | //--------------------------------------------------------------------------- | |
207 | ||
208 | #define PYCALLBACK__(PCLASS, CBNAME) \ | |
209 | void CBNAME() { \ | |
210 | if (m_myInst.findCallback(#CBNAME)) \ | |
211 | m_myInst.callCallback(Py_BuildValue("()")); \ | |
212 | else \ | |
213 | PCLASS::CBNAME(); \ | |
214 | } \ | |
215 | void base_##CBNAME() { \ | |
216 | PCLASS::CBNAME(); \ | |
217 | } | |
218 | ||
219 | //--------------------------------------------------------------------------- | |
220 | ||
221 | #define PYPRIVATE \ | |
222 | void _setSelf(PyObject* self) { \ | |
223 | m_myInst.setSelf(self); \ | |
224 | } \ | |
225 | private: wxPyCallbackHelper m_myInst; | |
226 | ||
227 | //--------------------------------------------------------------------------- | |
228 | ||
229 | #endif | |
230 |