1 /////////////////////////////////////////////////////////////////////////////
 
   3 // Purpose:     Special typemaps specifically for wxPython.
 
   9 // Copyright:   (c) 1998 by Total Control Software
 
  10 // Licence:     wxWindows license
 
  11 /////////////////////////////////////////////////////////////////////////////
 
  14 //----------------------------------------------------------------------
 
  15 // Typemaps to convert a list of items to an int (size) and an array
 
  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;
 
  24     %typemap(freearg) (int NAME, int* ARR_NAME) {
 
  29 MAKE_INT_ARRAY_TYPEMAPS(widths, widths_field)
 
  30 MAKE_INT_ARRAY_TYPEMAPS(styles, styles_field)
 
  34 // Same things for a wxString
 
  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;
 
  40 %typemap(freearg) (int choices, wxString* choices_array ) {
 
  46 //---------------------------------------------------------------------------
 
  50 %typemap(in) wxString& (bool temp=false) {
 
  51     $1 = wxString_in_helper($input);
 
  52     if ($1 == NULL) SWIG_fail;
 
  55 %typemap(freearg) wxString& {
 
  60 %typemap(out) wxString& {
 
  62     $result = PyUnicode_FromWideChar($1->c_str(), $1->Len());
 
  64     $result = PyString_FromStringAndSize($1->c_str(), $1->Len());
 
  69 %apply wxString& { wxString* }
 
  73 %typemap(out) wxString {
 
  75     $result = PyUnicode_FromWideChar($1.c_str(), $1.Len());
 
  77     $result = PyString_FromStringAndSize($1.c_str(), $1.Len());
 
  81 %typemap(varout) wxString {
 
  83     $result = PyUnicode_FromWideChar($1.c_str(), $1.Len());
 
  85     $result = PyString_FromStringAndSize($1.c_str(), $1.Len());
 
  90 %typemap(in) wxString {
 
  91     wxString* sptr = wxString_in_helper($input);
 
  92     if (sptr == NULL) SWIG_fail;
 
  97 %typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxString& {
 
  98     $1 = PyString_Check($input) || PyUnicode_Check($input);
 
 101 //---------------------------------------------------------------------------
 
 102 // wxMemoryBuffer  (needed for wxSTC)
 
 104 %typemap(in) wxMemoryBuffer& (bool temp=false) {
 
 105     if (!PyString_Check($input)) {
 
 106         PyErr_SetString(PyExc_TypeError, "String buffer expected");
 
 109     char* str = PyString_AS_STRING($input);
 
 110     int   len = PyString_GET_SIZE($input);
 
 111     $1 = new wxMemoryBuffer(len);
 
 113     memcpy($1->GetData(), str, len);
 
 117 %typemap(freearg) wxMemoryBuffer& {
 
 118     if (temp$argnum) delete $1;
 
 121 %typemap(out) wxMemoryBuffer {
 
 122     $result = PyString_FromStringAndSize((char*)$1.GetData(), $1.GetDataLen());
 
 127 %typemap(out) wxCharBuffer {
 
 129         $result = PyString_FromString((char*)$1.data());
 
 131         $result = PyString_FromString("");
 
 135 //---------------------------------------------------------------------------
 
 136 // Typemaps to convert Python sequence objects (tuples, etc.) to
 
 137 // wxSize, wxPoint, wxRealPoint, and wxRect.
 
 140 %typemap(in) wxSize& (wxSize temp) {
 
 142     if ( ! wxSize_helper($input, &$1)) SWIG_fail;
 
 144 %typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxSize& {
 
 145     $1 = wxPySimple_typecheck($input, wxT("wxSize"), 2);
 
 149 %typemap(in) wxPoint& (wxPoint temp) {
 
 151     if ( ! wxPoint_helper($input, &$1)) SWIG_fail;
 
 153 %typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxPoint& {
 
 154     $1 = wxPySimple_typecheck($input, wxT("wxPoint"), 2);
 
 158 %typemap(in) wxRealPoint& (wxRealPoint temp) {
 
 160     if ( ! wxRealPoint_helper($input, &$1)) SWIG_fail;
 
 162 %typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxRealPoint& {
 
 163     $1 = wxPySimple_typecheck($input, wxT("wxRealPoint"), 2);
 
 167 %typemap(in) wxRect& (wxRect temp) {
 
 169     if ( ! wxRect_helper($input, &$1)) SWIG_fail;
 
 171 %typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxRect& {
 
 172     $1 = wxPySimple_typecheck($input, wxT("wxRect"), 4);
 
 176 %typemap(in) wxPoint2D& (wxPoint2D temp) {
 
 178     if ( ! wxPoint2D_helper($input, &$1)) SWIG_fail;
 
 180 %typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxPoint2D& {
 
 181     $1 = wxPySimple_typecheck($input, wxT("wxPoint2D"), 2);
 
 185 %typemap(in) wxRect2D& (wxRect2D temp) {
 
 187     if ( ! wxRect2D_helper($input, &$1)) SWIG_fail;
 
 189 %typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxRect2D& {
 
 190     $1 = wxPySimple_typecheck($input, wxT("wxRect2D"), 4);
 
 194 //---------------------------------------------------------------------------
 
 195 // Typemap to convert strings to wxColour.  Two string formats are accepted,
 
 196 // either a colour name, or a hex colour spec like "#RRGGBB"
 
 198 %typemap(in) wxColour& (wxColour temp) {
 
 200     if ( ! wxColour_helper($input, &$1)) SWIG_fail;
 
 202 %typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxColour& {
 
 203     $1 = wxColour_typecheck($input);
 
 206 //---------------------------------------------------------------------------
 
 207 // Typemap for wxArrayString from Python sequence objects
 
 209 %typemap(in) wxArrayString& (bool temp=false) {
 
 210     if (! PySequence_Check($input)) {
 
 211         PyErr_SetString(PyExc_TypeError, "Sequence of strings expected.");
 
 214     $1 = new wxArrayString;
 
 216     int i, len=PySequence_Length($input);
 
 217     for (i=0; i<len; i++) {
 
 218         PyObject* item = PySequence_GetItem($input, i);
 
 219         wxString* s = wxString_in_helper(item);
 
 220         if (PyErr_Occurred())  SWIG_fail;
 
 227 %typemap(freearg) wxArrayString& {
 
 228     if (temp$argnum) delete $1;
 
 231 //---------------------------------------------------------------------------
 
 232 // Typemap for wxArrayInt from Python sequence objects
 
 234 %typemap(in) wxArrayInt& (bool temp=false) {
 
 235     if (! PySequence_Check($input)) {
 
 236         PyErr_SetString(PyExc_TypeError, "Sequence of integers expected.");
 
 241     int i, len=PySequence_Length($input);
 
 242     for (i=0; i<len; i++) {
 
 243         PyObject* item = PySequence_GetItem($input, i);
 
 244         PyObject* number  = PyNumber_Int(item);
 
 245         $1->Add(PyInt_AS_LONG(number));
 
 251 %typemap(freearg) wxArrayInt& {
 
 252     if (temp$argnum) delete $1;
 
 256 //---------------------------------------------------------------------------
 
 257 // Typemaps to convert an array of ints to a list for return values
 
 258 // %typemap(out) wxArrayInt& {
 
 259 //     $result = PyList_New(0);
 
 261 //     for (idx = 0; idx < $1->GetCount(); idx += 1) {
 
 262 //         PyObject* val = PyInt_FromLong( $1->Item(idx) );
 
 263 //         PyList_Append($result, val);
 
 268 // %typemap(out) wxArrayInt {
 
 269 //     $result = PyList_New(0);
 
 271 //     for (idx = 0; idx < $1.GetCount(); idx += 1) {
 
 272 //         PyObject* val = PyInt_FromLong( $1.Item(idx) );
 
 273 //         PyList_Append($result, val);
 
 278 %typemap(out) wxArrayInt& {
 
 279     $result = wxArrayInt2PyList_helper(*$1);
 
 282 %typemap(out) wxArrayInt {
 
 283     $result = wxArrayInt2PyList_helper($1);
 
 287 // convert array of doubles to a Python list
 
 288 %typemap(out) wxArrayDouble& {
 
 289     $result = wxArrayDouble2PyList_helper(*$1);
 
 292 %typemap(out) wxArrayDouble {
 
 293     $result = wxArrayDouble2PyList_helper($1);
 
 298 // Typemaps to convert an array of strings to a list for return values
 
 299 %typemap(out) wxArrayString& {
 
 300     $result = wxArrayString2PyList_helper(*$1);
 
 303 %typemap(out) wxArrayString {
 
 304     $result = wxArrayString2PyList_helper($1);
 
 308 //---------------------------------------------------------------------------
 
 311     $result = $1 ? Py_True : Py_False; Py_INCREF($result);
 
 315 //---------------------------------------------------------------------------
 
 316 // wxFileOffset, can be a 32-bit or a 64-bit integer
 
 318 %typemap(in) wxFileOffset {
 
 319     if (sizeof(wxFileOffset) > sizeof(long))
 
 320         $1 = PyLong_AsLongLong($input);
 
 322         $1 = PyInt_AsLong($input);
 
 325 %typemap(out) wxFileOffset {
 
 326     if (sizeof(wxFileOffset) > sizeof(long))
 
 327         $result = PyLong_FromLongLong($1);
 
 329         $result = PyInt_FromLong($1);
 
 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.
 
 340 //, wxRegion&, wxPalette&
 
 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);
 
 348 //---------------------------------------------------------------------------
 
 349 // Typemaps for loading a image or bitmap from an object that implements the
 
 352 %typemap(in) (buffer data, int DATASIZE) (Py_ssize_t temp)
 
 354     if (PyObject_AsReadBuffer($input, (const void**)(&$1), &temp) == -1) SWIG_fail;
 
 358 %typemap(in) (buffer alpha, int ALPHASIZE) (Py_ssize_t temp)
 
 360     if ($input != Py_None) {
 
 361         if (PyObject_AsReadBuffer($input, (const void**)(&$1), &temp) == -1) SWIG_fail;
 
 368 //---------------------------------------------------------------------------
 
 369 // Typemaps to convert return values that are base class pointers
 
 370 // to the real derived type, if possible.  See wxPyMake_wxObject in
 
 373 // NOTE: For those classes that also call _setOORInfo these typemaps should be
 
 374 // disabled for the constructor.
 
 376 %typemap(out) wxEvtHandler*             { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 377 %typemap(out) wxMenu*                   { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 378 %typemap(out) wxValidator*              { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 380 %typemap(out) wxApp*                    { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 381 %typemap(out) wxPyApp*                  { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 382 %typemap(out) wxDC*                     { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 383 %typemap(out) wxFSFile*                 { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 384 %typemap(out) wxFileSystem*             { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 385 %typemap(out) wxImageList*              { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 386 %typemap(out) wxImage*                  { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 387 %typemap(out) wxListItem*               { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 388 %typemap(out) wxMenuItem*               { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 389 %typemap(out) wxMouseEvent*             { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 390 %typemap(out) wxObject*                 { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 391 %typemap(out) wxPyPrintout*             { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 392 %typemap(out) wxToolBarToolBase*        { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 393 %typemap(out) wxToolTip*                { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 396 %typemap(out) wxBitmapButton*           { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 397 %typemap(out) wxButton*                 { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 398 %typemap(out) wxControl*                { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 399 %typemap(out) wxFrame*                  { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 400 %typemap(out) wxGrid*                   { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 401 %typemap(out) wxPyListCtrl*             { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 402 %typemap(out) wxMDIChildFrame*          { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 403 %typemap(out) wxMDIClientWindow*        { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 404 %typemap(out) wxMenuBar*                { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 405 %typemap(out) wxNotebook*               { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 406 %typemap(out) wxStaticBox*              { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 407 %typemap(out) wxStatusBar*              { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 408 %typemap(out) wxTextCtrl*               { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 409 %typemap(out) wxToolBar*                { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 410 %typemap(out) wxToolBarBase*            { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 411 %typemap(out) wxPyTreeCtrl*             { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 412 %typemap(out) wxWindow*                 { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 413 %typemap(out) wxPyHtmlWindow*           { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 414 %typemap(out) wxWizardPage*             { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 415 %typemap(out) wxPyWizardPage*           { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 416 %typemap(out) wxPanel*                  { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 417 %typemap(out) wxDialog*                 { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 418 %typemap(out) wxScrolledWindow*         { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 420 %typemap(out) wxSizer*                  { $result = wxPyMake_wxObject($1, (bool)$owner); }
 
 423 //---------------------------------------------------------------------------
 
 424 //---------------------------------------------------------------------------