]>
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>
26 #include <gdk/gdkprivate.h>
27 #include <wx/gtk/win_gtk.h>
33 #ifdef __WXMSW__ // If building for win32...
34 //----------------------------------------------------------------------
35 // This gets run when the DLL is loaded. We just need to save a handle.
36 //----------------------------------------------------------------------
39 HINSTANCE hinstDLL
, // handle to DLL module
40 DWORD fdwReason
, // reason for calling function
41 LPVOID lpvReserved
// reserved
44 wxSetInstance(hinstDLL
);
49 //----------------------------------------------------------------------
50 // Class for implementing the wxp main application shell.
51 //----------------------------------------------------------------------
53 wxPyApp
*wxPythonApp
= NULL
; // Global instance of application object
57 // printf("**** ctor\n");
61 // printf("**** dtor\n");
65 // This one isn't acutally called... See __wxStart()
66 bool wxPyApp::OnInit(void) {
70 int wxPyApp::MainLoop(void) {
73 DeletePendingObjects();
75 m_initialized
= wxTopLevelWindows
.GetCount() != 0;
79 retval
= wxApp::MainLoop();
80 wxPythonApp
->OnExit();
86 //---------------------------------------------------------------------
87 //----------------------------------------------------------------------
90 #include "wx/msw/msvcrt.h"
94 int WXDLLEXPORT
wxEntryStart( int argc
, char** argv
);
95 int WXDLLEXPORT
wxEntryInitGui();
96 void WXDLLEXPORT
wxEntryCleanup();
99 #ifdef WXP_WITH_THREAD
100 PyInterpreterState
* wxPyInterpreter
= NULL
;
104 // This is where we pick up the first part of the wxEntry functionality...
105 // The rest is in __wxStart and __wxCleanup. This function is called when
106 // wxcmodule is imported. (Before there is a wxApp object.)
111 // wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
114 #ifdef WXP_WITH_THREAD
115 PyEval_InitThreads();
116 wxPyInterpreter
= PyThreadState_Get()->interp
;
119 // Bail out if there is already windows created. This means that the
120 // toolkit has already been initialized, as in embedding wxPython in
121 // a C++ wxWindows app.
122 if (wxTopLevelWindows
.Number() > 0)
128 PyObject
* sysargv
= PySys_GetObject("argv");
129 if (sysargv
!= NULL
) {
130 argc
= PyList_Size(sysargv
);
131 argv
= new char*[argc
+1];
133 for(x
=0; x
<argc
; x
++)
134 argv
[x
] = copystring(PyString_AsString(PyList_GetItem(sysargv
, x
)));
138 wxEntryStart(argc
, argv
);
144 // Start the user application, user App's OnInit method is a parameter here
145 PyObject
* __wxStart(PyObject
* /* self */, PyObject
* args
)
147 PyObject
* onInitFunc
= NULL
;
152 if (!PyArg_ParseTuple(args
, "O", &onInitFunc
))
155 #if 0 // Try it out without this check, see how it does...
156 if (wxTopLevelWindows
.Number() > 0) {
157 PyErr_SetString(PyExc_TypeError
, "Only 1 wxApp per process!");
162 // This is the next part of the wxEntry functionality...
165 PyObject
* sysargv
= PySys_GetObject("argv");
166 if (sysargv
!= NULL
) {
167 argc
= PyList_Size(sysargv
);
168 argv
= new char*[argc
+1];
170 for(x
=0; x
<argc
; x
++)
171 argv
[x
] = copystring(PyString_AsString(PyList_GetItem(sysargv
, x
)));
174 wxPythonApp
->argc
= argc
;
175 wxPythonApp
->argv
= argv
;
179 // Call the Python App's OnInit function
180 arglist
= PyTuple_New(0);
181 result
= PyEval_CallObject(onInitFunc
, arglist
);
182 if (!result
) { // an exception was raised.
186 if (! PyInt_Check(result
)) {
187 PyErr_SetString(PyExc_TypeError
, "OnInit should return a boolean value");
190 bResult
= PyInt_AS_LONG(result
);
192 PyErr_SetString(PyExc_SystemExit
, "OnInit returned FALSE, exiting...");
197 wxTheApp
->m_initialized
= (wxTopLevelWindows
.GetCount() > 0);
210 static PyObject
* wxPython_dict
= NULL
;
211 static PyObject
* wxPyPtrTypeMap
= NULL
;
213 PyObject
* __wxSetDictionary(PyObject
* /* self */, PyObject
* args
)
216 if (!PyArg_ParseTuple(args
, "O", &wxPython_dict
))
219 if (!PyDict_Check(wxPython_dict
)) {
220 PyErr_SetString(PyExc_TypeError
, "_wxSetDictionary must have dictionary object!");
224 if (! wxPyPtrTypeMap
)
225 wxPyPtrTypeMap
= PyDict_New();
226 PyDict_SetItemString(wxPython_dict
, "__wxPyPtrTypeMap", wxPyPtrTypeMap
);
230 #define wxPlatform "__WXMOTIF__"
233 #define wxPlatform "__WXQT__"
236 #define wxPlatform "__WXGTK__"
238 #if defined(__WIN32__) || defined(__WXMSW__)
239 #define wxPlatform "__WXMSW__"
242 #define wxPlatform "__WXMAC__"
245 PyDict_SetItemString(wxPython_dict
, "wxPlatform", PyString_FromString(wxPlatform
));
252 //---------------------------------------------------------------------------
253 // Stuff used by OOR to find the right wxPython class type to return and to
257 // The pointer type map is used when the "pointer" type name generated by SWIG
258 // is not the same as the shadow class name, for example wxPyTreeCtrl
259 // vs. wxTreeCtrl. It needs to be referenced in Python as well as from C++,
260 // so we'll just make it a Python dictionary in the wx module's namespace.
261 void wxPyPtrTypeMap_Add(const char* commonName
, const char* ptrName
) {
262 if (! wxPyPtrTypeMap
)
263 wxPyPtrTypeMap
= PyDict_New();
265 PyDict_SetItemString(wxPyPtrTypeMap
,
267 PyString_FromString((char*)ptrName
));
272 PyObject
* wxPyClassExists(const char* className
) {
277 char buff
[64]; // should always be big enough...
279 sprintf(buff
, "%sPtr", className
);
280 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
282 return classobj
; // returns NULL if not found
286 PyObject
* wxPyMake_wxObject(wxObject
* source
, bool checkEvtHandler
) {
287 PyObject
* target
= NULL
;
288 bool isEvtHandler
= FALSE
;
291 // If it's derived from wxEvtHandler then there may
292 // already be a pointer to a Python object that we can use
294 if (checkEvtHandler
&& wxIsKindOf(source
, wxEvtHandler
)) {
296 wxEvtHandler
* eh
= (wxEvtHandler
*)source
;
297 wxPyClientData
* data
= (wxPyClientData
*)eh
->GetClientObject();
299 target
= data
->m_obj
;
305 // Otherwise make it the old fashioned way by making a
306 // new shadow object and putting this pointer in it.
307 wxClassInfo
* info
= source
->GetClassInfo();
308 wxChar
* name
= (wxChar
*)info
->GetClassName();
309 PyObject
* klass
= wxPyClassExists(name
);
310 while (info
&& !klass
) {
311 name
= (wxChar
*)info
->GetBaseClassName1();
312 info
= wxClassInfo::FindClass(name
);
313 klass
= wxPyClassExists(name
);
316 target
= wxPyConstructObject(source
, name
, klass
, FALSE
);
317 if (target
&& isEvtHandler
)
318 ((wxEvtHandler
*)source
)->SetClientObject(new wxPyClientData(target
));
320 wxString
msg("wxPython class not found for ");
321 msg
+= source
->GetClassInfo()->GetClassName();
322 PyErr_SetString(PyExc_NameError
, msg
.c_str());
326 } else { // source was NULL so return None.
327 Py_INCREF(Py_None
); target
= Py_None
;
333 PyObject
* wxPyMake_wxSizer(wxSizer
* source
) {
334 PyObject
* target
= NULL
;
336 if (source
&& wxIsKindOf(source
, wxSizer
)) {
337 // If it's derived from wxSizer then there may
338 // already be a pointer to a Python object that we can use
340 wxSizer
* sz
= (wxSizer
*)source
;
341 wxPyClientData
* data
= (wxPyClientData
*)sz
->GetClientObject();
343 target
= data
->m_obj
;
348 target
= wxPyMake_wxObject(source
, FALSE
);
349 if (target
!= Py_None
)
350 ((wxSizer
*)source
)->SetClientObject(new wxPyClientData(target
));
357 //---------------------------------------------------------------------------
359 PyObject
* wxPyConstructObject(void* ptr
,
360 const char* className
,
367 char swigptr
[64]; // should always be big enough...
370 if ((item
= PyDict_GetItemString(wxPyPtrTypeMap
, (char*)className
)) != NULL
) {
371 className
= PyString_AsString(item
);
373 sprintf(buff
, "_%s_p", className
);
374 SWIG_MakePtr(swigptr
, ptr
, buff
);
376 arg
= Py_BuildValue("(s)", swigptr
);
377 obj
= PyInstance_New(klass
, arg
, NULL
);
381 PyObject
* one
= PyInt_FromLong(1);
382 PyObject_SetAttrString(obj
, "thisown", one
);
390 PyObject
* wxPyConstructObject(void* ptr
,
391 const char* className
,
400 char buff
[64]; // should always be big enough...
401 sprintf(buff
, "%sPtr", className
);
403 wxASSERT_MSG(wxPython_dict
, "wxPython_dict is not set yet!!");
405 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
409 "*** Unknown class name %s, tell Robin about it please ***",
411 obj
= PyString_FromString(temp
);
415 return wxPyConstructObject(ptr
, className
, classobj
, setThisOwn
);
418 //---------------------------------------------------------------------------
421 wxPyTState
* wxPyBeginBlockThreads() {
422 wxPyTState
* state
= NULL
;
423 #ifdef WXP_WITH_THREAD
424 if (1) { // Can I check if I've already got the lock?
425 state
= new wxPyTState
;
426 PyEval_AcquireLock();
427 state
->newState
= PyThreadState_New(wxPyInterpreter
);
428 state
->prevState
= PyThreadState_Swap(state
->newState
);
435 void wxPyEndBlockThreads(wxPyTState
* state
) {
436 #ifdef WXP_WITH_THREAD
438 PyThreadState_Swap(state
->prevState
);
439 PyThreadState_Clear(state
->newState
);
440 PyEval_ReleaseLock();
441 PyThreadState_Delete(state
->newState
);
448 //---------------------------------------------------------------------------
450 IMPLEMENT_ABSTRACT_CLASS(wxPyCallback
, wxObject
);
452 wxPyCallback::wxPyCallback(PyObject
* func
) {
457 wxPyCallback::wxPyCallback(const wxPyCallback
& other
) {
458 m_func
= other
.m_func
;
462 wxPyCallback::~wxPyCallback() {
463 wxPyTState
* state
= wxPyBeginBlockThreads();
465 wxPyEndBlockThreads(state
);
470 // This function is used for all events destined for Python event handlers.
471 void wxPyCallback::EventThunker(wxEvent
& event
) {
472 wxPyCallback
* cb
= (wxPyCallback
*)event
.m_callbackUserData
;
473 PyObject
* func
= cb
->m_func
;
479 wxPyTState
* state
= wxPyBeginBlockThreads();
480 wxString className
= event
.GetClassInfo()->GetClassName();
482 if (className
== "wxPyEvent")
483 arg
= ((wxPyEvent
*)&event
)->GetSelf();
484 else if (className
== "wxPyCommandEvent")
485 arg
= ((wxPyCommandEvent
*)&event
)->GetSelf();
487 arg
= wxPyConstructObject((void*)&event
, className
);
489 tuple
= PyTuple_New(1);
490 PyTuple_SET_ITEM(tuple
, 0, arg
);
491 result
= PyEval_CallObject(func
, tuple
);
495 PyErr_Clear(); // Just in case...
499 wxPyEndBlockThreads(state
);
503 //----------------------------------------------------------------------
505 wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper
& other
) {
507 m_self
= other
.m_self
;
508 m_class
= other
.m_class
;
516 void wxPyCallbackHelper::setSelf(PyObject
* self
, PyObject
* klass
, int incref
) {
527 #if PYTHON_API_VERSION >= 1011
529 // Prior to Python 2.2 PyMethod_GetClass returned the class object
530 // in which the method was defined. Starting with 2.2 it returns
531 // "class that asked for the method" which seems totally bogus to me
532 // but apprently if fixes some obscure problem waiting to happen in
533 // Python. Since the API was not documented Guido and the gang felt
534 // safe in changing it. Needless to say that totally screwed up the
535 // logic below in wxPyCallbackHelper::findCallback, hence this icky
536 // code to find the class where the method is actuallt defined...
539 PyObject
* PyFindClassWithAttr(PyObject
*klass
, PyObject
*name
)
543 if (PyType_Check(klass
)) { // new style classes
544 // This code is borrowed/adapted from _PyType_Lookup in typeobject.c
545 // (TODO: This part is not tested yet, so I'm not sure it is correct...)
546 PyTypeObject
* type
= (PyTypeObject
*)klass
;
547 PyObject
*mro
, *res
, *base
, *dict
;
548 /* Look in tp_dict of types in MRO */
550 assert(PyTuple_Check(mro
));
551 n
= PyTuple_GET_SIZE(mro
);
552 for (i
= 0; i
< n
; i
++) {
553 base
= PyTuple_GET_ITEM(mro
, i
);
554 if (PyClass_Check(base
))
555 dict
= ((PyClassObject
*)base
)->cl_dict
;
557 assert(PyType_Check(base
));
558 dict
= ((PyTypeObject
*)base
)->tp_dict
;
560 assert(dict
&& PyDict_Check(dict
));
561 res
= PyDict_GetItem(dict
, name
);
568 else if (PyClass_Check(klass
)) { // old style classes
569 // This code is borrowed/adapted from class_lookup in classobject.c
570 PyClassObject
* cp
= (PyClassObject
*)klass
;
571 PyObject
*value
= PyDict_GetItem(cp
->cl_dict
, name
);
573 return (PyObject
*)cp
;
575 n
= PyTuple_Size(cp
->cl_bases
);
576 for (i
= 0; i
< n
; i
++) {
577 PyObject
* base
= PyTuple_GetItem(cp
->cl_bases
, i
);
578 PyObject
*v
= PyFindClassWithAttr(base
, name
);
589 PyObject
* PyMethod_GetDefiningClass(PyObject
* method
, const char* name
)
591 PyObject
* mgc
= PyMethod_GET_CLASS(method
);
593 #if PYTHON_API_VERSION <= 1010 // prior to Python 2.2, the easy way
595 #else // 2.2 and after, the hard way...
597 PyObject
* nameo
= PyString_FromString(name
);
598 PyObject
* klass
= PyFindClassWithAttr(mgc
, nameo
);
606 bool wxPyCallbackHelper::findCallback(const char* name
) const {
607 wxPyCallbackHelper
* self
= (wxPyCallbackHelper
*)this; // cast away const
608 self
->m_lastFound
= NULL
;
610 // If the object (m_self) has an attibute of the given name...
611 if (m_self
&& PyObject_HasAttrString(m_self
, (char*)name
)) {
612 PyObject
*method
, *klass
;
613 method
= PyObject_GetAttrString(m_self
, (char*)name
);
615 // ...and if that attribute is a method, and if that method's class is
616 // not from a base class...
617 if (PyMethod_Check(method
) &&
618 (klass
= PyMethod_GetDefiningClass(method
, (char*)name
)) != NULL
&&
619 ((klass
== m_class
) || PyClass_IsSubclass(klass
, m_class
))) {
621 // ...then we'll save a pointer to the method so callCallback can call it.
622 self
->m_lastFound
= method
;
628 return m_lastFound
!= NULL
;
632 int wxPyCallbackHelper::callCallback(PyObject
* argTuple
) const {
636 result
= callCallbackObj(argTuple
);
637 if (result
) { // Assumes an integer return type...
638 retval
= PyInt_AsLong(result
);
640 PyErr_Clear(); // forget about it if it's not...
645 // Invoke the Python callable object, returning the raw PyObject return
646 // value. Caller should DECREF the return value and also call PyEval_SaveThread.
647 PyObject
* wxPyCallbackHelper::callCallbackObj(PyObject
* argTuple
) const {
650 // Save a copy of the pointer in case the callback generates another
651 // callback. In that case m_lastFound will have a different value when
652 // it gets back here...
653 PyObject
* method
= m_lastFound
;
655 result
= PyEval_CallObject(method
, argTuple
);
665 void wxPyCBH_setCallbackInfo(wxPyCallbackHelper
& cbh
, PyObject
* self
, PyObject
* klass
, int incref
) {
666 cbh
.setSelf(self
, klass
, incref
);
669 bool wxPyCBH_findCallback(const wxPyCallbackHelper
& cbh
, const char* name
) {
670 return cbh
.findCallback(name
);
673 int wxPyCBH_callCallback(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
674 return cbh
.callCallback(argTuple
);
677 PyObject
* wxPyCBH_callCallbackObj(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
678 return cbh
.callCallbackObj(argTuple
);
682 void wxPyCBH_delete(wxPyCallbackHelper
* cbh
) {
684 wxPyTState
* state
= wxPyBeginBlockThreads();
685 Py_XDECREF(cbh
->m_self
);
686 Py_XDECREF(cbh
->m_class
);
687 wxPyEndBlockThreads(state
);
691 //---------------------------------------------------------------------------
692 //---------------------------------------------------------------------------
693 // These event classes can be derived from in Python and passed through the event
694 // system without losing anything. They do this by keeping a reference to
695 // themselves and some special case handling in wxPyCallback::EventThunker.
698 wxPyEvtSelfRef::wxPyEvtSelfRef() {
699 //m_self = Py_None; // **** We don't do normal ref counting to prevent
700 //Py_INCREF(m_self); // circular loops...
704 wxPyEvtSelfRef::~wxPyEvtSelfRef() {
705 wxPyTState
* state
= wxPyBeginBlockThreads();
708 wxPyEndBlockThreads(state
);
711 void wxPyEvtSelfRef::SetSelf(PyObject
* self
, bool clone
) {
712 wxPyTState
* state
= wxPyBeginBlockThreads();
720 wxPyEndBlockThreads(state
);
723 PyObject
* wxPyEvtSelfRef::GetSelf() const {
729 IMPLEMENT_ABSTRACT_CLASS(wxPyEvent
, wxEvent
);
730 IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent
, wxCommandEvent
);
733 wxPyEvent::wxPyEvent(int id
)
738 wxPyEvent::wxPyEvent(const wxPyEvent
& evt
)
741 SetSelf(evt
.m_self
, TRUE
);
745 wxPyEvent::~wxPyEvent() {
749 wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType
, int id
)
750 : wxCommandEvent(commandType
, id
) {
754 wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent
& evt
)
755 : wxCommandEvent(evt
)
757 SetSelf(evt
.m_self
, TRUE
);
761 wxPyCommandEvent::~wxPyCommandEvent() {
767 //---------------------------------------------------------------------------
768 //---------------------------------------------------------------------------
771 wxPyTimer::wxPyTimer(PyObject
* callback
) {
776 wxPyTimer::~wxPyTimer() {
777 wxPyTState
* state
= wxPyBeginBlockThreads();
779 wxPyEndBlockThreads(state
);
782 void wxPyTimer::Notify() {
783 if (!func
|| func
== Py_None
) {
787 wxPyTState
* state
= wxPyBeginBlockThreads();
790 PyObject
* args
= Py_BuildValue("()");
792 result
= PyEval_CallObject(func
, args
);
801 wxPyEndBlockThreads(state
);
807 //---------------------------------------------------------------------------
808 //---------------------------------------------------------------------------
809 // Convert a wxList to a Python List
811 PyObject
* wxPy_ConvertList(wxListBase
* list
, const char* className
) {
815 wxNode
* node
= list
->First();
817 wxPyTState
* state
= wxPyBeginBlockThreads();
818 pyList
= PyList_New(0);
820 wxObj
= node
->Data();
821 pyObj
= wxPyMake_wxObject(wxObj
); //wxPyConstructObject(wxObj, className);
822 PyList_Append(pyList
, pyObj
);
825 wxPyEndBlockThreads(state
);
829 //----------------------------------------------------------------------
831 long wxPyGetWinHandle(wxWindow
* win
) {
833 return (long)win
->GetHandle();
836 // Find and return the actual X-Window.
838 if (win
->m_wxwindow
) {
839 GdkWindowPrivate
* bwin
= (GdkWindowPrivate
*)GTK_PIZZA(win
->m_wxwindow
)->bin_window
;
841 return (long)bwin
->xwindow
;
848 //----------------------------------------------------------------------
849 // Some helper functions for typemaps in my_typemaps.i, so they won't be
850 // included in every file...
853 byte
* byte_LIST_helper(PyObject
* source
) {
854 if (!PyList_Check(source
)) {
855 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
858 int count
= PyList_Size(source
);
859 byte
* temp
= new byte
[count
];
861 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
864 for (int x
=0; x
<count
; x
++) {
865 PyObject
* o
= PyList_GetItem(source
, x
);
866 if (! PyInt_Check(o
)) {
867 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
870 temp
[x
] = (byte
)PyInt_AsLong(o
);
876 int* int_LIST_helper(PyObject
* source
) {
877 if (!PyList_Check(source
)) {
878 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
881 int count
= PyList_Size(source
);
882 int* temp
= new int[count
];
884 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
887 for (int x
=0; x
<count
; x
++) {
888 PyObject
* o
= PyList_GetItem(source
, x
);
889 if (! PyInt_Check(o
)) {
890 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
893 temp
[x
] = PyInt_AsLong(o
);
899 long* long_LIST_helper(PyObject
* source
) {
900 if (!PyList_Check(source
)) {
901 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
904 int count
= PyList_Size(source
);
905 long* temp
= new long[count
];
907 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
910 for (int x
=0; x
<count
; x
++) {
911 PyObject
* o
= PyList_GetItem(source
, x
);
912 if (! PyInt_Check(o
)) {
913 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
916 temp
[x
] = PyInt_AsLong(o
);
922 char** string_LIST_helper(PyObject
* source
) {
923 if (!PyList_Check(source
)) {
924 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
927 int count
= PyList_Size(source
);
928 char** temp
= new char*[count
];
930 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
933 for (int x
=0; x
<count
; x
++) {
934 PyObject
* o
= PyList_GetItem(source
, x
);
935 if (! PyString_Check(o
)) {
936 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
939 temp
[x
] = PyString_AsString(o
);
944 //--------------------------------
945 // Part of patch from Tim Hochberg
946 static inline bool wxPointFromObjects(PyObject
* o1
, PyObject
* o2
, wxPoint
* point
) {
947 if (PyInt_Check(o1
) && PyInt_Check(o2
)) {
948 point
->x
= PyInt_AS_LONG(o1
);
949 point
->y
= PyInt_AS_LONG(o2
);
952 if (PyFloat_Check(o1
) && PyFloat_Check(o2
)) {
953 point
->x
= (int)PyFloat_AS_DOUBLE(o1
);
954 point
->y
= (int)PyFloat_AS_DOUBLE(o2
);
957 if (PyInstance_Check(o1
) || PyInstance_Check(o2
)) {
958 // Disallow instances because they can cause havok
961 if (PyNumber_Check(o1
) && PyNumber_Check(o2
)) {
962 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
963 point
->x
= PyInt_AsLong(o1
);
964 point
->y
= PyInt_AsLong(o2
);
971 wxPoint
* wxPoint_LIST_helper(PyObject
* source
, int *count
) {
972 // Putting all of the declarations here allows
973 // us to put the error handling all in one place.
976 PyObject
*o
, *o1
, *o2
;
977 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
979 if (!PySequence_Check(source
)) {
983 // The length of the sequence is returned in count.
984 *count
= PySequence_Length(source
);
989 temp
= new wxPoint
[*count
];
991 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
994 for (x
=0; x
<*count
; x
++) {
995 // Get an item: try fast way first.
997 o
= PySequence_Fast_GET_ITEM(source
, x
);
1000 o
= PySequence_GetItem(source
, x
);
1006 // Convert o to wxPoint.
1007 if ((PyTuple_Check(o
) && PyTuple_GET_SIZE(o
) == 2) ||
1008 (PyList_Check(o
) && PyList_GET_SIZE(o
) == 2)) {
1009 o1
= PySequence_Fast_GET_ITEM(o
, 0);
1010 o2
= PySequence_Fast_GET_ITEM(o
, 1);
1011 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
1015 else if (PyInstance_Check(o
)) {
1017 if (SWIG_GetPtrObj(o
, (void **)&pt
, "_wxPoint_p")) {
1022 else if (PySequence_Check(o
) && PySequence_Length(o
) == 2) {
1023 o1
= PySequence_GetItem(o
, 0);
1024 o2
= PySequence_GetItem(o
, 1);
1025 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
1049 PyErr_SetString(PyExc_TypeError
, "Expected a sequence of length-2 sequences or wxPoints.");
1053 //------------------------------
1056 wxBitmap
** wxBitmap_LIST_helper(PyObject
* source
) {
1057 if (!PyList_Check(source
)) {
1058 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1061 int count
= PyList_Size(source
);
1062 wxBitmap
** temp
= new wxBitmap
*[count
];
1064 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1067 for (int x
=0; x
<count
; x
++) {
1068 PyObject
* o
= PyList_GetItem(source
, x
);
1069 if (PyInstance_Check(o
)) {
1071 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxBitmap_p")) {
1072 PyErr_SetString(PyExc_TypeError
,"Expected _wxBitmap_p.");
1078 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxBitmaps.");
1087 wxString
* wxString_LIST_helper(PyObject
* source
) {
1088 if (!PyList_Check(source
)) {
1089 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1092 int count
= PyList_Size(source
);
1093 wxString
* temp
= new wxString
[count
];
1095 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1098 for (int x
=0; x
<count
; x
++) {
1099 PyObject
* o
= PyList_GetItem(source
, x
);
1100 #if PYTHON_API_VERSION >= 1009
1101 if (! PyString_Check(o
) && ! PyUnicode_Check(o
)) {
1102 PyErr_SetString(PyExc_TypeError
, "Expected a list of string or unicode objects.");
1108 if (PyString_AsStringAndSize(o
, &buff
, &length
) == -1)
1110 temp
[x
] = wxString(buff
, length
);
1112 if (! PyString_Check(o
)) {
1113 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1116 temp
[x
] = PyString_AsString(o
);
1123 wxAcceleratorEntry
* wxAcceleratorEntry_LIST_helper(PyObject
* source
) {
1124 if (!PyList_Check(source
)) {
1125 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1128 int count
= PyList_Size(source
);
1129 wxAcceleratorEntry
* temp
= new wxAcceleratorEntry
[count
];
1131 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1134 for (int x
=0; x
<count
; x
++) {
1135 PyObject
* o
= PyList_GetItem(source
, x
);
1136 if (PyInstance_Check(o
)) {
1137 wxAcceleratorEntry
* ae
;
1138 if (SWIG_GetPtrObj(o
, (void **) &ae
,"_wxAcceleratorEntry_p")) {
1139 PyErr_SetString(PyExc_TypeError
,"Expected _wxAcceleratorEntry_p.");
1144 else if (PyTuple_Check(o
)) {
1145 PyObject
* o1
= PyTuple_GetItem(o
, 0);
1146 PyObject
* o2
= PyTuple_GetItem(o
, 1);
1147 PyObject
* o3
= PyTuple_GetItem(o
, 2);
1148 temp
[x
].Set(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
));
1151 PyErr_SetString(PyExc_TypeError
, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
1159 wxPen
** wxPen_LIST_helper(PyObject
* source
) {
1160 if (!PyList_Check(source
)) {
1161 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1164 int count
= PyList_Size(source
);
1165 wxPen
** temp
= new wxPen
*[count
];
1167 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1170 for (int x
=0; x
<count
; x
++) {
1171 PyObject
* o
= PyList_GetItem(source
, x
);
1172 if (PyInstance_Check(o
)) {
1174 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxPen_p")) {
1176 PyErr_SetString(PyExc_TypeError
,"Expected _wxPen_p.");
1183 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxPens.");
1191 bool _2int_seq_helper(PyObject
* source
, int* i1
, int* i2
) {
1192 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1195 if (!PySequence_Check(source
) || PySequence_Length(source
) != 2)
1199 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1200 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1203 o1
= PySequence_GetItem(source
, 0);
1204 o2
= PySequence_GetItem(source
, 1);
1207 *i1
= PyInt_AsLong(o1
);
1208 *i2
= PyInt_AsLong(o2
);
1218 bool _4int_seq_helper(PyObject
* source
, int* i1
, int* i2
, int* i3
, int* i4
) {
1219 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1220 PyObject
*o1
, *o2
, *o3
, *o4
;
1222 if (!PySequence_Check(source
) || PySequence_Length(source
) != 4)
1226 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1227 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1228 o3
= PySequence_Fast_GET_ITEM(source
, 2);
1229 o4
= PySequence_Fast_GET_ITEM(source
, 3);
1232 o1
= PySequence_GetItem(source
, 0);
1233 o2
= PySequence_GetItem(source
, 1);
1234 o3
= PySequence_GetItem(source
, 2);
1235 o4
= PySequence_GetItem(source
, 3);
1238 *i1
= PyInt_AsLong(o1
);
1239 *i2
= PyInt_AsLong(o2
);
1240 *i3
= PyInt_AsLong(o3
);
1241 *i4
= PyInt_AsLong(o4
);
1253 //----------------------------------------------------------------------
1255 bool wxSize_helper(PyObject
* source
, wxSize
** obj
) {
1257 // If source is an object instance then it may already be the right type
1258 if (PyInstance_Check(source
)) {
1260 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxSize_p"))
1265 // otherwise a 2-tuple of integers is expected
1266 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1267 PyObject
* o1
= PySequence_GetItem(source
, 0);
1268 PyObject
* o2
= PySequence_GetItem(source
, 1);
1269 **obj
= wxSize(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1274 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxSize object.");
1278 bool wxPoint_helper(PyObject
* source
, wxPoint
** obj
) {
1280 // If source is an object instance then it may already be the right type
1281 if (PyInstance_Check(source
)) {
1283 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxPoint_p"))
1288 // otherwise a length-2 sequence of integers is expected
1289 if (PySequence_Check(source
) && PySequence_Length(source
) == 2) {
1290 PyObject
* o1
= PySequence_GetItem(source
, 0);
1291 PyObject
* o2
= PySequence_GetItem(source
, 1);
1292 // This should really check for integers, not numbers -- but that would break code.
1293 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1298 **obj
= wxPoint(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1304 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxPoint object.");
1310 bool wxRealPoint_helper(PyObject
* source
, wxRealPoint
** obj
) {
1312 // If source is an object instance then it may already be the right type
1313 if (PyInstance_Check(source
)) {
1315 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRealPoint_p"))
1320 // otherwise a 2-tuple of floats is expected
1321 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1322 PyObject
* o1
= PySequence_GetItem(source
, 0);
1323 PyObject
* o2
= PySequence_GetItem(source
, 1);
1324 **obj
= wxRealPoint(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
));
1329 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxRealPoint object.");
1336 bool wxRect_helper(PyObject
* source
, wxRect
** obj
) {
1338 // If source is an object instance then it may already be the right type
1339 if (PyInstance_Check(source
)) {
1341 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRect_p"))
1346 // otherwise a 4-tuple of integers is expected
1347 else if (PySequence_Check(source
) && PyObject_Length(source
) == 4) {
1348 PyObject
* o1
= PySequence_GetItem(source
, 0);
1349 PyObject
* o2
= PySequence_GetItem(source
, 1);
1350 PyObject
* o3
= PySequence_GetItem(source
, 2);
1351 PyObject
* o4
= PySequence_GetItem(source
, 3);
1352 **obj
= wxRect(PyInt_AsLong(o1
), PyInt_AsLong(o2
),
1353 PyInt_AsLong(o3
), PyInt_AsLong(o4
));
1358 PyErr_SetString(PyExc_TypeError
, "Expected a 4-tuple of integers or a wxRect object.");
1364 bool wxColour_helper(PyObject
* source
, wxColour
** obj
) {
1366 // If source is an object instance then it may already be the right type
1367 if (PyInstance_Check(source
)) {
1369 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxColour_p"))
1374 // otherwise a string is expected
1375 else if (PyString_Check(source
)) {
1376 wxString spec
= PyString_AS_STRING(source
);
1377 if (spec
[0U] == '#' && spec
.Length() == 7) { // It's #RRGGBB
1379 int red
= strtol(spec
.Mid(1,2), &junk
, 16);
1380 int green
= strtol(spec
.Mid(3,2), &junk
, 16);
1381 int blue
= strtol(spec
.Mid(5,2), &junk
, 16);
1382 **obj
= wxColour(red
, green
, blue
);
1385 else { // it's a colour name
1386 **obj
= wxColour(spec
);
1392 PyErr_SetString(PyExc_TypeError
, "Expected a wxColour object or a string containing a colour name or '#RRGGBB'.");
1397 //----------------------------------------------------------------------
1399 PyObject
* wxArrayString2PyList_helper(const wxArrayString
& arr
) {
1401 PyObject
* list
= PyList_New(0);
1402 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
1403 PyObject
* str
= PyString_FromString(arr
[i
].c_str());
1404 PyList_Append(list
, str
);
1411 //----------------------------------------------------------------------
1412 //----------------------------------------------------------------------