]>
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
20 #include <wx/msw/private.h>
21 #include <wx/msw/winundef.h>
22 #include <wx/msw/msvcrt.h>
27 #include <gdk/gdkprivate.h>
28 #include <wx/gtk/win_gtk.h>
32 //----------------------------------------------------------------------
35 int WXDLLEXPORT
wxEntryStart( int& argc
, char** argv
);
37 int WXDLLEXPORT
wxEntryStart( int argc
, char** argv
);
39 int WXDLLEXPORT
wxEntryInitGui();
40 void WXDLLEXPORT
wxEntryCleanup();
42 wxPyApp
* wxPythonApp
= NULL
; // Global instance of application object
45 #ifdef WXP_WITH_THREAD
46 struct wxPyThreadState
{
48 PyThreadState
* tstate
;
50 wxPyThreadState(unsigned long _tid
=0, PyThreadState
* _tstate
=NULL
)
51 : tid(_tid
), tstate(_tstate
) {}
54 #include <wx/dynarray.h>
55 WX_DECLARE_OBJARRAY(wxPyThreadState
, wxPyThreadStateArray
);
56 #include <wx/arrimpl.cpp>
57 WX_DEFINE_OBJARRAY(wxPyThreadStateArray
);
59 wxPyThreadStateArray
* wxPyTStates
= NULL
;
60 wxMutex
* wxPyTMutex
= NULL
;
64 #ifdef __WXMSW__ // If building for win32...
65 //----------------------------------------------------------------------
66 // This gets run when the DLL is loaded. We just need to save a handle.
67 //----------------------------------------------------------------------
70 HINSTANCE hinstDLL
, // handle to DLL module
71 DWORD fdwReason
, // reason for calling function
72 LPVOID lpvReserved
// reserved
75 wxSetInstance(hinstDLL
);
80 //----------------------------------------------------------------------
81 // Classes for implementing the wxp main application shell.
82 //----------------------------------------------------------------------
86 // printf("**** ctor\n");
90 // printf("**** dtor\n");
94 // This one isn't acutally called... See __wxStart()
95 bool wxPyApp::OnInit() {
100 int wxPyApp::MainLoop() {
103 DeletePendingObjects();
104 bool initialized
= wxTopLevelWindows
.GetCount() != 0;
106 m_initialized
= initialized
;
110 retval
= wxApp::MainLoop();
118 //---------------------------------------------------------------------
119 //----------------------------------------------------------------------
122 // This is where we pick up the first part of the wxEntry functionality...
123 // The rest is in __wxStart and __wxCleanup. This function is called when
124 // wxcmodule is imported. (Before there is a wxApp object.)
129 // wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
132 #ifdef WXP_WITH_THREAD
133 PyEval_InitThreads();
134 wxPyTStates
= new wxPyThreadStateArray
;
135 wxPyTMutex
= new wxMutex
;
138 // Bail out if there is already windows created. This means that the
139 // toolkit has already been initialized, as in embedding wxPython in
140 // a C++ wxWindows app.
141 if (wxTopLevelWindows
.Number() > 0)
147 PyObject
* sysargv
= PySys_GetObject("argv");
148 if (sysargv
!= NULL
) {
149 argc
= PyList_Size(sysargv
);
150 argv
= new char*[argc
+1];
152 for(x
=0; x
<argc
; x
++)
153 argv
[x
] = copystring(PyString_AsString(PyList_GetItem(sysargv
, x
)));
157 wxEntryStart(argc
, argv
);
163 // Start the user application, user App's OnInit method is a parameter here
164 PyObject
* __wxStart(PyObject
* /* self */, PyObject
* args
)
166 PyObject
* onInitFunc
= NULL
;
171 if (!PyArg_ParseTuple(args
, "O", &onInitFunc
))
174 #if 0 // Try it out without this check, see how it does...
175 if (wxTopLevelWindows
.Number() > 0) {
176 PyErr_SetString(PyExc_TypeError
, "Only 1 wxApp per process!");
181 // This is the next part of the wxEntry functionality...
184 PyObject
* sysargv
= PySys_GetObject("argv");
185 if (sysargv
!= NULL
) {
186 argc
= PyList_Size(sysargv
);
187 argv
= new char*[argc
+1];
189 for(x
=0; x
<argc
; x
++)
190 argv
[x
] = copystring(PyString_AsString(PyList_GetItem(sysargv
, x
)));
193 wxPythonApp
->argc
= argc
;
194 wxPythonApp
->argv
= argv
;
198 // Call the Python App's OnInit function
199 arglist
= PyTuple_New(0);
200 result
= PyEval_CallObject(onInitFunc
, arglist
);
201 if (!result
) { // an exception was raised.
205 if (! PyInt_Check(result
)) {
206 PyErr_SetString(PyExc_TypeError
, "OnInit should return a boolean value");
209 bResult
= PyInt_AS_LONG(result
);
211 PyErr_SetString(PyExc_SystemExit
, "OnInit returned FALSE, exiting...");
216 wxTheApp
->m_initialized
= (wxTopLevelWindows
.GetCount() > 0);
226 #ifdef WXP_WITH_THREAD
229 wxPyTStates
->Empty();
237 static PyObject
* wxPython_dict
= NULL
;
238 static PyObject
* wxPyPtrTypeMap
= NULL
;
240 PyObject
* __wxSetDictionary(PyObject
* /* self */, PyObject
* args
)
243 if (!PyArg_ParseTuple(args
, "O", &wxPython_dict
))
246 if (!PyDict_Check(wxPython_dict
)) {
247 PyErr_SetString(PyExc_TypeError
, "_wxSetDictionary must have dictionary object!");
251 if (! wxPyPtrTypeMap
)
252 wxPyPtrTypeMap
= PyDict_New();
253 PyDict_SetItemString(wxPython_dict
, "__wxPyPtrTypeMap", wxPyPtrTypeMap
);
257 #define wxPlatform "__WXMOTIF__"
260 #define wxPlatform "__WXX11__"
263 #define wxPlatform "__WXGTK__"
265 #if defined(__WIN32__) || defined(__WXMSW__)
266 #define wxPlatform "__WXMSW__"
269 #define wxPlatform "__WXMAC__"
272 PyDict_SetItemString(wxPython_dict
, "wxPlatform", PyString_FromString(wxPlatform
));
279 //---------------------------------------------------------------------------
280 // Stuff used by OOR to find the right wxPython class type to return and to
284 // The pointer type map is used when the "pointer" type name generated by SWIG
285 // is not the same as the shadow class name, for example wxPyTreeCtrl
286 // vs. wxTreeCtrl. It needs to be referenced in Python as well as from C++,
287 // so we'll just make it a Python dictionary in the wx module's namespace.
288 void wxPyPtrTypeMap_Add(const char* commonName
, const char* ptrName
) {
289 if (! wxPyPtrTypeMap
)
290 wxPyPtrTypeMap
= PyDict_New();
291 PyDict_SetItemString(wxPyPtrTypeMap
,
293 PyString_FromString((char*)ptrName
));
298 PyObject
* wxPyClassExists(const char* className
) {
303 char buff
[64]; // should always be big enough...
305 sprintf(buff
, "%sPtr", className
);
306 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
308 return classobj
; // returns NULL if not found
312 PyObject
* wxPyMake_wxObject(wxObject
* source
, bool checkEvtHandler
) {
313 PyObject
* target
= NULL
;
314 bool isEvtHandler
= FALSE
;
317 // If it's derived from wxEvtHandler then there may
318 // already be a pointer to a Python object that we can use
320 if (checkEvtHandler
&& wxIsKindOf(source
, wxEvtHandler
)) {
322 wxEvtHandler
* eh
= (wxEvtHandler
*)source
;
323 wxPyClientData
* data
= (wxPyClientData
*)eh
->GetClientObject();
325 target
= data
->m_obj
;
331 // Otherwise make it the old fashioned way by making a
332 // new shadow object and putting this pointer in it.
333 wxClassInfo
* info
= source
->GetClassInfo();
334 wxChar
* name
= (wxChar
*)info
->GetClassName();
335 PyObject
* klass
= wxPyClassExists(name
);
336 while (info
&& !klass
) {
337 name
= (wxChar
*)info
->GetBaseClassName1();
338 info
= wxClassInfo::FindClass(name
);
339 klass
= wxPyClassExists(name
);
342 target
= wxPyConstructObject(source
, name
, klass
, FALSE
);
343 if (target
&& isEvtHandler
)
344 ((wxEvtHandler
*)source
)->SetClientObject(new wxPyClientData(target
));
346 wxString
msg("wxPython class not found for ");
347 msg
+= source
->GetClassInfo()->GetClassName();
348 PyErr_SetString(PyExc_NameError
, msg
.c_str());
352 } else { // source was NULL so return None.
353 Py_INCREF(Py_None
); target
= Py_None
;
359 PyObject
* wxPyMake_wxSizer(wxSizer
* source
) {
360 PyObject
* target
= NULL
;
362 if (source
&& wxIsKindOf(source
, wxSizer
)) {
363 // If it's derived from wxSizer then there may
364 // already be a pointer to a Python object that we can use
366 wxSizer
* sz
= (wxSizer
*)source
;
367 wxPyClientData
* data
= (wxPyClientData
*)sz
->GetClientObject();
369 target
= data
->m_obj
;
374 target
= wxPyMake_wxObject(source
, FALSE
);
375 if (target
!= Py_None
)
376 ((wxSizer
*)source
)->SetClientObject(new wxPyClientData(target
));
383 //---------------------------------------------------------------------------
385 PyObject
* wxPyConstructObject(void* ptr
,
386 const char* className
,
393 char swigptr
[64]; // should always be big enough...
396 if ((item
= PyDict_GetItemString(wxPyPtrTypeMap
, (char*)className
)) != NULL
) {
397 className
= PyString_AsString(item
);
399 sprintf(buff
, "_%s_p", className
);
400 SWIG_MakePtr(swigptr
, ptr
, buff
);
402 arg
= Py_BuildValue("(s)", swigptr
);
403 obj
= PyInstance_New(klass
, arg
, NULL
);
407 PyObject
* one
= PyInt_FromLong(1);
408 PyObject_SetAttrString(obj
, "thisown", one
);
416 PyObject
* wxPyConstructObject(void* ptr
,
417 const char* className
,
426 char buff
[64]; // should always be big enough...
427 sprintf(buff
, "%sPtr", className
);
429 wxASSERT_MSG(wxPython_dict
, "wxPython_dict is not set yet!!");
431 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
435 "*** Unknown class name %s, tell Robin about it please ***",
437 obj
= PyString_FromString(temp
);
441 return wxPyConstructObject(ptr
, className
, classobj
, setThisOwn
);
444 //---------------------------------------------------------------------------
447 #ifdef WXP_WITH_THREAD
449 unsigned long wxPyGetCurrentThreadId() {
450 return wxThread::GetCurrentId();
453 static PyThreadState
* gs_shutdownTState
;
455 PyThreadState
* wxPyGetThreadState() {
456 if (wxPyTMutex
== NULL
) // Python is shutting down...
457 return gs_shutdownTState
;
459 unsigned long ctid
= wxPyGetCurrentThreadId();
460 PyThreadState
* tstate
= NULL
;
463 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
464 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
465 if (info
.tid
== ctid
) {
466 tstate
= info
.tstate
;
470 wxPyTMutex
->Unlock();
471 wxASSERT_MSG(tstate
, "PyThreadState should not be NULL!");
476 void wxPySaveThreadState(PyThreadState
* tstate
) {
477 if (wxPyTMutex
== NULL
) { // Python is shutting down, assume a single thread...
478 gs_shutdownTState
= tstate
;
481 unsigned long ctid
= wxPyGetCurrentThreadId();
483 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
484 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
485 if (info
.tid
== ctid
) {
486 info
.tstate
= tstate
;
487 wxPyTMutex
->Unlock();
491 // not found, so add it...
492 wxPyTStates
->Add(new wxPyThreadState(ctid
, tstate
));
493 wxPyTMutex
->Unlock();
499 // Calls from Python to wxWindows code are wrapped in calls to these
502 PyThreadState
* wxPyBeginAllowThreads() {
503 #ifdef WXP_WITH_THREAD
504 PyThreadState
* saved
= PyEval_SaveThread(); // Py_BEGIN_ALLOW_THREADS;
505 wxPySaveThreadState(saved
);
512 void wxPyEndAllowThreads(PyThreadState
* saved
) {
513 #ifdef WXP_WITH_THREAD
514 PyEval_RestoreThread(saved
); // Py_END_ALLOW_THREADS;
520 // Calls from wxWindows back to Python code, or even any PyObject
521 // manipulations, PyDECREF's and etc. are wrapped in calls to these functions:
523 void wxPyBeginBlockThreads() {
524 #ifdef WXP_WITH_THREAD
525 PyThreadState
* tstate
= wxPyGetThreadState();
526 PyEval_RestoreThread(tstate
);
531 void wxPyEndBlockThreads() {
532 #ifdef WXP_WITH_THREAD
533 PyThreadState
* tstate
= PyEval_SaveThread();
534 // Is there any need to save it again?
539 //---------------------------------------------------------------------------
540 // wxPyInputStream and wxPyCBInputStream methods
542 #include <wx/listimpl.cpp>
543 WX_DEFINE_LIST(wxStringPtrList
);
546 void wxPyInputStream::close() {
550 void wxPyInputStream::flush() {
554 bool wxPyInputStream::eof() {
556 return m_wxis
->Eof();
561 wxPyInputStream::~wxPyInputStream() {
565 wxString
* wxPyInputStream::read(int size
) {
567 const int BUFSIZE
= 1024;
569 // check if we have a real wxInputStream to work with
571 PyErr_SetString(PyExc_IOError
, "no valid C-wxInputStream");
577 char * buf
= new char[BUFSIZE
];
591 while (! m_wxis
->Eof()) {
592 m_wxis
->Read(buf
, BUFSIZE
);
593 s
->Append(buf
, m_wxis
->LastRead());
598 if (m_wxis
->LastError() == wxSTREAM_READ_ERROR
) {
600 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
604 } else { // Read only size number of characters
612 m_wxis
->Read(s
->GetWriteBuf(size
+1), size
);
613 s
->UngetWriteBuf(m_wxis
->LastRead());
616 if (m_wxis
->LastError() == wxSTREAM_READ_ERROR
) {
618 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
626 wxString
* wxPyInputStream::readline (int size
) {
627 // check if we have a real wxInputStream to work with
629 PyErr_SetString(PyExc_IOError
,"no valid C-wxInputStream");
636 wxString
* s
= new wxString
;
642 // read until \n or byte limit reached
643 for (i
=ch
=0; (ch
!= '\n') && (!m_wxis
->Eof()) && ((size
< 0) || (i
< size
)); i
++) {
644 *s
+= ch
= m_wxis
->GetC();
648 if (m_wxis
->LastError() == wxSTREAM_READ_ERROR
) {
650 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
657 wxStringPtrList
* wxPyInputStream::readlines (int sizehint
) {
658 // check if we have a real wxInputStream to work with
660 PyErr_SetString(PyExc_IOError
,"no valid C-wxInputStream below");
665 wxStringPtrList
* l
= new wxStringPtrList();
671 // read sizehint bytes or until EOF
673 for (i
=0; (!m_wxis
->Eof()) && ((sizehint
< 0) || (i
< sizehint
));) {
674 wxString
* s
= readline();
676 l
->DeleteContents(TRUE
);
685 if (m_wxis
->LastError() == wxSTREAM_READ_ERROR
) {
686 l
->DeleteContents(TRUE
);
688 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
695 void wxPyInputStream::seek(int offset
, int whence
) {
697 m_wxis
->SeekI(offset
, wxSeekMode(whence
));
700 int wxPyInputStream::tell(){
702 return m_wxis
->TellI();
709 wxPyCBInputStream::wxPyCBInputStream(PyObject
*r
, PyObject
*s
, PyObject
*t
, bool block
)
710 : wxInputStream(), m_read(r
), m_seek(s
), m_tell(t
), m_block(block
)
714 wxPyCBInputStream::~wxPyCBInputStream() {
715 if (m_block
) wxPyBeginBlockThreads();
719 if (m_block
) wxPyEndBlockThreads();
723 wxPyCBInputStream
* wxPyCBInputStream::create(PyObject
*py
, bool block
) {
724 if (block
) wxPyBeginBlockThreads();
726 PyObject
* read
= getMethod(py
, "read");
727 PyObject
* seek
= getMethod(py
, "seek");
728 PyObject
* tell
= getMethod(py
, "tell");
731 PyErr_SetString(PyExc_TypeError
, "Not a file-like object");
735 if (block
) wxPyEndBlockThreads();
739 if (block
) wxPyEndBlockThreads();
740 return new wxPyCBInputStream(read
, seek
, tell
, block
);
743 PyObject
* wxPyCBInputStream::getMethod(PyObject
* py
, char* name
) {
744 if (!PyObject_HasAttrString(py
, name
))
746 PyObject
* o
= PyObject_GetAttrString(py
, name
);
747 if (!PyMethod_Check(o
) && !PyCFunction_Check(o
)) {
755 size_t wxPyCBInputStream::GetSize() const {
756 wxPyCBInputStream
* self
= (wxPyCBInputStream
*)this; // cast off const
757 if (m_seek
&& m_tell
) {
758 off_t temp
= self
->OnSysTell();
759 off_t ret
= self
->OnSysSeek(0, wxFromEnd
);
760 self
->OnSysSeek(temp
, wxFromStart
);
768 size_t wxPyCBInputStream::OnSysRead(void *buffer
, size_t bufsize
) {
772 wxPyBeginBlockThreads();
773 PyObject
* arglist
= Py_BuildValue("(i)", bufsize
);
774 PyObject
* result
= PyEval_CallObject(m_read
, arglist
);
778 if ((result
!= NULL
) && PyString_Check(result
)) { // TODO: unicode?
779 o
= PyString_Size(result
);
781 m_lasterror
= wxSTREAM_EOF
;
784 memcpy((char*)buffer
, PyString_AsString(result
), o
);
789 m_lasterror
= wxSTREAM_READ_ERROR
;
790 wxPyEndBlockThreads();
795 size_t wxPyCBInputStream::OnSysWrite(const void *buffer
, size_t bufsize
) {
796 m_lasterror
= wxSTREAM_WRITE_ERROR
;
800 off_t
wxPyCBInputStream::OnSysSeek(off_t off
, wxSeekMode mode
) {
801 wxPyBeginBlockThreads();
802 PyObject
* arglist
= Py_BuildValue("(ii)", off
, mode
);
803 PyObject
* result
= PyEval_CallObject(m_seek
, arglist
);
806 wxPyEndBlockThreads();
810 off_t
wxPyCBInputStream::OnSysTell() const {
811 wxPyBeginBlockThreads();
812 PyObject
* arglist
= Py_BuildValue("()");
813 PyObject
* result
= PyEval_CallObject(m_tell
, arglist
);
816 if (result
!= NULL
) {
817 o
= PyInt_AsLong(result
);
820 wxPyEndBlockThreads();
824 //----------------------------------------------------------------------
826 IMPLEMENT_ABSTRACT_CLASS(wxPyCallback
, wxObject
);
828 wxPyCallback::wxPyCallback(PyObject
* func
) {
833 wxPyCallback::wxPyCallback(const wxPyCallback
& other
) {
834 m_func
= other
.m_func
;
838 wxPyCallback::~wxPyCallback() {
839 wxPyBeginBlockThreads();
841 wxPyEndBlockThreads();
846 // This function is used for all events destined for Python event handlers.
847 void wxPyCallback::EventThunker(wxEvent
& event
) {
848 wxPyCallback
* cb
= (wxPyCallback
*)event
.m_callbackUserData
;
849 PyObject
* func
= cb
->m_func
;
855 wxPyBeginBlockThreads();
856 wxString className
= event
.GetClassInfo()->GetClassName();
858 if (className
== "wxPyEvent")
859 arg
= ((wxPyEvent
*)&event
)->GetSelf();
860 else if (className
== "wxPyCommandEvent")
861 arg
= ((wxPyCommandEvent
*)&event
)->GetSelf();
863 arg
= wxPyConstructObject((void*)&event
, className
);
865 tuple
= PyTuple_New(1);
866 PyTuple_SET_ITEM(tuple
, 0, arg
);
867 result
= PyEval_CallObject(func
, tuple
);
871 PyErr_Clear(); // Just in case...
875 wxPyEndBlockThreads();
879 //----------------------------------------------------------------------
881 wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper
& other
) {
883 m_self
= other
.m_self
;
884 m_class
= other
.m_class
;
892 void wxPyCallbackHelper::setSelf(PyObject
* self
, PyObject
* klass
, int incref
) {
903 #if PYTHON_API_VERSION >= 1011
905 // Prior to Python 2.2 PyMethod_GetClass returned the class object
906 // in which the method was defined. Starting with 2.2 it returns
907 // "class that asked for the method" which seems totally bogus to me
908 // but apprently if fixes some obscure problem waiting to happen in
909 // Python. Since the API was not documented Guido and the gang felt
910 // safe in changing it. Needless to say that totally screwed up the
911 // logic below in wxPyCallbackHelper::findCallback, hence this icky
912 // code to find the class where the method is actuallt defined...
915 PyObject
* PyFindClassWithAttr(PyObject
*klass
, PyObject
*name
)
919 if (PyType_Check(klass
)) { // new style classes
920 // This code is borrowed/adapted from _PyType_Lookup in typeobject.c
921 // (TODO: This part is not tested yet, so I'm not sure it is correct...)
922 PyTypeObject
* type
= (PyTypeObject
*)klass
;
923 PyObject
*mro
, *res
, *base
, *dict
;
924 /* Look in tp_dict of types in MRO */
926 assert(PyTuple_Check(mro
));
927 n
= PyTuple_GET_SIZE(mro
);
928 for (i
= 0; i
< n
; i
++) {
929 base
= PyTuple_GET_ITEM(mro
, i
);
930 if (PyClass_Check(base
))
931 dict
= ((PyClassObject
*)base
)->cl_dict
;
933 assert(PyType_Check(base
));
934 dict
= ((PyTypeObject
*)base
)->tp_dict
;
936 assert(dict
&& PyDict_Check(dict
));
937 res
= PyDict_GetItem(dict
, name
);
944 else if (PyClass_Check(klass
)) { // old style classes
945 // This code is borrowed/adapted from class_lookup in classobject.c
946 PyClassObject
* cp
= (PyClassObject
*)klass
;
947 PyObject
*value
= PyDict_GetItem(cp
->cl_dict
, name
);
949 return (PyObject
*)cp
;
951 n
= PyTuple_Size(cp
->cl_bases
);
952 for (i
= 0; i
< n
; i
++) {
953 PyObject
* base
= PyTuple_GetItem(cp
->cl_bases
, i
);
954 PyObject
*v
= PyFindClassWithAttr(base
, name
);
965 PyObject
* PyMethod_GetDefiningClass(PyObject
* method
, const char* name
)
967 PyObject
* mgc
= PyMethod_GET_CLASS(method
);
969 #if PYTHON_API_VERSION <= 1010 // prior to Python 2.2, the easy way
971 #else // 2.2 and after, the hard way...
973 PyObject
* nameo
= PyString_FromString(name
);
974 PyObject
* klass
= PyFindClassWithAttr(mgc
, nameo
);
982 bool wxPyCallbackHelper::findCallback(const char* name
) const {
983 wxPyCallbackHelper
* self
= (wxPyCallbackHelper
*)this; // cast away const
984 self
->m_lastFound
= NULL
;
986 // If the object (m_self) has an attibute of the given name...
987 if (m_self
&& PyObject_HasAttrString(m_self
, (char*)name
)) {
988 PyObject
*method
, *klass
;
989 method
= PyObject_GetAttrString(m_self
, (char*)name
);
991 // ...and if that attribute is a method, and if that method's class is
992 // not from a base class...
993 if (PyMethod_Check(method
) &&
994 (klass
= PyMethod_GetDefiningClass(method
, (char*)name
)) != NULL
&&
995 ((klass
== m_class
) || PyClass_IsSubclass(klass
, m_class
))) {
997 // ...then we'll save a pointer to the method so callCallback can call it.
998 self
->m_lastFound
= method
;
1004 return m_lastFound
!= NULL
;
1008 int wxPyCallbackHelper::callCallback(PyObject
* argTuple
) const {
1012 result
= callCallbackObj(argTuple
);
1013 if (result
) { // Assumes an integer return type...
1014 retval
= PyInt_AsLong(result
);
1016 PyErr_Clear(); // forget about it if it's not...
1021 // Invoke the Python callable object, returning the raw PyObject return
1022 // value. Caller should DECREF the return value and also call PyEval_SaveThread.
1023 PyObject
* wxPyCallbackHelper::callCallbackObj(PyObject
* argTuple
) const {
1026 // Save a copy of the pointer in case the callback generates another
1027 // callback. In that case m_lastFound will have a different value when
1028 // it gets back here...
1029 PyObject
* method
= m_lastFound
;
1031 result
= PyEval_CallObject(method
, argTuple
);
1032 Py_DECREF(argTuple
);
1041 void wxPyCBH_setCallbackInfo(wxPyCallbackHelper
& cbh
, PyObject
* self
, PyObject
* klass
, int incref
) {
1042 cbh
.setSelf(self
, klass
, incref
);
1045 bool wxPyCBH_findCallback(const wxPyCallbackHelper
& cbh
, const char* name
) {
1046 return cbh
.findCallback(name
);
1049 int wxPyCBH_callCallback(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
1050 return cbh
.callCallback(argTuple
);
1053 PyObject
* wxPyCBH_callCallbackObj(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
1054 return cbh
.callCallbackObj(argTuple
);
1058 void wxPyCBH_delete(wxPyCallbackHelper
* cbh
) {
1059 if (cbh
->m_incRef
) {
1060 wxPyBeginBlockThreads();
1061 Py_XDECREF(cbh
->m_self
);
1062 Py_XDECREF(cbh
->m_class
);
1063 wxPyEndBlockThreads();
1067 //---------------------------------------------------------------------------
1068 //---------------------------------------------------------------------------
1069 // These event classes can be derived from in Python and passed through the event
1070 // system without losing anything. They do this by keeping a reference to
1071 // themselves and some special case handling in wxPyCallback::EventThunker.
1074 wxPyEvtSelfRef::wxPyEvtSelfRef() {
1075 //m_self = Py_None; // **** We don't do normal ref counting to prevent
1076 //Py_INCREF(m_self); // circular loops...
1080 wxPyEvtSelfRef::~wxPyEvtSelfRef() {
1081 wxPyBeginBlockThreads();
1084 wxPyEndBlockThreads();
1087 void wxPyEvtSelfRef::SetSelf(PyObject
* self
, bool clone
) {
1088 wxPyBeginBlockThreads();
1096 wxPyEndBlockThreads();
1099 PyObject
* wxPyEvtSelfRef::GetSelf() const {
1105 IMPLEMENT_ABSTRACT_CLASS(wxPyEvent
, wxEvent
);
1106 IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent
, wxCommandEvent
);
1109 wxPyEvent::wxPyEvent(int id
)
1114 wxPyEvent::wxPyEvent(const wxPyEvent
& evt
)
1117 SetSelf(evt
.m_self
, TRUE
);
1121 wxPyEvent::~wxPyEvent() {
1125 wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType
, int id
)
1126 : wxCommandEvent(commandType
, id
) {
1130 wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent
& evt
)
1131 : wxCommandEvent(evt
)
1133 SetSelf(evt
.m_self
, TRUE
);
1137 wxPyCommandEvent::~wxPyCommandEvent() {
1143 //---------------------------------------------------------------------------
1144 //---------------------------------------------------------------------------
1147 wxPyTimer::wxPyTimer(PyObject
* callback
) {
1152 wxPyTimer::~wxPyTimer() {
1153 wxPyBeginBlockThreads();
1155 wxPyEndBlockThreads();
1158 void wxPyTimer::Notify() {
1159 if (!func
|| func
== Py_None
) {
1163 wxPyBeginBlockThreads();
1166 PyObject
* args
= Py_BuildValue("()");
1168 result
= PyEval_CallObject(func
, args
);
1177 wxPyEndBlockThreads();
1183 //---------------------------------------------------------------------------
1184 //---------------------------------------------------------------------------
1185 // Convert a wxList to a Python List
1187 PyObject
* wxPy_ConvertList(wxListBase
* list
, const char* className
) {
1191 wxNode
* node
= list
->First();
1193 wxPyBeginBlockThreads();
1194 pyList
= PyList_New(0);
1196 wxObj
= node
->Data();
1197 pyObj
= wxPyMake_wxObject(wxObj
); //wxPyConstructObject(wxObj, className);
1198 PyList_Append(pyList
, pyObj
);
1199 node
= node
->Next();
1201 wxPyEndBlockThreads();
1205 //----------------------------------------------------------------------
1207 long wxPyGetWinHandle(wxWindow
* win
) {
1209 return (long)win
->GetHandle();
1212 // Find and return the actual X-Window.
1214 if (win
->m_wxwindow
) {
1215 GdkWindowPrivate
* bwin
= (GdkWindowPrivate
*)GTK_PIZZA(win
->m_wxwindow
)->bin_window
;
1217 return (long)bwin
->xwindow
;
1224 //----------------------------------------------------------------------
1225 // Some helper functions for typemaps in my_typemaps.i, so they won't be
1226 // included in every file...
1229 byte
* byte_LIST_helper(PyObject
* source
) {
1230 if (!PyList_Check(source
)) {
1231 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1234 int count
= PyList_Size(source
);
1235 byte
* temp
= new byte
[count
];
1237 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1240 for (int x
=0; x
<count
; x
++) {
1241 PyObject
* o
= PyList_GetItem(source
, x
);
1242 if (! PyInt_Check(o
)) {
1243 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1246 temp
[x
] = (byte
)PyInt_AsLong(o
);
1252 int* int_LIST_helper(PyObject
* source
) {
1253 if (!PyList_Check(source
)) {
1254 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1257 int count
= PyList_Size(source
);
1258 int* temp
= new int[count
];
1260 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1263 for (int x
=0; x
<count
; x
++) {
1264 PyObject
* o
= PyList_GetItem(source
, x
);
1265 if (! PyInt_Check(o
)) {
1266 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1269 temp
[x
] = PyInt_AsLong(o
);
1275 long* long_LIST_helper(PyObject
* source
) {
1276 if (!PyList_Check(source
)) {
1277 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1280 int count
= PyList_Size(source
);
1281 long* temp
= new long[count
];
1283 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1286 for (int x
=0; x
<count
; x
++) {
1287 PyObject
* o
= PyList_GetItem(source
, x
);
1288 if (! PyInt_Check(o
)) {
1289 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1292 temp
[x
] = PyInt_AsLong(o
);
1298 char** string_LIST_helper(PyObject
* source
) {
1299 if (!PyList_Check(source
)) {
1300 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1303 int count
= PyList_Size(source
);
1304 char** temp
= new char*[count
];
1306 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1309 for (int x
=0; x
<count
; x
++) {
1310 PyObject
* o
= PyList_GetItem(source
, x
);
1311 if (! PyString_Check(o
)) {
1312 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1315 temp
[x
] = PyString_AsString(o
);
1320 //--------------------------------
1321 // Part of patch from Tim Hochberg
1322 static inline bool wxPointFromObjects(PyObject
* o1
, PyObject
* o2
, wxPoint
* point
) {
1323 if (PyInt_Check(o1
) && PyInt_Check(o2
)) {
1324 point
->x
= PyInt_AS_LONG(o1
);
1325 point
->y
= PyInt_AS_LONG(o2
);
1328 if (PyFloat_Check(o1
) && PyFloat_Check(o2
)) {
1329 point
->x
= (int)PyFloat_AS_DOUBLE(o1
);
1330 point
->y
= (int)PyFloat_AS_DOUBLE(o2
);
1333 if (PyInstance_Check(o1
) || PyInstance_Check(o2
)) {
1334 // Disallow instances because they can cause havok
1337 if (PyNumber_Check(o1
) && PyNumber_Check(o2
)) {
1338 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
1339 point
->x
= PyInt_AsLong(o1
);
1340 point
->y
= PyInt_AsLong(o2
);
1347 wxPoint
* wxPoint_LIST_helper(PyObject
* source
, int *count
) {
1348 // Putting all of the declarations here allows
1349 // us to put the error handling all in one place.
1352 PyObject
*o
, *o1
, *o2
;
1353 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1355 if (!PySequence_Check(source
)) {
1359 // The length of the sequence is returned in count.
1360 *count
= PySequence_Length(source
);
1365 temp
= new wxPoint
[*count
];
1367 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1370 for (x
=0; x
<*count
; x
++) {
1371 // Get an item: try fast way first.
1373 o
= PySequence_Fast_GET_ITEM(source
, x
);
1376 o
= PySequence_GetItem(source
, x
);
1382 // Convert o to wxPoint.
1383 if ((PyTuple_Check(o
) && PyTuple_GET_SIZE(o
) == 2) ||
1384 (PyList_Check(o
) && PyList_GET_SIZE(o
) == 2)) {
1385 o1
= PySequence_Fast_GET_ITEM(o
, 0);
1386 o2
= PySequence_Fast_GET_ITEM(o
, 1);
1387 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
1391 else if (PyInstance_Check(o
)) {
1393 if (SWIG_GetPtrObj(o
, (void **)&pt
, "_wxPoint_p")) {
1398 else if (PySequence_Check(o
) && PySequence_Length(o
) == 2) {
1399 o1
= PySequence_GetItem(o
, 0);
1400 o2
= PySequence_GetItem(o
, 1);
1401 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
1425 PyErr_SetString(PyExc_TypeError
, "Expected a sequence of length-2 sequences or wxPoints.");
1429 //------------------------------
1432 wxBitmap
** wxBitmap_LIST_helper(PyObject
* source
) {
1433 if (!PyList_Check(source
)) {
1434 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1437 int count
= PyList_Size(source
);
1438 wxBitmap
** temp
= new wxBitmap
*[count
];
1440 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1443 for (int x
=0; x
<count
; x
++) {
1444 PyObject
* o
= PyList_GetItem(source
, x
);
1445 if (PyInstance_Check(o
)) {
1447 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxBitmap_p")) {
1448 PyErr_SetString(PyExc_TypeError
,"Expected _wxBitmap_p.");
1454 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxBitmaps.");
1463 wxString
* wxString_LIST_helper(PyObject
* source
) {
1464 if (!PyList_Check(source
)) {
1465 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1468 int count
= PyList_Size(source
);
1469 wxString
* temp
= new wxString
[count
];
1471 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1474 for (int x
=0; x
<count
; x
++) {
1475 PyObject
* o
= PyList_GetItem(source
, x
);
1476 #if PYTHON_API_VERSION >= 1009
1477 if (! PyString_Check(o
) && ! PyUnicode_Check(o
)) {
1478 PyErr_SetString(PyExc_TypeError
, "Expected a list of string or unicode objects.");
1484 if (PyString_AsStringAndSize(o
, &buff
, &length
) == -1)
1486 temp
[x
] = wxString(buff
, length
);
1488 if (! PyString_Check(o
)) {
1489 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1492 temp
[x
] = PyString_AsString(o
);
1499 wxAcceleratorEntry
* wxAcceleratorEntry_LIST_helper(PyObject
* source
) {
1500 if (!PyList_Check(source
)) {
1501 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1504 int count
= PyList_Size(source
);
1505 wxAcceleratorEntry
* temp
= new wxAcceleratorEntry
[count
];
1507 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1510 for (int x
=0; x
<count
; x
++) {
1511 PyObject
* o
= PyList_GetItem(source
, x
);
1512 if (PyInstance_Check(o
)) {
1513 wxAcceleratorEntry
* ae
;
1514 if (SWIG_GetPtrObj(o
, (void **) &ae
,"_wxAcceleratorEntry_p")) {
1515 PyErr_SetString(PyExc_TypeError
,"Expected _wxAcceleratorEntry_p.");
1520 else if (PyTuple_Check(o
)) {
1521 PyObject
* o1
= PyTuple_GetItem(o
, 0);
1522 PyObject
* o2
= PyTuple_GetItem(o
, 1);
1523 PyObject
* o3
= PyTuple_GetItem(o
, 2);
1524 temp
[x
].Set(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
));
1527 PyErr_SetString(PyExc_TypeError
, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
1535 wxPen
** wxPen_LIST_helper(PyObject
* source
) {
1536 if (!PyList_Check(source
)) {
1537 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1540 int count
= PyList_Size(source
);
1541 wxPen
** temp
= new wxPen
*[count
];
1543 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1546 for (int x
=0; x
<count
; x
++) {
1547 PyObject
* o
= PyList_GetItem(source
, x
);
1548 if (PyInstance_Check(o
)) {
1550 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxPen_p")) {
1552 PyErr_SetString(PyExc_TypeError
,"Expected _wxPen_p.");
1559 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxPens.");
1567 bool _2int_seq_helper(PyObject
* source
, int* i1
, int* i2
) {
1568 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1571 if (!PySequence_Check(source
) || PySequence_Length(source
) != 2)
1575 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1576 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1579 o1
= PySequence_GetItem(source
, 0);
1580 o2
= PySequence_GetItem(source
, 1);
1583 *i1
= PyInt_AsLong(o1
);
1584 *i2
= PyInt_AsLong(o2
);
1594 bool _4int_seq_helper(PyObject
* source
, int* i1
, int* i2
, int* i3
, int* i4
) {
1595 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1596 PyObject
*o1
, *o2
, *o3
, *o4
;
1598 if (!PySequence_Check(source
) || PySequence_Length(source
) != 4)
1602 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1603 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1604 o3
= PySequence_Fast_GET_ITEM(source
, 2);
1605 o4
= PySequence_Fast_GET_ITEM(source
, 3);
1608 o1
= PySequence_GetItem(source
, 0);
1609 o2
= PySequence_GetItem(source
, 1);
1610 o3
= PySequence_GetItem(source
, 2);
1611 o4
= PySequence_GetItem(source
, 3);
1614 *i1
= PyInt_AsLong(o1
);
1615 *i2
= PyInt_AsLong(o2
);
1616 *i3
= PyInt_AsLong(o3
);
1617 *i4
= PyInt_AsLong(o4
);
1629 //----------------------------------------------------------------------
1631 bool wxSize_helper(PyObject
* source
, wxSize
** obj
) {
1633 // If source is an object instance then it may already be the right type
1634 if (PyInstance_Check(source
)) {
1636 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxSize_p"))
1641 // otherwise a 2-tuple of integers is expected
1642 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1643 PyObject
* o1
= PySequence_GetItem(source
, 0);
1644 PyObject
* o2
= PySequence_GetItem(source
, 1);
1645 **obj
= wxSize(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1650 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxSize object.");
1654 bool wxPoint_helper(PyObject
* source
, wxPoint
** obj
) {
1656 // If source is an object instance then it may already be the right type
1657 if (PyInstance_Check(source
)) {
1659 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxPoint_p"))
1664 // otherwise a length-2 sequence of integers is expected
1665 if (PySequence_Check(source
) && PySequence_Length(source
) == 2) {
1666 PyObject
* o1
= PySequence_GetItem(source
, 0);
1667 PyObject
* o2
= PySequence_GetItem(source
, 1);
1668 // This should really check for integers, not numbers -- but that would break code.
1669 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1674 **obj
= wxPoint(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1680 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxPoint object.");
1686 bool wxRealPoint_helper(PyObject
* source
, wxRealPoint
** obj
) {
1688 // If source is an object instance then it may already be the right type
1689 if (PyInstance_Check(source
)) {
1691 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRealPoint_p"))
1696 // otherwise a 2-tuple of floats is expected
1697 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1698 PyObject
* o1
= PySequence_GetItem(source
, 0);
1699 PyObject
* o2
= PySequence_GetItem(source
, 1);
1700 **obj
= wxRealPoint(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
));
1705 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxRealPoint object.");
1712 bool wxRect_helper(PyObject
* source
, wxRect
** obj
) {
1714 // If source is an object instance then it may already be the right type
1715 if (PyInstance_Check(source
)) {
1717 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRect_p"))
1722 // otherwise a 4-tuple of integers is expected
1723 else if (PySequence_Check(source
) && PyObject_Length(source
) == 4) {
1724 PyObject
* o1
= PySequence_GetItem(source
, 0);
1725 PyObject
* o2
= PySequence_GetItem(source
, 1);
1726 PyObject
* o3
= PySequence_GetItem(source
, 2);
1727 PyObject
* o4
= PySequence_GetItem(source
, 3);
1728 **obj
= wxRect(PyInt_AsLong(o1
), PyInt_AsLong(o2
),
1729 PyInt_AsLong(o3
), PyInt_AsLong(o4
));
1734 PyErr_SetString(PyExc_TypeError
, "Expected a 4-tuple of integers or a wxRect object.");
1740 bool wxColour_helper(PyObject
* source
, wxColour
** obj
) {
1742 // If source is an object instance then it may already be the right type
1743 if (PyInstance_Check(source
)) {
1745 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxColour_p"))
1750 // otherwise a string is expected
1751 else if (PyString_Check(source
)) {
1752 wxString spec
= PyString_AS_STRING(source
);
1753 if (spec
[0U] == '#' && spec
.Length() == 7) { // It's #RRGGBB
1755 int red
= strtol(spec
.Mid(1,2), &junk
, 16);
1756 int green
= strtol(spec
.Mid(3,2), &junk
, 16);
1757 int blue
= strtol(spec
.Mid(5,2), &junk
, 16);
1758 **obj
= wxColour(red
, green
, blue
);
1761 else { // it's a colour name
1762 **obj
= wxColour(spec
);
1768 PyErr_SetString(PyExc_TypeError
, "Expected a wxColour object or a string containing a colour name or '#RRGGBB'.");
1773 //----------------------------------------------------------------------
1775 PyObject
* wxArrayString2PyList_helper(const wxArrayString
& arr
) {
1777 PyObject
* list
= PyList_New(0);
1778 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
1779 PyObject
* str
= PyString_FromString(arr
[i
].c_str());
1780 PyList_Append(list
, str
);
1787 PyObject
* wxArrayInt2PyList_helper(const wxArrayInt
& arr
) {
1789 PyObject
* list
= PyList_New(0);
1790 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
1791 PyObject
* number
= PyInt_FromLong(arr
[i
]);
1792 PyList_Append(list
, number
);
1799 //----------------------------------------------------------------------
1800 //----------------------------------------------------------------------