]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/wxXmlResourceHandler.py
Applied patch [ 827011 ] Event-based processing of item tooltips in wxTreeCtrl
[wxWidgets.git] / wxPython / demo / wxXmlResourceHandler.py
CommitLineData
8fa876ca
RD
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
12import wx
13import wx.xrc as xrc
628c7f79
RD
14
15#----------------------------------------------------------------------
16
17resourceText = 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>
8fa876ca
RD
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>
628c7f79
RD
26 <pos>10,10</pos>
27 </object>
28</object>
29</resource>
30'''
31
32#----------------------------------------------------------------------
33
8fa876ca 34class MyBluePanel(wx.Panel):
628c7f79 35 def __init__(self, parent, id, pos, size, style, name):
8fa876ca 36 wx.Panel.__init__(self, parent, id, pos, size, style, name)
628c7f79
RD
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")
4d5a7477 42 self.SetForegroundColour("WHITE")
628c7f79
RD
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.
8fa876ca 49class PreMyBluePanel(wx.Panel):
628c7f79 50 def __init__(self):
8fa876ca 51 p = wx.PrePanel()
d14a1e28 52 self.PostCreate(p)
628c7f79
RD
53
54 def Create(self, parent, id, pos, size, style, name):
8fa876ca 55 wx.Panel.Create(self, parent, id, pos, size, style, name)
628c7f79 56 self.SetBackgroundColour("BLUE")
4d5a7477
RD
57 self.SetForegroundColour("WHITE")
58
628c7f79
RD
59
60#----------------------------------------------------------------------
61
8fa876ca 62class MyBluePanelXmlHandler(xrc.XmlResourceHandler):
628c7f79 63 def __init__(self):
8fa876ca 64 xrc.XmlResourceHandler.__init__(self)
628c7f79 65 # Specify the styles recognized by objects of this type
8fa876ca
RD
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);
628c7f79
RD
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
361cef5f 93 if 1:
628c7f79
RD
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(),
8fa876ca 103 self.GetStyle("style", wx.TAB_TRAVERSAL),
628c7f79
RD
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(),
8fa876ca 119 self.GetStyle("style", wx.TAB_TRAVERSAL),
628c7f79
RD
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
8fa876ca 135class TestPanel(wx.Panel):
628c7f79
RD
136 def __init__(self, parent, log):
137 self.log = log
8fa876ca 138 wx.Panel.__init__(self, parent, -1)
628c7f79
RD
139
140 # make the components
8fa876ca
RD
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))
628c7f79 143
8fa876ca
RD
144 text = wx.TextCtrl(self, -1, resourceText,
145 style=wx.TE_READONLY|wx.TE_MULTILINE)
628c7f79
RD
146 text.SetInsertionPoint(0)
147
8fa876ca 148 line = wx.StaticLine(self, -1)
628c7f79
RD
149
150 # Load the resource
8fa876ca 151 res = xrc.EmptyXmlResource()
628c7f79
RD
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
8fa876ca
RD
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)
628c7f79
RD
164
165 self.SetSizer(sizer)
1e4a197e 166 self.SetAutoLayout(True)
628c7f79
RD
167
168
169#----------------------------------------------------------------------
170
171def runTest(frame, nb, log):
172 win = TestPanel(nb, log)
173 return win
174
175#----------------------------------------------------------------------
176
177
178
179overview = """<html><body>
180<h2><center>wxXmlResourceHandler</center></h2>
181
182Deriving a class from wxXmlResourceHandler allows you to specify your
183own classes in XRC resources, and your handler class will then be used
184to create instances of that class when the resource is loaded.
185
186</body></html>
187"""
188
189
190
191if __name__ == '__main__':
192 import sys,os
193 import run
194 run.main(['', os.path.basename(sys.argv[0])])
195