]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/src/helpers.cpp
f653ed1c417440f5e844318dda13919b4fa9d4d6
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 if 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 actuallt 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
);
966 PyObject
* PyMethod_GetDefiningClass(PyObject
* method
, const char* name
)
968 PyObject
* mgc
= PyMethod_GET_CLASS(method
);
970 #if PYTHON_API_VERSION <= 1010 // prior to Python 2.2, the easy way
972 #else // 2.2 and after, the hard way...
974 PyObject
* nameo
= PyString_FromString(name
);
975 PyObject
* klass
= PyFindClassWithAttr(mgc
, nameo
);
983 bool wxPyCallbackHelper::findCallback(const char* name
) const {
984 wxPyCallbackHelper
* self
= (wxPyCallbackHelper
*)this; // cast away const
985 self
->m_lastFound
= NULL
;
987 // If the object (m_self) has an attibute of the given name...
988 if (m_self
&& PyObject_HasAttrString(m_self
, (char*)name
)) {
989 PyObject
*method
, *klass
;
990 method
= PyObject_GetAttrString(m_self
, (char*)name
);
992 // ...and if that attribute is a method, and if that method's class is
993 // not from a base class...
994 if (PyMethod_Check(method
) &&
995 (klass
= PyMethod_GetDefiningClass(method
, (char*)name
)) != NULL
&&
996 ((klass
== m_class
) || PyClass_IsSubclass(klass
, m_class
))) {
998 // ...then we'll save a pointer to the method so callCallback can call it.
999 self
->m_lastFound
= method
;
1005 return m_lastFound
!= NULL
;
1009 int wxPyCallbackHelper::callCallback(PyObject
* argTuple
) const {
1013 result
= callCallbackObj(argTuple
);
1014 if (result
) { // Assumes an integer return type...
1015 retval
= PyInt_AsLong(result
);
1017 PyErr_Clear(); // forget about it if it's not...
1022 // Invoke the Python callable object, returning the raw PyObject return
1023 // value. Caller should DECREF the return value and also call PyEval_SaveThread.
1024 PyObject
* wxPyCallbackHelper::callCallbackObj(PyObject
* argTuple
) const {
1027 // Save a copy of the pointer in case the callback generates another
1028 // callback. In that case m_lastFound will have a different value when
1029 // it gets back here...
1030 PyObject
* method
= m_lastFound
;
1032 result
= PyEval_CallObject(method
, argTuple
);
1033 Py_DECREF(argTuple
);
1042 void wxPyCBH_setCallbackInfo(wxPyCallbackHelper
& cbh
, PyObject
* self
, PyObject
* klass
, int incref
) {
1043 cbh
.setSelf(self
, klass
, incref
);
1046 bool wxPyCBH_findCallback(const wxPyCallbackHelper
& cbh
, const char* name
) {
1047 return cbh
.findCallback(name
);
1050 int wxPyCBH_callCallback(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
1051 return cbh
.callCallback(argTuple
);
1054 PyObject
* wxPyCBH_callCallbackObj(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
1055 return cbh
.callCallbackObj(argTuple
);
1059 void wxPyCBH_delete(wxPyCallbackHelper
* cbh
) {
1060 if (cbh
->m_incRef
) {
1061 wxPyBeginBlockThreads();
1062 Py_XDECREF(cbh
->m_self
);
1063 Py_XDECREF(cbh
->m_class
);
1064 wxPyEndBlockThreads();
1068 //---------------------------------------------------------------------------
1069 //---------------------------------------------------------------------------
1070 // These event classes can be derived from in Python and passed through the event
1071 // system without losing anything. They do this by keeping a reference to
1072 // themselves and some special case handling in wxPyCallback::EventThunker.
1075 wxPyEvtSelfRef::wxPyEvtSelfRef() {
1076 //m_self = Py_None; // **** We don't do normal ref counting to prevent
1077 //Py_INCREF(m_self); // circular loops...
1081 wxPyEvtSelfRef::~wxPyEvtSelfRef() {
1082 wxPyBeginBlockThreads();
1085 wxPyEndBlockThreads();
1088 void wxPyEvtSelfRef::SetSelf(PyObject
* self
, bool clone
) {
1089 wxPyBeginBlockThreads();
1097 wxPyEndBlockThreads();
1100 PyObject
* wxPyEvtSelfRef::GetSelf() const {
1106 IMPLEMENT_ABSTRACT_CLASS(wxPyEvent
, wxEvent
);
1107 IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent
, wxCommandEvent
);
1110 wxPyEvent::wxPyEvent(int id
)
1115 wxPyEvent::wxPyEvent(const wxPyEvent
& evt
)
1118 SetSelf(evt
.m_self
, TRUE
);
1122 wxPyEvent::~wxPyEvent() {
1126 wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType
, int id
)
1127 : wxCommandEvent(commandType
, id
) {
1131 wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent
& evt
)
1132 : wxCommandEvent(evt
)
1134 SetSelf(evt
.m_self
, TRUE
);
1138 wxPyCommandEvent::~wxPyCommandEvent() {
1144 //---------------------------------------------------------------------------
1145 //---------------------------------------------------------------------------
1148 wxPyTimer::wxPyTimer(PyObject
* callback
) {
1153 wxPyTimer::~wxPyTimer() {
1154 wxPyBeginBlockThreads();
1156 wxPyEndBlockThreads();
1159 void wxPyTimer::Notify() {
1160 if (!func
|| func
== Py_None
) {
1164 wxPyBeginBlockThreads();
1167 PyObject
* args
= Py_BuildValue("()");
1169 result
= PyEval_CallObject(func
, args
);
1178 wxPyEndBlockThreads();
1184 //---------------------------------------------------------------------------
1185 //---------------------------------------------------------------------------
1186 // Convert a wxList to a Python List
1188 PyObject
* wxPy_ConvertList(wxListBase
* list
, const char* className
) {
1192 wxNode
* node
= list
->First();
1194 wxPyBeginBlockThreads();
1195 pyList
= PyList_New(0);
1197 wxObj
= node
->Data();
1198 pyObj
= wxPyMake_wxObject(wxObj
); //wxPyConstructObject(wxObj, className);
1199 PyList_Append(pyList
, pyObj
);
1200 node
= node
->Next();
1202 wxPyEndBlockThreads();
1206 //----------------------------------------------------------------------
1208 long wxPyGetWinHandle(wxWindow
* win
) {
1210 return (long)win
->GetHandle();
1213 // Find and return the actual X-Window.
1215 if (win
->m_wxwindow
) {
1216 GdkWindowPrivate
* bwin
= (GdkWindowPrivate
*)GTK_PIZZA(win
->m_wxwindow
)->bin_window
;
1218 return (long)bwin
->xwindow
;
1225 //----------------------------------------------------------------------
1226 // Some helper functions for typemaps in my_typemaps.i, so they won't be
1227 // included in every file...
1230 byte
* byte_LIST_helper(PyObject
* source
) {
1231 if (!PyList_Check(source
)) {
1232 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1235 int count
= PyList_Size(source
);
1236 byte
* temp
= new byte
[count
];
1238 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1241 for (int x
=0; x
<count
; x
++) {
1242 PyObject
* o
= PyList_GetItem(source
, x
);
1243 if (! PyInt_Check(o
)) {
1244 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1247 temp
[x
] = (byte
)PyInt_AsLong(o
);
1253 int* int_LIST_helper(PyObject
* source
) {
1254 if (!PyList_Check(source
)) {
1255 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1258 int count
= PyList_Size(source
);
1259 int* temp
= new int[count
];
1261 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1264 for (int x
=0; x
<count
; x
++) {
1265 PyObject
* o
= PyList_GetItem(source
, x
);
1266 if (! PyInt_Check(o
)) {
1267 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1270 temp
[x
] = PyInt_AsLong(o
);
1276 long* long_LIST_helper(PyObject
* source
) {
1277 if (!PyList_Check(source
)) {
1278 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1281 int count
= PyList_Size(source
);
1282 long* temp
= new long[count
];
1284 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1287 for (int x
=0; x
<count
; x
++) {
1288 PyObject
* o
= PyList_GetItem(source
, x
);
1289 if (! PyInt_Check(o
)) {
1290 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1293 temp
[x
] = PyInt_AsLong(o
);
1299 char** string_LIST_helper(PyObject
* source
) {
1300 if (!PyList_Check(source
)) {
1301 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1304 int count
= PyList_Size(source
);
1305 char** temp
= new char*[count
];
1307 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1310 for (int x
=0; x
<count
; x
++) {
1311 PyObject
* o
= PyList_GetItem(source
, x
);
1312 if (! PyString_Check(o
)) {
1313 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1316 temp
[x
] = PyString_AsString(o
);
1321 //--------------------------------
1322 // Part of patch from Tim Hochberg
1323 static inline bool wxPointFromObjects(PyObject
* o1
, PyObject
* o2
, wxPoint
* point
) {
1324 if (PyInt_Check(o1
) && PyInt_Check(o2
)) {
1325 point
->x
= PyInt_AS_LONG(o1
);
1326 point
->y
= PyInt_AS_LONG(o2
);
1329 if (PyFloat_Check(o1
) && PyFloat_Check(o2
)) {
1330 point
->x
= (int)PyFloat_AS_DOUBLE(o1
);
1331 point
->y
= (int)PyFloat_AS_DOUBLE(o2
);
1334 if (PyInstance_Check(o1
) || PyInstance_Check(o2
)) {
1335 // Disallow instances because they can cause havok
1338 if (PyNumber_Check(o1
) && PyNumber_Check(o2
)) {
1339 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
1340 point
->x
= PyInt_AsLong(o1
);
1341 point
->y
= PyInt_AsLong(o2
);
1348 wxPoint
* wxPoint_LIST_helper(PyObject
* source
, int *count
) {
1349 // Putting all of the declarations here allows
1350 // us to put the error handling all in one place.
1353 PyObject
*o
, *o1
, *o2
;
1354 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1356 if (!PySequence_Check(source
)) {
1360 // The length of the sequence is returned in count.
1361 *count
= PySequence_Length(source
);
1366 temp
= new wxPoint
[*count
];
1368 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1371 for (x
=0; x
<*count
; x
++) {
1372 // Get an item: try fast way first.
1374 o
= PySequence_Fast_GET_ITEM(source
, x
);
1377 o
= PySequence_GetItem(source
, x
);
1383 // Convert o to wxPoint.
1384 if ((PyTuple_Check(o
) && PyTuple_GET_SIZE(o
) == 2) ||
1385 (PyList_Check(o
) && PyList_GET_SIZE(o
) == 2)) {
1386 o1
= PySequence_Fast_GET_ITEM(o
, 0);
1387 o2
= PySequence_Fast_GET_ITEM(o
, 1);
1388 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
1392 else if (PyInstance_Check(o
)) {
1394 if (SWIG_GetPtrObj(o
, (void **)&pt
, "_wxPoint_p")) {
1399 else if (PySequence_Check(o
) && PySequence_Length(o
) == 2) {
1400 o1
= PySequence_GetItem(o
, 0);
1401 o2
= PySequence_GetItem(o
, 1);
1402 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
1426 PyErr_SetString(PyExc_TypeError
, "Expected a sequence of length-2 sequences or wxPoints.");
1430 //------------------------------
1433 wxBitmap
** wxBitmap_LIST_helper(PyObject
* source
) {
1434 if (!PyList_Check(source
)) {
1435 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1438 int count
= PyList_Size(source
);
1439 wxBitmap
** temp
= new wxBitmap
*[count
];
1441 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1444 for (int x
=0; x
<count
; x
++) {
1445 PyObject
* o
= PyList_GetItem(source
, x
);
1446 if (PyInstance_Check(o
)) {
1448 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxBitmap_p")) {
1449 PyErr_SetString(PyExc_TypeError
,"Expected _wxBitmap_p.");
1455 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxBitmaps.");
1464 wxString
* wxString_LIST_helper(PyObject
* source
) {
1465 if (!PyList_Check(source
)) {
1466 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1469 int count
= PyList_Size(source
);
1470 wxString
* temp
= new wxString
[count
];
1472 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1475 for (int x
=0; x
<count
; x
++) {
1476 PyObject
* o
= PyList_GetItem(source
, x
);
1477 #if PYTHON_API_VERSION >= 1009
1478 if (! PyString_Check(o
) && ! PyUnicode_Check(o
)) {
1479 PyErr_SetString(PyExc_TypeError
, "Expected a list of string or unicode objects.");
1485 if (PyString_AsStringAndSize(o
, &buff
, &length
) == -1)
1487 temp
[x
] = wxString(buff
, length
);
1489 if (! PyString_Check(o
)) {
1490 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1493 temp
[x
] = PyString_AsString(o
);
1500 wxAcceleratorEntry
* wxAcceleratorEntry_LIST_helper(PyObject
* source
) {
1501 if (!PyList_Check(source
)) {
1502 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1505 int count
= PyList_Size(source
);
1506 wxAcceleratorEntry
* temp
= new wxAcceleratorEntry
[count
];
1508 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1511 for (int x
=0; x
<count
; x
++) {
1512 PyObject
* o
= PyList_GetItem(source
, x
);
1513 if (PyInstance_Check(o
)) {
1514 wxAcceleratorEntry
* ae
;
1515 if (SWIG_GetPtrObj(o
, (void **) &ae
,"_wxAcceleratorEntry_p")) {
1516 PyErr_SetString(PyExc_TypeError
,"Expected _wxAcceleratorEntry_p.");
1521 else if (PyTuple_Check(o
)) {
1522 PyObject
* o1
= PyTuple_GetItem(o
, 0);
1523 PyObject
* o2
= PyTuple_GetItem(o
, 1);
1524 PyObject
* o3
= PyTuple_GetItem(o
, 2);
1525 temp
[x
].Set(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
));
1528 PyErr_SetString(PyExc_TypeError
, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
1536 wxPen
** wxPen_LIST_helper(PyObject
* source
) {
1537 if (!PyList_Check(source
)) {
1538 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1541 int count
= PyList_Size(source
);
1542 wxPen
** temp
= new wxPen
*[count
];
1544 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1547 for (int x
=0; x
<count
; x
++) {
1548 PyObject
* o
= PyList_GetItem(source
, x
);
1549 if (PyInstance_Check(o
)) {
1551 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxPen_p")) {
1553 PyErr_SetString(PyExc_TypeError
,"Expected _wxPen_p.");
1560 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxPens.");
1568 bool _2int_seq_helper(PyObject
* source
, int* i1
, int* i2
) {
1569 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1572 if (!PySequence_Check(source
) || PySequence_Length(source
) != 2)
1576 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1577 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1580 o1
= PySequence_GetItem(source
, 0);
1581 o2
= PySequence_GetItem(source
, 1);
1584 *i1
= PyInt_AsLong(o1
);
1585 *i2
= PyInt_AsLong(o2
);
1595 bool _4int_seq_helper(PyObject
* source
, int* i1
, int* i2
, int* i3
, int* i4
) {
1596 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1597 PyObject
*o1
, *o2
, *o3
, *o4
;
1599 if (!PySequence_Check(source
) || PySequence_Length(source
) != 4)
1603 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1604 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1605 o3
= PySequence_Fast_GET_ITEM(source
, 2);
1606 o4
= PySequence_Fast_GET_ITEM(source
, 3);
1609 o1
= PySequence_GetItem(source
, 0);
1610 o2
= PySequence_GetItem(source
, 1);
1611 o3
= PySequence_GetItem(source
, 2);
1612 o4
= PySequence_GetItem(source
, 3);
1615 *i1
= PyInt_AsLong(o1
);
1616 *i2
= PyInt_AsLong(o2
);
1617 *i3
= PyInt_AsLong(o3
);
1618 *i4
= PyInt_AsLong(o4
);
1630 //----------------------------------------------------------------------
1632 bool wxSize_helper(PyObject
* source
, wxSize
** obj
) {
1634 // If source is an object instance then it may already be the right type
1635 if (PyInstance_Check(source
)) {
1637 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxSize_p"))
1642 // otherwise a 2-tuple of integers is expected
1643 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1644 PyObject
* o1
= PySequence_GetItem(source
, 0);
1645 PyObject
* o2
= PySequence_GetItem(source
, 1);
1646 **obj
= wxSize(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1651 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxSize object.");
1655 bool wxPoint_helper(PyObject
* source
, wxPoint
** obj
) {
1657 // If source is an object instance then it may already be the right type
1658 if (PyInstance_Check(source
)) {
1660 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxPoint_p"))
1665 // otherwise a length-2 sequence of integers is expected
1666 if (PySequence_Check(source
) && PySequence_Length(source
) == 2) {
1667 PyObject
* o1
= PySequence_GetItem(source
, 0);
1668 PyObject
* o2
= PySequence_GetItem(source
, 1);
1669 // This should really check for integers, not numbers -- but that would break code.
1670 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1675 **obj
= wxPoint(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1681 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxPoint object.");
1687 bool wxRealPoint_helper(PyObject
* source
, wxRealPoint
** obj
) {
1689 // If source is an object instance then it may already be the right type
1690 if (PyInstance_Check(source
)) {
1692 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRealPoint_p"))
1697 // otherwise a 2-tuple of floats is expected
1698 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1699 PyObject
* o1
= PySequence_GetItem(source
, 0);
1700 PyObject
* o2
= PySequence_GetItem(source
, 1);
1701 **obj
= wxRealPoint(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
));
1706 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxRealPoint object.");
1713 bool wxRect_helper(PyObject
* source
, wxRect
** obj
) {
1715 // If source is an object instance then it may already be the right type
1716 if (PyInstance_Check(source
)) {
1718 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRect_p"))
1723 // otherwise a 4-tuple of integers is expected
1724 else if (PySequence_Check(source
) && PyObject_Length(source
) == 4) {
1725 PyObject
* o1
= PySequence_GetItem(source
, 0);
1726 PyObject
* o2
= PySequence_GetItem(source
, 1);
1727 PyObject
* o3
= PySequence_GetItem(source
, 2);
1728 PyObject
* o4
= PySequence_GetItem(source
, 3);
1729 **obj
= wxRect(PyInt_AsLong(o1
), PyInt_AsLong(o2
),
1730 PyInt_AsLong(o3
), PyInt_AsLong(o4
));
1735 PyErr_SetString(PyExc_TypeError
, "Expected a 4-tuple of integers or a wxRect object.");
1741 bool wxColour_helper(PyObject
* source
, wxColour
** obj
) {
1743 // If source is an object instance then it may already be the right type
1744 if (PyInstance_Check(source
)) {
1746 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxColour_p"))
1751 // otherwise a string is expected
1752 else if (PyString_Check(source
)) {
1753 wxString spec
= PyString_AS_STRING(source
);
1754 if (spec
[0U] == '#' && spec
.Length() == 7) { // It's #RRGGBB
1756 int red
= strtol(spec
.Mid(1,2), &junk
, 16);
1757 int green
= strtol(spec
.Mid(3,2), &junk
, 16);
1758 int blue
= strtol(spec
.Mid(5,2), &junk
, 16);
1759 **obj
= wxColour(red
, green
, blue
);
1762 else { // it's a colour name
1763 **obj
= wxColour(spec
);
1769 PyErr_SetString(PyExc_TypeError
, "Expected a wxColour object or a string containing a colour name or '#RRGGBB'.");
1774 //----------------------------------------------------------------------
1776 PyObject
* wxArrayString2PyList_helper(const wxArrayString
& arr
) {
1778 PyObject
* list
= PyList_New(0);
1779 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
1780 PyObject
* str
= PyString_FromString(arr
[i
].c_str());
1781 PyList_Append(list
, str
);
1788 PyObject
* wxArrayInt2PyList_helper(const wxArrayInt
& arr
) {
1790 PyObject
* list
= PyList_New(0);
1791 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
1792 PyObject
* number
= PyInt_FromLong(arr
[i
]);
1793 PyList_Append(list
, number
);
1800 //----------------------------------------------------------------------
1801 //----------------------------------------------------------------------