]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/src/helpers.cpp
28f0621eedbacb91ccf8d38f9fd1f4c7edd819a5
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 PyInterpreterState
* wxPyInterpreter
= NULL
;
108 // This is where we pick up the first part of the wxEntry functionality...
109 // The rest is in __wxStart and __wxCleanup. This function is called when
110 // wxcmodule is imported. (Before there is a wxApp object.)
115 // wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
118 #ifdef WXP_WITH_THREAD
119 PyEval_InitThreads();
120 wxPyInterpreter
= PyThreadState_Get()->interp
;
123 // Bail out if there is already windows created. This means that the
124 // toolkit has already been initialized, as in embedding wxPython in
125 // a C++ wxWindows app.
126 if (wxTopLevelWindows
.Number() > 0)
132 PyObject
* sysargv
= PySys_GetObject("argv");
133 if (sysargv
!= NULL
) {
134 argc
= PyList_Size(sysargv
);
135 argv
= new char*[argc
+1];
137 for(x
=0; x
<argc
; x
++)
138 argv
[x
] = copystring(PyString_AsString(PyList_GetItem(sysargv
, x
)));
142 wxEntryStart(argc
, argv
);
148 // Start the user application, user App's OnInit method is a parameter here
149 PyObject
* __wxStart(PyObject
* /* self */, PyObject
* args
)
151 PyObject
* onInitFunc
= NULL
;
156 if (!PyArg_ParseTuple(args
, "O", &onInitFunc
))
159 #if 0 // Try it out without this check, see how it does...
160 if (wxTopLevelWindows
.Number() > 0) {
161 PyErr_SetString(PyExc_TypeError
, "Only 1 wxApp per process!");
166 // This is the next part of the wxEntry functionality...
169 PyObject
* sysargv
= PySys_GetObject("argv");
170 if (sysargv
!= NULL
) {
171 argc
= PyList_Size(sysargv
);
172 argv
= new char*[argc
+1];
174 for(x
=0; x
<argc
; x
++)
175 argv
[x
] = copystring(PyString_AsString(PyList_GetItem(sysargv
, x
)));
178 wxPythonApp
->argc
= argc
;
179 wxPythonApp
->argv
= argv
;
183 // Call the Python App's OnInit function
184 arglist
= PyTuple_New(0);
185 result
= PyEval_CallObject(onInitFunc
, arglist
);
186 if (!result
) { // an exception was raised.
190 if (! PyInt_Check(result
)) {
191 PyErr_SetString(PyExc_TypeError
, "OnInit should return a boolean value");
194 bResult
= PyInt_AS_LONG(result
);
196 PyErr_SetString(PyExc_SystemExit
, "OnInit returned FALSE, exiting...");
201 wxTheApp
->m_initialized
= (wxTopLevelWindows
.GetCount() > 0);
214 static PyObject
* wxPython_dict
= NULL
;
215 static PyObject
* wxPyPtrTypeMap
= NULL
;
217 PyObject
* __wxSetDictionary(PyObject
* /* self */, PyObject
* args
)
220 if (!PyArg_ParseTuple(args
, "O", &wxPython_dict
))
223 if (!PyDict_Check(wxPython_dict
)) {
224 PyErr_SetString(PyExc_TypeError
, "_wxSetDictionary must have dictionary object!");
228 if (! wxPyPtrTypeMap
)
229 wxPyPtrTypeMap
= PyDict_New();
230 PyDict_SetItemString(wxPython_dict
, "__wxPyPtrTypeMap", wxPyPtrTypeMap
);
234 #define wxPlatform "__WXMOTIF__"
237 #define wxPlatform "__WXQT__"
240 #define wxPlatform "__WXGTK__"
242 #if defined(__WIN32__) || defined(__WXMSW__)
243 #define wxPlatform "__WXMSW__"
246 #define wxPlatform "__WXMAC__"
249 PyDict_SetItemString(wxPython_dict
, "wxPlatform", PyString_FromString(wxPlatform
));
256 //---------------------------------------------------------------------------
257 // Stuff used by OOR to find the right wxPython class type to return and to
261 // The pointer type map is used when the "pointer" type name generated by SWIG
262 // is not the same as the shadow class name, for example wxPyTreeCtrl
263 // vs. wxTreeCtrl. It needs to be referenced in Python as well as from C++,
264 // so we'll just make it a Python dictionary in the wx module's namespace.
265 void wxPyPtrTypeMap_Add(const char* commonName
, const char* ptrName
) {
266 if (! wxPyPtrTypeMap
)
267 wxPyPtrTypeMap
= PyDict_New();
269 PyDict_SetItemString(wxPyPtrTypeMap
,
271 PyString_FromString((char*)ptrName
));
276 PyObject
* wxPyClassExists(const char* className
) {
281 char buff
[64]; // should always be big enough...
283 sprintf(buff
, "%sPtr", className
);
284 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
286 return classobj
; // returns NULL if not found
290 PyObject
* wxPyMake_wxObject(wxObject
* source
, bool checkEvtHandler
) {
291 PyObject
* target
= NULL
;
292 bool isEvtHandler
= FALSE
;
295 // If it's derived from wxEvtHandler then there may
296 // already be a pointer to a Python object that we can use
298 if (checkEvtHandler
&& wxIsKindOf(source
, wxEvtHandler
)) {
300 wxEvtHandler
* eh
= (wxEvtHandler
*)source
;
301 wxPyClientData
* data
= (wxPyClientData
*)eh
->GetClientObject();
303 target
= data
->m_obj
;
309 // Otherwise make it the old fashioned way by making a
310 // new shadow object and putting this pointer in it.
311 wxClassInfo
* info
= source
->GetClassInfo();
312 wxChar
* name
= (wxChar
*)info
->GetClassName();
313 PyObject
* klass
= wxPyClassExists(name
);
314 while (info
&& !klass
) {
315 name
= (wxChar
*)info
->GetBaseClassName1();
316 info
= wxClassInfo::FindClass(name
);
317 klass
= wxPyClassExists(name
);
320 target
= wxPyConstructObject(source
, name
, klass
, FALSE
);
321 if (target
&& isEvtHandler
)
322 ((wxEvtHandler
*)source
)->SetClientObject(new wxPyClientData(target
));
324 wxString
msg("wxPython class not found for ");
325 msg
+= source
->GetClassInfo()->GetClassName();
326 PyErr_SetString(PyExc_NameError
, msg
.c_str());
330 } else { // source was NULL so return None.
331 Py_INCREF(Py_None
); target
= Py_None
;
337 PyObject
* wxPyMake_wxSizer(wxSizer
* source
) {
338 PyObject
* target
= NULL
;
340 if (source
&& wxIsKindOf(source
, wxSizer
)) {
341 // If it's derived from wxSizer then there may
342 // already be a pointer to a Python object that we can use
344 wxSizer
* sz
= (wxSizer
*)source
;
345 wxPyClientData
* data
= (wxPyClientData
*)sz
->GetClientObject();
347 target
= data
->m_obj
;
352 target
= wxPyMake_wxObject(source
, FALSE
);
353 if (target
!= Py_None
)
354 ((wxSizer
*)source
)->SetClientObject(new wxPyClientData(target
));
361 //---------------------------------------------------------------------------
363 PyObject
* wxPyConstructObject(void* ptr
,
364 const char* className
,
371 char swigptr
[64]; // should always be big enough...
374 if ((item
= PyDict_GetItemString(wxPyPtrTypeMap
, (char*)className
)) != NULL
) {
375 className
= PyString_AsString(item
);
377 sprintf(buff
, "_%s_p", className
);
378 SWIG_MakePtr(swigptr
, ptr
, buff
);
380 arg
= Py_BuildValue("(s)", swigptr
);
381 obj
= PyInstance_New(klass
, arg
, NULL
);
385 PyObject
* one
= PyInt_FromLong(1);
386 PyObject_SetAttrString(obj
, "thisown", one
);
394 PyObject
* wxPyConstructObject(void* ptr
,
395 const char* className
,
404 char buff
[64]; // should always be big enough...
406 sprintf(buff
, "%sPtr", className
);
407 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
411 "*** Unknown class name %s, tell Robin about it please ***",
413 obj
= PyString_FromString(temp
);
417 return wxPyConstructObject(ptr
, className
, classobj
, setThisOwn
);
420 //---------------------------------------------------------------------------
423 wxPyTState
* wxPyBeginBlockThreads() {
424 wxPyTState
* state
= NULL
;
425 #ifdef WXP_WITH_THREAD
426 if (1) { // Can I check if I've already got the lock?
427 state
= new wxPyTState
;
428 PyEval_AcquireLock();
429 state
->newState
= PyThreadState_New(wxPyInterpreter
);
430 state
->prevState
= PyThreadState_Swap(state
->newState
);
437 void wxPyEndBlockThreads(wxPyTState
* state
) {
438 #ifdef WXP_WITH_THREAD
440 PyThreadState_Swap(state
->prevState
);
441 PyThreadState_Clear(state
->newState
);
442 PyEval_ReleaseLock();
443 PyThreadState_Delete(state
->newState
);
450 //---------------------------------------------------------------------------
452 IMPLEMENT_ABSTRACT_CLASS(wxPyCallback
, wxObject
);
454 wxPyCallback::wxPyCallback(PyObject
* func
) {
459 wxPyCallback::wxPyCallback(const wxPyCallback
& other
) {
460 m_func
= other
.m_func
;
464 wxPyCallback::~wxPyCallback() {
465 wxPyTState
* state
= wxPyBeginBlockThreads();
467 wxPyEndBlockThreads(state
);
472 // This function is used for all events destined for Python event handlers.
473 void wxPyCallback::EventThunker(wxEvent
& event
) {
474 wxPyCallback
* cb
= (wxPyCallback
*)event
.m_callbackUserData
;
475 PyObject
* func
= cb
->m_func
;
481 wxPyTState
* state
= wxPyBeginBlockThreads();
482 wxString className
= event
.GetClassInfo()->GetClassName();
484 if (className
== "wxPyEvent")
485 arg
= ((wxPyEvent
*)&event
)->GetSelf();
486 else if (className
== "wxPyCommandEvent")
487 arg
= ((wxPyCommandEvent
*)&event
)->GetSelf();
489 arg
= wxPyConstructObject((void*)&event
, className
);
491 tuple
= PyTuple_New(1);
492 PyTuple_SET_ITEM(tuple
, 0, arg
);
493 result
= PyEval_CallObject(func
, tuple
);
497 PyErr_Clear(); // Just in case...
501 wxPyEndBlockThreads(state
);
505 //----------------------------------------------------------------------
507 wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper
& other
) {
509 m_self
= other
.m_self
;
510 m_class
= other
.m_class
;
518 void wxPyCallbackHelper::setSelf(PyObject
* self
, PyObject
* klass
, int incref
) {
529 // If the object (m_self) has an attibute of the given name, and if that
530 // attribute is a method, and if that method's class is not from a base class,
531 // then we'll save a pointer to the method so callCallback can call it.
532 bool wxPyCallbackHelper::findCallback(const char* name
) const {
533 wxPyCallbackHelper
* self
= (wxPyCallbackHelper
*)this; // cast away const
534 self
->m_lastFound
= NULL
;
535 if (m_self
&& PyObject_HasAttrString(m_self
, (char*)name
)) {
537 method
= PyObject_GetAttrString(m_self
, (char*)name
);
539 if (PyMethod_Check(method
) &&
540 ((PyMethod_GET_CLASS(method
) == m_class
) ||
541 PyClass_IsSubclass(PyMethod_GET_CLASS(method
), m_class
))) {
543 self
->m_lastFound
= method
;
549 return m_lastFound
!= NULL
;
553 int wxPyCallbackHelper::callCallback(PyObject
* argTuple
) const {
557 result
= callCallbackObj(argTuple
);
558 if (result
) { // Assumes an integer return type...
559 retval
= PyInt_AsLong(result
);
561 PyErr_Clear(); // forget about it if it's not...
566 // Invoke the Python callable object, returning the raw PyObject return
567 // value. Caller should DECREF the return value and also call PyEval_SaveThread.
568 PyObject
* wxPyCallbackHelper::callCallbackObj(PyObject
* argTuple
) const {
571 // Save a copy of the pointer in case the callback generates another
572 // callback. In that case m_lastFound will have a different value when
573 // it gets back here...
574 PyObject
* method
= m_lastFound
;
576 result
= PyEval_CallObject(method
, argTuple
);
586 void wxPyCBH_setCallbackInfo(wxPyCallbackHelper
& cbh
, PyObject
* self
, PyObject
* klass
, int incref
) {
587 cbh
.setSelf(self
, klass
, incref
);
590 bool wxPyCBH_findCallback(const wxPyCallbackHelper
& cbh
, const char* name
) {
591 return cbh
.findCallback(name
);
594 int wxPyCBH_callCallback(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
595 return cbh
.callCallback(argTuple
);
598 PyObject
* wxPyCBH_callCallbackObj(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
599 return cbh
.callCallbackObj(argTuple
);
603 void wxPyCBH_delete(wxPyCallbackHelper
* cbh
) {
605 wxPyTState
* state
= wxPyBeginBlockThreads();
606 Py_XDECREF(cbh
->m_self
);
607 Py_XDECREF(cbh
->m_class
);
608 wxPyEndBlockThreads(state
);
612 //---------------------------------------------------------------------------
613 //---------------------------------------------------------------------------
614 // These event classes can be derived from in Python and passed through the event
615 // system without losing anything. They do this by keeping a reference to
616 // themselves and some special case handling in wxPyCallback::EventThunker.
619 wxPyEvtSelfRef::wxPyEvtSelfRef() {
620 //m_self = Py_None; // **** We don't do normal ref counting to prevent
621 //Py_INCREF(m_self); // circular loops...
625 wxPyEvtSelfRef::~wxPyEvtSelfRef() {
626 wxPyTState
* state
= wxPyBeginBlockThreads();
629 wxPyEndBlockThreads(state
);
632 void wxPyEvtSelfRef::SetSelf(PyObject
* self
, bool clone
) {
633 wxPyTState
* state
= wxPyBeginBlockThreads();
641 wxPyEndBlockThreads(state
);
644 PyObject
* wxPyEvtSelfRef::GetSelf() const {
650 IMPLEMENT_ABSTRACT_CLASS(wxPyEvent
, wxEvent
);
651 IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent
, wxCommandEvent
);
654 wxPyEvent::wxPyEvent(int id
)
659 wxPyEvent::wxPyEvent(const wxPyEvent
& evt
)
662 SetSelf(evt
.m_self
, TRUE
);
666 wxPyEvent::~wxPyEvent() {
670 wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType
, int id
)
671 : wxCommandEvent(commandType
, id
) {
675 wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent
& evt
)
676 : wxCommandEvent(evt
)
678 SetSelf(evt
.m_self
, TRUE
);
682 wxPyCommandEvent::~wxPyCommandEvent() {
688 //---------------------------------------------------------------------------
689 //---------------------------------------------------------------------------
692 wxPyTimer::wxPyTimer(PyObject
* callback
) {
697 wxPyTimer::~wxPyTimer() {
698 wxPyTState
* state
= wxPyBeginBlockThreads();
700 wxPyEndBlockThreads(state
);
703 void wxPyTimer::Notify() {
704 if (!func
|| func
== Py_None
) {
708 wxPyTState
* state
= wxPyBeginBlockThreads();
711 PyObject
* args
= Py_BuildValue("()");
713 result
= PyEval_CallObject(func
, args
);
722 wxPyEndBlockThreads(state
);
728 //---------------------------------------------------------------------------
729 //---------------------------------------------------------------------------
730 // Convert a wxList to a Python List
732 PyObject
* wxPy_ConvertList(wxListBase
* list
, const char* className
) {
736 wxNode
* node
= list
->First();
738 wxPyTState
* state
= wxPyBeginBlockThreads();
739 pyList
= PyList_New(0);
741 wxObj
= node
->Data();
742 pyObj
= wxPyMake_wxObject(wxObj
); //wxPyConstructObject(wxObj, className);
743 PyList_Append(pyList
, pyObj
);
746 wxPyEndBlockThreads(state
);
750 //----------------------------------------------------------------------
752 long wxPyGetWinHandle(wxWindow
* win
) {
754 return (long)win
->GetHandle();
757 // Find and return the actual X-Window.
759 if (win
->m_wxwindow
) {
760 GdkWindowPrivate
* bwin
= (GdkWindowPrivate
*)GTK_PIZZA(win
->m_wxwindow
)->bin_window
;
762 return (long)bwin
->xwindow
;
769 //----------------------------------------------------------------------
770 // Some helper functions for typemaps in my_typemaps.i, so they won't be
771 // included in every file...
774 byte
* byte_LIST_helper(PyObject
* source
) {
775 if (!PyList_Check(source
)) {
776 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
779 int count
= PyList_Size(source
);
780 byte
* temp
= new byte
[count
];
782 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
785 for (int x
=0; x
<count
; x
++) {
786 PyObject
* o
= PyList_GetItem(source
, x
);
787 if (! PyInt_Check(o
)) {
788 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
791 temp
[x
] = (byte
)PyInt_AsLong(o
);
797 int* int_LIST_helper(PyObject
* source
) {
798 if (!PyList_Check(source
)) {
799 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
802 int count
= PyList_Size(source
);
803 int* temp
= new int[count
];
805 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
808 for (int x
=0; x
<count
; x
++) {
809 PyObject
* o
= PyList_GetItem(source
, x
);
810 if (! PyInt_Check(o
)) {
811 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
814 temp
[x
] = PyInt_AsLong(o
);
820 long* long_LIST_helper(PyObject
* source
) {
821 if (!PyList_Check(source
)) {
822 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
825 int count
= PyList_Size(source
);
826 long* temp
= new long[count
];
828 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
831 for (int x
=0; x
<count
; x
++) {
832 PyObject
* o
= PyList_GetItem(source
, x
);
833 if (! PyInt_Check(o
)) {
834 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
837 temp
[x
] = PyInt_AsLong(o
);
843 char** string_LIST_helper(PyObject
* source
) {
844 if (!PyList_Check(source
)) {
845 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
848 int count
= PyList_Size(source
);
849 char** temp
= new char*[count
];
851 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
854 for (int x
=0; x
<count
; x
++) {
855 PyObject
* o
= PyList_GetItem(source
, x
);
856 if (! PyString_Check(o
)) {
857 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
860 temp
[x
] = PyString_AsString(o
);
865 //--------------------------------
866 // Part of patch from Tim Hochberg
867 static inline bool wxPointFromObjects(PyObject
* o1
, PyObject
* o2
, wxPoint
* point
) {
868 if (PyInt_Check(o1
) && PyInt_Check(o2
)) {
869 point
->x
= PyInt_AS_LONG(o1
);
870 point
->y
= PyInt_AS_LONG(o2
);
873 if (PyFloat_Check(o1
) && PyFloat_Check(o2
)) {
874 point
->x
= (int)PyFloat_AS_DOUBLE(o1
);
875 point
->y
= (int)PyFloat_AS_DOUBLE(o2
);
878 if (PyInstance_Check(o1
) || PyInstance_Check(o2
)) {
879 // Disallow instances because they can cause havok
882 if (PyNumber_Check(o1
) && PyNumber_Check(o2
)) {
883 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
884 point
->x
= PyInt_AsLong(o1
);
885 point
->y
= PyInt_AsLong(o2
);
892 wxPoint
* wxPoint_LIST_helper(PyObject
* source
, int *count
) {
893 // Putting all of the declarations here allows
894 // us to put the error handling all in one place.
897 PyObject
*o
, *o1
, *o2
;
898 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
900 if (!PySequence_Check(source
)) {
904 // The length of the sequence is returned in count.
905 *count
= PySequence_Length(source
);
910 temp
= new wxPoint
[*count
];
912 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
915 for (x
=0; x
<*count
; x
++) {
916 // Get an item: try fast way first.
918 o
= PySequence_Fast_GET_ITEM(source
, x
);
921 o
= PySequence_GetItem(source
, x
);
927 // Convert o to wxPoint.
928 if ((PyTuple_Check(o
) && PyTuple_GET_SIZE(o
) == 2) ||
929 (PyList_Check(o
) && PyList_GET_SIZE(o
) == 2)) {
930 o1
= PySequence_Fast_GET_ITEM(o
, 0);
931 o2
= PySequence_Fast_GET_ITEM(o
, 1);
932 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
936 else if (PyInstance_Check(o
)) {
938 if (SWIG_GetPtrObj(o
, (void **)&pt
, "_wxPoint_p")) {
943 else if (PySequence_Check(o
) && PySequence_Length(o
) == 2) {
944 o1
= PySequence_GetItem(o
, 0);
945 o2
= PySequence_GetItem(o
, 1);
946 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
970 PyErr_SetString(PyExc_TypeError
, "Expected a sequence of length-2 sequences or wxPoints.");
974 //------------------------------
977 wxBitmap
** wxBitmap_LIST_helper(PyObject
* source
) {
978 if (!PyList_Check(source
)) {
979 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
982 int count
= PyList_Size(source
);
983 wxBitmap
** temp
= new wxBitmap
*[count
];
985 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
988 for (int x
=0; x
<count
; x
++) {
989 PyObject
* o
= PyList_GetItem(source
, x
);
990 if (PyInstance_Check(o
)) {
992 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxBitmap_p")) {
993 PyErr_SetString(PyExc_TypeError
,"Expected _wxBitmap_p.");
999 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxBitmaps.");
1008 wxString
* wxString_LIST_helper(PyObject
* source
) {
1009 if (!PyList_Check(source
)) {
1010 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1013 int count
= PyList_Size(source
);
1014 wxString
* temp
= new wxString
[count
];
1016 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1019 for (int x
=0; x
<count
; x
++) {
1020 PyObject
* o
= PyList_GetItem(source
, x
);
1021 #if PYTHON_API_VERSION >= 1009
1022 if (! PyString_Check(o
) && ! PyUnicode_Check(o
)) {
1023 PyErr_SetString(PyExc_TypeError
, "Expected a list of string or unicode objects.");
1029 if (PyString_AsStringAndSize(o
, &buff
, &length
) == -1)
1031 temp
[x
] = wxString(buff
, length
);
1033 if (! PyString_Check(o
)) {
1034 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1037 temp
[x
] = PyString_AsString(o
);
1044 wxAcceleratorEntry
* wxAcceleratorEntry_LIST_helper(PyObject
* source
) {
1045 if (!PyList_Check(source
)) {
1046 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1049 int count
= PyList_Size(source
);
1050 wxAcceleratorEntry
* temp
= new wxAcceleratorEntry
[count
];
1052 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1055 for (int x
=0; x
<count
; x
++) {
1056 PyObject
* o
= PyList_GetItem(source
, x
);
1057 if (PyInstance_Check(o
)) {
1058 wxAcceleratorEntry
* ae
;
1059 if (SWIG_GetPtrObj(o
, (void **) &ae
,"_wxAcceleratorEntry_p")) {
1060 PyErr_SetString(PyExc_TypeError
,"Expected _wxAcceleratorEntry_p.");
1065 else if (PyTuple_Check(o
)) {
1066 PyObject
* o1
= PyTuple_GetItem(o
, 0);
1067 PyObject
* o2
= PyTuple_GetItem(o
, 1);
1068 PyObject
* o3
= PyTuple_GetItem(o
, 2);
1069 temp
[x
].Set(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
));
1072 PyErr_SetString(PyExc_TypeError
, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
1080 wxPen
** wxPen_LIST_helper(PyObject
* source
) {
1081 if (!PyList_Check(source
)) {
1082 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1085 int count
= PyList_Size(source
);
1086 wxPen
** temp
= new wxPen
*[count
];
1088 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1091 for (int x
=0; x
<count
; x
++) {
1092 PyObject
* o
= PyList_GetItem(source
, x
);
1093 if (PyInstance_Check(o
)) {
1095 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxPen_p")) {
1097 PyErr_SetString(PyExc_TypeError
,"Expected _wxPen_p.");
1104 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxPens.");
1112 bool _2int_seq_helper(PyObject
* source
, int* i1
, int* i2
) {
1113 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1116 if (!PySequence_Check(source
) || PySequence_Length(source
) != 2)
1120 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1121 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1124 o1
= PySequence_GetItem(source
, 0);
1125 o2
= PySequence_GetItem(source
, 1);
1128 *i1
= PyInt_AsLong(o1
);
1129 *i2
= PyInt_AsLong(o2
);
1139 bool _4int_seq_helper(PyObject
* source
, int* i1
, int* i2
, int* i3
, int* i4
) {
1140 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1141 PyObject
*o1
, *o2
, *o3
, *o4
;
1143 if (!PySequence_Check(source
) || PySequence_Length(source
) != 4)
1147 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1148 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1149 o3
= PySequence_Fast_GET_ITEM(source
, 2);
1150 o4
= PySequence_Fast_GET_ITEM(source
, 3);
1153 o1
= PySequence_GetItem(source
, 0);
1154 o2
= PySequence_GetItem(source
, 1);
1155 o3
= PySequence_GetItem(source
, 2);
1156 o4
= PySequence_GetItem(source
, 3);
1159 *i1
= PyInt_AsLong(o1
);
1160 *i2
= PyInt_AsLong(o2
);
1161 *i3
= PyInt_AsLong(o3
);
1162 *i4
= PyInt_AsLong(o4
);
1174 //----------------------------------------------------------------------
1176 bool wxSize_helper(PyObject
* source
, wxSize
** obj
) {
1178 // If source is an object instance then it may already be the right type
1179 if (PyInstance_Check(source
)) {
1181 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxSize_p"))
1186 // otherwise a 2-tuple of integers is expected
1187 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1188 PyObject
* o1
= PySequence_GetItem(source
, 0);
1189 PyObject
* o2
= PySequence_GetItem(source
, 1);
1190 **obj
= wxSize(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1195 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxSize object.");
1199 bool wxPoint_helper(PyObject
* source
, wxPoint
** obj
) {
1201 // If source is an object instance then it may already be the right type
1202 if (PyInstance_Check(source
)) {
1204 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxPoint_p"))
1209 // otherwise a length-2 sequence of integers is expected
1210 if (PySequence_Check(source
) && PySequence_Length(source
) == 2) {
1211 PyObject
* o1
= PySequence_GetItem(source
, 0);
1212 PyObject
* o2
= PySequence_GetItem(source
, 1);
1213 // This should really check for integers, not numbers -- but that would break code.
1214 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1219 **obj
= wxPoint(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1225 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxPoint object.");
1231 bool wxRealPoint_helper(PyObject
* source
, wxRealPoint
** obj
) {
1233 // If source is an object instance then it may already be the right type
1234 if (PyInstance_Check(source
)) {
1236 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRealPoint_p"))
1241 // otherwise a 2-tuple of floats is expected
1242 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1243 PyObject
* o1
= PySequence_GetItem(source
, 0);
1244 PyObject
* o2
= PySequence_GetItem(source
, 1);
1245 **obj
= wxRealPoint(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
));
1250 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxRealPoint object.");
1257 bool wxRect_helper(PyObject
* source
, wxRect
** obj
) {
1259 // If source is an object instance then it may already be the right type
1260 if (PyInstance_Check(source
)) {
1262 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRect_p"))
1267 // otherwise a 4-tuple of integers is expected
1268 else if (PySequence_Check(source
) && PyObject_Length(source
) == 4) {
1269 PyObject
* o1
= PySequence_GetItem(source
, 0);
1270 PyObject
* o2
= PySequence_GetItem(source
, 1);
1271 PyObject
* o3
= PySequence_GetItem(source
, 2);
1272 PyObject
* o4
= PySequence_GetItem(source
, 3);
1273 **obj
= wxRect(PyInt_AsLong(o1
), PyInt_AsLong(o2
),
1274 PyInt_AsLong(o3
), PyInt_AsLong(o4
));
1279 PyErr_SetString(PyExc_TypeError
, "Expected a 4-tuple of integers or a wxRect object.");
1285 bool wxColour_helper(PyObject
* source
, wxColour
** obj
) {
1287 // If source is an object instance then it may already be the right type
1288 if (PyInstance_Check(source
)) {
1290 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxColour_p"))
1295 // otherwise a string is expected
1296 else if (PyString_Check(source
)) {
1297 wxString spec
= PyString_AS_STRING(source
);
1298 if (spec
[0U] == '#' && spec
.Length() == 7) { // It's #RRGGBB
1300 int red
= strtol(spec
.Mid(1,2), &junk
, 16);
1301 int green
= strtol(spec
.Mid(3,2), &junk
, 16);
1302 int blue
= strtol(spec
.Mid(5,2), &junk
, 16);
1303 **obj
= wxColour(red
, green
, blue
);
1306 else { // it's a colour name
1307 **obj
= wxColour(spec
);
1313 PyErr_SetString(PyExc_TypeError
, "Expected a wxColour object or a string containing a colour name or '#RRGGBB'.");
1318 //----------------------------------------------------------------------
1320 PyObject
* wxArrayString2PyList_helper(const wxArrayString
& arr
) {
1322 PyObject
* list
= PyList_New(0);
1323 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
1324 PyObject
* str
= PyString_FromString(arr
[i
].c_str());
1325 PyList_Append(list
, str
);
1332 //----------------------------------------------------------------------
1333 //----------------------------------------------------------------------