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