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