]>
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 /////////////////////////////////////////////////////////////////////////////
17 #include "pyistream.h"
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>
31 //----------------------------------------------------------------------
33 #if PYTHON_API_VERSION <= 1007 && wxUSE_UNICODE
34 #error Python must support Unicode to use wxWindows Unicode
37 //----------------------------------------------------------------------
40 int WXDLLEXPORT
wxEntryStart( int& argc
, char** argv
);
42 int WXDLLEXPORT
wxEntryStart( int argc
, char** argv
);
44 int WXDLLEXPORT
wxEntryInitGui();
45 void WXDLLEXPORT
wxEntryCleanup();
47 wxPyApp
* wxPythonApp
= NULL
; // Global instance of application object
48 bool wxPyDoCleanup
= FALSE
;
51 #ifdef WXP_WITH_THREAD
52 struct wxPyThreadState
{
54 PyThreadState
* tstate
;
56 wxPyThreadState(unsigned long _tid
=0, PyThreadState
* _tstate
=NULL
)
57 : tid(_tid
), tstate(_tstate
) {}
60 #include <wx/dynarray.h>
61 WX_DECLARE_OBJARRAY(wxPyThreadState
, wxPyThreadStateArray
);
62 #include <wx/arrimpl.cpp>
63 WX_DEFINE_OBJARRAY(wxPyThreadStateArray
);
65 wxPyThreadStateArray
* wxPyTStates
= NULL
;
66 wxMutex
* wxPyTMutex
= NULL
;
70 #ifdef __WXMSW__ // If building for win32...
71 //----------------------------------------------------------------------
72 // This gets run when the DLL is loaded. We just need to save a handle.
73 //----------------------------------------------------------------------
76 HINSTANCE hinstDLL
, // handle to DLL module
77 DWORD fdwReason
, // reason for calling function
78 LPVOID lpvReserved
// reserved
81 // If wxPython is embedded in another wxWindows app then
82 // the inatance has already been set.
83 if (! wxGetInstance())
84 wxSetInstance(hinstDLL
);
89 //----------------------------------------------------------------------
90 // Classes for implementing the wxp main application shell.
91 //----------------------------------------------------------------------
95 SetUseBestVisual(TRUE
);
102 // This one isn't acutally called... See __wxStart()
103 bool wxPyApp::OnInit() {
108 int wxPyApp::MainLoop() {
111 DeletePendingObjects();
112 bool initialized
= wxTopLevelWindows
.GetCount() != 0;
114 m_initialized
= initialized
;
118 retval
= wxApp::MainLoop();
126 //---------------------------------------------------------------------
127 //----------------------------------------------------------------------
130 static char* wxPyCopyCString(const wxChar
* src
)
132 wxWX2MBbuf buff
= (wxWX2MBbuf
)wxConvCurrent
->cWX2MB(src
);
133 size_t len
= strlen(buff
);
134 char* dest
= new char[len
+1];
140 static char* wxPyCopyCString(const char* src
) // we need a char version too
142 size_t len
= strlen(src
);
143 char* dest
= new char[len
+1];
149 static wxChar
* wxPyCopyWString(const char *src
)
151 //wxMB2WXbuf buff = wxConvCurrent->cMB2WX(src);
152 wxString
str(src
, *wxConvCurrent
);
153 return copystring(str
);
157 static wxChar
* wxPyCopyWString(const wxChar
*src
)
159 return copystring(src
);
164 //----------------------------------------------------------------------
166 // This is where we pick up the first part of the wxEntry functionality...
167 // The rest is in __wxStart and __wxCleanup. This function is called when
168 // wxcmodule is imported. (Before there is a wxApp object.)
173 // wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
176 #ifdef WXP_WITH_THREAD
177 PyEval_InitThreads();
178 wxPyTStates
= new wxPyThreadStateArray
;
179 wxPyTMutex
= new wxMutex
;
182 wxApp::CheckBuildOptions(wxBuildOptions());
184 // Bail out if there is already a wxApp created. This means that the
185 // toolkit has already been initialized, as in embedding wxPython in
186 // a C++ wxWindows app, so we don't need to call wxEntryStart.
187 if (wxTheApp
!= NULL
) {
190 wxPyDoCleanup
= TRUE
;
194 PyObject
* sysargv
= PySys_GetObject("argv");
195 if (sysargv
!= NULL
) {
196 argc
= PyList_Size(sysargv
);
197 argv
= new char*[argc
+1];
199 for(x
=0; x
<argc
; x
++) {
200 PyObject
*item
= PyList_GetItem(sysargv
, x
);
202 if (PyUnicode_Check(item
))
203 argv
[x
] = wxPyCopyCString(PyUnicode_AS_UNICODE(item
));
206 argv
[x
] = wxPyCopyCString(PyString_AsString(item
));
211 wxEntryStart(argc
, argv
);
217 // Start the user application, user App's OnInit method is a parameter here
218 PyObject
* __wxStart(PyObject
* /* self */, PyObject
* args
)
220 PyObject
* onInitFunc
= NULL
;
225 if (!PyArg_ParseTuple(args
, "O", &onInitFunc
))
228 // This is the next part of the wxEntry functionality...
230 wxChar
** argv
= NULL
;
231 PyObject
* sysargv
= PySys_GetObject("argv");
232 if (sysargv
!= NULL
) {
233 argc
= PyList_Size(sysargv
);
234 argv
= new wxChar
*[argc
+1];
236 for(x
=0; x
<argc
; x
++) {
237 PyObject
*pyArg
= PyList_GetItem(sysargv
, x
);
239 if (PyUnicode_Check(pyArg
))
240 argv
[x
] = wxPyCopyWString(PyUnicode_AS_UNICODE(pyArg
));
243 argv
[x
] = wxPyCopyWString(PyString_AsString(pyArg
));
248 wxPythonApp
->argc
= argc
;
249 wxPythonApp
->argv
= argv
;
253 // Call the Python App's OnInit function
254 arglist
= PyTuple_New(0);
255 result
= PyEval_CallObject(onInitFunc
, arglist
);
256 if (!result
) { // an exception was raised.
260 if (! PyInt_Check(result
)) {
261 PyErr_SetString(PyExc_TypeError
, "OnInit should return a boolean value");
264 bResult
= PyInt_AS_LONG(result
);
266 PyErr_SetString(PyExc_SystemExit
, "OnInit returned FALSE, exiting...");
271 wxTheApp
->m_initialized
= (wxTopLevelWindows
.GetCount() > 0);
282 #ifdef WXP_WITH_THREAD
285 wxPyTStates
->Empty();
293 static PyObject
* wxPython_dict
= NULL
;
294 static PyObject
* wxPyPtrTypeMap
= NULL
;
297 PyObject
* __wxSetDictionary(PyObject
* /* self */, PyObject
* args
)
300 if (!PyArg_ParseTuple(args
, "O", &wxPython_dict
))
303 if (!PyDict_Check(wxPython_dict
)) {
304 PyErr_SetString(PyExc_TypeError
, "_wxSetDictionary must have dictionary object!");
308 if (! wxPyPtrTypeMap
)
309 wxPyPtrTypeMap
= PyDict_New();
310 PyDict_SetItemString(wxPython_dict
, "__wxPyPtrTypeMap", wxPyPtrTypeMap
);
314 #define wxPlatform "__WXMOTIF__"
317 #define wxPlatform "__WXX11__"
320 #define wxPlatform "__WXGTK__"
322 #if defined(__WIN32__) || defined(__WXMSW__)
323 #define wxPlatform "__WXMSW__"
326 #define wxPlatform "__WXMAC__"
329 PyDict_SetItemString(wxPython_dict
, "wxPlatform", PyString_FromString(wxPlatform
));
330 PyDict_SetItemString(wxPython_dict
, "wxUSE_UNICODE", PyInt_FromLong(wxUSE_UNICODE
));
336 //---------------------------------------------------------------------------
338 void wxPyClientData_dtor(wxPyClientData
* self
) {
339 wxPyBeginBlockThreads();
340 Py_DECREF(self
->m_obj
);
341 wxPyEndBlockThreads();
344 void wxPyUserData_dtor(wxPyUserData
* self
) {
345 wxPyBeginBlockThreads();
346 Py_DECREF(self
->m_obj
);
347 wxPyEndBlockThreads();
351 // This is called when an OOR controled object is being destroyed. Although
352 // the C++ object is going away there is no way to force the Python object
353 // (and all references to it) to die too. This causes problems (crashes) in
354 // wxPython when a python shadow object attempts to call a C++ method using
355 // the now bogus pointer... So to try and prevent this we'll do a little black
356 // magic and change the class of the python instance to a class that will
357 // raise an exception for any attempt to call methods with it. See
358 // _wxPyDeadObject in _extras.py for the implementation of this class.
359 void wxPyOORClientData_dtor(wxPyOORClientData
* self
) {
361 static PyObject
* deadObjectClass
= NULL
;
363 wxPyBeginBlockThreads();
364 if (deadObjectClass
== NULL
) {
365 deadObjectClass
= PyDict_GetItemString(wxPython_dict
, "_wxPyDeadObject");
366 wxASSERT_MSG(deadObjectClass
!= NULL
, wxT("Can't get _wxPyDeadObject class!"));
367 Py_INCREF(deadObjectClass
);
370 // Clear the instance's dictionary, put the name of the old class into the
371 // instance, and then reset the class to be the dead class.
372 if (self
->m_obj
->ob_refcnt
> 1) { // but only if there is more than one reference
373 wxASSERT_MSG(PyInstance_Check(self
->m_obj
), wxT("m_obj not an instance!?!?!"));
374 PyInstanceObject
* inst
= (PyInstanceObject
*)self
->m_obj
;
375 PyDict_Clear(inst
->in_dict
);
376 PyDict_SetItemString(inst
->in_dict
, "_name", inst
->in_class
->cl_name
);
377 inst
->in_class
= (PyClassObject
*)deadObjectClass
;
378 Py_INCREF(deadObjectClass
);
380 wxPyEndBlockThreads();
383 //---------------------------------------------------------------------------
384 // Stuff used by OOR to find the right wxPython class type to return and to
388 // The pointer type map is used when the "pointer" type name generated by SWIG
389 // is not the same as the shadow class name, for example wxPyTreeCtrl
390 // vs. wxTreeCtrl. It needs to be referenced in Python as well as from C++,
391 // so we'll just make it a Python dictionary in the wx module's namespace.
392 // (See __wxSetDictionary)
393 void wxPyPtrTypeMap_Add(const char* commonName
, const char* ptrName
) {
394 if (! wxPyPtrTypeMap
)
395 wxPyPtrTypeMap
= PyDict_New();
396 PyDict_SetItemString(wxPyPtrTypeMap
,
398 PyString_FromString((char*)ptrName
));
403 PyObject
* wxPyClassExists(const wxString
& className
) {
408 char buff
[64]; // should always be big enough...
410 sprintf(buff
, "%sPtr", className
.mbc_str());
411 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
413 return classobj
; // returns NULL if not found
417 PyObject
* wxPyMake_wxObject(wxObject
* source
, bool checkEvtHandler
) {
418 PyObject
* target
= NULL
;
419 bool isEvtHandler
= FALSE
;
422 // If it's derived from wxEvtHandler then there may
423 // already be a pointer to a Python object that we can use
425 if (checkEvtHandler
&& wxIsKindOf(source
, wxEvtHandler
)) {
427 wxEvtHandler
* eh
= (wxEvtHandler
*)source
;
428 wxPyOORClientData
* data
= (wxPyOORClientData
*)eh
->GetClientObject();
430 target
= data
->m_obj
;
436 // Otherwise make it the old fashioned way by making a
437 // new shadow object and putting this pointer in it.
438 wxClassInfo
* info
= source
->GetClassInfo();
439 wxChar
* name
= (wxChar
*)info
->GetClassName();
440 PyObject
* klass
= wxPyClassExists(name
);
441 while (info
&& !klass
) {
442 name
= (wxChar
*)info
->GetBaseClassName1();
443 info
= wxClassInfo::FindClass(name
);
444 klass
= wxPyClassExists(name
);
447 target
= wxPyConstructObject(source
, name
, klass
, FALSE
);
448 if (target
&& isEvtHandler
)
449 ((wxEvtHandler
*)source
)->SetClientObject(new wxPyOORClientData(target
));
451 wxString
msg(wxT("wxPython class not found for "));
452 msg
+= source
->GetClassInfo()->GetClassName();
453 PyErr_SetString(PyExc_NameError
, msg
.mbc_str());
457 } else { // source was NULL so return None.
458 Py_INCREF(Py_None
); target
= Py_None
;
464 PyObject
* wxPyMake_wxSizer(wxSizer
* source
) {
465 PyObject
* target
= NULL
;
467 if (source
&& wxIsKindOf(source
, wxSizer
)) {
468 // If it's derived from wxSizer then there may
469 // already be a pointer to a Python object that we can use
471 wxSizer
* sz
= (wxSizer
*)source
;
472 wxPyOORClientData
* data
= (wxPyOORClientData
*)sz
->GetClientObject();
474 target
= data
->m_obj
;
479 target
= wxPyMake_wxObject(source
, FALSE
);
480 if (target
!= Py_None
)
481 ((wxSizer
*)source
)->SetClientObject(new wxPyOORClientData(target
));
488 //---------------------------------------------------------------------------
490 PyObject
* wxPyConstructObject(void* ptr
,
491 const wxString
& className
,
498 wxString
name(className
);
499 char swigptr
[64]; // should always be big enough...
502 if ((item
= PyDict_GetItemString(wxPyPtrTypeMap
, (char*)(const char*)name
.mbc_str())) != NULL
) {
503 name
= wxString(PyString_AsString(item
), *wxConvCurrent
);
505 sprintf(buff
, "_%s_p", (const char*)name
.mbc_str());
506 SWIG_MakePtr(swigptr
, ptr
, buff
);
508 arg
= Py_BuildValue("(s)", swigptr
);
509 obj
= PyInstance_New(klass
, arg
, NULL
);
513 PyObject
* one
= PyInt_FromLong(1);
514 PyObject_SetAttrString(obj
, "thisown", one
);
522 PyObject
* wxPyConstructObject(void* ptr
,
523 const wxString
& className
,
530 char buff
[64]; // should always be big enough...
531 sprintf(buff
, "%sPtr", (const char*)className
.mbc_str());
533 wxASSERT_MSG(wxPython_dict
, wxT("wxPython_dict is not set yet!!"));
535 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
537 wxString
msg(wxT("wxPython class not found for "));
539 PyErr_SetString(PyExc_NameError
, msg
.mbc_str());
543 return wxPyConstructObject(ptr
, className
, classobj
, setThisOwn
);
547 //---------------------------------------------------------------------------
550 #ifdef WXP_WITH_THREAD
552 unsigned long wxPyGetCurrentThreadId() {
553 return wxThread::GetCurrentId();
556 static PyThreadState
* gs_shutdownTState
;
558 PyThreadState
* wxPyGetThreadState() {
559 if (wxPyTMutex
== NULL
) // Python is shutting down...
560 return gs_shutdownTState
;
562 unsigned long ctid
= wxPyGetCurrentThreadId();
563 PyThreadState
* tstate
= NULL
;
566 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
567 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
568 if (info
.tid
== ctid
) {
569 tstate
= info
.tstate
;
573 wxPyTMutex
->Unlock();
574 wxASSERT_MSG(tstate
, wxT("PyThreadState should not be NULL!"));
579 void wxPySaveThreadState(PyThreadState
* tstate
) {
580 if (wxPyTMutex
== NULL
) { // Python is shutting down, assume a single thread...
581 gs_shutdownTState
= tstate
;
584 unsigned long ctid
= wxPyGetCurrentThreadId();
586 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
587 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
588 if (info
.tid
== ctid
) {
590 if (info
.tstate
!= tstate
)
591 wxLogMessage("*** tstate mismatch!???");
593 // info.tstate = tstate; *** DO NOT update existing ones???
594 // Normally it will never change, but apparently COM callbacks
595 // (i.e. ActiveX controls) will (incorrectly IMHO) use a transient
596 // tstate which will then be garbage the next time we try to use
598 wxPyTMutex
->Unlock();
602 // not found, so add it...
603 wxPyTStates
->Add(new wxPyThreadState(ctid
, tstate
));
604 wxPyTMutex
->Unlock();
610 // Calls from Python to wxWindows code are wrapped in calls to these
613 PyThreadState
* wxPyBeginAllowThreads() {
614 #ifdef WXP_WITH_THREAD
615 PyThreadState
* saved
= PyEval_SaveThread(); // Py_BEGIN_ALLOW_THREADS;
616 wxPySaveThreadState(saved
);
623 void wxPyEndAllowThreads(PyThreadState
* saved
) {
624 #ifdef WXP_WITH_THREAD
625 PyEval_RestoreThread(saved
); // Py_END_ALLOW_THREADS;
631 // Calls from wxWindows back to Python code, or even any PyObject
632 // manipulations, PyDECREF's and etc. are wrapped in calls to these functions:
634 void wxPyBeginBlockThreads() {
635 #ifdef WXP_WITH_THREAD
636 PyThreadState
* tstate
= wxPyGetThreadState();
637 PyEval_RestoreThread(tstate
);
642 void wxPyEndBlockThreads() {
643 #ifdef WXP_WITH_THREAD
644 // Is there any need to save it again?
645 // PyThreadState* tstate =
651 //---------------------------------------------------------------------------
652 // wxPyInputStream and wxPyCBInputStream methods
655 void wxPyInputStream::close() {
656 /* do nothing for now */
659 void wxPyInputStream::flush() {
660 /* do nothing for now */
663 bool wxPyInputStream::eof() {
665 return m_wxis
->Eof();
670 wxPyInputStream::~wxPyInputStream() {
677 PyObject
* wxPyInputStream::read(int size
) {
678 PyObject
* obj
= NULL
;
680 const int BUFSIZE
= 1024;
682 // check if we have a real wxInputStream to work with
684 PyErr_SetString(PyExc_IOError
, "no valid C-wxInputStream");
690 while (! m_wxis
->Eof()) {
691 m_wxis
->Read(buf
.GetAppendBuf(BUFSIZE
), BUFSIZE
);
692 buf
.UngetAppendBuf(m_wxis
->LastRead());
695 } else { // Read only size number of characters
696 m_wxis
->Read(buf
.GetWriteBuf(size
), size
);
697 buf
.UngetWriteBuf(m_wxis
->LastRead());
701 if (m_wxis
->LastError() == wxSTREAM_READ_ERROR
) {
702 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
705 // We use only strings for the streams, not unicode
706 obj
= PyString_FromStringAndSize(buf
, buf
.GetDataLen());
712 PyObject
* wxPyInputStream::readline(int size
) {
713 PyObject
* obj
= NULL
;
718 // check if we have a real wxInputStream to work with
720 PyErr_SetString(PyExc_IOError
,"no valid C-wxInputStream");
724 // read until \n or byte limit reached
725 for (i
=ch
=0; (ch
!= '\n') && (!m_wxis
->Eof()) && ((size
< 0) || (i
< size
)); i
++) {
731 if (m_wxis
->LastError() == wxSTREAM_READ_ERROR
) {
732 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
735 // We use only strings for the streams, not unicode
736 obj
= PyString_FromStringAndSize((char*)buf
.GetData(), buf
.GetDataLen());
742 PyObject
* wxPyInputStream::readlines(int sizehint
) {
745 // check if we have a real wxInputStream to work with
747 PyErr_SetString(PyExc_IOError
,"no valid C-wxInputStream below");
752 pylist
= PyList_New(0);
758 // read sizehint bytes or until EOF
760 for (i
=0; (!m_wxis
->Eof()) && ((sizehint
< 0) || (i
< sizehint
));) {
761 PyObject
* s
= this->readline();
766 PyList_Append(pylist
, s
);
767 i
+= PyString_Size(s
);
771 if (m_wxis
->LastError() == wxSTREAM_READ_ERROR
) {
773 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
781 void wxPyInputStream::seek(int offset
, int whence
) {
783 m_wxis
->SeekI(offset
, wxSeekMode(whence
));
786 int wxPyInputStream::tell(){
788 return m_wxis
->TellI();
795 wxPyCBInputStream::wxPyCBInputStream(PyObject
*r
, PyObject
*s
, PyObject
*t
, bool block
)
796 : wxInputStream(), m_read(r
), m_seek(s
), m_tell(t
), m_block(block
)
800 wxPyCBInputStream::~wxPyCBInputStream() {
801 if (m_block
) wxPyBeginBlockThreads();
805 if (m_block
) wxPyEndBlockThreads();
809 wxPyCBInputStream
* wxPyCBInputStream::create(PyObject
*py
, bool block
) {
810 if (block
) wxPyBeginBlockThreads();
812 PyObject
* read
= getMethod(py
, "read");
813 PyObject
* seek
= getMethod(py
, "seek");
814 PyObject
* tell
= getMethod(py
, "tell");
817 PyErr_SetString(PyExc_TypeError
, "Not a file-like object");
821 if (block
) wxPyEndBlockThreads();
825 if (block
) wxPyEndBlockThreads();
826 return new wxPyCBInputStream(read
, seek
, tell
, block
);
829 PyObject
* wxPyCBInputStream::getMethod(PyObject
* py
, char* name
) {
830 if (!PyObject_HasAttrString(py
, name
))
832 PyObject
* o
= PyObject_GetAttrString(py
, name
);
833 if (!PyMethod_Check(o
) && !PyCFunction_Check(o
)) {
841 size_t wxPyCBInputStream::GetSize() const {
842 wxPyCBInputStream
* self
= (wxPyCBInputStream
*)this; // cast off const
843 if (m_seek
&& m_tell
) {
844 off_t temp
= self
->OnSysTell();
845 off_t ret
= self
->OnSysSeek(0, wxFromEnd
);
846 self
->OnSysSeek(temp
, wxFromStart
);
854 size_t wxPyCBInputStream::OnSysRead(void *buffer
, size_t bufsize
) {
858 wxPyBeginBlockThreads();
859 PyObject
* arglist
= Py_BuildValue("(i)", bufsize
);
860 PyObject
* result
= PyEval_CallObject(m_read
, arglist
);
864 if ((result
!= NULL
) && PyString_Check(result
)) {
865 o
= PyString_Size(result
);
867 m_lasterror
= wxSTREAM_EOF
;
870 memcpy((char*)buffer
, PyString_AsString(result
), o
); // strings only, not unicode...
875 m_lasterror
= wxSTREAM_READ_ERROR
;
876 wxPyEndBlockThreads();
881 size_t wxPyCBInputStream::OnSysWrite(const void *buffer
, size_t bufsize
) {
882 m_lasterror
= wxSTREAM_WRITE_ERROR
;
886 off_t
wxPyCBInputStream::OnSysSeek(off_t off
, wxSeekMode mode
) {
887 wxPyBeginBlockThreads();
888 PyObject
* arglist
= Py_BuildValue("(ii)", off
, mode
);
889 PyObject
* result
= PyEval_CallObject(m_seek
, arglist
);
892 wxPyEndBlockThreads();
896 off_t
wxPyCBInputStream::OnSysTell() const {
897 wxPyBeginBlockThreads();
898 PyObject
* arglist
= Py_BuildValue("()");
899 PyObject
* result
= PyEval_CallObject(m_tell
, arglist
);
902 if (result
!= NULL
) {
903 o
= PyInt_AsLong(result
);
906 wxPyEndBlockThreads();
910 //----------------------------------------------------------------------
912 IMPLEMENT_ABSTRACT_CLASS(wxPyCallback
, wxObject
);
914 wxPyCallback::wxPyCallback(PyObject
* func
) {
919 wxPyCallback::wxPyCallback(const wxPyCallback
& other
) {
920 m_func
= other
.m_func
;
924 wxPyCallback::~wxPyCallback() {
925 wxPyBeginBlockThreads();
927 wxPyEndBlockThreads();
932 // This function is used for all events destined for Python event handlers.
933 void wxPyCallback::EventThunker(wxEvent
& event
) {
934 wxPyCallback
* cb
= (wxPyCallback
*)event
.m_callbackUserData
;
935 PyObject
* func
= cb
->m_func
;
941 wxPyBeginBlockThreads();
942 wxString className
= event
.GetClassInfo()->GetClassName();
944 if (className
== "wxPyEvent")
945 arg
= ((wxPyEvent
*)&event
)->GetSelf();
946 else if (className
== "wxPyCommandEvent")
947 arg
= ((wxPyCommandEvent
*)&event
)->GetSelf();
949 arg
= wxPyConstructObject((void*)&event
, className
);
952 tuple
= PyTuple_New(1);
953 PyTuple_SET_ITEM(tuple
, 0, arg
);
954 result
= PyEval_CallObject(func
, tuple
);
958 PyErr_Clear(); // Just in case...
962 wxPyEndBlockThreads();
966 //----------------------------------------------------------------------
968 wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper
& other
) {
970 m_self
= other
.m_self
;
971 m_class
= other
.m_class
;
979 void wxPyCallbackHelper::setSelf(PyObject
* self
, PyObject
* klass
, int incref
) {
990 #if PYTHON_API_VERSION >= 1011
992 // Prior to Python 2.2 PyMethod_GetClass returned the class object
993 // in which the method was defined. Starting with 2.2 it returns
994 // "class that asked for the method" which seems totally bogus to me
995 // but apprently it fixes some obscure problem waiting to happen in
996 // Python. Since the API was not documented Guido and the gang felt
997 // safe in changing it. Needless to say that totally screwed up the
998 // logic below in wxPyCallbackHelper::findCallback, hence this icky
999 // code to find the class where the method is actually defined...
1002 PyObject
* PyFindClassWithAttr(PyObject
*klass
, PyObject
*name
)
1006 if (PyType_Check(klass
)) { // new style classes
1007 // This code is borrowed/adapted from _PyType_Lookup in typeobject.c
1008 // (TODO: This part is not tested yet, so I'm not sure it is correct...)
1009 PyTypeObject
* type
= (PyTypeObject
*)klass
;
1010 PyObject
*mro
, *res
, *base
, *dict
;
1011 /* Look in tp_dict of types in MRO */
1013 assert(PyTuple_Check(mro
));
1014 n
= PyTuple_GET_SIZE(mro
);
1015 for (i
= 0; i
< n
; i
++) {
1016 base
= PyTuple_GET_ITEM(mro
, i
);
1017 if (PyClass_Check(base
))
1018 dict
= ((PyClassObject
*)base
)->cl_dict
;
1020 assert(PyType_Check(base
));
1021 dict
= ((PyTypeObject
*)base
)->tp_dict
;
1023 assert(dict
&& PyDict_Check(dict
));
1024 res
= PyDict_GetItem(dict
, name
);
1031 else if (PyClass_Check(klass
)) { // old style classes
1032 // This code is borrowed/adapted from class_lookup in classobject.c
1033 PyClassObject
* cp
= (PyClassObject
*)klass
;
1034 PyObject
*value
= PyDict_GetItem(cp
->cl_dict
, name
);
1035 if (value
!= NULL
) {
1036 return (PyObject
*)cp
;
1038 n
= PyTuple_Size(cp
->cl_bases
);
1039 for (i
= 0; i
< n
; i
++) {
1040 PyObject
* base
= PyTuple_GetItem(cp
->cl_bases
, i
);
1041 PyObject
*v
= PyFindClassWithAttr(base
, name
);
1053 PyObject
* PyMethod_GetDefiningClass(PyObject
* method
, const char* name
)
1055 PyObject
* mgc
= PyMethod_GET_CLASS(method
);
1057 #if PYTHON_API_VERSION <= 1010 // prior to Python 2.2, the easy way
1059 #else // 2.2 and after, the hard way...
1061 PyObject
* nameo
= PyString_FromString(name
);
1062 PyObject
* klass
= PyFindClassWithAttr(mgc
, nameo
);
1070 bool wxPyCallbackHelper::findCallback(const char* name
) const {
1071 wxPyCallbackHelper
* self
= (wxPyCallbackHelper
*)this; // cast away const
1072 self
->m_lastFound
= NULL
;
1074 // If the object (m_self) has an attibute of the given name...
1075 if (m_self
&& PyObject_HasAttrString(m_self
, (char*)name
)) {
1076 PyObject
*method
, *klass
;
1077 method
= PyObject_GetAttrString(m_self
, (char*)name
);
1079 // ...and if that attribute is a method, and if that method's class is
1080 // not from a base class...
1081 if (PyMethod_Check(method
) &&
1082 (klass
= PyMethod_GetDefiningClass(method
, (char*)name
)) != NULL
&&
1083 ((klass
== m_class
) || PyClass_IsSubclass(klass
, m_class
))) {
1085 // ...then we'll save a pointer to the method so callCallback can call it.
1086 self
->m_lastFound
= method
;
1092 return m_lastFound
!= NULL
;
1096 int wxPyCallbackHelper::callCallback(PyObject
* argTuple
) const {
1100 result
= callCallbackObj(argTuple
);
1101 if (result
) { // Assumes an integer return type...
1102 retval
= PyInt_AsLong(result
);
1104 PyErr_Clear(); // forget about it if it's not...
1109 // Invoke the Python callable object, returning the raw PyObject return
1110 // value. Caller should DECREF the return value and also call PyEval_SaveThread.
1111 PyObject
* wxPyCallbackHelper::callCallbackObj(PyObject
* argTuple
) const {
1114 // Save a copy of the pointer in case the callback generates another
1115 // callback. In that case m_lastFound will have a different value when
1116 // it gets back here...
1117 PyObject
* method
= m_lastFound
;
1119 result
= PyEval_CallObject(method
, argTuple
);
1120 Py_DECREF(argTuple
);
1129 void wxPyCBH_setCallbackInfo(wxPyCallbackHelper
& cbh
, PyObject
* self
, PyObject
* klass
, int incref
) {
1130 cbh
.setSelf(self
, klass
, incref
);
1133 bool wxPyCBH_findCallback(const wxPyCallbackHelper
& cbh
, const char* name
) {
1134 return cbh
.findCallback(name
);
1137 int wxPyCBH_callCallback(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
1138 return cbh
.callCallback(argTuple
);
1141 PyObject
* wxPyCBH_callCallbackObj(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
1142 return cbh
.callCallbackObj(argTuple
);
1146 void wxPyCBH_delete(wxPyCallbackHelper
* cbh
) {
1147 if (cbh
->m_incRef
) {
1148 wxPyBeginBlockThreads();
1149 Py_XDECREF(cbh
->m_self
);
1150 Py_XDECREF(cbh
->m_class
);
1151 wxPyEndBlockThreads();
1155 //---------------------------------------------------------------------------
1156 //---------------------------------------------------------------------------
1157 // These event classes can be derived from in Python and passed through the event
1158 // system without losing anything. They do this by keeping a reference to
1159 // themselves and some special case handling in wxPyCallback::EventThunker.
1162 wxPyEvtSelfRef::wxPyEvtSelfRef() {
1163 //m_self = Py_None; // **** We don't do normal ref counting to prevent
1164 //Py_INCREF(m_self); // circular loops...
1168 wxPyEvtSelfRef::~wxPyEvtSelfRef() {
1169 wxPyBeginBlockThreads();
1172 wxPyEndBlockThreads();
1175 void wxPyEvtSelfRef::SetSelf(PyObject
* self
, bool clone
) {
1176 wxPyBeginBlockThreads();
1184 wxPyEndBlockThreads();
1187 PyObject
* wxPyEvtSelfRef::GetSelf() const {
1193 IMPLEMENT_ABSTRACT_CLASS(wxPyEvent
, wxEvent
);
1194 IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent
, wxCommandEvent
);
1197 wxPyEvent::wxPyEvent(int id
)
1202 wxPyEvent::wxPyEvent(const wxPyEvent
& evt
)
1205 SetSelf(evt
.m_self
, TRUE
);
1209 wxPyEvent::~wxPyEvent() {
1213 wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType
, int id
)
1214 : wxCommandEvent(commandType
, id
) {
1218 wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent
& evt
)
1219 : wxCommandEvent(evt
)
1221 SetSelf(evt
.m_self
, TRUE
);
1225 wxPyCommandEvent::~wxPyCommandEvent() {
1231 //---------------------------------------------------------------------------
1232 //---------------------------------------------------------------------------
1235 wxPyTimer::wxPyTimer(PyObject
* callback
) {
1240 wxPyTimer::~wxPyTimer() {
1241 wxPyBeginBlockThreads();
1243 wxPyEndBlockThreads();
1246 void wxPyTimer::Notify() {
1247 if (!func
|| func
== Py_None
) {
1251 wxPyBeginBlockThreads();
1254 PyObject
* args
= Py_BuildValue("()");
1256 result
= PyEval_CallObject(func
, args
);
1265 wxPyEndBlockThreads();
1271 //---------------------------------------------------------------------------
1272 //---------------------------------------------------------------------------
1273 // Convert a wxList to a Python List
1275 PyObject
* wxPy_ConvertList(wxListBase
* list
, const char* className
) {
1279 wxNode
* node
= list
->First();
1281 wxPyBeginBlockThreads();
1282 pyList
= PyList_New(0);
1284 wxObj
= node
->Data();
1285 pyObj
= wxPyMake_wxObject(wxObj
); //wxPyConstructObject(wxObj, className);
1286 PyList_Append(pyList
, pyObj
);
1287 node
= node
->Next();
1289 wxPyEndBlockThreads();
1293 //----------------------------------------------------------------------
1295 long wxPyGetWinHandle(wxWindow
* win
) {
1297 return (long)win
->GetHandle();
1300 // Find and return the actual X-Window.
1302 if (win
->m_wxwindow
) {
1303 GdkWindowPrivate
* bwin
= (GdkWindowPrivate
*)GTK_PIZZA(win
->m_wxwindow
)->bin_window
;
1305 return (long)bwin
->xwindow
;
1312 //----------------------------------------------------------------------
1313 // Some helper functions for typemaps in my_typemaps.i, so they won't be
1314 // included in every file over and over again...
1316 #if PYTHON_API_VERSION >= 1009
1317 static char* wxStringErrorMsg
= "String or Unicode type required";
1319 static char* wxStringErrorMsg
= "String type required";
1323 wxString
* wxString_in_helper(PyObject
* source
) {
1325 #if PYTHON_API_VERSION >= 1009 // Have Python unicode API
1326 if (!PyString_Check(source
) && !PyUnicode_Check(source
)) {
1327 PyErr_SetString(PyExc_TypeError
, wxStringErrorMsg
);
1331 if (PyUnicode_Check(source
)) {
1332 target
= new wxString(PyUnicode_AS_UNICODE(source
));
1334 // It is a string, get pointers to it and transform to unicode
1335 char* tmpPtr
; int tmpSize
;
1336 PyString_AsStringAndSize(source
, &tmpPtr
, &tmpSize
);
1337 target
= new wxString(tmpPtr
, *wxConvCurrent
, tmpSize
);
1340 char* tmpPtr
; int tmpSize
;
1341 if (PyString_AsStringAndSize(source
, &tmpPtr
, &tmpSize
) == -1) {
1342 PyErr_SetString(PyExc_TypeError
, "Unable to convert string");
1345 target
= new wxString(tmpPtr
, tmpSize
);
1346 #endif // wxUSE_UNICODE
1348 #else // No Python unicode API (1.5.2)
1349 if (!PyString_Check(source
)) {
1350 PyErr_SetString(PyExc_TypeError
, wxStringErrorMsg
);
1353 target
= new wxString(PyString_AS_STRING(source
), PyString_GET_SIZE(source
));
1359 // Similar to above except doesn't use "new" and doesn't set an exception
1360 wxString
Py2wxString(PyObject
* source
)
1363 bool doDecRef
= FALSE
;
1365 #if PYTHON_API_VERSION >= 1009 // Have Python unicode API
1366 if (!PyString_Check(source
) && !PyUnicode_Check(source
)) {
1367 // Convert to String if not one already... (TODO: Unicode too?)
1368 source
= PyObject_Str(source
);
1373 if (PyUnicode_Check(source
)) {
1374 target
= PyUnicode_AS_UNICODE(source
);
1376 // It is a string, get pointers to it and transform to unicode
1377 char* tmpPtr
; int tmpSize
;
1378 PyString_AsStringAndSize(source
, &tmpPtr
, &tmpSize
);
1379 target
= wxString(tmpPtr
, *wxConvCurrent
, tmpSize
);
1382 char* tmpPtr
; int tmpSize
;
1383 PyString_AsStringAndSize(source
, &tmpPtr
, &tmpSize
);
1384 target
= wxString(tmpPtr
, tmpSize
);
1385 #endif // wxUSE_UNICODE
1387 #else // No Python unicode API (1.5.2)
1388 if (!PyString_Check(source
)) {
1389 // Convert to String if not one already...
1390 source
= PyObject_Str(source
);
1393 target
= wxString(PyString_AS_STRING(source
), PyString_GET_SIZE(source
));
1402 // Make either a Python String or Unicode object, depending on build mode
1403 PyObject
* wx2PyString(const wxString
& src
)
1407 str
= PyUnicode_FromUnicode(src
.c_str(), src
.Len());
1409 str
= PyString_FromStringAndSize(src
.c_str(), src
.Len());
1415 //----------------------------------------------------------------------
1418 byte
* byte_LIST_helper(PyObject
* source
) {
1419 if (!PyList_Check(source
)) {
1420 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1423 int count
= PyList_Size(source
);
1424 byte
* temp
= new byte
[count
];
1426 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1429 for (int x
=0; x
<count
; x
++) {
1430 PyObject
* o
= PyList_GetItem(source
, x
);
1431 if (! PyInt_Check(o
)) {
1432 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1435 temp
[x
] = (byte
)PyInt_AsLong(o
);
1441 int* int_LIST_helper(PyObject
* source
) {
1442 if (!PyList_Check(source
)) {
1443 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1446 int count
= PyList_Size(source
);
1447 int* temp
= new int[count
];
1449 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1452 for (int x
=0; x
<count
; x
++) {
1453 PyObject
* o
= PyList_GetItem(source
, x
);
1454 if (! PyInt_Check(o
)) {
1455 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1458 temp
[x
] = PyInt_AsLong(o
);
1464 long* long_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 long* temp
= new long[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 (! PyInt_Check(o
)) {
1478 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1481 temp
[x
] = PyInt_AsLong(o
);
1487 char** string_LIST_helper(PyObject
* source
) {
1488 if (!PyList_Check(source
)) {
1489 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1492 int count
= PyList_Size(source
);
1493 char** temp
= new char*[count
];
1495 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1498 for (int x
=0; x
<count
; x
++) {
1499 PyObject
* o
= PyList_GetItem(source
, x
);
1500 if (! PyString_Check(o
)) {
1501 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1504 temp
[x
] = PyString_AsString(o
);
1509 //--------------------------------
1510 // Part of patch from Tim Hochberg
1511 static inline bool wxPointFromObjects(PyObject
* o1
, PyObject
* o2
, wxPoint
* point
) {
1512 if (PyInt_Check(o1
) && PyInt_Check(o2
)) {
1513 point
->x
= PyInt_AS_LONG(o1
);
1514 point
->y
= PyInt_AS_LONG(o2
);
1517 if (PyFloat_Check(o1
) && PyFloat_Check(o2
)) {
1518 point
->x
= (int)PyFloat_AS_DOUBLE(o1
);
1519 point
->y
= (int)PyFloat_AS_DOUBLE(o2
);
1522 if (PyInstance_Check(o1
) || PyInstance_Check(o2
)) {
1523 // Disallow instances because they can cause havok
1526 if (PyNumber_Check(o1
) && PyNumber_Check(o2
)) {
1527 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
1528 point
->x
= PyInt_AsLong(o1
);
1529 point
->y
= PyInt_AsLong(o2
);
1536 wxPoint
* wxPoint_LIST_helper(PyObject
* source
, int *count
) {
1537 // Putting all of the declarations here allows
1538 // us to put the error handling all in one place.
1541 PyObject
*o
, *o1
, *o2
;
1542 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1544 if (!PySequence_Check(source
)) {
1548 // The length of the sequence is returned in count.
1549 *count
= PySequence_Length(source
);
1554 temp
= new wxPoint
[*count
];
1556 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1559 for (x
=0; x
<*count
; x
++) {
1560 // Get an item: try fast way first.
1562 o
= PySequence_Fast_GET_ITEM(source
, x
);
1565 o
= PySequence_GetItem(source
, x
);
1571 // Convert o to wxPoint.
1572 if ((PyTuple_Check(o
) && PyTuple_GET_SIZE(o
) == 2) ||
1573 (PyList_Check(o
) && PyList_GET_SIZE(o
) == 2)) {
1574 o1
= PySequence_Fast_GET_ITEM(o
, 0);
1575 o2
= PySequence_Fast_GET_ITEM(o
, 1);
1576 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
1580 else if (PyInstance_Check(o
)) {
1582 if (SWIG_GetPtrObj(o
, (void **)&pt
, "_wxPoint_p")) {
1587 else if (PySequence_Check(o
) && PySequence_Length(o
) == 2) {
1588 o1
= PySequence_GetItem(o
, 0);
1589 o2
= PySequence_GetItem(o
, 1);
1590 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
1614 PyErr_SetString(PyExc_TypeError
, "Expected a sequence of length-2 sequences or wxPoints.");
1618 //------------------------------
1621 wxBitmap
** wxBitmap_LIST_helper(PyObject
* source
) {
1622 if (!PyList_Check(source
)) {
1623 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1626 int count
= PyList_Size(source
);
1627 wxBitmap
** temp
= new wxBitmap
*[count
];
1629 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1632 for (int x
=0; x
<count
; x
++) {
1633 PyObject
* o
= PyList_GetItem(source
, x
);
1634 if (PyInstance_Check(o
)) {
1636 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxBitmap_p")) {
1637 PyErr_SetString(PyExc_TypeError
,"Expected _wxBitmap_p.");
1643 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxBitmaps.");
1652 wxString
* wxString_LIST_helper(PyObject
* source
) {
1653 if (!PyList_Check(source
)) {
1654 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1657 int count
= PyList_Size(source
);
1658 wxString
* temp
= new wxString
[count
];
1660 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1663 for (int x
=0; x
<count
; x
++) {
1664 PyObject
* o
= PyList_GetItem(source
, x
);
1665 #if PYTHON_API_VERSION >= 1009
1666 if (! PyString_Check(o
) && ! PyUnicode_Check(o
)) {
1667 PyErr_SetString(PyExc_TypeError
, "Expected a list of string or unicode objects.");
1671 if (! PyString_Check(o
)) {
1672 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1677 wxString
* pStr
= wxString_in_helper(o
);
1685 wxAcceleratorEntry
* wxAcceleratorEntry_LIST_helper(PyObject
* source
) {
1686 if (!PyList_Check(source
)) {
1687 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1690 int count
= PyList_Size(source
);
1691 wxAcceleratorEntry
* temp
= new wxAcceleratorEntry
[count
];
1693 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1696 for (int x
=0; x
<count
; x
++) {
1697 PyObject
* o
= PyList_GetItem(source
, x
);
1698 if (PyInstance_Check(o
)) {
1699 wxAcceleratorEntry
* ae
;
1700 if (SWIG_GetPtrObj(o
, (void **) &ae
,"_wxAcceleratorEntry_p")) {
1701 PyErr_SetString(PyExc_TypeError
,"Expected _wxAcceleratorEntry_p.");
1706 else if (PyTuple_Check(o
)) {
1707 PyObject
* o1
= PyTuple_GetItem(o
, 0);
1708 PyObject
* o2
= PyTuple_GetItem(o
, 1);
1709 PyObject
* o3
= PyTuple_GetItem(o
, 2);
1710 temp
[x
].Set(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
));
1713 PyErr_SetString(PyExc_TypeError
, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
1721 wxPen
** wxPen_LIST_helper(PyObject
* source
) {
1722 if (!PyList_Check(source
)) {
1723 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1726 int count
= PyList_Size(source
);
1727 wxPen
** temp
= new wxPen
*[count
];
1729 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1732 for (int x
=0; x
<count
; x
++) {
1733 PyObject
* o
= PyList_GetItem(source
, x
);
1734 if (PyInstance_Check(o
)) {
1736 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxPen_p")) {
1738 PyErr_SetString(PyExc_TypeError
,"Expected _wxPen_p.");
1745 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxPens.");
1753 bool _2int_seq_helper(PyObject
* source
, int* i1
, int* i2
) {
1754 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1757 if (!PySequence_Check(source
) || PySequence_Length(source
) != 2)
1761 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1762 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1765 o1
= PySequence_GetItem(source
, 0);
1766 o2
= PySequence_GetItem(source
, 1);
1769 *i1
= PyInt_AsLong(o1
);
1770 *i2
= PyInt_AsLong(o2
);
1780 bool _4int_seq_helper(PyObject
* source
, int* i1
, int* i2
, int* i3
, int* i4
) {
1781 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1782 PyObject
*o1
, *o2
, *o3
, *o4
;
1784 if (!PySequence_Check(source
) || PySequence_Length(source
) != 4)
1788 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1789 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1790 o3
= PySequence_Fast_GET_ITEM(source
, 2);
1791 o4
= PySequence_Fast_GET_ITEM(source
, 3);
1794 o1
= PySequence_GetItem(source
, 0);
1795 o2
= PySequence_GetItem(source
, 1);
1796 o3
= PySequence_GetItem(source
, 2);
1797 o4
= PySequence_GetItem(source
, 3);
1800 *i1
= PyInt_AsLong(o1
);
1801 *i2
= PyInt_AsLong(o2
);
1802 *i3
= PyInt_AsLong(o3
);
1803 *i4
= PyInt_AsLong(o4
);
1815 //----------------------------------------------------------------------
1817 bool wxSize_helper(PyObject
* source
, wxSize
** obj
) {
1819 // If source is an object instance then it may already be the right type
1820 if (PyInstance_Check(source
)) {
1822 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxSize_p"))
1827 // otherwise a 2-tuple of integers is expected
1828 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1829 PyObject
* o1
= PySequence_GetItem(source
, 0);
1830 PyObject
* o2
= PySequence_GetItem(source
, 1);
1831 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1836 **obj
= wxSize(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1843 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxSize object.");
1847 bool wxPoint_helper(PyObject
* source
, wxPoint
** obj
) {
1849 // If source is an object instance then it may already be the right type
1850 if (PyInstance_Check(source
)) {
1852 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxPoint_p"))
1857 // otherwise a length-2 sequence of integers is expected
1858 if (PySequence_Check(source
) && PySequence_Length(source
) == 2) {
1859 PyObject
* o1
= PySequence_GetItem(source
, 0);
1860 PyObject
* o2
= PySequence_GetItem(source
, 1);
1861 // This should really check for integers, not numbers -- but that would break code.
1862 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1867 **obj
= wxPoint(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1873 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxPoint object.");
1879 bool wxRealPoint_helper(PyObject
* source
, wxRealPoint
** obj
) {
1881 // If source is an object instance then it may already be the right type
1882 if (PyInstance_Check(source
)) {
1884 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRealPoint_p"))
1889 // otherwise a 2-tuple of floats is expected
1890 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1891 PyObject
* o1
= PySequence_GetItem(source
, 0);
1892 PyObject
* o2
= PySequence_GetItem(source
, 1);
1893 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1898 **obj
= wxRealPoint(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
));
1905 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxRealPoint object.");
1912 bool wxRect_helper(PyObject
* source
, wxRect
** obj
) {
1914 // If source is an object instance then it may already be the right type
1915 if (PyInstance_Check(source
)) {
1917 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRect_p"))
1922 // otherwise a 4-tuple of integers is expected
1923 else if (PySequence_Check(source
) && PyObject_Length(source
) == 4) {
1924 PyObject
* o1
= PySequence_GetItem(source
, 0);
1925 PyObject
* o2
= PySequence_GetItem(source
, 1);
1926 PyObject
* o3
= PySequence_GetItem(source
, 2);
1927 PyObject
* o4
= PySequence_GetItem(source
, 3);
1928 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
) ||
1929 !PyNumber_Check(o3
) || !PyNumber_Check(o4
)) {
1936 **obj
= wxRect(PyInt_AsLong(o1
), PyInt_AsLong(o2
),
1937 PyInt_AsLong(o3
), PyInt_AsLong(o4
));
1946 PyErr_SetString(PyExc_TypeError
, "Expected a 4-tuple of integers or a wxRect object.");
1952 bool wxColour_helper(PyObject
* source
, wxColour
** obj
) {
1954 // If source is an object instance then it may already be the right type
1955 if (PyInstance_Check(source
)) {
1957 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxColour_p"))
1962 // otherwise a string is expected
1963 else if (PyString_Check(source
)) {
1964 wxString
spec(PyString_AS_STRING(source
), *wxConvCurrent
);
1965 if (spec
.GetChar(0) == '#' && spec
.Length() == 7) { // It's #RRGGBB
1966 long red
, green
, blue
;
1967 red
= green
= blue
= 0;
1968 spec
.Mid(1,2).ToLong(&red
, 16);
1969 spec
.Mid(3,2).ToLong(&green
, 16);
1970 spec
.Mid(5,2).ToLong(&blue
, 16);
1972 **obj
= wxColour(red
, green
, blue
);
1975 else { // it's a colour name
1976 **obj
= wxColour(spec
);
1982 PyErr_SetString(PyExc_TypeError
,
1983 "Expected a wxColour object or a string containing a colour "
1984 "name or '#RRGGBB'.");
1989 //----------------------------------------------------------------------
1991 PyObject
* wxArrayString2PyList_helper(const wxArrayString
& arr
) {
1993 PyObject
* list
= PyList_New(0);
1994 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
1996 PyObject
* str
= PyUnicode_FromUnicode(arr
[i
].c_str(), arr
[i
].Len());
1998 PyObject
* str
= PyString_FromStringAndSize(arr
[i
].c_str(), arr
[i
].Len());
2000 PyList_Append(list
, str
);
2007 PyObject
* wxArrayInt2PyList_helper(const wxArrayInt
& arr
) {
2009 PyObject
* list
= PyList_New(0);
2010 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
2011 PyObject
* number
= PyInt_FromLong(arr
[i
]);
2012 PyList_Append(list
, number
);
2019 //----------------------------------------------------------------------
2020 //----------------------------------------------------------------------