+//---------------------------------------------------------------------------
+// Stuff used by OOR to find the right wxPython class type to return and to
+// build it.
+
+
+// The pointer type map is used when the "pointer" type name generated by SWIG
+// is not the same as the shadow class name, for example wxPyTreeCtrl
+// vs. wxTreeCtrl. It needs to be referenced in Python as well as from C++,
+// so we'll just make it a Python dictionary in the wx module's namespace.
+void wxPyPtrTypeMap_Add(const char* commonName, const char* ptrName) {
+ if (! wxPyPtrTypeMap)
+ wxPyPtrTypeMap = PyDict_New();
+
+ PyDict_SetItemString(wxPyPtrTypeMap,
+ (char*)commonName,
+ PyString_FromString((char*)ptrName));
+}
+
+
+
+PyObject* wxPyClassExists(const char* className) {
+
+ if (!className)
+ return NULL;
+
+ char buff[64]; // should always be big enough...
+
+ sprintf(buff, "%sPtr", className);
+ PyObject* classobj = PyDict_GetItemString(wxPython_dict, buff);
+
+ return classobj; // returns NULL if not found
+}
+
+
+PyObject* wxPyMake_wxObject(wxObject* source) {
+ PyObject* target = NULL;
+ bool isEvtHandler = FALSE;
+
+ if (source) {
+ // If it's derived from wxEvtHandler then there may
+ // already be a pointer to a Python objec that we can use.
+ if (wxIsKindOf(source, wxEvtHandler)) {
+ wxEvtHandler* eh = (wxEvtHandler*)source;
+ wxPyClientData* data = (wxPyClientData*)eh->GetClientObject();
+ if (data) {
+ target = data->m_obj;
+ Py_INCREF(target);
+ }
+ }
+ else if (wxIsKindOf(source, wxSizer)) {
+ // wxSizers also track the original object
+ wxSizer* sz = (wxSizer*)source;
+ wxPyClientData* data = (wxPyClientData*)sz->GetClientObject();
+ if (data) {
+ target = data->m_obj;
+ Py_INCREF(target);
+ }
+ }
+
+ if (! target) {
+ // Otherwise make it the old fashioned way by making a
+ // new shadow object and putting this pointer in it.
+ wxClassInfo* info = source->GetClassInfo();
+ wxChar* name = (wxChar*)info->GetClassName();
+ PyObject* klass = wxPyClassExists(name);
+ while (info && !klass) {
+ name = (wxChar*)info->GetBaseClassName1();
+ info = wxClassInfo::FindClass(name);
+ klass = wxPyClassExists(name);
+ }
+ if (info) {
+ target = wxPyConstructObject(source, name, klass, FALSE);
+ if (target && isEvtHandler)
+ ((wxEvtHandler*)source)->SetClientObject(new wxPyClientData(target));
+ } else {
+ wxString msg("wxPython class not found for ");
+ msg += source->GetClassInfo()->GetClassName();
+ PyErr_SetString(PyExc_NameError, msg.c_str());
+ target = NULL;
+ }
+ }
+ } else { // source was NULL so return None.
+ Py_INCREF(Py_None); target = Py_None;
+ }
+ return target;
+}
+
+