]>
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 | extern wxHashTable* wxPyWindows; // keep track of all windows so we | |
62 | // don't accidentally delete them twice. | |
63 | ||
64 | void wxPyEventThunker(wxObject*, wxEvent& event); | |
65 | ||
66 | //---------------------------------------------------------------------- | |
67 | ||
68 | ||
69 | #ifndef SWIGCODE | |
70 | extern "C" void SWIG_MakePtr(char *, void *, char *); | |
71 | extern "C" char *SWIG_GetPtr(char *, void **, char *); | |
72 | #endif | |
73 | ||
74 | ||
75 | #ifdef _MSC_VER | |
76 | # pragma warning(disable:4800) | |
77 | #endif | |
78 | ||
79 | typedef unsigned char byte; | |
80 | ||
81 | ||
82 | // Non-const versions to keep SWIG happy. | |
83 | extern wxPoint wxPyDefaultPosition; | |
84 | extern wxSize wxPyDefaultSize; | |
85 | extern wxString wxPyEmptyStr; | |
86 | ||
87 | //---------------------------------------------------------------------- | |
88 | ||
89 | class wxPyCallback : public wxObject { | |
90 | public: | |
91 | wxPyCallback(PyObject* func); | |
92 | ~wxPyCallback(); | |
93 | ||
94 | void EventThunker(wxEvent& event); | |
95 | ||
96 | PyObject* m_func; | |
97 | }; | |
98 | ||
99 | //--------------------------------------------------------------------------- | |
100 | ||
101 | class wxPyMenu : public wxMenu { | |
102 | public: | |
103 | wxPyMenu(const wxString& title = "", PyObject* func=NULL); | |
104 | ~wxPyMenu(); | |
105 | ||
106 | private: | |
107 | static void MenuCallback(wxMenu& menu, wxCommandEvent& evt); | |
108 | PyObject* func; | |
109 | }; | |
110 | ||
111 | ||
112 | //--------------------------------------------------------------------------- | |
113 | ||
114 | class wxPyTimer : public wxTimer { | |
115 | public: | |
116 | wxPyTimer(PyObject* callback); | |
117 | ~wxPyTimer(); | |
118 | ||
119 | void Notify(); | |
120 | ||
121 | private: | |
122 | PyObject* func; | |
123 | }; | |
124 | ||
125 | //--------------------------------------------------------------------------- | |
126 | ||
127 | class wxPyEvent : public wxCommandEvent { | |
128 | DECLARE_DYNAMIC_CLASS(wxPyEvent) | |
129 | public: | |
130 | wxPyEvent(wxEventType commandType = wxEVT_NULL, PyObject* userData = Py_None); | |
131 | ~wxPyEvent(); | |
132 | ||
133 | void SetUserData(PyObject* userData); | |
134 | PyObject* GetUserData(); | |
135 | ||
136 | private: | |
137 | PyObject* m_userData; | |
138 | }; | |
139 | ||
140 | ||
141 | ||
142 | ||
143 | ||
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 | // wxPyCallbackHelper. | |
150 | //--------------------------------------------------------------------------- | |
151 | ||
152 | class wxPyCallbackHelper { | |
153 | public: | |
154 | wxPyCallbackHelper(); | |
155 | ~wxPyCallbackHelper(); | |
156 | ||
157 | void setSelf(PyObject* self); | |
158 | ||
159 | bool findCallback(const wxString& name); | |
160 | int callCallback(PyObject* argTuple); | |
161 | PyObject* callCallbackObj(PyObject* argTuple); | |
162 | ||
163 | private: | |
164 | PyObject* m_self; | |
165 | PyObject* m_lastFound; | |
166 | }; | |
167 | ||
168 | ||
169 | ||
170 | //--------------------------------------------------------------------------- | |
171 | // These macros are used to implement the virtual methods that should | |
172 | // redirect to a Python method if one exists. The names designate the | |
173 | // return type, if any as well as any parameter types. | |
174 | //--------------------------------------------------------------------------- | |
175 | ||
176 | #define PYCALLBACK_BOOL_INTINT(PCLASS, CBNAME) \ | |
177 | bool CBNAME(int a, int b) { \ | |
178 | if (m_myInst.findCallback(#CBNAME)) \ | |
179 | return m_myInst.callCallback(Py_BuildValue("(ii)",a,b)); \ | |
180 | else \ | |
181 | return PCLASS::CBNAME(a,b); \ | |
182 | } \ | |
183 | bool base_##CBNAME(int a, int b) { \ | |
184 | return PCLASS::CBNAME(a,b); \ | |
185 | } | |
186 | ||
187 | //--------------------------------------------------------------------------- | |
188 | ||
189 | #define PYCALLBACK_BOOL_INT(PCLASS, CBNAME) \ | |
190 | bool CBNAME(int a) { \ | |
191 | if (m_myInst.findCallback(#CBNAME)) \ | |
192 | return m_myInst.callCallback(Py_BuildValue("(i)",a)); \ | |
193 | else \ | |
194 | return PCLASS::CBNAME(a); \ | |
195 | } \ | |
196 | bool base_##CBNAME(int a) { \ | |
197 | return PCLASS::CBNAME(a); \ | |
198 | } | |
199 | ||
200 | #define PYCALLBACK_BOOL_INT_pure(PCLASS, CBNAME) \ | |
201 | bool CBNAME(int a) { \ | |
202 | if (m_myInst.findCallback(#CBNAME)) \ | |
203 | return m_myInst.callCallback(Py_BuildValue("(i)",a)); \ | |
204 | else return false; \ | |
205 | } | |
206 | ||
207 | ||
208 | //--------------------------------------------------------------------------- | |
209 | ||
210 | #define PYCALLBACK__(PCLASS, CBNAME) \ | |
211 | void CBNAME() { \ | |
212 | if (m_myInst.findCallback(#CBNAME)) \ | |
213 | m_myInst.callCallback(Py_BuildValue("()")); \ | |
214 | else \ | |
215 | PCLASS::CBNAME(); \ | |
216 | } \ | |
217 | void base_##CBNAME() { \ | |
218 | PCLASS::CBNAME(); \ | |
219 | } | |
220 | ||
221 | //--------------------------------------------------------------------------- | |
222 | ||
223 | #define PYPRIVATE \ | |
224 | void _setSelf(PyObject* self) { \ | |
225 | m_myInst.setSelf(self); \ | |
226 | } \ | |
227 | private: wxPyCallbackHelper m_myInst; | |
228 | ||
229 | //--------------------------------------------------------------------------- | |
230 | ||
231 | #endif | |
232 |