]>
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
;
299 PyObject
* __wxSetDictionary(PyObject
* /* self */, PyObject
* args
)
302 if (!PyArg_ParseTuple(args
, "O", &wxPython_dict
))
305 if (!PyDict_Check(wxPython_dict
)) {
306 PyErr_SetString(PyExc_TypeError
, "_wxSetDictionary must have dictionary object!");
310 if (! wxPyPtrTypeMap
)
311 wxPyPtrTypeMap
= PyDict_New();
312 PyDict_SetItemString(wxPython_dict
, "__wxPyPtrTypeMap", wxPyPtrTypeMap
);
316 #define wxPlatform "__WXMOTIF__"
319 #define wxPlatform "__WXX11__"
322 #define wxPlatform "__WXGTK__"
324 #if defined(__WIN32__) || defined(__WXMSW__)
325 #define wxPlatform "__WXMSW__"
328 #define wxPlatform "__WXMAC__"
331 PyDict_SetItemString(wxPython_dict
, "wxPlatform", PyString_FromString(wxPlatform
));
332 PyDict_SetItemString(wxPython_dict
, "wxUSE_UNICODE", PyInt_FromLong(wxUSE_UNICODE
));
338 //---------------------------------------------------------------------------
340 void wxPyClientData_dtor(wxPyClientData
* self
) {
341 wxPyBeginBlockThreads();
342 Py_DECREF(self
->m_obj
);
343 wxPyEndBlockThreads();
346 void wxPyUserData_dtor(wxPyUserData
* self
) {
347 wxPyBeginBlockThreads();
348 Py_DECREF(self
->m_obj
);
349 wxPyEndBlockThreads();
353 // This is called when an OOR controled object is being destroyed. Although
354 // the C++ object is going away there is no way to force the Python object
355 // (and all references to it) to die too. This causes problems (crashes) in
356 // wxPython when a python shadow object attempts to call a C++ method using
357 // the now bogus pointer... So to try and prevent this we'll do a little black
358 // magic and change the class of the python instance to a class that will
359 // raise an exception for any attempt to call methods with it. See
360 // _wxPyDeadObject in _extras.py for the implementation of this class.
361 void wxPyOORClientData_dtor(wxPyOORClientData
* self
) {
363 static PyObject
* deadObjectClass
= NULL
;
365 wxPyBeginBlockThreads();
366 if (deadObjectClass
== NULL
) {
367 deadObjectClass
= PyDict_GetItemString(wxPython_dict
, "_wxPyDeadObject");
368 wxASSERT_MSG(deadObjectClass
!= NULL
, wxT("Can't get _wxPyDeadObject class!"));
369 Py_INCREF(deadObjectClass
);
372 // Clear the instance's dictionary, put the name of the old class into the
373 // instance, and then reset the class to be the dead class.
374 if (self
->m_obj
->ob_refcnt
> 1) { // but only if there is more than one reference
375 wxASSERT_MSG(PyInstance_Check(self
->m_obj
), wxT("m_obj not an instance!?!?!"));
376 PyInstanceObject
* inst
= (PyInstanceObject
*)self
->m_obj
;
377 PyDict_Clear(inst
->in_dict
);
378 PyDict_SetItemString(inst
->in_dict
, "_name", inst
->in_class
->cl_name
);
379 inst
->in_class
= (PyClassObject
*)deadObjectClass
;
380 Py_INCREF(deadObjectClass
);
382 wxPyEndBlockThreads();
385 //---------------------------------------------------------------------------
386 // Stuff used by OOR to find the right wxPython class type to return and to
390 // The pointer type map is used when the "pointer" type name generated by SWIG
391 // is not the same as the shadow class name, for example wxPyTreeCtrl
392 // vs. wxTreeCtrl. It needs to be referenced in Python as well as from C++,
393 // so we'll just make it a Python dictionary in the wx module's namespace.
394 // (See __wxSetDictionary)
395 void wxPyPtrTypeMap_Add(const char* commonName
, const char* ptrName
) {
396 if (! wxPyPtrTypeMap
)
397 wxPyPtrTypeMap
= PyDict_New();
398 PyDict_SetItemString(wxPyPtrTypeMap
,
400 PyString_FromString((char*)ptrName
));
405 PyObject
* wxPyClassExists(const wxString
& className
) {
410 char buff
[64]; // should always be big enough...
412 sprintf(buff
, "%sPtr", className
.mbc_str());
413 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
415 return classobj
; // returns NULL if not found
419 PyObject
* wxPyMake_wxObject(wxObject
* source
, bool checkEvtHandler
) {
420 PyObject
* target
= NULL
;
421 bool isEvtHandler
= FALSE
;
424 // If it's derived from wxEvtHandler then there may
425 // already be a pointer to a Python object that we can use
427 if (checkEvtHandler
&& wxIsKindOf(source
, wxEvtHandler
)) {
429 wxEvtHandler
* eh
= (wxEvtHandler
*)source
;
430 wxPyOORClientData
* data
= (wxPyOORClientData
*)eh
->GetClientObject();
432 target
= data
->m_obj
;
438 // Otherwise make it the old fashioned way by making a
439 // new shadow object and putting this pointer in it.
440 wxClassInfo
* info
= source
->GetClassInfo();
441 wxChar
* name
= (wxChar
*)info
->GetClassName();
442 PyObject
* klass
= wxPyClassExists(name
);
443 while (info
&& !klass
) {
444 name
= (wxChar
*)info
->GetBaseClassName1();
445 info
= wxClassInfo::FindClass(name
);
446 klass
= wxPyClassExists(name
);
449 target
= wxPyConstructObject(source
, name
, klass
, FALSE
);
450 if (target
&& isEvtHandler
)
451 ((wxEvtHandler
*)source
)->SetClientObject(new wxPyOORClientData(target
));
453 wxString
msg(wxT("wxPython class not found for "));
454 msg
+= source
->GetClassInfo()->GetClassName();
455 PyErr_SetString(PyExc_NameError
, msg
.mbc_str());
459 } else { // source was NULL so return None.
460 Py_INCREF(Py_None
); target
= Py_None
;
466 PyObject
* wxPyMake_wxSizer(wxSizer
* source
) {
467 PyObject
* target
= NULL
;
469 if (source
&& wxIsKindOf(source
, wxSizer
)) {
470 // If it's derived from wxSizer then there may
471 // already be a pointer to a Python object that we can use
473 wxSizer
* sz
= (wxSizer
*)source
;
474 wxPyOORClientData
* data
= (wxPyOORClientData
*)sz
->GetClientObject();
476 target
= data
->m_obj
;
481 target
= wxPyMake_wxObject(source
, FALSE
);
482 if (target
!= Py_None
)
483 ((wxSizer
*)source
)->SetClientObject(new wxPyOORClientData(target
));
490 //---------------------------------------------------------------------------
492 PyObject
* wxPyConstructObject(void* ptr
,
493 const wxString
& className
,
500 wxString
name(className
);
501 char swigptr
[64]; // should always be big enough...
504 if ((item
= PyDict_GetItemString(wxPyPtrTypeMap
, (char*)(const char*)name
.mbc_str())) != NULL
) {
505 name
= wxString(PyString_AsString(item
), *wxConvCurrent
);
507 sprintf(buff
, "_%s_p", (const char*)name
.mbc_str());
508 SWIG_MakePtr(swigptr
, ptr
, buff
);
510 arg
= Py_BuildValue("(s)", swigptr
);
511 obj
= PyInstance_New(klass
, arg
, NULL
);
515 PyObject
* one
= PyInt_FromLong(1);
516 PyObject_SetAttrString(obj
, "thisown", one
);
524 PyObject
* wxPyConstructObject(void* ptr
,
525 const wxString
& className
,
534 char buff
[64]; // should always be big enough...
535 sprintf(buff
, "%sPtr", (const char*)className
.mbc_str());
537 wxASSERT_MSG(wxPython_dict
, wxT("wxPython_dict is not set yet!!"));
539 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
543 "*** Unknown class name %s, tell Robin about it please ***",
545 obj
= PyString_FromString(temp
);
549 return wxPyConstructObject(ptr
, className
, classobj
, setThisOwn
);
553 //---------------------------------------------------------------------------
556 #ifdef WXP_WITH_THREAD
558 unsigned long wxPyGetCurrentThreadId() {
559 return wxThread::GetCurrentId();
562 static PyThreadState
* gs_shutdownTState
;
564 PyThreadState
* wxPyGetThreadState() {
565 if (wxPyTMutex
== NULL
) // Python is shutting down...
566 return gs_shutdownTState
;
568 unsigned long ctid
= wxPyGetCurrentThreadId();
569 PyThreadState
* tstate
= NULL
;
572 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
573 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
574 if (info
.tid
== ctid
) {
575 tstate
= info
.tstate
;
579 wxPyTMutex
->Unlock();
580 wxASSERT_MSG(tstate
, wxT("PyThreadState should not be NULL!"));
585 void wxPySaveThreadState(PyThreadState
* tstate
) {
586 if (wxPyTMutex
== NULL
) { // Python is shutting down, assume a single thread...
587 gs_shutdownTState
= tstate
;
590 unsigned long ctid
= wxPyGetCurrentThreadId();
592 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
593 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
594 if (info
.tid
== ctid
) {
595 info
.tstate
= tstate
;
596 wxPyTMutex
->Unlock();
600 // not found, so add it...
601 wxPyTStates
->Add(new wxPyThreadState(ctid
, tstate
));
602 wxPyTMutex
->Unlock();
608 // Calls from Python to wxWindows code are wrapped in calls to these
611 PyThreadState
* wxPyBeginAllowThreads() {
612 #ifdef WXP_WITH_THREAD
613 PyThreadState
* saved
= PyEval_SaveThread(); // Py_BEGIN_ALLOW_THREADS;
614 wxPySaveThreadState(saved
);
621 void wxPyEndAllowThreads(PyThreadState
* saved
) {
622 #ifdef WXP_WITH_THREAD
623 PyEval_RestoreThread(saved
); // Py_END_ALLOW_THREADS;
629 // Calls from wxWindows back to Python code, or even any PyObject
630 // manipulations, PyDECREF's and etc. are wrapped in calls to these functions:
632 void wxPyBeginBlockThreads() {
633 #ifdef WXP_WITH_THREAD
634 PyThreadState
* tstate
= wxPyGetThreadState();
635 PyEval_RestoreThread(tstate
);
640 void wxPyEndBlockThreads() {
641 #ifdef WXP_WITH_THREAD
642 PyThreadState
* tstate
= PyEval_SaveThread();
643 // Is there any need to save it again?
648 //---------------------------------------------------------------------------
649 // wxPyInputStream and wxPyCBInputStream methods
652 void wxPyInputStream::close() {
653 /* do nothing for now */
656 void wxPyInputStream::flush() {
657 /* do nothing for now */
660 bool wxPyInputStream::eof() {
662 return m_wxis
->Eof();
667 wxPyInputStream::~wxPyInputStream() {
674 PyObject
* wxPyInputStream::read(int size
) {
675 PyObject
* obj
= NULL
;
677 const int BUFSIZE
= 1024;
679 // check if we have a real wxInputStream to work with
681 PyErr_SetString(PyExc_IOError
, "no valid C-wxInputStream");
687 while (! m_wxis
->Eof()) {
688 m_wxis
->Read(buf
.GetAppendBuf(BUFSIZE
), BUFSIZE
);
689 buf
.UngetAppendBuf(m_wxis
->LastRead());
692 } else { // Read only size number of characters
693 m_wxis
->Read(buf
.GetWriteBuf(size
), size
);
694 buf
.UngetWriteBuf(m_wxis
->LastRead());
698 if (m_wxis
->LastError() == wxSTREAM_READ_ERROR
) {
699 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
702 // We use only strings for the streams, not unicode
703 obj
= PyString_FromStringAndSize(buf
, buf
.GetDataLen());
709 PyObject
* wxPyInputStream::readline(int size
) {
710 PyObject
* obj
= NULL
;
715 // check if we have a real wxInputStream to work with
717 PyErr_SetString(PyExc_IOError
,"no valid C-wxInputStream");
721 // read until \n or byte limit reached
722 for (i
=ch
=0; (ch
!= '\n') && (!m_wxis
->Eof()) && ((size
< 0) || (i
< size
)); i
++) {
728 if (m_wxis
->LastError() == wxSTREAM_READ_ERROR
) {
729 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
732 // We use only strings for the streams, not unicode
733 obj
= PyString_FromStringAndSize((char*)buf
.GetData(), buf
.GetDataLen());
739 PyObject
* wxPyInputStream::readlines(int sizehint
) {
742 // check if we have a real wxInputStream to work with
744 PyErr_SetString(PyExc_IOError
,"no valid C-wxInputStream below");
749 pylist
= PyList_New(0);
755 // read sizehint bytes or until EOF
757 for (i
=0; (!m_wxis
->Eof()) && ((sizehint
< 0) || (i
< sizehint
));) {
758 PyObject
* s
= this->readline();
763 PyList_Append(pylist
, s
);
764 i
+= PyString_Size(s
);
768 if (m_wxis
->LastError() == wxSTREAM_READ_ERROR
) {
770 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
778 void wxPyInputStream::seek(int offset
, int whence
) {
780 m_wxis
->SeekI(offset
, wxSeekMode(whence
));
783 int wxPyInputStream::tell(){
785 return m_wxis
->TellI();
792 wxPyCBInputStream::wxPyCBInputStream(PyObject
*r
, PyObject
*s
, PyObject
*t
, bool block
)
793 : wxInputStream(), m_read(r
), m_seek(s
), m_tell(t
), m_block(block
)
797 wxPyCBInputStream::~wxPyCBInputStream() {
798 if (m_block
) wxPyBeginBlockThreads();
802 if (m_block
) wxPyEndBlockThreads();
806 wxPyCBInputStream
* wxPyCBInputStream::create(PyObject
*py
, bool block
) {
807 if (block
) wxPyBeginBlockThreads();
809 PyObject
* read
= getMethod(py
, "read");
810 PyObject
* seek
= getMethod(py
, "seek");
811 PyObject
* tell
= getMethod(py
, "tell");
814 PyErr_SetString(PyExc_TypeError
, "Not a file-like object");
818 if (block
) wxPyEndBlockThreads();
822 if (block
) wxPyEndBlockThreads();
823 return new wxPyCBInputStream(read
, seek
, tell
, block
);
826 PyObject
* wxPyCBInputStream::getMethod(PyObject
* py
, char* name
) {
827 if (!PyObject_HasAttrString(py
, name
))
829 PyObject
* o
= PyObject_GetAttrString(py
, name
);
830 if (!PyMethod_Check(o
) && !PyCFunction_Check(o
)) {
838 size_t wxPyCBInputStream::GetSize() const {
839 wxPyCBInputStream
* self
= (wxPyCBInputStream
*)this; // cast off const
840 if (m_seek
&& m_tell
) {
841 off_t temp
= self
->OnSysTell();
842 off_t ret
= self
->OnSysSeek(0, wxFromEnd
);
843 self
->OnSysSeek(temp
, wxFromStart
);
851 size_t wxPyCBInputStream::OnSysRead(void *buffer
, size_t bufsize
) {
855 wxPyBeginBlockThreads();
856 PyObject
* arglist
= Py_BuildValue("(i)", bufsize
);
857 PyObject
* result
= PyEval_CallObject(m_read
, arglist
);
861 if ((result
!= NULL
) && PyString_Check(result
)) {
862 o
= PyString_Size(result
);
864 m_lasterror
= wxSTREAM_EOF
;
867 memcpy((char*)buffer
, PyString_AsString(result
), o
); // strings only, not unicode...
872 m_lasterror
= wxSTREAM_READ_ERROR
;
873 wxPyEndBlockThreads();
878 size_t wxPyCBInputStream::OnSysWrite(const void *buffer
, size_t bufsize
) {
879 m_lasterror
= wxSTREAM_WRITE_ERROR
;
883 off_t
wxPyCBInputStream::OnSysSeek(off_t off
, wxSeekMode mode
) {
884 wxPyBeginBlockThreads();
885 PyObject
* arglist
= Py_BuildValue("(ii)", off
, mode
);
886 PyObject
* result
= PyEval_CallObject(m_seek
, arglist
);
889 wxPyEndBlockThreads();
893 off_t
wxPyCBInputStream::OnSysTell() const {
894 wxPyBeginBlockThreads();
895 PyObject
* arglist
= Py_BuildValue("()");
896 PyObject
* result
= PyEval_CallObject(m_tell
, arglist
);
899 if (result
!= NULL
) {
900 o
= PyInt_AsLong(result
);
903 wxPyEndBlockThreads();
907 //----------------------------------------------------------------------
909 IMPLEMENT_ABSTRACT_CLASS(wxPyCallback
, wxObject
);
911 wxPyCallback::wxPyCallback(PyObject
* func
) {
916 wxPyCallback::wxPyCallback(const wxPyCallback
& other
) {
917 m_func
= other
.m_func
;
921 wxPyCallback::~wxPyCallback() {
922 wxPyBeginBlockThreads();
924 wxPyEndBlockThreads();
929 // This function is used for all events destined for Python event handlers.
930 void wxPyCallback::EventThunker(wxEvent
& event
) {
931 wxPyCallback
* cb
= (wxPyCallback
*)event
.m_callbackUserData
;
932 PyObject
* func
= cb
->m_func
;
938 wxPyBeginBlockThreads();
939 wxString className
= event
.GetClassInfo()->GetClassName();
941 if (className
== "wxPyEvent")
942 arg
= ((wxPyEvent
*)&event
)->GetSelf();
943 else if (className
== "wxPyCommandEvent")
944 arg
= ((wxPyCommandEvent
*)&event
)->GetSelf();
946 arg
= wxPyConstructObject((void*)&event
, className
);
949 tuple
= PyTuple_New(1);
950 PyTuple_SET_ITEM(tuple
, 0, arg
);
951 result
= PyEval_CallObject(func
, tuple
);
955 PyErr_Clear(); // Just in case...
959 wxPyEndBlockThreads();
963 //----------------------------------------------------------------------
965 wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper
& other
) {
967 m_self
= other
.m_self
;
968 m_class
= other
.m_class
;
976 void wxPyCallbackHelper::setSelf(PyObject
* self
, PyObject
* klass
, int incref
) {
987 #if PYTHON_API_VERSION >= 1011
989 // Prior to Python 2.2 PyMethod_GetClass returned the class object
990 // in which the method was defined. Starting with 2.2 it returns
991 // "class that asked for the method" which seems totally bogus to me
992 // but apprently it fixes some obscure problem waiting to happen in
993 // Python. Since the API was not documented Guido and the gang felt
994 // safe in changing it. Needless to say that totally screwed up the
995 // logic below in wxPyCallbackHelper::findCallback, hence this icky
996 // code to find the class where the method is actually defined...
999 PyObject
* PyFindClassWithAttr(PyObject
*klass
, PyObject
*name
)
1003 if (PyType_Check(klass
)) { // new style classes
1004 // This code is borrowed/adapted from _PyType_Lookup in typeobject.c
1005 // (TODO: This part is not tested yet, so I'm not sure it is correct...)
1006 PyTypeObject
* type
= (PyTypeObject
*)klass
;
1007 PyObject
*mro
, *res
, *base
, *dict
;
1008 /* Look in tp_dict of types in MRO */
1010 assert(PyTuple_Check(mro
));
1011 n
= PyTuple_GET_SIZE(mro
);
1012 for (i
= 0; i
< n
; i
++) {
1013 base
= PyTuple_GET_ITEM(mro
, i
);
1014 if (PyClass_Check(base
))
1015 dict
= ((PyClassObject
*)base
)->cl_dict
;
1017 assert(PyType_Check(base
));
1018 dict
= ((PyTypeObject
*)base
)->tp_dict
;
1020 assert(dict
&& PyDict_Check(dict
));
1021 res
= PyDict_GetItem(dict
, name
);
1028 else if (PyClass_Check(klass
)) { // old style classes
1029 // This code is borrowed/adapted from class_lookup in classobject.c
1030 PyClassObject
* cp
= (PyClassObject
*)klass
;
1031 PyObject
*value
= PyDict_GetItem(cp
->cl_dict
, name
);
1032 if (value
!= NULL
) {
1033 return (PyObject
*)cp
;
1035 n
= PyTuple_Size(cp
->cl_bases
);
1036 for (i
= 0; i
< n
; i
++) {
1037 PyObject
* base
= PyTuple_GetItem(cp
->cl_bases
, i
);
1038 PyObject
*v
= PyFindClassWithAttr(base
, name
);
1050 PyObject
* PyMethod_GetDefiningClass(PyObject
* method
, const char* name
)
1052 PyObject
* mgc
= PyMethod_GET_CLASS(method
);
1054 #if PYTHON_API_VERSION <= 1010 // prior to Python 2.2, the easy way
1056 #else // 2.2 and after, the hard way...
1058 PyObject
* nameo
= PyString_FromString(name
);
1059 PyObject
* klass
= PyFindClassWithAttr(mgc
, nameo
);
1067 bool wxPyCallbackHelper::findCallback(const char* name
) const {
1068 wxPyCallbackHelper
* self
= (wxPyCallbackHelper
*)this; // cast away const
1069 self
->m_lastFound
= NULL
;
1071 // If the object (m_self) has an attibute of the given name...
1072 if (m_self
&& PyObject_HasAttrString(m_self
, (char*)name
)) {
1073 PyObject
*method
, *klass
;
1074 method
= PyObject_GetAttrString(m_self
, (char*)name
);
1076 // ...and if that attribute is a method, and if that method's class is
1077 // not from a base class...
1078 if (PyMethod_Check(method
) &&
1079 (klass
= PyMethod_GetDefiningClass(method
, (char*)name
)) != NULL
&&
1080 ((klass
== m_class
) || PyClass_IsSubclass(klass
, m_class
))) {
1082 // ...then we'll save a pointer to the method so callCallback can call it.
1083 self
->m_lastFound
= method
;
1089 return m_lastFound
!= NULL
;
1093 int wxPyCallbackHelper::callCallback(PyObject
* argTuple
) const {
1097 result
= callCallbackObj(argTuple
);
1098 if (result
) { // Assumes an integer return type...
1099 retval
= PyInt_AsLong(result
);
1101 PyErr_Clear(); // forget about it if it's not...
1106 // Invoke the Python callable object, returning the raw PyObject return
1107 // value. Caller should DECREF the return value and also call PyEval_SaveThread.
1108 PyObject
* wxPyCallbackHelper::callCallbackObj(PyObject
* argTuple
) const {
1111 // Save a copy of the pointer in case the callback generates another
1112 // callback. In that case m_lastFound will have a different value when
1113 // it gets back here...
1114 PyObject
* method
= m_lastFound
;
1116 result
= PyEval_CallObject(method
, argTuple
);
1117 Py_DECREF(argTuple
);
1126 void wxPyCBH_setCallbackInfo(wxPyCallbackHelper
& cbh
, PyObject
* self
, PyObject
* klass
, int incref
) {
1127 cbh
.setSelf(self
, klass
, incref
);
1130 bool wxPyCBH_findCallback(const wxPyCallbackHelper
& cbh
, const char* name
) {
1131 return cbh
.findCallback(name
);
1134 int wxPyCBH_callCallback(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
1135 return cbh
.callCallback(argTuple
);
1138 PyObject
* wxPyCBH_callCallbackObj(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
1139 return cbh
.callCallbackObj(argTuple
);
1143 void wxPyCBH_delete(wxPyCallbackHelper
* cbh
) {
1144 if (cbh
->m_incRef
) {
1145 wxPyBeginBlockThreads();
1146 Py_XDECREF(cbh
->m_self
);
1147 Py_XDECREF(cbh
->m_class
);
1148 wxPyEndBlockThreads();
1152 //---------------------------------------------------------------------------
1153 //---------------------------------------------------------------------------
1154 // These event classes can be derived from in Python and passed through the event
1155 // system without losing anything. They do this by keeping a reference to
1156 // themselves and some special case handling in wxPyCallback::EventThunker.
1159 wxPyEvtSelfRef::wxPyEvtSelfRef() {
1160 //m_self = Py_None; // **** We don't do normal ref counting to prevent
1161 //Py_INCREF(m_self); // circular loops...
1165 wxPyEvtSelfRef::~wxPyEvtSelfRef() {
1166 wxPyBeginBlockThreads();
1169 wxPyEndBlockThreads();
1172 void wxPyEvtSelfRef::SetSelf(PyObject
* self
, bool clone
) {
1173 wxPyBeginBlockThreads();
1181 wxPyEndBlockThreads();
1184 PyObject
* wxPyEvtSelfRef::GetSelf() const {
1190 IMPLEMENT_ABSTRACT_CLASS(wxPyEvent
, wxEvent
);
1191 IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent
, wxCommandEvent
);
1194 wxPyEvent::wxPyEvent(int id
)
1199 wxPyEvent::wxPyEvent(const wxPyEvent
& evt
)
1202 SetSelf(evt
.m_self
, TRUE
);
1206 wxPyEvent::~wxPyEvent() {
1210 wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType
, int id
)
1211 : wxCommandEvent(commandType
, id
) {
1215 wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent
& evt
)
1216 : wxCommandEvent(evt
)
1218 SetSelf(evt
.m_self
, TRUE
);
1222 wxPyCommandEvent::~wxPyCommandEvent() {
1228 //---------------------------------------------------------------------------
1229 //---------------------------------------------------------------------------
1232 wxPyTimer::wxPyTimer(PyObject
* callback
) {
1237 wxPyTimer::~wxPyTimer() {
1238 wxPyBeginBlockThreads();
1240 wxPyEndBlockThreads();
1243 void wxPyTimer::Notify() {
1244 if (!func
|| func
== Py_None
) {
1248 wxPyBeginBlockThreads();
1251 PyObject
* args
= Py_BuildValue("()");
1253 result
= PyEval_CallObject(func
, args
);
1262 wxPyEndBlockThreads();
1268 //---------------------------------------------------------------------------
1269 //---------------------------------------------------------------------------
1270 // Convert a wxList to a Python List
1272 PyObject
* wxPy_ConvertList(wxListBase
* list
, const char* className
) {
1276 wxNode
* node
= list
->First();
1278 wxPyBeginBlockThreads();
1279 pyList
= PyList_New(0);
1281 wxObj
= node
->Data();
1282 pyObj
= wxPyMake_wxObject(wxObj
); //wxPyConstructObject(wxObj, className);
1283 PyList_Append(pyList
, pyObj
);
1284 node
= node
->Next();
1286 wxPyEndBlockThreads();
1290 //----------------------------------------------------------------------
1292 long wxPyGetWinHandle(wxWindow
* win
) {
1294 return (long)win
->GetHandle();
1297 // Find and return the actual X-Window.
1299 if (win
->m_wxwindow
) {
1300 GdkWindowPrivate
* bwin
= (GdkWindowPrivate
*)GTK_PIZZA(win
->m_wxwindow
)->bin_window
;
1302 return (long)bwin
->xwindow
;
1309 //----------------------------------------------------------------------
1310 // Some helper functions for typemaps in my_typemaps.i, so they won't be
1311 // included in every file over and over again...
1313 #if PYTHON_API_VERSION >= 1009
1314 static char* wxStringErrorMsg
= "String or Unicode type required";
1316 static char* wxStringErrorMsg
= "String type required";
1320 wxString
* wxString_in_helper(PyObject
* source
) {
1322 #if PYTHON_API_VERSION >= 1009 // Have Python unicode API
1323 if (!PyString_Check(source
) && !PyUnicode_Check(source
)) {
1324 PyErr_SetString(PyExc_TypeError
, wxStringErrorMsg
);
1328 if (PyUnicode_Check(source
)) {
1329 target
= new wxString(PyUnicode_AS_UNICODE(source
));
1331 // It is a string, get pointers to it and transform to unicode
1332 char* tmpPtr
; int tmpSize
;
1333 PyString_AsStringAndSize(source
, &tmpPtr
, &tmpSize
);
1334 target
= new wxString(tmpPtr
, *wxConvCurrent
, tmpSize
);
1337 char* tmpPtr
; int tmpSize
;
1338 if (PyString_AsStringAndSize(source
, &tmpPtr
, &tmpSize
) == -1) {
1339 PyErr_SetString(PyExc_TypeError
, "Unable to convert string");
1342 target
= new wxString(tmpPtr
, tmpSize
);
1343 #endif // wxUSE_UNICODE
1345 #else // No Python unicode API (1.5.2)
1346 if (!PyString_Check(source
)) {
1347 PyErr_SetString(PyExc_TypeError
, wxStringErrorMsg
);
1350 target
= new wxString(PyString_AS_STRING(source
), PyString_GET_SIZE(source
));
1356 // Similar to above except doesn't use "new" and doesn't set an exception
1357 wxString
Py2wxString(PyObject
* source
)
1360 bool doDecRef
= FALSE
;
1362 #if PYTHON_API_VERSION >= 1009 // Have Python unicode API
1363 if (!PyString_Check(source
) && !PyUnicode_Check(source
)) {
1364 // Convert to String if not one already... (TODO: Unicode too?)
1365 source
= PyObject_Str(source
);
1370 if (PyUnicode_Check(source
)) {
1371 target
= PyUnicode_AS_UNICODE(source
);
1373 // It is a string, get pointers to it and transform to unicode
1374 char* tmpPtr
; int tmpSize
;
1375 PyString_AsStringAndSize(source
, &tmpPtr
, &tmpSize
);
1376 target
= wxString(tmpPtr
, *wxConvCurrent
, tmpSize
);
1379 char* tmpPtr
; int tmpSize
;
1380 PyString_AsStringAndSize(source
, &tmpPtr
, &tmpSize
);
1381 target
= wxString(tmpPtr
, tmpSize
);
1382 #endif // wxUSE_UNICODE
1384 #else // No Python unicode API (1.5.2)
1385 if (!PyString_Check(source
)) {
1386 // Convert to String if not one already...
1387 source
= PyObject_Str(source
);
1390 target
= wxString(PyString_AS_STRING(source
), PyString_GET_SIZE(source
));
1399 // Make either a Python String or Unicode object, depending on build mode
1400 PyObject
* wx2PyString(const wxString
& src
)
1404 str
= PyUnicode_FromUnicode(src
.c_str(), src
.Len());
1406 str
= PyString_FromStringAndSize(src
.c_str(), src
.Len());
1412 //----------------------------------------------------------------------
1415 byte
* byte_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 byte
* temp
= new byte
[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
] = (byte
)PyInt_AsLong(o
);
1438 int* int_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 int* temp
= new int[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 (! PyInt_Check(o
)) {
1452 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1455 temp
[x
] = PyInt_AsLong(o
);
1461 long* long_LIST_helper(PyObject
* source
) {
1462 if (!PyList_Check(source
)) {
1463 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1466 int count
= PyList_Size(source
);
1467 long* temp
= new long[count
];
1469 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1472 for (int x
=0; x
<count
; x
++) {
1473 PyObject
* o
= PyList_GetItem(source
, x
);
1474 if (! PyInt_Check(o
)) {
1475 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1478 temp
[x
] = PyInt_AsLong(o
);
1484 char** string_LIST_helper(PyObject
* source
) {
1485 if (!PyList_Check(source
)) {
1486 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1489 int count
= PyList_Size(source
);
1490 char** temp
= new char*[count
];
1492 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1495 for (int x
=0; x
<count
; x
++) {
1496 PyObject
* o
= PyList_GetItem(source
, x
);
1497 if (! PyString_Check(o
)) {
1498 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1501 temp
[x
] = PyString_AsString(o
);
1506 //--------------------------------
1507 // Part of patch from Tim Hochberg
1508 static inline bool wxPointFromObjects(PyObject
* o1
, PyObject
* o2
, wxPoint
* point
) {
1509 if (PyInt_Check(o1
) && PyInt_Check(o2
)) {
1510 point
->x
= PyInt_AS_LONG(o1
);
1511 point
->y
= PyInt_AS_LONG(o2
);
1514 if (PyFloat_Check(o1
) && PyFloat_Check(o2
)) {
1515 point
->x
= (int)PyFloat_AS_DOUBLE(o1
);
1516 point
->y
= (int)PyFloat_AS_DOUBLE(o2
);
1519 if (PyInstance_Check(o1
) || PyInstance_Check(o2
)) {
1520 // Disallow instances because they can cause havok
1523 if (PyNumber_Check(o1
) && PyNumber_Check(o2
)) {
1524 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
1525 point
->x
= PyInt_AsLong(o1
);
1526 point
->y
= PyInt_AsLong(o2
);
1533 wxPoint
* wxPoint_LIST_helper(PyObject
* source
, int *count
) {
1534 // Putting all of the declarations here allows
1535 // us to put the error handling all in one place.
1538 PyObject
*o
, *o1
, *o2
;
1539 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1541 if (!PySequence_Check(source
)) {
1545 // The length of the sequence is returned in count.
1546 *count
= PySequence_Length(source
);
1551 temp
= new wxPoint
[*count
];
1553 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1556 for (x
=0; x
<*count
; x
++) {
1557 // Get an item: try fast way first.
1559 o
= PySequence_Fast_GET_ITEM(source
, x
);
1562 o
= PySequence_GetItem(source
, x
);
1568 // Convert o to wxPoint.
1569 if ((PyTuple_Check(o
) && PyTuple_GET_SIZE(o
) == 2) ||
1570 (PyList_Check(o
) && PyList_GET_SIZE(o
) == 2)) {
1571 o1
= PySequence_Fast_GET_ITEM(o
, 0);
1572 o2
= PySequence_Fast_GET_ITEM(o
, 1);
1573 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
1577 else if (PyInstance_Check(o
)) {
1579 if (SWIG_GetPtrObj(o
, (void **)&pt
, "_wxPoint_p")) {
1584 else if (PySequence_Check(o
) && PySequence_Length(o
) == 2) {
1585 o1
= PySequence_GetItem(o
, 0);
1586 o2
= PySequence_GetItem(o
, 1);
1587 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
1611 PyErr_SetString(PyExc_TypeError
, "Expected a sequence of length-2 sequences or wxPoints.");
1615 //------------------------------
1618 wxBitmap
** wxBitmap_LIST_helper(PyObject
* source
) {
1619 if (!PyList_Check(source
)) {
1620 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1623 int count
= PyList_Size(source
);
1624 wxBitmap
** temp
= new wxBitmap
*[count
];
1626 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1629 for (int x
=0; x
<count
; x
++) {
1630 PyObject
* o
= PyList_GetItem(source
, x
);
1631 if (PyInstance_Check(o
)) {
1633 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxBitmap_p")) {
1634 PyErr_SetString(PyExc_TypeError
,"Expected _wxBitmap_p.");
1640 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxBitmaps.");
1649 wxString
* wxString_LIST_helper(PyObject
* source
) {
1650 if (!PyList_Check(source
)) {
1651 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1654 int count
= PyList_Size(source
);
1655 wxString
* temp
= new wxString
[count
];
1657 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1660 for (int x
=0; x
<count
; x
++) {
1661 PyObject
* o
= PyList_GetItem(source
, x
);
1662 #if PYTHON_API_VERSION >= 1009
1663 if (! PyString_Check(o
) && ! PyUnicode_Check(o
)) {
1664 PyErr_SetString(PyExc_TypeError
, "Expected a list of string or unicode objects.");
1668 if (! PyString_Check(o
)) {
1669 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1674 wxString
* pStr
= wxString_in_helper(o
);
1682 wxAcceleratorEntry
* wxAcceleratorEntry_LIST_helper(PyObject
* source
) {
1683 if (!PyList_Check(source
)) {
1684 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1687 int count
= PyList_Size(source
);
1688 wxAcceleratorEntry
* temp
= new wxAcceleratorEntry
[count
];
1690 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1693 for (int x
=0; x
<count
; x
++) {
1694 PyObject
* o
= PyList_GetItem(source
, x
);
1695 if (PyInstance_Check(o
)) {
1696 wxAcceleratorEntry
* ae
;
1697 if (SWIG_GetPtrObj(o
, (void **) &ae
,"_wxAcceleratorEntry_p")) {
1698 PyErr_SetString(PyExc_TypeError
,"Expected _wxAcceleratorEntry_p.");
1703 else if (PyTuple_Check(o
)) {
1704 PyObject
* o1
= PyTuple_GetItem(o
, 0);
1705 PyObject
* o2
= PyTuple_GetItem(o
, 1);
1706 PyObject
* o3
= PyTuple_GetItem(o
, 2);
1707 temp
[x
].Set(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
));
1710 PyErr_SetString(PyExc_TypeError
, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
1718 wxPen
** wxPen_LIST_helper(PyObject
* source
) {
1719 if (!PyList_Check(source
)) {
1720 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1723 int count
= PyList_Size(source
);
1724 wxPen
** temp
= new wxPen
*[count
];
1726 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1729 for (int x
=0; x
<count
; x
++) {
1730 PyObject
* o
= PyList_GetItem(source
, x
);
1731 if (PyInstance_Check(o
)) {
1733 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxPen_p")) {
1735 PyErr_SetString(PyExc_TypeError
,"Expected _wxPen_p.");
1742 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxPens.");
1750 bool _2int_seq_helper(PyObject
* source
, int* i1
, int* i2
) {
1751 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1754 if (!PySequence_Check(source
) || PySequence_Length(source
) != 2)
1758 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1759 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1762 o1
= PySequence_GetItem(source
, 0);
1763 o2
= PySequence_GetItem(source
, 1);
1766 *i1
= PyInt_AsLong(o1
);
1767 *i2
= PyInt_AsLong(o2
);
1777 bool _4int_seq_helper(PyObject
* source
, int* i1
, int* i2
, int* i3
, int* i4
) {
1778 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1779 PyObject
*o1
, *o2
, *o3
, *o4
;
1781 if (!PySequence_Check(source
) || PySequence_Length(source
) != 4)
1785 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1786 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1787 o3
= PySequence_Fast_GET_ITEM(source
, 2);
1788 o4
= PySequence_Fast_GET_ITEM(source
, 3);
1791 o1
= PySequence_GetItem(source
, 0);
1792 o2
= PySequence_GetItem(source
, 1);
1793 o3
= PySequence_GetItem(source
, 2);
1794 o4
= PySequence_GetItem(source
, 3);
1797 *i1
= PyInt_AsLong(o1
);
1798 *i2
= PyInt_AsLong(o2
);
1799 *i3
= PyInt_AsLong(o3
);
1800 *i4
= PyInt_AsLong(o4
);
1812 //----------------------------------------------------------------------
1814 bool wxSize_helper(PyObject
* source
, wxSize
** obj
) {
1816 // If source is an object instance then it may already be the right type
1817 if (PyInstance_Check(source
)) {
1819 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxSize_p"))
1824 // otherwise a 2-tuple of integers is expected
1825 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1826 PyObject
* o1
= PySequence_GetItem(source
, 0);
1827 PyObject
* o2
= PySequence_GetItem(source
, 1);
1828 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1833 **obj
= wxSize(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1840 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxSize object.");
1844 bool wxPoint_helper(PyObject
* source
, wxPoint
** obj
) {
1846 // If source is an object instance then it may already be the right type
1847 if (PyInstance_Check(source
)) {
1849 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxPoint_p"))
1854 // otherwise a length-2 sequence of integers is expected
1855 if (PySequence_Check(source
) && PySequence_Length(source
) == 2) {
1856 PyObject
* o1
= PySequence_GetItem(source
, 0);
1857 PyObject
* o2
= PySequence_GetItem(source
, 1);
1858 // This should really check for integers, not numbers -- but that would break code.
1859 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1864 **obj
= wxPoint(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1870 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxPoint object.");
1876 bool wxRealPoint_helper(PyObject
* source
, wxRealPoint
** obj
) {
1878 // If source is an object instance then it may already be the right type
1879 if (PyInstance_Check(source
)) {
1881 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRealPoint_p"))
1886 // otherwise a 2-tuple of floats is expected
1887 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1888 PyObject
* o1
= PySequence_GetItem(source
, 0);
1889 PyObject
* o2
= PySequence_GetItem(source
, 1);
1890 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1895 **obj
= wxRealPoint(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
));
1902 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxRealPoint object.");
1909 bool wxRect_helper(PyObject
* source
, wxRect
** obj
) {
1911 // If source is an object instance then it may already be the right type
1912 if (PyInstance_Check(source
)) {
1914 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRect_p"))
1919 // otherwise a 4-tuple of integers is expected
1920 else if (PySequence_Check(source
) && PyObject_Length(source
) == 4) {
1921 PyObject
* o1
= PySequence_GetItem(source
, 0);
1922 PyObject
* o2
= PySequence_GetItem(source
, 1);
1923 PyObject
* o3
= PySequence_GetItem(source
, 2);
1924 PyObject
* o4
= PySequence_GetItem(source
, 3);
1925 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
) ||
1926 !PyNumber_Check(o3
) || !PyNumber_Check(o4
)) {
1933 **obj
= wxRect(PyInt_AsLong(o1
), PyInt_AsLong(o2
),
1934 PyInt_AsLong(o3
), PyInt_AsLong(o4
));
1943 PyErr_SetString(PyExc_TypeError
, "Expected a 4-tuple of integers or a wxRect object.");
1949 bool wxColour_helper(PyObject
* source
, wxColour
** obj
) {
1951 // If source is an object instance then it may already be the right type
1952 if (PyInstance_Check(source
)) {
1954 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxColour_p"))
1959 // otherwise a string is expected
1960 else if (PyString_Check(source
)) {
1961 wxString
spec(PyString_AS_STRING(source
), *wxConvCurrent
);
1962 if (spec
.GetChar(0) == '#' && spec
.Length() == 7) { // It's #RRGGBB
1963 long red
, green
, blue
;
1964 red
= green
= blue
= 0;
1965 spec
.Mid(1,2).ToLong(&red
, 16);
1966 spec
.Mid(3,2).ToLong(&green
, 16);
1967 spec
.Mid(5,2).ToLong(&blue
, 16);
1969 **obj
= wxColour(red
, green
, blue
);
1972 else { // it's a colour name
1973 **obj
= wxColour(spec
);
1979 PyErr_SetString(PyExc_TypeError
,
1980 "Expected a wxColour object or a string containing a colour "
1981 "name or '#RRGGBB'.");
1986 //----------------------------------------------------------------------
1988 PyObject
* wxArrayString2PyList_helper(const wxArrayString
& arr
) {
1990 PyObject
* list
= PyList_New(0);
1991 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
1993 PyObject
* str
= PyUnicode_FromUnicode(arr
[i
].c_str(), arr
[i
].Len());
1995 PyObject
* str
= PyString_FromStringAndSize(arr
[i
].c_str(), arr
[i
].Len());
1997 PyList_Append(list
, str
);
2004 PyObject
* wxArrayInt2PyList_helper(const wxArrayInt
& arr
) {
2006 PyObject
* list
= PyList_New(0);
2007 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
2008 PyObject
* number
= PyInt_FromLong(arr
[i
]);
2009 PyList_Append(list
, number
);
2016 //----------------------------------------------------------------------
2017 //----------------------------------------------------------------------