1//--------------------------------------------------------------------------
2// Name:        string.sip
3// Purpose:     Implements a %MappedType for wxString
4//
5// Author:      Robin Dunn
6//
7// Created:     9-Nov-2010
8// Copyright:   (c) 2010-2018 by Total Control Software
9// Licence:     wxWindows license
10//--------------------------------------------------------------------------
11
12
13// We don't want the Python user to ever need to deal directly with wxString
14// at all, so it will be mapped to and from Python Unicode objects using the
15// code snippets below.
16// NOTE: Currently we assume that string objects are encoded in utf-8.
17
18%MappedType wxString
19{
20
21    // TODO: It is still possible to have a wx build that uses utf-8 inside
22    // wxString, so we should probably be checking wxUSE_UNICODE_WCHAR or
23    // wxUSE_UNICODE_UTF8 for this conversion.
24
25    %ConvertToTypeCode
26        #if wxUSE_UNICODE_WCHAR == 0
27        #error wxString conversion can only handle WCHAR wxStrings currently
28        #endif
29
30        // Code to test a PyObject for compatibility with wxString
31        if (!sipIsErr) {
32            if (PyBytes_Check(sipPy) || PyUnicode_Check(sipPy))
33                return TRUE;
34            return FALSE;
35        }
36
37        // Code to convert a compatible PyObject to a wxString
38        PyObject* uni = sipPy;
39        if (PyBytes_Check(sipPy)) {
40            // if it's a string object convert it to unicode first, assuming utf-8
41            uni = PyUnicode_FromEncodedObject(sipPy, "utf-8", "strict");
42            if (PyErr_Occurred()) {
43                *sipIsErr = 1;
44                return 0;
45            }
46        }
47        *sipCppPtr = new wxString();
48        size_t len = PyUnicode_GET_SIZE(uni);
49        if (len) {
50            wxPyUnicode_AsWideChar(uni, wxStringBuffer(**sipCppPtr, len), len);
51        }
52        if (PyBytes_Check(sipPy))
53            Py_DECREF(uni);  // release the temporary Unicode object we created
54        return sipGetState(sipTransferObj);
55    %End
56
57
58    %ConvertFromTypeCode
59        // Convert a wxString to a Python Unicode object.  See wxpy_api.sip
60        return wx2PyString(*sipCpp);
61    %End
62};
63
64
65
66// Used just for testing the MappedType code, it can be removed later
67%ModuleCode
68wxString testStringTypemap(const wxString& str)
69{
70    wxString local = str;
71    return local;
72}
73%End
74wxString testStringTypemap(const wxString& str);
75