]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/src/helpers.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Helper functions/classes for the wxPython extension module
9 // Copyright: (c) 1998 by Total Control Software
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
13 #include <stdio.h> // get the correct definition of NULL
18 #include "pyistream.h"
21 #include <wx/msw/private.h>
22 #include <wx/msw/winundef.h>
23 #include <wx/msw/msvcrt.h>
28 #include <gdk/gdkprivate.h>
29 #include <wx/gtk/win_gtk.h>
33 //----------------------------------------------------------------------
35 #if PYTHON_API_VERSION <= 1007 && wxUSE_UNICODE
36 #error Python must support Unicode to use wxWindows Unicode
39 //----------------------------------------------------------------------
42 int WXDLLEXPORT
wxEntryStart( int& argc
, char** argv
);
44 int WXDLLEXPORT
wxEntryStart( int argc
, char** argv
);
46 int WXDLLEXPORT
wxEntryInitGui();
47 void WXDLLEXPORT
wxEntryCleanup();
49 wxPyApp
* wxPythonApp
= NULL
; // Global instance of application object
52 #ifdef WXP_WITH_THREAD
53 struct wxPyThreadState
{
55 PyThreadState
* tstate
;
57 wxPyThreadState(unsigned long _tid
=0, PyThreadState
* _tstate
=NULL
)
58 : tid(_tid
), tstate(_tstate
) {}
61 #include <wx/dynarray.h>
62 WX_DECLARE_OBJARRAY(wxPyThreadState
, wxPyThreadStateArray
);
63 #include <wx/arrimpl.cpp>
64 WX_DEFINE_OBJARRAY(wxPyThreadStateArray
);
66 wxPyThreadStateArray
* wxPyTStates
= NULL
;
67 wxMutex
* wxPyTMutex
= NULL
;
71 #ifdef __WXMSW__ // If building for win32...
72 //----------------------------------------------------------------------
73 // This gets run when the DLL is loaded. We just need to save a handle.
74 //----------------------------------------------------------------------
77 HINSTANCE hinstDLL
, // handle to DLL module
78 DWORD fdwReason
, // reason for calling function
79 LPVOID lpvReserved
// reserved
82 wxSetInstance(hinstDLL
);
87 //----------------------------------------------------------------------
88 // Classes for implementing the wxp main application shell.
89 //----------------------------------------------------------------------
93 // printf("**** ctor\n");
97 // printf("**** dtor\n");
101 // This one isn't acutally called... See __wxStart()
102 bool wxPyApp::OnInit() {
107 int wxPyApp::MainLoop() {
110 DeletePendingObjects();
111 bool initialized
= wxTopLevelWindows
.GetCount() != 0;
113 m_initialized
= initialized
;
117 retval
= wxApp::MainLoop();
125 //---------------------------------------------------------------------
126 //----------------------------------------------------------------------
129 static char* wxPyCopyCString(const wxChar
* src
)
131 wxWX2MBbuf buff
= (wxWX2MBbuf
)wxConvCurrent
->cWX2MB(src
);
132 size_t len
= strlen(buff
);
133 char* dest
= new char[len
+1];
139 static char* wxPyCopyCString(const char* src
) // we need a char version too
141 size_t len
= strlen(src
);
142 char* dest
= new char[len
+1];
148 static wxChar
* wxPyCopyWString(const char *src
)
150 //wxMB2WXbuf buff = wxConvCurrent->cMB2WX(src);
151 wxString
str(src
, *wxConvCurrent
);
152 return copystring(str
);
156 static wxChar
* wxPyCopyWString(const wxChar
*src
)
158 return copystring(src
);
163 //----------------------------------------------------------------------
165 // This is where we pick up the first part of the wxEntry functionality...
166 // The rest is in __wxStart and __wxCleanup. This function is called when
167 // wxcmodule is imported. (Before there is a wxApp object.)
172 // wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
175 #ifdef WXP_WITH_THREAD
176 PyEval_InitThreads();
177 wxPyTStates
= new wxPyThreadStateArray
;
178 wxPyTMutex
= new wxMutex
;
181 // Bail out if there is already windows created. This means that the
182 // toolkit has already been initialized, as in embedding wxPython in
183 // a C++ wxWindows app.
184 if (wxTopLevelWindows
.Number() > 0)
190 PyObject
* sysargv
= PySys_GetObject("argv");
191 if (sysargv
!= NULL
) {
192 argc
= PyList_Size(sysargv
);
193 argv
= new char*[argc
+1];
195 for(x
=0; x
<argc
; x
++) {
196 PyObject
*item
= PyList_GetItem(sysargv
, x
);
198 if (PyUnicode_Check(item
))
199 argv
[x
] = wxPyCopyCString(PyUnicode_AS_UNICODE(item
));
202 argv
[x
] = wxPyCopyCString(PyString_AsString(item
));
207 wxEntryStart(argc
, argv
);
213 // Start the user application, user App's OnInit method is a parameter here
214 PyObject
* __wxStart(PyObject
* /* self */, PyObject
* args
)
216 PyObject
* onInitFunc
= NULL
;
221 if (!PyArg_ParseTuple(args
, "O", &onInitFunc
))
224 #if 0 // Try it out without this check, see how it does...
225 if (wxTopLevelWindows
.Number() > 0) {
226 PyErr_SetString(PyExc_TypeError
, "Only 1 wxApp per process!");
231 // This is the next part of the wxEntry functionality...
233 wxChar
** argv
= NULL
;
234 PyObject
* sysargv
= PySys_GetObject("argv");
235 if (sysargv
!= NULL
) {
236 argc
= PyList_Size(sysargv
);
237 argv
= new wxChar
*[argc
+1];
239 for(x
=0; x
<argc
; x
++) {
240 PyObject
*pyArg
= PyList_GetItem(sysargv
, x
);
242 if (PyUnicode_Check(pyArg
))
243 argv
[x
] = wxPyCopyWString(PyUnicode_AS_UNICODE(pyArg
));
246 argv
[x
] = wxPyCopyWString(PyString_AsString(pyArg
));
251 wxPythonApp
->argc
= argc
;
252 wxPythonApp
->argv
= argv
;
256 // Call the Python App's OnInit function
257 arglist
= PyTuple_New(0);
258 result
= PyEval_CallObject(onInitFunc
, arglist
);
259 if (!result
) { // an exception was raised.
263 if (! PyInt_Check(result
)) {
264 PyErr_SetString(PyExc_TypeError
, "OnInit should return a boolean value");
267 bResult
= PyInt_AS_LONG(result
);
269 PyErr_SetString(PyExc_SystemExit
, "OnInit returned FALSE, exiting...");
274 wxTheApp
->m_initialized
= (wxTopLevelWindows
.GetCount() > 0);
284 #ifdef WXP_WITH_THREAD
287 wxPyTStates
->Empty();
295 static PyObject
* wxPython_dict
= NULL
;
296 static PyObject
* wxPyPtrTypeMap
= NULL
;
298 PyObject
* __wxSetDictionary(PyObject
* /* self */, PyObject
* args
)
301 if (!PyArg_ParseTuple(args
, "O", &wxPython_dict
))
304 if (!PyDict_Check(wxPython_dict
)) {
305 PyErr_SetString(PyExc_TypeError
, "_wxSetDictionary must have dictionary object!");
309 if (! wxPyPtrTypeMap
)
310 wxPyPtrTypeMap
= PyDict_New();
311 PyDict_SetItemString(wxPython_dict
, "__wxPyPtrTypeMap", wxPyPtrTypeMap
);
315 #define wxPlatform "__WXMOTIF__"
318 #define wxPlatform "__WXX11__"
321 #define wxPlatform "__WXGTK__"
323 #if defined(__WIN32__) || defined(__WXMSW__)
324 #define wxPlatform "__WXMSW__"
327 #define wxPlatform "__WXMAC__"
330 PyDict_SetItemString(wxPython_dict
, "wxPlatform", PyString_FromString(wxPlatform
));
331 PyDict_SetItemString(wxPython_dict
, "wxUSE_UNICODE", PyInt_FromLong(wxUSE_UNICODE
));
338 //---------------------------------------------------------------------------
339 // Stuff used by OOR to find the right wxPython class type to return and to
343 // The pointer type map is used when the "pointer" type name generated by SWIG
344 // is not the same as the shadow class name, for example wxPyTreeCtrl
345 // vs. wxTreeCtrl. It needs to be referenced in Python as well as from C++,
346 // so we'll just make it a Python dictionary in the wx module's namespace.
347 void wxPyPtrTypeMap_Add(const char* commonName
, const char* ptrName
) {
348 if (! wxPyPtrTypeMap
)
349 wxPyPtrTypeMap
= PyDict_New();
350 PyDict_SetItemString(wxPyPtrTypeMap
,
352 PyString_FromString((char*)ptrName
));
357 PyObject
* wxPyClassExists(const wxString
& className
) {
362 char buff
[64]; // should always be big enough...
364 sprintf(buff
, "%sPtr", className
.mbc_str());
365 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
367 return classobj
; // returns NULL if not found
371 PyObject
* wxPyMake_wxObject(wxObject
* source
, bool checkEvtHandler
) {
372 PyObject
* target
= NULL
;
373 bool isEvtHandler
= FALSE
;
376 // If it's derived from wxEvtHandler then there may
377 // already be a pointer to a Python object that we can use
379 if (checkEvtHandler
&& wxIsKindOf(source
, wxEvtHandler
)) {
381 wxEvtHandler
* eh
= (wxEvtHandler
*)source
;
382 wxPyClientData
* data
= (wxPyClientData
*)eh
->GetClientObject();
384 target
= data
->m_obj
;
390 // Otherwise make it the old fashioned way by making a
391 // new shadow object and putting this pointer in it.
392 wxClassInfo
* info
= source
->GetClassInfo();
393 wxChar
* name
= (wxChar
*)info
->GetClassName();
394 PyObject
* klass
= wxPyClassExists(name
);
395 while (info
&& !klass
) {
396 name
= (wxChar
*)info
->GetBaseClassName1();
397 info
= wxClassInfo::FindClass(name
);
398 klass
= wxPyClassExists(name
);
401 target
= wxPyConstructObject(source
, name
, klass
, FALSE
);
402 if (target
&& isEvtHandler
)
403 ((wxEvtHandler
*)source
)->SetClientObject(new wxPyClientData(target
));
405 wxString
msg("wxPython class not found for ");
406 msg
+= source
->GetClassInfo()->GetClassName();
407 PyErr_SetString(PyExc_NameError
, msg
.mbc_str());
411 } else { // source was NULL so return None.
412 Py_INCREF(Py_None
); target
= Py_None
;
418 PyObject
* wxPyMake_wxSizer(wxSizer
* source
) {
419 PyObject
* target
= NULL
;
421 if (source
&& wxIsKindOf(source
, wxSizer
)) {
422 // If it's derived from wxSizer then there may
423 // already be a pointer to a Python object that we can use
425 wxSizer
* sz
= (wxSizer
*)source
;
426 wxPyClientData
* data
= (wxPyClientData
*)sz
->GetClientObject();
428 target
= data
->m_obj
;
433 target
= wxPyMake_wxObject(source
, FALSE
);
434 if (target
!= Py_None
)
435 ((wxSizer
*)source
)->SetClientObject(new wxPyClientData(target
));
442 //---------------------------------------------------------------------------
444 PyObject
* wxPyConstructObject(void* ptr
,
445 const wxString
& className
,
452 wxString
name(className
);
453 char swigptr
[64]; // should always be big enough...
456 if ((item
= PyDict_GetItemString(wxPyPtrTypeMap
, (char*)(const char*)name
.mbc_str())) != NULL
) {
457 name
= wxString(PyString_AsString(item
), *wxConvCurrent
);
459 sprintf(buff
, "_%s_p", (const char*)name
.mbc_str());
460 SWIG_MakePtr(swigptr
, ptr
, buff
);
462 arg
= Py_BuildValue("(s)", swigptr
);
463 obj
= PyInstance_New(klass
, arg
, NULL
);
467 PyObject
* one
= PyInt_FromLong(1);
468 PyObject_SetAttrString(obj
, "thisown", one
);
476 PyObject
* wxPyConstructObject(void* ptr
,
477 const wxString
& className
,
486 char buff
[64]; // should always be big enough...
487 sprintf(buff
, "%sPtr", (const char*)className
.mbc_str());
489 wxASSERT_MSG(wxPython_dict
, wxT("wxPython_dict is not set yet!!"));
491 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
495 "*** Unknown class name %s, tell Robin about it please ***",
497 obj
= PyString_FromString(temp
);
501 return wxPyConstructObject(ptr
, className
, classobj
, setThisOwn
);
505 //---------------------------------------------------------------------------
508 #ifdef WXP_WITH_THREAD
510 unsigned long wxPyGetCurrentThreadId() {
511 return wxThread::GetCurrentId();
514 static PyThreadState
* gs_shutdownTState
;
516 PyThreadState
* wxPyGetThreadState() {
517 if (wxPyTMutex
== NULL
) // Python is shutting down...
518 return gs_shutdownTState
;
520 unsigned long ctid
= wxPyGetCurrentThreadId();
521 PyThreadState
* tstate
= NULL
;
524 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
525 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
526 if (info
.tid
== ctid
) {
527 tstate
= info
.tstate
;
531 wxPyTMutex
->Unlock();
532 wxASSERT_MSG(tstate
, wxT("PyThreadState should not be NULL!"));
537 void wxPySaveThreadState(PyThreadState
* tstate
) {
538 if (wxPyTMutex
== NULL
) { // Python is shutting down, assume a single thread...
539 gs_shutdownTState
= tstate
;
542 unsigned long ctid
= wxPyGetCurrentThreadId();
544 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
545 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
546 if (info
.tid
== ctid
) {
547 info
.tstate
= tstate
;
548 wxPyTMutex
->Unlock();
552 // not found, so add it...
553 wxPyTStates
->Add(new wxPyThreadState(ctid
, tstate
));
554 wxPyTMutex
->Unlock();
560 // Calls from Python to wxWindows code are wrapped in calls to these
563 PyThreadState
* wxPyBeginAllowThreads() {
564 #ifdef WXP_WITH_THREAD
565 PyThreadState
* saved
= PyEval_SaveThread(); // Py_BEGIN_ALLOW_THREADS;
566 wxPySaveThreadState(saved
);
573 void wxPyEndAllowThreads(PyThreadState
* saved
) {
574 #ifdef WXP_WITH_THREAD
575 PyEval_RestoreThread(saved
); // Py_END_ALLOW_THREADS;
581 // Calls from wxWindows back to Python code, or even any PyObject
582 // manipulations, PyDECREF's and etc. are wrapped in calls to these functions:
584 void wxPyBeginBlockThreads() {
585 #ifdef WXP_WITH_THREAD
586 PyThreadState
* tstate
= wxPyGetThreadState();
587 PyEval_RestoreThread(tstate
);
592 void wxPyEndBlockThreads() {
593 #ifdef WXP_WITH_THREAD
594 PyThreadState
* tstate
= PyEval_SaveThread();
595 // Is there any need to save it again?
600 //---------------------------------------------------------------------------
601 // wxPyInputStream and wxPyCBInputStream methods
604 void wxPyInputStream::close() {
605 /* do nothing for now */
608 void wxPyInputStream::flush() {
609 /* do nothing for now */
612 bool wxPyInputStream::eof() {
614 return m_wxis
->Eof();
619 wxPyInputStream::~wxPyInputStream() {
626 PyObject
* wxPyInputStream::read(int size
) {
627 PyObject
* obj
= NULL
;
629 const int BUFSIZE
= 1024;
631 // check if we have a real wxInputStream to work with
633 PyErr_SetString(PyExc_IOError
, "no valid C-wxInputStream");
639 while (! m_wxis
->Eof()) {
640 m_wxis
->Read(buf
.GetAppendBuf(BUFSIZE
), BUFSIZE
);
641 buf
.UngetAppendBuf(m_wxis
->LastRead());
644 } else { // Read only size number of characters
645 m_wxis
->Read(buf
.GetWriteBuf(size
), size
);
646 buf
.UngetWriteBuf(m_wxis
->LastRead());
650 if (m_wxis
->LastError() == wxSTREAM_READ_ERROR
) {
651 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
654 // We use only strings for the streams, not unicode
655 obj
= PyString_FromStringAndSize(buf
, buf
.GetDataLen());
661 PyObject
* wxPyInputStream::readline(int size
) {
662 PyObject
* obj
= NULL
;
667 // check if we have a real wxInputStream to work with
669 PyErr_SetString(PyExc_IOError
,"no valid C-wxInputStream");
673 // read until \n or byte limit reached
674 for (i
=ch
=0; (ch
!= '\n') && (!m_wxis
->Eof()) && ((size
< 0) || (i
< size
)); i
++) {
680 if (m_wxis
->LastError() == wxSTREAM_READ_ERROR
) {
681 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
684 // We use only strings for the streams, not unicode
685 obj
= PyString_FromStringAndSize((char*)buf
.GetData(), buf
.GetDataLen());
691 PyObject
* wxPyInputStream::readlines(int sizehint
) {
694 // check if we have a real wxInputStream to work with
696 PyErr_SetString(PyExc_IOError
,"no valid C-wxInputStream below");
701 pylist
= PyList_New(0);
707 // read sizehint bytes or until EOF
709 for (i
=0; (!m_wxis
->Eof()) && ((sizehint
< 0) || (i
< sizehint
));) {
710 PyObject
* s
= this->readline();
715 PyList_Append(pylist
, s
);
716 i
+= PyString_Size(s
);
720 if (m_wxis
->LastError() == wxSTREAM_READ_ERROR
) {
722 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
730 void wxPyInputStream::seek(int offset
, int whence
) {
732 m_wxis
->SeekI(offset
, wxSeekMode(whence
));
735 int wxPyInputStream::tell(){
737 return m_wxis
->TellI();
744 wxPyCBInputStream::wxPyCBInputStream(PyObject
*r
, PyObject
*s
, PyObject
*t
, bool block
)
745 : wxInputStream(), m_read(r
), m_seek(s
), m_tell(t
), m_block(block
)
749 wxPyCBInputStream::~wxPyCBInputStream() {
750 if (m_block
) wxPyBeginBlockThreads();
754 if (m_block
) wxPyEndBlockThreads();
758 wxPyCBInputStream
* wxPyCBInputStream::create(PyObject
*py
, bool block
) {
759 if (block
) wxPyBeginBlockThreads();
761 PyObject
* read
= getMethod(py
, "read");
762 PyObject
* seek
= getMethod(py
, "seek");
763 PyObject
* tell
= getMethod(py
, "tell");
766 PyErr_SetString(PyExc_TypeError
, "Not a file-like object");
770 if (block
) wxPyEndBlockThreads();
774 if (block
) wxPyEndBlockThreads();
775 return new wxPyCBInputStream(read
, seek
, tell
, block
);
778 PyObject
* wxPyCBInputStream::getMethod(PyObject
* py
, char* name
) {
779 if (!PyObject_HasAttrString(py
, name
))
781 PyObject
* o
= PyObject_GetAttrString(py
, name
);
782 if (!PyMethod_Check(o
) && !PyCFunction_Check(o
)) {
790 size_t wxPyCBInputStream::GetSize() const {
791 wxPyCBInputStream
* self
= (wxPyCBInputStream
*)this; // cast off const
792 if (m_seek
&& m_tell
) {
793 off_t temp
= self
->OnSysTell();
794 off_t ret
= self
->OnSysSeek(0, wxFromEnd
);
795 self
->OnSysSeek(temp
, wxFromStart
);
803 size_t wxPyCBInputStream::OnSysRead(void *buffer
, size_t bufsize
) {
807 wxPyBeginBlockThreads();
808 PyObject
* arglist
= Py_BuildValue("(i)", bufsize
);
809 PyObject
* result
= PyEval_CallObject(m_read
, arglist
);
813 if ((result
!= NULL
) && PyString_Check(result
)) {
814 o
= PyString_Size(result
);
816 m_lasterror
= wxSTREAM_EOF
;
819 memcpy((char*)buffer
, PyString_AsString(result
), o
); // strings only, not unicode...
824 m_lasterror
= wxSTREAM_READ_ERROR
;
825 wxPyEndBlockThreads();
830 size_t wxPyCBInputStream::OnSysWrite(const void *buffer
, size_t bufsize
) {
831 m_lasterror
= wxSTREAM_WRITE_ERROR
;
835 off_t
wxPyCBInputStream::OnSysSeek(off_t off
, wxSeekMode mode
) {
836 wxPyBeginBlockThreads();
837 PyObject
* arglist
= Py_BuildValue("(ii)", off
, mode
);
838 PyObject
* result
= PyEval_CallObject(m_seek
, arglist
);
841 wxPyEndBlockThreads();
845 off_t
wxPyCBInputStream::OnSysTell() const {
846 wxPyBeginBlockThreads();
847 PyObject
* arglist
= Py_BuildValue("()");
848 PyObject
* result
= PyEval_CallObject(m_tell
, arglist
);
851 if (result
!= NULL
) {
852 o
= PyInt_AsLong(result
);
855 wxPyEndBlockThreads();
859 //----------------------------------------------------------------------
861 IMPLEMENT_ABSTRACT_CLASS(wxPyCallback
, wxObject
);
863 wxPyCallback::wxPyCallback(PyObject
* func
) {
868 wxPyCallback::wxPyCallback(const wxPyCallback
& other
) {
869 m_func
= other
.m_func
;
873 wxPyCallback::~wxPyCallback() {
874 wxPyBeginBlockThreads();
876 wxPyEndBlockThreads();
881 // This function is used for all events destined for Python event handlers.
882 void wxPyCallback::EventThunker(wxEvent
& event
) {
883 wxPyCallback
* cb
= (wxPyCallback
*)event
.m_callbackUserData
;
884 PyObject
* func
= cb
->m_func
;
890 wxPyBeginBlockThreads();
891 wxString className
= event
.GetClassInfo()->GetClassName();
893 if (className
== "wxPyEvent")
894 arg
= ((wxPyEvent
*)&event
)->GetSelf();
895 else if (className
== "wxPyCommandEvent")
896 arg
= ((wxPyCommandEvent
*)&event
)->GetSelf();
898 arg
= wxPyConstructObject((void*)&event
, className
);
901 tuple
= PyTuple_New(1);
902 PyTuple_SET_ITEM(tuple
, 0, arg
);
903 result
= PyEval_CallObject(func
, tuple
);
907 PyErr_Clear(); // Just in case...
911 wxPyEndBlockThreads();
915 //----------------------------------------------------------------------
917 wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper
& other
) {
919 m_self
= other
.m_self
;
920 m_class
= other
.m_class
;
928 void wxPyCallbackHelper::setSelf(PyObject
* self
, PyObject
* klass
, int incref
) {
939 #if PYTHON_API_VERSION >= 1011
941 // Prior to Python 2.2 PyMethod_GetClass returned the class object
942 // in which the method was defined. Starting with 2.2 it returns
943 // "class that asked for the method" which seems totally bogus to me
944 // but apprently it fixes some obscure problem waiting to happen in
945 // Python. Since the API was not documented Guido and the gang felt
946 // safe in changing it. Needless to say that totally screwed up the
947 // logic below in wxPyCallbackHelper::findCallback, hence this icky
948 // code to find the class where the method is actually defined...
951 PyObject
* PyFindClassWithAttr(PyObject
*klass
, PyObject
*name
)
955 if (PyType_Check(klass
)) { // new style classes
956 // This code is borrowed/adapted from _PyType_Lookup in typeobject.c
957 // (TODO: This part is not tested yet, so I'm not sure it is correct...)
958 PyTypeObject
* type
= (PyTypeObject
*)klass
;
959 PyObject
*mro
, *res
, *base
, *dict
;
960 /* Look in tp_dict of types in MRO */
962 assert(PyTuple_Check(mro
));
963 n
= PyTuple_GET_SIZE(mro
);
964 for (i
= 0; i
< n
; i
++) {
965 base
= PyTuple_GET_ITEM(mro
, i
);
966 if (PyClass_Check(base
))
967 dict
= ((PyClassObject
*)base
)->cl_dict
;
969 assert(PyType_Check(base
));
970 dict
= ((PyTypeObject
*)base
)->tp_dict
;
972 assert(dict
&& PyDict_Check(dict
));
973 res
= PyDict_GetItem(dict
, name
);
980 else if (PyClass_Check(klass
)) { // old style classes
981 // This code is borrowed/adapted from class_lookup in classobject.c
982 PyClassObject
* cp
= (PyClassObject
*)klass
;
983 PyObject
*value
= PyDict_GetItem(cp
->cl_dict
, name
);
985 return (PyObject
*)cp
;
987 n
= PyTuple_Size(cp
->cl_bases
);
988 for (i
= 0; i
< n
; i
++) {
989 PyObject
* base
= PyTuple_GetItem(cp
->cl_bases
, i
);
990 PyObject
*v
= PyFindClassWithAttr(base
, name
);
1002 PyObject
* PyMethod_GetDefiningClass(PyObject
* method
, const char* name
)
1004 PyObject
* mgc
= PyMethod_GET_CLASS(method
);
1006 #if PYTHON_API_VERSION <= 1010 // prior to Python 2.2, the easy way
1008 #else // 2.2 and after, the hard way...
1010 PyObject
* nameo
= PyString_FromString(name
);
1011 PyObject
* klass
= PyFindClassWithAttr(mgc
, nameo
);
1019 bool wxPyCallbackHelper::findCallback(const char* name
) const {
1020 wxPyCallbackHelper
* self
= (wxPyCallbackHelper
*)this; // cast away const
1021 self
->m_lastFound
= NULL
;
1023 // If the object (m_self) has an attibute of the given name...
1024 if (m_self
&& PyObject_HasAttrString(m_self
, (char*)name
)) {
1025 PyObject
*method
, *klass
;
1026 method
= PyObject_GetAttrString(m_self
, (char*)name
);
1028 // ...and if that attribute is a method, and if that method's class is
1029 // not from a base class...
1030 if (PyMethod_Check(method
) &&
1031 (klass
= PyMethod_GetDefiningClass(method
, (char*)name
)) != NULL
&&
1032 ((klass
== m_class
) || PyClass_IsSubclass(klass
, m_class
))) {
1034 // ...then we'll save a pointer to the method so callCallback can call it.
1035 self
->m_lastFound
= method
;
1041 return m_lastFound
!= NULL
;
1045 int wxPyCallbackHelper::callCallback(PyObject
* argTuple
) const {
1049 result
= callCallbackObj(argTuple
);
1050 if (result
) { // Assumes an integer return type...
1051 retval
= PyInt_AsLong(result
);
1053 PyErr_Clear(); // forget about it if it's not...
1058 // Invoke the Python callable object, returning the raw PyObject return
1059 // value. Caller should DECREF the return value and also call PyEval_SaveThread.
1060 PyObject
* wxPyCallbackHelper::callCallbackObj(PyObject
* argTuple
) const {
1063 // Save a copy of the pointer in case the callback generates another
1064 // callback. In that case m_lastFound will have a different value when
1065 // it gets back here...
1066 PyObject
* method
= m_lastFound
;
1068 result
= PyEval_CallObject(method
, argTuple
);
1069 Py_DECREF(argTuple
);
1078 void wxPyCBH_setCallbackInfo(wxPyCallbackHelper
& cbh
, PyObject
* self
, PyObject
* klass
, int incref
) {
1079 cbh
.setSelf(self
, klass
, incref
);
1082 bool wxPyCBH_findCallback(const wxPyCallbackHelper
& cbh
, const char* name
) {
1083 return cbh
.findCallback(name
);
1086 int wxPyCBH_callCallback(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
1087 return cbh
.callCallback(argTuple
);
1090 PyObject
* wxPyCBH_callCallbackObj(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
1091 return cbh
.callCallbackObj(argTuple
);
1095 void wxPyCBH_delete(wxPyCallbackHelper
* cbh
) {
1096 if (cbh
->m_incRef
) {
1097 wxPyBeginBlockThreads();
1098 Py_XDECREF(cbh
->m_self
);
1099 Py_XDECREF(cbh
->m_class
);
1100 wxPyEndBlockThreads();
1104 //---------------------------------------------------------------------------
1105 //---------------------------------------------------------------------------
1106 // These event classes can be derived from in Python and passed through the event
1107 // system without losing anything. They do this by keeping a reference to
1108 // themselves and some special case handling in wxPyCallback::EventThunker.
1111 wxPyEvtSelfRef::wxPyEvtSelfRef() {
1112 //m_self = Py_None; // **** We don't do normal ref counting to prevent
1113 //Py_INCREF(m_self); // circular loops...
1117 wxPyEvtSelfRef::~wxPyEvtSelfRef() {
1118 wxPyBeginBlockThreads();
1121 wxPyEndBlockThreads();
1124 void wxPyEvtSelfRef::SetSelf(PyObject
* self
, bool clone
) {
1125 wxPyBeginBlockThreads();
1133 wxPyEndBlockThreads();
1136 PyObject
* wxPyEvtSelfRef::GetSelf() const {
1142 IMPLEMENT_ABSTRACT_CLASS(wxPyEvent
, wxEvent
);
1143 IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent
, wxCommandEvent
);
1146 wxPyEvent::wxPyEvent(int id
)
1151 wxPyEvent::wxPyEvent(const wxPyEvent
& evt
)
1154 SetSelf(evt
.m_self
, TRUE
);
1158 wxPyEvent::~wxPyEvent() {
1162 wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType
, int id
)
1163 : wxCommandEvent(commandType
, id
) {
1167 wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent
& evt
)
1168 : wxCommandEvent(evt
)
1170 SetSelf(evt
.m_self
, TRUE
);
1174 wxPyCommandEvent::~wxPyCommandEvent() {
1180 //---------------------------------------------------------------------------
1181 //---------------------------------------------------------------------------
1184 wxPyTimer::wxPyTimer(PyObject
* callback
) {
1189 wxPyTimer::~wxPyTimer() {
1190 wxPyBeginBlockThreads();
1192 wxPyEndBlockThreads();
1195 void wxPyTimer::Notify() {
1196 if (!func
|| func
== Py_None
) {
1200 wxPyBeginBlockThreads();
1203 PyObject
* args
= Py_BuildValue("()");
1205 result
= PyEval_CallObject(func
, args
);
1214 wxPyEndBlockThreads();
1220 //---------------------------------------------------------------------------
1221 //---------------------------------------------------------------------------
1222 // Convert a wxList to a Python List
1224 PyObject
* wxPy_ConvertList(wxListBase
* list
, const char* className
) {
1228 wxNode
* node
= list
->First();
1230 wxPyBeginBlockThreads();
1231 pyList
= PyList_New(0);
1233 wxObj
= node
->Data();
1234 pyObj
= wxPyMake_wxObject(wxObj
); //wxPyConstructObject(wxObj, className);
1235 PyList_Append(pyList
, pyObj
);
1236 node
= node
->Next();
1238 wxPyEndBlockThreads();
1242 //----------------------------------------------------------------------
1244 long wxPyGetWinHandle(wxWindow
* win
) {
1246 return (long)win
->GetHandle();
1249 // Find and return the actual X-Window.
1251 if (win
->m_wxwindow
) {
1252 GdkWindowPrivate
* bwin
= (GdkWindowPrivate
*)GTK_PIZZA(win
->m_wxwindow
)->bin_window
;
1254 return (long)bwin
->xwindow
;
1261 //----------------------------------------------------------------------
1262 // Some helper functions for typemaps in my_typemaps.i, so they won't be
1263 // included in every file over and over again...
1265 #if PYTHON_API_VERSION >= 1009
1266 static char* wxStringErrorMsg
= "String or Unicode type required";
1268 static char* wxStringErrorMsg
= "String type required";
1272 wxString
* wxString_in_helper(PyObject
* source
) {
1274 #if PYTHON_API_VERSION >= 1009 // Have Python unicode API
1275 if (!PyString_Check(source
) && !PyUnicode_Check(source
)) {
1276 PyErr_SetString(PyExc_TypeError
, wxStringErrorMsg
);
1280 if (PyUnicode_Check(source
)) {
1281 target
= new wxString(PyUnicode_AS_UNICODE(source
));
1283 // It is a string, get pointers to it and transform to unicode
1284 char* tmpPtr
; int tmpSize
;
1285 PyString_AsStringAndSize(source
, &tmpPtr
, &tmpSize
);
1286 target
= new wxString(tmpPtr
, *wxConvCurrent
, tmpSize
);
1289 char* tmpPtr
; int tmpSize
;
1290 if (PyString_AsStringAndSize(source
, &tmpPtr
, &tmpSize
) == -1) {
1291 PyErr_SetString(PyExc_TypeError
, "Unable to convert string");
1294 target
= new wxString(tmpPtr
, tmpSize
);
1295 #endif // wxUSE_UNICODE
1297 #else // No Python unicode API (1.5.2)
1298 if (!PyString_Check(source
)) {
1299 PyErr_SetString(PyExc_TypeError
, wxStringErrorMsg
);
1302 target
= new wxString(PyString_AS_STRING(source
), PyString_GET_SIZE(source
));
1308 // Similar to above except doesn't use "new" and doesn't set an exception
1309 wxString
Py2wxString(PyObject
* source
)
1312 bool doDecRef
= FALSE
;
1314 #if PYTHON_API_VERSION >= 1009 // Have Python unicode API
1315 if (!PyString_Check(source
) && !PyUnicode_Check(source
)) {
1316 // Convert to String if not one already... (TODO: Unicode too?)
1317 source
= PyObject_Str(source
);
1322 if (PyUnicode_Check(source
)) {
1323 target
= PyUnicode_AS_UNICODE(source
);
1325 // It is a string, get pointers to it and transform to unicode
1326 char* tmpPtr
; int tmpSize
;
1327 PyString_AsStringAndSize(source
, &tmpPtr
, &tmpSize
);
1328 target
= wxString(tmpPtr
, *wxConvCurrent
, tmpSize
);
1331 char* tmpPtr
; int tmpSize
;
1332 PyString_AsStringAndSize(source
, &tmpPtr
, &tmpSize
);
1333 target
= wxString(tmpPtr
, tmpSize
);
1334 #endif // wxUSE_UNICODE
1336 #else // No Python unicode API (1.5.2)
1337 if (!PyString_Check(source
)) {
1338 // Convert to String if not one already...
1339 source
= PyObject_Str(source
);
1342 target
= wxString(PyString_AS_STRING(source
), PyString_GET_SIZE(source
));
1351 // Make either a Python String or Unicode object, depending on build mode
1352 PyObject
* wx2PyString(const wxString
& src
)
1356 str
= PyUnicode_FromUnicode(src
.c_str(), src
.Len());
1358 str
= PyString_FromStringAndSize(src
.c_str(), src
.Len());
1364 //----------------------------------------------------------------------
1367 byte
* byte_LIST_helper(PyObject
* source
) {
1368 if (!PyList_Check(source
)) {
1369 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1372 int count
= PyList_Size(source
);
1373 byte
* temp
= new byte
[count
];
1375 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1378 for (int x
=0; x
<count
; x
++) {
1379 PyObject
* o
= PyList_GetItem(source
, x
);
1380 if (! PyInt_Check(o
)) {
1381 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1384 temp
[x
] = (byte
)PyInt_AsLong(o
);
1390 int* int_LIST_helper(PyObject
* source
) {
1391 if (!PyList_Check(source
)) {
1392 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1395 int count
= PyList_Size(source
);
1396 int* temp
= new int[count
];
1398 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1401 for (int x
=0; x
<count
; x
++) {
1402 PyObject
* o
= PyList_GetItem(source
, x
);
1403 if (! PyInt_Check(o
)) {
1404 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1407 temp
[x
] = PyInt_AsLong(o
);
1413 long* long_LIST_helper(PyObject
* source
) {
1414 if (!PyList_Check(source
)) {
1415 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1418 int count
= PyList_Size(source
);
1419 long* temp
= new long[count
];
1421 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1424 for (int x
=0; x
<count
; x
++) {
1425 PyObject
* o
= PyList_GetItem(source
, x
);
1426 if (! PyInt_Check(o
)) {
1427 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1430 temp
[x
] = PyInt_AsLong(o
);
1436 char** string_LIST_helper(PyObject
* source
) {
1437 if (!PyList_Check(source
)) {
1438 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1441 int count
= PyList_Size(source
);
1442 char** temp
= new char*[count
];
1444 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1447 for (int x
=0; x
<count
; x
++) {
1448 PyObject
* o
= PyList_GetItem(source
, x
);
1449 if (! PyString_Check(o
)) {
1450 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1453 temp
[x
] = PyString_AsString(o
);
1458 //--------------------------------
1459 // Part of patch from Tim Hochberg
1460 static inline bool wxPointFromObjects(PyObject
* o1
, PyObject
* o2
, wxPoint
* point
) {
1461 if (PyInt_Check(o1
) && PyInt_Check(o2
)) {
1462 point
->x
= PyInt_AS_LONG(o1
);
1463 point
->y
= PyInt_AS_LONG(o2
);
1466 if (PyFloat_Check(o1
) && PyFloat_Check(o2
)) {
1467 point
->x
= (int)PyFloat_AS_DOUBLE(o1
);
1468 point
->y
= (int)PyFloat_AS_DOUBLE(o2
);
1471 if (PyInstance_Check(o1
) || PyInstance_Check(o2
)) {
1472 // Disallow instances because they can cause havok
1475 if (PyNumber_Check(o1
) && PyNumber_Check(o2
)) {
1476 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
1477 point
->x
= PyInt_AsLong(o1
);
1478 point
->y
= PyInt_AsLong(o2
);
1485 wxPoint
* wxPoint_LIST_helper(PyObject
* source
, int *count
) {
1486 // Putting all of the declarations here allows
1487 // us to put the error handling all in one place.
1490 PyObject
*o
, *o1
, *o2
;
1491 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1493 if (!PySequence_Check(source
)) {
1497 // The length of the sequence is returned in count.
1498 *count
= PySequence_Length(source
);
1503 temp
= new wxPoint
[*count
];
1505 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1508 for (x
=0; x
<*count
; x
++) {
1509 // Get an item: try fast way first.
1511 o
= PySequence_Fast_GET_ITEM(source
, x
);
1514 o
= PySequence_GetItem(source
, x
);
1520 // Convert o to wxPoint.
1521 if ((PyTuple_Check(o
) && PyTuple_GET_SIZE(o
) == 2) ||
1522 (PyList_Check(o
) && PyList_GET_SIZE(o
) == 2)) {
1523 o1
= PySequence_Fast_GET_ITEM(o
, 0);
1524 o2
= PySequence_Fast_GET_ITEM(o
, 1);
1525 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
1529 else if (PyInstance_Check(o
)) {
1531 if (SWIG_GetPtrObj(o
, (void **)&pt
, "_wxPoint_p")) {
1536 else if (PySequence_Check(o
) && PySequence_Length(o
) == 2) {
1537 o1
= PySequence_GetItem(o
, 0);
1538 o2
= PySequence_GetItem(o
, 1);
1539 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
1563 PyErr_SetString(PyExc_TypeError
, "Expected a sequence of length-2 sequences or wxPoints.");
1567 //------------------------------
1570 wxBitmap
** wxBitmap_LIST_helper(PyObject
* source
) {
1571 if (!PyList_Check(source
)) {
1572 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1575 int count
= PyList_Size(source
);
1576 wxBitmap
** temp
= new wxBitmap
*[count
];
1578 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1581 for (int x
=0; x
<count
; x
++) {
1582 PyObject
* o
= PyList_GetItem(source
, x
);
1583 if (PyInstance_Check(o
)) {
1585 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxBitmap_p")) {
1586 PyErr_SetString(PyExc_TypeError
,"Expected _wxBitmap_p.");
1592 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxBitmaps.");
1601 wxString
* wxString_LIST_helper(PyObject
* source
) {
1602 if (!PyList_Check(source
)) {
1603 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1606 int count
= PyList_Size(source
);
1607 wxString
* temp
= new wxString
[count
];
1609 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1612 for (int x
=0; x
<count
; x
++) {
1613 PyObject
* o
= PyList_GetItem(source
, x
);
1614 #if PYTHON_API_VERSION >= 1009
1615 if (! PyString_Check(o
) && ! PyUnicode_Check(o
)) {
1616 PyErr_SetString(PyExc_TypeError
, "Expected a list of string or unicode objects.");
1620 if (! PyString_Check(o
)) {
1621 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1626 wxString
* pStr
= wxString_in_helper(o
);
1634 wxAcceleratorEntry
* wxAcceleratorEntry_LIST_helper(PyObject
* source
) {
1635 if (!PyList_Check(source
)) {
1636 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1639 int count
= PyList_Size(source
);
1640 wxAcceleratorEntry
* temp
= new wxAcceleratorEntry
[count
];
1642 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1645 for (int x
=0; x
<count
; x
++) {
1646 PyObject
* o
= PyList_GetItem(source
, x
);
1647 if (PyInstance_Check(o
)) {
1648 wxAcceleratorEntry
* ae
;
1649 if (SWIG_GetPtrObj(o
, (void **) &ae
,"_wxAcceleratorEntry_p")) {
1650 PyErr_SetString(PyExc_TypeError
,"Expected _wxAcceleratorEntry_p.");
1655 else if (PyTuple_Check(o
)) {
1656 PyObject
* o1
= PyTuple_GetItem(o
, 0);
1657 PyObject
* o2
= PyTuple_GetItem(o
, 1);
1658 PyObject
* o3
= PyTuple_GetItem(o
, 2);
1659 temp
[x
].Set(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
));
1662 PyErr_SetString(PyExc_TypeError
, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
1670 wxPen
** wxPen_LIST_helper(PyObject
* source
) {
1671 if (!PyList_Check(source
)) {
1672 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1675 int count
= PyList_Size(source
);
1676 wxPen
** temp
= new wxPen
*[count
];
1678 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1681 for (int x
=0; x
<count
; x
++) {
1682 PyObject
* o
= PyList_GetItem(source
, x
);
1683 if (PyInstance_Check(o
)) {
1685 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxPen_p")) {
1687 PyErr_SetString(PyExc_TypeError
,"Expected _wxPen_p.");
1694 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxPens.");
1702 bool _2int_seq_helper(PyObject
* source
, int* i1
, int* i2
) {
1703 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1706 if (!PySequence_Check(source
) || PySequence_Length(source
) != 2)
1710 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1711 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1714 o1
= PySequence_GetItem(source
, 0);
1715 o2
= PySequence_GetItem(source
, 1);
1718 *i1
= PyInt_AsLong(o1
);
1719 *i2
= PyInt_AsLong(o2
);
1729 bool _4int_seq_helper(PyObject
* source
, int* i1
, int* i2
, int* i3
, int* i4
) {
1730 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1731 PyObject
*o1
, *o2
, *o3
, *o4
;
1733 if (!PySequence_Check(source
) || PySequence_Length(source
) != 4)
1737 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1738 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1739 o3
= PySequence_Fast_GET_ITEM(source
, 2);
1740 o4
= PySequence_Fast_GET_ITEM(source
, 3);
1743 o1
= PySequence_GetItem(source
, 0);
1744 o2
= PySequence_GetItem(source
, 1);
1745 o3
= PySequence_GetItem(source
, 2);
1746 o4
= PySequence_GetItem(source
, 3);
1749 *i1
= PyInt_AsLong(o1
);
1750 *i2
= PyInt_AsLong(o2
);
1751 *i3
= PyInt_AsLong(o3
);
1752 *i4
= PyInt_AsLong(o4
);
1764 //----------------------------------------------------------------------
1766 bool wxSize_helper(PyObject
* source
, wxSize
** obj
) {
1768 // If source is an object instance then it may already be the right type
1769 if (PyInstance_Check(source
)) {
1771 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxSize_p"))
1776 // otherwise a 2-tuple of integers is expected
1777 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1778 PyObject
* o1
= PySequence_GetItem(source
, 0);
1779 PyObject
* o2
= PySequence_GetItem(source
, 1);
1780 **obj
= wxSize(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1785 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxSize object.");
1789 bool wxPoint_helper(PyObject
* source
, wxPoint
** obj
) {
1791 // If source is an object instance then it may already be the right type
1792 if (PyInstance_Check(source
)) {
1794 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxPoint_p"))
1799 // otherwise a length-2 sequence of integers is expected
1800 if (PySequence_Check(source
) && PySequence_Length(source
) == 2) {
1801 PyObject
* o1
= PySequence_GetItem(source
, 0);
1802 PyObject
* o2
= PySequence_GetItem(source
, 1);
1803 // This should really check for integers, not numbers -- but that would break code.
1804 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1809 **obj
= wxPoint(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1815 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxPoint object.");
1821 bool wxRealPoint_helper(PyObject
* source
, wxRealPoint
** obj
) {
1823 // If source is an object instance then it may already be the right type
1824 if (PyInstance_Check(source
)) {
1826 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRealPoint_p"))
1831 // otherwise a 2-tuple of floats is expected
1832 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1833 PyObject
* o1
= PySequence_GetItem(source
, 0);
1834 PyObject
* o2
= PySequence_GetItem(source
, 1);
1835 **obj
= wxRealPoint(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
));
1840 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxRealPoint object.");
1847 bool wxRect_helper(PyObject
* source
, wxRect
** 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
, "_wxRect_p"))
1857 // otherwise a 4-tuple of integers is expected
1858 else if (PySequence_Check(source
) && PyObject_Length(source
) == 4) {
1859 PyObject
* o1
= PySequence_GetItem(source
, 0);
1860 PyObject
* o2
= PySequence_GetItem(source
, 1);
1861 PyObject
* o3
= PySequence_GetItem(source
, 2);
1862 PyObject
* o4
= PySequence_GetItem(source
, 3);
1863 **obj
= wxRect(PyInt_AsLong(o1
), PyInt_AsLong(o2
),
1864 PyInt_AsLong(o3
), PyInt_AsLong(o4
));
1869 PyErr_SetString(PyExc_TypeError
, "Expected a 4-tuple of integers or a wxRect object.");
1875 bool wxColour_helper(PyObject
* source
, wxColour
** obj
) {
1877 // If source is an object instance then it may already be the right type
1878 if (PyInstance_Check(source
)) {
1880 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxColour_p"))
1885 // otherwise a string is expected
1886 else if (PyString_Check(source
)) {
1887 wxString
spec(PyString_AS_STRING(source
), *wxConvCurrent
);
1888 if (spec
.GetChar(0) == '#' && spec
.Length() == 7) { // It's #RRGGBB
1889 long red
, green
, blue
;
1890 red
= green
= blue
= 0;
1891 spec
.Mid(1,2).ToLong(&red
, 16);
1892 spec
.Mid(3,2).ToLong(&green
, 16);
1893 spec
.Mid(5,2).ToLong(&blue
, 16);
1895 **obj
= wxColour(red
, green
, blue
);
1898 else { // it's a colour name
1899 **obj
= wxColour(spec
);
1905 PyErr_SetString(PyExc_TypeError
,
1906 "Expected a wxColour object or a string containing a colour "
1907 "name or '#RRGGBB'.");
1912 //----------------------------------------------------------------------
1914 PyObject
* wxArrayString2PyList_helper(const wxArrayString
& arr
) {
1916 PyObject
* list
= PyList_New(0);
1917 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
1919 PyObject
* str
= PyUnicode_FromUnicode(arr
[i
].c_str(), arr
[i
].Len());
1921 PyObject
* str
= PyString_FromStringAndSize(arr
[i
].c_str(), arr
[i
].Len());
1923 PyList_Append(list
, str
);
1930 PyObject
* wxArrayInt2PyList_helper(const wxArrayInt
& arr
) {
1932 PyObject
* list
= PyList_New(0);
1933 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
1934 PyObject
* number
= PyInt_FromLong(arr
[i
]);
1935 PyList_Append(list
, number
);
1942 //----------------------------------------------------------------------
1943 //----------------------------------------------------------------------