2003-12-08 20:23:28 -05:00
|
|
|
|
|
|
|
import wx
|
|
|
|
import wx.xrc as xrc
|
2002-08-23 22:29:34 -04:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
resourceText = r'''<?xml version="1.0"?>
|
|
|
|
<resource>
|
|
|
|
|
|
|
|
<!-- Notice that the class is NOT a standard wx class -->
|
|
|
|
|
2004-03-28 12:02:16 -05:00
|
|
|
<object class="MyCustomPanel" name="MyPanel">
|
2002-08-23 22:29:34 -04:00
|
|
|
<size>200,100</size>
|
2004-01-12 22:17:17 -05:00
|
|
|
<object class="wxStaticText" name="label1">
|
2004-04-07 17:25:11 -04:00
|
|
|
<label>This panel is a custom class derived from wx.Panel,\nand is loaded by a custom XmlResourceHandler.</label>
|
2002-08-23 22:29:34 -04:00
|
|
|
<pos>10,10</pos>
|
|
|
|
</object>
|
|
|
|
</object>
|
|
|
|
</resource>
|
|
|
|
'''
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
2004-03-28 12:02:16 -05:00
|
|
|
class MyCustomPanel(wx.Panel):
|
2002-08-23 22:29:34 -04:00
|
|
|
def __init__(self, parent, id, pos, size, style, name):
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.Panel.__init__(self, parent, id, pos, size, style, name)
|
2002-08-23 22:29:34 -04:00
|
|
|
|
|
|
|
# This is the little bit of customization that we do for this
|
2004-03-28 12:02:16 -05:00
|
|
|
# silly example.
|
|
|
|
self.Bind(wx.EVT_SIZE, self.OnSize)
|
|
|
|
t = wx.StaticText(self, -1, "MyCustomPanel")
|
|
|
|
f = t.GetFont()
|
|
|
|
f.SetWeight(wx.BOLD)
|
|
|
|
f.SetPointSize(f.GetPointSize()+2)
|
|
|
|
t.SetFont(f)
|
|
|
|
self.t = t
|
|
|
|
|
|
|
|
def OnSize(self, evt):
|
|
|
|
sz = self.GetSize()
|
|
|
|
w, h = self.t.GetTextExtent(self.t.GetLabel())
|
|
|
|
self.t.SetPosition(((sz.width-w)/2, (sz.height-h)/2))
|
2002-08-23 22:29:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
# To do it the more complex way, (see below) we need to write the
|
|
|
|
# class a little differently... This could obviously be done with a
|
|
|
|
# single class, but I wanted to make separate ones to make clear what
|
|
|
|
# the different requirements are.
|
2004-03-28 12:02:16 -05:00
|
|
|
class PreMyCustomPanel(wx.Panel):
|
2002-08-23 22:29:34 -04:00
|
|
|
def __init__(self):
|
2003-12-08 20:23:28 -05:00
|
|
|
p = wx.PrePanel()
|
2003-11-12 16:34:20 -05:00
|
|
|
self.PostCreate(p)
|
2002-08-23 22:29:34 -04:00
|
|
|
|
|
|
|
def Create(self, parent, id, pos, size, style, name):
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.Panel.Create(self, parent, id, pos, size, style, name)
|
2004-03-28 12:02:16 -05:00
|
|
|
self.Bind(wx.EVT_SIZE, self.OnSize)
|
|
|
|
t = wx.StaticText(self, -1, "MyCustomPanel")
|
|
|
|
f = t.GetFont()
|
|
|
|
f.SetWeight(wx.BOLD)
|
|
|
|
f.SetPointSize(f.GetPointSize()+2)
|
|
|
|
t.SetFont(f)
|
|
|
|
self.t = t
|
|
|
|
|
|
|
|
def OnSize(self, evt):
|
|
|
|
sz = self.GetSize()
|
|
|
|
w, h = self.t.GetTextExtent(self.t.GetLabel())
|
|
|
|
self.t.SetPosition(((sz.width-w)/2, (sz.height-h)/2))
|
2002-08-23 22:29:34 -04:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
2004-03-28 12:02:16 -05:00
|
|
|
class MyCustomPanelXmlHandler(xrc.XmlResourceHandler):
|
2002-08-23 22:29:34 -04:00
|
|
|
def __init__(self):
|
2003-12-08 20:23:28 -05:00
|
|
|
xrc.XmlResourceHandler.__init__(self)
|
2002-08-23 22:29:34 -04:00
|
|
|
# Specify the styles recognized by objects of this type
|
2004-06-17 01:16:30 -04:00
|
|
|
self.AddStyle("wxNO_3D", wx.NO_3D);
|
|
|
|
self.AddStyle("wxTAB_TRAVERSAL", wx.TAB_TRAVERSAL);
|
|
|
|
self.AddStyle("wxWS_EX_VALIDATE_RECURSIVELY", wx.WS_EX_VALIDATE_RECURSIVELY);
|
|
|
|
self.AddStyle("wxCLIP_CHILDREN", wx.CLIP_CHILDREN);
|
2002-08-23 22:29:34 -04:00
|
|
|
self.AddWindowStyles();
|
|
|
|
|
|
|
|
# This method and the next one are required for XmlResourceHandlers
|
|
|
|
def CanHandle(self, node):
|
2004-03-28 12:02:16 -05:00
|
|
|
return self.IsOfClass(node, "MyCustomPanel")
|
2002-08-23 22:29:34 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
# 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
|
|
|
|
# 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
|
|
|
|
# the handlers a bit more complex. If you can be sure that a
|
|
|
|
# particular class will never be loaded using a pre-existing
|
|
|
|
# instance, then you can make the handle much simpler. I'll
|
|
|
|
# show both methods below.
|
|
|
|
|
2002-08-23 22:44:30 -04:00
|
|
|
if 1:
|
2002-08-23 22:29:34 -04:00
|
|
|
# The simple method assumes that there is no existing
|
|
|
|
# instance. Be sure of that with an assert.
|
|
|
|
assert self.GetInstance() is None
|
|
|
|
|
|
|
|
# Now create the object
|
2004-03-28 12:02:16 -05:00
|
|
|
panel = MyCustomPanel(self.GetParentAsWindow(),
|
|
|
|
self.GetID(),
|
|
|
|
self.GetPosition(),
|
|
|
|
self.GetSize(),
|
|
|
|
self.GetStyle("style", wx.TAB_TRAVERSAL),
|
|
|
|
self.GetName()
|
|
|
|
)
|
2002-08-23 22:29:34 -04:00
|
|
|
else:
|
|
|
|
# When using the more complex (but more flexible) method
|
|
|
|
# the instance may already have been created, check for it
|
|
|
|
panel = self.GetInstance()
|
|
|
|
if panel is None:
|
|
|
|
# if not, then create the instance (but not the window)
|
2004-03-28 12:02:16 -05:00
|
|
|
panel = PreMyCustomPanel()
|
2002-08-23 22:29:34 -04:00
|
|
|
|
|
|
|
# Now call the panel's Create method to actually create the window
|
|
|
|
panel.Create(self.GetParentAsWindow(),
|
|
|
|
self.GetID(),
|
|
|
|
self.GetPosition(),
|
|
|
|
self.GetSize(),
|
2003-12-08 20:23:28 -05:00
|
|
|
self.GetStyle("style", wx.TAB_TRAVERSAL),
|
2002-08-23 22:29:34 -04:00
|
|
|
self.GetName()
|
|
|
|
)
|
|
|
|
|
|
|
|
# These two things should be done in either case:
|
|
|
|
# Set standard window attributes
|
|
|
|
self.SetupWindow(panel)
|
|
|
|
# Create any child windows of this node
|
|
|
|
self.CreateChildren(panel)
|
|
|
|
|
|
|
|
return panel
|
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
class TestPanel(wx.Panel):
|
2002-08-23 22:29:34 -04:00
|
|
|
def __init__(self, parent, log):
|
|
|
|
self.log = log
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.Panel.__init__(self, parent, -1)
|
2002-08-23 22:29:34 -04:00
|
|
|
|
|
|
|
# make the components
|
2003-12-08 20:23:28 -05:00
|
|
|
label = wx.StaticText(self, -1, "The lower panel was built from this XML:")
|
|
|
|
label.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD))
|
2002-08-23 22:29:34 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
text = wx.TextCtrl(self, -1, resourceText,
|
|
|
|
style=wx.TE_READONLY|wx.TE_MULTILINE)
|
2002-08-23 22:29:34 -04:00
|
|
|
text.SetInsertionPoint(0)
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
line = wx.StaticLine(self, -1)
|
2002-08-23 22:29:34 -04:00
|
|
|
|
|
|
|
# Load the resource
|
2003-12-08 20:23:28 -05:00
|
|
|
res = xrc.EmptyXmlResource()
|
2004-03-28 12:02:16 -05:00
|
|
|
res.InsertHandler(MyCustomPanelXmlHandler())
|
2002-08-23 22:29:34 -04:00
|
|
|
res.LoadFromString(resourceText)
|
|
|
|
|
|
|
|
# Now create a panel from the resource data
|
2004-03-28 12:02:16 -05:00
|
|
|
panel = res.LoadObject(self, "MyPanel", "MyCustomPanel")
|
2002-08-23 22:29:34 -04:00
|
|
|
|
|
|
|
# and do the layout
|
2003-12-08 20:23:28 -05:00
|
|
|
sizer = wx.BoxSizer(wx.VERTICAL)
|
|
|
|
sizer.Add(label, 0, wx.EXPAND|wx.TOP|wx.LEFT, 5)
|
|
|
|
sizer.Add(text, 1, wx.EXPAND|wx.ALL, 5)
|
|
|
|
sizer.Add(line, 0, wx.EXPAND)
|
|
|
|
sizer.Add(panel, 1, wx.EXPAND|wx.ALL, 5)
|
2002-08-23 22:29:34 -04:00
|
|
|
|
|
|
|
self.SetSizer(sizer)
|
2003-03-25 01:35:27 -05:00
|
|
|
self.SetAutoLayout(True)
|
2002-08-23 22:29:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
def runTest(frame, nb, log):
|
|
|
|
win = TestPanel(nb, log)
|
|
|
|
return win
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
overview = """<html><body>
|
2004-01-12 22:17:17 -05:00
|
|
|
<h2><center>wx.XmlResourceHandler</center></h2>
|
2002-08-23 22:29:34 -04:00
|
|
|
|
2004-01-12 22:17:17 -05:00
|
|
|
Deriving a class from wx.XmlResourceHandler allows you to specify your
|
2002-08-23 22:29:34 -04:00
|
|
|
own classes in XRC resources, and your handler class will then be used
|
|
|
|
to create instances of that class when the resource is loaded.
|
|
|
|
|
|
|
|
</body></html>
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys,os
|
|
|
|
import run
|
2004-03-04 19:06:33 -05:00
|
|
|
run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
|
2002-08-23 22:29:34 -04:00
|
|
|
|