]> git.saurik.com Git - wxWidgets.git/blobdiff - wxPython/src/my_typemaps.i
test wxString's char<->wchar_t ctors
[wxWidgets.git] / wxPython / src / my_typemaps.i
index 349cc9245d0b4ccc51c1fb8ce4422a50ed8fad12..f1bfc3cd7d5f12debab316ece71830acee2fd957 100644 (file)
 /////////////////////////////////////////////////////////////////////////////
 
 
-//---------------------------------------------------------------------------
-// Tell SWIG to wrap all the wrappers with Python's thread macros
+//----------------------------------------------------------------------
+// Typemaps to convert a list of items to an int (size) and an array
 
-%except(python) {
-    wxPy_BEGIN_ALLOW_THREADS;
-    $function
-    wxPy_END_ALLOW_THREADS;
+%typemap(in) (int choices, wxString* choices_array ) {
+    $1 = PyList_Size($input);
+    $2 = wxString_LIST_helper($input);
+    if ($2 == NULL) SWIG_fail;
+}
+%typemap(freearg) (int choices, wxString* choices_array ) {
+    if ($2) delete [] $2;
 }
 
-//----------------------------------------------------------------------
-// LCOUNT and choices go together.  They allow a single Python list to be
-// converted to an integer count and an array count items long.
 
-%typemap(python,build) int LCOUNT {
-    if (_in_choices) {
-        $target = PyList_Size(_in_choices);
-    }
-    else {
-        $target = 0;
-    }
+//---------------------------------------------------------------------------
+// wxString typemaps
+
+
+%typemap(in) wxString& (bool temp=False) {
+    $1 = wxString_in_helper($input);
+    if ($1 == NULL) SWIG_fail;
+    temp = True;
+}
+%typemap(freearg) wxString& {
+    if (temp$argnum)
+        delete $1;
 }
 
+%typemap(out) wxString& {
+%#if wxUSE_UNICODE
+    $result = PyUnicode_FromWideChar($1->c_str(), $1->Len());
+%#else
+    $result = PyString_FromStringAndSize($1->c_str(), $1->Len());
+%#endif
+}
 
 
-%typemap(python,in) byte* choices  {
-    $target = byte_LIST_helper($source);
-    if ($target == NULL) {
-        return NULL;
-    }
+%apply wxString& { wxString* }
+
+
+
+%typemap(out) wxString {
+%#if wxUSE_UNICODE
+    $result = PyUnicode_FromWideChar($1.c_str(), $1.Len());
+%#else
+    $result = PyString_FromStringAndSize($1.c_str(), $1.Len());
+%#endif
 }
-%typemap(python,freearg) byte* choices {
-    delete [] $source;
+
+%typemap(varout) wxString {
+%#if wxUSE_UNICODE
+    $result = PyUnicode_FromWideChar($1.c_str(), $1.Len());
+%#else
+    $result = PyString_FromStringAndSize($1.c_str(), $1.Len());
+%#endif
 }
 
-// wxDash is a signed char
-%typemap(python,in) wxDash* choices  {
-    $target = (wxDash*)byte_LIST_helper($source);
-    if ($target == NULL) {
-        return NULL;
-    }
+
+%typemap(in) wxString {
+    wxString* sptr = wxString_in_helper($input);
+    if (sptr == NULL) SWIG_fail;
+    $1 = *sptr;
+    delete sptr;
 }
-%typemap(python,freearg) wxDash* choices {
-    delete [] $source;
+
+%typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxString& {
+    $1 = PyString_Check($input) || PyUnicode_Check($input);
 }
 
+//---------------------------------------------------------------------------
+// wxMemoryBuffer  (needed for wxSTC)
 
-%typemap(python,in) int* choices  {
-    $target = int_LIST_helper($source);
-    if ($target == NULL) {
-        return NULL;
+%typemap(in) wxMemoryBuffer& (bool temp=False) {
+    if (!PyString_Check($input)) {
+        PyErr_SetString(PyExc_TypeError, "String buffer expected");
+        SWIG_fail;
     }
+    char* str = PyString_AS_STRING($input);
+    int   len = PyString_GET_SIZE($input);
+    $1 = new wxMemoryBuffer(len);
+    temp = True;
+    memcpy($1->GetData(), str, len);
+    $1->SetDataLen(len);
 }
-%typemap(python,freearg) int* choices {
-    delete [] $source;
+
+%typemap(freearg) wxMemoryBuffer& {
+    if (temp$argnum) delete $1;
 }
 
+%typemap(out) wxMemoryBuffer {
+    $result = PyString_FromStringAndSize((char*)$1.GetData(), $1.GetDataLen());
+}
 
-%typemap(python,in) long* choices {
-    $target = long_LIST_helper($source);
-    if ($target == NULL) {
-        return NULL;
-    }
+
+
+//---------------------------------------------------------------------------
+// Typemaps to convert Python sequence objects (tuples, etc.) to
+// wxSize, wxPoint, wxRealPoint, and wxRect.
+
+
+%typemap(in) wxSize& (wxSize temp) {
+    $1 = &temp;
+    if ( ! wxSize_helper($input, &$1)) SWIG_fail;
 }
-%typemap(python,freearg) long* choices {
-    delete [] $source;
+%typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxSize& {
+    $1 = wxPySimple_typecheck($input, wxT("wxSize"), 2);
 }
 
 
-%typemap(python,in) unsigned long* choices {
-    $target = (unsigned long*)long_LIST_helper($source);
-    if ($target == NULL) {
-        return NULL;
-    }
+%typemap(in) wxPoint& (wxPoint temp) {
+    $1 = &temp;
+    if ( ! wxPoint_helper($input, &$1)) SWIG_fail;
 }
-%typemap(python,freearg) unsigned long* choices {
-    delete [] $source;
+%typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxPoint& {
+    $1 = wxPySimple_typecheck($input, wxT("wxPoint"), 2);
 }
 
 
-%typemap(python,in) char** choices {
-    $target = string_LIST_helper($source);
-    if ($target == NULL) {
-        return NULL;
-    }
+%typemap(in) wxRealPoint& (wxRealPoint temp) {
+    $1 = &temp;
+    if ( ! wxRealPoint_helper($input, &$1)) SWIG_fail;
 }
-%typemap(python,freearg) char** choices {
-    delete [] $source;
+%typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxRealPoint& {
+    $1 = wxPySimple_typecheck($input, wxT("wxRealPoint"), 2);
 }
 
 
-%typemap(python,in) wxBitmap** choices  {
-    $target = wxBitmap_LIST_helper($source);
-    if ($target == NULL) {
-        return NULL;
-    }
+%typemap(in) wxRect& (wxRect temp) {
+    $1 = &temp;
+    if ( ! wxRect_helper($input, &$1)) SWIG_fail;
 }
-%typemap(python,freearg) wxBitmap** choices {
-    delete [] $source;
+%typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxRect& {
+    $1 = wxPySimple_typecheck($input, wxT("wxRect"), 4);
 }
 
-%typemap(python,in) wxString* choices  {
-    $target = wxString_LIST_helper($source);
-    if ($target == NULL) {
-        return NULL;
-    }
+
+%typemap(in) wxPoint2D& (wxPoint2D temp) {
+    $1 = &temp;
+    if ( ! wxPoint2D_helper($input, &$1)) SWIG_fail;
 }
-%typemap(python,freearg) wxString* choices {
-    delete [] $source;
+%typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxPoint2D& {
+    $1 = wxPySimple_typecheck($input, wxT("wxPoint2D"), 2);
 }
 
-%typemap(python,in) wxAcceleratorEntry* choices  {
-    $target = wxAcceleratorEntry_LIST_helper($source);
-    if ($target == NULL) {
-        return NULL;
-    }
+
+//---------------------------------------------------------------------------
+// Typemap to convert strings to wxColour.  Two string formats are accepted,
+// either a colour name, or a hex colour spec like "#RRGGBB"
+
+%typemap(in) wxColour& (wxColour temp) {
+    $1 = &temp;
+    if ( ! wxColour_helper($input, &$1)) SWIG_fail;
 }
-%typemap(python,freearg) wxAcceleratorEntry* choices {
-    delete [] $source;
+%typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxColour& {
+    $1 = wxColour_typecheck($input);
 }
 
 
 
+//---------------------------------------------------------------------------
+// Typemap for wxArrayString from Python sequence objects
 
-
-%typemap(python,build) int PCOUNT {
-    if (_in_points) {
-        $target = PyList_Size(_in_points);
+%typemap(in) wxArrayString& (bool temp=False) {
+    if (! PySequence_Check($input)) {
+        PyErr_SetString(PyExc_TypeError, "Sequence of strings expected.");
+        SWIG_fail;
     }
-    else {
-        $target = 0;
+    $1 = new wxArrayString;
+    temp = True;
+    int i, len=PySequence_Length($input);
+    for (i=0; i<len; i++) {
+        PyObject* item = PySequence_GetItem($input, i);
+%#if wxUSE_UNICODE
+        PyObject* str  = PyObject_Unicode(item);
+%#else
+        PyObject* str  = PyObject_Str(item);
+%#endif
+        if (PyErr_Occurred())  SWIG_fail;
+        $1->Add(Py2wxString(str));
+        Py_DECREF(item);
+        Py_DECREF(str);
     }
 }
 
-%typemap(python,in) wxPoint* points  {
-    $target = wxPoint_LIST_helper($source);
-    if ($target == NULL) {
-        return NULL;
-    }
-}
-%typemap(python,freearg) wxPoint* points {
-    delete [] $source;
+%typemap(freearg) wxArrayString& {
+    if (temp$argnum) delete $1;
 }
 
-
-
 //---------------------------------------------------------------------------
+// Typemap for wxArrayInt from Python sequence objects
 
-%{
-static char* wxStringErrorMsg = "string type is required for parameter";
-%}
-
-// TODO:  Which works best???
-
-// Implementation #1
-//  %typemap(python, in) wxString& (PyObject* temp, int tmpDoDecRef) {
-//      temp = $source;
-//      tmpDoDecRef = 0;
-//  #if PYTHON_API_VERSION >= 1009
-//      if (PyUnicode_Check(temp) {
-//          temp = PyUnicode_AsUTF8String(temp);
-//          if (! temp) {
-//              PyErr_SetString(PyExc_TypeError, "Unicode encoding to UTF8 failed.");
-//              return NULL;
-//          }
-//          tmpDoDecRef = 1;
-//  #endif
-//      if (!PyString_Check(temp)) {
-//          PyErr_SetString(PyExc_TypeError, wxStringErrorMsg);
-//          return NULL;
-//      }
-//      $target = new wxString(PyString_AsString(temp), PyString_Size(temp));
-//  #if PYTHON_API_VERESION >= 1009
-//      if (tmpDoDecRef) Py_DECREF(temp);
-//  #endif
-//  }
-
-
-// Implementation #2
-%typemap(python, in) wxString& {
-#if PYTHON_API_VERSION >= 1009
-    char* tmpPtr; int tmpSize;
-    if (!PyString_Check($source) && !PyUnicode_Check($source)) {
-        PyErr_SetString(PyExc_TypeError, "String or Unicode type required");
-        return NULL;
+%typemap(in) wxArrayInt& (bool temp=False) {
+    if (! PySequence_Check($input)) {
+        PyErr_SetString(PyExc_TypeError, "Sequence of integers expected.");
+        SWIG_fail;
     }
-    if (PyString_AsStringAndSize($source, &tmpPtr, &tmpSize) == -1)
-        return NULL;
-    $target = new wxString(tmpPtr, tmpSize);
-#else
-    if (!PyString_Check($source)) {
-        PyErr_SetString(PyExc_TypeError, wxStringErrorMsg);
-        return NULL;
+    $1 = new wxArrayInt;
+    temp = True;
+    int i, len=PySequence_Length($input);
+    for (i=0; i<len; i++) {
+        PyObject* item = PySequence_GetItem($input, i);
+        PyObject* number  = PyNumber_Int(item);
+        $1->Add(PyInt_AS_LONG(number));
+        Py_DECREF(item);
+        Py_DECREF(number);
     }
-    $target = new wxString(PyString_AS_STRING($source), PyString_GET_SIZE($source));
-#endif
 }
 
+%typemap(freearg) wxArrayInt& {
+    if (temp$argnum) delete $1;
+}
 
 
+//---------------------------------------------------------------------------
+// Typemaps to convert an array of ints to a list for return values
+%typemap(out) wxArrayInt& {
+    $result = PyList_New(0);
+    size_t idx;
+    for (idx = 0; idx < $1->GetCount(); idx += 1) {
+        PyObject* val = PyInt_FromLong( $1->Item(idx) );
+        PyList_Append($result, val);
+        Py_DECREF(val);
+    }
+}
 
-%typemap(python, freearg) wxString& {
-    if ($target)
-        delete $source;
+%typemap(out) wxArrayInt {
+    $result = PyList_New(0);
+    size_t idx;
+    for (idx = 0; idx < $1.GetCount(); idx += 1) {
+        PyObject* val = PyInt_FromLong( $1.Item(idx) );
+        PyList_Append($result, val);
+        Py_DECREF(val);
+    }
 }
 
 
 
-%typemap(python, out) wxString {
-    $target = PyString_FromStringAndSize($source->c_str(), $source->Len());
-}
-%typemap(python, ret) wxString {
-    delete $source;
+// Typemaps to convert an array of ints to a list for return values
+%typemap(out) wxArrayString& {
+    $result = wxArrayString2PyList_helper(*$1);
 }
 
-
-%typemap(python, out) wxString* {
-    $target = PyString_FromStringAndSize($source->c_str(), $source->Len());
+%typemap(out) wxArrayString {
+    $result = wxArrayString2PyList_helper($1);
 }
 
 
-
-
-
 //---------------------------------------------------------------------------
-// Typemaps to convert Python sequence objects (2-tuples, etc.) to
-// wxSize, wxPoint, wxRealPoint, and wxRect.
 
-%typemap(python,in) wxSize& (wxSize temp) {
-    $target = &temp;
-    if (! wxSize_helper($source, &$target))
-        return NULL;
+%typemap(out) bool {
+    $result = $1 ? Py_True : Py_False; Py_INCREF($result);
 }
 
-%typemap(python,in) wxPoint& (wxPoint temp) {
-    $target = &temp;
-    if (! wxPoint_helper($source, &$target))
-        return NULL;
-}
 
-%typemap(python,in) wxRealPoint& (wxRealPoint temp) {
-    $target = &temp;
-    if (! wxRealPoint_helper($source, &$target))
-        return NULL;
+// These fragments are iserted in modules that need to convert PyObjects to
+// integer values, my versions allow any numeric type to be used, as long as
+// it can be converted to a PyInt.  (Specifically, I allow floats where the
+// default SWIG_AsLong would raise an obsucre exception from within
+// PyLong_AsLong.)
+
+%fragment("SWIG_AsLong","header") %{
+SWIGSTATICINLINE(long)
+SWIG_AsLong(PyObject * obj)
+{
+    if (PyNumber_Check(obj))
+        return PyInt_AsLong(obj);
+    else {
+        PyObject* errmsg = PyString_FromFormat("Expected number, got %s",
+                                               obj->ob_type->tp_name);
+        PyErr_SetObject(PyExc_TypeError, errmsg);
+        Py_DECREF(errmsg);
+        return 0;
+    }
 }
+%}
 
-%typemap(python,in) wxRect& (wxRect temp) {
-    $target = &temp;
-    if (! wxRect_helper($source, &$target))
-        return NULL;
+%fragment("SWIG_AsUnsignedLong","header", fragment="SWIG_AsLong") %{
+SWIGSTATICINLINE(unsigned long)
+SWIG_AsUnsignedLong(PyObject * obj) 
+{
+  if (PyLong_Check(obj)) {
+    return PyLong_AsUnsignedLong(obj);
+  } else {
+    long i = SWIG_AsLong(obj);
+    if ( !PyErr_Occurred() && (i < 0)) {
+      PyErr_SetString(PyExc_TypeError, "negative value received for unsigned type");
+    }
+    return i;
+  }
 }
+%}
 
-//---------------------------------------------------------------------------
-// Typemap to convert strings to wxColour.  Two string formats are accepted,
-// either a colour name, or a hex colour spec like "#RRGGBB"
 
-%typemap(python,in) wxColour& (wxColour temp) {
-    $target = &temp;
-    if (! wxColour_helper($source, &$target))
-        return NULL;
+%fragment("SWIG_AsDouble","header") %{
+SWIGSTATICINLINE(double)
+SWIG_AsDouble(PyObject *obj)
+{
+    if (PyNumber_Check(obj))
+        return PyFloat_AsDouble(obj);
+    else {
+        PyObject* errmsg = PyString_FromFormat("Expected number, got %s",
+                                               obj->ob_type->tp_name);
+        PyErr_SetObject(PyExc_TypeError, errmsg);
+        Py_DECREF(errmsg);
+        return 0;
+    }
 }
+%}
 
 
 //---------------------------------------------------------------------------
-// Map T_OUTPUTs for floats to return ints.
+// Typemap for when GDI objects are returned by reference.  This will cause a
+// copy to be made instead of returning a reference to the same instance.  The
+// GDI object's internal refcounting scheme will do a copy-on-write of the
+// internal data as needed.
 
+// These too?
+//, wxRegion&, wxPalette&
 
-%typemap(python,ignore) float          *T_OUTPUT_TOINT(float temp),
-                        double         *T_OUTPUT_TOINT(double temp)
-{
-  $target = &temp;
+%typemap(out) wxBrush&, wxPen&, wxFont&, wxBitmap&, wxIcon&, wxCursor& {
+    $*1_ltype* resultptr = new $*1_ltype(*$1);
+    $result = SWIG_NewPointerObj((void*)(resultptr), $1_descriptor, 1);
 }
 
 
-%typemap(python,argout) float          *T_OUTPUT_TOINT,
-                        double         *T_OUTPUT_TOINT
-{
-    PyObject *o;
-    o = PyInt_FromLong((long) (*$source));
-    $target = t_output_helper($target, o);
-}
+//---------------------------------------------------------------------------
+// Typemaps to convert return values that are base class pointers
+// to the real derived type, if possible.  See wxPyMake_wxObject in
+// helpers.cpp
+
+%typemap(out) wxEvtHandler*             { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxMenu*                   { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxValidator*              { $result = wxPyMake_wxObject($1); }
+
+%typemap(out) wxApp*                    { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxPyApp*                  { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxDC*                     { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxFSFile*                 { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxFileSystem*             { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxGridTableBase*          { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxImageList*              { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxListItem*               { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxMenuItem*               { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxMouseEvent*             { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxObject*                 { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxPyPrintout*             { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxToolBarToolBase*        { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxToolTip*                { $result = wxPyMake_wxObject($1); }
+
+
+%typemap(out) wxBitmapButton*           { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxButton*                 { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxControl*                { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxFrame*                  { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxGrid*                   { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxListCtrl*               { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxMDIChildFrame*          { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxMDIClientWindow*        { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxMenuBar*                { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxNotebook*               { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxStaticBox*              { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxStatusBar*              { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxTextCtrl*               { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxToolBar*                { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxToolBarBase*            { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxTreeCtrl*               { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxPyTreeCtrl*             { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxWindow*                 { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxHtmlWindow*             { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxPyHtmlWindow*           { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxWizardPage*             { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxPanel*                  { $result = wxPyMake_wxObject($1); }
+%typemap(out) wxDialog*                 { $result = wxPyMake_wxObject($1); }
+
+%typemap(out) wxSizer*                  { $result = wxPyMake_wxSizer($1); }
+
+
+//%typemap(python, out) wxHtmlCell*               { $target = wxPyMake_wxObject($source); }
+//%typemap(python, out) wxHtmlContainerCell*      { $target = wxPyMake_wxObject($source); }
+//%typemap(python, out) wxHtmlParser*             { $target = wxPyMake_wxObject($source); }
+//%typemap(python, out) wxHtmlWinParser*          { $target = wxPyMake_wxObject($source); }
 
 //---------------------------------------------------------------------------
 //---------------------------------------------------------------------------
 
+
+
+
+
+