]>
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 //---------------------------------------------------------------------------
444 // TODO: This should really be wxThread::GetCurrentId(), and I will do so
445 // after I make a quick 2.3.2.1 release.
446 #ifdef WXP_WITH_THREAD
447 #ifdef __WXGTK__ // does wxGTK always use pthreads?
452 unsigned long wxPyGetCurrentThreadId() {
454 return (unsigned long)::GetCurrentThreadId();
456 #ifdef __WXGTK__ // does wxGTK always use pthreads?
457 return (unsigned long)pthread_self();
461 MacGetCurrentThread( ¤t
) ;
462 return (unsigned long)current
;
469 PyThreadState
* wxPyGetThreadState() {
470 unsigned long ctid
= wxPyGetCurrentThreadId();
471 PyThreadState
* tstate
= NULL
;
474 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
475 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
476 if (info
.tid
== ctid
) {
477 tstate
= info
.tstate
;
481 wxPyTMutex
->Unlock();
482 wxASSERT_MSG(tstate
, "PyThreadState should not be NULL!");
487 void wxPySaveThreadState(PyThreadState
* tstate
) {
488 unsigned long ctid
= wxPyGetCurrentThreadId();
490 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
491 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
492 if (info
.tid
== ctid
) {
493 info
.tstate
= tstate
;
494 wxPyTMutex
->Unlock();
498 // not found, so add it...
499 wxPyTStates
->Add(new wxPyThreadState(ctid
, tstate
));
500 wxPyTMutex
->Unlock();
506 // Calls from Python to wxWindows code are wrapped in calls to these
509 PyThreadState
* wxPyBeginAllowThreads() {
510 #ifdef WXP_WITH_THREAD
511 PyThreadState
* saved
= PyEval_SaveThread(); // Py_BEGIN_ALLOW_THREADS;
512 wxPySaveThreadState(saved
);
519 void wxPyEndAllowThreads(PyThreadState
* saved
) {
520 #ifdef WXP_WITH_THREAD
521 PyEval_RestoreThread(saved
); // Py_END_ALLOW_THREADS;
527 // Calls from wxWindows back to Python code, or even any PyObject
528 // manipulations, PyDECREF's and etc. are wrapped in calls to these functions:
530 void wxPyBeginBlockThreads() {
531 #ifdef WXP_WITH_THREAD
532 PyThreadState
* tstate
= wxPyGetThreadState();
533 PyEval_RestoreThread(tstate
);
538 void wxPyEndBlockThreads() {
539 #ifdef WXP_WITH_THREAD
540 PyThreadState
* tstate
= PyEval_SaveThread();
541 // Is there any need to save it again?
546 //---------------------------------------------------------------------------
548 IMPLEMENT_ABSTRACT_CLASS(wxPyCallback
, wxObject
);
550 wxPyCallback::wxPyCallback(PyObject
* func
) {
555 wxPyCallback::wxPyCallback(const wxPyCallback
& other
) {
556 m_func
= other
.m_func
;
560 wxPyCallback::~wxPyCallback() {
561 wxPyBeginBlockThreads();
563 wxPyEndBlockThreads();
568 // This function is used for all events destined for Python event handlers.
569 void wxPyCallback::EventThunker(wxEvent
& event
) {
570 wxPyCallback
* cb
= (wxPyCallback
*)event
.m_callbackUserData
;
571 PyObject
* func
= cb
->m_func
;
577 wxPyBeginBlockThreads();
578 wxString className
= event
.GetClassInfo()->GetClassName();
580 if (className
== "wxPyEvent")
581 arg
= ((wxPyEvent
*)&event
)->GetSelf();
582 else if (className
== "wxPyCommandEvent")
583 arg
= ((wxPyCommandEvent
*)&event
)->GetSelf();
585 arg
= wxPyConstructObject((void*)&event
, className
);
587 tuple
= PyTuple_New(1);
588 PyTuple_SET_ITEM(tuple
, 0, arg
);
589 result
= PyEval_CallObject(func
, tuple
);
593 PyErr_Clear(); // Just in case...
597 wxPyEndBlockThreads();
601 //----------------------------------------------------------------------
603 wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper
& other
) {
605 m_self
= other
.m_self
;
606 m_class
= other
.m_class
;
614 void wxPyCallbackHelper::setSelf(PyObject
* self
, PyObject
* klass
, int incref
) {
625 #if PYTHON_API_VERSION >= 1011
627 // Prior to Python 2.2 PyMethod_GetClass returned the class object
628 // in which the method was defined. Starting with 2.2 it returns
629 // "class that asked for the method" which seems totally bogus to me
630 // but apprently if fixes some obscure problem waiting to happen in
631 // Python. Since the API was not documented Guido and the gang felt
632 // safe in changing it. Needless to say that totally screwed up the
633 // logic below in wxPyCallbackHelper::findCallback, hence this icky
634 // code to find the class where the method is actuallt defined...
637 PyObject
* PyFindClassWithAttr(PyObject
*klass
, PyObject
*name
)
641 if (PyType_Check(klass
)) { // new style classes
642 // This code is borrowed/adapted from _PyType_Lookup in typeobject.c
643 // (TODO: This part is not tested yet, so I'm not sure it is correct...)
644 PyTypeObject
* type
= (PyTypeObject
*)klass
;
645 PyObject
*mro
, *res
, *base
, *dict
;
646 /* Look in tp_dict of types in MRO */
648 assert(PyTuple_Check(mro
));
649 n
= PyTuple_GET_SIZE(mro
);
650 for (i
= 0; i
< n
; i
++) {
651 base
= PyTuple_GET_ITEM(mro
, i
);
652 if (PyClass_Check(base
))
653 dict
= ((PyClassObject
*)base
)->cl_dict
;
655 assert(PyType_Check(base
));
656 dict
= ((PyTypeObject
*)base
)->tp_dict
;
658 assert(dict
&& PyDict_Check(dict
));
659 res
= PyDict_GetItem(dict
, name
);
666 else if (PyClass_Check(klass
)) { // old style classes
667 // This code is borrowed/adapted from class_lookup in classobject.c
668 PyClassObject
* cp
= (PyClassObject
*)klass
;
669 PyObject
*value
= PyDict_GetItem(cp
->cl_dict
, name
);
671 return (PyObject
*)cp
;
673 n
= PyTuple_Size(cp
->cl_bases
);
674 for (i
= 0; i
< n
; i
++) {
675 PyObject
* base
= PyTuple_GetItem(cp
->cl_bases
, i
);
676 PyObject
*v
= PyFindClassWithAttr(base
, name
);
687 PyObject
* PyMethod_GetDefiningClass(PyObject
* method
, const char* name
)
689 PyObject
* mgc
= PyMethod_GET_CLASS(method
);
691 #if PYTHON_API_VERSION <= 1010 // prior to Python 2.2, the easy way
693 #else // 2.2 and after, the hard way...
695 PyObject
* nameo
= PyString_FromString(name
);
696 PyObject
* klass
= PyFindClassWithAttr(mgc
, nameo
);
704 bool wxPyCallbackHelper::findCallback(const char* name
) const {
705 wxPyCallbackHelper
* self
= (wxPyCallbackHelper
*)this; // cast away const
706 self
->m_lastFound
= NULL
;
708 // If the object (m_self) has an attibute of the given name...
709 if (m_self
&& PyObject_HasAttrString(m_self
, (char*)name
)) {
710 PyObject
*method
, *klass
;
711 method
= PyObject_GetAttrString(m_self
, (char*)name
);
713 // ...and if that attribute is a method, and if that method's class is
714 // not from a base class...
715 if (PyMethod_Check(method
) &&
716 (klass
= PyMethod_GetDefiningClass(method
, (char*)name
)) != NULL
&&
717 ((klass
== m_class
) || PyClass_IsSubclass(klass
, m_class
))) {
719 // ...then we'll save a pointer to the method so callCallback can call it.
720 self
->m_lastFound
= method
;
726 return m_lastFound
!= NULL
;
730 int wxPyCallbackHelper::callCallback(PyObject
* argTuple
) const {
734 result
= callCallbackObj(argTuple
);
735 if (result
) { // Assumes an integer return type...
736 retval
= PyInt_AsLong(result
);
738 PyErr_Clear(); // forget about it if it's not...
743 // Invoke the Python callable object, returning the raw PyObject return
744 // value. Caller should DECREF the return value and also call PyEval_SaveThread.
745 PyObject
* wxPyCallbackHelper::callCallbackObj(PyObject
* argTuple
) const {
748 // Save a copy of the pointer in case the callback generates another
749 // callback. In that case m_lastFound will have a different value when
750 // it gets back here...
751 PyObject
* method
= m_lastFound
;
753 result
= PyEval_CallObject(method
, argTuple
);
763 void wxPyCBH_setCallbackInfo(wxPyCallbackHelper
& cbh
, PyObject
* self
, PyObject
* klass
, int incref
) {
764 cbh
.setSelf(self
, klass
, incref
);
767 bool wxPyCBH_findCallback(const wxPyCallbackHelper
& cbh
, const char* name
) {
768 return cbh
.findCallback(name
);
771 int wxPyCBH_callCallback(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
772 return cbh
.callCallback(argTuple
);
775 PyObject
* wxPyCBH_callCallbackObj(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
776 return cbh
.callCallbackObj(argTuple
);
780 void wxPyCBH_delete(wxPyCallbackHelper
* cbh
) {
782 wxPyBeginBlockThreads();
783 Py_XDECREF(cbh
->m_self
);
784 Py_XDECREF(cbh
->m_class
);
785 wxPyEndBlockThreads();
789 //---------------------------------------------------------------------------
790 //---------------------------------------------------------------------------
791 // These event classes can be derived from in Python and passed through the event
792 // system without losing anything. They do this by keeping a reference to
793 // themselves and some special case handling in wxPyCallback::EventThunker.
796 wxPyEvtSelfRef::wxPyEvtSelfRef() {
797 //m_self = Py_None; // **** We don't do normal ref counting to prevent
798 //Py_INCREF(m_self); // circular loops...
802 wxPyEvtSelfRef::~wxPyEvtSelfRef() {
803 wxPyBeginBlockThreads();
806 wxPyEndBlockThreads();
809 void wxPyEvtSelfRef::SetSelf(PyObject
* self
, bool clone
) {
810 wxPyBeginBlockThreads();
818 wxPyEndBlockThreads();
821 PyObject
* wxPyEvtSelfRef::GetSelf() const {
827 IMPLEMENT_ABSTRACT_CLASS(wxPyEvent
, wxEvent
);
828 IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent
, wxCommandEvent
);
831 wxPyEvent::wxPyEvent(int id
)
836 wxPyEvent::wxPyEvent(const wxPyEvent
& evt
)
839 SetSelf(evt
.m_self
, TRUE
);
843 wxPyEvent::~wxPyEvent() {
847 wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType
, int id
)
848 : wxCommandEvent(commandType
, id
) {
852 wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent
& evt
)
853 : wxCommandEvent(evt
)
855 SetSelf(evt
.m_self
, TRUE
);
859 wxPyCommandEvent::~wxPyCommandEvent() {
865 //---------------------------------------------------------------------------
866 //---------------------------------------------------------------------------
869 wxPyTimer::wxPyTimer(PyObject
* callback
) {
874 wxPyTimer::~wxPyTimer() {
875 wxPyBeginBlockThreads();
877 wxPyEndBlockThreads();
880 void wxPyTimer::Notify() {
881 if (!func
|| func
== Py_None
) {
885 wxPyBeginBlockThreads();
888 PyObject
* args
= Py_BuildValue("()");
890 result
= PyEval_CallObject(func
, args
);
899 wxPyEndBlockThreads();
905 //---------------------------------------------------------------------------
906 //---------------------------------------------------------------------------
907 // Convert a wxList to a Python List
909 PyObject
* wxPy_ConvertList(wxListBase
* list
, const char* className
) {
913 wxNode
* node
= list
->First();
915 wxPyBeginBlockThreads();
916 pyList
= PyList_New(0);
918 wxObj
= node
->Data();
919 pyObj
= wxPyMake_wxObject(wxObj
); //wxPyConstructObject(wxObj, className);
920 PyList_Append(pyList
, pyObj
);
923 wxPyEndBlockThreads();
927 //----------------------------------------------------------------------
929 long wxPyGetWinHandle(wxWindow
* win
) {
931 return (long)win
->GetHandle();
934 // Find and return the actual X-Window.
936 if (win
->m_wxwindow
) {
937 GdkWindowPrivate
* bwin
= (GdkWindowPrivate
*)GTK_PIZZA(win
->m_wxwindow
)->bin_window
;
939 return (long)bwin
->xwindow
;
946 //----------------------------------------------------------------------
947 // Some helper functions for typemaps in my_typemaps.i, so they won't be
948 // included in every file...
951 byte
* byte_LIST_helper(PyObject
* source
) {
952 if (!PyList_Check(source
)) {
953 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
956 int count
= PyList_Size(source
);
957 byte
* temp
= new byte
[count
];
959 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
962 for (int x
=0; x
<count
; x
++) {
963 PyObject
* o
= PyList_GetItem(source
, x
);
964 if (! PyInt_Check(o
)) {
965 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
968 temp
[x
] = (byte
)PyInt_AsLong(o
);
974 int* int_LIST_helper(PyObject
* source
) {
975 if (!PyList_Check(source
)) {
976 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
979 int count
= PyList_Size(source
);
980 int* temp
= new int[count
];
982 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
985 for (int x
=0; x
<count
; x
++) {
986 PyObject
* o
= PyList_GetItem(source
, x
);
987 if (! PyInt_Check(o
)) {
988 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
991 temp
[x
] = PyInt_AsLong(o
);
997 long* long_LIST_helper(PyObject
* source
) {
998 if (!PyList_Check(source
)) {
999 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1002 int count
= PyList_Size(source
);
1003 long* temp
= new long[count
];
1005 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1008 for (int x
=0; x
<count
; x
++) {
1009 PyObject
* o
= PyList_GetItem(source
, x
);
1010 if (! PyInt_Check(o
)) {
1011 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1014 temp
[x
] = PyInt_AsLong(o
);
1020 char** string_LIST_helper(PyObject
* source
) {
1021 if (!PyList_Check(source
)) {
1022 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1025 int count
= PyList_Size(source
);
1026 char** temp
= new char*[count
];
1028 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1031 for (int x
=0; x
<count
; x
++) {
1032 PyObject
* o
= PyList_GetItem(source
, x
);
1033 if (! PyString_Check(o
)) {
1034 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1037 temp
[x
] = PyString_AsString(o
);
1042 //--------------------------------
1043 // Part of patch from Tim Hochberg
1044 static inline bool wxPointFromObjects(PyObject
* o1
, PyObject
* o2
, wxPoint
* point
) {
1045 if (PyInt_Check(o1
) && PyInt_Check(o2
)) {
1046 point
->x
= PyInt_AS_LONG(o1
);
1047 point
->y
= PyInt_AS_LONG(o2
);
1050 if (PyFloat_Check(o1
) && PyFloat_Check(o2
)) {
1051 point
->x
= (int)PyFloat_AS_DOUBLE(o1
);
1052 point
->y
= (int)PyFloat_AS_DOUBLE(o2
);
1055 if (PyInstance_Check(o1
) || PyInstance_Check(o2
)) {
1056 // Disallow instances because they can cause havok
1059 if (PyNumber_Check(o1
) && PyNumber_Check(o2
)) {
1060 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
1061 point
->x
= PyInt_AsLong(o1
);
1062 point
->y
= PyInt_AsLong(o2
);
1069 wxPoint
* wxPoint_LIST_helper(PyObject
* source
, int *count
) {
1070 // Putting all of the declarations here allows
1071 // us to put the error handling all in one place.
1074 PyObject
*o
, *o1
, *o2
;
1075 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1077 if (!PySequence_Check(source
)) {
1081 // The length of the sequence is returned in count.
1082 *count
= PySequence_Length(source
);
1087 temp
= new wxPoint
[*count
];
1089 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1092 for (x
=0; x
<*count
; x
++) {
1093 // Get an item: try fast way first.
1095 o
= PySequence_Fast_GET_ITEM(source
, x
);
1098 o
= PySequence_GetItem(source
, x
);
1104 // Convert o to wxPoint.
1105 if ((PyTuple_Check(o
) && PyTuple_GET_SIZE(o
) == 2) ||
1106 (PyList_Check(o
) && PyList_GET_SIZE(o
) == 2)) {
1107 o1
= PySequence_Fast_GET_ITEM(o
, 0);
1108 o2
= PySequence_Fast_GET_ITEM(o
, 1);
1109 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
1113 else if (PyInstance_Check(o
)) {
1115 if (SWIG_GetPtrObj(o
, (void **)&pt
, "_wxPoint_p")) {
1120 else if (PySequence_Check(o
) && PySequence_Length(o
) == 2) {
1121 o1
= PySequence_GetItem(o
, 0);
1122 o2
= PySequence_GetItem(o
, 1);
1123 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
1147 PyErr_SetString(PyExc_TypeError
, "Expected a sequence of length-2 sequences or wxPoints.");
1151 //------------------------------
1154 wxBitmap
** wxBitmap_LIST_helper(PyObject
* source
) {
1155 if (!PyList_Check(source
)) {
1156 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1159 int count
= PyList_Size(source
);
1160 wxBitmap
** temp
= new wxBitmap
*[count
];
1162 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1165 for (int x
=0; x
<count
; x
++) {
1166 PyObject
* o
= PyList_GetItem(source
, x
);
1167 if (PyInstance_Check(o
)) {
1169 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxBitmap_p")) {
1170 PyErr_SetString(PyExc_TypeError
,"Expected _wxBitmap_p.");
1176 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxBitmaps.");
1185 wxString
* wxString_LIST_helper(PyObject
* source
) {
1186 if (!PyList_Check(source
)) {
1187 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1190 int count
= PyList_Size(source
);
1191 wxString
* temp
= new wxString
[count
];
1193 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1196 for (int x
=0; x
<count
; x
++) {
1197 PyObject
* o
= PyList_GetItem(source
, x
);
1198 #if PYTHON_API_VERSION >= 1009
1199 if (! PyString_Check(o
) && ! PyUnicode_Check(o
)) {
1200 PyErr_SetString(PyExc_TypeError
, "Expected a list of string or unicode objects.");
1206 if (PyString_AsStringAndSize(o
, &buff
, &length
) == -1)
1208 temp
[x
] = wxString(buff
, length
);
1210 if (! PyString_Check(o
)) {
1211 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1214 temp
[x
] = PyString_AsString(o
);
1221 wxAcceleratorEntry
* wxAcceleratorEntry_LIST_helper(PyObject
* source
) {
1222 if (!PyList_Check(source
)) {
1223 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1226 int count
= PyList_Size(source
);
1227 wxAcceleratorEntry
* temp
= new wxAcceleratorEntry
[count
];
1229 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1232 for (int x
=0; x
<count
; x
++) {
1233 PyObject
* o
= PyList_GetItem(source
, x
);
1234 if (PyInstance_Check(o
)) {
1235 wxAcceleratorEntry
* ae
;
1236 if (SWIG_GetPtrObj(o
, (void **) &ae
,"_wxAcceleratorEntry_p")) {
1237 PyErr_SetString(PyExc_TypeError
,"Expected _wxAcceleratorEntry_p.");
1242 else if (PyTuple_Check(o
)) {
1243 PyObject
* o1
= PyTuple_GetItem(o
, 0);
1244 PyObject
* o2
= PyTuple_GetItem(o
, 1);
1245 PyObject
* o3
= PyTuple_GetItem(o
, 2);
1246 temp
[x
].Set(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
));
1249 PyErr_SetString(PyExc_TypeError
, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
1257 wxPen
** wxPen_LIST_helper(PyObject
* source
) {
1258 if (!PyList_Check(source
)) {
1259 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1262 int count
= PyList_Size(source
);
1263 wxPen
** temp
= new wxPen
*[count
];
1265 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1268 for (int x
=0; x
<count
; x
++) {
1269 PyObject
* o
= PyList_GetItem(source
, x
);
1270 if (PyInstance_Check(o
)) {
1272 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxPen_p")) {
1274 PyErr_SetString(PyExc_TypeError
,"Expected _wxPen_p.");
1281 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxPens.");
1289 bool _2int_seq_helper(PyObject
* source
, int* i1
, int* i2
) {
1290 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1293 if (!PySequence_Check(source
) || PySequence_Length(source
) != 2)
1297 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1298 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1301 o1
= PySequence_GetItem(source
, 0);
1302 o2
= PySequence_GetItem(source
, 1);
1305 *i1
= PyInt_AsLong(o1
);
1306 *i2
= PyInt_AsLong(o2
);
1316 bool _4int_seq_helper(PyObject
* source
, int* i1
, int* i2
, int* i3
, int* i4
) {
1317 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1318 PyObject
*o1
, *o2
, *o3
, *o4
;
1320 if (!PySequence_Check(source
) || PySequence_Length(source
) != 4)
1324 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1325 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1326 o3
= PySequence_Fast_GET_ITEM(source
, 2);
1327 o4
= PySequence_Fast_GET_ITEM(source
, 3);
1330 o1
= PySequence_GetItem(source
, 0);
1331 o2
= PySequence_GetItem(source
, 1);
1332 o3
= PySequence_GetItem(source
, 2);
1333 o4
= PySequence_GetItem(source
, 3);
1336 *i1
= PyInt_AsLong(o1
);
1337 *i2
= PyInt_AsLong(o2
);
1338 *i3
= PyInt_AsLong(o3
);
1339 *i4
= PyInt_AsLong(o4
);
1351 //----------------------------------------------------------------------
1353 bool wxSize_helper(PyObject
* source
, wxSize
** obj
) {
1355 // If source is an object instance then it may already be the right type
1356 if (PyInstance_Check(source
)) {
1358 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxSize_p"))
1363 // otherwise a 2-tuple of integers is expected
1364 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1365 PyObject
* o1
= PySequence_GetItem(source
, 0);
1366 PyObject
* o2
= PySequence_GetItem(source
, 1);
1367 **obj
= wxSize(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1372 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxSize object.");
1376 bool wxPoint_helper(PyObject
* source
, wxPoint
** obj
) {
1378 // If source is an object instance then it may already be the right type
1379 if (PyInstance_Check(source
)) {
1381 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxPoint_p"))
1386 // otherwise a length-2 sequence of integers is expected
1387 if (PySequence_Check(source
) && PySequence_Length(source
) == 2) {
1388 PyObject
* o1
= PySequence_GetItem(source
, 0);
1389 PyObject
* o2
= PySequence_GetItem(source
, 1);
1390 // This should really check for integers, not numbers -- but that would break code.
1391 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1396 **obj
= wxPoint(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1402 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxPoint object.");
1408 bool wxRealPoint_helper(PyObject
* source
, wxRealPoint
** obj
) {
1410 // If source is an object instance then it may already be the right type
1411 if (PyInstance_Check(source
)) {
1413 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRealPoint_p"))
1418 // otherwise a 2-tuple of floats is expected
1419 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1420 PyObject
* o1
= PySequence_GetItem(source
, 0);
1421 PyObject
* o2
= PySequence_GetItem(source
, 1);
1422 **obj
= wxRealPoint(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
));
1427 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxRealPoint object.");
1434 bool wxRect_helper(PyObject
* source
, wxRect
** obj
) {
1436 // If source is an object instance then it may already be the right type
1437 if (PyInstance_Check(source
)) {
1439 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRect_p"))
1444 // otherwise a 4-tuple of integers is expected
1445 else if (PySequence_Check(source
) && PyObject_Length(source
) == 4) {
1446 PyObject
* o1
= PySequence_GetItem(source
, 0);
1447 PyObject
* o2
= PySequence_GetItem(source
, 1);
1448 PyObject
* o3
= PySequence_GetItem(source
, 2);
1449 PyObject
* o4
= PySequence_GetItem(source
, 3);
1450 **obj
= wxRect(PyInt_AsLong(o1
), PyInt_AsLong(o2
),
1451 PyInt_AsLong(o3
), PyInt_AsLong(o4
));
1456 PyErr_SetString(PyExc_TypeError
, "Expected a 4-tuple of integers or a wxRect object.");
1462 bool wxColour_helper(PyObject
* source
, wxColour
** obj
) {
1464 // If source is an object instance then it may already be the right type
1465 if (PyInstance_Check(source
)) {
1467 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxColour_p"))
1472 // otherwise a string is expected
1473 else if (PyString_Check(source
)) {
1474 wxString spec
= PyString_AS_STRING(source
);
1475 if (spec
[0U] == '#' && spec
.Length() == 7) { // It's #RRGGBB
1477 int red
= strtol(spec
.Mid(1,2), &junk
, 16);
1478 int green
= strtol(spec
.Mid(3,2), &junk
, 16);
1479 int blue
= strtol(spec
.Mid(5,2), &junk
, 16);
1480 **obj
= wxColour(red
, green
, blue
);
1483 else { // it's a colour name
1484 **obj
= wxColour(spec
);
1490 PyErr_SetString(PyExc_TypeError
, "Expected a wxColour object or a string containing a colour name or '#RRGGBB'.");
1495 //----------------------------------------------------------------------
1497 PyObject
* wxArrayString2PyList_helper(const wxArrayString
& arr
) {
1499 PyObject
* list
= PyList_New(0);
1500 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
1501 PyObject
* str
= PyString_FromString(arr
[i
].c_str());
1502 PyList_Append(list
, str
);
1509 //----------------------------------------------------------------------
1510 //----------------------------------------------------------------------