1//--------------------------------------------------------------------------
2// Name:        pgvariant.sip
3// Purpose:     MappedType for wxPGVariant
4//
5// Author:      Robin Dunn
6//
7// Created:     24-Feb-2018
8// Copyright:   (c) 2017-2018 by Total Control Software
9// Licence:     wxWindows license
10//--------------------------------------------------------------------------
11
12%ModuleHeaderCode
13// A wxPGVariant is really the same thing as a wxVariant.  We just create
14// this new type so a different %MappedType can be created for it that
15// also supports the variant data types that are in the propgrid APIs
16// instead of core.
17typedef wxVariant wxPGVariant;
18typedef wxVariantList wxPGVariantList;
19
20wxVariant wxPGVariant_in_helper(PyObject* source);
21PyObject* wxPGVariant_out_helper(const wxVariant& value);
22%End
23
24
25%ModuleCode
26wxVariant wxPGVariant_in_helper(PyObject* obj)
27{
28    wxVariant value;
29    int state = 0;
30    int isErr = 0;
31
32    // Always check for None first so we don't just get NULL pointers for the
33    // other types.
34    if (obj == Py_None)
35        value.MakeNull();
36
37    else if (wxPyWrappedPtr_TypeCheck(obj, wxT("wxFont"))) {
38        wxFont* ptr;
39        wxPyConvertWrappedPtr(obj, (void**)&ptr, wxT("wxFont"));
40        value << *ptr;
41    }
42
43    else if (wxPyWrappedPtr_TypeCheck(obj, wxT("wxPoint"))) {
44        wxPoint* ptr;
45        wxPyConvertWrappedPtr(obj, (void**)&ptr, wxT("wxPoint"));
46        value << *ptr;
47    }
48
49    else if (wxPyWrappedPtr_TypeCheck(obj, wxT("wxSize"))) {
50        wxSize* ptr;
51        wxPyConvertWrappedPtr(obj, (void**)&ptr, wxT("wxSize"));
52        value << *ptr;
53    }
54
55    else if (wxPyWrappedPtr_TypeCheck(obj, wxT("wxColourPropertyValue"))) {
56        wxColourPropertyValue* ptr;
57        wxPyConvertWrappedPtr(obj, (void**)&ptr, wxT("wxColourPropertyValue"));
58        value << *ptr;
59    }
60
61    else if (sipCanConvertToType(obj, sipType_wxArrayInt, 0)) {
62        wxArrayInt* ptr;
63        ptr = (wxArrayInt*)sipConvertToType(obj, sipType_wxArrayInt, NULL, 0, &state, &isErr);
64        if (!isErr) {
65            value << *ptr;
66            sipReleaseType(ptr, sipType_wxArrayInt, state);
67        }
68    }
69
70    else
71        value = wxVariant_in_helper(obj);
72    return value;
73}
74
75
76PyObject* wxPGVariant_out_helper(const wxVariant& value)
77{
78    PyObject* obj;
79
80
81    if (value.IsNull()) {
82        obj = Py_None;
83        Py_INCREF(obj);
84    }
85
86    else if ( value.IsType("wxFont") ) {
87        wxFont val;
88        val << value;
89        obj = wxPyConstructObject(new wxFont(val), "wxFont", true);
90    }
91
92    else if ( value.IsType("wxPoint") ) {
93        const wxPoint& val = wxPointRefFromVariant(value);
94        obj = wxPyConstructObject(new wxPoint(val), "wxPoint", true);
95    }
96
97    else if ( value.IsType("wxSize") ) {
98        const wxSize& val = wxSizeRefFromVariant(value);
99        obj = wxPyConstructObject(new wxSize(val), "wxSize", true);
100    }
101
102    else if ( value.IsType("wxColourPropertyValue") ) {
103        wxColourPropertyValue val;
104         val << value;
105        obj = wxPyConstructObject(new wxColourPropertyValue(val), "wxColourPropertyValue", true);
106    }
107
108    else if ( value.IsType("wxArrayInt") ) {
109        const wxArrayInt& arr = wxArrayIntRefFromVariant(value);
110        obj = sipConvertFromType((void*)&arr, sipType_wxArrayInt, NULL);
111    }
112
113    else
114        obj = wxVariant_out_helper(value);
115    return obj;
116}
117%End
118
119
120
121%MappedType wxPGVariant /AllowNone/
122{
123    %ConvertToTypeCode
124        // Code to test a PyObject for compatibility.
125        if (!sipIsErr) {
126            // Any type should work since we'll just use the PyObject directly
127            // if the type is not one that is explicitly supported.
128            return TRUE;
129        }
130
131        // Code to create a new wxVariant from the PyObject
132        wxVariant* value =  new wxVariant(wxPGVariant_in_helper(sipPy));
133        *sipCppPtr = value;
134        return sipGetState(sipTransferObj);
135    %End
136
137
138    %ConvertFromTypeCode
139        // Code to convert a wxVariant to a PyObject.
140        if (sipCpp == NULL) {
141            return Py_None;
142        } else {
143            return wxPGVariant_out_helper(*sipCpp);
144        }
145    %End
146};
147
148
149
150// Add a typemap for wxVariantList
151%MappedType wxPGVariantList
152{
153    %ConvertToTypeCode
154        // Code to test a PyObject for compatibility.
155        if (!sipIsErr) {
156            // Any type sequence type is okay.
157            int success = PySequence_Check(sipPy);
158            if (!success)
159                PyErr_SetString(PyExc_TypeError, "Sequence type expected.");
160            return success;
161        }
162
163        // Code to create a new wxVariantList from the PyObject sequence
164        wxVariantList* value =  new wxVariantList();
165        Py_ssize_t len = PySequence_Length(sipPy);
166        Py_ssize_t idx = 0;
167        while (idx < len) {
168            PyObject* item = PySequence_GetItem(sipPy, idx);
169            value->Append(new wxVariant(wxPGVariant_in_helper(item)));
170            Py_DECREF(item);
171        }
172        *sipCppPtr = value;
173        return sipGetState(sipTransferObj);
174    %End
175
176
177    %ConvertFromTypeCode
178        // Code to convert a wxVariantList to a Python list.
179        if (sipCpp == NULL) {
180            return Py_None;
181        } else {
182            size_t idx = 0;
183            PyObject* value = PyList_New(0);
184            for (idx=0; idx < sipCpp->GetCount(); idx++) {
185                PyObject* item = wxPGVariant_out_helper(sipCpp->Item(idx));
186                PyList_Append(value, item);
187            }
188            return value;
189        }
190    %End
191};
192
193
194
195
196
197// Used just for unit testing the MappedType code, it can be removed later
198%ModuleCode
199wxPGVariant testPGVariantTypemap(const wxPGVariant& var)
200{
201    wxVariant local = var;  // force a copy
202    return local;
203}
204
205wxString testPGVariantTypeName(const wxPGVariant& var)
206{
207    return var.GetType();
208}
209%End
210wxPGVariant testPGVariantTypemap(const wxPGVariant& var);
211wxString testPGVariantTypeName(const wxPGVariant& var);
212