+//----------------------------------------------------------------------
+// wxPyImageHandler methods
+//
+// TODO: Switch these to use wxPython's standard macros and helper classes
+// for calling callbacks.
+
+PyObject* wxPyImageHandler::m_DoCanRead_Name = NULL;
+PyObject* wxPyImageHandler::m_GetImageCount_Name = NULL;
+PyObject* wxPyImageHandler::m_LoadFile_Name = NULL;
+PyObject* wxPyImageHandler::m_SaveFile_Name = NULL;
+
+PyObject* wxPyImageHandler::py_InputStream(wxInputStream* stream) {
+ return wxPyConstructObject(new wxPyInputStream(stream),
+ wxT("wxPyInputStream"), 0);
+}
+
+PyObject* wxPyImageHandler::py_Image(wxImage* image) {
+ return wxPyConstructObject(image, wxT("wxImage"), 0);
+}
+
+PyObject* wxPyImageHandler::py_OutputStream(wxOutputStream* stream) {
+ return wxPyConstructObject(stream, wxT("wxOutputStream"), 0);
+}
+
+wxPyImageHandler::wxPyImageHandler():
+ m_self(NULL)
+{
+ if (!m_DoCanRead_Name) {
+ m_DoCanRead_Name = PyString_FromString("DoCanRead");
+ m_GetImageCount_Name = PyString_FromString("GetImageCount");
+ m_LoadFile_Name = PyString_FromString("LoadFile");
+ m_SaveFile_Name = PyString_FromString("SaveFile");
+ }
+}
+
+wxPyImageHandler::~wxPyImageHandler() {
+ if (m_self) {
+ Py_DECREF(m_self);
+ m_self = NULL;
+ }
+}
+
+void wxPyImageHandler::_SetSelf(PyObject *self) {
+ // should check here for isinstance(PyImageHandler) ??
+ m_self = self;
+ Py_INCREF(m_self);
+}
+
+bool wxPyImageHandler::DoCanRead(wxInputStream& stream) {
+ // check if our object has this method
+ wxPyBlock_t blocked = wxPyBeginBlockThreads();
+ if (!m_self || !PyObject_HasAttr(m_self, m_DoCanRead_Name)) {
+ wxPyEndBlockThreads(blocked);
+ return false;
+ }
+
+ PyObject* res = PyObject_CallMethodObjArgs(m_self, m_DoCanRead_Name,
+ py_InputStream(&stream), NULL);
+ bool retval = false;
+ if (res) {
+ retval = PyInt_AsLong(res);
+ Py_DECREF(res);
+ PyErr_Clear();
+ }
+ else
+ PyErr_Print();
+ wxPyEndBlockThreads(blocked);
+ return retval;
+}
+
+bool wxPyImageHandler::LoadFile( wxImage* image, wxInputStream& stream,
+ bool verbose, int index ) {
+ // check if our object has this method
+ wxPyBlock_t blocked = wxPyBeginBlockThreads();
+ if (!m_self || !PyObject_HasAttr(m_self, m_LoadFile_Name)) {
+ wxPyEndBlockThreads(blocked);
+ return false;
+ }
+ PyObject* res = PyObject_CallMethodObjArgs(m_self, m_LoadFile_Name,
+ py_Image(image),
+ py_InputStream(&stream),
+ PyInt_FromLong(verbose),
+ PyInt_FromLong(index),
+ NULL);
+ bool retval = false;
+ if (res) {
+ retval = PyInt_AsLong(res);
+ Py_DECREF(res);
+ PyErr_Clear();
+ } else
+ PyErr_Print();
+ wxPyEndBlockThreads(blocked);
+ return retval;
+}
+
+bool wxPyImageHandler::SaveFile( wxImage* image, wxOutputStream& stream,
+ bool verbose ) {
+ wxPyBlock_t blocked = wxPyBeginBlockThreads();
+ if (!m_self || !PyObject_HasAttr(m_self, m_SaveFile_Name)) {
+ wxPyEndBlockThreads(blocked);
+ return false;
+ }
+ PyObject* res = PyObject_CallMethodObjArgs(m_self, m_SaveFile_Name,
+ py_Image(image),
+ py_OutputStream(&stream),
+ PyInt_FromLong(verbose),
+ NULL);
+ bool retval = false;
+ if(res) {
+ retval=PyInt_AsLong(res);
+ Py_DECREF(res);
+ PyErr_Clear();
+ } else
+ PyErr_Print();
+ wxPyEndBlockThreads(blocked);
+ return retval;
+}
+
+int wxPyImageHandler::GetImageCount( wxInputStream& stream ) {
+ wxPyBlock_t blocked = wxPyBeginBlockThreads();
+ if (!m_self || !PyObject_HasAttr(m_self, m_GetImageCount_Name)) {
+ wxPyEndBlockThreads(blocked);
+ return 1;
+ }
+ PyObject *res=PyObject_CallMethodObjArgs(m_self, m_GetImageCount_Name,
+ py_InputStream(&stream),
+ NULL);
+ int retval = 1;
+ if(res) {
+ retval=PyInt_AsLong(res);
+ Py_DECREF(res);
+ PyErr_Clear();
+ } else
+ PyErr_Print();
+ wxPyEndBlockThreads(blocked);
+ return retval;
+}
+
+