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