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