From cd834e7b5a80dc0b0b9782de8606bdb401824e95 Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Fri, 9 Aug 2002 19:29:52 +0000 Subject: [PATCH] Allow loading xrc resources from a string git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@16433 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- wxPython/contrib/xrc/xrc.cpp | 119 +++++++++++++++++++++++++--------- wxPython/contrib/xrc/xrc.i | 45 ++++++++++--- wxPython/contrib/xrc/xrc.py | 11 +++- wxPython/demo/XML_Resource.py | 21 +++++- 4 files changed, 152 insertions(+), 44 deletions(-) diff --git a/wxPython/contrib/xrc/xrc.cpp b/wxPython/contrib/xrc/xrc.cpp index 8d89d01abe..e408c71f00 100644 --- a/wxPython/contrib/xrc/xrc.cpp +++ b/wxPython/contrib/xrc/xrc.cpp @@ -57,6 +57,8 @@ extern PyObject *SWIG_newvarlink(void); #include "wxPython.h" #include "wx/xrc/xmlres.h" +#include +#include static PyObject* t_output_helper(PyObject* target, PyObject* o) { @@ -95,20 +97,27 @@ static void *SwigwxXmlResourceTowxObject(void *ptr) { return (void *) dest; } -#define new_wxXmlResourceEmpty(_swigarg0) (new wxXmlResource(_swigarg0)) -static PyObject *_wrap_new_wxXmlResourceEmpty(PyObject *self, PyObject *args, PyObject *kwargs) { +#define new_wxXmlResource(_swigarg0,_swigarg1) (new wxXmlResource(_swigarg0,_swigarg1)) +static PyObject *_wrap_new_wxXmlResource(PyObject *self, PyObject *args, PyObject *kwargs) { PyObject * _resultobj; wxXmlResource * _result; - int _arg0 = (int ) (wxXRC_USE_LOCALE); - char *_kwnames[] = { "flags", NULL }; + wxString * _arg0; + int _arg1 = (int ) (wxXRC_USE_LOCALE); + PyObject * _obj0 = 0; + char *_kwnames[] = { "filemask","flags", NULL }; char _ptemp[128]; self = self; - if(!PyArg_ParseTupleAndKeywords(args,kwargs,"|i:new_wxXmlResourceEmpty",_kwnames,&_arg0)) + if(!PyArg_ParseTupleAndKeywords(args,kwargs,"O|i:new_wxXmlResource",_kwnames,&_obj0,&_arg1)) + return NULL; +{ + _arg0 = wxString_in_helper(_obj0); + if (_arg0 == NULL) return NULL; +} { PyThreadState* __tstate = wxPyBeginAllowThreads(); - _result = (wxXmlResource *)new_wxXmlResourceEmpty(_arg0); + _result = (wxXmlResource *)new_wxXmlResource(*_arg0,_arg1); wxPyEndAllowThreads(__tstate); if (PyErr_Occurred()) return NULL; @@ -119,35 +128,27 @@ static PyObject *_wrap_new_wxXmlResourceEmpty(PyObject *self, PyObject *args, Py Py_INCREF(Py_None); _resultobj = Py_None; } +{ + if (_obj0) + delete _arg0; +} return _resultobj; } -static wxXmlResource *new_wxXmlResource(const wxString *filemask,int flags) { - wxXmlResource* res = new wxXmlResource(*filemask, flags); - res->InitAllHandlers(); - return res; - } - -static PyObject *_wrap_new_wxXmlResource(PyObject *self, PyObject *args, PyObject *kwargs) { +#define new_wxEmptyXmlResource(_swigarg0) (new wxXmlResource(_swigarg0)) +static PyObject *_wrap_new_wxEmptyXmlResource(PyObject *self, PyObject *args, PyObject *kwargs) { PyObject * _resultobj; wxXmlResource * _result; - wxString * _arg0; - int _arg1 = (int ) (wxXRC_USE_LOCALE); - PyObject * _obj0 = 0; - char *_kwnames[] = { "filemask","flags", NULL }; + int _arg0 = (int ) (wxXRC_USE_LOCALE); + char *_kwnames[] = { "flags", NULL }; char _ptemp[128]; self = self; - if(!PyArg_ParseTupleAndKeywords(args,kwargs,"O|i:new_wxXmlResource",_kwnames,&_obj0,&_arg1)) + if(!PyArg_ParseTupleAndKeywords(args,kwargs,"|i:new_wxEmptyXmlResource",_kwnames,&_arg0)) return NULL; -{ - _arg0 = wxString_in_helper(_obj0); - if (_arg0 == NULL) - return NULL; -} { PyThreadState* __tstate = wxPyBeginAllowThreads(); - _result = (wxXmlResource *)new_wxXmlResource(_arg0,_arg1); + _result = (wxXmlResource *)new_wxEmptyXmlResource(_arg0); wxPyEndAllowThreads(__tstate); if (PyErr_Occurred()) return NULL; @@ -158,10 +159,6 @@ static PyObject *_wrap_new_wxXmlResource(PyObject *self, PyObject *args, PyObjec Py_INCREF(Py_None); _resultobj = Py_None; } -{ - if (_obj0) - delete _arg0; -} return _resultobj; } @@ -232,6 +229,69 @@ static PyObject *_wrap_wxXmlResource_Load(PyObject *self, PyObject *args, PyObje return _resultobj; } +static bool wxXmlResource_LoadFromString(wxXmlResource *self,const wxString & data) { + static int s_memFileIdx = 0; + + // Check for memory FS. If not present, load the handler: + wxMemoryFSHandler::AddFile(wxT("XRC_resource/dummy_file"), + wxT("dummy data")); + wxFileSystem fsys; + wxFSFile *f = fsys.OpenFile(wxT("memory:XRC_resource/dummy_file")); + wxMemoryFSHandler::RemoveFile(wxT("XRC_resource/dummy_file")); + if (f) + delete f; + else + wxFileSystem::AddHandler(new wxMemoryFSHandler); + + // Now put the resource data into the memory FS + wxString filename(wxT("XRC_resource/data_string_")); + filename << s_memFileIdx; + s_memFileIdx += 1; + wxMemoryFSHandler::AddFile(filename, data); + + // Load the "file" into the resource object + bool retval = self->Load(wxT("memory:") + filename ); + + return retval; + } +static PyObject *_wrap_wxXmlResource_LoadFromString(PyObject *self, PyObject *args, PyObject *kwargs) { + PyObject * _resultobj; + bool _result; + wxXmlResource * _arg0; + wxString * _arg1; + PyObject * _argo0 = 0; + PyObject * _obj1 = 0; + char *_kwnames[] = { "self","data", NULL }; + + self = self; + if(!PyArg_ParseTupleAndKeywords(args,kwargs,"OO:wxXmlResource_LoadFromString",_kwnames,&_argo0,&_obj1)) + return NULL; + if (_argo0) { + if (_argo0 == Py_None) { _arg0 = NULL; } + else if (SWIG_GetPtrObj(_argo0,(void **) &_arg0,"_wxXmlResource_p")) { + PyErr_SetString(PyExc_TypeError,"Type error in argument 1 of wxXmlResource_LoadFromString. Expected _wxXmlResource_p."); + return NULL; + } + } +{ + _arg1 = wxString_in_helper(_obj1); + if (_arg1 == NULL) + return NULL; +} +{ + PyThreadState* __tstate = wxPyBeginAllowThreads(); + _result = (bool )wxXmlResource_LoadFromString(_arg0,*_arg1); + + wxPyEndAllowThreads(__tstate); + if (PyErr_Occurred()) return NULL; +} _resultobj = Py_BuildValue("i",_result); +{ + if (_obj1) + delete _arg1; +} + return _resultobj; +} + #define wxXmlResource_InitAllHandlers(_swigobj) (_swigobj->InitAllHandlers()) static PyObject *_wrap_wxXmlResource_InitAllHandlers(PyObject *self, PyObject *args, PyObject *kwargs) { PyObject * _resultobj; @@ -1088,10 +1148,11 @@ static PyMethodDef xrccMethods[] = { { "wxXmlResource_ClearHandlers", (PyCFunction) _wrap_wxXmlResource_ClearHandlers, METH_VARARGS | METH_KEYWORDS }, { "wxXmlResource_AddHandler", (PyCFunction) _wrap_wxXmlResource_AddHandler, METH_VARARGS | METH_KEYWORDS }, { "wxXmlResource_InitAllHandlers", (PyCFunction) _wrap_wxXmlResource_InitAllHandlers, METH_VARARGS | METH_KEYWORDS }, + { "wxXmlResource_LoadFromString", (PyCFunction) _wrap_wxXmlResource_LoadFromString, METH_VARARGS | METH_KEYWORDS }, { "wxXmlResource_Load", (PyCFunction) _wrap_wxXmlResource_Load, METH_VARARGS | METH_KEYWORDS }, { "delete_wxXmlResource", (PyCFunction) _wrap_delete_wxXmlResource, METH_VARARGS | METH_KEYWORDS }, + { "new_wxEmptyXmlResource", (PyCFunction) _wrap_new_wxEmptyXmlResource, METH_VARARGS | METH_KEYWORDS }, { "new_wxXmlResource", (PyCFunction) _wrap_new_wxXmlResource, METH_VARARGS | METH_KEYWORDS }, - { "new_wxXmlResourceEmpty", (PyCFunction) _wrap_new_wxXmlResourceEmpty, METH_VARARGS | METH_KEYWORDS }, { NULL, NULL } }; #ifdef __cplusplus diff --git a/wxPython/contrib/xrc/xrc.i b/wxPython/contrib/xrc/xrc.i index 2f481c01d1..6783f33e8d 100644 --- a/wxPython/contrib/xrc/xrc.i +++ b/wxPython/contrib/xrc/xrc.i @@ -16,6 +16,8 @@ %{ #include "wxPython.h" #include "wx/xrc/xmlres.h" +#include +#include %} //--------------------------------------------------------------------------- @@ -46,21 +48,16 @@ enum wxXmlResourceFlags class wxXmlResource : public wxObject { public: - // Ctor. + // Ctors. // Flags: wxXRC_USE_LOCALE // translatable strings will be translated via _() // wxXRC_NO_SUBCLASSING // subclass property of object nodes will be ignored // (useful for previews in XRC editors) - %name(wxXmlResourceEmpty)wxXmlResource(int flags = wxXRC_USE_LOCALE); // TODO, a better %name - - %addmethods { - wxXmlResource(const wxString* filemask, int flags = wxXRC_USE_LOCALE) { - wxXmlResource* res = new wxXmlResource(*filemask, flags); - res->InitAllHandlers(); - return res; - } - } + wxXmlResource(const wxString& filemask, int flags = wxXRC_USE_LOCALE); + %name(wxEmptyXmlResource) wxXmlResource(int flags = wxXRC_USE_LOCALE); + %pragma(python) addtomethod = "__init__:self.InitAllHandlers()" + %pragma(python) addtomethod = "wxEmptyXmlResource:val.InitAllHandlers()" ~wxXmlResource(); @@ -69,6 +66,34 @@ public: // This method understands VFS (see filesys.h). bool Load(const wxString& filemask); + %addmethods { + bool LoadFromString(const wxString& data) { + static int s_memFileIdx = 0; + + // Check for memory FS. If not present, load the handler: + wxMemoryFSHandler::AddFile(wxT("XRC_resource/dummy_file"), + wxT("dummy data")); + wxFileSystem fsys; + wxFSFile *f = fsys.OpenFile(wxT("memory:XRC_resource/dummy_file")); + wxMemoryFSHandler::RemoveFile(wxT("XRC_resource/dummy_file")); + if (f) + delete f; + else + wxFileSystem::AddHandler(new wxMemoryFSHandler); + + // Now put the resource data into the memory FS + wxString filename(wxT("XRC_resource/data_string_")); + filename << s_memFileIdx; + s_memFileIdx += 1; + wxMemoryFSHandler::AddFile(filename, data); + + // Load the "file" into the resource object + bool retval = self->Load(wxT("memory:") + filename ); + + return retval; + } + } + // Initialize handlers for all supported controls/windows. This will // make the executable quite big because it forces linking against // most of wxWin library diff --git a/wxPython/contrib/xrc/xrc.py b/wxPython/contrib/xrc/xrc.py index 45f3673db6..2837e83749 100644 --- a/wxPython/contrib/xrc/xrc.py +++ b/wxPython/contrib/xrc/xrc.py @@ -62,6 +62,9 @@ class wxXmlResourcePtr(wxObjectPtr): def Load(self, *_args, **_kwargs): val = apply(xrcc.wxXmlResource_Load,(self,) + _args, _kwargs) return val + def LoadFromString(self, *_args, **_kwargs): + val = apply(xrcc.wxXmlResource_LoadFromString,(self,) + _args, _kwargs) + return val def InitAllHandlers(self, *_args, **_kwargs): val = apply(xrcc.wxXmlResource_InitAllHandlers,(self,) + _args, _kwargs) return val @@ -121,14 +124,16 @@ class wxXmlResourcePtr(wxObjectPtr): return "" % (self.this,) class wxXmlResource(wxXmlResourcePtr): def __init__(self,*_args,**_kwargs): - self.this = apply(xrcc.new_wxXmlResourceEmpty,_args,_kwargs) + self.this = apply(xrcc.new_wxXmlResource,_args,_kwargs) self.thisown = 1 + self.InitAllHandlers() -def wxXmlResource(*_args,**_kwargs): - val = wxXmlResourcePtr(apply(xrcc.new_wxXmlResource,_args,_kwargs)) +def wxEmptyXmlResource(*_args,**_kwargs): + val = wxXmlResourcePtr(apply(xrcc.new_wxEmptyXmlResource,_args,_kwargs)) val.thisown = 1 + val.InitAllHandlers() return val diff --git a/wxPython/demo/XML_Resource.py b/wxPython/demo/XML_Resource.py index ba5350f539..b300cd8c91 100644 --- a/wxPython/demo/XML_Resource.py +++ b/wxPython/demo/XML_Resource.py @@ -16,13 +16,22 @@ class TestPanel(wxPanel): label = wxStaticText(self, -1, "The lower panel was built from this XML:") label.SetFont(wxFont(12, wxSWISS, wxNORMAL, wxBOLD)) - text = wxTextCtrl(self, -1, open(RESFILE).read(), + resourceText = open(RESFILE).read() + text = wxTextCtrl(self, -1, resourceText, style=wxTE_READONLY|wxTE_MULTILINE) text.SetInsertionPoint(0) line = wxStaticLine(self, -1) - res = wxXmlResource(RESFILE) + if 0: + # XML Resources can be loaded from a file like this: + res = wxXmlResource(RESFILE) + else: + # or from a string, like this: + res = wxEmptyXmlResource() + res.LoadFromString(resourceText) + + # Now create a panel from the resource data panel = res.LoadPanel(self, "MyPanel") # and do the layout @@ -48,3 +57,11 @@ def runTest(frame, nb, log): overview = """ """ + + + +if __name__ == '__main__': + import sys,os + import run + run.main(['', os.path.basename(sys.argv[0])]) + -- 2.45.2