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