]>
Commit | Line | Data |
---|---|---|
d14a1e28 RD |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: _core_api.i | |
3 | // Purpose: | |
4 | // | |
5 | // Author: Robin Dunn | |
6 | // | |
7 | // Created: 13-Sept-2003 | |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) 2003 by Total Control Software | |
10 | // Licence: wxWindows license | |
11 | ///////////////////////////////////////////////////////////////////////////// | |
12 | ||
13 | // Not a %module | |
14 | ||
15 | ||
16 | //--------------------------------------------------------------------------- | |
17 | %{ | |
18 | #ifndef wxPyUSE_EXPORT | |
19 | // Helper functions for dealing with SWIG objects and such. These are | |
20 | // located here so they know about the SWIG types and functions declared | |
21 | // in the wrapper code. | |
22 | ||
23 | #include <wx/hashmap.h> | |
24 | WX_DECLARE_STRING_HASH_MAP( swig_type_info*, wxPyTypeInfoHashMap ); | |
25 | ||
26 | ||
27 | // Maintains a hashmap of className to swig_type_info pointers. Given the | |
28 | // name of a class either looks up the type info in the cache, or scans the | |
29 | // SWIG tables for it. | |
30 | extern PyObject* wxPyPtrTypeMap; | |
31 | static | |
32 | swig_type_info* wxPyFindSwigType(const wxChar* className) { | |
33 | ||
34 | static wxPyTypeInfoHashMap* typeInfoCache = NULL; | |
35 | ||
36 | if (typeInfoCache == NULL) | |
37 | typeInfoCache = new wxPyTypeInfoHashMap; | |
38 | ||
39 | wxString name(className); | |
40 | swig_type_info* swigType = (*typeInfoCache)[name]; | |
41 | ||
42 | if (! swigType) { | |
43 | // it wasn't in the cache, so look it up from SWIG | |
44 | name.Append(wxT(" *")); | |
1b8c7ba6 | 45 | swigType = SWIG_TypeQuery(name.mb_str()); |
d14a1e28 RD |
46 | |
47 | // if it still wasn't found, try looking for a mapped name | |
48 | if (!swigType) { | |
49 | PyObject* item; | |
50 | name = className; | |
51 | ||
52 | if ((item = PyDict_GetItemString(wxPyPtrTypeMap, | |
53 | (char*)(const char*)name.mbc_str())) != NULL) { | |
54 | name = wxString(PyString_AsString(item), *wxConvCurrent); | |
55 | name.Append(wxT(" *")); | |
1b8c7ba6 | 56 | swigType = SWIG_TypeQuery(name.mb_str()); |
d14a1e28 RD |
57 | } |
58 | } | |
59 | if (swigType) { | |
60 | // and add it to the map if found | |
61 | (*typeInfoCache)[className] = swigType; | |
62 | } | |
63 | } | |
64 | return swigType; | |
65 | } | |
66 | ||
67 | ||
68 | // Check if a class name is a type known to SWIG | |
69 | bool wxPyCheckSwigType(const wxChar* className) { | |
70 | ||
71 | swig_type_info* swigType = wxPyFindSwigType(className); | |
72 | return swigType != NULL; | |
73 | } | |
74 | ||
75 | ||
76 | // Given a pointer to a C++ object and a class name, construct a Python proxy | |
77 | // object for it. | |
78 | PyObject* wxPyConstructObject(void* ptr, | |
79 | const wxChar* className, | |
80 | int setThisOwn) { | |
81 | ||
82 | swig_type_info* swigType = wxPyFindSwigType(className); | |
83 | wxCHECK_MSG(swigType != NULL, NULL, wxT("Unknown type in wxPyConstructObject")); | |
84 | ||
85 | return SWIG_Python_NewPointerObj(ptr, swigType, setThisOwn); | |
86 | } | |
87 | ||
88 | ||
89 | // Extract a pointer to the wrapped C++ object from a Python proxy object. | |
90 | // Ensures that the proxy object is of the specified (or derived) type. If | |
91 | // not able to perform the conversion then a Python exception is set and the | |
dd9f7fea | 92 | // error should be handled properly in the caller. Returns True on success. |
d14a1e28 RD |
93 | bool wxPyConvertSwigPtr(PyObject* obj, void **ptr, |
94 | const wxChar* className) { | |
95 | ||
96 | swig_type_info* swigType = wxPyFindSwigType(className); | |
a72f4631 | 97 | wxCHECK_MSG(swigType != NULL, false, wxT("Unknown type in wxPyConvertSwigPtr")); |
d14a1e28 RD |
98 | |
99 | return SWIG_Python_ConvertPtr(obj, ptr, swigType, SWIG_POINTER_EXCEPTION) != -1; | |
100 | } | |
101 | ||
102 | ||
103 | // Make a SWIGified pointer object suitable for a .this attribute | |
104 | PyObject* wxPyMakeSwigPtr(void* ptr, const wxChar* className) { | |
105 | ||
106 | PyObject* robj = NULL; | |
107 | ||
108 | swig_type_info* swigType = wxPyFindSwigType(className); | |
109 | wxCHECK_MSG(swigType != NULL, NULL, wxT("Unknown type in wxPyConvertSwigPtr")); | |
110 | ||
111 | #ifdef SWIG_COBJECT_TYPES | |
1b8c7ba6 | 112 | robj = PySwigObject_FromVoidPtrAndDesc((void *) ptr, (char *)swigType->name); |
d14a1e28 RD |
113 | #else |
114 | { | |
115 | char result[1024]; | |
1b8c7ba6 RD |
116 | robj = SWIG_PackVoidPtr(result, ptr, swigType->name, sizeof(result)) ? |
117 | PyString_FromString(result) : 0; | |
118 | } | |
d14a1e28 RD |
119 | #endif |
120 | ||
121 | return robj; | |
122 | } | |
123 | ||
124 | ||
02b800ce RD |
125 | // Python's PyInstance_Check does not return True for instances of new-style |
126 | // classes. This should get close enough for both new and old classes but I | |
127 | // should re-evaluate the need for doing instance checks... | |
128 | bool wxPyInstance_Check(PyObject* obj) { | |
129 | return PyObject_HasAttrString(obj, "__class__") != 0; | |
130 | } | |
131 | ||
132 | ||
133 | ||
134 | // This one checks if the object is an instance of a SWIG proxy class (it has | |
135 | // a .this attribute, and the .this attribute is a PySwigObject.) | |
136 | bool wxPySwigInstance_Check(PyObject* obj) { | |
137 | static PyObject* this_str = NULL; | |
138 | if (this_str == NULL) | |
139 | this_str = PyString_FromString("this"); | |
140 | ||
141 | PyObject* this_attr = PyObject_GetAttr(obj, this_str); | |
142 | if (this_attr) { | |
143 | bool retval = (PySwigObject_Check(this_attr) != 0); | |
144 | Py_DECREF(this_attr); | |
145 | return retval; | |
146 | } | |
147 | ||
148 | PyErr_Clear(); | |
149 | return false; | |
150 | } | |
151 | ||
d14a1e28 RD |
152 | |
153 | ||
154 | // Export a C API in a struct. Other modules will be able to load this from | |
54f9ee45 RD |
155 | // the wx._core_ module and will then have safe access to these functions, |
156 | // even if they are located in another shared library. | |
d14a1e28 RD |
157 | static wxPyCoreAPI API = { |
158 | ||
d14a1e28 RD |
159 | wxPyCheckSwigType, |
160 | wxPyConstructObject, | |
161 | wxPyConvertSwigPtr, | |
162 | wxPyMakeSwigPtr, | |
163 | ||
164 | wxPyBeginAllowThreads, | |
165 | wxPyEndAllowThreads, | |
166 | wxPyBeginBlockThreads, | |
167 | wxPyEndBlockThreads, | |
168 | ||
169 | wxPy_ConvertList, | |
170 | ||
171 | wxString_in_helper, | |
172 | Py2wxString, | |
173 | wx2PyString, | |
174 | ||
175 | byte_LIST_helper, | |
176 | int_LIST_helper, | |
177 | long_LIST_helper, | |
178 | string_LIST_helper, | |
179 | wxPoint_LIST_helper, | |
180 | wxBitmap_LIST_helper, | |
181 | wxString_LIST_helper, | |
182 | wxAcceleratorEntry_LIST_helper, | |
183 | ||
184 | wxSize_helper, | |
185 | wxPoint_helper, | |
186 | wxRealPoint_helper, | |
187 | wxRect_helper, | |
188 | wxColour_helper, | |
189 | wxPoint2D_helper, | |
190 | ||
191 | wxPySimple_typecheck, | |
192 | wxColour_typecheck, | |
193 | ||
194 | wxPyCBH_setCallbackInfo, | |
195 | wxPyCBH_findCallback, | |
196 | wxPyCBH_callCallback, | |
197 | wxPyCBH_callCallbackObj, | |
198 | wxPyCBH_delete, | |
199 | ||
200 | wxPyMake_wxObject, | |
201 | wxPyMake_wxSizer, | |
202 | wxPyPtrTypeMap_Add, | |
203 | wxPy2int_seq_helper, | |
204 | wxPy4int_seq_helper, | |
205 | wxArrayString2PyList_helper, | |
206 | wxArrayInt2PyList_helper, | |
207 | ||
208 | wxPyClientData_dtor, | |
209 | wxPyUserData_dtor, | |
210 | wxPyOORClientData_dtor, | |
211 | ||
212 | wxPyCBInputStream_create, | |
d02ea46c RD |
213 | wxPyCBInputStream_copy, |
214 | ||
d14a1e28 | 215 | wxPyInstance_Check, |
ab1f7d2a RD |
216 | wxPySwigInstance_Check, |
217 | ||
218 | wxPyCheckForApp | |
d14a1e28 RD |
219 | |
220 | }; | |
221 | ||
222 | #endif | |
223 | %} | |
224 | ||
225 | ||
226 | ||
227 | ||
228 | %init %{ | |
229 | #ifndef wxPyUSE_EXPORT | |
230 | // Make our API structure a CObject so other modules can import it | |
231 | // from this module. | |
232 | PyObject* cobj = PyCObject_FromVoidPtr(&API, NULL); | |
233 | PyDict_SetItemString(d,"_wxPyCoreAPI", cobj); | |
234 | Py_XDECREF(cobj); | |
235 | #endif | |
236 | %} | |
237 | ||
238 | //--------------------------------------------------------------------------- |