]>
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
) {
296 wxClassInfo
* info
= source
->GetClassInfo();
297 wxChar
* name
= (wxChar
*)info
->GetClassName();
298 PyObject
* klass
= wxPyClassExists(name
);
299 while (info
&& !klass
) {
300 name
= (wxChar
*)info
->GetBaseClassName1();
301 info
= wxClassInfo::FindClass(name
);
302 klass
= wxPyClassExists(name
);
305 target
= wxPyConstructObject(source
, name
, klass
, FALSE
);
307 wxString
msg("wxPython class not found for ");
308 msg
+= source
->GetClassInfo()->GetClassName();
309 PyErr_SetString(PyExc_NameError
, msg
.c_str());
312 } else { // source was NULL so return None.
313 Py_INCREF(Py_None
); target
= Py_None
;
318 //---------------------------------------------------------------------------
320 PyObject
* wxPyConstructObject(void* ptr
,
321 const char* className
,
328 char swigptr
[64]; // should always be big enough...
331 if ((item
= PyDict_GetItemString(wxPyPtrTypeMap
, (char*)className
)) != NULL
) {
332 className
= PyString_AsString(item
);
334 sprintf(buff
, "_%s_p", className
);
335 SWIG_MakePtr(swigptr
, ptr
, buff
);
337 arg
= Py_BuildValue("(s)", swigptr
);
338 obj
= PyInstance_New(klass
, arg
, NULL
);
342 PyObject
* one
= PyInt_FromLong(1);
343 PyObject_SetAttrString(obj
, "thisown", one
);
351 PyObject
* wxPyConstructObject(void* ptr
,
352 const char* className
,
361 char buff
[64]; // should always be big enough...
363 sprintf(buff
, "%sPtr", className
);
364 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
368 "*** Unknown class name %s, tell Robin about it please ***",
370 obj
= PyString_FromString(temp
);
374 return wxPyConstructObject(ptr
, className
, classobj
, setThisOwn
);
377 //---------------------------------------------------------------------------
379 // static PyThreadState* myPyThreadState_Get() {
380 // PyThreadState* current;
381 // current = PyThreadState_Swap(NULL);
382 // PyThreadState_Swap(current);
387 // bool wxPyRestoreThread() {
388 // // NOTE: The Python API docs state that if a thread already has the
389 // // interpreter lock and calls PyEval_RestoreThread again a deadlock
390 // // occurs, so I put in this code as a guard condition since there are
391 // // many possibilites for nested events and callbacks in wxPython. If
392 // // The current thread is our thread, then we can assume that we
393 // // already have the lock. (I hope!)
395 // #ifdef WXP_WITH_THREAD
396 // if (wxPyEventThreadState != myPyThreadState_Get()) {
397 // PyEval_AcquireThread(wxPyEventThreadState);
406 // void wxPySaveThread(bool doSave) {
407 // #ifdef WXP_WITH_THREAD
409 // PyEval_ReleaseThread(wxPyEventThreadState);
416 wxPyTState
* wxPyBeginBlockThreads() {
417 wxPyTState
* state
= NULL
;
418 #ifdef WXP_WITH_THREAD
419 if (1) { // Can I check if I've already got the lock?
420 state
= new wxPyTState
;
421 PyEval_AcquireLock();
422 state
->newState
= PyThreadState_New(wxPyInterpreter
);
423 state
->prevState
= PyThreadState_Swap(state
->newState
);
430 void wxPyEndBlockThreads(wxPyTState
* state
) {
431 #ifdef WXP_WITH_THREAD
433 PyThreadState_Swap(state
->prevState
);
434 PyThreadState_Clear(state
->newState
);
435 PyEval_ReleaseLock();
436 PyThreadState_Delete(state
->newState
);
443 //---------------------------------------------------------------------------
445 IMPLEMENT_ABSTRACT_CLASS(wxPyCallback
, wxObject
);
447 wxPyCallback::wxPyCallback(PyObject
* func
) {
452 wxPyCallback::wxPyCallback(const wxPyCallback
& other
) {
453 m_func
= other
.m_func
;
457 wxPyCallback::~wxPyCallback() {
458 wxPyTState
* state
= wxPyBeginBlockThreads();
460 wxPyEndBlockThreads(state
);
465 // This function is used for all events destined for Python event handlers.
466 void wxPyCallback::EventThunker(wxEvent
& event
) {
467 wxPyCallback
* cb
= (wxPyCallback
*)event
.m_callbackUserData
;
468 PyObject
* func
= cb
->m_func
;
474 wxPyTState
* state
= wxPyBeginBlockThreads();
475 wxString className
= event
.GetClassInfo()->GetClassName();
477 if (className
== "wxPyEvent")
478 arg
= ((wxPyEvent
*)&event
)->GetSelf();
479 else if (className
== "wxPyCommandEvent")
480 arg
= ((wxPyCommandEvent
*)&event
)->GetSelf();
482 arg
= wxPyConstructObject((void*)&event
, className
);
484 tuple
= PyTuple_New(1);
485 PyTuple_SET_ITEM(tuple
, 0, arg
);
486 result
= PyEval_CallObject(func
, tuple
);
490 PyErr_Clear(); // Just in case...
494 wxPyEndBlockThreads(state
);
498 //----------------------------------------------------------------------
500 wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper
& other
) {
502 m_self
= other
.m_self
;
503 m_class
= other
.m_class
;
511 void wxPyCallbackHelper::setSelf(PyObject
* self
, PyObject
* klass
, int incref
) {
522 // If the object (m_self) has an attibute of the given name, and if that
523 // attribute is a method, and if that method's class is not from a base class,
524 // then we'll save a pointer to the method so callCallback can call it.
525 bool wxPyCallbackHelper::findCallback(const char* name
) const {
526 wxPyCallbackHelper
* self
= (wxPyCallbackHelper
*)this; // cast away const
527 self
->m_lastFound
= NULL
;
528 if (m_self
&& PyObject_HasAttrString(m_self
, (char*)name
)) {
530 method
= PyObject_GetAttrString(m_self
, (char*)name
);
532 if (PyMethod_Check(method
) &&
533 ((PyMethod_GET_CLASS(method
) == m_class
) ||
534 PyClass_IsSubclass(PyMethod_GET_CLASS(method
), m_class
))) {
536 self
->m_lastFound
= method
;
542 return m_lastFound
!= NULL
;
546 int wxPyCallbackHelper::callCallback(PyObject
* argTuple
) const {
550 result
= callCallbackObj(argTuple
);
551 if (result
) { // Assumes an integer return type...
552 retval
= PyInt_AsLong(result
);
554 PyErr_Clear(); // forget about it if it's not...
559 // Invoke the Python callable object, returning the raw PyObject return
560 // value. Caller should DECREF the return value and also call PyEval_SaveThread.
561 PyObject
* wxPyCallbackHelper::callCallbackObj(PyObject
* argTuple
) const {
564 // Save a copy of the pointer in case the callback generates another
565 // callback. In that case m_lastFound will have a different value when
566 // it gets back here...
567 PyObject
* method
= m_lastFound
;
569 result
= PyEval_CallObject(method
, argTuple
);
579 void wxPyCBH_setSelf(wxPyCallbackHelper
& cbh
, PyObject
* self
, PyObject
* klass
, int incref
) {
580 cbh
.setSelf(self
, klass
, incref
);
583 bool wxPyCBH_findCallback(const wxPyCallbackHelper
& cbh
, const char* name
) {
584 return cbh
.findCallback(name
);
587 int wxPyCBH_callCallback(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
588 return cbh
.callCallback(argTuple
);
591 PyObject
* wxPyCBH_callCallbackObj(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
592 return cbh
.callCallbackObj(argTuple
);
596 void wxPyCBH_delete(wxPyCallbackHelper
* cbh
) {
598 wxPyTState
* state
= wxPyBeginBlockThreads();
599 Py_XDECREF(cbh
->m_self
);
600 Py_XDECREF(cbh
->m_class
);
601 wxPyEndBlockThreads(state
);
605 //---------------------------------------------------------------------------
606 //---------------------------------------------------------------------------
607 // These classes can be derived from in Python and passed through the event
608 // system without losing anything. They do this by keeping a reference to
609 // themselves and some special case handling in wxPyCallback::EventThunker.
612 wxPyEvtSelfRef::wxPyEvtSelfRef() {
613 //m_self = Py_None; // **** We don't do normal ref counting to prevent
614 //Py_INCREF(m_self); // circular loops...
618 wxPyEvtSelfRef::~wxPyEvtSelfRef() {
619 wxPyTState
* state
= wxPyBeginBlockThreads();
622 wxPyEndBlockThreads(state
);
625 void wxPyEvtSelfRef::SetSelf(PyObject
* self
, bool clone
) {
626 wxPyTState
* state
= wxPyBeginBlockThreads();
634 wxPyEndBlockThreads(state
);
637 PyObject
* wxPyEvtSelfRef::GetSelf() const {
643 wxPyEvent::wxPyEvent(int id
)
647 wxPyEvent::~wxPyEvent() {
650 // This one is so the event object can be Cloned...
651 void wxPyEvent::CopyObject(wxObject
& dest
) const {
652 wxEvent::CopyObject(dest
);
653 ((wxPyEvent
*)&dest
)->SetSelf(m_self
, TRUE
);
657 IMPLEMENT_DYNAMIC_CLASS(wxPyEvent
, wxEvent
);
660 wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType
, int id
)
661 : wxCommandEvent(commandType
, id
) {
664 wxPyCommandEvent::~wxPyCommandEvent() {
667 void wxPyCommandEvent::CopyObject(wxObject
& dest
) const {
668 wxCommandEvent::CopyObject(dest
);
669 ((wxPyCommandEvent
*)&dest
)->SetSelf(m_self
, TRUE
);
673 IMPLEMENT_DYNAMIC_CLASS(wxPyCommandEvent
, wxCommandEvent
);
677 //---------------------------------------------------------------------------
678 //---------------------------------------------------------------------------
681 wxPyTimer::wxPyTimer(PyObject
* callback
) {
686 wxPyTimer::~wxPyTimer() {
687 wxPyTState
* state
= wxPyBeginBlockThreads();
689 wxPyEndBlockThreads(state
);
692 void wxPyTimer::Notify() {
693 if (!func
|| func
== Py_None
) {
697 wxPyTState
* state
= wxPyBeginBlockThreads();
700 PyObject
* args
= Py_BuildValue("()");
702 result
= PyEval_CallObject(func
, args
);
711 wxPyEndBlockThreads(state
);
717 //---------------------------------------------------------------------------
718 //---------------------------------------------------------------------------
719 // Convert a wxList to a Python List
721 PyObject
* wxPy_ConvertList(wxListBase
* list
, const char* className
) {
725 wxNode
* node
= list
->First();
727 wxPyTState
* state
= wxPyBeginBlockThreads();
728 pyList
= PyList_New(0);
730 wxObj
= node
->Data();
731 pyObj
= wxPyMake_wxObject(wxObj
); //wxPyConstructObject(wxObj, className);
732 PyList_Append(pyList
, pyObj
);
735 wxPyEndBlockThreads(state
);
739 //----------------------------------------------------------------------
741 long wxPyGetWinHandle(wxWindow
* win
) {
743 return (long)win
->GetHandle();
746 // Find and return the actual X-Window.
748 if (win
->m_wxwindow
) {
749 GdkWindowPrivate
* bwin
= (GdkWindowPrivate
*)GTK_PIZZA(win
->m_wxwindow
)->bin_window
;
751 return (long)bwin
->xwindow
;
758 //----------------------------------------------------------------------
759 // Some helper functions for typemaps in my_typemaps.i, so they won't be
760 // included in every file...
763 byte
* byte_LIST_helper(PyObject
* source
) {
764 if (!PyList_Check(source
)) {
765 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
768 int count
= PyList_Size(source
);
769 byte
* temp
= new byte
[count
];
771 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
774 for (int x
=0; x
<count
; x
++) {
775 PyObject
* o
= PyList_GetItem(source
, x
);
776 if (! PyInt_Check(o
)) {
777 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
780 temp
[x
] = (byte
)PyInt_AsLong(o
);
786 int* int_LIST_helper(PyObject
* source
) {
787 if (!PyList_Check(source
)) {
788 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
791 int count
= PyList_Size(source
);
792 int* temp
= new int[count
];
794 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
797 for (int x
=0; x
<count
; x
++) {
798 PyObject
* o
= PyList_GetItem(source
, x
);
799 if (! PyInt_Check(o
)) {
800 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
803 temp
[x
] = PyInt_AsLong(o
);
809 long* long_LIST_helper(PyObject
* source
) {
810 if (!PyList_Check(source
)) {
811 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
814 int count
= PyList_Size(source
);
815 long* temp
= new long[count
];
817 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
820 for (int x
=0; x
<count
; x
++) {
821 PyObject
* o
= PyList_GetItem(source
, x
);
822 if (! PyInt_Check(o
)) {
823 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
826 temp
[x
] = PyInt_AsLong(o
);
832 char** string_LIST_helper(PyObject
* source
) {
833 if (!PyList_Check(source
)) {
834 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
837 int count
= PyList_Size(source
);
838 char** temp
= new char*[count
];
840 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
843 for (int x
=0; x
<count
; x
++) {
844 PyObject
* o
= PyList_GetItem(source
, x
);
845 if (! PyString_Check(o
)) {
846 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
849 temp
[x
] = PyString_AsString(o
);
854 //--------------------------------
855 // Part of patch from Tim Hochberg
856 static inline bool wxPointFromObjects(PyObject
* o1
, PyObject
* o2
, wxPoint
* point
) {
857 if (PyInt_Check(o1
) && PyInt_Check(o2
)) {
858 point
->x
= PyInt_AS_LONG(o1
);
859 point
->y
= PyInt_AS_LONG(o2
);
862 if (PyFloat_Check(o1
) && PyFloat_Check(o2
)) {
863 point
->x
= (int)PyFloat_AS_DOUBLE(o1
);
864 point
->y
= (int)PyFloat_AS_DOUBLE(o2
);
867 if (PyInstance_Check(o1
) || PyInstance_Check(o2
)) {
868 // Disallow instances because they can cause havok
871 if (PyNumber_Check(o1
) && PyNumber_Check(o2
)) {
872 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
873 point
->x
= PyInt_AsLong(o1
);
874 point
->y
= PyInt_AsLong(o2
);
881 #if PYTHON_API_VERSION < 1009
882 #define PySequence_Fast_GET_ITEM(o, i)\
883 (PyList_Check(o) ? PyList_GET_ITEM(o, i) : PyTuple_GET_ITEM(o, i))
886 wxPoint
* wxPoint_LIST_helper(PyObject
* source
, int *count
) {
887 // Putting all of the declarations here allows
888 // us to put the error handling all in one place.
891 PyObject
*o
, *o1
, *o2
;
892 int isFast
= PyList_Check(source
) || PyTuple_Check(source
);
894 // The length of the sequence is returned in count.
895 if (!PySequence_Check(source
)) {
898 *count
= PySequence_Length(source
);
903 temp
= new wxPoint
[*count
];
905 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
908 for (x
=0; x
<*count
; x
++) {
909 // Get an item: try fast way first.
911 o
= PySequence_Fast_GET_ITEM(source
, x
);
914 o
= PySequence_GetItem(source
, x
);
920 // Convert o to wxPoint.
921 if ((PyTuple_Check(o
) && PyTuple_GET_SIZE(o
) == 2) ||
922 (PyList_Check(o
) && PyList_GET_SIZE(o
) == 2)) {
923 o1
= PySequence_Fast_GET_ITEM(o
, 0);
924 o2
= PySequence_Fast_GET_ITEM(o
, 1);
925 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
929 else if (PyInstance_Check(o
)) {
931 if (SWIG_GetPtrObj(o
, (void **)&pt
, "_wxPoint_p")) {
936 else if (PySequence_Check(o
) && PySequence_Length(o
) == 2) {
937 o1
= PySequence_GetItem(o
, 0);
938 o2
= PySequence_GetItem(o
, 1);
939 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
963 PyErr_SetString(PyExc_TypeError
, "Expected a sequence of length-2 sequences or wxPoints.");
967 //------------------------------
970 wxBitmap
** wxBitmap_LIST_helper(PyObject
* source
) {
971 if (!PyList_Check(source
)) {
972 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
975 int count
= PyList_Size(source
);
976 wxBitmap
** temp
= new wxBitmap
*[count
];
978 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
981 for (int x
=0; x
<count
; x
++) {
982 PyObject
* o
= PyList_GetItem(source
, x
);
983 if (PyInstance_Check(o
)) {
985 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxBitmap_p")) {
986 PyErr_SetString(PyExc_TypeError
,"Expected _wxBitmap_p.");
992 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxBitmaps.");
1001 wxString
* wxString_LIST_helper(PyObject
* source
) {
1002 if (!PyList_Check(source
)) {
1003 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1006 int count
= PyList_Size(source
);
1007 wxString
* temp
= new wxString
[count
];
1009 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1012 for (int x
=0; x
<count
; x
++) {
1013 PyObject
* o
= PyList_GetItem(source
, x
);
1014 if (! PyString_Check(o
)) {
1015 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1018 temp
[x
] = PyString_AsString(o
);
1024 wxAcceleratorEntry
* wxAcceleratorEntry_LIST_helper(PyObject
* source
) {
1025 if (!PyList_Check(source
)) {
1026 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1029 int count
= PyList_Size(source
);
1030 wxAcceleratorEntry
* temp
= new wxAcceleratorEntry
[count
];
1032 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1035 for (int x
=0; x
<count
; x
++) {
1036 PyObject
* o
= PyList_GetItem(source
, x
);
1037 if (PyInstance_Check(o
)) {
1038 wxAcceleratorEntry
* ae
;
1039 if (SWIG_GetPtrObj(o
, (void **) &ae
,"_wxAcceleratorEntry_p")) {
1040 PyErr_SetString(PyExc_TypeError
,"Expected _wxAcceleratorEntry_p.");
1045 else if (PyTuple_Check(o
)) {
1046 PyObject
* o1
= PyTuple_GetItem(o
, 0);
1047 PyObject
* o2
= PyTuple_GetItem(o
, 1);
1048 PyObject
* o3
= PyTuple_GetItem(o
, 2);
1049 temp
[x
].Set(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
));
1052 PyErr_SetString(PyExc_TypeError
, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
1061 //----------------------------------------------------------------------
1063 bool wxSize_helper(PyObject
* source
, wxSize
** obj
) {
1065 // If source is an object instance then it may already be the right type
1066 if (PyInstance_Check(source
)) {
1068 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxSize_p"))
1073 // otherwise a 2-tuple of integers is expected
1074 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1075 PyObject
* o1
= PySequence_GetItem(source
, 0);
1076 PyObject
* o2
= PySequence_GetItem(source
, 1);
1077 **obj
= wxSize(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1082 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxSize object.");
1086 bool wxPoint_helper(PyObject
* source
, wxPoint
** obj
) {
1088 // If source is an object instance then it may already be the right type
1089 if (PyInstance_Check(source
)) {
1091 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxPoint_p"))
1096 // otherwise a length-2 sequence of integers is expected
1097 if (PySequence_Check(source
) && PySequence_Length(source
) == 2) {
1098 PyObject
* o1
= PySequence_GetItem(source
, 0);
1099 PyObject
* o2
= PySequence_GetItem(source
, 1);
1100 // This should really check for integers, not numbers -- but that would break code.
1101 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1106 **obj
= wxPoint(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1112 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxPoint object.");
1118 bool wxRealPoint_helper(PyObject
* source
, wxRealPoint
** obj
) {
1120 // If source is an object instance then it may already be the right type
1121 if (PyInstance_Check(source
)) {
1123 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRealPoint_p"))
1128 // otherwise a 2-tuple of floats is expected
1129 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1130 PyObject
* o1
= PySequence_GetItem(source
, 0);
1131 PyObject
* o2
= PySequence_GetItem(source
, 1);
1132 **obj
= wxRealPoint(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
));
1137 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxRealPoint object.");
1144 bool wxRect_helper(PyObject
* source
, wxRect
** obj
) {
1146 // If source is an object instance then it may already be the right type
1147 if (PyInstance_Check(source
)) {
1149 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRect_p"))
1154 // otherwise a 4-tuple of integers is expected
1155 else if (PySequence_Check(source
) && PyObject_Length(source
) == 4) {
1156 PyObject
* o1
= PySequence_GetItem(source
, 0);
1157 PyObject
* o2
= PySequence_GetItem(source
, 1);
1158 PyObject
* o3
= PySequence_GetItem(source
, 2);
1159 PyObject
* o4
= PySequence_GetItem(source
, 3);
1160 **obj
= wxRect(PyInt_AsLong(o1
), PyInt_AsLong(o2
),
1161 PyInt_AsLong(o3
), PyInt_AsLong(o4
));
1166 PyErr_SetString(PyExc_TypeError
, "Expected a 4-tuple of integers or a wxRect object.");
1172 bool wxColour_helper(PyObject
* source
, wxColour
** obj
) {
1174 // If source is an object instance then it may already be the right type
1175 if (PyInstance_Check(source
)) {
1177 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxColour_p"))
1182 // otherwise a string is expected
1183 else if (PyString_Check(source
)) {
1184 wxString spec
= PyString_AS_STRING(source
);
1185 if (spec
[0U] == '#' && spec
.Length() == 7) { // It's #RRGGBB
1187 int red
= strtol(spec
.Mid(1,2), &junk
, 16);
1188 int green
= strtol(spec
.Mid(3,2), &junk
, 16);
1189 int blue
= strtol(spec
.Mid(5,2), &junk
, 16);
1190 **obj
= wxColour(red
, green
, blue
);
1193 else { // it's a colour name
1194 **obj
= wxColour(spec
);
1200 PyErr_SetString(PyExc_TypeError
, "Expected a wxColour object or a string containing a colour name or '#RRGGBB'.");
1205 //----------------------------------------------------------------------
1206 //----------------------------------------------------------------------
1207 //----------------------------------------------------------------------