]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/XmlResourceSubclass.py
Updated DrawPolyPolygon note
[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
299647ac 14<object class="wxPanel" subclass="XmlResourceSubclass.MyBluePanel" name="MyPanel">
6fa68946 15 <size>200,100</size>
d14a1e28 16 <object class="wxStaticText" name="label1">
95bfd958 17 <label>This blue panel is a class derived from wx.Panel
6fa68946
RD
18and is loaded by a using a subclass attribute of the object tag.</label>
19 <pos>10,10</pos>
20 </object>
21</object>
22</resource>
23'''
24
25#----------------------------------------------------------------------
26
8fa876ca 27class MyBluePanel(wx.Panel):
6fa68946 28 def __init__(self):
8fa876ca 29 p = wx.PrePanel()
299647ac 30 # the Create step is done by XRC.
d14a1e28 31 self.PostCreate(p)
8fa876ca 32 self.Bind(wx.EVT_WINDOW_CREATE, self.OnCreate)
6fa68946
RD
33
34 def OnCreate(self, evt):
35 # This is the little bit of customization that we do for this
36 # silly example. It could just as easily have been done in
37 # the resource. We do it in the EVT_WINDOW_CREATE handler
38 # because the window doesn't really exist yet in the __init__.
39 self.SetBackgroundColour("BLUE")
40 self.SetForegroundColour("WHITE")
41
42#----------------------------------------------------------------------
43
44
8fa876ca 45class TestPanel(wx.Panel):
6fa68946
RD
46 def __init__(self, parent, log):
47 self.log = log
8fa876ca 48 wx.Panel.__init__(self, parent, -1)
6fa68946
RD
49
50 # make the components
8fa876ca
RD
51 label = wx.StaticText(self, -1, "The lower panel was built from this XML:")
52 label.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD))
6fa68946 53
8fa876ca
RD
54 text = wx.TextCtrl(self, -1, resourceText,
55 style=wx.TE_READONLY|wx.TE_MULTILINE)
6fa68946
RD
56 text.SetInsertionPoint(0)
57
8fa876ca 58 line = wx.StaticLine(self, -1)
6fa68946
RD
59
60 # Load the resource
8fa876ca 61 res = xrc.EmptyXmlResource()
6fa68946
RD
62 res.LoadFromString(resourceText)
63
64 # Now create a panel from the resource data
65 panel = res.LoadPanel(self, "MyPanel")
66
67 # and do the layout
8fa876ca
RD
68 sizer = wx.BoxSizer(wx.VERTICAL)
69 sizer.Add(label, 0, wx.EXPAND|wx.TOP|wx.LEFT, 5)
70 sizer.Add(text, 1, wx.EXPAND|wx.ALL, 5)
71 sizer.Add(line, 0, wx.EXPAND)
72 sizer.Add(panel, 1, wx.EXPAND|wx.ALL, 5)
6fa68946
RD
73
74 self.SetSizer(sizer)
75 self.SetAutoLayout(True)
76
77
78#----------------------------------------------------------------------
79
80def runTest(frame, nb, log):
81 win = TestPanel(nb, log)
82 return win
83
84#----------------------------------------------------------------------
85
86
87
88overview = """<html><body>
95bfd958 89<h2><center>wx.XmlResourceSubclass</center></h2>
6fa68946
RD
90
91Sometimes it is necessary to use custom classes, but you still want
92them to be created from XRC. The subclass XRC attribute allows you to
93do that.
94
95</body></html>
96"""
97
98
99
100if __name__ == '__main__':
101 import sys,os
102 import run
8eca4fef 103 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
6fa68946 104