]>
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>
32 //#include <gdk/gdk.h>
33 //#include <gdk/gdkx.h>
34 //#include <gtk/gtkwindow.h>
36 //extern GtkWidget *wxRootWindow;
43 #ifdef __WXMSW__ // If building for win32...
44 //----------------------------------------------------------------------
45 // This gets run when the DLL is loaded. We just need to save a handle.
46 //----------------------------------------------------------------------
49 HINSTANCE hinstDLL
, // handle to DLL module
50 DWORD fdwReason
, // reason for calling function
51 LPVOID lpvReserved
// reserved
54 wxSetInstance(hinstDLL
);
59 //----------------------------------------------------------------------
60 // Class for implementing the wxp main application shell.
61 //----------------------------------------------------------------------
63 wxPyApp
*wxPythonApp
= NULL
; // Global instance of application object
67 // printf("**** ctor\n");
71 // printf("**** dtor\n");
75 // This one isn't acutally called... See __wxStart()
76 bool wxPyApp::OnInit(void) {
80 int wxPyApp::MainLoop(void) {
83 DeletePendingObjects();
85 m_initialized
= wxTopLevelWindows
.GetCount() != 0;
89 retval
= wxApp::MainLoop();
90 wxPythonApp
->OnExit();
96 //---------------------------------------------------------------------
97 //----------------------------------------------------------------------
100 #include "wx/msw/msvcrt.h"
104 int WXDLLEXPORT
wxEntryStart( int argc
, char** argv
);
105 int WXDLLEXPORT
wxEntryInitGui();
106 void WXDLLEXPORT
wxEntryCleanup();
109 #ifdef WXP_WITH_THREAD
110 PyThreadState
* wxPyEventThreadState
= NULL
;
114 // This is where we pick up the first part of the wxEntry functionality...
115 // The rest is in __wxStart and __wxCleanup. This function is called when
116 // wxcmodule is imported. (Before there is a wxApp object.)
121 // wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
124 #ifdef WXP_WITH_THREAD
125 #if 0 // OLD THREAD STUFF
126 PyEval_InitThreads();
127 wxPyEventThreadState
= PyThreadState_Get();
129 PyEval_InitThreads();
130 wxPyEventThreadState
= PyThreadState_New(PyThreadState_Get()->interp
);
134 // Bail out if there is already windows created. This means that the
135 // toolkit has already been initialized, as in embedding wxPython in
136 // a C++ wxWindows app.
137 if (wxTopLevelWindows
.Number() > 0)
141 PyObject
* sysargv
= PySys_GetObject("argv");
142 int argc
= PyList_Size(sysargv
);
143 char** argv
= new char*[argc
+1];
145 for(x
=0; x
<argc
; x
++)
146 argv
[x
] = PyString_AsString(PyList_GetItem(sysargv
, x
));
149 wxEntryStart(argc
, argv
);
155 // Start the user application, user App's OnInit method is a parameter here
156 PyObject
* __wxStart(PyObject
* /* self */, PyObject
* args
)
158 PyObject
* onInitFunc
= NULL
;
163 if (!PyArg_ParseTuple(args
, "O", &onInitFunc
))
166 #if 0 // Try it out without this check, see how it does...
167 if (wxTopLevelWindows
.Number() > 0) {
168 PyErr_SetString(PyExc_TypeError
, "Only 1 wxApp per process!");
173 // This is the next part of the wxEntry functionality...
174 PyObject
* sysargv
= PySys_GetObject("argv");
175 int argc
= PyList_Size(sysargv
);
176 char** argv
= new char*[argc
+1];
178 for(x
=0; x
<argc
; x
++)
179 argv
[x
] = copystring(PyString_AsString(PyList_GetItem(sysargv
, x
)));
182 wxPythonApp
->argc
= argc
;
183 wxPythonApp
->argv
= argv
;
187 // Call the Python App's OnInit function
188 arglist
= PyTuple_New(0);
189 result
= PyEval_CallObject(onInitFunc
, arglist
);
190 if (!result
) { // an exception was raised.
194 if (! PyInt_Check(result
)) {
195 PyErr_SetString(PyExc_TypeError
, "OnInit should return a boolean value");
198 bResult
= PyInt_AS_LONG(result
);
200 PyErr_SetString(PyExc_SystemExit
, "OnInit returned FALSE, exiting...");
205 wxTheApp
->m_initialized
= (wxTopLevelWindows
.GetCount() > 0);
218 PyObject
* wxPython_dict
;
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 #define wxPlatform "__WXMOTIF__"
233 #define wxPlatform "__WXQT__"
236 #define wxPlatform "__WXGTK__"
238 #if defined(__WIN32__) || defined(__WXMSW__)
239 #define wxPlatform "__WXMSW__"
242 #define wxPlatform "__WXMAC__"
245 PyDict_SetItemString(wxPython_dict
, "wxPlatform", PyString_FromString(wxPlatform
));
252 //---------------------------------------------------------------------------
254 PyObject
* wxPyConstructObject(void* ptr
,
255 const char* className
,
265 char buff
[64]; // should always be big enough...
268 sprintf(buff
, "_%s_p", className
);
269 SWIG_MakePtr(swigptr
, ptr
, buff
);
271 sprintf(buff
, "%sPtr", className
);
272 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
274 //Py_INCREF(Py_None);
278 "*** Unknown class name %s, tell Robin about it please ***",
280 obj
= PyString_FromString(temp
);
284 arg
= Py_BuildValue("(s)", swigptr
);
285 obj
= PyInstance_New(classobj
, arg
, NULL
);
289 PyObject
* one
= PyInt_FromLong(1);
290 PyObject_SetAttrString(obj
, "thisown", one
);
297 //---------------------------------------------------------------------------
299 static PyThreadState
* myPyThreadState_Get() {
300 PyThreadState
* current
;
301 current
= PyThreadState_Swap(NULL
);
302 PyThreadState_Swap(current
);
307 bool wxPyRestoreThread() {
308 // NOTE: The Python API docs state that if a thread already has the
309 // interpreter lock and calls PyEval_RestoreThread again a deadlock
310 // occurs, so I put in this code as a guard condition since there are
311 // many possibilites for nested events and callbacks in wxPython. If
312 // The current thread is our thread, then we can assume that we
313 // already have the lock. (I hope!)
315 #ifdef WXP_WITH_THREAD
316 #if 0 // OLD THREAD STUFF
317 if (wxPyEventThreadState
!= myPyThreadState_Get()) {
318 PyEval_RestoreThread(wxPyEventThreadState
);
323 if (wxPyEventThreadState
!= myPyThreadState_Get()) {
324 PyEval_AcquireThread(wxPyEventThreadState
);
334 void wxPySaveThread(bool doSave
) {
335 #ifdef WXP_WITH_THREAD
336 #if 0 // OLD THREAD STUFF
338 wxPyEventThreadState
= PyEval_SaveThread();
342 PyEval_ReleaseThread(wxPyEventThreadState
);
348 //---------------------------------------------------------------------------
351 IMPLEMENT_ABSTRACT_CLASS(wxPyCallback
, wxObject
);
353 wxPyCallback::wxPyCallback(PyObject
* func
) {
358 wxPyCallback::wxPyCallback(const wxPyCallback
& other
) {
359 m_func
= other
.m_func
;
363 wxPyCallback::~wxPyCallback() {
364 bool doSave
= wxPyRestoreThread();
366 wxPySaveThread(doSave
);
371 // This function is used for all events destined for Python event handlers.
372 void wxPyCallback::EventThunker(wxEvent
& event
) {
373 wxPyCallback
* cb
= (wxPyCallback
*)event
.m_callbackUserData
;
374 PyObject
* func
= cb
->m_func
;
380 bool doSave
= wxPyRestoreThread();
381 wxString className
= event
.GetClassInfo()->GetClassName();
383 if (className
== "wxPyEvent")
384 arg
= ((wxPyEvent
*)&event
)->GetSelf();
385 else if (className
== "wxPyCommandEvent")
386 arg
= ((wxPyCommandEvent
*)&event
)->GetSelf();
388 arg
= wxPyConstructObject((void*)&event
, className
);
390 tuple
= PyTuple_New(1);
391 PyTuple_SET_ITEM(tuple
, 0, arg
);
392 result
= PyEval_CallObject(func
, tuple
);
400 wxPySaveThread(doSave
);
404 //----------------------------------------------------------------------
406 wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper
& other
) {
408 m_self
= other
.m_self
;
409 m_class
= other
.m_class
;
417 void wxPyCallbackHelper::setSelf(PyObject
* self
, PyObject
* klass
, int incref
) {
428 // If the object (m_self) has an attibute of the given name, and if that
429 // attribute is a method, and if that method's class is not from a base class,
430 // then we'll save a pointer to the method so callCallback can call it.
431 bool wxPyCallbackHelper::findCallback(const char* name
) const {
432 wxPyCallbackHelper
* self
= (wxPyCallbackHelper
*)this; // cast away const
433 self
->m_lastFound
= NULL
;
434 if (m_self
&& PyObject_HasAttrString(m_self
, (char*)name
)) {
436 method
= PyObject_GetAttrString(m_self
, (char*)name
);
438 if (PyMethod_Check(method
) &&
439 ((PyMethod_GET_CLASS(method
) == m_class
) ||
440 PyClass_IsSubclass(PyMethod_GET_CLASS(method
), m_class
))) {
442 self
->m_lastFound
= method
;
448 return m_lastFound
!= NULL
;
452 int wxPyCallbackHelper::callCallback(PyObject
* argTuple
) const {
456 result
= callCallbackObj(argTuple
);
457 if (result
) { // Assumes an integer return type...
458 retval
= PyInt_AsLong(result
);
460 PyErr_Clear(); // forget about it if it's not...
465 // Invoke the Python callable object, returning the raw PyObject return
466 // value. Caller should DECREF the return value and also call PyEval_SaveThread.
467 PyObject
* wxPyCallbackHelper::callCallbackObj(PyObject
* argTuple
) const {
470 // Save a copy of the pointer in case the callback generates another
471 // callback. In that case m_lastFound will have a different value when
472 // it gets back here...
473 PyObject
* method
= m_lastFound
;
475 result
= PyEval_CallObject(method
, argTuple
);
485 void wxPyCBH_setSelf(wxPyCallbackHelper
& cbh
, PyObject
* self
, PyObject
* klass
, int incref
) {
486 cbh
.setSelf(self
, klass
, incref
);
489 bool wxPyCBH_findCallback(const wxPyCallbackHelper
& cbh
, const char* name
) {
490 return cbh
.findCallback(name
);
493 int wxPyCBH_callCallback(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
494 return cbh
.callCallback(argTuple
);
497 PyObject
* wxPyCBH_callCallbackObj(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
498 return cbh
.callCallbackObj(argTuple
);
502 void wxPyCBH_delete(wxPyCallbackHelper
* cbh
) {
503 bool doSave
= wxPyRestoreThread();
505 Py_XDECREF(cbh
->m_self
);
506 Py_XDECREF(cbh
->m_class
);
508 wxPySaveThread(doSave
);
511 //---------------------------------------------------------------------------
512 //---------------------------------------------------------------------------
513 // These classes can be derived from in Python and passed through the event
514 // system without losing anything. They do this by keeping a reference to
515 // themselves and some special case handling in wxPyCallback::EventThunker.
518 wxPyEvtSelfRef::wxPyEvtSelfRef() {
519 //m_self = Py_None; // **** We don't do normal ref counting to prevent
520 //Py_INCREF(m_self); // circular loops...
524 wxPyEvtSelfRef::~wxPyEvtSelfRef() {
525 bool doSave
= wxPyRestoreThread();
528 wxPySaveThread(doSave
);
531 void wxPyEvtSelfRef::SetSelf(PyObject
* self
, bool clone
) {
532 bool doSave
= wxPyRestoreThread();
540 wxPySaveThread(doSave
);
543 PyObject
* wxPyEvtSelfRef::GetSelf() const {
549 wxPyEvent::wxPyEvent(int id
)
553 wxPyEvent::~wxPyEvent() {
556 // This one is so the event object can be Cloned...
557 void wxPyEvent::CopyObject(wxObject
& dest
) const {
558 wxEvent::CopyObject(dest
);
559 ((wxPyEvent
*)&dest
)->SetSelf(m_self
, TRUE
);
563 IMPLEMENT_DYNAMIC_CLASS(wxPyEvent
, wxEvent
);
566 wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType
, int id
)
567 : wxCommandEvent(commandType
, id
) {
570 wxPyCommandEvent::~wxPyCommandEvent() {
573 void wxPyCommandEvent::CopyObject(wxObject
& dest
) const {
574 wxCommandEvent::CopyObject(dest
);
575 ((wxPyCommandEvent
*)&dest
)->SetSelf(m_self
, TRUE
);
579 IMPLEMENT_DYNAMIC_CLASS(wxPyCommandEvent
, wxCommandEvent
);
583 //---------------------------------------------------------------------------
584 //---------------------------------------------------------------------------
587 wxPyTimer::wxPyTimer(PyObject
* callback
) {
592 wxPyTimer::~wxPyTimer() {
593 bool doSave
= wxPyRestoreThread();
595 wxPySaveThread(doSave
);
598 void wxPyTimer::Notify() {
599 if (!func
|| func
== Py_None
) {
603 bool doSave
= wxPyRestoreThread();
606 PyObject
* args
= Py_BuildValue("()");
608 result
= PyEval_CallObject(func
, args
);
617 wxPySaveThread(doSave
);
623 //---------------------------------------------------------------------------
624 //---------------------------------------------------------------------------
625 // Convert a wxList to a Python List
627 PyObject
* wxPy_ConvertList(wxListBase
* list
, const char* className
) {
631 wxNode
* node
= list
->First();
633 bool doSave
= wxPyRestoreThread();
634 pyList
= PyList_New(0);
636 wxObj
= node
->Data();
637 pyObj
= wxPyConstructObject(wxObj
, className
);
638 PyList_Append(pyList
, pyObj
);
641 wxPySaveThread(doSave
);
645 //----------------------------------------------------------------------
647 long wxPyGetWinHandle(wxWindow
* win
) {
649 return (long)win
->GetHandle();
652 // Find and return the actual X-Window.
654 if (win
->m_wxwindow
) {
655 GdkWindowPrivate
* bwin
= (GdkWindowPrivate
*)GTK_PIZZA(win
->m_wxwindow
)->bin_window
;
657 return (long)bwin
->xwindow
;
664 //----------------------------------------------------------------------
665 // Some helper functions for typemaps in my_typemaps.i, so they won't be
666 // included in every file...
669 byte
* byte_LIST_helper(PyObject
* source
) {
670 if (!PyList_Check(source
)) {
671 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
674 int count
= PyList_Size(source
);
675 byte
* temp
= new byte
[count
];
677 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
680 for (int x
=0; x
<count
; x
++) {
681 PyObject
* o
= PyList_GetItem(source
, x
);
682 if (! PyInt_Check(o
)) {
683 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
686 temp
[x
] = (byte
)PyInt_AsLong(o
);
692 int* int_LIST_helper(PyObject
* source
) {
693 if (!PyList_Check(source
)) {
694 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
697 int count
= PyList_Size(source
);
698 int* temp
= new int[count
];
700 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
703 for (int x
=0; x
<count
; x
++) {
704 PyObject
* o
= PyList_GetItem(source
, x
);
705 if (! PyInt_Check(o
)) {
706 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
709 temp
[x
] = PyInt_AsLong(o
);
715 long* long_LIST_helper(PyObject
* source
) {
716 if (!PyList_Check(source
)) {
717 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
720 int count
= PyList_Size(source
);
721 long* temp
= new long[count
];
723 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
726 for (int x
=0; x
<count
; x
++) {
727 PyObject
* o
= PyList_GetItem(source
, x
);
728 if (! PyInt_Check(o
)) {
729 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
732 temp
[x
] = PyInt_AsLong(o
);
738 char** string_LIST_helper(PyObject
* source
) {
739 if (!PyList_Check(source
)) {
740 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
743 int count
= PyList_Size(source
);
744 char** temp
= new char*[count
];
746 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
749 for (int x
=0; x
<count
; x
++) {
750 PyObject
* o
= PyList_GetItem(source
, x
);
751 if (! PyString_Check(o
)) {
752 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
755 temp
[x
] = PyString_AsString(o
);
760 //--------------------------------
761 // Part of patch from Tim Hochberg
762 static inline bool wxPointFromObjects(PyObject
* o1
, PyObject
* o2
, wxPoint
* point
) {
763 if (PyInt_Check(o1
) && PyInt_Check(o2
)) {
764 point
->x
= PyInt_AS_LONG(o1
);
765 point
->y
= PyInt_AS_LONG(o2
);
768 if (PyFloat_Check(o1
) && PyFloat_Check(o2
)) {
769 point
->x
= (int)PyFloat_AS_DOUBLE(o1
);
770 point
->y
= (int)PyFloat_AS_DOUBLE(o2
);
773 if (PyInstance_Check(o1
) || PyInstance_Check(o2
)) {
774 // Disallow instances because they can cause havok
777 if (PyNumber_Check(o1
) && PyNumber_Check(o2
)) {
778 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
779 point
->x
= PyInt_AsLong(o1
);
780 point
->y
= PyInt_AsLong(o2
);
787 #if PYTHON_API_VERSION < 1009
788 #define PySequence_Fast_GET_ITEM(o, i)\
789 (PyList_Check(o) ? PyList_GET_ITEM(o, i) : PyTuple_GET_ITEM(o, i))
792 wxPoint
* wxPoint_LIST_helper(PyObject
* source
, int *count
) {
793 // Putting all of the declarations here allows
794 // us to put the error handling all in one place.
797 PyObject
*o
, *o1
, *o2
;
798 int isFast
= PyList_Check(source
) || PyTuple_Check(source
);
800 // The length of the sequence is returned in count.
801 if (!PySequence_Check(source
)) {
804 *count
= PySequence_Length(source
);
809 temp
= new wxPoint
[*count
];
811 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
814 for (x
=0; x
<*count
; x
++) {
815 // Get an item: try fast way first.
817 o
= PySequence_Fast_GET_ITEM(source
, x
);
820 o
= PySequence_GetItem(source
, x
);
826 // Convert o to wxPoint.
827 if ((PyTuple_Check(o
) && PyTuple_GET_SIZE(o
) == 2) ||
828 (PyList_Check(o
) && PyList_GET_SIZE(o
) == 2)) {
829 o1
= PySequence_Fast_GET_ITEM(o
, 0);
830 o2
= PySequence_Fast_GET_ITEM(o
, 1);
831 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
835 else if (PyInstance_Check(o
)) {
837 if (SWIG_GetPtrObj(o
, (void **)&pt
, "_wxPoint_p")) {
842 else if (PySequence_Check(o
) && PySequence_Length(o
) == 2) {
843 o1
= PySequence_GetItem(o
, 0);
844 o2
= PySequence_GetItem(o
, 1);
845 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
869 PyErr_SetString(PyExc_TypeError
, "Expected a sequence of length-2 sequences or wxPoints.");
873 //------------------------------
876 wxBitmap
** wxBitmap_LIST_helper(PyObject
* source
) {
877 if (!PyList_Check(source
)) {
878 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
881 int count
= PyList_Size(source
);
882 wxBitmap
** temp
= new wxBitmap
*[count
];
884 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
887 for (int x
=0; x
<count
; x
++) {
888 PyObject
* o
= PyList_GetItem(source
, x
);
889 if (PyInstance_Check(o
)) {
891 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxBitmap_p")) {
892 PyErr_SetString(PyExc_TypeError
,"Expected _wxBitmap_p.");
898 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxBitmaps.");
907 wxString
* wxString_LIST_helper(PyObject
* source
) {
908 if (!PyList_Check(source
)) {
909 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
912 int count
= PyList_Size(source
);
913 wxString
* temp
= new wxString
[count
];
915 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
918 for (int x
=0; x
<count
; x
++) {
919 PyObject
* o
= PyList_GetItem(source
, x
);
920 if (! PyString_Check(o
)) {
921 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
924 temp
[x
] = PyString_AsString(o
);
930 wxAcceleratorEntry
* wxAcceleratorEntry_LIST_helper(PyObject
* source
) {
931 if (!PyList_Check(source
)) {
932 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
935 int count
= PyList_Size(source
);
936 wxAcceleratorEntry
* temp
= new wxAcceleratorEntry
[count
];
938 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
941 for (int x
=0; x
<count
; x
++) {
942 PyObject
* o
= PyList_GetItem(source
, x
);
943 if (PyInstance_Check(o
)) {
944 wxAcceleratorEntry
* ae
;
945 if (SWIG_GetPtrObj(o
, (void **) &ae
,"_wxAcceleratorEntry_p")) {
946 PyErr_SetString(PyExc_TypeError
,"Expected _wxAcceleratorEntry_p.");
951 else if (PyTuple_Check(o
)) {
952 PyObject
* o1
= PyTuple_GetItem(o
, 0);
953 PyObject
* o2
= PyTuple_GetItem(o
, 1);
954 PyObject
* o3
= PyTuple_GetItem(o
, 2);
956 temp
[x
].m_flags
= PyInt_AsLong(o1
);
957 temp
[x
].m_keyCode
= PyInt_AsLong(o2
);
958 temp
[x
].m_command
= PyInt_AsLong(o3
);
961 PyErr_SetString(PyExc_TypeError
, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
970 //----------------------------------------------------------------------
972 bool wxSize_helper(PyObject
* source
, wxSize
** obj
) {
974 // If source is an object instance then it may already be the right type
975 if (PyInstance_Check(source
)) {
977 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxSize_p"))
982 // otherwise a 2-tuple of integers is expected
983 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
984 PyObject
* o1
= PySequence_GetItem(source
, 0);
985 PyObject
* o2
= PySequence_GetItem(source
, 1);
986 **obj
= wxSize(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
991 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxSize object.");
995 bool wxPoint_helper(PyObject
* source
, wxPoint
** obj
) {
997 // If source is an object instance then it may already be the right type
998 if (PyInstance_Check(source
)) {
1000 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxPoint_p"))
1005 // otherwise a length-2 sequence of integers is expected
1006 if (PySequence_Check(source
) && PySequence_Length(source
) == 2) {
1007 PyObject
* o1
= PySequence_GetItem(source
, 0);
1008 PyObject
* o2
= PySequence_GetItem(source
, 1);
1009 // This should really check for integers, not numbers -- but that would break code.
1010 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1015 **obj
= wxPoint(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1021 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxPoint object.");
1027 bool wxRealPoint_helper(PyObject
* source
, wxRealPoint
** obj
) {
1029 // If source is an object instance then it may already be the right type
1030 if (PyInstance_Check(source
)) {
1032 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRealPoint_p"))
1037 // otherwise a 2-tuple of floats is expected
1038 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1039 PyObject
* o1
= PySequence_GetItem(source
, 0);
1040 PyObject
* o2
= PySequence_GetItem(source
, 1);
1041 **obj
= wxRealPoint(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
));
1046 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxRealPoint object.");
1053 bool wxRect_helper(PyObject
* source
, wxRect
** obj
) {
1055 // If source is an object instance then it may already be the right type
1056 if (PyInstance_Check(source
)) {
1058 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRect_p"))
1063 // otherwise a 4-tuple of integers is expected
1064 else if (PySequence_Check(source
) && PyObject_Length(source
) == 4) {
1065 PyObject
* o1
= PySequence_GetItem(source
, 0);
1066 PyObject
* o2
= PySequence_GetItem(source
, 1);
1067 PyObject
* o3
= PySequence_GetItem(source
, 2);
1068 PyObject
* o4
= PySequence_GetItem(source
, 3);
1069 **obj
= wxRect(PyInt_AsLong(o1
), PyInt_AsLong(o2
),
1070 PyInt_AsLong(o3
), PyInt_AsLong(o4
));
1075 PyErr_SetString(PyExc_TypeError
, "Expected a 4-tuple of integers or a wxRect object.");
1081 bool wxColour_helper(PyObject
* source
, wxColour
** obj
) {
1083 // If source is an object instance then it may already be the right type
1084 if (PyInstance_Check(source
)) {
1086 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxColour_p"))
1091 // otherwise a string is expected
1092 else if (PyString_Check(source
)) {
1093 wxString spec
= PyString_AS_STRING(source
);
1094 if (spec
[0U] == '#' && spec
.Length() == 7) { // It's #RRGGBB
1096 int red
= strtol(spec
.Mid(1,2), &junk
, 16);
1097 int green
= strtol(spec
.Mid(3,2), &junk
, 16);
1098 int blue
= strtol(spec
.Mid(5,2), &junk
, 16);
1099 **obj
= wxColour(red
, green
, blue
);
1102 else { // it's a colour name
1103 **obj
= wxColour(spec
);
1109 PyErr_SetString(PyExc_TypeError
, "Expected a wxColour object or a string containing a colour name or '#RRGGBB'.");
1114 //----------------------------------------------------------------------
1115 //----------------------------------------------------------------------
1116 //----------------------------------------------------------------------