]>
Commit | Line | Data |
---|---|---|
1 | ||
2 | import wx | |
3 | import wx.xrc as xrc | |
4 | ||
5 | #---------------------------------------------------------------------- | |
6 | ||
7 | resourceText = r'''<?xml version="1.0"?> | |
8 | <resource> | |
9 | ||
10 | <!-- Notice that the class IS a standard wx class, and a custom | |
11 | subclass is specified as "moduleName.ClassName" Try changing | |
12 | the classname to one that does not exist and see what happens --> | |
13 | ||
14 | <object class="wxPanel" subclass="XmlResourceSubclass.MyCustomPanel" name="MyPanel"> | |
15 | <size>200,100</size> | |
16 | <object class="wxStaticText" name="label1"> | |
17 | <label>This panel is a custom class derived from wx.Panel,\nand is loaded by a custom XmlResourceHandler.</label> | |
18 | <pos>10,10</pos> | |
19 | </object> | |
20 | </object> | |
21 | </resource> | |
22 | ''' | |
23 | ||
24 | #---------------------------------------------------------------------- | |
25 | ||
26 | class MyCustomPanel(wx.Panel): | |
27 | def __init__(self): | |
28 | p = wx.PrePanel() | |
29 | # the Create step is done by XRC. | |
30 | self.PostCreate(p) | |
31 | self.Bind(wx.EVT_WINDOW_CREATE, self.OnCreate) | |
32 | self.Bind(wx.EVT_SIZE, self.OnSize) | |
33 | ||
34 | ||
35 | def OnCreate(self, evt): | |
36 | # This is the little bit of customization that we do for this | |
37 | # silly example. It could just as easily have been done in | |
38 | # the resource. We do it in the EVT_WINDOW_CREATE handler | |
39 | # because the window doesn't really exist yet in the __init__. | |
40 | if self is evt.GetEventObject(): | |
41 | t = wx.StaticText(self, -1, "MyCustomPanel") | |
42 | f = t.GetFont() | |
43 | f.SetWeight(wx.BOLD) | |
44 | f.SetPointSize(f.GetPointSize()+2) | |
45 | t.SetFont(f) | |
46 | self.t = t | |
47 | # On OSX the EVT_SIZE happens before EVT_WINDOW_CREATE !?! | |
48 | # so give it another kick | |
49 | wx.CallAfter(self.OnSize, None) | |
50 | evt.Skip() | |
51 | ||
52 | def OnSize(self, evt): | |
53 | if hasattr(self, 't'): | |
54 | sz = self.GetSize() | |
55 | w, h = self.t.GetTextExtent(self.t.GetLabel()) | |
56 | print w, h | |
57 | self.t.SetPosition(((sz.width-w)/2, (sz.height-h)/2)) | |
58 | ||
59 | #---------------------------------------------------------------------- | |
60 | ||
61 | ||
62 | class TestPanel(wx.Panel): | |
63 | def __init__(self, parent, log): | |
64 | self.log = log | |
65 | wx.Panel.__init__(self, parent, -1) | |
66 | ||
67 | # make the components | |
68 | label = wx.StaticText(self, -1, "The lower panel was built from this XML:") | |
69 | label.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD)) | |
70 | ||
71 | text = wx.TextCtrl(self, -1, resourceText, | |
72 | style=wx.TE_READONLY|wx.TE_MULTILINE) | |
73 | text.SetInsertionPoint(0) | |
74 | ||
75 | line = wx.StaticLine(self, -1) | |
76 | ||
77 | # Load the resource | |
78 | res = xrc.EmptyXmlResource() | |
79 | res.LoadFromString(resourceText) | |
80 | ||
81 | # Now create a panel from the resource data | |
82 | panel = res.LoadPanel(self, "MyPanel") | |
83 | ||
84 | # and do the layout | |
85 | sizer = wx.BoxSizer(wx.VERTICAL) | |
86 | sizer.Add(label, 0, wx.EXPAND|wx.TOP|wx.LEFT, 5) | |
87 | sizer.Add(text, 1, wx.EXPAND|wx.ALL, 5) | |
88 | sizer.Add(line, 0, wx.EXPAND) | |
89 | sizer.Add(panel, 1, wx.EXPAND|wx.ALL, 5) | |
90 | ||
91 | self.SetSizer(sizer) | |
92 | self.SetAutoLayout(True) | |
93 | ||
94 | ||
95 | #---------------------------------------------------------------------- | |
96 | ||
97 | def runTest(frame, nb, log): | |
98 | win = TestPanel(nb, log) | |
99 | return win | |
100 | ||
101 | #---------------------------------------------------------------------- | |
102 | ||
103 | ||
104 | ||
105 | overview = """<html><body> | |
106 | <h2><center>wx.XmlResourceSubclass</center></h2> | |
107 | ||
108 | Sometimes it is necessary to use custom classes, but you still want | |
109 | them to be created from XRC. The subclass XRC attribute allows you to | |
110 | do that. | |
111 | ||
112 | </body></html> | |
113 | """ | |
114 | ||
115 | ||
116 | ||
117 | if __name__ == '__main__': | |
118 | import sys,os | |
119 | import run | |
120 | run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:]) | |
121 |