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