1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Helper functions/classes for the wxPython extension module
7 // Created: 1-July-1997
9 // Copyright: (c) 1998 by Total Control Software
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
16 #include "wx/wxPython/wxPython_int.h"
17 #include "wx/wxPython/pyistream.h"
18 #include "wx/wxPython/swigver.h"
19 #include "wx/wxPython/twoitem.h"
22 #include <wx/msw/private.h>
23 #include <wx/msw/winundef.h>
24 #include <wx/msw/msvcrt.h>
31 #include <gdk/gdkprivate.h>
33 #include <wx/gtk/win_gtk.h>
35 #include <wx/gtk1/win_gtk.h>
37 #define GetXWindow(wxwin) (wxwin)->m_wxwindow ? \
38 GDK_WINDOW_XWINDOW(GTK_PIZZA((wxwin)->m_wxwindow)->bin_window) : \
39 GDK_WINDOW_XWINDOW((wxwin)->m_widget->window)
44 #include "wx/x11/privx.h"
45 #define GetXWindow(wxwin) ((Window)(wxwin)->GetHandle())
49 #include <wx/mac/private.h>
52 #include <wx/clipbrd.h>
53 #include <wx/mimetype.h>
56 //----------------------------------------------------------------------
58 #if PYTHON_API_VERSION < 1009 && wxUSE_UNICODE
59 #error Python must support Unicode to use wxWindows Unicode
62 //----------------------------------------------------------------------
64 wxPyApp
* wxPythonApp
= NULL
; // Global instance of application object
65 bool wxPyDoCleanup
= false;
66 bool wxPyDoingCleanup
= false;
69 #ifdef WXP_WITH_THREAD
70 #if !wxPyUSE_GIL_STATE
71 struct wxPyThreadState
{
73 PyThreadState
* tstate
;
75 wxPyThreadState(unsigned long _tid
=0, PyThreadState
* _tstate
=NULL
)
76 : tid(_tid
), tstate(_tstate
) {}
79 #include <wx/dynarray.h>
80 WX_DECLARE_OBJARRAY(wxPyThreadState
, wxPyThreadStateArray
);
81 #include <wx/arrimpl.cpp>
82 WX_DEFINE_OBJARRAY(wxPyThreadStateArray
);
84 wxPyThreadStateArray
* wxPyTStates
= NULL
;
85 wxMutex
* wxPyTMutex
= NULL
;
91 #define DEFAULTENCODING_SIZE 64
92 static char wxPyDefaultEncoding
[DEFAULTENCODING_SIZE
] = "ascii";
94 static PyObject
* wxPython_dict
= NULL
;
95 static PyObject
* wxPyAssertionError
= NULL
;
96 static PyObject
* wxPyNoAppError
= NULL
;
98 PyObject
* wxPyPtrTypeMap
= NULL
;
101 #ifdef __WXMSW__ // If building for win32...
102 //----------------------------------------------------------------------
103 // This gets run when the DLL is loaded. We just need to save a handle.
104 //----------------------------------------------------------------------
108 HINSTANCE hinstDLL
, // handle to DLL module
109 DWORD fdwReason
, // reason for calling function
110 LPVOID lpvReserved
// reserved
113 // If wxPython is embedded in another wxWidgets app then
114 // the instance has already been set.
115 if (! wxGetInstance())
116 wxSetInstance(hinstDLL
);
121 //----------------------------------------------------------------------
122 // Classes for implementing the wxp main application shell.
123 //----------------------------------------------------------------------
125 IMPLEMENT_ABSTRACT_CLASS(wxPyApp
, wxApp
);
129 m_assertMode
= wxPYAPP_ASSERT_EXCEPTION
;
130 m_startupComplete
= false;
134 wxPyApp::~wxPyApp() {
136 wxApp::SetInstance(NULL
);
140 // This one isn't acutally called... We fake it with _BootstrapApp
141 bool wxPyApp::OnInit() {
146 int wxPyApp::MainLoop() {
149 DeletePendingObjects();
150 bool initialized
= wxTopLevelWindows
.GetCount() != 0;
152 if ( m_exitOnFrameDelete
== Later
) {
153 m_exitOnFrameDelete
= Yes
;
156 retval
= wxApp::MainLoop();
163 bool wxPyApp::OnInitGui() {
165 wxApp::OnInitGui(); // in this case always call the base class version
166 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
167 if (wxPyCBH_findCallback(m_myInst
, "OnInitGui"))
168 rval
= wxPyCBH_callCallback(m_myInst
, Py_BuildValue("()"));
169 wxPyEndBlockThreads(blocked
);
174 int wxPyApp::OnExit() {
176 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
177 if (wxPyCBH_findCallback(m_myInst
, "OnExit"))
178 rval
= wxPyCBH_callCallback(m_myInst
, Py_BuildValue("()"));
179 wxPyEndBlockThreads(blocked
);
180 wxApp::OnExit(); // in this case always call the base class version
186 void wxPyApp::ExitMainLoop() {
188 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
189 if ((found
= wxPyCBH_findCallback(m_myInst
, "ExitMainLoop")))
190 wxPyCBH_callCallback(m_myInst
, Py_BuildValue("()"));
191 wxPyEndBlockThreads(blocked
);
193 wxApp::ExitMainLoop();
198 void wxPyApp::OnAssertFailure(const wxChar
*file
,
204 // if we're not fully initialized then just log the error
205 if (! m_startupComplete
) {
208 buf
.Printf(wxT("%s(%d): assert \"%s\" failed"),
211 buf
<< wxT(" in ") << func
<< wxT("()");
213 buf
<< wxT(": ") << msg
;
219 // If the OnAssert is overloaded in the Python class then call it...
221 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
222 if ((found
= wxPyCBH_findCallback(m_myInst
, "OnAssert"))) {
223 PyObject
* fso
= wx2PyString(file
);
224 PyObject
* cso
= wx2PyString(file
);
227 mso
= wx2PyString(file
);
229 mso
= Py_None
; Py_INCREF(Py_None
);
231 wxPyCBH_callCallback(m_myInst
, Py_BuildValue("(OiOO)", fso
, line
, cso
, mso
));
236 wxPyEndBlockThreads(blocked
);
238 // ...otherwise do our own thing with it
241 if (m_assertMode
& wxPYAPP_ASSERT_SUPPRESS
)
244 // turn it into a Python exception?
245 if (m_assertMode
& wxPYAPP_ASSERT_EXCEPTION
) {
248 buf
.Printf(wxT("C++ assertion \"%s\" failed at %s(%d)"), cond
, file
, line
);
250 buf
<< wxT(" in ") << func
<< wxT("()");
252 buf
<< wxT(": ") << msg
;
256 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
257 PyObject
* s
= wx2PyString(buf
);
258 PyErr_SetObject(wxPyAssertionError
, s
);
260 wxPyEndBlockThreads(blocked
);
262 // Now when control returns to whatever API wrapper was called from
263 // Python it should detect that an exception is set and will return
264 // NULL, signalling the exception to Python.
267 // Send it to the normal log destination, but only if
268 // not _DIALOG because it will call this too
269 if ( (m_assertMode
& wxPYAPP_ASSERT_LOG
) && !(m_assertMode
& wxPYAPP_ASSERT_DIALOG
)) {
272 buf
.Printf(wxT("%s(%d): assert \"%s\" failed"),
275 buf
<< wxT(" in ") << func
<< wxT("()");
277 buf
<< wxT(": ") << msg
;
281 // do the normal wx assert dialog?
282 if (m_assertMode
& wxPYAPP_ASSERT_DIALOG
)
283 wxApp::OnAssertFailure(file
, line
, func
, cond
, msg
);
288 // For catching Apple Events
289 void wxPyApp::MacOpenFile(const wxString
&fileName
)
291 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
292 if (wxPyCBH_findCallback(m_myInst
, "MacOpenFile")) {
293 PyObject
* s
= wx2PyString(fileName
);
294 wxPyCBH_callCallback(m_myInst
, Py_BuildValue("(O)", s
));
297 wxPyEndBlockThreads(blocked
);
300 void wxPyApp::MacOpenURL(const wxString
&url
)
302 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
303 if (wxPyCBH_findCallback(m_myInst
, "MacOpenURL")) {
304 PyObject
* s
= wx2PyString(url
);
305 wxPyCBH_callCallback(m_myInst
, Py_BuildValue("(O)", s
));
308 wxPyEndBlockThreads(blocked
);
311 void wxPyApp::MacPrintFile(const wxString
&fileName
)
313 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
314 if (wxPyCBH_findCallback(m_myInst
, "MacPrintFile")) {
315 PyObject
* s
= wx2PyString(fileName
);
316 wxPyCBH_callCallback(m_myInst
, Py_BuildValue("(O)", s
));
319 wxPyEndBlockThreads(blocked
);
322 void wxPyApp::MacNewFile()
324 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
325 if (wxPyCBH_findCallback(m_myInst
, "MacNewFile"))
326 wxPyCBH_callCallback(m_myInst
, Py_BuildValue("()"));
327 wxPyEndBlockThreads(blocked
);
330 void wxPyApp::MacReopenApp()
332 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
333 if (wxPyCBH_findCallback(m_myInst
, "MacReopenApp"))
334 wxPyCBH_callCallback(m_myInst
, Py_BuildValue("()"));
335 wxPyEndBlockThreads(blocked
);
340 bool wxPyApp::GetMacSupportPCMenuShortcuts() {
345 long wxPyApp::GetMacAboutMenuItemId() {
347 return s_macAboutMenuItemId
;
354 long wxPyApp::GetMacPreferencesMenuItemId() {
356 return s_macPreferencesMenuItemId
;
363 long wxPyApp::GetMacExitMenuItemId() {
365 return s_macExitMenuItemId
;
372 wxString
wxPyApp::GetMacHelpMenuTitleName() {
374 return s_macHelpMenuTitleName
;
376 return wxEmptyString
;
381 void wxPyApp::SetMacSupportPCMenuShortcuts(bool) {
385 void wxPyApp::SetMacAboutMenuItemId(long val
) {
387 s_macAboutMenuItemId
= val
;
392 void wxPyApp::SetMacPreferencesMenuItemId(long val
) {
394 s_macPreferencesMenuItemId
= val
;
399 void wxPyApp::SetMacExitMenuItemId(long val
) {
401 s_macExitMenuItemId
= val
;
406 void wxPyApp::SetMacHelpMenuTitleName(const wxString
& val
) {
408 s_macHelpMenuTitleName
= val
;
413 // This finishes the initialization of wxWindows and then calls the OnInit
414 // that should be present in the derived (Python) class.
415 void wxPyApp::_BootstrapApp()
417 static bool haveInitialized
= false;
420 PyObject
* retval
= NULL
;
421 PyObject
* pyint
= NULL
;
424 // Only initialize wxWidgets once
425 if (! haveInitialized
) {
427 // Get any command-line args passed to this program from the sys module
430 blocked
= wxPyBeginBlockThreads();
432 PyObject
* sysargv
= PySys_GetObject("argv");
433 PyObject
* executable
= PySys_GetObject("executable");
435 if (sysargv
!= NULL
&& executable
!= NULL
) {
436 argc
= PyList_Size(sysargv
) + 1;
437 argv
= new char*[argc
+1];
438 argv
[0] = strdup(PyString_AsString(executable
));
440 for(x
=1; x
<argc
; x
++) {
441 PyObject
*pyArg
= PyList_GetItem(sysargv
, x
-1);
442 argv
[x
] = strdup(PyString_AsString(pyArg
));
446 wxPyEndBlockThreads(blocked
);
448 // Initialize wxWidgets
449 result
= wxEntryStart(argc
, argv
);
450 // wxApp takes ownership of the argv array, don't delete it here
452 blocked
= wxPyBeginBlockThreads();
454 PyErr_SetString(PyExc_SystemError
,
455 "wxEntryStart failed, unable to initialize wxWidgets!"
457 " (Is DISPLAY set properly?)"
463 // On wxGTK the locale will be changed to match the system settings,
464 // but Python before 2.4 needs to have LC_NUMERIC set to "C" in order
465 // for the floating point conversions and such to work right.
466 #if defined(__WXGTK__) && PY_VERSION_HEX < 0x02040000
467 setlocale(LC_NUMERIC
, "C");
470 // wxSystemOptions::SetOption(wxT("mac.textcontrol-use-mlte"), 1);
472 wxPyEndBlockThreads(blocked
);
473 haveInitialized
= true;
481 // It's now ok to generate exceptions for assertion errors.
482 wxPythonApp
->SetStartupComplete(true);
485 // Call the Python wxApp's OnPreInit and OnInit functions
486 blocked
= wxPyBeginBlockThreads();
487 if (wxPyCBH_findCallback(m_myInst
, "OnPreInit")) {
488 PyObject
* method
= m_myInst
.GetLastFound();
489 PyObject
* argTuple
= PyTuple_New(0);
490 retval
= PyEval_CallObject(method
, argTuple
);
491 m_myInst
.clearRecursionGuard(method
);
497 if (wxPyCBH_findCallback(m_myInst
, "OnInit")) {
499 PyObject
* method
= m_myInst
.GetLastFound();
500 PyObject
* argTuple
= PyTuple_New(0);
501 retval
= PyEval_CallObject(method
, argTuple
);
502 m_myInst
.clearRecursionGuard(method
);
506 // Don't PyErr_Print here, let the exception in this case go back
507 // up to the wx.PyApp.__init__ scope.
510 pyint
= PyNumber_Int(retval
);
512 PyErr_SetString(PyExc_TypeError
, "OnInit should return a boolean value");
515 result
= PyInt_AS_LONG(pyint
);
518 // Is it okay if there is no OnInit? Probably so...
523 PyErr_SetString(PyExc_SystemExit
, "OnInit returned false, exiting...");
530 wxPyEndBlockThreads(blocked
);
533 //---------------------------------------------------------------------
534 //----------------------------------------------------------------------
538 static char* wxPyCopyCString(const wxChar
* src
)
540 wxWX2MBbuf buff
= (wxWX2MBbuf
)wxConvCurrent
->cWX2MB(src
);
541 size_t len
= strlen(buff
);
542 char* dest
= new char[len
+1];
548 static char* wxPyCopyCString(const char* src
) // we need a char version too
550 size_t len
= strlen(src
);
551 char* dest
= new char[len
+1];
557 static wxChar
* wxPyCopyWString(const char *src
)
559 //wxMB2WXbuf buff = wxConvCurrent->cMB2WX(src);
560 wxString
str(src
, *wxConvCurrent
);
561 return copystring(str
);
565 static wxChar
* wxPyCopyWString(const wxChar
*src
)
567 return copystring(src
);
573 inline const char* dropwx(const char* name
) {
574 if (name
[0] == 'w' && name
[1] == 'x')
580 //----------------------------------------------------------------------
582 // This function is called when the wx._core_ module is imported to do some
583 // initial setup. (Before there is a wxApp object.) The rest happens in
584 // wxPyApp::_BootstrapApp
585 void __wxPyPreStart(PyObject
* moduleDict
)
589 // wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF
590 // | _CRTDBG_CHECK_ALWAYS_DF
591 // | _CRTDBG_DELAY_FREE_MEM_DF
595 #ifdef WXP_WITH_THREAD
596 #if wxPyUSE_GIL_STATE
597 PyEval_InitThreads();
599 PyEval_InitThreads();
600 wxPyTStates
= new wxPyThreadStateArray
;
601 wxPyTMutex
= new wxMutex
;
603 // Save the current (main) thread state in our array
604 PyThreadState
* tstate
= wxPyBeginAllowThreads();
605 wxPyEndAllowThreads(tstate
);
609 // Ensure that the build options in the DLL (or whatever) match this build
610 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE
, "wxPython");
612 wxInitAllImageHandlers();
617 void __wxPyCleanup() {
618 wxPyDoingCleanup
= true;
620 wxPyDoCleanup
= false;
623 #ifdef WXP_WITH_THREAD
624 #if !wxPyUSE_GIL_STATE
627 wxPyTStates
->Empty();
635 // Save a reference to the dictionary of the wx._core module, and inject
636 // a few more things into it.
637 PyObject
* __wxPySetDictionary(PyObject
* /* self */, PyObject
* args
)
640 if (!PyArg_ParseTuple(args
, "O", &wxPython_dict
))
643 if (!PyDict_Check(wxPython_dict
)) {
644 PyErr_SetString(PyExc_TypeError
,
645 "_wxPySetDictionary must have dictionary object!");
649 if (! wxPyPtrTypeMap
)
650 wxPyPtrTypeMap
= PyDict_New();
651 PyDict_SetItemString(wxPython_dict
, "__wxPyPtrTypeMap", wxPyPtrTypeMap
);
653 // Create an exception object to use for wxASSERTions
654 wxPyAssertionError
= PyErr_NewException("wx._core.PyAssertionError",
655 PyExc_AssertionError
, NULL
);
656 PyDict_SetItemString(wxPython_dict
, "PyAssertionError", wxPyAssertionError
);
658 // Create an exception object to use when the app object hasn't been created yet
659 wxPyNoAppError
= PyErr_NewException("wx._core.PyNoAppError",
660 PyExc_RuntimeError
, NULL
);
661 PyDict_SetItemString(wxPython_dict
, "PyNoAppError", wxPyNoAppError
);
666 #define wxPlatform "__WXMOTIF__"
667 #define wxPlatName "wxMotif"
670 #define wxPlatform "__WXX11__"
671 #define wxPlatName "wxX11"
674 #define wxPlatform "__WXGTK__"
675 #define wxPlatName "wxGTK"
678 #define wxPlatform "__WXMSW__"
679 #define wxPlatName "wxMSW"
682 #define wxPlatform "__WXMAC__"
683 #define wxPlatName "wxMac"
692 // These should be deprecated in favor of the PlatformInfo tuple built below...
693 PyDict_SetItemString(wxPython_dict
, "Platform", PyString_FromString(wxPlatform
));
694 PyDict_SetItemString(wxPython_dict
, "USE_UNICODE", PyInt_FromLong(wxUSE_UNICODE
));
695 PyDict_SetItemString(wxPython_dict
, "__WXDEBUG__", PyInt_FromLong(wxdebug
));
697 // Make a tuple of strings that gives more info about the platform.
698 PyObject
* PlatInfo
= PyList_New(0);
701 #define _AddInfoString(st) \
702 obj = PyString_FromString(st); \
703 PyList_Append(PlatInfo, obj); \
706 _AddInfoString(wxPlatform
);
707 _AddInfoString(wxPlatName
);
709 _AddInfoString("unicode");
711 _AddInfoString("ansi");
715 _AddInfoString("gtk2");
717 _AddInfoString("gtk1");
721 _AddInfoString("wx-assertions-on");
723 _AddInfoString("wx-assertions-off");
725 _AddInfoString(wxPy_SWIG_VERSION
);
727 #if wxMAC_USE_CORE_GRAPHICS
728 _AddInfoString("mac-cg");
730 _AddInfoString("mac-qd");
732 #if wxMAC_USE_NATIVE_TOOLBAR
733 _AddInfoString("mac-native-tb");
735 _AddInfoString("mac-no-native-tb");
739 #undef _AddInfoString
741 PyObject
* PlatInfoTuple
= PyList_AsTuple(PlatInfo
);
743 PyDict_SetItemString(wxPython_dict
, "PlatformInfo", PlatInfoTuple
);
750 //---------------------------------------------------------------------------
752 // Check for existence of a wxApp, setting an exception if there isn't one.
753 // This doesn't need to aquire the GIL because it should only be called from
754 // an %exception before the lock is released.
756 bool wxPyCheckForApp() {
757 if (wxTheApp
!= NULL
)
760 PyErr_SetString(wxPyNoAppError
, "The wx.App object must be created first!");
765 //---------------------------------------------------------------------------
767 void wxPyUserData_dtor(wxPyUserData
* self
) {
768 if (! wxPyDoingCleanup
) {
769 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
770 Py_DECREF(self
->m_obj
);
772 wxPyEndBlockThreads(blocked
);
777 void wxPyClientData_dtor(wxPyClientData
* self
) {
778 if (! wxPyDoingCleanup
) { // Don't do it during cleanup as Python
779 // may have already garbage collected the object...
780 if (self
->m_incRef
) {
781 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
782 Py_DECREF(self
->m_obj
);
783 wxPyEndBlockThreads(blocked
);
791 // This is called when an OOR controled object is being destroyed. Although
792 // the C++ object is going away there is no way to force the Python object
793 // (and all references to it) to die too. This causes problems (crashes) in
794 // wxPython when a python shadow object attempts to call a C++ method using
795 // the now bogus pointer... So to try and prevent this we'll do a little black
796 // magic and change the class of the python instance to a class that will
797 // raise an exception for any attempt to call methods with it. See
798 // _wxPyDeadObject in _core_ex.py for the implementation of this class.
799 void wxPyOORClientData_dtor(wxPyOORClientData
* self
) {
801 static PyObject
* deadObjectClass
= NULL
;
803 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
804 if (deadObjectClass
== NULL
) {
805 deadObjectClass
= PyDict_GetItemString(wxPython_dict
, "_wxPyDeadObject");
806 // TODO: Can not wxASSERT here because inside a wxPyBeginBlock Threads,
807 // will lead to a deadlock when it tries to aquire the GIL again.
808 //wxASSERT_MSG(deadObjectClass != NULL, wxT("Can't get _wxPyDeadObject class!"));
809 Py_INCREF(deadObjectClass
);
813 // Only if there is more than one reference to the object and we are
814 // holding the OOR reference:
815 if ( !wxPyDoingCleanup
&& self
->m_obj
->ob_refcnt
> 1 && self
->m_incRef
) {
816 // bool isInstance = wxPyInstance_Check(self->m_obj);
818 //wxASSERT_MSG(isInstance, wxT("m_obj not an instance!?!?!"));
820 // Call __del__, if there is one.
821 PyObject
* func
= PyObject_GetAttrString(self
->m_obj
, "__del__");
823 PyObject
* rv
= PyObject_CallMethod(self
->m_obj
, "__del__", NULL
);
827 if (PyErr_Occurred())
828 PyErr_Clear(); // just ignore it for now
831 PyObject
* dict
= PyObject_GetAttrString(self
->m_obj
, "__dict__");
833 // Clear the instance's dictionary
836 // put the name of the old class into the instance, and then reset the
837 // class to be the dead class.
838 PyObject
* klass
= PyObject_GetAttrString(self
->m_obj
, "__class__");
839 PyObject
* name
= PyObject_GetAttrString(klass
, "__name__");
840 PyDict_SetItemString(dict
, "_name", name
);
841 PyObject_SetAttrString(self
->m_obj
, "__class__", deadObjectClass
);
842 //Py_INCREF(deadObjectClass);
849 // m_obj is DECREF'd in the base class dtor...
850 wxPyEndBlockThreads(blocked
);
854 //---------------------------------------------------------------------------
855 // Stuff used by OOR to find the right wxPython class type to return and to
859 // The pointer type map is used when the "pointer" type name generated by SWIG
860 // is not the same as the shadow class name, for example wxPyTreeCtrl
861 // vs. wxTreeCtrl. It needs to be referenced in Python as well as from C++,
862 // so we'll just make it a Python dictionary in the wx module's namespace.
863 // (See __wxSetDictionary)
864 void wxPyPtrTypeMap_Add(const char* commonName
, const char* ptrName
) {
865 if (! wxPyPtrTypeMap
)
866 wxPyPtrTypeMap
= PyDict_New();
867 PyDict_SetItemString(wxPyPtrTypeMap
,
869 PyString_FromString((char*)ptrName
));
875 PyObject
* wxPyMake_wxObject(wxObject
* source
, bool setThisOwn
, bool checkEvtHandler
) {
876 PyObject
* target
= NULL
;
877 bool isEvtHandler
= false;
878 bool isSizer
= false;
881 // If it's derived from wxEvtHandler then there may
882 // already be a pointer to a Python object that we can use
884 if (checkEvtHandler
&& wxIsKindOf(source
, wxEvtHandler
)) {
886 wxEvtHandler
* eh
= (wxEvtHandler
*)source
;
887 wxPyOORClientData
* data
= (wxPyOORClientData
*)eh
->GetClientObject();
889 target
= data
->m_obj
;
895 // Also check for wxSizer
896 if (!target
&& wxIsKindOf(source
, wxSizer
)) {
898 wxSizer
* sz
= (wxSizer
*)source
;
899 wxPyOORClientData
* data
= (wxPyOORClientData
*)sz
->GetClientObject();
901 target
= data
->m_obj
;
908 // Otherwise make it the old fashioned way by making a new shadow
909 // object and putting this pointer in it. Look up the class
910 // heirarchy until we find a class name that is located in the
912 const wxClassInfo
* info
= source
->GetClassInfo();
913 wxString name
= info
->GetClassName();
914 bool exists
= wxPyCheckSwigType(name
);
915 while (info
&& !exists
) {
916 info
= info
->GetBaseClass1();
917 name
= info
->GetClassName();
918 exists
= wxPyCheckSwigType(name
);
921 target
= wxPyConstructObject((void*)source
, name
, setThisOwn
);
922 if (target
&& isEvtHandler
)
923 ((wxEvtHandler
*)source
)->SetClientObject(new wxPyOORClientData(target
));
924 if (target
&& isSizer
)
925 ((wxSizer
*)source
)->SetClientObject(new wxPyOORClientData(target
));
927 wxString
msg(wxT("wxPython class not found for "));
928 msg
+= source
->GetClassInfo()->GetClassName();
929 PyErr_SetString(PyExc_NameError
, msg
.mbc_str());
933 } else { // source was NULL so return None.
934 Py_INCREF(Py_None
); target
= Py_None
;
940 PyObject
* wxPyMake_wxSizer(wxSizer
* source
, bool setThisOwn
) {
942 return wxPyMake_wxObject(source
, setThisOwn
);
946 //---------------------------------------------------------------------------
949 #ifdef WXP_WITH_THREAD
950 #if !wxPyUSE_GIL_STATE
953 unsigned long wxPyGetCurrentThreadId() {
954 return wxThread::GetCurrentId();
957 static wxPyThreadState gs_shutdownTState
;
960 wxPyThreadState
* wxPyGetThreadState() {
961 if (wxPyTMutex
== NULL
) // Python is shutting down...
962 return &gs_shutdownTState
;
964 unsigned long ctid
= wxPyGetCurrentThreadId();
965 wxPyThreadState
* tstate
= NULL
;
968 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
969 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
970 if (info
.tid
== ctid
) {
975 wxPyTMutex
->Unlock();
976 wxASSERT_MSG(tstate
, wxT("PyThreadState should not be NULL!"));
982 void wxPySaveThreadState(PyThreadState
* tstate
) {
983 if (wxPyTMutex
== NULL
) { // Python is shutting down, assume a single thread...
984 gs_shutdownTState
.tstate
= tstate
;
987 unsigned long ctid
= wxPyGetCurrentThreadId();
989 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
990 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
991 if (info
.tid
== ctid
) {
993 if (info
.tstate
!= tstate
)
994 wxLogMessage("*** tstate mismatch!???");
996 info
.tstate
= tstate
; // allow for transient tstates
997 // Normally it will never change, but apparently COM callbacks
998 // (i.e. ActiveX controls) will (incorrectly IMHO) use a transient
999 // tstate which will then be garbage the next time we try to use
1002 wxPyTMutex
->Unlock();
1006 // not found, so add it...
1007 wxPyTStates
->Add(new wxPyThreadState(ctid
, tstate
));
1008 wxPyTMutex
->Unlock();
1016 // Calls from Python to wxWindows code are wrapped in calls to these
1019 PyThreadState
* wxPyBeginAllowThreads() {
1020 #ifdef WXP_WITH_THREAD
1021 PyThreadState
* saved
= PyEval_SaveThread(); // Py_BEGIN_ALLOW_THREADS;
1022 #if !wxPyUSE_GIL_STATE
1023 wxPySaveThreadState(saved
);
1031 void wxPyEndAllowThreads(PyThreadState
* saved
) {
1032 #ifdef WXP_WITH_THREAD
1033 PyEval_RestoreThread(saved
); // Py_END_ALLOW_THREADS;
1039 // Calls from wxWindows back to Python code, or even any PyObject
1040 // manipulations, PyDECREF's and etc. are wrapped in calls to these functions:
1042 wxPyBlock_t
wxPyBeginBlockThreads() {
1043 #ifdef WXP_WITH_THREAD
1044 if (! Py_IsInitialized()) {
1045 return (wxPyBlock_t
)0;
1047 #if wxPyUSE_GIL_STATE
1048 PyGILState_STATE state
= PyGILState_Ensure();
1051 PyThreadState
*current
= _PyThreadState_Current
;
1053 // Only block if there wasn't already a tstate, or if the current one is
1054 // not the one we are wanting to change to. This should prevent deadlock
1055 // if there are nested calls to wxPyBeginBlockThreads
1056 wxPyBlock_t blocked
= false;
1057 wxPyThreadState
* tstate
= wxPyGetThreadState();
1058 if (current
!= tstate
->tstate
) {
1059 PyEval_RestoreThread(tstate
->tstate
);
1065 return (wxPyBlock_t
)0;
1070 void wxPyEndBlockThreads(wxPyBlock_t blocked
) {
1071 #ifdef WXP_WITH_THREAD
1072 if (! Py_IsInitialized()) {
1075 #if wxPyUSE_GIL_STATE
1076 PyGILState_Release(blocked
);
1078 // Only unblock if we blocked in the last call to wxPyBeginBlockThreads.
1079 // The value of blocked passed in needs to be the same as that returned
1080 // from wxPyBeginBlockThreads at the same nesting level.
1082 PyEval_SaveThread();
1089 //---------------------------------------------------------------------------
1090 // wxPyInputStream and wxPyCBInputStream methods
1093 static PyObject
* wxPyGetMethod(PyObject
* py
, char* name
)
1095 if (!PyObject_HasAttrString(py
, name
))
1097 PyObject
* o
= PyObject_GetAttrString(py
, name
);
1098 if (!PyMethod_Check(o
) && !PyCFunction_Check(o
)) {
1108 void wxPyInputStream::close() {
1109 /* do nothing for now */
1112 void wxPyInputStream::flush() {
1113 /* do nothing for now */
1116 bool wxPyInputStream::eof() {
1118 return m_wxis
->Eof();
1123 wxPyInputStream::~wxPyInputStream() {
1131 PyObject
* wxPyInputStream::read(int size
) {
1132 PyObject
* obj
= NULL
;
1134 const int BUFSIZE
= 1024;
1136 // check if we have a real wxInputStream to work with
1138 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
1139 PyErr_SetString(PyExc_IOError
, "no valid C-wxInputStream");
1140 wxPyEndBlockThreads(blocked
);
1145 // read while bytes are available on the stream
1146 while ( m_wxis
->CanRead() ) {
1147 m_wxis
->Read(buf
.GetAppendBuf(BUFSIZE
), BUFSIZE
);
1148 buf
.UngetAppendBuf(m_wxis
->LastRead());
1151 } else { // Read only size number of characters
1152 m_wxis
->Read(buf
.GetWriteBuf(size
), size
);
1153 buf
.UngetWriteBuf(m_wxis
->LastRead());
1157 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
1158 wxStreamError err
= m_wxis
->GetLastError();
1159 if (err
!= wxSTREAM_NO_ERROR
&& err
!= wxSTREAM_EOF
) {
1160 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
1163 // We use only strings for the streams, not unicode
1164 obj
= PyString_FromStringAndSize(buf
, buf
.GetDataLen());
1166 wxPyEndBlockThreads(blocked
);
1171 PyObject
* wxPyInputStream::readline(int size
) {
1172 PyObject
* obj
= NULL
;
1177 // check if we have a real wxInputStream to work with
1179 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
1180 PyErr_SetString(PyExc_IOError
,"no valid C-wxInputStream");
1181 wxPyEndBlockThreads(blocked
);
1185 // read until \n or byte limit reached
1186 for (i
=ch
=0; (ch
!= '\n') && (m_wxis
->CanRead()) && ((size
< 0) || (i
< size
)); i
++) {
1187 ch
= m_wxis
->GetC();
1192 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
1193 wxStreamError err
= m_wxis
->GetLastError();
1194 if (err
!= wxSTREAM_NO_ERROR
&& err
!= wxSTREAM_EOF
) {
1195 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
1198 // We use only strings for the streams, not unicode
1199 obj
= PyString_FromStringAndSize((char*)buf
.GetData(), buf
.GetDataLen());
1201 wxPyEndBlockThreads(blocked
);
1206 PyObject
* wxPyInputStream::readlines(int sizehint
) {
1209 // check if we have a real wxInputStream to work with
1211 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
1212 PyErr_SetString(PyExc_IOError
,"no valid C-wxInputStream");
1213 wxPyEndBlockThreads(blocked
);
1218 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
1219 pylist
= PyList_New(0);
1220 wxPyEndBlockThreads(blocked
);
1223 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
1225 wxPyEndBlockThreads(blocked
);
1229 // read sizehint bytes or until EOF
1231 for (i
=0; (m_wxis
->CanRead()) && ((sizehint
< 0) || (i
< sizehint
));) {
1232 PyObject
* s
= this->readline();
1234 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
1236 wxPyEndBlockThreads(blocked
);
1239 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
1240 PyList_Append(pylist
, s
);
1241 i
+= PyString_Size(s
);
1242 wxPyEndBlockThreads(blocked
);
1246 wxStreamError err
= m_wxis
->GetLastError();
1247 if (err
!= wxSTREAM_NO_ERROR
&& err
!= wxSTREAM_EOF
) {
1248 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
1250 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
1251 wxPyEndBlockThreads(blocked
);
1259 void wxPyInputStream::seek(int offset
, int whence
) {
1261 m_wxis
->SeekI(offset
, wxSeekMode(whence
));
1264 int wxPyInputStream::tell() {
1266 return m_wxis
->TellI();
1273 wxPyCBInputStream::wxPyCBInputStream(PyObject
*r
, PyObject
*s
, PyObject
*t
, bool block
)
1274 : wxInputStream(), m_read(r
), m_seek(s
), m_tell(t
), m_block(block
)
1277 wxPyCBInputStream::wxPyCBInputStream(const wxPyCBInputStream
& other
)
1279 m_read
= other
.m_read
;
1280 m_seek
= other
.m_seek
;
1281 m_tell
= other
.m_tell
;
1282 m_block
= other
.m_block
;
1289 wxPyCBInputStream::~wxPyCBInputStream() {
1290 wxPyBlock_t blocked
= wxPyBlock_t_default
;
1291 if (m_block
) blocked
= wxPyBeginBlockThreads();
1295 if (m_block
) wxPyEndBlockThreads(blocked
);
1299 wxPyCBInputStream
* wxPyCBInputStream::create(PyObject
*py
, bool block
) {
1300 wxPyBlock_t blocked
= wxPyBlock_t_default
;
1301 if (block
) blocked
= wxPyBeginBlockThreads();
1303 PyObject
* read
= wxPyGetMethod(py
, "read");
1304 PyObject
* seek
= wxPyGetMethod(py
, "seek");
1305 PyObject
* tell
= wxPyGetMethod(py
, "tell");
1308 PyErr_SetString(PyExc_TypeError
, "Not a file-like object");
1312 if (block
) wxPyEndBlockThreads(blocked
);
1316 if (block
) wxPyEndBlockThreads(blocked
);
1317 return new wxPyCBInputStream(read
, seek
, tell
, block
);
1321 wxPyCBInputStream
* wxPyCBInputStream_create(PyObject
*py
, bool block
) {
1322 return wxPyCBInputStream::create(py
, block
);
1325 wxPyCBInputStream
* wxPyCBInputStream_copy(wxPyCBInputStream
* other
) {
1326 return new wxPyCBInputStream(*other
);
1330 wxFileOffset
wxPyCBInputStream::GetLength() const {
1331 wxPyCBInputStream
* self
= (wxPyCBInputStream
*)this; // cast off const
1332 if (m_seek
&& m_tell
) {
1333 wxFileOffset temp
= self
->OnSysTell();
1334 wxFileOffset ret
= self
->OnSysSeek(0, wxFromEnd
);
1335 self
->OnSysSeek(temp
, wxFromStart
);
1339 return wxInvalidOffset
;
1343 size_t wxPyCBInputStream::OnSysRead(void *buffer
, size_t bufsize
) {
1347 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
1348 PyObject
* arglist
= Py_BuildValue("(i)", bufsize
);
1349 PyObject
* result
= PyEval_CallObject(m_read
, arglist
);
1353 if ((result
!= NULL
) && PyString_Check(result
)) {
1354 o
= PyString_Size(result
);
1356 m_lasterror
= wxSTREAM_EOF
;
1359 memcpy((char*)buffer
, PyString_AsString(result
), o
); // strings only, not unicode...
1364 m_lasterror
= wxSTREAM_READ_ERROR
;
1365 wxPyEndBlockThreads(blocked
);
1369 size_t wxPyCBInputStream::OnSysWrite(const void *buffer
, size_t bufsize
) {
1370 m_lasterror
= wxSTREAM_WRITE_ERROR
;
1375 wxFileOffset
wxPyCBInputStream::OnSysSeek(wxFileOffset off
, wxSeekMode mode
) {
1376 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
1377 PyObject
* arglist
= PyTuple_New(2);
1379 if (sizeof(wxFileOffset
) > sizeof(long))
1380 // wxFileOffset is a 64-bit value...
1381 PyTuple_SET_ITEM(arglist
, 0, PyLong_FromLongLong(off
));
1383 PyTuple_SET_ITEM(arglist
, 0, PyInt_FromLong(off
));
1385 PyTuple_SET_ITEM(arglist
, 1, PyInt_FromLong(mode
));
1388 PyObject
* result
= PyEval_CallObject(m_seek
, arglist
);
1391 wxPyEndBlockThreads(blocked
);
1396 wxFileOffset
wxPyCBInputStream::OnSysTell() const {
1397 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
1398 PyObject
* arglist
= Py_BuildValue("()");
1399 PyObject
* result
= PyEval_CallObject(m_tell
, arglist
);
1402 if (result
!= NULL
) {
1403 if (PyLong_Check(result
))
1404 o
= PyLong_AsLongLong(result
);
1406 o
= PyInt_AsLong(result
);
1409 wxPyEndBlockThreads(blocked
);
1413 //----------------------------------------------------------------------
1416 wxPyOutputStream::~wxPyOutputStream()
1422 void wxPyOutputStream::close()
1426 void wxPyOutputStream::flush()
1430 bool wxPyOutputStream::eof()
1435 void wxPyOutputStream::seek(int offset
, int whence
)
1438 m_wxos
->SeekO(offset
, wxSeekMode(whence
));
1441 int wxPyOutputStream::tell()
1444 return m_wxos
->TellO();
1448 void wxPyOutputStream::write(PyObject
* data
)
1453 // We use only strings for the streams, not unicode
1454 PyObject
* str
= PyObject_Str(data
);
1456 PyErr_SetString(PyExc_TypeError
, "Unable to convert to string");
1459 m_wxos
->Write(PyString_AS_STRING(str
), PyString_GET_SIZE(str
));
1468 wxPyCBOutputStream::wxPyCBOutputStream(PyObject
*w
, PyObject
*s
, PyObject
*t
, bool block
)
1469 : wxOutputStream(), m_write(w
), m_seek(s
), m_tell(t
), m_block(block
)
1472 wxPyCBOutputStream::wxPyCBOutputStream(const wxPyCBOutputStream
& other
)
1474 m_write
= other
.m_write
;
1475 m_seek
= other
.m_seek
;
1476 m_tell
= other
.m_tell
;
1477 m_block
= other
.m_block
;
1484 wxPyCBOutputStream::~wxPyCBOutputStream() {
1485 wxPyBlock_t blocked
= wxPyBlock_t_default
;
1486 if (m_block
) blocked
= wxPyBeginBlockThreads();
1487 Py_XDECREF(m_write
);
1490 if (m_block
) wxPyEndBlockThreads(blocked
);
1494 wxPyCBOutputStream
* wxPyCBOutputStream::create(PyObject
*py
, bool block
) {
1495 wxPyBlock_t blocked
= wxPyBlock_t_default
;
1496 if (block
) blocked
= wxPyBeginBlockThreads();
1498 PyObject
* write
= wxPyGetMethod(py
, "write");
1499 PyObject
* seek
= wxPyGetMethod(py
, "seek");
1500 PyObject
* tell
= wxPyGetMethod(py
, "tell");
1503 PyErr_SetString(PyExc_TypeError
, "Not a file-like object");
1507 if (block
) wxPyEndBlockThreads(blocked
);
1511 if (block
) wxPyEndBlockThreads(blocked
);
1512 return new wxPyCBOutputStream(write
, seek
, tell
, block
);
1516 wxPyCBOutputStream
* wxPyCBOutputStream_create(PyObject
*py
, bool block
) {
1517 return wxPyCBOutputStream::create(py
, block
);
1520 wxPyCBOutputStream
* wxPyCBOutputStream_copy(wxPyCBOutputStream
* other
) {
1521 return new wxPyCBOutputStream(*other
);
1525 wxFileOffset
wxPyCBOutputStream::GetLength() const {
1526 wxPyCBOutputStream
* self
= (wxPyCBOutputStream
*)this; // cast off const
1527 if (m_seek
&& m_tell
) {
1528 wxFileOffset temp
= self
->OnSysTell();
1529 wxFileOffset ret
= self
->OnSysSeek(0, wxFromEnd
);
1530 self
->OnSysSeek(temp
, wxFromStart
);
1534 return wxInvalidOffset
;
1538 size_t wxPyCBOutputStream::OnSysRead(void *buffer
, size_t bufsize
) {
1539 m_lasterror
= wxSTREAM_READ_ERROR
;
1543 size_t wxPyCBOutputStream::OnSysWrite(const void *buffer
, size_t bufsize
) {
1547 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
1548 PyObject
* arglist
= PyTuple_New(1);
1549 PyTuple_SET_ITEM(arglist
, 0, PyString_FromStringAndSize((char*)buffer
, bufsize
));
1551 PyObject
* result
= PyEval_CallObject(m_write
, arglist
);
1557 m_lasterror
= wxSTREAM_WRITE_ERROR
;
1558 wxPyEndBlockThreads(blocked
);
1563 wxFileOffset
wxPyCBOutputStream::OnSysSeek(wxFileOffset off
, wxSeekMode mode
) {
1564 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
1565 PyObject
* arglist
= PyTuple_New(2);
1567 if (sizeof(wxFileOffset
) > sizeof(long))
1568 // wxFileOffset is a 64-bit value...
1569 PyTuple_SET_ITEM(arglist
, 0, PyLong_FromLongLong(off
));
1571 PyTuple_SET_ITEM(arglist
, 0, PyInt_FromLong(off
));
1573 PyTuple_SET_ITEM(arglist
, 1, PyInt_FromLong(mode
));
1576 PyObject
* result
= PyEval_CallObject(m_seek
, arglist
);
1579 wxPyEndBlockThreads(blocked
);
1584 wxFileOffset
wxPyCBOutputStream::OnSysTell() const {
1585 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
1586 PyObject
* arglist
= Py_BuildValue("()");
1587 PyObject
* result
= PyEval_CallObject(m_tell
, arglist
);
1590 if (result
!= NULL
) {
1591 if (PyLong_Check(result
))
1592 o
= PyLong_AsLongLong(result
);
1594 o
= PyInt_AsLong(result
);
1597 wxPyEndBlockThreads(blocked
);
1603 //----------------------------------------------------------------------
1605 IMPLEMENT_ABSTRACT_CLASS(wxPyCallback
, wxObject
);
1607 wxPyCallback::wxPyCallback(PyObject
* func
) {
1612 wxPyCallback::wxPyCallback(const wxPyCallback
& other
) {
1613 m_func
= other
.m_func
;
1617 wxPyCallback::~wxPyCallback() {
1618 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
1620 wxPyEndBlockThreads(blocked
);
1624 #define wxPy_PRECALLINIT "_preCallInit"
1625 #define wxPy_POSTCALLCLEANUP "_postCallCleanup"
1627 // This function is used for all events destined for Python event handlers.
1628 void wxPyCallback::EventThunker(wxEvent
& event
) {
1629 wxPyCallback
* cb
= (wxPyCallback
*)event
.m_callbackUserData
;
1630 PyObject
* func
= cb
->m_func
;
1634 bool checkSkip
= false;
1636 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
1637 wxString className
= event
.GetClassInfo()->GetClassName();
1639 // If the event is one of these types then pass the original
1640 // event object instead of the one passed to us.
1641 if ( className
== wxT("wxPyEvent") ) {
1642 arg
= ((wxPyEvent
*)&event
)->GetSelf();
1643 checkSkip
= ((wxPyEvent
*)&event
)->GetCloned();
1645 else if ( className
== wxT("wxPyCommandEvent") ) {
1646 arg
= ((wxPyCommandEvent
*)&event
)->GetSelf();
1647 checkSkip
= ((wxPyCommandEvent
*)&event
)->GetCloned();
1650 arg
= wxPyConstructObject((void*)&event
, className
);
1656 // "intern" the pre/post method names to speed up the HasAttr
1657 static PyObject
* s_preName
= NULL
;
1658 static PyObject
* s_postName
= NULL
;
1659 if (s_preName
== NULL
) {
1660 s_preName
= PyString_FromString(wxPy_PRECALLINIT
);
1661 s_postName
= PyString_FromString(wxPy_POSTCALLCLEANUP
);
1664 // Check if the event object needs some preinitialization
1665 if (PyObject_HasAttr(arg
, s_preName
)) {
1666 result
= PyObject_CallMethodObjArgs(arg
, s_preName
, arg
, NULL
);
1668 Py_DECREF(result
); // result is ignored, but we still need to decref it
1669 PyErr_Clear(); // Just in case...
1675 // Call the event handler, passing the event object
1676 tuple
= PyTuple_New(1);
1677 PyTuple_SET_ITEM(tuple
, 0, arg
); // steals ref to arg
1678 result
= PyEval_CallObject(func
, tuple
);
1680 Py_DECREF(result
); // result is ignored, but we still need to decref it
1681 PyErr_Clear(); // Just in case...
1686 // Check if the event object needs some post cleanup
1687 if (PyObject_HasAttr(arg
, s_postName
)) {
1688 result
= PyObject_CallMethodObjArgs(arg
, s_postName
, arg
, NULL
);
1690 Py_DECREF(result
); // result is ignored, but we still need to decref it
1691 PyErr_Clear(); // Just in case...
1698 // if the event object was one of our special types and
1699 // it had been cloned, then we need to extract the Skipped
1700 // value from the original and set it in the clone.
1701 result
= PyObject_CallMethod(arg
, "GetSkipped", "");
1703 event
.Skip(PyInt_AsLong(result
));
1711 wxPyEndBlockThreads(blocked
);
1715 //----------------------------------------------------------------------
1717 wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper
& other
) {
1719 m_self
= other
.m_self
;
1720 m_class
= other
.m_class
;
1728 void wxPyCallbackHelper::setSelf(PyObject
* self
, PyObject
* klass
, int incref
) {
1739 #if PYTHON_API_VERSION >= 1011
1741 // Prior to Python 2.2 PyMethod_GetClass returned the class object
1742 // in which the method was defined. Starting with 2.2 it returns
1743 // "class that asked for the method" which seems totally bogus to me
1744 // but apprently it fixes some obscure problem waiting to happen in
1745 // Python. Since the API was not documented Guido and the gang felt
1746 // safe in changing it. Needless to say that totally screwed up the
1747 // logic below in wxPyCallbackHelper::findCallback, hence this icky
1748 // code to find the class where the method is actually defined...
1751 PyObject
* PyFindClassWithAttr(PyObject
*klass
, PyObject
*name
)
1755 if (PyType_Check(klass
)) { // new style classes
1756 // This code is borrowed/adapted from _PyType_Lookup in typeobject.c
1757 PyTypeObject
* type
= (PyTypeObject
*)klass
;
1758 PyObject
*mro
, *res
, *base
, *dict
;
1759 /* Look in tp_dict of types in MRO */
1761 assert(PyTuple_Check(mro
));
1762 n
= PyTuple_GET_SIZE(mro
);
1763 for (i
= 0; i
< n
; i
++) {
1764 base
= PyTuple_GET_ITEM(mro
, i
);
1765 if (PyClass_Check(base
))
1766 dict
= ((PyClassObject
*)base
)->cl_dict
;
1768 assert(PyType_Check(base
));
1769 dict
= ((PyTypeObject
*)base
)->tp_dict
;
1771 assert(dict
&& PyDict_Check(dict
));
1772 res
= PyDict_GetItem(dict
, name
);
1779 else if (PyClass_Check(klass
)) { // old style classes
1780 // This code is borrowed/adapted from class_lookup in classobject.c
1781 PyClassObject
* cp
= (PyClassObject
*)klass
;
1782 PyObject
*value
= PyDict_GetItem(cp
->cl_dict
, name
);
1783 if (value
!= NULL
) {
1784 return (PyObject
*)cp
;
1786 n
= PyTuple_Size(cp
->cl_bases
);
1787 for (i
= 0; i
< n
; i
++) {
1788 PyObject
* base
= PyTuple_GetItem(cp
->cl_bases
, i
);
1789 PyObject
*v
= PyFindClassWithAttr(base
, name
);
1801 PyObject
* PyMethod_GetDefiningClass(PyObject
* method
, PyObject
* nameo
)
1803 PyObject
* mgc
= PyMethod_GET_CLASS(method
);
1805 #if PYTHON_API_VERSION <= 1010 // prior to Python 2.2, the easy way
1807 #else // 2.2 and after, the hard way...
1808 return PyFindClassWithAttr(mgc
, nameo
);
1814 // To avoid recursion when an overridden virtual method wants to call the base
1815 // class version, temporarily set an attribute in the instance with the same
1816 // name as the method. Then the PyObject_GetAttr in the next findCallback
1817 // will return this attribute and the PyMethod_Check will fail.
1819 void wxPyCallbackHelper::setRecursionGuard(PyObject
* method
) const
1821 PyFunctionObject
* func
= (PyFunctionObject
*)PyMethod_Function(method
);
1822 PyObject_SetAttr(m_self
, func
->func_name
, Py_None
);
1825 void wxPyCallbackHelper::clearRecursionGuard(PyObject
* method
) const
1827 PyFunctionObject
* func
= (PyFunctionObject
*)PyMethod_Function(method
);
1828 if (PyObject_HasAttr(m_self
, func
->func_name
)) {
1829 PyObject_DelAttr(m_self
, func
->func_name
);
1833 // bool wxPyCallbackHelper::hasRecursionGuard(PyObject* method) const
1835 // PyFunctionObject* func = (PyFunctionObject*)PyMethod_Function(method);
1836 // if (PyObject_HasAttr(m_self, func->func_name)) {
1837 // PyObject* attr = PyObject_GetAttr(m_self, func->func_name);
1838 // bool retval = (attr == Py_None);
1846 bool wxPyCallbackHelper::findCallback(const char* name
, bool setGuard
) const {
1847 wxPyCallbackHelper
* self
= (wxPyCallbackHelper
*)this; // cast away const
1848 PyObject
*method
, *klass
;
1849 PyObject
* nameo
= PyString_FromString(name
);
1850 self
->m_lastFound
= NULL
;
1852 // If the object (m_self) has an attibute of the given name...
1853 if (m_self
&& PyObject_HasAttr(m_self
, nameo
)) {
1854 method
= PyObject_GetAttr(m_self
, nameo
);
1856 // ...and if that attribute is a method, and if that method's class is
1857 // not from the registered class or a base class...
1858 if (PyMethod_Check(method
) &&
1859 (klass
= PyMethod_GetDefiningClass(method
, nameo
)) != NULL
&&
1860 (klass
!= m_class
) &&
1861 PyObject_IsSubclass(klass
, m_class
)) {
1863 // ...then we'll save a pointer to the method so callCallback can
1864 // call it. But first, set a recursion guard in case the
1865 // overridden method wants to call the base class version.
1867 setRecursionGuard(method
);
1868 self
->m_lastFound
= method
;
1876 return m_lastFound
!= NULL
;
1880 int wxPyCallbackHelper::callCallback(PyObject
* argTuple
) const {
1884 result
= callCallbackObj(argTuple
);
1885 if (result
) { // Assumes an integer return type...
1886 retval
= PyInt_AsLong(result
);
1888 PyErr_Clear(); // forget about it if it's not...
1893 // Invoke the Python callable object, returning the raw PyObject return
1894 // value. Caller should DECREF the return value and also manage the GIL.
1895 PyObject
* wxPyCallbackHelper::callCallbackObj(PyObject
* argTuple
) const {
1898 // Save a copy of the pointer in case the callback generates another
1899 // callback. In that case m_lastFound will have a different value when
1900 // it gets back here...
1901 PyObject
* method
= m_lastFound
;
1903 result
= PyEval_CallObject(method
, argTuple
);
1904 clearRecursionGuard(method
);
1906 Py_DECREF(argTuple
);
1915 void wxPyCBH_setCallbackInfo(wxPyCallbackHelper
& cbh
, PyObject
* self
, PyObject
* klass
, int incref
) {
1916 cbh
.setSelf(self
, klass
, incref
);
1919 bool wxPyCBH_findCallback(const wxPyCallbackHelper
& cbh
, const char* name
, bool setGuard
) {
1920 return cbh
.findCallback(name
, setGuard
);
1923 int wxPyCBH_callCallback(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
1924 return cbh
.callCallback(argTuple
);
1927 PyObject
* wxPyCBH_callCallbackObj(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
1928 return cbh
.callCallbackObj(argTuple
);
1932 void wxPyCBH_delete(wxPyCallbackHelper
* cbh
) {
1933 if (cbh
->m_incRef
&& Py_IsInitialized()) {
1934 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
1935 Py_XDECREF(cbh
->m_self
);
1936 Py_XDECREF(cbh
->m_class
);
1937 wxPyEndBlockThreads(blocked
);
1941 //---------------------------------------------------------------------------
1942 //---------------------------------------------------------------------------
1943 // These event classes can be derived from in Python and passed through the event
1944 // system without losing anything. They do this by keeping a reference to
1945 // themselves and some special case handling in wxPyCallback::EventThunker.
1948 wxPyEvtSelfRef::wxPyEvtSelfRef() {
1949 //m_self = Py_None; // **** We don't do normal ref counting to prevent
1950 //Py_INCREF(m_self); // circular loops...
1954 wxPyEvtSelfRef::~wxPyEvtSelfRef() {
1955 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
1958 wxPyEndBlockThreads(blocked
);
1961 void wxPyEvtSelfRef::SetSelf(PyObject
* self
, bool clone
) {
1962 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
1970 wxPyEndBlockThreads(blocked
);
1973 PyObject
* wxPyEvtSelfRef::GetSelf() const {
1979 IMPLEMENT_ABSTRACT_CLASS(wxPyEvent
, wxEvent
);
1980 IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent
, wxCommandEvent
);
1983 wxPyEvent::wxPyEvent(int winid
, wxEventType commandType
)
1984 : wxEvent(winid
, commandType
) {
1988 wxPyEvent::wxPyEvent(const wxPyEvent
& evt
)
1991 SetSelf(evt
.m_self
, true);
1995 wxPyEvent::~wxPyEvent() {
1999 wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType
, int id
)
2000 : wxCommandEvent(commandType
, id
) {
2004 wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent
& evt
)
2005 : wxCommandEvent(evt
)
2007 SetSelf(evt
.m_self
, true);
2011 wxPyCommandEvent::~wxPyCommandEvent() {
2018 //---------------------------------------------------------------------------
2019 //---------------------------------------------------------------------------
2020 // Convert a wxList to a Python List, only works for lists of wxObjects
2022 PyObject
* wxPy_ConvertList(wxListBase
* listbase
) {
2023 wxList
* list
= (wxList
*)listbase
; // this is probably bad...
2027 wxNode
* node
= list
->GetFirst();
2029 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
2030 pyList
= PyList_New(0);
2032 wxObj
= node
->GetData();
2033 pyObj
= wxPyMake_wxObject(wxObj
,false);
2034 PyList_Append(pyList
, pyObj
);
2035 Py_DECREF(pyObj
); // the Append also does an INCREF, that's one more than we need.
2036 node
= node
->GetNext();
2038 wxPyEndBlockThreads(blocked
);
2042 //----------------------------------------------------------------------
2044 long wxPyGetWinHandle(wxWindow
* win
) {
2047 return (long)win
->GetHandle();
2050 #if defined(__WXGTK__) || defined(__WXX11)
2051 return (long)GetXWindow(win
);
2055 //return (long)MAC_WXHWND(win->MacGetTopLevelWindowRef());
2056 return (long)win
->GetHandle();
2062 //----------------------------------------------------------------------
2063 // Some helper functions for typemaps in my_typemaps.i, so they won't be
2064 // included in every file over and over again...
2066 wxString
* wxString_in_helper(PyObject
* source
) {
2067 wxString
* target
= NULL
;
2069 if (!PyString_Check(source
) && !PyUnicode_Check(source
)) {
2070 PyErr_SetString(PyExc_TypeError
, "String or Unicode type required");
2074 PyObject
* uni
= source
;
2075 if (PyString_Check(source
)) {
2076 uni
= PyUnicode_FromEncodedObject(source
, wxPyDefaultEncoding
, "strict");
2077 if (PyErr_Occurred()) return NULL
;
2079 target
= new wxString();
2080 size_t len
= PyUnicode_GET_SIZE(uni
);
2082 PyUnicode_AsWideChar((PyUnicodeObject
*)uni
, wxStringBuffer(*target
, len
), len
);
2085 if (PyString_Check(source
))
2088 // Convert to a string object if it isn't already, then to wxString
2089 PyObject
* str
= source
;
2090 if (PyUnicode_Check(source
)) {
2091 str
= PyUnicode_AsEncodedString(source
, wxPyDefaultEncoding
, "strict");
2092 if (PyErr_Occurred()) return NULL
;
2094 else if (!PyString_Check(source
)) {
2095 str
= PyObject_Str(source
);
2096 if (PyErr_Occurred()) return NULL
;
2098 char* tmpPtr
; Py_ssize_t tmpSize
;
2099 PyString_AsStringAndSize(str
, &tmpPtr
, &tmpSize
);
2100 target
= new wxString(tmpPtr
, tmpSize
);
2102 if (!PyString_Check(source
))
2104 #endif // wxUSE_UNICODE
2110 // Similar to above except doesn't use "new" and doesn't set an exception
2111 wxString
Py2wxString(PyObject
* source
)
2116 // Convert to a unicode object, if not already, then to a wxString
2117 PyObject
* uni
= source
;
2118 if (!PyUnicode_Check(source
)) {
2119 uni
= PyUnicode_FromEncodedObject(source
, wxPyDefaultEncoding
, "strict");
2120 if (PyErr_Occurred()) return wxEmptyString
; // TODO: should we PyErr_Clear?
2122 size_t len
= PyUnicode_GET_SIZE(uni
);
2124 PyUnicode_AsWideChar((PyUnicodeObject
*)uni
, wxStringBuffer(target
, len
), len
);
2127 if (!PyUnicode_Check(source
))
2130 // Convert to a string object if it isn't already, then to wxString
2131 PyObject
* str
= source
;
2132 if (PyUnicode_Check(source
)) {
2133 str
= PyUnicode_AsEncodedString(source
, wxPyDefaultEncoding
, "strict");
2134 if (PyErr_Occurred()) return wxEmptyString
; // TODO: should we PyErr_Clear?
2136 else if (!PyString_Check(source
)) {
2137 str
= PyObject_Str(source
);
2138 if (PyErr_Occurred()) return wxEmptyString
; // TODO: should we PyErr_Clear?
2140 char* tmpPtr
; Py_ssize_t tmpSize
;
2141 PyString_AsStringAndSize(str
, &tmpPtr
, &tmpSize
);
2142 target
= wxString(tmpPtr
, tmpSize
);
2144 if (!PyString_Check(source
))
2146 #endif // wxUSE_UNICODE
2152 // Make either a Python String or Unicode object, depending on build mode
2153 PyObject
* wx2PyString(const wxString
& src
)
2157 str
= PyUnicode_FromWideChar(src
.c_str(), src
.Len());
2159 str
= PyString_FromStringAndSize(src
.c_str(), src
.Len());
2166 void wxSetDefaultPyEncoding(const char* encoding
)
2168 strncpy(wxPyDefaultEncoding
, encoding
, DEFAULTENCODING_SIZE
);
2171 const char* wxGetDefaultPyEncoding()
2173 return wxPyDefaultEncoding
;
2176 //----------------------------------------------------------------------
2179 byte
* byte_LIST_helper(PyObject
* source
) {
2180 if (!PyList_Check(source
)) {
2181 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
2184 int count
= PyList_Size(source
);
2185 byte
* temp
= new byte
[count
];
2187 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
2190 for (int x
=0; x
<count
; x
++) {
2191 PyObject
* o
= PyList_GetItem(source
, x
);
2192 if (! PyInt_Check(o
)) {
2193 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
2196 temp
[x
] = (byte
)PyInt_AsLong(o
);
2202 int* int_LIST_helper(PyObject
* source
) {
2203 if (!PyList_Check(source
)) {
2204 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
2207 int count
= PyList_Size(source
);
2208 int* temp
= new int[count
];
2210 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
2213 for (int x
=0; x
<count
; x
++) {
2214 PyObject
* o
= PyList_GetItem(source
, x
);
2215 if (! PyInt_Check(o
)) {
2216 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
2219 temp
[x
] = PyInt_AsLong(o
);
2225 long* long_LIST_helper(PyObject
* source
) {
2226 if (!PyList_Check(source
)) {
2227 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
2230 int count
= PyList_Size(source
);
2231 long* temp
= new long[count
];
2233 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
2236 for (int x
=0; x
<count
; x
++) {
2237 PyObject
* o
= PyList_GetItem(source
, x
);
2238 if (! PyInt_Check(o
)) {
2239 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
2242 temp
[x
] = PyInt_AsLong(o
);
2248 char** string_LIST_helper(PyObject
* source
) {
2249 if (!PyList_Check(source
)) {
2250 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
2253 int count
= PyList_Size(source
);
2254 char** temp
= new char*[count
];
2256 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
2259 for (int x
=0; x
<count
; x
++) {
2260 PyObject
* o
= PyList_GetItem(source
, x
);
2261 if (! PyString_Check(o
)) {
2262 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
2265 temp
[x
] = PyString_AsString(o
);
2270 //---------------------------------------------------------------------------
2271 // NOTE: The following functions could really use some refactoring using
2272 // templates to make things somewhat simpler...
2274 inline bool wxPointFromObjects(PyObject
* o1
, PyObject
* o2
, wxPoint
* point
)
2277 if (PyInt_Check(o1
))
2278 point
->x
= (int)PyInt_AS_LONG(o1
);
2279 else if (PyFloat_Check(o1
))
2280 point
->x
= (int)PyFloat_AS_DOUBLE(o1
);
2281 else if (PyNumber_Check(o1
))
2282 point
->x
= (int)PyInt_AsLong(o1
);
2287 if (PyInt_Check(o2
))
2288 point
->y
= (int)PyInt_AS_LONG(o2
);
2289 else if (PyFloat_Check(o2
))
2290 point
->y
= (int)PyFloat_AS_DOUBLE(o2
);
2291 else if (PyNumber_Check(o2
))
2292 point
->y
= (int)PyInt_AsLong(o2
);
2298 // NOTE: This function used to have this code in it, but I don't know why it
2299 // is a problem nor what havok it will cause, so removing for now...
2300 // if (wxPySwigInstance_Check(o1) || wxPySwigInstance_Check(o2)) {
2301 // // Disallow instances because they can cause havok
2307 inline bool wxPoint2DFromObjects(PyObject
* o1
, PyObject
* o2
, wxPoint2D
* point
)
2310 if (PyInt_Check(o1
))
2311 point
->m_x
= (double)PyInt_AS_LONG(o1
);
2312 else if (PyFloat_Check(o1
))
2313 point
->m_x
= (double)PyFloat_AS_DOUBLE(o1
);
2314 else if (PyNumber_Check(o1
))
2315 point
->m_x
= (double)PyFloat_AsDouble(o1
);
2320 if (PyInt_Check(o2
))
2321 point
->m_y
= (double)PyInt_AS_LONG(o2
);
2322 else if (PyFloat_Check(o2
))
2323 point
->m_y
= (double)PyFloat_AS_DOUBLE(o2
);
2324 else if (PyNumber_Check(o2
))
2325 point
->m_y
= (double)PyFloat_AsDouble(o2
);
2334 wxPoint
* wxPoint_LIST_helper(PyObject
* source
, int *count
)
2338 PyObject
*o
, *o1
, *o2
;
2339 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
2341 if (!PySequence_Check(source
)) {
2345 // The length of the sequence is returned in count.
2346 *count
= PySequence_Length(source
);
2351 temp
= new wxPoint
[*count
];
2353 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
2356 for (idx
=0; idx
<*count
; idx
++) {
2357 // Get an item: try fast way first.
2359 o
= PySequence_Fast_GET_ITEM(source
, idx
);
2362 o
= PySequence_GetItem(source
, idx
);
2368 // Convert o to wxPoint.
2369 if ((PyTuple_Check(o
) && PyTuple_GET_SIZE(o
) == 2) ||
2370 (PyList_Check(o
) && PyList_GET_SIZE(o
) == 2)) {
2371 o1
= PySequence_Fast_GET_ITEM(o
, 0);
2372 o2
= PySequence_Fast_GET_ITEM(o
, 1);
2373 if (!wxPointFromObjects(o1
, o2
, &temp
[idx
])) {
2377 else if (wxPySwigInstance_Check(o
)) {
2379 if (! wxPyConvertSwigPtr(o
, (void **)&pt
, wxT("wxPoint"))) {
2384 else if (PySequence_Check(o
) && PySequence_Length(o
) == 2) {
2385 o1
= PySequence_GetItem(o
, 0);
2386 o2
= PySequence_GetItem(o
, 1);
2387 if (!wxPointFromObjects(o1
, o2
, &temp
[idx
])) {
2411 PyErr_SetString(PyExc_TypeError
, "Expected a sequence of length-2 sequences or wxPoints.");
2417 wxPoint2D
* wxPoint2D_LIST_helper(PyObject
* source
, size_t *count
)
2421 PyObject
*o
, *o1
, *o2
;
2422 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
2424 if (!PySequence_Check(source
)) {
2428 // The length of the sequence is returned in count.
2429 *count
= PySequence_Length(source
);
2434 temp
= new wxPoint2D
[*count
];
2436 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
2439 for (idx
=0; idx
<*count
; idx
++) {
2440 // Get an item: try fast way first.
2442 o
= PySequence_Fast_GET_ITEM(source
, idx
);
2445 o
= PySequence_GetItem(source
, idx
);
2451 // Convert o to wxPoint.
2452 if ((PyTuple_Check(o
) && PyTuple_GET_SIZE(o
) == 2) ||
2453 (PyList_Check(o
) && PyList_GET_SIZE(o
) == 2)) {
2454 o1
= PySequence_Fast_GET_ITEM(o
, 0);
2455 o2
= PySequence_Fast_GET_ITEM(o
, 1);
2456 if (!wxPoint2DFromObjects(o1
, o2
, &temp
[idx
])) {
2460 else if (wxPySwigInstance_Check(o
)) {
2462 if (! wxPyConvertSwigPtr(o
, (void **)&pt
, wxT("wxPoint2D"))) {
2467 else if (PySequence_Check(o
) && PySequence_Length(o
) == 2) {
2468 o1
= PySequence_GetItem(o
, 0);
2469 o2
= PySequence_GetItem(o
, 1);
2470 if (!wxPoint2DFromObjects(o1
, o2
, &temp
[idx
])) {
2494 PyErr_SetString(PyExc_TypeError
, "Expected a sequence of length-2 sequences or wxPoint2Ds.");
2498 //---------------------------------------------------------------------------
2501 wxBitmap
** wxBitmap_LIST_helper(PyObject
* source
) {
2502 if (!PyList_Check(source
)) {
2503 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
2506 int count
= PyList_Size(source
);
2507 wxBitmap
** temp
= new wxBitmap
*[count
];
2509 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
2512 for (int x
=0; x
<count
; x
++) {
2513 PyObject
* o
= PyList_GetItem(source
, x
);
2514 if (wxPySwigInstance_Check(o
)) {
2516 if (! wxPyConvertSwigPtr(o
, (void **) &pt
, wxT("wxBitmap"))) {
2517 PyErr_SetString(PyExc_TypeError
,"Expected wxBitmap.");
2523 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxBitmaps.");
2532 wxString
* wxString_LIST_helper(PyObject
* source
) {
2533 if (!PyList_Check(source
)) {
2534 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
2537 int count
= PyList_Size(source
);
2538 wxString
* temp
= new wxString
[count
];
2540 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
2543 for (int x
=0; x
<count
; x
++) {
2544 PyObject
* o
= PyList_GetItem(source
, x
);
2545 #if PYTHON_API_VERSION >= 1009
2546 if (! PyString_Check(o
) && ! PyUnicode_Check(o
)) {
2547 PyErr_SetString(PyExc_TypeError
, "Expected a list of string or unicode objects.");
2551 if (! PyString_Check(o
)) {
2552 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
2557 wxString
* pStr
= wxString_in_helper(o
);
2565 wxAcceleratorEntry
* wxAcceleratorEntry_LIST_helper(PyObject
* source
) {
2566 if (!PyList_Check(source
)) {
2567 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
2570 int count
= PyList_Size(source
);
2571 wxAcceleratorEntry
* temp
= new wxAcceleratorEntry
[count
];
2573 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
2576 for (int x
=0; x
<count
; x
++) {
2577 PyObject
* o
= PyList_GetItem(source
, x
);
2578 if (wxPySwigInstance_Check(o
)) {
2579 wxAcceleratorEntry
* ae
;
2580 if (! wxPyConvertSwigPtr(o
, (void **) &ae
, wxT("wxAcceleratorEntry"))) {
2581 PyErr_SetString(PyExc_TypeError
,"Expected wxAcceleratorEntry.");
2586 else if (PyTuple_Check(o
)) {
2587 PyObject
* o1
= PyTuple_GetItem(o
, 0);
2588 PyObject
* o2
= PyTuple_GetItem(o
, 1);
2589 PyObject
* o3
= PyTuple_GetItem(o
, 2);
2590 temp
[x
].Set(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
));
2593 PyErr_SetString(PyExc_TypeError
, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
2601 wxPen
** wxPen_LIST_helper(PyObject
* source
) {
2602 if (!PyList_Check(source
)) {
2603 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
2606 int count
= PyList_Size(source
);
2607 wxPen
** temp
= new wxPen
*[count
];
2609 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
2612 for (int x
=0; x
<count
; x
++) {
2613 PyObject
* o
= PyList_GetItem(source
, x
);
2614 if (wxPySwigInstance_Check(o
)) {
2616 if (! wxPyConvertSwigPtr(o
, (void **)&pt
, wxT("wxPen"))) {
2618 PyErr_SetString(PyExc_TypeError
,"Expected wxPen.");
2625 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxPens.");
2633 bool wxPy2int_seq_helper(PyObject
* source
, int* i1
, int* i2
) {
2634 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
2637 if (!PySequence_Check(source
) || PySequence_Length(source
) != 2)
2641 o1
= PySequence_Fast_GET_ITEM(source
, 0);
2642 o2
= PySequence_Fast_GET_ITEM(source
, 1);
2645 o1
= PySequence_GetItem(source
, 0);
2646 o2
= PySequence_GetItem(source
, 1);
2649 *i1
= PyInt_AsLong(o1
);
2650 *i2
= PyInt_AsLong(o2
);
2660 bool wxPy4int_seq_helper(PyObject
* source
, int* i1
, int* i2
, int* i3
, int* i4
) {
2661 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
2662 PyObject
*o1
, *o2
, *o3
, *o4
;
2664 if (!PySequence_Check(source
) || PySequence_Length(source
) != 4)
2668 o1
= PySequence_Fast_GET_ITEM(source
, 0);
2669 o2
= PySequence_Fast_GET_ITEM(source
, 1);
2670 o3
= PySequence_Fast_GET_ITEM(source
, 2);
2671 o4
= PySequence_Fast_GET_ITEM(source
, 3);
2674 o1
= PySequence_GetItem(source
, 0);
2675 o2
= PySequence_GetItem(source
, 1);
2676 o3
= PySequence_GetItem(source
, 2);
2677 o4
= PySequence_GetItem(source
, 3);
2680 *i1
= PyInt_AsLong(o1
);
2681 *i2
= PyInt_AsLong(o2
);
2682 *i3
= PyInt_AsLong(o3
);
2683 *i4
= PyInt_AsLong(o4
);
2695 //----------------------------------------------------------------------
2697 bool wxPySimple_typecheck(PyObject
* source
, const wxChar
* classname
, int seqLen
)
2701 if (wxPySwigInstance_Check(source
) &&
2702 wxPyConvertSwigPtr(source
, (void **)&ptr
, classname
))
2706 if (PySequence_Check(source
) && PySequence_Length(source
) == seqLen
)
2712 bool wxSize_helper(PyObject
* source
, wxSize
** obj
)
2714 if (source
== Py_None
) {
2715 **obj
= wxSize(-1,-1);
2718 return wxPyTwoIntItem_helper(source
, obj
, wxT("wxSize"));
2722 bool wxPoint_helper(PyObject
* source
, wxPoint
** obj
)
2724 if (source
== Py_None
) {
2725 **obj
= wxPoint(-1,-1);
2728 return wxPyTwoIntItem_helper(source
, obj
, wxT("wxPoint"));
2733 bool wxPosition_helper(PyObject
* source
, wxPosition
** obj
)
2735 if (source
== Py_None
) {
2736 **obj
= wxPosition(-1,-1);
2739 return wxPyTwoIntItem_helper(source
, obj
, wxT("wxPosition"));
2744 bool wxRealPoint_helper(PyObject
* source
, wxRealPoint
** obj
) {
2746 if (source
== Py_None
) {
2747 **obj
= wxRealPoint(-1,-1);
2751 // If source is an object instance then it may already be the right type
2752 if (wxPySwigInstance_Check(source
)) {
2754 if (! wxPyConvertSwigPtr(source
, (void **)&ptr
, wxT("wxRealPoint")))
2759 // otherwise a 2-tuple of floats is expected
2760 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
2761 PyObject
* o1
= PySequence_GetItem(source
, 0);
2762 PyObject
* o2
= PySequence_GetItem(source
, 1);
2763 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
2768 **obj
= wxRealPoint(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
));
2775 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxRealPoint object.");
2781 bool wxRect_helper(PyObject
* source
, wxRect
** obj
) {
2783 if (source
== Py_None
) {
2784 **obj
= wxRect(-1,-1,-1,-1);
2788 // If source is an object instance then it may already be the right type
2789 if (wxPySwigInstance_Check(source
)) {
2791 if (! wxPyConvertSwigPtr(source
, (void **)&ptr
, wxT("wxRect")))
2796 // otherwise a 4-tuple of integers is expected
2797 else if (PySequence_Check(source
) && PyObject_Length(source
) == 4) {
2798 PyObject
* o1
= PySequence_GetItem(source
, 0);
2799 PyObject
* o2
= PySequence_GetItem(source
, 1);
2800 PyObject
* o3
= PySequence_GetItem(source
, 2);
2801 PyObject
* o4
= PySequence_GetItem(source
, 3);
2802 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
) ||
2803 !PyNumber_Check(o3
) || !PyNumber_Check(o4
)) {
2810 **obj
= wxRect(PyInt_AsLong(o1
), PyInt_AsLong(o2
),
2811 PyInt_AsLong(o3
), PyInt_AsLong(o4
));
2820 PyErr_SetString(PyExc_TypeError
, "Expected a 4-tuple of integers or a wxRect object.");
2826 bool wxColour_helper(PyObject
* source
, wxColour
** obj
) {
2828 if (source
== Py_None
) {
2829 **obj
= wxNullColour
;
2833 // If source is an object instance then it may already be the right type
2834 if (wxPySwigInstance_Check(source
)) {
2836 if (! wxPyConvertSwigPtr(source
, (void **)&ptr
, wxT("wxColour")))
2841 // otherwise check for a string
2842 else if (PyString_Check(source
) || PyUnicode_Check(source
)) {
2843 wxString spec
= Py2wxString(source
);
2844 if (spec
.GetChar(0) == '#' && spec
.Length() == 7) { // It's #RRGGBB
2845 long red
, green
, blue
;
2846 red
= green
= blue
= 0;
2847 spec
.Mid(1,2).ToLong(&red
, 16);
2848 spec
.Mid(3,2).ToLong(&green
, 16);
2849 spec
.Mid(5,2).ToLong(&blue
, 16);
2851 **obj
= wxColour(red
, green
, blue
);
2854 else { // it's a colour name
2855 **obj
= wxColour(spec
);
2859 // last chance: 3-tuple or 4-tuple of integers is expected
2860 else if (PySequence_Check(source
) && PyObject_Length(source
) == 3) {
2861 PyObject
* o1
= PySequence_GetItem(source
, 0);
2862 PyObject
* o2
= PySequence_GetItem(source
, 1);
2863 PyObject
* o3
= PySequence_GetItem(source
, 2);
2864 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
) || !PyNumber_Check(o3
)) {
2870 **obj
= wxColour(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
));
2876 else if (PySequence_Check(source
) && PyObject_Length(source
) == 4) {
2877 PyObject
* o1
= PySequence_GetItem(source
, 0);
2878 PyObject
* o2
= PySequence_GetItem(source
, 1);
2879 PyObject
* o3
= PySequence_GetItem(source
, 2);
2880 PyObject
* o4
= PySequence_GetItem(source
, 3);
2881 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
) || !PyNumber_Check(o3
) || !PyNumber_Check(o4
)) {
2888 **obj
= wxColour(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
), PyInt_AsLong(o4
));
2897 PyErr_SetString(PyExc_TypeError
,
2898 "Expected a wxColour object, a string containing a colour name or '#RRGGBB', or a 3- or 4-tuple of integers.");
2903 bool wxColour_typecheck(PyObject
* source
) {
2905 if (wxPySimple_typecheck(source
, wxT("wxColour"), 3))
2908 if (PyString_Check(source
) || PyUnicode_Check(source
))
2916 bool wxPoint2D_helper(PyObject
* source
, wxPoint2D
** obj
) {
2918 if (source
== Py_None
) {
2919 **obj
= wxPoint2D(-1,-1);
2923 // If source is an object instance then it may already be the right type
2924 if (wxPySwigInstance_Check(source
)) {
2926 if (! wxPyConvertSwigPtr(source
, (void **)&ptr
, wxT("wxPoint2D")))
2931 // otherwise a length-2 sequence of floats is expected
2932 if (PySequence_Check(source
) && PySequence_Length(source
) == 2) {
2933 PyObject
* o1
= PySequence_GetItem(source
, 0);
2934 PyObject
* o2
= PySequence_GetItem(source
, 1);
2935 // This should really check for floats, not numbers -- but that would break code.
2936 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
2941 **obj
= wxPoint2D(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
));
2947 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wx.Point2D object.");
2953 bool wxRect2D_helper(PyObject
* source
, wxRect2D
** obj
) {
2955 if (source
== Py_None
) {
2956 **obj
= wxRect2D(-1,-1,-1,-1);
2960 // If source is an object instance then it may already be the right type
2961 if (wxPySwigInstance_Check(source
)) {
2963 if (! wxPyConvertSwigPtr(source
, (void **)&ptr
, wxT("wxRect2D")))
2968 // otherwise a length-4 sequence of floats is expected
2969 if (PySequence_Check(source
) && PySequence_Length(source
) == 4) {
2970 PyObject
* o1
= PySequence_GetItem(source
, 0);
2971 PyObject
* o2
= PySequence_GetItem(source
, 1);
2972 PyObject
* o3
= PySequence_GetItem(source
, 2);
2973 PyObject
* o4
= PySequence_GetItem(source
, 3);
2974 // This should really check for floats, not numbers -- but that would break code.
2975 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
) ||
2976 !PyNumber_Check(o3
) || !PyNumber_Check(o4
)) {
2983 **obj
= wxRect2D(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
),
2984 PyFloat_AsDouble(o3
), PyFloat_AsDouble(o4
));
2992 PyErr_SetString(PyExc_TypeError
, "Expected a 4-tuple of floats or a wx.Rect2D object.");
2997 //----------------------------------------------------------------------
2999 PyObject
* wxArrayString2PyList_helper(const wxArrayString
& arr
)
3001 PyObject
* list
= PyList_New(0);
3002 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
3004 PyObject
* str
= PyUnicode_FromWideChar(arr
[i
].c_str(), arr
[i
].Len());
3006 PyObject
* str
= PyString_FromStringAndSize(arr
[i
].c_str(), arr
[i
].Len());
3008 PyList_Append(list
, str
);
3015 PyObject
* wxArrayInt2PyList_helper(const wxArrayInt
& arr
)
3017 PyObject
* list
= PyList_New(0);
3018 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
3019 PyObject
* number
= PyInt_FromLong(arr
[i
]);
3020 PyList_Append(list
, number
);
3027 PyObject
* wxArrayDouble2PyList_helper(const wxArrayDouble
& arr
)
3029 PyObject
* list
= PyList_New(0);
3030 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
3031 PyObject
* number
= PyFloat_FromDouble(arr
[i
]);
3032 PyList_Append(list
, number
);
3039 //----------------------------------------------------------------------
3040 // wxPyImageHandler methods
3042 // TODO: Switch these to use wxPython's standard macros and helper classes
3043 // for calling callbacks.
3045 PyObject
* wxPyImageHandler::m_DoCanRead_Name
= NULL
;
3046 PyObject
* wxPyImageHandler::m_GetImageCount_Name
= NULL
;
3047 PyObject
* wxPyImageHandler::m_LoadFile_Name
= NULL
;
3048 PyObject
* wxPyImageHandler::m_SaveFile_Name
= NULL
;
3050 PyObject
* wxPyImageHandler::py_InputStream(wxInputStream
* stream
) {
3051 return wxPyConstructObject(new wxPyInputStream(stream
),
3052 wxT("wxPyInputStream"), 0);
3055 PyObject
* wxPyImageHandler::py_Image(wxImage
* image
) {
3056 return wxPyConstructObject(image
, wxT("wxImage"), 0);
3059 PyObject
* wxPyImageHandler::py_OutputStream(wxOutputStream
* stream
) {
3060 return wxPyConstructObject(stream
, wxT("wxOutputStream"), 0);
3063 wxPyImageHandler::wxPyImageHandler():
3066 if (!m_DoCanRead_Name
) {
3067 m_DoCanRead_Name
= PyString_FromString("DoCanRead");
3068 m_GetImageCount_Name
= PyString_FromString("GetImageCount");
3069 m_LoadFile_Name
= PyString_FromString("LoadFile");
3070 m_SaveFile_Name
= PyString_FromString("SaveFile");
3074 wxPyImageHandler::~wxPyImageHandler() {
3081 void wxPyImageHandler::_SetSelf(PyObject
*self
) {
3082 // should check here for isinstance(PyImageHandler) ??
3087 bool wxPyImageHandler::DoCanRead(wxInputStream
& stream
) {
3088 // check if our object has this method
3089 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
3090 if (!m_self
|| !PyObject_HasAttr(m_self
, m_DoCanRead_Name
)) {
3091 wxPyEndBlockThreads(blocked
);
3095 PyObject
* res
= PyObject_CallMethodObjArgs(m_self
, m_DoCanRead_Name
,
3096 py_InputStream(&stream
), NULL
);
3097 bool retval
= false;
3099 retval
= PyInt_AsLong(res
);
3105 wxPyEndBlockThreads(blocked
);
3109 bool wxPyImageHandler::LoadFile( wxImage
* image
, wxInputStream
& stream
,
3110 bool verbose
, int index
) {
3111 // check if our object has this method
3112 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
3113 if (!m_self
|| !PyObject_HasAttr(m_self
, m_LoadFile_Name
)) {
3114 wxPyEndBlockThreads(blocked
);
3117 PyObject
* res
= PyObject_CallMethodObjArgs(m_self
, m_LoadFile_Name
,
3119 py_InputStream(&stream
),
3120 PyInt_FromLong(verbose
),
3121 PyInt_FromLong(index
),
3123 bool retval
= false;
3125 retval
= PyInt_AsLong(res
);
3130 wxPyEndBlockThreads(blocked
);
3134 bool wxPyImageHandler::SaveFile( wxImage
* image
, wxOutputStream
& stream
,
3136 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
3137 if (!m_self
|| !PyObject_HasAttr(m_self
, m_SaveFile_Name
)) {
3138 wxPyEndBlockThreads(blocked
);
3141 PyObject
* res
= PyObject_CallMethodObjArgs(m_self
, m_SaveFile_Name
,
3143 py_OutputStream(&stream
),
3144 PyInt_FromLong(verbose
),
3146 bool retval
= false;
3148 retval
=PyInt_AsLong(res
);
3153 wxPyEndBlockThreads(blocked
);
3157 int wxPyImageHandler::GetImageCount( wxInputStream
& stream
) {
3158 wxPyBlock_t blocked
= wxPyBeginBlockThreads();
3159 if (!m_self
|| !PyObject_HasAttr(m_self
, m_GetImageCount_Name
)) {
3160 wxPyEndBlockThreads(blocked
);
3163 PyObject
*res
=PyObject_CallMethodObjArgs(m_self
, m_GetImageCount_Name
,
3164 py_InputStream(&stream
),
3168 retval
=PyInt_AsLong(res
);
3173 wxPyEndBlockThreads(blocked
);
3178 //----------------------------------------------------------------------
3179 // Function to test if the Display (or whatever is the platform equivallent)
3180 // can be connected to. This is accessable from wxPython as a staticmethod of
3181 // wx.App called DisplayAvailable().
3184 bool wxPyTestDisplayAvailable()
3188 display
= XOpenDisplay(NULL
);
3189 if (display
== NULL
)
3191 XCloseDisplay(display
);
3196 // This is adapted from Python's Mac/Modules/MacOS.c in the
3197 // MacOS_WMAvailable function.
3199 ProcessSerialNumber psn
;
3202 ** This is a fairly innocuous call to make if we don't have a window
3203 ** manager, or if we have no permission to talk to it. It will print
3204 ** a message on stderr, but at least it won't abort the process.
3205 ** It appears the function caches the result itself, and it's cheap, so
3206 ** no need for us to cache.
3208 #ifdef kCGNullDirectDisplay
3209 /* On 10.1 CGMainDisplayID() isn't available, and
3210 ** kCGNullDirectDisplay isn't defined.
3212 if (CGMainDisplayID() == 0) {
3217 // Also foreground the application on the first call as a side-effect.
3218 if (GetCurrentProcess(&psn
) < 0 || SetFrontProcess(&psn
) < 0) {
3234 //----------------------------------------------------------------------
3235 //----------------------------------------------------------------------