]>
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
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>
32 //----------------------------------------------------------------------
35 int WXDLLEXPORT
wxEntryStart( int argc
, char** argv
);
36 int WXDLLEXPORT
wxEntryInitGui();
37 void WXDLLEXPORT
wxEntryCleanup();
39 wxPyApp
* wxPythonApp
= NULL
; // Global instance of application object
42 #ifdef WXP_WITH_THREAD
43 struct wxPyThreadState
{
45 PyThreadState
* tstate
;
47 wxPyThreadState(unsigned long _tid
=0, PyThreadState
* _tstate
=NULL
)
48 : tid(_tid
), tstate(_tstate
) {}
51 #include <wx/dynarray.h>
52 WX_DECLARE_OBJARRAY(wxPyThreadState
, wxPyThreadStateArray
);
53 #include <wx/arrimpl.cpp>
54 WX_DEFINE_OBJARRAY(wxPyThreadStateArray
);
56 wxPyThreadStateArray
* wxPyTStates
= NULL
;
57 wxMutex
* wxPyTMutex
= NULL
;
61 #ifdef __WXMSW__ // If building for win32...
62 //----------------------------------------------------------------------
63 // This gets run when the DLL is loaded. We just need to save a handle.
64 //----------------------------------------------------------------------
67 HINSTANCE hinstDLL
, // handle to DLL module
68 DWORD fdwReason
, // reason for calling function
69 LPVOID lpvReserved
// reserved
72 wxSetInstance(hinstDLL
);
77 //----------------------------------------------------------------------
78 // Classes for implementing the wxp main application shell.
79 //----------------------------------------------------------------------
83 // printf("**** ctor\n");
87 // printf("**** dtor\n");
91 // This one isn't acutally called... See __wxStart()
92 bool wxPyApp::OnInit() {
97 int wxPyApp::MainLoop() {
100 DeletePendingObjects();
101 bool initialized
= wxTopLevelWindows
.GetCount() != 0;
103 m_initialized
= initialized
;
107 retval
= wxApp::MainLoop();
115 //---------------------------------------------------------------------
116 //----------------------------------------------------------------------
119 // This is where we pick up the first part of the wxEntry functionality...
120 // The rest is in __wxStart and __wxCleanup. This function is called when
121 // wxcmodule is imported. (Before there is a wxApp object.)
126 // wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
129 #ifdef WXP_WITH_THREAD
130 PyEval_InitThreads();
131 wxPyTStates
= new wxPyThreadStateArray
;
132 wxPyTMutex
= new wxMutex
;
135 // Bail out if there is already windows created. This means that the
136 // toolkit has already been initialized, as in embedding wxPython in
137 // a C++ wxWindows app.
138 if (wxTopLevelWindows
.Number() > 0)
144 PyObject
* sysargv
= PySys_GetObject("argv");
145 if (sysargv
!= NULL
) {
146 argc
= PyList_Size(sysargv
);
147 argv
= new char*[argc
+1];
149 for(x
=0; x
<argc
; x
++)
150 argv
[x
] = copystring(PyString_AsString(PyList_GetItem(sysargv
, x
)));
154 wxEntryStart(argc
, argv
);
160 // Start the user application, user App's OnInit method is a parameter here
161 PyObject
* __wxStart(PyObject
* /* self */, PyObject
* args
)
163 PyObject
* onInitFunc
= NULL
;
168 if (!PyArg_ParseTuple(args
, "O", &onInitFunc
))
171 #if 0 // Try it out without this check, see how it does...
172 if (wxTopLevelWindows
.Number() > 0) {
173 PyErr_SetString(PyExc_TypeError
, "Only 1 wxApp per process!");
178 // This is the next part of the wxEntry functionality...
181 PyObject
* sysargv
= PySys_GetObject("argv");
182 if (sysargv
!= NULL
) {
183 argc
= PyList_Size(sysargv
);
184 argv
= new char*[argc
+1];
186 for(x
=0; x
<argc
; x
++)
187 argv
[x
] = copystring(PyString_AsString(PyList_GetItem(sysargv
, x
)));
190 wxPythonApp
->argc
= argc
;
191 wxPythonApp
->argv
= argv
;
195 // Call the Python App's OnInit function
196 arglist
= PyTuple_New(0);
197 result
= PyEval_CallObject(onInitFunc
, arglist
);
198 if (!result
) { // an exception was raised.
202 if (! PyInt_Check(result
)) {
203 PyErr_SetString(PyExc_TypeError
, "OnInit should return a boolean value");
206 bResult
= PyInt_AS_LONG(result
);
208 PyErr_SetString(PyExc_SystemExit
, "OnInit returned FALSE, exiting...");
213 wxTheApp
->m_initialized
= (wxTopLevelWindows
.GetCount() > 0);
225 wxPyTStates
->Empty();
232 static PyObject
* wxPython_dict
= NULL
;
233 static PyObject
* wxPyPtrTypeMap
= NULL
;
235 PyObject
* __wxSetDictionary(PyObject
* /* self */, PyObject
* args
)
238 if (!PyArg_ParseTuple(args
, "O", &wxPython_dict
))
241 if (!PyDict_Check(wxPython_dict
)) {
242 PyErr_SetString(PyExc_TypeError
, "_wxSetDictionary must have dictionary object!");
246 if (! wxPyPtrTypeMap
)
247 wxPyPtrTypeMap
= PyDict_New();
248 PyDict_SetItemString(wxPython_dict
, "__wxPyPtrTypeMap", wxPyPtrTypeMap
);
252 #define wxPlatform "__WXMOTIF__"
255 #define wxPlatform "__WXQT__"
258 #define wxPlatform "__WXGTK__"
260 #if defined(__WIN32__) || defined(__WXMSW__)
261 #define wxPlatform "__WXMSW__"
264 #define wxPlatform "__WXMAC__"
267 PyDict_SetItemString(wxPython_dict
, "wxPlatform", PyString_FromString(wxPlatform
));
274 //---------------------------------------------------------------------------
275 // Stuff used by OOR to find the right wxPython class type to return and to
279 // The pointer type map is used when the "pointer" type name generated by SWIG
280 // is not the same as the shadow class name, for example wxPyTreeCtrl
281 // vs. wxTreeCtrl. It needs to be referenced in Python as well as from C++,
282 // so we'll just make it a Python dictionary in the wx module's namespace.
283 void wxPyPtrTypeMap_Add(const char* commonName
, const char* ptrName
) {
284 if (! wxPyPtrTypeMap
)
285 wxPyPtrTypeMap
= PyDict_New();
286 PyDict_SetItemString(wxPyPtrTypeMap
,
288 PyString_FromString((char*)ptrName
));
293 PyObject
* wxPyClassExists(const char* className
) {
298 char buff
[64]; // should always be big enough...
300 sprintf(buff
, "%sPtr", className
);
301 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
303 return classobj
; // returns NULL if not found
307 PyObject
* wxPyMake_wxObject(wxObject
* source
, bool checkEvtHandler
) {
308 PyObject
* target
= NULL
;
309 bool isEvtHandler
= FALSE
;
312 // If it's derived from wxEvtHandler then there may
313 // already be a pointer to a Python object that we can use
315 if (checkEvtHandler
&& wxIsKindOf(source
, wxEvtHandler
)) {
317 wxEvtHandler
* eh
= (wxEvtHandler
*)source
;
318 wxPyClientData
* data
= (wxPyClientData
*)eh
->GetClientObject();
320 target
= data
->m_obj
;
326 // Otherwise make it the old fashioned way by making a
327 // new shadow object and putting this pointer in it.
328 wxClassInfo
* info
= source
->GetClassInfo();
329 wxChar
* name
= (wxChar
*)info
->GetClassName();
330 PyObject
* klass
= wxPyClassExists(name
);
331 while (info
&& !klass
) {
332 name
= (wxChar
*)info
->GetBaseClassName1();
333 info
= wxClassInfo::FindClass(name
);
334 klass
= wxPyClassExists(name
);
337 target
= wxPyConstructObject(source
, name
, klass
, FALSE
);
338 if (target
&& isEvtHandler
)
339 ((wxEvtHandler
*)source
)->SetClientObject(new wxPyClientData(target
));
341 wxString
msg("wxPython class not found for ");
342 msg
+= source
->GetClassInfo()->GetClassName();
343 PyErr_SetString(PyExc_NameError
, msg
.c_str());
347 } else { // source was NULL so return None.
348 Py_INCREF(Py_None
); target
= Py_None
;
354 PyObject
* wxPyMake_wxSizer(wxSizer
* source
) {
355 PyObject
* target
= NULL
;
357 if (source
&& wxIsKindOf(source
, wxSizer
)) {
358 // If it's derived from wxSizer then there may
359 // already be a pointer to a Python object that we can use
361 wxSizer
* sz
= (wxSizer
*)source
;
362 wxPyClientData
* data
= (wxPyClientData
*)sz
->GetClientObject();
364 target
= data
->m_obj
;
369 target
= wxPyMake_wxObject(source
, FALSE
);
370 if (target
!= Py_None
)
371 ((wxSizer
*)source
)->SetClientObject(new wxPyClientData(target
));
378 //---------------------------------------------------------------------------
380 PyObject
* wxPyConstructObject(void* ptr
,
381 const char* className
,
388 char swigptr
[64]; // should always be big enough...
391 if ((item
= PyDict_GetItemString(wxPyPtrTypeMap
, (char*)className
)) != NULL
) {
392 className
= PyString_AsString(item
);
394 sprintf(buff
, "_%s_p", className
);
395 SWIG_MakePtr(swigptr
, ptr
, buff
);
397 arg
= Py_BuildValue("(s)", swigptr
);
398 obj
= PyInstance_New(klass
, arg
, NULL
);
402 PyObject
* one
= PyInt_FromLong(1);
403 PyObject_SetAttrString(obj
, "thisown", one
);
411 PyObject
* wxPyConstructObject(void* ptr
,
412 const char* className
,
421 char buff
[64]; // should always be big enough...
422 sprintf(buff
, "%sPtr", className
);
424 wxASSERT_MSG(wxPython_dict
, "wxPython_dict is not set yet!!");
426 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
430 "*** Unknown class name %s, tell Robin about it please ***",
432 obj
= PyString_FromString(temp
);
436 return wxPyConstructObject(ptr
, className
, classobj
, setThisOwn
);
439 //---------------------------------------------------------------------------
441 // TODO: This should really be wxThread::GetCurrentId(), and I will do so
442 // after I make a quick 2.3.2.1 release.
443 #ifdef WXP_WITH_THREAD
444 #ifdef __WXGTK__ // does wxGTK always use pthreads?
449 unsigned long wxPyGetCurrentThreadId() {
451 return (unsigned long)::GetCurrentThreadId();
453 #ifdef __WXGTK__ // does wxGTK always use pthreads?
454 return (unsigned long)pthread_self();
458 MacGetCurrentThread( ¤t
) ;
459 return (unsigned long)current
;
466 PyThreadState
* wxPyGetThreadState() {
467 unsigned long ctid
= wxPyGetCurrentThreadId();
468 PyThreadState
* tstate
= NULL
;
471 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
472 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
473 if (info
.tid
== ctid
) {
474 tstate
= info
.tstate
;
478 wxPyTMutex
->Unlock();
479 wxASSERT_MSG(tstate
, "PyThreadState should not be NULL!");
484 void wxPySaveThreadState(PyThreadState
* tstate
) {
485 unsigned long ctid
= wxPyGetCurrentThreadId();
487 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
488 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
489 if (info
.tid
== ctid
) {
490 info
.tstate
= tstate
;
491 wxPyTMutex
->Unlock();
495 // not found, so add it...
496 wxPyTStates
->Add(new wxPyThreadState(ctid
, tstate
));
497 wxPyTMutex
->Unlock();
503 // Calls from Python to wxWindows code are wrapped in calls to these
506 PyThreadState
* wxPyBeginAllowThreads() {
507 #ifdef WXP_WITH_THREAD
508 PyThreadState
* saved
= PyEval_SaveThread(); // Py_BEGIN_ALLOW_THREADS;
509 wxPySaveThreadState(saved
);
516 void wxPyEndAllowThreads(PyThreadState
* saved
) {
517 #ifdef WXP_WITH_THREAD
518 PyEval_RestoreThread(saved
); // Py_END_ALLOW_THREADS;
524 // Calls from wxWindows back to Python code, or even any PyObject
525 // manipulations, PyDECREF's and etc. are wrapped in calls to these functions:
527 void wxPyBeginBlockThreads() {
528 #ifdef WXP_WITH_THREAD
529 PyThreadState
* tstate
= wxPyGetThreadState();
530 PyEval_RestoreThread(tstate
);
535 void wxPyEndBlockThreads() {
536 #ifdef WXP_WITH_THREAD
537 PyThreadState
* tstate
= PyEval_SaveThread();
538 // Is there any need to save it again?
543 //---------------------------------------------------------------------------
545 IMPLEMENT_ABSTRACT_CLASS(wxPyCallback
, wxObject
);
547 wxPyCallback::wxPyCallback(PyObject
* func
) {
552 wxPyCallback::wxPyCallback(const wxPyCallback
& other
) {
553 m_func
= other
.m_func
;
557 wxPyCallback::~wxPyCallback() {
558 wxPyBeginBlockThreads();
560 wxPyEndBlockThreads();
565 // This function is used for all events destined for Python event handlers.
566 void wxPyCallback::EventThunker(wxEvent
& event
) {
567 wxPyCallback
* cb
= (wxPyCallback
*)event
.m_callbackUserData
;
568 PyObject
* func
= cb
->m_func
;
574 wxPyBeginBlockThreads();
575 wxString className
= event
.GetClassInfo()->GetClassName();
577 if (className
== "wxPyEvent")
578 arg
= ((wxPyEvent
*)&event
)->GetSelf();
579 else if (className
== "wxPyCommandEvent")
580 arg
= ((wxPyCommandEvent
*)&event
)->GetSelf();
582 arg
= wxPyConstructObject((void*)&event
, className
);
584 tuple
= PyTuple_New(1);
585 PyTuple_SET_ITEM(tuple
, 0, arg
);
586 result
= PyEval_CallObject(func
, tuple
);
590 PyErr_Clear(); // Just in case...
594 wxPyEndBlockThreads();
598 //----------------------------------------------------------------------
600 wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper
& other
) {
602 m_self
= other
.m_self
;
603 m_class
= other
.m_class
;
611 void wxPyCallbackHelper::setSelf(PyObject
* self
, PyObject
* klass
, int incref
) {
622 #if PYTHON_API_VERSION >= 1011
624 // Prior to Python 2.2 PyMethod_GetClass returned the class object
625 // in which the method was defined. Starting with 2.2 it returns
626 // "class that asked for the method" which seems totally bogus to me
627 // but apprently if fixes some obscure problem waiting to happen in
628 // Python. Since the API was not documented Guido and the gang felt
629 // safe in changing it. Needless to say that totally screwed up the
630 // logic below in wxPyCallbackHelper::findCallback, hence this icky
631 // code to find the class where the method is actuallt defined...
634 PyObject
* PyFindClassWithAttr(PyObject
*klass
, PyObject
*name
)
638 if (PyType_Check(klass
)) { // new style classes
639 // This code is borrowed/adapted from _PyType_Lookup in typeobject.c
640 // (TODO: This part is not tested yet, so I'm not sure it is correct...)
641 PyTypeObject
* type
= (PyTypeObject
*)klass
;
642 PyObject
*mro
, *res
, *base
, *dict
;
643 /* Look in tp_dict of types in MRO */
645 assert(PyTuple_Check(mro
));
646 n
= PyTuple_GET_SIZE(mro
);
647 for (i
= 0; i
< n
; i
++) {
648 base
= PyTuple_GET_ITEM(mro
, i
);
649 if (PyClass_Check(base
))
650 dict
= ((PyClassObject
*)base
)->cl_dict
;
652 assert(PyType_Check(base
));
653 dict
= ((PyTypeObject
*)base
)->tp_dict
;
655 assert(dict
&& PyDict_Check(dict
));
656 res
= PyDict_GetItem(dict
, name
);
663 else if (PyClass_Check(klass
)) { // old style classes
664 // This code is borrowed/adapted from class_lookup in classobject.c
665 PyClassObject
* cp
= (PyClassObject
*)klass
;
666 PyObject
*value
= PyDict_GetItem(cp
->cl_dict
, name
);
668 return (PyObject
*)cp
;
670 n
= PyTuple_Size(cp
->cl_bases
);
671 for (i
= 0; i
< n
; i
++) {
672 PyObject
* base
= PyTuple_GetItem(cp
->cl_bases
, i
);
673 PyObject
*v
= PyFindClassWithAttr(base
, name
);
684 PyObject
* PyMethod_GetDefiningClass(PyObject
* method
, const char* name
)
686 PyObject
* mgc
= PyMethod_GET_CLASS(method
);
688 #if PYTHON_API_VERSION <= 1010 // prior to Python 2.2, the easy way
690 #else // 2.2 and after, the hard way...
692 PyObject
* nameo
= PyString_FromString(name
);
693 PyObject
* klass
= PyFindClassWithAttr(mgc
, nameo
);
701 bool wxPyCallbackHelper::findCallback(const char* name
) const {
702 wxPyCallbackHelper
* self
= (wxPyCallbackHelper
*)this; // cast away const
703 self
->m_lastFound
= NULL
;
705 // If the object (m_self) has an attibute of the given name...
706 if (m_self
&& PyObject_HasAttrString(m_self
, (char*)name
)) {
707 PyObject
*method
, *klass
;
708 method
= PyObject_GetAttrString(m_self
, (char*)name
);
710 // ...and if that attribute is a method, and if that method's class is
711 // not from a base class...
712 if (PyMethod_Check(method
) &&
713 (klass
= PyMethod_GetDefiningClass(method
, (char*)name
)) != NULL
&&
714 ((klass
== m_class
) || PyClass_IsSubclass(klass
, m_class
))) {
716 // ...then we'll save a pointer to the method so callCallback can call it.
717 self
->m_lastFound
= method
;
723 return m_lastFound
!= NULL
;
727 int wxPyCallbackHelper::callCallback(PyObject
* argTuple
) const {
731 result
= callCallbackObj(argTuple
);
732 if (result
) { // Assumes an integer return type...
733 retval
= PyInt_AsLong(result
);
735 PyErr_Clear(); // forget about it if it's not...
740 // Invoke the Python callable object, returning the raw PyObject return
741 // value. Caller should DECREF the return value and also call PyEval_SaveThread.
742 PyObject
* wxPyCallbackHelper::callCallbackObj(PyObject
* argTuple
) const {
745 // Save a copy of the pointer in case the callback generates another
746 // callback. In that case m_lastFound will have a different value when
747 // it gets back here...
748 PyObject
* method
= m_lastFound
;
750 result
= PyEval_CallObject(method
, argTuple
);
760 void wxPyCBH_setCallbackInfo(wxPyCallbackHelper
& cbh
, PyObject
* self
, PyObject
* klass
, int incref
) {
761 cbh
.setSelf(self
, klass
, incref
);
764 bool wxPyCBH_findCallback(const wxPyCallbackHelper
& cbh
, const char* name
) {
765 return cbh
.findCallback(name
);
768 int wxPyCBH_callCallback(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
769 return cbh
.callCallback(argTuple
);
772 PyObject
* wxPyCBH_callCallbackObj(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
773 return cbh
.callCallbackObj(argTuple
);
777 void wxPyCBH_delete(wxPyCallbackHelper
* cbh
) {
779 wxPyBeginBlockThreads();
780 Py_XDECREF(cbh
->m_self
);
781 Py_XDECREF(cbh
->m_class
);
782 wxPyEndBlockThreads();
786 //---------------------------------------------------------------------------
787 //---------------------------------------------------------------------------
788 // These event classes can be derived from in Python and passed through the event
789 // system without losing anything. They do this by keeping a reference to
790 // themselves and some special case handling in wxPyCallback::EventThunker.
793 wxPyEvtSelfRef::wxPyEvtSelfRef() {
794 //m_self = Py_None; // **** We don't do normal ref counting to prevent
795 //Py_INCREF(m_self); // circular loops...
799 wxPyEvtSelfRef::~wxPyEvtSelfRef() {
800 wxPyBeginBlockThreads();
803 wxPyEndBlockThreads();
806 void wxPyEvtSelfRef::SetSelf(PyObject
* self
, bool clone
) {
807 wxPyBeginBlockThreads();
815 wxPyEndBlockThreads();
818 PyObject
* wxPyEvtSelfRef::GetSelf() const {
824 IMPLEMENT_ABSTRACT_CLASS(wxPyEvent
, wxEvent
);
825 IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent
, wxCommandEvent
);
828 wxPyEvent::wxPyEvent(int id
)
833 wxPyEvent::wxPyEvent(const wxPyEvent
& evt
)
836 SetSelf(evt
.m_self
, TRUE
);
840 wxPyEvent::~wxPyEvent() {
844 wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType
, int id
)
845 : wxCommandEvent(commandType
, id
) {
849 wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent
& evt
)
850 : wxCommandEvent(evt
)
852 SetSelf(evt
.m_self
, TRUE
);
856 wxPyCommandEvent::~wxPyCommandEvent() {
862 //---------------------------------------------------------------------------
863 //---------------------------------------------------------------------------
866 wxPyTimer::wxPyTimer(PyObject
* callback
) {
871 wxPyTimer::~wxPyTimer() {
872 wxPyBeginBlockThreads();
874 wxPyEndBlockThreads();
877 void wxPyTimer::Notify() {
878 if (!func
|| func
== Py_None
) {
882 wxPyBeginBlockThreads();
885 PyObject
* args
= Py_BuildValue("()");
887 result
= PyEval_CallObject(func
, args
);
896 wxPyEndBlockThreads();
902 //---------------------------------------------------------------------------
903 //---------------------------------------------------------------------------
904 // Convert a wxList to a Python List
906 PyObject
* wxPy_ConvertList(wxListBase
* list
, const char* className
) {
910 wxNode
* node
= list
->First();
912 wxPyBeginBlockThreads();
913 pyList
= PyList_New(0);
915 wxObj
= node
->Data();
916 pyObj
= wxPyMake_wxObject(wxObj
); //wxPyConstructObject(wxObj, className);
917 PyList_Append(pyList
, pyObj
);
920 wxPyEndBlockThreads();
924 //----------------------------------------------------------------------
926 long wxPyGetWinHandle(wxWindow
* win
) {
928 return (long)win
->GetHandle();
931 // Find and return the actual X-Window.
933 if (win
->m_wxwindow
) {
934 GdkWindowPrivate
* bwin
= (GdkWindowPrivate
*)GTK_PIZZA(win
->m_wxwindow
)->bin_window
;
936 return (long)bwin
->xwindow
;
943 //----------------------------------------------------------------------
944 // Some helper functions for typemaps in my_typemaps.i, so they won't be
945 // included in every file...
948 byte
* byte_LIST_helper(PyObject
* source
) {
949 if (!PyList_Check(source
)) {
950 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
953 int count
= PyList_Size(source
);
954 byte
* temp
= new byte
[count
];
956 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
959 for (int x
=0; x
<count
; x
++) {
960 PyObject
* o
= PyList_GetItem(source
, x
);
961 if (! PyInt_Check(o
)) {
962 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
965 temp
[x
] = (byte
)PyInt_AsLong(o
);
971 int* int_LIST_helper(PyObject
* source
) {
972 if (!PyList_Check(source
)) {
973 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
976 int count
= PyList_Size(source
);
977 int* temp
= new int[count
];
979 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
982 for (int x
=0; x
<count
; x
++) {
983 PyObject
* o
= PyList_GetItem(source
, x
);
984 if (! PyInt_Check(o
)) {
985 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
988 temp
[x
] = PyInt_AsLong(o
);
994 long* long_LIST_helper(PyObject
* source
) {
995 if (!PyList_Check(source
)) {
996 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
999 int count
= PyList_Size(source
);
1000 long* temp
= new long[count
];
1002 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1005 for (int x
=0; x
<count
; x
++) {
1006 PyObject
* o
= PyList_GetItem(source
, x
);
1007 if (! PyInt_Check(o
)) {
1008 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1011 temp
[x
] = PyInt_AsLong(o
);
1017 char** string_LIST_helper(PyObject
* source
) {
1018 if (!PyList_Check(source
)) {
1019 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1022 int count
= PyList_Size(source
);
1023 char** temp
= new char*[count
];
1025 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1028 for (int x
=0; x
<count
; x
++) {
1029 PyObject
* o
= PyList_GetItem(source
, x
);
1030 if (! PyString_Check(o
)) {
1031 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1034 temp
[x
] = PyString_AsString(o
);
1039 //--------------------------------
1040 // Part of patch from Tim Hochberg
1041 static inline bool wxPointFromObjects(PyObject
* o1
, PyObject
* o2
, wxPoint
* point
) {
1042 if (PyInt_Check(o1
) && PyInt_Check(o2
)) {
1043 point
->x
= PyInt_AS_LONG(o1
);
1044 point
->y
= PyInt_AS_LONG(o2
);
1047 if (PyFloat_Check(o1
) && PyFloat_Check(o2
)) {
1048 point
->x
= (int)PyFloat_AS_DOUBLE(o1
);
1049 point
->y
= (int)PyFloat_AS_DOUBLE(o2
);
1052 if (PyInstance_Check(o1
) || PyInstance_Check(o2
)) {
1053 // Disallow instances because they can cause havok
1056 if (PyNumber_Check(o1
) && PyNumber_Check(o2
)) {
1057 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
1058 point
->x
= PyInt_AsLong(o1
);
1059 point
->y
= PyInt_AsLong(o2
);
1066 wxPoint
* wxPoint_LIST_helper(PyObject
* source
, int *count
) {
1067 // Putting all of the declarations here allows
1068 // us to put the error handling all in one place.
1071 PyObject
*o
, *o1
, *o2
;
1072 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1074 if (!PySequence_Check(source
)) {
1078 // The length of the sequence is returned in count.
1079 *count
= PySequence_Length(source
);
1084 temp
= new wxPoint
[*count
];
1086 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1089 for (x
=0; x
<*count
; x
++) {
1090 // Get an item: try fast way first.
1092 o
= PySequence_Fast_GET_ITEM(source
, x
);
1095 o
= PySequence_GetItem(source
, x
);
1101 // Convert o to wxPoint.
1102 if ((PyTuple_Check(o
) && PyTuple_GET_SIZE(o
) == 2) ||
1103 (PyList_Check(o
) && PyList_GET_SIZE(o
) == 2)) {
1104 o1
= PySequence_Fast_GET_ITEM(o
, 0);
1105 o2
= PySequence_Fast_GET_ITEM(o
, 1);
1106 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
1110 else if (PyInstance_Check(o
)) {
1112 if (SWIG_GetPtrObj(o
, (void **)&pt
, "_wxPoint_p")) {
1117 else if (PySequence_Check(o
) && PySequence_Length(o
) == 2) {
1118 o1
= PySequence_GetItem(o
, 0);
1119 o2
= PySequence_GetItem(o
, 1);
1120 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
1144 PyErr_SetString(PyExc_TypeError
, "Expected a sequence of length-2 sequences or wxPoints.");
1148 //------------------------------
1151 wxBitmap
** wxBitmap_LIST_helper(PyObject
* source
) {
1152 if (!PyList_Check(source
)) {
1153 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1156 int count
= PyList_Size(source
);
1157 wxBitmap
** temp
= new wxBitmap
*[count
];
1159 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1162 for (int x
=0; x
<count
; x
++) {
1163 PyObject
* o
= PyList_GetItem(source
, x
);
1164 if (PyInstance_Check(o
)) {
1166 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxBitmap_p")) {
1167 PyErr_SetString(PyExc_TypeError
,"Expected _wxBitmap_p.");
1173 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxBitmaps.");
1182 wxString
* wxString_LIST_helper(PyObject
* source
) {
1183 if (!PyList_Check(source
)) {
1184 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1187 int count
= PyList_Size(source
);
1188 wxString
* temp
= new wxString
[count
];
1190 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1193 for (int x
=0; x
<count
; x
++) {
1194 PyObject
* o
= PyList_GetItem(source
, x
);
1195 #if PYTHON_API_VERSION >= 1009
1196 if (! PyString_Check(o
) && ! PyUnicode_Check(o
)) {
1197 PyErr_SetString(PyExc_TypeError
, "Expected a list of string or unicode objects.");
1203 if (PyString_AsStringAndSize(o
, &buff
, &length
) == -1)
1205 temp
[x
] = wxString(buff
, length
);
1207 if (! PyString_Check(o
)) {
1208 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1211 temp
[x
] = PyString_AsString(o
);
1218 wxAcceleratorEntry
* wxAcceleratorEntry_LIST_helper(PyObject
* source
) {
1219 if (!PyList_Check(source
)) {
1220 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1223 int count
= PyList_Size(source
);
1224 wxAcceleratorEntry
* temp
= new wxAcceleratorEntry
[count
];
1226 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1229 for (int x
=0; x
<count
; x
++) {
1230 PyObject
* o
= PyList_GetItem(source
, x
);
1231 if (PyInstance_Check(o
)) {
1232 wxAcceleratorEntry
* ae
;
1233 if (SWIG_GetPtrObj(o
, (void **) &ae
,"_wxAcceleratorEntry_p")) {
1234 PyErr_SetString(PyExc_TypeError
,"Expected _wxAcceleratorEntry_p.");
1239 else if (PyTuple_Check(o
)) {
1240 PyObject
* o1
= PyTuple_GetItem(o
, 0);
1241 PyObject
* o2
= PyTuple_GetItem(o
, 1);
1242 PyObject
* o3
= PyTuple_GetItem(o
, 2);
1243 temp
[x
].Set(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
));
1246 PyErr_SetString(PyExc_TypeError
, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
1254 wxPen
** wxPen_LIST_helper(PyObject
* source
) {
1255 if (!PyList_Check(source
)) {
1256 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1259 int count
= PyList_Size(source
);
1260 wxPen
** temp
= new wxPen
*[count
];
1262 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1265 for (int x
=0; x
<count
; x
++) {
1266 PyObject
* o
= PyList_GetItem(source
, x
);
1267 if (PyInstance_Check(o
)) {
1269 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxPen_p")) {
1271 PyErr_SetString(PyExc_TypeError
,"Expected _wxPen_p.");
1278 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxPens.");
1286 bool _2int_seq_helper(PyObject
* source
, int* i1
, int* i2
) {
1287 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1290 if (!PySequence_Check(source
) || PySequence_Length(source
) != 2)
1294 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1295 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1298 o1
= PySequence_GetItem(source
, 0);
1299 o2
= PySequence_GetItem(source
, 1);
1302 *i1
= PyInt_AsLong(o1
);
1303 *i2
= PyInt_AsLong(o2
);
1313 bool _4int_seq_helper(PyObject
* source
, int* i1
, int* i2
, int* i3
, int* i4
) {
1314 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1315 PyObject
*o1
, *o2
, *o3
, *o4
;
1317 if (!PySequence_Check(source
) || PySequence_Length(source
) != 4)
1321 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1322 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1323 o3
= PySequence_Fast_GET_ITEM(source
, 2);
1324 o4
= PySequence_Fast_GET_ITEM(source
, 3);
1327 o1
= PySequence_GetItem(source
, 0);
1328 o2
= PySequence_GetItem(source
, 1);
1329 o3
= PySequence_GetItem(source
, 2);
1330 o4
= PySequence_GetItem(source
, 3);
1333 *i1
= PyInt_AsLong(o1
);
1334 *i2
= PyInt_AsLong(o2
);
1335 *i3
= PyInt_AsLong(o3
);
1336 *i4
= PyInt_AsLong(o4
);
1348 //----------------------------------------------------------------------
1350 bool wxSize_helper(PyObject
* source
, wxSize
** obj
) {
1352 // If source is an object instance then it may already be the right type
1353 if (PyInstance_Check(source
)) {
1355 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxSize_p"))
1360 // otherwise a 2-tuple of integers is expected
1361 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1362 PyObject
* o1
= PySequence_GetItem(source
, 0);
1363 PyObject
* o2
= PySequence_GetItem(source
, 1);
1364 **obj
= wxSize(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1369 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxSize object.");
1373 bool wxPoint_helper(PyObject
* source
, wxPoint
** obj
) {
1375 // If source is an object instance then it may already be the right type
1376 if (PyInstance_Check(source
)) {
1378 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxPoint_p"))
1383 // otherwise a length-2 sequence of integers is expected
1384 if (PySequence_Check(source
) && PySequence_Length(source
) == 2) {
1385 PyObject
* o1
= PySequence_GetItem(source
, 0);
1386 PyObject
* o2
= PySequence_GetItem(source
, 1);
1387 // This should really check for integers, not numbers -- but that would break code.
1388 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1393 **obj
= wxPoint(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1399 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxPoint object.");
1405 bool wxRealPoint_helper(PyObject
* source
, wxRealPoint
** obj
) {
1407 // If source is an object instance then it may already be the right type
1408 if (PyInstance_Check(source
)) {
1410 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRealPoint_p"))
1415 // otherwise a 2-tuple of floats is expected
1416 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1417 PyObject
* o1
= PySequence_GetItem(source
, 0);
1418 PyObject
* o2
= PySequence_GetItem(source
, 1);
1419 **obj
= wxRealPoint(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
));
1424 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxRealPoint object.");
1431 bool wxRect_helper(PyObject
* source
, wxRect
** obj
) {
1433 // If source is an object instance then it may already be the right type
1434 if (PyInstance_Check(source
)) {
1436 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRect_p"))
1441 // otherwise a 4-tuple of integers is expected
1442 else if (PySequence_Check(source
) && PyObject_Length(source
) == 4) {
1443 PyObject
* o1
= PySequence_GetItem(source
, 0);
1444 PyObject
* o2
= PySequence_GetItem(source
, 1);
1445 PyObject
* o3
= PySequence_GetItem(source
, 2);
1446 PyObject
* o4
= PySequence_GetItem(source
, 3);
1447 **obj
= wxRect(PyInt_AsLong(o1
), PyInt_AsLong(o2
),
1448 PyInt_AsLong(o3
), PyInt_AsLong(o4
));
1453 PyErr_SetString(PyExc_TypeError
, "Expected a 4-tuple of integers or a wxRect object.");
1459 bool wxColour_helper(PyObject
* source
, wxColour
** obj
) {
1461 // If source is an object instance then it may already be the right type
1462 if (PyInstance_Check(source
)) {
1464 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxColour_p"))
1469 // otherwise a string is expected
1470 else if (PyString_Check(source
)) {
1471 wxString spec
= PyString_AS_STRING(source
);
1472 if (spec
[0U] == '#' && spec
.Length() == 7) { // It's #RRGGBB
1474 int red
= strtol(spec
.Mid(1,2), &junk
, 16);
1475 int green
= strtol(spec
.Mid(3,2), &junk
, 16);
1476 int blue
= strtol(spec
.Mid(5,2), &junk
, 16);
1477 **obj
= wxColour(red
, green
, blue
);
1480 else { // it's a colour name
1481 **obj
= wxColour(spec
);
1487 PyErr_SetString(PyExc_TypeError
, "Expected a wxColour object or a string containing a colour name or '#RRGGBB'.");
1492 //----------------------------------------------------------------------
1494 PyObject
* wxArrayString2PyList_helper(const wxArrayString
& arr
) {
1496 PyObject
* list
= PyList_New(0);
1497 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
1498 PyObject
* str
= PyString_FromString(arr
[i
].c_str());
1499 PyList_Append(list
, str
);
1506 //----------------------------------------------------------------------
1507 //----------------------------------------------------------------------