+//----------------------------------------------------------------------
+// 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;
+}
+
+
+//----------------------------------------------------------------------
+// Function to test if the Display (or whatever is the platform equivallent)
+// can be connected to. This is accessable from wxPython as a staticmethod of
+// wx.App called DisplayAvailable().
+
+
+bool wxPyTestDisplayAvailable()
+{
+#ifdef __WXGTK__
+ Display* display;
+ display = XOpenDisplay(NULL);
+ if (display == NULL)
+ return false;
+ XCloseDisplay(display);
+ return true;
+#endif
+
+#ifdef __WXMAC__
+ // This is adapted from Python's Mac/Modules/MacOS.c in the
+ // MacOS_WMAvailable function.
+ bool rv;
+ ProcessSerialNumber psn;
+
+ /*
+ ** This is a fairly innocuous call to make if we don't have a window
+ ** manager, or if we have no permission to talk to it. It will print
+ ** a message on stderr, but at least it won't abort the process.
+ ** It appears the function caches the result itself, and it's cheap, so
+ ** no need for us to cache.
+ */
+#ifdef kCGNullDirectDisplay
+ /* On 10.1 CGMainDisplayID() isn't available, and
+ ** kCGNullDirectDisplay isn't defined.
+ */
+ if (CGMainDisplayID() == 0) {
+ rv = false;
+ } else
+#endif
+ {
+ // Also foreground the application on the first call as a side-effect.
+ if (GetCurrentProcess(&psn) < 0 || SetFrontProcess(&psn) < 0) {
+ rv = false;
+ } else {
+ rv = true;
+ }
+ }
+ return rv;
+#endif
+
+#ifdef __WXMSW__
+ // TODO...
+ return true;
+#endif
+}
+
+