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