]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: my_typemaps.i | |
3 | // Purpose: Special typemaps specifically for wxPython. | |
4 | // | |
5 | // Author: Robin Dunn | |
6 | // | |
7 | // Created: 7/3/97 | |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) 1998 by Total Control Software | |
10 | // Licence: wxWindows license | |
11 | ///////////////////////////////////////////////////////////////////////////// | |
12 | ||
13 | ||
14 | //--------------------------------------------------------------------------- | |
15 | // Tell SWIG to wrap all the wrappers with our thread protection | |
16 | ||
17 | %exception { | |
18 | PyThreadState* __tstate = wxPyBeginAllowThreads(); | |
19 | $action | |
20 | wxPyEndAllowThreads(__tstate); | |
21 | if (PyErr_Occurred()) SWIG_fail; | |
22 | } | |
23 | ||
24 | ||
25 | //---------------------------------------------------------------------- | |
26 | // Typemaps to convert a list of items to an int (size) and an array | |
27 | ||
28 | %typemap(in) (int choices, wxString* choices_array ) { | |
29 | $1 = PyList_Size($input); | |
30 | $2 = wxString_LIST_helper($input); | |
31 | if ($2 == NULL) SWIG_fail; | |
32 | } | |
33 | %typemap(freearg) (int choices, wxString* choices_array ) { | |
34 | if ($2) delete [] $2; | |
35 | } | |
36 | ||
37 | ||
38 | //--------------------------------------------------------------------------- | |
39 | // wxString typemaps | |
40 | ||
41 | ||
42 | %typemap(in) wxString& (bool temp=False) { | |
43 | $1 = wxString_in_helper($input); | |
44 | if ($1 == NULL) SWIG_fail; | |
45 | temp = True; | |
46 | } | |
47 | %typemap(freearg) wxString& { | |
48 | if (temp$argnum) | |
49 | delete $1; | |
50 | } | |
51 | ||
52 | %typemap(out) wxString& { | |
53 | %#if wxUSE_UNICODE | |
54 | $result = PyUnicode_FromWideChar($1->c_str(), $1->Len()); | |
55 | %#else | |
56 | $result = PyString_FromStringAndSize($1->c_str(), $1->Len()); | |
57 | %#endif | |
58 | } | |
59 | ||
60 | ||
61 | %apply wxString& { wxString* } | |
62 | ||
63 | ||
64 | ||
65 | %typemap(out) wxString { | |
66 | %#if wxUSE_UNICODE | |
67 | $result = PyUnicode_FromWideChar($1.c_str(), $1.Len()); | |
68 | %#else | |
69 | $result = PyString_FromStringAndSize($1.c_str(), $1.Len()); | |
70 | %#endif | |
71 | } | |
72 | ||
73 | %typemap(varout) wxString { | |
74 | %#if wxUSE_UNICODE | |
75 | $result = PyUnicode_FromWideChar($1.c_str(), $1.Len()); | |
76 | %#else | |
77 | $result = PyString_FromStringAndSize($1.c_str(), $1.Len()); | |
78 | %#endif | |
79 | } | |
80 | ||
81 | ||
82 | %typemap(in) wxString { | |
83 | wxString* sptr = wxString_in_helper($input); | |
84 | if (sptr == NULL) SWIG_fail; | |
85 | $1 = *sptr; | |
86 | delete sptr; | |
87 | } | |
88 | ||
89 | %typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxString& { | |
90 | $1 = PyString_Check($input) || PyUnicode_Check($input); | |
91 | } | |
92 | ||
93 | //--------------------------------------------------------------------------- | |
94 | // wxMemoryBuffer (needed for wxSTC) | |
95 | ||
96 | %typemap(in) wxMemoryBuffer& (bool temp=False) { | |
97 | if (!PyString_Check($input)) { | |
98 | PyErr_SetString(PyExc_TypeError, "String buffer expected"); | |
99 | SWIG_fail; | |
100 | } | |
101 | char* str = PyString_AS_STRING($input); | |
102 | int len = PyString_GET_SIZE($input); | |
103 | $1 = new wxMemoryBuffer(len); | |
104 | temp = True; | |
105 | memcpy($1->GetData(), str, len); | |
106 | $1->SetDataLen(len); | |
107 | } | |
108 | ||
109 | %typemap(freearg) wxMemoryBuffer& { | |
110 | if (temp$argnum) delete $1; | |
111 | } | |
112 | ||
113 | %typemap(out) wxMemoryBuffer { | |
114 | $result = PyString_FromStringAndSize((char*)$1.GetData(), $1.GetDataLen()); | |
115 | } | |
116 | ||
117 | ||
118 | ||
119 | //--------------------------------------------------------------------------- | |
120 | // Typemaps to convert Python sequence objects (tuples, etc.) to | |
121 | // wxSize, wxPoint, wxRealPoint, and wxRect. | |
122 | ||
123 | ||
124 | %typemap(in) wxSize& (wxSize temp) { | |
125 | $1 = &temp; | |
126 | if ( ! wxSize_helper($input, &$1)) SWIG_fail; | |
127 | } | |
128 | %typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxSize& { | |
129 | $1 = wxPySimple_typecheck($input, wxT("wxSize"), 2); | |
130 | } | |
131 | ||
132 | ||
133 | %typemap(in) wxPoint& (wxPoint temp) { | |
134 | $1 = &temp; | |
135 | if ( ! wxPoint_helper($input, &$1)) SWIG_fail; | |
136 | } | |
137 | %typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxPoint& { | |
138 | $1 = wxPySimple_typecheck($input, wxT("wxPoint"), 2); | |
139 | } | |
140 | ||
141 | ||
142 | %typemap(in) wxRealPoint& (wxRealPoint temp) { | |
143 | $1 = &temp; | |
144 | if ( ! wxRealPoint_helper($input, &$1)) SWIG_fail; | |
145 | } | |
146 | %typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxRealPoint& { | |
147 | $1 = wxPySimple_typecheck($input, wxT("wxRealPoint"), 2); | |
148 | } | |
149 | ||
150 | ||
151 | %typemap(in) wxRect& (wxRect temp) { | |
152 | $1 = &temp; | |
153 | if ( ! wxRect_helper($input, &$1)) SWIG_fail; | |
154 | } | |
155 | %typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxRect& { | |
156 | $1 = wxPySimple_typecheck($input, wxT("wxRect"), 4); | |
157 | } | |
158 | ||
159 | ||
160 | %typemap(in) wxPoint2D& (wxPoint2D temp) { | |
161 | $1 = &temp; | |
162 | if ( ! wxPoint2D_helper($input, &$1)) SWIG_fail; | |
163 | } | |
164 | %typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxPoint2D& { | |
165 | $1 = wxPySimple_typecheck($input, wxT("wxPoint2D"), 2); | |
166 | } | |
167 | ||
168 | ||
169 | //--------------------------------------------------------------------------- | |
170 | // Typemap to convert strings to wxColour. Two string formats are accepted, | |
171 | // either a colour name, or a hex colour spec like "#RRGGBB" | |
172 | ||
173 | %typemap(in) wxColour& (wxColour temp) { | |
174 | $1 = &temp; | |
175 | if ( ! wxColour_helper($input, &$1)) SWIG_fail; | |
176 | } | |
177 | %typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxColour& { | |
178 | $1 = wxColour_typecheck($input); | |
179 | } | |
180 | ||
181 | ||
182 | ||
183 | //--------------------------------------------------------------------------- | |
184 | // Typemap for wxArrayString from Python sequence objects | |
185 | ||
186 | %typemap(in) wxArrayString& (bool temp=False) { | |
187 | if (! PySequence_Check($input)) { | |
188 | PyErr_SetString(PyExc_TypeError, "Sequence of strings expected."); | |
189 | SWIG_fail; | |
190 | } | |
191 | $1 = new wxArrayString; | |
192 | temp = True; | |
193 | int i, len=PySequence_Length($input); | |
194 | for (i=0; i<len; i++) { | |
195 | PyObject* item = PySequence_GetItem($input, i); | |
196 | %#if wxUSE_UNICODE | |
197 | PyObject* str = PyObject_Unicode(item); | |
198 | %#else | |
199 | PyObject* str = PyObject_Str(item); | |
200 | %#endif | |
201 | if (PyErr_Occurred()) SWIG_fail; | |
202 | $1->Add(Py2wxString(str)); | |
203 | Py_DECREF(item); | |
204 | Py_DECREF(str); | |
205 | } | |
206 | } | |
207 | ||
208 | %typemap(freearg) wxArrayString& { | |
209 | if (temp$argnum) delete $1; | |
210 | } | |
211 | ||
212 | //--------------------------------------------------------------------------- | |
213 | // Typemap for wxArrayInt from Python sequence objects | |
214 | ||
215 | %typemap(in) wxArrayInt& (bool temp=False) { | |
216 | if (! PySequence_Check($input)) { | |
217 | PyErr_SetString(PyExc_TypeError, "Sequence of integers expected."); | |
218 | SWIG_fail; | |
219 | } | |
220 | $1 = new wxArrayInt; | |
221 | temp = True; | |
222 | int i, len=PySequence_Length($input); | |
223 | for (i=0; i<len; i++) { | |
224 | PyObject* item = PySequence_GetItem($input, i); | |
225 | PyObject* number = PyNumber_Int(item); | |
226 | $1->Add(PyInt_AS_LONG(number)); | |
227 | Py_DECREF(item); | |
228 | Py_DECREF(number); | |
229 | } | |
230 | } | |
231 | ||
232 | %typemap(freearg) wxArrayInt& { | |
233 | if (temp$argnum) delete $1; | |
234 | } | |
235 | ||
236 | ||
237 | //--------------------------------------------------------------------------- | |
238 | // Typemaps to convert an array of ints to a list for return values | |
239 | %typemap(out) wxArrayInt& { | |
240 | $result = PyList_New(0); | |
241 | size_t idx; | |
242 | for (idx = 0; idx < $1->GetCount(); idx += 1) { | |
243 | PyObject* val = PyInt_FromLong( $1->Item(idx) ); | |
244 | PyList_Append($result, val); | |
245 | Py_DECREF(val); | |
246 | } | |
247 | } | |
248 | ||
249 | %typemap(out) wxArrayInt { | |
250 | $result = PyList_New(0); | |
251 | size_t idx; | |
252 | for (idx = 0; idx < $1.GetCount(); idx += 1) { | |
253 | PyObject* val = PyInt_FromLong( $1.Item(idx) ); | |
254 | PyList_Append($result, val); | |
255 | Py_DECREF(val); | |
256 | } | |
257 | } | |
258 | ||
259 | ||
260 | ||
261 | // Typemaps to convert an array of ints to a list for return values | |
262 | %typemap(out) wxArrayString& { | |
263 | $result = wxArrayString2PyList_helper(*$1); | |
264 | } | |
265 | ||
266 | %typemap(out) wxArrayString { | |
267 | $result = wxArrayString2PyList_helper($1); | |
268 | } | |
269 | ||
270 | ||
271 | //--------------------------------------------------------------------------- | |
272 | ||
273 | %typemap(out) bool { | |
274 | $result = $1 ? Py_True : Py_False; Py_INCREF($result); | |
275 | } | |
276 | ||
277 | ||
278 | // These fragments are iserted in modules that need to convert PyObjects to | |
279 | // integer values, my versions allow any numeric type to be used, as long as | |
280 | // it can be converted to a PyInt. (Specifically, I allow floats where the | |
281 | // default SWIG_AsLong would raise an obsucre exception from within | |
282 | // PyLong_AsLong.) | |
283 | ||
284 | %fragment("SWIG_AsLong","header") %{ | |
285 | SWIGSTATICINLINE(long) | |
286 | SWIG_AsLong(PyObject * obj) | |
287 | { | |
288 | if (PyNumber_Check(obj)) | |
289 | return PyInt_AsLong(obj); | |
290 | else { | |
291 | PyObject* errmsg = PyString_FromFormat("Expected number, got %s", | |
292 | obj->ob_type->tp_name); | |
293 | PyErr_SetObject(PyExc_TypeError, errmsg); | |
294 | Py_DECREF(errmsg); | |
295 | return 0; | |
296 | } | |
297 | } | |
298 | %} | |
299 | ||
300 | %fragment("SWIG_AsUnsignedLong","header", fragment="SWIG_AsLong") %{ | |
301 | SWIGSTATICINLINE(unsigned long) | |
302 | SWIG_AsUnsignedLong(PyObject * obj) | |
303 | { | |
304 | if (PyLong_Check(obj)) { | |
305 | return PyLong_AsUnsignedLong(obj); | |
306 | } else { | |
307 | long i = SWIG_AsLong(obj); | |
308 | if ( !PyErr_Occurred() && (i < 0)) { | |
309 | PyErr_SetString(PyExc_TypeError, "negative value received for unsigned type"); | |
310 | } | |
311 | return i; | |
312 | } | |
313 | } | |
314 | %} | |
315 | ||
316 | ||
317 | %fragment("SWIG_AsDouble","header") %{ | |
318 | SWIGSTATICINLINE(double) | |
319 | SWIG_AsDouble(PyObject *obj) | |
320 | { | |
321 | if (PyNumber_Check(obj)) | |
322 | return PyFloat_AsDouble(obj); | |
323 | else { | |
324 | PyObject* errmsg = PyString_FromFormat("Expected number, got %s", | |
325 | obj->ob_type->tp_name); | |
326 | PyErr_SetObject(PyExc_TypeError, errmsg); | |
327 | Py_DECREF(errmsg); | |
328 | return 0; | |
329 | } | |
330 | } | |
331 | %} | |
332 | ||
333 | ||
334 | //--------------------------------------------------------------------------- | |
335 | // Typemap for when GDI objects are returned by reference. This will cause a | |
336 | // copy to be made instead of returning a reference to the same instance. The | |
337 | // GDI object's internal refcounting scheme will do a copy-on-write of the | |
338 | // internal data as needed. | |
339 | ||
340 | // These too? | |
341 | //, wxRegion&, wxPalette& | |
342 | ||
343 | %typemap(out) wxBrush&, wxPen&, wxFont&, wxBitmap&, wxIcon&, wxCursor& { | |
344 | $*1_ltype* resultptr = new $*1_ltype(*$1); | |
345 | $result = SWIG_NewPointerObj((void*)(resultptr), $1_descriptor, 1); | |
346 | } | |
347 | ||
348 | ||
349 | //--------------------------------------------------------------------------- | |
350 | // Typemaps to convert return values that are base class pointers | |
351 | // to the real derived type, if possible. See wxPyMake_wxObject in | |
352 | // helpers.cpp | |
353 | ||
354 | %typemap(out) wxEvtHandler* { $result = wxPyMake_wxObject($1); } | |
355 | %typemap(out) wxMenu* { $result = wxPyMake_wxObject($1); } | |
356 | %typemap(out) wxValidator* { $result = wxPyMake_wxObject($1); } | |
357 | ||
358 | %typemap(out) wxApp* { $result = wxPyMake_wxObject($1); } | |
359 | %typemap(out) wxPyApp* { $result = wxPyMake_wxObject($1); } | |
360 | %typemap(out) wxDC* { $result = wxPyMake_wxObject($1); } | |
361 | %typemap(out) wxFSFile* { $result = wxPyMake_wxObject($1); } | |
362 | %typemap(out) wxFileSystem* { $result = wxPyMake_wxObject($1); } | |
363 | %typemap(out) wxGridTableBase* { $result = wxPyMake_wxObject($1); } | |
364 | %typemap(out) wxImageList* { $result = wxPyMake_wxObject($1); } | |
365 | %typemap(out) wxListItem* { $result = wxPyMake_wxObject($1); } | |
366 | %typemap(out) wxMenuItem* { $result = wxPyMake_wxObject($1); } | |
367 | %typemap(out) wxMouseEvent* { $result = wxPyMake_wxObject($1); } | |
368 | %typemap(out) wxObject* { $result = wxPyMake_wxObject($1); } | |
369 | %typemap(out) wxPyPrintout* { $result = wxPyMake_wxObject($1); } | |
370 | %typemap(out) wxToolBarToolBase* { $result = wxPyMake_wxObject($1); } | |
371 | %typemap(out) wxToolTip* { $result = wxPyMake_wxObject($1); } | |
372 | ||
373 | ||
374 | %typemap(out) wxBitmapButton* { $result = wxPyMake_wxObject($1); } | |
375 | %typemap(out) wxButton* { $result = wxPyMake_wxObject($1); } | |
376 | %typemap(out) wxControl* { $result = wxPyMake_wxObject($1); } | |
377 | %typemap(out) wxFrame* { $result = wxPyMake_wxObject($1); } | |
378 | %typemap(out) wxGrid* { $result = wxPyMake_wxObject($1); } | |
379 | %typemap(out) wxListCtrl* { $result = wxPyMake_wxObject($1); } | |
380 | %typemap(out) wxMDIChildFrame* { $result = wxPyMake_wxObject($1); } | |
381 | %typemap(out) wxMDIClientWindow* { $result = wxPyMake_wxObject($1); } | |
382 | %typemap(out) wxMenuBar* { $result = wxPyMake_wxObject($1); } | |
383 | %typemap(out) wxNotebook* { $result = wxPyMake_wxObject($1); } | |
384 | %typemap(out) wxStaticBox* { $result = wxPyMake_wxObject($1); } | |
385 | %typemap(out) wxStatusBar* { $result = wxPyMake_wxObject($1); } | |
386 | %typemap(out) wxTextCtrl* { $result = wxPyMake_wxObject($1); } | |
387 | %typemap(out) wxToolBar* { $result = wxPyMake_wxObject($1); } | |
388 | %typemap(out) wxToolBarBase* { $result = wxPyMake_wxObject($1); } | |
389 | %typemap(out) wxTreeCtrl* { $result = wxPyMake_wxObject($1); } | |
390 | %typemap(out) wxPyTreeCtrl* { $result = wxPyMake_wxObject($1); } | |
391 | %typemap(out) wxWindow* { $result = wxPyMake_wxObject($1); } | |
392 | %typemap(out) wxHtmlWindow* { $result = wxPyMake_wxObject($1); } | |
393 | %typemap(out) wxPyHtmlWindow* { $result = wxPyMake_wxObject($1); } | |
394 | %typemap(out) wxWizardPage* { $result = wxPyMake_wxObject($1); } | |
395 | %typemap(out) wxPanel* { $result = wxPyMake_wxObject($1); } | |
396 | %typemap(out) wxDialog* { $result = wxPyMake_wxObject($1); } | |
397 | ||
398 | %typemap(out) wxSizer* { $result = wxPyMake_wxSizer($1); } | |
399 | ||
400 | ||
401 | //%typemap(python, out) wxHtmlCell* { $target = wxPyMake_wxObject($source); } | |
402 | //%typemap(python, out) wxHtmlContainerCell* { $target = wxPyMake_wxObject($source); } | |
403 | //%typemap(python, out) wxHtmlParser* { $target = wxPyMake_wxObject($source); } | |
404 | //%typemap(python, out) wxHtmlWinParser* { $target = wxPyMake_wxObject($source); } | |
405 | ||
406 | //--------------------------------------------------------------------------- | |
407 | //--------------------------------------------------------------------------- | |
408 | ||
409 | ||
410 | ||
411 | ||
412 | ||
413 |