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