]>
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);
226 #ifdef WXP_WITH_THREAD
229 wxPyTStates
->Empty();
237 static PyObject
* wxPython_dict
= NULL
;
238 static PyObject
* wxPyPtrTypeMap
= NULL
;
240 PyObject
* __wxSetDictionary(PyObject
* /* self */, PyObject
* args
)
243 if (!PyArg_ParseTuple(args
, "O", &wxPython_dict
))
246 if (!PyDict_Check(wxPython_dict
)) {
247 PyErr_SetString(PyExc_TypeError
, "_wxSetDictionary must have dictionary object!");
251 if (! wxPyPtrTypeMap
)
252 wxPyPtrTypeMap
= PyDict_New();
253 PyDict_SetItemString(wxPython_dict
, "__wxPyPtrTypeMap", wxPyPtrTypeMap
);
257 #define wxPlatform "__WXMOTIF__"
260 #define wxPlatform "__WXX11__"
263 #define wxPlatform "__WXGTK__"
265 #if defined(__WIN32__) || defined(__WXMSW__)
266 #define wxPlatform "__WXMSW__"
269 #define wxPlatform "__WXMAC__"
272 PyDict_SetItemString(wxPython_dict
, "wxPlatform", PyString_FromString(wxPlatform
));
279 //---------------------------------------------------------------------------
280 // Stuff used by OOR to find the right wxPython class type to return and to
284 // The pointer type map is used when the "pointer" type name generated by SWIG
285 // is not the same as the shadow class name, for example wxPyTreeCtrl
286 // vs. wxTreeCtrl. It needs to be referenced in Python as well as from C++,
287 // so we'll just make it a Python dictionary in the wx module's namespace.
288 void wxPyPtrTypeMap_Add(const char* commonName
, const char* ptrName
) {
289 if (! wxPyPtrTypeMap
)
290 wxPyPtrTypeMap
= PyDict_New();
291 PyDict_SetItemString(wxPyPtrTypeMap
,
293 PyString_FromString((char*)ptrName
));
298 PyObject
* wxPyClassExists(const char* className
) {
303 char buff
[64]; // should always be big enough...
305 sprintf(buff
, "%sPtr", className
);
306 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
308 return classobj
; // returns NULL if not found
312 PyObject
* wxPyMake_wxObject(wxObject
* source
, bool checkEvtHandler
) {
313 PyObject
* target
= NULL
;
314 bool isEvtHandler
= FALSE
;
317 // If it's derived from wxEvtHandler then there may
318 // already be a pointer to a Python object that we can use
320 if (checkEvtHandler
&& wxIsKindOf(source
, wxEvtHandler
)) {
322 wxEvtHandler
* eh
= (wxEvtHandler
*)source
;
323 wxPyClientData
* data
= (wxPyClientData
*)eh
->GetClientObject();
325 target
= data
->m_obj
;
331 // Otherwise make it the old fashioned way by making a
332 // new shadow object and putting this pointer in it.
333 wxClassInfo
* info
= source
->GetClassInfo();
334 wxChar
* name
= (wxChar
*)info
->GetClassName();
335 PyObject
* klass
= wxPyClassExists(name
);
336 while (info
&& !klass
) {
337 name
= (wxChar
*)info
->GetBaseClassName1();
338 info
= wxClassInfo::FindClass(name
);
339 klass
= wxPyClassExists(name
);
342 target
= wxPyConstructObject(source
, name
, klass
, FALSE
);
343 if (target
&& isEvtHandler
)
344 ((wxEvtHandler
*)source
)->SetClientObject(new wxPyClientData(target
));
346 wxString
msg("wxPython class not found for ");
347 msg
+= source
->GetClassInfo()->GetClassName();
348 PyErr_SetString(PyExc_NameError
, msg
.c_str());
352 } else { // source was NULL so return None.
353 Py_INCREF(Py_None
); target
= Py_None
;
359 PyObject
* wxPyMake_wxSizer(wxSizer
* source
) {
360 PyObject
* target
= NULL
;
362 if (source
&& wxIsKindOf(source
, wxSizer
)) {
363 // If it's derived from wxSizer then there may
364 // already be a pointer to a Python object that we can use
366 wxSizer
* sz
= (wxSizer
*)source
;
367 wxPyClientData
* data
= (wxPyClientData
*)sz
->GetClientObject();
369 target
= data
->m_obj
;
374 target
= wxPyMake_wxObject(source
, FALSE
);
375 if (target
!= Py_None
)
376 ((wxSizer
*)source
)->SetClientObject(new wxPyClientData(target
));
383 //---------------------------------------------------------------------------
385 PyObject
* wxPyConstructObject(void* ptr
,
386 const char* className
,
393 char swigptr
[64]; // should always be big enough...
396 if ((item
= PyDict_GetItemString(wxPyPtrTypeMap
, (char*)className
)) != NULL
) {
397 className
= PyString_AsString(item
);
399 sprintf(buff
, "_%s_p", className
);
400 SWIG_MakePtr(swigptr
, ptr
, buff
);
402 arg
= Py_BuildValue("(s)", swigptr
);
403 obj
= PyInstance_New(klass
, arg
, NULL
);
407 PyObject
* one
= PyInt_FromLong(1);
408 PyObject_SetAttrString(obj
, "thisown", one
);
416 PyObject
* wxPyConstructObject(void* ptr
,
417 const char* className
,
426 char buff
[64]; // should always be big enough...
427 sprintf(buff
, "%sPtr", className
);
429 wxASSERT_MSG(wxPython_dict
, "wxPython_dict is not set yet!!");
431 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
435 "*** Unknown class name %s, tell Robin about it please ***",
437 obj
= PyString_FromString(temp
);
441 return wxPyConstructObject(ptr
, className
, classobj
, setThisOwn
);
444 //---------------------------------------------------------------------------
447 #ifdef WXP_WITH_THREAD
449 unsigned long wxPyGetCurrentThreadId() {
450 return wxThread::GetCurrentId();
453 static PyThreadState
* gs_shutdownTState
;
455 PyThreadState
* wxPyGetThreadState() {
456 if (wxPyTMutex
== NULL
) // Python is shutting down...
457 return gs_shutdownTState
;
459 unsigned long ctid
= wxPyGetCurrentThreadId();
460 PyThreadState
* tstate
= NULL
;
463 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
464 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
465 if (info
.tid
== ctid
) {
466 tstate
= info
.tstate
;
470 wxPyTMutex
->Unlock();
471 wxASSERT_MSG(tstate
, "PyThreadState should not be NULL!");
476 void wxPySaveThreadState(PyThreadState
* tstate
) {
477 if (wxPyTMutex
== NULL
) { // Python is shutting down, assume a single thread...
478 gs_shutdownTState
= tstate
;
481 unsigned long ctid
= wxPyGetCurrentThreadId();
483 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
484 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
485 if (info
.tid
== ctid
) {
486 info
.tstate
= tstate
;
487 wxPyTMutex
->Unlock();
491 // not found, so add it...
492 wxPyTStates
->Add(new wxPyThreadState(ctid
, tstate
));
493 wxPyTMutex
->Unlock();
499 // Calls from Python to wxWindows code are wrapped in calls to these
502 PyThreadState
* wxPyBeginAllowThreads() {
503 #ifdef WXP_WITH_THREAD
504 PyThreadState
* saved
= PyEval_SaveThread(); // Py_BEGIN_ALLOW_THREADS;
505 wxPySaveThreadState(saved
);
512 void wxPyEndAllowThreads(PyThreadState
* saved
) {
513 #ifdef WXP_WITH_THREAD
514 PyEval_RestoreThread(saved
); // Py_END_ALLOW_THREADS;
520 // Calls from wxWindows back to Python code, or even any PyObject
521 // manipulations, PyDECREF's and etc. are wrapped in calls to these functions:
523 void wxPyBeginBlockThreads() {
524 #ifdef WXP_WITH_THREAD
525 PyThreadState
* tstate
= wxPyGetThreadState();
526 PyEval_RestoreThread(tstate
);
531 void wxPyEndBlockThreads() {
532 #ifdef WXP_WITH_THREAD
533 PyThreadState
* tstate
= PyEval_SaveThread();
534 // Is there any need to save it again?
539 //---------------------------------------------------------------------------
541 IMPLEMENT_ABSTRACT_CLASS(wxPyCallback
, wxObject
);
543 wxPyCallback::wxPyCallback(PyObject
* func
) {
548 wxPyCallback::wxPyCallback(const wxPyCallback
& other
) {
549 m_func
= other
.m_func
;
553 wxPyCallback::~wxPyCallback() {
554 wxPyBeginBlockThreads();
556 wxPyEndBlockThreads();
561 // This function is used for all events destined for Python event handlers.
562 void wxPyCallback::EventThunker(wxEvent
& event
) {
563 wxPyCallback
* cb
= (wxPyCallback
*)event
.m_callbackUserData
;
564 PyObject
* func
= cb
->m_func
;
570 wxPyBeginBlockThreads();
571 wxString className
= event
.GetClassInfo()->GetClassName();
573 if (className
== "wxPyEvent")
574 arg
= ((wxPyEvent
*)&event
)->GetSelf();
575 else if (className
== "wxPyCommandEvent")
576 arg
= ((wxPyCommandEvent
*)&event
)->GetSelf();
578 arg
= wxPyConstructObject((void*)&event
, className
);
580 tuple
= PyTuple_New(1);
581 PyTuple_SET_ITEM(tuple
, 0, arg
);
582 result
= PyEval_CallObject(func
, tuple
);
586 PyErr_Clear(); // Just in case...
590 wxPyEndBlockThreads();
594 //----------------------------------------------------------------------
596 wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper
& other
) {
598 m_self
= other
.m_self
;
599 m_class
= other
.m_class
;
607 void wxPyCallbackHelper::setSelf(PyObject
* self
, PyObject
* klass
, int incref
) {
618 #if PYTHON_API_VERSION >= 1011
620 // Prior to Python 2.2 PyMethod_GetClass returned the class object
621 // in which the method was defined. Starting with 2.2 it returns
622 // "class that asked for the method" which seems totally bogus to me
623 // but apprently if fixes some obscure problem waiting to happen in
624 // Python. Since the API was not documented Guido and the gang felt
625 // safe in changing it. Needless to say that totally screwed up the
626 // logic below in wxPyCallbackHelper::findCallback, hence this icky
627 // code to find the class where the method is actuallt defined...
630 PyObject
* PyFindClassWithAttr(PyObject
*klass
, PyObject
*name
)
634 if (PyType_Check(klass
)) { // new style classes
635 // This code is borrowed/adapted from _PyType_Lookup in typeobject.c
636 // (TODO: This part is not tested yet, so I'm not sure it is correct...)
637 PyTypeObject
* type
= (PyTypeObject
*)klass
;
638 PyObject
*mro
, *res
, *base
, *dict
;
639 /* Look in tp_dict of types in MRO */
641 assert(PyTuple_Check(mro
));
642 n
= PyTuple_GET_SIZE(mro
);
643 for (i
= 0; i
< n
; i
++) {
644 base
= PyTuple_GET_ITEM(mro
, i
);
645 if (PyClass_Check(base
))
646 dict
= ((PyClassObject
*)base
)->cl_dict
;
648 assert(PyType_Check(base
));
649 dict
= ((PyTypeObject
*)base
)->tp_dict
;
651 assert(dict
&& PyDict_Check(dict
));
652 res
= PyDict_GetItem(dict
, name
);
659 else if (PyClass_Check(klass
)) { // old style classes
660 // This code is borrowed/adapted from class_lookup in classobject.c
661 PyClassObject
* cp
= (PyClassObject
*)klass
;
662 PyObject
*value
= PyDict_GetItem(cp
->cl_dict
, name
);
664 return (PyObject
*)cp
;
666 n
= PyTuple_Size(cp
->cl_bases
);
667 for (i
= 0; i
< n
; i
++) {
668 PyObject
* base
= PyTuple_GetItem(cp
->cl_bases
, i
);
669 PyObject
*v
= PyFindClassWithAttr(base
, name
);
680 PyObject
* PyMethod_GetDefiningClass(PyObject
* method
, const char* name
)
682 PyObject
* mgc
= PyMethod_GET_CLASS(method
);
684 #if PYTHON_API_VERSION <= 1010 // prior to Python 2.2, the easy way
686 #else // 2.2 and after, the hard way...
688 PyObject
* nameo
= PyString_FromString(name
);
689 PyObject
* klass
= PyFindClassWithAttr(mgc
, nameo
);
697 bool wxPyCallbackHelper::findCallback(const char* name
) const {
698 wxPyCallbackHelper
* self
= (wxPyCallbackHelper
*)this; // cast away const
699 self
->m_lastFound
= NULL
;
701 // If the object (m_self) has an attibute of the given name...
702 if (m_self
&& PyObject_HasAttrString(m_self
, (char*)name
)) {
703 PyObject
*method
, *klass
;
704 method
= PyObject_GetAttrString(m_self
, (char*)name
);
706 // ...and if that attribute is a method, and if that method's class is
707 // not from a base class...
708 if (PyMethod_Check(method
) &&
709 (klass
= PyMethod_GetDefiningClass(method
, (char*)name
)) != NULL
&&
710 ((klass
== m_class
) || PyClass_IsSubclass(klass
, m_class
))) {
712 // ...then we'll save a pointer to the method so callCallback can call it.
713 self
->m_lastFound
= method
;
719 return m_lastFound
!= NULL
;
723 int wxPyCallbackHelper::callCallback(PyObject
* argTuple
) const {
727 result
= callCallbackObj(argTuple
);
728 if (result
) { // Assumes an integer return type...
729 retval
= PyInt_AsLong(result
);
731 PyErr_Clear(); // forget about it if it's not...
736 // Invoke the Python callable object, returning the raw PyObject return
737 // value. Caller should DECREF the return value and also call PyEval_SaveThread.
738 PyObject
* wxPyCallbackHelper::callCallbackObj(PyObject
* argTuple
) const {
741 // Save a copy of the pointer in case the callback generates another
742 // callback. In that case m_lastFound will have a different value when
743 // it gets back here...
744 PyObject
* method
= m_lastFound
;
746 result
= PyEval_CallObject(method
, argTuple
);
756 void wxPyCBH_setCallbackInfo(wxPyCallbackHelper
& cbh
, PyObject
* self
, PyObject
* klass
, int incref
) {
757 cbh
.setSelf(self
, klass
, incref
);
760 bool wxPyCBH_findCallback(const wxPyCallbackHelper
& cbh
, const char* name
) {
761 return cbh
.findCallback(name
);
764 int wxPyCBH_callCallback(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
765 return cbh
.callCallback(argTuple
);
768 PyObject
* wxPyCBH_callCallbackObj(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
769 return cbh
.callCallbackObj(argTuple
);
773 void wxPyCBH_delete(wxPyCallbackHelper
* cbh
) {
775 wxPyBeginBlockThreads();
776 Py_XDECREF(cbh
->m_self
);
777 Py_XDECREF(cbh
->m_class
);
778 wxPyEndBlockThreads();
782 //---------------------------------------------------------------------------
783 //---------------------------------------------------------------------------
784 // These event classes can be derived from in Python and passed through the event
785 // system without losing anything. They do this by keeping a reference to
786 // themselves and some special case handling in wxPyCallback::EventThunker.
789 wxPyEvtSelfRef::wxPyEvtSelfRef() {
790 //m_self = Py_None; // **** We don't do normal ref counting to prevent
791 //Py_INCREF(m_self); // circular loops...
795 wxPyEvtSelfRef::~wxPyEvtSelfRef() {
796 wxPyBeginBlockThreads();
799 wxPyEndBlockThreads();
802 void wxPyEvtSelfRef::SetSelf(PyObject
* self
, bool clone
) {
803 wxPyBeginBlockThreads();
811 wxPyEndBlockThreads();
814 PyObject
* wxPyEvtSelfRef::GetSelf() const {
820 IMPLEMENT_ABSTRACT_CLASS(wxPyEvent
, wxEvent
);
821 IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent
, wxCommandEvent
);
824 wxPyEvent::wxPyEvent(int id
)
829 wxPyEvent::wxPyEvent(const wxPyEvent
& evt
)
832 SetSelf(evt
.m_self
, TRUE
);
836 wxPyEvent::~wxPyEvent() {
840 wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType
, int id
)
841 : wxCommandEvent(commandType
, id
) {
845 wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent
& evt
)
846 : wxCommandEvent(evt
)
848 SetSelf(evt
.m_self
, TRUE
);
852 wxPyCommandEvent::~wxPyCommandEvent() {
858 //---------------------------------------------------------------------------
859 //---------------------------------------------------------------------------
862 wxPyTimer::wxPyTimer(PyObject
* callback
) {
867 wxPyTimer::~wxPyTimer() {
868 wxPyBeginBlockThreads();
870 wxPyEndBlockThreads();
873 void wxPyTimer::Notify() {
874 if (!func
|| func
== Py_None
) {
878 wxPyBeginBlockThreads();
881 PyObject
* args
= Py_BuildValue("()");
883 result
= PyEval_CallObject(func
, args
);
892 wxPyEndBlockThreads();
898 //---------------------------------------------------------------------------
899 //---------------------------------------------------------------------------
900 // Convert a wxList to a Python List
902 PyObject
* wxPy_ConvertList(wxListBase
* list
, const char* className
) {
906 wxNode
* node
= list
->First();
908 wxPyBeginBlockThreads();
909 pyList
= PyList_New(0);
911 wxObj
= node
->Data();
912 pyObj
= wxPyMake_wxObject(wxObj
); //wxPyConstructObject(wxObj, className);
913 PyList_Append(pyList
, pyObj
);
916 wxPyEndBlockThreads();
920 //----------------------------------------------------------------------
922 long wxPyGetWinHandle(wxWindow
* win
) {
924 return (long)win
->GetHandle();
927 // Find and return the actual X-Window.
929 if (win
->m_wxwindow
) {
930 GdkWindowPrivate
* bwin
= (GdkWindowPrivate
*)GTK_PIZZA(win
->m_wxwindow
)->bin_window
;
932 return (long)bwin
->xwindow
;
939 //----------------------------------------------------------------------
940 // Some helper functions for typemaps in my_typemaps.i, so they won't be
941 // included in every file...
944 byte
* byte_LIST_helper(PyObject
* source
) {
945 if (!PyList_Check(source
)) {
946 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
949 int count
= PyList_Size(source
);
950 byte
* temp
= new byte
[count
];
952 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
955 for (int x
=0; x
<count
; x
++) {
956 PyObject
* o
= PyList_GetItem(source
, x
);
957 if (! PyInt_Check(o
)) {
958 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
961 temp
[x
] = (byte
)PyInt_AsLong(o
);
967 int* int_LIST_helper(PyObject
* source
) {
968 if (!PyList_Check(source
)) {
969 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
972 int count
= PyList_Size(source
);
973 int* temp
= new int[count
];
975 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
978 for (int x
=0; x
<count
; x
++) {
979 PyObject
* o
= PyList_GetItem(source
, x
);
980 if (! PyInt_Check(o
)) {
981 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
984 temp
[x
] = PyInt_AsLong(o
);
990 long* long_LIST_helper(PyObject
* source
) {
991 if (!PyList_Check(source
)) {
992 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
995 int count
= PyList_Size(source
);
996 long* temp
= new long[count
];
998 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1001 for (int x
=0; x
<count
; x
++) {
1002 PyObject
* o
= PyList_GetItem(source
, x
);
1003 if (! PyInt_Check(o
)) {
1004 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1007 temp
[x
] = PyInt_AsLong(o
);
1013 char** string_LIST_helper(PyObject
* source
) {
1014 if (!PyList_Check(source
)) {
1015 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1018 int count
= PyList_Size(source
);
1019 char** temp
= new char*[count
];
1021 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1024 for (int x
=0; x
<count
; x
++) {
1025 PyObject
* o
= PyList_GetItem(source
, x
);
1026 if (! PyString_Check(o
)) {
1027 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1030 temp
[x
] = PyString_AsString(o
);
1035 //--------------------------------
1036 // Part of patch from Tim Hochberg
1037 static inline bool wxPointFromObjects(PyObject
* o1
, PyObject
* o2
, wxPoint
* point
) {
1038 if (PyInt_Check(o1
) && PyInt_Check(o2
)) {
1039 point
->x
= PyInt_AS_LONG(o1
);
1040 point
->y
= PyInt_AS_LONG(o2
);
1043 if (PyFloat_Check(o1
) && PyFloat_Check(o2
)) {
1044 point
->x
= (int)PyFloat_AS_DOUBLE(o1
);
1045 point
->y
= (int)PyFloat_AS_DOUBLE(o2
);
1048 if (PyInstance_Check(o1
) || PyInstance_Check(o2
)) {
1049 // Disallow instances because they can cause havok
1052 if (PyNumber_Check(o1
) && PyNumber_Check(o2
)) {
1053 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
1054 point
->x
= PyInt_AsLong(o1
);
1055 point
->y
= PyInt_AsLong(o2
);
1062 wxPoint
* wxPoint_LIST_helper(PyObject
* source
, int *count
) {
1063 // Putting all of the declarations here allows
1064 // us to put the error handling all in one place.
1067 PyObject
*o
, *o1
, *o2
;
1068 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1070 if (!PySequence_Check(source
)) {
1074 // The length of the sequence is returned in count.
1075 *count
= PySequence_Length(source
);
1080 temp
= new wxPoint
[*count
];
1082 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1085 for (x
=0; x
<*count
; x
++) {
1086 // Get an item: try fast way first.
1088 o
= PySequence_Fast_GET_ITEM(source
, x
);
1091 o
= PySequence_GetItem(source
, x
);
1097 // Convert o to wxPoint.
1098 if ((PyTuple_Check(o
) && PyTuple_GET_SIZE(o
) == 2) ||
1099 (PyList_Check(o
) && PyList_GET_SIZE(o
) == 2)) {
1100 o1
= PySequence_Fast_GET_ITEM(o
, 0);
1101 o2
= PySequence_Fast_GET_ITEM(o
, 1);
1102 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
1106 else if (PyInstance_Check(o
)) {
1108 if (SWIG_GetPtrObj(o
, (void **)&pt
, "_wxPoint_p")) {
1113 else if (PySequence_Check(o
) && PySequence_Length(o
) == 2) {
1114 o1
= PySequence_GetItem(o
, 0);
1115 o2
= PySequence_GetItem(o
, 1);
1116 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
1140 PyErr_SetString(PyExc_TypeError
, "Expected a sequence of length-2 sequences or wxPoints.");
1144 //------------------------------
1147 wxBitmap
** wxBitmap_LIST_helper(PyObject
* source
) {
1148 if (!PyList_Check(source
)) {
1149 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1152 int count
= PyList_Size(source
);
1153 wxBitmap
** temp
= new wxBitmap
*[count
];
1155 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1158 for (int x
=0; x
<count
; x
++) {
1159 PyObject
* o
= PyList_GetItem(source
, x
);
1160 if (PyInstance_Check(o
)) {
1162 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxBitmap_p")) {
1163 PyErr_SetString(PyExc_TypeError
,"Expected _wxBitmap_p.");
1169 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxBitmaps.");
1178 wxString
* wxString_LIST_helper(PyObject
* source
) {
1179 if (!PyList_Check(source
)) {
1180 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1183 int count
= PyList_Size(source
);
1184 wxString
* temp
= new wxString
[count
];
1186 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1189 for (int x
=0; x
<count
; x
++) {
1190 PyObject
* o
= PyList_GetItem(source
, x
);
1191 #if PYTHON_API_VERSION >= 1009
1192 if (! PyString_Check(o
) && ! PyUnicode_Check(o
)) {
1193 PyErr_SetString(PyExc_TypeError
, "Expected a list of string or unicode objects.");
1199 if (PyString_AsStringAndSize(o
, &buff
, &length
) == -1)
1201 temp
[x
] = wxString(buff
, length
);
1203 if (! PyString_Check(o
)) {
1204 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1207 temp
[x
] = PyString_AsString(o
);
1214 wxAcceleratorEntry
* wxAcceleratorEntry_LIST_helper(PyObject
* source
) {
1215 if (!PyList_Check(source
)) {
1216 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1219 int count
= PyList_Size(source
);
1220 wxAcceleratorEntry
* temp
= new wxAcceleratorEntry
[count
];
1222 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1225 for (int x
=0; x
<count
; x
++) {
1226 PyObject
* o
= PyList_GetItem(source
, x
);
1227 if (PyInstance_Check(o
)) {
1228 wxAcceleratorEntry
* ae
;
1229 if (SWIG_GetPtrObj(o
, (void **) &ae
,"_wxAcceleratorEntry_p")) {
1230 PyErr_SetString(PyExc_TypeError
,"Expected _wxAcceleratorEntry_p.");
1235 else if (PyTuple_Check(o
)) {
1236 PyObject
* o1
= PyTuple_GetItem(o
, 0);
1237 PyObject
* o2
= PyTuple_GetItem(o
, 1);
1238 PyObject
* o3
= PyTuple_GetItem(o
, 2);
1239 temp
[x
].Set(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
));
1242 PyErr_SetString(PyExc_TypeError
, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
1250 wxPen
** wxPen_LIST_helper(PyObject
* source
) {
1251 if (!PyList_Check(source
)) {
1252 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1255 int count
= PyList_Size(source
);
1256 wxPen
** temp
= new wxPen
*[count
];
1258 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1261 for (int x
=0; x
<count
; x
++) {
1262 PyObject
* o
= PyList_GetItem(source
, x
);
1263 if (PyInstance_Check(o
)) {
1265 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxPen_p")) {
1267 PyErr_SetString(PyExc_TypeError
,"Expected _wxPen_p.");
1274 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxPens.");
1282 bool _2int_seq_helper(PyObject
* source
, int* i1
, int* i2
) {
1283 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1286 if (!PySequence_Check(source
) || PySequence_Length(source
) != 2)
1290 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1291 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1294 o1
= PySequence_GetItem(source
, 0);
1295 o2
= PySequence_GetItem(source
, 1);
1298 *i1
= PyInt_AsLong(o1
);
1299 *i2
= PyInt_AsLong(o2
);
1309 bool _4int_seq_helper(PyObject
* source
, int* i1
, int* i2
, int* i3
, int* i4
) {
1310 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1311 PyObject
*o1
, *o2
, *o3
, *o4
;
1313 if (!PySequence_Check(source
) || PySequence_Length(source
) != 4)
1317 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1318 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1319 o3
= PySequence_Fast_GET_ITEM(source
, 2);
1320 o4
= PySequence_Fast_GET_ITEM(source
, 3);
1323 o1
= PySequence_GetItem(source
, 0);
1324 o2
= PySequence_GetItem(source
, 1);
1325 o3
= PySequence_GetItem(source
, 2);
1326 o4
= PySequence_GetItem(source
, 3);
1329 *i1
= PyInt_AsLong(o1
);
1330 *i2
= PyInt_AsLong(o2
);
1331 *i3
= PyInt_AsLong(o3
);
1332 *i4
= PyInt_AsLong(o4
);
1344 //----------------------------------------------------------------------
1346 bool wxSize_helper(PyObject
* source
, wxSize
** obj
) {
1348 // If source is an object instance then it may already be the right type
1349 if (PyInstance_Check(source
)) {
1351 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxSize_p"))
1356 // otherwise a 2-tuple of integers is expected
1357 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1358 PyObject
* o1
= PySequence_GetItem(source
, 0);
1359 PyObject
* o2
= PySequence_GetItem(source
, 1);
1360 **obj
= wxSize(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1365 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxSize object.");
1369 bool wxPoint_helper(PyObject
* source
, wxPoint
** obj
) {
1371 // If source is an object instance then it may already be the right type
1372 if (PyInstance_Check(source
)) {
1374 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxPoint_p"))
1379 // otherwise a length-2 sequence of integers is expected
1380 if (PySequence_Check(source
) && PySequence_Length(source
) == 2) {
1381 PyObject
* o1
= PySequence_GetItem(source
, 0);
1382 PyObject
* o2
= PySequence_GetItem(source
, 1);
1383 // This should really check for integers, not numbers -- but that would break code.
1384 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1389 **obj
= wxPoint(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1395 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxPoint object.");
1401 bool wxRealPoint_helper(PyObject
* source
, wxRealPoint
** obj
) {
1403 // If source is an object instance then it may already be the right type
1404 if (PyInstance_Check(source
)) {
1406 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRealPoint_p"))
1411 // otherwise a 2-tuple of floats is expected
1412 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1413 PyObject
* o1
= PySequence_GetItem(source
, 0);
1414 PyObject
* o2
= PySequence_GetItem(source
, 1);
1415 **obj
= wxRealPoint(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
));
1420 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxRealPoint object.");
1427 bool wxRect_helper(PyObject
* source
, wxRect
** obj
) {
1429 // If source is an object instance then it may already be the right type
1430 if (PyInstance_Check(source
)) {
1432 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRect_p"))
1437 // otherwise a 4-tuple of integers is expected
1438 else if (PySequence_Check(source
) && PyObject_Length(source
) == 4) {
1439 PyObject
* o1
= PySequence_GetItem(source
, 0);
1440 PyObject
* o2
= PySequence_GetItem(source
, 1);
1441 PyObject
* o3
= PySequence_GetItem(source
, 2);
1442 PyObject
* o4
= PySequence_GetItem(source
, 3);
1443 **obj
= wxRect(PyInt_AsLong(o1
), PyInt_AsLong(o2
),
1444 PyInt_AsLong(o3
), PyInt_AsLong(o4
));
1449 PyErr_SetString(PyExc_TypeError
, "Expected a 4-tuple of integers or a wxRect object.");
1455 bool wxColour_helper(PyObject
* source
, wxColour
** obj
) {
1457 // If source is an object instance then it may already be the right type
1458 if (PyInstance_Check(source
)) {
1460 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxColour_p"))
1465 // otherwise a string is expected
1466 else if (PyString_Check(source
)) {
1467 wxString spec
= PyString_AS_STRING(source
);
1468 if (spec
[0U] == '#' && spec
.Length() == 7) { // It's #RRGGBB
1470 int red
= strtol(spec
.Mid(1,2), &junk
, 16);
1471 int green
= strtol(spec
.Mid(3,2), &junk
, 16);
1472 int blue
= strtol(spec
.Mid(5,2), &junk
, 16);
1473 **obj
= wxColour(red
, green
, blue
);
1476 else { // it's a colour name
1477 **obj
= wxColour(spec
);
1483 PyErr_SetString(PyExc_TypeError
, "Expected a wxColour object or a string containing a colour name or '#RRGGBB'.");
1488 //----------------------------------------------------------------------
1490 PyObject
* wxArrayString2PyList_helper(const wxArrayString
& arr
) {
1492 PyObject
* list
= PyList_New(0);
1493 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
1494 PyObject
* str
= PyString_FromString(arr
[i
].c_str());
1495 PyList_Append(list
, str
);
1502 PyObject
* wxArrayInt2PyList_helper(const wxArrayInt
& arr
) {
1504 PyObject
* list
= PyList_New(0);
1505 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
1506 PyObject
* number
= PyInt_FromLong(arr
[i
]);
1507 PyList_Append(list
, number
);
1514 //----------------------------------------------------------------------
1515 //----------------------------------------------------------------------