2 This test app is meant to help check if a widget has a good
3 DoGetBestSize method (in C++) by allowing the user to dynamically
4 create any non-toplevel widget which will be placed in a Sizer
5 designed to show how the widget will be sized naturally.
13 class LayoutTestFrame(wx
.Frame
):
15 wx
.Frame
.__init
__(self
, None, -1, "Widget Layout Tester")
19 # Create control widgets
20 self
.testHistory
= wx
.ListBox(p
, -1, size
=(150, 250))
21 self
.moduleName
= wx
.TextCtrl(p
, -1, "wx")
22 self
.className
= wx
.TextCtrl(p
, -1, "")
23 self
.parameters
= wx
.TextCtrl(p
, -1, "")
24 self
.expression
= wx
.TextCtrl(p
, -1, "", style
=wx
.TE_READONLY
)
25 self
.docstring
= wx
.TextCtrl(p
, -1, "", size
=(1,125),
26 style
=wx
.TE_READONLY|wx
.TE_MULTILINE|wx
.TE_DONTWRAP
)
29 addBtn
= wx
.Button(p
, -1, "Add")
30 remBtn
= wx
.Button(p
, -1, "Remove")
31 repBtn
= wx
.Button(p
, -1, "Replace")
32 createBtn
= wx
.Button(p
, -1, " Create Widget ")
33 createBtn
.SetDefault()
34 destroyBtn
= wx
.Button(p
, -1, "Destroy Widget")
35 clearBtn
= wx
.Button(p
, -1, "Clear")
37 self
.createBtn
= createBtn
38 self
.destroyBtn
= destroyBtn
40 bottomPanel
= wx
.Panel(p
, style
=wx
.SUNKEN_BORDER
, name
="bottomPanel")
41 bottomPanel
.SetSizeHints((640,240))
42 bottomPanel
.SetDefaultBackgroundColour("light blue")
44 self
.testPanel
= wx
.Panel(bottomPanel
, name
="testPanel")
45 self
.testPanel
.SetDefaultBackgroundColour((205, 183, 181)) # mistyrose3
46 self
.testWidget
= None
49 # setup event bindings
50 self
.Bind(wx
.EVT_TEXT
, self
.OnUpdate
, self
.moduleName
)
51 self
.Bind(wx
.EVT_TEXT
, self
.OnUpdate
, self
.className
)
52 self
.Bind(wx
.EVT_TEXT
, self
.OnUpdate
, self
.parameters
)
53 self
.Bind(wx
.EVT_UPDATE_UI
, self
.OnEnableCreate
, createBtn
)
54 self
.Bind(wx
.EVT_UPDATE_UI
, self
.OnEnableDestroy
, destroyBtn
)
55 self
.Bind(wx
.EVT_BUTTON
, self
.OnCreateWidget
, createBtn
)
56 self
.Bind(wx
.EVT_BUTTON
, self
.OnDestroyWidget
, destroyBtn
)
57 self
.Bind(wx
.EVT_BUTTON
, self
.OnClear
, clearBtn
)
59 self
.Bind(wx
.EVT_CLOSE
, self
.OnSaveHistory
)
61 self
.Bind(wx
.EVT_BUTTON
, self
.OnAddHistory
, addBtn
)
62 self
.Bind(wx
.EVT_BUTTON
, self
.OnRemoveHistory
, remBtn
)
63 self
.Bind(wx
.EVT_BUTTON
, self
.OnReplaceHistory
, repBtn
)
64 self
.Bind(wx
.EVT_LISTBOX
, self
.OnHistorySelect
, self
.testHistory
)
65 self
.Bind(wx
.EVT_LISTBOX_DCLICK
, self
.OnHistoryActivate
, self
.testHistory
)
69 mainSizer
= wx
.BoxSizer(wx
.VERTICAL
)
70 topSizer
= wx
.BoxSizer(wx
.HORIZONTAL
)
71 ctlsSizer
= wx
.FlexGridSizer(2, 2, 5, 5)
72 ctlsSizer
.AddGrowableCol(1)
73 btnSizer
= wx
.BoxSizer(wx
.HORIZONTAL
)
75 topSizer
.Add(self
.testHistory
, 0, wx
.RIGHT
, 30)
79 ctlsSizer
.Add(wx
.StaticText(p
, -1, "Module name:"),
80 0, wx
.ALIGN_RIGHT|wx
.ALIGN_CENTER_VERTICAL
)
81 ctlsSizer
.Add(self
.moduleName
, 0, wx
.EXPAND
)
82 ctlsSizer
.Add(wx
.StaticText(p
, -1, "Class name:"),
83 0, wx
.ALIGN_RIGHT|wx
.ALIGN_CENTER_VERTICAL
)
84 ctlsSizer
.Add(self
.className
, 0, wx
.EXPAND
)
85 ctlsSizer
.Add(wx
.StaticText(p
, -1, "Parameters:"),
86 0, wx
.ALIGN_RIGHT|wx
.ALIGN_CENTER_VERTICAL
)
87 ctlsSizer
.Add(self
.parameters
, 0, wx
.EXPAND
)
88 ctlsSizer
.Add(wx
.StaticText(p
, -1, "Expression:"),
89 0, wx
.ALIGN_RIGHT|wx
.ALIGN_CENTER_VERTICAL
)
90 ctlsSizer
.Add(self
.expression
, 0, wx
.EXPAND
)
91 ctlsSizer
.Add(wx
.StaticText(p
, -1, "DocString:"), 0, wx
.ALIGN_RIGHT
)
92 ctlsSizer
.Add(self
.docstring
, 0, wx
.EXPAND
)
93 topSizer
.Add(ctlsSizer
, 1)
96 btnSizer
.Add(addBtn
, 0, wx
.RIGHT
, 5)
97 btnSizer
.Add(remBtn
, 0, wx
.RIGHT
, 5)
98 btnSizer
.Add(repBtn
, 0, wx
.RIGHT
, 5)
99 btnSizer
.Add((0,0), 1)
100 btnSizer
.Add(createBtn
, 0, wx
.RIGHT
, 5)
101 btnSizer
.Add(destroyBtn
, 0, wx
.RIGHT
, 5)
102 btnSizer
.Add(clearBtn
, 0, wx
.RIGHT
, 5)
103 btnSizer
.Add((0,0), 1)
105 mainSizer
.Add(topSizer
, 0, wx
.EXPAND|wx
.ALL
, 10)
106 mainSizer
.Add(btnSizer
, 0, wx
.EXPAND
)
107 mainSizer
.Add((10,10))
108 ##mainSizer.Add(wx.StaticLine(p, -1), 0, wx.EXPAND)
109 mainSizer
.Add(bottomPanel
, 1, wx
.EXPAND
)
111 self
.bottomSizer
= sz
= wx
.BoxSizer(wx
.VERTICAL
)
113 sz
.Add(self
.testPanel
, 0, wx
.ALIGN_CENTER
)
115 bottomPanel
.SetSizer(sz
)
117 p
.SetSizerAndFit(mainSizer
)
120 self
.PopulateHistory()
125 def PopulateHistory(self
):
127 Load and eval a list of lists from a file, load the contents
128 into self.testHistory
130 fname
= os
.path
.join(os
.path
.dirname(sys
.argv
[0]),
131 'widgetLayoutTest.cfg')
133 self
.history
= eval(open(fname
).read())
137 self
.testHistory
.Clear()
139 for idx
in range(len(self
.history
)):
140 item
= self
.history
[idx
]
141 # check if it is too short
145 # add it to the listbox
146 self
.testHistory
.Append(item
[0] + '.' + item
[1])
148 self
.needSaved
= False
151 def OnSaveHistory(self
, evt
):
153 fname
= os
.path
.join(os
.path
.dirname(sys
.argv
[0]),
154 'widgetLayoutTest.cfg')
155 f
= open(fname
, 'wb')
157 for item
in self
.history
:
158 f
.write(str(item
) + ',\n')
164 def OnAddHistory(self
, evt
):
165 moduleName
= self
.moduleName
.GetValue()
166 className
= self
.className
.GetValue()
167 parameters
= self
.parameters
.GetValue()
169 item
= [str(moduleName
), str(className
), str(parameters
)]
170 self
.history
.append(item
)
171 self
.testHistory
.Append(item
[0] + '.' + item
[1])
173 self
.testHistory
.SetSelection(len(self
.history
)-1)
174 self
.needSaved
= True
177 def OnRemoveHistory(self
, evt
):
178 idx
= self
.testHistory
.GetSelection()
179 if idx
!= wx
.NOT_FOUND
:
180 del self
.history
[idx
]
181 self
.testHistory
.Delete(idx
)
182 self
.needSaved
= True
185 def OnReplaceHistory(self
, evt
):
186 idx
= self
.testHistory
.GetSelection()
187 if idx
!= wx
.NOT_FOUND
:
188 moduleName
= self
.moduleName
.GetValue()
189 className
= self
.className
.GetValue()
190 parameters
= self
.parameters
.GetValue()
192 item
= [str(moduleName
), str(className
), str(parameters
)]
193 self
.history
[idx
] = item
194 self
.testHistory
.SetString(idx
, item
[0] + '.' + item
[1])
195 self
.needSaved
= True
198 def OnHistorySelect(self
, evt
):
199 idx
= self
.testHistory
.GetSelection()
200 if idx
!= wx
.NOT_FOUND
:
201 item
= self
.history
[idx
]
202 self
.moduleName
.SetValue(item
[0])
203 self
.className
.SetValue(item
[1])
204 self
.parameters
.SetValue(item
[2])
207 def OnHistoryActivate(self
, evt
):
208 btn
= self
.testHistory
.GetParent().GetDefaultItem()
210 e
= wx
.CommandEvent(wx
.wxEVT_COMMAND_BUTTON_CLICKED
, btn
.GetId())
215 def OnUpdate(self
, evt
):
216 # get the details from the form
217 moduleName
= self
.moduleName
.GetValue()
218 className
= self
.className
.GetValue()
219 parameters
= self
.parameters
.GetValue()
221 expr
= "%s.%s(self.testPanel, %s)" % (moduleName
, className
, parameters
)
222 self
.expression
.SetValue(expr
)
226 docstring
= eval("%s.%s.__init__.__doc__" % (moduleName
, className
))
229 self
.docstring
.SetValue(docstring
)
232 def OnEnableDestroy(self
, evt
):
233 evt
.Enable(self
.testWidget
is not None)
235 def OnEnableCreate(self
, evt
):
236 evt
.Enable(self
.testWidget
is None)
239 def OnCreateWidget(self
, evt
):
240 if self
.testWidget
is not None:
243 # get the details from the form
244 moduleName
= self
.moduleName
.GetValue()
245 className
= self
.className
.GetValue()
246 parameters
= self
.parameters
.GetValue()
247 expr
= self
.expression
.GetValue()
249 # make sure the module is imported already
250 if not sys
.modules
.has_key(moduleName
):
252 m
= __import__(moduleName
)
254 wx
.MessageBox("Unable to import module!", "Error")
261 wx
.MessageBox("Got a '%s' Exception!" % e
.__class
__.__name
__, "Error")
263 traceback
.print_exc()
266 # Put the widget in a sizer and the sizer in the testPanel
267 sizer
= wx
.BoxSizer(wx
.VERTICAL
)
268 sizer
.Add(w
, 0, wx
.ALL
, 5)
269 self
.testPanel
.SetSizer(sizer
)
271 self
.bottomSizer
.Layout()
273 # make the destroy button be default now
274 self
.destroyBtn
.SetDefault()
277 print 'w size', w
.GetSize()
278 print 'w minsize', w
.GetMinSize()
279 print 'w bestsize', w
.GetBestSize()
280 print 'w abstsize', w
.GetAdjustedBestSize()
284 print 'tp size', tp
.GetSize()
285 print 'tp minsize', tp
.GetMinSize()
286 print 'tp bestsize', tp
.GetBestSize()
287 print 'tp abstsize', tp
.GetAdjustedBestSize()
291 def OnDestroyWidget(self
, evt
):
292 self
.testWidget
.Destroy()
293 self
.testWidget
= None
294 self
.testPanel
.SetSizer(None, True)
295 self
.testPanel
.Refresh()
297 # ensure the panel shrinks again
298 self
.testPanel
.SetSizeHints((20,20))
299 self
.bottomSizer
.Layout()
300 self
.testPanel
.SetSizeHints(wx
.DefaultSize
)
302 # make the create button be default now
303 self
.createBtn
.SetDefault()
306 def OnClear(self
, evt
):
307 self
.moduleName
.SetValue("")
308 self
.className
.SetValue("")
309 self
.parameters
.SetValue("")
310 self
.expression
.SetValue("")
311 self
.docstring
.SetValue("")
315 app
= wx
.PySimpleApp(redirect
=False)
316 frame
= LayoutTestFrame()
317 app
.SetTopWindow(frame
)