Files
Phoenix/src/string.sip
Robin Dunn 3ce4dd7927 Rename wxpy_utils to wxpy_api
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxPython/Phoenix/trunk@70966 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
2012-03-22 04:14:55 +00:00

87 lines
2.8 KiB
Plaintext

//--------------------------------------------------------------------------
// Name: string.sip
// Purpose: Implements a %MappedType for wxString
//
// Author: Robin Dunn
//
// Created: 9-Nov-2010
// Copyright: (c) 2011 by Total Control Software
// Licence: wxWindows license
//--------------------------------------------------------------------------
// We don't want the Python user to ever need to deal directly with wxString
// at all, so it will be mapped to and from Python Unicode objects using the
// code snippets below.
// NOTE: Currently we assume that string objects are encoded in utf-8.
%MappedType wxString
{
// TODO: Will something like this work (and be a good idea) in order to
// share the conversion code here and in the utlities module?
//%ConvertToTypeCode
// // Code to test a PyObject for compatibility with wxString
// if (!sipIsErr) {
// if (PyString_Check(sipPy) || PyUnicode_Check(sipPy))
// return TRUE;
// return FALSE;
// }
//
// // Convert a compatible PyObject to a wxString
// wxString str = Py2wxString(sipPy, true, *sipIsErr);
// if (*sipIsErr)
// return 0;
// *sipCppPtr = new wxString(str);
// return sipGetState(sipTransferObj);
//%End
%ConvertToTypeCode
// Code to test a PyObject for compatibility with wxString
if (!sipIsErr) {
if (PyString_Check(sipPy) || PyUnicode_Check(sipPy))
return TRUE;
return FALSE;
}
// Code to convert a compatible PyObject to a wxString
PyObject* uni = sipPy;
if (PyString_Check(sipPy)) {
// if it's a string object convert it to unicode first, assuming utf-8
uni = PyUnicode_FromEncodedObject(sipPy, "utf-8", "strict");
if (PyErr_Occurred()) {
*sipIsErr = 1;
return 0;
}
}
*sipCppPtr = new wxString();
size_t len = PyUnicode_GET_SIZE(uni);
if (len) {
PyUnicode_AsWideChar((PyUnicodeObject*)uni,
wxStringBuffer(**sipCppPtr, len), len);
}
if (PyString_Check(sipPy))
Py_DECREF(uni); // release the temporary Unicode object we created
return sipGetState(sipTransferObj);
%End
%ConvertFromTypeCode
// Convert a wxString to a Python Unicode object. See wxpy_api.sip
return wx2PyString(*sipCpp);
%End
};
// Used just for testing the MappedType code, it can be removed later
%ModuleCode
wxString _testStringTypemap(const wxString& str)
{
wxString local = str;
return local;
}
%End
wxString _testStringTypemap(const wxString& str);