]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/src/helpers.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Helper functions/classes for the wxPython extension module
9 // Copyright: (c) 1998 by Total Control Software
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
13 #include <stdio.h> // get the correct definition of NULL
18 #include "pyistream.h"
21 #include <wx/msw/private.h>
22 #include <wx/msw/winundef.h>
23 #include <wx/msw/msvcrt.h>
28 #include <gdk/gdkprivate.h>
29 #include <wx/gtk/win_gtk.h>
33 //----------------------------------------------------------------------
36 int WXDLLEXPORT
wxEntryStart( int& argc
, char** argv
);
38 int WXDLLEXPORT
wxEntryStart( int argc
, char** argv
);
40 int WXDLLEXPORT
wxEntryInitGui();
41 void WXDLLEXPORT
wxEntryCleanup();
43 wxPyApp
* wxPythonApp
= NULL
; // Global instance of application object
46 #ifdef WXP_WITH_THREAD
47 struct wxPyThreadState
{
49 PyThreadState
* tstate
;
51 wxPyThreadState(unsigned long _tid
=0, PyThreadState
* _tstate
=NULL
)
52 : tid(_tid
), tstate(_tstate
) {}
55 #include <wx/dynarray.h>
56 WX_DECLARE_OBJARRAY(wxPyThreadState
, wxPyThreadStateArray
);
57 #include <wx/arrimpl.cpp>
58 WX_DEFINE_OBJARRAY(wxPyThreadStateArray
);
60 wxPyThreadStateArray
* wxPyTStates
= NULL
;
61 wxMutex
* wxPyTMutex
= NULL
;
65 #ifdef __WXMSW__ // If building for win32...
66 //----------------------------------------------------------------------
67 // This gets run when the DLL is loaded. We just need to save a handle.
68 //----------------------------------------------------------------------
71 HINSTANCE hinstDLL
, // handle to DLL module
72 DWORD fdwReason
, // reason for calling function
73 LPVOID lpvReserved
// reserved
76 wxSetInstance(hinstDLL
);
81 //----------------------------------------------------------------------
82 // Classes for implementing the wxp main application shell.
83 //----------------------------------------------------------------------
87 // printf("**** ctor\n");
91 // printf("**** dtor\n");
95 // This one isn't acutally called... See __wxStart()
96 bool wxPyApp::OnInit() {
101 int wxPyApp::MainLoop() {
104 DeletePendingObjects();
105 bool initialized
= wxTopLevelWindows
.GetCount() != 0;
107 m_initialized
= initialized
;
111 retval
= wxApp::MainLoop();
119 //---------------------------------------------------------------------
120 //----------------------------------------------------------------------
123 // This is where we pick up the first part of the wxEntry functionality...
124 // The rest is in __wxStart and __wxCleanup. This function is called when
125 // wxcmodule is imported. (Before there is a wxApp object.)
130 // wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
133 #ifdef WXP_WITH_THREAD
134 PyEval_InitThreads();
135 wxPyTStates
= new wxPyThreadStateArray
;
136 wxPyTMutex
= new wxMutex
;
139 // Bail out if there is already windows created. This means that the
140 // toolkit has already been initialized, as in embedding wxPython in
141 // a C++ wxWindows app.
142 if (wxTopLevelWindows
.Number() > 0)
148 PyObject
* sysargv
= PySys_GetObject("argv");
149 if (sysargv
!= NULL
) {
150 argc
= PyList_Size(sysargv
);
151 argv
= new char*[argc
+1];
153 for(x
=0; x
<argc
; x
++)
154 argv
[x
] = copystring(PyString_AsString(PyList_GetItem(sysargv
, x
)));
158 wxEntryStart(argc
, argv
);
164 // Start the user application, user App's OnInit method is a parameter here
165 PyObject
* __wxStart(PyObject
* /* self */, PyObject
* args
)
167 PyObject
* onInitFunc
= NULL
;
172 if (!PyArg_ParseTuple(args
, "O", &onInitFunc
))
175 #if 0 // Try it out without this check, see how it does...
176 if (wxTopLevelWindows
.Number() > 0) {
177 PyErr_SetString(PyExc_TypeError
, "Only 1 wxApp per process!");
182 // This is the next part of the wxEntry functionality...
185 PyObject
* sysargv
= PySys_GetObject("argv");
186 if (sysargv
!= NULL
) {
187 argc
= PyList_Size(sysargv
);
188 argv
= new char*[argc
+1];
190 for(x
=0; x
<argc
; x
++)
191 argv
[x
] = copystring(PyString_AsString(PyList_GetItem(sysargv
, x
)));
194 wxPythonApp
->argc
= argc
;
195 wxPythonApp
->argv
= argv
;
199 // Call the Python App's OnInit function
200 arglist
= PyTuple_New(0);
201 result
= PyEval_CallObject(onInitFunc
, arglist
);
202 if (!result
) { // an exception was raised.
206 if (! PyInt_Check(result
)) {
207 PyErr_SetString(PyExc_TypeError
, "OnInit should return a boolean value");
210 bResult
= PyInt_AS_LONG(result
);
212 PyErr_SetString(PyExc_SystemExit
, "OnInit returned FALSE, exiting...");
217 wxTheApp
->m_initialized
= (wxTopLevelWindows
.GetCount() > 0);
227 #ifdef WXP_WITH_THREAD
230 wxPyTStates
->Empty();
238 static PyObject
* wxPython_dict
= NULL
;
239 static PyObject
* wxPyPtrTypeMap
= NULL
;
241 PyObject
* __wxSetDictionary(PyObject
* /* self */, PyObject
* args
)
244 if (!PyArg_ParseTuple(args
, "O", &wxPython_dict
))
247 if (!PyDict_Check(wxPython_dict
)) {
248 PyErr_SetString(PyExc_TypeError
, "_wxSetDictionary must have dictionary object!");
252 if (! wxPyPtrTypeMap
)
253 wxPyPtrTypeMap
= PyDict_New();
254 PyDict_SetItemString(wxPython_dict
, "__wxPyPtrTypeMap", wxPyPtrTypeMap
);
258 #define wxPlatform "__WXMOTIF__"
261 #define wxPlatform "__WXX11__"
264 #define wxPlatform "__WXGTK__"
266 #if defined(__WIN32__) || defined(__WXMSW__)
267 #define wxPlatform "__WXMSW__"
270 #define wxPlatform "__WXMAC__"
273 PyDict_SetItemString(wxPython_dict
, "wxPlatform", PyString_FromString(wxPlatform
));
280 //---------------------------------------------------------------------------
281 // Stuff used by OOR to find the right wxPython class type to return and to
285 // The pointer type map is used when the "pointer" type name generated by SWIG
286 // is not the same as the shadow class name, for example wxPyTreeCtrl
287 // vs. wxTreeCtrl. It needs to be referenced in Python as well as from C++,
288 // so we'll just make it a Python dictionary in the wx module's namespace.
289 void wxPyPtrTypeMap_Add(const char* commonName
, const char* ptrName
) {
290 if (! wxPyPtrTypeMap
)
291 wxPyPtrTypeMap
= PyDict_New();
292 PyDict_SetItemString(wxPyPtrTypeMap
,
294 PyString_FromString((char*)ptrName
));
299 PyObject
* wxPyClassExists(const char* className
) {
304 char buff
[64]; // should always be big enough...
306 sprintf(buff
, "%sPtr", className
);
307 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
309 return classobj
; // returns NULL if not found
313 PyObject
* wxPyMake_wxObject(wxObject
* source
, bool checkEvtHandler
) {
314 PyObject
* target
= NULL
;
315 bool isEvtHandler
= FALSE
;
318 // If it's derived from wxEvtHandler then there may
319 // already be a pointer to a Python object that we can use
321 if (checkEvtHandler
&& wxIsKindOf(source
, wxEvtHandler
)) {
323 wxEvtHandler
* eh
= (wxEvtHandler
*)source
;
324 wxPyClientData
* data
= (wxPyClientData
*)eh
->GetClientObject();
326 target
= data
->m_obj
;
332 // Otherwise make it the old fashioned way by making a
333 // new shadow object and putting this pointer in it.
334 wxClassInfo
* info
= source
->GetClassInfo();
335 wxChar
* name
= (wxChar
*)info
->GetClassName();
336 PyObject
* klass
= wxPyClassExists(name
);
337 while (info
&& !klass
) {
338 name
= (wxChar
*)info
->GetBaseClassName1();
339 info
= wxClassInfo::FindClass(name
);
340 klass
= wxPyClassExists(name
);
343 target
= wxPyConstructObject(source
, name
, klass
, FALSE
);
344 if (target
&& isEvtHandler
)
345 ((wxEvtHandler
*)source
)->SetClientObject(new wxPyClientData(target
));
347 wxString
msg("wxPython class not found for ");
348 msg
+= source
->GetClassInfo()->GetClassName();
349 PyErr_SetString(PyExc_NameError
, msg
.c_str());
353 } else { // source was NULL so return None.
354 Py_INCREF(Py_None
); target
= Py_None
;
360 PyObject
* wxPyMake_wxSizer(wxSizer
* source
) {
361 PyObject
* target
= NULL
;
363 if (source
&& wxIsKindOf(source
, wxSizer
)) {
364 // If it's derived from wxSizer then there may
365 // already be a pointer to a Python object that we can use
367 wxSizer
* sz
= (wxSizer
*)source
;
368 wxPyClientData
* data
= (wxPyClientData
*)sz
->GetClientObject();
370 target
= data
->m_obj
;
375 target
= wxPyMake_wxObject(source
, FALSE
);
376 if (target
!= Py_None
)
377 ((wxSizer
*)source
)->SetClientObject(new wxPyClientData(target
));
384 //---------------------------------------------------------------------------
386 PyObject
* wxPyConstructObject(void* ptr
,
387 const char* className
,
394 char swigptr
[64]; // should always be big enough...
397 if ((item
= PyDict_GetItemString(wxPyPtrTypeMap
, (char*)className
)) != NULL
) {
398 className
= PyString_AsString(item
);
400 sprintf(buff
, "_%s_p", className
);
401 SWIG_MakePtr(swigptr
, ptr
, buff
);
403 arg
= Py_BuildValue("(s)", swigptr
);
404 obj
= PyInstance_New(klass
, arg
, NULL
);
408 PyObject
* one
= PyInt_FromLong(1);
409 PyObject_SetAttrString(obj
, "thisown", one
);
417 PyObject
* wxPyConstructObject(void* ptr
,
418 const char* className
,
427 char buff
[64]; // should always be big enough...
428 sprintf(buff
, "%sPtr", className
);
430 wxASSERT_MSG(wxPython_dict
, "wxPython_dict is not set yet!!");
432 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
436 "*** Unknown class name %s, tell Robin about it please ***",
438 obj
= PyString_FromString(temp
);
442 return wxPyConstructObject(ptr
, className
, classobj
, setThisOwn
);
445 //---------------------------------------------------------------------------
448 #ifdef WXP_WITH_THREAD
450 unsigned long wxPyGetCurrentThreadId() {
451 return wxThread::GetCurrentId();
454 static PyThreadState
* gs_shutdownTState
;
456 PyThreadState
* wxPyGetThreadState() {
457 if (wxPyTMutex
== NULL
) // Python is shutting down...
458 return gs_shutdownTState
;
460 unsigned long ctid
= wxPyGetCurrentThreadId();
461 PyThreadState
* tstate
= NULL
;
464 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
465 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
466 if (info
.tid
== ctid
) {
467 tstate
= info
.tstate
;
471 wxPyTMutex
->Unlock();
472 wxASSERT_MSG(tstate
, "PyThreadState should not be NULL!");
477 void wxPySaveThreadState(PyThreadState
* tstate
) {
478 if (wxPyTMutex
== NULL
) { // Python is shutting down, assume a single thread...
479 gs_shutdownTState
= tstate
;
482 unsigned long ctid
= wxPyGetCurrentThreadId();
484 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
485 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
486 if (info
.tid
== ctid
) {
487 info
.tstate
= tstate
;
488 wxPyTMutex
->Unlock();
492 // not found, so add it...
493 wxPyTStates
->Add(new wxPyThreadState(ctid
, tstate
));
494 wxPyTMutex
->Unlock();
500 // Calls from Python to wxWindows code are wrapped in calls to these
503 PyThreadState
* wxPyBeginAllowThreads() {
504 #ifdef WXP_WITH_THREAD
505 PyThreadState
* saved
= PyEval_SaveThread(); // Py_BEGIN_ALLOW_THREADS;
506 wxPySaveThreadState(saved
);
513 void wxPyEndAllowThreads(PyThreadState
* saved
) {
514 #ifdef WXP_WITH_THREAD
515 PyEval_RestoreThread(saved
); // Py_END_ALLOW_THREADS;
521 // Calls from wxWindows back to Python code, or even any PyObject
522 // manipulations, PyDECREF's and etc. are wrapped in calls to these functions:
524 void wxPyBeginBlockThreads() {
525 #ifdef WXP_WITH_THREAD
526 PyThreadState
* tstate
= wxPyGetThreadState();
527 PyEval_RestoreThread(tstate
);
532 void wxPyEndBlockThreads() {
533 #ifdef WXP_WITH_THREAD
534 PyThreadState
* tstate
= PyEval_SaveThread();
535 // Is there any need to save it again?
540 //---------------------------------------------------------------------------
541 // wxPyInputStream and wxPyCBInputStream methods
543 #include <wx/listimpl.cpp>
544 WX_DEFINE_LIST(wxStringPtrList
);
547 void wxPyInputStream::close() {
551 void wxPyInputStream::flush() {
555 bool wxPyInputStream::eof() {
557 return m_wxis
->Eof();
562 wxPyInputStream::~wxPyInputStream() {
566 wxString
* wxPyInputStream::read(int size
) {
568 const int BUFSIZE
= 1024;
570 // check if we have a real wxInputStream to work with
572 PyErr_SetString(PyExc_IOError
, "no valid C-wxInputStream");
578 char * buf
= new char[BUFSIZE
];
592 while (! m_wxis
->Eof()) {
593 m_wxis
->Read(buf
, BUFSIZE
);
594 s
->Append(buf
, m_wxis
->LastRead());
599 if (m_wxis
->LastError() == wxSTREAM_READ_ERROR
) {
601 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
605 } else { // Read only size number of characters
613 m_wxis
->Read(s
->GetWriteBuf(size
+1), size
);
614 s
->UngetWriteBuf(m_wxis
->LastRead());
617 if (m_wxis
->LastError() == wxSTREAM_READ_ERROR
) {
619 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
627 wxString
* wxPyInputStream::readline (int size
) {
628 // check if we have a real wxInputStream to work with
630 PyErr_SetString(PyExc_IOError
,"no valid C-wxInputStream");
637 wxString
* s
= new wxString
;
643 // read until \n or byte limit reached
644 for (i
=ch
=0; (ch
!= '\n') && (!m_wxis
->Eof()) && ((size
< 0) || (i
< size
)); i
++) {
645 *s
+= ch
= m_wxis
->GetC();
649 if (m_wxis
->LastError() == wxSTREAM_READ_ERROR
) {
651 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
658 wxStringPtrList
* wxPyInputStream::readlines (int sizehint
) {
659 // check if we have a real wxInputStream to work with
661 PyErr_SetString(PyExc_IOError
,"no valid C-wxInputStream below");
666 wxStringPtrList
* l
= new wxStringPtrList();
672 // read sizehint bytes or until EOF
674 for (i
=0; (!m_wxis
->Eof()) && ((sizehint
< 0) || (i
< sizehint
));) {
675 wxString
* s
= readline();
677 l
->DeleteContents(TRUE
);
686 if (m_wxis
->LastError() == wxSTREAM_READ_ERROR
) {
687 l
->DeleteContents(TRUE
);
689 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
696 void wxPyInputStream::seek(int offset
, int whence
) {
698 m_wxis
->SeekI(offset
, wxSeekMode(whence
));
701 int wxPyInputStream::tell(){
703 return m_wxis
->TellI();
710 wxPyCBInputStream::wxPyCBInputStream(PyObject
*r
, PyObject
*s
, PyObject
*t
, bool block
)
711 : wxInputStream(), m_read(r
), m_seek(s
), m_tell(t
), m_block(block
)
715 wxPyCBInputStream::~wxPyCBInputStream() {
716 if (m_block
) wxPyBeginBlockThreads();
720 if (m_block
) wxPyEndBlockThreads();
724 wxPyCBInputStream
* wxPyCBInputStream::create(PyObject
*py
, bool block
) {
725 if (block
) wxPyBeginBlockThreads();
727 PyObject
* read
= getMethod(py
, "read");
728 PyObject
* seek
= getMethod(py
, "seek");
729 PyObject
* tell
= getMethod(py
, "tell");
732 PyErr_SetString(PyExc_TypeError
, "Not a file-like object");
736 if (block
) wxPyEndBlockThreads();
740 if (block
) wxPyEndBlockThreads();
741 return new wxPyCBInputStream(read
, seek
, tell
, block
);
744 PyObject
* wxPyCBInputStream::getMethod(PyObject
* py
, char* name
) {
745 if (!PyObject_HasAttrString(py
, name
))
747 PyObject
* o
= PyObject_GetAttrString(py
, name
);
748 if (!PyMethod_Check(o
) && !PyCFunction_Check(o
)) {
756 size_t wxPyCBInputStream::GetSize() const {
757 wxPyCBInputStream
* self
= (wxPyCBInputStream
*)this; // cast off const
758 if (m_seek
&& m_tell
) {
759 off_t temp
= self
->OnSysTell();
760 off_t ret
= self
->OnSysSeek(0, wxFromEnd
);
761 self
->OnSysSeek(temp
, wxFromStart
);
769 size_t wxPyCBInputStream::OnSysRead(void *buffer
, size_t bufsize
) {
773 wxPyBeginBlockThreads();
774 PyObject
* arglist
= Py_BuildValue("(i)", bufsize
);
775 PyObject
* result
= PyEval_CallObject(m_read
, arglist
);
779 if ((result
!= NULL
) && PyString_Check(result
)) { // TODO: unicode?
780 o
= PyString_Size(result
);
782 m_lasterror
= wxSTREAM_EOF
;
785 memcpy((char*)buffer
, PyString_AsString(result
), o
);
790 m_lasterror
= wxSTREAM_READ_ERROR
;
791 wxPyEndBlockThreads();
796 size_t wxPyCBInputStream::OnSysWrite(const void *buffer
, size_t bufsize
) {
797 m_lasterror
= wxSTREAM_WRITE_ERROR
;
801 off_t
wxPyCBInputStream::OnSysSeek(off_t off
, wxSeekMode mode
) {
802 wxPyBeginBlockThreads();
803 PyObject
* arglist
= Py_BuildValue("(ii)", off
, mode
);
804 PyObject
* result
= PyEval_CallObject(m_seek
, arglist
);
807 wxPyEndBlockThreads();
811 off_t
wxPyCBInputStream::OnSysTell() const {
812 wxPyBeginBlockThreads();
813 PyObject
* arglist
= Py_BuildValue("()");
814 PyObject
* result
= PyEval_CallObject(m_tell
, arglist
);
817 if (result
!= NULL
) {
818 o
= PyInt_AsLong(result
);
821 wxPyEndBlockThreads();
825 //----------------------------------------------------------------------
827 IMPLEMENT_ABSTRACT_CLASS(wxPyCallback
, wxObject
);
829 wxPyCallback::wxPyCallback(PyObject
* func
) {
834 wxPyCallback::wxPyCallback(const wxPyCallback
& other
) {
835 m_func
= other
.m_func
;
839 wxPyCallback::~wxPyCallback() {
840 wxPyBeginBlockThreads();
842 wxPyEndBlockThreads();
847 // This function is used for all events destined for Python event handlers.
848 void wxPyCallback::EventThunker(wxEvent
& event
) {
849 wxPyCallback
* cb
= (wxPyCallback
*)event
.m_callbackUserData
;
850 PyObject
* func
= cb
->m_func
;
856 wxPyBeginBlockThreads();
857 wxString className
= event
.GetClassInfo()->GetClassName();
859 if (className
== "wxPyEvent")
860 arg
= ((wxPyEvent
*)&event
)->GetSelf();
861 else if (className
== "wxPyCommandEvent")
862 arg
= ((wxPyCommandEvent
*)&event
)->GetSelf();
864 arg
= wxPyConstructObject((void*)&event
, className
);
866 tuple
= PyTuple_New(1);
867 PyTuple_SET_ITEM(tuple
, 0, arg
);
868 result
= PyEval_CallObject(func
, tuple
);
872 PyErr_Clear(); // Just in case...
876 wxPyEndBlockThreads();
880 //----------------------------------------------------------------------
882 wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper
& other
) {
884 m_self
= other
.m_self
;
885 m_class
= other
.m_class
;
893 void wxPyCallbackHelper::setSelf(PyObject
* self
, PyObject
* klass
, int incref
) {
904 #if PYTHON_API_VERSION >= 1011
906 // Prior to Python 2.2 PyMethod_GetClass returned the class object
907 // in which the method was defined. Starting with 2.2 it returns
908 // "class that asked for the method" which seems totally bogus to me
909 // but apprently it fixes some obscure problem waiting to happen in
910 // Python. Since the API was not documented Guido and the gang felt
911 // safe in changing it. Needless to say that totally screwed up the
912 // logic below in wxPyCallbackHelper::findCallback, hence this icky
913 // code to find the class where the method is actually defined...
916 PyObject
* PyFindClassWithAttr(PyObject
*klass
, PyObject
*name
)
920 if (PyType_Check(klass
)) { // new style classes
921 // This code is borrowed/adapted from _PyType_Lookup in typeobject.c
922 // (TODO: This part is not tested yet, so I'm not sure it is correct...)
923 PyTypeObject
* type
= (PyTypeObject
*)klass
;
924 PyObject
*mro
, *res
, *base
, *dict
;
925 /* Look in tp_dict of types in MRO */
927 assert(PyTuple_Check(mro
));
928 n
= PyTuple_GET_SIZE(mro
);
929 for (i
= 0; i
< n
; i
++) {
930 base
= PyTuple_GET_ITEM(mro
, i
);
931 if (PyClass_Check(base
))
932 dict
= ((PyClassObject
*)base
)->cl_dict
;
934 assert(PyType_Check(base
));
935 dict
= ((PyTypeObject
*)base
)->tp_dict
;
937 assert(dict
&& PyDict_Check(dict
));
938 res
= PyDict_GetItem(dict
, name
);
945 else if (PyClass_Check(klass
)) { // old style classes
946 // This code is borrowed/adapted from class_lookup in classobject.c
947 PyClassObject
* cp
= (PyClassObject
*)klass
;
948 PyObject
*value
= PyDict_GetItem(cp
->cl_dict
, name
);
950 return (PyObject
*)cp
;
952 n
= PyTuple_Size(cp
->cl_bases
);
953 for (i
= 0; i
< n
; i
++) {
954 PyObject
* base
= PyTuple_GetItem(cp
->cl_bases
, i
);
955 PyObject
*v
= PyFindClassWithAttr(base
, name
);
967 PyObject
* PyMethod_GetDefiningClass(PyObject
* method
, const char* name
)
969 PyObject
* mgc
= PyMethod_GET_CLASS(method
);
971 #if PYTHON_API_VERSION <= 1010 // prior to Python 2.2, the easy way
973 #else // 2.2 and after, the hard way...
975 PyObject
* nameo
= PyString_FromString(name
);
976 PyObject
* klass
= PyFindClassWithAttr(mgc
, nameo
);
984 bool wxPyCallbackHelper::findCallback(const char* name
) const {
985 wxPyCallbackHelper
* self
= (wxPyCallbackHelper
*)this; // cast away const
986 self
->m_lastFound
= NULL
;
988 // If the object (m_self) has an attibute of the given name...
989 if (m_self
&& PyObject_HasAttrString(m_self
, (char*)name
)) {
990 PyObject
*method
, *klass
;
991 method
= PyObject_GetAttrString(m_self
, (char*)name
);
993 // ...and if that attribute is a method, and if that method's class is
994 // not from a base class...
995 if (PyMethod_Check(method
) &&
996 (klass
= PyMethod_GetDefiningClass(method
, (char*)name
)) != NULL
&&
997 ((klass
== m_class
) || PyClass_IsSubclass(klass
, m_class
))) {
999 // ...then we'll save a pointer to the method so callCallback can call it.
1000 self
->m_lastFound
= method
;
1006 return m_lastFound
!= NULL
;
1010 int wxPyCallbackHelper::callCallback(PyObject
* argTuple
) const {
1014 result
= callCallbackObj(argTuple
);
1015 if (result
) { // Assumes an integer return type...
1016 retval
= PyInt_AsLong(result
);
1018 PyErr_Clear(); // forget about it if it's not...
1023 // Invoke the Python callable object, returning the raw PyObject return
1024 // value. Caller should DECREF the return value and also call PyEval_SaveThread.
1025 PyObject
* wxPyCallbackHelper::callCallbackObj(PyObject
* argTuple
) const {
1028 // Save a copy of the pointer in case the callback generates another
1029 // callback. In that case m_lastFound will have a different value when
1030 // it gets back here...
1031 PyObject
* method
= m_lastFound
;
1033 result
= PyEval_CallObject(method
, argTuple
);
1034 Py_DECREF(argTuple
);
1043 void wxPyCBH_setCallbackInfo(wxPyCallbackHelper
& cbh
, PyObject
* self
, PyObject
* klass
, int incref
) {
1044 cbh
.setSelf(self
, klass
, incref
);
1047 bool wxPyCBH_findCallback(const wxPyCallbackHelper
& cbh
, const char* name
) {
1048 return cbh
.findCallback(name
);
1051 int wxPyCBH_callCallback(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
1052 return cbh
.callCallback(argTuple
);
1055 PyObject
* wxPyCBH_callCallbackObj(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
1056 return cbh
.callCallbackObj(argTuple
);
1060 void wxPyCBH_delete(wxPyCallbackHelper
* cbh
) {
1061 if (cbh
->m_incRef
) {
1062 wxPyBeginBlockThreads();
1063 Py_XDECREF(cbh
->m_self
);
1064 Py_XDECREF(cbh
->m_class
);
1065 wxPyEndBlockThreads();
1069 //---------------------------------------------------------------------------
1070 //---------------------------------------------------------------------------
1071 // These event classes can be derived from in Python and passed through the event
1072 // system without losing anything. They do this by keeping a reference to
1073 // themselves and some special case handling in wxPyCallback::EventThunker.
1076 wxPyEvtSelfRef::wxPyEvtSelfRef() {
1077 //m_self = Py_None; // **** We don't do normal ref counting to prevent
1078 //Py_INCREF(m_self); // circular loops...
1082 wxPyEvtSelfRef::~wxPyEvtSelfRef() {
1083 wxPyBeginBlockThreads();
1086 wxPyEndBlockThreads();
1089 void wxPyEvtSelfRef::SetSelf(PyObject
* self
, bool clone
) {
1090 wxPyBeginBlockThreads();
1098 wxPyEndBlockThreads();
1101 PyObject
* wxPyEvtSelfRef::GetSelf() const {
1107 IMPLEMENT_ABSTRACT_CLASS(wxPyEvent
, wxEvent
);
1108 IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent
, wxCommandEvent
);
1111 wxPyEvent::wxPyEvent(int id
)
1116 wxPyEvent::wxPyEvent(const wxPyEvent
& evt
)
1119 SetSelf(evt
.m_self
, TRUE
);
1123 wxPyEvent::~wxPyEvent() {
1127 wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType
, int id
)
1128 : wxCommandEvent(commandType
, id
) {
1132 wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent
& evt
)
1133 : wxCommandEvent(evt
)
1135 SetSelf(evt
.m_self
, TRUE
);
1139 wxPyCommandEvent::~wxPyCommandEvent() {
1145 //---------------------------------------------------------------------------
1146 //---------------------------------------------------------------------------
1149 wxPyTimer::wxPyTimer(PyObject
* callback
) {
1154 wxPyTimer::~wxPyTimer() {
1155 wxPyBeginBlockThreads();
1157 wxPyEndBlockThreads();
1160 void wxPyTimer::Notify() {
1161 if (!func
|| func
== Py_None
) {
1165 wxPyBeginBlockThreads();
1168 PyObject
* args
= Py_BuildValue("()");
1170 result
= PyEval_CallObject(func
, args
);
1179 wxPyEndBlockThreads();
1185 //---------------------------------------------------------------------------
1186 //---------------------------------------------------------------------------
1187 // Convert a wxList to a Python List
1189 PyObject
* wxPy_ConvertList(wxListBase
* list
, const char* className
) {
1193 wxNode
* node
= list
->First();
1195 wxPyBeginBlockThreads();
1196 pyList
= PyList_New(0);
1198 wxObj
= node
->Data();
1199 pyObj
= wxPyMake_wxObject(wxObj
); //wxPyConstructObject(wxObj, className);
1200 PyList_Append(pyList
, pyObj
);
1201 node
= node
->Next();
1203 wxPyEndBlockThreads();
1207 //----------------------------------------------------------------------
1209 long wxPyGetWinHandle(wxWindow
* win
) {
1211 return (long)win
->GetHandle();
1214 // Find and return the actual X-Window.
1216 if (win
->m_wxwindow
) {
1217 GdkWindowPrivate
* bwin
= (GdkWindowPrivate
*)GTK_PIZZA(win
->m_wxwindow
)->bin_window
;
1219 return (long)bwin
->xwindow
;
1226 //----------------------------------------------------------------------
1227 // Some helper functions for typemaps in my_typemaps.i, so they won't be
1228 // included in every file...
1231 byte
* byte_LIST_helper(PyObject
* source
) {
1232 if (!PyList_Check(source
)) {
1233 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1236 int count
= PyList_Size(source
);
1237 byte
* temp
= new byte
[count
];
1239 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1242 for (int x
=0; x
<count
; x
++) {
1243 PyObject
* o
= PyList_GetItem(source
, x
);
1244 if (! PyInt_Check(o
)) {
1245 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1248 temp
[x
] = (byte
)PyInt_AsLong(o
);
1254 int* int_LIST_helper(PyObject
* source
) {
1255 if (!PyList_Check(source
)) {
1256 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1259 int count
= PyList_Size(source
);
1260 int* temp
= new int[count
];
1262 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1265 for (int x
=0; x
<count
; x
++) {
1266 PyObject
* o
= PyList_GetItem(source
, x
);
1267 if (! PyInt_Check(o
)) {
1268 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1271 temp
[x
] = PyInt_AsLong(o
);
1277 long* long_LIST_helper(PyObject
* source
) {
1278 if (!PyList_Check(source
)) {
1279 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1282 int count
= PyList_Size(source
);
1283 long* temp
= new long[count
];
1285 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1288 for (int x
=0; x
<count
; x
++) {
1289 PyObject
* o
= PyList_GetItem(source
, x
);
1290 if (! PyInt_Check(o
)) {
1291 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1294 temp
[x
] = PyInt_AsLong(o
);
1300 char** string_LIST_helper(PyObject
* source
) {
1301 if (!PyList_Check(source
)) {
1302 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1305 int count
= PyList_Size(source
);
1306 char** temp
= new char*[count
];
1308 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1311 for (int x
=0; x
<count
; x
++) {
1312 PyObject
* o
= PyList_GetItem(source
, x
);
1313 if (! PyString_Check(o
)) {
1314 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1317 temp
[x
] = PyString_AsString(o
);
1322 //--------------------------------
1323 // Part of patch from Tim Hochberg
1324 static inline bool wxPointFromObjects(PyObject
* o1
, PyObject
* o2
, wxPoint
* point
) {
1325 if (PyInt_Check(o1
) && PyInt_Check(o2
)) {
1326 point
->x
= PyInt_AS_LONG(o1
);
1327 point
->y
= PyInt_AS_LONG(o2
);
1330 if (PyFloat_Check(o1
) && PyFloat_Check(o2
)) {
1331 point
->x
= (int)PyFloat_AS_DOUBLE(o1
);
1332 point
->y
= (int)PyFloat_AS_DOUBLE(o2
);
1335 if (PyInstance_Check(o1
) || PyInstance_Check(o2
)) {
1336 // Disallow instances because they can cause havok
1339 if (PyNumber_Check(o1
) && PyNumber_Check(o2
)) {
1340 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
1341 point
->x
= PyInt_AsLong(o1
);
1342 point
->y
= PyInt_AsLong(o2
);
1349 wxPoint
* wxPoint_LIST_helper(PyObject
* source
, int *count
) {
1350 // Putting all of the declarations here allows
1351 // us to put the error handling all in one place.
1354 PyObject
*o
, *o1
, *o2
;
1355 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1357 if (!PySequence_Check(source
)) {
1361 // The length of the sequence is returned in count.
1362 *count
= PySequence_Length(source
);
1367 temp
= new wxPoint
[*count
];
1369 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1372 for (x
=0; x
<*count
; x
++) {
1373 // Get an item: try fast way first.
1375 o
= PySequence_Fast_GET_ITEM(source
, x
);
1378 o
= PySequence_GetItem(source
, x
);
1384 // Convert o to wxPoint.
1385 if ((PyTuple_Check(o
) && PyTuple_GET_SIZE(o
) == 2) ||
1386 (PyList_Check(o
) && PyList_GET_SIZE(o
) == 2)) {
1387 o1
= PySequence_Fast_GET_ITEM(o
, 0);
1388 o2
= PySequence_Fast_GET_ITEM(o
, 1);
1389 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
1393 else if (PyInstance_Check(o
)) {
1395 if (SWIG_GetPtrObj(o
, (void **)&pt
, "_wxPoint_p")) {
1400 else if (PySequence_Check(o
) && PySequence_Length(o
) == 2) {
1401 o1
= PySequence_GetItem(o
, 0);
1402 o2
= PySequence_GetItem(o
, 1);
1403 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
1427 PyErr_SetString(PyExc_TypeError
, "Expected a sequence of length-2 sequences or wxPoints.");
1431 //------------------------------
1434 wxBitmap
** wxBitmap_LIST_helper(PyObject
* source
) {
1435 if (!PyList_Check(source
)) {
1436 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1439 int count
= PyList_Size(source
);
1440 wxBitmap
** temp
= new wxBitmap
*[count
];
1442 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1445 for (int x
=0; x
<count
; x
++) {
1446 PyObject
* o
= PyList_GetItem(source
, x
);
1447 if (PyInstance_Check(o
)) {
1449 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxBitmap_p")) {
1450 PyErr_SetString(PyExc_TypeError
,"Expected _wxBitmap_p.");
1456 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxBitmaps.");
1465 wxString
* wxString_LIST_helper(PyObject
* source
) {
1466 if (!PyList_Check(source
)) {
1467 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1470 int count
= PyList_Size(source
);
1471 wxString
* temp
= new wxString
[count
];
1473 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1476 for (int x
=0; x
<count
; x
++) {
1477 PyObject
* o
= PyList_GetItem(source
, x
);
1478 #if PYTHON_API_VERSION >= 1009
1479 if (! PyString_Check(o
) && ! PyUnicode_Check(o
)) {
1480 PyErr_SetString(PyExc_TypeError
, "Expected a list of string or unicode objects.");
1486 if (PyString_AsStringAndSize(o
, &buff
, &length
) == -1)
1488 temp
[x
] = wxString(buff
, length
);
1490 if (! PyString_Check(o
)) {
1491 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1494 temp
[x
] = PyString_AsString(o
);
1501 wxAcceleratorEntry
* wxAcceleratorEntry_LIST_helper(PyObject
* source
) {
1502 if (!PyList_Check(source
)) {
1503 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1506 int count
= PyList_Size(source
);
1507 wxAcceleratorEntry
* temp
= new wxAcceleratorEntry
[count
];
1509 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1512 for (int x
=0; x
<count
; x
++) {
1513 PyObject
* o
= PyList_GetItem(source
, x
);
1514 if (PyInstance_Check(o
)) {
1515 wxAcceleratorEntry
* ae
;
1516 if (SWIG_GetPtrObj(o
, (void **) &ae
,"_wxAcceleratorEntry_p")) {
1517 PyErr_SetString(PyExc_TypeError
,"Expected _wxAcceleratorEntry_p.");
1522 else if (PyTuple_Check(o
)) {
1523 PyObject
* o1
= PyTuple_GetItem(o
, 0);
1524 PyObject
* o2
= PyTuple_GetItem(o
, 1);
1525 PyObject
* o3
= PyTuple_GetItem(o
, 2);
1526 temp
[x
].Set(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
));
1529 PyErr_SetString(PyExc_TypeError
, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
1537 wxPen
** wxPen_LIST_helper(PyObject
* source
) {
1538 if (!PyList_Check(source
)) {
1539 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1542 int count
= PyList_Size(source
);
1543 wxPen
** temp
= new wxPen
*[count
];
1545 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1548 for (int x
=0; x
<count
; x
++) {
1549 PyObject
* o
= PyList_GetItem(source
, x
);
1550 if (PyInstance_Check(o
)) {
1552 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxPen_p")) {
1554 PyErr_SetString(PyExc_TypeError
,"Expected _wxPen_p.");
1561 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxPens.");
1569 bool _2int_seq_helper(PyObject
* source
, int* i1
, int* i2
) {
1570 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1573 if (!PySequence_Check(source
) || PySequence_Length(source
) != 2)
1577 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1578 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1581 o1
= PySequence_GetItem(source
, 0);
1582 o2
= PySequence_GetItem(source
, 1);
1585 *i1
= PyInt_AsLong(o1
);
1586 *i2
= PyInt_AsLong(o2
);
1596 bool _4int_seq_helper(PyObject
* source
, int* i1
, int* i2
, int* i3
, int* i4
) {
1597 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1598 PyObject
*o1
, *o2
, *o3
, *o4
;
1600 if (!PySequence_Check(source
) || PySequence_Length(source
) != 4)
1604 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1605 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1606 o3
= PySequence_Fast_GET_ITEM(source
, 2);
1607 o4
= PySequence_Fast_GET_ITEM(source
, 3);
1610 o1
= PySequence_GetItem(source
, 0);
1611 o2
= PySequence_GetItem(source
, 1);
1612 o3
= PySequence_GetItem(source
, 2);
1613 o4
= PySequence_GetItem(source
, 3);
1616 *i1
= PyInt_AsLong(o1
);
1617 *i2
= PyInt_AsLong(o2
);
1618 *i3
= PyInt_AsLong(o3
);
1619 *i4
= PyInt_AsLong(o4
);
1631 //----------------------------------------------------------------------
1633 bool wxSize_helper(PyObject
* source
, wxSize
** obj
) {
1635 // If source is an object instance then it may already be the right type
1636 if (PyInstance_Check(source
)) {
1638 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxSize_p"))
1643 // otherwise a 2-tuple of integers is expected
1644 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1645 PyObject
* o1
= PySequence_GetItem(source
, 0);
1646 PyObject
* o2
= PySequence_GetItem(source
, 1);
1647 **obj
= wxSize(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1652 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxSize object.");
1656 bool wxPoint_helper(PyObject
* source
, wxPoint
** obj
) {
1658 // If source is an object instance then it may already be the right type
1659 if (PyInstance_Check(source
)) {
1661 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxPoint_p"))
1666 // otherwise a length-2 sequence of integers is expected
1667 if (PySequence_Check(source
) && PySequence_Length(source
) == 2) {
1668 PyObject
* o1
= PySequence_GetItem(source
, 0);
1669 PyObject
* o2
= PySequence_GetItem(source
, 1);
1670 // This should really check for integers, not numbers -- but that would break code.
1671 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1676 **obj
= wxPoint(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1682 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxPoint object.");
1688 bool wxRealPoint_helper(PyObject
* source
, wxRealPoint
** obj
) {
1690 // If source is an object instance then it may already be the right type
1691 if (PyInstance_Check(source
)) {
1693 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRealPoint_p"))
1698 // otherwise a 2-tuple of floats is expected
1699 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1700 PyObject
* o1
= PySequence_GetItem(source
, 0);
1701 PyObject
* o2
= PySequence_GetItem(source
, 1);
1702 **obj
= wxRealPoint(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
));
1707 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxRealPoint object.");
1714 bool wxRect_helper(PyObject
* source
, wxRect
** obj
) {
1716 // If source is an object instance then it may already be the right type
1717 if (PyInstance_Check(source
)) {
1719 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRect_p"))
1724 // otherwise a 4-tuple of integers is expected
1725 else if (PySequence_Check(source
) && PyObject_Length(source
) == 4) {
1726 PyObject
* o1
= PySequence_GetItem(source
, 0);
1727 PyObject
* o2
= PySequence_GetItem(source
, 1);
1728 PyObject
* o3
= PySequence_GetItem(source
, 2);
1729 PyObject
* o4
= PySequence_GetItem(source
, 3);
1730 **obj
= wxRect(PyInt_AsLong(o1
), PyInt_AsLong(o2
),
1731 PyInt_AsLong(o3
), PyInt_AsLong(o4
));
1736 PyErr_SetString(PyExc_TypeError
, "Expected a 4-tuple of integers or a wxRect object.");
1742 bool wxColour_helper(PyObject
* source
, wxColour
** obj
) {
1744 // If source is an object instance then it may already be the right type
1745 if (PyInstance_Check(source
)) {
1747 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxColour_p"))
1752 // otherwise a string is expected
1753 else if (PyString_Check(source
)) {
1754 wxString spec
= PyString_AS_STRING(source
);
1755 if (spec
[0U] == '#' && spec
.Length() == 7) { // It's #RRGGBB
1757 int red
= strtol(spec
.Mid(1,2), &junk
, 16);
1758 int green
= strtol(spec
.Mid(3,2), &junk
, 16);
1759 int blue
= strtol(spec
.Mid(5,2), &junk
, 16);
1760 **obj
= wxColour(red
, green
, blue
);
1763 else { // it's a colour name
1764 **obj
= wxColour(spec
);
1770 PyErr_SetString(PyExc_TypeError
, "Expected a wxColour object or a string containing a colour name or '#RRGGBB'.");
1775 //----------------------------------------------------------------------
1777 PyObject
* wxArrayString2PyList_helper(const wxArrayString
& arr
) {
1779 PyObject
* list
= PyList_New(0);
1780 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
1781 PyObject
* str
= PyString_FromString(arr
[i
].c_str());
1782 PyList_Append(list
, str
);
1789 PyObject
* wxArrayInt2PyList_helper(const wxArrayInt
& arr
) {
1791 PyObject
* list
= PyList_New(0);
1792 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
1793 PyObject
* number
= PyInt_FromLong(arr
[i
]);
1794 PyList_Append(list
, number
);
1801 //----------------------------------------------------------------------
1802 //----------------------------------------------------------------------