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
.SetSizeHints((20,20))
46 #self.testPanel.SetDefaultBackgroundColour("dark red")
47 self
.testWidget
= None
50 # setup event bindings
51 self
.Bind(wx
.EVT_TEXT
, self
.OnUpdate
, self
.moduleName
)
52 self
.Bind(wx
.EVT_TEXT
, self
.OnUpdate
, self
.className
)
53 self
.Bind(wx
.EVT_TEXT
, self
.OnUpdate
, self
.parameters
)
54 self
.Bind(wx
.EVT_UPDATE_UI
, self
.OnEnableCreate
, createBtn
)
55 self
.Bind(wx
.EVT_UPDATE_UI
, self
.OnEnableDestroy
, destroyBtn
)
56 self
.Bind(wx
.EVT_BUTTON
, self
.OnCreateWidget
, createBtn
)
57 self
.Bind(wx
.EVT_BUTTON
, self
.OnDestroyWidget
, destroyBtn
)
58 self
.Bind(wx
.EVT_BUTTON
, self
.OnClear
, clearBtn
)
60 self
.Bind(wx
.EVT_CLOSE
, self
.OnSaveHistory
)
62 self
.Bind(wx
.EVT_BUTTON
, self
.OnAddHistory
, addBtn
)
63 self
.Bind(wx
.EVT_BUTTON
, self
.OnRemoveHistory
, remBtn
)
64 self
.Bind(wx
.EVT_BUTTON
, self
.OnReplaceHistory
, repBtn
)
65 self
.Bind(wx
.EVT_LISTBOX
, self
.OnHistorySelect
, self
.testHistory
)
66 self
.Bind(wx
.EVT_LISTBOX_DCLICK
, self
.OnHistoryActivate
, self
.testHistory
)
70 mainSizer
= wx
.BoxSizer(wx
.VERTICAL
)
71 topSizer
= wx
.BoxSizer(wx
.HORIZONTAL
)
72 ctlsSizer
= wx
.FlexGridSizer(2, 2, 5, 5)
73 ctlsSizer
.AddGrowableCol(1)
74 btnSizer
= wx
.BoxSizer(wx
.HORIZONTAL
)
76 topSizer
.Add(self
.testHistory
, 0, wx
.RIGHT
, 30)
80 ctlsSizer
.Add(wx
.StaticText(p
, -1, "Module name:"),
81 0, wx
.ALIGN_RIGHT|wx
.ALIGN_CENTER_VERTICAL
)
82 ctlsSizer
.Add(self
.moduleName
, 0, wx
.EXPAND
)
83 ctlsSizer
.Add(wx
.StaticText(p
, -1, "Class name:"),
84 0, wx
.ALIGN_RIGHT|wx
.ALIGN_CENTER_VERTICAL
)
85 ctlsSizer
.Add(self
.className
, 0, wx
.EXPAND
)
86 ctlsSizer
.Add(wx
.StaticText(p
, -1, "Parameters:"),
87 0, wx
.ALIGN_RIGHT|wx
.ALIGN_CENTER_VERTICAL
)
88 ctlsSizer
.Add(self
.parameters
, 0, wx
.EXPAND
)
89 ctlsSizer
.Add(wx
.StaticText(p
, -1, "Expression:"),
90 0, wx
.ALIGN_RIGHT|wx
.ALIGN_CENTER_VERTICAL
)
91 ctlsSizer
.Add(self
.expression
, 0, wx
.EXPAND
)
92 ctlsSizer
.Add(wx
.StaticText(p
, -1, "DocString:"), 0, wx
.ALIGN_RIGHT
)
93 ctlsSizer
.Add(self
.docstring
, 0, wx
.EXPAND
)
94 topSizer
.Add(ctlsSizer
, 1)
97 btnSizer
.Add(addBtn
, 0, wx
.RIGHT
, 5)
98 btnSizer
.Add(remBtn
, 0, wx
.RIGHT
, 5)
99 btnSizer
.Add(repBtn
, 0, wx
.RIGHT
, 5)
100 btnSizer
.Add((0,0), 1)
101 btnSizer
.Add(createBtn
, 0, wx
.RIGHT
, 5)
102 btnSizer
.Add(destroyBtn
, 0, wx
.RIGHT
, 5)
103 btnSizer
.Add(clearBtn
, 0, wx
.RIGHT
, 5)
104 btnSizer
.Add((0,0), 1)
106 mainSizer
.Add(topSizer
, 0, wx
.EXPAND|wx
.ALL
, 10)
107 mainSizer
.Add(btnSizer
, 0, wx
.EXPAND
)
108 mainSizer
.Add((10,10))
109 ##mainSizer.Add(wx.StaticLine(p, -1), 0, wx.EXPAND)
110 mainSizer
.Add(bottomPanel
, 1, wx
.EXPAND
)
112 self
.bottomSizer
= sz
= wx
.BoxSizer(wx
.VERTICAL
)
114 sz
.Add(self
.testPanel
, 0, wx
.ALIGN_CENTER
)
116 bottomPanel
.SetSizer(sz
)
118 p
.SetSizerAndFit(mainSizer
)
121 self
.PopulateHistory()
126 def PopulateHistory(self
):
128 Load and eval a list of lists from a file, load the contents
129 into self.testHistory
131 fname
= os
.path
.join(os
.path
.dirname(sys
.argv
[0]),
132 'widgetLayoutTest.cfg')
134 self
.history
= eval(open(fname
).read())
138 self
.testHistory
.Clear()
140 for idx
in range(len(self
.history
)):
141 item
= self
.history
[idx
]
142 # check if it is too short
146 # add it to the listbox
147 self
.testHistory
.Append(item
[0] + '.' + item
[1])
149 self
.needSaved
= False
152 def OnSaveHistory(self
, evt
):
154 fname
= os
.path
.join(os
.path
.dirname(sys
.argv
[0]),
155 'widgetLayoutTest.cfg')
156 f
= open(fname
, 'wb')
158 for item
in self
.history
:
159 f
.write(str(item
) + ',\n')
165 def OnAddHistory(self
, evt
):
166 moduleName
= self
.moduleName
.GetValue()
167 className
= self
.className
.GetValue()
168 parameters
= self
.parameters
.GetValue()
170 item
= [str(moduleName
), str(className
), str(parameters
)]
171 self
.history
.append(item
)
172 self
.testHistory
.Append(item
[0] + '.' + item
[1])
174 self
.testHistory
.SetSelection(len(self
.history
)-1)
175 self
.needSaved
= True
178 def OnRemoveHistory(self
, evt
):
179 idx
= self
.testHistory
.GetSelection()
180 if idx
!= wx
.NOT_FOUND
:
181 del self
.history
[idx
]
182 self
.testHistory
.Delete(idx
)
183 self
.needSaved
= True
186 def OnReplaceHistory(self
, evt
):
187 idx
= self
.testHistory
.GetSelection()
188 if idx
!= wx
.NOT_FOUND
:
189 moduleName
= self
.moduleName
.GetValue()
190 className
= self
.className
.GetValue()
191 parameters
= self
.parameters
.GetValue()
193 item
= [str(moduleName
), str(className
), str(parameters
)]
194 self
.history
[idx
] = item
195 self
.testHistory
.SetString(idx
, item
[0] + '.' + item
[1])
196 self
.needSaved
= True
199 def OnHistorySelect(self
, evt
):
200 idx
= self
.testHistory
.GetSelection()
201 if idx
!= wx
.NOT_FOUND
:
202 item
= self
.history
[idx
]
203 self
.moduleName
.SetValue(item
[0])
204 self
.className
.SetValue(item
[1])
205 self
.parameters
.SetValue(item
[2])
208 def OnHistoryActivate(self
, evt
):
209 btn
= self
.testHistory
.GetParent().GetDefaultItem()
211 e
= wx
.CommandEvent(wx
.wxEVT_COMMAND_BUTTON_CLICKED
, btn
.GetId())
216 def OnUpdate(self
, evt
):
217 # get the details from the form
218 moduleName
= self
.moduleName
.GetValue()
219 className
= self
.className
.GetValue()
220 parameters
= self
.parameters
.GetValue()
222 expr
= "%s.%s(self.testPanel, %s)" % (moduleName
, className
, parameters
)
223 self
.expression
.SetValue(expr
)
227 docstring
= eval("%s.%s.__init__.__doc__" % (moduleName
, className
))
230 self
.docstring
.SetValue(docstring
)
233 def OnEnableDestroy(self
, evt
):
234 evt
.Enable(self
.testWidget
is not None)
236 def OnEnableCreate(self
, evt
):
237 evt
.Enable(self
.testWidget
is None)
240 def OnCreateWidget(self
, evt
):
241 if self
.testWidget
is not None:
244 # get the details from the form
245 moduleName
= self
.moduleName
.GetValue()
246 className
= self
.className
.GetValue()
247 parameters
= self
.parameters
.GetValue()
248 expr
= self
.expression
.GetValue()
250 # make sure the module is imported already
251 if not sys
.modules
.has_key(moduleName
):
253 m
= __import__(moduleName
)
255 wx
.MessageBox("Unable to import module!", "Error")
262 wx
.MessageBox("Got a '%s' Exception!" % e
.__class
__.__name
__, "Error")
264 traceback
.print_exc()
267 # Put the widget in a sizer and the sizer in the testPanel
268 sizer
= wx
.BoxSizer(wx
.VERTICAL
)
269 sizer
.Add(w
, 0, wx
.ALL
, 5)
270 self
.testPanel
.SetSizer(sizer
)
272 self
.bottomSizer
.Layout()
274 # make the destroy button be default now
275 self
.destroyBtn
.SetDefault()
279 def OnDestroyWidget(self
, evt
):
280 self
.testWidget
.Destroy()
281 self
.testWidget
= None
282 self
.testPanel
.SetSizer(None, True)
283 self
.bottomSizer
.Layout()
285 # make the create button be default now
286 self
.createBtn
.SetDefault()
288 ## tp = self.testPanel
289 ## print tp.GetSizer()
290 ## print 'size', tp.GetSize()
291 ## print 'minsize', tp.GetMinSize()
292 ## print 'bestsize', tp.GetBestSize()
293 ## print 'abstsize', tp.GetAdjustedBestSize()
297 def OnClear(self
, evt
):
298 self
.moduleName
.SetValue("")
299 self
.className
.SetValue("")
300 self
.parameters
.SetValue("")
301 self
.expression
.SetValue("")
302 self
.docstring
.SetValue("")
306 app
= wx
.PySimpleApp(redirect
=False)
307 frame
= LayoutTestFrame()
308 app
.SetTopWindow(frame
)