]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: helpers.cpp | |
3 | // Purpose: Helper functions/classes for the wxPython extension module | |
4 | // | |
5 | // Author: Robin Dunn | |
6 | // | |
7 | // Created: 1-July-1997 | |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) 1998 by Total Control Software | |
10 | // Licence: wxWindows license | |
11 | ///////////////////////////////////////////////////////////////////////////// | |
12 | ||
13 | ||
14 | #undef DEBUG | |
15 | #include <Python.h> | |
16 | #include "wx/wxPython/wxPython_int.h" | |
17 | #include "wx/wxPython/pyistream.h" | |
18 | ||
19 | #ifdef __WXMSW__ | |
20 | #include <wx/msw/private.h> | |
21 | #include <wx/msw/winundef.h> | |
22 | #include <wx/msw/msvcrt.h> | |
23 | #endif | |
24 | ||
25 | #ifdef __WXGTK__ | |
26 | #include <gdk/gdk.h> | |
27 | #include <gdk/gdkx.h> | |
28 | #include <gtk/gtk.h> | |
29 | #include <gdk/gdkprivate.h> | |
30 | #include <wx/gtk/win_gtk.h> | |
31 | #define GetXWindow(wxwin) (wxwin)->m_wxwindow ? \ | |
32 | GDK_WINDOW_XWINDOW(GTK_PIZZA((wxwin)->m_wxwindow)->bin_window) : \ | |
33 | GDK_WINDOW_XWINDOW((wxwin)->m_widget->window) | |
34 | #include <locale.h> | |
35 | #endif | |
36 | ||
37 | #ifdef __WXX11__ | |
38 | #include "wx/x11/privx.h" | |
39 | #define GetXWindow(wxwin) ((Window)(wxwin)->GetHandle()) | |
40 | #endif | |
41 | ||
42 | #ifdef __WXMAC__ | |
43 | #include <wx/mac/private.h> | |
44 | #endif | |
45 | ||
46 | #include <wx/clipbrd.h> | |
47 | #include <wx/mimetype.h> | |
48 | #include <wx/image.h> | |
49 | ||
50 | //---------------------------------------------------------------------- | |
51 | ||
52 | #if PYTHON_API_VERSION < 1009 && wxUSE_UNICODE | |
53 | #error Python must support Unicode to use wxWindows Unicode | |
54 | #endif | |
55 | ||
56 | //---------------------------------------------------------------------- | |
57 | ||
58 | wxPyApp* wxPythonApp = NULL; // Global instance of application object | |
59 | bool wxPyDoCleanup = false; | |
60 | bool wxPyDoingCleanup = false; | |
61 | ||
62 | ||
63 | #ifdef WXP_WITH_THREAD | |
64 | struct wxPyThreadState { | |
65 | unsigned long tid; | |
66 | PyThreadState* tstate; | |
67 | ||
68 | wxPyThreadState(unsigned long _tid=0, PyThreadState* _tstate=NULL) | |
69 | : tid(_tid), tstate(_tstate) {} | |
70 | }; | |
71 | ||
72 | #include <wx/dynarray.h> | |
73 | WX_DECLARE_OBJARRAY(wxPyThreadState, wxPyThreadStateArray); | |
74 | #include <wx/arrimpl.cpp> | |
75 | WX_DEFINE_OBJARRAY(wxPyThreadStateArray); | |
76 | ||
77 | wxPyThreadStateArray* wxPyTStates = NULL; | |
78 | wxMutex* wxPyTMutex = NULL; | |
79 | #endif | |
80 | ||
81 | #define DEFAULTENCODING_SIZE 64 | |
82 | static char wxPyDefaultEncoding[DEFAULTENCODING_SIZE] = "ascii"; | |
83 | ||
84 | static PyObject* wxPython_dict = NULL; | |
85 | static PyObject* wxPyAssertionError = NULL; | |
86 | static PyObject* wxPyNoAppError = NULL; | |
87 | ||
88 | PyObject* wxPyPtrTypeMap = NULL; | |
89 | ||
90 | ||
91 | #ifdef __WXMSW__ // If building for win32... | |
92 | //---------------------------------------------------------------------- | |
93 | // This gets run when the DLL is loaded. We just need to save a handle. | |
94 | //---------------------------------------------------------------------- | |
95 | ||
96 | BOOL WINAPI DllMain( | |
97 | HINSTANCE hinstDLL, // handle to DLL module | |
98 | DWORD fdwReason, // reason for calling function | |
99 | LPVOID lpvReserved // reserved | |
100 | ) | |
101 | { | |
102 | // If wxPython is embedded in another wxWindows app then | |
103 | // the inatance has already been set. | |
104 | if (! wxGetInstance()) | |
105 | wxSetInstance(hinstDLL); | |
106 | return true; | |
107 | } | |
108 | #endif | |
109 | ||
110 | //---------------------------------------------------------------------- | |
111 | // Classes for implementing the wxp main application shell. | |
112 | //---------------------------------------------------------------------- | |
113 | ||
114 | IMPLEMENT_ABSTRACT_CLASS(wxPyApp, wxApp); | |
115 | ||
116 | ||
117 | wxPyApp::wxPyApp() { | |
118 | m_assertMode = wxPYAPP_ASSERT_EXCEPTION; | |
119 | m_startupComplete = false; | |
120 | } | |
121 | ||
122 | ||
123 | wxPyApp::~wxPyApp() { | |
124 | } | |
125 | ||
126 | ||
127 | // This one isn't acutally called... We fake it with _BootstrapApp | |
128 | bool wxPyApp::OnInit() { | |
129 | return false; | |
130 | } | |
131 | ||
132 | ||
133 | int wxPyApp::MainLoop() { | |
134 | int retval = 0; | |
135 | ||
136 | DeletePendingObjects(); | |
137 | bool initialized = wxTopLevelWindows.GetCount() != 0; | |
138 | if (initialized) { | |
139 | if ( m_exitOnFrameDelete == Later ) { | |
140 | m_exitOnFrameDelete = Yes; | |
141 | } | |
142 | ||
143 | retval = wxApp::MainLoop(); | |
144 | OnExit(); | |
145 | } | |
146 | return retval; | |
147 | } | |
148 | ||
149 | ||
150 | bool wxPyApp::OnInitGui() { | |
151 | bool rval=true; | |
152 | wxApp::OnInitGui(); // in this case always call the base class version | |
153 | bool blocked = wxPyBeginBlockThreads(); | |
154 | if (wxPyCBH_findCallback(m_myInst, "OnInitGui")) | |
155 | rval = wxPyCBH_callCallback(m_myInst, Py_BuildValue("()")); | |
156 | wxPyEndBlockThreads(blocked); | |
157 | return rval; | |
158 | } | |
159 | ||
160 | ||
161 | int wxPyApp::OnExit() { | |
162 | int rval=0; | |
163 | bool blocked = wxPyBeginBlockThreads(); | |
164 | if (wxPyCBH_findCallback(m_myInst, "OnExit")) | |
165 | rval = wxPyCBH_callCallback(m_myInst, Py_BuildValue("()")); | |
166 | wxPyEndBlockThreads(blocked); | |
167 | wxApp::OnExit(); // in this case always call the base class version | |
168 | return rval; | |
169 | } | |
170 | ||
171 | ||
172 | #ifdef __WXDEBUG__ | |
173 | void wxPyApp::OnAssert(const wxChar *file, | |
174 | int line, | |
175 | const wxChar *cond, | |
176 | const wxChar *msg) { | |
177 | ||
178 | // if we're not fully initialized then just log the error | |
179 | if (! m_startupComplete) { | |
180 | wxString buf; | |
181 | buf.Alloc(4096); | |
182 | buf.Printf(wxT("%s(%d): assert \"%s\" failed"), | |
183 | file, line, cond); | |
184 | if (msg != NULL) { | |
185 | buf += wxT(": "); | |
186 | buf += msg; | |
187 | } | |
188 | wxLogDebug(buf); | |
189 | return; | |
190 | } | |
191 | ||
192 | // If the OnAssert is overloaded in the Python class then call it... | |
193 | bool found; | |
194 | bool blocked = wxPyBeginBlockThreads(); | |
195 | if ((found = wxPyCBH_findCallback(m_myInst, "OnAssert"))) { | |
196 | PyObject* fso = wx2PyString(file); | |
197 | PyObject* cso = wx2PyString(file); | |
198 | PyObject* mso; | |
199 | if (msg != NULL) | |
200 | mso = wx2PyString(file); | |
201 | else { | |
202 | mso = Py_None; Py_INCREF(Py_None); | |
203 | } | |
204 | wxPyCBH_callCallback(m_myInst, Py_BuildValue("(OiOO)", fso, line, cso, mso)); | |
205 | Py_DECREF(fso); | |
206 | Py_DECREF(cso); | |
207 | Py_DECREF(mso); | |
208 | } | |
209 | wxPyEndBlockThreads(blocked); | |
210 | ||
211 | // ...otherwise do our own thing with it | |
212 | if (! found) { | |
213 | // ignore it? | |
214 | if (m_assertMode & wxPYAPP_ASSERT_SUPPRESS) | |
215 | return; | |
216 | ||
217 | // turn it into a Python exception? | |
218 | if (m_assertMode & wxPYAPP_ASSERT_EXCEPTION) { | |
219 | wxString buf; | |
220 | buf.Alloc(4096); | |
221 | buf.Printf(wxT("C++ assertion \"%s\" failed in %s(%d)"), cond, file, line); | |
222 | if (msg != NULL) { | |
223 | buf += wxT(": "); | |
224 | buf += msg; | |
225 | } | |
226 | ||
227 | // set the exception | |
228 | bool blocked = wxPyBeginBlockThreads(); | |
229 | PyObject* s = wx2PyString(buf); | |
230 | PyErr_SetObject(wxPyAssertionError, s); | |
231 | Py_DECREF(s); | |
232 | wxPyEndBlockThreads(blocked); | |
233 | ||
234 | // Now when control returns to whatever API wrapper was called from | |
235 | // Python it should detect that an exception is set and will return | |
236 | // NULL, signalling the exception to Python. | |
237 | } | |
238 | ||
239 | // Send it to the normal log destination, but only if | |
240 | // not _DIALOG because it will call this too | |
241 | if ( (m_assertMode & wxPYAPP_ASSERT_LOG) && !(m_assertMode & wxPYAPP_ASSERT_DIALOG)) { | |
242 | wxString buf; | |
243 | buf.Alloc(4096); | |
244 | buf.Printf(wxT("%s(%d): assert \"%s\" failed"), | |
245 | file, line, cond); | |
246 | if (msg != NULL) { | |
247 | buf += wxT(": "); | |
248 | buf += msg; | |
249 | } | |
250 | wxLogDebug(buf); | |
251 | } | |
252 | ||
253 | // do the normal wx assert dialog? | |
254 | if (m_assertMode & wxPYAPP_ASSERT_DIALOG) | |
255 | wxApp::OnAssert(file, line, cond, msg); | |
256 | } | |
257 | } | |
258 | #endif | |
259 | ||
260 | // For catching Apple Events | |
261 | void wxPyApp::MacOpenFile(const wxString &fileName) | |
262 | { | |
263 | bool blocked = wxPyBeginBlockThreads(); | |
264 | if (wxPyCBH_findCallback(m_myInst, "MacOpenFile")) { | |
265 | PyObject* s = wx2PyString(fileName); | |
266 | wxPyCBH_callCallback(m_myInst, Py_BuildValue("(O)", s)); | |
267 | Py_DECREF(s); | |
268 | } | |
269 | wxPyEndBlockThreads(blocked); | |
270 | } | |
271 | ||
272 | void wxPyApp::MacPrintFile(const wxString &fileName) | |
273 | { | |
274 | bool blocked = wxPyBeginBlockThreads(); | |
275 | if (wxPyCBH_findCallback(m_myInst, "MacPrintFile")) { | |
276 | PyObject* s = wx2PyString(fileName); | |
277 | wxPyCBH_callCallback(m_myInst, Py_BuildValue("(O)", s)); | |
278 | Py_DECREF(s); | |
279 | } | |
280 | wxPyEndBlockThreads(blocked); | |
281 | } | |
282 | ||
283 | void wxPyApp::MacNewFile() | |
284 | { | |
285 | bool blocked = wxPyBeginBlockThreads(); | |
286 | if (wxPyCBH_findCallback(m_myInst, "MacNewFile")) | |
287 | wxPyCBH_callCallback(m_myInst, Py_BuildValue("()")); | |
288 | wxPyEndBlockThreads(blocked); | |
289 | } | |
290 | ||
291 | void wxPyApp::MacReopenApp() | |
292 | { | |
293 | bool blocked = wxPyBeginBlockThreads(); | |
294 | if (wxPyCBH_findCallback(m_myInst, "MacReopenApp")) | |
295 | wxPyCBH_callCallback(m_myInst, Py_BuildValue("()")); | |
296 | wxPyEndBlockThreads(blocked); | |
297 | } | |
298 | ||
299 | ||
300 | /*static*/ | |
301 | bool wxPyApp::GetMacSupportPCMenuShortcuts() { | |
302 | #ifdef __WXMAC__ | |
303 | return s_macSupportPCMenuShortcuts; | |
304 | #else | |
305 | return 0; | |
306 | #endif | |
307 | } | |
308 | ||
309 | /*static*/ | |
310 | long wxPyApp::GetMacAboutMenuItemId() { | |
311 | #ifdef __WXMAC__ | |
312 | return s_macAboutMenuItemId; | |
313 | #else | |
314 | return 0; | |
315 | #endif | |
316 | } | |
317 | ||
318 | /*static*/ | |
319 | long wxPyApp::GetMacPreferencesMenuItemId() { | |
320 | #ifdef __WXMAC__ | |
321 | return s_macPreferencesMenuItemId; | |
322 | #else | |
323 | return 0; | |
324 | #endif | |
325 | } | |
326 | ||
327 | /*static*/ | |
328 | long wxPyApp::GetMacExitMenuItemId() { | |
329 | #ifdef __WXMAC__ | |
330 | return s_macExitMenuItemId; | |
331 | #else | |
332 | return 0; | |
333 | #endif | |
334 | } | |
335 | ||
336 | /*static*/ | |
337 | wxString wxPyApp::GetMacHelpMenuTitleName() { | |
338 | #ifdef __WXMAC__ | |
339 | return s_macHelpMenuTitleName; | |
340 | #else | |
341 | return wxEmptyString; | |
342 | #endif | |
343 | } | |
344 | ||
345 | /*static*/ | |
346 | void wxPyApp::SetMacSupportPCMenuShortcuts(bool val) { | |
347 | #ifdef __WXMAC__ | |
348 | s_macSupportPCMenuShortcuts = val; | |
349 | #endif | |
350 | } | |
351 | ||
352 | /*static*/ | |
353 | void wxPyApp::SetMacAboutMenuItemId(long val) { | |
354 | #ifdef __WXMAC__ | |
355 | s_macAboutMenuItemId = val; | |
356 | #endif | |
357 | } | |
358 | ||
359 | /*static*/ | |
360 | void wxPyApp::SetMacPreferencesMenuItemId(long val) { | |
361 | #ifdef __WXMAC__ | |
362 | s_macPreferencesMenuItemId = val; | |
363 | #endif | |
364 | } | |
365 | ||
366 | /*static*/ | |
367 | void wxPyApp::SetMacExitMenuItemId(long val) { | |
368 | #ifdef __WXMAC__ | |
369 | s_macExitMenuItemId = val; | |
370 | #endif | |
371 | } | |
372 | ||
373 | /*static*/ | |
374 | void wxPyApp::SetMacHelpMenuTitleName(const wxString& val) { | |
375 | #ifdef __WXMAC__ | |
376 | s_macHelpMenuTitleName = val; | |
377 | #endif | |
378 | } | |
379 | ||
380 | ||
381 | // This finishes the initialization of wxWindows and then calls the OnInit | |
382 | // that should be present in the derived (Python) class. | |
383 | void wxPyApp::_BootstrapApp() | |
384 | { | |
385 | static bool haveInitialized = false; | |
386 | bool result, blocked; | |
387 | PyObject* retval = NULL; | |
388 | PyObject* pyint = NULL; | |
389 | ||
390 | ||
391 | // Only initialize wxWidgets once | |
392 | if (! haveInitialized) { | |
393 | ||
394 | // Get any command-line args passed to this program from the sys module | |
395 | int argc = 0; | |
396 | char** argv = NULL; | |
397 | blocked = wxPyBeginBlockThreads(); | |
398 | PyObject* sysargv = PySys_GetObject("argv"); | |
399 | if (sysargv != NULL) { | |
400 | argc = PyList_Size(sysargv); | |
401 | argv = new char*[argc+1]; | |
402 | int x; | |
403 | for(x=0; x<argc; x++) { | |
404 | PyObject *pyArg = PyList_GetItem(sysargv, x); | |
405 | argv[x] = PyString_AsString(pyArg); | |
406 | } | |
407 | argv[argc] = NULL; | |
408 | } | |
409 | wxPyEndBlockThreads(blocked); | |
410 | ||
411 | // Initialize wxWidgets | |
412 | result = wxEntryStart(argc, argv); | |
413 | delete [] argv; | |
414 | ||
415 | blocked = wxPyBeginBlockThreads(); | |
416 | if (! result) { | |
417 | PyErr_SetString(PyExc_SystemError, | |
418 | "wxEntryStart failed, unable to initialize wxWidgets!" | |
419 | #ifdef __WXGTK__ | |
420 | " (Is DISPLAY set properly?)" | |
421 | #endif | |
422 | ); | |
423 | goto error; | |
424 | } | |
425 | ||
426 | // On wxGTK the locale will be changed to match the system settings, | |
427 | // but Python before 2.4 needs to have LC_NUMERIC set to "C" in order | |
428 | // for the floating point conversions and such to work right. | |
429 | #if defined(__WXGTK__) && PY_VERSION_HEX < 0x02040000 | |
430 | setlocale(LC_NUMERIC, "C"); | |
431 | #endif | |
432 | ||
433 | // The stock objects were all NULL when they were loaded into | |
434 | // SWIG generated proxies, so re-init those now... | |
435 | wxPy_ReinitStockObjects(3); | |
436 | ||
437 | wxPyEndBlockThreads(blocked); | |
438 | haveInitialized = true; | |
439 | } | |
440 | ||
441 | // It's now ok to generate exceptions for assertion errors. | |
442 | wxPythonApp->SetStartupComplete(true); | |
443 | ||
444 | // Call the Python wxApp's OnInit function | |
445 | blocked = wxPyBeginBlockThreads(); | |
446 | if (wxPyCBH_findCallback(m_myInst, "OnInit")) { | |
447 | ||
448 | PyObject* method = m_myInst.GetLastFound(); | |
449 | PyObject* argTuple = PyTuple_New(0); | |
450 | retval = PyEval_CallObject(method, argTuple); | |
451 | Py_DECREF(argTuple); | |
452 | Py_DECREF(method); | |
453 | if (retval == NULL) | |
454 | goto error; | |
455 | ||
456 | pyint = PyNumber_Int(retval); | |
457 | if (! pyint) { | |
458 | PyErr_SetString(PyExc_TypeError, "OnInit should return a boolean value"); | |
459 | goto error; | |
460 | } | |
461 | result = PyInt_AS_LONG(pyint); | |
462 | } | |
463 | else { | |
464 | // Is it okay if there is no OnInit? Probably so... | |
465 | result = true; | |
466 | } | |
467 | ||
468 | if (! result) { | |
469 | PyErr_SetString(PyExc_SystemExit, "OnInit returned false, exiting..."); | |
470 | } | |
471 | ||
472 | error: | |
473 | Py_XDECREF(retval); | |
474 | Py_XDECREF(pyint); | |
475 | ||
476 | wxPyEndBlockThreads(blocked); | |
477 | }; | |
478 | ||
479 | //--------------------------------------------------------------------- | |
480 | //---------------------------------------------------------------------- | |
481 | ||
482 | ||
483 | #if 0 | |
484 | static char* wxPyCopyCString(const wxChar* src) | |
485 | { | |
486 | wxWX2MBbuf buff = (wxWX2MBbuf)wxConvCurrent->cWX2MB(src); | |
487 | size_t len = strlen(buff); | |
488 | char* dest = new char[len+1]; | |
489 | strcpy(dest, buff); | |
490 | return dest; | |
491 | } | |
492 | ||
493 | #if wxUSE_UNICODE | |
494 | static char* wxPyCopyCString(const char* src) // we need a char version too | |
495 | { | |
496 | size_t len = strlen(src); | |
497 | char* dest = new char[len+1]; | |
498 | strcpy(dest, src); | |
499 | return dest; | |
500 | } | |
501 | #endif | |
502 | ||
503 | static wxChar* wxPyCopyWString(const char *src) | |
504 | { | |
505 | //wxMB2WXbuf buff = wxConvCurrent->cMB2WX(src); | |
506 | wxString str(src, *wxConvCurrent); | |
507 | return copystring(str); | |
508 | } | |
509 | ||
510 | #if wxUSE_UNICODE | |
511 | static wxChar* wxPyCopyWString(const wxChar *src) | |
512 | { | |
513 | return copystring(src); | |
514 | } | |
515 | #endif | |
516 | #endif | |
517 | ||
518 | ||
519 | inline const char* dropwx(const char* name) { | |
520 | if (name[0] == 'w' && name[1] == 'x') | |
521 | return name+2; | |
522 | else | |
523 | return name; | |
524 | } | |
525 | ||
526 | //---------------------------------------------------------------------- | |
527 | ||
528 | // This function is called when the wx._core_ module is imported to do some | |
529 | // initial setup. (Before there is a wxApp object.) The rest happens in | |
530 | // wxPyApp::_BootstrapApp | |
531 | void __wxPyPreStart(PyObject* moduleDict) | |
532 | { | |
533 | ||
534 | #ifdef __WXMSW__ | |
535 | // wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | |
536 | // | _CRTDBG_CHECK_ALWAYS_DF | |
537 | // | _CRTDBG_DELAY_FREE_MEM_DF | |
538 | // ); | |
539 | #endif | |
540 | ||
541 | #ifdef WXP_WITH_THREAD | |
542 | PyEval_InitThreads(); | |
543 | wxPyTStates = new wxPyThreadStateArray; | |
544 | wxPyTMutex = new wxMutex; | |
545 | ||
546 | // Save the current (main) thread state in our array | |
547 | PyThreadState* tstate = wxPyBeginAllowThreads(); | |
548 | wxPyEndAllowThreads(tstate); | |
549 | #endif | |
550 | ||
551 | // Ensure that the build options in the DLL (or whatever) match this build | |
552 | wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "wxPython"); | |
553 | ||
554 | // Init the stock objects to a non-NULL value so SWIG doesn't create them as None | |
555 | wxPy_ReinitStockObjects(1); | |
556 | ||
557 | wxInitAllImageHandlers(); | |
558 | } | |
559 | ||
560 | ||
561 | ||
562 | void __wxPyCleanup() { | |
563 | wxPyDoingCleanup = true; | |
564 | if (wxPyDoCleanup) { | |
565 | wxPyDoCleanup = false; | |
566 | wxEntryCleanup(); | |
567 | } | |
568 | #ifdef WXP_WITH_THREAD | |
569 | delete wxPyTMutex; | |
570 | wxPyTMutex = NULL; | |
571 | wxPyTStates->Empty(); | |
572 | delete wxPyTStates; | |
573 | wxPyTStates = NULL; | |
574 | #endif | |
575 | } | |
576 | ||
577 | ||
578 | // Save a reference to the dictionary of the wx._core module, and inject | |
579 | // a few more things into it. | |
580 | PyObject* __wxPySetDictionary(PyObject* /* self */, PyObject* args) | |
581 | { | |
582 | ||
583 | if (!PyArg_ParseTuple(args, "O", &wxPython_dict)) | |
584 | return NULL; | |
585 | ||
586 | if (!PyDict_Check(wxPython_dict)) { | |
587 | PyErr_SetString(PyExc_TypeError, | |
588 | "_wxPySetDictionary must have dictionary object!"); | |
589 | return NULL; | |
590 | } | |
591 | ||
592 | if (! wxPyPtrTypeMap) | |
593 | wxPyPtrTypeMap = PyDict_New(); | |
594 | PyDict_SetItemString(wxPython_dict, "__wxPyPtrTypeMap", wxPyPtrTypeMap); | |
595 | ||
596 | // Create an exception object to use for wxASSERTions | |
597 | wxPyAssertionError = PyErr_NewException("wx._core.PyAssertionError", | |
598 | PyExc_AssertionError, NULL); | |
599 | PyDict_SetItemString(wxPython_dict, "PyAssertionError", wxPyAssertionError); | |
600 | ||
601 | // Create an exception object to use when the app object hasn't been created yet | |
602 | wxPyNoAppError = PyErr_NewException("wx._core.PyNoAppError", | |
603 | PyExc_RuntimeError, NULL); | |
604 | PyDict_SetItemString(wxPython_dict, "PyNoAppError", wxPyNoAppError); | |
605 | ||
606 | ||
607 | ||
608 | #ifdef __WXMOTIF__ | |
609 | #define wxPlatform "__WXMOTIF__" | |
610 | #define wxPlatName "wxMotif" | |
611 | #endif | |
612 | #ifdef __WXX11__ | |
613 | #define wxPlatform "__WXX11__" | |
614 | #define wxPlatName "wxX11" | |
615 | #endif | |
616 | #ifdef __WXGTK__ | |
617 | #define wxPlatform "__WXGTK__" | |
618 | #define wxPlatName "wxGTK" | |
619 | #endif | |
620 | #ifdef __WXMSW__ | |
621 | #define wxPlatform "__WXMSW__" | |
622 | #define wxPlatName "wxMSW" | |
623 | #endif | |
624 | #ifdef __WXMAC__ | |
625 | #define wxPlatform "__WXMAC__" | |
626 | #define wxPlatName "wxMac" | |
627 | #endif | |
628 | ||
629 | #ifdef __WXDEBUG__ | |
630 | int wxdebug = 1; | |
631 | #else | |
632 | int wxdebug = 0; | |
633 | #endif | |
634 | ||
635 | // These should be deprecated in favor of the PlatformInfo tuple built below... | |
636 | PyDict_SetItemString(wxPython_dict, "Platform", PyString_FromString(wxPlatform)); | |
637 | PyDict_SetItemString(wxPython_dict, "USE_UNICODE", PyInt_FromLong(wxUSE_UNICODE)); | |
638 | PyDict_SetItemString(wxPython_dict, "__WXDEBUG__", PyInt_FromLong(wxdebug)); | |
639 | ||
640 | // Make a tuple of strings that gives more info about the platform. | |
641 | PyObject* PlatInfo = PyList_New(0); | |
642 | PyObject* obj; | |
643 | ||
644 | #define _AddInfoString(st) \ | |
645 | obj = PyString_FromString(st); \ | |
646 | PyList_Append(PlatInfo, obj); \ | |
647 | Py_DECREF(obj) | |
648 | ||
649 | _AddInfoString(wxPlatform); | |
650 | _AddInfoString(wxPlatName); | |
651 | #if wxUSE_UNICODE | |
652 | _AddInfoString("unicode"); | |
653 | #else | |
654 | _AddInfoString("ansi"); | |
655 | #endif | |
656 | #ifdef __WXGTK__ | |
657 | #ifdef __WXGTK20__ | |
658 | _AddInfoString("gtk2"); | |
659 | #else | |
660 | _AddInfoString("gtk1"); | |
661 | #endif | |
662 | #endif | |
663 | #ifdef __WXDEBUG__ | |
664 | _AddInfoString("wx-assertions-on"); | |
665 | #else | |
666 | _AddInfoString("wx-assertions-off"); | |
667 | #endif | |
668 | ||
669 | #undef _AddInfoString | |
670 | ||
671 | PyObject* PlatInfoTuple = PyList_AsTuple(PlatInfo); | |
672 | Py_DECREF(PlatInfo); | |
673 | PyDict_SetItemString(wxPython_dict, "PlatformInfo", PlatInfoTuple); | |
674 | ||
675 | RETURN_NONE(); | |
676 | } | |
677 | ||
678 | ||
679 | //--------------------------------------------------------------------------- | |
680 | ||
681 | // Python's PyInstance_Check does not return True for instances of new-style | |
682 | // classes. This should get close enough for both new and old classes but I | |
683 | // should re-evaluate the need for doing instance checks... | |
684 | bool wxPyInstance_Check(PyObject* obj) { | |
685 | return PyObject_HasAttrString(obj, "__class__") != 0; | |
686 | } | |
687 | ||
688 | ||
689 | // This one checks if the object is an instance of a SWIG proxy class (it has | |
690 | // a .this attribute) | |
691 | bool wxPySwigInstance_Check(PyObject* obj) { | |
692 | return PyObject_HasAttrString(obj, "this") != 0; | |
693 | } | |
694 | ||
695 | //--------------------------------------------------------------------------- | |
696 | ||
697 | // The stock objects are no longer created when the wx._core_ module is | |
698 | // imported, but only after the app object has been created. The | |
699 | // wxPy_ReinitStockObjects function will be called 3 times to pass the stock | |
700 | // objects though various stages of evolution: | |
701 | // | |
702 | // pass 1: Set all the pointers to a non-NULL value so the Python proxy | |
703 | // object will be created (otherwise SWIG will just use None.) | |
704 | // | |
705 | // pass 2: After the module has been imported and the python proxys have | |
706 | // been created, then set the __class__ to be _wxPyUnbornObject so | |
707 | // it will catch any access to the object and will raise an exception. | |
708 | // | |
709 | // pass 3: Finally, from BootstrapApp patch things up so the stock objects | |
710 | // can be used. | |
711 | ||
712 | ||
713 | PyObject* __wxPyFixStockObjects(PyObject* /* self */, PyObject* args) | |
714 | { | |
715 | wxPy_ReinitStockObjects(2); | |
716 | RETURN_NONE(); | |
717 | } | |
718 | ||
719 | ||
720 | static void rsoPass2(const char* name) | |
721 | { | |
722 | static PyObject* unbornObjectClass = NULL; | |
723 | PyObject* obj; | |
724 | ||
725 | if (unbornObjectClass == NULL) { | |
726 | unbornObjectClass = PyDict_GetItemString(wxPython_dict, "_wxPyUnbornObject"); | |
727 | Py_INCREF(unbornObjectClass); | |
728 | } | |
729 | ||
730 | // Find the object instance | |
731 | obj = PyDict_GetItemString(wxPython_dict, (char*)dropwx(name)); | |
732 | wxCHECK_RET(obj != NULL, wxT("Unable to find stock object")); | |
733 | wxCHECK_RET(wxPySwigInstance_Check(obj), wxT("Not a swig instance")); | |
734 | ||
735 | // Change its class | |
736 | PyObject_SetAttrString(obj, "__class__", unbornObjectClass); | |
737 | ||
738 | } | |
739 | ||
740 | static void rsoPass3(const char* name, const char* classname, void* ptr) | |
741 | { | |
742 | PyObject* obj; | |
743 | PyObject* classobj; | |
744 | PyObject* ptrobj; | |
745 | ||
746 | // Find the object instance | |
747 | obj = PyDict_GetItemString(wxPython_dict, (char*)dropwx(name)); | |
748 | wxCHECK_RET(obj != NULL, wxT("Unable to find stock object")); | |
749 | wxCHECK_RET(wxPySwigInstance_Check(obj), wxT("Not a swig instance")); | |
750 | ||
751 | // Find the class object and put it back in the instance | |
752 | classobj = PyDict_GetItemString(wxPython_dict, (char*)dropwx(classname)); | |
753 | wxCHECK_RET(classobj != NULL, wxT("Unable to find stock class object")); | |
754 | PyObject_SetAttrString(obj, "__class__", classobj); | |
755 | ||
756 | // Rebuild the .this swigified pointer with the new value of the C++ pointer | |
757 | ptrobj = wxPyMakeSwigPtr(ptr, wxString(classname, *wxConvCurrent)); | |
758 | PyObject_SetAttrString(obj, "this", ptrobj); | |
759 | Py_DECREF(ptrobj); | |
760 | } | |
761 | ||
762 | ||
763 | ||
764 | void wxPy_ReinitStockObjects(int pass) | |
765 | { | |
766 | ||
767 | // If there is already an App object then wxPython is probably embedded in | |
768 | // a wx C++ application, so there is no need to do all this. | |
769 | static bool embedded = false; | |
770 | if ((pass == 1 || pass == 2) && wxTheApp) { | |
771 | embedded = true; | |
772 | return; | |
773 | } | |
774 | if (pass == 3 && embedded) | |
775 | return; | |
776 | ||
777 | ||
778 | #define REINITOBJ(name, classname) \ | |
779 | if (pass == 1) { name = (classname*)0xC0C0C0C0; } \ | |
780 | else if (pass == 2) { rsoPass2(#name); } \ | |
781 | else if (pass == 3) { rsoPass3(#name, #classname, (void*)name); } | |
782 | ||
783 | ||
784 | #define REINITOBJ2(name, classname) \ | |
785 | if (pass == 1) { } \ | |
786 | else if (pass == 2) { rsoPass2(#name); } \ | |
787 | else if (pass == 3) { rsoPass3(#name, #classname, (void*)&name); } | |
788 | ||
789 | ||
790 | REINITOBJ(wxNORMAL_FONT, wxFont); | |
791 | REINITOBJ(wxSMALL_FONT, wxFont); | |
792 | REINITOBJ(wxITALIC_FONT, wxFont); | |
793 | REINITOBJ(wxSWISS_FONT, wxFont); | |
794 | ||
795 | REINITOBJ(wxRED_PEN, wxPen); | |
796 | REINITOBJ(wxCYAN_PEN, wxPen); | |
797 | REINITOBJ(wxGREEN_PEN, wxPen); | |
798 | REINITOBJ(wxBLACK_PEN, wxPen); | |
799 | REINITOBJ(wxWHITE_PEN, wxPen); | |
800 | REINITOBJ(wxTRANSPARENT_PEN, wxPen); | |
801 | REINITOBJ(wxBLACK_DASHED_PEN, wxPen); | |
802 | REINITOBJ(wxGREY_PEN, wxPen); | |
803 | REINITOBJ(wxMEDIUM_GREY_PEN, wxPen); | |
804 | REINITOBJ(wxLIGHT_GREY_PEN, wxPen); | |
805 | ||
806 | REINITOBJ(wxBLUE_BRUSH, wxBrush); | |
807 | REINITOBJ(wxGREEN_BRUSH, wxBrush); | |
808 | REINITOBJ(wxWHITE_BRUSH, wxBrush); | |
809 | REINITOBJ(wxBLACK_BRUSH, wxBrush); | |
810 | REINITOBJ(wxTRANSPARENT_BRUSH, wxBrush); | |
811 | REINITOBJ(wxCYAN_BRUSH, wxBrush); | |
812 | REINITOBJ(wxRED_BRUSH, wxBrush); | |
813 | REINITOBJ(wxGREY_BRUSH, wxBrush); | |
814 | REINITOBJ(wxMEDIUM_GREY_BRUSH, wxBrush); | |
815 | REINITOBJ(wxLIGHT_GREY_BRUSH, wxBrush); | |
816 | ||
817 | REINITOBJ(wxBLACK, wxColour); | |
818 | REINITOBJ(wxWHITE, wxColour); | |
819 | REINITOBJ(wxRED, wxColour); | |
820 | REINITOBJ(wxBLUE, wxColour); | |
821 | REINITOBJ(wxGREEN, wxColour); | |
822 | REINITOBJ(wxCYAN, wxColour); | |
823 | REINITOBJ(wxLIGHT_GREY, wxColour); | |
824 | ||
825 | REINITOBJ(wxSTANDARD_CURSOR, wxCursor); | |
826 | REINITOBJ(wxHOURGLASS_CURSOR, wxCursor); | |
827 | REINITOBJ(wxCROSS_CURSOR, wxCursor); | |
828 | ||
829 | REINITOBJ2(wxNullBitmap, wxBitmap); | |
830 | REINITOBJ2(wxNullIcon, wxIcon); | |
831 | REINITOBJ2(wxNullCursor, wxCursor); | |
832 | REINITOBJ2(wxNullPen, wxPen); | |
833 | REINITOBJ2(wxNullBrush, wxBrush); | |
834 | REINITOBJ2(wxNullPalette, wxPalette); | |
835 | REINITOBJ2(wxNullFont, wxFont); | |
836 | REINITOBJ2(wxNullColour, wxColour); | |
837 | ||
838 | REINITOBJ(wxTheFontList, wxFontList); | |
839 | REINITOBJ(wxThePenList, wxPenList); | |
840 | REINITOBJ(wxTheBrushList, wxBrushList); | |
841 | REINITOBJ(wxTheColourDatabase, wxColourDatabase); | |
842 | ||
843 | ||
844 | REINITOBJ2(wxDefaultValidator, wxValidator); | |
845 | REINITOBJ2(wxNullImage, wxImage); | |
846 | REINITOBJ2(wxNullAcceleratorTable, wxAcceleratorTable); | |
847 | ||
848 | #undef REINITOBJ | |
849 | #undef REINITOBJ2 | |
850 | } | |
851 | ||
852 | //--------------------------------------------------------------------------- | |
853 | ||
854 | // Check for existence of a wxApp, setting an exception if there isn't one. | |
855 | // This doesn't need to aquire the GIL because it should only be called from | |
856 | // an %exception before the lock is released. | |
857 | ||
858 | bool wxPyCheckForApp() { | |
859 | if (wxTheApp != NULL) | |
860 | return true; | |
861 | else { | |
862 | PyErr_SetString(wxPyNoAppError, "The wx.App object must be created first!"); | |
863 | return false; | |
864 | } | |
865 | } | |
866 | ||
867 | //--------------------------------------------------------------------------- | |
868 | ||
869 | void wxPyUserData_dtor(wxPyUserData* self) { | |
870 | if (! wxPyDoingCleanup) { | |
871 | bool blocked = wxPyBeginBlockThreads(); | |
872 | Py_DECREF(self->m_obj); | |
873 | self->m_obj = NULL; | |
874 | wxPyEndBlockThreads(blocked); | |
875 | } | |
876 | } | |
877 | ||
878 | ||
879 | void wxPyClientData_dtor(wxPyClientData* self) { | |
880 | if (! wxPyDoingCleanup) { // Don't do it during cleanup as Python | |
881 | // may have already garbage collected the object... | |
882 | if (self->m_incRef) { | |
883 | bool blocked = wxPyBeginBlockThreads(); | |
884 | Py_DECREF(self->m_obj); | |
885 | wxPyEndBlockThreads(blocked); | |
886 | } | |
887 | self->m_obj = NULL; | |
888 | } | |
889 | } | |
890 | ||
891 | ||
892 | ||
893 | // This is called when an OOR controled object is being destroyed. Although | |
894 | // the C++ object is going away there is no way to force the Python object | |
895 | // (and all references to it) to die too. This causes problems (crashes) in | |
896 | // wxPython when a python shadow object attempts to call a C++ method using | |
897 | // the now bogus pointer... So to try and prevent this we'll do a little black | |
898 | // magic and change the class of the python instance to a class that will | |
899 | // raise an exception for any attempt to call methods with it. See | |
900 | // _wxPyDeadObject in _core_ex.py for the implementation of this class. | |
901 | void wxPyOORClientData_dtor(wxPyOORClientData* self) { | |
902 | ||
903 | static PyObject* deadObjectClass = NULL; | |
904 | ||
905 | bool blocked = wxPyBeginBlockThreads(); | |
906 | if (deadObjectClass == NULL) { | |
907 | deadObjectClass = PyDict_GetItemString(wxPython_dict, "_wxPyDeadObject"); | |
908 | // TODO: Can not wxASSERT here because inside a wxPyBeginBlock Threads, | |
909 | // will lead to a deadlock when it tries to aquire the GIL again. | |
910 | //wxASSERT_MSG(deadObjectClass != NULL, wxT("Can't get _wxPyDeadObject class!")); | |
911 | Py_INCREF(deadObjectClass); | |
912 | } | |
913 | ||
914 | ||
915 | // Only if there is more than one reference to the object and we are | |
916 | // holding the OOR reference: | |
917 | if ( !wxPyDoingCleanup && self->m_obj->ob_refcnt > 1 && self->m_incRef) { | |
918 | // bool isInstance = wxPyInstance_Check(self->m_obj); | |
919 | // TODO same here | |
920 | //wxASSERT_MSG(isInstance, wxT("m_obj not an instance!?!?!")); | |
921 | ||
922 | // Call __del__, if there is one. | |
923 | PyObject* func = PyObject_GetAttrString(self->m_obj, "__del__"); | |
924 | if (func) { | |
925 | PyObject* rv = PyObject_CallMethod(self->m_obj, "__del__", NULL); | |
926 | Py_XDECREF(rv); | |
927 | Py_DECREF(func); | |
928 | } | |
929 | if (PyErr_Occurred()) | |
930 | PyErr_Clear(); // just ignore it for now | |
931 | ||
932 | ||
933 | PyObject* dict = PyObject_GetAttrString(self->m_obj, "__dict__"); | |
934 | if (dict) { | |
935 | // Clear the instance's dictionary | |
936 | PyDict_Clear(dict); | |
937 | ||
938 | // put the name of the old class into the instance, and then reset the | |
939 | // class to be the dead class. | |
940 | PyObject* klass = PyObject_GetAttrString(self->m_obj, "__class__"); | |
941 | PyObject* name = PyObject_GetAttrString(klass, "__name__"); | |
942 | PyDict_SetItemString(dict, "_name", name); | |
943 | PyObject_SetAttrString(self->m_obj, "__class__", deadObjectClass); | |
944 | //Py_INCREF(deadObjectClass); | |
945 | Py_DECREF(klass); | |
946 | Py_DECREF(name); | |
947 | } | |
948 | } | |
949 | ||
950 | // m_obj is DECREF'd in the base class dtor... | |
951 | wxPyEndBlockThreads(blocked); | |
952 | } | |
953 | ||
954 | ||
955 | //--------------------------------------------------------------------------- | |
956 | // Stuff used by OOR to find the right wxPython class type to return and to | |
957 | // build it. | |
958 | ||
959 | ||
960 | // The pointer type map is used when the "pointer" type name generated by SWIG | |
961 | // is not the same as the shadow class name, for example wxPyTreeCtrl | |
962 | // vs. wxTreeCtrl. It needs to be referenced in Python as well as from C++, | |
963 | // so we'll just make it a Python dictionary in the wx module's namespace. | |
964 | // (See __wxSetDictionary) | |
965 | void wxPyPtrTypeMap_Add(const char* commonName, const char* ptrName) { | |
966 | if (! wxPyPtrTypeMap) | |
967 | wxPyPtrTypeMap = PyDict_New(); | |
968 | PyDict_SetItemString(wxPyPtrTypeMap, | |
969 | (char*)commonName, | |
970 | PyString_FromString((char*)ptrName)); | |
971 | } | |
972 | ||
973 | ||
974 | ||
975 | ||
976 | PyObject* wxPyMake_wxObject(wxObject* source, bool setThisOwn, bool checkEvtHandler) { | |
977 | PyObject* target = NULL; | |
978 | bool isEvtHandler = false; | |
979 | ||
980 | if (source) { | |
981 | // If it's derived from wxEvtHandler then there may | |
982 | // already be a pointer to a Python object that we can use | |
983 | // in the OOR data. | |
984 | if (checkEvtHandler && wxIsKindOf(source, wxEvtHandler)) { | |
985 | isEvtHandler = true; | |
986 | wxEvtHandler* eh = (wxEvtHandler*)source; | |
987 | wxPyOORClientData* data = (wxPyOORClientData*)eh->GetClientObject(); | |
988 | if (data) { | |
989 | target = data->m_obj; | |
990 | if (target) | |
991 | Py_INCREF(target); | |
992 | } | |
993 | } | |
994 | ||
995 | if (! target) { | |
996 | // Otherwise make it the old fashioned way by making a new shadow | |
997 | // object and putting this pointer in it. Look up the class | |
998 | // heirarchy until we find a class name that is located in the | |
999 | // python module. | |
1000 | const wxClassInfo* info = source->GetClassInfo(); | |
1001 | wxString name = info->GetClassName(); | |
1002 | bool exists = wxPyCheckSwigType(name); | |
1003 | while (info && !exists) { | |
1004 | info = info->GetBaseClass1(); | |
1005 | name = info->GetClassName(); | |
1006 | exists = wxPyCheckSwigType(name); | |
1007 | } | |
1008 | if (info) { | |
1009 | target = wxPyConstructObject((void*)source, name, setThisOwn); | |
1010 | if (target && isEvtHandler) | |
1011 | ((wxEvtHandler*)source)->SetClientObject(new wxPyOORClientData(target)); | |
1012 | } else { | |
1013 | wxString msg(wxT("wxPython class not found for ")); | |
1014 | msg += source->GetClassInfo()->GetClassName(); | |
1015 | PyErr_SetString(PyExc_NameError, msg.mbc_str()); | |
1016 | target = NULL; | |
1017 | } | |
1018 | } | |
1019 | } else { // source was NULL so return None. | |
1020 | Py_INCREF(Py_None); target = Py_None; | |
1021 | } | |
1022 | return target; | |
1023 | } | |
1024 | ||
1025 | ||
1026 | PyObject* wxPyMake_wxSizer(wxSizer* source, bool setThisOwn) { | |
1027 | PyObject* target = NULL; | |
1028 | ||
1029 | if (source && wxIsKindOf(source, wxSizer)) { | |
1030 | // If it's derived from wxSizer then there may already be a pointer to | |
1031 | // a Python object that we can use in the OOR data. | |
1032 | wxSizer* sz = (wxSizer*)source; | |
1033 | wxPyOORClientData* data = (wxPyOORClientData*)sz->GetClientObject(); | |
1034 | if (data) { | |
1035 | target = data->m_obj; | |
1036 | if (target) | |
1037 | Py_INCREF(target); | |
1038 | } | |
1039 | } | |
1040 | if (! target) { | |
1041 | target = wxPyMake_wxObject(source, setThisOwn, false); | |
1042 | if (target != Py_None) | |
1043 | ((wxSizer*)source)->SetClientObject(new wxPyOORClientData(target)); | |
1044 | } | |
1045 | return target; | |
1046 | } | |
1047 | ||
1048 | ||
1049 | //--------------------------------------------------------------------------- | |
1050 | ||
1051 | ||
1052 | #ifdef WXP_WITH_THREAD | |
1053 | inline | |
1054 | unsigned long wxPyGetCurrentThreadId() { | |
1055 | return wxThread::GetCurrentId(); | |
1056 | } | |
1057 | ||
1058 | static wxPyThreadState gs_shutdownTState; | |
1059 | ||
1060 | static | |
1061 | wxPyThreadState* wxPyGetThreadState() { | |
1062 | if (wxPyTMutex == NULL) // Python is shutting down... | |
1063 | return &gs_shutdownTState; | |
1064 | ||
1065 | unsigned long ctid = wxPyGetCurrentThreadId(); | |
1066 | wxPyThreadState* tstate = NULL; | |
1067 | ||
1068 | wxPyTMutex->Lock(); | |
1069 | for(size_t i=0; i < wxPyTStates->GetCount(); i++) { | |
1070 | wxPyThreadState& info = wxPyTStates->Item(i); | |
1071 | if (info.tid == ctid) { | |
1072 | tstate = &info; | |
1073 | break; | |
1074 | } | |
1075 | } | |
1076 | wxPyTMutex->Unlock(); | |
1077 | wxASSERT_MSG(tstate, wxT("PyThreadState should not be NULL!")); | |
1078 | return tstate; | |
1079 | } | |
1080 | ||
1081 | ||
1082 | static | |
1083 | void wxPySaveThreadState(PyThreadState* tstate) { | |
1084 | if (wxPyTMutex == NULL) { // Python is shutting down, assume a single thread... | |
1085 | gs_shutdownTState.tstate = tstate; | |
1086 | return; | |
1087 | } | |
1088 | unsigned long ctid = wxPyGetCurrentThreadId(); | |
1089 | wxPyTMutex->Lock(); | |
1090 | for(size_t i=0; i < wxPyTStates->GetCount(); i++) { | |
1091 | wxPyThreadState& info = wxPyTStates->Item(i); | |
1092 | if (info.tid == ctid) { | |
1093 | #if 0 | |
1094 | if (info.tstate != tstate) | |
1095 | wxLogMessage("*** tstate mismatch!???"); | |
1096 | #endif | |
1097 | info.tstate = tstate; // allow for transient tstates | |
1098 | // Normally it will never change, but apparently COM callbacks | |
1099 | // (i.e. ActiveX controls) will (incorrectly IMHO) use a transient | |
1100 | // tstate which will then be garbage the next time we try to use | |
1101 | // it... | |
1102 | ||
1103 | wxPyTMutex->Unlock(); | |
1104 | return; | |
1105 | } | |
1106 | } | |
1107 | // not found, so add it... | |
1108 | wxPyTStates->Add(new wxPyThreadState(ctid, tstate)); | |
1109 | wxPyTMutex->Unlock(); | |
1110 | } | |
1111 | ||
1112 | #endif | |
1113 | ||
1114 | ||
1115 | ||
1116 | // Calls from Python to wxWindows code are wrapped in calls to these | |
1117 | // functions: | |
1118 | ||
1119 | PyThreadState* wxPyBeginAllowThreads() { | |
1120 | #ifdef WXP_WITH_THREAD | |
1121 | PyThreadState* saved = PyEval_SaveThread(); // Py_BEGIN_ALLOW_THREADS; | |
1122 | wxPySaveThreadState(saved); | |
1123 | return saved; | |
1124 | #else | |
1125 | return NULL; | |
1126 | #endif | |
1127 | } | |
1128 | ||
1129 | void wxPyEndAllowThreads(PyThreadState* saved) { | |
1130 | #ifdef WXP_WITH_THREAD | |
1131 | PyEval_RestoreThread(saved); // Py_END_ALLOW_THREADS; | |
1132 | #endif | |
1133 | } | |
1134 | ||
1135 | ||
1136 | ||
1137 | // Calls from wxWindows back to Python code, or even any PyObject | |
1138 | // manipulations, PyDECREF's and etc. are wrapped in calls to these functions: | |
1139 | ||
1140 | bool wxPyBeginBlockThreads() { | |
1141 | #ifdef WXP_WITH_THREAD | |
1142 | // This works in for 2.3, maybe a good alternative to find the needed tstate? | |
1143 | // PyThreadState *check = PyGILState_GetThisThreadState(); | |
1144 | ||
1145 | PyThreadState *current = _PyThreadState_Current; | |
1146 | ||
1147 | // Only block if there wasn't already a tstate, or if the current one is | |
1148 | // not the one we are wanting to change to. This should prevent deadlock | |
1149 | // if there are nested calls to wxPyBeginBlockThreads | |
1150 | bool blocked = false; | |
1151 | wxPyThreadState* tstate = wxPyGetThreadState(); | |
1152 | if (current != tstate->tstate) { | |
1153 | PyEval_RestoreThread(tstate->tstate); | |
1154 | blocked = true; | |
1155 | } | |
1156 | return blocked; | |
1157 | #endif | |
1158 | } | |
1159 | ||
1160 | ||
1161 | void wxPyEndBlockThreads(bool blocked) { | |
1162 | #ifdef WXP_WITH_THREAD | |
1163 | // Only unblock if we blocked in the last call to wxPyBeginBlockThreads. | |
1164 | // The value of blocked passed in needs to be the same as that returned | |
1165 | // from wxPyBeginBlockThreads at the same nesting level. | |
1166 | if ( blocked ) { | |
1167 | PyEval_SaveThread(); | |
1168 | } | |
1169 | #endif | |
1170 | } | |
1171 | ||
1172 | ||
1173 | //--------------------------------------------------------------------------- | |
1174 | // wxPyInputStream and wxPyCBInputStream methods | |
1175 | ||
1176 | ||
1177 | void wxPyInputStream::close() { | |
1178 | /* do nothing for now */ | |
1179 | } | |
1180 | ||
1181 | void wxPyInputStream::flush() { | |
1182 | /* do nothing for now */ | |
1183 | } | |
1184 | ||
1185 | bool wxPyInputStream::eof() { | |
1186 | if (m_wxis) | |
1187 | return m_wxis->Eof(); | |
1188 | else | |
1189 | return true; | |
1190 | } | |
1191 | ||
1192 | wxPyInputStream::~wxPyInputStream() { | |
1193 | if (m_wxis) | |
1194 | delete m_wxis; | |
1195 | } | |
1196 | ||
1197 | ||
1198 | ||
1199 | ||
1200 | PyObject* wxPyInputStream::read(int size) { | |
1201 | PyObject* obj = NULL; | |
1202 | wxMemoryBuffer buf; | |
1203 | const int BUFSIZE = 1024; | |
1204 | ||
1205 | // check if we have a real wxInputStream to work with | |
1206 | if (!m_wxis) { | |
1207 | bool blocked = wxPyBeginBlockThreads(); | |
1208 | PyErr_SetString(PyExc_IOError, "no valid C-wxInputStream"); | |
1209 | wxPyEndBlockThreads(blocked); | |
1210 | return NULL; | |
1211 | } | |
1212 | ||
1213 | if (size < 0) { | |
1214 | // read while bytes are available on the stream | |
1215 | while ( m_wxis->CanRead() ) { | |
1216 | m_wxis->Read(buf.GetAppendBuf(BUFSIZE), BUFSIZE); | |
1217 | buf.UngetAppendBuf(m_wxis->LastRead()); | |
1218 | } | |
1219 | ||
1220 | } else { // Read only size number of characters | |
1221 | m_wxis->Read(buf.GetWriteBuf(size), size); | |
1222 | buf.UngetWriteBuf(m_wxis->LastRead()); | |
1223 | } | |
1224 | ||
1225 | // error check | |
1226 | bool blocked = wxPyBeginBlockThreads(); | |
1227 | wxStreamError err = m_wxis->GetLastError(); | |
1228 | if (err != wxSTREAM_NO_ERROR && err != wxSTREAM_EOF) { | |
1229 | PyErr_SetString(PyExc_IOError,"IOError in wxInputStream"); | |
1230 | } | |
1231 | else { | |
1232 | // We use only strings for the streams, not unicode | |
1233 | obj = PyString_FromStringAndSize(buf, buf.GetDataLen()); | |
1234 | } | |
1235 | wxPyEndBlockThreads(blocked); | |
1236 | return obj; | |
1237 | } | |
1238 | ||
1239 | ||
1240 | PyObject* wxPyInputStream::readline(int size) { | |
1241 | PyObject* obj = NULL; | |
1242 | wxMemoryBuffer buf; | |
1243 | int i; | |
1244 | char ch; | |
1245 | ||
1246 | // check if we have a real wxInputStream to work with | |
1247 | if (!m_wxis) { | |
1248 | bool blocked = wxPyBeginBlockThreads(); | |
1249 | PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream"); | |
1250 | wxPyEndBlockThreads(blocked); | |
1251 | return NULL; | |
1252 | } | |
1253 | ||
1254 | // read until \n or byte limit reached | |
1255 | for (i=ch=0; (ch != '\n') && (m_wxis->CanRead()) && ((size < 0) || (i < size)); i++) { | |
1256 | ch = m_wxis->GetC(); | |
1257 | buf.AppendByte(ch); | |
1258 | } | |
1259 | ||
1260 | // errorcheck | |
1261 | bool blocked = wxPyBeginBlockThreads(); | |
1262 | wxStreamError err = m_wxis->GetLastError(); | |
1263 | if (err != wxSTREAM_NO_ERROR && err != wxSTREAM_EOF) { | |
1264 | PyErr_SetString(PyExc_IOError,"IOError in wxInputStream"); | |
1265 | } | |
1266 | else { | |
1267 | // We use only strings for the streams, not unicode | |
1268 | obj = PyString_FromStringAndSize((char*)buf.GetData(), buf.GetDataLen()); | |
1269 | } | |
1270 | wxPyEndBlockThreads(blocked); | |
1271 | return obj; | |
1272 | } | |
1273 | ||
1274 | ||
1275 | PyObject* wxPyInputStream::readlines(int sizehint) { | |
1276 | PyObject* pylist; | |
1277 | ||
1278 | // check if we have a real wxInputStream to work with | |
1279 | if (!m_wxis) { | |
1280 | bool blocked = wxPyBeginBlockThreads(); | |
1281 | PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream"); | |
1282 | wxPyEndBlockThreads(blocked); | |
1283 | return NULL; | |
1284 | } | |
1285 | ||
1286 | // init list | |
1287 | bool blocked = wxPyBeginBlockThreads(); | |
1288 | pylist = PyList_New(0); | |
1289 | wxPyEndBlockThreads(blocked); | |
1290 | ||
1291 | if (!pylist) { | |
1292 | bool blocked = wxPyBeginBlockThreads(); | |
1293 | PyErr_NoMemory(); | |
1294 | wxPyEndBlockThreads(blocked); | |
1295 | return NULL; | |
1296 | } | |
1297 | ||
1298 | // read sizehint bytes or until EOF | |
1299 | int i; | |
1300 | for (i=0; (m_wxis->CanRead()) && ((sizehint < 0) || (i < sizehint));) { | |
1301 | PyObject* s = this->readline(); | |
1302 | if (s == NULL) { | |
1303 | bool blocked = wxPyBeginBlockThreads(); | |
1304 | Py_DECREF(pylist); | |
1305 | wxPyEndBlockThreads(blocked); | |
1306 | return NULL; | |
1307 | } | |
1308 | bool blocked = wxPyBeginBlockThreads(); | |
1309 | PyList_Append(pylist, s); | |
1310 | i += PyString_Size(s); | |
1311 | wxPyEndBlockThreads(blocked); | |
1312 | } | |
1313 | ||
1314 | // error check | |
1315 | wxStreamError err = m_wxis->GetLastError(); | |
1316 | if (err != wxSTREAM_NO_ERROR && err != wxSTREAM_EOF) { | |
1317 | bool blocked = wxPyBeginBlockThreads(); | |
1318 | Py_DECREF(pylist); | |
1319 | PyErr_SetString(PyExc_IOError,"IOError in wxInputStream"); | |
1320 | wxPyEndBlockThreads(blocked); | |
1321 | return NULL; | |
1322 | } | |
1323 | ||
1324 | return pylist; | |
1325 | } | |
1326 | ||
1327 | ||
1328 | void wxPyInputStream::seek(int offset, int whence) { | |
1329 | if (m_wxis) | |
1330 | m_wxis->SeekI(offset, wxSeekMode(whence)); | |
1331 | } | |
1332 | ||
1333 | int wxPyInputStream::tell(){ | |
1334 | if (m_wxis) | |
1335 | return m_wxis->TellI(); | |
1336 | else return 0; | |
1337 | } | |
1338 | ||
1339 | ||
1340 | ||
1341 | ||
1342 | wxPyCBInputStream::wxPyCBInputStream(PyObject *r, PyObject *s, PyObject *t, bool block) | |
1343 | : wxInputStream(), m_read(r), m_seek(s), m_tell(t), m_block(block) | |
1344 | {} | |
1345 | ||
1346 | ||
1347 | wxPyCBInputStream::~wxPyCBInputStream() { | |
1348 | bool blocked=false; | |
1349 | if (m_block) blocked = wxPyBeginBlockThreads(); | |
1350 | Py_XDECREF(m_read); | |
1351 | Py_XDECREF(m_seek); | |
1352 | Py_XDECREF(m_tell); | |
1353 | if (m_block) wxPyEndBlockThreads(blocked); | |
1354 | } | |
1355 | ||
1356 | ||
1357 | wxPyCBInputStream* wxPyCBInputStream::create(PyObject *py, bool block) { | |
1358 | bool blocked=false; | |
1359 | if (block) blocked = wxPyBeginBlockThreads(); | |
1360 | ||
1361 | PyObject* read = getMethod(py, "read"); | |
1362 | PyObject* seek = getMethod(py, "seek"); | |
1363 | PyObject* tell = getMethod(py, "tell"); | |
1364 | ||
1365 | if (!read) { | |
1366 | PyErr_SetString(PyExc_TypeError, "Not a file-like object"); | |
1367 | Py_XDECREF(read); | |
1368 | Py_XDECREF(seek); | |
1369 | Py_XDECREF(tell); | |
1370 | if (block) wxPyEndBlockThreads(blocked); | |
1371 | return NULL; | |
1372 | } | |
1373 | ||
1374 | if (block) wxPyEndBlockThreads(blocked); | |
1375 | return new wxPyCBInputStream(read, seek, tell, block); | |
1376 | } | |
1377 | ||
1378 | ||
1379 | wxPyCBInputStream* wxPyCBInputStream_create(PyObject *py, bool block) { | |
1380 | return wxPyCBInputStream::create(py, block); | |
1381 | } | |
1382 | ||
1383 | PyObject* wxPyCBInputStream::getMethod(PyObject* py, char* name) { | |
1384 | if (!PyObject_HasAttrString(py, name)) | |
1385 | return NULL; | |
1386 | PyObject* o = PyObject_GetAttrString(py, name); | |
1387 | if (!PyMethod_Check(o) && !PyCFunction_Check(o)) { | |
1388 | Py_DECREF(o); | |
1389 | return NULL; | |
1390 | } | |
1391 | return o; | |
1392 | } | |
1393 | ||
1394 | ||
1395 | wxFileOffset wxPyCBInputStream::GetLength() const { | |
1396 | wxPyCBInputStream* self = (wxPyCBInputStream*)this; // cast off const | |
1397 | if (m_seek && m_tell) { | |
1398 | wxFileOffset temp = self->OnSysTell(); | |
1399 | wxFileOffset ret = self->OnSysSeek(0, wxFromEnd); | |
1400 | self->OnSysSeek(temp, wxFromStart); | |
1401 | return ret; | |
1402 | } | |
1403 | else | |
1404 | return wxInvalidOffset; | |
1405 | } | |
1406 | ||
1407 | ||
1408 | size_t wxPyCBInputStream::OnSysRead(void *buffer, size_t bufsize) { | |
1409 | if (bufsize == 0) | |
1410 | return 0; | |
1411 | ||
1412 | bool blocked = wxPyBeginBlockThreads(); | |
1413 | PyObject* arglist = Py_BuildValue("(i)", bufsize); | |
1414 | PyObject* result = PyEval_CallObject(m_read, arglist); | |
1415 | Py_DECREF(arglist); | |
1416 | ||
1417 | size_t o = 0; | |
1418 | if ((result != NULL) && PyString_Check(result)) { | |
1419 | o = PyString_Size(result); | |
1420 | if (o == 0) | |
1421 | m_lasterror = wxSTREAM_EOF; | |
1422 | if (o > bufsize) | |
1423 | o = bufsize; | |
1424 | memcpy((char*)buffer, PyString_AsString(result), o); // strings only, not unicode... | |
1425 | Py_DECREF(result); | |
1426 | ||
1427 | } | |
1428 | else | |
1429 | m_lasterror = wxSTREAM_READ_ERROR; | |
1430 | wxPyEndBlockThreads(blocked); | |
1431 | return o; | |
1432 | } | |
1433 | ||
1434 | size_t wxPyCBInputStream::OnSysWrite(const void *buffer, size_t bufsize) { | |
1435 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
1436 | return 0; | |
1437 | } | |
1438 | ||
1439 | ||
1440 | wxFileOffset wxPyCBInputStream::OnSysSeek(wxFileOffset off, wxSeekMode mode) { | |
1441 | bool blocked = wxPyBeginBlockThreads(); | |
1442 | PyObject* arglist = PyTuple_New(2); | |
1443 | ||
1444 | if (sizeof(wxFileOffset) > sizeof(long)) | |
1445 | // wxFileOffset is a 64-bit value... | |
1446 | PyTuple_SET_ITEM(arglist, 0, PyLong_FromLongLong(off)); | |
1447 | else | |
1448 | PyTuple_SET_ITEM(arglist, 0, PyInt_FromLong(off)); | |
1449 | ||
1450 | PyTuple_SET_ITEM(arglist, 1, PyInt_FromLong(mode)); | |
1451 | ||
1452 | ||
1453 | PyObject* result = PyEval_CallObject(m_seek, arglist); | |
1454 | Py_DECREF(arglist); | |
1455 | Py_XDECREF(result); | |
1456 | wxPyEndBlockThreads(blocked); | |
1457 | return OnSysTell(); | |
1458 | } | |
1459 | ||
1460 | ||
1461 | wxFileOffset wxPyCBInputStream::OnSysTell() const { | |
1462 | bool blocked = wxPyBeginBlockThreads(); | |
1463 | PyObject* arglist = Py_BuildValue("()"); | |
1464 | PyObject* result = PyEval_CallObject(m_tell, arglist); | |
1465 | Py_DECREF(arglist); | |
1466 | wxFileOffset o = 0; | |
1467 | if (result != NULL) { | |
1468 | if (PyLong_Check(result)) | |
1469 | o = PyLong_AsLongLong(result); | |
1470 | else | |
1471 | o = PyInt_AsLong(result); | |
1472 | Py_DECREF(result); | |
1473 | }; | |
1474 | wxPyEndBlockThreads(blocked); | |
1475 | return o; | |
1476 | } | |
1477 | ||
1478 | //---------------------------------------------------------------------- | |
1479 | ||
1480 | IMPLEMENT_ABSTRACT_CLASS(wxPyCallback, wxEvtHandler); | |
1481 | ||
1482 | wxPyCallback::wxPyCallback(PyObject* func) { | |
1483 | m_func = func; | |
1484 | Py_INCREF(m_func); | |
1485 | } | |
1486 | ||
1487 | wxPyCallback::wxPyCallback(const wxPyCallback& other) { | |
1488 | m_func = other.m_func; | |
1489 | Py_INCREF(m_func); | |
1490 | } | |
1491 | ||
1492 | wxPyCallback::~wxPyCallback() { | |
1493 | bool blocked = wxPyBeginBlockThreads(); | |
1494 | Py_DECREF(m_func); | |
1495 | wxPyEndBlockThreads(blocked); | |
1496 | } | |
1497 | ||
1498 | ||
1499 | #define wxPy_PRECALLINIT "_preCallInit" | |
1500 | #define wxPy_POSTCALLCLEANUP "_postCallCleanup" | |
1501 | ||
1502 | // This function is used for all events destined for Python event handlers. | |
1503 | void wxPyCallback::EventThunker(wxEvent& event) { | |
1504 | wxPyCallback* cb = (wxPyCallback*)event.m_callbackUserData; | |
1505 | PyObject* func = cb->m_func; | |
1506 | PyObject* result; | |
1507 | PyObject* arg; | |
1508 | PyObject* tuple; | |
1509 | bool checkSkip = false; | |
1510 | ||
1511 | bool blocked = wxPyBeginBlockThreads(); | |
1512 | wxString className = event.GetClassInfo()->GetClassName(); | |
1513 | ||
1514 | // If the event is one of these types then pass the original | |
1515 | // event object instead of the one passed to us. | |
1516 | if ( className == wxT("wxPyEvent") ) { | |
1517 | arg = ((wxPyEvent*)&event)->GetSelf(); | |
1518 | checkSkip = ((wxPyEvent*)&event)->GetCloned(); | |
1519 | } | |
1520 | else if ( className == wxT("wxPyCommandEvent") ) { | |
1521 | arg = ((wxPyCommandEvent*)&event)->GetSelf(); | |
1522 | checkSkip = ((wxPyCommandEvent*)&event)->GetCloned(); | |
1523 | } | |
1524 | else { | |
1525 | arg = wxPyConstructObject((void*)&event, className); | |
1526 | } | |
1527 | ||
1528 | if (!arg) { | |
1529 | PyErr_Print(); | |
1530 | } else { | |
1531 | // "intern" the pre/post method names to speed up the HasAttr | |
1532 | static PyObject* s_preName = NULL; | |
1533 | static PyObject* s_postName = NULL; | |
1534 | if (s_preName == NULL) { | |
1535 | s_preName = PyString_FromString(wxPy_PRECALLINIT); | |
1536 | s_postName = PyString_FromString(wxPy_POSTCALLCLEANUP); | |
1537 | } | |
1538 | ||
1539 | // Check if the event object needs some preinitialization | |
1540 | if (PyObject_HasAttr(arg, s_preName)) { | |
1541 | result = PyObject_CallMethodObjArgs(arg, s_preName, arg, NULL); | |
1542 | if ( result ) { | |
1543 | Py_DECREF(result); // result is ignored, but we still need to decref it | |
1544 | PyErr_Clear(); // Just in case... | |
1545 | } else { | |
1546 | PyErr_Print(); | |
1547 | } | |
1548 | } | |
1549 | ||
1550 | // Call the event handler, passing the event object | |
1551 | tuple = PyTuple_New(1); | |
1552 | PyTuple_SET_ITEM(tuple, 0, arg); // steals ref to arg | |
1553 | result = PyEval_CallObject(func, tuple); | |
1554 | if ( result ) { | |
1555 | Py_DECREF(result); // result is ignored, but we still need to decref it | |
1556 | PyErr_Clear(); // Just in case... | |
1557 | } else { | |
1558 | PyErr_Print(); | |
1559 | } | |
1560 | ||
1561 | // Check if the event object needs some post cleanup | |
1562 | if (PyObject_HasAttr(arg, s_postName)) { | |
1563 | result = PyObject_CallMethodObjArgs(arg, s_postName, arg, NULL); | |
1564 | if ( result ) { | |
1565 | Py_DECREF(result); // result is ignored, but we still need to decref it | |
1566 | PyErr_Clear(); // Just in case... | |
1567 | } else { | |
1568 | PyErr_Print(); | |
1569 | } | |
1570 | } | |
1571 | ||
1572 | if ( checkSkip ) { | |
1573 | // if the event object was one of our special types and | |
1574 | // it had been cloned, then we need to extract the Skipped | |
1575 | // value from the original and set it in the clone. | |
1576 | result = PyObject_CallMethod(arg, "GetSkipped", ""); | |
1577 | if ( result ) { | |
1578 | event.Skip(PyInt_AsLong(result)); | |
1579 | Py_DECREF(result); | |
1580 | } else { | |
1581 | PyErr_Print(); | |
1582 | } | |
1583 | } | |
1584 | Py_DECREF(tuple); | |
1585 | } | |
1586 | wxPyEndBlockThreads(blocked); | |
1587 | } | |
1588 | ||
1589 | ||
1590 | //---------------------------------------------------------------------- | |
1591 | ||
1592 | wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper& other) { | |
1593 | m_lastFound = NULL; | |
1594 | m_self = other.m_self; | |
1595 | m_class = other.m_class; | |
1596 | if (m_self) { | |
1597 | Py_INCREF(m_self); | |
1598 | Py_INCREF(m_class); | |
1599 | } | |
1600 | } | |
1601 | ||
1602 | ||
1603 | void wxPyCallbackHelper::setSelf(PyObject* self, PyObject* klass, int incref) { | |
1604 | m_self = self; | |
1605 | m_class = klass; | |
1606 | m_incRef = incref; | |
1607 | if (incref) { | |
1608 | Py_INCREF(m_self); | |
1609 | Py_INCREF(m_class); | |
1610 | } | |
1611 | } | |
1612 | ||
1613 | ||
1614 | #if PYTHON_API_VERSION >= 1011 | |
1615 | ||
1616 | // Prior to Python 2.2 PyMethod_GetClass returned the class object | |
1617 | // in which the method was defined. Starting with 2.2 it returns | |
1618 | // "class that asked for the method" which seems totally bogus to me | |
1619 | // but apprently it fixes some obscure problem waiting to happen in | |
1620 | // Python. Since the API was not documented Guido and the gang felt | |
1621 | // safe in changing it. Needless to say that totally screwed up the | |
1622 | // logic below in wxPyCallbackHelper::findCallback, hence this icky | |
1623 | // code to find the class where the method is actually defined... | |
1624 | ||
1625 | static | |
1626 | PyObject* PyFindClassWithAttr(PyObject *klass, PyObject *name) | |
1627 | { | |
1628 | int i, n; | |
1629 | ||
1630 | if (PyType_Check(klass)) { // new style classes | |
1631 | // This code is borrowed/adapted from _PyType_Lookup in typeobject.c | |
1632 | PyTypeObject* type = (PyTypeObject*)klass; | |
1633 | PyObject *mro, *res, *base, *dict; | |
1634 | /* Look in tp_dict of types in MRO */ | |
1635 | mro = type->tp_mro; | |
1636 | assert(PyTuple_Check(mro)); | |
1637 | n = PyTuple_GET_SIZE(mro); | |
1638 | for (i = 0; i < n; i++) { | |
1639 | base = PyTuple_GET_ITEM(mro, i); | |
1640 | if (PyClass_Check(base)) | |
1641 | dict = ((PyClassObject *)base)->cl_dict; | |
1642 | else { | |
1643 | assert(PyType_Check(base)); | |
1644 | dict = ((PyTypeObject *)base)->tp_dict; | |
1645 | } | |
1646 | assert(dict && PyDict_Check(dict)); | |
1647 | res = PyDict_GetItem(dict, name); | |
1648 | if (res != NULL) | |
1649 | return base; | |
1650 | } | |
1651 | return NULL; | |
1652 | } | |
1653 | ||
1654 | else if (PyClass_Check(klass)) { // old style classes | |
1655 | // This code is borrowed/adapted from class_lookup in classobject.c | |
1656 | PyClassObject* cp = (PyClassObject*)klass; | |
1657 | PyObject *value = PyDict_GetItem(cp->cl_dict, name); | |
1658 | if (value != NULL) { | |
1659 | return (PyObject*)cp; | |
1660 | } | |
1661 | n = PyTuple_Size(cp->cl_bases); | |
1662 | for (i = 0; i < n; i++) { | |
1663 | PyObject* base = PyTuple_GetItem(cp->cl_bases, i); | |
1664 | PyObject *v = PyFindClassWithAttr(base, name); | |
1665 | if (v != NULL) | |
1666 | return v; | |
1667 | } | |
1668 | return NULL; | |
1669 | } | |
1670 | return NULL; | |
1671 | } | |
1672 | #endif | |
1673 | ||
1674 | ||
1675 | static | |
1676 | PyObject* PyMethod_GetDefiningClass(PyObject* method, const char* name) | |
1677 | { | |
1678 | PyObject* mgc = PyMethod_GET_CLASS(method); | |
1679 | ||
1680 | #if PYTHON_API_VERSION <= 1010 // prior to Python 2.2, the easy way | |
1681 | return mgc; | |
1682 | #else // 2.2 and after, the hard way... | |
1683 | ||
1684 | PyObject* nameo = PyString_FromString(name); | |
1685 | PyObject* klass = PyFindClassWithAttr(mgc, nameo); | |
1686 | Py_DECREF(nameo); | |
1687 | return klass; | |
1688 | #endif | |
1689 | } | |
1690 | ||
1691 | ||
1692 | ||
1693 | bool wxPyCallbackHelper::findCallback(const char* name) const { | |
1694 | wxPyCallbackHelper* self = (wxPyCallbackHelper*)this; // cast away const | |
1695 | self->m_lastFound = NULL; | |
1696 | ||
1697 | // If the object (m_self) has an attibute of the given name... | |
1698 | if (m_self && PyObject_HasAttrString(m_self, (char*)name)) { | |
1699 | PyObject *method, *klass; | |
1700 | method = PyObject_GetAttrString(m_self, (char*)name); | |
1701 | ||
1702 | // ...and if that attribute is a method, and if that method's class is | |
1703 | // not from a base class... | |
1704 | if (PyMethod_Check(method) && | |
1705 | (klass = PyMethod_GetDefiningClass(method, (char*)name)) != NULL && | |
1706 | ((klass == m_class) || PyObject_IsSubclass(klass, m_class))) { | |
1707 | ||
1708 | // ...then we'll save a pointer to the method so callCallback can call it. | |
1709 | self->m_lastFound = method; | |
1710 | } | |
1711 | else { | |
1712 | Py_DECREF(method); | |
1713 | } | |
1714 | } | |
1715 | return m_lastFound != NULL; | |
1716 | } | |
1717 | ||
1718 | ||
1719 | int wxPyCallbackHelper::callCallback(PyObject* argTuple) const { | |
1720 | PyObject* result; | |
1721 | int retval = false; | |
1722 | ||
1723 | result = callCallbackObj(argTuple); | |
1724 | if (result) { // Assumes an integer return type... | |
1725 | retval = PyInt_AsLong(result); | |
1726 | Py_DECREF(result); | |
1727 | PyErr_Clear(); // forget about it if it's not... | |
1728 | } | |
1729 | return retval; | |
1730 | } | |
1731 | ||
1732 | // Invoke the Python callable object, returning the raw PyObject return | |
1733 | // value. Caller should DECREF the return value and also manage the GIL. | |
1734 | PyObject* wxPyCallbackHelper::callCallbackObj(PyObject* argTuple) const { | |
1735 | PyObject* result; | |
1736 | ||
1737 | // Save a copy of the pointer in case the callback generates another | |
1738 | // callback. In that case m_lastFound will have a different value when | |
1739 | // it gets back here... | |
1740 | PyObject* method = m_lastFound; | |
1741 | ||
1742 | result = PyEval_CallObject(method, argTuple); | |
1743 | Py_DECREF(argTuple); | |
1744 | Py_DECREF(method); | |
1745 | if (!result) { | |
1746 | PyErr_Print(); | |
1747 | } | |
1748 | return result; | |
1749 | } | |
1750 | ||
1751 | ||
1752 | void wxPyCBH_setCallbackInfo(wxPyCallbackHelper& cbh, PyObject* self, PyObject* klass, int incref) { | |
1753 | cbh.setSelf(self, klass, incref); | |
1754 | } | |
1755 | ||
1756 | bool wxPyCBH_findCallback(const wxPyCallbackHelper& cbh, const char* name) { | |
1757 | return cbh.findCallback(name); | |
1758 | } | |
1759 | ||
1760 | int wxPyCBH_callCallback(const wxPyCallbackHelper& cbh, PyObject* argTuple) { | |
1761 | return cbh.callCallback(argTuple); | |
1762 | } | |
1763 | ||
1764 | PyObject* wxPyCBH_callCallbackObj(const wxPyCallbackHelper& cbh, PyObject* argTuple) { | |
1765 | return cbh.callCallbackObj(argTuple); | |
1766 | } | |
1767 | ||
1768 | ||
1769 | void wxPyCBH_delete(wxPyCallbackHelper* cbh) { | |
1770 | if (cbh->m_incRef) { | |
1771 | bool blocked = wxPyBeginBlockThreads(); | |
1772 | Py_XDECREF(cbh->m_self); | |
1773 | Py_XDECREF(cbh->m_class); | |
1774 | wxPyEndBlockThreads(blocked); | |
1775 | } | |
1776 | } | |
1777 | ||
1778 | //--------------------------------------------------------------------------- | |
1779 | //--------------------------------------------------------------------------- | |
1780 | // These event classes can be derived from in Python and passed through the event | |
1781 | // system without losing anything. They do this by keeping a reference to | |
1782 | // themselves and some special case handling in wxPyCallback::EventThunker. | |
1783 | ||
1784 | ||
1785 | wxPyEvtSelfRef::wxPyEvtSelfRef() { | |
1786 | //m_self = Py_None; // **** We don't do normal ref counting to prevent | |
1787 | //Py_INCREF(m_self); // circular loops... | |
1788 | m_cloned = false; | |
1789 | } | |
1790 | ||
1791 | wxPyEvtSelfRef::~wxPyEvtSelfRef() { | |
1792 | bool blocked = wxPyBeginBlockThreads(); | |
1793 | if (m_cloned) | |
1794 | Py_DECREF(m_self); | |
1795 | wxPyEndBlockThreads(blocked); | |
1796 | } | |
1797 | ||
1798 | void wxPyEvtSelfRef::SetSelf(PyObject* self, bool clone) { | |
1799 | bool blocked = wxPyBeginBlockThreads(); | |
1800 | if (m_cloned) | |
1801 | Py_DECREF(m_self); | |
1802 | m_self = self; | |
1803 | if (clone) { | |
1804 | Py_INCREF(m_self); | |
1805 | m_cloned = true; | |
1806 | } | |
1807 | wxPyEndBlockThreads(blocked); | |
1808 | } | |
1809 | ||
1810 | PyObject* wxPyEvtSelfRef::GetSelf() const { | |
1811 | Py_INCREF(m_self); | |
1812 | return m_self; | |
1813 | } | |
1814 | ||
1815 | ||
1816 | IMPLEMENT_ABSTRACT_CLASS(wxPyEvent, wxEvent); | |
1817 | IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent, wxCommandEvent); | |
1818 | ||
1819 | ||
1820 | wxPyEvent::wxPyEvent(int winid, wxEventType commandType) | |
1821 | : wxEvent(winid, commandType) { | |
1822 | } | |
1823 | ||
1824 | ||
1825 | wxPyEvent::wxPyEvent(const wxPyEvent& evt) | |
1826 | : wxEvent(evt) | |
1827 | { | |
1828 | SetSelf(evt.m_self, true); | |
1829 | } | |
1830 | ||
1831 | ||
1832 | wxPyEvent::~wxPyEvent() { | |
1833 | } | |
1834 | ||
1835 | ||
1836 | wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType, int id) | |
1837 | : wxCommandEvent(commandType, id) { | |
1838 | } | |
1839 | ||
1840 | ||
1841 | wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent& evt) | |
1842 | : wxCommandEvent(evt) | |
1843 | { | |
1844 | SetSelf(evt.m_self, true); | |
1845 | } | |
1846 | ||
1847 | ||
1848 | wxPyCommandEvent::~wxPyCommandEvent() { | |
1849 | } | |
1850 | ||
1851 | ||
1852 | ||
1853 | ||
1854 | ||
1855 | //--------------------------------------------------------------------------- | |
1856 | //--------------------------------------------------------------------------- | |
1857 | // Convert a wxList to a Python List, only works for lists of wxObjects | |
1858 | ||
1859 | PyObject* wxPy_ConvertList(wxListBase* listbase) { | |
1860 | wxList* list = (wxList*)listbase; // this is probably bad... | |
1861 | PyObject* pyList; | |
1862 | PyObject* pyObj; | |
1863 | wxObject* wxObj; | |
1864 | wxNode* node = list->GetFirst(); | |
1865 | ||
1866 | bool blocked = wxPyBeginBlockThreads(); | |
1867 | pyList = PyList_New(0); | |
1868 | while (node) { | |
1869 | wxObj = node->GetData(); | |
1870 | pyObj = wxPyMake_wxObject(wxObj,false); | |
1871 | PyList_Append(pyList, pyObj); | |
1872 | node = node->GetNext(); | |
1873 | } | |
1874 | wxPyEndBlockThreads(blocked); | |
1875 | return pyList; | |
1876 | } | |
1877 | ||
1878 | //---------------------------------------------------------------------- | |
1879 | ||
1880 | long wxPyGetWinHandle(wxWindow* win) { | |
1881 | ||
1882 | #ifdef __WXMSW__ | |
1883 | return (long)win->GetHandle(); | |
1884 | #endif | |
1885 | ||
1886 | #if defined(__WXGTK__) || defined(__WXX11) | |
1887 | return (long)GetXWindow(win); | |
1888 | #endif | |
1889 | ||
1890 | #ifdef __WXMAC__ | |
1891 | //return (long)MAC_WXHWND(win->MacGetTopLevelWindowRef()); | |
1892 | return (long)win->GetHandle(); | |
1893 | #endif | |
1894 | ||
1895 | return 0; | |
1896 | } | |
1897 | ||
1898 | //---------------------------------------------------------------------- | |
1899 | // Some helper functions for typemaps in my_typemaps.i, so they won't be | |
1900 | // included in every file over and over again... | |
1901 | ||
1902 | wxString* wxString_in_helper(PyObject* source) { | |
1903 | wxString* target = NULL; | |
1904 | ||
1905 | if (!PyString_Check(source) && !PyUnicode_Check(source)) { | |
1906 | PyErr_SetString(PyExc_TypeError, "String or Unicode type required"); | |
1907 | return NULL; | |
1908 | } | |
1909 | #if wxUSE_UNICODE | |
1910 | PyObject* uni = source; | |
1911 | if (PyString_Check(source)) { | |
1912 | uni = PyUnicode_FromEncodedObject(source, wxPyDefaultEncoding, "strict"); | |
1913 | if (PyErr_Occurred()) return NULL; | |
1914 | } | |
1915 | target = new wxString(); | |
1916 | size_t len = PyUnicode_GET_SIZE(uni); | |
1917 | if (len) { | |
1918 | PyUnicode_AsWideChar((PyUnicodeObject*)uni, target->GetWriteBuf(len), len); | |
1919 | target->UngetWriteBuf(len); | |
1920 | } | |
1921 | ||
1922 | if (PyString_Check(source)) | |
1923 | Py_DECREF(uni); | |
1924 | #else | |
1925 | // Convert to a string object if it isn't already, then to wxString | |
1926 | PyObject* str = source; | |
1927 | if (PyUnicode_Check(source)) { | |
1928 | str = PyUnicode_AsEncodedString(source, wxPyDefaultEncoding, "strict"); | |
1929 | if (PyErr_Occurred()) return NULL; | |
1930 | } | |
1931 | else if (!PyString_Check(source)) { | |
1932 | str = PyObject_Str(source); | |
1933 | if (PyErr_Occurred()) return NULL; | |
1934 | } | |
1935 | char* tmpPtr; int tmpSize; | |
1936 | PyString_AsStringAndSize(str, &tmpPtr, &tmpSize); | |
1937 | target = new wxString(tmpPtr, tmpSize); | |
1938 | ||
1939 | if (!PyString_Check(source)) | |
1940 | Py_DECREF(str); | |
1941 | #endif // wxUSE_UNICODE | |
1942 | ||
1943 | return target; | |
1944 | } | |
1945 | ||
1946 | ||
1947 | // Similar to above except doesn't use "new" and doesn't set an exception | |
1948 | wxString Py2wxString(PyObject* source) | |
1949 | { | |
1950 | wxString target; | |
1951 | ||
1952 | #if wxUSE_UNICODE | |
1953 | // Convert to a unicode object, if not already, then to a wxString | |
1954 | PyObject* uni = source; | |
1955 | if (!PyUnicode_Check(source)) { | |
1956 | uni = PyUnicode_FromEncodedObject(source, wxPyDefaultEncoding, "strict"); | |
1957 | if (PyErr_Occurred()) return wxEmptyString; // TODO: should we PyErr_Clear? | |
1958 | } | |
1959 | size_t len = PyUnicode_GET_SIZE(uni); | |
1960 | if (len) { | |
1961 | PyUnicode_AsWideChar((PyUnicodeObject*)uni, target.GetWriteBuf(len), len); | |
1962 | target.UngetWriteBuf(); | |
1963 | } | |
1964 | ||
1965 | if (!PyUnicode_Check(source)) | |
1966 | Py_DECREF(uni); | |
1967 | #else | |
1968 | // Convert to a string object if it isn't already, then to wxString | |
1969 | PyObject* str = source; | |
1970 | if (PyUnicode_Check(source)) { | |
1971 | str = PyUnicode_AsEncodedString(source, wxPyDefaultEncoding, "strict"); | |
1972 | if (PyErr_Occurred()) return wxEmptyString; // TODO: should we PyErr_Clear? | |
1973 | } | |
1974 | else if (!PyString_Check(source)) { | |
1975 | str = PyObject_Str(source); | |
1976 | if (PyErr_Occurred()) return wxEmptyString; // TODO: should we PyErr_Clear? | |
1977 | } | |
1978 | char* tmpPtr; int tmpSize; | |
1979 | PyString_AsStringAndSize(str, &tmpPtr, &tmpSize); | |
1980 | target = wxString(tmpPtr, tmpSize); | |
1981 | ||
1982 | if (!PyString_Check(source)) | |
1983 | Py_DECREF(str); | |
1984 | #endif // wxUSE_UNICODE | |
1985 | ||
1986 | return target; | |
1987 | } | |
1988 | ||
1989 | ||
1990 | // Make either a Python String or Unicode object, depending on build mode | |
1991 | PyObject* wx2PyString(const wxString& src) | |
1992 | { | |
1993 | PyObject* str; | |
1994 | #if wxUSE_UNICODE | |
1995 | str = PyUnicode_FromWideChar(src.c_str(), src.Len()); | |
1996 | #else | |
1997 | str = PyString_FromStringAndSize(src.c_str(), src.Len()); | |
1998 | #endif | |
1999 | return str; | |
2000 | } | |
2001 | ||
2002 | ||
2003 | ||
2004 | void wxSetDefaultPyEncoding(const char* encoding) | |
2005 | { | |
2006 | strncpy(wxPyDefaultEncoding, encoding, DEFAULTENCODING_SIZE); | |
2007 | } | |
2008 | ||
2009 | const char* wxGetDefaultPyEncoding() | |
2010 | { | |
2011 | return wxPyDefaultEncoding; | |
2012 | } | |
2013 | ||
2014 | //---------------------------------------------------------------------- | |
2015 | ||
2016 | ||
2017 | byte* byte_LIST_helper(PyObject* source) { | |
2018 | if (!PyList_Check(source)) { | |
2019 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
2020 | return NULL; | |
2021 | } | |
2022 | int count = PyList_Size(source); | |
2023 | byte* temp = new byte[count]; | |
2024 | if (! temp) { | |
2025 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
2026 | return NULL; | |
2027 | } | |
2028 | for (int x=0; x<count; x++) { | |
2029 | PyObject* o = PyList_GetItem(source, x); | |
2030 | if (! PyInt_Check(o)) { | |
2031 | PyErr_SetString(PyExc_TypeError, "Expected a list of integers."); | |
2032 | return NULL; | |
2033 | } | |
2034 | temp[x] = (byte)PyInt_AsLong(o); | |
2035 | } | |
2036 | return temp; | |
2037 | } | |
2038 | ||
2039 | ||
2040 | int* int_LIST_helper(PyObject* source) { | |
2041 | if (!PyList_Check(source)) { | |
2042 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
2043 | return NULL; | |
2044 | } | |
2045 | int count = PyList_Size(source); | |
2046 | int* temp = new int[count]; | |
2047 | if (! temp) { | |
2048 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
2049 | return NULL; | |
2050 | } | |
2051 | for (int x=0; x<count; x++) { | |
2052 | PyObject* o = PyList_GetItem(source, x); | |
2053 | if (! PyInt_Check(o)) { | |
2054 | PyErr_SetString(PyExc_TypeError, "Expected a list of integers."); | |
2055 | return NULL; | |
2056 | } | |
2057 | temp[x] = PyInt_AsLong(o); | |
2058 | } | |
2059 | return temp; | |
2060 | } | |
2061 | ||
2062 | ||
2063 | long* long_LIST_helper(PyObject* source) { | |
2064 | if (!PyList_Check(source)) { | |
2065 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
2066 | return NULL; | |
2067 | } | |
2068 | int count = PyList_Size(source); | |
2069 | long* temp = new long[count]; | |
2070 | if (! temp) { | |
2071 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
2072 | return NULL; | |
2073 | } | |
2074 | for (int x=0; x<count; x++) { | |
2075 | PyObject* o = PyList_GetItem(source, x); | |
2076 | if (! PyInt_Check(o)) { | |
2077 | PyErr_SetString(PyExc_TypeError, "Expected a list of integers."); | |
2078 | return NULL; | |
2079 | } | |
2080 | temp[x] = PyInt_AsLong(o); | |
2081 | } | |
2082 | return temp; | |
2083 | } | |
2084 | ||
2085 | ||
2086 | char** string_LIST_helper(PyObject* source) { | |
2087 | if (!PyList_Check(source)) { | |
2088 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
2089 | return NULL; | |
2090 | } | |
2091 | int count = PyList_Size(source); | |
2092 | char** temp = new char*[count]; | |
2093 | if (! temp) { | |
2094 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
2095 | return NULL; | |
2096 | } | |
2097 | for (int x=0; x<count; x++) { | |
2098 | PyObject* o = PyList_GetItem(source, x); | |
2099 | if (! PyString_Check(o)) { | |
2100 | PyErr_SetString(PyExc_TypeError, "Expected a list of strings."); | |
2101 | return NULL; | |
2102 | } | |
2103 | temp[x] = PyString_AsString(o); | |
2104 | } | |
2105 | return temp; | |
2106 | } | |
2107 | ||
2108 | //-------------------------------- | |
2109 | // Part of patch from Tim Hochberg | |
2110 | static inline bool wxPointFromObjects(PyObject* o1, PyObject* o2, wxPoint* point) { | |
2111 | if (PyInt_Check(o1) && PyInt_Check(o2)) { | |
2112 | point->x = PyInt_AS_LONG(o1); | |
2113 | point->y = PyInt_AS_LONG(o2); | |
2114 | return true; | |
2115 | } | |
2116 | if (PyFloat_Check(o1) && PyFloat_Check(o2)) { | |
2117 | point->x = (int)PyFloat_AS_DOUBLE(o1); | |
2118 | point->y = (int)PyFloat_AS_DOUBLE(o2); | |
2119 | return true; | |
2120 | } | |
2121 | if (wxPySwigInstance_Check(o1) || wxPySwigInstance_Check(o2)) { // TODO: Why??? | |
2122 | // Disallow instances because they can cause havok | |
2123 | return false; | |
2124 | } | |
2125 | if (PyNumber_Check(o1) && PyNumber_Check(o2)) { | |
2126 | // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2 | |
2127 | point->x = PyInt_AsLong(o1); | |
2128 | point->y = PyInt_AsLong(o2); | |
2129 | return true; | |
2130 | } | |
2131 | return false; | |
2132 | } | |
2133 | ||
2134 | ||
2135 | wxPoint* wxPoint_LIST_helper(PyObject* source, int *count) { | |
2136 | // Putting all of the declarations here allows | |
2137 | // us to put the error handling all in one place. | |
2138 | int x; | |
2139 | wxPoint* temp; | |
2140 | PyObject *o, *o1, *o2; | |
2141 | bool isFast = PyList_Check(source) || PyTuple_Check(source); | |
2142 | ||
2143 | if (!PySequence_Check(source)) { | |
2144 | goto error0; | |
2145 | } | |
2146 | ||
2147 | // The length of the sequence is returned in count. | |
2148 | *count = PySequence_Length(source); | |
2149 | if (*count < 0) { | |
2150 | goto error0; | |
2151 | } | |
2152 | ||
2153 | temp = new wxPoint[*count]; | |
2154 | if (!temp) { | |
2155 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
2156 | return NULL; | |
2157 | } | |
2158 | for (x=0; x<*count; x++) { | |
2159 | // Get an item: try fast way first. | |
2160 | if (isFast) { | |
2161 | o = PySequence_Fast_GET_ITEM(source, x); | |
2162 | } | |
2163 | else { | |
2164 | o = PySequence_GetItem(source, x); | |
2165 | if (o == NULL) { | |
2166 | goto error1; | |
2167 | } | |
2168 | } | |
2169 | ||
2170 | // Convert o to wxPoint. | |
2171 | if ((PyTuple_Check(o) && PyTuple_GET_SIZE(o) == 2) || | |
2172 | (PyList_Check(o) && PyList_GET_SIZE(o) == 2)) { | |
2173 | o1 = PySequence_Fast_GET_ITEM(o, 0); | |
2174 | o2 = PySequence_Fast_GET_ITEM(o, 1); | |
2175 | if (!wxPointFromObjects(o1, o2, &temp[x])) { | |
2176 | goto error2; | |
2177 | } | |
2178 | } | |
2179 | else if (wxPySwigInstance_Check(o)) { | |
2180 | wxPoint* pt; | |
2181 | if (! wxPyConvertSwigPtr(o, (void **)&pt, wxT("wxPoint"))) { | |
2182 | goto error2; | |
2183 | } | |
2184 | temp[x] = *pt; | |
2185 | } | |
2186 | else if (PySequence_Check(o) && PySequence_Length(o) == 2) { | |
2187 | o1 = PySequence_GetItem(o, 0); | |
2188 | o2 = PySequence_GetItem(o, 1); | |
2189 | if (!wxPointFromObjects(o1, o2, &temp[x])) { | |
2190 | goto error3; | |
2191 | } | |
2192 | Py_DECREF(o1); | |
2193 | Py_DECREF(o2); | |
2194 | } | |
2195 | else { | |
2196 | goto error2; | |
2197 | } | |
2198 | // Clean up. | |
2199 | if (!isFast) | |
2200 | Py_DECREF(o); | |
2201 | } | |
2202 | return temp; | |
2203 | ||
2204 | error3: | |
2205 | Py_DECREF(o1); | |
2206 | Py_DECREF(o2); | |
2207 | error2: | |
2208 | if (!isFast) | |
2209 | Py_DECREF(o); | |
2210 | error1: | |
2211 | delete [] temp; | |
2212 | error0: | |
2213 | PyErr_SetString(PyExc_TypeError, "Expected a sequence of length-2 sequences or wxPoints."); | |
2214 | return NULL; | |
2215 | } | |
2216 | // end of patch | |
2217 | //------------------------------ | |
2218 | ||
2219 | ||
2220 | wxBitmap** wxBitmap_LIST_helper(PyObject* source) { | |
2221 | if (!PyList_Check(source)) { | |
2222 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
2223 | return NULL; | |
2224 | } | |
2225 | int count = PyList_Size(source); | |
2226 | wxBitmap** temp = new wxBitmap*[count]; | |
2227 | if (! temp) { | |
2228 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
2229 | return NULL; | |
2230 | } | |
2231 | for (int x=0; x<count; x++) { | |
2232 | PyObject* o = PyList_GetItem(source, x); | |
2233 | if (wxPySwigInstance_Check(o)) { | |
2234 | wxBitmap* pt; | |
2235 | if (! wxPyConvertSwigPtr(o, (void **) &pt, wxT("wxBitmap"))) { | |
2236 | PyErr_SetString(PyExc_TypeError,"Expected wxBitmap."); | |
2237 | return NULL; | |
2238 | } | |
2239 | temp[x] = pt; | |
2240 | } | |
2241 | else { | |
2242 | PyErr_SetString(PyExc_TypeError, "Expected a list of wxBitmaps."); | |
2243 | return NULL; | |
2244 | } | |
2245 | } | |
2246 | return temp; | |
2247 | } | |
2248 | ||
2249 | ||
2250 | ||
2251 | wxString* wxString_LIST_helper(PyObject* source) { | |
2252 | if (!PyList_Check(source)) { | |
2253 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
2254 | return NULL; | |
2255 | } | |
2256 | int count = PyList_Size(source); | |
2257 | wxString* temp = new wxString[count]; | |
2258 | if (! temp) { | |
2259 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
2260 | return NULL; | |
2261 | } | |
2262 | for (int x=0; x<count; x++) { | |
2263 | PyObject* o = PyList_GetItem(source, x); | |
2264 | #if PYTHON_API_VERSION >= 1009 | |
2265 | if (! PyString_Check(o) && ! PyUnicode_Check(o)) { | |
2266 | PyErr_SetString(PyExc_TypeError, "Expected a list of string or unicode objects."); | |
2267 | return NULL; | |
2268 | } | |
2269 | #else | |
2270 | if (! PyString_Check(o)) { | |
2271 | PyErr_SetString(PyExc_TypeError, "Expected a list of strings."); | |
2272 | return NULL; | |
2273 | } | |
2274 | #endif | |
2275 | ||
2276 | wxString* pStr = wxString_in_helper(o); | |
2277 | temp[x] = *pStr; | |
2278 | delete pStr; | |
2279 | } | |
2280 | return temp; | |
2281 | } | |
2282 | ||
2283 | ||
2284 | wxAcceleratorEntry* wxAcceleratorEntry_LIST_helper(PyObject* source) { | |
2285 | if (!PyList_Check(source)) { | |
2286 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
2287 | return NULL; | |
2288 | } | |
2289 | int count = PyList_Size(source); | |
2290 | wxAcceleratorEntry* temp = new wxAcceleratorEntry[count]; | |
2291 | if (! temp) { | |
2292 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
2293 | return NULL; | |
2294 | } | |
2295 | for (int x=0; x<count; x++) { | |
2296 | PyObject* o = PyList_GetItem(source, x); | |
2297 | if (wxPySwigInstance_Check(o)) { | |
2298 | wxAcceleratorEntry* ae; | |
2299 | if (! wxPyConvertSwigPtr(o, (void **) &ae, wxT("wxAcceleratorEntry"))) { | |
2300 | PyErr_SetString(PyExc_TypeError,"Expected wxAcceleratorEntry."); | |
2301 | return NULL; | |
2302 | } | |
2303 | temp[x] = *ae; | |
2304 | } | |
2305 | else if (PyTuple_Check(o)) { | |
2306 | PyObject* o1 = PyTuple_GetItem(o, 0); | |
2307 | PyObject* o2 = PyTuple_GetItem(o, 1); | |
2308 | PyObject* o3 = PyTuple_GetItem(o, 2); | |
2309 | temp[x].Set(PyInt_AsLong(o1), PyInt_AsLong(o2), PyInt_AsLong(o3)); | |
2310 | } | |
2311 | else { | |
2312 | PyErr_SetString(PyExc_TypeError, "Expected a list of 3-tuples or wxAcceleratorEntry objects."); | |
2313 | return NULL; | |
2314 | } | |
2315 | } | |
2316 | return temp; | |
2317 | } | |
2318 | ||
2319 | ||
2320 | wxPen** wxPen_LIST_helper(PyObject* source) { | |
2321 | if (!PyList_Check(source)) { | |
2322 | PyErr_SetString(PyExc_TypeError, "Expected a list object."); | |
2323 | return NULL; | |
2324 | } | |
2325 | int count = PyList_Size(source); | |
2326 | wxPen** temp = new wxPen*[count]; | |
2327 | if (!temp) { | |
2328 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array"); | |
2329 | return NULL; | |
2330 | } | |
2331 | for (int x=0; x<count; x++) { | |
2332 | PyObject* o = PyList_GetItem(source, x); | |
2333 | if (wxPySwigInstance_Check(o)) { | |
2334 | wxPen* pt; | |
2335 | if (! wxPyConvertSwigPtr(o, (void **)&pt, wxT("wxPen"))) { | |
2336 | delete temp; | |
2337 | PyErr_SetString(PyExc_TypeError,"Expected wxPen."); | |
2338 | return NULL; | |
2339 | } | |
2340 | temp[x] = pt; | |
2341 | } | |
2342 | else { | |
2343 | delete temp; | |
2344 | PyErr_SetString(PyExc_TypeError, "Expected a list of wxPens."); | |
2345 | return NULL; | |
2346 | } | |
2347 | } | |
2348 | return temp; | |
2349 | } | |
2350 | ||
2351 | ||
2352 | bool wxPy2int_seq_helper(PyObject* source, int* i1, int* i2) { | |
2353 | bool isFast = PyList_Check(source) || PyTuple_Check(source); | |
2354 | PyObject *o1, *o2; | |
2355 | ||
2356 | if (!PySequence_Check(source) || PySequence_Length(source) != 2) | |
2357 | return false; | |
2358 | ||
2359 | if (isFast) { | |
2360 | o1 = PySequence_Fast_GET_ITEM(source, 0); | |
2361 | o2 = PySequence_Fast_GET_ITEM(source, 1); | |
2362 | } | |
2363 | else { | |
2364 | o1 = PySequence_GetItem(source, 0); | |
2365 | o2 = PySequence_GetItem(source, 1); | |
2366 | } | |
2367 | ||
2368 | *i1 = PyInt_AsLong(o1); | |
2369 | *i2 = PyInt_AsLong(o2); | |
2370 | ||
2371 | if (! isFast) { | |
2372 | Py_DECREF(o1); | |
2373 | Py_DECREF(o2); | |
2374 | } | |
2375 | return true; | |
2376 | } | |
2377 | ||
2378 | ||
2379 | bool wxPy4int_seq_helper(PyObject* source, int* i1, int* i2, int* i3, int* i4) { | |
2380 | bool isFast = PyList_Check(source) || PyTuple_Check(source); | |
2381 | PyObject *o1, *o2, *o3, *o4; | |
2382 | ||
2383 | if (!PySequence_Check(source) || PySequence_Length(source) != 4) | |
2384 | return false; | |
2385 | ||
2386 | if (isFast) { | |
2387 | o1 = PySequence_Fast_GET_ITEM(source, 0); | |
2388 | o2 = PySequence_Fast_GET_ITEM(source, 1); | |
2389 | o3 = PySequence_Fast_GET_ITEM(source, 2); | |
2390 | o4 = PySequence_Fast_GET_ITEM(source, 3); | |
2391 | } | |
2392 | else { | |
2393 | o1 = PySequence_GetItem(source, 0); | |
2394 | o2 = PySequence_GetItem(source, 1); | |
2395 | o3 = PySequence_GetItem(source, 2); | |
2396 | o4 = PySequence_GetItem(source, 3); | |
2397 | } | |
2398 | ||
2399 | *i1 = PyInt_AsLong(o1); | |
2400 | *i2 = PyInt_AsLong(o2); | |
2401 | *i3 = PyInt_AsLong(o3); | |
2402 | *i4 = PyInt_AsLong(o4); | |
2403 | ||
2404 | if (! isFast) { | |
2405 | Py_DECREF(o1); | |
2406 | Py_DECREF(o2); | |
2407 | Py_DECREF(o3); | |
2408 | Py_DECREF(o4); | |
2409 | } | |
2410 | return true; | |
2411 | } | |
2412 | ||
2413 | ||
2414 | //---------------------------------------------------------------------- | |
2415 | ||
2416 | bool wxPySimple_typecheck(PyObject* source, const wxChar* classname, int seqLen) | |
2417 | { | |
2418 | void* ptr; | |
2419 | ||
2420 | if (wxPySwigInstance_Check(source) && | |
2421 | wxPyConvertSwigPtr(source, (void **)&ptr, classname)) | |
2422 | return true; | |
2423 | ||
2424 | PyErr_Clear(); | |
2425 | if (PySequence_Check(source) && PySequence_Length(source) == seqLen) | |
2426 | return true; | |
2427 | ||
2428 | return false; | |
2429 | } | |
2430 | ||
2431 | bool wxSize_helper(PyObject* source, wxSize** obj) | |
2432 | { | |
2433 | if (source == Py_None) { | |
2434 | **obj = wxSize(-1,-1); | |
2435 | return true; | |
2436 | } | |
2437 | return wxPyTwoIntItem_helper(source, obj, wxT("wxSize")); | |
2438 | } | |
2439 | ||
2440 | ||
2441 | bool wxPoint_helper(PyObject* source, wxPoint** obj) | |
2442 | { | |
2443 | if (source == Py_None) { | |
2444 | **obj = wxPoint(-1,-1); | |
2445 | return true; | |
2446 | } | |
2447 | return wxPyTwoIntItem_helper(source, obj, wxT("wxPoint")); | |
2448 | } | |
2449 | ||
2450 | ||
2451 | ||
2452 | bool wxRealPoint_helper(PyObject* source, wxRealPoint** obj) { | |
2453 | ||
2454 | if (source == Py_None) { | |
2455 | **obj = wxRealPoint(-1,-1); | |
2456 | return true; | |
2457 | } | |
2458 | ||
2459 | // If source is an object instance then it may already be the right type | |
2460 | if (wxPySwigInstance_Check(source)) { | |
2461 | wxRealPoint* ptr; | |
2462 | if (! wxPyConvertSwigPtr(source, (void **)&ptr, wxT("wxRealPoint"))) | |
2463 | goto error; | |
2464 | *obj = ptr; | |
2465 | return true; | |
2466 | } | |
2467 | // otherwise a 2-tuple of floats is expected | |
2468 | else if (PySequence_Check(source) && PyObject_Length(source) == 2) { | |
2469 | PyObject* o1 = PySequence_GetItem(source, 0); | |
2470 | PyObject* o2 = PySequence_GetItem(source, 1); | |
2471 | if (!PyNumber_Check(o1) || !PyNumber_Check(o2)) { | |
2472 | Py_DECREF(o1); | |
2473 | Py_DECREF(o2); | |
2474 | goto error; | |
2475 | } | |
2476 | **obj = wxRealPoint(PyFloat_AsDouble(o1), PyFloat_AsDouble(o2)); | |
2477 | Py_DECREF(o1); | |
2478 | Py_DECREF(o2); | |
2479 | return true; | |
2480 | } | |
2481 | ||
2482 | error: | |
2483 | PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of floats or a wxRealPoint object."); | |
2484 | return false; | |
2485 | } | |
2486 | ||
2487 | ||
2488 | ||
2489 | bool wxRect_helper(PyObject* source, wxRect** obj) { | |
2490 | ||
2491 | if (source == Py_None) { | |
2492 | **obj = wxRect(-1,-1,-1,-1); | |
2493 | return true; | |
2494 | } | |
2495 | ||
2496 | // If source is an object instance then it may already be the right type | |
2497 | if (wxPySwigInstance_Check(source)) { | |
2498 | wxRect* ptr; | |
2499 | if (! wxPyConvertSwigPtr(source, (void **)&ptr, wxT("wxRect"))) | |
2500 | goto error; | |
2501 | *obj = ptr; | |
2502 | return true; | |
2503 | } | |
2504 | // otherwise a 4-tuple of integers is expected | |
2505 | else if (PySequence_Check(source) && PyObject_Length(source) == 4) { | |
2506 | PyObject* o1 = PySequence_GetItem(source, 0); | |
2507 | PyObject* o2 = PySequence_GetItem(source, 1); | |
2508 | PyObject* o3 = PySequence_GetItem(source, 2); | |
2509 | PyObject* o4 = PySequence_GetItem(source, 3); | |
2510 | if (!PyNumber_Check(o1) || !PyNumber_Check(o2) || | |
2511 | !PyNumber_Check(o3) || !PyNumber_Check(o4)) { | |
2512 | Py_DECREF(o1); | |
2513 | Py_DECREF(o2); | |
2514 | Py_DECREF(o3); | |
2515 | Py_DECREF(o4); | |
2516 | goto error; | |
2517 | } | |
2518 | **obj = wxRect(PyInt_AsLong(o1), PyInt_AsLong(o2), | |
2519 | PyInt_AsLong(o3), PyInt_AsLong(o4)); | |
2520 | Py_DECREF(o1); | |
2521 | Py_DECREF(o2); | |
2522 | Py_DECREF(o3); | |
2523 | Py_DECREF(o4); | |
2524 | return true; | |
2525 | } | |
2526 | ||
2527 | error: | |
2528 | PyErr_SetString(PyExc_TypeError, "Expected a 4-tuple of integers or a wxRect object."); | |
2529 | return false; | |
2530 | } | |
2531 | ||
2532 | ||
2533 | ||
2534 | bool wxColour_helper(PyObject* source, wxColour** obj) { | |
2535 | ||
2536 | if (source == Py_None) { | |
2537 | **obj = wxNullColour; | |
2538 | return true; | |
2539 | } | |
2540 | ||
2541 | // If source is an object instance then it may already be the right type | |
2542 | if (wxPySwigInstance_Check(source)) { | |
2543 | wxColour* ptr; | |
2544 | if (! wxPyConvertSwigPtr(source, (void **)&ptr, wxT("wxColour"))) | |
2545 | goto error; | |
2546 | *obj = ptr; | |
2547 | return true; | |
2548 | } | |
2549 | // otherwise check for a string | |
2550 | else if (PyString_Check(source) || PyUnicode_Check(source)) { | |
2551 | wxString spec = Py2wxString(source); | |
2552 | if (spec.GetChar(0) == '#' && spec.Length() == 7) { // It's #RRGGBB | |
2553 | long red, green, blue; | |
2554 | red = green = blue = 0; | |
2555 | spec.Mid(1,2).ToLong(&red, 16); | |
2556 | spec.Mid(3,2).ToLong(&green, 16); | |
2557 | spec.Mid(5,2).ToLong(&blue, 16); | |
2558 | ||
2559 | **obj = wxColour(red, green, blue); | |
2560 | return true; | |
2561 | } | |
2562 | else { // it's a colour name | |
2563 | **obj = wxColour(spec); | |
2564 | return true; | |
2565 | } | |
2566 | } | |
2567 | // last chance: 3-tuple of integers is expected | |
2568 | else if (PySequence_Check(source) && PyObject_Length(source) == 3) { | |
2569 | PyObject* o1 = PySequence_GetItem(source, 0); | |
2570 | PyObject* o2 = PySequence_GetItem(source, 1); | |
2571 | PyObject* o3 = PySequence_GetItem(source, 2); | |
2572 | if (!PyNumber_Check(o1) || !PyNumber_Check(o2) || !PyNumber_Check(o3)) { | |
2573 | Py_DECREF(o1); | |
2574 | Py_DECREF(o2); | |
2575 | Py_DECREF(o3); | |
2576 | goto error; | |
2577 | } | |
2578 | **obj = wxColour(PyInt_AsLong(o1), PyInt_AsLong(o2), PyInt_AsLong(o3)); | |
2579 | Py_DECREF(o1); | |
2580 | Py_DECREF(o2); | |
2581 | Py_DECREF(o3); | |
2582 | return true; | |
2583 | } | |
2584 | ||
2585 | error: | |
2586 | PyErr_SetString(PyExc_TypeError, | |
2587 | "Expected a wxColour object or a string containing a colour name or '#RRGGBB'."); | |
2588 | return false; | |
2589 | } | |
2590 | ||
2591 | ||
2592 | bool wxColour_typecheck(PyObject* source) { | |
2593 | ||
2594 | if (wxPySimple_typecheck(source, wxT("wxColour"), 3)) | |
2595 | return true; | |
2596 | ||
2597 | if (PyString_Check(source) || PyUnicode_Check(source)) | |
2598 | return true; | |
2599 | ||
2600 | return false; | |
2601 | } | |
2602 | ||
2603 | ||
2604 | ||
2605 | bool wxPoint2D_helper(PyObject* source, wxPoint2D** obj) { | |
2606 | ||
2607 | if (source == Py_None) { | |
2608 | **obj = wxPoint2D(-1,-1); | |
2609 | return true; | |
2610 | } | |
2611 | ||
2612 | // If source is an object instance then it may already be the right type | |
2613 | if (wxPySwigInstance_Check(source)) { | |
2614 | wxPoint2D* ptr; | |
2615 | if (! wxPyConvertSwigPtr(source, (void **)&ptr, wxT("wxPoint2D"))) | |
2616 | goto error; | |
2617 | *obj = ptr; | |
2618 | return true; | |
2619 | } | |
2620 | // otherwise a length-2 sequence of floats is expected | |
2621 | if (PySequence_Check(source) && PySequence_Length(source) == 2) { | |
2622 | PyObject* o1 = PySequence_GetItem(source, 0); | |
2623 | PyObject* o2 = PySequence_GetItem(source, 1); | |
2624 | // This should really check for floats, not numbers -- but that would break code. | |
2625 | if (!PyNumber_Check(o1) || !PyNumber_Check(o2)) { | |
2626 | Py_DECREF(o1); | |
2627 | Py_DECREF(o2); | |
2628 | goto error; | |
2629 | } | |
2630 | **obj = wxPoint2D(PyFloat_AsDouble(o1), PyFloat_AsDouble(o2)); | |
2631 | Py_DECREF(o1); | |
2632 | Py_DECREF(o2); | |
2633 | return true; | |
2634 | } | |
2635 | error: | |
2636 | PyErr_SetString(PyExc_TypeError, "Expected a 2-tuple of floats or a wxPoint2D object."); | |
2637 | return false; | |
2638 | } | |
2639 | ||
2640 | ||
2641 | //---------------------------------------------------------------------- | |
2642 | ||
2643 | PyObject* wxArrayString2PyList_helper(const wxArrayString& arr) { | |
2644 | ||
2645 | PyObject* list = PyList_New(0); | |
2646 | for (size_t i=0; i < arr.GetCount(); i++) { | |
2647 | #if wxUSE_UNICODE | |
2648 | PyObject* str = PyUnicode_FromWideChar(arr[i].c_str(), arr[i].Len()); | |
2649 | #else | |
2650 | PyObject* str = PyString_FromStringAndSize(arr[i].c_str(), arr[i].Len()); | |
2651 | #endif | |
2652 | PyList_Append(list, str); | |
2653 | Py_DECREF(str); | |
2654 | } | |
2655 | return list; | |
2656 | } | |
2657 | ||
2658 | ||
2659 | PyObject* wxArrayInt2PyList_helper(const wxArrayInt& arr) { | |
2660 | ||
2661 | PyObject* list = PyList_New(0); | |
2662 | for (size_t i=0; i < arr.GetCount(); i++) { | |
2663 | PyObject* number = PyInt_FromLong(arr[i]); | |
2664 | PyList_Append(list, number); | |
2665 | Py_DECREF(number); | |
2666 | } | |
2667 | return list; | |
2668 | } | |
2669 | ||
2670 | ||
2671 | //---------------------------------------------------------------------- | |
2672 | //---------------------------------------------------------------------- | |
2673 | ||
2674 | ||
2675 | ||
2676 |