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