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, 300))
21 self
.moduleName
= wx
.TextCtrl(p
, -1, "wx")
22 self
.className
= wx
.TextCtrl(p
, -1, "")
23 self
.parameters
= wx
.TextCtrl(p
, -1, "")
24 self
.postCreate
= wx
.TextCtrl(p
, -1, "", size
=(1,75),
25 style
=wx
.TE_MULTILINE|wx
.TE_DONTWRAP
)
26 self
.expression
= wx
.TextCtrl(p
, -1, "", style
=wx
.TE_READONLY
)
27 self
.docstring
= wx
.TextCtrl(p
, -1, "", size
=(1,75),
28 style
=wx
.TE_READONLY|wx
.TE_MULTILINE|wx
.TE_DONTWRAP
)
30 self
.expression
.SetBackgroundColour(
31 wx
.SystemSettings
.GetColour(wx
.SYS_COLOUR_INFOBK
))
32 self
.docstring
.SetBackgroundColour(
33 wx
.SystemSettings
.GetColour(wx
.SYS_COLOUR_INFOBK
))
36 addBtn
= wx
.Button(p
, -1, "Add")
37 remBtn
= wx
.Button(p
, -1, "Remove")
38 repBtn
= wx
.Button(p
, -1, "Replace")
39 createBtn
= wx
.Button(p
, -1, " Create Widget ")
40 createBtn
.SetDefault()
41 destroyBtn
= wx
.Button(p
, -1, "Destroy Widget")
42 clearBtn
= wx
.Button(p
, -1, "Clear")
44 self
.createBtn
= createBtn
45 self
.destroyBtn
= destroyBtn
47 bottomPanel
= wx
.Panel(p
, style
=wx
.SUNKEN_BORDER
, name
="bottomPanel")
48 bottomPanel
.SetSizeHints((640,240))
49 bottomPanel
.SetDefaultBackgroundColour("light blue")
51 self
.testPanel
= wx
.Panel(bottomPanel
, name
="testPanel")
52 self
.testPanel
.SetDefaultBackgroundColour((205, 183, 181)) # mistyrose3
53 self
.testWidget
= None
56 # setup event bindings
57 self
.Bind(wx
.EVT_TEXT
, self
.OnUpdate
, self
.moduleName
)
58 self
.Bind(wx
.EVT_TEXT
, self
.OnUpdate
, self
.className
)
59 self
.Bind(wx
.EVT_TEXT
, self
.OnUpdate
, self
.parameters
)
60 self
.Bind(wx
.EVT_UPDATE_UI
, self
.OnEnableCreate
, createBtn
)
61 self
.Bind(wx
.EVT_UPDATE_UI
, self
.OnEnableDestroy
, destroyBtn
)
62 self
.Bind(wx
.EVT_BUTTON
, self
.OnCreateWidget
, createBtn
)
63 self
.Bind(wx
.EVT_BUTTON
, self
.OnDestroyWidget
, destroyBtn
)
64 self
.Bind(wx
.EVT_BUTTON
, self
.OnClear
, clearBtn
)
66 self
.Bind(wx
.EVT_CLOSE
, self
.OnSaveHistory
)
68 self
.Bind(wx
.EVT_BUTTON
, self
.OnAddHistory
, addBtn
)
69 self
.Bind(wx
.EVT_BUTTON
, self
.OnRemoveHistory
, remBtn
)
70 self
.Bind(wx
.EVT_BUTTON
, self
.OnReplaceHistory
, repBtn
)
71 self
.Bind(wx
.EVT_LISTBOX
, self
.OnHistorySelect
, self
.testHistory
)
72 self
.Bind(wx
.EVT_LISTBOX_DCLICK
, self
.OnHistoryActivate
, self
.testHistory
)
76 mainSizer
= wx
.BoxSizer(wx
.VERTICAL
)
77 topSizer
= wx
.BoxSizer(wx
.HORIZONTAL
)
78 ctlsSizer
= wx
.FlexGridSizer(2, 2, 5, 5)
79 ctlsSizer
.AddGrowableCol(1)
80 btnSizer
= wx
.BoxSizer(wx
.HORIZONTAL
)
82 topSizer
.Add(self
.testHistory
, 0, wx
.RIGHT
, 30)
87 ctlsSizer
.Add(wx
.StaticText(p
, -1, "Module name:"),
88 0, wx
.ALIGN_RIGHT|wx
.ALIGN_CENTER_VERTICAL
)
89 mcSizer
= wx
.BoxSizer(wx
.HORIZONTAL
)
90 mcSizer
.Add(self
.moduleName
, 0, 0)
91 mcSizer
.Add(wx
.StaticText(p
, -1, "Class name:"),
92 0, wx
.ALIGN_RIGHT|wx
.ALIGN_CENTER_VERTICAL |wx
.LEFT
, 10)
93 mcSizer
.Add(self
.className
, 1, 0)
94 ctlsSizer
.Add(mcSizer
, 0, wx
.EXPAND
)
96 ctlsSizer
.Add(wx
.StaticText(p
, -1, "Parameters:"),
97 0, wx
.ALIGN_RIGHT|wx
.ALIGN_CENTER_VERTICAL
)
98 ctlsSizer
.Add(self
.parameters
, 0, wx
.EXPAND
)
99 ctlsSizer
.Add(wx
.StaticText(p
, -1, "Create Expr:"),
100 0, wx
.ALIGN_RIGHT|wx
.ALIGN_CENTER_VERTICAL
)
101 ctlsSizer
.Add(self
.expression
, 0, wx
.EXPAND
)
102 ctlsSizer
.Add(wx
.StaticText(p
, -1, "Post create:"), 0, wx
.ALIGN_RIGHT
)
103 ctlsSizer
.Add(self
.postCreate
, 0, wx
.EXPAND
)
104 ctlsSizer
.Add(wx
.StaticText(p
, -1, "DocString:"), 0, wx
.ALIGN_RIGHT
)
105 ctlsSizer
.Add(self
.docstring
, 0, wx
.EXPAND
)
106 ctlsSizer
.AddGrowableRow(5)
107 topSizer
.Add(ctlsSizer
, 1, wx
.EXPAND
)
110 btnSizer
.Add(addBtn
, 0, wx
.RIGHT
, 5)
111 btnSizer
.Add(remBtn
, 0, wx
.RIGHT
, 5)
112 btnSizer
.Add(repBtn
, 0, wx
.RIGHT
, 5)
113 btnSizer
.Add((0,0), 1)
114 btnSizer
.Add(createBtn
, 0, wx
.RIGHT
, 5)
115 btnSizer
.Add(destroyBtn
, 0, wx
.RIGHT
, 5)
116 btnSizer
.Add(clearBtn
, 0, wx
.RIGHT
, 5)
117 btnSizer
.Add((0,0), 1)
119 mainSizer
.Add(topSizer
, 0, wx
.EXPAND|wx
.ALL
, 10)
120 mainSizer
.Add(btnSizer
, 0, wx
.EXPAND
)
121 mainSizer
.Add((10,10))
122 ##mainSizer.Add(wx.StaticLine(p, -1), 0, wx.EXPAND)
123 mainSizer
.Add(bottomPanel
, 1, wx
.EXPAND
)
125 self
.bottomSizer
= sz
= wx
.BoxSizer(wx
.VERTICAL
)
127 sz
.Add(self
.testPanel
, 0, wx
.ALIGN_CENTER
)
129 bottomPanel
.SetSizer(sz
)
131 p
.SetSizerAndFit(mainSizer
)
134 self
.PopulateHistory()
139 def PopulateHistory(self
):
141 Load and eval a list of lists from a file, load the contents
142 into self.testHistory
144 fname
= os
.path
.join(os
.path
.dirname(sys
.argv
[0]),
145 'widgetLayoutTest.cfg')
147 self
.history
= eval(open(fname
).read())
151 self
.testHistory
.Clear()
153 for idx
in range(len(self
.history
)):
154 item
= self
.history
[idx
]
155 # check if it is too short
159 # add it to the listbox
160 self
.testHistory
.Append(item
[0] + '.' + item
[1])
162 self
.needSaved
= False
165 def OnSaveHistory(self
, evt
):
167 fname
= os
.path
.join(os
.path
.dirname(sys
.argv
[0]),
168 'widgetLayoutTest.cfg')
169 f
= open(fname
, 'wb')
171 for item
in self
.history
:
172 f
.write(str(item
) + ',\n')
178 def OnAddHistory(self
, evt
):
179 moduleName
= self
.moduleName
.GetValue()
180 className
= self
.className
.GetValue()
181 parameters
= self
.parameters
.GetValue()
182 postCreate
= self
.postCreate
.GetValue()
184 item
= [str(moduleName
), str(className
), str(parameters
), str(postCreate
)]
185 self
.history
.append(item
)
186 self
.testHistory
.Append(item
[0] + '.' + item
[1])
188 self
.testHistory
.SetSelection(len(self
.history
)-1)
189 self
.needSaved
= True
192 def OnRemoveHistory(self
, evt
):
193 idx
= self
.testHistory
.GetSelection()
194 if idx
!= wx
.NOT_FOUND
:
195 del self
.history
[idx
]
196 self
.testHistory
.Delete(idx
)
197 self
.needSaved
= True
201 def OnReplaceHistory(self
, evt
):
202 idx
= self
.testHistory
.GetSelection()
203 if idx
!= wx
.NOT_FOUND
:
204 moduleName
= self
.moduleName
.GetValue()
205 className
= self
.className
.GetValue()
206 parameters
= self
.parameters
.GetValue()
207 postCreate
= self
.postCreate
.GetValue()
209 item
= [str(moduleName
), str(className
), str(parameters
), str(postCreate
)]
210 self
.history
[idx
] = item
211 self
.testHistory
.SetString(idx
, item
[0] + '.' + item
[1])
212 self
.needSaved
= True
215 def OnHistorySelect(self
, evt
):
216 idx
= self
.testHistory
.GetSelection()
217 if idx
!= wx
.NOT_FOUND
:
218 item
= self
.history
[idx
]
219 self
.moduleName
.SetValue(item
[0])
220 self
.className
.SetValue(item
[1])
221 self
.parameters
.SetValue(item
[2])
222 self
.postCreate
.SetValue(item
[3])
225 def OnHistoryActivate(self
, evt
):
226 btn
= self
.testHistory
.GetParent().GetDefaultItem()
228 e
= wx
.CommandEvent(wx
.wxEVT_COMMAND_BUTTON_CLICKED
, btn
.GetId())
233 def OnUpdate(self
, evt
):
234 # get the details from the form
235 moduleName
= self
.moduleName
.GetValue()
236 className
= self
.className
.GetValue()
237 parameters
= self
.parameters
.GetValue()
239 expr
= "w = %s.%s( testPanel, %s )" % (moduleName
, className
, parameters
)
240 self
.expression
.SetValue(expr
)
244 docstring
= eval("%s.%s.__init__.__doc__" % (moduleName
, className
))
247 self
.docstring
.SetValue(docstring
)
250 def OnEnableDestroy(self
, evt
):
251 evt
.Enable(self
.testWidget
is not None)
253 def OnEnableCreate(self
, evt
):
254 evt
.Enable(self
.testWidget
is None)
257 def OnCreateWidget(self
, evt
):
258 if self
.testWidget
is not None:
261 testPanel
= self
.testPanel
263 # get the details from the form
264 moduleName
= self
.moduleName
.GetValue()
265 className
= self
.className
.GetValue()
266 parameters
= self
.parameters
.GetValue()
267 expr
= self
.expression
.GetValue()[4:]
268 postCreate
= self
.postCreate
.GetValue()
270 # make sure the module is imported already
271 if not sys
.modules
.has_key(moduleName
):
273 m
= __import__(moduleName
)
275 wx
.MessageBox("Unable to import module!", "Error")
282 wx
.MessageBox("Got a '%s' Exception!" % e
.__class
__.__name
__, "Error")
284 traceback
.print_exc()
287 # Is there postCreate code?
293 exec postCreate
in ns
295 wx
.MessageBox("Got a '%s' Exception!" % e
.__class
__.__name
__, "Error")
297 traceback
.print_exc()
300 # Put the widget in a sizer and the sizer in the testPanel
301 sizer
= wx
.BoxSizer(wx
.VERTICAL
)
302 sizer
.Add(w
, 0, wx
.ALL
, 5)
303 self
.testPanel
.SetSizer(sizer
)
305 self
.bottomSizer
.Layout()
307 # make the destroy button be default now
308 self
.destroyBtn
.SetDefault()
311 print 'w size', w
.GetSize()
312 print 'w minsize', w
.GetMinSize()
313 print 'w bestsize', w
.GetBestSize()
314 print 'w abstsize', w
.GetAdjustedBestSize()
318 print 'tp size', tp
.GetSize()
319 print 'tp minsize', tp
.GetMinSize()
320 print 'tp bestsize', tp
.GetBestSize()
321 print 'tp abstsize', tp
.GetAdjustedBestSize()
325 def OnDestroyWidget(self
, evt
):
326 self
.testWidget
.Destroy()
327 self
.testWidget
= None
328 self
.testPanel
.SetSizer(None, True)
329 self
.testPanel
.Refresh()
331 # ensure the panel shrinks again
332 self
.testPanel
.SetSizeHints((20,20))
333 self
.bottomSizer
.Layout()
334 self
.testPanel
.SetSizeHints(wx
.DefaultSize
)
336 # make the create button be default now
337 self
.createBtn
.SetDefault()
340 def OnClear(self
, evt
):
341 self
.moduleName
.SetValue("")
342 self
.className
.SetValue("")
343 self
.parameters
.SetValue("")
344 self
.expression
.SetValue("")
345 self
.docstring
.SetValue("")
346 self
.postCreate
.SetValue("")
350 app
= wx
.PySimpleApp(redirect
=True)
351 frame
= LayoutTestFrame()
352 app
.SetTopWindow(frame
)