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