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