X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/d559219f4016139d6c52d3b386903821a99c54d2..4afd752902ae7c44f4b3dc2edb11d7c24f95ed54:/utils/wxPython/src/helpers.h?ds=sidebyside diff --git a/utils/wxPython/src/helpers.h b/utils/wxPython/src/helpers.h index 54baeab6f6..57f670fb19 100644 --- a/utils/wxPython/src/helpers.h +++ b/utils/wxPython/src/helpers.h @@ -1,4 +1,4 @@ -///////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////// // Name: helpers.h // Purpose: Helper functions/classes for the wxPython extenaion module // @@ -40,6 +40,8 @@ # define HELPEREXPORT #endif +typedef unsigned char byte; + //---------------------------------------------------------------------- class wxPyApp: public wxApp @@ -64,12 +66,29 @@ PyObject* __wxSetDictionary(PyObject*, PyObject* args); void wxPyEventThunker(wxObject*, wxEvent& event); -HELPEREXPORT PyObject* wxPyConstructObject(void* ptr, char* className); +HELPEREXPORT PyObject* wxPyConstructObject(void* ptr, const char* className); HELPEREXPORT bool wxPyRestoreThread(); HELPEREXPORT void wxPySaveThread(bool doSave); +HELPEREXPORT PyObject* wxPy_ConvertList(wxListBase* list, const char* className); //---------------------------------------------------------------------- +// These are helpers used by the typemaps + +HELPEREXPORT byte* byte_LIST_helper(PyObject* source); +HELPEREXPORT int* int_LIST_helper(PyObject* source); +HELPEREXPORT long* long_LIST_helper(PyObject* source); +HELPEREXPORT char** string_LIST_helper(PyObject* source); +HELPEREXPORT wxPoint* wxPoint_LIST_helper(PyObject* source); +HELPEREXPORT wxBitmap** wxBitmap_LIST_helper(PyObject* source); +HELPEREXPORT wxString* wxString_LIST_helper(PyObject* source); +HELPEREXPORT wxAcceleratorEntry* wxAcceleratorEntry_LIST_helper(PyObject* source); + +HELPEREXPORT bool wxSize_helper(PyObject* source, wxSize** obj); +HELPEREXPORT bool wxPoint_helper(PyObject* source, wxPoint** obj); +HELPEREXPORT bool wxRealPoint_helper(PyObject* source, wxRealPoint** obj); +HELPEREXPORT bool wxRect_helper(PyObject* source, wxRect** obj); +//---------------------------------------------------------------------- #ifndef SWIGCODE extern "C" void SWIG_MakePtr(char *, void *, char *); @@ -82,7 +101,6 @@ extern "C" char *SWIG_GetPtrObj(PyObject *obj, void **ptr, char *type); # pragma warning(disable:4800) #endif -typedef unsigned char byte; // Non-const versions to keep SWIG happy. @@ -93,8 +111,10 @@ extern wxString wxPyEmptyStr; //---------------------------------------------------------------------- class wxPyCallback : public wxObject { + DECLARE_ABSTRACT_CLASS(wxPyCallback); public: wxPyCallback(PyObject* func); + wxPyCallback(const wxPyCallback& other); ~wxPyCallback(); void EventThunker(wxEvent& event); @@ -130,20 +150,6 @@ private: //--------------------------------------------------------------------------- -class wxPyEvent : public wxCommandEvent { - DECLARE_DYNAMIC_CLASS(wxPyEvent) -public: - wxPyEvent(wxEventType commandType = wxEVT_NULL, PyObject* userData = Py_None); - ~wxPyEvent(); - - void SetUserData(PyObject* userData); - PyObject* GetUserData(); - -private: - PyObject* m_userData; -}; - - @@ -156,14 +162,15 @@ private: // // **** This class should be combined with wxPyCallback defined above. // -//--------------------------------------------------------------------------- class HELPEREXPORT wxPyCallbackHelper { public: wxPyCallbackHelper(); ~wxPyCallbackHelper(); - void setSelf(PyObject* self); + wxPyCallbackHelper(const wxPyCallbackHelper& other); + + void setSelf(PyObject* self, int incref=TRUE); bool findCallback(const wxString& name); int callCallback(PyObject* argTuple); @@ -172,20 +179,61 @@ public: private: PyObject* m_self; PyObject* m_lastFound; + int m_incRef; }; +//--------------------------------------------------------------------------- +//--------------------------------------------------------------------------- +// These classes can be derived from in Python and passed through the event +// system without loosing anything. They do this by keeping a reference to +// themselves and some special case handling in wxPyCallback::EventThunker. + + +class wxPyEvtSelfRef { +public: + wxPyEvtSelfRef(); + ~wxPyEvtSelfRef(); + + void SetSelf(PyObject* self, bool clone=FALSE); + PyObject* GetSelf() const; + +protected: + PyObject* m_self; + bool m_cloned; +}; + + +class wxPyEvent : public wxEvent, public wxPyEvtSelfRef { + DECLARE_DYNAMIC_CLASS(wxPyEvent) +public: + wxPyEvent(int id=0); + ~wxPyEvent(); + + void CopyObject(wxObject& dest) const; +}; + + +class wxPyCommandEvent : public wxCommandEvent, public wxPyEvtSelfRef { + DECLARE_DYNAMIC_CLASS(wxPyCommandEvent) +public: + wxPyCommandEvent(wxEventType commandType = wxEVT_NULL, int id=0); + ~wxPyCommandEvent(); + + void CopyObject(wxObject& dest) const; +}; + //--------------------------------------------------------------------------- // These macros are used to implement the virtual methods that should // redirect to a Python method if one exists. The names designate the -// return type, if any as well as any parameter types. +// return type, if any, as well as any parameter types. //--------------------------------------------------------------------------- -#define PYPRIVATE \ - void _setSelf(PyObject* self) { \ - m_myInst.setSelf(self); \ - } \ +#define PYPRIVATE \ + void _setSelf(PyObject* self, int incref=1) { \ + m_myInst.setSelf(self, incref); \ + } \ private: wxPyCallbackHelper m_myInst; //--------------------------------------------------------------------------- @@ -408,13 +456,15 @@ private: #define IMP_PYCALLBACK_BOOL_DC4DBLBOOL(CLASS, PCLASS, CBNAME) \ bool CLASS::CBNAME(wxDC& a, double b, double c, double d, double e, bool f) { \ bool doSave = wxPyRestoreThread(); \ + bool rval; \ if (m_myInst.findCallback(#CBNAME)) \ - return m_myInst.callCallback(Py_BuildValue("(Oddddi)", \ + rval = m_myInst.callCallback(Py_BuildValue("(Oddddi)", \ wxPyConstructObject(&a, "wxDC"), \ b, c, d, e, (int)f)); \ else \ - return PCLASS::CBNAME(a, b, c, d, e, f); \ + rval = PCLASS::CBNAME(a, b, c, d, e, f); \ wxPySaveThread(doSave); \ + return rval; \ } \ bool CLASS::base_##CBNAME(wxDC& a, double b, double c, double d, double e, bool f) {\ return PCLASS::CBNAME(a, b, c, d, e, f); \ @@ -498,7 +548,7 @@ private: bool doSave = wxPyRestoreThread(); \ if (m_myInst.findCallback(#CBNAME)) \ m_myInst.callCallback(Py_BuildValue("(Oiddii)", \ - wxPyConstructObject(a, "wxControlPoint"), \ + wxPyConstructObject(a, "wxPyControlPoint"), \ (int)b, c, d, e, f)); \ else \ PCLASS::CBNAME(a, b, c, d, e, f); \ @@ -521,7 +571,7 @@ private: bool doSave = wxPyRestoreThread(); \ if (m_myInst.findCallback(#CBNAME)) \ m_myInst.callCallback(Py_BuildValue("(Oddii)", \ - wxPyConstructObject(a, "wxControlPoint"), \ + wxPyConstructObject(a, "wxPyControlPoint"), \ b, c, d, e)); \ else \ PCLASS::CBNAME(a, b, c, d, e); \ @@ -574,6 +624,337 @@ private: //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- + +#define DEC_PYCALLBACK__STRING(CBNAME) \ + void CBNAME(const wxString& a); \ + void base_##CBNAME(const wxString& a); + + +#define IMP_PYCALLBACK__STRING(CLASS, PCLASS, CBNAME) \ + void CLASS::CBNAME(const wxString& a) { \ + bool doSave = wxPyRestoreThread(); \ + if (m_myInst.findCallback(#CBNAME)) \ + m_myInst.callCallback(Py_BuildValue("(s)", a.c_str())); \ + else \ + PCLASS::CBNAME(a); \ + wxPySaveThread(doSave); \ + } \ + void CLASS::base_##CBNAME(const wxString& a) { \ + PCLASS::CBNAME(a); \ + } + +//--------------------------------------------------------------------------- + +#define DEC_PYCALLBACK_BOOL_STRING(CBNAME) \ + bool CBNAME(const wxString& a); \ + bool base_##CBNAME(const wxString& a); + + +#define IMP_PYCALLBACK_BOOL_STRING(CLASS, PCLASS, CBNAME) \ + bool CLASS::CBNAME(const wxString& a) { \ + bool rval; \ + bool doSave = wxPyRestoreThread(); \ + if (m_myInst.findCallback(#CBNAME)) \ + rval = m_myInst.callCallback(Py_BuildValue("(s)", a.c_str())); \ + else \ + rval = PCLASS::CBNAME(a); \ + wxPySaveThread(doSave); \ + return rval; \ + } \ + bool CLASS::base_##CBNAME(const wxString& a) { \ + return PCLASS::CBNAME(a); \ + } + +//--------------------------------------------------------------------------- + +#define DEC_PYCALLBACK_BOOL_STRINGSTRING(CBNAME) \ + bool CBNAME(const wxString& a, const wxString& b); \ + bool base_##CBNAME(const wxString& a, const wxString& b); + + +#define IMP_PYCALLBACK_BOOL_STRINGSTRING(CLASS, PCLASS, CBNAME) \ + bool CLASS::CBNAME(const wxString& a, const wxString& b) { \ + bool rval; \ + bool doSave = wxPyRestoreThread(); \ + if (m_myInst.findCallback(#CBNAME)) \ + rval = m_myInst.callCallback(Py_BuildValue("(ss)", \ + a.c_str(), b.c_str())); \ + else \ + rval = PCLASS::CBNAME(a, b); \ + wxPySaveThread(doSave); \ + return rval; \ + } \ + bool CLASS::base_##CBNAME(const wxString& a, const wxString& b) { \ + return PCLASS::CBNAME(a, b); \ + } + +//--------------------------------------------------------------------------- + +#define DEC_PYCALLBACK_STRING_(CBNAME) \ + wxString CBNAME(); \ + wxString base_##CBNAME(); + + +#define IMP_PYCALLBACK_STRING_(CLASS, PCLASS, CBNAME) \ + wxString CLASS::CBNAME() { \ + wxString rval; \ + bool doSave = wxPyRestoreThread(); \ + if (m_myInst.findCallback(#CBNAME)) { \ + PyObject* ro; \ + ro = m_myInst.callCallbackObj(Py_BuildValue("()")); \ + rval = PyString_AsString(PyObject_Str(ro)); \ + } \ + else \ + rval = PCLASS::CBNAME(); \ + wxPySaveThread(doSave); \ + return rval; \ + } \ + wxString CLASS::base_##CBNAME() { \ + return PCLASS::CBNAME(); \ + } + +//--------------------------------------------------------------------------- + +#define DEC_PYCALLBACK_STRING__pure(CBNAME) \ + wxString CBNAME(); + + +#define IMP_PYCALLBACK_STRING__pure(CLASS, PCLASS, CBNAME) \ + wxString CLASS::CBNAME() { \ + wxString rval; \ + bool doSave = wxPyRestoreThread(); \ + if (m_myInst.findCallback(#CBNAME)) { \ + PyObject* ro; \ + ro = m_myInst.callCallbackObj(Py_BuildValue("()")); \ + rval = PyString_AsString(PyObject_Str(ro)); \ + } \ + wxPySaveThread(doSave); \ + return rval; \ + } + +//--------------------------------------------------------------------------- + +#define DEC_PYCALLBACK_BOOL_TAG_pure(CBNAME) \ + bool CBNAME(const wxHtmlTag& a); \ + + +#define IMP_PYCALLBACK_BOOL_TAG_pure(CLASS, PCLASS, CBNAME) \ + bool CLASS::CBNAME(const wxHtmlTag& a) { \ + bool rval = false; \ + bool doSave = wxPyRestoreThread(); \ + if (m_myInst.findCallback(#CBNAME)) \ + rval = m_myInst.callCallback(Py_BuildValue("(O)", \ + wxPyConstructObject((void*)&a,"wxHtmlTag"))); \ + wxPySaveThread(doSave); \ + return rval; \ + } + +//--------------------------------------------------------------------------- + +#define DEC_PYCALLBACK___pure(CBNAME) \ + void CBNAME(); \ + + +#define IMP_PYCALLBACK___pure(CLASS, PCLASS, CBNAME) \ + void CLASS::CBNAME() { \ + bool doSave = wxPyRestoreThread(); \ + if (m_myInst.findCallback(#CBNAME)) \ + m_myInst.callCallback(Py_BuildValue("()")); \ + wxPySaveThread(doSave); \ + } + +//--------------------------------------------------------------------------- + +#define DEC_PYCALLBACK_wxSize__pure(CBNAME) \ + wxSize CBNAME(); \ + + +#define IMP_PYCALLBACK_wxSize__pure(CLASS, PCLASS, CBNAME) \ + wxSize CLASS::CBNAME() { \ + wxSize rval(0,0); \ + bool doSave = wxPyRestoreThread(); \ + if (m_myInst.findCallback(#CBNAME)) { \ + PyObject* ro; \ + wxSize* ptr; \ + ro = m_myInst.callCallbackObj(Py_BuildValue("()")); \ + if (! SWIG_GetPtrObj(ro, (void **)&ptr, "_wxSize_p")) \ + rval = *ptr; \ + } \ + wxPySaveThread(doSave); \ + return rval; \ + } + +//--------------------------------------------------------------------------- + +#define DEC_PYCALLBACK_BOOL_WXWIN(CBNAME) \ + bool CBNAME(wxWindow* a); \ + bool base_##CBNAME(wxWindow* a); + + +#define IMP_PYCALLBACK_BOOL_WXWIN(CLASS, PCLASS, CBNAME) \ + bool CLASS::CBNAME(wxWindow* a) { \ + bool rval; \ + bool doSave = wxPyRestoreThread(); \ + if (m_myInst.findCallback(#CBNAME)) \ + rval = m_myInst.callCallback(Py_BuildValue("(O)", \ + wxPyConstructObject((void*)a,"wxWindow"))); \ + else \ + rval = PCLASS::CBNAME(a); \ + wxPySaveThread(doSave); \ + return rval; \ + } \ + bool CLASS::base_##CBNAME(wxWindow* a) { \ + return PCLASS::CBNAME(a); \ + } + +//--------------------------------------------------------------------------- + +#define DEC_PYCALLBACK_BOOL_(CBNAME) \ + bool CBNAME(); \ + bool base_##CBNAME(); + + +#define IMP_PYCALLBACK_BOOL_(CLASS, PCLASS, CBNAME) \ + bool CLASS::CBNAME() { \ + bool rval; \ + bool doSave = wxPyRestoreThread(); \ + if (m_myInst.findCallback(#CBNAME)) \ + rval = m_myInst.callCallback(Py_BuildValue("()")); \ + else \ + rval = PCLASS::CBNAME(); \ + wxPySaveThread(doSave); \ + return rval; \ + } \ + bool CLASS::base_##CBNAME() { \ + return PCLASS::CBNAME(); \ + } + +//--------------------------------------------------------------------------- + +#define DEC_PYCALLBACK_DR_2WXCDR(CBNAME) \ + wxDragResult CBNAME(wxCoord x, wxCoord y, wxDragResult def); \ + wxDragResult base_##CBNAME(wxCoord x, wxCoord y, wxDragResult def); + + +#define IMP_PYCALLBACK_DR_2WXCDR(CLASS, PCLASS, CBNAME) \ + wxDragResult CLASS::CBNAME(wxCoord a, wxCoord b, wxDragResult c) { \ + bool doSave = wxPyRestoreThread(); \ + int rval; \ + if (m_myInst.findCallback(#CBNAME)) \ + rval = m_myInst.callCallback(Py_BuildValue("(iii)", a,b,c));\ + else \ + rval = PCLASS::CBNAME(a, b, c); \ + wxPySaveThread(doSave); \ + return (wxDragResult)rval; \ + } \ + wxDragResult CLASS::base_##CBNAME(wxCoord a, wxCoord b, wxDragResult c) { \ + return PCLASS::CBNAME(a, b, c); \ + } + +//--------------------------------------------------------------------------- + +#define DEC_PYCALLBACK_BOOL_DR(CBNAME) \ + bool CBNAME(wxDragResult a); \ + bool base_##CBNAME(wxDragResult a); + + +#define IMP_PYCALLBACK_BOOL_DR(CLASS, PCLASS, CBNAME) \ + bool CLASS::CBNAME(wxDragResult a) { \ + bool doSave = wxPyRestoreThread(); \ + bool rval; \ + if (m_myInst.findCallback(#CBNAME)) \ + rval = m_myInst.callCallback(Py_BuildValue("(i)", a)); \ + else \ + rval = PCLASS::CBNAME(a); \ + wxPySaveThread(doSave); \ + return rval; \ + } \ + bool CLASS::base_##CBNAME(wxDragResult a) { \ + return PCLASS::CBNAME(a); \ + } + +//--------------------------------------------------------------------------- + +#define DEC_PYCALLBACK_DR_2WXCDR_pure(CBNAME) \ + wxDragResult CBNAME(wxCoord x, wxCoord y, wxDragResult def); + + +#define IMP_PYCALLBACK_DR_2WXCDR_pure(CLASS, PCLASS, CBNAME) \ + wxDragResult CLASS::CBNAME(wxCoord a, wxCoord b, wxDragResult c) { \ + bool doSave = wxPyRestoreThread(); \ + int rval; \ + if (m_myInst.findCallback(#CBNAME)) \ + rval = m_myInst.callCallback(Py_BuildValue("(iii)", a,b,c));\ + wxPySaveThread(doSave); \ + return (wxDragResult)rval; \ + } \ + +//--------------------------------------------------------------------------- + +#define DEC_PYCALLBACK_BOOL_INTINTSTR_pure(CBNAME) \ + bool CBNAME(int a, int b, const wxString& c); + + +#define IMP_PYCALLBACK_BOOL_INTINTSTR_pure(CLASS, PCLASS, CBNAME) \ + bool CLASS::CBNAME(int a, int b, const wxString& c) { \ + bool rval; \ + bool doSave = wxPyRestoreThread(); \ + if (m_myInst.findCallback(#CBNAME)) \ + rval = m_myInst.callCallback(Py_BuildValue("(iis)",a,b,c.c_str()));\ + wxPySaveThread(doSave); \ + return rval; \ + } \ + +//--------------------------------------------------------------------------- + +#define DEC_PYCALLBACK_SIZET_(CBNAME) \ + size_t CBNAME(); \ + size_t base_##CBNAME(); + + +#define IMP_PYCALLBACK_SIZET_(CLASS, PCLASS, CBNAME) \ + size_t CLASS::CBNAME() { \ + size_t rval; \ + bool doSave = wxPyRestoreThread(); \ + if (m_myInst.findCallback(#CBNAME)) \ + rval = m_myInst.callCallback(Py_BuildValue("()")); \ + else \ + rval = PCLASS::CBNAME(); \ + wxPySaveThread(doSave); \ + return rval; \ + } \ + size_t CLASS::base_##CBNAME() { \ + return PCLASS::CBNAME(); \ + } + +//--------------------------------------------------------------------------- + +#define DEC_PYCALLBACK_DATAFMT_SIZET(CBNAME) \ + wxDataFormat CBNAME(); \ + wxDataFormat base_##CBNAME(); + + +#define IMP_PYCALLBACK_DATAFMT_SIZET(CLASS, PCLASS, CBNAME) \ + wxDataFormat CLASS::CBNAME(size_t a) { \ + wxDataFormat rval; \ + bool doSave = wxPyRestoreThread(); \ + if (m_myInst.findCallback(#CBNAME)) { \ + PyObject* ro; \ + wxDataFormat* ptr; \ + ro = m_myInst.callCallbackObj(Py_BuildValue("(i)", a)); \ + if (! SWIG_GetPtrObj(ro, (void **)&ptr, "_wxDataFormat_p")) \ + rval = *ptr; \ + } \ + else \ + rval = PCLASS::CBNAME(a); \ + wxPySaveThread(doSave); \ + return rval; \ + } \ + wxDataFormat CLASS::base_##CBNAME(size_t a) { \ + return PCLASS::CBNAME(a); \ + } + +//--------------------------------------------------------------------------- //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- @@ -581,3 +962,4 @@ private: +