]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/XmlResourceHandler.py
GetFirstChild does't need the cookie anymore
[wxWidgets.git] / wxPython / demo / XmlResourceHandler.py
CommitLineData
8fa876ca
RD
1
2import wx
3import wx.xrc as xrc
628c7f79
RD
4
5#----------------------------------------------------------------------
6
7resourceText = r'''<?xml version="1.0"?>
8<resource>
9
10<!-- Notice that the class is NOT a standard wx class -->
11
12<object class="MyBluePanel" name="MyPanel">
13 <size>200,100</size>
95bfd958 14 <object class="wxStaticText" name="label1">
8fa876ca 15 <label>This blue panel is a class derived from wx.Panel,\nand is loaded by a custom XmlResourceHandler.</label>
628c7f79
RD
16 <pos>10,10</pos>
17 </object>
18</object>
19</resource>
20'''
21
22#----------------------------------------------------------------------
23
8fa876ca 24class MyBluePanel(wx.Panel):
628c7f79 25 def __init__(self, parent, id, pos, size, style, name):
8fa876ca 26 wx.Panel.__init__(self, parent, id, pos, size, style, name)
628c7f79
RD
27
28 # This is the little bit of customization that we do for this
29 # silly example. It could just as easily have been done in
30 # the resource.
31 self.SetBackgroundColour("BLUE")
4d5a7477 32 self.SetForegroundColour("WHITE")
628c7f79
RD
33
34
35# To do it the more complex way, (see below) we need to write the
36# class a little differently... This could obviously be done with a
37# single class, but I wanted to make separate ones to make clear what
38# the different requirements are.
8fa876ca 39class PreMyBluePanel(wx.Panel):
628c7f79 40 def __init__(self):
8fa876ca 41 p = wx.PrePanel()
d14a1e28 42 self.PostCreate(p)
628c7f79
RD
43
44 def Create(self, parent, id, pos, size, style, name):
8fa876ca 45 wx.Panel.Create(self, parent, id, pos, size, style, name)
628c7f79 46 self.SetBackgroundColour("BLUE")
4d5a7477
RD
47 self.SetForegroundColour("WHITE")
48
628c7f79
RD
49
50#----------------------------------------------------------------------
51
8fa876ca 52class MyBluePanelXmlHandler(xrc.XmlResourceHandler):
628c7f79 53 def __init__(self):
8fa876ca 54 xrc.XmlResourceHandler.__init__(self)
628c7f79 55 # Specify the styles recognized by objects of this type
8fa876ca
RD
56 self.AddStyle("wx.NO_3D", wx.NO_3D);
57 self.AddStyle("wx.TAB_TRAVERSAL", wx.TAB_TRAVERSAL);
58 self.AddStyle("wx.WS_EX_VALIDATE_RECURSIVELY", wx.WS_EX_VALIDATE_RECURSIVELY);
59 self.AddStyle("wx.CLIP_CHILDREN", wx.CLIP_CHILDREN);
628c7f79
RD
60 self.AddWindowStyles();
61
62 # This method and the next one are required for XmlResourceHandlers
63 def CanHandle(self, node):
64 return self.IsOfClass(node, "MyBluePanel")
65
66 def DoCreateResource(self):
67 # NOTE: wxWindows can be created in either a single-phase or
68 # in a two-phase way. Single phase is what you normally do,
69 # and two-phase creates the instnace first, and then later
70 # creates the actual window when the Create method is called.
71 # (In wxPython the first phase is done using the wxPre*
72 # function, for example, wxPreFrame, wxPrePanel, etc.)
73 #
74 # wxXmlResource supports either method, a premade instance can
75 # be created and populated by xrc using the appropriate
76 # LoadOn* method (such as LoadOnPanel) or xrc can create the
77 # instance too, using the Load* method. However this makes
78 # the handlers a bit more complex. If you can be sure that a
79 # particular class will never be loaded using a pre-existing
80 # instance, then you can make the handle much simpler. I'll
81 # show both methods below.
82
361cef5f 83 if 1:
628c7f79
RD
84 # The simple method assumes that there is no existing
85 # instance. Be sure of that with an assert.
86 assert self.GetInstance() is None
87
88 # Now create the object
89 panel = MyBluePanel(self.GetParentAsWindow(),
90 self.GetID(),
91 self.GetPosition(),
92 self.GetSize(),
8fa876ca 93 self.GetStyle("style", wx.TAB_TRAVERSAL),
628c7f79
RD
94 self.GetName()
95 )
96 else:
97 # When using the more complex (but more flexible) method
98 # the instance may already have been created, check for it
99 panel = self.GetInstance()
100 if panel is None:
101 # if not, then create the instance (but not the window)
102 panel = PreMyBluePanel()
103
104 # Now call the panel's Create method to actually create the window
105 panel.Create(self.GetParentAsWindow(),
106 self.GetID(),
107 self.GetPosition(),
108 self.GetSize(),
8fa876ca 109 self.GetStyle("style", wx.TAB_TRAVERSAL),
628c7f79
RD
110 self.GetName()
111 )
112
113 # These two things should be done in either case:
114 # Set standard window attributes
115 self.SetupWindow(panel)
116 # Create any child windows of this node
117 self.CreateChildren(panel)
118
119 return panel
120
121
122#----------------------------------------------------------------------
123
124
8fa876ca 125class TestPanel(wx.Panel):
628c7f79
RD
126 def __init__(self, parent, log):
127 self.log = log
8fa876ca 128 wx.Panel.__init__(self, parent, -1)
628c7f79
RD
129
130 # make the components
8fa876ca
RD
131 label = wx.StaticText(self, -1, "The lower panel was built from this XML:")
132 label.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD))
628c7f79 133
8fa876ca
RD
134 text = wx.TextCtrl(self, -1, resourceText,
135 style=wx.TE_READONLY|wx.TE_MULTILINE)
628c7f79
RD
136 text.SetInsertionPoint(0)
137
8fa876ca 138 line = wx.StaticLine(self, -1)
628c7f79
RD
139
140 # Load the resource
8fa876ca 141 res = xrc.EmptyXmlResource()
628c7f79
RD
142 res.InsertHandler(MyBluePanelXmlHandler())
143 res.LoadFromString(resourceText)
144
145 # Now create a panel from the resource data
146 panel = res.LoadObject(self, "MyPanel", "MyBluePanel")
147
148 # and do the layout
8fa876ca
RD
149 sizer = wx.BoxSizer(wx.VERTICAL)
150 sizer.Add(label, 0, wx.EXPAND|wx.TOP|wx.LEFT, 5)
151 sizer.Add(text, 1, wx.EXPAND|wx.ALL, 5)
152 sizer.Add(line, 0, wx.EXPAND)
153 sizer.Add(panel, 1, wx.EXPAND|wx.ALL, 5)
628c7f79
RD
154
155 self.SetSizer(sizer)
1e4a197e 156 self.SetAutoLayout(True)
628c7f79
RD
157
158
159#----------------------------------------------------------------------
160
161def runTest(frame, nb, log):
162 win = TestPanel(nb, log)
163 return win
164
165#----------------------------------------------------------------------
166
167
168
169overview = """<html><body>
95bfd958 170<h2><center>wx.XmlResourceHandler</center></h2>
628c7f79 171
95bfd958 172Deriving a class from wx.XmlResourceHandler allows you to specify your
628c7f79
RD
173own classes in XRC resources, and your handler class will then be used
174to create instances of that class when the resource is loaded.
175
176</body></html>
177"""
178
179
180
181if __name__ == '__main__':
182 import sys,os
183 import run
8eca4fef 184 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
628c7f79 185