]>
Commit | Line | Data |
---|---|---|
7bf85405 RD |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: helpers.cpp | |
03e9bead | 3 | // Purpose: Helper functions/classes for the wxPython extension module |
7bf85405 RD |
4 | // |
5 | // Author: Robin Dunn | |
6 | // | |
7 | // Created: 7/1/97 | |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) 1998 by Total Control Software | |
10 | // Licence: wxWindows license | |
11 | ///////////////////////////////////////////////////////////////////////////// | |
12 | ||
08127323 | 13 | |
7bf85405 RD |
14 | #undef DEBUG |
15 | #include <Python.h> | |
16 | #include "helpers.h" | |
cbf60e09 | 17 | #include "pyistream.h" |
694759cf | 18 | |
af309447 RD |
19 | #ifdef __WXMSW__ |
20 | #include <wx/msw/private.h> | |
78b57918 | 21 | #include <wx/msw/winundef.h> |
4268f798 | 22 | #include <wx/msw/msvcrt.h> |
af309447 | 23 | #endif |
694759cf RD |
24 | |
25 | #ifdef __WXGTK__ | |
26 | #include <gtk/gtk.h> | |
54b96882 RD |
27 | #include <gdk/gdkprivate.h> |
28 | #include <wx/gtk/win_gtk.h> | |
694759cf | 29 | #endif |
7bf85405 | 30 | |
c8bc7bb8 RD |
31 | //---------------------------------------------------------------------- |
32 | ||
33 | #if PYTHON_API_VERSION <= 1007 && wxUSE_UNICODE | |
34 | #error Python must support Unicode to use wxWindows Unicode | |
35 | #endif | |
36 | ||
4268f798 RD |
37 | //---------------------------------------------------------------------- |
38 | ||
1893b029 RD |
39 | #ifdef __WXGTK__ |
40 | int WXDLLEXPORT wxEntryStart( int& argc, char** argv ); | |
41 | #else | |
4268f798 | 42 | int WXDLLEXPORT wxEntryStart( int argc, char** argv ); |
1893b029 | 43 | #endif |
4268f798 RD |
44 | int WXDLLEXPORT wxEntryInitGui(); |
45 | void WXDLLEXPORT wxEntryCleanup(); | |
46 | ||
47 | wxPyApp* wxPythonApp = NULL; // Global instance of application object | |
e3dbf593 | 48 | bool wxPyDoCleanup = FALSE; |
4268f798 RD |
49 | |
50 | ||
51 | #ifdef WXP_WITH_THREAD | |
52 | struct wxPyThreadState { | |
53 | unsigned long tid; | |
54 | PyThreadState* tstate; | |
55 | ||
56 | wxPyThreadState(unsigned long _tid=0, PyThreadState* _tstate=NULL) | |
57 | : tid(_tid), tstate(_tstate) {} | |
58 | }; | |
59 | ||
60 | #include <wx/dynarray.h> | |
61 | WX_DECLARE_OBJARRAY(wxPyThreadState, wxPyThreadStateArray); | |
62 | #include <wx/arrimpl.cpp> | |
63 | WX_DEFINE_OBJARRAY(wxPyThreadStateArray); | |
64 | ||
65 | wxPyThreadStateArray* wxPyTStates = NULL; | |
66 | wxMutex* wxPyTMutex = NULL; | |
67 | #endif | |
7bf85405 RD |
68 | |
69 | ||
21f4bf45 | 70 | #ifdef __WXMSW__ // If building for win32... |
21f4bf45 RD |
71 | //---------------------------------------------------------------------- |
72 | // This gets run when the DLL is loaded. We just need to save a handle. | |
73 | //---------------------------------------------------------------------- | |
9c039d08 | 74 | |
21f4bf45 RD |
75 | BOOL WINAPI DllMain( |
76 | HINSTANCE hinstDLL, // handle to DLL module | |
77 | DWORD fdwReason, // reason for calling function | |
78 | LPVOID lpvReserved // reserved | |
79 | ) | |
80 | { | |
a2426843 RD |
81 | // If wxPython is embedded in another wxWindows app then |
82 | // the inatance has already been set. | |
83 | if (! wxGetInstance()) | |
84 | wxSetInstance(hinstDLL); | |
85 | return TRUE; | |
21f4bf45 RD |
86 | } |
87 | #endif | |
88 | ||
7bf85405 | 89 | //---------------------------------------------------------------------- |
4268f798 | 90 | // Classes for implementing the wxp main application shell. |
7bf85405 RD |
91 | //---------------------------------------------------------------------- |
92 | ||
7bf85405 | 93 | |
cf694132 | 94 | wxPyApp::wxPyApp() { |
bc5f2236 | 95 | SetUseBestVisual(TRUE); |
cf694132 RD |
96 | } |
97 | ||
98 | wxPyApp::~wxPyApp() { | |
cf694132 RD |
99 | } |
100 | ||
101 | ||
7bf85405 | 102 | // This one isn't acutally called... See __wxStart() |
4268f798 | 103 | bool wxPyApp::OnInit() { |
6999b0d8 | 104 | return FALSE; |
7bf85405 RD |
105 | } |
106 | ||
4268f798 RD |
107 | |
108 | int wxPyApp::MainLoop() { | |
7ff49f0c | 109 | int retval = 0; |
af309447 | 110 | |
7ff49f0c | 111 | DeletePendingObjects(); |
4268f798 | 112 | bool initialized = wxTopLevelWindows.GetCount() != 0; |
7ff49f0c | 113 | #ifdef __WXGTK__ |
4268f798 | 114 | m_initialized = initialized; |
7ff49f0c | 115 | #endif |
7bf85405 | 116 | |
4268f798 | 117 | if (initialized) { |
098be78d RD |
118 | if ( m_exitOnFrameDelete == Later ) { |
119 | m_exitOnFrameDelete = Yes; | |
120 | } | |
121 | ||
7ff49f0c | 122 | retval = wxApp::MainLoop(); |
4268f798 | 123 | OnExit(); |
7ff49f0c RD |
124 | } |
125 | return retval; | |
126 | } | |
7bf85405 RD |
127 | |
128 | ||
4268f798 | 129 | |
fb5e0af0 | 130 | //--------------------------------------------------------------------- |
7bf85405 RD |
131 | //---------------------------------------------------------------------- |
132 | ||
a541c325 RD |
133 | |
134 | static char* wxPyCopyCString(const wxChar* src) | |
135 | { | |
136 | wxWX2MBbuf buff = (wxWX2MBbuf)wxConvCurrent->cWX2MB(src); | |
137 | size_t len = strlen(buff); | |
138 | char* dest = new char[len+1]; | |
139 | strcpy(dest, buff); | |
140 | return dest; | |
141 | } | |
142 | ||
c8bc7bb8 | 143 | #if wxUSE_UNICODE |
a541c325 | 144 | static char* wxPyCopyCString(const char* src) // we need a char version too |
c8bc7bb8 | 145 | { |
a541c325 RD |
146 | size_t len = strlen(src); |
147 | char* dest = new char[len+1]; | |
148 | strcpy(dest, src); | |
149 | return dest; | |
c8bc7bb8 | 150 | } |
a541c325 | 151 | #endif |
c8bc7bb8 | 152 | |
a541c325 | 153 | static wxChar* wxPyCopyWString(const char *src) |
c8bc7bb8 | 154 | { |
a541c325 RD |
155 | //wxMB2WXbuf buff = wxConvCurrent->cMB2WX(src); |
156 | wxString str(src, *wxConvCurrent); | |
157 | return copystring(str); | |
c8bc7bb8 RD |
158 | } |
159 | ||
a541c325 RD |
160 | #if wxUSE_UNICODE |
161 | static wxChar* wxPyCopyWString(const wxChar *src) | |
c8bc7bb8 | 162 | { |
a541c325 | 163 | return copystring(src); |
c8bc7bb8 RD |
164 | } |
165 | #endif | |
f6bcfd97 | 166 | |
a541c325 RD |
167 | |
168 | //---------------------------------------------------------------------- | |
169 | ||
0d6f9504 | 170 | // This is where we pick up the first part of the wxEntry functionality... |
f6bcfd97 | 171 | // The rest is in __wxStart and __wxCleanup. This function is called when |
8bf5d46e | 172 | // wxcmodule is imported. (Before there is a wxApp object.) |
0d6f9504 | 173 | void __wxPreStart() |
7bf85405 | 174 | { |
de20db99 RD |
175 | |
176 | #ifdef __WXMSW__ | |
44f68505 | 177 | // wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF); |
de20db99 RD |
178 | #endif |
179 | ||
9d8bd15f | 180 | #ifdef WXP_WITH_THREAD |
1112b0c6 | 181 | PyEval_InitThreads(); |
4268f798 RD |
182 | wxPyTStates = new wxPyThreadStateArray; |
183 | wxPyTMutex = new wxMutex; | |
9d8bd15f RD |
184 | #endif |
185 | ||
0b85cc38 RD |
186 | wxApp::CheckBuildOptions(wxBuildOptions()); |
187 | ||
a2426843 | 188 | // Bail out if there is already a wxApp created. This means that the |
0d6f9504 | 189 | // toolkit has already been initialized, as in embedding wxPython in |
a2426843 RD |
190 | // a C++ wxWindows app, so we don't need to call wxEntryStart. |
191 | if (wxTheApp != NULL) { | |
0d6f9504 | 192 | return; |
a2426843 | 193 | } |
e3dbf593 | 194 | wxPyDoCleanup = TRUE; |
7ff49f0c | 195 | |
9416aa89 RD |
196 | int argc = 0; |
197 | char** argv = NULL; | |
7ff49f0c | 198 | PyObject* sysargv = PySys_GetObject("argv"); |
9416aa89 RD |
199 | if (sysargv != NULL) { |
200 | argc = PyList_Size(sysargv); | |
201 | argv = new char*[argc+1]; | |
202 | int x; | |
c8bc7bb8 RD |
203 | for(x=0; x<argc; x++) { |
204 | PyObject *item = PyList_GetItem(sysargv, x); | |
205 | #if wxUSE_UNICODE | |
206 | if (PyUnicode_Check(item)) | |
a541c325 RD |
207 | argv[x] = wxPyCopyCString(PyUnicode_AS_UNICODE(item)); |
208 | else | |
c8bc7bb8 | 209 | #endif |
a541c325 | 210 | argv[x] = wxPyCopyCString(PyString_AsString(item)); |
c8bc7bb8 | 211 | } |
9416aa89 RD |
212 | argv[argc] = NULL; |
213 | } | |
7bf85405 | 214 | |
7ff49f0c | 215 | wxEntryStart(argc, argv); |
f6bcfd97 | 216 | delete [] argv; |
0d6f9504 RD |
217 | } |
218 | ||
219 | ||
0d6f9504 RD |
220 | // Start the user application, user App's OnInit method is a parameter here |
221 | PyObject* __wxStart(PyObject* /* self */, PyObject* args) | |
222 | { | |
223 | PyObject* onInitFunc = NULL; | |
224 | PyObject* arglist; | |
225 | PyObject* result; | |
226 | long bResult; | |
227 | ||
0d6f9504 RD |
228 | if (!PyArg_ParseTuple(args, "O", &onInitFunc)) |
229 | return NULL; | |
230 | ||
0d6f9504 | 231 | // This is the next part of the wxEntry functionality... |
9416aa89 | 232 | int argc = 0; |
c8bc7bb8 | 233 | wxChar** argv = NULL; |
f6bcfd97 | 234 | PyObject* sysargv = PySys_GetObject("argv"); |
9416aa89 RD |
235 | if (sysargv != NULL) { |
236 | argc = PyList_Size(sysargv); | |
c8bc7bb8 | 237 | argv = new wxChar*[argc+1]; |
9416aa89 | 238 | int x; |
c8bc7bb8 RD |
239 | for(x=0; x<argc; x++) { |
240 | PyObject *pyArg = PyList_GetItem(sysargv, x); | |
241 | #if wxUSE_UNICODE | |
a541c325 RD |
242 | if (PyUnicode_Check(pyArg)) |
243 | argv[x] = wxPyCopyWString(PyUnicode_AS_UNICODE(pyArg)); | |
244 | else | |
c8bc7bb8 | 245 | #endif |
a541c325 | 246 | argv[x] = wxPyCopyWString(PyString_AsString(pyArg)); |
c8bc7bb8 | 247 | } |
9416aa89 RD |
248 | argv[argc] = NULL; |
249 | } | |
c8bc7bb8 | 250 | |
f6bcfd97 BP |
251 | wxPythonApp->argc = argc; |
252 | wxPythonApp->argv = argv; | |
0d6f9504 | 253 | |
7ff49f0c | 254 | wxEntryInitGui(); |
7bf85405 RD |
255 | |
256 | // Call the Python App's OnInit function | |
257 | arglist = PyTuple_New(0); | |
258 | result = PyEval_CallObject(onInitFunc, arglist); | |
8bf5d46e RD |
259 | if (!result) { // an exception was raised. |
260 | return NULL; | |
7bf85405 RD |
261 | } |
262 | ||
263 | if (! PyInt_Check(result)) { | |
264 | PyErr_SetString(PyExc_TypeError, "OnInit should return a boolean value"); | |
265 | return NULL; | |
266 | } | |
267 | bResult = PyInt_AS_LONG(result); | |
268 | if (! bResult) { | |
194fa2ac | 269 | PyErr_SetString(PyExc_SystemExit, "OnInit returned FALSE, exiting..."); |
7bf85405 RD |
270 | return NULL; |
271 | } | |
272 | ||
13dfc243 | 273 | #ifdef __WXGTK__ |
7ff49f0c | 274 | wxTheApp->m_initialized = (wxTopLevelWindows.GetCount() > 0); |
13dfc243 | 275 | #endif |
fb5e0af0 | 276 | |
7bf85405 RD |
277 | Py_INCREF(Py_None); |
278 | return Py_None; | |
279 | } | |
280 | ||
4268f798 | 281 | |
7ff49f0c | 282 | void __wxCleanup() { |
e3dbf593 RD |
283 | if (wxPyDoCleanup) |
284 | wxEntryCleanup(); | |
7faa9591 | 285 | #ifdef WXP_WITH_THREAD |
4268f798 RD |
286 | delete wxPyTMutex; |
287 | wxPyTMutex = NULL; | |
288 | wxPyTStates->Empty(); | |
289 | delete wxPyTStates; | |
290 | wxPyTStates = NULL; | |
7faa9591 | 291 | #endif |
7ff49f0c | 292 | } |
7bf85405 RD |
293 | |
294 | ||
295 | ||
9416aa89 RD |
296 | static PyObject* wxPython_dict = NULL; |
297 | static PyObject* wxPyPtrTypeMap = NULL; | |
298 | ||
4acff284 | 299 | |
7bf85405 RD |
300 | PyObject* __wxSetDictionary(PyObject* /* self */, PyObject* args) |
301 | { | |
302 | ||
303 | if (!PyArg_ParseTuple(args, "O", &wxPython_dict)) | |
304 | return NULL; | |
305 | ||
306 | if (!PyDict_Check(wxPython_dict)) { | |
307 | PyErr_SetString(PyExc_TypeError, "_wxSetDictionary must have dictionary object!"); | |
308 | return NULL; | |
309 | } | |
9416aa89 RD |
310 | |
311 | if (! wxPyPtrTypeMap) | |
312 | wxPyPtrTypeMap = PyDict_New(); | |
313 | PyDict_SetItemString(wxPython_dict, "__wxPyPtrTypeMap", wxPyPtrTypeMap); | |
314 | ||
315 | ||
7bf85405 | 316 | #ifdef __WXMOTIF__ |
21f4bf45 RD |
317 | #define wxPlatform "__WXMOTIF__" |
318 | #endif | |
be43cc44 RD |
319 | #ifdef __WXX11__ |
320 | #define wxPlatform "__WXX11__" | |
7bf85405 RD |
321 | #endif |
322 | #ifdef __WXGTK__ | |
21f4bf45 | 323 | #define wxPlatform "__WXGTK__" |
7bf85405 RD |
324 | #endif |
325 | #if defined(__WIN32__) || defined(__WXMSW__) | |
fb5e0af0 | 326 | #define wxPlatform "__WXMSW__" |
7bf85405 RD |
327 | #endif |
328 | #ifdef __WXMAC__ | |
21f4bf45 | 329 | #define wxPlatform "__WXMAC__" |
7bf85405 RD |
330 | #endif |
331 | ||
249a57f4 RD |
332 | #ifdef __WXDEBUG__ |
333 | int wxdebug = 1; | |
334 | #else | |
335 | int wxdebug = 0; | |
336 | #endif | |
337 | ||
7bf85405 | 338 | PyDict_SetItemString(wxPython_dict, "wxPlatform", PyString_FromString(wxPlatform)); |
c8bc7bb8 | 339 | PyDict_SetItemString(wxPython_dict, "wxUSE_UNICODE", PyInt_FromLong(wxUSE_UNICODE)); |
249a57f4 | 340 | PyDict_SetItemString(wxPython_dict, "__WXDEBUG__", PyInt_FromLong(wxdebug)); |
c8bc7bb8 | 341 | |
7bf85405 RD |
342 | Py_INCREF(Py_None); |
343 | return Py_None; | |
344 | } | |
345 | ||
4acff284 RD |
346 | //--------------------------------------------------------------------------- |
347 | ||
348 | void wxPyClientData_dtor(wxPyClientData* self) { | |
349 | wxPyBeginBlockThreads(); | |
350 | Py_DECREF(self->m_obj); | |
351 | wxPyEndBlockThreads(); | |
352 | } | |
353 | ||
354 | void wxPyUserData_dtor(wxPyUserData* self) { | |
355 | wxPyBeginBlockThreads(); | |
356 | Py_DECREF(self->m_obj); | |
357 | wxPyEndBlockThreads(); | |
358 | } | |
359 | ||
360 | ||
361 | // This is called when an OOR controled object is being destroyed. Although | |
362 | // the C++ object is going away there is no way to force the Python object | |
363 | // (and all references to it) to die too. This causes problems (crashes) in | |
364 | // wxPython when a python shadow object attempts to call a C++ method using | |
365 | // the now bogus pointer... So to try and prevent this we'll do a little black | |
366 | // magic and change the class of the python instance to a class that will | |
367 | // raise an exception for any attempt to call methods with it. See | |
368 | // _wxPyDeadObject in _extras.py for the implementation of this class. | |
369 | void wxPyOORClientData_dtor(wxPyOORClientData* self) { | |
370 | ||
371 | static PyObject* deadObjectClass = NULL; | |
372 | ||
373 | wxPyBeginBlockThreads(); | |
374 | if (deadObjectClass == NULL) { | |
375 | deadObjectClass = PyDict_GetItemString(wxPython_dict, "_wxPyDeadObject"); | |
376 | wxASSERT_MSG(deadObjectClass != NULL, wxT("Can't get _wxPyDeadObject class!")); | |
377 | Py_INCREF(deadObjectClass); | |
378 | } | |
379 | ||
380 | // Clear the instance's dictionary, put the name of the old class into the | |
381 | // instance, and then reset the class to be the dead class. | |
382 | if (self->m_obj->ob_refcnt > 1) { // but only if there is more than one reference | |
383 | wxASSERT_MSG(PyInstance_Check(self->m_obj), wxT("m_obj not an instance!?!?!")); | |
384 | PyInstanceObject* inst = (PyInstanceObject*)self->m_obj; | |
385 | PyDict_Clear(inst->in_dict); | |
386 | PyDict_SetItemString(inst->in_dict, "_name", inst->in_class->cl_name); | |
387 | inst->in_class = (PyClassObject*)deadObjectClass; | |
388 | Py_INCREF(deadObjectClass); | |
389 | } | |
390 | wxPyEndBlockThreads(); | |
391 | } | |
7bf85405 | 392 | |
9416aa89 RD |
393 | //--------------------------------------------------------------------------- |
394 | // Stuff used by OOR to find the right wxPython class type to return and to | |
395 | // build it. | |
396 | ||
397 | ||
398 | // The pointer type map is used when the "pointer" type name generated by SWIG | |
399 | // is not the same as the shadow class name, for example wxPyTreeCtrl | |
400 | // vs. wxTreeCtrl. It needs to be referenced in Python as well as from C++, | |
401 | // so we'll just make it a Python dictionary in the wx module's namespace. | |
4acff284 | 402 | // (See __wxSetDictionary) |
9416aa89 RD |
403 | void wxPyPtrTypeMap_Add(const char* commonName, const char* ptrName) { |
404 | if (! wxPyPtrTypeMap) | |
405 | wxPyPtrTypeMap = PyDict_New(); | |
9416aa89 RD |
406 | PyDict_SetItemString(wxPyPtrTypeMap, |
407 | (char*)commonName, | |
408 | PyString_FromString((char*)ptrName)); | |
409 | } | |
410 | ||
411 | ||
412 | ||
a541c325 | 413 | PyObject* wxPyClassExists(const wxString& className) { |
9416aa89 RD |
414 | |
415 | if (!className) | |
416 | return NULL; | |
417 | ||
418 | char buff[64]; // should always be big enough... | |
419 | ||
a541c325 | 420 | sprintf(buff, "%sPtr", className.mbc_str()); |
9416aa89 RD |
421 | PyObject* classobj = PyDict_GetItemString(wxPython_dict, buff); |
422 | ||
423 | return classobj; // returns NULL if not found | |
424 | } | |
425 | ||
426 | ||
2f4e9287 | 427 | PyObject* wxPyMake_wxObject(wxObject* source, bool checkEvtHandler) { |
0122b7e3 RD |
428 | PyObject* target = NULL; |
429 | bool isEvtHandler = FALSE; | |
9416aa89 RD |
430 | |
431 | if (source) { | |
2aab8f16 | 432 | // If it's derived from wxEvtHandler then there may |
2f4e9287 RD |
433 | // already be a pointer to a Python object that we can use |
434 | // in the OOR data. | |
435 | if (checkEvtHandler && wxIsKindOf(source, wxEvtHandler)) { | |
436 | isEvtHandler = TRUE; | |
0122b7e3 | 437 | wxEvtHandler* eh = (wxEvtHandler*)source; |
4acff284 | 438 | wxPyOORClientData* data = (wxPyOORClientData*)eh->GetClientObject(); |
0122b7e3 RD |
439 | if (data) { |
440 | target = data->m_obj; | |
441 | Py_INCREF(target); | |
442 | } | |
9416aa89 | 443 | } |
0122b7e3 RD |
444 | |
445 | if (! target) { | |
2aab8f16 RD |
446 | // Otherwise make it the old fashioned way by making a |
447 | // new shadow object and putting this pointer in it. | |
0122b7e3 RD |
448 | wxClassInfo* info = source->GetClassInfo(); |
449 | wxChar* name = (wxChar*)info->GetClassName(); | |
450 | PyObject* klass = wxPyClassExists(name); | |
451 | while (info && !klass) { | |
452 | name = (wxChar*)info->GetBaseClassName1(); | |
453 | info = wxClassInfo::FindClass(name); | |
454 | klass = wxPyClassExists(name); | |
455 | } | |
456 | if (info) { | |
457 | target = wxPyConstructObject(source, name, klass, FALSE); | |
458 | if (target && isEvtHandler) | |
4acff284 | 459 | ((wxEvtHandler*)source)->SetClientObject(new wxPyOORClientData(target)); |
0122b7e3 | 460 | } else { |
4acff284 | 461 | wxString msg(wxT("wxPython class not found for ")); |
0122b7e3 | 462 | msg += source->GetClassInfo()->GetClassName(); |
a541c325 | 463 | PyErr_SetString(PyExc_NameError, msg.mbc_str()); |
0122b7e3 RD |
464 | target = NULL; |
465 | } | |
9416aa89 RD |
466 | } |
467 | } else { // source was NULL so return None. | |
468 | Py_INCREF(Py_None); target = Py_None; | |
469 | } | |
470 | return target; | |
471 | } | |
472 | ||
0122b7e3 | 473 | |
2f4e9287 RD |
474 | PyObject* wxPyMake_wxSizer(wxSizer* source) { |
475 | PyObject* target = NULL; | |
476 | ||
477 | if (source && wxIsKindOf(source, wxSizer)) { | |
478 | // If it's derived from wxSizer then there may | |
479 | // already be a pointer to a Python object that we can use | |
480 | // in the OOR data. | |
481 | wxSizer* sz = (wxSizer*)source; | |
4acff284 | 482 | wxPyOORClientData* data = (wxPyOORClientData*)sz->GetClientObject(); |
2f4e9287 RD |
483 | if (data) { |
484 | target = data->m_obj; | |
485 | Py_INCREF(target); | |
486 | } | |
487 | } | |
488 | if (! target) { | |
489 | target = wxPyMake_wxObject(source, FALSE); | |
490 | if (target != Py_None) | |
4acff284 | 491 | ((wxSizer*)source)->SetClientObject(new wxPyOORClientData(target)); |
2f4e9287 RD |
492 | } |
493 | return target; | |
494 | } | |
495 | ||
496 | ||
497 | ||
7bf85405 RD |
498 | //--------------------------------------------------------------------------- |
499 | ||
c368d904 | 500 | PyObject* wxPyConstructObject(void* ptr, |
a541c325 | 501 | const wxString& className, |
9416aa89 | 502 | PyObject* klass, |
1e7ecb7b | 503 | int setThisOwn) { |
9416aa89 | 504 | |
33510773 RD |
505 | PyObject* obj; |
506 | PyObject* arg; | |
9416aa89 | 507 | PyObject* item; |
a541c325 | 508 | wxString name(className); |
9416aa89 RD |
509 | char swigptr[64]; // should always be big enough... |
510 | char buff[64]; | |
511 | ||
a541c325 RD |
512 | if ((item = PyDict_GetItemString(wxPyPtrTypeMap, (char*)(const char*)name.mbc_str())) != NULL) { |
513 | name = wxString(PyString_AsString(item), *wxConvCurrent); | |
9416aa89 | 514 | } |
a541c325 | 515 | sprintf(buff, "_%s_p", (const char*)name.mbc_str()); |
9416aa89 RD |
516 | SWIG_MakePtr(swigptr, ptr, buff); |
517 | ||
518 | arg = Py_BuildValue("(s)", swigptr); | |
519 | obj = PyInstance_New(klass, arg, NULL); | |
520 | Py_DECREF(arg); | |
521 | ||
522 | if (setThisOwn) { | |
523 | PyObject* one = PyInt_FromLong(1); | |
524 | PyObject_SetAttrString(obj, "thisown", one); | |
525 | Py_DECREF(one); | |
526 | } | |
527 | ||
528 | return obj; | |
529 | } | |
530 | ||
531 | ||
532 | PyObject* wxPyConstructObject(void* ptr, | |
a541c325 | 533 | const wxString& className, |
9416aa89 | 534 | int setThisOwn) { |
33510773 RD |
535 | if (!ptr) { |
536 | Py_INCREF(Py_None); | |
537 | return Py_None; | |
538 | } | |
539 | ||
9c039d08 | 540 | char buff[64]; // should always be big enough... |
a541c325 | 541 | sprintf(buff, "%sPtr", (const char*)className.mbc_str()); |
c5943253 | 542 | |
a541c325 | 543 | wxASSERT_MSG(wxPython_dict, wxT("wxPython_dict is not set yet!!")); |
c5943253 | 544 | |
7bf85405 RD |
545 | PyObject* classobj = PyDict_GetItemString(wxPython_dict, buff); |
546 | if (! classobj) { | |
e3dbf593 RD |
547 | wxString msg(wxT("wxPython class not found for ")); |
548 | msg += className; | |
549 | PyErr_SetString(PyExc_NameError, msg.mbc_str()); | |
550 | return NULL; | |
7bf85405 RD |
551 | } |
552 | ||
9416aa89 | 553 | return wxPyConstructObject(ptr, className, classobj, setThisOwn); |
7bf85405 RD |
554 | } |
555 | ||
a541c325 | 556 | |
d559219f | 557 | //--------------------------------------------------------------------------- |
7bf85405 | 558 | |
4958ea8f | 559 | |
cf694132 | 560 | #ifdef WXP_WITH_THREAD |
4268f798 RD |
561 | inline |
562 | unsigned long wxPyGetCurrentThreadId() { | |
4958ea8f | 563 | return wxThread::GetCurrentId(); |
4268f798 RD |
564 | } |
565 | ||
be43cc44 | 566 | static PyThreadState* gs_shutdownTState; |
4268f798 RD |
567 | static |
568 | PyThreadState* wxPyGetThreadState() { | |
be43cc44 RD |
569 | if (wxPyTMutex == NULL) // Python is shutting down... |
570 | return gs_shutdownTState; | |
571 | ||
4268f798 RD |
572 | unsigned long ctid = wxPyGetCurrentThreadId(); |
573 | PyThreadState* tstate = NULL; | |
574 | ||
575 | wxPyTMutex->Lock(); | |
576 | for(size_t i=0; i < wxPyTStates->GetCount(); i++) { | |
577 | wxPyThreadState& info = wxPyTStates->Item(i); | |
578 | if (info.tid == ctid) { | |
579 | tstate = info.tstate; | |
580 | break; | |
581 | } | |
582 | } | |
583 | wxPyTMutex->Unlock(); | |
a541c325 | 584 | wxASSERT_MSG(tstate, wxT("PyThreadState should not be NULL!")); |
4268f798 RD |
585 | return tstate; |
586 | } | |
587 | ||
588 | static | |
589 | void wxPySaveThreadState(PyThreadState* tstate) { | |
be43cc44 RD |
590 | if (wxPyTMutex == NULL) { // Python is shutting down, assume a single thread... |
591 | gs_shutdownTState = tstate; | |
592 | return; | |
593 | } | |
4268f798 RD |
594 | unsigned long ctid = wxPyGetCurrentThreadId(); |
595 | wxPyTMutex->Lock(); | |
596 | for(size_t i=0; i < wxPyTStates->GetCount(); i++) { | |
597 | wxPyThreadState& info = wxPyTStates->Item(i); | |
598 | if (info.tid == ctid) { | |
763d71e4 RD |
599 | #if 0 |
600 | if (info.tstate != tstate) | |
601 | wxLogMessage("*** tstate mismatch!???"); | |
602 | #endif | |
46e10fde | 603 | // info.tstate = tstate; *** DO NOT update existing ones??? |
763d71e4 RD |
604 | // Normally it will never change, but apparently COM callbacks |
605 | // (i.e. ActiveX controls) will (incorrectly IMHO) use a transient | |
46e10fde | 606 | // tstate which will then be garbage the next time we try to use |
763d71e4 | 607 | // it... |
4268f798 RD |
608 | wxPyTMutex->Unlock(); |
609 | return; | |
610 | } | |
1112b0c6 | 611 | } |
4268f798 RD |
612 | // not found, so add it... |
613 | wxPyTStates->Add(new wxPyThreadState(ctid, tstate)); | |
614 | wxPyTMutex->Unlock(); | |
615 | } | |
616 | ||
617 | #endif | |
618 | ||
619 | ||
620 | // Calls from Python to wxWindows code are wrapped in calls to these | |
621 | // functions: | |
622 | ||
623 | PyThreadState* wxPyBeginAllowThreads() { | |
624 | #ifdef WXP_WITH_THREAD | |
625 | PyThreadState* saved = PyEval_SaveThread(); // Py_BEGIN_ALLOW_THREADS; | |
626 | wxPySaveThreadState(saved); | |
627 | return saved; | |
628 | #else | |
629 | return NULL; | |
630 | #endif | |
631 | } | |
632 | ||
633 | void wxPyEndAllowThreads(PyThreadState* saved) { | |
634 | #ifdef WXP_WITH_THREAD | |
635 | PyEval_RestoreThread(saved); // Py_END_ALLOW_THREADS; | |
cf694132 | 636 | #endif |
d559219f | 637 | } |
cf694132 | 638 | |
cf694132 | 639 | |
4268f798 RD |
640 | |
641 | // Calls from wxWindows back to Python code, or even any PyObject | |
642 | // manipulations, PyDECREF's and etc. are wrapped in calls to these functions: | |
643 | ||
644 | void wxPyBeginBlockThreads() { | |
cf694132 | 645 | #ifdef WXP_WITH_THREAD |
4268f798 RD |
646 | PyThreadState* tstate = wxPyGetThreadState(); |
647 | PyEval_RestoreThread(tstate); | |
648 | #endif | |
649 | } | |
650 | ||
651 | ||
652 | void wxPyEndBlockThreads() { | |
653 | #ifdef WXP_WITH_THREAD | |
4268f798 | 654 | // Is there any need to save it again? |
c4770edd RD |
655 | // PyThreadState* tstate = |
656 | PyEval_SaveThread(); | |
1112b0c6 | 657 | #endif |
cf694132 RD |
658 | } |
659 | ||
19a97bd6 | 660 | |
d559219f | 661 | //--------------------------------------------------------------------------- |
f74ff5ef RD |
662 | // wxPyInputStream and wxPyCBInputStream methods |
663 | ||
f74ff5ef RD |
664 | |
665 | void wxPyInputStream::close() { | |
a541c325 | 666 | /* do nothing for now */ |
f74ff5ef RD |
667 | } |
668 | ||
669 | void wxPyInputStream::flush() { | |
a541c325 | 670 | /* do nothing for now */ |
f74ff5ef RD |
671 | } |
672 | ||
673 | bool wxPyInputStream::eof() { | |
674 | if (m_wxis) | |
675 | return m_wxis->Eof(); | |
676 | else | |
677 | return TRUE; | |
678 | } | |
679 | ||
680 | wxPyInputStream::~wxPyInputStream() { | |
681 | /* do nothing */ | |
682 | } | |
683 | ||
a541c325 RD |
684 | |
685 | ||
686 | ||
687 | PyObject* wxPyInputStream::read(int size) { | |
688 | PyObject* obj = NULL; | |
689 | wxMemoryBuffer buf; | |
f74ff5ef RD |
690 | const int BUFSIZE = 1024; |
691 | ||
692 | // check if we have a real wxInputStream to work with | |
693 | if (!m_wxis) { | |
694 | PyErr_SetString(PyExc_IOError, "no valid C-wxInputStream"); | |
695 | return NULL; | |
696 | } | |
697 | ||
698 | if (size < 0) { | |
f74ff5ef RD |
699 | // read until EOF |
700 | while (! m_wxis->Eof()) { | |
a541c325 RD |
701 | m_wxis->Read(buf.GetAppendBuf(BUFSIZE), BUFSIZE); |
702 | buf.UngetAppendBuf(m_wxis->LastRead()); | |
f74ff5ef RD |
703 | } |
704 | ||
705 | } else { // Read only size number of characters | |
a541c325 RD |
706 | m_wxis->Read(buf.GetWriteBuf(size), size); |
707 | buf.UngetWriteBuf(m_wxis->LastRead()); | |
708 | } | |
f74ff5ef | 709 | |
a541c325 RD |
710 | // error check |
711 | if (m_wxis->LastError() == wxSTREAM_READ_ERROR) { | |
712 | PyErr_SetString(PyExc_IOError,"IOError in wxInputStream"); | |
713 | } | |
714 | else { | |
715 | // We use only strings for the streams, not unicode | |
716 | obj = PyString_FromStringAndSize(buf, buf.GetDataLen()); | |
f74ff5ef | 717 | } |
a541c325 | 718 | return obj; |
f74ff5ef RD |
719 | } |
720 | ||
721 | ||
a541c325 RD |
722 | PyObject* wxPyInputStream::readline(int size) { |
723 | PyObject* obj = NULL; | |
724 | wxMemoryBuffer buf; | |
725 | int i; | |
726 | char ch; | |
727 | ||
f74ff5ef RD |
728 | // check if we have a real wxInputStream to work with |
729 | if (!m_wxis) { | |
730 | PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream"); | |
731 | return NULL; | |
732 | } | |
733 | ||
f74ff5ef RD |
734 | // read until \n or byte limit reached |
735 | for (i=ch=0; (ch != '\n') && (!m_wxis->Eof()) && ((size < 0) || (i < size)); i++) { | |
a541c325 RD |
736 | ch = m_wxis->GetC(); |
737 | buf.AppendByte(ch); | |
f74ff5ef RD |
738 | } |
739 | ||
740 | // errorcheck | |
741 | if (m_wxis->LastError() == wxSTREAM_READ_ERROR) { | |
f74ff5ef | 742 | PyErr_SetString(PyExc_IOError,"IOError in wxInputStream"); |
f74ff5ef | 743 | } |
a541c325 RD |
744 | else { |
745 | // We use only strings for the streams, not unicode | |
746 | obj = PyString_FromStringAndSize((char*)buf.GetData(), buf.GetDataLen()); | |
747 | } | |
748 | return obj; | |
f74ff5ef RD |
749 | } |
750 | ||
751 | ||
a541c325 RD |
752 | PyObject* wxPyInputStream::readlines(int sizehint) { |
753 | PyObject* pylist; | |
754 | ||
f74ff5ef RD |
755 | // check if we have a real wxInputStream to work with |
756 | if (!m_wxis) { | |
757 | PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream below"); | |
758 | return NULL; | |
759 | } | |
760 | ||
761 | // init list | |
a541c325 RD |
762 | pylist = PyList_New(0); |
763 | if (!pylist) { | |
f74ff5ef RD |
764 | PyErr_NoMemory(); |
765 | return NULL; | |
766 | } | |
767 | ||
768 | // read sizehint bytes or until EOF | |
769 | int i; | |
770 | for (i=0; (!m_wxis->Eof()) && ((sizehint < 0) || (i < sizehint));) { | |
a541c325 | 771 | PyObject* s = this->readline(); |
f74ff5ef | 772 | if (s == NULL) { |
a541c325 | 773 | Py_DECREF(pylist); |
f74ff5ef RD |
774 | return NULL; |
775 | } | |
a541c325 RD |
776 | PyList_Append(pylist, s); |
777 | i += PyString_Size(s); | |
f74ff5ef RD |
778 | } |
779 | ||
780 | // error check | |
781 | if (m_wxis->LastError() == wxSTREAM_READ_ERROR) { | |
a541c325 | 782 | Py_DECREF(pylist); |
f74ff5ef RD |
783 | PyErr_SetString(PyExc_IOError,"IOError in wxInputStream"); |
784 | return NULL; | |
785 | } | |
a541c325 RD |
786 | |
787 | return pylist; | |
f74ff5ef RD |
788 | } |
789 | ||
790 | ||
791 | void wxPyInputStream::seek(int offset, int whence) { | |
792 | if (m_wxis) | |
793 | m_wxis->SeekI(offset, wxSeekMode(whence)); | |
794 | } | |
795 | ||
796 | int wxPyInputStream::tell(){ | |
797 | if (m_wxis) | |
798 | return m_wxis->TellI(); | |
799 | else return 0; | |
800 | } | |
801 | ||
802 | ||
803 | ||
804 | ||
805 | wxPyCBInputStream::wxPyCBInputStream(PyObject *r, PyObject *s, PyObject *t, bool block) | |
806 | : wxInputStream(), m_read(r), m_seek(s), m_tell(t), m_block(block) | |
807 | {} | |
808 | ||
809 | ||
810 | wxPyCBInputStream::~wxPyCBInputStream() { | |
811 | if (m_block) wxPyBeginBlockThreads(); | |
812 | Py_XDECREF(m_read); | |
813 | Py_XDECREF(m_seek); | |
814 | Py_XDECREF(m_tell); | |
815 | if (m_block) wxPyEndBlockThreads(); | |
816 | } | |
817 | ||
818 | ||
819 | wxPyCBInputStream* wxPyCBInputStream::create(PyObject *py, bool block) { | |
820 | if (block) wxPyBeginBlockThreads(); | |
821 | ||
822 | PyObject* read = getMethod(py, "read"); | |
823 | PyObject* seek = getMethod(py, "seek"); | |
824 | PyObject* tell = getMethod(py, "tell"); | |
825 | ||
826 | if (!read) { | |
827 | PyErr_SetString(PyExc_TypeError, "Not a file-like object"); | |
828 | Py_XDECREF(read); | |
829 | Py_XDECREF(seek); | |
830 | Py_XDECREF(tell); | |
831 | if (block) wxPyEndBlockThreads(); | |
832 | return NULL; | |
833 | } | |
834 | ||
835 | if (block) wxPyEndBlockThreads(); | |
836 | return new wxPyCBInputStream(read, seek, tell, block); | |
837 | } | |
838 | ||
249a57f4 RD |
839 | |
840 | wxPyCBInputStream* wxPyCBInputStream_create(PyObject *py, bool block) { | |
841 | return wxPyCBInputStream::create(py, block); | |
842 | } | |
843 | ||
f74ff5ef RD |
844 | PyObject* wxPyCBInputStream::getMethod(PyObject* py, char* name) { |
845 | if (!PyObject_HasAttrString(py, name)) | |
846 | return NULL; | |
847 | PyObject* o = PyObject_GetAttrString(py, name); | |
848 | if (!PyMethod_Check(o) && !PyCFunction_Check(o)) { | |
849 | Py_DECREF(o); | |
850 | return NULL; | |
851 | } | |
852 | return o; | |
853 | } | |
854 | ||
855 | ||
856 | size_t wxPyCBInputStream::GetSize() const { | |
857 | wxPyCBInputStream* self = (wxPyCBInputStream*)this; // cast off const | |
858 | if (m_seek && m_tell) { | |
859 | off_t temp = self->OnSysTell(); | |
860 | off_t ret = self->OnSysSeek(0, wxFromEnd); | |
861 | self->OnSysSeek(temp, wxFromStart); | |
862 | return ret; | |
863 | } | |
864 | else | |
865 | return 0; | |
866 | } | |
867 | ||
868 | ||
869 | size_t wxPyCBInputStream::OnSysRead(void *buffer, size_t bufsize) { | |
870 | if (bufsize == 0) | |
871 | return 0; | |
872 | ||
873 | wxPyBeginBlockThreads(); | |
874 | PyObject* arglist = Py_BuildValue("(i)", bufsize); | |
875 | PyObject* result = PyEval_CallObject(m_read, arglist); | |
876 | Py_DECREF(arglist); | |
877 | ||
878 | size_t o = 0; | |
a541c325 | 879 | if ((result != NULL) && PyString_Check(result)) { |
f74ff5ef RD |
880 | o = PyString_Size(result); |
881 | if (o == 0) | |
882 | m_lasterror = wxSTREAM_EOF; | |
883 | if (o > bufsize) | |
884 | o = bufsize; | |
a541c325 | 885 | memcpy((char*)buffer, PyString_AsString(result), o); // strings only, not unicode... |
f74ff5ef RD |
886 | Py_DECREF(result); |
887 | ||
888 | } | |
889 | else | |
890 | m_lasterror = wxSTREAM_READ_ERROR; | |
891 | wxPyEndBlockThreads(); | |
892 | m_lastcount = o; | |
893 | return o; | |
894 | } | |
895 | ||
896 | size_t wxPyCBInputStream::OnSysWrite(const void *buffer, size_t bufsize) { | |
897 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
898 | return 0; | |
899 | } | |
900 | ||
901 | off_t wxPyCBInputStream::OnSysSeek(off_t off, wxSeekMode mode) { | |
902 | wxPyBeginBlockThreads(); | |
903 | PyObject* arglist = Py_BuildValue("(ii)", off, mode); | |
904 | PyObject* result = PyEval_CallObject(m_seek, arglist); | |
905 | Py_DECREF(arglist); | |
906 | Py_XDECREF(result); | |
907 | wxPyEndBlockThreads(); | |
908 | return OnSysTell(); | |
909 | } | |
910 | ||
911 | off_t wxPyCBInputStream::OnSysTell() const { | |
912 | wxPyBeginBlockThreads(); | |
913 | PyObject* arglist = Py_BuildValue("()"); | |
914 | PyObject* result = PyEval_CallObject(m_tell, arglist); | |
915 | Py_DECREF(arglist); | |
916 | off_t o = 0; | |
917 | if (result != NULL) { | |
918 | o = PyInt_AsLong(result); | |
919 | Py_DECREF(result); | |
920 | }; | |
921 | wxPyEndBlockThreads(); | |
922 | return o; | |
923 | } | |
924 | ||
925 | //---------------------------------------------------------------------- | |
d559219f | 926 | |
2f90df85 RD |
927 | IMPLEMENT_ABSTRACT_CLASS(wxPyCallback, wxObject); |
928 | ||
d559219f RD |
929 | wxPyCallback::wxPyCallback(PyObject* func) { |
930 | m_func = func; | |
931 | Py_INCREF(m_func); | |
932 | } | |
933 | ||
2f90df85 RD |
934 | wxPyCallback::wxPyCallback(const wxPyCallback& other) { |
935 | m_func = other.m_func; | |
936 | Py_INCREF(m_func); | |
937 | } | |
938 | ||
d559219f | 939 | wxPyCallback::~wxPyCallback() { |
4268f798 | 940 | wxPyBeginBlockThreads(); |
d559219f | 941 | Py_DECREF(m_func); |
4268f798 | 942 | wxPyEndBlockThreads(); |
d559219f RD |
943 | } |
944 | ||
cf694132 RD |
945 | |
946 | ||
7bf85405 RD |
947 | // This function is used for all events destined for Python event handlers. |
948 | void wxPyCallback::EventThunker(wxEvent& event) { | |
949 | wxPyCallback* cb = (wxPyCallback*)event.m_callbackUserData; | |
950 | PyObject* func = cb->m_func; | |
951 | PyObject* result; | |
952 | PyObject* arg; | |
953 | PyObject* tuple; | |
954 | ||
cf694132 | 955 | |
4268f798 | 956 | wxPyBeginBlockThreads(); |
65dd82cb RD |
957 | wxString className = event.GetClassInfo()->GetClassName(); |
958 | ||
959 | if (className == "wxPyEvent") | |
960 | arg = ((wxPyEvent*)&event)->GetSelf(); | |
961 | else if (className == "wxPyCommandEvent") | |
962 | arg = ((wxPyCommandEvent*)&event)->GetSelf(); | |
c8bc7bb8 | 963 | else { |
a541c325 | 964 | arg = wxPyConstructObject((void*)&event, className); |
c8bc7bb8 | 965 | } |
7bf85405 RD |
966 | |
967 | tuple = PyTuple_New(1); | |
968 | PyTuple_SET_ITEM(tuple, 0, arg); | |
969 | result = PyEval_CallObject(func, tuple); | |
7bf85405 RD |
970 | Py_DECREF(tuple); |
971 | if (result) { | |
972 | Py_DECREF(result); | |
ac346f50 | 973 | PyErr_Clear(); // Just in case... |
7bf85405 RD |
974 | } else { |
975 | PyErr_Print(); | |
976 | } | |
4268f798 | 977 | wxPyEndBlockThreads(); |
7bf85405 RD |
978 | } |
979 | ||
980 | ||
d559219f | 981 | //---------------------------------------------------------------------- |
7bf85405 | 982 | |
2f90df85 RD |
983 | wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper& other) { |
984 | m_lastFound = NULL; | |
985 | m_self = other.m_self; | |
f6bcfd97 BP |
986 | m_class = other.m_class; |
987 | if (m_self) { | |
2f90df85 | 988 | Py_INCREF(m_self); |
f6bcfd97 BP |
989 | Py_INCREF(m_class); |
990 | } | |
2f90df85 RD |
991 | } |
992 | ||
993 | ||
33510773 | 994 | void wxPyCallbackHelper::setSelf(PyObject* self, PyObject* klass, int incref) { |
d559219f | 995 | m_self = self; |
33510773 | 996 | m_class = klass; |
b7312675 | 997 | m_incRef = incref; |
f6bcfd97 | 998 | if (incref) { |
2f90df85 | 999 | Py_INCREF(m_self); |
f6bcfd97 BP |
1000 | Py_INCREF(m_class); |
1001 | } | |
d559219f | 1002 | } |
8bf5d46e | 1003 | |
8bf5d46e | 1004 | |
78b57918 RD |
1005 | #if PYTHON_API_VERSION >= 1011 |
1006 | ||
1007 | // Prior to Python 2.2 PyMethod_GetClass returned the class object | |
1008 | // in which the method was defined. Starting with 2.2 it returns | |
1009 | // "class that asked for the method" which seems totally bogus to me | |
4152e8b9 | 1010 | // but apprently it fixes some obscure problem waiting to happen in |
78b57918 RD |
1011 | // Python. Since the API was not documented Guido and the gang felt |
1012 | // safe in changing it. Needless to say that totally screwed up the | |
1013 | // logic below in wxPyCallbackHelper::findCallback, hence this icky | |
4152e8b9 | 1014 | // code to find the class where the method is actually defined... |
78b57918 RD |
1015 | |
1016 | static | |
1017 | PyObject* PyFindClassWithAttr(PyObject *klass, PyObject *name) | |
1018 | { | |
1019 | int i, n; | |
1020 | ||
1021 | if (PyType_Check(klass)) { // new style classes | |
1022 | // This code is borrowed/adapted from _PyType_Lookup in typeobject.c | |
4a98c806 | 1023 | // (TODO: This part is not tested yet, so I'm not sure it is correct...) |
78b57918 RD |
1024 | PyTypeObject* type = (PyTypeObject*)klass; |
1025 | PyObject *mro, *res, *base, *dict; | |
1026 | /* Look in tp_dict of types in MRO */ | |
1027 | mro = type->tp_mro; | |
1028 | assert(PyTuple_Check(mro)); | |
1029 | n = PyTuple_GET_SIZE(mro); | |
1030 | for (i = 0; i < n; i++) { | |
1031 | base = PyTuple_GET_ITEM(mro, i); | |
1032 | if (PyClass_Check(base)) | |
1033 | dict = ((PyClassObject *)base)->cl_dict; | |
1034 | else { | |
1035 | assert(PyType_Check(base)); | |
1036 | dict = ((PyTypeObject *)base)->tp_dict; | |
1037 | } | |
1038 | assert(dict && PyDict_Check(dict)); | |
1039 | res = PyDict_GetItem(dict, name); | |
1040 | if (res != NULL) | |
4a98c806 | 1041 | return base; |
78b57918 RD |
1042 | } |
1043 | return NULL; | |
1044 | } | |
1045 | ||
1046 | else if (PyClass_Check(klass)) { // old style classes | |
1047 | // This code is borrowed/adapted from class_lookup in classobject.c | |
1048 | PyClassObject* cp = (PyClassObject*)klass; | |
1049 | PyObject *value = PyDict_GetItem(cp->cl_dict, name); | |
1050 | if (value != NULL) { | |
1051 | return (PyObject*)cp; | |
1052 | } | |
1053 | n = PyTuple_Size(cp->cl_bases); | |
1054 | for (i = 0; i < n; i++) { | |
1055 | PyObject* base = PyTuple_GetItem(cp->cl_bases, i); | |
1056 | PyObject *v = PyFindClassWithAttr(base, name); | |
1057 | if (v != NULL) | |
1058 | return v; | |
1059 | } | |
1060 | return NULL; | |
1061 | } | |
4152e8b9 | 1062 | return NULL; |
78b57918 RD |
1063 | } |
1064 | #endif | |
1065 | ||
1066 | ||
1067 | static | |
1068 | PyObject* PyMethod_GetDefiningClass(PyObject* method, const char* name) | |
1069 | { | |
1070 | PyObject* mgc = PyMethod_GET_CLASS(method); | |
1071 | ||
1072 | #if PYTHON_API_VERSION <= 1010 // prior to Python 2.2, the easy way | |
1073 | return mgc; | |
1074 | #else // 2.2 and after, the hard way... | |
1075 | ||
1076 | PyObject* nameo = PyString_FromString(name); | |
1077 | PyObject* klass = PyFindClassWithAttr(mgc, nameo); | |
1078 | Py_DECREF(nameo); | |
1079 | return klass; | |
1080 | #endif | |
1081 | } | |
1082 | ||
1083 | ||
1084 | ||
de20db99 | 1085 | bool wxPyCallbackHelper::findCallback(const char* name) const { |
f6bcfd97 BP |
1086 | wxPyCallbackHelper* self = (wxPyCallbackHelper*)this; // cast away const |
1087 | self->m_lastFound = NULL; | |
78b57918 RD |
1088 | |
1089 | // If the object (m_self) has an attibute of the given name... | |
de20db99 | 1090 | if (m_self && PyObject_HasAttrString(m_self, (char*)name)) { |
78b57918 | 1091 | PyObject *method, *klass; |
de20db99 | 1092 | method = PyObject_GetAttrString(m_self, (char*)name); |
f6bcfd97 | 1093 | |
78b57918 RD |
1094 | // ...and if that attribute is a method, and if that method's class is |
1095 | // not from a base class... | |
f6bcfd97 | 1096 | if (PyMethod_Check(method) && |
78b57918 RD |
1097 | (klass = PyMethod_GetDefiningClass(method, (char*)name)) != NULL && |
1098 | ((klass == m_class) || PyClass_IsSubclass(klass, m_class))) { | |
8bf5d46e | 1099 | |
78b57918 | 1100 | // ...then we'll save a pointer to the method so callCallback can call it. |
f6bcfd97 BP |
1101 | self->m_lastFound = method; |
1102 | } | |
de20db99 RD |
1103 | else { |
1104 | Py_DECREF(method); | |
1105 | } | |
f6bcfd97 | 1106 | } |
d559219f RD |
1107 | return m_lastFound != NULL; |
1108 | } | |
8bf5d46e | 1109 | |
d559219f | 1110 | |
f6bcfd97 | 1111 | int wxPyCallbackHelper::callCallback(PyObject* argTuple) const { |
d559219f RD |
1112 | PyObject* result; |
1113 | int retval = FALSE; | |
1114 | ||
1115 | result = callCallbackObj(argTuple); | |
1116 | if (result) { // Assumes an integer return type... | |
1117 | retval = PyInt_AsLong(result); | |
1118 | Py_DECREF(result); | |
1119 | PyErr_Clear(); // forget about it if it's not... | |
1120 | } | |
1121 | return retval; | |
1122 | } | |
1123 | ||
1124 | // Invoke the Python callable object, returning the raw PyObject return | |
1125 | // value. Caller should DECREF the return value and also call PyEval_SaveThread. | |
f6bcfd97 | 1126 | PyObject* wxPyCallbackHelper::callCallbackObj(PyObject* argTuple) const { |
de20db99 | 1127 | PyObject* result; |
d559219f | 1128 | |
de20db99 RD |
1129 | // Save a copy of the pointer in case the callback generates another |
1130 | // callback. In that case m_lastFound will have a different value when | |
1131 | // it gets back here... | |
1132 | PyObject* method = m_lastFound; | |
1133 | ||
1134 | result = PyEval_CallObject(method, argTuple); | |
d559219f | 1135 | Py_DECREF(argTuple); |
de20db99 | 1136 | Py_DECREF(method); |
d559219f RD |
1137 | if (!result) { |
1138 | PyErr_Print(); | |
1139 | } | |
1140 | return result; | |
1141 | } | |
714e6a9e | 1142 | |
7bf85405 | 1143 | |
0122b7e3 | 1144 | void wxPyCBH_setCallbackInfo(wxPyCallbackHelper& cbh, PyObject* self, PyObject* klass, int incref) { |
1e7ecb7b RD |
1145 | cbh.setSelf(self, klass, incref); |
1146 | } | |
1147 | ||
1148 | bool wxPyCBH_findCallback(const wxPyCallbackHelper& cbh, const char* name) { | |
1149 | return cbh.findCallback(name); | |
1150 | } | |
1151 | ||
1152 | int wxPyCBH_callCallback(const wxPyCallbackHelper& cbh, PyObject* argTuple) { | |
1153 | return cbh.callCallback(argTuple); | |
1154 | } | |
1155 | ||
1156 | PyObject* wxPyCBH_callCallbackObj(const wxPyCallbackHelper& cbh, PyObject* argTuple) { | |
1157 | return cbh.callCallbackObj(argTuple); | |
1158 | } | |
1159 | ||
1160 | ||
1161 | void wxPyCBH_delete(wxPyCallbackHelper* cbh) { | |
1e7ecb7b | 1162 | if (cbh->m_incRef) { |
4268f798 | 1163 | wxPyBeginBlockThreads(); |
1e7ecb7b RD |
1164 | Py_XDECREF(cbh->m_self); |
1165 | Py_XDECREF(cbh->m_class); | |
4268f798 | 1166 | wxPyEndBlockThreads(); |
1e7ecb7b | 1167 | } |
1e7ecb7b | 1168 | } |
d559219f | 1169 | |
65dd82cb RD |
1170 | //--------------------------------------------------------------------------- |
1171 | //--------------------------------------------------------------------------- | |
2aab8f16 | 1172 | // These event classes can be derived from in Python and passed through the event |
c368d904 | 1173 | // system without losing anything. They do this by keeping a reference to |
65dd82cb RD |
1174 | // themselves and some special case handling in wxPyCallback::EventThunker. |
1175 | ||
1176 | ||
e19b7164 | 1177 | wxPyEvtSelfRef::wxPyEvtSelfRef() { |
65dd82cb RD |
1178 | //m_self = Py_None; // **** We don't do normal ref counting to prevent |
1179 | //Py_INCREF(m_self); // circular loops... | |
194fa2ac | 1180 | m_cloned = FALSE; |
65dd82cb RD |
1181 | } |
1182 | ||
e19b7164 | 1183 | wxPyEvtSelfRef::~wxPyEvtSelfRef() { |
4268f798 | 1184 | wxPyBeginBlockThreads(); |
65dd82cb RD |
1185 | if (m_cloned) |
1186 | Py_DECREF(m_self); | |
4268f798 | 1187 | wxPyEndBlockThreads(); |
65dd82cb RD |
1188 | } |
1189 | ||
e19b7164 | 1190 | void wxPyEvtSelfRef::SetSelf(PyObject* self, bool clone) { |
4268f798 | 1191 | wxPyBeginBlockThreads(); |
65dd82cb RD |
1192 | if (m_cloned) |
1193 | Py_DECREF(m_self); | |
1194 | m_self = self; | |
1195 | if (clone) { | |
1196 | Py_INCREF(m_self); | |
1197 | m_cloned = TRUE; | |
1198 | } | |
4268f798 | 1199 | wxPyEndBlockThreads(); |
65dd82cb RD |
1200 | } |
1201 | ||
e19b7164 | 1202 | PyObject* wxPyEvtSelfRef::GetSelf() const { |
65dd82cb RD |
1203 | Py_INCREF(m_self); |
1204 | return m_self; | |
1205 | } | |
1206 | ||
1207 | ||
07b2e1cd RD |
1208 | IMPLEMENT_ABSTRACT_CLASS(wxPyEvent, wxEvent); |
1209 | IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent, wxCommandEvent); | |
1210 | ||
1211 | ||
65dd82cb RD |
1212 | wxPyEvent::wxPyEvent(int id) |
1213 | : wxEvent(id) { | |
1214 | } | |
1215 | ||
65dd82cb | 1216 | |
07b2e1cd RD |
1217 | wxPyEvent::wxPyEvent(const wxPyEvent& evt) |
1218 | : wxEvent(evt) | |
1219 | { | |
1220 | SetSelf(evt.m_self, TRUE); | |
65dd82cb RD |
1221 | } |
1222 | ||
1223 | ||
07b2e1cd RD |
1224 | wxPyEvent::~wxPyEvent() { |
1225 | } | |
65dd82cb RD |
1226 | |
1227 | ||
1228 | wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType, int id) | |
1229 | : wxCommandEvent(commandType, id) { | |
1230 | } | |
1231 | ||
65dd82cb | 1232 | |
07b2e1cd RD |
1233 | wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent& evt) |
1234 | : wxCommandEvent(evt) | |
1235 | { | |
1236 | SetSelf(evt.m_self, TRUE); | |
65dd82cb RD |
1237 | } |
1238 | ||
1239 | ||
07b2e1cd RD |
1240 | wxPyCommandEvent::~wxPyCommandEvent() { |
1241 | } | |
1242 | ||
65dd82cb RD |
1243 | |
1244 | ||
1245 | ||
d559219f | 1246 | //--------------------------------------------------------------------------- |
7bf85405 RD |
1247 | //--------------------------------------------------------------------------- |
1248 | ||
d559219f | 1249 | |
7bf85405 RD |
1250 | wxPyTimer::wxPyTimer(PyObject* callback) { |
1251 | func = callback; | |
1252 | Py_INCREF(func); | |
1253 | } | |
1254 | ||
1255 | wxPyTimer::~wxPyTimer() { | |
4268f798 | 1256 | wxPyBeginBlockThreads(); |
7bf85405 | 1257 | Py_DECREF(func); |
4268f798 | 1258 | wxPyEndBlockThreads(); |
7bf85405 RD |
1259 | } |
1260 | ||
1261 | void wxPyTimer::Notify() { | |
185d7c3e RD |
1262 | if (!func || func == Py_None) { |
1263 | wxTimer::Notify(); | |
1264 | } | |
1265 | else { | |
4268f798 | 1266 | wxPyBeginBlockThreads(); |
185d7c3e RD |
1267 | |
1268 | PyObject* result; | |
1269 | PyObject* args = Py_BuildValue("()"); | |
1270 | ||
1271 | result = PyEval_CallObject(func, args); | |
1272 | Py_DECREF(args); | |
1273 | if (result) { | |
1274 | Py_DECREF(result); | |
1275 | PyErr_Clear(); | |
1276 | } else { | |
1277 | PyErr_Print(); | |
1278 | } | |
7bf85405 | 1279 | |
4268f798 | 1280 | wxPyEndBlockThreads(); |
7bf85405 RD |
1281 | } |
1282 | } | |
1283 | ||
1284 | ||
cf694132 | 1285 | |
2f90df85 | 1286 | //--------------------------------------------------------------------------- |
389c5527 RD |
1287 | //--------------------------------------------------------------------------- |
1288 | // Convert a wxList to a Python List | |
1289 | ||
65dd82cb | 1290 | PyObject* wxPy_ConvertList(wxListBase* list, const char* className) { |
389c5527 RD |
1291 | PyObject* pyList; |
1292 | PyObject* pyObj; | |
1293 | wxObject* wxObj; | |
1294 | wxNode* node = list->First(); | |
1295 | ||
4268f798 | 1296 | wxPyBeginBlockThreads(); |
389c5527 RD |
1297 | pyList = PyList_New(0); |
1298 | while (node) { | |
1299 | wxObj = node->Data(); | |
9416aa89 | 1300 | pyObj = wxPyMake_wxObject(wxObj); //wxPyConstructObject(wxObj, className); |
389c5527 RD |
1301 | PyList_Append(pyList, pyObj); |
1302 | node = node->Next(); | |
1303 | } | |
4268f798 | 1304 | wxPyEndBlockThreads(); |
389c5527 RD |
1305 | return pyList; |
1306 | } | |
1307 | ||
54b96882 RD |
1308 | //---------------------------------------------------------------------- |
1309 | ||
1310 | long wxPyGetWinHandle(wxWindow* win) { | |
1311 | #ifdef __WXMSW__ | |
1312 | return (long)win->GetHandle(); | |
1313 | #endif | |
1314 | ||
1315 | // Find and return the actual X-Window. | |
1316 | #ifdef __WXGTK__ | |
1317 | if (win->m_wxwindow) { | |
1318 | GdkWindowPrivate* bwin = (GdkWindowPrivate*)GTK_PIZZA(win->m_wxwindow)->bin_window; | |
1319 | if (bwin) { | |
1320 | return (long)bwin->xwindow; | |
1321 | } | |
1322 | } | |
1323 | #endif | |
1324 | return 0; | |
1325 | } | |
1326 | ||
7bf85405 RD |
1327 | //---------------------------------------------------------------------- |
1328 | // Some helper functions for typemaps in my_typemaps.i, so they won't be | |
c8bc7bb8 RD |
1329 | // included in every file over and over again... |
1330 | ||
1331 | #if PYTHON_API_VERSION >= 1009 | |
1332 | static char* wxStringErrorMsg = "String or Unicode type required"; | |
1333 | #else | |
1334 | static char* wxStringErrorMsg = "String type required"; | |
1335 | #endif | |
1336 | ||
1337 | ||
1338 | wxString* wxString_in_helper(PyObject* source) { | |
1339 | wxString* target; | |
1340 | #if PYTHON_API_VERSION >= 1009 // Have Python unicode API | |
1341 | if (!PyString_Check(source) && !PyUnicode_Check(source)) { | |
1342 | PyErr_SetString(PyExc_TypeError, wxStringErrorMsg); | |
1343 | return NULL; | |
1344 | } | |
1345 | #if wxUSE_UNICODE | |
1346 | if (PyUnicode_Check(source)) { | |
1347 | target = new wxString(PyUnicode_AS_UNICODE(source)); | |
1348 | } else { | |
a541c325 RD |
1349 | // It is a string, get pointers to it and transform to unicode |
1350 | char* tmpPtr; int tmpSize; | |
1351 | PyString_AsStringAndSize(source, &tmpPtr, &tmpSize); | |
1352 | target = new wxString(tmpPtr, *wxConvCurrent, tmpSize); | |
c8bc7bb8 RD |
1353 | } |
1354 | #else | |
1355 | char* tmpPtr; int tmpSize; | |
1356 | if (PyString_AsStringAndSize(source, &tmpPtr, &tmpSize) == -1) { | |
1357 | PyErr_SetString(PyExc_TypeError, "Unable to convert string"); | |
1358 | return NULL; | |
1359 | } | |
1360 | target = new wxString(tmpPtr, tmpSize); | |
1361 | #endif // wxUSE_UNICODE | |
1362 | ||
1363 | #else // No Python unicode API (1.5.2) | |
1364 | if (!PyString_Check(source)) { | |
1365 | PyErr_SetString(PyExc_TypeError, wxStringErrorMsg); | |
1366 | return NULL; | |
1367 | } | |
1368 | target = new wxString(PyString_AS_STRING(source), PyString_GET_SIZE(source)); | |
1369 | #endif | |
1370 | return target; | |
1371 | } | |
1372 | ||
7bf85405 | 1373 | |
a541c325 RD |
1374 | // Similar to above except doesn't use "new" and doesn't set an exception |
1375 | wxString Py2wxString(PyObject* source) | |
1376 | { | |
1377 | wxString target; | |
1378 | bool doDecRef = FALSE; | |
1379 | ||
1380 | #if PYTHON_API_VERSION >= 1009 // Have Python unicode API | |
1381 | if (!PyString_Check(source) && !PyUnicode_Check(source)) { | |
1382 | // Convert to String if not one already... (TODO: Unicode too?) | |
1383 | source = PyObject_Str(source); | |
1384 | doDecRef = TRUE; | |
1385 | } | |
1386 | ||
1387 | #if wxUSE_UNICODE | |
1388 | if (PyUnicode_Check(source)) { | |
1389 | target = PyUnicode_AS_UNICODE(source); | |
1390 | } else { | |
1391 | // It is a string, get pointers to it and transform to unicode | |
1392 | char* tmpPtr; int tmpSize; | |
1393 | PyString_AsStringAndSize(source, &tmpPtr, &tmpSize); | |
1394 | target = wxString(tmpPtr, *wxConvCurrent, tmpSize); | |
1395 | } | |
1396 | #else | |
1397 | char* tmpPtr; int tmpSize; | |
1398 | PyString_AsStringAndSize(source, &tmpPtr, &tmpSize); | |
1399 | target = wxString(tmpPtr, tmpSize); | |
1400 | #endif // wxUSE_UNICODE | |
1401 | ||
1402 | #else // No Python unicode API (1.5.2) | |
1403 | if (!PyString_Check(source)) { | |
1404 | // Convert to String if not one already... | |
1405 | source = PyObject_Str(source); | |
1406 | doDecRef = TRUE; | |
1407 | } | |
1408 | target = wxString(PyString_AS_STRING(source), PyString_GET_SIZE(source)); | |
1409 | #endif | |
1410 | ||
1411 | if (doDecRef) | |
1412 | Py_DECREF(source); | |
1413 | return target; | |
1414 | } | |
1415 | ||
1416 | ||
1417 | // Make either a Python String or Unicode object, depending on build mode | |
1418 | PyObject* wx2PyString(const wxString& src) | |
1419 | { | |
1420 | PyObject* str; | |
1421 | #if wxUSE_UNICODE | |
1422 | str = PyUnicode_FromUnicode(src.c_str(), src.Len()); | |
1423 | #else | |
1424 | str = PyString_FromStringAndSize(src.c_str(), src.Len()); | |
1425 | #endif | |
1426 | return str; | |
1427 | } | |
1428 | ||
1429 | ||
1430 | //---------------------------------------------------------------------- | |
1431 | ||
7bf85405 | 1432 | |
2f90df85 | 1433 | byte* byte_LIST_helper(PyObject* source) { |
b639c3c5 RD |
1434 | if (!PyList_Check(source)) { |
1435 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
1436 | return NULL; | |
1437 | } | |
1438 | int count = PyList_Size(source); | |
1439 | byte* temp = new byte[count]; | |
1440 | if (! temp) { | |
1441 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
1442 | return NULL; | |
1443 | } | |
1444 | for (int x=0; x<count; x++) { | |
1445 | PyObject* o = PyList_GetItem(source, x); | |
1446 | if (! PyInt_Check(o)) { | |
1447 | PyErr_SetString(PyExc_TypeError, "Expected a list of integers."); | |
1448 | return NULL; | |
1449 | } | |
1450 | temp[x] = (byte)PyInt_AsLong(o); | |
1451 | } | |
1452 | return temp; | |
1453 | } | |
1454 | ||
1455 | ||
2f90df85 | 1456 | int* int_LIST_helper(PyObject* source) { |
7bf85405 RD |
1457 | if (!PyList_Check(source)) { |
1458 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
1459 | return NULL; | |
1460 | } | |
1461 | int count = PyList_Size(source); | |
1462 | int* temp = new int[count]; | |
1463 | if (! temp) { | |
1464 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
1465 | return NULL; | |
1466 | } | |
1467 | for (int x=0; x<count; x++) { | |
1468 | PyObject* o = PyList_GetItem(source, x); | |
1469 | if (! PyInt_Check(o)) { | |
1470 | PyErr_SetString(PyExc_TypeError, "Expected a list of integers."); | |
1471 | return NULL; | |
1472 | } | |
1473 | temp[x] = PyInt_AsLong(o); | |
1474 | } | |
1475 | return temp; | |
1476 | } | |
1477 | ||
1478 | ||
2f90df85 | 1479 | long* long_LIST_helper(PyObject* source) { |
7bf85405 RD |
1480 | if (!PyList_Check(source)) { |
1481 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
1482 | return NULL; | |
1483 | } | |
1484 | int count = PyList_Size(source); | |
1485 | long* temp = new long[count]; | |
1486 | if (! temp) { | |
1487 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
1488 | return NULL; | |
1489 | } | |
1490 | for (int x=0; x<count; x++) { | |
1491 | PyObject* o = PyList_GetItem(source, x); | |
1492 | if (! PyInt_Check(o)) { | |
1493 | PyErr_SetString(PyExc_TypeError, "Expected a list of integers."); | |
1494 | return NULL; | |
1495 | } | |
1496 | temp[x] = PyInt_AsLong(o); | |
1497 | } | |
1498 | return temp; | |
1499 | } | |
1500 | ||
1501 | ||
2f90df85 | 1502 | char** string_LIST_helper(PyObject* source) { |
7bf85405 RD |
1503 | if (!PyList_Check(source)) { |
1504 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
1505 | return NULL; | |
1506 | } | |
1507 | int count = PyList_Size(source); | |
1508 | char** temp = new char*[count]; | |
1509 | if (! temp) { | |
1510 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
1511 | return NULL; | |
1512 | } | |
1513 | for (int x=0; x<count; x++) { | |
1514 | PyObject* o = PyList_GetItem(source, x); | |
1515 | if (! PyString_Check(o)) { | |
1516 | PyErr_SetString(PyExc_TypeError, "Expected a list of strings."); | |
1517 | return NULL; | |
1518 | } | |
1519 | temp[x] = PyString_AsString(o); | |
1520 | } | |
1521 | return temp; | |
1522 | } | |
1523 | ||
e0672e2f RD |
1524 | //-------------------------------- |
1525 | // Part of patch from Tim Hochberg | |
1526 | static inline bool wxPointFromObjects(PyObject* o1, PyObject* o2, wxPoint* point) { | |
1527 | if (PyInt_Check(o1) && PyInt_Check(o2)) { | |
1528 | point->x = PyInt_AS_LONG(o1); | |
1529 | point->y = PyInt_AS_LONG(o2); | |
1530 | return true; | |
1531 | } | |
1532 | if (PyFloat_Check(o1) && PyFloat_Check(o2)) { | |
1533 | point->x = (int)PyFloat_AS_DOUBLE(o1); | |
1534 | point->y = (int)PyFloat_AS_DOUBLE(o2); | |
1535 | return true; | |
1536 | } | |
1537 | if (PyInstance_Check(o1) || PyInstance_Check(o2)) { | |
1538 | // Disallow instances because they can cause havok | |
1539 | return false; | |
1540 | } | |
1541 | if (PyNumber_Check(o1) && PyNumber_Check(o2)) { | |
1542 | // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2 | |
1543 | point->x = PyInt_AsLong(o1); | |
1544 | point->y = PyInt_AsLong(o2); | |
1545 | return true; | |
1546 | } | |
1547 | return false; | |
1548 | } | |
7bf85405 RD |
1549 | |
1550 | ||
e0672e2f RD |
1551 | wxPoint* wxPoint_LIST_helper(PyObject* source, int *count) { |
1552 | // Putting all of the declarations here allows | |
1553 | // us to put the error handling all in one place. | |
1554 | int x; | |
1555 | wxPoint* temp; | |
1556 | PyObject *o, *o1, *o2; | |
9d37f964 | 1557 | bool isFast = PyList_Check(source) || PyTuple_Check(source); |
e0672e2f | 1558 | |
e0672e2f RD |
1559 | if (!PySequence_Check(source)) { |
1560 | goto error0; | |
7bf85405 | 1561 | } |
9d37f964 RD |
1562 | |
1563 | // The length of the sequence is returned in count. | |
e0672e2f RD |
1564 | *count = PySequence_Length(source); |
1565 | if (*count < 0) { | |
1566 | goto error0; | |
1567 | } | |
1568 | ||
1569 | temp = new wxPoint[*count]; | |
1570 | if (!temp) { | |
7bf85405 RD |
1571 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); |
1572 | return NULL; | |
1573 | } | |
e0672e2f RD |
1574 | for (x=0; x<*count; x++) { |
1575 | // Get an item: try fast way first. | |
1576 | if (isFast) { | |
1577 | o = PySequence_Fast_GET_ITEM(source, x); | |
1578 | } | |
1579 | else { | |
1580 | o = PySequence_GetItem(source, x); | |
1581 | if (o == NULL) { | |
1582 | goto error1; | |
1583 | } | |
1584 | } | |
7bf85405 | 1585 | |
e0672e2f RD |
1586 | // Convert o to wxPoint. |
1587 | if ((PyTuple_Check(o) && PyTuple_GET_SIZE(o) == 2) || | |
1588 | (PyList_Check(o) && PyList_GET_SIZE(o) == 2)) { | |
1589 | o1 = PySequence_Fast_GET_ITEM(o, 0); | |
1590 | o2 = PySequence_Fast_GET_ITEM(o, 1); | |
1591 | if (!wxPointFromObjects(o1, o2, &temp[x])) { | |
1592 | goto error2; | |
1593 | } | |
7bf85405 | 1594 | } |
d559219f RD |
1595 | else if (PyInstance_Check(o)) { |
1596 | wxPoint* pt; | |
e0672e2f RD |
1597 | if (SWIG_GetPtrObj(o, (void **)&pt, "_wxPoint_p")) { |
1598 | goto error2; | |
d559219f RD |
1599 | } |
1600 | temp[x] = *pt; | |
1601 | } | |
e0672e2f RD |
1602 | else if (PySequence_Check(o) && PySequence_Length(o) == 2) { |
1603 | o1 = PySequence_GetItem(o, 0); | |
1604 | o2 = PySequence_GetItem(o, 1); | |
1605 | if (!wxPointFromObjects(o1, o2, &temp[x])) { | |
1606 | goto error3; | |
1607 | } | |
1608 | Py_DECREF(o1); | |
1609 | Py_DECREF(o2); | |
1610 | } | |
7bf85405 | 1611 | else { |
e0672e2f | 1612 | goto error2; |
7bf85405 | 1613 | } |
e0672e2f RD |
1614 | // Clean up. |
1615 | if (!isFast) | |
1616 | Py_DECREF(o); | |
7bf85405 RD |
1617 | } |
1618 | return temp; | |
e0672e2f RD |
1619 | |
1620 | error3: | |
1621 | Py_DECREF(o1); | |
1622 | Py_DECREF(o2); | |
1623 | error2: | |
1624 | if (!isFast) | |
1625 | Py_DECREF(o); | |
1626 | error1: | |
1627 | delete temp; | |
1628 | error0: | |
1629 | PyErr_SetString(PyExc_TypeError, "Expected a sequence of length-2 sequences or wxPoints."); | |
1630 | return NULL; | |
7bf85405 | 1631 | } |
e0672e2f RD |
1632 | // end of patch |
1633 | //------------------------------ | |
7bf85405 RD |
1634 | |
1635 | ||
2f90df85 | 1636 | wxBitmap** wxBitmap_LIST_helper(PyObject* source) { |
7bf85405 RD |
1637 | if (!PyList_Check(source)) { |
1638 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
1639 | return NULL; | |
1640 | } | |
1641 | int count = PyList_Size(source); | |
1642 | wxBitmap** temp = new wxBitmap*[count]; | |
1643 | if (! temp) { | |
1644 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
1645 | return NULL; | |
1646 | } | |
1647 | for (int x=0; x<count; x++) { | |
1648 | PyObject* o = PyList_GetItem(source, x); | |
e166644c | 1649 | if (PyInstance_Check(o)) { |
7bf85405 | 1650 | wxBitmap* pt; |
e166644c | 1651 | if (SWIG_GetPtrObj(o, (void **) &pt,"_wxBitmap_p")) { |
7bf85405 RD |
1652 | PyErr_SetString(PyExc_TypeError,"Expected _wxBitmap_p."); |
1653 | return NULL; | |
1654 | } | |
1655 | temp[x] = pt; | |
1656 | } | |
1657 | else { | |
1658 | PyErr_SetString(PyExc_TypeError, "Expected a list of wxBitmaps."); | |
1659 | return NULL; | |
1660 | } | |
1661 | } | |
1662 | return temp; | |
1663 | } | |
1664 | ||
1665 | ||
1666 | ||
2f90df85 | 1667 | wxString* wxString_LIST_helper(PyObject* source) { |
7bf85405 RD |
1668 | if (!PyList_Check(source)) { |
1669 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
1670 | return NULL; | |
1671 | } | |
1672 | int count = PyList_Size(source); | |
1673 | wxString* temp = new wxString[count]; | |
1674 | if (! temp) { | |
1675 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
1676 | return NULL; | |
1677 | } | |
1678 | for (int x=0; x<count; x++) { | |
1679 | PyObject* o = PyList_GetItem(source, x); | |
ecc08ead RD |
1680 | #if PYTHON_API_VERSION >= 1009 |
1681 | if (! PyString_Check(o) && ! PyUnicode_Check(o)) { | |
1682 | PyErr_SetString(PyExc_TypeError, "Expected a list of string or unicode objects."); | |
1683 | return NULL; | |
1684 | } | |
ecc08ead | 1685 | #else |
7bf85405 RD |
1686 | if (! PyString_Check(o)) { |
1687 | PyErr_SetString(PyExc_TypeError, "Expected a list of strings."); | |
1688 | return NULL; | |
1689 | } | |
ecc08ead | 1690 | #endif |
a541c325 RD |
1691 | |
1692 | wxString* pStr = wxString_in_helper(o); | |
1693 | temp[x] = *pStr; | |
1694 | delete pStr; | |
7bf85405 RD |
1695 | } |
1696 | return temp; | |
1697 | } | |
1698 | ||
1699 | ||
2f90df85 | 1700 | wxAcceleratorEntry* wxAcceleratorEntry_LIST_helper(PyObject* source) { |
7bf85405 RD |
1701 | if (!PyList_Check(source)) { |
1702 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
1703 | return NULL; | |
1704 | } | |
1705 | int count = PyList_Size(source); | |
1706 | wxAcceleratorEntry* temp = new wxAcceleratorEntry[count]; | |
1707 | if (! temp) { | |
1708 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
1709 | return NULL; | |
1710 | } | |
1711 | for (int x=0; x<count; x++) { | |
1712 | PyObject* o = PyList_GetItem(source, x); | |
e166644c | 1713 | if (PyInstance_Check(o)) { |
7bf85405 | 1714 | wxAcceleratorEntry* ae; |
e166644c | 1715 | if (SWIG_GetPtrObj(o, (void **) &ae,"_wxAcceleratorEntry_p")) { |
7bf85405 RD |
1716 | PyErr_SetString(PyExc_TypeError,"Expected _wxAcceleratorEntry_p."); |
1717 | return NULL; | |
1718 | } | |
1719 | temp[x] = *ae; | |
1720 | } | |
1721 | else if (PyTuple_Check(o)) { | |
1722 | PyObject* o1 = PyTuple_GetItem(o, 0); | |
1723 | PyObject* o2 = PyTuple_GetItem(o, 1); | |
1724 | PyObject* o3 = PyTuple_GetItem(o, 2); | |
0adbc166 | 1725 | temp[x].Set(PyInt_AsLong(o1), PyInt_AsLong(o2), PyInt_AsLong(o3)); |
7bf85405 RD |
1726 | } |
1727 | else { | |
1728 | PyErr_SetString(PyExc_TypeError, "Expected a list of 3-tuples or wxAcceleratorEntry objects."); | |
1729 | return NULL; | |
1730 | } | |
1731 | } | |
1732 | return temp; | |
1733 | } | |
1734 | ||
bb0054cd | 1735 | |
9d37f964 RD |
1736 | wxPen** wxPen_LIST_helper(PyObject* source) { |
1737 | if (!PyList_Check(source)) { | |
1738 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
1739 | return NULL; | |
1740 | } | |
1741 | int count = PyList_Size(source); | |
1742 | wxPen** temp = new wxPen*[count]; | |
1743 | if (!temp) { | |
1744 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
1745 | return NULL; | |
1746 | } | |
1747 | for (int x=0; x<count; x++) { | |
1748 | PyObject* o = PyList_GetItem(source, x); | |
1749 | if (PyInstance_Check(o)) { | |
1750 | wxPen* pt; | |
1751 | if (SWIG_GetPtrObj(o, (void **) &pt,"_wxPen_p")) { | |
1752 | delete temp; | |
1753 | PyErr_SetString(PyExc_TypeError,"Expected _wxPen_p."); | |
1754 | return NULL; | |
1755 | } | |
1756 | temp[x] = pt; | |
1757 | } | |
1758 | else { | |
1759 | delete temp; | |
1760 | PyErr_SetString(PyExc_TypeError, "Expected a list of wxPens."); | |
1761 | return NULL; | |
1762 | } | |
1763 | } | |
1764 | return temp; | |
1765 | } | |
1766 | ||
1767 | ||
1768 | bool _2int_seq_helper(PyObject* source, int* i1, int* i2) { | |
1769 | bool isFast = PyList_Check(source) || PyTuple_Check(source); | |
1770 | PyObject *o1, *o2; | |
1771 | ||
1772 | if (!PySequence_Check(source) || PySequence_Length(source) != 2) | |
1773 | return FALSE; | |
1774 | ||
1775 | if (isFast) { | |
1776 | o1 = PySequence_Fast_GET_ITEM(source, 0); | |
1777 | o2 = PySequence_Fast_GET_ITEM(source, 1); | |
1778 | } | |
1779 | else { | |
1780 | o1 = PySequence_GetItem(source, 0); | |
1781 | o2 = PySequence_GetItem(source, 1); | |
1782 | } | |
1783 | ||
1784 | *i1 = PyInt_AsLong(o1); | |
1785 | *i2 = PyInt_AsLong(o2); | |
1786 | ||
1787 | if (! isFast) { | |
1788 | Py_DECREF(o1); | |
1789 | Py_DECREF(o2); | |
1790 | } | |
1791 | return TRUE; | |
1792 | } | |
1793 | ||
1794 | ||
1795 | bool _4int_seq_helper(PyObject* source, int* i1, int* i2, int* i3, int* i4) { | |
1796 | bool isFast = PyList_Check(source) || PyTuple_Check(source); | |
1797 | PyObject *o1, *o2, *o3, *o4; | |
1798 | ||
1799 | if (!PySequence_Check(source) || PySequence_Length(source) != 4) | |
1800 | return FALSE; | |
1801 | ||
1802 | if (isFast) { | |
1803 | o1 = PySequence_Fast_GET_ITEM(source, 0); | |
1804 | o2 = PySequence_Fast_GET_ITEM(source, 1); | |
1805 | o3 = PySequence_Fast_GET_ITEM(source, 2); | |
1806 | o4 = PySequence_Fast_GET_ITEM(source, 3); | |
1807 | } | |
1808 | else { | |
1809 | o1 = PySequence_GetItem(source, 0); | |
1810 | o2 = PySequence_GetItem(source, 1); | |
1811 | o3 = PySequence_GetItem(source, 2); | |
1812 | o4 = PySequence_GetItem(source, 3); | |
1813 | } | |
1814 | ||
1815 | *i1 = PyInt_AsLong(o1); | |
1816 | *i2 = PyInt_AsLong(o2); | |
1817 | *i3 = PyInt_AsLong(o3); | |
1818 | *i4 = PyInt_AsLong(o4); | |
1819 | ||
1820 | if (! isFast) { | |
1821 | Py_DECREF(o1); | |
1822 | Py_DECREF(o2); | |
1823 | Py_DECREF(o3); | |
1824 | Py_DECREF(o4); | |
1825 | } | |
1826 | return TRUE; | |
1827 | } | |
1828 | ||
bb0054cd RD |
1829 | |
1830 | //---------------------------------------------------------------------- | |
bb0054cd | 1831 | |
2f90df85 RD |
1832 | bool wxSize_helper(PyObject* source, wxSize** obj) { |
1833 | ||
1834 | // If source is an object instance then it may already be the right type | |
1835 | if (PyInstance_Check(source)) { | |
1836 | wxSize* ptr; | |
1837 | if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxSize_p")) | |
1838 | goto error; | |
1839 | *obj = ptr; | |
1840 | return TRUE; | |
1841 | } | |
1842 | // otherwise a 2-tuple of integers is expected | |
1843 | else if (PySequence_Check(source) && PyObject_Length(source) == 2) { | |
1844 | PyObject* o1 = PySequence_GetItem(source, 0); | |
1845 | PyObject* o2 = PySequence_GetItem(source, 1); | |
db0ff83e RD |
1846 | if (!PyNumber_Check(o1) || !PyNumber_Check(o2)) { |
1847 | Py_DECREF(o1); | |
1848 | Py_DECREF(o2); | |
1849 | goto error; | |
1850 | } | |
2f90df85 | 1851 | **obj = wxSize(PyInt_AsLong(o1), PyInt_AsLong(o2)); |
db0ff83e RD |
1852 | Py_DECREF(o1); |
1853 | Py_DECREF(o2); | |
2f90df85 RD |
1854 | return TRUE; |
1855 | } | |
1856 | ||
1857 | error: | |
1858 | PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of integers or a wxSize object."); | |
1859 | return FALSE; | |
1860 | } | |
1861 | ||
1862 | bool wxPoint_helper(PyObject* source, wxPoint** obj) { | |
1863 | ||
1864 | // If source is an object instance then it may already be the right type | |
1865 | if (PyInstance_Check(source)) { | |
1866 | wxPoint* ptr; | |
1867 | if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxPoint_p")) | |
1868 | goto error; | |
1869 | *obj = ptr; | |
1870 | return TRUE; | |
1871 | } | |
e0672e2f RD |
1872 | // otherwise a length-2 sequence of integers is expected |
1873 | if (PySequence_Check(source) && PySequence_Length(source) == 2) { | |
2f90df85 RD |
1874 | PyObject* o1 = PySequence_GetItem(source, 0); |
1875 | PyObject* o2 = PySequence_GetItem(source, 1); | |
db0ff83e RD |
1876 | // This should really check for integers, not numbers -- but that would break code. |
1877 | if (!PyNumber_Check(o1) || !PyNumber_Check(o2)) { | |
1878 | Py_DECREF(o1); | |
1879 | Py_DECREF(o2); | |
1880 | goto error; | |
1881 | } | |
1882 | **obj = wxPoint(PyInt_AsLong(o1), PyInt_AsLong(o2)); | |
1883 | Py_DECREF(o1); | |
1884 | Py_DECREF(o2); | |
2f90df85 RD |
1885 | return TRUE; |
1886 | } | |
2f90df85 RD |
1887 | error: |
1888 | PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of integers or a wxPoint object."); | |
1889 | return FALSE; | |
1890 | } | |
1891 | ||
1892 | ||
1893 | ||
1894 | bool wxRealPoint_helper(PyObject* source, wxRealPoint** obj) { | |
1895 | ||
1896 | // If source is an object instance then it may already be the right type | |
1897 | if (PyInstance_Check(source)) { | |
1898 | wxRealPoint* ptr; | |
1899 | if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxRealPoint_p")) | |
1900 | goto error; | |
1901 | *obj = ptr; | |
1902 | return TRUE; | |
1903 | } | |
1904 | // otherwise a 2-tuple of floats is expected | |
1905 | else if (PySequence_Check(source) && PyObject_Length(source) == 2) { | |
1906 | PyObject* o1 = PySequence_GetItem(source, 0); | |
1907 | PyObject* o2 = PySequence_GetItem(source, 1); | |
db0ff83e RD |
1908 | if (!PyNumber_Check(o1) || !PyNumber_Check(o2)) { |
1909 | Py_DECREF(o1); | |
1910 | Py_DECREF(o2); | |
1911 | goto error; | |
1912 | } | |
2f90df85 | 1913 | **obj = wxRealPoint(PyFloat_AsDouble(o1), PyFloat_AsDouble(o2)); |
db0ff83e RD |
1914 | Py_DECREF(o1); |
1915 | Py_DECREF(o2); | |
2f90df85 RD |
1916 | return TRUE; |
1917 | } | |
1918 | ||
1919 | error: | |
1920 | PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of floats or a wxRealPoint object."); | |
1921 | return FALSE; | |
1922 | } | |
1923 | ||
1924 | ||
1925 | ||
1926 | ||
1927 | bool wxRect_helper(PyObject* source, wxRect** obj) { | |
1928 | ||
1929 | // If source is an object instance then it may already be the right type | |
1930 | if (PyInstance_Check(source)) { | |
1931 | wxRect* ptr; | |
1932 | if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxRect_p")) | |
1933 | goto error; | |
1934 | *obj = ptr; | |
1935 | return TRUE; | |
1936 | } | |
1937 | // otherwise a 4-tuple of integers is expected | |
1938 | else if (PySequence_Check(source) && PyObject_Length(source) == 4) { | |
1939 | PyObject* o1 = PySequence_GetItem(source, 0); | |
1940 | PyObject* o2 = PySequence_GetItem(source, 1); | |
1941 | PyObject* o3 = PySequence_GetItem(source, 2); | |
1942 | PyObject* o4 = PySequence_GetItem(source, 3); | |
db0ff83e RD |
1943 | if (!PyNumber_Check(o1) || !PyNumber_Check(o2) || |
1944 | !PyNumber_Check(o3) || !PyNumber_Check(o4)) { | |
1945 | Py_DECREF(o1); | |
1946 | Py_DECREF(o2); | |
1947 | Py_DECREF(o3); | |
1948 | Py_DECREF(o4); | |
1949 | goto error; | |
1950 | } | |
2f90df85 | 1951 | **obj = wxRect(PyInt_AsLong(o1), PyInt_AsLong(o2), |
db0ff83e RD |
1952 | PyInt_AsLong(o3), PyInt_AsLong(o4)); |
1953 | Py_DECREF(o1); | |
1954 | Py_DECREF(o2); | |
1955 | Py_DECREF(o3); | |
1956 | Py_DECREF(o4); | |
2f90df85 RD |
1957 | return TRUE; |
1958 | } | |
1959 | ||
1960 | error: | |
1961 | PyErr_SetString(PyExc_TypeError, "Expected a 4-tuple of integers or a wxRect object."); | |
1962 | return FALSE; | |
1963 | } | |
1964 | ||
1965 | ||
1966 | ||
f6bcfd97 BP |
1967 | bool wxColour_helper(PyObject* source, wxColour** obj) { |
1968 | ||
1969 | // If source is an object instance then it may already be the right type | |
1970 | if (PyInstance_Check(source)) { | |
1971 | wxColour* ptr; | |
1972 | if (SWIG_GetPtrObj(source, (void **)&ptr, "_wxColour_p")) | |
1973 | goto error; | |
1974 | *obj = ptr; | |
1975 | return TRUE; | |
1976 | } | |
1977 | // otherwise a string is expected | |
1978 | else if (PyString_Check(source)) { | |
a541c325 RD |
1979 | wxString spec(PyString_AS_STRING(source), *wxConvCurrent); |
1980 | if (spec.GetChar(0) == '#' && spec.Length() == 7) { // It's #RRGGBB | |
1981 | long red, green, blue; | |
1982 | red = green = blue = 0; | |
a541c325 RD |
1983 | spec.Mid(1,2).ToLong(&red, 16); |
1984 | spec.Mid(3,2).ToLong(&green, 16); | |
1985 | spec.Mid(5,2).ToLong(&blue, 16); | |
1986 | ||
f6bcfd97 BP |
1987 | **obj = wxColour(red, green, blue); |
1988 | return TRUE; | |
1989 | } | |
1990 | else { // it's a colour name | |
1991 | **obj = wxColour(spec); | |
1992 | return TRUE; | |
1993 | } | |
1994 | } | |
1995 | ||
1996 | error: | |
a541c325 RD |
1997 | PyErr_SetString(PyExc_TypeError, |
1998 | "Expected a wxColour object or a string containing a colour " | |
1999 | "name or '#RRGGBB'."); | |
f6bcfd97 BP |
2000 | return FALSE; |
2001 | } | |
2002 | ||
2003 | ||
bb0054cd | 2004 | //---------------------------------------------------------------------- |
b37c7e1d RD |
2005 | |
2006 | PyObject* wxArrayString2PyList_helper(const wxArrayString& arr) { | |
2007 | ||
2008 | PyObject* list = PyList_New(0); | |
2009 | for (size_t i=0; i < arr.GetCount(); i++) { | |
c8bc7bb8 RD |
2010 | #if wxUSE_UNICODE |
2011 | PyObject* str = PyUnicode_FromUnicode(arr[i].c_str(), arr[i].Len()); | |
2012 | #else | |
2013 | PyObject* str = PyString_FromStringAndSize(arr[i].c_str(), arr[i].Len()); | |
2014 | #endif | |
b37c7e1d | 2015 | PyList_Append(list, str); |
8af26133 | 2016 | Py_DECREF(str); |
b37c7e1d RD |
2017 | } |
2018 | return list; | |
2019 | } | |
2020 | ||
2021 | ||
293a0a86 RD |
2022 | PyObject* wxArrayInt2PyList_helper(const wxArrayInt& arr) { |
2023 | ||
2024 | PyObject* list = PyList_New(0); | |
2025 | for (size_t i=0; i < arr.GetCount(); i++) { | |
2026 | PyObject* number = PyInt_FromLong(arr[i]); | |
2027 | PyList_Append(list, number); | |
2028 | Py_DECREF(number); | |
2029 | } | |
2030 | return list; | |
2031 | } | |
2032 | ||
2033 | ||
bb0054cd | 2034 | //---------------------------------------------------------------------- |
7bf85405 | 2035 | //---------------------------------------------------------------------- |
7bf85405 | 2036 | |
7bf85405 | 2037 | |
7bf85405 | 2038 | |
de20db99 | 2039 |