]>
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
50 bool wxPyDoCleanup
= FALSE
;
53 #ifdef WXP_WITH_THREAD
54 struct wxPyThreadState
{
56 PyThreadState
* tstate
;
58 wxPyThreadState(unsigned long _tid
=0, PyThreadState
* _tstate
=NULL
)
59 : tid(_tid
), tstate(_tstate
) {}
62 #include <wx/dynarray.h>
63 WX_DECLARE_OBJARRAY(wxPyThreadState
, wxPyThreadStateArray
);
64 #include <wx/arrimpl.cpp>
65 WX_DEFINE_OBJARRAY(wxPyThreadStateArray
);
67 wxPyThreadStateArray
* wxPyTStates
= NULL
;
68 wxMutex
* wxPyTMutex
= NULL
;
72 #ifdef __WXMSW__ // If building for win32...
73 //----------------------------------------------------------------------
74 // This gets run when the DLL is loaded. We just need to save a handle.
75 //----------------------------------------------------------------------
78 HINSTANCE hinstDLL
, // handle to DLL module
79 DWORD fdwReason
, // reason for calling function
80 LPVOID lpvReserved
// reserved
83 // If wxPython is embedded in another wxWindows app then
84 // the inatance has already been set.
85 if (! wxGetInstance())
86 wxSetInstance(hinstDLL
);
91 //----------------------------------------------------------------------
92 // Classes for implementing the wxp main application shell.
93 //----------------------------------------------------------------------
97 // printf("**** ctor\n");
100 wxPyApp::~wxPyApp() {
101 // printf("**** dtor\n");
105 // This one isn't acutally called... See __wxStart()
106 bool wxPyApp::OnInit() {
111 int wxPyApp::MainLoop() {
114 DeletePendingObjects();
115 bool initialized
= wxTopLevelWindows
.GetCount() != 0;
117 m_initialized
= initialized
;
121 retval
= wxApp::MainLoop();
129 //---------------------------------------------------------------------
130 //----------------------------------------------------------------------
133 static char* wxPyCopyCString(const wxChar
* src
)
135 wxWX2MBbuf buff
= (wxWX2MBbuf
)wxConvCurrent
->cWX2MB(src
);
136 size_t len
= strlen(buff
);
137 char* dest
= new char[len
+1];
143 static char* wxPyCopyCString(const char* src
) // we need a char version too
145 size_t len
= strlen(src
);
146 char* dest
= new char[len
+1];
152 static wxChar
* wxPyCopyWString(const char *src
)
154 //wxMB2WXbuf buff = wxConvCurrent->cMB2WX(src);
155 wxString
str(src
, *wxConvCurrent
);
156 return copystring(str
);
160 static wxChar
* wxPyCopyWString(const wxChar
*src
)
162 return copystring(src
);
167 //----------------------------------------------------------------------
169 // This is where we pick up the first part of the wxEntry functionality...
170 // The rest is in __wxStart and __wxCleanup. This function is called when
171 // wxcmodule is imported. (Before there is a wxApp object.)
176 // wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
179 #ifdef WXP_WITH_THREAD
180 PyEval_InitThreads();
181 wxPyTStates
= new wxPyThreadStateArray
;
182 wxPyTMutex
= new wxMutex
;
185 wxApp::CheckBuildOptions(wxBuildOptions());
187 // Bail out if there is already a wxApp created. This means that the
188 // toolkit has already been initialized, as in embedding wxPython in
189 // a C++ wxWindows app, so we don't need to call wxEntryStart.
190 if (wxTheApp
!= NULL
) {
193 wxPyDoCleanup
= TRUE
;
197 PyObject
* sysargv
= PySys_GetObject("argv");
198 if (sysargv
!= NULL
) {
199 argc
= PyList_Size(sysargv
);
200 argv
= new char*[argc
+1];
202 for(x
=0; x
<argc
; x
++) {
203 PyObject
*item
= PyList_GetItem(sysargv
, x
);
205 if (PyUnicode_Check(item
))
206 argv
[x
] = wxPyCopyCString(PyUnicode_AS_UNICODE(item
));
209 argv
[x
] = wxPyCopyCString(PyString_AsString(item
));
214 wxEntryStart(argc
, argv
);
220 // Start the user application, user App's OnInit method is a parameter here
221 PyObject
* __wxStart(PyObject
* /* self */, PyObject
* args
)
223 PyObject
* onInitFunc
= NULL
;
228 if (!PyArg_ParseTuple(args
, "O", &onInitFunc
))
231 #if 0 // Try it out without this check, see how it does...
232 if (wxTopLevelWindows
.Number() > 0) {
233 PyErr_SetString(PyExc_TypeError
, "Only 1 wxApp per process!");
238 // This is the next part of the wxEntry functionality...
240 wxChar
** argv
= NULL
;
241 PyObject
* sysargv
= PySys_GetObject("argv");
242 if (sysargv
!= NULL
) {
243 argc
= PyList_Size(sysargv
);
244 argv
= new wxChar
*[argc
+1];
246 for(x
=0; x
<argc
; x
++) {
247 PyObject
*pyArg
= PyList_GetItem(sysargv
, x
);
249 if (PyUnicode_Check(pyArg
))
250 argv
[x
] = wxPyCopyWString(PyUnicode_AS_UNICODE(pyArg
));
253 argv
[x
] = wxPyCopyWString(PyString_AsString(pyArg
));
258 wxPythonApp
->argc
= argc
;
259 wxPythonApp
->argv
= argv
;
263 // Call the Python App's OnInit function
264 arglist
= PyTuple_New(0);
265 result
= PyEval_CallObject(onInitFunc
, arglist
);
266 if (!result
) { // an exception was raised.
270 if (! PyInt_Check(result
)) {
271 PyErr_SetString(PyExc_TypeError
, "OnInit should return a boolean value");
274 bResult
= PyInt_AS_LONG(result
);
276 PyErr_SetString(PyExc_SystemExit
, "OnInit returned FALSE, exiting...");
281 wxTheApp
->m_initialized
= (wxTopLevelWindows
.GetCount() > 0);
292 #ifdef WXP_WITH_THREAD
295 wxPyTStates
->Empty();
303 static PyObject
* wxPython_dict
= NULL
;
304 static PyObject
* wxPyPtrTypeMap
= NULL
;
307 PyObject
* __wxSetDictionary(PyObject
* /* self */, PyObject
* args
)
310 if (!PyArg_ParseTuple(args
, "O", &wxPython_dict
))
313 if (!PyDict_Check(wxPython_dict
)) {
314 PyErr_SetString(PyExc_TypeError
, "_wxSetDictionary must have dictionary object!");
318 if (! wxPyPtrTypeMap
)
319 wxPyPtrTypeMap
= PyDict_New();
320 PyDict_SetItemString(wxPython_dict
, "__wxPyPtrTypeMap", wxPyPtrTypeMap
);
324 #define wxPlatform "__WXMOTIF__"
327 #define wxPlatform "__WXX11__"
330 #define wxPlatform "__WXGTK__"
332 #if defined(__WIN32__) || defined(__WXMSW__)
333 #define wxPlatform "__WXMSW__"
336 #define wxPlatform "__WXMAC__"
339 PyDict_SetItemString(wxPython_dict
, "wxPlatform", PyString_FromString(wxPlatform
));
340 PyDict_SetItemString(wxPython_dict
, "wxUSE_UNICODE", PyInt_FromLong(wxUSE_UNICODE
));
346 //---------------------------------------------------------------------------
348 void wxPyClientData_dtor(wxPyClientData
* self
) {
349 wxPyBeginBlockThreads();
350 Py_DECREF(self
->m_obj
);
351 wxPyEndBlockThreads();
354 void wxPyUserData_dtor(wxPyUserData
* self
) {
355 wxPyBeginBlockThreads();
356 Py_DECREF(self
->m_obj
);
357 wxPyEndBlockThreads();
361 // This is called when an OOR controled object is being destroyed. Although
362 // the C++ object is going away there is no way to force the Python object
363 // (and all references to it) to die too. This causes problems (crashes) in
364 // wxPython when a python shadow object attempts to call a C++ method using
365 // the now bogus pointer... So to try and prevent this we'll do a little black
366 // magic and change the class of the python instance to a class that will
367 // raise an exception for any attempt to call methods with it. See
368 // _wxPyDeadObject in _extras.py for the implementation of this class.
369 void wxPyOORClientData_dtor(wxPyOORClientData
* self
) {
371 static PyObject
* deadObjectClass
= NULL
;
373 wxPyBeginBlockThreads();
374 if (deadObjectClass
== NULL
) {
375 deadObjectClass
= PyDict_GetItemString(wxPython_dict
, "_wxPyDeadObject");
376 wxASSERT_MSG(deadObjectClass
!= NULL
, wxT("Can't get _wxPyDeadObject class!"));
377 Py_INCREF(deadObjectClass
);
380 // Clear the instance's dictionary, put the name of the old class into the
381 // instance, and then reset the class to be the dead class.
382 if (self
->m_obj
->ob_refcnt
> 1) { // but only if there is more than one reference
383 wxASSERT_MSG(PyInstance_Check(self
->m_obj
), wxT("m_obj not an instance!?!?!"));
384 PyInstanceObject
* inst
= (PyInstanceObject
*)self
->m_obj
;
385 PyDict_Clear(inst
->in_dict
);
386 PyDict_SetItemString(inst
->in_dict
, "_name", inst
->in_class
->cl_name
);
387 inst
->in_class
= (PyClassObject
*)deadObjectClass
;
388 Py_INCREF(deadObjectClass
);
390 wxPyEndBlockThreads();
393 //---------------------------------------------------------------------------
394 // Stuff used by OOR to find the right wxPython class type to return and to
398 // The pointer type map is used when the "pointer" type name generated by SWIG
399 // is not the same as the shadow class name, for example wxPyTreeCtrl
400 // vs. wxTreeCtrl. It needs to be referenced in Python as well as from C++,
401 // so we'll just make it a Python dictionary in the wx module's namespace.
402 // (See __wxSetDictionary)
403 void wxPyPtrTypeMap_Add(const char* commonName
, const char* ptrName
) {
404 if (! wxPyPtrTypeMap
)
405 wxPyPtrTypeMap
= PyDict_New();
406 PyDict_SetItemString(wxPyPtrTypeMap
,
408 PyString_FromString((char*)ptrName
));
413 PyObject
* wxPyClassExists(const wxString
& className
) {
418 char buff
[64]; // should always be big enough...
420 sprintf(buff
, "%sPtr", className
.mbc_str());
421 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
423 return classobj
; // returns NULL if not found
427 PyObject
* wxPyMake_wxObject(wxObject
* source
, bool checkEvtHandler
) {
428 PyObject
* target
= NULL
;
429 bool isEvtHandler
= FALSE
;
432 // If it's derived from wxEvtHandler then there may
433 // already be a pointer to a Python object that we can use
435 if (checkEvtHandler
&& wxIsKindOf(source
, wxEvtHandler
)) {
437 wxEvtHandler
* eh
= (wxEvtHandler
*)source
;
438 wxPyOORClientData
* data
= (wxPyOORClientData
*)eh
->GetClientObject();
440 target
= data
->m_obj
;
446 // Otherwise make it the old fashioned way by making a
447 // new shadow object and putting this pointer in it.
448 wxClassInfo
* info
= source
->GetClassInfo();
449 wxChar
* name
= (wxChar
*)info
->GetClassName();
450 PyObject
* klass
= wxPyClassExists(name
);
451 while (info
&& !klass
) {
452 name
= (wxChar
*)info
->GetBaseClassName1();
453 info
= wxClassInfo::FindClass(name
);
454 klass
= wxPyClassExists(name
);
457 target
= wxPyConstructObject(source
, name
, klass
, FALSE
);
458 if (target
&& isEvtHandler
)
459 ((wxEvtHandler
*)source
)->SetClientObject(new wxPyOORClientData(target
));
461 wxString
msg(wxT("wxPython class not found for "));
462 msg
+= source
->GetClassInfo()->GetClassName();
463 PyErr_SetString(PyExc_NameError
, msg
.mbc_str());
467 } else { // source was NULL so return None.
468 Py_INCREF(Py_None
); target
= Py_None
;
474 PyObject
* wxPyMake_wxSizer(wxSizer
* source
) {
475 PyObject
* target
= NULL
;
477 if (source
&& wxIsKindOf(source
, wxSizer
)) {
478 // If it's derived from wxSizer then there may
479 // already be a pointer to a Python object that we can use
481 wxSizer
* sz
= (wxSizer
*)source
;
482 wxPyOORClientData
* data
= (wxPyOORClientData
*)sz
->GetClientObject();
484 target
= data
->m_obj
;
489 target
= wxPyMake_wxObject(source
, FALSE
);
490 if (target
!= Py_None
)
491 ((wxSizer
*)source
)->SetClientObject(new wxPyOORClientData(target
));
498 //---------------------------------------------------------------------------
500 PyObject
* wxPyConstructObject(void* ptr
,
501 const wxString
& className
,
508 wxString
name(className
);
509 char swigptr
[64]; // should always be big enough...
512 if ((item
= PyDict_GetItemString(wxPyPtrTypeMap
, (char*)(const char*)name
.mbc_str())) != NULL
) {
513 name
= wxString(PyString_AsString(item
), *wxConvCurrent
);
515 sprintf(buff
, "_%s_p", (const char*)name
.mbc_str());
516 SWIG_MakePtr(swigptr
, ptr
, buff
);
518 arg
= Py_BuildValue("(s)", swigptr
);
519 obj
= PyInstance_New(klass
, arg
, NULL
);
523 PyObject
* one
= PyInt_FromLong(1);
524 PyObject_SetAttrString(obj
, "thisown", one
);
532 PyObject
* wxPyConstructObject(void* ptr
,
533 const wxString
& className
,
540 char buff
[64]; // should always be big enough...
541 sprintf(buff
, "%sPtr", (const char*)className
.mbc_str());
543 wxASSERT_MSG(wxPython_dict
, wxT("wxPython_dict is not set yet!!"));
545 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
547 wxString
msg(wxT("wxPython class not found for "));
549 PyErr_SetString(PyExc_NameError
, msg
.mbc_str());
553 return wxPyConstructObject(ptr
, className
, classobj
, setThisOwn
);
557 //---------------------------------------------------------------------------
560 #ifdef WXP_WITH_THREAD
562 unsigned long wxPyGetCurrentThreadId() {
563 return wxThread::GetCurrentId();
566 static PyThreadState
* gs_shutdownTState
;
568 PyThreadState
* wxPyGetThreadState() {
569 if (wxPyTMutex
== NULL
) // Python is shutting down...
570 return gs_shutdownTState
;
572 unsigned long ctid
= wxPyGetCurrentThreadId();
573 PyThreadState
* tstate
= NULL
;
576 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
577 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
578 if (info
.tid
== ctid
) {
579 tstate
= info
.tstate
;
583 wxPyTMutex
->Unlock();
584 wxASSERT_MSG(tstate
, wxT("PyThreadState should not be NULL!"));
589 void wxPySaveThreadState(PyThreadState
* tstate
) {
590 if (wxPyTMutex
== NULL
) { // Python is shutting down, assume a single thread...
591 gs_shutdownTState
= tstate
;
594 unsigned long ctid
= wxPyGetCurrentThreadId();
596 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
597 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
598 if (info
.tid
== ctid
) {
599 info
.tstate
= tstate
;
600 wxPyTMutex
->Unlock();
604 // not found, so add it...
605 wxPyTStates
->Add(new wxPyThreadState(ctid
, tstate
));
606 wxPyTMutex
->Unlock();
612 // Calls from Python to wxWindows code are wrapped in calls to these
615 PyThreadState
* wxPyBeginAllowThreads() {
616 #ifdef WXP_WITH_THREAD
617 PyThreadState
* saved
= PyEval_SaveThread(); // Py_BEGIN_ALLOW_THREADS;
618 wxPySaveThreadState(saved
);
625 void wxPyEndAllowThreads(PyThreadState
* saved
) {
626 #ifdef WXP_WITH_THREAD
627 PyEval_RestoreThread(saved
); // Py_END_ALLOW_THREADS;
633 // Calls from wxWindows back to Python code, or even any PyObject
634 // manipulations, PyDECREF's and etc. are wrapped in calls to these functions:
636 void wxPyBeginBlockThreads() {
637 #ifdef WXP_WITH_THREAD
638 PyThreadState
* tstate
= wxPyGetThreadState();
639 PyEval_RestoreThread(tstate
);
644 void wxPyEndBlockThreads() {
645 #ifdef WXP_WITH_THREAD
646 // Is there any need to save it again?
647 // PyThreadState* tstate =
653 //---------------------------------------------------------------------------
654 // wxPyInputStream and wxPyCBInputStream methods
657 void wxPyInputStream::close() {
658 /* do nothing for now */
661 void wxPyInputStream::flush() {
662 /* do nothing for now */
665 bool wxPyInputStream::eof() {
667 return m_wxis
->Eof();
672 wxPyInputStream::~wxPyInputStream() {
679 PyObject
* wxPyInputStream::read(int size
) {
680 PyObject
* obj
= NULL
;
682 const int BUFSIZE
= 1024;
684 // check if we have a real wxInputStream to work with
686 PyErr_SetString(PyExc_IOError
, "no valid C-wxInputStream");
692 while (! m_wxis
->Eof()) {
693 m_wxis
->Read(buf
.GetAppendBuf(BUFSIZE
), BUFSIZE
);
694 buf
.UngetAppendBuf(m_wxis
->LastRead());
697 } else { // Read only size number of characters
698 m_wxis
->Read(buf
.GetWriteBuf(size
), size
);
699 buf
.UngetWriteBuf(m_wxis
->LastRead());
703 if (m_wxis
->LastError() == wxSTREAM_READ_ERROR
) {
704 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
707 // We use only strings for the streams, not unicode
708 obj
= PyString_FromStringAndSize(buf
, buf
.GetDataLen());
714 PyObject
* wxPyInputStream::readline(int size
) {
715 PyObject
* obj
= NULL
;
720 // check if we have a real wxInputStream to work with
722 PyErr_SetString(PyExc_IOError
,"no valid C-wxInputStream");
726 // read until \n or byte limit reached
727 for (i
=ch
=0; (ch
!= '\n') && (!m_wxis
->Eof()) && ((size
< 0) || (i
< size
)); i
++) {
733 if (m_wxis
->LastError() == wxSTREAM_READ_ERROR
) {
734 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
737 // We use only strings for the streams, not unicode
738 obj
= PyString_FromStringAndSize((char*)buf
.GetData(), buf
.GetDataLen());
744 PyObject
* wxPyInputStream::readlines(int sizehint
) {
747 // check if we have a real wxInputStream to work with
749 PyErr_SetString(PyExc_IOError
,"no valid C-wxInputStream below");
754 pylist
= PyList_New(0);
760 // read sizehint bytes or until EOF
762 for (i
=0; (!m_wxis
->Eof()) && ((sizehint
< 0) || (i
< sizehint
));) {
763 PyObject
* s
= this->readline();
768 PyList_Append(pylist
, s
);
769 i
+= PyString_Size(s
);
773 if (m_wxis
->LastError() == wxSTREAM_READ_ERROR
) {
775 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
783 void wxPyInputStream::seek(int offset
, int whence
) {
785 m_wxis
->SeekI(offset
, wxSeekMode(whence
));
788 int wxPyInputStream::tell(){
790 return m_wxis
->TellI();
797 wxPyCBInputStream::wxPyCBInputStream(PyObject
*r
, PyObject
*s
, PyObject
*t
, bool block
)
798 : wxInputStream(), m_read(r
), m_seek(s
), m_tell(t
), m_block(block
)
802 wxPyCBInputStream::~wxPyCBInputStream() {
803 if (m_block
) wxPyBeginBlockThreads();
807 if (m_block
) wxPyEndBlockThreads();
811 wxPyCBInputStream
* wxPyCBInputStream::create(PyObject
*py
, bool block
) {
812 if (block
) wxPyBeginBlockThreads();
814 PyObject
* read
= getMethod(py
, "read");
815 PyObject
* seek
= getMethod(py
, "seek");
816 PyObject
* tell
= getMethod(py
, "tell");
819 PyErr_SetString(PyExc_TypeError
, "Not a file-like object");
823 if (block
) wxPyEndBlockThreads();
827 if (block
) wxPyEndBlockThreads();
828 return new wxPyCBInputStream(read
, seek
, tell
, block
);
831 PyObject
* wxPyCBInputStream::getMethod(PyObject
* py
, char* name
) {
832 if (!PyObject_HasAttrString(py
, name
))
834 PyObject
* o
= PyObject_GetAttrString(py
, name
);
835 if (!PyMethod_Check(o
) && !PyCFunction_Check(o
)) {
843 size_t wxPyCBInputStream::GetSize() const {
844 wxPyCBInputStream
* self
= (wxPyCBInputStream
*)this; // cast off const
845 if (m_seek
&& m_tell
) {
846 off_t temp
= self
->OnSysTell();
847 off_t ret
= self
->OnSysSeek(0, wxFromEnd
);
848 self
->OnSysSeek(temp
, wxFromStart
);
856 size_t wxPyCBInputStream::OnSysRead(void *buffer
, size_t bufsize
) {
860 wxPyBeginBlockThreads();
861 PyObject
* arglist
= Py_BuildValue("(i)", bufsize
);
862 PyObject
* result
= PyEval_CallObject(m_read
, arglist
);
866 if ((result
!= NULL
) && PyString_Check(result
)) {
867 o
= PyString_Size(result
);
869 m_lasterror
= wxSTREAM_EOF
;
872 memcpy((char*)buffer
, PyString_AsString(result
), o
); // strings only, not unicode...
877 m_lasterror
= wxSTREAM_READ_ERROR
;
878 wxPyEndBlockThreads();
883 size_t wxPyCBInputStream::OnSysWrite(const void *buffer
, size_t bufsize
) {
884 m_lasterror
= wxSTREAM_WRITE_ERROR
;
888 off_t
wxPyCBInputStream::OnSysSeek(off_t off
, wxSeekMode mode
) {
889 wxPyBeginBlockThreads();
890 PyObject
* arglist
= Py_BuildValue("(ii)", off
, mode
);
891 PyObject
* result
= PyEval_CallObject(m_seek
, arglist
);
894 wxPyEndBlockThreads();
898 off_t
wxPyCBInputStream::OnSysTell() const {
899 wxPyBeginBlockThreads();
900 PyObject
* arglist
= Py_BuildValue("()");
901 PyObject
* result
= PyEval_CallObject(m_tell
, arglist
);
904 if (result
!= NULL
) {
905 o
= PyInt_AsLong(result
);
908 wxPyEndBlockThreads();
912 //----------------------------------------------------------------------
914 IMPLEMENT_ABSTRACT_CLASS(wxPyCallback
, wxObject
);
916 wxPyCallback::wxPyCallback(PyObject
* func
) {
921 wxPyCallback::wxPyCallback(const wxPyCallback
& other
) {
922 m_func
= other
.m_func
;
926 wxPyCallback::~wxPyCallback() {
927 wxPyBeginBlockThreads();
929 wxPyEndBlockThreads();
934 // This function is used for all events destined for Python event handlers.
935 void wxPyCallback::EventThunker(wxEvent
& event
) {
936 wxPyCallback
* cb
= (wxPyCallback
*)event
.m_callbackUserData
;
937 PyObject
* func
= cb
->m_func
;
943 wxPyBeginBlockThreads();
944 wxString className
= event
.GetClassInfo()->GetClassName();
946 if (className
== "wxPyEvent")
947 arg
= ((wxPyEvent
*)&event
)->GetSelf();
948 else if (className
== "wxPyCommandEvent")
949 arg
= ((wxPyCommandEvent
*)&event
)->GetSelf();
951 arg
= wxPyConstructObject((void*)&event
, className
);
954 tuple
= PyTuple_New(1);
955 PyTuple_SET_ITEM(tuple
, 0, arg
);
956 result
= PyEval_CallObject(func
, tuple
);
960 PyErr_Clear(); // Just in case...
964 wxPyEndBlockThreads();
968 //----------------------------------------------------------------------
970 wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper
& other
) {
972 m_self
= other
.m_self
;
973 m_class
= other
.m_class
;
981 void wxPyCallbackHelper::setSelf(PyObject
* self
, PyObject
* klass
, int incref
) {
992 #if PYTHON_API_VERSION >= 1011
994 // Prior to Python 2.2 PyMethod_GetClass returned the class object
995 // in which the method was defined. Starting with 2.2 it returns
996 // "class that asked for the method" which seems totally bogus to me
997 // but apprently it fixes some obscure problem waiting to happen in
998 // Python. Since the API was not documented Guido and the gang felt
999 // safe in changing it. Needless to say that totally screwed up the
1000 // logic below in wxPyCallbackHelper::findCallback, hence this icky
1001 // code to find the class where the method is actually defined...
1004 PyObject
* PyFindClassWithAttr(PyObject
*klass
, PyObject
*name
)
1008 if (PyType_Check(klass
)) { // new style classes
1009 // This code is borrowed/adapted from _PyType_Lookup in typeobject.c
1010 // (TODO: This part is not tested yet, so I'm not sure it is correct...)
1011 PyTypeObject
* type
= (PyTypeObject
*)klass
;
1012 PyObject
*mro
, *res
, *base
, *dict
;
1013 /* Look in tp_dict of types in MRO */
1015 assert(PyTuple_Check(mro
));
1016 n
= PyTuple_GET_SIZE(mro
);
1017 for (i
= 0; i
< n
; i
++) {
1018 base
= PyTuple_GET_ITEM(mro
, i
);
1019 if (PyClass_Check(base
))
1020 dict
= ((PyClassObject
*)base
)->cl_dict
;
1022 assert(PyType_Check(base
));
1023 dict
= ((PyTypeObject
*)base
)->tp_dict
;
1025 assert(dict
&& PyDict_Check(dict
));
1026 res
= PyDict_GetItem(dict
, name
);
1033 else if (PyClass_Check(klass
)) { // old style classes
1034 // This code is borrowed/adapted from class_lookup in classobject.c
1035 PyClassObject
* cp
= (PyClassObject
*)klass
;
1036 PyObject
*value
= PyDict_GetItem(cp
->cl_dict
, name
);
1037 if (value
!= NULL
) {
1038 return (PyObject
*)cp
;
1040 n
= PyTuple_Size(cp
->cl_bases
);
1041 for (i
= 0; i
< n
; i
++) {
1042 PyObject
* base
= PyTuple_GetItem(cp
->cl_bases
, i
);
1043 PyObject
*v
= PyFindClassWithAttr(base
, name
);
1055 PyObject
* PyMethod_GetDefiningClass(PyObject
* method
, const char* name
)
1057 PyObject
* mgc
= PyMethod_GET_CLASS(method
);
1059 #if PYTHON_API_VERSION <= 1010 // prior to Python 2.2, the easy way
1061 #else // 2.2 and after, the hard way...
1063 PyObject
* nameo
= PyString_FromString(name
);
1064 PyObject
* klass
= PyFindClassWithAttr(mgc
, nameo
);
1072 bool wxPyCallbackHelper::findCallback(const char* name
) const {
1073 wxPyCallbackHelper
* self
= (wxPyCallbackHelper
*)this; // cast away const
1074 self
->m_lastFound
= NULL
;
1076 // If the object (m_self) has an attibute of the given name...
1077 if (m_self
&& PyObject_HasAttrString(m_self
, (char*)name
)) {
1078 PyObject
*method
, *klass
;
1079 method
= PyObject_GetAttrString(m_self
, (char*)name
);
1081 // ...and if that attribute is a method, and if that method's class is
1082 // not from a base class...
1083 if (PyMethod_Check(method
) &&
1084 (klass
= PyMethod_GetDefiningClass(method
, (char*)name
)) != NULL
&&
1085 ((klass
== m_class
) || PyClass_IsSubclass(klass
, m_class
))) {
1087 // ...then we'll save a pointer to the method so callCallback can call it.
1088 self
->m_lastFound
= method
;
1094 return m_lastFound
!= NULL
;
1098 int wxPyCallbackHelper::callCallback(PyObject
* argTuple
) const {
1102 result
= callCallbackObj(argTuple
);
1103 if (result
) { // Assumes an integer return type...
1104 retval
= PyInt_AsLong(result
);
1106 PyErr_Clear(); // forget about it if it's not...
1111 // Invoke the Python callable object, returning the raw PyObject return
1112 // value. Caller should DECREF the return value and also call PyEval_SaveThread.
1113 PyObject
* wxPyCallbackHelper::callCallbackObj(PyObject
* argTuple
) const {
1116 // Save a copy of the pointer in case the callback generates another
1117 // callback. In that case m_lastFound will have a different value when
1118 // it gets back here...
1119 PyObject
* method
= m_lastFound
;
1121 result
= PyEval_CallObject(method
, argTuple
);
1122 Py_DECREF(argTuple
);
1131 void wxPyCBH_setCallbackInfo(wxPyCallbackHelper
& cbh
, PyObject
* self
, PyObject
* klass
, int incref
) {
1132 cbh
.setSelf(self
, klass
, incref
);
1135 bool wxPyCBH_findCallback(const wxPyCallbackHelper
& cbh
, const char* name
) {
1136 return cbh
.findCallback(name
);
1139 int wxPyCBH_callCallback(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
1140 return cbh
.callCallback(argTuple
);
1143 PyObject
* wxPyCBH_callCallbackObj(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
1144 return cbh
.callCallbackObj(argTuple
);
1148 void wxPyCBH_delete(wxPyCallbackHelper
* cbh
) {
1149 if (cbh
->m_incRef
) {
1150 wxPyBeginBlockThreads();
1151 Py_XDECREF(cbh
->m_self
);
1152 Py_XDECREF(cbh
->m_class
);
1153 wxPyEndBlockThreads();
1157 //---------------------------------------------------------------------------
1158 //---------------------------------------------------------------------------
1159 // These event classes can be derived from in Python and passed through the event
1160 // system without losing anything. They do this by keeping a reference to
1161 // themselves and some special case handling in wxPyCallback::EventThunker.
1164 wxPyEvtSelfRef::wxPyEvtSelfRef() {
1165 //m_self = Py_None; // **** We don't do normal ref counting to prevent
1166 //Py_INCREF(m_self); // circular loops...
1170 wxPyEvtSelfRef::~wxPyEvtSelfRef() {
1171 wxPyBeginBlockThreads();
1174 wxPyEndBlockThreads();
1177 void wxPyEvtSelfRef::SetSelf(PyObject
* self
, bool clone
) {
1178 wxPyBeginBlockThreads();
1186 wxPyEndBlockThreads();
1189 PyObject
* wxPyEvtSelfRef::GetSelf() const {
1195 IMPLEMENT_ABSTRACT_CLASS(wxPyEvent
, wxEvent
);
1196 IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent
, wxCommandEvent
);
1199 wxPyEvent::wxPyEvent(int id
)
1204 wxPyEvent::wxPyEvent(const wxPyEvent
& evt
)
1207 SetSelf(evt
.m_self
, TRUE
);
1211 wxPyEvent::~wxPyEvent() {
1215 wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType
, int id
)
1216 : wxCommandEvent(commandType
, id
) {
1220 wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent
& evt
)
1221 : wxCommandEvent(evt
)
1223 SetSelf(evt
.m_self
, TRUE
);
1227 wxPyCommandEvent::~wxPyCommandEvent() {
1233 //---------------------------------------------------------------------------
1234 //---------------------------------------------------------------------------
1237 wxPyTimer::wxPyTimer(PyObject
* callback
) {
1242 wxPyTimer::~wxPyTimer() {
1243 wxPyBeginBlockThreads();
1245 wxPyEndBlockThreads();
1248 void wxPyTimer::Notify() {
1249 if (!func
|| func
== Py_None
) {
1253 wxPyBeginBlockThreads();
1256 PyObject
* args
= Py_BuildValue("()");
1258 result
= PyEval_CallObject(func
, args
);
1267 wxPyEndBlockThreads();
1273 //---------------------------------------------------------------------------
1274 //---------------------------------------------------------------------------
1275 // Convert a wxList to a Python List
1277 PyObject
* wxPy_ConvertList(wxListBase
* list
, const char* className
) {
1281 wxNode
* node
= list
->First();
1283 wxPyBeginBlockThreads();
1284 pyList
= PyList_New(0);
1286 wxObj
= node
->Data();
1287 pyObj
= wxPyMake_wxObject(wxObj
); //wxPyConstructObject(wxObj, className);
1288 PyList_Append(pyList
, pyObj
);
1289 node
= node
->Next();
1291 wxPyEndBlockThreads();
1295 //----------------------------------------------------------------------
1297 long wxPyGetWinHandle(wxWindow
* win
) {
1299 return (long)win
->GetHandle();
1302 // Find and return the actual X-Window.
1304 if (win
->m_wxwindow
) {
1305 GdkWindowPrivate
* bwin
= (GdkWindowPrivate
*)GTK_PIZZA(win
->m_wxwindow
)->bin_window
;
1307 return (long)bwin
->xwindow
;
1314 //----------------------------------------------------------------------
1315 // Some helper functions for typemaps in my_typemaps.i, so they won't be
1316 // included in every file over and over again...
1318 #if PYTHON_API_VERSION >= 1009
1319 static char* wxStringErrorMsg
= "String or Unicode type required";
1321 static char* wxStringErrorMsg
= "String type required";
1325 wxString
* wxString_in_helper(PyObject
* source
) {
1327 #if PYTHON_API_VERSION >= 1009 // Have Python unicode API
1328 if (!PyString_Check(source
) && !PyUnicode_Check(source
)) {
1329 PyErr_SetString(PyExc_TypeError
, wxStringErrorMsg
);
1333 if (PyUnicode_Check(source
)) {
1334 target
= new wxString(PyUnicode_AS_UNICODE(source
));
1336 // It is a string, get pointers to it and transform to unicode
1337 char* tmpPtr
; int tmpSize
;
1338 PyString_AsStringAndSize(source
, &tmpPtr
, &tmpSize
);
1339 target
= new wxString(tmpPtr
, *wxConvCurrent
, tmpSize
);
1342 char* tmpPtr
; int tmpSize
;
1343 if (PyString_AsStringAndSize(source
, &tmpPtr
, &tmpSize
) == -1) {
1344 PyErr_SetString(PyExc_TypeError
, "Unable to convert string");
1347 target
= new wxString(tmpPtr
, tmpSize
);
1348 #endif // wxUSE_UNICODE
1350 #else // No Python unicode API (1.5.2)
1351 if (!PyString_Check(source
)) {
1352 PyErr_SetString(PyExc_TypeError
, wxStringErrorMsg
);
1355 target
= new wxString(PyString_AS_STRING(source
), PyString_GET_SIZE(source
));
1361 // Similar to above except doesn't use "new" and doesn't set an exception
1362 wxString
Py2wxString(PyObject
* source
)
1365 bool doDecRef
= FALSE
;
1367 #if PYTHON_API_VERSION >= 1009 // Have Python unicode API
1368 if (!PyString_Check(source
) && !PyUnicode_Check(source
)) {
1369 // Convert to String if not one already... (TODO: Unicode too?)
1370 source
= PyObject_Str(source
);
1375 if (PyUnicode_Check(source
)) {
1376 target
= PyUnicode_AS_UNICODE(source
);
1378 // It is a string, get pointers to it and transform to unicode
1379 char* tmpPtr
; int tmpSize
;
1380 PyString_AsStringAndSize(source
, &tmpPtr
, &tmpSize
);
1381 target
= wxString(tmpPtr
, *wxConvCurrent
, tmpSize
);
1384 char* tmpPtr
; int tmpSize
;
1385 PyString_AsStringAndSize(source
, &tmpPtr
, &tmpSize
);
1386 target
= wxString(tmpPtr
, tmpSize
);
1387 #endif // wxUSE_UNICODE
1389 #else // No Python unicode API (1.5.2)
1390 if (!PyString_Check(source
)) {
1391 // Convert to String if not one already...
1392 source
= PyObject_Str(source
);
1395 target
= wxString(PyString_AS_STRING(source
), PyString_GET_SIZE(source
));
1404 // Make either a Python String or Unicode object, depending on build mode
1405 PyObject
* wx2PyString(const wxString
& src
)
1409 str
= PyUnicode_FromUnicode(src
.c_str(), src
.Len());
1411 str
= PyString_FromStringAndSize(src
.c_str(), src
.Len());
1417 //----------------------------------------------------------------------
1420 byte
* byte_LIST_helper(PyObject
* source
) {
1421 if (!PyList_Check(source
)) {
1422 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1425 int count
= PyList_Size(source
);
1426 byte
* temp
= new byte
[count
];
1428 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1431 for (int x
=0; x
<count
; x
++) {
1432 PyObject
* o
= PyList_GetItem(source
, x
);
1433 if (! PyInt_Check(o
)) {
1434 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1437 temp
[x
] = (byte
)PyInt_AsLong(o
);
1443 int* int_LIST_helper(PyObject
* source
) {
1444 if (!PyList_Check(source
)) {
1445 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1448 int count
= PyList_Size(source
);
1449 int* temp
= new int[count
];
1451 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1454 for (int x
=0; x
<count
; x
++) {
1455 PyObject
* o
= PyList_GetItem(source
, x
);
1456 if (! PyInt_Check(o
)) {
1457 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1460 temp
[x
] = PyInt_AsLong(o
);
1466 long* long_LIST_helper(PyObject
* source
) {
1467 if (!PyList_Check(source
)) {
1468 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1471 int count
= PyList_Size(source
);
1472 long* temp
= new long[count
];
1474 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1477 for (int x
=0; x
<count
; x
++) {
1478 PyObject
* o
= PyList_GetItem(source
, x
);
1479 if (! PyInt_Check(o
)) {
1480 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1483 temp
[x
] = PyInt_AsLong(o
);
1489 char** string_LIST_helper(PyObject
* source
) {
1490 if (!PyList_Check(source
)) {
1491 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1494 int count
= PyList_Size(source
);
1495 char** temp
= new char*[count
];
1497 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1500 for (int x
=0; x
<count
; x
++) {
1501 PyObject
* o
= PyList_GetItem(source
, x
);
1502 if (! PyString_Check(o
)) {
1503 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1506 temp
[x
] = PyString_AsString(o
);
1511 //--------------------------------
1512 // Part of patch from Tim Hochberg
1513 static inline bool wxPointFromObjects(PyObject
* o1
, PyObject
* o2
, wxPoint
* point
) {
1514 if (PyInt_Check(o1
) && PyInt_Check(o2
)) {
1515 point
->x
= PyInt_AS_LONG(o1
);
1516 point
->y
= PyInt_AS_LONG(o2
);
1519 if (PyFloat_Check(o1
) && PyFloat_Check(o2
)) {
1520 point
->x
= (int)PyFloat_AS_DOUBLE(o1
);
1521 point
->y
= (int)PyFloat_AS_DOUBLE(o2
);
1524 if (PyInstance_Check(o1
) || PyInstance_Check(o2
)) {
1525 // Disallow instances because they can cause havok
1528 if (PyNumber_Check(o1
) && PyNumber_Check(o2
)) {
1529 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
1530 point
->x
= PyInt_AsLong(o1
);
1531 point
->y
= PyInt_AsLong(o2
);
1538 wxPoint
* wxPoint_LIST_helper(PyObject
* source
, int *count
) {
1539 // Putting all of the declarations here allows
1540 // us to put the error handling all in one place.
1543 PyObject
*o
, *o1
, *o2
;
1544 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1546 if (!PySequence_Check(source
)) {
1550 // The length of the sequence is returned in count.
1551 *count
= PySequence_Length(source
);
1556 temp
= new wxPoint
[*count
];
1558 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1561 for (x
=0; x
<*count
; x
++) {
1562 // Get an item: try fast way first.
1564 o
= PySequence_Fast_GET_ITEM(source
, x
);
1567 o
= PySequence_GetItem(source
, x
);
1573 // Convert o to wxPoint.
1574 if ((PyTuple_Check(o
) && PyTuple_GET_SIZE(o
) == 2) ||
1575 (PyList_Check(o
) && PyList_GET_SIZE(o
) == 2)) {
1576 o1
= PySequence_Fast_GET_ITEM(o
, 0);
1577 o2
= PySequence_Fast_GET_ITEM(o
, 1);
1578 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
1582 else if (PyInstance_Check(o
)) {
1584 if (SWIG_GetPtrObj(o
, (void **)&pt
, "_wxPoint_p")) {
1589 else if (PySequence_Check(o
) && PySequence_Length(o
) == 2) {
1590 o1
= PySequence_GetItem(o
, 0);
1591 o2
= PySequence_GetItem(o
, 1);
1592 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
1616 PyErr_SetString(PyExc_TypeError
, "Expected a sequence of length-2 sequences or wxPoints.");
1620 //------------------------------
1623 wxBitmap
** wxBitmap_LIST_helper(PyObject
* source
) {
1624 if (!PyList_Check(source
)) {
1625 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1628 int count
= PyList_Size(source
);
1629 wxBitmap
** temp
= new wxBitmap
*[count
];
1631 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1634 for (int x
=0; x
<count
; x
++) {
1635 PyObject
* o
= PyList_GetItem(source
, x
);
1636 if (PyInstance_Check(o
)) {
1638 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxBitmap_p")) {
1639 PyErr_SetString(PyExc_TypeError
,"Expected _wxBitmap_p.");
1645 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxBitmaps.");
1654 wxString
* wxString_LIST_helper(PyObject
* source
) {
1655 if (!PyList_Check(source
)) {
1656 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1659 int count
= PyList_Size(source
);
1660 wxString
* temp
= new wxString
[count
];
1662 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1665 for (int x
=0; x
<count
; x
++) {
1666 PyObject
* o
= PyList_GetItem(source
, x
);
1667 #if PYTHON_API_VERSION >= 1009
1668 if (! PyString_Check(o
) && ! PyUnicode_Check(o
)) {
1669 PyErr_SetString(PyExc_TypeError
, "Expected a list of string or unicode objects.");
1673 if (! PyString_Check(o
)) {
1674 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1679 wxString
* pStr
= wxString_in_helper(o
);
1687 wxAcceleratorEntry
* wxAcceleratorEntry_LIST_helper(PyObject
* source
) {
1688 if (!PyList_Check(source
)) {
1689 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1692 int count
= PyList_Size(source
);
1693 wxAcceleratorEntry
* temp
= new wxAcceleratorEntry
[count
];
1695 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1698 for (int x
=0; x
<count
; x
++) {
1699 PyObject
* o
= PyList_GetItem(source
, x
);
1700 if (PyInstance_Check(o
)) {
1701 wxAcceleratorEntry
* ae
;
1702 if (SWIG_GetPtrObj(o
, (void **) &ae
,"_wxAcceleratorEntry_p")) {
1703 PyErr_SetString(PyExc_TypeError
,"Expected _wxAcceleratorEntry_p.");
1708 else if (PyTuple_Check(o
)) {
1709 PyObject
* o1
= PyTuple_GetItem(o
, 0);
1710 PyObject
* o2
= PyTuple_GetItem(o
, 1);
1711 PyObject
* o3
= PyTuple_GetItem(o
, 2);
1712 temp
[x
].Set(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
));
1715 PyErr_SetString(PyExc_TypeError
, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
1723 wxPen
** wxPen_LIST_helper(PyObject
* source
) {
1724 if (!PyList_Check(source
)) {
1725 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1728 int count
= PyList_Size(source
);
1729 wxPen
** temp
= new wxPen
*[count
];
1731 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1734 for (int x
=0; x
<count
; x
++) {
1735 PyObject
* o
= PyList_GetItem(source
, x
);
1736 if (PyInstance_Check(o
)) {
1738 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxPen_p")) {
1740 PyErr_SetString(PyExc_TypeError
,"Expected _wxPen_p.");
1747 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxPens.");
1755 bool _2int_seq_helper(PyObject
* source
, int* i1
, int* i2
) {
1756 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1759 if (!PySequence_Check(source
) || PySequence_Length(source
) != 2)
1763 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1764 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1767 o1
= PySequence_GetItem(source
, 0);
1768 o2
= PySequence_GetItem(source
, 1);
1771 *i1
= PyInt_AsLong(o1
);
1772 *i2
= PyInt_AsLong(o2
);
1782 bool _4int_seq_helper(PyObject
* source
, int* i1
, int* i2
, int* i3
, int* i4
) {
1783 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1784 PyObject
*o1
, *o2
, *o3
, *o4
;
1786 if (!PySequence_Check(source
) || PySequence_Length(source
) != 4)
1790 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1791 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1792 o3
= PySequence_Fast_GET_ITEM(source
, 2);
1793 o4
= PySequence_Fast_GET_ITEM(source
, 3);
1796 o1
= PySequence_GetItem(source
, 0);
1797 o2
= PySequence_GetItem(source
, 1);
1798 o3
= PySequence_GetItem(source
, 2);
1799 o4
= PySequence_GetItem(source
, 3);
1802 *i1
= PyInt_AsLong(o1
);
1803 *i2
= PyInt_AsLong(o2
);
1804 *i3
= PyInt_AsLong(o3
);
1805 *i4
= PyInt_AsLong(o4
);
1817 //----------------------------------------------------------------------
1819 bool wxSize_helper(PyObject
* source
, wxSize
** obj
) {
1821 // If source is an object instance then it may already be the right type
1822 if (PyInstance_Check(source
)) {
1824 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxSize_p"))
1829 // otherwise a 2-tuple of integers is expected
1830 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1831 PyObject
* o1
= PySequence_GetItem(source
, 0);
1832 PyObject
* o2
= PySequence_GetItem(source
, 1);
1833 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1838 **obj
= wxSize(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1845 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxSize object.");
1849 bool wxPoint_helper(PyObject
* source
, wxPoint
** obj
) {
1851 // If source is an object instance then it may already be the right type
1852 if (PyInstance_Check(source
)) {
1854 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxPoint_p"))
1859 // otherwise a length-2 sequence of integers is expected
1860 if (PySequence_Check(source
) && PySequence_Length(source
) == 2) {
1861 PyObject
* o1
= PySequence_GetItem(source
, 0);
1862 PyObject
* o2
= PySequence_GetItem(source
, 1);
1863 // This should really check for integers, not numbers -- but that would break code.
1864 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1869 **obj
= wxPoint(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1875 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxPoint object.");
1881 bool wxRealPoint_helper(PyObject
* source
, wxRealPoint
** obj
) {
1883 // If source is an object instance then it may already be the right type
1884 if (PyInstance_Check(source
)) {
1886 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRealPoint_p"))
1891 // otherwise a 2-tuple of floats is expected
1892 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1893 PyObject
* o1
= PySequence_GetItem(source
, 0);
1894 PyObject
* o2
= PySequence_GetItem(source
, 1);
1895 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1900 **obj
= wxRealPoint(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
));
1907 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxRealPoint object.");
1914 bool wxRect_helper(PyObject
* source
, wxRect
** obj
) {
1916 // If source is an object instance then it may already be the right type
1917 if (PyInstance_Check(source
)) {
1919 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRect_p"))
1924 // otherwise a 4-tuple of integers is expected
1925 else if (PySequence_Check(source
) && PyObject_Length(source
) == 4) {
1926 PyObject
* o1
= PySequence_GetItem(source
, 0);
1927 PyObject
* o2
= PySequence_GetItem(source
, 1);
1928 PyObject
* o3
= PySequence_GetItem(source
, 2);
1929 PyObject
* o4
= PySequence_GetItem(source
, 3);
1930 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
) ||
1931 !PyNumber_Check(o3
) || !PyNumber_Check(o4
)) {
1938 **obj
= wxRect(PyInt_AsLong(o1
), PyInt_AsLong(o2
),
1939 PyInt_AsLong(o3
), PyInt_AsLong(o4
));
1948 PyErr_SetString(PyExc_TypeError
, "Expected a 4-tuple of integers or a wxRect object.");
1954 bool wxColour_helper(PyObject
* source
, wxColour
** obj
) {
1956 // If source is an object instance then it may already be the right type
1957 if (PyInstance_Check(source
)) {
1959 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxColour_p"))
1964 // otherwise a string is expected
1965 else if (PyString_Check(source
)) {
1966 wxString
spec(PyString_AS_STRING(source
), *wxConvCurrent
);
1967 if (spec
.GetChar(0) == '#' && spec
.Length() == 7) { // It's #RRGGBB
1968 long red
, green
, blue
;
1969 red
= green
= blue
= 0;
1970 spec
.Mid(1,2).ToLong(&red
, 16);
1971 spec
.Mid(3,2).ToLong(&green
, 16);
1972 spec
.Mid(5,2).ToLong(&blue
, 16);
1974 **obj
= wxColour(red
, green
, blue
);
1977 else { // it's a colour name
1978 **obj
= wxColour(spec
);
1984 PyErr_SetString(PyExc_TypeError
,
1985 "Expected a wxColour object or a string containing a colour "
1986 "name or '#RRGGBB'.");
1991 //----------------------------------------------------------------------
1993 PyObject
* wxArrayString2PyList_helper(const wxArrayString
& arr
) {
1995 PyObject
* list
= PyList_New(0);
1996 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
1998 PyObject
* str
= PyUnicode_FromUnicode(arr
[i
].c_str(), arr
[i
].Len());
2000 PyObject
* str
= PyString_FromStringAndSize(arr
[i
].c_str(), arr
[i
].Len());
2002 PyList_Append(list
, str
);
2009 PyObject
* wxArrayInt2PyList_helper(const wxArrayInt
& arr
) {
2011 PyObject
* list
= PyList_New(0);
2012 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
2013 PyObject
* number
= PyInt_FromLong(arr
[i
]);
2014 PyList_Append(list
, number
);
2021 //----------------------------------------------------------------------
2022 //----------------------------------------------------------------------