]>
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 // TODO: Is this really the right way to do these????
130 static char* copyUniString(const wxChar
*s
)
132 if (s
== NULL
) s
= wxT("");
133 wxString tmpStr
= wxString(s
);
134 char *news
= new char[tmpStr
.Len()+1];
135 for (unsigned int i
=0; i
<tmpStr
.Len(); i
++)
141 static char* copyCString(const char *s
)
143 if (s
== NULL
) s
= "";
145 char *news
= new char[len
+1];
146 memcpy(news
, s
, len
+1);
150 static wxChar
* wCharFromCStr(const char *s
)
152 if (s
== NULL
) s
= "";
153 size_t len
= strlen(s
) + 1;
154 wxChar
*news
= new wxChar
[len
];
155 for (size_t i
=0; i
<len
; i
++) {
156 news
[i
] = (wxChar
)s
[i
];
162 // This is where we pick up the first part of the wxEntry functionality...
163 // The rest is in __wxStart and __wxCleanup. This function is called when
164 // wxcmodule is imported. (Before there is a wxApp object.)
169 // wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
172 #ifdef WXP_WITH_THREAD
173 PyEval_InitThreads();
174 wxPyTStates
= new wxPyThreadStateArray
;
175 wxPyTMutex
= new wxMutex
;
178 // Bail out if there is already windows created. This means that the
179 // toolkit has already been initialized, as in embedding wxPython in
180 // a C++ wxWindows app.
181 if (wxTopLevelWindows
.Number() > 0)
187 PyObject
* sysargv
= PySys_GetObject("argv");
188 if (sysargv
!= NULL
) {
189 argc
= PyList_Size(sysargv
);
190 argv
= new char*[argc
+1];
192 for(x
=0; x
<argc
; x
++) {
193 PyObject
*item
= PyList_GetItem(sysargv
, x
);
195 if (PyUnicode_Check(item
))
196 argv
[x
] = copyUniString(PyUnicode_AS_UNICODE(item
));
198 argv
[x
] = copyCString(PyString_AsString(item
));
200 argv
[x
] = copystring(PyString_AsString(item
));
206 wxEntryStart(argc
, argv
);
212 // Start the user application, user App's OnInit method is a parameter here
213 PyObject
* __wxStart(PyObject
* /* self */, PyObject
* args
)
215 PyObject
* onInitFunc
= NULL
;
220 if (!PyArg_ParseTuple(args
, "O", &onInitFunc
))
223 #if 0 // Try it out without this check, see how it does...
224 if (wxTopLevelWindows
.Number() > 0) {
225 PyErr_SetString(PyExc_TypeError
, "Only 1 wxApp per process!");
230 // This is the next part of the wxEntry functionality...
232 wxChar
** argv
= NULL
;
233 PyObject
* sysargv
= PySys_GetObject("argv");
234 if (sysargv
!= NULL
) {
235 argc
= PyList_Size(sysargv
);
236 argv
= new wxChar
*[argc
+1];
238 for(x
=0; x
<argc
; x
++) {
239 PyObject
*pyArg
= PyList_GetItem(sysargv
, x
);
241 if (PyUnicode_Check(pyArg
)) {
242 argv
[x
] = copystring(PyUnicode_AS_UNICODE(pyArg
));
244 assert(PyString_Check(pyArg
));
245 argv
[x
] = wCharFromCStr(PyString_AsString(pyArg
));
248 argv
[x
] = copystring(PyString_AsString(pyArg
));
254 wxPythonApp
->argc
= argc
;
255 wxPythonApp
->argv
= argv
;
259 // Call the Python App's OnInit function
260 arglist
= PyTuple_New(0);
261 result
= PyEval_CallObject(onInitFunc
, arglist
);
262 if (!result
) { // an exception was raised.
266 if (! PyInt_Check(result
)) {
267 PyErr_SetString(PyExc_TypeError
, "OnInit should return a boolean value");
270 bResult
= PyInt_AS_LONG(result
);
272 PyErr_SetString(PyExc_SystemExit
, "OnInit returned FALSE, exiting...");
277 wxTheApp
->m_initialized
= (wxTopLevelWindows
.GetCount() > 0);
287 #ifdef WXP_WITH_THREAD
290 wxPyTStates
->Empty();
298 static PyObject
* wxPython_dict
= NULL
;
299 static PyObject
* wxPyPtrTypeMap
= NULL
;
301 PyObject
* __wxSetDictionary(PyObject
* /* self */, PyObject
* args
)
304 if (!PyArg_ParseTuple(args
, "O", &wxPython_dict
))
307 if (!PyDict_Check(wxPython_dict
)) {
308 PyErr_SetString(PyExc_TypeError
, "_wxSetDictionary must have dictionary object!");
312 if (! wxPyPtrTypeMap
)
313 wxPyPtrTypeMap
= PyDict_New();
314 PyDict_SetItemString(wxPython_dict
, "__wxPyPtrTypeMap", wxPyPtrTypeMap
);
318 #define wxPlatform "__WXMOTIF__"
321 #define wxPlatform "__WXX11__"
324 #define wxPlatform "__WXGTK__"
326 #if defined(__WIN32__) || defined(__WXMSW__)
327 #define wxPlatform "__WXMSW__"
330 #define wxPlatform "__WXMAC__"
333 PyDict_SetItemString(wxPython_dict
, "wxPlatform", PyString_FromString(wxPlatform
));
334 PyDict_SetItemString(wxPython_dict
, "wxUSE_UNICODE", PyInt_FromLong(wxUSE_UNICODE
));
342 //---------------------------------------------------------------------------
343 // Stuff used by OOR to find the right wxPython class type to return and to
347 // The pointer type map is used when the "pointer" type name generated by SWIG
348 // is not the same as the shadow class name, for example wxPyTreeCtrl
349 // vs. wxTreeCtrl. It needs to be referenced in Python as well as from C++,
350 // so we'll just make it a Python dictionary in the wx module's namespace.
351 void wxPyPtrTypeMap_Add(const char* commonName
, const char* ptrName
) {
352 if (! wxPyPtrTypeMap
)
353 wxPyPtrTypeMap
= PyDict_New();
354 PyDict_SetItemString(wxPyPtrTypeMap
,
356 PyString_FromString((char*)ptrName
));
361 PyObject
* wxPyClassExists(const char* className
) {
366 char buff
[64]; // should always be big enough...
368 sprintf(buff
, "%sPtr", className
);
369 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
371 return classobj
; // returns NULL if not found
376 void unicodeToChar(const wxString
*src
, char *dest
)
378 for (unsigned int i
=0; i
<src
->Len(); i
++) {
379 dest
[i
] = (char)(*src
)[i
];
383 PyObject
* wxPyClassExistsUnicode(const wxString
*className
) {
384 if (!className
->Len())
386 char buff
[64]; // should always be big enough...
387 char *nameBuf
= new char[className
->Len()+1];
388 unicodeToChar(className
, nameBuf
);
389 sprintf(buff
, "%sPtr", nameBuf
);
390 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
392 return classobj
; // returns NULL if not found
397 PyObject
* wxPyMake_wxObject(wxObject
* source
, bool checkEvtHandler
) {
398 PyObject
* target
= NULL
;
399 bool isEvtHandler
= FALSE
;
402 // If it's derived from wxEvtHandler then there may
403 // already be a pointer to a Python object that we can use
405 if (checkEvtHandler
&& wxIsKindOf(source
, wxEvtHandler
)) {
407 wxEvtHandler
* eh
= (wxEvtHandler
*)source
;
408 wxPyClientData
* data
= (wxPyClientData
*)eh
->GetClientObject();
410 target
= data
->m_obj
;
417 // Otherwise make it the old fashioned way by making a
418 // new shadow object and putting this pointer in it.
419 wxClassInfo
* info
= source
->GetClassInfo();
420 wxChar
* name
= (wxChar
*)info
->GetClassName();
421 PyObject
* klass
= wxPyClassExists(name
);
422 while (info
&& !klass
) {
423 name
= (wxChar
*)info
->GetBaseClassName1();
424 info
= wxClassInfo::FindClass(name
);
425 klass
= wxPyClassExists(name
);
428 target
= wxPyConstructObject(source
, name
, klass
, FALSE
);
429 if (target
&& isEvtHandler
)
430 ((wxEvtHandler
*)source
)->SetClientObject(new wxPyClientData(target
));
432 wxString
msg("wxPython class not found for ");
433 msg
+= source
->GetClassInfo()->GetClassName();
434 PyErr_SetString(PyExc_NameError
, msg
.c_str());
438 } else { // source was NULL so return None.
439 Py_INCREF(Py_None
); target
= Py_None
;
445 PyObject
* wxPyMake_wxSizer(wxSizer
* source
) {
446 PyObject
* target
= NULL
;
448 if (source
&& wxIsKindOf(source
, wxSizer
)) {
449 // If it's derived from wxSizer then there may
450 // already be a pointer to a Python object that we can use
452 wxSizer
* sz
= (wxSizer
*)source
;
453 wxPyClientData
* data
= (wxPyClientData
*)sz
->GetClientObject();
455 target
= data
->m_obj
;
460 target
= wxPyMake_wxObject(source
, FALSE
);
461 if (target
!= Py_None
)
462 ((wxSizer
*)source
)->SetClientObject(new wxPyClientData(target
));
469 //---------------------------------------------------------------------------
471 PyObject
* wxPyConstructObject(void* ptr
,
472 const char* className
,
479 char swigptr
[64]; // should always be big enough...
482 if ((item
= PyDict_GetItemString(wxPyPtrTypeMap
, (char*)className
)) != NULL
) {
483 className
= PyString_AsString(item
);
485 sprintf(buff
, "_%s_p", className
);
486 SWIG_MakePtr(swigptr
, ptr
, buff
);
488 arg
= Py_BuildValue("(s)", swigptr
);
489 obj
= PyInstance_New(klass
, arg
, NULL
);
493 PyObject
* one
= PyInt_FromLong(1);
494 PyObject_SetAttrString(obj
, "thisown", one
);
502 PyObject
* wxPyConstructObject(void* ptr
,
503 const char* className
,
512 char buff
[64]; // should always be big enough...
513 sprintf(buff
, "%sPtr", className
);
515 wxASSERT_MSG(wxPython_dict
, "wxPython_dict is not set yet!!");
517 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
521 "*** Unknown class name %s, tell Robin about it please ***",
523 obj
= PyString_FromString(temp
);
527 return wxPyConstructObject(ptr
, className
, classobj
, setThisOwn
);
530 //---------------------------------------------------------------------------
533 #ifdef WXP_WITH_THREAD
535 unsigned long wxPyGetCurrentThreadId() {
536 return wxThread::GetCurrentId();
539 static PyThreadState
* gs_shutdownTState
;
541 PyThreadState
* wxPyGetThreadState() {
542 if (wxPyTMutex
== NULL
) // Python is shutting down...
543 return gs_shutdownTState
;
545 unsigned long ctid
= wxPyGetCurrentThreadId();
546 PyThreadState
* tstate
= NULL
;
549 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
550 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
551 if (info
.tid
== ctid
) {
552 tstate
= info
.tstate
;
556 wxPyTMutex
->Unlock();
557 wxASSERT_MSG(tstate
, "PyThreadState should not be NULL!");
562 void wxPySaveThreadState(PyThreadState
* tstate
) {
563 if (wxPyTMutex
== NULL
) { // Python is shutting down, assume a single thread...
564 gs_shutdownTState
= tstate
;
567 unsigned long ctid
= wxPyGetCurrentThreadId();
569 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
570 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
571 if (info
.tid
== ctid
) {
572 info
.tstate
= tstate
;
573 wxPyTMutex
->Unlock();
577 // not found, so add it...
578 wxPyTStates
->Add(new wxPyThreadState(ctid
, tstate
));
579 wxPyTMutex
->Unlock();
585 // Calls from Python to wxWindows code are wrapped in calls to these
588 PyThreadState
* wxPyBeginAllowThreads() {
589 #ifdef WXP_WITH_THREAD
590 PyThreadState
* saved
= PyEval_SaveThread(); // Py_BEGIN_ALLOW_THREADS;
591 wxPySaveThreadState(saved
);
598 void wxPyEndAllowThreads(PyThreadState
* saved
) {
599 #ifdef WXP_WITH_THREAD
600 PyEval_RestoreThread(saved
); // Py_END_ALLOW_THREADS;
606 // Calls from wxWindows back to Python code, or even any PyObject
607 // manipulations, PyDECREF's and etc. are wrapped in calls to these functions:
609 void wxPyBeginBlockThreads() {
610 #ifdef WXP_WITH_THREAD
611 PyThreadState
* tstate
= wxPyGetThreadState();
612 PyEval_RestoreThread(tstate
);
617 void wxPyEndBlockThreads() {
618 #ifdef WXP_WITH_THREAD
619 PyThreadState
* tstate
= PyEval_SaveThread();
620 // Is there any need to save it again?
625 //---------------------------------------------------------------------------
626 // wxPyInputStream and wxPyCBInputStream methods
628 #include <wx/listimpl.cpp>
629 WX_DEFINE_LIST(wxStringPtrList
);
632 void wxPyInputStream::close() {
636 void wxPyInputStream::flush() {
640 bool wxPyInputStream::eof() {
642 return m_wxis
->Eof();
647 wxPyInputStream::~wxPyInputStream() {
651 wxString
* wxPyInputStream::read(int size
) {
653 const int BUFSIZE
= 1024;
655 // check if we have a real wxInputStream to work with
657 PyErr_SetString(PyExc_IOError
, "no valid C-wxInputStream");
663 char * buf
= new char[BUFSIZE
];
677 while (! m_wxis
->Eof()) {
678 m_wxis
->Read(buf
, BUFSIZE
);
679 s
->Append(buf
, m_wxis
->LastRead());
684 if (m_wxis
->LastError() == wxSTREAM_READ_ERROR
) {
686 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
690 } else { // Read only size number of characters
698 m_wxis
->Read(s
->GetWriteBuf(size
+1), size
);
699 s
->UngetWriteBuf(m_wxis
->LastRead());
702 if (m_wxis
->LastError() == wxSTREAM_READ_ERROR
) {
704 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
712 wxString
* wxPyInputStream::readline (int size
) {
713 // check if we have a real wxInputStream to work with
715 PyErr_SetString(PyExc_IOError
,"no valid C-wxInputStream");
722 wxString
* s
= new wxString
;
728 // read until \n or byte limit reached
729 for (i
=ch
=0; (ch
!= '\n') && (!m_wxis
->Eof()) && ((size
< 0) || (i
< size
)); i
++) {
730 *s
+= ch
= m_wxis
->GetC();
734 if (m_wxis
->LastError() == wxSTREAM_READ_ERROR
) {
736 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
743 wxStringPtrList
* wxPyInputStream::readlines (int sizehint
) {
744 // check if we have a real wxInputStream to work with
746 PyErr_SetString(PyExc_IOError
,"no valid C-wxInputStream below");
751 wxStringPtrList
* l
= new wxStringPtrList();
757 // read sizehint bytes or until EOF
759 for (i
=0; (!m_wxis
->Eof()) && ((sizehint
< 0) || (i
< sizehint
));) {
760 wxString
* s
= readline();
762 l
->DeleteContents(TRUE
);
771 if (m_wxis
->LastError() == wxSTREAM_READ_ERROR
) {
772 l
->DeleteContents(TRUE
);
774 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
)) { // TODO: unicode?
865 o
= PyString_Size(result
);
867 m_lasterror
= wxSTREAM_EOF
;
870 memcpy((char*)buffer
, PyString_AsString(result
), o
);
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();
950 // TODO: get rid of this ifdef by changing wxPyConstructObject to take a wxString
952 char *classNameAsChrStr
= new char[className
.Len()+1];
953 unicodeToChar(&className
, classNameAsChrStr
);
954 arg
= wxPyConstructObject((void*)&event
, classNameAsChrStr
);
955 delete [] classNameAsChrStr
;
957 arg
= wxPyConstructObject((void*)&event
, className
);
961 tuple
= PyTuple_New(1);
962 PyTuple_SET_ITEM(tuple
, 0, arg
);
963 result
= PyEval_CallObject(func
, tuple
);
967 PyErr_Clear(); // Just in case...
971 wxPyEndBlockThreads();
975 //----------------------------------------------------------------------
977 wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper
& other
) {
979 m_self
= other
.m_self
;
980 m_class
= other
.m_class
;
988 void wxPyCallbackHelper::setSelf(PyObject
* self
, PyObject
* klass
, int incref
) {
999 #if PYTHON_API_VERSION >= 1011
1001 // Prior to Python 2.2 PyMethod_GetClass returned the class object
1002 // in which the method was defined. Starting with 2.2 it returns
1003 // "class that asked for the method" which seems totally bogus to me
1004 // but apprently it fixes some obscure problem waiting to happen in
1005 // Python. Since the API was not documented Guido and the gang felt
1006 // safe in changing it. Needless to say that totally screwed up the
1007 // logic below in wxPyCallbackHelper::findCallback, hence this icky
1008 // code to find the class where the method is actually defined...
1011 PyObject
* PyFindClassWithAttr(PyObject
*klass
, PyObject
*name
)
1015 if (PyType_Check(klass
)) { // new style classes
1016 // This code is borrowed/adapted from _PyType_Lookup in typeobject.c
1017 // (TODO: This part is not tested yet, so I'm not sure it is correct...)
1018 PyTypeObject
* type
= (PyTypeObject
*)klass
;
1019 PyObject
*mro
, *res
, *base
, *dict
;
1020 /* Look in tp_dict of types in MRO */
1022 assert(PyTuple_Check(mro
));
1023 n
= PyTuple_GET_SIZE(mro
);
1024 for (i
= 0; i
< n
; i
++) {
1025 base
= PyTuple_GET_ITEM(mro
, i
);
1026 if (PyClass_Check(base
))
1027 dict
= ((PyClassObject
*)base
)->cl_dict
;
1029 assert(PyType_Check(base
));
1030 dict
= ((PyTypeObject
*)base
)->tp_dict
;
1032 assert(dict
&& PyDict_Check(dict
));
1033 res
= PyDict_GetItem(dict
, name
);
1040 else if (PyClass_Check(klass
)) { // old style classes
1041 // This code is borrowed/adapted from class_lookup in classobject.c
1042 PyClassObject
* cp
= (PyClassObject
*)klass
;
1043 PyObject
*value
= PyDict_GetItem(cp
->cl_dict
, name
);
1044 if (value
!= NULL
) {
1045 return (PyObject
*)cp
;
1047 n
= PyTuple_Size(cp
->cl_bases
);
1048 for (i
= 0; i
< n
; i
++) {
1049 PyObject
* base
= PyTuple_GetItem(cp
->cl_bases
, i
);
1050 PyObject
*v
= PyFindClassWithAttr(base
, name
);
1062 PyObject
* PyMethod_GetDefiningClass(PyObject
* method
, const char* name
)
1064 PyObject
* mgc
= PyMethod_GET_CLASS(method
);
1066 #if PYTHON_API_VERSION <= 1010 // prior to Python 2.2, the easy way
1068 #else // 2.2 and after, the hard way...
1070 PyObject
* nameo
= PyString_FromString(name
);
1071 PyObject
* klass
= PyFindClassWithAttr(mgc
, nameo
);
1079 bool wxPyCallbackHelper::findCallback(const char* name
) const {
1080 wxPyCallbackHelper
* self
= (wxPyCallbackHelper
*)this; // cast away const
1081 self
->m_lastFound
= NULL
;
1083 // If the object (m_self) has an attibute of the given name...
1084 if (m_self
&& PyObject_HasAttrString(m_self
, (char*)name
)) {
1085 PyObject
*method
, *klass
;
1086 method
= PyObject_GetAttrString(m_self
, (char*)name
);
1088 // ...and if that attribute is a method, and if that method's class is
1089 // not from a base class...
1090 if (PyMethod_Check(method
) &&
1091 (klass
= PyMethod_GetDefiningClass(method
, (char*)name
)) != NULL
&&
1092 ((klass
== m_class
) || PyClass_IsSubclass(klass
, m_class
))) {
1094 // ...then we'll save a pointer to the method so callCallback can call it.
1095 self
->m_lastFound
= method
;
1101 return m_lastFound
!= NULL
;
1105 int wxPyCallbackHelper::callCallback(PyObject
* argTuple
) const {
1109 result
= callCallbackObj(argTuple
);
1110 if (result
) { // Assumes an integer return type...
1111 retval
= PyInt_AsLong(result
);
1113 PyErr_Clear(); // forget about it if it's not...
1118 // Invoke the Python callable object, returning the raw PyObject return
1119 // value. Caller should DECREF the return value and also call PyEval_SaveThread.
1120 PyObject
* wxPyCallbackHelper::callCallbackObj(PyObject
* argTuple
) const {
1123 // Save a copy of the pointer in case the callback generates another
1124 // callback. In that case m_lastFound will have a different value when
1125 // it gets back here...
1126 PyObject
* method
= m_lastFound
;
1128 result
= PyEval_CallObject(method
, argTuple
);
1129 Py_DECREF(argTuple
);
1138 void wxPyCBH_setCallbackInfo(wxPyCallbackHelper
& cbh
, PyObject
* self
, PyObject
* klass
, int incref
) {
1139 cbh
.setSelf(self
, klass
, incref
);
1142 bool wxPyCBH_findCallback(const wxPyCallbackHelper
& cbh
, const char* name
) {
1143 return cbh
.findCallback(name
);
1146 int wxPyCBH_callCallback(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
1147 return cbh
.callCallback(argTuple
);
1150 PyObject
* wxPyCBH_callCallbackObj(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
1151 return cbh
.callCallbackObj(argTuple
);
1155 void wxPyCBH_delete(wxPyCallbackHelper
* cbh
) {
1156 if (cbh
->m_incRef
) {
1157 wxPyBeginBlockThreads();
1158 Py_XDECREF(cbh
->m_self
);
1159 Py_XDECREF(cbh
->m_class
);
1160 wxPyEndBlockThreads();
1164 //---------------------------------------------------------------------------
1165 //---------------------------------------------------------------------------
1166 // These event classes can be derived from in Python and passed through the event
1167 // system without losing anything. They do this by keeping a reference to
1168 // themselves and some special case handling in wxPyCallback::EventThunker.
1171 wxPyEvtSelfRef::wxPyEvtSelfRef() {
1172 //m_self = Py_None; // **** We don't do normal ref counting to prevent
1173 //Py_INCREF(m_self); // circular loops...
1177 wxPyEvtSelfRef::~wxPyEvtSelfRef() {
1178 wxPyBeginBlockThreads();
1181 wxPyEndBlockThreads();
1184 void wxPyEvtSelfRef::SetSelf(PyObject
* self
, bool clone
) {
1185 wxPyBeginBlockThreads();
1193 wxPyEndBlockThreads();
1196 PyObject
* wxPyEvtSelfRef::GetSelf() const {
1202 IMPLEMENT_ABSTRACT_CLASS(wxPyEvent
, wxEvent
);
1203 IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent
, wxCommandEvent
);
1206 wxPyEvent::wxPyEvent(int id
)
1211 wxPyEvent::wxPyEvent(const wxPyEvent
& evt
)
1214 SetSelf(evt
.m_self
, TRUE
);
1218 wxPyEvent::~wxPyEvent() {
1222 wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType
, int id
)
1223 : wxCommandEvent(commandType
, id
) {
1227 wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent
& evt
)
1228 : wxCommandEvent(evt
)
1230 SetSelf(evt
.m_self
, TRUE
);
1234 wxPyCommandEvent::~wxPyCommandEvent() {
1240 //---------------------------------------------------------------------------
1241 //---------------------------------------------------------------------------
1244 wxPyTimer::wxPyTimer(PyObject
* callback
) {
1249 wxPyTimer::~wxPyTimer() {
1250 wxPyBeginBlockThreads();
1252 wxPyEndBlockThreads();
1255 void wxPyTimer::Notify() {
1256 if (!func
|| func
== Py_None
) {
1260 wxPyBeginBlockThreads();
1263 PyObject
* args
= Py_BuildValue("()");
1265 result
= PyEval_CallObject(func
, args
);
1274 wxPyEndBlockThreads();
1280 //---------------------------------------------------------------------------
1281 //---------------------------------------------------------------------------
1282 // Convert a wxList to a Python List
1284 PyObject
* wxPy_ConvertList(wxListBase
* list
, const char* className
) {
1288 wxNode
* node
= list
->First();
1290 wxPyBeginBlockThreads();
1291 pyList
= PyList_New(0);
1293 wxObj
= node
->Data();
1294 pyObj
= wxPyMake_wxObject(wxObj
); //wxPyConstructObject(wxObj, className);
1295 PyList_Append(pyList
, pyObj
);
1296 node
= node
->Next();
1298 wxPyEndBlockThreads();
1302 //----------------------------------------------------------------------
1304 long wxPyGetWinHandle(wxWindow
* win
) {
1306 return (long)win
->GetHandle();
1309 // Find and return the actual X-Window.
1311 if (win
->m_wxwindow
) {
1312 GdkWindowPrivate
* bwin
= (GdkWindowPrivate
*)GTK_PIZZA(win
->m_wxwindow
)->bin_window
;
1314 return (long)bwin
->xwindow
;
1321 //----------------------------------------------------------------------
1322 // Some helper functions for typemaps in my_typemaps.i, so they won't be
1323 // included in every file over and over again...
1325 #if PYTHON_API_VERSION >= 1009
1326 static char* wxStringErrorMsg
= "String or Unicode type required";
1328 static char* wxStringErrorMsg
= "String type required";
1332 wxString
* wxString_in_helper(PyObject
* source
) {
1334 #if PYTHON_API_VERSION >= 1009 // Have Python unicode API
1335 if (!PyString_Check(source
) && !PyUnicode_Check(source
)) {
1336 PyErr_SetString(PyExc_TypeError
, wxStringErrorMsg
);
1340 if (PyUnicode_Check(source
)) {
1341 target
= new wxString(PyUnicode_AS_UNICODE(source
));
1343 // It is a string, transform to unicode
1344 PyObject
*tempUniStr
= PyObject_Unicode(source
);
1345 target
= new wxString(PyUnicode_AS_UNICODE(tempUniStr
));
1346 Py_DECREF(tempUniStr
);
1349 char* tmpPtr
; int tmpSize
;
1350 if (PyString_AsStringAndSize(source
, &tmpPtr
, &tmpSize
) == -1) {
1351 PyErr_SetString(PyExc_TypeError
, "Unable to convert string");
1354 target
= new wxString(tmpPtr
, tmpSize
);
1355 #endif // wxUSE_UNICODE
1357 #else // No Python unicode API (1.5.2)
1358 if (!PyString_Check(source
)) {
1359 PyErr_SetString(PyExc_TypeError
, wxStringErrorMsg
);
1362 target
= new wxString(PyString_AS_STRING(source
), PyString_GET_SIZE(source
));
1369 byte
* byte_LIST_helper(PyObject
* source
) {
1370 if (!PyList_Check(source
)) {
1371 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1374 int count
= PyList_Size(source
);
1375 byte
* temp
= new byte
[count
];
1377 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1380 for (int x
=0; x
<count
; x
++) {
1381 PyObject
* o
= PyList_GetItem(source
, x
);
1382 if (! PyInt_Check(o
)) {
1383 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1386 temp
[x
] = (byte
)PyInt_AsLong(o
);
1392 int* int_LIST_helper(PyObject
* source
) {
1393 if (!PyList_Check(source
)) {
1394 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1397 int count
= PyList_Size(source
);
1398 int* temp
= new int[count
];
1400 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1403 for (int x
=0; x
<count
; x
++) {
1404 PyObject
* o
= PyList_GetItem(source
, x
);
1405 if (! PyInt_Check(o
)) {
1406 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1409 temp
[x
] = PyInt_AsLong(o
);
1415 long* long_LIST_helper(PyObject
* source
) {
1416 if (!PyList_Check(source
)) {
1417 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1420 int count
= PyList_Size(source
);
1421 long* temp
= new long[count
];
1423 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1426 for (int x
=0; x
<count
; x
++) {
1427 PyObject
* o
= PyList_GetItem(source
, x
);
1428 if (! PyInt_Check(o
)) {
1429 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1432 temp
[x
] = PyInt_AsLong(o
);
1438 char** string_LIST_helper(PyObject
* source
) {
1439 if (!PyList_Check(source
)) {
1440 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1443 int count
= PyList_Size(source
);
1444 char** temp
= new char*[count
];
1446 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1449 for (int x
=0; x
<count
; x
++) {
1450 PyObject
* o
= PyList_GetItem(source
, x
);
1451 if (! PyString_Check(o
)) {
1452 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1455 temp
[x
] = PyString_AsString(o
);
1460 //--------------------------------
1461 // Part of patch from Tim Hochberg
1462 static inline bool wxPointFromObjects(PyObject
* o1
, PyObject
* o2
, wxPoint
* point
) {
1463 if (PyInt_Check(o1
) && PyInt_Check(o2
)) {
1464 point
->x
= PyInt_AS_LONG(o1
);
1465 point
->y
= PyInt_AS_LONG(o2
);
1468 if (PyFloat_Check(o1
) && PyFloat_Check(o2
)) {
1469 point
->x
= (int)PyFloat_AS_DOUBLE(o1
);
1470 point
->y
= (int)PyFloat_AS_DOUBLE(o2
);
1473 if (PyInstance_Check(o1
) || PyInstance_Check(o2
)) {
1474 // Disallow instances because they can cause havok
1477 if (PyNumber_Check(o1
) && PyNumber_Check(o2
)) {
1478 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
1479 point
->x
= PyInt_AsLong(o1
);
1480 point
->y
= PyInt_AsLong(o2
);
1487 wxPoint
* wxPoint_LIST_helper(PyObject
* source
, int *count
) {
1488 // Putting all of the declarations here allows
1489 // us to put the error handling all in one place.
1492 PyObject
*o
, *o1
, *o2
;
1493 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1495 if (!PySequence_Check(source
)) {
1499 // The length of the sequence is returned in count.
1500 *count
= PySequence_Length(source
);
1505 temp
= new wxPoint
[*count
];
1507 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1510 for (x
=0; x
<*count
; x
++) {
1511 // Get an item: try fast way first.
1513 o
= PySequence_Fast_GET_ITEM(source
, x
);
1516 o
= PySequence_GetItem(source
, x
);
1522 // Convert o to wxPoint.
1523 if ((PyTuple_Check(o
) && PyTuple_GET_SIZE(o
) == 2) ||
1524 (PyList_Check(o
) && PyList_GET_SIZE(o
) == 2)) {
1525 o1
= PySequence_Fast_GET_ITEM(o
, 0);
1526 o2
= PySequence_Fast_GET_ITEM(o
, 1);
1527 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
1531 else if (PyInstance_Check(o
)) {
1533 if (SWIG_GetPtrObj(o
, (void **)&pt
, "_wxPoint_p")) {
1538 else if (PySequence_Check(o
) && PySequence_Length(o
) == 2) {
1539 o1
= PySequence_GetItem(o
, 0);
1540 o2
= PySequence_GetItem(o
, 1);
1541 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
1565 PyErr_SetString(PyExc_TypeError
, "Expected a sequence of length-2 sequences or wxPoints.");
1569 //------------------------------
1572 wxBitmap
** wxBitmap_LIST_helper(PyObject
* source
) {
1573 if (!PyList_Check(source
)) {
1574 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1577 int count
= PyList_Size(source
);
1578 wxBitmap
** temp
= new wxBitmap
*[count
];
1580 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1583 for (int x
=0; x
<count
; x
++) {
1584 PyObject
* o
= PyList_GetItem(source
, x
);
1585 if (PyInstance_Check(o
)) {
1587 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxBitmap_p")) {
1588 PyErr_SetString(PyExc_TypeError
,"Expected _wxBitmap_p.");
1594 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxBitmaps.");
1603 wxString
* wxString_LIST_helper(PyObject
* source
) {
1604 if (!PyList_Check(source
)) {
1605 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1608 int count
= PyList_Size(source
);
1609 wxString
* temp
= new wxString
[count
];
1611 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1614 for (int x
=0; x
<count
; x
++) {
1615 PyObject
* o
= PyList_GetItem(source
, x
);
1616 #if PYTHON_API_VERSION >= 1009
1617 if (! PyString_Check(o
) && ! PyUnicode_Check(o
)) {
1618 PyErr_SetString(PyExc_TypeError
, "Expected a list of string or unicode objects.");
1624 if (PyString_AsStringAndSize(o
, &buff
, &length
) == -1)
1626 #if wxUSE_UNICODE // TODO: unicode fix. this is wrong!
1627 wxChar
*uniBuff
= wCharFromCStr(buff
);
1628 temp
[x
] = wxString(uniBuff
, length
);
1631 temp
[x
] = wxString(buff
, length
);
1632 #endif //wxUSE_UNICODE
1634 if (! PyString_Check(o
)) {
1635 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1638 temp
[x
] = PyString_AsString(o
);
1645 wxAcceleratorEntry
* wxAcceleratorEntry_LIST_helper(PyObject
* source
) {
1646 if (!PyList_Check(source
)) {
1647 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1650 int count
= PyList_Size(source
);
1651 wxAcceleratorEntry
* temp
= new wxAcceleratorEntry
[count
];
1653 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1656 for (int x
=0; x
<count
; x
++) {
1657 PyObject
* o
= PyList_GetItem(source
, x
);
1658 if (PyInstance_Check(o
)) {
1659 wxAcceleratorEntry
* ae
;
1660 if (SWIG_GetPtrObj(o
, (void **) &ae
,"_wxAcceleratorEntry_p")) {
1661 PyErr_SetString(PyExc_TypeError
,"Expected _wxAcceleratorEntry_p.");
1666 else if (PyTuple_Check(o
)) {
1667 PyObject
* o1
= PyTuple_GetItem(o
, 0);
1668 PyObject
* o2
= PyTuple_GetItem(o
, 1);
1669 PyObject
* o3
= PyTuple_GetItem(o
, 2);
1670 temp
[x
].Set(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
));
1673 PyErr_SetString(PyExc_TypeError
, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
1681 wxPen
** wxPen_LIST_helper(PyObject
* source
) {
1682 if (!PyList_Check(source
)) {
1683 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1686 int count
= PyList_Size(source
);
1687 wxPen
** temp
= new wxPen
*[count
];
1689 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1692 for (int x
=0; x
<count
; x
++) {
1693 PyObject
* o
= PyList_GetItem(source
, x
);
1694 if (PyInstance_Check(o
)) {
1696 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxPen_p")) {
1698 PyErr_SetString(PyExc_TypeError
,"Expected _wxPen_p.");
1705 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxPens.");
1713 bool _2int_seq_helper(PyObject
* source
, int* i1
, int* i2
) {
1714 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1717 if (!PySequence_Check(source
) || PySequence_Length(source
) != 2)
1721 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1722 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1725 o1
= PySequence_GetItem(source
, 0);
1726 o2
= PySequence_GetItem(source
, 1);
1729 *i1
= PyInt_AsLong(o1
);
1730 *i2
= PyInt_AsLong(o2
);
1740 bool _4int_seq_helper(PyObject
* source
, int* i1
, int* i2
, int* i3
, int* i4
) {
1741 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1742 PyObject
*o1
, *o2
, *o3
, *o4
;
1744 if (!PySequence_Check(source
) || PySequence_Length(source
) != 4)
1748 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1749 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1750 o3
= PySequence_Fast_GET_ITEM(source
, 2);
1751 o4
= PySequence_Fast_GET_ITEM(source
, 3);
1754 o1
= PySequence_GetItem(source
, 0);
1755 o2
= PySequence_GetItem(source
, 1);
1756 o3
= PySequence_GetItem(source
, 2);
1757 o4
= PySequence_GetItem(source
, 3);
1760 *i1
= PyInt_AsLong(o1
);
1761 *i2
= PyInt_AsLong(o2
);
1762 *i3
= PyInt_AsLong(o3
);
1763 *i4
= PyInt_AsLong(o4
);
1775 //----------------------------------------------------------------------
1777 bool wxSize_helper(PyObject
* source
, wxSize
** obj
) {
1779 // If source is an object instance then it may already be the right type
1780 if (PyInstance_Check(source
)) {
1782 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxSize_p"))
1787 // otherwise a 2-tuple of integers is expected
1788 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1789 PyObject
* o1
= PySequence_GetItem(source
, 0);
1790 PyObject
* o2
= PySequence_GetItem(source
, 1);
1791 **obj
= wxSize(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1796 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxSize object.");
1800 bool wxPoint_helper(PyObject
* source
, wxPoint
** obj
) {
1802 // If source is an object instance then it may already be the right type
1803 if (PyInstance_Check(source
)) {
1805 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxPoint_p"))
1810 // otherwise a length-2 sequence of integers is expected
1811 if (PySequence_Check(source
) && PySequence_Length(source
) == 2) {
1812 PyObject
* o1
= PySequence_GetItem(source
, 0);
1813 PyObject
* o2
= PySequence_GetItem(source
, 1);
1814 // This should really check for integers, not numbers -- but that would break code.
1815 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1820 **obj
= wxPoint(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1826 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxPoint object.");
1832 bool wxRealPoint_helper(PyObject
* source
, wxRealPoint
** obj
) {
1834 // If source is an object instance then it may already be the right type
1835 if (PyInstance_Check(source
)) {
1837 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRealPoint_p"))
1842 // otherwise a 2-tuple of floats is expected
1843 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1844 PyObject
* o1
= PySequence_GetItem(source
, 0);
1845 PyObject
* o2
= PySequence_GetItem(source
, 1);
1846 **obj
= wxRealPoint(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
));
1851 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxRealPoint object.");
1858 bool wxRect_helper(PyObject
* source
, wxRect
** obj
) {
1860 // If source is an object instance then it may already be the right type
1861 if (PyInstance_Check(source
)) {
1863 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRect_p"))
1868 // otherwise a 4-tuple of integers is expected
1869 else if (PySequence_Check(source
) && PyObject_Length(source
) == 4) {
1870 PyObject
* o1
= PySequence_GetItem(source
, 0);
1871 PyObject
* o2
= PySequence_GetItem(source
, 1);
1872 PyObject
* o3
= PySequence_GetItem(source
, 2);
1873 PyObject
* o4
= PySequence_GetItem(source
, 3);
1874 **obj
= wxRect(PyInt_AsLong(o1
), PyInt_AsLong(o2
),
1875 PyInt_AsLong(o3
), PyInt_AsLong(o4
));
1880 PyErr_SetString(PyExc_TypeError
, "Expected a 4-tuple of integers or a wxRect object.");
1886 bool wxColour_helper(PyObject
* source
, wxColour
** obj
) {
1888 // If source is an object instance then it may already be the right type
1889 if (PyInstance_Check(source
)) {
1891 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxColour_p"))
1896 // otherwise a string is expected
1897 else if (PyString_Check(source
)) {
1898 wxString spec
= PyString_AS_STRING(source
);
1899 if (spec
[0U] == '#' && spec
.Length() == 7) { // It's #RRGGBB
1901 #if wxUSE_UNICODE // TODO: unicode fix.
1902 // This ifdef can be removed by using wxString methods to
1903 // convert to long instead of strtol
1904 char *tmpAsChar
= new char[spec
.Len()+1];
1905 unicodeToChar(&spec
.Mid(1,2), tmpAsChar
);
1906 int red
= strtol(tmpAsChar
, &junk
, 16);
1907 unicodeToChar(&spec
.Mid(3,2), tmpAsChar
);
1908 int green
= strtol(tmpAsChar
, &junk
, 16);
1909 unicodeToChar(&spec
.Mid(5,2), tmpAsChar
);
1910 int blue
= strtol(tmpAsChar
, &junk
, 16);
1911 delete [] tmpAsChar
;
1913 int red
= strtol(spec
.Mid(1,2), &junk
, 16);
1914 int green
= strtol(spec
.Mid(3,2), &junk
, 16);
1915 int blue
= strtol(spec
.Mid(5,2), &junk
, 16);
1917 **obj
= wxColour(red
, green
, blue
);
1920 else { // it's a colour name
1921 **obj
= wxColour(spec
);
1927 PyErr_SetString(PyExc_TypeError
, "Expected a wxColour object or a string containing a colour name or '#RRGGBB'.");
1932 //----------------------------------------------------------------------
1934 PyObject
* wxArrayString2PyList_helper(const wxArrayString
& arr
) {
1936 PyObject
* list
= PyList_New(0);
1937 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
1939 PyObject
* str
= PyUnicode_FromUnicode(arr
[i
].c_str(), arr
[i
].Len());
1941 PyObject
* str
= PyString_FromStringAndSize(arr
[i
].c_str(), arr
[i
].Len());
1943 PyList_Append(list
, str
);
1950 PyObject
* wxArrayInt2PyList_helper(const wxArrayInt
& arr
) {
1952 PyObject
* list
= PyList_New(0);
1953 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
1954 PyObject
* number
= PyInt_FromLong(arr
[i
]);
1955 PyList_Append(list
, number
);
1962 //----------------------------------------------------------------------
1963 //----------------------------------------------------------------------