]> git.saurik.com Git - wxWidgets.git/blob - wxPython/src/my_typemaps.i
embedded sample's README updates
[wxWidgets.git] / wxPython / src / my_typemaps.i
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 }
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
117
118 //---------------------------------------------------------------------------
119 // Typemaps to convert Python sequence objects (tuples, etc.) to
120 // wxSize, wxPoint, wxRealPoint, and wxRect.
121
122
123 %typemap(in) wxSize& (wxSize temp) {
124 $1 = &temp;
125 if ( ! wxSize_helper($input, &$1)) SWIG_fail;
126 }
127 %typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxSize& {
128 $1 = wxPySimple_typecheck($input, wxT("wxSize"), 2);
129 }
130
131
132 %typemap(in) wxPoint& (wxPoint temp) {
133 $1 = &temp;
134 if ( ! wxPoint_helper($input, &$1)) SWIG_fail;
135 }
136 %typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxPoint& {
137 $1 = wxPySimple_typecheck($input, wxT("wxPoint"), 2);
138 }
139
140
141 %typemap(in) wxRealPoint& (wxRealPoint temp) {
142 $1 = &temp;
143 if ( ! wxRealPoint_helper($input, &$1)) SWIG_fail;
144 }
145 %typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxRealPoint& {
146 $1 = wxPySimple_typecheck($input, wxT("wxRealPoint"), 2);
147 }
148
149
150 %typemap(in) wxRect& (wxRect temp) {
151 $1 = &temp;
152 if ( ! wxRect_helper($input, &$1)) SWIG_fail;
153 }
154 %typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxRect& {
155 $1 = wxPySimple_typecheck($input, wxT("wxRect"), 4);
156 }
157
158
159 %typemap(in) wxPoint2D& (wxPoint2D temp) {
160 $1 = &temp;
161 if ( ! wxPoint2D_helper($input, &$1)) SWIG_fail;
162 }
163 %typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxPoint2D& {
164 $1 = wxPySimple_typecheck($input, wxT("wxPoint2D"), 2);
165 }
166
167
168 //---------------------------------------------------------------------------
169 // Typemap to convert strings to wxColour. Two string formats are accepted,
170 // either a colour name, or a hex colour spec like "#RRGGBB"
171
172 %typemap(in) wxColour& (wxColour temp) {
173 $1 = &temp;
174 if ( ! wxColour_helper($input, &$1)) SWIG_fail;
175 }
176 %typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxColour& {
177 $1 = wxColour_typecheck($input);
178 }
179
180
181
182 //---------------------------------------------------------------------------
183 // Typemap for wxArrayString from Python sequence objects
184
185 %typemap(in) wxArrayString& (bool temp=False) {
186 if (! PySequence_Check($input)) {
187 PyErr_SetString(PyExc_TypeError, "Sequence of strings expected.");
188 SWIG_fail;
189 }
190 $1 = new wxArrayString;
191 temp = True;
192 int i, len=PySequence_Length($input);
193 for (i=0; i<len; i++) {
194 PyObject* item = PySequence_GetItem($input, i);
195 %#if wxUSE_UNICODE
196 PyObject* str = PyObject_Unicode(item);
197 %#else
198 PyObject* str = PyObject_Str(item);
199 %#endif
200 $1->Add(Py2wxString(str));
201 Py_DECREF(item);
202 Py_DECREF(str);
203 }
204 }
205
206 %typemap(freearg) wxArrayString& {
207 if (temp$argnum) delete $1;
208 }
209
210 //---------------------------------------------------------------------------
211 // Typemap for wxArrayInt from Python sequence objects
212
213 %typemap(in) wxArrayInt& (bool temp=False) {
214 if (! PySequence_Check($input)) {
215 PyErr_SetString(PyExc_TypeError, "Sequence of integers expected.");
216 SWIG_fail;
217 }
218 $1 = new wxArrayInt;
219 temp = True;
220 int i, len=PySequence_Length($input);
221 for (i=0; i<len; i++) {
222 PyObject* item = PySequence_GetItem($input, i);
223 PyObject* number = PyNumber_Int(item);
224 $1->Add(PyInt_AS_LONG(number));
225 Py_DECREF(item);
226 Py_DECREF(number);
227 }
228 }
229
230 %typemap(freearg) wxArrayInt& {
231 if (temp$argnum) delete $1;
232 }
233
234
235 //---------------------------------------------------------------------------
236 // Typemaps to convert an array of ints to a list for return values
237 %typemap(out) wxArrayInt& {
238 $result = PyList_New(0);
239 size_t idx;
240 for (idx = 0; idx < $1->GetCount(); idx += 1) {
241 PyObject* val = PyInt_FromLong( $1->Item(idx) );
242 PyList_Append($result, val);
243 Py_DECREF(val);
244 }
245 }
246
247 %typemap(out) wxArrayInt {
248 $result = PyList_New(0);
249 size_t idx;
250 for (idx = 0; idx < $1.GetCount(); idx += 1) {
251 PyObject* val = PyInt_FromLong( $1.Item(idx) );
252 PyList_Append($result, val);
253 Py_DECREF(val);
254 }
255 }
256
257
258
259 // Typemaps to convert an array of ints to a list for return values
260 %typemap(out) wxArrayString& {
261 $result = wxArrayString2PyList_helper(*$1);
262 }
263
264 %typemap(out) wxArrayString {
265 $result = wxArrayString2PyList_helper($1);
266 }
267
268
269 //---------------------------------------------------------------------------
270
271 %typemap(out) bool {
272 $result = $1 ? Py_True : Py_False; Py_INCREF($result);
273 }
274
275
276 // These fragments are iserted in modules that need to convert PyObjects to
277 // integer values, my versions allow any numeric type to be used, as long as
278 // it can be converted to a PyInt. (Specifically, I allow floats where the
279 // default SWIG_AsLong would raise an obsucre exception from within
280 // PyLong_AsLong.)
281
282 %fragment("SWIG_AsLong","header") %{
283 SWIGSTATICINLINE(long)
284 SWIG_AsLong(PyObject * obj)
285 {
286 if (PyNumber_Check(obj))
287 return PyInt_AsLong(obj);
288 else {
289 PyObject* errmsg = PyString_FromFormat("Expected number, got %s",
290 obj->ob_type->tp_name);
291 PyErr_SetObject(PyExc_TypeError, errmsg);
292 Py_DECREF(errmsg);
293 return 0;
294 }
295 }
296 %}
297
298 %fragment("SWIG_AsUnsignedLong","header", fragment="SWIG_AsLong") %{
299 SWIGSTATICINLINE(unsigned long)
300 SWIG_AsUnsignedLong(PyObject * obj)
301 {
302 if (PyLong_Check(obj)) {
303 return PyLong_AsUnsignedLong(obj);
304 } else {
305 long i = SWIG_AsLong(obj);
306 if ( !PyErr_Occurred() && (i < 0)) {
307 PyErr_SetString(PyExc_TypeError, "negative value received for unsigned type");
308 }
309 return i;
310 }
311 }
312 %}
313
314
315 %fragment("SWIG_AsDouble","header") %{
316 SWIGSTATICINLINE(double)
317 SWIG_AsDouble(PyObject *obj)
318 {
319 if (PyNumber_Check(obj))
320 return PyFloat_AsDouble(obj);
321 else {
322 PyObject* errmsg = PyString_FromFormat("Expected number, got %s",
323 obj->ob_type->tp_name);
324 PyErr_SetObject(PyExc_TypeError, errmsg);
325 Py_DECREF(errmsg);
326 return 0;
327 }
328 }
329 %}
330
331
332 //---------------------------------------------------------------------------
333 // Typemap for when GDI objects are returned by reference. This will cause a
334 // copy to be made instead of returning a reference to the same instance. The
335 // GDI object's internal refcounting scheme will do a copy-on-write of the
336 // internal data as needed.
337
338 // These too?
339 //, wxRegion&, wxPalette&
340
341 %typemap(out) wxBrush&, wxPen&, wxFont&, wxBitmap&, wxIcon&, wxCursor& {
342 $*1_ltype* resultptr = new $*1_ltype(*$1);
343 $result = SWIG_NewPointerObj((void*)(resultptr), $1_descriptor, 1);
344 }
345
346
347 //---------------------------------------------------------------------------
348 // Typemaps to convert return values that are base class pointers
349 // to the real derived type, if possible. See wxPyMake_wxObject in
350 // helpers.cpp
351
352 %typemap(out) wxEvtHandler* { $result = wxPyMake_wxObject($1); }
353 %typemap(out) wxMenu* { $result = wxPyMake_wxObject($1); }
354 %typemap(out) wxValidator* { $result = wxPyMake_wxObject($1); }
355
356 %typemap(out) wxApp* { $result = wxPyMake_wxObject($1); }
357 %typemap(out) wxPyApp* { $result = wxPyMake_wxObject($1); }
358 %typemap(out) wxDC* { $result = wxPyMake_wxObject($1); }
359 %typemap(out) wxFSFile* { $result = wxPyMake_wxObject($1); }
360 %typemap(out) wxFileSystem* { $result = wxPyMake_wxObject($1); }
361 %typemap(out) wxGridTableBase* { $result = wxPyMake_wxObject($1); }
362 %typemap(out) wxImageList* { $result = wxPyMake_wxObject($1); }
363 %typemap(out) wxListItem* { $result = wxPyMake_wxObject($1); }
364 %typemap(out) wxMenuItem* { $result = wxPyMake_wxObject($1); }
365 %typemap(out) wxMouseEvent* { $result = wxPyMake_wxObject($1); }
366 %typemap(out) wxObject* { $result = wxPyMake_wxObject($1); }
367 %typemap(out) wxPyPrintout* { $result = wxPyMake_wxObject($1); }
368 %typemap(out) wxToolBarToolBase* { $result = wxPyMake_wxObject($1); }
369 %typemap(out) wxToolTip* { $result = wxPyMake_wxObject($1); }
370
371
372 %typemap(out) wxBitmapButton* { $result = wxPyMake_wxObject($1); }
373 %typemap(out) wxButton* { $result = wxPyMake_wxObject($1); }
374 %typemap(out) wxControl* { $result = wxPyMake_wxObject($1); }
375 %typemap(out) wxFrame* { $result = wxPyMake_wxObject($1); }
376 %typemap(out) wxGrid* { $result = wxPyMake_wxObject($1); }
377 %typemap(out) wxListCtrl* { $result = wxPyMake_wxObject($1); }
378 %typemap(out) wxMDIChildFrame* { $result = wxPyMake_wxObject($1); }
379 %typemap(out) wxMDIClientWindow* { $result = wxPyMake_wxObject($1); }
380 %typemap(out) wxMenuBar* { $result = wxPyMake_wxObject($1); }
381 %typemap(out) wxNotebook* { $result = wxPyMake_wxObject($1); }
382 %typemap(out) wxStaticBox* { $result = wxPyMake_wxObject($1); }
383 %typemap(out) wxStatusBar* { $result = wxPyMake_wxObject($1); }
384 %typemap(out) wxTextCtrl* { $result = wxPyMake_wxObject($1); }
385 %typemap(out) wxToolBar* { $result = wxPyMake_wxObject($1); }
386 %typemap(out) wxToolBarBase* { $result = wxPyMake_wxObject($1); }
387 %typemap(out) wxTreeCtrl* { $result = wxPyMake_wxObject($1); }
388 %typemap(out) wxPyTreeCtrl* { $result = wxPyMake_wxObject($1); }
389 %typemap(out) wxWindow* { $result = wxPyMake_wxObject($1); }
390 %typemap(out) wxHtmlWindow* { $result = wxPyMake_wxObject($1); }
391 %typemap(out) wxPyHtmlWindow* { $result = wxPyMake_wxObject($1); }
392 %typemap(out) wxWizardPage* { $result = wxPyMake_wxObject($1); }
393
394 %typemap(out) wxSizer* { $result = wxPyMake_wxSizer($1); }
395
396
397 //%typemap(python, out) wxHtmlCell* { $target = wxPyMake_wxObject($source); }
398 //%typemap(python, out) wxHtmlContainerCell* { $target = wxPyMake_wxObject($source); }
399 //%typemap(python, out) wxHtmlParser* { $target = wxPyMake_wxObject($source); }
400 //%typemap(python, out) wxHtmlWinParser* { $target = wxPyMake_wxObject($source); }
401
402 //---------------------------------------------------------------------------
403 //---------------------------------------------------------------------------
404
405
406
407
408
409