+#define GetXWindow(wxwin) (wxwin)->m_wxwindow ? \
+ GDK_WINDOW_XWINDOW(GTK_PIZZA((wxwin)->m_wxwindow)->bin_window) : \
+ GDK_WINDOW_XWINDOW((wxwin)->m_widget->window)
+#include <locale.h>
+#endif
+
+#ifdef __WXX11__
+#include "wx/x11/privx.h"
+#define GetXWindow(wxwin) ((Window)(wxwin)->GetHandle())
+#endif
+
+#ifdef __WXMAC__
+#include <wx/mac/private.h>
+#endif
+
+#include <wx/clipbrd.h>
+#include <wx/mimetype.h>
+#include <wx/image.h>
+
+//----------------------------------------------------------------------
+
+#if PYTHON_API_VERSION < 1009 && wxUSE_UNICODE
+#error Python must support Unicode to use wxWindows Unicode
+#endif
+
+//----------------------------------------------------------------------
+
+wxPyApp* wxPythonApp = NULL; // Global instance of application object
+bool wxPyDoCleanup = false;
+bool wxPyDoingCleanup = false;
+
+
+#ifdef WXP_WITH_THREAD
+#if !wxPyUSE_GIL_STATE
+struct wxPyThreadState {
+ unsigned long tid;
+ PyThreadState* tstate;
+
+ wxPyThreadState(unsigned long _tid=0, PyThreadState* _tstate=NULL)
+ : tid(_tid), tstate(_tstate) {}
+};
+
+#include <wx/dynarray.h>
+WX_DECLARE_OBJARRAY(wxPyThreadState, wxPyThreadStateArray);
+#include <wx/arrimpl.cpp>
+WX_DEFINE_OBJARRAY(wxPyThreadStateArray);
+
+wxPyThreadStateArray* wxPyTStates = NULL;
+wxMutex* wxPyTMutex = NULL;
+
+#endif
+#endif
+
+
+#define DEFAULTENCODING_SIZE 64
+static char wxPyDefaultEncoding[DEFAULTENCODING_SIZE] = "ascii";
+
+static PyObject* wxPython_dict = NULL;
+static PyObject* wxPyAssertionError = NULL;
+static PyObject* wxPyNoAppError = NULL;
+
+PyObject* wxPyPtrTypeMap = NULL;
+
+
+#ifdef __WXMSW__ // If building for win32...
+//----------------------------------------------------------------------
+// This gets run when the DLL is loaded. We just need to save a handle.
+//----------------------------------------------------------------------
+
+extern "C"
+BOOL WINAPI DllMain(
+ HINSTANCE hinstDLL, // handle to DLL module
+ DWORD fdwReason, // reason for calling function
+ LPVOID lpvReserved // reserved
+ )
+{
+ // If wxPython is embedded in another wxWidgets app then
+ // the instance has already been set.
+ if (! wxGetInstance())
+ wxSetInstance(hinstDLL);
+ return true;
+}
+#endif
+
+//----------------------------------------------------------------------
+// Classes for implementing the wxp main application shell.
+//----------------------------------------------------------------------
+
+IMPLEMENT_ABSTRACT_CLASS(wxPyApp, wxApp);
+
+
+wxPyApp::wxPyApp() {
+ m_assertMode = wxPYAPP_ASSERT_EXCEPTION;
+ m_startupComplete = false;
+}
+
+
+wxPyApp::~wxPyApp() {
+ wxPythonApp = NULL;
+ wxApp::SetInstance(NULL);
+}
+
+
+// This one isn't acutally called... We fake it with _BootstrapApp
+bool wxPyApp::OnInit() {
+ return false;
+}
+
+
+int wxPyApp::MainLoop() {
+ int retval = 0;
+
+ DeletePendingObjects();
+ bool initialized = wxTopLevelWindows.GetCount() != 0;
+ if (initialized) {
+ if ( m_exitOnFrameDelete == Later ) {
+ m_exitOnFrameDelete = Yes;
+ }
+
+ retval = wxApp::MainLoop();
+ OnExit();
+ }
+ return retval;
+}
+
+
+bool wxPyApp::OnInitGui() {
+ bool rval=true;
+ wxApp::OnInitGui(); // in this case always call the base class version
+ wxPyBlock_t blocked = wxPyBeginBlockThreads();
+ if (wxPyCBH_findCallback(m_myInst, "OnInitGui"))
+ rval = wxPyCBH_callCallback(m_myInst, Py_BuildValue("()"));
+ wxPyEndBlockThreads(blocked);
+ return rval;
+}
+
+
+int wxPyApp::OnExit() {
+ int rval=0;
+ wxPyBlock_t blocked = wxPyBeginBlockThreads();
+ if (wxPyCBH_findCallback(m_myInst, "OnExit"))
+ rval = wxPyCBH_callCallback(m_myInst, Py_BuildValue("()"));
+ wxPyEndBlockThreads(blocked);
+ wxApp::OnExit(); // in this case always call the base class version
+ return rval;
+}
+
+
+
+void wxPyApp::ExitMainLoop() {
+ bool found;
+ wxPyBlock_t blocked = wxPyBeginBlockThreads();
+ if ((found = wxPyCBH_findCallback(m_myInst, "ExitMainLoop")))
+ wxPyCBH_callCallback(m_myInst, Py_BuildValue("()"));
+ wxPyEndBlockThreads(blocked);
+ if (! found)
+ wxApp::ExitMainLoop();
+}
+
+
+#ifdef __WXDEBUG__
+void wxPyApp::OnAssert(const wxChar *file,
+ int line,
+ const wxChar *cond,
+ const wxChar *msg) {
+
+ // if we're not fully initialized then just log the error
+ if (! m_startupComplete) {
+ wxString buf;
+ buf.Alloc(4096);
+ buf.Printf(wxT("%s(%d): assert \"%s\" failed"),
+ file, line, cond);
+ if (msg != NULL) {
+ buf += wxT(": ");
+ buf += msg;
+ }
+ wxLogDebug(buf);
+ return;
+ }
+
+ // If the OnAssert is overloaded in the Python class then call it...
+ bool found;
+ wxPyBlock_t blocked = wxPyBeginBlockThreads();
+ if ((found = wxPyCBH_findCallback(m_myInst, "OnAssert"))) {
+ PyObject* fso = wx2PyString(file);
+ PyObject* cso = wx2PyString(file);
+ PyObject* mso;
+ if (msg != NULL)
+ mso = wx2PyString(file);
+ else {
+ mso = Py_None; Py_INCREF(Py_None);
+ }
+ wxPyCBH_callCallback(m_myInst, Py_BuildValue("(OiOO)", fso, line, cso, mso));
+ Py_DECREF(fso);
+ Py_DECREF(cso);
+ Py_DECREF(mso);
+ }
+ wxPyEndBlockThreads(blocked);
+
+ // ...otherwise do our own thing with it
+ if (! found) {
+ // ignore it?
+ if (m_assertMode & wxPYAPP_ASSERT_SUPPRESS)
+ return;
+
+ // turn it into a Python exception?
+ if (m_assertMode & wxPYAPP_ASSERT_EXCEPTION) {
+ wxString buf;
+ buf.Alloc(4096);
+ buf.Printf(wxT("C++ assertion \"%s\" failed in %s(%d)"), cond, file, line);
+ if (msg != NULL) {
+ buf += wxT(": ");
+ buf += msg;
+ }
+
+ // set the exception
+ wxPyBlock_t blocked = wxPyBeginBlockThreads();
+ PyObject* s = wx2PyString(buf);
+ PyErr_SetObject(wxPyAssertionError, s);
+ Py_DECREF(s);
+ wxPyEndBlockThreads(blocked);
+
+ // Now when control returns to whatever API wrapper was called from
+ // Python it should detect that an exception is set and will return
+ // NULL, signalling the exception to Python.
+ }
+
+ // Send it to the normal log destination, but only if
+ // not _DIALOG because it will call this too
+ if ( (m_assertMode & wxPYAPP_ASSERT_LOG) && !(m_assertMode & wxPYAPP_ASSERT_DIALOG)) {
+ wxString buf;
+ buf.Alloc(4096);
+ buf.Printf(wxT("%s(%d): assert \"%s\" failed"),
+ file, line, cond);
+ if (msg != NULL) {
+ buf += wxT(": ");
+ buf += msg;
+ }
+ wxLogDebug(buf);
+ }
+
+ // do the normal wx assert dialog?
+ if (m_assertMode & wxPYAPP_ASSERT_DIALOG)
+ wxApp::OnAssert(file, line, cond, msg);
+ }
+}
+#endif
+
+ // For catching Apple Events
+void wxPyApp::MacOpenFile(const wxString &fileName)
+{
+ wxPyBlock_t blocked = wxPyBeginBlockThreads();
+ if (wxPyCBH_findCallback(m_myInst, "MacOpenFile")) {
+ PyObject* s = wx2PyString(fileName);
+ wxPyCBH_callCallback(m_myInst, Py_BuildValue("(O)", s));
+ Py_DECREF(s);
+ }
+ wxPyEndBlockThreads(blocked);
+}
+
+void wxPyApp::MacPrintFile(const wxString &fileName)
+{
+ wxPyBlock_t blocked = wxPyBeginBlockThreads();
+ if (wxPyCBH_findCallback(m_myInst, "MacPrintFile")) {
+ PyObject* s = wx2PyString(fileName);
+ wxPyCBH_callCallback(m_myInst, Py_BuildValue("(O)", s));
+ Py_DECREF(s);
+ }
+ wxPyEndBlockThreads(blocked);
+}
+
+void wxPyApp::MacNewFile()
+{
+ wxPyBlock_t blocked = wxPyBeginBlockThreads();
+ if (wxPyCBH_findCallback(m_myInst, "MacNewFile"))
+ wxPyCBH_callCallback(m_myInst, Py_BuildValue("()"));
+ wxPyEndBlockThreads(blocked);
+}
+
+void wxPyApp::MacReopenApp()
+{
+ wxPyBlock_t blocked = wxPyBeginBlockThreads();
+ if (wxPyCBH_findCallback(m_myInst, "MacReopenApp"))
+ wxPyCBH_callCallback(m_myInst, Py_BuildValue("()"));
+ wxPyEndBlockThreads(blocked);
+}
+
+
+/*static*/
+bool wxPyApp::GetMacSupportPCMenuShortcuts() {
+#ifdef __WXMAC__
+ return s_macSupportPCMenuShortcuts;
+#else
+ return 0;
+#endif
+}
+
+/*static*/
+long wxPyApp::GetMacAboutMenuItemId() {
+#ifdef __WXMAC__
+ return s_macAboutMenuItemId;
+#else
+ return 0;
+#endif
+}
+
+/*static*/
+long wxPyApp::GetMacPreferencesMenuItemId() {
+#ifdef __WXMAC__
+ return s_macPreferencesMenuItemId;
+#else
+ return 0;
+#endif
+}
+
+/*static*/
+long wxPyApp::GetMacExitMenuItemId() {
+#ifdef __WXMAC__
+ return s_macExitMenuItemId;
+#else
+ return 0;
+#endif
+}
+
+/*static*/
+wxString wxPyApp::GetMacHelpMenuTitleName() {
+#ifdef __WXMAC__
+ return s_macHelpMenuTitleName;
+#else
+ return wxEmptyString;
+#endif
+}
+
+/*static*/
+void wxPyApp::SetMacSupportPCMenuShortcuts(bool val) {
+#ifdef __WXMAC__
+ s_macSupportPCMenuShortcuts = val;
+#endif
+}
+
+/*static*/
+void wxPyApp::SetMacAboutMenuItemId(long val) {
+#ifdef __WXMAC__
+ s_macAboutMenuItemId = val;
+#endif
+}
+
+/*static*/
+void wxPyApp::SetMacPreferencesMenuItemId(long val) {
+#ifdef __WXMAC__
+ s_macPreferencesMenuItemId = val;
+#endif
+}
+
+/*static*/
+void wxPyApp::SetMacExitMenuItemId(long val) {
+#ifdef __WXMAC__
+ s_macExitMenuItemId = val;
+#endif
+}
+
+/*static*/
+void wxPyApp::SetMacHelpMenuTitleName(const wxString& val) {
+#ifdef __WXMAC__
+ s_macHelpMenuTitleName = val;
+#endif
+}
+
+
+// This finishes the initialization of wxWindows and then calls the OnInit
+// that should be present in the derived (Python) class.
+void wxPyApp::_BootstrapApp()
+{
+ static bool haveInitialized = false;
+ bool result;
+ wxPyBlock_t blocked;
+ PyObject* retval = NULL;
+ PyObject* pyint = NULL;
+
+
+ // Only initialize wxWidgets once
+ if (! haveInitialized) {
+
+ // Get any command-line args passed to this program from the sys module
+ int argc = 0;
+ char** argv = NULL;
+ blocked = wxPyBeginBlockThreads();
+
+ PyObject* sysargv = PySys_GetObject("argv");
+ PyObject* executable = PySys_GetObject("executable");
+
+ if (sysargv != NULL && executable != NULL) {
+ argc = PyList_Size(sysargv) + 1;
+ argv = new char*[argc+1];
+ argv[0] = strdup(PyString_AsString(executable));
+ int x;
+ for(x=1; x<argc; x++) {
+ PyObject *pyArg = PyList_GetItem(sysargv, x-1);
+ argv[x] = strdup(PyString_AsString(pyArg));
+ }
+ argv[argc] = NULL;
+ }
+ wxPyEndBlockThreads(blocked);
+
+ // Initialize wxWidgets
+ result = wxEntryStart(argc, argv);
+ // wxApp takes ownership of the argv array, don't delete it here
+
+ blocked = wxPyBeginBlockThreads();
+ if (! result) {
+ PyErr_SetString(PyExc_SystemError,
+ "wxEntryStart failed, unable to initialize wxWidgets!"
+#ifdef __WXGTK__
+ " (Is DISPLAY set properly?)"
+#endif
+ );
+ goto error;
+ }
+
+ // On wxGTK the locale will be changed to match the system settings,
+ // but Python before 2.4 needs to have LC_NUMERIC set to "C" in order
+ // for the floating point conversions and such to work right.
+#if defined(__WXGTK__) && PY_VERSION_HEX < 0x02040000
+ setlocale(LC_NUMERIC, "C");
+#endif
+
+// wxSystemOptions::SetOption(wxT("mac.textcontrol-use-mlte"), 1);
+
+ // The stock objects were all NULL when they were loaded into
+ // SWIG generated proxies, so re-init those now...
+ wxPy_ReinitStockObjects(3);
+
+ wxPyEndBlockThreads(blocked);
+ haveInitialized = true;
+ }
+ else {
+ this->argc = 0;
+ this->argv = NULL;
+ }
+
+
+ // It's now ok to generate exceptions for assertion errors.
+ wxPythonApp->SetStartupComplete(true);
+
+ // Call the Python wxApp's OnInit function
+ blocked = wxPyBeginBlockThreads();
+ if (wxPyCBH_findCallback(m_myInst, "OnInit")) {
+
+ PyObject* method = m_myInst.GetLastFound();
+ PyObject* argTuple = PyTuple_New(0);
+ retval = PyEval_CallObject(method, argTuple);
+ m_myInst.clearRecursionGuard(method);
+ Py_DECREF(argTuple);
+ Py_DECREF(method);
+ if (retval == NULL)
+ goto error;
+
+ pyint = PyNumber_Int(retval);
+ if (! pyint) {
+ PyErr_SetString(PyExc_TypeError, "OnInit should return a boolean value");
+ goto error;
+ }
+ result = PyInt_AS_LONG(pyint);
+ }
+ else {
+ // Is it okay if there is no OnInit? Probably so...
+ result = true;
+ }
+
+ if (! result) {
+ PyErr_SetString(PyExc_SystemExit, "OnInit returned false, exiting...");
+ }
+
+ error:
+ Py_XDECREF(retval);
+ Py_XDECREF(pyint);
+
+ wxPyEndBlockThreads(blocked);
+};
+
+//---------------------------------------------------------------------
+//----------------------------------------------------------------------
+
+
+#if 0
+static char* wxPyCopyCString(const wxChar* src)
+{
+ wxWX2MBbuf buff = (wxWX2MBbuf)wxConvCurrent->cWX2MB(src);
+ size_t len = strlen(buff);
+ char* dest = new char[len+1];
+ strcpy(dest, buff);
+ return dest;
+}
+
+#if wxUSE_UNICODE
+static char* wxPyCopyCString(const char* src) // we need a char version too
+{
+ size_t len = strlen(src);
+ char* dest = new char[len+1];
+ strcpy(dest, src);
+ return dest;
+}
+#endif
+
+static wxChar* wxPyCopyWString(const char *src)
+{
+ //wxMB2WXbuf buff = wxConvCurrent->cMB2WX(src);
+ wxString str(src, *wxConvCurrent);
+ return copystring(str);
+}
+
+#if wxUSE_UNICODE
+static wxChar* wxPyCopyWString(const wxChar *src)
+{
+ return copystring(src);
+}
+#endif
+#endif
+
+
+inline const char* dropwx(const char* name) {
+ if (name[0] == 'w' && name[1] == 'x')
+ return name+2;
+ else
+ return name;
+}
+
+//----------------------------------------------------------------------
+
+// This function is called when the wx._core_ module is imported to do some
+// initial setup. (Before there is a wxApp object.) The rest happens in
+// wxPyApp::_BootstrapApp
+void __wxPyPreStart(PyObject* moduleDict)
+{
+
+#ifdef __WXMSW__
+// wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF
+// | _CRTDBG_CHECK_ALWAYS_DF
+// | _CRTDBG_DELAY_FREE_MEM_DF
+// );
+#endif
+
+#ifdef WXP_WITH_THREAD
+#if wxPyUSE_GIL_STATE
+ PyEval_InitThreads();
+#else
+ PyEval_InitThreads();
+ wxPyTStates = new wxPyThreadStateArray;
+ wxPyTMutex = new wxMutex;
+
+ // Save the current (main) thread state in our array
+ PyThreadState* tstate = wxPyBeginAllowThreads();
+ wxPyEndAllowThreads(tstate);
+#endif
+#endif
+
+ // Ensure that the build options in the DLL (or whatever) match this build
+ wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "wxPython");
+
+ // Init the stock objects to a non-NULL value so SWIG doesn't create them as None
+ wxPy_ReinitStockObjects(1);
+
+ wxInitAllImageHandlers();
+}
+
+
+
+void __wxPyCleanup() {
+ wxPyDoingCleanup = true;
+ if (wxPyDoCleanup) {
+ wxPyDoCleanup = false;
+ wxEntryCleanup();
+ }
+#ifdef WXP_WITH_THREAD
+#if !wxPyUSE_GIL_STATE
+ delete wxPyTMutex;
+ wxPyTMutex = NULL;
+ wxPyTStates->Empty();
+ delete wxPyTStates;
+ wxPyTStates = NULL;
+#endif
+#endif
+}
+
+
+// Save a reference to the dictionary of the wx._core module, and inject
+// a few more things into it.
+PyObject* __wxPySetDictionary(PyObject* /* self */, PyObject* args)
+{
+
+ if (!PyArg_ParseTuple(args, "O", &wxPython_dict))
+ return NULL;
+
+ if (!PyDict_Check(wxPython_dict)) {
+ PyErr_SetString(PyExc_TypeError,
+ "_wxPySetDictionary must have dictionary object!");
+ return NULL;
+ }
+
+ if (! wxPyPtrTypeMap)
+ wxPyPtrTypeMap = PyDict_New();
+ PyDict_SetItemString(wxPython_dict, "__wxPyPtrTypeMap", wxPyPtrTypeMap);
+
+ // Create an exception object to use for wxASSERTions
+ wxPyAssertionError = PyErr_NewException("wx._core.PyAssertionError",
+ PyExc_AssertionError, NULL);
+ PyDict_SetItemString(wxPython_dict, "PyAssertionError", wxPyAssertionError);
+
+ // Create an exception object to use when the app object hasn't been created yet
+ wxPyNoAppError = PyErr_NewException("wx._core.PyNoAppError",
+ PyExc_RuntimeError, NULL);
+ PyDict_SetItemString(wxPython_dict, "PyNoAppError", wxPyNoAppError);
+
+
+
+#ifdef __WXMOTIF__
+#define wxPlatform "__WXMOTIF__"
+#define wxPlatName "wxMotif"
+#endif
+#ifdef __WXX11__
+#define wxPlatform "__WXX11__"
+#define wxPlatName "wxX11"
+#endif
+#ifdef __WXGTK__
+#define wxPlatform "__WXGTK__"
+#define wxPlatName "wxGTK"
+#endif
+#ifdef __WXMSW__
+#define wxPlatform "__WXMSW__"
+#define wxPlatName "wxMSW"
+#endif
+#ifdef __WXMAC__
+#define wxPlatform "__WXMAC__"
+#define wxPlatName "wxMac"
+#endif
+
+#ifdef __WXDEBUG__
+ int wxdebug = 1;
+#else
+ int wxdebug = 0;
+#endif
+
+ // These should be deprecated in favor of the PlatformInfo tuple built below...
+ PyDict_SetItemString(wxPython_dict, "Platform", PyString_FromString(wxPlatform));
+ PyDict_SetItemString(wxPython_dict, "USE_UNICODE", PyInt_FromLong(wxUSE_UNICODE));
+ PyDict_SetItemString(wxPython_dict, "__WXDEBUG__", PyInt_FromLong(wxdebug));
+
+ // Make a tuple of strings that gives more info about the platform.
+ PyObject* PlatInfo = PyList_New(0);
+ PyObject* obj;
+
+#define _AddInfoString(st) \
+ obj = PyString_FromString(st); \
+ PyList_Append(PlatInfo, obj); \
+ Py_DECREF(obj)
+
+ _AddInfoString(wxPlatform);
+ _AddInfoString(wxPlatName);
+#if wxUSE_UNICODE
+ _AddInfoString("unicode");
+#else
+ _AddInfoString("ansi");
+#endif
+#ifdef __WXGTK__
+#ifdef __WXGTK20__
+ _AddInfoString("gtk2");
+#else
+ _AddInfoString("gtk1");
+#endif
+#endif
+#ifdef __WXDEBUG__
+ _AddInfoString("wx-assertions-on");
+#else
+ _AddInfoString("wx-assertions-off");
+#endif
+ _AddInfoString(wxPy_SWIG_VERSION);
+
+#undef _AddInfoString
+
+ PyObject* PlatInfoTuple = PyList_AsTuple(PlatInfo);
+ Py_DECREF(PlatInfo);
+ PyDict_SetItemString(wxPython_dict, "PlatformInfo", PlatInfoTuple);
+
+ RETURN_NONE();
+}
+
+
+
+//---------------------------------------------------------------------------
+
+// The stock objects are no longer created when the wx._core_ module is
+// imported, but only after the app object has been created. The
+// wxPy_ReinitStockObjects function will be called 3 times to pass the stock
+// objects though various stages of evolution:
+//
+// pass 1: Set all the pointers to a non-NULL value so the Python proxy
+// object will be created (otherwise SWIG will just use None.)
+//
+// pass 2: After the module has been imported and the python proxys have
+// been created, then set the __class__ to be _wxPyUnbornObject so
+// it will catch any access to the object and will raise an exception.
+//
+// pass 3: Finally, from BootstrapApp patch things up so the stock objects
+// can be used.
+
+
+PyObject* __wxPyFixStockObjects(PyObject* /* self */, PyObject* args)
+{
+ wxPy_ReinitStockObjects(2);
+ RETURN_NONE();
+}
+
+
+static void rsoPass2(const char* name)
+{
+ static PyObject* unbornObjectClass = NULL;
+ PyObject* obj;
+
+ if (unbornObjectClass == NULL) {
+ unbornObjectClass = PyDict_GetItemString(wxPython_dict, "_wxPyUnbornObject");
+ Py_INCREF(unbornObjectClass);
+ }
+
+ // Find the object instance
+ obj = PyDict_GetItemString(wxPython_dict, (char*)dropwx(name));
+ wxCHECK_RET(obj != NULL, wxT("Unable to find stock object"));
+ wxCHECK_RET(wxPySwigInstance_Check(obj), wxT("Not a swig instance"));
+
+ // Change its class
+ PyObject_SetAttrString(obj, "__class__", unbornObjectClass);
+
+}
+
+static void rsoPass3(const char* name, const char* classname, void* ptr)
+{
+ PyObject* obj;
+ PyObject* classobj;
+ PyObject* ptrobj;
+
+ // Find the object instance
+ obj = PyDict_GetItemString(wxPython_dict, (char*)dropwx(name));
+ wxCHECK_RET(obj != NULL, wxT("Unable to find stock object"));
+ wxCHECK_RET(wxPySwigInstance_Check(obj), wxT("Not a swig instance"));
+
+ // Find the class object and put it back in the instance
+ classobj = PyDict_GetItemString(wxPython_dict, (char*)dropwx(classname));
+ wxCHECK_RET(classobj != NULL, wxT("Unable to find stock class object"));
+ PyObject_SetAttrString(obj, "__class__", classobj);
+
+ // Rebuild the .this swigified pointer with the new value of the C++ pointer
+ ptrobj = wxPyMakeSwigPtr(ptr, wxString(classname, *wxConvCurrent));
+ PyObject_SetAttrString(obj, "this", ptrobj);
+ Py_DECREF(ptrobj);
+}
+
+
+
+void wxPy_ReinitStockObjects(int pass)
+{
+
+ // If there is already an App object then wxPython is probably embedded in
+ // a wx C++ application, so there is no need to do all this.
+ static bool embedded = false;
+ if ((pass == 1 || pass == 2) && wxTheApp) {
+ embedded = true;
+ return;
+ }
+ if (pass == 3 && embedded)
+ return;
+
+
+#define REINITOBJ(name, classname) \
+ if (pass == 1) { name = (classname*)0xC0C0C0C0; } \
+ else if (pass == 2) { rsoPass2(#name); } \
+ else if (pass == 3) { rsoPass3(#name, #classname, (void*)name); }
+
+
+#define REINITOBJ2(name, classname) \
+ if (pass == 1) { } \
+ else if (pass == 2) { rsoPass2(#name); } \
+ else if (pass == 3) { rsoPass3(#name, #classname, (void*)&name); }
+
+
+ REINITOBJ(wxNORMAL_FONT, wxFont);
+ REINITOBJ(wxSMALL_FONT, wxFont);
+ REINITOBJ(wxITALIC_FONT, wxFont);
+ REINITOBJ(wxSWISS_FONT, wxFont);
+
+ REINITOBJ(wxRED_PEN, wxPen);
+ REINITOBJ(wxCYAN_PEN, wxPen);
+ REINITOBJ(wxGREEN_PEN, wxPen);
+ REINITOBJ(wxBLACK_PEN, wxPen);
+ REINITOBJ(wxWHITE_PEN, wxPen);
+ REINITOBJ(wxTRANSPARENT_PEN, wxPen);
+ REINITOBJ(wxBLACK_DASHED_PEN, wxPen);
+ REINITOBJ(wxGREY_PEN, wxPen);
+ REINITOBJ(wxMEDIUM_GREY_PEN, wxPen);
+ REINITOBJ(wxLIGHT_GREY_PEN, wxPen);
+
+ REINITOBJ(wxBLUE_BRUSH, wxBrush);
+ REINITOBJ(wxGREEN_BRUSH, wxBrush);
+ REINITOBJ(wxWHITE_BRUSH, wxBrush);
+ REINITOBJ(wxBLACK_BRUSH, wxBrush);
+ REINITOBJ(wxTRANSPARENT_BRUSH, wxBrush);
+ REINITOBJ(wxCYAN_BRUSH, wxBrush);
+ REINITOBJ(wxRED_BRUSH, wxBrush);
+ REINITOBJ(wxGREY_BRUSH, wxBrush);
+ REINITOBJ(wxMEDIUM_GREY_BRUSH, wxBrush);
+ REINITOBJ(wxLIGHT_GREY_BRUSH, wxBrush);
+
+ REINITOBJ(wxBLACK, wxColour);
+ REINITOBJ(wxWHITE, wxColour);
+ REINITOBJ(wxRED, wxColour);
+ REINITOBJ(wxBLUE, wxColour);
+ REINITOBJ(wxGREEN, wxColour);
+ REINITOBJ(wxCYAN, wxColour);
+ REINITOBJ(wxLIGHT_GREY, wxColour);
+
+ REINITOBJ(wxSTANDARD_CURSOR, wxCursor);
+ REINITOBJ(wxHOURGLASS_CURSOR, wxCursor);
+ REINITOBJ(wxCROSS_CURSOR, wxCursor);
+
+ REINITOBJ2(wxNullBitmap, wxBitmap);
+ REINITOBJ2(wxNullIcon, wxIcon);
+ REINITOBJ2(wxNullCursor, wxCursor);
+ REINITOBJ2(wxNullPen, wxPen);
+ REINITOBJ2(wxNullBrush, wxBrush);
+ REINITOBJ2(wxNullPalette, wxPalette);
+ REINITOBJ2(wxNullFont, wxFont);
+ REINITOBJ2(wxNullColour, wxColour);
+
+ REINITOBJ(wxTheFontList, wxFontList);
+ REINITOBJ(wxThePenList, wxPenList);
+ REINITOBJ(wxTheBrushList, wxBrushList);
+ REINITOBJ(wxTheColourDatabase, wxColourDatabase);
+
+
+ REINITOBJ2(wxDefaultValidator, wxValidator);
+ REINITOBJ2(wxNullImage, wxImage);
+ REINITOBJ2(wxNullAcceleratorTable, wxAcceleratorTable);
+
+#undef REINITOBJ
+#undef REINITOBJ2
+}
+
+//---------------------------------------------------------------------------
+
+// Check for existence of a wxApp, setting an exception if there isn't one.
+// This doesn't need to aquire the GIL because it should only be called from
+// an %exception before the lock is released.
+
+bool wxPyCheckForApp() {
+ if (wxTheApp != NULL)
+ return true;
+ else {
+ PyErr_SetString(wxPyNoAppError, "The wx.App object must be created first!");
+ return false;
+ }
+}
+
+//---------------------------------------------------------------------------
+
+void wxPyUserData_dtor(wxPyUserData* self) {
+ if (! wxPyDoingCleanup) {
+ wxPyBlock_t blocked = wxPyBeginBlockThreads();
+ Py_DECREF(self->m_obj);
+ self->m_obj = NULL;
+ wxPyEndBlockThreads(blocked);
+ }
+}
+
+
+void wxPyClientData_dtor(wxPyClientData* self) {
+ if (! wxPyDoingCleanup) { // Don't do it during cleanup as Python
+ // may have already garbage collected the object...
+ if (self->m_incRef) {
+ wxPyBlock_t blocked = wxPyBeginBlockThreads();
+ Py_DECREF(self->m_obj);
+ wxPyEndBlockThreads(blocked);
+ }
+ self->m_obj = NULL;
+ }
+}
+
+
+
+// This is called when an OOR controled object is being destroyed. Although
+// the C++ object is going away there is no way to force the Python object
+// (and all references to it) to die too. This causes problems (crashes) in
+// wxPython when a python shadow object attempts to call a C++ method using
+// the now bogus pointer... So to try and prevent this we'll do a little black
+// magic and change the class of the python instance to a class that will
+// raise an exception for any attempt to call methods with it. See
+// _wxPyDeadObject in _core_ex.py for the implementation of this class.
+void wxPyOORClientData_dtor(wxPyOORClientData* self) {
+
+ static PyObject* deadObjectClass = NULL;
+
+ wxPyBlock_t blocked = wxPyBeginBlockThreads();
+ if (deadObjectClass == NULL) {
+ deadObjectClass = PyDict_GetItemString(wxPython_dict, "_wxPyDeadObject");
+ // TODO: Can not wxASSERT here because inside a wxPyBeginBlock Threads,
+ // will lead to a deadlock when it tries to aquire the GIL again.
+ //wxASSERT_MSG(deadObjectClass != NULL, wxT("Can't get _wxPyDeadObject class!"));
+ Py_INCREF(deadObjectClass);
+ }
+
+
+ // Only if there is more than one reference to the object and we are
+ // holding the OOR reference:
+ if ( !wxPyDoingCleanup && self->m_obj->ob_refcnt > 1 && self->m_incRef) {
+ // bool isInstance = wxPyInstance_Check(self->m_obj);
+ // TODO same here
+ //wxASSERT_MSG(isInstance, wxT("m_obj not an instance!?!?!"));
+
+ // Call __del__, if there is one.
+ PyObject* func = PyObject_GetAttrString(self->m_obj, "__del__");
+ if (func) {
+ PyObject* rv = PyObject_CallMethod(self->m_obj, "__del__", NULL);
+ Py_XDECREF(rv);
+ Py_DECREF(func);
+ }
+ if (PyErr_Occurred())
+ PyErr_Clear(); // just ignore it for now
+
+
+ PyObject* dict = PyObject_GetAttrString(self->m_obj, "__dict__");
+ if (dict) {
+ // Clear the instance's dictionary
+ PyDict_Clear(dict);
+
+ // put the name of the old class into the instance, and then reset the
+ // class to be the dead class.
+ PyObject* klass = PyObject_GetAttrString(self->m_obj, "__class__");
+ PyObject* name = PyObject_GetAttrString(klass, "__name__");
+ PyDict_SetItemString(dict, "_name", name);
+ PyObject_SetAttrString(self->m_obj, "__class__", deadObjectClass);
+ //Py_INCREF(deadObjectClass);
+ Py_DECREF(klass);
+ Py_DECREF(name);
+ }
+ }
+
+ // m_obj is DECREF'd in the base class dtor...
+ wxPyEndBlockThreads(blocked);
+}
+