]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/XmlResourceSubclass.py
update from herbert breunung
[wxWidgets.git] / wxPython / demo / XmlResourceSubclass.py
CommitLineData
6fa68946 1
8fa876ca
RD
2import wx
3import wx.xrc as xrc
6fa68946
RD
4
5#----------------------------------------------------------------------
6
7resourceText = r'''<?xml version="1.0"?>
8<resource>
9
10<!-- Notice that the class IS a standard wx class, and a custom
d14a1e28
RD
11 subclass is specified as "moduleName.ClassName" Try changing
12 the classname to one that does not exist and see what happens -->
6fa68946 13
5cd7ab8d 14<object class="wxPanel" subclass="XmlResourceSubclass.MyCustomPanel" name="MyPanel">
6fa68946 15 <size>200,100</size>
d14a1e28 16 <object class="wxStaticText" name="label1">
102e2b26 17 <label>This panel is a custom class derived from wx.Panel,\nand is loaded by a custom XmlResourceHandler.</label>
6fa68946
RD
18 <pos>10,10</pos>
19 </object>
20</object>
21</resource>
22'''
23
24#----------------------------------------------------------------------
25
5cd7ab8d 26class MyCustomPanel(wx.Panel):
6fa68946 27 def __init__(self):
8fa876ca 28 p = wx.PrePanel()
299647ac 29 # the Create step is done by XRC.
d14a1e28 30 self.PostCreate(p)
8fa876ca 31 self.Bind(wx.EVT_WINDOW_CREATE, self.OnCreate)
5cd7ab8d
RD
32 self.Bind(wx.EVT_SIZE, self.OnSize)
33
6fa68946
RD
34
35 def OnCreate(self, evt):
36 # This is the little bit of customization that we do for this
37 # silly example. It could just as easily have been done in
38 # the resource. We do it in the EVT_WINDOW_CREATE handler
39 # because the window doesn't really exist yet in the __init__.
45cf74cc
RD
40 if self is evt.GetEventObject():
41 t = wx.StaticText(self, -1, "MyCustomPanel")
42 f = t.GetFont()
43 f.SetWeight(wx.BOLD)
44 f.SetPointSize(f.GetPointSize()+2)
45 t.SetFont(f)
46 self.t = t
47 # On OSX the EVT_SIZE happens before EVT_WINDOW_CREATE !?!
48 # so give it another kick
49 wx.CallAfter(self.OnSize, None)
102e2b26 50 evt.Skip()
6fa68946 51
5cd7ab8d 52 def OnSize(self, evt):
45cf74cc
RD
53 if hasattr(self, 't'):
54 sz = self.GetSize()
55 w, h = self.t.GetTextExtent(self.t.GetLabel())
45cf74cc 56 self.t.SetPosition(((sz.width-w)/2, (sz.height-h)/2))
5cd7ab8d 57
6fa68946
RD
58#----------------------------------------------------------------------
59
60
8fa876ca 61class TestPanel(wx.Panel):
6fa68946
RD
62 def __init__(self, parent, log):
63 self.log = log
8fa876ca 64 wx.Panel.__init__(self, parent, -1)
6fa68946
RD
65
66 # make the components
8fa876ca
RD
67 label = wx.StaticText(self, -1, "The lower panel was built from this XML:")
68 label.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD))
6fa68946 69
8fa876ca
RD
70 text = wx.TextCtrl(self, -1, resourceText,
71 style=wx.TE_READONLY|wx.TE_MULTILINE)
6fa68946
RD
72 text.SetInsertionPoint(0)
73
8fa876ca 74 line = wx.StaticLine(self, -1)
6fa68946
RD
75
76 # Load the resource
8fa876ca 77 res = xrc.EmptyXmlResource()
6fa68946
RD
78 res.LoadFromString(resourceText)
79
80 # Now create a panel from the resource data
102e2b26 81 panel = res.LoadPanel(self, "MyPanel")
6fa68946
RD
82
83 # and do the layout
8fa876ca
RD
84 sizer = wx.BoxSizer(wx.VERTICAL)
85 sizer.Add(label, 0, wx.EXPAND|wx.TOP|wx.LEFT, 5)
86 sizer.Add(text, 1, wx.EXPAND|wx.ALL, 5)
87 sizer.Add(line, 0, wx.EXPAND)
88 sizer.Add(panel, 1, wx.EXPAND|wx.ALL, 5)
6fa68946
RD
89
90 self.SetSizer(sizer)
91 self.SetAutoLayout(True)
92
93
94#----------------------------------------------------------------------
95
96def runTest(frame, nb, log):
97 win = TestPanel(nb, log)
98 return win
99
100#----------------------------------------------------------------------
101
102
103
104overview = """<html><body>
95bfd958 105<h2><center>wx.XmlResourceSubclass</center></h2>
6fa68946
RD
106
107Sometimes it is necessary to use custom classes, but you still want
108them to be created from XRC. The subclass XRC attribute allows you to
109do that.
110
111</body></html>
112"""
113
114
115
116if __name__ == '__main__':
117 import sys,os
118 import run
8eca4fef 119 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
6fa68946 120