]>
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
);
37 int WXDLLEXPORT
wxEntryStart( int argc
, char** argv
);
39 int WXDLLEXPORT
wxEntryInitGui();
40 void WXDLLEXPORT
wxEntryCleanup();
42 wxPyApp
* wxPythonApp
= NULL
; // Global instance of application object
45 #ifdef WXP_WITH_THREAD
46 struct wxPyThreadState
{
48 PyThreadState
* tstate
;
50 wxPyThreadState(unsigned long _tid
=0, PyThreadState
* _tstate
=NULL
)
51 : tid(_tid
), tstate(_tstate
) {}
54 #include <wx/dynarray.h>
55 WX_DECLARE_OBJARRAY(wxPyThreadState
, wxPyThreadStateArray
);
56 #include <wx/arrimpl.cpp>
57 WX_DEFINE_OBJARRAY(wxPyThreadStateArray
);
59 wxPyThreadStateArray
* wxPyTStates
= NULL
;
60 wxMutex
* wxPyTMutex
= NULL
;
64 #ifdef __WXMSW__ // If building for win32...
65 //----------------------------------------------------------------------
66 // This gets run when the DLL is loaded. We just need to save a handle.
67 //----------------------------------------------------------------------
70 HINSTANCE hinstDLL
, // handle to DLL module
71 DWORD fdwReason
, // reason for calling function
72 LPVOID lpvReserved
// reserved
75 wxSetInstance(hinstDLL
);
80 //----------------------------------------------------------------------
81 // Classes for implementing the wxp main application shell.
82 //----------------------------------------------------------------------
86 // printf("**** ctor\n");
90 // printf("**** dtor\n");
94 // This one isn't acutally called... See __wxStart()
95 bool wxPyApp::OnInit() {
100 int wxPyApp::MainLoop() {
103 DeletePendingObjects();
104 bool initialized
= wxTopLevelWindows
.GetCount() != 0;
106 m_initialized
= initialized
;
110 retval
= wxApp::MainLoop();
118 //---------------------------------------------------------------------
119 //----------------------------------------------------------------------
122 // This is where we pick up the first part of the wxEntry functionality...
123 // The rest is in __wxStart and __wxCleanup. This function is called when
124 // wxcmodule is imported. (Before there is a wxApp object.)
129 // wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
132 #ifdef WXP_WITH_THREAD
133 PyEval_InitThreads();
134 wxPyTStates
= new wxPyThreadStateArray
;
135 wxPyTMutex
= new wxMutex
;
138 // Bail out if there is already windows created. This means that the
139 // toolkit has already been initialized, as in embedding wxPython in
140 // a C++ wxWindows app.
141 if (wxTopLevelWindows
.Number() > 0)
147 PyObject
* sysargv
= PySys_GetObject("argv");
148 if (sysargv
!= NULL
) {
149 argc
= PyList_Size(sysargv
);
150 argv
= new char*[argc
+1];
152 for(x
=0; x
<argc
; x
++)
153 argv
[x
] = copystring(PyString_AsString(PyList_GetItem(sysargv
, x
)));
157 wxEntryStart(argc
, argv
);
163 // Start the user application, user App's OnInit method is a parameter here
164 PyObject
* __wxStart(PyObject
* /* self */, PyObject
* args
)
166 PyObject
* onInitFunc
= NULL
;
171 if (!PyArg_ParseTuple(args
, "O", &onInitFunc
))
174 #if 0 // Try it out without this check, see how it does...
175 if (wxTopLevelWindows
.Number() > 0) {
176 PyErr_SetString(PyExc_TypeError
, "Only 1 wxApp per process!");
181 // This is the next part of the wxEntry functionality...
184 PyObject
* sysargv
= PySys_GetObject("argv");
185 if (sysargv
!= NULL
) {
186 argc
= PyList_Size(sysargv
);
187 argv
= new char*[argc
+1];
189 for(x
=0; x
<argc
; x
++)
190 argv
[x
] = copystring(PyString_AsString(PyList_GetItem(sysargv
, x
)));
193 wxPythonApp
->argc
= argc
;
194 wxPythonApp
->argv
= argv
;
198 // Call the Python App's OnInit function
199 arglist
= PyTuple_New(0);
200 result
= PyEval_CallObject(onInitFunc
, arglist
);
201 if (!result
) { // an exception was raised.
205 if (! PyInt_Check(result
)) {
206 PyErr_SetString(PyExc_TypeError
, "OnInit should return a boolean value");
209 bResult
= PyInt_AS_LONG(result
);
211 PyErr_SetString(PyExc_SystemExit
, "OnInit returned FALSE, exiting...");
216 wxTheApp
->m_initialized
= (wxTopLevelWindows
.GetCount() > 0);
228 wxPyTStates
->Empty();
235 static PyObject
* wxPython_dict
= NULL
;
236 static PyObject
* wxPyPtrTypeMap
= NULL
;
238 PyObject
* __wxSetDictionary(PyObject
* /* self */, PyObject
* args
)
241 if (!PyArg_ParseTuple(args
, "O", &wxPython_dict
))
244 if (!PyDict_Check(wxPython_dict
)) {
245 PyErr_SetString(PyExc_TypeError
, "_wxSetDictionary must have dictionary object!");
249 if (! wxPyPtrTypeMap
)
250 wxPyPtrTypeMap
= PyDict_New();
251 PyDict_SetItemString(wxPython_dict
, "__wxPyPtrTypeMap", wxPyPtrTypeMap
);
255 #define wxPlatform "__WXMOTIF__"
258 #define wxPlatform "__WXQT__"
261 #define wxPlatform "__WXGTK__"
263 #if defined(__WIN32__) || defined(__WXMSW__)
264 #define wxPlatform "__WXMSW__"
267 #define wxPlatform "__WXMAC__"
270 PyDict_SetItemString(wxPython_dict
, "wxPlatform", PyString_FromString(wxPlatform
));
277 //---------------------------------------------------------------------------
278 // Stuff used by OOR to find the right wxPython class type to return and to
282 // The pointer type map is used when the "pointer" type name generated by SWIG
283 // is not the same as the shadow class name, for example wxPyTreeCtrl
284 // vs. wxTreeCtrl. It needs to be referenced in Python as well as from C++,
285 // so we'll just make it a Python dictionary in the wx module's namespace.
286 void wxPyPtrTypeMap_Add(const char* commonName
, const char* ptrName
) {
287 if (! wxPyPtrTypeMap
)
288 wxPyPtrTypeMap
= PyDict_New();
289 PyDict_SetItemString(wxPyPtrTypeMap
,
291 PyString_FromString((char*)ptrName
));
296 PyObject
* wxPyClassExists(const char* className
) {
301 char buff
[64]; // should always be big enough...
303 sprintf(buff
, "%sPtr", className
);
304 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
306 return classobj
; // returns NULL if not found
310 PyObject
* wxPyMake_wxObject(wxObject
* source
, bool checkEvtHandler
) {
311 PyObject
* target
= NULL
;
312 bool isEvtHandler
= FALSE
;
315 // If it's derived from wxEvtHandler then there may
316 // already be a pointer to a Python object that we can use
318 if (checkEvtHandler
&& wxIsKindOf(source
, wxEvtHandler
)) {
320 wxEvtHandler
* eh
= (wxEvtHandler
*)source
;
321 wxPyClientData
* data
= (wxPyClientData
*)eh
->GetClientObject();
323 target
= data
->m_obj
;
329 // Otherwise make it the old fashioned way by making a
330 // new shadow object and putting this pointer in it.
331 wxClassInfo
* info
= source
->GetClassInfo();
332 wxChar
* name
= (wxChar
*)info
->GetClassName();
333 PyObject
* klass
= wxPyClassExists(name
);
334 while (info
&& !klass
) {
335 name
= (wxChar
*)info
->GetBaseClassName1();
336 info
= wxClassInfo::FindClass(name
);
337 klass
= wxPyClassExists(name
);
340 target
= wxPyConstructObject(source
, name
, klass
, FALSE
);
341 if (target
&& isEvtHandler
)
342 ((wxEvtHandler
*)source
)->SetClientObject(new wxPyClientData(target
));
344 wxString
msg("wxPython class not found for ");
345 msg
+= source
->GetClassInfo()->GetClassName();
346 PyErr_SetString(PyExc_NameError
, msg
.c_str());
350 } else { // source was NULL so return None.
351 Py_INCREF(Py_None
); target
= Py_None
;
357 PyObject
* wxPyMake_wxSizer(wxSizer
* source
) {
358 PyObject
* target
= NULL
;
360 if (source
&& wxIsKindOf(source
, wxSizer
)) {
361 // If it's derived from wxSizer then there may
362 // already be a pointer to a Python object that we can use
364 wxSizer
* sz
= (wxSizer
*)source
;
365 wxPyClientData
* data
= (wxPyClientData
*)sz
->GetClientObject();
367 target
= data
->m_obj
;
372 target
= wxPyMake_wxObject(source
, FALSE
);
373 if (target
!= Py_None
)
374 ((wxSizer
*)source
)->SetClientObject(new wxPyClientData(target
));
381 //---------------------------------------------------------------------------
383 PyObject
* wxPyConstructObject(void* ptr
,
384 const char* className
,
391 char swigptr
[64]; // should always be big enough...
394 if ((item
= PyDict_GetItemString(wxPyPtrTypeMap
, (char*)className
)) != NULL
) {
395 className
= PyString_AsString(item
);
397 sprintf(buff
, "_%s_p", className
);
398 SWIG_MakePtr(swigptr
, ptr
, buff
);
400 arg
= Py_BuildValue("(s)", swigptr
);
401 obj
= PyInstance_New(klass
, arg
, NULL
);
405 PyObject
* one
= PyInt_FromLong(1);
406 PyObject_SetAttrString(obj
, "thisown", one
);
414 PyObject
* wxPyConstructObject(void* ptr
,
415 const char* className
,
424 char buff
[64]; // should always be big enough...
425 sprintf(buff
, "%sPtr", className
);
427 wxASSERT_MSG(wxPython_dict
, "wxPython_dict is not set yet!!");
429 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
433 "*** Unknown class name %s, tell Robin about it please ***",
435 obj
= PyString_FromString(temp
);
439 return wxPyConstructObject(ptr
, className
, classobj
, setThisOwn
);
442 //---------------------------------------------------------------------------
445 #ifdef WXP_WITH_THREAD
447 unsigned long wxPyGetCurrentThreadId() {
448 return wxThread::GetCurrentId();
453 PyThreadState
* wxPyGetThreadState() {
454 unsigned long ctid
= wxPyGetCurrentThreadId();
455 PyThreadState
* tstate
= NULL
;
458 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
459 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
460 if (info
.tid
== ctid
) {
461 tstate
= info
.tstate
;
465 wxPyTMutex
->Unlock();
466 wxASSERT_MSG(tstate
, "PyThreadState should not be NULL!");
471 void wxPySaveThreadState(PyThreadState
* tstate
) {
472 unsigned long ctid
= wxPyGetCurrentThreadId();
474 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
475 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
476 if (info
.tid
== ctid
) {
477 info
.tstate
= tstate
;
478 wxPyTMutex
->Unlock();
482 // not found, so add it...
483 wxPyTStates
->Add(new wxPyThreadState(ctid
, tstate
));
484 wxPyTMutex
->Unlock();
490 // Calls from Python to wxWindows code are wrapped in calls to these
493 PyThreadState
* wxPyBeginAllowThreads() {
494 #ifdef WXP_WITH_THREAD
495 PyThreadState
* saved
= PyEval_SaveThread(); // Py_BEGIN_ALLOW_THREADS;
496 wxPySaveThreadState(saved
);
503 void wxPyEndAllowThreads(PyThreadState
* saved
) {
504 #ifdef WXP_WITH_THREAD
505 PyEval_RestoreThread(saved
); // Py_END_ALLOW_THREADS;
511 // Calls from wxWindows back to Python code, or even any PyObject
512 // manipulations, PyDECREF's and etc. are wrapped in calls to these functions:
514 void wxPyBeginBlockThreads() {
515 #ifdef WXP_WITH_THREAD
516 PyThreadState
* tstate
= wxPyGetThreadState();
517 PyEval_RestoreThread(tstate
);
522 void wxPyEndBlockThreads() {
523 #ifdef WXP_WITH_THREAD
524 PyThreadState
* tstate
= PyEval_SaveThread();
525 // Is there any need to save it again?
530 //---------------------------------------------------------------------------
532 IMPLEMENT_ABSTRACT_CLASS(wxPyCallback
, wxObject
);
534 wxPyCallback::wxPyCallback(PyObject
* func
) {
539 wxPyCallback::wxPyCallback(const wxPyCallback
& other
) {
540 m_func
= other
.m_func
;
544 wxPyCallback::~wxPyCallback() {
545 wxPyBeginBlockThreads();
547 wxPyEndBlockThreads();
552 // This function is used for all events destined for Python event handlers.
553 void wxPyCallback::EventThunker(wxEvent
& event
) {
554 wxPyCallback
* cb
= (wxPyCallback
*)event
.m_callbackUserData
;
555 PyObject
* func
= cb
->m_func
;
561 wxPyBeginBlockThreads();
562 wxString className
= event
.GetClassInfo()->GetClassName();
564 if (className
== "wxPyEvent")
565 arg
= ((wxPyEvent
*)&event
)->GetSelf();
566 else if (className
== "wxPyCommandEvent")
567 arg
= ((wxPyCommandEvent
*)&event
)->GetSelf();
569 arg
= wxPyConstructObject((void*)&event
, className
);
571 tuple
= PyTuple_New(1);
572 PyTuple_SET_ITEM(tuple
, 0, arg
);
573 result
= PyEval_CallObject(func
, tuple
);
577 PyErr_Clear(); // Just in case...
581 wxPyEndBlockThreads();
585 //----------------------------------------------------------------------
587 wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper
& other
) {
589 m_self
= other
.m_self
;
590 m_class
= other
.m_class
;
598 void wxPyCallbackHelper::setSelf(PyObject
* self
, PyObject
* klass
, int incref
) {
609 #if PYTHON_API_VERSION >= 1011
611 // Prior to Python 2.2 PyMethod_GetClass returned the class object
612 // in which the method was defined. Starting with 2.2 it returns
613 // "class that asked for the method" which seems totally bogus to me
614 // but apprently if fixes some obscure problem waiting to happen in
615 // Python. Since the API was not documented Guido and the gang felt
616 // safe in changing it. Needless to say that totally screwed up the
617 // logic below in wxPyCallbackHelper::findCallback, hence this icky
618 // code to find the class where the method is actuallt defined...
621 PyObject
* PyFindClassWithAttr(PyObject
*klass
, PyObject
*name
)
625 if (PyType_Check(klass
)) { // new style classes
626 // This code is borrowed/adapted from _PyType_Lookup in typeobject.c
627 // (TODO: This part is not tested yet, so I'm not sure it is correct...)
628 PyTypeObject
* type
= (PyTypeObject
*)klass
;
629 PyObject
*mro
, *res
, *base
, *dict
;
630 /* Look in tp_dict of types in MRO */
632 assert(PyTuple_Check(mro
));
633 n
= PyTuple_GET_SIZE(mro
);
634 for (i
= 0; i
< n
; i
++) {
635 base
= PyTuple_GET_ITEM(mro
, i
);
636 if (PyClass_Check(base
))
637 dict
= ((PyClassObject
*)base
)->cl_dict
;
639 assert(PyType_Check(base
));
640 dict
= ((PyTypeObject
*)base
)->tp_dict
;
642 assert(dict
&& PyDict_Check(dict
));
643 res
= PyDict_GetItem(dict
, name
);
650 else if (PyClass_Check(klass
)) { // old style classes
651 // This code is borrowed/adapted from class_lookup in classobject.c
652 PyClassObject
* cp
= (PyClassObject
*)klass
;
653 PyObject
*value
= PyDict_GetItem(cp
->cl_dict
, name
);
655 return (PyObject
*)cp
;
657 n
= PyTuple_Size(cp
->cl_bases
);
658 for (i
= 0; i
< n
; i
++) {
659 PyObject
* base
= PyTuple_GetItem(cp
->cl_bases
, i
);
660 PyObject
*v
= PyFindClassWithAttr(base
, name
);
671 PyObject
* PyMethod_GetDefiningClass(PyObject
* method
, const char* name
)
673 PyObject
* mgc
= PyMethod_GET_CLASS(method
);
675 #if PYTHON_API_VERSION <= 1010 // prior to Python 2.2, the easy way
677 #else // 2.2 and after, the hard way...
679 PyObject
* nameo
= PyString_FromString(name
);
680 PyObject
* klass
= PyFindClassWithAttr(mgc
, nameo
);
688 bool wxPyCallbackHelper::findCallback(const char* name
) const {
689 wxPyCallbackHelper
* self
= (wxPyCallbackHelper
*)this; // cast away const
690 self
->m_lastFound
= NULL
;
692 // If the object (m_self) has an attibute of the given name...
693 if (m_self
&& PyObject_HasAttrString(m_self
, (char*)name
)) {
694 PyObject
*method
, *klass
;
695 method
= PyObject_GetAttrString(m_self
, (char*)name
);
697 // ...and if that attribute is a method, and if that method's class is
698 // not from a base class...
699 if (PyMethod_Check(method
) &&
700 (klass
= PyMethod_GetDefiningClass(method
, (char*)name
)) != NULL
&&
701 ((klass
== m_class
) || PyClass_IsSubclass(klass
, m_class
))) {
703 // ...then we'll save a pointer to the method so callCallback can call it.
704 self
->m_lastFound
= method
;
710 return m_lastFound
!= NULL
;
714 int wxPyCallbackHelper::callCallback(PyObject
* argTuple
) const {
718 result
= callCallbackObj(argTuple
);
719 if (result
) { // Assumes an integer return type...
720 retval
= PyInt_AsLong(result
);
722 PyErr_Clear(); // forget about it if it's not...
727 // Invoke the Python callable object, returning the raw PyObject return
728 // value. Caller should DECREF the return value and also call PyEval_SaveThread.
729 PyObject
* wxPyCallbackHelper::callCallbackObj(PyObject
* argTuple
) const {
732 // Save a copy of the pointer in case the callback generates another
733 // callback. In that case m_lastFound will have a different value when
734 // it gets back here...
735 PyObject
* method
= m_lastFound
;
737 result
= PyEval_CallObject(method
, argTuple
);
747 void wxPyCBH_setCallbackInfo(wxPyCallbackHelper
& cbh
, PyObject
* self
, PyObject
* klass
, int incref
) {
748 cbh
.setSelf(self
, klass
, incref
);
751 bool wxPyCBH_findCallback(const wxPyCallbackHelper
& cbh
, const char* name
) {
752 return cbh
.findCallback(name
);
755 int wxPyCBH_callCallback(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
756 return cbh
.callCallback(argTuple
);
759 PyObject
* wxPyCBH_callCallbackObj(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
760 return cbh
.callCallbackObj(argTuple
);
764 void wxPyCBH_delete(wxPyCallbackHelper
* cbh
) {
766 wxPyBeginBlockThreads();
767 Py_XDECREF(cbh
->m_self
);
768 Py_XDECREF(cbh
->m_class
);
769 wxPyEndBlockThreads();
773 //---------------------------------------------------------------------------
774 //---------------------------------------------------------------------------
775 // These event classes can be derived from in Python and passed through the event
776 // system without losing anything. They do this by keeping a reference to
777 // themselves and some special case handling in wxPyCallback::EventThunker.
780 wxPyEvtSelfRef::wxPyEvtSelfRef() {
781 //m_self = Py_None; // **** We don't do normal ref counting to prevent
782 //Py_INCREF(m_self); // circular loops...
786 wxPyEvtSelfRef::~wxPyEvtSelfRef() {
787 wxPyBeginBlockThreads();
790 wxPyEndBlockThreads();
793 void wxPyEvtSelfRef::SetSelf(PyObject
* self
, bool clone
) {
794 wxPyBeginBlockThreads();
802 wxPyEndBlockThreads();
805 PyObject
* wxPyEvtSelfRef::GetSelf() const {
811 IMPLEMENT_ABSTRACT_CLASS(wxPyEvent
, wxEvent
);
812 IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent
, wxCommandEvent
);
815 wxPyEvent::wxPyEvent(int id
)
820 wxPyEvent::wxPyEvent(const wxPyEvent
& evt
)
823 SetSelf(evt
.m_self
, TRUE
);
827 wxPyEvent::~wxPyEvent() {
831 wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType
, int id
)
832 : wxCommandEvent(commandType
, id
) {
836 wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent
& evt
)
837 : wxCommandEvent(evt
)
839 SetSelf(evt
.m_self
, TRUE
);
843 wxPyCommandEvent::~wxPyCommandEvent() {
849 //---------------------------------------------------------------------------
850 //---------------------------------------------------------------------------
853 wxPyTimer::wxPyTimer(PyObject
* callback
) {
858 wxPyTimer::~wxPyTimer() {
859 wxPyBeginBlockThreads();
861 wxPyEndBlockThreads();
864 void wxPyTimer::Notify() {
865 if (!func
|| func
== Py_None
) {
869 wxPyBeginBlockThreads();
872 PyObject
* args
= Py_BuildValue("()");
874 result
= PyEval_CallObject(func
, args
);
883 wxPyEndBlockThreads();
889 //---------------------------------------------------------------------------
890 //---------------------------------------------------------------------------
891 // Convert a wxList to a Python List
893 PyObject
* wxPy_ConvertList(wxListBase
* list
, const char* className
) {
897 wxNode
* node
= list
->First();
899 wxPyBeginBlockThreads();
900 pyList
= PyList_New(0);
902 wxObj
= node
->Data();
903 pyObj
= wxPyMake_wxObject(wxObj
); //wxPyConstructObject(wxObj, className);
904 PyList_Append(pyList
, pyObj
);
907 wxPyEndBlockThreads();
911 //----------------------------------------------------------------------
913 long wxPyGetWinHandle(wxWindow
* win
) {
915 return (long)win
->GetHandle();
918 // Find and return the actual X-Window.
920 if (win
->m_wxwindow
) {
921 GdkWindowPrivate
* bwin
= (GdkWindowPrivate
*)GTK_PIZZA(win
->m_wxwindow
)->bin_window
;
923 return (long)bwin
->xwindow
;
930 //----------------------------------------------------------------------
931 // Some helper functions for typemaps in my_typemaps.i, so they won't be
932 // included in every file...
935 byte
* byte_LIST_helper(PyObject
* source
) {
936 if (!PyList_Check(source
)) {
937 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
940 int count
= PyList_Size(source
);
941 byte
* temp
= new byte
[count
];
943 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
946 for (int x
=0; x
<count
; x
++) {
947 PyObject
* o
= PyList_GetItem(source
, x
);
948 if (! PyInt_Check(o
)) {
949 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
952 temp
[x
] = (byte
)PyInt_AsLong(o
);
958 int* int_LIST_helper(PyObject
* source
) {
959 if (!PyList_Check(source
)) {
960 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
963 int count
= PyList_Size(source
);
964 int* temp
= new int[count
];
966 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
969 for (int x
=0; x
<count
; x
++) {
970 PyObject
* o
= PyList_GetItem(source
, x
);
971 if (! PyInt_Check(o
)) {
972 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
975 temp
[x
] = PyInt_AsLong(o
);
981 long* long_LIST_helper(PyObject
* source
) {
982 if (!PyList_Check(source
)) {
983 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
986 int count
= PyList_Size(source
);
987 long* temp
= new long[count
];
989 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
992 for (int x
=0; x
<count
; x
++) {
993 PyObject
* o
= PyList_GetItem(source
, x
);
994 if (! PyInt_Check(o
)) {
995 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
998 temp
[x
] = PyInt_AsLong(o
);
1004 char** string_LIST_helper(PyObject
* source
) {
1005 if (!PyList_Check(source
)) {
1006 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1009 int count
= PyList_Size(source
);
1010 char** temp
= new char*[count
];
1012 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1015 for (int x
=0; x
<count
; x
++) {
1016 PyObject
* o
= PyList_GetItem(source
, x
);
1017 if (! PyString_Check(o
)) {
1018 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1021 temp
[x
] = PyString_AsString(o
);
1026 //--------------------------------
1027 // Part of patch from Tim Hochberg
1028 static inline bool wxPointFromObjects(PyObject
* o1
, PyObject
* o2
, wxPoint
* point
) {
1029 if (PyInt_Check(o1
) && PyInt_Check(o2
)) {
1030 point
->x
= PyInt_AS_LONG(o1
);
1031 point
->y
= PyInt_AS_LONG(o2
);
1034 if (PyFloat_Check(o1
) && PyFloat_Check(o2
)) {
1035 point
->x
= (int)PyFloat_AS_DOUBLE(o1
);
1036 point
->y
= (int)PyFloat_AS_DOUBLE(o2
);
1039 if (PyInstance_Check(o1
) || PyInstance_Check(o2
)) {
1040 // Disallow instances because they can cause havok
1043 if (PyNumber_Check(o1
) && PyNumber_Check(o2
)) {
1044 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
1045 point
->x
= PyInt_AsLong(o1
);
1046 point
->y
= PyInt_AsLong(o2
);
1053 wxPoint
* wxPoint_LIST_helper(PyObject
* source
, int *count
) {
1054 // Putting all of the declarations here allows
1055 // us to put the error handling all in one place.
1058 PyObject
*o
, *o1
, *o2
;
1059 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1061 if (!PySequence_Check(source
)) {
1065 // The length of the sequence is returned in count.
1066 *count
= PySequence_Length(source
);
1071 temp
= new wxPoint
[*count
];
1073 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1076 for (x
=0; x
<*count
; x
++) {
1077 // Get an item: try fast way first.
1079 o
= PySequence_Fast_GET_ITEM(source
, x
);
1082 o
= PySequence_GetItem(source
, x
);
1088 // Convert o to wxPoint.
1089 if ((PyTuple_Check(o
) && PyTuple_GET_SIZE(o
) == 2) ||
1090 (PyList_Check(o
) && PyList_GET_SIZE(o
) == 2)) {
1091 o1
= PySequence_Fast_GET_ITEM(o
, 0);
1092 o2
= PySequence_Fast_GET_ITEM(o
, 1);
1093 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
1097 else if (PyInstance_Check(o
)) {
1099 if (SWIG_GetPtrObj(o
, (void **)&pt
, "_wxPoint_p")) {
1104 else if (PySequence_Check(o
) && PySequence_Length(o
) == 2) {
1105 o1
= PySequence_GetItem(o
, 0);
1106 o2
= PySequence_GetItem(o
, 1);
1107 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
1131 PyErr_SetString(PyExc_TypeError
, "Expected a sequence of length-2 sequences or wxPoints.");
1135 //------------------------------
1138 wxBitmap
** wxBitmap_LIST_helper(PyObject
* source
) {
1139 if (!PyList_Check(source
)) {
1140 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1143 int count
= PyList_Size(source
);
1144 wxBitmap
** temp
= new wxBitmap
*[count
];
1146 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1149 for (int x
=0; x
<count
; x
++) {
1150 PyObject
* o
= PyList_GetItem(source
, x
);
1151 if (PyInstance_Check(o
)) {
1153 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxBitmap_p")) {
1154 PyErr_SetString(PyExc_TypeError
,"Expected _wxBitmap_p.");
1160 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxBitmaps.");
1169 wxString
* wxString_LIST_helper(PyObject
* source
) {
1170 if (!PyList_Check(source
)) {
1171 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1174 int count
= PyList_Size(source
);
1175 wxString
* temp
= new wxString
[count
];
1177 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1180 for (int x
=0; x
<count
; x
++) {
1181 PyObject
* o
= PyList_GetItem(source
, x
);
1182 #if PYTHON_API_VERSION >= 1009
1183 if (! PyString_Check(o
) && ! PyUnicode_Check(o
)) {
1184 PyErr_SetString(PyExc_TypeError
, "Expected a list of string or unicode objects.");
1190 if (PyString_AsStringAndSize(o
, &buff
, &length
) == -1)
1192 temp
[x
] = wxString(buff
, length
);
1194 if (! PyString_Check(o
)) {
1195 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1198 temp
[x
] = PyString_AsString(o
);
1205 wxAcceleratorEntry
* wxAcceleratorEntry_LIST_helper(PyObject
* source
) {
1206 if (!PyList_Check(source
)) {
1207 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1210 int count
= PyList_Size(source
);
1211 wxAcceleratorEntry
* temp
= new wxAcceleratorEntry
[count
];
1213 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1216 for (int x
=0; x
<count
; x
++) {
1217 PyObject
* o
= PyList_GetItem(source
, x
);
1218 if (PyInstance_Check(o
)) {
1219 wxAcceleratorEntry
* ae
;
1220 if (SWIG_GetPtrObj(o
, (void **) &ae
,"_wxAcceleratorEntry_p")) {
1221 PyErr_SetString(PyExc_TypeError
,"Expected _wxAcceleratorEntry_p.");
1226 else if (PyTuple_Check(o
)) {
1227 PyObject
* o1
= PyTuple_GetItem(o
, 0);
1228 PyObject
* o2
= PyTuple_GetItem(o
, 1);
1229 PyObject
* o3
= PyTuple_GetItem(o
, 2);
1230 temp
[x
].Set(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
));
1233 PyErr_SetString(PyExc_TypeError
, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
1241 wxPen
** wxPen_LIST_helper(PyObject
* source
) {
1242 if (!PyList_Check(source
)) {
1243 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1246 int count
= PyList_Size(source
);
1247 wxPen
** temp
= new wxPen
*[count
];
1249 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1252 for (int x
=0; x
<count
; x
++) {
1253 PyObject
* o
= PyList_GetItem(source
, x
);
1254 if (PyInstance_Check(o
)) {
1256 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxPen_p")) {
1258 PyErr_SetString(PyExc_TypeError
,"Expected _wxPen_p.");
1265 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxPens.");
1273 bool _2int_seq_helper(PyObject
* source
, int* i1
, int* i2
) {
1274 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1277 if (!PySequence_Check(source
) || PySequence_Length(source
) != 2)
1281 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1282 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1285 o1
= PySequence_GetItem(source
, 0);
1286 o2
= PySequence_GetItem(source
, 1);
1289 *i1
= PyInt_AsLong(o1
);
1290 *i2
= PyInt_AsLong(o2
);
1300 bool _4int_seq_helper(PyObject
* source
, int* i1
, int* i2
, int* i3
, int* i4
) {
1301 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1302 PyObject
*o1
, *o2
, *o3
, *o4
;
1304 if (!PySequence_Check(source
) || PySequence_Length(source
) != 4)
1308 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1309 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1310 o3
= PySequence_Fast_GET_ITEM(source
, 2);
1311 o4
= PySequence_Fast_GET_ITEM(source
, 3);
1314 o1
= PySequence_GetItem(source
, 0);
1315 o2
= PySequence_GetItem(source
, 1);
1316 o3
= PySequence_GetItem(source
, 2);
1317 o4
= PySequence_GetItem(source
, 3);
1320 *i1
= PyInt_AsLong(o1
);
1321 *i2
= PyInt_AsLong(o2
);
1322 *i3
= PyInt_AsLong(o3
);
1323 *i4
= PyInt_AsLong(o4
);
1335 //----------------------------------------------------------------------
1337 bool wxSize_helper(PyObject
* source
, wxSize
** obj
) {
1339 // If source is an object instance then it may already be the right type
1340 if (PyInstance_Check(source
)) {
1342 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxSize_p"))
1347 // otherwise a 2-tuple of integers is expected
1348 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1349 PyObject
* o1
= PySequence_GetItem(source
, 0);
1350 PyObject
* o2
= PySequence_GetItem(source
, 1);
1351 **obj
= wxSize(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1356 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxSize object.");
1360 bool wxPoint_helper(PyObject
* source
, wxPoint
** obj
) {
1362 // If source is an object instance then it may already be the right type
1363 if (PyInstance_Check(source
)) {
1365 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxPoint_p"))
1370 // otherwise a length-2 sequence of integers is expected
1371 if (PySequence_Check(source
) && PySequence_Length(source
) == 2) {
1372 PyObject
* o1
= PySequence_GetItem(source
, 0);
1373 PyObject
* o2
= PySequence_GetItem(source
, 1);
1374 // This should really check for integers, not numbers -- but that would break code.
1375 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1380 **obj
= wxPoint(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1386 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxPoint object.");
1392 bool wxRealPoint_helper(PyObject
* source
, wxRealPoint
** obj
) {
1394 // If source is an object instance then it may already be the right type
1395 if (PyInstance_Check(source
)) {
1397 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRealPoint_p"))
1402 // otherwise a 2-tuple of floats is expected
1403 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1404 PyObject
* o1
= PySequence_GetItem(source
, 0);
1405 PyObject
* o2
= PySequence_GetItem(source
, 1);
1406 **obj
= wxRealPoint(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
));
1411 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxRealPoint object.");
1418 bool wxRect_helper(PyObject
* source
, wxRect
** obj
) {
1420 // If source is an object instance then it may already be the right type
1421 if (PyInstance_Check(source
)) {
1423 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRect_p"))
1428 // otherwise a 4-tuple of integers is expected
1429 else if (PySequence_Check(source
) && PyObject_Length(source
) == 4) {
1430 PyObject
* o1
= PySequence_GetItem(source
, 0);
1431 PyObject
* o2
= PySequence_GetItem(source
, 1);
1432 PyObject
* o3
= PySequence_GetItem(source
, 2);
1433 PyObject
* o4
= PySequence_GetItem(source
, 3);
1434 **obj
= wxRect(PyInt_AsLong(o1
), PyInt_AsLong(o2
),
1435 PyInt_AsLong(o3
), PyInt_AsLong(o4
));
1440 PyErr_SetString(PyExc_TypeError
, "Expected a 4-tuple of integers or a wxRect object.");
1446 bool wxColour_helper(PyObject
* source
, wxColour
** obj
) {
1448 // If source is an object instance then it may already be the right type
1449 if (PyInstance_Check(source
)) {
1451 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxColour_p"))
1456 // otherwise a string is expected
1457 else if (PyString_Check(source
)) {
1458 wxString spec
= PyString_AS_STRING(source
);
1459 if (spec
[0U] == '#' && spec
.Length() == 7) { // It's #RRGGBB
1461 int red
= strtol(spec
.Mid(1,2), &junk
, 16);
1462 int green
= strtol(spec
.Mid(3,2), &junk
, 16);
1463 int blue
= strtol(spec
.Mid(5,2), &junk
, 16);
1464 **obj
= wxColour(red
, green
, blue
);
1467 else { // it's a colour name
1468 **obj
= wxColour(spec
);
1474 PyErr_SetString(PyExc_TypeError
, "Expected a wxColour object or a string containing a colour name or '#RRGGBB'.");
1479 //----------------------------------------------------------------------
1481 PyObject
* wxArrayString2PyList_helper(const wxArrayString
& arr
) {
1483 PyObject
* list
= PyList_New(0);
1484 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
1485 PyObject
* str
= PyString_FromString(arr
[i
].c_str());
1486 PyList_Append(list
, str
);
1493 //----------------------------------------------------------------------
1494 //----------------------------------------------------------------------