]>
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 PyEval_InitThreads();
126 wxPyEventThreadState
= PyThreadState_Get();
129 // Bail out if there is already windows created. This means that the
130 // toolkit has already been initialized, as in embedding wxPython in
131 // a C++ wxWindows app.
132 if (wxTopLevelWindows
.Number() > 0)
136 PyObject
* sysargv
= PySys_GetObject("argv");
137 int argc
= PyList_Size(sysargv
);
138 char** argv
= new char*[argc
+1];
140 for(x
=0; x
<argc
; x
++)
141 argv
[x
] = 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, soo 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...
169 PyObject
* sysargv
= PySys_GetObject("argv");
170 int argc
= PyList_Size(sysargv
);
171 char** argv
= new char*[argc
+1];
173 for(x
=0; x
<argc
; x
++)
174 argv
[x
] = copystring(PyString_AsString(PyList_GetItem(sysargv
, x
)));
177 wxPythonApp
->argc
= argc
;
178 wxPythonApp
->argv
= argv
;
182 // Call the Python App's OnInit function
183 arglist
= PyTuple_New(0);
184 result
= PyEval_CallObject(onInitFunc
, arglist
);
185 if (!result
) { // an exception was raised.
189 if (! PyInt_Check(result
)) {
190 PyErr_SetString(PyExc_TypeError
, "OnInit should return a boolean value");
193 bResult
= PyInt_AS_LONG(result
);
195 PyErr_SetString(PyExc_SystemExit
, "OnInit returned FALSE, exiting...");
200 wxTheApp
->m_initialized
= (wxTopLevelWindows
.GetCount() > 0);
213 PyObject
* wxPython_dict
;
214 PyObject
* __wxSetDictionary(PyObject
* /* self */, PyObject
* args
)
217 if (!PyArg_ParseTuple(args
, "O", &wxPython_dict
))
220 if (!PyDict_Check(wxPython_dict
)) {
221 PyErr_SetString(PyExc_TypeError
, "_wxSetDictionary must have dictionary object!");
225 #define wxPlatform "__WXMOTIF__"
228 #define wxPlatform "__WXQT__"
231 #define wxPlatform "__WXGTK__"
233 #if defined(__WIN32__) || defined(__WXMSW__)
234 #define wxPlatform "__WXMSW__"
237 #define wxPlatform "__WXMAC__"
240 PyDict_SetItemString(wxPython_dict
, "wxPlatform", PyString_FromString(wxPlatform
));
247 //---------------------------------------------------------------------------
249 PyObject
* wxPyConstructObject(void* ptr
,
250 const char* className
,
260 char buff
[64]; // should always be big enough...
263 sprintf(buff
, "_%s_p", className
);
264 SWIG_MakePtr(swigptr
, ptr
, buff
);
266 sprintf(buff
, "%sPtr", className
);
267 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
269 //Py_INCREF(Py_None);
273 "*** Unknown class name %s, tell Robin about it please ***",
275 obj
= PyString_FromString(temp
);
279 arg
= Py_BuildValue("(s)", swigptr
);
280 obj
= PyInstance_New(classobj
, arg
, NULL
);
284 PyObject
* one
= PyInt_FromLong(1);
285 PyObject_SetAttrString(obj
, "thisown", one
);
292 //---------------------------------------------------------------------------
294 static PyThreadState
* myPyThreadState_Get() {
295 PyThreadState
* current
;
296 current
= PyThreadState_Swap(NULL
);
297 PyThreadState_Swap(current
);
302 bool wxPyRestoreThread() {
303 // NOTE: The Python API docs state that if a thread already has the
304 // interpreter lock and calls PyEval_RestoreThread again a deadlock
305 // occurs, so I put in this code as a guard condition since there are
306 // many possibilites for nested events and callbacks in wxPython. If
307 // The current thread is our thread, then we can assume that we
308 // already have the lock. (I hope!)
310 #ifdef WXP_WITH_THREAD
311 if (wxPyEventThreadState
!= myPyThreadState_Get()) {
312 PyEval_RestoreThread(wxPyEventThreadState
);
321 void wxPySaveThread(bool doSave
) {
322 #ifdef WXP_WITH_THREAD
324 wxPyEventThreadState
= PyEval_SaveThread();
329 //---------------------------------------------------------------------------
332 IMPLEMENT_ABSTRACT_CLASS(wxPyCallback
, wxObject
);
334 wxPyCallback::wxPyCallback(PyObject
* func
) {
339 wxPyCallback::wxPyCallback(const wxPyCallback
& other
) {
340 m_func
= other
.m_func
;
344 wxPyCallback::~wxPyCallback() {
345 bool doSave
= wxPyRestoreThread();
347 wxPySaveThread(doSave
);
352 // This function is used for all events destined for Python event handlers.
353 void wxPyCallback::EventThunker(wxEvent
& event
) {
354 wxPyCallback
* cb
= (wxPyCallback
*)event
.m_callbackUserData
;
355 PyObject
* func
= cb
->m_func
;
361 bool doSave
= wxPyRestoreThread();
362 wxString className
= event
.GetClassInfo()->GetClassName();
364 if (className
== "wxPyEvent")
365 arg
= ((wxPyEvent
*)&event
)->GetSelf();
366 else if (className
== "wxPyCommandEvent")
367 arg
= ((wxPyCommandEvent
*)&event
)->GetSelf();
369 arg
= wxPyConstructObject((void*)&event
, className
);
371 tuple
= PyTuple_New(1);
372 PyTuple_SET_ITEM(tuple
, 0, arg
);
373 result
= PyEval_CallObject(func
, tuple
);
381 wxPySaveThread(doSave
);
385 //----------------------------------------------------------------------
387 wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper
& other
) {
389 m_self
= other
.m_self
;
390 m_class
= other
.m_class
;
398 void wxPyCallbackHelper::setSelf(PyObject
* self
, PyObject
* klass
, int incref
) {
409 // If the object (m_self) has an attibute of the given name, and if that
410 // attribute is a method, and if that method's class is not from a base class,
411 // then we'll save a pointer to the method so callCallback can call it.
412 bool wxPyCallbackHelper::findCallback(const char* name
) const {
413 wxPyCallbackHelper
* self
= (wxPyCallbackHelper
*)this; // cast away const
414 self
->m_lastFound
= NULL
;
415 if (m_self
&& PyObject_HasAttrString(m_self
, (char*)name
)) {
417 method
= PyObject_GetAttrString(m_self
, (char*)name
);
419 if (PyMethod_Check(method
) &&
420 ((PyMethod_GET_CLASS(method
) == m_class
) ||
421 PyClass_IsSubclass(PyMethod_GET_CLASS(method
), m_class
))) {
423 self
->m_lastFound
= method
;
429 return m_lastFound
!= NULL
;
433 int wxPyCallbackHelper::callCallback(PyObject
* argTuple
) const {
437 result
= callCallbackObj(argTuple
);
438 if (result
) { // Assumes an integer return type...
439 retval
= PyInt_AsLong(result
);
441 PyErr_Clear(); // forget about it if it's not...
446 // Invoke the Python callable object, returning the raw PyObject return
447 // value. Caller should DECREF the return value and also call PyEval_SaveThread.
448 PyObject
* wxPyCallbackHelper::callCallbackObj(PyObject
* argTuple
) const {
451 // Save a copy of the pointer in case the callback generates another
452 // callback. In that case m_lastFound will have a different value when
453 // it gets back here...
454 PyObject
* method
= m_lastFound
;
456 result
= PyEval_CallObject(method
, argTuple
);
466 void wxPyCBH_setSelf(wxPyCallbackHelper
& cbh
, PyObject
* self
, PyObject
* klass
, int incref
) {
467 cbh
.setSelf(self
, klass
, incref
);
470 bool wxPyCBH_findCallback(const wxPyCallbackHelper
& cbh
, const char* name
) {
471 return cbh
.findCallback(name
);
474 int wxPyCBH_callCallback(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
475 return cbh
.callCallback(argTuple
);
478 PyObject
* wxPyCBH_callCallbackObj(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
479 return cbh
.callCallbackObj(argTuple
);
483 void wxPyCBH_delete(wxPyCallbackHelper
* cbh
) {
484 bool doSave
= wxPyRestoreThread();
486 Py_XDECREF(cbh
->m_self
);
487 Py_XDECREF(cbh
->m_class
);
489 wxPySaveThread(doSave
);
492 //---------------------------------------------------------------------------
493 //---------------------------------------------------------------------------
494 // These classes can be derived from in Python and passed through the event
495 // system without losing anything. They do this by keeping a reference to
496 // themselves and some special case handling in wxPyCallback::EventThunker.
499 wxPyEvtSelfRef::wxPyEvtSelfRef() {
500 //m_self = Py_None; // **** We don't do normal ref counting to prevent
501 //Py_INCREF(m_self); // circular loops...
505 wxPyEvtSelfRef::~wxPyEvtSelfRef() {
506 bool doSave
= wxPyRestoreThread();
509 wxPySaveThread(doSave
);
512 void wxPyEvtSelfRef::SetSelf(PyObject
* self
, bool clone
) {
513 bool doSave
= wxPyRestoreThread();
521 wxPySaveThread(doSave
);
524 PyObject
* wxPyEvtSelfRef::GetSelf() const {
530 wxPyEvent::wxPyEvent(int id
)
534 wxPyEvent::~wxPyEvent() {
537 // This one is so the event object can be Cloned...
538 void wxPyEvent::CopyObject(wxObject
& dest
) const {
539 wxEvent::CopyObject(dest
);
540 ((wxPyEvent
*)&dest
)->SetSelf(m_self
, TRUE
);
544 IMPLEMENT_DYNAMIC_CLASS(wxPyEvent
, wxEvent
);
547 wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType
, int id
)
548 : wxCommandEvent(commandType
, id
) {
551 wxPyCommandEvent::~wxPyCommandEvent() {
554 void wxPyCommandEvent::CopyObject(wxObject
& dest
) const {
555 wxCommandEvent::CopyObject(dest
);
556 ((wxPyCommandEvent
*)&dest
)->SetSelf(m_self
, TRUE
);
560 IMPLEMENT_DYNAMIC_CLASS(wxPyCommandEvent
, wxCommandEvent
);
564 //---------------------------------------------------------------------------
565 //---------------------------------------------------------------------------
568 wxPyTimer::wxPyTimer(PyObject
* callback
) {
573 wxPyTimer::~wxPyTimer() {
574 bool doSave
= wxPyRestoreThread();
576 wxPySaveThread(doSave
);
579 void wxPyTimer::Notify() {
580 if (!func
|| func
== Py_None
) {
584 bool doSave
= wxPyRestoreThread();
587 PyObject
* args
= Py_BuildValue("()");
589 result
= PyEval_CallObject(func
, args
);
598 wxPySaveThread(doSave
);
604 //---------------------------------------------------------------------------
605 //---------------------------------------------------------------------------
606 // Convert a wxList to a Python List
608 PyObject
* wxPy_ConvertList(wxListBase
* list
, const char* className
) {
612 wxNode
* node
= list
->First();
614 bool doSave
= wxPyRestoreThread();
615 pyList
= PyList_New(0);
617 wxObj
= node
->Data();
618 pyObj
= wxPyConstructObject(wxObj
, className
);
619 PyList_Append(pyList
, pyObj
);
622 wxPySaveThread(doSave
);
626 //----------------------------------------------------------------------
628 long wxPyGetWinHandle(wxWindow
* win
) {
630 return (long)win
->GetHandle();
633 // Find and return the actual X-Window.
635 if (win
->m_wxwindow
) {
636 GdkWindowPrivate
* bwin
= (GdkWindowPrivate
*)GTK_PIZZA(win
->m_wxwindow
)->bin_window
;
638 return (long)bwin
->xwindow
;
645 //----------------------------------------------------------------------
646 // Some helper functions for typemaps in my_typemaps.i, so they won't be
647 // included in every file...
650 byte
* byte_LIST_helper(PyObject
* source
) {
651 if (!PyList_Check(source
)) {
652 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
655 int count
= PyList_Size(source
);
656 byte
* temp
= new byte
[count
];
658 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
661 for (int x
=0; x
<count
; x
++) {
662 PyObject
* o
= PyList_GetItem(source
, x
);
663 if (! PyInt_Check(o
)) {
664 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
667 temp
[x
] = (byte
)PyInt_AsLong(o
);
673 int* int_LIST_helper(PyObject
* source
) {
674 if (!PyList_Check(source
)) {
675 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
678 int count
= PyList_Size(source
);
679 int* temp
= new int[count
];
681 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
684 for (int x
=0; x
<count
; x
++) {
685 PyObject
* o
= PyList_GetItem(source
, x
);
686 if (! PyInt_Check(o
)) {
687 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
690 temp
[x
] = PyInt_AsLong(o
);
696 long* long_LIST_helper(PyObject
* source
) {
697 if (!PyList_Check(source
)) {
698 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
701 int count
= PyList_Size(source
);
702 long* temp
= new long[count
];
704 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
707 for (int x
=0; x
<count
; x
++) {
708 PyObject
* o
= PyList_GetItem(source
, x
);
709 if (! PyInt_Check(o
)) {
710 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
713 temp
[x
] = PyInt_AsLong(o
);
719 char** string_LIST_helper(PyObject
* source
) {
720 if (!PyList_Check(source
)) {
721 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
724 int count
= PyList_Size(source
);
725 char** temp
= new char*[count
];
727 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
730 for (int x
=0; x
<count
; x
++) {
731 PyObject
* o
= PyList_GetItem(source
, x
);
732 if (! PyString_Check(o
)) {
733 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
736 temp
[x
] = PyString_AsString(o
);
743 wxPoint
* wxPoint_LIST_helper(PyObject
* source
) {
744 if (!PyList_Check(source
)) {
745 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
748 int count
= PyList_Size(source
);
749 wxPoint
* temp
= new wxPoint
[count
];
751 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
754 for (int x
=0; x
<count
; x
++) {
755 PyObject
* o
= PyList_GetItem(source
, x
);
756 if (PyTuple_Check(o
)) {
757 PyObject
* o1
= PyTuple_GetItem(o
, 0);
758 PyObject
* o2
= PyTuple_GetItem(o
, 1);
760 temp
[x
].x
= PyInt_AsLong(o1
);
761 temp
[x
].y
= PyInt_AsLong(o2
);
763 else if (PyInstance_Check(o
)) {
765 if (SWIG_GetPtrObj(o
,(void **) &pt
,"_wxPoint_p")) {
766 PyErr_SetString(PyExc_TypeError
,"Expected _wxPoint_p.");
772 PyErr_SetString(PyExc_TypeError
, "Expected a list of 2-tuples or wxPoints.");
780 wxBitmap
** wxBitmap_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 wxBitmap
** temp
= new wxBitmap
*[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 (PyInstance_Check(o
)) {
795 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxBitmap_p")) {
796 PyErr_SetString(PyExc_TypeError
,"Expected _wxBitmap_p.");
802 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxBitmaps.");
811 wxString
* wxString_LIST_helper(PyObject
* source
) {
812 if (!PyList_Check(source
)) {
813 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
816 int count
= PyList_Size(source
);
817 wxString
* temp
= new wxString
[count
];
819 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
822 for (int x
=0; x
<count
; x
++) {
823 PyObject
* o
= PyList_GetItem(source
, x
);
824 if (! PyString_Check(o
)) {
825 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
828 temp
[x
] = PyString_AsString(o
);
834 wxAcceleratorEntry
* wxAcceleratorEntry_LIST_helper(PyObject
* source
) {
835 if (!PyList_Check(source
)) {
836 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
839 int count
= PyList_Size(source
);
840 wxAcceleratorEntry
* temp
= new wxAcceleratorEntry
[count
];
842 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
845 for (int x
=0; x
<count
; x
++) {
846 PyObject
* o
= PyList_GetItem(source
, x
);
847 if (PyInstance_Check(o
)) {
848 wxAcceleratorEntry
* ae
;
849 if (SWIG_GetPtrObj(o
, (void **) &ae
,"_wxAcceleratorEntry_p")) {
850 PyErr_SetString(PyExc_TypeError
,"Expected _wxAcceleratorEntry_p.");
855 else if (PyTuple_Check(o
)) {
856 PyObject
* o1
= PyTuple_GetItem(o
, 0);
857 PyObject
* o2
= PyTuple_GetItem(o
, 1);
858 PyObject
* o3
= PyTuple_GetItem(o
, 2);
860 temp
[x
].m_flags
= PyInt_AsLong(o1
);
861 temp
[x
].m_keyCode
= PyInt_AsLong(o2
);
862 temp
[x
].m_command
= PyInt_AsLong(o3
);
865 PyErr_SetString(PyExc_TypeError
, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
874 //----------------------------------------------------------------------
876 bool wxSize_helper(PyObject
* source
, wxSize
** obj
) {
878 // If source is an object instance then it may already be the right type
879 if (PyInstance_Check(source
)) {
881 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxSize_p"))
886 // otherwise a 2-tuple of integers is expected
887 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
888 PyObject
* o1
= PySequence_GetItem(source
, 0);
889 PyObject
* o2
= PySequence_GetItem(source
, 1);
890 **obj
= wxSize(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
895 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxSize object.");
899 bool wxPoint_helper(PyObject
* source
, wxPoint
** obj
) {
901 // If source is an object instance then it may already be the right type
902 if (PyInstance_Check(source
)) {
904 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxPoint_p"))
909 // otherwise a 2-tuple of integers is expected
910 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
911 PyObject
* o1
= PySequence_GetItem(source
, 0);
912 PyObject
* o2
= PySequence_GetItem(source
, 1);
913 **obj
= wxPoint(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
918 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxPoint object.");
924 bool wxRealPoint_helper(PyObject
* source
, wxRealPoint
** obj
) {
926 // If source is an object instance then it may already be the right type
927 if (PyInstance_Check(source
)) {
929 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRealPoint_p"))
934 // otherwise a 2-tuple of floats is expected
935 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
936 PyObject
* o1
= PySequence_GetItem(source
, 0);
937 PyObject
* o2
= PySequence_GetItem(source
, 1);
938 **obj
= wxRealPoint(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
));
943 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxRealPoint object.");
950 bool wxRect_helper(PyObject
* source
, wxRect
** obj
) {
952 // If source is an object instance then it may already be the right type
953 if (PyInstance_Check(source
)) {
955 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRect_p"))
960 // otherwise a 4-tuple of integers is expected
961 else if (PySequence_Check(source
) && PyObject_Length(source
) == 4) {
962 PyObject
* o1
= PySequence_GetItem(source
, 0);
963 PyObject
* o2
= PySequence_GetItem(source
, 1);
964 PyObject
* o3
= PySequence_GetItem(source
, 2);
965 PyObject
* o4
= PySequence_GetItem(source
, 3);
966 **obj
= wxRect(PyInt_AsLong(o1
), PyInt_AsLong(o2
),
967 PyInt_AsLong(o3
), PyInt_AsLong(o4
));
972 PyErr_SetString(PyExc_TypeError
, "Expected a 4-tuple of integers or a wxRect object.");
978 bool wxColour_helper(PyObject
* source
, wxColour
** obj
) {
980 // If source is an object instance then it may already be the right type
981 if (PyInstance_Check(source
)) {
983 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxColour_p"))
988 // otherwise a string is expected
989 else if (PyString_Check(source
)) {
990 wxString spec
= PyString_AS_STRING(source
);
991 if (spec
[0] == '#' && spec
.Length() == 7) { // It's #RRGGBB
993 int red
= strtol(spec
.Mid(1,2), &junk
, 16);
994 int green
= strtol(spec
.Mid(3,2), &junk
, 16);
995 int blue
= strtol(spec
.Mid(5,2), &junk
, 16);
996 **obj
= wxColour(red
, green
, blue
);
999 else { // it's a colour name
1000 **obj
= wxColour(spec
);
1006 PyErr_SetString(PyExc_TypeError
, "Expected a wxColour object or a string containing a colour name or '#RRGGBB'.");
1011 //----------------------------------------------------------------------
1012 //----------------------------------------------------------------------
1013 //----------------------------------------------------------------------