]> git.saurik.com Git - wxWidgets.git/blob - wxPython/misc/widgetLayoutTest.py
a little cleanup and refresh
[wxWidgets.git] / wxPython / misc / widgetLayoutTest.py
1 """
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.
6 """
7
8 import wx
9 import sys
10 import os
11
12
13 class LayoutTestFrame(wx.Frame):
14 def __init__(self):
15 wx.Frame.__init__(self, None, -1, "Widget Layout Tester")
16
17 p = wx.Panel(self)
18
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)
27
28
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")
36
37 self.createBtn = createBtn
38 self.destroyBtn = destroyBtn
39
40 bottomPanel = wx.Panel(p, style=wx.SUNKEN_BORDER, name="bottomPanel")
41 bottomPanel.SetSizeHints((640,240))
42 bottomPanel.SetDefaultBackgroundColour("light blue")
43
44 self.testPanel = wx.Panel(bottomPanel, name="testPanel")
45 self.testPanel.SetDefaultBackgroundColour((205, 183, 181)) # mistyrose3
46 self.testWidget = None
47
48
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)
58
59 self.Bind(wx.EVT_CLOSE, self.OnSaveHistory)
60
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)
66
67
68 # setup the layout
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)
74
75 topSizer.Add(self.testHistory, 0, wx.RIGHT, 30)
76
77 ctlsSizer.Add((1,25))
78 ctlsSizer.Add((1,25))
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)
94
95 btnSizer.Add((5,5))
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)
104
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)
110
111 self.bottomSizer = sz = wx.BoxSizer(wx.VERTICAL)
112 sz.Add((0,0), 1)
113 sz.Add(self.testPanel, 0, wx.ALIGN_CENTER)
114 sz.Add((0,0), 1)
115 bottomPanel.SetSizer(sz)
116
117 p.SetSizerAndFit(mainSizer)
118 self.Fit()
119
120 self.PopulateHistory()
121
122
123
124
125 def PopulateHistory(self):
126 """
127 Load and eval a list of lists from a file, load the contents
128 into self.testHistory
129 """
130 fname = os.path.join(os.path.dirname(sys.argv[0]),
131 'widgetLayoutTest.cfg')
132 try:
133 self.history = eval(open(fname).read())
134 except:
135 self.history = []
136
137 self.testHistory.Clear()
138
139 for idx in range(len(self.history)):
140 item = self.history[idx]
141 # check if it is too short
142 while len(item) < 3:
143 item.append('')
144
145 # add it to the listbox
146 self.testHistory.Append(item[0] + '.' + item[1])
147
148 self.needSaved = False
149
150
151 def OnSaveHistory(self, evt):
152 if self.needSaved:
153 fname = os.path.join(os.path.dirname(sys.argv[0]),
154 'widgetLayoutTest.cfg')
155 f = open(fname, 'wb')
156 f.write('[\n')
157 for item in self.history:
158 f.write(str(item) + ',\n')
159 f.write(']\n')
160 f.close()
161 evt.Skip()
162
163
164 def OnAddHistory(self, evt):
165 moduleName = self.moduleName.GetValue()
166 className = self.className.GetValue()
167 parameters = self.parameters.GetValue()
168
169 item = [str(moduleName), str(className), str(parameters)]
170 self.history.append(item)
171 self.testHistory.Append(item[0] + '.' + item[1])
172
173 self.testHistory.SetSelection(len(self.history)-1)
174 self.needSaved = True
175
176
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
183
184
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()
191
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
196
197
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])
205
206
207 def OnHistoryActivate(self, evt):
208 btn = self.testHistory.GetParent().GetDefaultItem()
209 if btn is not None:
210 e = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, btn.GetId())
211 btn.Command(e)
212
213
214
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()
220
221 expr = "%s.%s(self.testPanel, %s)" % (moduleName, className, parameters)
222 self.expression.SetValue(expr)
223
224 docstring = ""
225 try:
226 docstring = eval("%s.%s.__init__.__doc__" % (moduleName, className))
227 except:
228 pass
229 self.docstring.SetValue(docstring)
230
231
232 def OnEnableDestroy(self, evt):
233 evt.Enable(self.testWidget is not None)
234
235 def OnEnableCreate(self, evt):
236 evt.Enable(self.testWidget is None)
237
238
239 def OnCreateWidget(self, evt):
240 if self.testWidget is not None:
241 return
242
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()
248
249 # make sure the module is imported already
250 if not sys.modules.has_key(moduleName):
251 try:
252 m = __import__(moduleName)
253 except importError:
254 wx.MessageBox("Unable to import module!", "Error")
255 return
256
257 # create the widget
258 try:
259 w = eval(expr)
260 except Exception, e:
261 wx.MessageBox("Got a '%s' Exception!" % e.__class__.__name__, "Error")
262 import traceback
263 traceback.print_exc()
264 return
265
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)
270 self.testWidget = w
271 self.bottomSizer.Layout()
272
273 # make the destroy button be default now
274 self.destroyBtn.SetDefault()
275
276 if False:
277 print 'w size', w.GetSize()
278 print 'w minsize', w.GetMinSize()
279 print 'w bestsize', w.GetBestSize()
280 print 'w abstsize', w.GetAdjustedBestSize()
281
282 tp = self.testPanel
283 #print tp.GetSizer()
284 print 'tp size', tp.GetSize()
285 print 'tp minsize', tp.GetMinSize()
286 print 'tp bestsize', tp.GetBestSize()
287 print 'tp abstsize', tp.GetAdjustedBestSize()
288
289
290
291 def OnDestroyWidget(self, evt):
292 self.testWidget.Destroy()
293 self.testWidget = None
294 self.testPanel.SetSizer(None, True)
295 self.testPanel.Refresh()
296
297 # ensure the panel shrinks again
298 self.testPanel.SetSizeHints((20,20))
299 self.bottomSizer.Layout()
300 self.testPanel.SetSizeHints(wx.DefaultSize)
301
302 # make the create button be default now
303 self.createBtn.SetDefault()
304
305
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("")
312
313
314
315 app = wx.PySimpleApp(redirect=False)
316 frame = LayoutTestFrame()
317 app.SetTopWindow(frame)
318 frame.Show()
319 app.MainLoop()
320