]>
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
, 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...
405 sprintf(buff
, "%sPtr", className
);
407 wxASSERT_MSG(wxPython_dict
, "wxPython_dict is not set yet!!");
409 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
413 "*** Unknown class name %s, tell Robin about it please ***",
415 obj
= PyString_FromString(temp
);
419 return wxPyConstructObject(ptr
, className
, classobj
, setThisOwn
);
422 //---------------------------------------------------------------------------
425 wxPyTState
* wxPyBeginBlockThreads() {
426 wxPyTState
* state
= NULL
;
427 #ifdef WXP_WITH_THREAD
428 if (1) { // Can I check if I've already got the lock?
429 state
= new wxPyTState
;
430 PyEval_AcquireLock();
431 state
->newState
= PyThreadState_New(wxPyInterpreter
);
432 state
->prevState
= PyThreadState_Swap(state
->newState
);
439 void wxPyEndBlockThreads(wxPyTState
* state
) {
440 #ifdef WXP_WITH_THREAD
442 PyThreadState_Swap(state
->prevState
);
443 PyThreadState_Clear(state
->newState
);
444 PyEval_ReleaseLock();
445 PyThreadState_Delete(state
->newState
);
452 //---------------------------------------------------------------------------
454 IMPLEMENT_ABSTRACT_CLASS(wxPyCallback
, wxObject
);
456 wxPyCallback::wxPyCallback(PyObject
* func
) {
461 wxPyCallback::wxPyCallback(const wxPyCallback
& other
) {
462 m_func
= other
.m_func
;
466 wxPyCallback::~wxPyCallback() {
467 wxPyTState
* state
= wxPyBeginBlockThreads();
469 wxPyEndBlockThreads(state
);
474 // This function is used for all events destined for Python event handlers.
475 void wxPyCallback::EventThunker(wxEvent
& event
) {
476 wxPyCallback
* cb
= (wxPyCallback
*)event
.m_callbackUserData
;
477 PyObject
* func
= cb
->m_func
;
483 wxPyTState
* state
= wxPyBeginBlockThreads();
484 wxString className
= event
.GetClassInfo()->GetClassName();
486 if (className
== "wxPyEvent")
487 arg
= ((wxPyEvent
*)&event
)->GetSelf();
488 else if (className
== "wxPyCommandEvent")
489 arg
= ((wxPyCommandEvent
*)&event
)->GetSelf();
491 arg
= wxPyConstructObject((void*)&event
, className
);
493 tuple
= PyTuple_New(1);
494 PyTuple_SET_ITEM(tuple
, 0, arg
);
495 result
= PyEval_CallObject(func
, tuple
);
499 PyErr_Clear(); // Just in case...
503 wxPyEndBlockThreads(state
);
507 //----------------------------------------------------------------------
509 wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper
& other
) {
511 m_self
= other
.m_self
;
512 m_class
= other
.m_class
;
520 void wxPyCallbackHelper::setSelf(PyObject
* self
, PyObject
* klass
, int incref
) {
531 // If the object (m_self) has an attibute of the given name, and if that
532 // attribute is a method, and if that method's class is not from a base class,
533 // then we'll save a pointer to the method so callCallback can call it.
534 bool wxPyCallbackHelper::findCallback(const char* name
) const {
535 wxPyCallbackHelper
* self
= (wxPyCallbackHelper
*)this; // cast away const
536 self
->m_lastFound
= NULL
;
537 if (m_self
&& PyObject_HasAttrString(m_self
, (char*)name
)) {
539 method
= PyObject_GetAttrString(m_self
, (char*)name
);
541 if (PyMethod_Check(method
) &&
542 ((PyMethod_GET_CLASS(method
) == m_class
) ||
543 PyClass_IsSubclass(PyMethod_GET_CLASS(method
), m_class
))) {
545 self
->m_lastFound
= method
;
551 return m_lastFound
!= NULL
;
555 int wxPyCallbackHelper::callCallback(PyObject
* argTuple
) const {
559 result
= callCallbackObj(argTuple
);
560 if (result
) { // Assumes an integer return type...
561 retval
= PyInt_AsLong(result
);
563 PyErr_Clear(); // forget about it if it's not...
568 // Invoke the Python callable object, returning the raw PyObject return
569 // value. Caller should DECREF the return value and also call PyEval_SaveThread.
570 PyObject
* wxPyCallbackHelper::callCallbackObj(PyObject
* argTuple
) const {
573 // Save a copy of the pointer in case the callback generates another
574 // callback. In that case m_lastFound will have a different value when
575 // it gets back here...
576 PyObject
* method
= m_lastFound
;
578 result
= PyEval_CallObject(method
, argTuple
);
588 void wxPyCBH_setCallbackInfo(wxPyCallbackHelper
& cbh
, PyObject
* self
, PyObject
* klass
, int incref
) {
589 cbh
.setSelf(self
, klass
, incref
);
592 bool wxPyCBH_findCallback(const wxPyCallbackHelper
& cbh
, const char* name
) {
593 return cbh
.findCallback(name
);
596 int wxPyCBH_callCallback(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
597 return cbh
.callCallback(argTuple
);
600 PyObject
* wxPyCBH_callCallbackObj(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
601 return cbh
.callCallbackObj(argTuple
);
605 void wxPyCBH_delete(wxPyCallbackHelper
* cbh
) {
607 wxPyTState
* state
= wxPyBeginBlockThreads();
608 Py_XDECREF(cbh
->m_self
);
609 Py_XDECREF(cbh
->m_class
);
610 wxPyEndBlockThreads(state
);
614 //---------------------------------------------------------------------------
615 //---------------------------------------------------------------------------
616 // These event classes can be derived from in Python and passed through the event
617 // system without losing anything. They do this by keeping a reference to
618 // themselves and some special case handling in wxPyCallback::EventThunker.
621 wxPyEvtSelfRef::wxPyEvtSelfRef() {
622 //m_self = Py_None; // **** We don't do normal ref counting to prevent
623 //Py_INCREF(m_self); // circular loops...
627 wxPyEvtSelfRef::~wxPyEvtSelfRef() {
628 wxPyTState
* state
= wxPyBeginBlockThreads();
631 wxPyEndBlockThreads(state
);
634 void wxPyEvtSelfRef::SetSelf(PyObject
* self
, bool clone
) {
635 wxPyTState
* state
= wxPyBeginBlockThreads();
643 wxPyEndBlockThreads(state
);
646 PyObject
* wxPyEvtSelfRef::GetSelf() const {
652 IMPLEMENT_ABSTRACT_CLASS(wxPyEvent
, wxEvent
);
653 IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent
, wxCommandEvent
);
656 wxPyEvent::wxPyEvent(int id
)
661 wxPyEvent::wxPyEvent(const wxPyEvent
& evt
)
664 SetSelf(evt
.m_self
, TRUE
);
668 wxPyEvent::~wxPyEvent() {
672 wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType
, int id
)
673 : wxCommandEvent(commandType
, id
) {
677 wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent
& evt
)
678 : wxCommandEvent(evt
)
680 SetSelf(evt
.m_self
, TRUE
);
684 wxPyCommandEvent::~wxPyCommandEvent() {
690 //---------------------------------------------------------------------------
691 //---------------------------------------------------------------------------
694 wxPyTimer::wxPyTimer(PyObject
* callback
) {
699 wxPyTimer::~wxPyTimer() {
700 wxPyTState
* state
= wxPyBeginBlockThreads();
702 wxPyEndBlockThreads(state
);
705 void wxPyTimer::Notify() {
706 if (!func
|| func
== Py_None
) {
710 wxPyTState
* state
= wxPyBeginBlockThreads();
713 PyObject
* args
= Py_BuildValue("()");
715 result
= PyEval_CallObject(func
, args
);
724 wxPyEndBlockThreads(state
);
730 //---------------------------------------------------------------------------
731 //---------------------------------------------------------------------------
732 // Convert a wxList to a Python List
734 PyObject
* wxPy_ConvertList(wxListBase
* list
, const char* className
) {
738 wxNode
* node
= list
->First();
740 wxPyTState
* state
= wxPyBeginBlockThreads();
741 pyList
= PyList_New(0);
743 wxObj
= node
->Data();
744 pyObj
= wxPyMake_wxObject(wxObj
); //wxPyConstructObject(wxObj, className);
745 PyList_Append(pyList
, pyObj
);
748 wxPyEndBlockThreads(state
);
752 //----------------------------------------------------------------------
754 long wxPyGetWinHandle(wxWindow
* win
) {
756 return (long)win
->GetHandle();
759 // Find and return the actual X-Window.
761 if (win
->m_wxwindow
) {
762 GdkWindowPrivate
* bwin
= (GdkWindowPrivate
*)GTK_PIZZA(win
->m_wxwindow
)->bin_window
;
764 return (long)bwin
->xwindow
;
771 //----------------------------------------------------------------------
772 // Some helper functions for typemaps in my_typemaps.i, so they won't be
773 // included in every file...
776 byte
* byte_LIST_helper(PyObject
* source
) {
777 if (!PyList_Check(source
)) {
778 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
781 int count
= PyList_Size(source
);
782 byte
* temp
= new byte
[count
];
784 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
787 for (int x
=0; x
<count
; x
++) {
788 PyObject
* o
= PyList_GetItem(source
, x
);
789 if (! PyInt_Check(o
)) {
790 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
793 temp
[x
] = (byte
)PyInt_AsLong(o
);
799 int* int_LIST_helper(PyObject
* source
) {
800 if (!PyList_Check(source
)) {
801 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
804 int count
= PyList_Size(source
);
805 int* temp
= new int[count
];
807 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
810 for (int x
=0; x
<count
; x
++) {
811 PyObject
* o
= PyList_GetItem(source
, x
);
812 if (! PyInt_Check(o
)) {
813 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
816 temp
[x
] = PyInt_AsLong(o
);
822 long* long_LIST_helper(PyObject
* source
) {
823 if (!PyList_Check(source
)) {
824 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
827 int count
= PyList_Size(source
);
828 long* temp
= new long[count
];
830 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
833 for (int x
=0; x
<count
; x
++) {
834 PyObject
* o
= PyList_GetItem(source
, x
);
835 if (! PyInt_Check(o
)) {
836 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
839 temp
[x
] = PyInt_AsLong(o
);
845 char** string_LIST_helper(PyObject
* source
) {
846 if (!PyList_Check(source
)) {
847 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
850 int count
= PyList_Size(source
);
851 char** temp
= new char*[count
];
853 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
856 for (int x
=0; x
<count
; x
++) {
857 PyObject
* o
= PyList_GetItem(source
, x
);
858 if (! PyString_Check(o
)) {
859 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
862 temp
[x
] = PyString_AsString(o
);
867 //--------------------------------
868 // Part of patch from Tim Hochberg
869 static inline bool wxPointFromObjects(PyObject
* o1
, PyObject
* o2
, wxPoint
* point
) {
870 if (PyInt_Check(o1
) && PyInt_Check(o2
)) {
871 point
->x
= PyInt_AS_LONG(o1
);
872 point
->y
= PyInt_AS_LONG(o2
);
875 if (PyFloat_Check(o1
) && PyFloat_Check(o2
)) {
876 point
->x
= (int)PyFloat_AS_DOUBLE(o1
);
877 point
->y
= (int)PyFloat_AS_DOUBLE(o2
);
880 if (PyInstance_Check(o1
) || PyInstance_Check(o2
)) {
881 // Disallow instances because they can cause havok
884 if (PyNumber_Check(o1
) && PyNumber_Check(o2
)) {
885 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
886 point
->x
= PyInt_AsLong(o1
);
887 point
->y
= PyInt_AsLong(o2
);
894 wxPoint
* wxPoint_LIST_helper(PyObject
* source
, int *count
) {
895 // Putting all of the declarations here allows
896 // us to put the error handling all in one place.
899 PyObject
*o
, *o1
, *o2
;
900 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
902 if (!PySequence_Check(source
)) {
906 // The length of the sequence is returned in count.
907 *count
= PySequence_Length(source
);
912 temp
= new wxPoint
[*count
];
914 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
917 for (x
=0; x
<*count
; x
++) {
918 // Get an item: try fast way first.
920 o
= PySequence_Fast_GET_ITEM(source
, x
);
923 o
= PySequence_GetItem(source
, x
);
929 // Convert o to wxPoint.
930 if ((PyTuple_Check(o
) && PyTuple_GET_SIZE(o
) == 2) ||
931 (PyList_Check(o
) && PyList_GET_SIZE(o
) == 2)) {
932 o1
= PySequence_Fast_GET_ITEM(o
, 0);
933 o2
= PySequence_Fast_GET_ITEM(o
, 1);
934 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
938 else if (PyInstance_Check(o
)) {
940 if (SWIG_GetPtrObj(o
, (void **)&pt
, "_wxPoint_p")) {
945 else if (PySequence_Check(o
) && PySequence_Length(o
) == 2) {
946 o1
= PySequence_GetItem(o
, 0);
947 o2
= PySequence_GetItem(o
, 1);
948 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
972 PyErr_SetString(PyExc_TypeError
, "Expected a sequence of length-2 sequences or wxPoints.");
976 //------------------------------
979 wxBitmap
** wxBitmap_LIST_helper(PyObject
* source
) {
980 if (!PyList_Check(source
)) {
981 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
984 int count
= PyList_Size(source
);
985 wxBitmap
** temp
= new wxBitmap
*[count
];
987 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
990 for (int x
=0; x
<count
; x
++) {
991 PyObject
* o
= PyList_GetItem(source
, x
);
992 if (PyInstance_Check(o
)) {
994 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxBitmap_p")) {
995 PyErr_SetString(PyExc_TypeError
,"Expected _wxBitmap_p.");
1001 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxBitmaps.");
1010 wxString
* wxString_LIST_helper(PyObject
* source
) {
1011 if (!PyList_Check(source
)) {
1012 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1015 int count
= PyList_Size(source
);
1016 wxString
* temp
= new wxString
[count
];
1018 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1021 for (int x
=0; x
<count
; x
++) {
1022 PyObject
* o
= PyList_GetItem(source
, x
);
1023 #if PYTHON_API_VERSION >= 1009
1024 if (! PyString_Check(o
) && ! PyUnicode_Check(o
)) {
1025 PyErr_SetString(PyExc_TypeError
, "Expected a list of string or unicode objects.");
1031 if (PyString_AsStringAndSize(o
, &buff
, &length
) == -1)
1033 temp
[x
] = wxString(buff
, length
);
1035 if (! PyString_Check(o
)) {
1036 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1039 temp
[x
] = PyString_AsString(o
);
1046 wxAcceleratorEntry
* wxAcceleratorEntry_LIST_helper(PyObject
* source
) {
1047 if (!PyList_Check(source
)) {
1048 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1051 int count
= PyList_Size(source
);
1052 wxAcceleratorEntry
* temp
= new wxAcceleratorEntry
[count
];
1054 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1057 for (int x
=0; x
<count
; x
++) {
1058 PyObject
* o
= PyList_GetItem(source
, x
);
1059 if (PyInstance_Check(o
)) {
1060 wxAcceleratorEntry
* ae
;
1061 if (SWIG_GetPtrObj(o
, (void **) &ae
,"_wxAcceleratorEntry_p")) {
1062 PyErr_SetString(PyExc_TypeError
,"Expected _wxAcceleratorEntry_p.");
1067 else if (PyTuple_Check(o
)) {
1068 PyObject
* o1
= PyTuple_GetItem(o
, 0);
1069 PyObject
* o2
= PyTuple_GetItem(o
, 1);
1070 PyObject
* o3
= PyTuple_GetItem(o
, 2);
1071 temp
[x
].Set(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
));
1074 PyErr_SetString(PyExc_TypeError
, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
1082 wxPen
** wxPen_LIST_helper(PyObject
* source
) {
1083 if (!PyList_Check(source
)) {
1084 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1087 int count
= PyList_Size(source
);
1088 wxPen
** temp
= new wxPen
*[count
];
1090 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1093 for (int x
=0; x
<count
; x
++) {
1094 PyObject
* o
= PyList_GetItem(source
, x
);
1095 if (PyInstance_Check(o
)) {
1097 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxPen_p")) {
1099 PyErr_SetString(PyExc_TypeError
,"Expected _wxPen_p.");
1106 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxPens.");
1114 bool _2int_seq_helper(PyObject
* source
, int* i1
, int* i2
) {
1115 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1118 if (!PySequence_Check(source
) || PySequence_Length(source
) != 2)
1122 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1123 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1126 o1
= PySequence_GetItem(source
, 0);
1127 o2
= PySequence_GetItem(source
, 1);
1130 *i1
= PyInt_AsLong(o1
);
1131 *i2
= PyInt_AsLong(o2
);
1141 bool _4int_seq_helper(PyObject
* source
, int* i1
, int* i2
, int* i3
, int* i4
) {
1142 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1143 PyObject
*o1
, *o2
, *o3
, *o4
;
1145 if (!PySequence_Check(source
) || PySequence_Length(source
) != 4)
1149 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1150 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1151 o3
= PySequence_Fast_GET_ITEM(source
, 2);
1152 o4
= PySequence_Fast_GET_ITEM(source
, 3);
1155 o1
= PySequence_GetItem(source
, 0);
1156 o2
= PySequence_GetItem(source
, 1);
1157 o3
= PySequence_GetItem(source
, 2);
1158 o4
= PySequence_GetItem(source
, 3);
1161 *i1
= PyInt_AsLong(o1
);
1162 *i2
= PyInt_AsLong(o2
);
1163 *i3
= PyInt_AsLong(o3
);
1164 *i4
= PyInt_AsLong(o4
);
1176 //----------------------------------------------------------------------
1178 bool wxSize_helper(PyObject
* source
, wxSize
** obj
) {
1180 // If source is an object instance then it may already be the right type
1181 if (PyInstance_Check(source
)) {
1183 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxSize_p"))
1188 // otherwise a 2-tuple of integers is expected
1189 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1190 PyObject
* o1
= PySequence_GetItem(source
, 0);
1191 PyObject
* o2
= PySequence_GetItem(source
, 1);
1192 **obj
= wxSize(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1197 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxSize object.");
1201 bool wxPoint_helper(PyObject
* source
, wxPoint
** obj
) {
1203 // If source is an object instance then it may already be the right type
1204 if (PyInstance_Check(source
)) {
1206 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxPoint_p"))
1211 // otherwise a length-2 sequence of integers is expected
1212 if (PySequence_Check(source
) && PySequence_Length(source
) == 2) {
1213 PyObject
* o1
= PySequence_GetItem(source
, 0);
1214 PyObject
* o2
= PySequence_GetItem(source
, 1);
1215 // This should really check for integers, not numbers -- but that would break code.
1216 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1221 **obj
= wxPoint(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1227 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxPoint object.");
1233 bool wxRealPoint_helper(PyObject
* source
, wxRealPoint
** obj
) {
1235 // If source is an object instance then it may already be the right type
1236 if (PyInstance_Check(source
)) {
1238 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRealPoint_p"))
1243 // otherwise a 2-tuple of floats is expected
1244 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1245 PyObject
* o1
= PySequence_GetItem(source
, 0);
1246 PyObject
* o2
= PySequence_GetItem(source
, 1);
1247 **obj
= wxRealPoint(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
));
1252 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxRealPoint object.");
1259 bool wxRect_helper(PyObject
* source
, wxRect
** obj
) {
1261 // If source is an object instance then it may already be the right type
1262 if (PyInstance_Check(source
)) {
1264 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRect_p"))
1269 // otherwise a 4-tuple of integers is expected
1270 else if (PySequence_Check(source
) && PyObject_Length(source
) == 4) {
1271 PyObject
* o1
= PySequence_GetItem(source
, 0);
1272 PyObject
* o2
= PySequence_GetItem(source
, 1);
1273 PyObject
* o3
= PySequence_GetItem(source
, 2);
1274 PyObject
* o4
= PySequence_GetItem(source
, 3);
1275 **obj
= wxRect(PyInt_AsLong(o1
), PyInt_AsLong(o2
),
1276 PyInt_AsLong(o3
), PyInt_AsLong(o4
));
1281 PyErr_SetString(PyExc_TypeError
, "Expected a 4-tuple of integers or a wxRect object.");
1287 bool wxColour_helper(PyObject
* source
, wxColour
** obj
) {
1289 // If source is an object instance then it may already be the right type
1290 if (PyInstance_Check(source
)) {
1292 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxColour_p"))
1297 // otherwise a string is expected
1298 else if (PyString_Check(source
)) {
1299 wxString spec
= PyString_AS_STRING(source
);
1300 if (spec
[0U] == '#' && spec
.Length() == 7) { // It's #RRGGBB
1302 int red
= strtol(spec
.Mid(1,2), &junk
, 16);
1303 int green
= strtol(spec
.Mid(3,2), &junk
, 16);
1304 int blue
= strtol(spec
.Mid(5,2), &junk
, 16);
1305 **obj
= wxColour(red
, green
, blue
);
1308 else { // it's a colour name
1309 **obj
= wxColour(spec
);
1315 PyErr_SetString(PyExc_TypeError
, "Expected a wxColour object or a string containing a colour name or '#RRGGBB'.");
1320 //----------------------------------------------------------------------
1322 PyObject
* wxArrayString2PyList_helper(const wxArrayString
& arr
) {
1324 PyObject
* list
= PyList_New(0);
1325 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
1326 PyObject
* str
= PyString_FromString(arr
[i
].c_str());
1327 PyList_Append(list
, str
);
1334 //----------------------------------------------------------------------
1335 //----------------------------------------------------------------------