]>
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>
23 #undef LoadAccelerators
30 #include <gdk/gdkprivate.h>
31 #include <wx/gtk/win_gtk.h>
37 #ifdef __WXMSW__ // If building for win32...
38 //----------------------------------------------------------------------
39 // This gets run when the DLL is loaded. We just need to save a handle.
40 //----------------------------------------------------------------------
43 HINSTANCE hinstDLL
, // handle to DLL module
44 DWORD fdwReason
, // reason for calling function
45 LPVOID lpvReserved
// reserved
48 wxSetInstance(hinstDLL
);
53 //----------------------------------------------------------------------
54 // Class for implementing the wxp main application shell.
55 //----------------------------------------------------------------------
57 wxPyApp
*wxPythonApp
= NULL
; // Global instance of application object
61 // printf("**** ctor\n");
65 // printf("**** dtor\n");
69 // This one isn't acutally called... See __wxStart()
70 bool wxPyApp::OnInit(void) {
74 int wxPyApp::MainLoop(void) {
77 DeletePendingObjects();
79 m_initialized
= wxTopLevelWindows
.GetCount() != 0;
83 retval
= wxApp::MainLoop();
84 wxPythonApp
->OnExit();
90 //---------------------------------------------------------------------
91 //----------------------------------------------------------------------
94 #include "wx/msw/msvcrt.h"
98 int WXDLLEXPORT
wxEntryStart( int argc
, char** argv
);
99 int WXDLLEXPORT
wxEntryInitGui();
100 void WXDLLEXPORT
wxEntryCleanup();
103 #ifdef WXP_WITH_THREAD
104 //PyThreadState* wxPyEventThreadState = NULL;
105 PyInterpreterState
* wxPyInterpreter
= NULL
;
109 // This is where we pick up the first part of the wxEntry functionality...
110 // The rest is in __wxStart and __wxCleanup. This function is called when
111 // wxcmodule is imported. (Before there is a wxApp object.)
116 // wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
119 #ifdef WXP_WITH_THREAD
120 PyEval_InitThreads();
121 // wxPyEventThreadState = PyThreadState_Get(); // PyThreadState_New(PyThreadState_Get()->interp);
122 wxPyInterpreter
= PyThreadState_Get()->interp
;
125 // Bail out if there is already windows created. This means that the
126 // toolkit has already been initialized, as in embedding wxPython in
127 // a C++ wxWindows app.
128 if (wxTopLevelWindows
.Number() > 0)
134 PyObject
* sysargv
= PySys_GetObject("argv");
135 if (sysargv
!= NULL
) {
136 argc
= PyList_Size(sysargv
);
137 argv
= new char*[argc
+1];
139 for(x
=0; x
<argc
; x
++)
140 argv
[x
] = copystring(PyString_AsString(PyList_GetItem(sysargv
, x
)));
144 wxEntryStart(argc
, argv
);
150 // Start the user application, user App's OnInit method is a parameter here
151 PyObject
* __wxStart(PyObject
* /* self */, PyObject
* args
)
153 PyObject
* onInitFunc
= NULL
;
158 if (!PyArg_ParseTuple(args
, "O", &onInitFunc
))
161 #if 0 // Try it out without this check, see how it does...
162 if (wxTopLevelWindows
.Number() > 0) {
163 PyErr_SetString(PyExc_TypeError
, "Only 1 wxApp per process!");
168 // This is the next part of the wxEntry functionality...
171 PyObject
* sysargv
= PySys_GetObject("argv");
172 if (sysargv
!= NULL
) {
173 argc
= PyList_Size(sysargv
);
174 argv
= new char*[argc
+1];
176 for(x
=0; x
<argc
; x
++)
177 argv
[x
] = copystring(PyString_AsString(PyList_GetItem(sysargv
, x
)));
180 wxPythonApp
->argc
= argc
;
181 wxPythonApp
->argv
= argv
;
185 // Call the Python App's OnInit function
186 arglist
= PyTuple_New(0);
187 result
= PyEval_CallObject(onInitFunc
, arglist
);
188 if (!result
) { // an exception was raised.
192 if (! PyInt_Check(result
)) {
193 PyErr_SetString(PyExc_TypeError
, "OnInit should return a boolean value");
196 bResult
= PyInt_AS_LONG(result
);
198 PyErr_SetString(PyExc_SystemExit
, "OnInit returned FALSE, exiting...");
203 wxTheApp
->m_initialized
= (wxTopLevelWindows
.GetCount() > 0);
216 static PyObject
* wxPython_dict
= NULL
;
217 static PyObject
* wxPyPtrTypeMap
= NULL
;
219 PyObject
* __wxSetDictionary(PyObject
* /* self */, PyObject
* args
)
222 if (!PyArg_ParseTuple(args
, "O", &wxPython_dict
))
225 if (!PyDict_Check(wxPython_dict
)) {
226 PyErr_SetString(PyExc_TypeError
, "_wxSetDictionary must have dictionary object!");
230 if (! wxPyPtrTypeMap
)
231 wxPyPtrTypeMap
= PyDict_New();
232 PyDict_SetItemString(wxPython_dict
, "__wxPyPtrTypeMap", wxPyPtrTypeMap
);
236 #define wxPlatform "__WXMOTIF__"
239 #define wxPlatform "__WXQT__"
242 #define wxPlatform "__WXGTK__"
244 #if defined(__WIN32__) || defined(__WXMSW__)
245 #define wxPlatform "__WXMSW__"
248 #define wxPlatform "__WXMAC__"
251 PyDict_SetItemString(wxPython_dict
, "wxPlatform", PyString_FromString(wxPlatform
));
258 //---------------------------------------------------------------------------
259 // Stuff used by OOR to find the right wxPython class type to return and to
263 // The pointer type map is used when the "pointer" type name generated by SWIG
264 // is not the same as the shadow class name, for example wxPyTreeCtrl
265 // vs. wxTreeCtrl. It needs to be referenced in Python as well as from C++,
266 // so we'll just make it a Python dictionary in the wx module's namespace.
267 void wxPyPtrTypeMap_Add(const char* commonName
, const char* ptrName
) {
268 if (! wxPyPtrTypeMap
)
269 wxPyPtrTypeMap
= PyDict_New();
271 PyDict_SetItemString(wxPyPtrTypeMap
,
273 PyString_FromString((char*)ptrName
));
278 PyObject
* wxPyClassExists(const char* className
) {
283 char buff
[64]; // should always be big enough...
285 sprintf(buff
, "%sPtr", className
);
286 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
288 return classobj
; // returns NULL if not found
292 PyObject
* wxPyMake_wxObject(wxObject
* source
) {
293 PyObject
* target
= NULL
;
294 bool isEvtHandler
= FALSE
;
297 if (wxIsKindOf(source
, wxEvtHandler
)) {
298 wxEvtHandler
* eh
= (wxEvtHandler
*)source
;
299 wxPyClientData
* data
= (wxPyClientData
*)eh
->GetClientObject();
301 target
= data
->m_obj
;
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 //---------------------------------------------------------------------------
335 PyObject
* wxPyConstructObject(void* ptr
,
336 const char* className
,
343 char swigptr
[64]; // should always be big enough...
346 if ((item
= PyDict_GetItemString(wxPyPtrTypeMap
, (char*)className
)) != NULL
) {
347 className
= PyString_AsString(item
);
349 sprintf(buff
, "_%s_p", className
);
350 SWIG_MakePtr(swigptr
, ptr
, buff
);
352 arg
= Py_BuildValue("(s)", swigptr
);
353 obj
= PyInstance_New(klass
, arg
, NULL
);
357 PyObject
* one
= PyInt_FromLong(1);
358 PyObject_SetAttrString(obj
, "thisown", one
);
366 PyObject
* wxPyConstructObject(void* ptr
,
367 const char* className
,
376 char buff
[64]; // should always be big enough...
378 sprintf(buff
, "%sPtr", className
);
379 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
383 "*** Unknown class name %s, tell Robin about it please ***",
385 obj
= PyString_FromString(temp
);
389 return wxPyConstructObject(ptr
, className
, classobj
, setThisOwn
);
392 //---------------------------------------------------------------------------
394 // static PyThreadState* myPyThreadState_Get() {
395 // PyThreadState* current;
396 // current = PyThreadState_Swap(NULL);
397 // PyThreadState_Swap(current);
402 // bool wxPyRestoreThread() {
403 // // NOTE: The Python API docs state that if a thread already has the
404 // // interpreter lock and calls PyEval_RestoreThread again a deadlock
405 // // occurs, so I put in this code as a guard condition since there are
406 // // many possibilites for nested events and callbacks in wxPython. If
407 // // The current thread is our thread, then we can assume that we
408 // // already have the lock. (I hope!)
410 // #ifdef WXP_WITH_THREAD
411 // if (wxPyEventThreadState != myPyThreadState_Get()) {
412 // PyEval_AcquireThread(wxPyEventThreadState);
421 // void wxPySaveThread(bool doSave) {
422 // #ifdef WXP_WITH_THREAD
424 // PyEval_ReleaseThread(wxPyEventThreadState);
431 wxPyTState
* wxPyBeginBlockThreads() {
432 wxPyTState
* state
= NULL
;
433 #ifdef WXP_WITH_THREAD
434 if (1) { // Can I check if I've already got the lock?
435 state
= new wxPyTState
;
436 PyEval_AcquireLock();
437 state
->newState
= PyThreadState_New(wxPyInterpreter
);
438 state
->prevState
= PyThreadState_Swap(state
->newState
);
445 void wxPyEndBlockThreads(wxPyTState
* state
) {
446 #ifdef WXP_WITH_THREAD
448 PyThreadState_Swap(state
->prevState
);
449 PyThreadState_Clear(state
->newState
);
450 PyEval_ReleaseLock();
451 PyThreadState_Delete(state
->newState
);
458 //---------------------------------------------------------------------------
460 IMPLEMENT_ABSTRACT_CLASS(wxPyCallback
, wxObject
);
462 wxPyCallback::wxPyCallback(PyObject
* func
) {
467 wxPyCallback::wxPyCallback(const wxPyCallback
& other
) {
468 m_func
= other
.m_func
;
472 wxPyCallback::~wxPyCallback() {
473 wxPyTState
* state
= wxPyBeginBlockThreads();
475 wxPyEndBlockThreads(state
);
480 // This function is used for all events destined for Python event handlers.
481 void wxPyCallback::EventThunker(wxEvent
& event
) {
482 wxPyCallback
* cb
= (wxPyCallback
*)event
.m_callbackUserData
;
483 PyObject
* func
= cb
->m_func
;
489 wxPyTState
* state
= wxPyBeginBlockThreads();
490 wxString className
= event
.GetClassInfo()->GetClassName();
492 if (className
== "wxPyEvent")
493 arg
= ((wxPyEvent
*)&event
)->GetSelf();
494 else if (className
== "wxPyCommandEvent")
495 arg
= ((wxPyCommandEvent
*)&event
)->GetSelf();
497 arg
= wxPyConstructObject((void*)&event
, className
);
499 tuple
= PyTuple_New(1);
500 PyTuple_SET_ITEM(tuple
, 0, arg
);
501 result
= PyEval_CallObject(func
, tuple
);
505 PyErr_Clear(); // Just in case...
509 wxPyEndBlockThreads(state
);
513 //----------------------------------------------------------------------
515 wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper
& other
) {
517 m_self
= other
.m_self
;
518 m_class
= other
.m_class
;
526 void wxPyCallbackHelper::setSelf(PyObject
* self
, PyObject
* klass
, int incref
) {
537 // If the object (m_self) has an attibute of the given name, and if that
538 // attribute is a method, and if that method's class is not from a base class,
539 // then we'll save a pointer to the method so callCallback can call it.
540 bool wxPyCallbackHelper::findCallback(const char* name
) const {
541 wxPyCallbackHelper
* self
= (wxPyCallbackHelper
*)this; // cast away const
542 self
->m_lastFound
= NULL
;
543 if (m_self
&& PyObject_HasAttrString(m_self
, (char*)name
)) {
545 method
= PyObject_GetAttrString(m_self
, (char*)name
);
547 if (PyMethod_Check(method
) &&
548 ((PyMethod_GET_CLASS(method
) == m_class
) ||
549 PyClass_IsSubclass(PyMethod_GET_CLASS(method
), m_class
))) {
551 self
->m_lastFound
= method
;
557 return m_lastFound
!= NULL
;
561 int wxPyCallbackHelper::callCallback(PyObject
* argTuple
) const {
565 result
= callCallbackObj(argTuple
);
566 if (result
) { // Assumes an integer return type...
567 retval
= PyInt_AsLong(result
);
569 PyErr_Clear(); // forget about it if it's not...
574 // Invoke the Python callable object, returning the raw PyObject return
575 // value. Caller should DECREF the return value and also call PyEval_SaveThread.
576 PyObject
* wxPyCallbackHelper::callCallbackObj(PyObject
* argTuple
) const {
579 // Save a copy of the pointer in case the callback generates another
580 // callback. In that case m_lastFound will have a different value when
581 // it gets back here...
582 PyObject
* method
= m_lastFound
;
584 result
= PyEval_CallObject(method
, argTuple
);
594 void wxPyCBH_setCallbackInfo(wxPyCallbackHelper
& cbh
, PyObject
* self
, PyObject
* klass
, int incref
) {
595 cbh
.setSelf(self
, klass
, incref
);
598 bool wxPyCBH_findCallback(const wxPyCallbackHelper
& cbh
, const char* name
) {
599 return cbh
.findCallback(name
);
602 int wxPyCBH_callCallback(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
603 return cbh
.callCallback(argTuple
);
606 PyObject
* wxPyCBH_callCallbackObj(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
607 return cbh
.callCallbackObj(argTuple
);
611 void wxPyCBH_delete(wxPyCallbackHelper
* cbh
) {
613 wxPyTState
* state
= wxPyBeginBlockThreads();
614 Py_XDECREF(cbh
->m_self
);
615 Py_XDECREF(cbh
->m_class
);
616 wxPyEndBlockThreads(state
);
620 //---------------------------------------------------------------------------
621 //---------------------------------------------------------------------------
622 // These classes can be derived from in Python and passed through the event
623 // system without losing anything. They do this by keeping a reference to
624 // themselves and some special case handling in wxPyCallback::EventThunker.
627 wxPyEvtSelfRef::wxPyEvtSelfRef() {
628 //m_self = Py_None; // **** We don't do normal ref counting to prevent
629 //Py_INCREF(m_self); // circular loops...
633 wxPyEvtSelfRef::~wxPyEvtSelfRef() {
634 wxPyTState
* state
= wxPyBeginBlockThreads();
637 wxPyEndBlockThreads(state
);
640 void wxPyEvtSelfRef::SetSelf(PyObject
* self
, bool clone
) {
641 wxPyTState
* state
= wxPyBeginBlockThreads();
649 wxPyEndBlockThreads(state
);
652 PyObject
* wxPyEvtSelfRef::GetSelf() const {
658 wxPyEvent::wxPyEvent(int id
)
662 wxPyEvent::~wxPyEvent() {
665 // This one is so the event object can be Cloned...
666 void wxPyEvent::CopyObject(wxObject
& dest
) const {
667 wxEvent::CopyObject(dest
);
668 ((wxPyEvent
*)&dest
)->SetSelf(m_self
, TRUE
);
672 IMPLEMENT_DYNAMIC_CLASS(wxPyEvent
, wxEvent
);
675 wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType
, int id
)
676 : wxCommandEvent(commandType
, id
) {
679 wxPyCommandEvent::~wxPyCommandEvent() {
682 void wxPyCommandEvent::CopyObject(wxObject
& dest
) const {
683 wxCommandEvent::CopyObject(dest
);
684 ((wxPyCommandEvent
*)&dest
)->SetSelf(m_self
, TRUE
);
688 IMPLEMENT_DYNAMIC_CLASS(wxPyCommandEvent
, wxCommandEvent
);
692 //---------------------------------------------------------------------------
693 //---------------------------------------------------------------------------
696 wxPyTimer::wxPyTimer(PyObject
* callback
) {
701 wxPyTimer::~wxPyTimer() {
702 wxPyTState
* state
= wxPyBeginBlockThreads();
704 wxPyEndBlockThreads(state
);
707 void wxPyTimer::Notify() {
708 if (!func
|| func
== Py_None
) {
712 wxPyTState
* state
= wxPyBeginBlockThreads();
715 PyObject
* args
= Py_BuildValue("()");
717 result
= PyEval_CallObject(func
, args
);
726 wxPyEndBlockThreads(state
);
732 //---------------------------------------------------------------------------
733 //---------------------------------------------------------------------------
734 // Convert a wxList to a Python List
736 PyObject
* wxPy_ConvertList(wxListBase
* list
, const char* className
) {
740 wxNode
* node
= list
->First();
742 wxPyTState
* state
= wxPyBeginBlockThreads();
743 pyList
= PyList_New(0);
745 wxObj
= node
->Data();
746 pyObj
= wxPyMake_wxObject(wxObj
); //wxPyConstructObject(wxObj, className);
747 PyList_Append(pyList
, pyObj
);
750 wxPyEndBlockThreads(state
);
754 //----------------------------------------------------------------------
756 long wxPyGetWinHandle(wxWindow
* win
) {
758 return (long)win
->GetHandle();
761 // Find and return the actual X-Window.
763 if (win
->m_wxwindow
) {
764 GdkWindowPrivate
* bwin
= (GdkWindowPrivate
*)GTK_PIZZA(win
->m_wxwindow
)->bin_window
;
766 return (long)bwin
->xwindow
;
773 //----------------------------------------------------------------------
774 // Some helper functions for typemaps in my_typemaps.i, so they won't be
775 // included in every file...
778 byte
* byte_LIST_helper(PyObject
* source
) {
779 if (!PyList_Check(source
)) {
780 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
783 int count
= PyList_Size(source
);
784 byte
* temp
= new byte
[count
];
786 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
789 for (int x
=0; x
<count
; x
++) {
790 PyObject
* o
= PyList_GetItem(source
, x
);
791 if (! PyInt_Check(o
)) {
792 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
795 temp
[x
] = (byte
)PyInt_AsLong(o
);
801 int* int_LIST_helper(PyObject
* source
) {
802 if (!PyList_Check(source
)) {
803 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
806 int count
= PyList_Size(source
);
807 int* temp
= new int[count
];
809 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
812 for (int x
=0; x
<count
; x
++) {
813 PyObject
* o
= PyList_GetItem(source
, x
);
814 if (! PyInt_Check(o
)) {
815 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
818 temp
[x
] = PyInt_AsLong(o
);
824 long* long_LIST_helper(PyObject
* source
) {
825 if (!PyList_Check(source
)) {
826 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
829 int count
= PyList_Size(source
);
830 long* temp
= new long[count
];
832 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
835 for (int x
=0; x
<count
; x
++) {
836 PyObject
* o
= PyList_GetItem(source
, x
);
837 if (! PyInt_Check(o
)) {
838 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
841 temp
[x
] = PyInt_AsLong(o
);
847 char** string_LIST_helper(PyObject
* source
) {
848 if (!PyList_Check(source
)) {
849 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
852 int count
= PyList_Size(source
);
853 char** temp
= new char*[count
];
855 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
858 for (int x
=0; x
<count
; x
++) {
859 PyObject
* o
= PyList_GetItem(source
, x
);
860 if (! PyString_Check(o
)) {
861 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
864 temp
[x
] = PyString_AsString(o
);
869 //--------------------------------
870 // Part of patch from Tim Hochberg
871 static inline bool wxPointFromObjects(PyObject
* o1
, PyObject
* o2
, wxPoint
* point
) {
872 if (PyInt_Check(o1
) && PyInt_Check(o2
)) {
873 point
->x
= PyInt_AS_LONG(o1
);
874 point
->y
= PyInt_AS_LONG(o2
);
877 if (PyFloat_Check(o1
) && PyFloat_Check(o2
)) {
878 point
->x
= (int)PyFloat_AS_DOUBLE(o1
);
879 point
->y
= (int)PyFloat_AS_DOUBLE(o2
);
882 if (PyInstance_Check(o1
) || PyInstance_Check(o2
)) {
883 // Disallow instances because they can cause havok
886 if (PyNumber_Check(o1
) && PyNumber_Check(o2
)) {
887 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
888 point
->x
= PyInt_AsLong(o1
);
889 point
->y
= PyInt_AsLong(o2
);
896 wxPoint
* wxPoint_LIST_helper(PyObject
* source
, int *count
) {
897 // Putting all of the declarations here allows
898 // us to put the error handling all in one place.
901 PyObject
*o
, *o1
, *o2
;
902 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
904 if (!PySequence_Check(source
)) {
908 // The length of the sequence is returned in count.
909 *count
= PySequence_Length(source
);
914 temp
= new wxPoint
[*count
];
916 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
919 for (x
=0; x
<*count
; x
++) {
920 // Get an item: try fast way first.
922 o
= PySequence_Fast_GET_ITEM(source
, x
);
925 o
= PySequence_GetItem(source
, x
);
931 // Convert o to wxPoint.
932 if ((PyTuple_Check(o
) && PyTuple_GET_SIZE(o
) == 2) ||
933 (PyList_Check(o
) && PyList_GET_SIZE(o
) == 2)) {
934 o1
= PySequence_Fast_GET_ITEM(o
, 0);
935 o2
= PySequence_Fast_GET_ITEM(o
, 1);
936 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
940 else if (PyInstance_Check(o
)) {
942 if (SWIG_GetPtrObj(o
, (void **)&pt
, "_wxPoint_p")) {
947 else if (PySequence_Check(o
) && PySequence_Length(o
) == 2) {
948 o1
= PySequence_GetItem(o
, 0);
949 o2
= PySequence_GetItem(o
, 1);
950 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
974 PyErr_SetString(PyExc_TypeError
, "Expected a sequence of length-2 sequences or wxPoints.");
978 //------------------------------
981 wxBitmap
** wxBitmap_LIST_helper(PyObject
* source
) {
982 if (!PyList_Check(source
)) {
983 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
986 int count
= PyList_Size(source
);
987 wxBitmap
** temp
= new wxBitmap
*[count
];
989 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
992 for (int x
=0; x
<count
; x
++) {
993 PyObject
* o
= PyList_GetItem(source
, x
);
994 if (PyInstance_Check(o
)) {
996 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxBitmap_p")) {
997 PyErr_SetString(PyExc_TypeError
,"Expected _wxBitmap_p.");
1003 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxBitmaps.");
1012 wxString
* wxString_LIST_helper(PyObject
* source
) {
1013 if (!PyList_Check(source
)) {
1014 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1017 int count
= PyList_Size(source
);
1018 wxString
* temp
= new wxString
[count
];
1020 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1023 for (int x
=0; x
<count
; x
++) {
1024 PyObject
* o
= PyList_GetItem(source
, x
);
1025 #if PYTHON_API_VERSION >= 1009
1026 if (! PyString_Check(o
) && ! PyUnicode_Check(o
)) {
1027 PyErr_SetString(PyExc_TypeError
, "Expected a list of string or unicode objects.");
1033 if (PyString_AsStringAndSize(o
, &buff
, &length
) == -1)
1035 temp
[x
] = wxString(buff
, length
);
1037 if (! PyString_Check(o
)) {
1038 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1041 temp
[x
] = PyString_AsString(o
);
1048 wxAcceleratorEntry
* wxAcceleratorEntry_LIST_helper(PyObject
* source
) {
1049 if (!PyList_Check(source
)) {
1050 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1053 int count
= PyList_Size(source
);
1054 wxAcceleratorEntry
* temp
= new wxAcceleratorEntry
[count
];
1056 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1059 for (int x
=0; x
<count
; x
++) {
1060 PyObject
* o
= PyList_GetItem(source
, x
);
1061 if (PyInstance_Check(o
)) {
1062 wxAcceleratorEntry
* ae
;
1063 if (SWIG_GetPtrObj(o
, (void **) &ae
,"_wxAcceleratorEntry_p")) {
1064 PyErr_SetString(PyExc_TypeError
,"Expected _wxAcceleratorEntry_p.");
1069 else if (PyTuple_Check(o
)) {
1070 PyObject
* o1
= PyTuple_GetItem(o
, 0);
1071 PyObject
* o2
= PyTuple_GetItem(o
, 1);
1072 PyObject
* o3
= PyTuple_GetItem(o
, 2);
1073 temp
[x
].Set(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
));
1076 PyErr_SetString(PyExc_TypeError
, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
1084 wxPen
** wxPen_LIST_helper(PyObject
* source
) {
1085 if (!PyList_Check(source
)) {
1086 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1089 int count
= PyList_Size(source
);
1090 wxPen
** temp
= new wxPen
*[count
];
1092 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1095 for (int x
=0; x
<count
; x
++) {
1096 PyObject
* o
= PyList_GetItem(source
, x
);
1097 if (PyInstance_Check(o
)) {
1099 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxPen_p")) {
1101 PyErr_SetString(PyExc_TypeError
,"Expected _wxPen_p.");
1108 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxPens.");
1116 bool _2int_seq_helper(PyObject
* source
, int* i1
, int* i2
) {
1117 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1120 if (!PySequence_Check(source
) || PySequence_Length(source
) != 2)
1124 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1125 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1128 o1
= PySequence_GetItem(source
, 0);
1129 o2
= PySequence_GetItem(source
, 1);
1132 *i1
= PyInt_AsLong(o1
);
1133 *i2
= PyInt_AsLong(o2
);
1143 bool _4int_seq_helper(PyObject
* source
, int* i1
, int* i2
, int* i3
, int* i4
) {
1144 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1145 PyObject
*o1
, *o2
, *o3
, *o4
;
1147 if (!PySequence_Check(source
) || PySequence_Length(source
) != 4)
1151 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1152 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1153 o3
= PySequence_Fast_GET_ITEM(source
, 2);
1154 o4
= PySequence_Fast_GET_ITEM(source
, 3);
1157 o1
= PySequence_GetItem(source
, 0);
1158 o2
= PySequence_GetItem(source
, 1);
1159 o3
= PySequence_GetItem(source
, 2);
1160 o4
= PySequence_GetItem(source
, 3);
1163 *i1
= PyInt_AsLong(o1
);
1164 *i2
= PyInt_AsLong(o2
);
1165 *i3
= PyInt_AsLong(o3
);
1166 *i4
= PyInt_AsLong(o4
);
1178 //----------------------------------------------------------------------
1180 bool wxSize_helper(PyObject
* source
, wxSize
** obj
) {
1182 // If source is an object instance then it may already be the right type
1183 if (PyInstance_Check(source
)) {
1185 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxSize_p"))
1190 // otherwise a 2-tuple of integers is expected
1191 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1192 PyObject
* o1
= PySequence_GetItem(source
, 0);
1193 PyObject
* o2
= PySequence_GetItem(source
, 1);
1194 **obj
= wxSize(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1199 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxSize object.");
1203 bool wxPoint_helper(PyObject
* source
, wxPoint
** obj
) {
1205 // If source is an object instance then it may already be the right type
1206 if (PyInstance_Check(source
)) {
1208 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxPoint_p"))
1213 // otherwise a length-2 sequence of integers is expected
1214 if (PySequence_Check(source
) && PySequence_Length(source
) == 2) {
1215 PyObject
* o1
= PySequence_GetItem(source
, 0);
1216 PyObject
* o2
= PySequence_GetItem(source
, 1);
1217 // This should really check for integers, not numbers -- but that would break code.
1218 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1223 **obj
= wxPoint(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1229 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxPoint object.");
1235 bool wxRealPoint_helper(PyObject
* source
, wxRealPoint
** obj
) {
1237 // If source is an object instance then it may already be the right type
1238 if (PyInstance_Check(source
)) {
1240 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRealPoint_p"))
1245 // otherwise a 2-tuple of floats is expected
1246 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1247 PyObject
* o1
= PySequence_GetItem(source
, 0);
1248 PyObject
* o2
= PySequence_GetItem(source
, 1);
1249 **obj
= wxRealPoint(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
));
1254 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxRealPoint object.");
1261 bool wxRect_helper(PyObject
* source
, wxRect
** obj
) {
1263 // If source is an object instance then it may already be the right type
1264 if (PyInstance_Check(source
)) {
1266 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRect_p"))
1271 // otherwise a 4-tuple of integers is expected
1272 else if (PySequence_Check(source
) && PyObject_Length(source
) == 4) {
1273 PyObject
* o1
= PySequence_GetItem(source
, 0);
1274 PyObject
* o2
= PySequence_GetItem(source
, 1);
1275 PyObject
* o3
= PySequence_GetItem(source
, 2);
1276 PyObject
* o4
= PySequence_GetItem(source
, 3);
1277 **obj
= wxRect(PyInt_AsLong(o1
), PyInt_AsLong(o2
),
1278 PyInt_AsLong(o3
), PyInt_AsLong(o4
));
1283 PyErr_SetString(PyExc_TypeError
, "Expected a 4-tuple of integers or a wxRect object.");
1289 bool wxColour_helper(PyObject
* source
, wxColour
** obj
) {
1291 // If source is an object instance then it may already be the right type
1292 if (PyInstance_Check(source
)) {
1294 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxColour_p"))
1299 // otherwise a string is expected
1300 else if (PyString_Check(source
)) {
1301 wxString spec
= PyString_AS_STRING(source
);
1302 if (spec
[0U] == '#' && spec
.Length() == 7) { // It's #RRGGBB
1304 int red
= strtol(spec
.Mid(1,2), &junk
, 16);
1305 int green
= strtol(spec
.Mid(3,2), &junk
, 16);
1306 int blue
= strtol(spec
.Mid(5,2), &junk
, 16);
1307 **obj
= wxColour(red
, green
, blue
);
1310 else { // it's a colour name
1311 **obj
= wxColour(spec
);
1317 PyErr_SetString(PyExc_TypeError
, "Expected a wxColour object or a string containing a colour name or '#RRGGBB'.");
1322 //----------------------------------------------------------------------
1323 //----------------------------------------------------------------------
1324 //----------------------------------------------------------------------