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