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"
20 #include <wx/msw/private.h>
21 #include <wx/msw/winundef.h>
22 #include <wx/msw/msvcrt.h>
29 #include <gdk/gdkprivate.h>
30 #include <wx/gtk/win_gtk.h>
31 #define GetXWindow(wxwin) GDK_WINDOW_XWINDOW((wxwin)->m_widget->window)
36 #include "wx/x11/privx.h"
37 #define GetXWindow(wxwin) ((Window)(wxwin)->GetHandle())
41 #include <wx/mac/private.h>
44 #include <wx/clipbrd.h>
45 #include <wx/mimetype.h>
48 //----------------------------------------------------------------------
50 #if PYTHON_API_VERSION <= 1007 && wxUSE_UNICODE
51 #error Python must support Unicode to use wxWindows Unicode
54 //----------------------------------------------------------------------
56 wxPyApp
* wxPythonApp
= NULL
; // Global instance of application object
57 bool wxPyDoCleanup
= False
;
58 bool wxPyDoingCleanup
= False
;
61 #ifdef WXP_WITH_THREAD
62 struct wxPyThreadState
{
64 PyThreadState
* tstate
;
66 wxPyThreadState(unsigned long _tid
=0, PyThreadState
* _tstate
=NULL
)
67 : tid(_tid
), tstate(_tstate
) {}
70 #include <wx/dynarray.h>
71 WX_DECLARE_OBJARRAY(wxPyThreadState
, wxPyThreadStateArray
);
72 #include <wx/arrimpl.cpp>
73 WX_DEFINE_OBJARRAY(wxPyThreadStateArray
);
75 wxPyThreadStateArray
* wxPyTStates
= NULL
;
76 wxMutex
* wxPyTMutex
= NULL
;
80 static PyObject
* wxPython_dict
= NULL
;
81 static PyObject
* wxPyAssertionError
= NULL
;
83 PyObject
* wxPyPtrTypeMap
= NULL
;
86 #ifdef __WXMSW__ // If building for win32...
87 //----------------------------------------------------------------------
88 // This gets run when the DLL is loaded. We just need to save a handle.
89 //----------------------------------------------------------------------
92 HINSTANCE hinstDLL
, // handle to DLL module
93 DWORD fdwReason
, // reason for calling function
94 LPVOID lpvReserved
// reserved
97 // If wxPython is embedded in another wxWindows app then
98 // the inatance has already been set.
99 if (! wxGetInstance())
100 wxSetInstance(hinstDLL
);
105 //----------------------------------------------------------------------
106 // Classes for implementing the wxp main application shell.
107 //----------------------------------------------------------------------
109 IMPLEMENT_ABSTRACT_CLASS(wxPyApp
, wxApp
);
113 m_assertMode
= wxPYAPP_ASSERT_EXCEPTION
;
114 m_startupComplete
= False
;
118 wxPyApp::~wxPyApp() {
122 // This one isn't acutally called... We fake it with _BootstrapApp
123 bool wxPyApp::OnInit() {
128 int wxPyApp::MainLoop() {
131 DeletePendingObjects();
132 bool initialized
= wxTopLevelWindows
.GetCount() != 0;
134 if ( m_exitOnFrameDelete
== Later
) {
135 m_exitOnFrameDelete
= Yes
;
138 retval
= wxApp::MainLoop();
145 bool wxPyApp::OnInitGui() {
147 wxApp::OnInitGui(); // in this case always call the base class version
148 bool blocked
= wxPyBeginBlockThreads();
149 if (wxPyCBH_findCallback(m_myInst
, "OnInitGui"))
150 rval
= wxPyCBH_callCallback(m_myInst
, Py_BuildValue("()"));
151 wxPyEndBlockThreads(blocked
);
156 int wxPyApp::OnExit() {
158 bool blocked
= wxPyBeginBlockThreads();
159 if (wxPyCBH_findCallback(m_myInst
, "OnExit"))
160 rval
= wxPyCBH_callCallback(m_myInst
, Py_BuildValue("()"));
161 wxPyEndBlockThreads(blocked
);
162 wxApp::OnExit(); // in this case always call the base class version
168 void wxPyApp::OnAssert(const wxChar
*file
,
173 // if we're not fully initialized then just log the error
174 if (! m_startupComplete
) {
177 buf
.Printf(wxT("%s(%d): assert \"%s\" failed"),
187 // If the OnAssert is overloaded in the Python class then call it...
189 bool blocked
= wxPyBeginBlockThreads();
190 if ((found
= wxPyCBH_findCallback(m_myInst
, "OnAssert"))) {
191 PyObject
* fso
= wx2PyString(file
);
192 PyObject
* cso
= wx2PyString(file
);
195 mso
= wx2PyString(file
);
197 mso
= Py_None
; Py_INCREF(Py_None
);
199 wxPyCBH_callCallback(m_myInst
, Py_BuildValue("(OiOO)", fso
, line
, cso
, mso
));
204 wxPyEndBlockThreads(blocked
);
206 // ...otherwise do our own thing with it
209 if (m_assertMode
& wxPYAPP_ASSERT_SUPPRESS
)
212 // turn it into a Python exception?
213 if (m_assertMode
& wxPYAPP_ASSERT_EXCEPTION
) {
216 buf
.Printf(wxT("C++ assertion \"%s\" failed in %s(%d)"), cond
, file
, line
);
223 bool blocked
= wxPyBeginBlockThreads();
224 PyObject
* s
= wx2PyString(buf
);
225 PyErr_SetObject(wxPyAssertionError
, s
);
227 wxPyEndBlockThreads(blocked
);
229 // Now when control returns to whatever API wrapper was called from
230 // Python it should detect that an exception is set and will return
231 // NULL, signalling the exception to Python.
234 // Send it to the normal log destination, but only if
235 // not _DIALOG because it will call this too
236 if ( (m_assertMode
& wxPYAPP_ASSERT_LOG
) && !(m_assertMode
& wxPYAPP_ASSERT_DIALOG
)) {
239 buf
.Printf(wxT("%s(%d): assert \"%s\" failed"),
248 // do the normal wx assert dialog?
249 if (m_assertMode
& wxPYAPP_ASSERT_DIALOG
)
250 wxApp::OnAssert(file
, line
, cond
, msg
);
255 // For catching Apple Events
256 void wxPyApp::MacOpenFile(const wxString
&fileName
)
258 bool blocked
= wxPyBeginBlockThreads();
259 if (wxPyCBH_findCallback(m_myInst
, "MacOpenFile")) {
260 PyObject
* s
= wx2PyString(fileName
);
261 wxPyCBH_callCallback(m_myInst
, Py_BuildValue("(O)", s
));
264 wxPyEndBlockThreads(blocked
);
267 void wxPyApp::MacPrintFile(const wxString
&fileName
)
269 bool blocked
= wxPyBeginBlockThreads();
270 if (wxPyCBH_findCallback(m_myInst
, "MacPrintFile")) {
271 PyObject
* s
= wx2PyString(fileName
);
272 wxPyCBH_callCallback(m_myInst
, Py_BuildValue("(O)", s
));
275 wxPyEndBlockThreads(blocked
);
278 void wxPyApp::MacNewFile()
280 bool blocked
= wxPyBeginBlockThreads();
281 if (wxPyCBH_findCallback(m_myInst
, "MacNewFile"))
282 wxPyCBH_callCallback(m_myInst
, Py_BuildValue("()"));
283 wxPyEndBlockThreads(blocked
);
286 void wxPyApp::MacReopenApp()
288 bool blocked
= wxPyBeginBlockThreads();
289 if (wxPyCBH_findCallback(m_myInst
, "MacReopenApp"))
290 wxPyCBH_callCallback(m_myInst
, Py_BuildValue("()"));
291 wxPyEndBlockThreads(blocked
);
296 bool wxPyApp::GetMacSupportPCMenuShortcuts() {
298 return s_macSupportPCMenuShortcuts
;
305 long wxPyApp::GetMacAboutMenuItemId() {
307 return s_macAboutMenuItemId
;
314 long wxPyApp::GetMacPreferencesMenuItemId() {
316 return s_macPreferencesMenuItemId
;
323 long wxPyApp::GetMacExitMenuItemId() {
325 return s_macExitMenuItemId
;
332 wxString
wxPyApp::GetMacHelpMenuTitleName() {
334 return s_macHelpMenuTitleName
;
336 return wxEmptyString
;
341 void wxPyApp::SetMacSupportPCMenuShortcuts(bool val
) {
343 s_macSupportPCMenuShortcuts
= val
;
348 void wxPyApp::SetMacAboutMenuItemId(long val
) {
350 s_macAboutMenuItemId
= val
;
355 void wxPyApp::SetMacPreferencesMenuItemId(long val
) {
357 s_macPreferencesMenuItemId
= val
;
362 void wxPyApp::SetMacExitMenuItemId(long val
) {
364 s_macExitMenuItemId
= val
;
369 void wxPyApp::SetMacHelpMenuTitleName(const wxString
& val
) {
371 s_macHelpMenuTitleName
= val
;
376 // This finishes the initialization of wxWindows and then calls the OnInit
377 // that should be present in the derived (Python) class.
378 void wxPyApp::_BootstrapApp()
381 PyObject
* retval
= NULL
;
382 PyObject
* pyint
= NULL
;
385 // Get any command-line args passed to this program from the sys module
388 bool blocked
= wxPyBeginBlockThreads();
389 PyObject
* sysargv
= PySys_GetObject("argv");
390 if (sysargv
!= NULL
) {
391 argc
= PyList_Size(sysargv
);
392 argv
= new char*[argc
+1];
394 for(x
=0; x
<argc
; x
++) {
395 PyObject
*pyArg
= PyList_GetItem(sysargv
, x
);
396 argv
[x
] = PyString_AsString(pyArg
);
400 wxPyEndBlockThreads(blocked
);
402 // Initialize wxWidgets
403 result
= wxEntryStart(argc
, argv
);
406 blocked
= wxPyBeginBlockThreads();
408 PyErr_SetString(PyExc_SystemError
,
409 "wxEntryStart failed, unable to initialize wxWidgets!"
411 " (Is DISPLAY set properly?)"
417 // On wxGTK the locale will be changed to match the system settings, but
418 // Python needs to have LC_NUMERIC set to "C" in order for the floating
419 // point conversions and such to work right.
421 setlocale(LC_NUMERIC
, "C");
424 // The stock objects were all NULL when they were loaded into
425 // SWIG generated proxies, so re-init those now...
426 wxPy_ReinitStockObjects(3);
428 // It's now ok to generate exceptions for assertion errors.
429 wxPythonApp
->SetStartupComplete(True
);
431 // Call the Python wxApp's OnInit function
432 if (wxPyCBH_findCallback(m_myInst
, "OnInit")) {
434 PyObject
* method
= m_myInst
.GetLastFound();
435 PyObject
* argTuple
= PyTuple_New(0);
436 retval
= PyEval_CallObject(method
, argTuple
);
442 pyint
= PyNumber_Int(retval
);
444 PyErr_SetString(PyExc_TypeError
, "OnInit should return a boolean value");
447 result
= PyInt_AS_LONG(pyint
);
450 // Is it okay if there is no OnInit? Probably so...
455 PyErr_SetString(PyExc_SystemExit
, "OnInit returned False, exiting...");
462 wxPyEndBlockThreads(blocked
);
465 //---------------------------------------------------------------------
466 //----------------------------------------------------------------------
470 static char* wxPyCopyCString(const wxChar
* src
)
472 wxWX2MBbuf buff
= (wxWX2MBbuf
)wxConvCurrent
->cWX2MB(src
);
473 size_t len
= strlen(buff
);
474 char* dest
= new char[len
+1];
480 static char* wxPyCopyCString(const char* src
) // we need a char version too
482 size_t len
= strlen(src
);
483 char* dest
= new char[len
+1];
489 static wxChar
* wxPyCopyWString(const char *src
)
491 //wxMB2WXbuf buff = wxConvCurrent->cMB2WX(src);
492 wxString
str(src
, *wxConvCurrent
);
493 return copystring(str
);
497 static wxChar
* wxPyCopyWString(const wxChar
*src
)
499 return copystring(src
);
505 inline const char* dropwx(const char* name
) {
506 if (name
[0] == 'w' && name
[1] == 'x')
512 //----------------------------------------------------------------------
514 // This function is called when the wxc module is imported to do some initial
515 // setup. (Before there is a wxApp object.) The rest happens in
516 // wxPyApp::_BootstrapApp
517 void __wxPyPreStart(PyObject
* moduleDict
)
521 // wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
524 #ifdef WXP_WITH_THREAD
525 PyEval_InitThreads();
526 wxPyTStates
= new wxPyThreadStateArray
;
527 wxPyTMutex
= new wxMutex
;
529 // Save the current (main) thread state in our array
530 PyThreadState
* tstate
= wxPyBeginAllowThreads();
531 wxPyEndAllowThreads(tstate
);
534 // Ensure that the build options in the DLL (or whatever) match this build
535 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE
, "wxPython");
537 // Init the stock objects to a non-NULL value so SWIG doesn't create them as None
538 wxPy_ReinitStockObjects(1);
540 wxInitAllImageHandlers();
545 void __wxPyCleanup() {
546 wxPyDoingCleanup
= True
;
548 wxPyDoCleanup
= False
;
551 #ifdef WXP_WITH_THREAD
554 wxPyTStates
->Empty();
561 // Save a reference to the dictionary of the wx._core module, and inject
562 // a few more things into it.
563 PyObject
* __wxPySetDictionary(PyObject
* /* self */, PyObject
* args
)
566 if (!PyArg_ParseTuple(args
, "O", &wxPython_dict
))
569 if (!PyDict_Check(wxPython_dict
)) {
570 PyErr_SetString(PyExc_TypeError
, "_wxPySetDictionary must have dictionary object!");
574 if (! wxPyPtrTypeMap
)
575 wxPyPtrTypeMap
= PyDict_New();
576 PyDict_SetItemString(wxPython_dict
, "__wxPyPtrTypeMap", wxPyPtrTypeMap
);
578 // Create an exception object to use for wxASSERTions
579 wxPyAssertionError
= PyErr_NewException("wx._core.PyAssertionError",
580 PyExc_AssertionError
, NULL
);
581 PyDict_SetItemString(wxPython_dict
, "PyAssertionError", wxPyAssertionError
);
585 #define wxPlatform "__WXMOTIF__"
586 #define wxPlatName "wxMotif"
589 #define wxPlatform "__WXX11__"
590 #define wxPlatName "wxX11"
593 #define wxPlatform "__WXGTK__"
594 #define wxPlatName "wxGTK"
597 #define wxPlatform "__WXMSW__"
598 #define wxPlatName "wxMSW"
601 #define wxPlatform "__WXMAC__"
602 #define wxPlatName "wxMac"
611 // These should be deprecated in favor of the PlatformInfo tuple built below...
612 PyDict_SetItemString(wxPython_dict
, "Platform", PyString_FromString(wxPlatform
));
613 PyDict_SetItemString(wxPython_dict
, "USE_UNICODE", PyInt_FromLong(wxUSE_UNICODE
));
614 PyDict_SetItemString(wxPython_dict
, "__WXDEBUG__", PyInt_FromLong(wxdebug
));
617 PyObject
* PlatInfo
= PyList_New(0);
620 #define _AddInfoString(st) \
621 obj = PyString_FromString(st); \
622 PyList_Append(PlatInfo, obj); \
625 _AddInfoString(wxPlatform
);
626 _AddInfoString(wxPlatName
);
628 _AddInfoString("unicode");
630 _AddInfoString("ascii");
634 _AddInfoString("gtk2");
636 _AddInfoString("gtk1");
640 #undef _AddInfoString
642 PyObject
* PlatInfoTuple
= PyList_AsTuple(PlatInfo
);
644 PyDict_SetItemString(wxPython_dict
, "PlatformInfo", PlatInfoTuple
);
650 //---------------------------------------------------------------------------
652 // Python's PyInstance_Check does not return True for instances of new-style
653 // classes. This should get close enough for both new and old classes but I
654 // should re-evaluate the need for doing instance checks...
655 bool wxPyInstance_Check(PyObject
* obj
) {
656 return PyObject_HasAttrString(obj
, "__class__") != 0;
660 // This one checks if the object is an instance of a SWIG proxy class (it has
661 // a .this attribute)
662 bool wxPySwigInstance_Check(PyObject
* obj
) {
663 return PyObject_HasAttrString(obj
, "this") != 0;
666 //---------------------------------------------------------------------------
668 // The stock objects are no longer created when the wxc module is imported,
669 // but only after the app object has been created. The
670 // wxPy_ReinitStockObjects function will be called 3 times to pass the stock
671 // objects though various stages of evolution:
673 // pass 1: Set all the pointers to a non-NULL value so the Python proxy
674 // object will be created (otherwise it will just use None.)
676 // pass 2: After the module has been imported and the python proxys have
677 // been created, then set the __class__ to be _wxPyUnbornObject so
678 // it will catch any access to the object and will raise an exception.
680 // pass 3: Finally, from OnInit patch things up so the stock objects can
684 PyObject
* __wxPyFixStockObjects(PyObject
* /* self */, PyObject
* args
)
686 wxPy_ReinitStockObjects(2);
691 static void rsoPass2(const char* name
)
693 static PyObject
* unbornObjectClass
= NULL
;
696 if (unbornObjectClass
== NULL
) {
697 unbornObjectClass
= PyDict_GetItemString(wxPython_dict
, "_wxPyUnbornObject");
698 Py_INCREF(unbornObjectClass
);
701 // Find the object instance
702 obj
= PyDict_GetItemString(wxPython_dict
, (char*)dropwx(name
));
703 wxCHECK_RET(obj
!= NULL
, wxT("Unable to find stock object"));
704 wxCHECK_RET(wxPySwigInstance_Check(obj
), wxT("Not a swig instance"));
707 PyObject_SetAttrString(obj
, "__class__", unbornObjectClass
);
711 static void rsoPass3(const char* name
, const char* classname
, void* ptr
)
717 // Find the object instance
718 obj
= PyDict_GetItemString(wxPython_dict
, (char*)dropwx(name
));
719 wxCHECK_RET(obj
!= NULL
, wxT("Unable to find stock object"));
720 wxCHECK_RET(wxPySwigInstance_Check(obj
), wxT("Not a swig instance"));
722 // Find the class object and put it back in the instance
723 classobj
= PyDict_GetItemString(wxPython_dict
, (char*)dropwx(classname
));
724 wxCHECK_RET(classobj
!= NULL
, wxT("Unable to find stock class object"));
725 PyObject_SetAttrString(obj
, "__class__", classobj
);
727 // Rebuild the .this swigified pointer with the new value of the C++ pointer
728 ptrobj
= wxPyMakeSwigPtr(ptr
, wxString(classname
, *wxConvCurrent
));
729 PyObject_SetAttrString(obj
, "this", ptrobj
);
735 void wxPy_ReinitStockObjects(int pass
)
738 #define REINITOBJ(name, classname) \
739 if (pass == 1) { name = (classname*)0xC0C0C0C0; } \
740 else if (pass == 2) { rsoPass2(#name); } \
741 else if (pass == 3) { rsoPass3(#name, #classname, (void*)name); }
744 #define REINITOBJ2(name, classname) \
746 else if (pass == 2) { rsoPass2(#name); } \
747 else if (pass == 3) { rsoPass3(#name, #classname, (void*)&name); }
749 // If there is already an App object then wxPython is probably embedded in
750 // a wx C++ application, so there is no need to do all this.
751 static bool embedded
= false;
752 if ((pass
== 1 || pass
== 2) && wxTheApp
) {
756 if (pass
== 3 && embedded
)
760 REINITOBJ(wxNORMAL_FONT
, wxFont
);
761 REINITOBJ(wxSMALL_FONT
, wxFont
);
762 REINITOBJ(wxITALIC_FONT
, wxFont
);
763 REINITOBJ(wxSWISS_FONT
, wxFont
);
765 REINITOBJ(wxRED_PEN
, wxPen
);
766 REINITOBJ(wxCYAN_PEN
, wxPen
);
767 REINITOBJ(wxGREEN_PEN
, wxPen
);
768 REINITOBJ(wxBLACK_PEN
, wxPen
);
769 REINITOBJ(wxWHITE_PEN
, wxPen
);
770 REINITOBJ(wxTRANSPARENT_PEN
, wxPen
);
771 REINITOBJ(wxBLACK_DASHED_PEN
, wxPen
);
772 REINITOBJ(wxGREY_PEN
, wxPen
);
773 REINITOBJ(wxMEDIUM_GREY_PEN
, wxPen
);
774 REINITOBJ(wxLIGHT_GREY_PEN
, wxPen
);
776 REINITOBJ(wxBLUE_BRUSH
, wxBrush
);
777 REINITOBJ(wxGREEN_BRUSH
, wxBrush
);
778 REINITOBJ(wxWHITE_BRUSH
, wxBrush
);
779 REINITOBJ(wxBLACK_BRUSH
, wxBrush
);
780 REINITOBJ(wxTRANSPARENT_BRUSH
, wxBrush
);
781 REINITOBJ(wxCYAN_BRUSH
, wxBrush
);
782 REINITOBJ(wxRED_BRUSH
, wxBrush
);
783 REINITOBJ(wxGREY_BRUSH
, wxBrush
);
784 REINITOBJ(wxMEDIUM_GREY_BRUSH
, wxBrush
);
785 REINITOBJ(wxLIGHT_GREY_BRUSH
, wxBrush
);
787 REINITOBJ(wxBLACK
, wxColour
);
788 REINITOBJ(wxWHITE
, wxColour
);
789 REINITOBJ(wxRED
, wxColour
);
790 REINITOBJ(wxBLUE
, wxColour
);
791 REINITOBJ(wxGREEN
, wxColour
);
792 REINITOBJ(wxCYAN
, wxColour
);
793 REINITOBJ(wxLIGHT_GREY
, wxColour
);
795 REINITOBJ(wxSTANDARD_CURSOR
, wxCursor
);
796 REINITOBJ(wxHOURGLASS_CURSOR
, wxCursor
);
797 REINITOBJ(wxCROSS_CURSOR
, wxCursor
);
799 REINITOBJ2(wxNullBitmap
, wxBitmap
);
800 REINITOBJ2(wxNullIcon
, wxIcon
);
801 REINITOBJ2(wxNullCursor
, wxCursor
);
802 REINITOBJ2(wxNullPen
, wxPen
);
803 REINITOBJ2(wxNullBrush
, wxBrush
);
804 REINITOBJ2(wxNullPalette
, wxPalette
);
805 REINITOBJ2(wxNullFont
, wxFont
);
806 REINITOBJ2(wxNullColour
, wxColour
);
808 REINITOBJ(wxTheFontList
, wxFontList
);
809 REINITOBJ(wxThePenList
, wxPenList
);
810 REINITOBJ(wxTheBrushList
, wxBrushList
);
811 REINITOBJ(wxTheColourDatabase
, wxColourDatabase
);
814 REINITOBJ(wxTheClipboard
, wxClipboard
);
815 REINITOBJ2(wxDefaultValidator
, wxValidator
);
816 REINITOBJ2(wxNullImage
, wxImage
);
817 REINITOBJ2(wxNullAcceleratorTable
, wxAcceleratorTable
);
823 //---------------------------------------------------------------------------
825 void wxPyClientData_dtor(wxPyClientData
* self
) {
826 if (! wxPyDoingCleanup
) { // Don't do it during cleanup as Python
827 // may have already garbage collected the object...
828 bool blocked
= wxPyBeginBlockThreads();
829 Py_DECREF(self
->m_obj
);
831 wxPyEndBlockThreads(blocked
);
835 void wxPyUserData_dtor(wxPyUserData
* self
) {
836 if (! wxPyDoingCleanup
) {
837 bool blocked
= wxPyBeginBlockThreads();
838 Py_DECREF(self
->m_obj
);
840 wxPyEndBlockThreads(blocked
);
845 // This is called when an OOR controled object is being destroyed. Although
846 // the C++ object is going away there is no way to force the Python object
847 // (and all references to it) to die too. This causes problems (crashes) in
848 // wxPython when a python shadow object attempts to call a C++ method using
849 // the now bogus pointer... So to try and prevent this we'll do a little black
850 // magic and change the class of the python instance to a class that will
851 // raise an exception for any attempt to call methods with it. See
852 // _wxPyDeadObject in _core_ex.py for the implementation of this class.
853 void wxPyOORClientData_dtor(wxPyOORClientData
* self
) {
855 static PyObject
* deadObjectClass
= NULL
;
857 bool blocked
= wxPyBeginBlockThreads();
858 if (deadObjectClass
== NULL
) {
859 deadObjectClass
= PyDict_GetItemString(wxPython_dict
, "_wxPyDeadObject");
860 // TODO: Can not wxASSERT here because inside a wxPyBeginBlock Threads,
861 // will lead to a deadlock when it tries to aquire the GIL again.
862 //wxASSERT_MSG(deadObjectClass != NULL, wxT("Can't get _wxPyDeadObject class!"));
863 Py_INCREF(deadObjectClass
);
867 // Only if there is more than one reference to the object
868 if ( !wxPyDoingCleanup
&& self
->m_obj
->ob_refcnt
> 1 ) {
869 // bool isInstance = wxPyInstance_Check(self->m_obj);
871 //wxASSERT_MSG(isInstance, wxT("m_obj not an instance!?!?!"));
873 // Call __del__, if there is one.
874 PyObject
* func
= PyObject_GetAttrString(self
->m_obj
, "__del__");
876 PyObject
* rv
= PyObject_CallMethod(self
->m_obj
, "__del__", NULL
);
880 if (PyErr_Occurred())
881 PyErr_Clear(); // just ignore it for now
884 PyObject
* dict
= PyObject_GetAttrString(self
->m_obj
, "__dict__");
886 // Clear the instance's dictionary
889 // put the name of the old class into the instance, and then reset the
890 // class to be the dead class.
891 PyObject
* klass
= PyObject_GetAttrString(self
->m_obj
, "__class__");
892 PyObject
* name
= PyObject_GetAttrString(klass
, "__name__");
893 PyDict_SetItemString(dict
, "_name", name
);
894 PyObject_SetAttrString(self
->m_obj
, "__class__", deadObjectClass
);
895 //Py_INCREF(deadObjectClass);
901 // m_obj is DECREF'd in the base class dtor...
902 wxPyEndBlockThreads(blocked
);
906 //---------------------------------------------------------------------------
907 // Stuff used by OOR to find the right wxPython class type to return and to
911 // The pointer type map is used when the "pointer" type name generated by SWIG
912 // is not the same as the shadow class name, for example wxPyTreeCtrl
913 // vs. wxTreeCtrl. It needs to be referenced in Python as well as from C++,
914 // so we'll just make it a Python dictionary in the wx module's namespace.
915 // (See __wxSetDictionary)
916 void wxPyPtrTypeMap_Add(const char* commonName
, const char* ptrName
) {
917 if (! wxPyPtrTypeMap
)
918 wxPyPtrTypeMap
= PyDict_New();
919 PyDict_SetItemString(wxPyPtrTypeMap
,
921 PyString_FromString((char*)ptrName
));
927 PyObject
* wxPyMake_wxObject(wxObject
* source
, bool checkEvtHandler
) {
928 PyObject
* target
= NULL
;
929 bool isEvtHandler
= False
;
932 // If it's derived from wxEvtHandler then there may
933 // already be a pointer to a Python object that we can use
935 if (checkEvtHandler
&& wxIsKindOf(source
, wxEvtHandler
)) {
937 wxEvtHandler
* eh
= (wxEvtHandler
*)source
;
938 wxPyOORClientData
* data
= (wxPyOORClientData
*)eh
->GetClientObject();
940 target
= data
->m_obj
;
947 // Otherwise make it the old fashioned way by making a new shadow
948 // object and putting this pointer in it. Look up the class
949 // heirarchy until we find a class name that is located in the
951 const wxClassInfo
* info
= source
->GetClassInfo();
952 wxString name
= info
->GetClassName();
953 bool exists
= wxPyCheckSwigType(name
);
954 while (info
&& !exists
) {
955 info
= info
->GetBaseClass1();
956 name
= info
->GetClassName();
957 exists
= wxPyCheckSwigType(name
);
960 target
= wxPyConstructObject((void*)source
, name
, False
);
961 if (target
&& isEvtHandler
)
962 ((wxEvtHandler
*)source
)->SetClientObject(new wxPyOORClientData(target
));
964 wxString
msg(wxT("wxPython class not found for "));
965 msg
+= source
->GetClassInfo()->GetClassName();
966 PyErr_SetString(PyExc_NameError
, msg
.mbc_str());
970 } else { // source was NULL so return None.
971 Py_INCREF(Py_None
); target
= Py_None
;
977 PyObject
* wxPyMake_wxSizer(wxSizer
* source
) {
978 PyObject
* target
= NULL
;
980 if (source
&& wxIsKindOf(source
, wxSizer
)) {
981 // If it's derived from wxSizer then there may already be a pointer to
982 // a Python object that we can use in the OOR data.
983 wxSizer
* sz
= (wxSizer
*)source
;
984 wxPyOORClientData
* data
= (wxPyOORClientData
*)sz
->GetClientObject();
986 target
= data
->m_obj
;
992 target
= wxPyMake_wxObject(source
, False
);
993 if (target
!= Py_None
)
994 ((wxSizer
*)source
)->SetClientObject(new wxPyOORClientData(target
));
1000 //---------------------------------------------------------------------------
1003 #ifdef WXP_WITH_THREAD
1005 unsigned long wxPyGetCurrentThreadId() {
1006 return wxThread::GetCurrentId();
1009 static wxPyThreadState gs_shutdownTState
;
1012 wxPyThreadState
* wxPyGetThreadState() {
1013 if (wxPyTMutex
== NULL
) // Python is shutting down...
1014 return &gs_shutdownTState
;
1016 unsigned long ctid
= wxPyGetCurrentThreadId();
1017 wxPyThreadState
* tstate
= NULL
;
1020 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
1021 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
1022 if (info
.tid
== ctid
) {
1027 wxPyTMutex
->Unlock();
1028 wxASSERT_MSG(tstate
, wxT("PyThreadState should not be NULL!"));
1034 void wxPySaveThreadState(PyThreadState
* tstate
) {
1035 if (wxPyTMutex
== NULL
) { // Python is shutting down, assume a single thread...
1036 gs_shutdownTState
.tstate
= tstate
;
1039 unsigned long ctid
= wxPyGetCurrentThreadId();
1041 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
1042 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
1043 if (info
.tid
== ctid
) {
1045 if (info
.tstate
!= tstate
)
1046 wxLogMessage("*** tstate mismatch!???");
1048 // info.tstate = tstate; *** DO NOT update existing ones???
1049 // Normally it will never change, but apparently COM callbacks
1050 // (i.e. ActiveX controls) will (incorrectly IMHO) use a transient
1051 // tstate which will then be garbage the next time we try to use
1053 wxPyTMutex
->Unlock();
1057 // not found, so add it...
1058 wxPyTStates
->Add(new wxPyThreadState(ctid
, tstate
));
1059 wxPyTMutex
->Unlock();
1066 // Calls from Python to wxWindows code are wrapped in calls to these
1069 PyThreadState
* wxPyBeginAllowThreads() {
1070 #ifdef WXP_WITH_THREAD
1071 PyThreadState
* saved
= PyEval_SaveThread(); // Py_BEGIN_ALLOW_THREADS;
1072 wxPySaveThreadState(saved
);
1079 void wxPyEndAllowThreads(PyThreadState
* saved
) {
1080 #ifdef WXP_WITH_THREAD
1081 PyEval_RestoreThread(saved
); // Py_END_ALLOW_THREADS;
1087 // Calls from wxWindows back to Python code, or even any PyObject
1088 // manipulations, PyDECREF's and etc. are wrapped in calls to these functions:
1090 bool wxPyBeginBlockThreads() {
1091 #ifdef WXP_WITH_THREAD
1092 // This works in for 2.3, maybe a good alternative to find the needed tstate?
1093 // PyThreadState *check = PyGILState_GetThisThreadState();
1095 PyThreadState
*current
= _PyThreadState_Current
;
1097 // Only block if there wasn't already a tstate, or if the current one is
1098 // not the one we are wanting to change to. This should prevent deadlock
1099 // if there are nested calls to wxPyBeginBlockThreads
1100 bool blocked
= false;
1101 wxPyThreadState
* tstate
= wxPyGetThreadState();
1102 if (current
!= tstate
->tstate
) {
1103 PyEval_RestoreThread(tstate
->tstate
);
1111 void wxPyEndBlockThreads(bool blocked
) {
1112 #ifdef WXP_WITH_THREAD
1113 // Only unblock if we blocked in the last call to wxPyBeginBlockThreads.
1114 // The value of blocked passed in needs to be the same as that returned
1115 // from wxPyBeginBlockThreads at the same nesting level.
1117 PyEval_SaveThread();
1123 //---------------------------------------------------------------------------
1124 // wxPyInputStream and wxPyCBInputStream methods
1127 void wxPyInputStream::close() {
1128 /* do nothing for now */
1131 void wxPyInputStream::flush() {
1132 /* do nothing for now */
1135 bool wxPyInputStream::eof() {
1137 return m_wxis
->Eof();
1142 wxPyInputStream::~wxPyInputStream() {
1149 PyObject
* wxPyInputStream::read(int size
) {
1150 PyObject
* obj
= NULL
;
1152 const int BUFSIZE
= 1024;
1154 // check if we have a real wxInputStream to work with
1156 bool blocked
= wxPyBeginBlockThreads();
1157 PyErr_SetString(PyExc_IOError
, "no valid C-wxInputStream");
1158 wxPyEndBlockThreads(blocked
);
1163 // read while bytes are available on the stream
1164 while ( m_wxis
->CanRead() ) {
1165 m_wxis
->Read(buf
.GetAppendBuf(BUFSIZE
), BUFSIZE
);
1166 buf
.UngetAppendBuf(m_wxis
->LastRead());
1169 } else { // Read only size number of characters
1170 m_wxis
->Read(buf
.GetWriteBuf(size
), size
);
1171 buf
.UngetWriteBuf(m_wxis
->LastRead());
1175 bool blocked
= wxPyBeginBlockThreads();
1176 wxStreamError err
= m_wxis
->GetLastError();
1177 if (err
!= wxSTREAM_NO_ERROR
&& err
!= wxSTREAM_EOF
) {
1178 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
1181 // We use only strings for the streams, not unicode
1182 obj
= PyString_FromStringAndSize(buf
, buf
.GetDataLen());
1184 wxPyEndBlockThreads(blocked
);
1189 PyObject
* wxPyInputStream::readline(int size
) {
1190 PyObject
* obj
= NULL
;
1195 // check if we have a real wxInputStream to work with
1197 bool blocked
= wxPyBeginBlockThreads();
1198 PyErr_SetString(PyExc_IOError
,"no valid C-wxInputStream");
1199 wxPyEndBlockThreads(blocked
);
1203 // read until \n or byte limit reached
1204 for (i
=ch
=0; (ch
!= '\n') && (m_wxis
->CanRead()) && ((size
< 0) || (i
< size
)); i
++) {
1205 ch
= m_wxis
->GetC();
1210 bool blocked
= wxPyBeginBlockThreads();
1211 wxStreamError err
= m_wxis
->GetLastError();
1212 if (err
!= wxSTREAM_NO_ERROR
&& err
!= wxSTREAM_EOF
) {
1213 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
1216 // We use only strings for the streams, not unicode
1217 obj
= PyString_FromStringAndSize((char*)buf
.GetData(), buf
.GetDataLen());
1219 wxPyEndBlockThreads(blocked
);
1224 PyObject
* wxPyInputStream::readlines(int sizehint
) {
1227 // check if we have a real wxInputStream to work with
1229 bool blocked
= wxPyBeginBlockThreads();
1230 PyErr_SetString(PyExc_IOError
,"no valid C-wxInputStream");
1231 wxPyEndBlockThreads(blocked
);
1236 bool blocked
= wxPyBeginBlockThreads();
1237 pylist
= PyList_New(0);
1238 wxPyEndBlockThreads(blocked
);
1241 bool blocked
= wxPyBeginBlockThreads();
1243 wxPyEndBlockThreads(blocked
);
1247 // read sizehint bytes or until EOF
1249 for (i
=0; (m_wxis
->CanRead()) && ((sizehint
< 0) || (i
< sizehint
));) {
1250 PyObject
* s
= this->readline();
1252 bool blocked
= wxPyBeginBlockThreads();
1254 wxPyEndBlockThreads(blocked
);
1257 bool blocked
= wxPyBeginBlockThreads();
1258 PyList_Append(pylist
, s
);
1259 i
+= PyString_Size(s
);
1260 wxPyEndBlockThreads(blocked
);
1264 wxStreamError err
= m_wxis
->GetLastError();
1265 if (err
!= wxSTREAM_NO_ERROR
&& err
!= wxSTREAM_EOF
) {
1266 bool blocked
= wxPyBeginBlockThreads();
1268 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
1269 wxPyEndBlockThreads(blocked
);
1277 void wxPyInputStream::seek(int offset
, int whence
) {
1279 m_wxis
->SeekI(offset
, wxSeekMode(whence
));
1282 int wxPyInputStream::tell(){
1284 return m_wxis
->TellI();
1291 wxPyCBInputStream::wxPyCBInputStream(PyObject
*r
, PyObject
*s
, PyObject
*t
, bool block
)
1292 : wxInputStream(), m_read(r
), m_seek(s
), m_tell(t
), m_block(block
)
1296 wxPyCBInputStream::~wxPyCBInputStream() {
1298 if (m_block
) blocked
= wxPyBeginBlockThreads();
1302 if (m_block
) wxPyEndBlockThreads(blocked
);
1306 wxPyCBInputStream
* wxPyCBInputStream::create(PyObject
*py
, bool block
) {
1308 if (block
) blocked
= wxPyBeginBlockThreads();
1310 PyObject
* read
= getMethod(py
, "read");
1311 PyObject
* seek
= getMethod(py
, "seek");
1312 PyObject
* tell
= getMethod(py
, "tell");
1315 PyErr_SetString(PyExc_TypeError
, "Not a file-like object");
1319 if (block
) wxPyEndBlockThreads(blocked
);
1323 if (block
) wxPyEndBlockThreads(blocked
);
1324 return new wxPyCBInputStream(read
, seek
, tell
, block
);
1328 wxPyCBInputStream
* wxPyCBInputStream_create(PyObject
*py
, bool block
) {
1329 return wxPyCBInputStream::create(py
, block
);
1332 PyObject
* wxPyCBInputStream::getMethod(PyObject
* py
, char* name
) {
1333 if (!PyObject_HasAttrString(py
, name
))
1335 PyObject
* o
= PyObject_GetAttrString(py
, name
);
1336 if (!PyMethod_Check(o
) && !PyCFunction_Check(o
)) {
1344 size_t wxPyCBInputStream::GetSize() const {
1345 wxPyCBInputStream
* self
= (wxPyCBInputStream
*)this; // cast off const
1346 if (m_seek
&& m_tell
) {
1347 off_t temp
= self
->OnSysTell();
1348 off_t ret
= self
->OnSysSeek(0, wxFromEnd
);
1349 self
->OnSysSeek(temp
, wxFromStart
);
1357 size_t wxPyCBInputStream::OnSysRead(void *buffer
, size_t bufsize
) {
1361 bool blocked
= wxPyBeginBlockThreads();
1362 PyObject
* arglist
= Py_BuildValue("(i)", bufsize
);
1363 PyObject
* result
= PyEval_CallObject(m_read
, arglist
);
1367 if ((result
!= NULL
) && PyString_Check(result
)) {
1368 o
= PyString_Size(result
);
1370 m_lasterror
= wxSTREAM_EOF
;
1373 memcpy((char*)buffer
, PyString_AsString(result
), o
); // strings only, not unicode...
1378 m_lasterror
= wxSTREAM_READ_ERROR
;
1379 wxPyEndBlockThreads(blocked
);
1383 size_t wxPyCBInputStream::OnSysWrite(const void *buffer
, size_t bufsize
) {
1384 m_lasterror
= wxSTREAM_WRITE_ERROR
;
1388 off_t
wxPyCBInputStream::OnSysSeek(off_t off
, wxSeekMode mode
) {
1389 bool blocked
= wxPyBeginBlockThreads();
1391 // off_t is a 64-bit value...
1392 PyObject
* arglist
= Py_BuildValue("(Li)", off
, mode
);
1394 PyObject
* arglist
= Py_BuildValue("(ii)", off
, mode
);
1396 PyObject
* result
= PyEval_CallObject(m_seek
, arglist
);
1399 wxPyEndBlockThreads(blocked
);
1404 off_t
wxPyCBInputStream::OnSysTell() const {
1405 bool blocked
= wxPyBeginBlockThreads();
1406 PyObject
* arglist
= Py_BuildValue("()");
1407 PyObject
* result
= PyEval_CallObject(m_tell
, arglist
);
1410 if (result
!= NULL
) {
1412 if (PyLong_Check(result
))
1413 o
= PyLong_AsLongLong(result
);
1416 o
= PyInt_AsLong(result
);
1419 wxPyEndBlockThreads(blocked
);
1423 //----------------------------------------------------------------------
1425 IMPLEMENT_ABSTRACT_CLASS(wxPyCallback
, wxObject
);
1427 wxPyCallback::wxPyCallback(PyObject
* func
) {
1432 wxPyCallback::wxPyCallback(const wxPyCallback
& other
) {
1433 m_func
= other
.m_func
;
1437 wxPyCallback::~wxPyCallback() {
1438 bool blocked
= wxPyBeginBlockThreads();
1440 wxPyEndBlockThreads(blocked
);
1444 #define wxPy_PRECALLINIT "_preCallInit"
1445 #define wxPy_POSTCALLCLEANUP "_postCallCleanup"
1447 // This function is used for all events destined for Python event handlers.
1448 void wxPyCallback::EventThunker(wxEvent
& event
) {
1449 wxPyCallback
* cb
= (wxPyCallback
*)event
.m_callbackUserData
;
1450 PyObject
* func
= cb
->m_func
;
1454 bool checkSkip
= False
;
1456 bool blocked
= wxPyBeginBlockThreads();
1457 wxString className
= event
.GetClassInfo()->GetClassName();
1459 // If the event is one of these types then pass the original
1460 // event object instead of the one passed to us.
1461 if ( className
== wxT("wxPyEvent") ) {
1462 arg
= ((wxPyEvent
*)&event
)->GetSelf();
1463 checkSkip
= ((wxPyEvent
*)&event
)->GetCloned();
1465 else if ( className
== wxT("wxPyCommandEvent") ) {
1466 arg
= ((wxPyCommandEvent
*)&event
)->GetSelf();
1467 checkSkip
= ((wxPyCommandEvent
*)&event
)->GetCloned();
1470 arg
= wxPyConstructObject((void*)&event
, className
);
1476 // "intern" the pre/post method names to speed up the HasAttr
1477 static PyObject
* s_preName
= NULL
;
1478 static PyObject
* s_postName
= NULL
;
1479 if (s_preName
== NULL
) {
1480 s_preName
= PyString_FromString(wxPy_PRECALLINIT
);
1481 s_postName
= PyString_FromString(wxPy_POSTCALLCLEANUP
);
1484 // Check if the event object needs some preinitialization
1485 if (PyObject_HasAttr(arg
, s_preName
)) {
1486 result
= PyObject_CallMethodObjArgs(arg
, s_preName
, arg
, NULL
);
1488 Py_DECREF(result
); // result is ignored, but we still need to decref it
1489 PyErr_Clear(); // Just in case...
1495 // Call the event handler, passing the event object
1496 tuple
= PyTuple_New(1);
1497 PyTuple_SET_ITEM(tuple
, 0, arg
); // steals ref to arg
1498 result
= PyEval_CallObject(func
, tuple
);
1500 Py_DECREF(result
); // result is ignored, but we still need to decref it
1501 PyErr_Clear(); // Just in case...
1506 // Check if the event object needs some post cleanup
1507 if (PyObject_HasAttr(arg
, s_postName
)) {
1508 result
= PyObject_CallMethodObjArgs(arg
, s_postName
, arg
, NULL
);
1510 Py_DECREF(result
); // result is ignored, but we still need to decref it
1511 PyErr_Clear(); // Just in case...
1518 // if the event object was one of our special types and
1519 // it had been cloned, then we need to extract the Skipped
1520 // value from the original and set it in the clone.
1521 result
= PyObject_CallMethod(arg
, "GetSkipped", "");
1523 event
.Skip(PyInt_AsLong(result
));
1531 wxPyEndBlockThreads(blocked
);
1535 //----------------------------------------------------------------------
1537 wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper
& other
) {
1539 m_self
= other
.m_self
;
1540 m_class
= other
.m_class
;
1548 void wxPyCallbackHelper::setSelf(PyObject
* self
, PyObject
* klass
, int incref
) {
1559 #if PYTHON_API_VERSION >= 1011
1561 // Prior to Python 2.2 PyMethod_GetClass returned the class object
1562 // in which the method was defined. Starting with 2.2 it returns
1563 // "class that asked for the method" which seems totally bogus to me
1564 // but apprently it fixes some obscure problem waiting to happen in
1565 // Python. Since the API was not documented Guido and the gang felt
1566 // safe in changing it. Needless to say that totally screwed up the
1567 // logic below in wxPyCallbackHelper::findCallback, hence this icky
1568 // code to find the class where the method is actually defined...
1571 PyObject
* PyFindClassWithAttr(PyObject
*klass
, PyObject
*name
)
1575 if (PyType_Check(klass
)) { // new style classes
1576 // This code is borrowed/adapted from _PyType_Lookup in typeobject.c
1577 PyTypeObject
* type
= (PyTypeObject
*)klass
;
1578 PyObject
*mro
, *res
, *base
, *dict
;
1579 /* Look in tp_dict of types in MRO */
1581 assert(PyTuple_Check(mro
));
1582 n
= PyTuple_GET_SIZE(mro
);
1583 for (i
= 0; i
< n
; i
++) {
1584 base
= PyTuple_GET_ITEM(mro
, i
);
1585 if (PyClass_Check(base
))
1586 dict
= ((PyClassObject
*)base
)->cl_dict
;
1588 assert(PyType_Check(base
));
1589 dict
= ((PyTypeObject
*)base
)->tp_dict
;
1591 assert(dict
&& PyDict_Check(dict
));
1592 res
= PyDict_GetItem(dict
, name
);
1599 else if (PyClass_Check(klass
)) { // old style classes
1600 // This code is borrowed/adapted from class_lookup in classobject.c
1601 PyClassObject
* cp
= (PyClassObject
*)klass
;
1602 PyObject
*value
= PyDict_GetItem(cp
->cl_dict
, name
);
1603 if (value
!= NULL
) {
1604 return (PyObject
*)cp
;
1606 n
= PyTuple_Size(cp
->cl_bases
);
1607 for (i
= 0; i
< n
; i
++) {
1608 PyObject
* base
= PyTuple_GetItem(cp
->cl_bases
, i
);
1609 PyObject
*v
= PyFindClassWithAttr(base
, name
);
1621 PyObject
* PyMethod_GetDefiningClass(PyObject
* method
, const char* name
)
1623 PyObject
* mgc
= PyMethod_GET_CLASS(method
);
1625 #if PYTHON_API_VERSION <= 1010 // prior to Python 2.2, the easy way
1627 #else // 2.2 and after, the hard way...
1629 PyObject
* nameo
= PyString_FromString(name
);
1630 PyObject
* klass
= PyFindClassWithAttr(mgc
, nameo
);
1638 bool wxPyCallbackHelper::findCallback(const char* name
) const {
1639 wxPyCallbackHelper
* self
= (wxPyCallbackHelper
*)this; // cast away const
1640 self
->m_lastFound
= NULL
;
1642 // If the object (m_self) has an attibute of the given name...
1643 if (m_self
&& PyObject_HasAttrString(m_self
, (char*)name
)) {
1644 PyObject
*method
, *klass
;
1645 method
= PyObject_GetAttrString(m_self
, (char*)name
);
1647 // ...and if that attribute is a method, and if that method's class is
1648 // not from a base class...
1649 if (PyMethod_Check(method
) &&
1650 (klass
= PyMethod_GetDefiningClass(method
, (char*)name
)) != NULL
&&
1651 ((klass
== m_class
) || PyObject_IsSubclass(klass
, m_class
))) {
1653 // ...then we'll save a pointer to the method so callCallback can call it.
1654 self
->m_lastFound
= method
;
1660 return m_lastFound
!= NULL
;
1664 int wxPyCallbackHelper::callCallback(PyObject
* argTuple
) const {
1668 result
= callCallbackObj(argTuple
);
1669 if (result
) { // Assumes an integer return type...
1670 retval
= PyInt_AsLong(result
);
1672 PyErr_Clear(); // forget about it if it's not...
1677 // Invoke the Python callable object, returning the raw PyObject return
1678 // value. Caller should DECREF the return value and also manage the GIL.
1679 PyObject
* wxPyCallbackHelper::callCallbackObj(PyObject
* argTuple
) const {
1682 // Save a copy of the pointer in case the callback generates another
1683 // callback. In that case m_lastFound will have a different value when
1684 // it gets back here...
1685 PyObject
* method
= m_lastFound
;
1687 result
= PyEval_CallObject(method
, argTuple
);
1688 Py_DECREF(argTuple
);
1697 void wxPyCBH_setCallbackInfo(wxPyCallbackHelper
& cbh
, PyObject
* self
, PyObject
* klass
, int incref
) {
1698 cbh
.setSelf(self
, klass
, incref
);
1701 bool wxPyCBH_findCallback(const wxPyCallbackHelper
& cbh
, const char* name
) {
1702 return cbh
.findCallback(name
);
1705 int wxPyCBH_callCallback(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
1706 return cbh
.callCallback(argTuple
);
1709 PyObject
* wxPyCBH_callCallbackObj(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
1710 return cbh
.callCallbackObj(argTuple
);
1714 void wxPyCBH_delete(wxPyCallbackHelper
* cbh
) {
1715 if (cbh
->m_incRef
) {
1716 bool blocked
= wxPyBeginBlockThreads();
1717 Py_XDECREF(cbh
->m_self
);
1718 Py_XDECREF(cbh
->m_class
);
1719 wxPyEndBlockThreads(blocked
);
1723 //---------------------------------------------------------------------------
1724 //---------------------------------------------------------------------------
1725 // These event classes can be derived from in Python and passed through the event
1726 // system without losing anything. They do this by keeping a reference to
1727 // themselves and some special case handling in wxPyCallback::EventThunker.
1730 wxPyEvtSelfRef::wxPyEvtSelfRef() {
1731 //m_self = Py_None; // **** We don't do normal ref counting to prevent
1732 //Py_INCREF(m_self); // circular loops...
1736 wxPyEvtSelfRef::~wxPyEvtSelfRef() {
1737 bool blocked
= wxPyBeginBlockThreads();
1740 wxPyEndBlockThreads(blocked
);
1743 void wxPyEvtSelfRef::SetSelf(PyObject
* self
, bool clone
) {
1744 bool blocked
= wxPyBeginBlockThreads();
1752 wxPyEndBlockThreads(blocked
);
1755 PyObject
* wxPyEvtSelfRef::GetSelf() const {
1761 IMPLEMENT_ABSTRACT_CLASS(wxPyEvent
, wxEvent
);
1762 IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent
, wxCommandEvent
);
1765 wxPyEvent::wxPyEvent(int winid
, wxEventType commandType
)
1766 : wxEvent(winid
, commandType
) {
1770 wxPyEvent::wxPyEvent(const wxPyEvent
& evt
)
1773 SetSelf(evt
.m_self
, True
);
1777 wxPyEvent::~wxPyEvent() {
1781 wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType
, int id
)
1782 : wxCommandEvent(commandType
, id
) {
1786 wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent
& evt
)
1787 : wxCommandEvent(evt
)
1789 SetSelf(evt
.m_self
, True
);
1793 wxPyCommandEvent::~wxPyCommandEvent() {
1800 //---------------------------------------------------------------------------
1801 //---------------------------------------------------------------------------
1802 // Convert a wxList to a Python List, only works for lists of wxObjects
1804 PyObject
* wxPy_ConvertList(wxListBase
* listbase
) {
1805 wxList
* list
= (wxList
*)listbase
; // this is probably bad...
1809 wxNode
* node
= list
->GetFirst();
1811 bool blocked
= wxPyBeginBlockThreads();
1812 pyList
= PyList_New(0);
1814 wxObj
= node
->GetData();
1815 pyObj
= wxPyMake_wxObject(wxObj
);
1816 PyList_Append(pyList
, pyObj
);
1817 node
= node
->GetNext();
1819 wxPyEndBlockThreads(blocked
);
1823 //----------------------------------------------------------------------
1825 long wxPyGetWinHandle(wxWindow
* win
) {
1828 return (long)win
->GetHandle();
1831 #if defined(__WXGTK__) || defined(__WXX11)
1832 return (long)GetXWindow(win
);
1836 //return (long)MAC_WXHWND(win->MacGetTopLevelWindowRef());
1837 return (long)win
->GetHandle();
1843 //----------------------------------------------------------------------
1844 // Some helper functions for typemaps in my_typemaps.i, so they won't be
1845 // included in every file over and over again...
1847 wxString
* wxString_in_helper(PyObject
* source
) {
1848 wxString
* target
= NULL
;
1850 if (!PyString_Check(source
) && !PyUnicode_Check(source
)) {
1851 PyErr_SetString(PyExc_TypeError
, "String or Unicode type required");
1855 PyObject
* uni
= source
;
1856 if (PyString_Check(source
)) {
1857 uni
= PyUnicode_FromObject(source
);
1858 if (PyErr_Occurred()) return NULL
;
1860 target
= new wxString();
1861 size_t len
= PyUnicode_GET_SIZE(uni
);
1863 PyUnicode_AsWideChar((PyUnicodeObject
*)uni
, target
->GetWriteBuf(len
), len
);
1864 target
->UngetWriteBuf();
1867 if (PyString_Check(source
))
1870 char* tmpPtr
; int tmpSize
;
1871 if (PyString_AsStringAndSize(source
, &tmpPtr
, &tmpSize
) == -1) {
1872 PyErr_SetString(PyExc_TypeError
, "Unable to convert string");
1875 target
= new wxString(tmpPtr
, tmpSize
);
1876 #endif // wxUSE_UNICODE
1882 // Similar to above except doesn't use "new" and doesn't set an exception
1883 wxString
Py2wxString(PyObject
* source
)
1888 // Convert to a unicode object, if not already, then to a wxString
1889 PyObject
* uni
= source
;
1890 if (!PyUnicode_Check(source
)) {
1891 uni
= PyUnicode_FromObject(source
);
1892 if (PyErr_Occurred()) return wxEmptyString
; // TODO: should we PyErr_Clear?
1894 size_t len
= PyUnicode_GET_SIZE(uni
);
1896 PyUnicode_AsWideChar((PyUnicodeObject
*)uni
, target
.GetWriteBuf(len
), len
);
1897 target
.UngetWriteBuf();
1900 if (!PyUnicode_Check(source
))
1903 // Convert to a string object if it isn't already, then to wxString
1904 PyObject
* str
= source
;
1905 if (!PyString_Check(source
)) {
1906 str
= PyObject_Str(source
);
1907 if (PyErr_Occurred()) return wxEmptyString
; // TODO: should we PyErr_Clear?
1909 char* tmpPtr
; int tmpSize
;
1910 PyString_AsStringAndSize(str
, &tmpPtr
, &tmpSize
);
1911 target
= wxString(tmpPtr
, tmpSize
);
1913 if (!PyString_Check(source
))
1915 #endif // wxUSE_UNICODE
1921 // Make either a Python String or Unicode object, depending on build mode
1922 PyObject
* wx2PyString(const wxString
& src
)
1926 str
= PyUnicode_FromWideChar(src
.c_str(), src
.Len());
1928 str
= PyString_FromStringAndSize(src
.c_str(), src
.Len());
1934 //----------------------------------------------------------------------
1937 byte
* byte_LIST_helper(PyObject
* source
) {
1938 if (!PyList_Check(source
)) {
1939 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1942 int count
= PyList_Size(source
);
1943 byte
* temp
= new byte
[count
];
1945 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1948 for (int x
=0; x
<count
; x
++) {
1949 PyObject
* o
= PyList_GetItem(source
, x
);
1950 if (! PyInt_Check(o
)) {
1951 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1954 temp
[x
] = (byte
)PyInt_AsLong(o
);
1960 int* int_LIST_helper(PyObject
* source
) {
1961 if (!PyList_Check(source
)) {
1962 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1965 int count
= PyList_Size(source
);
1966 int* temp
= new int[count
];
1968 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1971 for (int x
=0; x
<count
; x
++) {
1972 PyObject
* o
= PyList_GetItem(source
, x
);
1973 if (! PyInt_Check(o
)) {
1974 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1977 temp
[x
] = PyInt_AsLong(o
);
1983 long* long_LIST_helper(PyObject
* source
) {
1984 if (!PyList_Check(source
)) {
1985 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1988 int count
= PyList_Size(source
);
1989 long* temp
= new long[count
];
1991 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1994 for (int x
=0; x
<count
; x
++) {
1995 PyObject
* o
= PyList_GetItem(source
, x
);
1996 if (! PyInt_Check(o
)) {
1997 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
2000 temp
[x
] = PyInt_AsLong(o
);
2006 char** string_LIST_helper(PyObject
* source
) {
2007 if (!PyList_Check(source
)) {
2008 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
2011 int count
= PyList_Size(source
);
2012 char** temp
= new char*[count
];
2014 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
2017 for (int x
=0; x
<count
; x
++) {
2018 PyObject
* o
= PyList_GetItem(source
, x
);
2019 if (! PyString_Check(o
)) {
2020 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
2023 temp
[x
] = PyString_AsString(o
);
2028 //--------------------------------
2029 // Part of patch from Tim Hochberg
2030 static inline bool wxPointFromObjects(PyObject
* o1
, PyObject
* o2
, wxPoint
* point
) {
2031 if (PyInt_Check(o1
) && PyInt_Check(o2
)) {
2032 point
->x
= PyInt_AS_LONG(o1
);
2033 point
->y
= PyInt_AS_LONG(o2
);
2036 if (PyFloat_Check(o1
) && PyFloat_Check(o2
)) {
2037 point
->x
= (int)PyFloat_AS_DOUBLE(o1
);
2038 point
->y
= (int)PyFloat_AS_DOUBLE(o2
);
2041 if (wxPySwigInstance_Check(o1
) || wxPySwigInstance_Check(o2
)) { // TODO: Why???
2042 // Disallow instances because they can cause havok
2045 if (PyNumber_Check(o1
) && PyNumber_Check(o2
)) {
2046 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
2047 point
->x
= PyInt_AsLong(o1
);
2048 point
->y
= PyInt_AsLong(o2
);
2055 wxPoint
* wxPoint_LIST_helper(PyObject
* source
, int *count
) {
2056 // Putting all of the declarations here allows
2057 // us to put the error handling all in one place.
2060 PyObject
*o
, *o1
, *o2
;
2061 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
2063 if (!PySequence_Check(source
)) {
2067 // The length of the sequence is returned in count.
2068 *count
= PySequence_Length(source
);
2073 temp
= new wxPoint
[*count
];
2075 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
2078 for (x
=0; x
<*count
; x
++) {
2079 // Get an item: try fast way first.
2081 o
= PySequence_Fast_GET_ITEM(source
, x
);
2084 o
= PySequence_GetItem(source
, x
);
2090 // Convert o to wxPoint.
2091 if ((PyTuple_Check(o
) && PyTuple_GET_SIZE(o
) == 2) ||
2092 (PyList_Check(o
) && PyList_GET_SIZE(o
) == 2)) {
2093 o1
= PySequence_Fast_GET_ITEM(o
, 0);
2094 o2
= PySequence_Fast_GET_ITEM(o
, 1);
2095 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
2099 else if (wxPySwigInstance_Check(o
)) {
2101 if (! wxPyConvertSwigPtr(o
, (void **)&pt
, wxT("wxPoint"))) {
2106 else if (PySequence_Check(o
) && PySequence_Length(o
) == 2) {
2107 o1
= PySequence_GetItem(o
, 0);
2108 o2
= PySequence_GetItem(o
, 1);
2109 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
2133 PyErr_SetString(PyExc_TypeError
, "Expected a sequence of length-2 sequences or wxPoints.");
2137 //------------------------------
2140 wxBitmap
** wxBitmap_LIST_helper(PyObject
* source
) {
2141 if (!PyList_Check(source
)) {
2142 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
2145 int count
= PyList_Size(source
);
2146 wxBitmap
** temp
= new wxBitmap
*[count
];
2148 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
2151 for (int x
=0; x
<count
; x
++) {
2152 PyObject
* o
= PyList_GetItem(source
, x
);
2153 if (wxPySwigInstance_Check(o
)) {
2155 if (! wxPyConvertSwigPtr(o
, (void **) &pt
, wxT("wxBitmap"))) {
2156 PyErr_SetString(PyExc_TypeError
,"Expected wxBitmap.");
2162 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxBitmaps.");
2171 wxString
* wxString_LIST_helper(PyObject
* source
) {
2172 if (!PyList_Check(source
)) {
2173 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
2176 int count
= PyList_Size(source
);
2177 wxString
* temp
= new wxString
[count
];
2179 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
2182 for (int x
=0; x
<count
; x
++) {
2183 PyObject
* o
= PyList_GetItem(source
, x
);
2184 #if PYTHON_API_VERSION >= 1009
2185 if (! PyString_Check(o
) && ! PyUnicode_Check(o
)) {
2186 PyErr_SetString(PyExc_TypeError
, "Expected a list of string or unicode objects.");
2190 if (! PyString_Check(o
)) {
2191 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
2196 wxString
* pStr
= wxString_in_helper(o
);
2204 wxAcceleratorEntry
* wxAcceleratorEntry_LIST_helper(PyObject
* source
) {
2205 if (!PyList_Check(source
)) {
2206 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
2209 int count
= PyList_Size(source
);
2210 wxAcceleratorEntry
* temp
= new wxAcceleratorEntry
[count
];
2212 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
2215 for (int x
=0; x
<count
; x
++) {
2216 PyObject
* o
= PyList_GetItem(source
, x
);
2217 if (wxPySwigInstance_Check(o
)) {
2218 wxAcceleratorEntry
* ae
;
2219 if (! wxPyConvertSwigPtr(o
, (void **) &ae
, wxT("wxAcceleratorEntry"))) {
2220 PyErr_SetString(PyExc_TypeError
,"Expected wxAcceleratorEntry.");
2225 else if (PyTuple_Check(o
)) {
2226 PyObject
* o1
= PyTuple_GetItem(o
, 0);
2227 PyObject
* o2
= PyTuple_GetItem(o
, 1);
2228 PyObject
* o3
= PyTuple_GetItem(o
, 2);
2229 temp
[x
].Set(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
));
2232 PyErr_SetString(PyExc_TypeError
, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
2240 wxPen
** wxPen_LIST_helper(PyObject
* source
) {
2241 if (!PyList_Check(source
)) {
2242 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
2245 int count
= PyList_Size(source
);
2246 wxPen
** temp
= new wxPen
*[count
];
2248 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
2251 for (int x
=0; x
<count
; x
++) {
2252 PyObject
* o
= PyList_GetItem(source
, x
);
2253 if (wxPySwigInstance_Check(o
)) {
2255 if (! wxPyConvertSwigPtr(o
, (void **)&pt
, wxT("wxPen"))) {
2257 PyErr_SetString(PyExc_TypeError
,"Expected wxPen.");
2264 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxPens.");
2272 bool wxPy2int_seq_helper(PyObject
* source
, int* i1
, int* i2
) {
2273 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
2276 if (!PySequence_Check(source
) || PySequence_Length(source
) != 2)
2280 o1
= PySequence_Fast_GET_ITEM(source
, 0);
2281 o2
= PySequence_Fast_GET_ITEM(source
, 1);
2284 o1
= PySequence_GetItem(source
, 0);
2285 o2
= PySequence_GetItem(source
, 1);
2288 *i1
= PyInt_AsLong(o1
);
2289 *i2
= PyInt_AsLong(o2
);
2299 bool wxPy4int_seq_helper(PyObject
* source
, int* i1
, int* i2
, int* i3
, int* i4
) {
2300 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
2301 PyObject
*o1
, *o2
, *o3
, *o4
;
2303 if (!PySequence_Check(source
) || PySequence_Length(source
) != 4)
2307 o1
= PySequence_Fast_GET_ITEM(source
, 0);
2308 o2
= PySequence_Fast_GET_ITEM(source
, 1);
2309 o3
= PySequence_Fast_GET_ITEM(source
, 2);
2310 o4
= PySequence_Fast_GET_ITEM(source
, 3);
2313 o1
= PySequence_GetItem(source
, 0);
2314 o2
= PySequence_GetItem(source
, 1);
2315 o3
= PySequence_GetItem(source
, 2);
2316 o4
= PySequence_GetItem(source
, 3);
2319 *i1
= PyInt_AsLong(o1
);
2320 *i2
= PyInt_AsLong(o2
);
2321 *i3
= PyInt_AsLong(o3
);
2322 *i4
= PyInt_AsLong(o4
);
2334 //----------------------------------------------------------------------
2336 bool wxPySimple_typecheck(PyObject
* source
, const wxChar
* classname
, int seqLen
)
2340 if (wxPySwigInstance_Check(source
) &&
2341 wxPyConvertSwigPtr(source
, (void **)&ptr
, classname
))
2345 if (PySequence_Check(source
) && PySequence_Length(source
) == seqLen
)
2351 bool wxSize_helper(PyObject
* source
, wxSize
** obj
)
2353 if (source
== Py_None
) {
2354 **obj
= wxSize(-1,-1);
2357 return wxPyTwoIntItem_helper(source
, obj
, wxT("wxSize"));
2361 bool wxPoint_helper(PyObject
* source
, wxPoint
** obj
)
2363 if (source
== Py_None
) {
2364 **obj
= wxPoint(-1,-1);
2367 return wxPyTwoIntItem_helper(source
, obj
, wxT("wxPoint"));
2372 bool wxRealPoint_helper(PyObject
* source
, wxRealPoint
** obj
) {
2374 if (source
== Py_None
) {
2375 **obj
= wxRealPoint(-1,-1);
2379 // If source is an object instance then it may already be the right type
2380 if (wxPySwigInstance_Check(source
)) {
2382 if (! wxPyConvertSwigPtr(source
, (void **)&ptr
, wxT("wxRealPoint")))
2387 // otherwise a 2-tuple of floats is expected
2388 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
2389 PyObject
* o1
= PySequence_GetItem(source
, 0);
2390 PyObject
* o2
= PySequence_GetItem(source
, 1);
2391 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
2396 **obj
= wxRealPoint(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
));
2403 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxRealPoint object.");
2409 bool wxRect_helper(PyObject
* source
, wxRect
** obj
) {
2411 if (source
== Py_None
) {
2412 **obj
= wxRect(-1,-1,-1,-1);
2416 // If source is an object instance then it may already be the right type
2417 if (wxPySwigInstance_Check(source
)) {
2419 if (! wxPyConvertSwigPtr(source
, (void **)&ptr
, wxT("wxRect")))
2424 // otherwise a 4-tuple of integers is expected
2425 else if (PySequence_Check(source
) && PyObject_Length(source
) == 4) {
2426 PyObject
* o1
= PySequence_GetItem(source
, 0);
2427 PyObject
* o2
= PySequence_GetItem(source
, 1);
2428 PyObject
* o3
= PySequence_GetItem(source
, 2);
2429 PyObject
* o4
= PySequence_GetItem(source
, 3);
2430 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
) ||
2431 !PyNumber_Check(o3
) || !PyNumber_Check(o4
)) {
2438 **obj
= wxRect(PyInt_AsLong(o1
), PyInt_AsLong(o2
),
2439 PyInt_AsLong(o3
), PyInt_AsLong(o4
));
2448 PyErr_SetString(PyExc_TypeError
, "Expected a 4-tuple of integers or a wxRect object.");
2454 bool wxColour_helper(PyObject
* source
, wxColour
** obj
) {
2456 if (source
== Py_None
) {
2457 **obj
= wxNullColour
;
2461 // If source is an object instance then it may already be the right type
2462 if (wxPySwigInstance_Check(source
)) {
2464 if (! wxPyConvertSwigPtr(source
, (void **)&ptr
, wxT("wxColour")))
2469 // otherwise check for a string
2470 else if (PyString_Check(source
) || PyUnicode_Check(source
)) {
2471 wxString spec
= Py2wxString(source
);
2472 if (spec
.GetChar(0) == '#' && spec
.Length() == 7) { // It's #RRGGBB
2473 long red
, green
, blue
;
2474 red
= green
= blue
= 0;
2475 spec
.Mid(1,2).ToLong(&red
, 16);
2476 spec
.Mid(3,2).ToLong(&green
, 16);
2477 spec
.Mid(5,2).ToLong(&blue
, 16);
2479 **obj
= wxColour(red
, green
, blue
);
2482 else { // it's a colour name
2483 **obj
= wxColour(spec
);
2487 // last chance: 3-tuple of integers is expected
2488 else if (PySequence_Check(source
) && PyObject_Length(source
) == 3) {
2489 PyObject
* o1
= PySequence_GetItem(source
, 0);
2490 PyObject
* o2
= PySequence_GetItem(source
, 1);
2491 PyObject
* o3
= PySequence_GetItem(source
, 2);
2492 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
) || !PyNumber_Check(o3
)) {
2498 **obj
= wxColour(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
));
2506 PyErr_SetString(PyExc_TypeError
,
2507 "Expected a wxColour object or a string containing a colour name or '#RRGGBB'.");
2512 bool wxColour_typecheck(PyObject
* source
) {
2514 if (wxPySimple_typecheck(source
, wxT("wxColour"), 3))
2517 if (PyString_Check(source
) || PyUnicode_Check(source
))
2525 bool wxPoint2D_helper(PyObject
* source
, wxPoint2D
** obj
) {
2527 if (source
== Py_None
) {
2528 **obj
= wxPoint2D(-1,-1);
2532 // If source is an object instance then it may already be the right type
2533 if (wxPySwigInstance_Check(source
)) {
2535 if (! wxPyConvertSwigPtr(source
, (void **)&ptr
, wxT("wxPoint2D")))
2540 // otherwise a length-2 sequence of floats is expected
2541 if (PySequence_Check(source
) && PySequence_Length(source
) == 2) {
2542 PyObject
* o1
= PySequence_GetItem(source
, 0);
2543 PyObject
* o2
= PySequence_GetItem(source
, 1);
2544 // This should really check for floats, not numbers -- but that would break code.
2545 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
2550 **obj
= wxPoint2D(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
));
2556 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxPoint2D object.");
2561 //----------------------------------------------------------------------
2563 PyObject
* wxArrayString2PyList_helper(const wxArrayString
& arr
) {
2565 PyObject
* list
= PyList_New(0);
2566 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
2568 PyObject
* str
= PyUnicode_FromWideChar(arr
[i
].c_str(), arr
[i
].Len());
2570 PyObject
* str
= PyString_FromStringAndSize(arr
[i
].c_str(), arr
[i
].Len());
2572 PyList_Append(list
, str
);
2579 PyObject
* wxArrayInt2PyList_helper(const wxArrayInt
& arr
) {
2581 PyObject
* list
= PyList_New(0);
2582 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
2583 PyObject
* number
= PyInt_FromLong(arr
[i
]);
2584 PyList_Append(list
, number
);
2591 //----------------------------------------------------------------------
2592 //----------------------------------------------------------------------