]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/src/helpers.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Helper functions/classes for the wxPython extension module
9 // Copyright: (c) 1998 by Total Control Software
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
17 #include "pyistream.h"
20 #include <wx/msw/private.h>
21 #include <wx/msw/winundef.h>
22 #include <wx/msw/msvcrt.h>
27 #include <gdk/gdkprivate.h>
28 #include <wx/gtk/win_gtk.h>
31 //----------------------------------------------------------------------
33 #if PYTHON_API_VERSION <= 1007 && wxUSE_UNICODE
34 #error Python must support Unicode to use wxWindows Unicode
37 //----------------------------------------------------------------------
40 int WXDLLEXPORT
wxEntryStart( int& argc
, char** argv
);
42 int WXDLLEXPORT
wxEntryStart( int argc
, char** argv
);
44 int WXDLLEXPORT
wxEntryInitGui();
45 void WXDLLEXPORT
wxEntryCleanup();
47 wxPyApp
* wxPythonApp
= NULL
; // Global instance of application object
48 bool wxPyDoCleanup
= FALSE
;
51 #ifdef WXP_WITH_THREAD
52 struct wxPyThreadState
{
54 PyThreadState
* tstate
;
56 wxPyThreadState(unsigned long _tid
=0, PyThreadState
* _tstate
=NULL
)
57 : tid(_tid
), tstate(_tstate
) {}
60 #include <wx/dynarray.h>
61 WX_DECLARE_OBJARRAY(wxPyThreadState
, wxPyThreadStateArray
);
62 #include <wx/arrimpl.cpp>
63 WX_DEFINE_OBJARRAY(wxPyThreadStateArray
);
65 wxPyThreadStateArray
* wxPyTStates
= NULL
;
66 wxMutex
* wxPyTMutex
= NULL
;
70 #ifdef __WXMSW__ // If building for win32...
71 //----------------------------------------------------------------------
72 // This gets run when the DLL is loaded. We just need to save a handle.
73 //----------------------------------------------------------------------
76 HINSTANCE hinstDLL
, // handle to DLL module
77 DWORD fdwReason
, // reason for calling function
78 LPVOID lpvReserved
// reserved
81 // If wxPython is embedded in another wxWindows app then
82 // the inatance has already been set.
83 if (! wxGetInstance())
84 wxSetInstance(hinstDLL
);
89 //----------------------------------------------------------------------
90 // Classes for implementing the wxp main application shell.
91 //----------------------------------------------------------------------
95 SetUseBestVisual(TRUE
);
102 // This one isn't acutally called... See __wxStart()
103 bool wxPyApp::OnInit() {
108 int wxPyApp::MainLoop() {
111 DeletePendingObjects();
112 bool initialized
= wxTopLevelWindows
.GetCount() != 0;
114 m_initialized
= initialized
;
118 if ( m_exitOnFrameDelete
== Later
) {
119 m_exitOnFrameDelete
= Yes
;
122 retval
= wxApp::MainLoop();
130 //---------------------------------------------------------------------
131 //----------------------------------------------------------------------
134 static char* wxPyCopyCString(const wxChar
* src
)
136 wxWX2MBbuf buff
= (wxWX2MBbuf
)wxConvCurrent
->cWX2MB(src
);
137 size_t len
= strlen(buff
);
138 char* dest
= new char[len
+1];
144 static char* wxPyCopyCString(const char* src
) // we need a char version too
146 size_t len
= strlen(src
);
147 char* dest
= new char[len
+1];
153 static wxChar
* wxPyCopyWString(const char *src
)
155 //wxMB2WXbuf buff = wxConvCurrent->cMB2WX(src);
156 wxString
str(src
, *wxConvCurrent
);
157 return copystring(str
);
161 static wxChar
* wxPyCopyWString(const wxChar
*src
)
163 return copystring(src
);
168 //----------------------------------------------------------------------
170 // This is where we pick up the first part of the wxEntry functionality...
171 // The rest is in __wxStart and __wxCleanup. This function is called when
172 // wxcmodule is imported. (Before there is a wxApp object.)
177 // wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
180 #ifdef WXP_WITH_THREAD
181 PyEval_InitThreads();
182 wxPyTStates
= new wxPyThreadStateArray
;
183 wxPyTMutex
= new wxMutex
;
186 wxApp::CheckBuildOptions(wxBuildOptions());
188 // Bail out if there is already a wxApp created. This means that the
189 // toolkit has already been initialized, as in embedding wxPython in
190 // a C++ wxWindows app, so we don't need to call wxEntryStart.
191 if (wxTheApp
!= NULL
) {
194 wxPyDoCleanup
= TRUE
;
198 PyObject
* sysargv
= PySys_GetObject("argv");
199 if (sysargv
!= NULL
) {
200 argc
= PyList_Size(sysargv
);
201 argv
= new char*[argc
+1];
203 for(x
=0; x
<argc
; x
++) {
204 PyObject
*item
= PyList_GetItem(sysargv
, x
);
206 if (PyUnicode_Check(item
))
207 argv
[x
] = wxPyCopyCString(PyUnicode_AS_UNICODE(item
));
210 argv
[x
] = wxPyCopyCString(PyString_AsString(item
));
215 wxEntryStart(argc
, argv
);
221 // Start the user application, user App's OnInit method is a parameter here
222 PyObject
* __wxStart(PyObject
* /* self */, PyObject
* args
)
224 PyObject
* onInitFunc
= NULL
;
229 if (!PyArg_ParseTuple(args
, "O", &onInitFunc
))
232 // This is the next part of the wxEntry functionality...
234 wxChar
** argv
= NULL
;
235 PyObject
* sysargv
= PySys_GetObject("argv");
236 if (sysargv
!= NULL
) {
237 argc
= PyList_Size(sysargv
);
238 argv
= new wxChar
*[argc
+1];
240 for(x
=0; x
<argc
; x
++) {
241 PyObject
*pyArg
= PyList_GetItem(sysargv
, x
);
243 if (PyUnicode_Check(pyArg
))
244 argv
[x
] = wxPyCopyWString(PyUnicode_AS_UNICODE(pyArg
));
247 argv
[x
] = wxPyCopyWString(PyString_AsString(pyArg
));
252 wxPythonApp
->argc
= argc
;
253 wxPythonApp
->argv
= argv
;
257 // Call the Python App's OnInit function
258 arglist
= PyTuple_New(0);
259 result
= PyEval_CallObject(onInitFunc
, arglist
);
260 if (!result
) { // an exception was raised.
264 if (! PyInt_Check(result
)) {
265 PyErr_SetString(PyExc_TypeError
, "OnInit should return a boolean value");
268 bResult
= PyInt_AS_LONG(result
);
270 PyErr_SetString(PyExc_SystemExit
, "OnInit returned FALSE, exiting...");
275 wxTheApp
->m_initialized
= (wxTopLevelWindows
.GetCount() > 0);
286 #ifdef WXP_WITH_THREAD
289 wxPyTStates
->Empty();
297 static PyObject
* wxPython_dict
= NULL
;
298 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__"
339 PyDict_SetItemString(wxPython_dict
, "wxPlatform", PyString_FromString(wxPlatform
));
340 PyDict_SetItemString(wxPython_dict
, "wxUSE_UNICODE", PyInt_FromLong(wxUSE_UNICODE
));
341 PyDict_SetItemString(wxPython_dict
, "__WXDEBUG__", PyInt_FromLong(wxdebug
));
347 //---------------------------------------------------------------------------
349 void wxPyClientData_dtor(wxPyClientData
* self
) {
350 wxPyBeginBlockThreads();
351 Py_DECREF(self
->m_obj
);
352 wxPyEndBlockThreads();
355 void wxPyUserData_dtor(wxPyUserData
* self
) {
356 wxPyBeginBlockThreads();
357 Py_DECREF(self
->m_obj
);
358 wxPyEndBlockThreads();
362 // This is called when an OOR controled object is being destroyed. Although
363 // the C++ object is going away there is no way to force the Python object
364 // (and all references to it) to die too. This causes problems (crashes) in
365 // wxPython when a python shadow object attempts to call a C++ method using
366 // the now bogus pointer... So to try and prevent this we'll do a little black
367 // magic and change the class of the python instance to a class that will
368 // raise an exception for any attempt to call methods with it. See
369 // _wxPyDeadObject in _extras.py for the implementation of this class.
370 void wxPyOORClientData_dtor(wxPyOORClientData
* self
) {
372 static PyObject
* deadObjectClass
= NULL
;
374 wxPyBeginBlockThreads();
375 if (deadObjectClass
== NULL
) {
376 deadObjectClass
= PyDict_GetItemString(wxPython_dict
, "_wxPyDeadObject");
377 wxASSERT_MSG(deadObjectClass
!= NULL
, wxT("Can't get _wxPyDeadObject class!"));
378 Py_INCREF(deadObjectClass
);
381 // Clear the instance's dictionary, put the name of the old class into the
382 // instance, and then reset the class to be the dead class.
383 if (self
->m_obj
->ob_refcnt
> 1) { // but only if there is more than one reference
384 wxASSERT_MSG(PyInstance_Check(self
->m_obj
), wxT("m_obj not an instance!?!?!"));
385 PyInstanceObject
* inst
= (PyInstanceObject
*)self
->m_obj
;
386 PyDict_Clear(inst
->in_dict
);
387 PyDict_SetItemString(inst
->in_dict
, "_name", inst
->in_class
->cl_name
);
388 inst
->in_class
= (PyClassObject
*)deadObjectClass
;
389 Py_INCREF(deadObjectClass
);
391 wxPyEndBlockThreads();
394 //---------------------------------------------------------------------------
395 // Stuff used by OOR to find the right wxPython class type to return and to
399 // The pointer type map is used when the "pointer" type name generated by SWIG
400 // is not the same as the shadow class name, for example wxPyTreeCtrl
401 // vs. wxTreeCtrl. It needs to be referenced in Python as well as from C++,
402 // so we'll just make it a Python dictionary in the wx module's namespace.
403 // (See __wxSetDictionary)
404 void wxPyPtrTypeMap_Add(const char* commonName
, const char* ptrName
) {
405 if (! wxPyPtrTypeMap
)
406 wxPyPtrTypeMap
= PyDict_New();
407 PyDict_SetItemString(wxPyPtrTypeMap
,
409 PyString_FromString((char*)ptrName
));
414 PyObject
* wxPyClassExists(const wxString
& className
) {
419 char buff
[64]; // should always be big enough...
421 sprintf(buff
, "%sPtr", className
.mbc_str());
422 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
424 return classobj
; // returns NULL if not found
428 PyObject
* wxPyMake_wxObject(wxObject
* source
, bool checkEvtHandler
) {
429 PyObject
* target
= NULL
;
430 bool isEvtHandler
= FALSE
;
433 // If it's derived from wxEvtHandler then there may
434 // already be a pointer to a Python object that we can use
436 if (checkEvtHandler
&& wxIsKindOf(source
, wxEvtHandler
)) {
438 wxEvtHandler
* eh
= (wxEvtHandler
*)source
;
439 wxPyOORClientData
* data
= (wxPyOORClientData
*)eh
->GetClientObject();
441 target
= data
->m_obj
;
447 // Otherwise make it the old fashioned way by making a
448 // new shadow object and putting this pointer in it.
449 wxClassInfo
* info
= source
->GetClassInfo();
450 wxChar
* name
= (wxChar
*)info
->GetClassName();
451 PyObject
* klass
= wxPyClassExists(name
);
452 while (info
&& !klass
) {
453 name
= (wxChar
*)info
->GetBaseClassName1();
454 info
= wxClassInfo::FindClass(name
);
455 klass
= wxPyClassExists(name
);
458 target
= wxPyConstructObject(source
, name
, klass
, FALSE
);
459 if (target
&& isEvtHandler
)
460 ((wxEvtHandler
*)source
)->SetClientObject(new wxPyOORClientData(target
));
462 wxString
msg(wxT("wxPython class not found for "));
463 msg
+= source
->GetClassInfo()->GetClassName();
464 PyErr_SetString(PyExc_NameError
, msg
.mbc_str());
468 } else { // source was NULL so return None.
469 Py_INCREF(Py_None
); target
= Py_None
;
475 PyObject
* wxPyMake_wxSizer(wxSizer
* source
) {
476 PyObject
* target
= NULL
;
478 if (source
&& wxIsKindOf(source
, wxSizer
)) {
479 // If it's derived from wxSizer then there may
480 // already be a pointer to a Python object that we can use
482 wxSizer
* sz
= (wxSizer
*)source
;
483 wxPyOORClientData
* data
= (wxPyOORClientData
*)sz
->GetClientObject();
485 target
= data
->m_obj
;
490 target
= wxPyMake_wxObject(source
, FALSE
);
491 if (target
!= Py_None
)
492 ((wxSizer
*)source
)->SetClientObject(new wxPyOORClientData(target
));
499 //---------------------------------------------------------------------------
501 PyObject
* wxPyConstructObject(void* ptr
,
502 const wxString
& className
,
509 wxString
name(className
);
510 char swigptr
[64]; // should always be big enough...
513 if ((item
= PyDict_GetItemString(wxPyPtrTypeMap
, (char*)(const char*)name
.mbc_str())) != NULL
) {
514 name
= wxString(PyString_AsString(item
), *wxConvCurrent
);
516 sprintf(buff
, "_%s_p", (const char*)name
.mbc_str());
517 SWIG_MakePtr(swigptr
, ptr
, buff
);
519 arg
= Py_BuildValue("(s)", swigptr
);
520 obj
= PyInstance_New(klass
, arg
, NULL
);
524 PyObject
* one
= PyInt_FromLong(1);
525 PyObject_SetAttrString(obj
, "thisown", one
);
533 PyObject
* wxPyConstructObject(void* ptr
,
534 const wxString
& className
,
541 char buff
[64]; // should always be big enough...
542 sprintf(buff
, "%sPtr", (const char*)className
.mbc_str());
544 wxASSERT_MSG(wxPython_dict
, wxT("wxPython_dict is not set yet!!"));
546 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
548 wxString
msg(wxT("wxPython class not found for "));
550 PyErr_SetString(PyExc_NameError
, msg
.mbc_str());
554 return wxPyConstructObject(ptr
, className
, classobj
, setThisOwn
);
558 //---------------------------------------------------------------------------
561 #ifdef WXP_WITH_THREAD
563 unsigned long wxPyGetCurrentThreadId() {
564 return wxThread::GetCurrentId();
567 static PyThreadState
* gs_shutdownTState
;
569 PyThreadState
* wxPyGetThreadState() {
570 if (wxPyTMutex
== NULL
) // Python is shutting down...
571 return gs_shutdownTState
;
573 unsigned long ctid
= wxPyGetCurrentThreadId();
574 PyThreadState
* tstate
= NULL
;
577 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
578 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
579 if (info
.tid
== ctid
) {
580 tstate
= info
.tstate
;
584 wxPyTMutex
->Unlock();
585 wxASSERT_MSG(tstate
, wxT("PyThreadState should not be NULL!"));
590 void wxPySaveThreadState(PyThreadState
* tstate
) {
591 if (wxPyTMutex
== NULL
) { // Python is shutting down, assume a single thread...
592 gs_shutdownTState
= tstate
;
595 unsigned long ctid
= wxPyGetCurrentThreadId();
597 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
598 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
599 if (info
.tid
== ctid
) {
601 if (info
.tstate
!= tstate
)
602 wxLogMessage("*** tstate mismatch!???");
604 // info.tstate = tstate; *** DO NOT update existing ones???
605 // Normally it will never change, but apparently COM callbacks
606 // (i.e. ActiveX controls) will (incorrectly IMHO) use a transient
607 // tstate which will then be garbage the next time we try to use
609 wxPyTMutex
->Unlock();
613 // not found, so add it...
614 wxPyTStates
->Add(new wxPyThreadState(ctid
, tstate
));
615 wxPyTMutex
->Unlock();
621 // Calls from Python to wxWindows code are wrapped in calls to these
624 PyThreadState
* wxPyBeginAllowThreads() {
625 #ifdef WXP_WITH_THREAD
626 PyThreadState
* saved
= PyEval_SaveThread(); // Py_BEGIN_ALLOW_THREADS;
627 wxPySaveThreadState(saved
);
634 void wxPyEndAllowThreads(PyThreadState
* saved
) {
635 #ifdef WXP_WITH_THREAD
636 PyEval_RestoreThread(saved
); // Py_END_ALLOW_THREADS;
642 // Calls from wxWindows back to Python code, or even any PyObject
643 // manipulations, PyDECREF's and etc. are wrapped in calls to these functions:
645 void wxPyBeginBlockThreads() {
646 #ifdef WXP_WITH_THREAD
647 PyThreadState
* tstate
= wxPyGetThreadState();
648 PyEval_RestoreThread(tstate
);
653 void wxPyEndBlockThreads() {
654 #ifdef WXP_WITH_THREAD
655 // Is there any need to save it again?
656 // PyThreadState* tstate =
662 //---------------------------------------------------------------------------
663 // wxPyInputStream and wxPyCBInputStream methods
666 void wxPyInputStream::close() {
667 /* do nothing for now */
670 void wxPyInputStream::flush() {
671 /* do nothing for now */
674 bool wxPyInputStream::eof() {
676 return m_wxis
->Eof();
681 wxPyInputStream::~wxPyInputStream() {
688 PyObject
* wxPyInputStream::read(int size
) {
689 PyObject
* obj
= NULL
;
691 const int BUFSIZE
= 1024;
693 // check if we have a real wxInputStream to work with
695 PyErr_SetString(PyExc_IOError
, "no valid C-wxInputStream");
701 while (! m_wxis
->Eof()) {
702 m_wxis
->Read(buf
.GetAppendBuf(BUFSIZE
), BUFSIZE
);
703 buf
.UngetAppendBuf(m_wxis
->LastRead());
706 } else { // Read only size number of characters
707 m_wxis
->Read(buf
.GetWriteBuf(size
), size
);
708 buf
.UngetWriteBuf(m_wxis
->LastRead());
712 if (m_wxis
->LastError() == wxSTREAM_READ_ERROR
) {
713 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
716 // We use only strings for the streams, not unicode
717 obj
= PyString_FromStringAndSize(buf
, buf
.GetDataLen());
723 PyObject
* wxPyInputStream::readline(int size
) {
724 PyObject
* obj
= NULL
;
729 // check if we have a real wxInputStream to work with
731 PyErr_SetString(PyExc_IOError
,"no valid C-wxInputStream");
735 // read until \n or byte limit reached
736 for (i
=ch
=0; (ch
!= '\n') && (!m_wxis
->Eof()) && ((size
< 0) || (i
< size
)); i
++) {
742 if (m_wxis
->LastError() == wxSTREAM_READ_ERROR
) {
743 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
746 // We use only strings for the streams, not unicode
747 obj
= PyString_FromStringAndSize((char*)buf
.GetData(), buf
.GetDataLen());
753 PyObject
* wxPyInputStream::readlines(int sizehint
) {
756 // check if we have a real wxInputStream to work with
758 PyErr_SetString(PyExc_IOError
,"no valid C-wxInputStream below");
763 pylist
= PyList_New(0);
769 // read sizehint bytes or until EOF
771 for (i
=0; (!m_wxis
->Eof()) && ((sizehint
< 0) || (i
< sizehint
));) {
772 PyObject
* s
= this->readline();
777 PyList_Append(pylist
, s
);
778 i
+= PyString_Size(s
);
782 if (m_wxis
->LastError() == wxSTREAM_READ_ERROR
) {
784 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
792 void wxPyInputStream::seek(int offset
, int whence
) {
794 m_wxis
->SeekI(offset
, wxSeekMode(whence
));
797 int wxPyInputStream::tell(){
799 return m_wxis
->TellI();
806 wxPyCBInputStream::wxPyCBInputStream(PyObject
*r
, PyObject
*s
, PyObject
*t
, bool block
)
807 : wxInputStream(), m_read(r
), m_seek(s
), m_tell(t
), m_block(block
)
811 wxPyCBInputStream::~wxPyCBInputStream() {
812 if (m_block
) wxPyBeginBlockThreads();
816 if (m_block
) wxPyEndBlockThreads();
820 wxPyCBInputStream
* wxPyCBInputStream::create(PyObject
*py
, bool block
) {
821 if (block
) wxPyBeginBlockThreads();
823 PyObject
* read
= getMethod(py
, "read");
824 PyObject
* seek
= getMethod(py
, "seek");
825 PyObject
* tell
= getMethod(py
, "tell");
828 PyErr_SetString(PyExc_TypeError
, "Not a file-like object");
832 if (block
) wxPyEndBlockThreads();
836 if (block
) wxPyEndBlockThreads();
837 return new wxPyCBInputStream(read
, seek
, tell
, block
);
841 wxPyCBInputStream
* wxPyCBInputStream_create(PyObject
*py
, bool block
) {
842 return wxPyCBInputStream::create(py
, block
);
845 PyObject
* wxPyCBInputStream::getMethod(PyObject
* py
, char* name
) {
846 if (!PyObject_HasAttrString(py
, name
))
848 PyObject
* o
= PyObject_GetAttrString(py
, name
);
849 if (!PyMethod_Check(o
) && !PyCFunction_Check(o
)) {
857 size_t wxPyCBInputStream::GetSize() const {
858 wxPyCBInputStream
* self
= (wxPyCBInputStream
*)this; // cast off const
859 if (m_seek
&& m_tell
) {
860 off_t temp
= self
->OnSysTell();
861 off_t ret
= self
->OnSysSeek(0, wxFromEnd
);
862 self
->OnSysSeek(temp
, wxFromStart
);
870 size_t wxPyCBInputStream::OnSysRead(void *buffer
, size_t bufsize
) {
874 wxPyBeginBlockThreads();
875 PyObject
* arglist
= Py_BuildValue("(i)", bufsize
);
876 PyObject
* result
= PyEval_CallObject(m_read
, arglist
);
880 if ((result
!= NULL
) && PyString_Check(result
)) {
881 o
= PyString_Size(result
);
883 m_lasterror
= wxSTREAM_EOF
;
886 memcpy((char*)buffer
, PyString_AsString(result
), o
); // strings only, not unicode...
891 m_lasterror
= wxSTREAM_READ_ERROR
;
892 wxPyEndBlockThreads();
897 size_t wxPyCBInputStream::OnSysWrite(const void *buffer
, size_t bufsize
) {
898 m_lasterror
= wxSTREAM_WRITE_ERROR
;
902 off_t
wxPyCBInputStream::OnSysSeek(off_t off
, wxSeekMode mode
) {
903 wxPyBeginBlockThreads();
904 PyObject
* arglist
= Py_BuildValue("(ii)", off
, mode
);
905 PyObject
* result
= PyEval_CallObject(m_seek
, arglist
);
908 wxPyEndBlockThreads();
912 off_t
wxPyCBInputStream::OnSysTell() const {
913 wxPyBeginBlockThreads();
914 PyObject
* arglist
= Py_BuildValue("()");
915 PyObject
* result
= PyEval_CallObject(m_tell
, arglist
);
918 if (result
!= NULL
) {
919 o
= PyInt_AsLong(result
);
922 wxPyEndBlockThreads();
926 //----------------------------------------------------------------------
928 IMPLEMENT_ABSTRACT_CLASS(wxPyCallback
, wxObject
);
930 wxPyCallback::wxPyCallback(PyObject
* func
) {
935 wxPyCallback::wxPyCallback(const wxPyCallback
& other
) {
936 m_func
= other
.m_func
;
940 wxPyCallback::~wxPyCallback() {
941 wxPyBeginBlockThreads();
943 wxPyEndBlockThreads();
948 // This function is used for all events destined for Python event handlers.
949 void wxPyCallback::EventThunker(wxEvent
& event
) {
950 wxPyCallback
* cb
= (wxPyCallback
*)event
.m_callbackUserData
;
951 PyObject
* func
= cb
->m_func
;
957 wxPyBeginBlockThreads();
958 wxString className
= event
.GetClassInfo()->GetClassName();
960 if (className
== "wxPyEvent")
961 arg
= ((wxPyEvent
*)&event
)->GetSelf();
962 else if (className
== "wxPyCommandEvent")
963 arg
= ((wxPyCommandEvent
*)&event
)->GetSelf();
965 arg
= wxPyConstructObject((void*)&event
, className
);
968 tuple
= PyTuple_New(1);
969 PyTuple_SET_ITEM(tuple
, 0, arg
);
970 result
= PyEval_CallObject(func
, tuple
);
974 PyErr_Clear(); // Just in case...
978 wxPyEndBlockThreads();
982 //----------------------------------------------------------------------
984 wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper
& other
) {
986 m_self
= other
.m_self
;
987 m_class
= other
.m_class
;
995 void wxPyCallbackHelper::setSelf(PyObject
* self
, PyObject
* klass
, int incref
) {
1006 #if PYTHON_API_VERSION >= 1011
1008 // Prior to Python 2.2 PyMethod_GetClass returned the class object
1009 // in which the method was defined. Starting with 2.2 it returns
1010 // "class that asked for the method" which seems totally bogus to me
1011 // but apprently it fixes some obscure problem waiting to happen in
1012 // Python. Since the API was not documented Guido and the gang felt
1013 // safe in changing it. Needless to say that totally screwed up the
1014 // logic below in wxPyCallbackHelper::findCallback, hence this icky
1015 // code to find the class where the method is actually defined...
1018 PyObject
* PyFindClassWithAttr(PyObject
*klass
, PyObject
*name
)
1022 if (PyType_Check(klass
)) { // new style classes
1023 // This code is borrowed/adapted from _PyType_Lookup in typeobject.c
1024 // (TODO: This part is not tested yet, so I'm not sure it is correct...)
1025 PyTypeObject
* type
= (PyTypeObject
*)klass
;
1026 PyObject
*mro
, *res
, *base
, *dict
;
1027 /* Look in tp_dict of types in MRO */
1029 assert(PyTuple_Check(mro
));
1030 n
= PyTuple_GET_SIZE(mro
);
1031 for (i
= 0; i
< n
; i
++) {
1032 base
= PyTuple_GET_ITEM(mro
, i
);
1033 if (PyClass_Check(base
))
1034 dict
= ((PyClassObject
*)base
)->cl_dict
;
1036 assert(PyType_Check(base
));
1037 dict
= ((PyTypeObject
*)base
)->tp_dict
;
1039 assert(dict
&& PyDict_Check(dict
));
1040 res
= PyDict_GetItem(dict
, name
);
1047 else if (PyClass_Check(klass
)) { // old style classes
1048 // This code is borrowed/adapted from class_lookup in classobject.c
1049 PyClassObject
* cp
= (PyClassObject
*)klass
;
1050 PyObject
*value
= PyDict_GetItem(cp
->cl_dict
, name
);
1051 if (value
!= NULL
) {
1052 return (PyObject
*)cp
;
1054 n
= PyTuple_Size(cp
->cl_bases
);
1055 for (i
= 0; i
< n
; i
++) {
1056 PyObject
* base
= PyTuple_GetItem(cp
->cl_bases
, i
);
1057 PyObject
*v
= PyFindClassWithAttr(base
, name
);
1069 PyObject
* PyMethod_GetDefiningClass(PyObject
* method
, const char* name
)
1071 PyObject
* mgc
= PyMethod_GET_CLASS(method
);
1073 #if PYTHON_API_VERSION <= 1010 // prior to Python 2.2, the easy way
1075 #else // 2.2 and after, the hard way...
1077 PyObject
* nameo
= PyString_FromString(name
);
1078 PyObject
* klass
= PyFindClassWithAttr(mgc
, nameo
);
1086 bool wxPyCallbackHelper::findCallback(const char* name
) const {
1087 wxPyCallbackHelper
* self
= (wxPyCallbackHelper
*)this; // cast away const
1088 self
->m_lastFound
= NULL
;
1090 // If the object (m_self) has an attibute of the given name...
1091 if (m_self
&& PyObject_HasAttrString(m_self
, (char*)name
)) {
1092 PyObject
*method
, *klass
;
1093 method
= PyObject_GetAttrString(m_self
, (char*)name
);
1095 // ...and if that attribute is a method, and if that method's class is
1096 // not from a base class...
1097 if (PyMethod_Check(method
) &&
1098 (klass
= PyMethod_GetDefiningClass(method
, (char*)name
)) != NULL
&&
1099 ((klass
== m_class
) || PyClass_IsSubclass(klass
, m_class
))) {
1101 // ...then we'll save a pointer to the method so callCallback can call it.
1102 self
->m_lastFound
= method
;
1108 return m_lastFound
!= NULL
;
1112 int wxPyCallbackHelper::callCallback(PyObject
* argTuple
) const {
1116 result
= callCallbackObj(argTuple
);
1117 if (result
) { // Assumes an integer return type...
1118 retval
= PyInt_AsLong(result
);
1120 PyErr_Clear(); // forget about it if it's not...
1125 // Invoke the Python callable object, returning the raw PyObject return
1126 // value. Caller should DECREF the return value and also call PyEval_SaveThread.
1127 PyObject
* wxPyCallbackHelper::callCallbackObj(PyObject
* argTuple
) const {
1130 // Save a copy of the pointer in case the callback generates another
1131 // callback. In that case m_lastFound will have a different value when
1132 // it gets back here...
1133 PyObject
* method
= m_lastFound
;
1135 result
= PyEval_CallObject(method
, argTuple
);
1136 Py_DECREF(argTuple
);
1145 void wxPyCBH_setCallbackInfo(wxPyCallbackHelper
& cbh
, PyObject
* self
, PyObject
* klass
, int incref
) {
1146 cbh
.setSelf(self
, klass
, incref
);
1149 bool wxPyCBH_findCallback(const wxPyCallbackHelper
& cbh
, const char* name
) {
1150 return cbh
.findCallback(name
);
1153 int wxPyCBH_callCallback(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
1154 return cbh
.callCallback(argTuple
);
1157 PyObject
* wxPyCBH_callCallbackObj(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
1158 return cbh
.callCallbackObj(argTuple
);
1162 void wxPyCBH_delete(wxPyCallbackHelper
* cbh
) {
1163 if (cbh
->m_incRef
) {
1164 wxPyBeginBlockThreads();
1165 Py_XDECREF(cbh
->m_self
);
1166 Py_XDECREF(cbh
->m_class
);
1167 wxPyEndBlockThreads();
1171 //---------------------------------------------------------------------------
1172 //---------------------------------------------------------------------------
1173 // These event classes can be derived from in Python and passed through the event
1174 // system without losing anything. They do this by keeping a reference to
1175 // themselves and some special case handling in wxPyCallback::EventThunker.
1178 wxPyEvtSelfRef::wxPyEvtSelfRef() {
1179 //m_self = Py_None; // **** We don't do normal ref counting to prevent
1180 //Py_INCREF(m_self); // circular loops...
1184 wxPyEvtSelfRef::~wxPyEvtSelfRef() {
1185 wxPyBeginBlockThreads();
1188 wxPyEndBlockThreads();
1191 void wxPyEvtSelfRef::SetSelf(PyObject
* self
, bool clone
) {
1192 wxPyBeginBlockThreads();
1200 wxPyEndBlockThreads();
1203 PyObject
* wxPyEvtSelfRef::GetSelf() const {
1209 IMPLEMENT_ABSTRACT_CLASS(wxPyEvent
, wxEvent
);
1210 IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent
, wxCommandEvent
);
1213 wxPyEvent::wxPyEvent(int id
)
1218 wxPyEvent::wxPyEvent(const wxPyEvent
& evt
)
1221 SetSelf(evt
.m_self
, TRUE
);
1225 wxPyEvent::~wxPyEvent() {
1229 wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType
, int id
)
1230 : wxCommandEvent(commandType
, id
) {
1234 wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent
& evt
)
1235 : wxCommandEvent(evt
)
1237 SetSelf(evt
.m_self
, TRUE
);
1241 wxPyCommandEvent::~wxPyCommandEvent() {
1247 //---------------------------------------------------------------------------
1248 //---------------------------------------------------------------------------
1251 wxPyTimer::wxPyTimer(PyObject
* callback
) {
1256 wxPyTimer::~wxPyTimer() {
1257 wxPyBeginBlockThreads();
1259 wxPyEndBlockThreads();
1262 void wxPyTimer::Notify() {
1263 if (!func
|| func
== Py_None
) {
1267 wxPyBeginBlockThreads();
1270 PyObject
* args
= Py_BuildValue("()");
1272 result
= PyEval_CallObject(func
, args
);
1281 wxPyEndBlockThreads();
1287 //---------------------------------------------------------------------------
1288 //---------------------------------------------------------------------------
1289 // Convert a wxList to a Python List
1291 PyObject
* wxPy_ConvertList(wxListBase
* list
, const char* className
) {
1295 wxNode
* node
= list
->First();
1297 wxPyBeginBlockThreads();
1298 pyList
= PyList_New(0);
1300 wxObj
= node
->Data();
1301 pyObj
= wxPyMake_wxObject(wxObj
); //wxPyConstructObject(wxObj, className);
1302 PyList_Append(pyList
, pyObj
);
1303 node
= node
->Next();
1305 wxPyEndBlockThreads();
1309 //----------------------------------------------------------------------
1311 long wxPyGetWinHandle(wxWindow
* win
) {
1313 return (long)win
->GetHandle();
1316 // Find and return the actual X-Window.
1318 if (win
->m_wxwindow
) {
1319 GdkWindowPrivate
* bwin
= (GdkWindowPrivate
*)GTK_PIZZA(win
->m_wxwindow
)->bin_window
;
1321 return (long)bwin
->xwindow
;
1328 //----------------------------------------------------------------------
1329 // Some helper functions for typemaps in my_typemaps.i, so they won't be
1330 // included in every file over and over again...
1332 #if PYTHON_API_VERSION >= 1009
1333 static char* wxStringErrorMsg
= "String or Unicode type required";
1335 static char* wxStringErrorMsg
= "String type required";
1339 wxString
* wxString_in_helper(PyObject
* source
) {
1341 #if PYTHON_API_VERSION >= 1009 // Have Python unicode API
1342 if (!PyString_Check(source
) && !PyUnicode_Check(source
)) {
1343 PyErr_SetString(PyExc_TypeError
, wxStringErrorMsg
);
1347 if (PyUnicode_Check(source
)) {
1348 target
= new wxString(PyUnicode_AS_UNICODE(source
));
1350 // It is a string, get pointers to it and transform to unicode
1351 char* tmpPtr
; int tmpSize
;
1352 PyString_AsStringAndSize(source
, &tmpPtr
, &tmpSize
);
1353 target
= new wxString(tmpPtr
, *wxConvCurrent
, tmpSize
);
1356 char* tmpPtr
; int tmpSize
;
1357 if (PyString_AsStringAndSize(source
, &tmpPtr
, &tmpSize
) == -1) {
1358 PyErr_SetString(PyExc_TypeError
, "Unable to convert string");
1361 target
= new wxString(tmpPtr
, tmpSize
);
1362 #endif // wxUSE_UNICODE
1364 #else // No Python unicode API (1.5.2)
1365 if (!PyString_Check(source
)) {
1366 PyErr_SetString(PyExc_TypeError
, wxStringErrorMsg
);
1369 target
= new wxString(PyString_AS_STRING(source
), PyString_GET_SIZE(source
));
1375 // Similar to above except doesn't use "new" and doesn't set an exception
1376 wxString
Py2wxString(PyObject
* source
)
1379 bool doDecRef
= FALSE
;
1381 #if PYTHON_API_VERSION >= 1009 // Have Python unicode API
1382 if (!PyString_Check(source
) && !PyUnicode_Check(source
)) {
1383 // Convert to String if not one already... (TODO: Unicode too?)
1384 source
= PyObject_Str(source
);
1389 if (PyUnicode_Check(source
)) {
1390 target
= PyUnicode_AS_UNICODE(source
);
1392 // It is a string, get pointers to it and transform to unicode
1393 char* tmpPtr
; int tmpSize
;
1394 PyString_AsStringAndSize(source
, &tmpPtr
, &tmpSize
);
1395 target
= wxString(tmpPtr
, *wxConvCurrent
, tmpSize
);
1398 char* tmpPtr
; int tmpSize
;
1399 PyString_AsStringAndSize(source
, &tmpPtr
, &tmpSize
);
1400 target
= wxString(tmpPtr
, tmpSize
);
1401 #endif // wxUSE_UNICODE
1403 #else // No Python unicode API (1.5.2)
1404 if (!PyString_Check(source
)) {
1405 // Convert to String if not one already...
1406 source
= PyObject_Str(source
);
1409 target
= wxString(PyString_AS_STRING(source
), PyString_GET_SIZE(source
));
1418 // Make either a Python String or Unicode object, depending on build mode
1419 PyObject
* wx2PyString(const wxString
& src
)
1423 str
= PyUnicode_FromUnicode(src
.c_str(), src
.Len());
1425 str
= PyString_FromStringAndSize(src
.c_str(), src
.Len());
1431 //----------------------------------------------------------------------
1434 byte
* byte_LIST_helper(PyObject
* source
) {
1435 if (!PyList_Check(source
)) {
1436 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1439 int count
= PyList_Size(source
);
1440 byte
* temp
= new byte
[count
];
1442 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1445 for (int x
=0; x
<count
; x
++) {
1446 PyObject
* o
= PyList_GetItem(source
, x
);
1447 if (! PyInt_Check(o
)) {
1448 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1451 temp
[x
] = (byte
)PyInt_AsLong(o
);
1457 int* int_LIST_helper(PyObject
* source
) {
1458 if (!PyList_Check(source
)) {
1459 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1462 int count
= PyList_Size(source
);
1463 int* temp
= new int[count
];
1465 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1468 for (int x
=0; x
<count
; x
++) {
1469 PyObject
* o
= PyList_GetItem(source
, x
);
1470 if (! PyInt_Check(o
)) {
1471 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1474 temp
[x
] = PyInt_AsLong(o
);
1480 long* long_LIST_helper(PyObject
* source
) {
1481 if (!PyList_Check(source
)) {
1482 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1485 int count
= PyList_Size(source
);
1486 long* temp
= new long[count
];
1488 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1491 for (int x
=0; x
<count
; x
++) {
1492 PyObject
* o
= PyList_GetItem(source
, x
);
1493 if (! PyInt_Check(o
)) {
1494 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1497 temp
[x
] = PyInt_AsLong(o
);
1503 char** string_LIST_helper(PyObject
* source
) {
1504 if (!PyList_Check(source
)) {
1505 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1508 int count
= PyList_Size(source
);
1509 char** temp
= new char*[count
];
1511 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1514 for (int x
=0; x
<count
; x
++) {
1515 PyObject
* o
= PyList_GetItem(source
, x
);
1516 if (! PyString_Check(o
)) {
1517 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1520 temp
[x
] = PyString_AsString(o
);
1525 //--------------------------------
1526 // Part of patch from Tim Hochberg
1527 static inline bool wxPointFromObjects(PyObject
* o1
, PyObject
* o2
, wxPoint
* point
) {
1528 if (PyInt_Check(o1
) && PyInt_Check(o2
)) {
1529 point
->x
= PyInt_AS_LONG(o1
);
1530 point
->y
= PyInt_AS_LONG(o2
);
1533 if (PyFloat_Check(o1
) && PyFloat_Check(o2
)) {
1534 point
->x
= (int)PyFloat_AS_DOUBLE(o1
);
1535 point
->y
= (int)PyFloat_AS_DOUBLE(o2
);
1538 if (PyInstance_Check(o1
) || PyInstance_Check(o2
)) {
1539 // Disallow instances because they can cause havok
1542 if (PyNumber_Check(o1
) && PyNumber_Check(o2
)) {
1543 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
1544 point
->x
= PyInt_AsLong(o1
);
1545 point
->y
= PyInt_AsLong(o2
);
1552 wxPoint
* wxPoint_LIST_helper(PyObject
* source
, int *count
) {
1553 // Putting all of the declarations here allows
1554 // us to put the error handling all in one place.
1557 PyObject
*o
, *o1
, *o2
;
1558 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1560 if (!PySequence_Check(source
)) {
1564 // The length of the sequence is returned in count.
1565 *count
= PySequence_Length(source
);
1570 temp
= new wxPoint
[*count
];
1572 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1575 for (x
=0; x
<*count
; x
++) {
1576 // Get an item: try fast way first.
1578 o
= PySequence_Fast_GET_ITEM(source
, x
);
1581 o
= PySequence_GetItem(source
, x
);
1587 // Convert o to wxPoint.
1588 if ((PyTuple_Check(o
) && PyTuple_GET_SIZE(o
) == 2) ||
1589 (PyList_Check(o
) && PyList_GET_SIZE(o
) == 2)) {
1590 o1
= PySequence_Fast_GET_ITEM(o
, 0);
1591 o2
= PySequence_Fast_GET_ITEM(o
, 1);
1592 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
1596 else if (PyInstance_Check(o
)) {
1598 if (SWIG_GetPtrObj(o
, (void **)&pt
, "_wxPoint_p")) {
1603 else if (PySequence_Check(o
) && PySequence_Length(o
) == 2) {
1604 o1
= PySequence_GetItem(o
, 0);
1605 o2
= PySequence_GetItem(o
, 1);
1606 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
1630 PyErr_SetString(PyExc_TypeError
, "Expected a sequence of length-2 sequences or wxPoints.");
1634 //------------------------------
1637 wxBitmap
** wxBitmap_LIST_helper(PyObject
* source
) {
1638 if (!PyList_Check(source
)) {
1639 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1642 int count
= PyList_Size(source
);
1643 wxBitmap
** temp
= new wxBitmap
*[count
];
1645 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1648 for (int x
=0; x
<count
; x
++) {
1649 PyObject
* o
= PyList_GetItem(source
, x
);
1650 if (PyInstance_Check(o
)) {
1652 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxBitmap_p")) {
1653 PyErr_SetString(PyExc_TypeError
,"Expected _wxBitmap_p.");
1659 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxBitmaps.");
1668 wxString
* wxString_LIST_helper(PyObject
* source
) {
1669 if (!PyList_Check(source
)) {
1670 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1673 int count
= PyList_Size(source
);
1674 wxString
* temp
= new wxString
[count
];
1676 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1679 for (int x
=0; x
<count
; x
++) {
1680 PyObject
* o
= PyList_GetItem(source
, x
);
1681 #if PYTHON_API_VERSION >= 1009
1682 if (! PyString_Check(o
) && ! PyUnicode_Check(o
)) {
1683 PyErr_SetString(PyExc_TypeError
, "Expected a list of string or unicode objects.");
1687 if (! PyString_Check(o
)) {
1688 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1693 wxString
* pStr
= wxString_in_helper(o
);
1701 wxAcceleratorEntry
* wxAcceleratorEntry_LIST_helper(PyObject
* source
) {
1702 if (!PyList_Check(source
)) {
1703 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1706 int count
= PyList_Size(source
);
1707 wxAcceleratorEntry
* temp
= new wxAcceleratorEntry
[count
];
1709 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1712 for (int x
=0; x
<count
; x
++) {
1713 PyObject
* o
= PyList_GetItem(source
, x
);
1714 if (PyInstance_Check(o
)) {
1715 wxAcceleratorEntry
* ae
;
1716 if (SWIG_GetPtrObj(o
, (void **) &ae
,"_wxAcceleratorEntry_p")) {
1717 PyErr_SetString(PyExc_TypeError
,"Expected _wxAcceleratorEntry_p.");
1722 else if (PyTuple_Check(o
)) {
1723 PyObject
* o1
= PyTuple_GetItem(o
, 0);
1724 PyObject
* o2
= PyTuple_GetItem(o
, 1);
1725 PyObject
* o3
= PyTuple_GetItem(o
, 2);
1726 temp
[x
].Set(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
));
1729 PyErr_SetString(PyExc_TypeError
, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
1737 wxPen
** wxPen_LIST_helper(PyObject
* source
) {
1738 if (!PyList_Check(source
)) {
1739 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1742 int count
= PyList_Size(source
);
1743 wxPen
** temp
= new wxPen
*[count
];
1745 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1748 for (int x
=0; x
<count
; x
++) {
1749 PyObject
* o
= PyList_GetItem(source
, x
);
1750 if (PyInstance_Check(o
)) {
1752 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxPen_p")) {
1754 PyErr_SetString(PyExc_TypeError
,"Expected _wxPen_p.");
1761 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxPens.");
1769 bool _2int_seq_helper(PyObject
* source
, int* i1
, int* i2
) {
1770 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1773 if (!PySequence_Check(source
) || PySequence_Length(source
) != 2)
1777 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1778 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1781 o1
= PySequence_GetItem(source
, 0);
1782 o2
= PySequence_GetItem(source
, 1);
1785 *i1
= PyInt_AsLong(o1
);
1786 *i2
= PyInt_AsLong(o2
);
1796 bool _4int_seq_helper(PyObject
* source
, int* i1
, int* i2
, int* i3
, int* i4
) {
1797 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1798 PyObject
*o1
, *o2
, *o3
, *o4
;
1800 if (!PySequence_Check(source
) || PySequence_Length(source
) != 4)
1804 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1805 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1806 o3
= PySequence_Fast_GET_ITEM(source
, 2);
1807 o4
= PySequence_Fast_GET_ITEM(source
, 3);
1810 o1
= PySequence_GetItem(source
, 0);
1811 o2
= PySequence_GetItem(source
, 1);
1812 o3
= PySequence_GetItem(source
, 2);
1813 o4
= PySequence_GetItem(source
, 3);
1816 *i1
= PyInt_AsLong(o1
);
1817 *i2
= PyInt_AsLong(o2
);
1818 *i3
= PyInt_AsLong(o3
);
1819 *i4
= PyInt_AsLong(o4
);
1831 //----------------------------------------------------------------------
1833 bool wxSize_helper(PyObject
* source
, wxSize
** obj
) {
1835 // If source is an object instance then it may already be the right type
1836 if (PyInstance_Check(source
)) {
1838 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxSize_p"))
1843 // otherwise a 2-tuple of integers is expected
1844 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1845 PyObject
* o1
= PySequence_GetItem(source
, 0);
1846 PyObject
* o2
= PySequence_GetItem(source
, 1);
1847 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1852 **obj
= wxSize(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1859 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxSize object.");
1863 bool wxPoint_helper(PyObject
* source
, wxPoint
** obj
) {
1865 // If source is an object instance then it may already be the right type
1866 if (PyInstance_Check(source
)) {
1868 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxPoint_p"))
1873 // otherwise a length-2 sequence of integers is expected
1874 if (PySequence_Check(source
) && PySequence_Length(source
) == 2) {
1875 PyObject
* o1
= PySequence_GetItem(source
, 0);
1876 PyObject
* o2
= PySequence_GetItem(source
, 1);
1877 // This should really check for integers, not numbers -- but that would break code.
1878 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1883 **obj
= wxPoint(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1889 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxPoint object.");
1895 bool wxRealPoint_helper(PyObject
* source
, wxRealPoint
** obj
) {
1897 // If source is an object instance then it may already be the right type
1898 if (PyInstance_Check(source
)) {
1900 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRealPoint_p"))
1905 // otherwise a 2-tuple of floats is expected
1906 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1907 PyObject
* o1
= PySequence_GetItem(source
, 0);
1908 PyObject
* o2
= PySequence_GetItem(source
, 1);
1909 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1914 **obj
= wxRealPoint(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
));
1921 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxRealPoint object.");
1928 bool wxRect_helper(PyObject
* source
, wxRect
** obj
) {
1930 // If source is an object instance then it may already be the right type
1931 if (PyInstance_Check(source
)) {
1933 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRect_p"))
1938 // otherwise a 4-tuple of integers is expected
1939 else if (PySequence_Check(source
) && PyObject_Length(source
) == 4) {
1940 PyObject
* o1
= PySequence_GetItem(source
, 0);
1941 PyObject
* o2
= PySequence_GetItem(source
, 1);
1942 PyObject
* o3
= PySequence_GetItem(source
, 2);
1943 PyObject
* o4
= PySequence_GetItem(source
, 3);
1944 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
) ||
1945 !PyNumber_Check(o3
) || !PyNumber_Check(o4
)) {
1952 **obj
= wxRect(PyInt_AsLong(o1
), PyInt_AsLong(o2
),
1953 PyInt_AsLong(o3
), PyInt_AsLong(o4
));
1962 PyErr_SetString(PyExc_TypeError
, "Expected a 4-tuple of integers or a wxRect object.");
1968 bool wxColour_helper(PyObject
* source
, wxColour
** obj
) {
1970 // If source is an object instance then it may already be the right type
1971 if (PyInstance_Check(source
)) {
1973 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxColour_p"))
1978 // otherwise a string is expected
1979 else if (PyString_Check(source
)) {
1980 wxString
spec(PyString_AS_STRING(source
), *wxConvCurrent
);
1981 if (spec
.GetChar(0) == '#' && spec
.Length() == 7) { // It's #RRGGBB
1982 long red
, green
, blue
;
1983 red
= green
= blue
= 0;
1984 spec
.Mid(1,2).ToLong(&red
, 16);
1985 spec
.Mid(3,2).ToLong(&green
, 16);
1986 spec
.Mid(5,2).ToLong(&blue
, 16);
1988 **obj
= wxColour(red
, green
, blue
);
1991 else { // it's a colour name
1992 **obj
= wxColour(spec
);
1998 PyErr_SetString(PyExc_TypeError
,
1999 "Expected a wxColour object or a string containing a colour "
2000 "name or '#RRGGBB'.");
2005 //----------------------------------------------------------------------
2007 PyObject
* wxArrayString2PyList_helper(const wxArrayString
& arr
) {
2009 PyObject
* list
= PyList_New(0);
2010 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
2012 PyObject
* str
= PyUnicode_FromUnicode(arr
[i
].c_str(), arr
[i
].Len());
2014 PyObject
* str
= PyString_FromStringAndSize(arr
[i
].c_str(), arr
[i
].Len());
2016 PyList_Append(list
, str
);
2023 PyObject
* wxArrayInt2PyList_helper(const wxArrayInt
& arr
) {
2025 PyObject
* list
= PyList_New(0);
2026 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
2027 PyObject
* number
= PyInt_FromLong(arr
[i
]);
2028 PyList_Append(list
, number
);
2035 //----------------------------------------------------------------------
2036 //----------------------------------------------------------------------