XRC and MemoryFSHandler fixes

This commit is contained in:
Robin Dunn
2016-08-10 15:57:47 -07:00
parent 9d5e743e50
commit 2f1f0ee1e9
7 changed files with 56 additions and 37 deletions

View File

@@ -18,8 +18,10 @@ class TestPanel(wx.Panel):
label = wx.StaticText(self, -1, "The lower panel was built from this XML:")
label.SetFont(wx.Font(12, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD))
resourceText = open(RESFILE).read()
text = wx.TextCtrl(self, -1, resourceText,
with open(RESFILE, 'rb') as f:
resourceBytes = f.read()
text = wx.TextCtrl(self, -1, resourceBytes,
style=wx.TE_READONLY|wx.TE_MULTILINE)
text.SetInsertionPoint(0)
@@ -33,13 +35,14 @@ class TestPanel(wx.Panel):
elif 1:
# or from a Virtual FileSystem:
wx.FileSystem.AddHandler(wx.MemoryFSHandler())
wx.MemoryFSHandler().AddFile("XRC_Resources/data_file", resourceText)
res = xrc.XmlResource("memory:XRC_Resources/data_file")
wx.MemoryFSHandler.AddFile("my_XRC_data", resourceBytes)
# notice the matching filename
res = xrc.XmlResource("memory:my_XRC_data")
else:
# or from a string, like this:
res = xrc.EmptyXmlResource()
res.LoadFromString(resourceText)
# or from a buffer compatible object, like this:
res = xrc.XmlResource()
res.LoadFromBuffer(resourceBytes)
# Now create a panel from the resource data

View File

@@ -5,7 +5,7 @@ import wx.xrc as xrc
#----------------------------------------------------------------------
resourceText = r'''<?xml version="1.0"?>
resourceText = br'''<?xml version="1.0"?>
<resource>
<!-- Notice that the class is NOT a standard wx class -->
@@ -48,8 +48,7 @@ class MyCustomPanel(wx.Panel):
# the different requirements are.
class PreMyCustomPanel(wx.Panel):
def __init__(self):
p = wx.PrePanel()
self.PostCreate(p)
wx.Panel.__init__(self)
def Create(self, parent, id, pos, size, style, name):
wx.Panel.Create(self, parent, id, pos, size, style, name)
@@ -84,12 +83,10 @@ class MyCustomPanelXmlHandler(xrc.XmlResourceHandler):
def DoCreateResource(self):
# NOTE: wxWindows can be created in either a single-phase or
# in a two-phase way. Single phase is what you normally do,
# and two-phase creates the instnace first, and then later
# and two-phase creates the instance first, and then later
# creates the actual window when the Create method is called.
# (In wxPython the first phase is done using the wxPre*
# function, for example, wxPreFrame, wxPrePanel, etc.)
#
# wxXmlResource supports either method, a premade instance can
# wxXmlResource supports either method, a pre-made instance can
# be created and populated by xrc using the appropriate
# LoadOn* method (such as LoadOnPanel) or xrc can create the
# instance too, using the Load* method. However this makes
@@ -98,7 +95,7 @@ class MyCustomPanelXmlHandler(xrc.XmlResourceHandler):
# instance, then you can make the handle much simpler. I'll
# show both methods below.
if 1:
if 0:
# The simple method assumes that there is no existing
# instance. Be sure of that with an assert.
assert self.GetInstance() is None
@@ -156,9 +153,9 @@ class TestPanel(wx.Panel):
line = wx.StaticLine(self, -1)
# Load the resource
res = xrc.EmptyXmlResource()
res = xrc.XmlResource()
res.InsertHandler(MyCustomPanelXmlHandler())
res.LoadFromString(resourceText)
res.LoadFromBuffer(resourceText)
# Now create a panel from the resource data
panel = res.LoadObject(self, "MyPanel", "MyCustomPanel")
@@ -171,7 +168,6 @@ class TestPanel(wx.Panel):
sizer.Add(panel, 1, wx.EXPAND|wx.ALL, 5)
self.SetSizer(sizer)
self.SetAutoLayout(True)
#----------------------------------------------------------------------

View File

@@ -5,7 +5,7 @@ import wx.xrc as xrc
#----------------------------------------------------------------------
resourceText = r'''<?xml version="1.0"?>
resourceText = br'''<?xml version="1.0"?>
<resource>
<!-- Notice that the class IS a standard wx class, and a custom
@@ -26,9 +26,8 @@ resourceText = r'''<?xml version="1.0"?>
class MyCustomPanel(wx.Panel):
def __init__(self):
p = wx.PrePanel()
wx.Panel.__init__(self)
# the Create step is done by XRC.
self.PostCreate(p)
self.Bind(wx.EVT_WINDOW_CREATE, self.OnCreate)
self.Bind(wx.EVT_SIZE, self.OnSize)
@@ -75,8 +74,8 @@ class TestPanel(wx.Panel):
line = wx.StaticLine(self, -1)
# Load the resource
res = xrc.EmptyXmlResource()
res.LoadFromString(resourceText)
res = xrc.XmlResource()
res.LoadFromBuffer(resourceText)
# Now create a panel from the resource data
panel = res.LoadPanel(self, "MyPanel")
@@ -89,7 +88,6 @@ class TestPanel(wx.Panel):
sizer.Add(panel, 1, wx.EXPAND|wx.ALL, 5)
self.SetSizer(sizer)
self.SetAutoLayout(True)
#----------------------------------------------------------------------

View File

@@ -98,8 +98,8 @@ def run():
c.addPublic()
c.addCppMethod('bool', 'LoadFromString', '(wxPyBuffer* data)',
doc="Load the resource from a string or other data buffer compatible object.",
c.addCppMethod('bool', 'LoadFromBuffer', '(wxPyBuffer* data)',
doc="Load the resource from a bytes string or other data buffer compatible object.",
#protection='public',
body="""\
static int s_memFileIdx = 0;
@@ -125,6 +125,7 @@ def run():
bool retval = self->Load(wxT("memory:") + filename );
return retval;
""")
c.addPyCode("XmlResource.LoadFromString = wx.deprecated(XmlResource.LoadFromBuffer, 'Use LoadFromBuffer instead')")
c.find('AddHandler.handler').transfer = True
c.find('InsertHandler.handler').transfer = True

View File

@@ -58,27 +58,48 @@ def run():
c = module.find('wxArchiveFSHandler')
c.addPrivateCopyCtor();
c.addPrivateCopyCtor()
module.addPyCode('ZipFSHandler = wx.deprecated(ArchiveFSHandler, "Use ArchiveFSHandler instead.")')
_fixHandlerClass(c)
c = module.find('wxFSFile')
c.addPrivateCopyCtor();
c.addPrivateCopyCtor()
_fixHandlerClass(c)
c = module.find('wxFilterFSHandler')
c.addPrivateCopyCtor();
c.addPrivateCopyCtor()
_fixHandlerClass(c)
c = module.find('wxInternetFSHandler')
c.addPrivateCopyCtor();
_fixHandlerClass(c)
c = module.find('wxMemoryFSHandler')
c.addPrivateCopyCtor();
c.addPrivateCopyCtor()
_fixHandlerClass(c)
c = module.find('wxMemoryFSHandler')
c.addPrivateCopyCtor()
_fixHandlerClass(c)
# Make some more python-friendly versions of the AddFile methods accepting raw data
c.find('AddFile').findOverload('binarydata').ignore()
c.find('AddFileWithMimeType').findOverload('binarydata').ignore()
c.addCppMethod('void', 'AddFile', '(const wxString& filename, wxPyBuffer* binarydata)',
isStatic=True,
doc="Add a file from raw data in a python buffer compatible object.",
body="""\
wxMemoryFSHandler::AddFile(*filename, binarydata->m_ptr, binarydata->m_len);
""")
c.addCppMethod('void', 'AddFileWithMimeType',
'(const wxString& filename, wxPyBuffer* binarydata, const wxString& mimetype)',
isStatic=True,
doc="Add a file from raw data in a python buffer compatible object.",
body="""\
wxMemoryFSHandler::AddFileWithMimeType(
*filename, binarydata->m_ptr, binarydata->m_len, *mimetype);
""")
#-----------------------------------------------------------------
tools.doCommonTweaks(module)
tools.runGenerators(module)

View File

@@ -585,7 +585,7 @@ class ParamDef(BaseDef):
self.arraySize = False # the param is the size of the array
self.transfer = False # transfer ownership of arg to C++?
self.transferBack = False # transfer ownership of arg from C++ to Python?
self.transferThis = False # ownership of 'this' pointer transfered to this arg
self.transferThis = False # ownership of 'this' pointer transferred to this arg
self.keepReference = False # an extra reference to the arg is held
self.__dict__.update(kw)
if element is not None: