]>
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 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
) {
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 objec that we can use.
297 if (wxIsKindOf(source
, wxEvtHandler
)) {
298 wxEvtHandler
* eh
= (wxEvtHandler
*)source
;
299 wxPyClientData
* data
= (wxPyClientData
*)eh
->GetClientObject();
301 target
= data
->m_obj
;
305 else if (wxIsKindOf(source
, wxSizer
)) {
306 // wxSizers also track the original object
307 wxSizer
* sz
= (wxSizer
*)source
;
308 wxPyClientData
* data
= (wxPyClientData
*)sz
->GetClientObject();
310 target
= data
->m_obj
;
316 // Otherwise make it the old fashioned way by making a
317 // new shadow object and putting this pointer in it.
318 wxClassInfo
* info
= source
->GetClassInfo();
319 wxChar
* name
= (wxChar
*)info
->GetClassName();
320 PyObject
* klass
= wxPyClassExists(name
);
321 while (info
&& !klass
) {
322 name
= (wxChar
*)info
->GetBaseClassName1();
323 info
= wxClassInfo::FindClass(name
);
324 klass
= wxPyClassExists(name
);
327 target
= wxPyConstructObject(source
, name
, klass
, FALSE
);
328 if (target
&& isEvtHandler
)
329 ((wxEvtHandler
*)source
)->SetClientObject(new wxPyClientData(target
));
331 wxString
msg("wxPython class not found for ");
332 msg
+= source
->GetClassInfo()->GetClassName();
333 PyErr_SetString(PyExc_NameError
, msg
.c_str());
337 } else { // source was NULL so return None.
338 Py_INCREF(Py_None
); target
= Py_None
;
344 //---------------------------------------------------------------------------
346 PyObject
* wxPyConstructObject(void* ptr
,
347 const char* className
,
354 char swigptr
[64]; // should always be big enough...
357 if ((item
= PyDict_GetItemString(wxPyPtrTypeMap
, (char*)className
)) != NULL
) {
358 className
= PyString_AsString(item
);
360 sprintf(buff
, "_%s_p", className
);
361 SWIG_MakePtr(swigptr
, ptr
, buff
);
363 arg
= Py_BuildValue("(s)", swigptr
);
364 obj
= PyInstance_New(klass
, arg
, NULL
);
368 PyObject
* one
= PyInt_FromLong(1);
369 PyObject_SetAttrString(obj
, "thisown", one
);
377 PyObject
* wxPyConstructObject(void* ptr
,
378 const char* className
,
387 char buff
[64]; // should always be big enough...
389 sprintf(buff
, "%sPtr", className
);
390 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
394 "*** Unknown class name %s, tell Robin about it please ***",
396 obj
= PyString_FromString(temp
);
400 return wxPyConstructObject(ptr
, className
, classobj
, setThisOwn
);
403 //---------------------------------------------------------------------------
406 wxPyTState
* wxPyBeginBlockThreads() {
407 wxPyTState
* state
= NULL
;
408 #ifdef WXP_WITH_THREAD
409 if (1) { // Can I check if I've already got the lock?
410 state
= new wxPyTState
;
411 PyEval_AcquireLock();
412 state
->newState
= PyThreadState_New(wxPyInterpreter
);
413 state
->prevState
= PyThreadState_Swap(state
->newState
);
420 void wxPyEndBlockThreads(wxPyTState
* state
) {
421 #ifdef WXP_WITH_THREAD
423 PyThreadState_Swap(state
->prevState
);
424 PyThreadState_Clear(state
->newState
);
425 PyEval_ReleaseLock();
426 PyThreadState_Delete(state
->newState
);
433 //---------------------------------------------------------------------------
435 IMPLEMENT_ABSTRACT_CLASS(wxPyCallback
, wxObject
);
437 wxPyCallback::wxPyCallback(PyObject
* func
) {
442 wxPyCallback::wxPyCallback(const wxPyCallback
& other
) {
443 m_func
= other
.m_func
;
447 wxPyCallback::~wxPyCallback() {
448 wxPyTState
* state
= wxPyBeginBlockThreads();
450 wxPyEndBlockThreads(state
);
455 // This function is used for all events destined for Python event handlers.
456 void wxPyCallback::EventThunker(wxEvent
& event
) {
457 wxPyCallback
* cb
= (wxPyCallback
*)event
.m_callbackUserData
;
458 PyObject
* func
= cb
->m_func
;
464 wxPyTState
* state
= wxPyBeginBlockThreads();
465 wxString className
= event
.GetClassInfo()->GetClassName();
467 if (className
== "wxPyEvent")
468 arg
= ((wxPyEvent
*)&event
)->GetSelf();
469 else if (className
== "wxPyCommandEvent")
470 arg
= ((wxPyCommandEvent
*)&event
)->GetSelf();
472 arg
= wxPyConstructObject((void*)&event
, className
);
474 tuple
= PyTuple_New(1);
475 PyTuple_SET_ITEM(tuple
, 0, arg
);
476 result
= PyEval_CallObject(func
, tuple
);
480 PyErr_Clear(); // Just in case...
484 wxPyEndBlockThreads(state
);
488 //----------------------------------------------------------------------
490 wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper
& other
) {
492 m_self
= other
.m_self
;
493 m_class
= other
.m_class
;
501 void wxPyCallbackHelper::setSelf(PyObject
* self
, PyObject
* klass
, int incref
) {
512 // If the object (m_self) has an attibute of the given name, and if that
513 // attribute is a method, and if that method's class is not from a base class,
514 // then we'll save a pointer to the method so callCallback can call it.
515 bool wxPyCallbackHelper::findCallback(const char* name
) const {
516 wxPyCallbackHelper
* self
= (wxPyCallbackHelper
*)this; // cast away const
517 self
->m_lastFound
= NULL
;
518 if (m_self
&& PyObject_HasAttrString(m_self
, (char*)name
)) {
520 method
= PyObject_GetAttrString(m_self
, (char*)name
);
522 if (PyMethod_Check(method
) &&
523 ((PyMethod_GET_CLASS(method
) == m_class
) ||
524 PyClass_IsSubclass(PyMethod_GET_CLASS(method
), m_class
))) {
526 self
->m_lastFound
= method
;
532 return m_lastFound
!= NULL
;
536 int wxPyCallbackHelper::callCallback(PyObject
* argTuple
) const {
540 result
= callCallbackObj(argTuple
);
541 if (result
) { // Assumes an integer return type...
542 retval
= PyInt_AsLong(result
);
544 PyErr_Clear(); // forget about it if it's not...
549 // Invoke the Python callable object, returning the raw PyObject return
550 // value. Caller should DECREF the return value and also call PyEval_SaveThread.
551 PyObject
* wxPyCallbackHelper::callCallbackObj(PyObject
* argTuple
) const {
554 // Save a copy of the pointer in case the callback generates another
555 // callback. In that case m_lastFound will have a different value when
556 // it gets back here...
557 PyObject
* method
= m_lastFound
;
559 result
= PyEval_CallObject(method
, argTuple
);
569 void wxPyCBH_setCallbackInfo(wxPyCallbackHelper
& cbh
, PyObject
* self
, PyObject
* klass
, int incref
) {
570 cbh
.setSelf(self
, klass
, incref
);
573 bool wxPyCBH_findCallback(const wxPyCallbackHelper
& cbh
, const char* name
) {
574 return cbh
.findCallback(name
);
577 int wxPyCBH_callCallback(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
578 return cbh
.callCallback(argTuple
);
581 PyObject
* wxPyCBH_callCallbackObj(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
582 return cbh
.callCallbackObj(argTuple
);
586 void wxPyCBH_delete(wxPyCallbackHelper
* cbh
) {
588 wxPyTState
* state
= wxPyBeginBlockThreads();
589 Py_XDECREF(cbh
->m_self
);
590 Py_XDECREF(cbh
->m_class
);
591 wxPyEndBlockThreads(state
);
595 //---------------------------------------------------------------------------
596 //---------------------------------------------------------------------------
597 // These event classes can be derived from in Python and passed through the event
598 // system without losing anything. They do this by keeping a reference to
599 // themselves and some special case handling in wxPyCallback::EventThunker.
602 wxPyEvtSelfRef::wxPyEvtSelfRef() {
603 //m_self = Py_None; // **** We don't do normal ref counting to prevent
604 //Py_INCREF(m_self); // circular loops...
608 wxPyEvtSelfRef::~wxPyEvtSelfRef() {
609 wxPyTState
* state
= wxPyBeginBlockThreads();
612 wxPyEndBlockThreads(state
);
615 void wxPyEvtSelfRef::SetSelf(PyObject
* self
, bool clone
) {
616 wxPyTState
* state
= wxPyBeginBlockThreads();
624 wxPyEndBlockThreads(state
);
627 PyObject
* wxPyEvtSelfRef::GetSelf() const {
633 IMPLEMENT_ABSTRACT_CLASS(wxPyEvent
, wxEvent
);
634 IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent
, wxCommandEvent
);
637 wxPyEvent::wxPyEvent(int id
)
642 wxPyEvent::wxPyEvent(const wxPyEvent
& evt
)
645 SetSelf(evt
.m_self
, TRUE
);
649 wxPyEvent::~wxPyEvent() {
653 wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType
, int id
)
654 : wxCommandEvent(commandType
, id
) {
658 wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent
& evt
)
659 : wxCommandEvent(evt
)
661 SetSelf(evt
.m_self
, TRUE
);
665 wxPyCommandEvent::~wxPyCommandEvent() {
671 //---------------------------------------------------------------------------
672 //---------------------------------------------------------------------------
675 wxPyTimer::wxPyTimer(PyObject
* callback
) {
680 wxPyTimer::~wxPyTimer() {
681 wxPyTState
* state
= wxPyBeginBlockThreads();
683 wxPyEndBlockThreads(state
);
686 void wxPyTimer::Notify() {
687 if (!func
|| func
== Py_None
) {
691 wxPyTState
* state
= wxPyBeginBlockThreads();
694 PyObject
* args
= Py_BuildValue("()");
696 result
= PyEval_CallObject(func
, args
);
705 wxPyEndBlockThreads(state
);
711 //---------------------------------------------------------------------------
712 //---------------------------------------------------------------------------
713 // Convert a wxList to a Python List
715 PyObject
* wxPy_ConvertList(wxListBase
* list
, const char* className
) {
719 wxNode
* node
= list
->First();
721 wxPyTState
* state
= wxPyBeginBlockThreads();
722 pyList
= PyList_New(0);
724 wxObj
= node
->Data();
725 pyObj
= wxPyMake_wxObject(wxObj
); //wxPyConstructObject(wxObj, className);
726 PyList_Append(pyList
, pyObj
);
729 wxPyEndBlockThreads(state
);
733 //----------------------------------------------------------------------
735 long wxPyGetWinHandle(wxWindow
* win
) {
737 return (long)win
->GetHandle();
740 // Find and return the actual X-Window.
742 if (win
->m_wxwindow
) {
743 GdkWindowPrivate
* bwin
= (GdkWindowPrivate
*)GTK_PIZZA(win
->m_wxwindow
)->bin_window
;
745 return (long)bwin
->xwindow
;
752 //----------------------------------------------------------------------
753 // Some helper functions for typemaps in my_typemaps.i, so they won't be
754 // included in every file...
757 byte
* byte_LIST_helper(PyObject
* source
) {
758 if (!PyList_Check(source
)) {
759 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
762 int count
= PyList_Size(source
);
763 byte
* temp
= new byte
[count
];
765 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
768 for (int x
=0; x
<count
; x
++) {
769 PyObject
* o
= PyList_GetItem(source
, x
);
770 if (! PyInt_Check(o
)) {
771 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
774 temp
[x
] = (byte
)PyInt_AsLong(o
);
780 int* int_LIST_helper(PyObject
* source
) {
781 if (!PyList_Check(source
)) {
782 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
785 int count
= PyList_Size(source
);
786 int* temp
= new int[count
];
788 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
791 for (int x
=0; x
<count
; x
++) {
792 PyObject
* o
= PyList_GetItem(source
, x
);
793 if (! PyInt_Check(o
)) {
794 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
797 temp
[x
] = PyInt_AsLong(o
);
803 long* long_LIST_helper(PyObject
* source
) {
804 if (!PyList_Check(source
)) {
805 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
808 int count
= PyList_Size(source
);
809 long* temp
= new long[count
];
811 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
814 for (int x
=0; x
<count
; x
++) {
815 PyObject
* o
= PyList_GetItem(source
, x
);
816 if (! PyInt_Check(o
)) {
817 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
820 temp
[x
] = PyInt_AsLong(o
);
826 char** string_LIST_helper(PyObject
* source
) {
827 if (!PyList_Check(source
)) {
828 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
831 int count
= PyList_Size(source
);
832 char** temp
= new char*[count
];
834 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
837 for (int x
=0; x
<count
; x
++) {
838 PyObject
* o
= PyList_GetItem(source
, x
);
839 if (! PyString_Check(o
)) {
840 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
843 temp
[x
] = PyString_AsString(o
);
848 //--------------------------------
849 // Part of patch from Tim Hochberg
850 static inline bool wxPointFromObjects(PyObject
* o1
, PyObject
* o2
, wxPoint
* point
) {
851 if (PyInt_Check(o1
) && PyInt_Check(o2
)) {
852 point
->x
= PyInt_AS_LONG(o1
);
853 point
->y
= PyInt_AS_LONG(o2
);
856 if (PyFloat_Check(o1
) && PyFloat_Check(o2
)) {
857 point
->x
= (int)PyFloat_AS_DOUBLE(o1
);
858 point
->y
= (int)PyFloat_AS_DOUBLE(o2
);
861 if (PyInstance_Check(o1
) || PyInstance_Check(o2
)) {
862 // Disallow instances because they can cause havok
865 if (PyNumber_Check(o1
) && PyNumber_Check(o2
)) {
866 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
867 point
->x
= PyInt_AsLong(o1
);
868 point
->y
= PyInt_AsLong(o2
);
875 wxPoint
* wxPoint_LIST_helper(PyObject
* source
, int *count
) {
876 // Putting all of the declarations here allows
877 // us to put the error handling all in one place.
880 PyObject
*o
, *o1
, *o2
;
881 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
883 if (!PySequence_Check(source
)) {
887 // The length of the sequence is returned in count.
888 *count
= PySequence_Length(source
);
893 temp
= new wxPoint
[*count
];
895 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
898 for (x
=0; x
<*count
; x
++) {
899 // Get an item: try fast way first.
901 o
= PySequence_Fast_GET_ITEM(source
, x
);
904 o
= PySequence_GetItem(source
, x
);
910 // Convert o to wxPoint.
911 if ((PyTuple_Check(o
) && PyTuple_GET_SIZE(o
) == 2) ||
912 (PyList_Check(o
) && PyList_GET_SIZE(o
) == 2)) {
913 o1
= PySequence_Fast_GET_ITEM(o
, 0);
914 o2
= PySequence_Fast_GET_ITEM(o
, 1);
915 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
919 else if (PyInstance_Check(o
)) {
921 if (SWIG_GetPtrObj(o
, (void **)&pt
, "_wxPoint_p")) {
926 else if (PySequence_Check(o
) && PySequence_Length(o
) == 2) {
927 o1
= PySequence_GetItem(o
, 0);
928 o2
= PySequence_GetItem(o
, 1);
929 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
953 PyErr_SetString(PyExc_TypeError
, "Expected a sequence of length-2 sequences or wxPoints.");
957 //------------------------------
960 wxBitmap
** wxBitmap_LIST_helper(PyObject
* source
) {
961 if (!PyList_Check(source
)) {
962 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
965 int count
= PyList_Size(source
);
966 wxBitmap
** temp
= new wxBitmap
*[count
];
968 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
971 for (int x
=0; x
<count
; x
++) {
972 PyObject
* o
= PyList_GetItem(source
, x
);
973 if (PyInstance_Check(o
)) {
975 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxBitmap_p")) {
976 PyErr_SetString(PyExc_TypeError
,"Expected _wxBitmap_p.");
982 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxBitmaps.");
991 wxString
* wxString_LIST_helper(PyObject
* source
) {
992 if (!PyList_Check(source
)) {
993 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
996 int count
= PyList_Size(source
);
997 wxString
* temp
= new wxString
[count
];
999 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1002 for (int x
=0; x
<count
; x
++) {
1003 PyObject
* o
= PyList_GetItem(source
, x
);
1004 #if PYTHON_API_VERSION >= 1009
1005 if (! PyString_Check(o
) && ! PyUnicode_Check(o
)) {
1006 PyErr_SetString(PyExc_TypeError
, "Expected a list of string or unicode objects.");
1012 if (PyString_AsStringAndSize(o
, &buff
, &length
) == -1)
1014 temp
[x
] = wxString(buff
, length
);
1016 if (! PyString_Check(o
)) {
1017 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1020 temp
[x
] = PyString_AsString(o
);
1027 wxAcceleratorEntry
* wxAcceleratorEntry_LIST_helper(PyObject
* source
) {
1028 if (!PyList_Check(source
)) {
1029 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1032 int count
= PyList_Size(source
);
1033 wxAcceleratorEntry
* temp
= new wxAcceleratorEntry
[count
];
1035 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1038 for (int x
=0; x
<count
; x
++) {
1039 PyObject
* o
= PyList_GetItem(source
, x
);
1040 if (PyInstance_Check(o
)) {
1041 wxAcceleratorEntry
* ae
;
1042 if (SWIG_GetPtrObj(o
, (void **) &ae
,"_wxAcceleratorEntry_p")) {
1043 PyErr_SetString(PyExc_TypeError
,"Expected _wxAcceleratorEntry_p.");
1048 else if (PyTuple_Check(o
)) {
1049 PyObject
* o1
= PyTuple_GetItem(o
, 0);
1050 PyObject
* o2
= PyTuple_GetItem(o
, 1);
1051 PyObject
* o3
= PyTuple_GetItem(o
, 2);
1052 temp
[x
].Set(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
));
1055 PyErr_SetString(PyExc_TypeError
, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
1063 wxPen
** wxPen_LIST_helper(PyObject
* source
) {
1064 if (!PyList_Check(source
)) {
1065 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1068 int count
= PyList_Size(source
);
1069 wxPen
** temp
= new wxPen
*[count
];
1071 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1074 for (int x
=0; x
<count
; x
++) {
1075 PyObject
* o
= PyList_GetItem(source
, x
);
1076 if (PyInstance_Check(o
)) {
1078 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxPen_p")) {
1080 PyErr_SetString(PyExc_TypeError
,"Expected _wxPen_p.");
1087 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxPens.");
1095 bool _2int_seq_helper(PyObject
* source
, int* i1
, int* i2
) {
1096 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1099 if (!PySequence_Check(source
) || PySequence_Length(source
) != 2)
1103 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1104 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1107 o1
= PySequence_GetItem(source
, 0);
1108 o2
= PySequence_GetItem(source
, 1);
1111 *i1
= PyInt_AsLong(o1
);
1112 *i2
= PyInt_AsLong(o2
);
1122 bool _4int_seq_helper(PyObject
* source
, int* i1
, int* i2
, int* i3
, int* i4
) {
1123 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1124 PyObject
*o1
, *o2
, *o3
, *o4
;
1126 if (!PySequence_Check(source
) || PySequence_Length(source
) != 4)
1130 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1131 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1132 o3
= PySequence_Fast_GET_ITEM(source
, 2);
1133 o4
= PySequence_Fast_GET_ITEM(source
, 3);
1136 o1
= PySequence_GetItem(source
, 0);
1137 o2
= PySequence_GetItem(source
, 1);
1138 o3
= PySequence_GetItem(source
, 2);
1139 o4
= PySequence_GetItem(source
, 3);
1142 *i1
= PyInt_AsLong(o1
);
1143 *i2
= PyInt_AsLong(o2
);
1144 *i3
= PyInt_AsLong(o3
);
1145 *i4
= PyInt_AsLong(o4
);
1157 //----------------------------------------------------------------------
1159 bool wxSize_helper(PyObject
* source
, wxSize
** obj
) {
1161 // If source is an object instance then it may already be the right type
1162 if (PyInstance_Check(source
)) {
1164 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxSize_p"))
1169 // otherwise a 2-tuple of integers is expected
1170 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1171 PyObject
* o1
= PySequence_GetItem(source
, 0);
1172 PyObject
* o2
= PySequence_GetItem(source
, 1);
1173 **obj
= wxSize(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1178 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxSize object.");
1182 bool wxPoint_helper(PyObject
* source
, wxPoint
** obj
) {
1184 // If source is an object instance then it may already be the right type
1185 if (PyInstance_Check(source
)) {
1187 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxPoint_p"))
1192 // otherwise a length-2 sequence of integers is expected
1193 if (PySequence_Check(source
) && PySequence_Length(source
) == 2) {
1194 PyObject
* o1
= PySequence_GetItem(source
, 0);
1195 PyObject
* o2
= PySequence_GetItem(source
, 1);
1196 // This should really check for integers, not numbers -- but that would break code.
1197 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1202 **obj
= wxPoint(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1208 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxPoint object.");
1214 bool wxRealPoint_helper(PyObject
* source
, wxRealPoint
** obj
) {
1216 // If source is an object instance then it may already be the right type
1217 if (PyInstance_Check(source
)) {
1219 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRealPoint_p"))
1224 // otherwise a 2-tuple of floats is expected
1225 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1226 PyObject
* o1
= PySequence_GetItem(source
, 0);
1227 PyObject
* o2
= PySequence_GetItem(source
, 1);
1228 **obj
= wxRealPoint(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
));
1233 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxRealPoint object.");
1240 bool wxRect_helper(PyObject
* source
, wxRect
** obj
) {
1242 // If source is an object instance then it may already be the right type
1243 if (PyInstance_Check(source
)) {
1245 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRect_p"))
1250 // otherwise a 4-tuple of integers is expected
1251 else if (PySequence_Check(source
) && PyObject_Length(source
) == 4) {
1252 PyObject
* o1
= PySequence_GetItem(source
, 0);
1253 PyObject
* o2
= PySequence_GetItem(source
, 1);
1254 PyObject
* o3
= PySequence_GetItem(source
, 2);
1255 PyObject
* o4
= PySequence_GetItem(source
, 3);
1256 **obj
= wxRect(PyInt_AsLong(o1
), PyInt_AsLong(o2
),
1257 PyInt_AsLong(o3
), PyInt_AsLong(o4
));
1262 PyErr_SetString(PyExc_TypeError
, "Expected a 4-tuple of integers or a wxRect object.");
1268 bool wxColour_helper(PyObject
* source
, wxColour
** obj
) {
1270 // If source is an object instance then it may already be the right type
1271 if (PyInstance_Check(source
)) {
1273 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxColour_p"))
1278 // otherwise a string is expected
1279 else if (PyString_Check(source
)) {
1280 wxString spec
= PyString_AS_STRING(source
);
1281 if (spec
[0U] == '#' && spec
.Length() == 7) { // It's #RRGGBB
1283 int red
= strtol(spec
.Mid(1,2), &junk
, 16);
1284 int green
= strtol(spec
.Mid(3,2), &junk
, 16);
1285 int blue
= strtol(spec
.Mid(5,2), &junk
, 16);
1286 **obj
= wxColour(red
, green
, blue
);
1289 else { // it's a colour name
1290 **obj
= wxColour(spec
);
1296 PyErr_SetString(PyExc_TypeError
, "Expected a wxColour object or a string containing a colour name or '#RRGGBB'.");
1301 //----------------------------------------------------------------------
1303 PyObject
* wxArrayString2PyList_helper(const wxArrayString
& arr
) {
1305 PyObject
* list
= PyList_New(0);
1306 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
1307 PyObject
* str
= PyString_FromString(arr
[i
].c_str());
1308 PyList_Append(list
, str
);
1309 // TODO: Check refcount on str...
1315 //----------------------------------------------------------------------
1316 //----------------------------------------------------------------------