]>
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
18 #include "pyistream.h"
21 #include <wx/msw/private.h>
22 #include <wx/msw/winundef.h>
23 #include <wx/msw/msvcrt.h>
28 #include <gdk/gdkprivate.h>
29 #include <wx/gtk/win_gtk.h>
33 //----------------------------------------------------------------------
35 #if PYTHON_API_VERSION <= 1007 && wxUSE_UNICODE
36 #error Python must support Unicode to use wxWindows Unicode
39 //----------------------------------------------------------------------
42 int WXDLLEXPORT
wxEntryStart( int& argc
, char** argv
);
44 int WXDLLEXPORT
wxEntryStart( int argc
, char** argv
);
46 int WXDLLEXPORT
wxEntryInitGui();
47 void WXDLLEXPORT
wxEntryCleanup();
49 wxPyApp
* wxPythonApp
= NULL
; // Global instance of application object
50 bool wxPyDoCleanup
= FALSE
;
53 #ifdef WXP_WITH_THREAD
54 struct wxPyThreadState
{
56 PyThreadState
* tstate
;
58 wxPyThreadState(unsigned long _tid
=0, PyThreadState
* _tstate
=NULL
)
59 : tid(_tid
), tstate(_tstate
) {}
62 #include <wx/dynarray.h>
63 WX_DECLARE_OBJARRAY(wxPyThreadState
, wxPyThreadStateArray
);
64 #include <wx/arrimpl.cpp>
65 WX_DEFINE_OBJARRAY(wxPyThreadStateArray
);
67 wxPyThreadStateArray
* wxPyTStates
= NULL
;
68 wxMutex
* wxPyTMutex
= NULL
;
72 #ifdef __WXMSW__ // If building for win32...
73 //----------------------------------------------------------------------
74 // This gets run when the DLL is loaded. We just need to save a handle.
75 //----------------------------------------------------------------------
78 HINSTANCE hinstDLL
, // handle to DLL module
79 DWORD fdwReason
, // reason for calling function
80 LPVOID lpvReserved
// reserved
83 // If wxPython is embedded in another wxWindows app then
84 // the inatance has already been set.
85 if (! wxGetInstance())
86 wxSetInstance(hinstDLL
);
91 //----------------------------------------------------------------------
92 // Classes for implementing the wxp main application shell.
93 //----------------------------------------------------------------------
97 // printf("**** ctor\n");
100 wxPyApp::~wxPyApp() {
101 // printf("**** dtor\n");
105 // This one isn't acutally called... See __wxStart()
106 bool wxPyApp::OnInit() {
111 int wxPyApp::MainLoop() {
114 DeletePendingObjects();
115 bool initialized
= wxTopLevelWindows
.GetCount() != 0;
117 m_initialized
= initialized
;
121 retval
= wxApp::MainLoop();
129 //---------------------------------------------------------------------
130 //----------------------------------------------------------------------
133 static char* wxPyCopyCString(const wxChar
* src
)
135 wxWX2MBbuf buff
= (wxWX2MBbuf
)wxConvCurrent
->cWX2MB(src
);
136 size_t len
= strlen(buff
);
137 char* dest
= new char[len
+1];
143 static char* wxPyCopyCString(const char* src
) // we need a char version too
145 size_t len
= strlen(src
);
146 char* dest
= new char[len
+1];
152 static wxChar
* wxPyCopyWString(const char *src
)
154 //wxMB2WXbuf buff = wxConvCurrent->cMB2WX(src);
155 wxString
str(src
, *wxConvCurrent
);
156 return copystring(str
);
160 static wxChar
* wxPyCopyWString(const wxChar
*src
)
162 return copystring(src
);
167 //----------------------------------------------------------------------
169 // This is where we pick up the first part of the wxEntry functionality...
170 // The rest is in __wxStart and __wxCleanup. This function is called when
171 // wxcmodule is imported. (Before there is a wxApp object.)
176 // wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
179 #ifdef WXP_WITH_THREAD
180 PyEval_InitThreads();
181 wxPyTStates
= new wxPyThreadStateArray
;
182 wxPyTMutex
= new wxMutex
;
185 // Bail out if there is already a wxApp created. This means that the
186 // toolkit has already been initialized, as in embedding wxPython in
187 // a C++ wxWindows app, so we don't need to call wxEntryStart.
188 if (wxTheApp
!= NULL
) {
191 wxPyDoCleanup
= TRUE
;
195 PyObject
* sysargv
= PySys_GetObject("argv");
196 if (sysargv
!= NULL
) {
197 argc
= PyList_Size(sysargv
);
198 argv
= new char*[argc
+1];
200 for(x
=0; x
<argc
; x
++) {
201 PyObject
*item
= PyList_GetItem(sysargv
, x
);
203 if (PyUnicode_Check(item
))
204 argv
[x
] = wxPyCopyCString(PyUnicode_AS_UNICODE(item
));
207 argv
[x
] = wxPyCopyCString(PyString_AsString(item
));
212 wxEntryStart(argc
, argv
);
218 // Start the user application, user App's OnInit method is a parameter here
219 PyObject
* __wxStart(PyObject
* /* self */, PyObject
* args
)
221 PyObject
* onInitFunc
= NULL
;
226 if (!PyArg_ParseTuple(args
, "O", &onInitFunc
))
229 #if 0 // Try it out without this check, see how it does...
230 if (wxTopLevelWindows
.Number() > 0) {
231 PyErr_SetString(PyExc_TypeError
, "Only 1 wxApp per process!");
236 // This is the next part of the wxEntry functionality...
238 wxChar
** argv
= NULL
;
239 PyObject
* sysargv
= PySys_GetObject("argv");
240 if (sysargv
!= NULL
) {
241 argc
= PyList_Size(sysargv
);
242 argv
= new wxChar
*[argc
+1];
244 for(x
=0; x
<argc
; x
++) {
245 PyObject
*pyArg
= PyList_GetItem(sysargv
, x
);
247 if (PyUnicode_Check(pyArg
))
248 argv
[x
] = wxPyCopyWString(PyUnicode_AS_UNICODE(pyArg
));
251 argv
[x
] = wxPyCopyWString(PyString_AsString(pyArg
));
256 wxPythonApp
->argc
= argc
;
257 wxPythonApp
->argv
= argv
;
261 // Call the Python App's OnInit function
262 arglist
= PyTuple_New(0);
263 result
= PyEval_CallObject(onInitFunc
, arglist
);
264 if (!result
) { // an exception was raised.
268 if (! PyInt_Check(result
)) {
269 PyErr_SetString(PyExc_TypeError
, "OnInit should return a boolean value");
272 bResult
= PyInt_AS_LONG(result
);
274 PyErr_SetString(PyExc_SystemExit
, "OnInit returned FALSE, exiting...");
279 wxTheApp
->m_initialized
= (wxTopLevelWindows
.GetCount() > 0);
290 #ifdef WXP_WITH_THREAD
293 wxPyTStates
->Empty();
301 static PyObject
* wxPython_dict
= NULL
;
302 static PyObject
* wxPyPtrTypeMap
= NULL
;
305 PyObject
* __wxSetDictionary(PyObject
* /* self */, PyObject
* args
)
308 if (!PyArg_ParseTuple(args
, "O", &wxPython_dict
))
311 if (!PyDict_Check(wxPython_dict
)) {
312 PyErr_SetString(PyExc_TypeError
, "_wxSetDictionary must have dictionary object!");
316 if (! wxPyPtrTypeMap
)
317 wxPyPtrTypeMap
= PyDict_New();
318 PyDict_SetItemString(wxPython_dict
, "__wxPyPtrTypeMap", wxPyPtrTypeMap
);
322 #define wxPlatform "__WXMOTIF__"
325 #define wxPlatform "__WXX11__"
328 #define wxPlatform "__WXGTK__"
330 #if defined(__WIN32__) || defined(__WXMSW__)
331 #define wxPlatform "__WXMSW__"
334 #define wxPlatform "__WXMAC__"
337 PyDict_SetItemString(wxPython_dict
, "wxPlatform", PyString_FromString(wxPlatform
));
338 PyDict_SetItemString(wxPython_dict
, "wxUSE_UNICODE", PyInt_FromLong(wxUSE_UNICODE
));
344 //---------------------------------------------------------------------------
346 void wxPyClientData_dtor(wxPyClientData
* self
) {
347 wxPyBeginBlockThreads();
348 Py_DECREF(self
->m_obj
);
349 wxPyEndBlockThreads();
352 void wxPyUserData_dtor(wxPyUserData
* self
) {
353 wxPyBeginBlockThreads();
354 Py_DECREF(self
->m_obj
);
355 wxPyEndBlockThreads();
359 // This is called when an OOR controled object is being destroyed. Although
360 // the C++ object is going away there is no way to force the Python object
361 // (and all references to it) to die too. This causes problems (crashes) in
362 // wxPython when a python shadow object attempts to call a C++ method using
363 // the now bogus pointer... So to try and prevent this we'll do a little black
364 // magic and change the class of the python instance to a class that will
365 // raise an exception for any attempt to call methods with it. See
366 // _wxPyDeadObject in _extras.py for the implementation of this class.
367 void wxPyOORClientData_dtor(wxPyOORClientData
* self
) {
369 static PyObject
* deadObjectClass
= NULL
;
371 wxPyBeginBlockThreads();
372 if (deadObjectClass
== NULL
) {
373 deadObjectClass
= PyDict_GetItemString(wxPython_dict
, "_wxPyDeadObject");
374 wxASSERT_MSG(deadObjectClass
!= NULL
, wxT("Can't get _wxPyDeadObject class!"));
375 Py_INCREF(deadObjectClass
);
378 // Clear the instance's dictionary, put the name of the old class into the
379 // instance, and then reset the class to be the dead class.
380 if (self
->m_obj
->ob_refcnt
> 1) { // but only if there is more than one reference
381 wxASSERT_MSG(PyInstance_Check(self
->m_obj
), wxT("m_obj not an instance!?!?!"));
382 PyInstanceObject
* inst
= (PyInstanceObject
*)self
->m_obj
;
383 PyDict_Clear(inst
->in_dict
);
384 PyDict_SetItemString(inst
->in_dict
, "_name", inst
->in_class
->cl_name
);
385 inst
->in_class
= (PyClassObject
*)deadObjectClass
;
386 Py_INCREF(deadObjectClass
);
388 wxPyEndBlockThreads();
391 //---------------------------------------------------------------------------
392 // Stuff used by OOR to find the right wxPython class type to return and to
396 // The pointer type map is used when the "pointer" type name generated by SWIG
397 // is not the same as the shadow class name, for example wxPyTreeCtrl
398 // vs. wxTreeCtrl. It needs to be referenced in Python as well as from C++,
399 // so we'll just make it a Python dictionary in the wx module's namespace.
400 // (See __wxSetDictionary)
401 void wxPyPtrTypeMap_Add(const char* commonName
, const char* ptrName
) {
402 if (! wxPyPtrTypeMap
)
403 wxPyPtrTypeMap
= PyDict_New();
404 PyDict_SetItemString(wxPyPtrTypeMap
,
406 PyString_FromString((char*)ptrName
));
411 PyObject
* wxPyClassExists(const wxString
& className
) {
416 char buff
[64]; // should always be big enough...
418 sprintf(buff
, "%sPtr", className
.mbc_str());
419 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
421 return classobj
; // returns NULL if not found
425 PyObject
* wxPyMake_wxObject(wxObject
* source
, bool checkEvtHandler
) {
426 PyObject
* target
= NULL
;
427 bool isEvtHandler
= FALSE
;
430 // If it's derived from wxEvtHandler then there may
431 // already be a pointer to a Python object that we can use
433 if (checkEvtHandler
&& wxIsKindOf(source
, wxEvtHandler
)) {
435 wxEvtHandler
* eh
= (wxEvtHandler
*)source
;
436 wxPyOORClientData
* data
= (wxPyOORClientData
*)eh
->GetClientObject();
438 target
= data
->m_obj
;
444 // Otherwise make it the old fashioned way by making a
445 // new shadow object and putting this pointer in it.
446 wxClassInfo
* info
= source
->GetClassInfo();
447 wxChar
* name
= (wxChar
*)info
->GetClassName();
448 PyObject
* klass
= wxPyClassExists(name
);
449 while (info
&& !klass
) {
450 name
= (wxChar
*)info
->GetBaseClassName1();
451 info
= wxClassInfo::FindClass(name
);
452 klass
= wxPyClassExists(name
);
455 target
= wxPyConstructObject(source
, name
, klass
, FALSE
);
456 if (target
&& isEvtHandler
)
457 ((wxEvtHandler
*)source
)->SetClientObject(new wxPyOORClientData(target
));
459 wxString
msg(wxT("wxPython class not found for "));
460 msg
+= source
->GetClassInfo()->GetClassName();
461 PyErr_SetString(PyExc_NameError
, msg
.mbc_str());
465 } else { // source was NULL so return None.
466 Py_INCREF(Py_None
); target
= Py_None
;
472 PyObject
* wxPyMake_wxSizer(wxSizer
* source
) {
473 PyObject
* target
= NULL
;
475 if (source
&& wxIsKindOf(source
, wxSizer
)) {
476 // If it's derived from wxSizer then there may
477 // already be a pointer to a Python object that we can use
479 wxSizer
* sz
= (wxSizer
*)source
;
480 wxPyOORClientData
* data
= (wxPyOORClientData
*)sz
->GetClientObject();
482 target
= data
->m_obj
;
487 target
= wxPyMake_wxObject(source
, FALSE
);
488 if (target
!= Py_None
)
489 ((wxSizer
*)source
)->SetClientObject(new wxPyOORClientData(target
));
496 //---------------------------------------------------------------------------
498 PyObject
* wxPyConstructObject(void* ptr
,
499 const wxString
& className
,
506 wxString
name(className
);
507 char swigptr
[64]; // should always be big enough...
510 if ((item
= PyDict_GetItemString(wxPyPtrTypeMap
, (char*)(const char*)name
.mbc_str())) != NULL
) {
511 name
= wxString(PyString_AsString(item
), *wxConvCurrent
);
513 sprintf(buff
, "_%s_p", (const char*)name
.mbc_str());
514 SWIG_MakePtr(swigptr
, ptr
, buff
);
516 arg
= Py_BuildValue("(s)", swigptr
);
517 obj
= PyInstance_New(klass
, arg
, NULL
);
521 PyObject
* one
= PyInt_FromLong(1);
522 PyObject_SetAttrString(obj
, "thisown", one
);
530 PyObject
* wxPyConstructObject(void* ptr
,
531 const wxString
& className
,
538 char buff
[64]; // should always be big enough...
539 sprintf(buff
, "%sPtr", (const char*)className
.mbc_str());
541 wxASSERT_MSG(wxPython_dict
, wxT("wxPython_dict is not set yet!!"));
543 PyObject
* classobj
= PyDict_GetItemString(wxPython_dict
, buff
);
545 wxString
msg(wxT("wxPython class not found for "));
547 PyErr_SetString(PyExc_NameError
, msg
.mbc_str());
551 return wxPyConstructObject(ptr
, className
, classobj
, setThisOwn
);
555 //---------------------------------------------------------------------------
558 #ifdef WXP_WITH_THREAD
560 unsigned long wxPyGetCurrentThreadId() {
561 return wxThread::GetCurrentId();
564 static PyThreadState
* gs_shutdownTState
;
566 PyThreadState
* wxPyGetThreadState() {
567 if (wxPyTMutex
== NULL
) // Python is shutting down...
568 return gs_shutdownTState
;
570 unsigned long ctid
= wxPyGetCurrentThreadId();
571 PyThreadState
* tstate
= NULL
;
574 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
575 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
576 if (info
.tid
== ctid
) {
577 tstate
= info
.tstate
;
581 wxPyTMutex
->Unlock();
582 wxASSERT_MSG(tstate
, wxT("PyThreadState should not be NULL!"));
587 void wxPySaveThreadState(PyThreadState
* tstate
) {
588 if (wxPyTMutex
== NULL
) { // Python is shutting down, assume a single thread...
589 gs_shutdownTState
= tstate
;
592 unsigned long ctid
= wxPyGetCurrentThreadId();
594 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
595 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
596 if (info
.tid
== ctid
) {
597 info
.tstate
= tstate
;
598 wxPyTMutex
->Unlock();
602 // not found, so add it...
603 wxPyTStates
->Add(new wxPyThreadState(ctid
, tstate
));
604 wxPyTMutex
->Unlock();
610 // Calls from Python to wxWindows code are wrapped in calls to these
613 PyThreadState
* wxPyBeginAllowThreads() {
614 #ifdef WXP_WITH_THREAD
615 PyThreadState
* saved
= PyEval_SaveThread(); // Py_BEGIN_ALLOW_THREADS;
616 wxPySaveThreadState(saved
);
623 void wxPyEndAllowThreads(PyThreadState
* saved
) {
624 #ifdef WXP_WITH_THREAD
625 PyEval_RestoreThread(saved
); // Py_END_ALLOW_THREADS;
631 // Calls from wxWindows back to Python code, or even any PyObject
632 // manipulations, PyDECREF's and etc. are wrapped in calls to these functions:
634 void wxPyBeginBlockThreads() {
635 #ifdef WXP_WITH_THREAD
636 PyThreadState
* tstate
= wxPyGetThreadState();
637 PyEval_RestoreThread(tstate
);
642 void wxPyEndBlockThreads() {
643 #ifdef WXP_WITH_THREAD
644 PyThreadState
* tstate
= PyEval_SaveThread();
645 // Is there any need to save it again?
650 //---------------------------------------------------------------------------
651 // wxPyInputStream and wxPyCBInputStream methods
654 void wxPyInputStream::close() {
655 /* do nothing for now */
658 void wxPyInputStream::flush() {
659 /* do nothing for now */
662 bool wxPyInputStream::eof() {
664 return m_wxis
->Eof();
669 wxPyInputStream::~wxPyInputStream() {
676 PyObject
* wxPyInputStream::read(int size
) {
677 PyObject
* obj
= NULL
;
679 const int BUFSIZE
= 1024;
681 // check if we have a real wxInputStream to work with
683 PyErr_SetString(PyExc_IOError
, "no valid C-wxInputStream");
689 while (! m_wxis
->Eof()) {
690 m_wxis
->Read(buf
.GetAppendBuf(BUFSIZE
), BUFSIZE
);
691 buf
.UngetAppendBuf(m_wxis
->LastRead());
694 } else { // Read only size number of characters
695 m_wxis
->Read(buf
.GetWriteBuf(size
), size
);
696 buf
.UngetWriteBuf(m_wxis
->LastRead());
700 if (m_wxis
->LastError() == wxSTREAM_READ_ERROR
) {
701 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
704 // We use only strings for the streams, not unicode
705 obj
= PyString_FromStringAndSize(buf
, buf
.GetDataLen());
711 PyObject
* wxPyInputStream::readline(int size
) {
712 PyObject
* obj
= NULL
;
717 // check if we have a real wxInputStream to work with
719 PyErr_SetString(PyExc_IOError
,"no valid C-wxInputStream");
723 // read until \n or byte limit reached
724 for (i
=ch
=0; (ch
!= '\n') && (!m_wxis
->Eof()) && ((size
< 0) || (i
< size
)); i
++) {
730 if (m_wxis
->LastError() == wxSTREAM_READ_ERROR
) {
731 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
734 // We use only strings for the streams, not unicode
735 obj
= PyString_FromStringAndSize((char*)buf
.GetData(), buf
.GetDataLen());
741 PyObject
* wxPyInputStream::readlines(int sizehint
) {
744 // check if we have a real wxInputStream to work with
746 PyErr_SetString(PyExc_IOError
,"no valid C-wxInputStream below");
751 pylist
= PyList_New(0);
757 // read sizehint bytes or until EOF
759 for (i
=0; (!m_wxis
->Eof()) && ((sizehint
< 0) || (i
< sizehint
));) {
760 PyObject
* s
= this->readline();
765 PyList_Append(pylist
, s
);
766 i
+= PyString_Size(s
);
770 if (m_wxis
->LastError() == wxSTREAM_READ_ERROR
) {
772 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
780 void wxPyInputStream::seek(int offset
, int whence
) {
782 m_wxis
->SeekI(offset
, wxSeekMode(whence
));
785 int wxPyInputStream::tell(){
787 return m_wxis
->TellI();
794 wxPyCBInputStream::wxPyCBInputStream(PyObject
*r
, PyObject
*s
, PyObject
*t
, bool block
)
795 : wxInputStream(), m_read(r
), m_seek(s
), m_tell(t
), m_block(block
)
799 wxPyCBInputStream::~wxPyCBInputStream() {
800 if (m_block
) wxPyBeginBlockThreads();
804 if (m_block
) wxPyEndBlockThreads();
808 wxPyCBInputStream
* wxPyCBInputStream::create(PyObject
*py
, bool block
) {
809 if (block
) wxPyBeginBlockThreads();
811 PyObject
* read
= getMethod(py
, "read");
812 PyObject
* seek
= getMethod(py
, "seek");
813 PyObject
* tell
= getMethod(py
, "tell");
816 PyErr_SetString(PyExc_TypeError
, "Not a file-like object");
820 if (block
) wxPyEndBlockThreads();
824 if (block
) wxPyEndBlockThreads();
825 return new wxPyCBInputStream(read
, seek
, tell
, block
);
828 PyObject
* wxPyCBInputStream::getMethod(PyObject
* py
, char* name
) {
829 if (!PyObject_HasAttrString(py
, name
))
831 PyObject
* o
= PyObject_GetAttrString(py
, name
);
832 if (!PyMethod_Check(o
) && !PyCFunction_Check(o
)) {
840 size_t wxPyCBInputStream::GetSize() const {
841 wxPyCBInputStream
* self
= (wxPyCBInputStream
*)this; // cast off const
842 if (m_seek
&& m_tell
) {
843 off_t temp
= self
->OnSysTell();
844 off_t ret
= self
->OnSysSeek(0, wxFromEnd
);
845 self
->OnSysSeek(temp
, wxFromStart
);
853 size_t wxPyCBInputStream::OnSysRead(void *buffer
, size_t bufsize
) {
857 wxPyBeginBlockThreads();
858 PyObject
* arglist
= Py_BuildValue("(i)", bufsize
);
859 PyObject
* result
= PyEval_CallObject(m_read
, arglist
);
863 if ((result
!= NULL
) && PyString_Check(result
)) {
864 o
= PyString_Size(result
);
866 m_lasterror
= wxSTREAM_EOF
;
869 memcpy((char*)buffer
, PyString_AsString(result
), o
); // strings only, not unicode...
874 m_lasterror
= wxSTREAM_READ_ERROR
;
875 wxPyEndBlockThreads();
880 size_t wxPyCBInputStream::OnSysWrite(const void *buffer
, size_t bufsize
) {
881 m_lasterror
= wxSTREAM_WRITE_ERROR
;
885 off_t
wxPyCBInputStream::OnSysSeek(off_t off
, wxSeekMode mode
) {
886 wxPyBeginBlockThreads();
887 PyObject
* arglist
= Py_BuildValue("(ii)", off
, mode
);
888 PyObject
* result
= PyEval_CallObject(m_seek
, arglist
);
891 wxPyEndBlockThreads();
895 off_t
wxPyCBInputStream::OnSysTell() const {
896 wxPyBeginBlockThreads();
897 PyObject
* arglist
= Py_BuildValue("()");
898 PyObject
* result
= PyEval_CallObject(m_tell
, arglist
);
901 if (result
!= NULL
) {
902 o
= PyInt_AsLong(result
);
905 wxPyEndBlockThreads();
909 //----------------------------------------------------------------------
911 IMPLEMENT_ABSTRACT_CLASS(wxPyCallback
, wxObject
);
913 wxPyCallback::wxPyCallback(PyObject
* func
) {
918 wxPyCallback::wxPyCallback(const wxPyCallback
& other
) {
919 m_func
= other
.m_func
;
923 wxPyCallback::~wxPyCallback() {
924 wxPyBeginBlockThreads();
926 wxPyEndBlockThreads();
931 // This function is used for all events destined for Python event handlers.
932 void wxPyCallback::EventThunker(wxEvent
& event
) {
933 wxPyCallback
* cb
= (wxPyCallback
*)event
.m_callbackUserData
;
934 PyObject
* func
= cb
->m_func
;
940 wxPyBeginBlockThreads();
941 wxString className
= event
.GetClassInfo()->GetClassName();
943 if (className
== "wxPyEvent")
944 arg
= ((wxPyEvent
*)&event
)->GetSelf();
945 else if (className
== "wxPyCommandEvent")
946 arg
= ((wxPyCommandEvent
*)&event
)->GetSelf();
948 arg
= wxPyConstructObject((void*)&event
, className
);
951 tuple
= PyTuple_New(1);
952 PyTuple_SET_ITEM(tuple
, 0, arg
);
953 result
= PyEval_CallObject(func
, tuple
);
957 PyErr_Clear(); // Just in case...
961 wxPyEndBlockThreads();
965 //----------------------------------------------------------------------
967 wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper
& other
) {
969 m_self
= other
.m_self
;
970 m_class
= other
.m_class
;
978 void wxPyCallbackHelper::setSelf(PyObject
* self
, PyObject
* klass
, int incref
) {
989 #if PYTHON_API_VERSION >= 1011
991 // Prior to Python 2.2 PyMethod_GetClass returned the class object
992 // in which the method was defined. Starting with 2.2 it returns
993 // "class that asked for the method" which seems totally bogus to me
994 // but apprently it fixes some obscure problem waiting to happen in
995 // Python. Since the API was not documented Guido and the gang felt
996 // safe in changing it. Needless to say that totally screwed up the
997 // logic below in wxPyCallbackHelper::findCallback, hence this icky
998 // code to find the class where the method is actually defined...
1001 PyObject
* PyFindClassWithAttr(PyObject
*klass
, PyObject
*name
)
1005 if (PyType_Check(klass
)) { // new style classes
1006 // This code is borrowed/adapted from _PyType_Lookup in typeobject.c
1007 // (TODO: This part is not tested yet, so I'm not sure it is correct...)
1008 PyTypeObject
* type
= (PyTypeObject
*)klass
;
1009 PyObject
*mro
, *res
, *base
, *dict
;
1010 /* Look in tp_dict of types in MRO */
1012 assert(PyTuple_Check(mro
));
1013 n
= PyTuple_GET_SIZE(mro
);
1014 for (i
= 0; i
< n
; i
++) {
1015 base
= PyTuple_GET_ITEM(mro
, i
);
1016 if (PyClass_Check(base
))
1017 dict
= ((PyClassObject
*)base
)->cl_dict
;
1019 assert(PyType_Check(base
));
1020 dict
= ((PyTypeObject
*)base
)->tp_dict
;
1022 assert(dict
&& PyDict_Check(dict
));
1023 res
= PyDict_GetItem(dict
, name
);
1030 else if (PyClass_Check(klass
)) { // old style classes
1031 // This code is borrowed/adapted from class_lookup in classobject.c
1032 PyClassObject
* cp
= (PyClassObject
*)klass
;
1033 PyObject
*value
= PyDict_GetItem(cp
->cl_dict
, name
);
1034 if (value
!= NULL
) {
1035 return (PyObject
*)cp
;
1037 n
= PyTuple_Size(cp
->cl_bases
);
1038 for (i
= 0; i
< n
; i
++) {
1039 PyObject
* base
= PyTuple_GetItem(cp
->cl_bases
, i
);
1040 PyObject
*v
= PyFindClassWithAttr(base
, name
);
1052 PyObject
* PyMethod_GetDefiningClass(PyObject
* method
, const char* name
)
1054 PyObject
* mgc
= PyMethod_GET_CLASS(method
);
1056 #if PYTHON_API_VERSION <= 1010 // prior to Python 2.2, the easy way
1058 #else // 2.2 and after, the hard way...
1060 PyObject
* nameo
= PyString_FromString(name
);
1061 PyObject
* klass
= PyFindClassWithAttr(mgc
, nameo
);
1069 bool wxPyCallbackHelper::findCallback(const char* name
) const {
1070 wxPyCallbackHelper
* self
= (wxPyCallbackHelper
*)this; // cast away const
1071 self
->m_lastFound
= NULL
;
1073 // If the object (m_self) has an attibute of the given name...
1074 if (m_self
&& PyObject_HasAttrString(m_self
, (char*)name
)) {
1075 PyObject
*method
, *klass
;
1076 method
= PyObject_GetAttrString(m_self
, (char*)name
);
1078 // ...and if that attribute is a method, and if that method's class is
1079 // not from a base class...
1080 if (PyMethod_Check(method
) &&
1081 (klass
= PyMethod_GetDefiningClass(method
, (char*)name
)) != NULL
&&
1082 ((klass
== m_class
) || PyClass_IsSubclass(klass
, m_class
))) {
1084 // ...then we'll save a pointer to the method so callCallback can call it.
1085 self
->m_lastFound
= method
;
1091 return m_lastFound
!= NULL
;
1095 int wxPyCallbackHelper::callCallback(PyObject
* argTuple
) const {
1099 result
= callCallbackObj(argTuple
);
1100 if (result
) { // Assumes an integer return type...
1101 retval
= PyInt_AsLong(result
);
1103 PyErr_Clear(); // forget about it if it's not...
1108 // Invoke the Python callable object, returning the raw PyObject return
1109 // value. Caller should DECREF the return value and also call PyEval_SaveThread.
1110 PyObject
* wxPyCallbackHelper::callCallbackObj(PyObject
* argTuple
) const {
1113 // Save a copy of the pointer in case the callback generates another
1114 // callback. In that case m_lastFound will have a different value when
1115 // it gets back here...
1116 PyObject
* method
= m_lastFound
;
1118 result
= PyEval_CallObject(method
, argTuple
);
1119 Py_DECREF(argTuple
);
1128 void wxPyCBH_setCallbackInfo(wxPyCallbackHelper
& cbh
, PyObject
* self
, PyObject
* klass
, int incref
) {
1129 cbh
.setSelf(self
, klass
, incref
);
1132 bool wxPyCBH_findCallback(const wxPyCallbackHelper
& cbh
, const char* name
) {
1133 return cbh
.findCallback(name
);
1136 int wxPyCBH_callCallback(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
1137 return cbh
.callCallback(argTuple
);
1140 PyObject
* wxPyCBH_callCallbackObj(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
1141 return cbh
.callCallbackObj(argTuple
);
1145 void wxPyCBH_delete(wxPyCallbackHelper
* cbh
) {
1146 if (cbh
->m_incRef
) {
1147 wxPyBeginBlockThreads();
1148 Py_XDECREF(cbh
->m_self
);
1149 Py_XDECREF(cbh
->m_class
);
1150 wxPyEndBlockThreads();
1154 //---------------------------------------------------------------------------
1155 //---------------------------------------------------------------------------
1156 // These event classes can be derived from in Python and passed through the event
1157 // system without losing anything. They do this by keeping a reference to
1158 // themselves and some special case handling in wxPyCallback::EventThunker.
1161 wxPyEvtSelfRef::wxPyEvtSelfRef() {
1162 //m_self = Py_None; // **** We don't do normal ref counting to prevent
1163 //Py_INCREF(m_self); // circular loops...
1167 wxPyEvtSelfRef::~wxPyEvtSelfRef() {
1168 wxPyBeginBlockThreads();
1171 wxPyEndBlockThreads();
1174 void wxPyEvtSelfRef::SetSelf(PyObject
* self
, bool clone
) {
1175 wxPyBeginBlockThreads();
1183 wxPyEndBlockThreads();
1186 PyObject
* wxPyEvtSelfRef::GetSelf() const {
1192 IMPLEMENT_ABSTRACT_CLASS(wxPyEvent
, wxEvent
);
1193 IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent
, wxCommandEvent
);
1196 wxPyEvent::wxPyEvent(int id
)
1201 wxPyEvent::wxPyEvent(const wxPyEvent
& evt
)
1204 SetSelf(evt
.m_self
, TRUE
);
1208 wxPyEvent::~wxPyEvent() {
1212 wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType
, int id
)
1213 : wxCommandEvent(commandType
, id
) {
1217 wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent
& evt
)
1218 : wxCommandEvent(evt
)
1220 SetSelf(evt
.m_self
, TRUE
);
1224 wxPyCommandEvent::~wxPyCommandEvent() {
1230 //---------------------------------------------------------------------------
1231 //---------------------------------------------------------------------------
1234 wxPyTimer::wxPyTimer(PyObject
* callback
) {
1239 wxPyTimer::~wxPyTimer() {
1240 wxPyBeginBlockThreads();
1242 wxPyEndBlockThreads();
1245 void wxPyTimer::Notify() {
1246 if (!func
|| func
== Py_None
) {
1250 wxPyBeginBlockThreads();
1253 PyObject
* args
= Py_BuildValue("()");
1255 result
= PyEval_CallObject(func
, args
);
1264 wxPyEndBlockThreads();
1270 //---------------------------------------------------------------------------
1271 //---------------------------------------------------------------------------
1272 // Convert a wxList to a Python List
1274 PyObject
* wxPy_ConvertList(wxListBase
* list
, const char* className
) {
1278 wxNode
* node
= list
->First();
1280 wxPyBeginBlockThreads();
1281 pyList
= PyList_New(0);
1283 wxObj
= node
->Data();
1284 pyObj
= wxPyMake_wxObject(wxObj
); //wxPyConstructObject(wxObj, className);
1285 PyList_Append(pyList
, pyObj
);
1286 node
= node
->Next();
1288 wxPyEndBlockThreads();
1292 //----------------------------------------------------------------------
1294 long wxPyGetWinHandle(wxWindow
* win
) {
1296 return (long)win
->GetHandle();
1299 // Find and return the actual X-Window.
1301 if (win
->m_wxwindow
) {
1302 GdkWindowPrivate
* bwin
= (GdkWindowPrivate
*)GTK_PIZZA(win
->m_wxwindow
)->bin_window
;
1304 return (long)bwin
->xwindow
;
1311 //----------------------------------------------------------------------
1312 // Some helper functions for typemaps in my_typemaps.i, so they won't be
1313 // included in every file over and over again...
1315 #if PYTHON_API_VERSION >= 1009
1316 static char* wxStringErrorMsg
= "String or Unicode type required";
1318 static char* wxStringErrorMsg
= "String type required";
1322 wxString
* wxString_in_helper(PyObject
* source
) {
1324 #if PYTHON_API_VERSION >= 1009 // Have Python unicode API
1325 if (!PyString_Check(source
) && !PyUnicode_Check(source
)) {
1326 PyErr_SetString(PyExc_TypeError
, wxStringErrorMsg
);
1330 if (PyUnicode_Check(source
)) {
1331 target
= new wxString(PyUnicode_AS_UNICODE(source
));
1333 // It is a string, get pointers to it and transform to unicode
1334 char* tmpPtr
; int tmpSize
;
1335 PyString_AsStringAndSize(source
, &tmpPtr
, &tmpSize
);
1336 target
= new wxString(tmpPtr
, *wxConvCurrent
, tmpSize
);
1339 char* tmpPtr
; int tmpSize
;
1340 if (PyString_AsStringAndSize(source
, &tmpPtr
, &tmpSize
) == -1) {
1341 PyErr_SetString(PyExc_TypeError
, "Unable to convert string");
1344 target
= new wxString(tmpPtr
, tmpSize
);
1345 #endif // wxUSE_UNICODE
1347 #else // No Python unicode API (1.5.2)
1348 if (!PyString_Check(source
)) {
1349 PyErr_SetString(PyExc_TypeError
, wxStringErrorMsg
);
1352 target
= new wxString(PyString_AS_STRING(source
), PyString_GET_SIZE(source
));
1358 // Similar to above except doesn't use "new" and doesn't set an exception
1359 wxString
Py2wxString(PyObject
* source
)
1362 bool doDecRef
= FALSE
;
1364 #if PYTHON_API_VERSION >= 1009 // Have Python unicode API
1365 if (!PyString_Check(source
) && !PyUnicode_Check(source
)) {
1366 // Convert to String if not one already... (TODO: Unicode too?)
1367 source
= PyObject_Str(source
);
1372 if (PyUnicode_Check(source
)) {
1373 target
= PyUnicode_AS_UNICODE(source
);
1375 // It is a string, get pointers to it and transform to unicode
1376 char* tmpPtr
; int tmpSize
;
1377 PyString_AsStringAndSize(source
, &tmpPtr
, &tmpSize
);
1378 target
= wxString(tmpPtr
, *wxConvCurrent
, tmpSize
);
1381 char* tmpPtr
; int tmpSize
;
1382 PyString_AsStringAndSize(source
, &tmpPtr
, &tmpSize
);
1383 target
= wxString(tmpPtr
, tmpSize
);
1384 #endif // wxUSE_UNICODE
1386 #else // No Python unicode API (1.5.2)
1387 if (!PyString_Check(source
)) {
1388 // Convert to String if not one already...
1389 source
= PyObject_Str(source
);
1392 target
= wxString(PyString_AS_STRING(source
), PyString_GET_SIZE(source
));
1401 // Make either a Python String or Unicode object, depending on build mode
1402 PyObject
* wx2PyString(const wxString
& src
)
1406 str
= PyUnicode_FromUnicode(src
.c_str(), src
.Len());
1408 str
= PyString_FromStringAndSize(src
.c_str(), src
.Len());
1414 //----------------------------------------------------------------------
1417 byte
* byte_LIST_helper(PyObject
* source
) {
1418 if (!PyList_Check(source
)) {
1419 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1422 int count
= PyList_Size(source
);
1423 byte
* temp
= new byte
[count
];
1425 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1428 for (int x
=0; x
<count
; x
++) {
1429 PyObject
* o
= PyList_GetItem(source
, x
);
1430 if (! PyInt_Check(o
)) {
1431 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1434 temp
[x
] = (byte
)PyInt_AsLong(o
);
1440 int* int_LIST_helper(PyObject
* source
) {
1441 if (!PyList_Check(source
)) {
1442 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1445 int count
= PyList_Size(source
);
1446 int* temp
= new int[count
];
1448 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1451 for (int x
=0; x
<count
; x
++) {
1452 PyObject
* o
= PyList_GetItem(source
, x
);
1453 if (! PyInt_Check(o
)) {
1454 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1457 temp
[x
] = PyInt_AsLong(o
);
1463 long* long_LIST_helper(PyObject
* source
) {
1464 if (!PyList_Check(source
)) {
1465 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1468 int count
= PyList_Size(source
);
1469 long* temp
= new long[count
];
1471 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1474 for (int x
=0; x
<count
; x
++) {
1475 PyObject
* o
= PyList_GetItem(source
, x
);
1476 if (! PyInt_Check(o
)) {
1477 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1480 temp
[x
] = PyInt_AsLong(o
);
1486 char** string_LIST_helper(PyObject
* source
) {
1487 if (!PyList_Check(source
)) {
1488 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1491 int count
= PyList_Size(source
);
1492 char** temp
= new char*[count
];
1494 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1497 for (int x
=0; x
<count
; x
++) {
1498 PyObject
* o
= PyList_GetItem(source
, x
);
1499 if (! PyString_Check(o
)) {
1500 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1503 temp
[x
] = PyString_AsString(o
);
1508 //--------------------------------
1509 // Part of patch from Tim Hochberg
1510 static inline bool wxPointFromObjects(PyObject
* o1
, PyObject
* o2
, wxPoint
* point
) {
1511 if (PyInt_Check(o1
) && PyInt_Check(o2
)) {
1512 point
->x
= PyInt_AS_LONG(o1
);
1513 point
->y
= PyInt_AS_LONG(o2
);
1516 if (PyFloat_Check(o1
) && PyFloat_Check(o2
)) {
1517 point
->x
= (int)PyFloat_AS_DOUBLE(o1
);
1518 point
->y
= (int)PyFloat_AS_DOUBLE(o2
);
1521 if (PyInstance_Check(o1
) || PyInstance_Check(o2
)) {
1522 // Disallow instances because they can cause havok
1525 if (PyNumber_Check(o1
) && PyNumber_Check(o2
)) {
1526 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
1527 point
->x
= PyInt_AsLong(o1
);
1528 point
->y
= PyInt_AsLong(o2
);
1535 wxPoint
* wxPoint_LIST_helper(PyObject
* source
, int *count
) {
1536 // Putting all of the declarations here allows
1537 // us to put the error handling all in one place.
1540 PyObject
*o
, *o1
, *o2
;
1541 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1543 if (!PySequence_Check(source
)) {
1547 // The length of the sequence is returned in count.
1548 *count
= PySequence_Length(source
);
1553 temp
= new wxPoint
[*count
];
1555 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1558 for (x
=0; x
<*count
; x
++) {
1559 // Get an item: try fast way first.
1561 o
= PySequence_Fast_GET_ITEM(source
, x
);
1564 o
= PySequence_GetItem(source
, x
);
1570 // Convert o to wxPoint.
1571 if ((PyTuple_Check(o
) && PyTuple_GET_SIZE(o
) == 2) ||
1572 (PyList_Check(o
) && PyList_GET_SIZE(o
) == 2)) {
1573 o1
= PySequence_Fast_GET_ITEM(o
, 0);
1574 o2
= PySequence_Fast_GET_ITEM(o
, 1);
1575 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
1579 else if (PyInstance_Check(o
)) {
1581 if (SWIG_GetPtrObj(o
, (void **)&pt
, "_wxPoint_p")) {
1586 else if (PySequence_Check(o
) && PySequence_Length(o
) == 2) {
1587 o1
= PySequence_GetItem(o
, 0);
1588 o2
= PySequence_GetItem(o
, 1);
1589 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
1613 PyErr_SetString(PyExc_TypeError
, "Expected a sequence of length-2 sequences or wxPoints.");
1617 //------------------------------
1620 wxBitmap
** wxBitmap_LIST_helper(PyObject
* source
) {
1621 if (!PyList_Check(source
)) {
1622 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1625 int count
= PyList_Size(source
);
1626 wxBitmap
** temp
= new wxBitmap
*[count
];
1628 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1631 for (int x
=0; x
<count
; x
++) {
1632 PyObject
* o
= PyList_GetItem(source
, x
);
1633 if (PyInstance_Check(o
)) {
1635 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxBitmap_p")) {
1636 PyErr_SetString(PyExc_TypeError
,"Expected _wxBitmap_p.");
1642 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxBitmaps.");
1651 wxString
* wxString_LIST_helper(PyObject
* source
) {
1652 if (!PyList_Check(source
)) {
1653 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1656 int count
= PyList_Size(source
);
1657 wxString
* temp
= new wxString
[count
];
1659 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1662 for (int x
=0; x
<count
; x
++) {
1663 PyObject
* o
= PyList_GetItem(source
, x
);
1664 #if PYTHON_API_VERSION >= 1009
1665 if (! PyString_Check(o
) && ! PyUnicode_Check(o
)) {
1666 PyErr_SetString(PyExc_TypeError
, "Expected a list of string or unicode objects.");
1670 if (! PyString_Check(o
)) {
1671 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
1676 wxString
* pStr
= wxString_in_helper(o
);
1684 wxAcceleratorEntry
* wxAcceleratorEntry_LIST_helper(PyObject
* source
) {
1685 if (!PyList_Check(source
)) {
1686 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1689 int count
= PyList_Size(source
);
1690 wxAcceleratorEntry
* temp
= new wxAcceleratorEntry
[count
];
1692 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1695 for (int x
=0; x
<count
; x
++) {
1696 PyObject
* o
= PyList_GetItem(source
, x
);
1697 if (PyInstance_Check(o
)) {
1698 wxAcceleratorEntry
* ae
;
1699 if (SWIG_GetPtrObj(o
, (void **) &ae
,"_wxAcceleratorEntry_p")) {
1700 PyErr_SetString(PyExc_TypeError
,"Expected _wxAcceleratorEntry_p.");
1705 else if (PyTuple_Check(o
)) {
1706 PyObject
* o1
= PyTuple_GetItem(o
, 0);
1707 PyObject
* o2
= PyTuple_GetItem(o
, 1);
1708 PyObject
* o3
= PyTuple_GetItem(o
, 2);
1709 temp
[x
].Set(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
));
1712 PyErr_SetString(PyExc_TypeError
, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
1720 wxPen
** wxPen_LIST_helper(PyObject
* source
) {
1721 if (!PyList_Check(source
)) {
1722 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1725 int count
= PyList_Size(source
);
1726 wxPen
** temp
= new wxPen
*[count
];
1728 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1731 for (int x
=0; x
<count
; x
++) {
1732 PyObject
* o
= PyList_GetItem(source
, x
);
1733 if (PyInstance_Check(o
)) {
1735 if (SWIG_GetPtrObj(o
, (void **) &pt
,"_wxPen_p")) {
1737 PyErr_SetString(PyExc_TypeError
,"Expected _wxPen_p.");
1744 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxPens.");
1752 bool _2int_seq_helper(PyObject
* source
, int* i1
, int* i2
) {
1753 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1756 if (!PySequence_Check(source
) || PySequence_Length(source
) != 2)
1760 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1761 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1764 o1
= PySequence_GetItem(source
, 0);
1765 o2
= PySequence_GetItem(source
, 1);
1768 *i1
= PyInt_AsLong(o1
);
1769 *i2
= PyInt_AsLong(o2
);
1779 bool _4int_seq_helper(PyObject
* source
, int* i1
, int* i2
, int* i3
, int* i4
) {
1780 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
1781 PyObject
*o1
, *o2
, *o3
, *o4
;
1783 if (!PySequence_Check(source
) || PySequence_Length(source
) != 4)
1787 o1
= PySequence_Fast_GET_ITEM(source
, 0);
1788 o2
= PySequence_Fast_GET_ITEM(source
, 1);
1789 o3
= PySequence_Fast_GET_ITEM(source
, 2);
1790 o4
= PySequence_Fast_GET_ITEM(source
, 3);
1793 o1
= PySequence_GetItem(source
, 0);
1794 o2
= PySequence_GetItem(source
, 1);
1795 o3
= PySequence_GetItem(source
, 2);
1796 o4
= PySequence_GetItem(source
, 3);
1799 *i1
= PyInt_AsLong(o1
);
1800 *i2
= PyInt_AsLong(o2
);
1801 *i3
= PyInt_AsLong(o3
);
1802 *i4
= PyInt_AsLong(o4
);
1814 //----------------------------------------------------------------------
1816 bool wxSize_helper(PyObject
* source
, wxSize
** obj
) {
1818 // If source is an object instance then it may already be the right type
1819 if (PyInstance_Check(source
)) {
1821 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxSize_p"))
1826 // otherwise a 2-tuple of integers is expected
1827 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1828 PyObject
* o1
= PySequence_GetItem(source
, 0);
1829 PyObject
* o2
= PySequence_GetItem(source
, 1);
1830 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1835 **obj
= wxSize(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1842 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxSize object.");
1846 bool wxPoint_helper(PyObject
* source
, wxPoint
** obj
) {
1848 // If source is an object instance then it may already be the right type
1849 if (PyInstance_Check(source
)) {
1851 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxPoint_p"))
1856 // otherwise a length-2 sequence of integers is expected
1857 if (PySequence_Check(source
) && PySequence_Length(source
) == 2) {
1858 PyObject
* o1
= PySequence_GetItem(source
, 0);
1859 PyObject
* o2
= PySequence_GetItem(source
, 1);
1860 // This should really check for integers, not numbers -- but that would break code.
1861 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1866 **obj
= wxPoint(PyInt_AsLong(o1
), PyInt_AsLong(o2
));
1872 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of integers or a wxPoint object.");
1878 bool wxRealPoint_helper(PyObject
* source
, wxRealPoint
** obj
) {
1880 // If source is an object instance then it may already be the right type
1881 if (PyInstance_Check(source
)) {
1883 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRealPoint_p"))
1888 // otherwise a 2-tuple of floats is expected
1889 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
1890 PyObject
* o1
= PySequence_GetItem(source
, 0);
1891 PyObject
* o2
= PySequence_GetItem(source
, 1);
1892 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
1897 **obj
= wxRealPoint(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
));
1904 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxRealPoint object.");
1911 bool wxRect_helper(PyObject
* source
, wxRect
** obj
) {
1913 // If source is an object instance then it may already be the right type
1914 if (PyInstance_Check(source
)) {
1916 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxRect_p"))
1921 // otherwise a 4-tuple of integers is expected
1922 else if (PySequence_Check(source
) && PyObject_Length(source
) == 4) {
1923 PyObject
* o1
= PySequence_GetItem(source
, 0);
1924 PyObject
* o2
= PySequence_GetItem(source
, 1);
1925 PyObject
* o3
= PySequence_GetItem(source
, 2);
1926 PyObject
* o4
= PySequence_GetItem(source
, 3);
1927 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
) ||
1928 !PyNumber_Check(o3
) || !PyNumber_Check(o4
)) {
1935 **obj
= wxRect(PyInt_AsLong(o1
), PyInt_AsLong(o2
),
1936 PyInt_AsLong(o3
), PyInt_AsLong(o4
));
1945 PyErr_SetString(PyExc_TypeError
, "Expected a 4-tuple of integers or a wxRect object.");
1951 bool wxColour_helper(PyObject
* source
, wxColour
** obj
) {
1953 // If source is an object instance then it may already be the right type
1954 if (PyInstance_Check(source
)) {
1956 if (SWIG_GetPtrObj(source
, (void **)&ptr
, "_wxColour_p"))
1961 // otherwise a string is expected
1962 else if (PyString_Check(source
)) {
1963 wxString
spec(PyString_AS_STRING(source
), *wxConvCurrent
);
1964 if (spec
.GetChar(0) == '#' && spec
.Length() == 7) { // It's #RRGGBB
1965 long red
, green
, blue
;
1966 red
= green
= blue
= 0;
1967 spec
.Mid(1,2).ToLong(&red
, 16);
1968 spec
.Mid(3,2).ToLong(&green
, 16);
1969 spec
.Mid(5,2).ToLong(&blue
, 16);
1971 **obj
= wxColour(red
, green
, blue
);
1974 else { // it's a colour name
1975 **obj
= wxColour(spec
);
1981 PyErr_SetString(PyExc_TypeError
,
1982 "Expected a wxColour object or a string containing a colour "
1983 "name or '#RRGGBB'.");
1988 //----------------------------------------------------------------------
1990 PyObject
* wxArrayString2PyList_helper(const wxArrayString
& arr
) {
1992 PyObject
* list
= PyList_New(0);
1993 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
1995 PyObject
* str
= PyUnicode_FromUnicode(arr
[i
].c_str(), arr
[i
].Len());
1997 PyObject
* str
= PyString_FromStringAndSize(arr
[i
].c_str(), arr
[i
].Len());
1999 PyList_Append(list
, str
);
2006 PyObject
* wxArrayInt2PyList_helper(const wxArrayInt
& arr
) {
2008 PyObject
* list
= PyList_New(0);
2009 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
2010 PyObject
* number
= PyInt_FromLong(arr
[i
]);
2011 PyList_Append(list
, number
);
2018 //----------------------------------------------------------------------
2019 //----------------------------------------------------------------------