]> git.saurik.com Git - wxWidgets.git/blob - wxPython/misc/widgetLayoutTest.py
Added ability to execute more code after the widget is created
[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, 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)
29
30 self.expression.SetBackgroundColour(
31 wx.SystemSettings.GetColour(wx.SYS_COLOUR_INFOBK))
32 self.docstring.SetBackgroundColour(
33 wx.SystemSettings.GetColour(wx.SYS_COLOUR_INFOBK))
34
35
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")
43
44 self.createBtn = createBtn
45 self.destroyBtn = destroyBtn
46
47 bottomPanel = wx.Panel(p, style=wx.SUNKEN_BORDER, name="bottomPanel")
48 bottomPanel.SetSizeHints((640,240))
49 bottomPanel.SetDefaultBackgroundColour("light blue")
50
51 self.testPanel = wx.Panel(bottomPanel, name="testPanel")
52 self.testPanel.SetDefaultBackgroundColour((205, 183, 181)) # mistyrose3
53 self.testWidget = None
54
55
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)
65
66 self.Bind(wx.EVT_CLOSE, self.OnSaveHistory)
67
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)
73
74
75 # setup the layout
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)
81
82 topSizer.Add(self.testHistory, 0, wx.RIGHT, 30)
83
84 ctlsSizer.Add((1,25))
85 ctlsSizer.Add((1,25))
86
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)
95
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)
108
109 btnSizer.Add((5,5))
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)
118
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)
124
125 self.bottomSizer = sz = wx.BoxSizer(wx.VERTICAL)
126 sz.Add((0,0), 1)
127 sz.Add(self.testPanel, 0, wx.ALIGN_CENTER)
128 sz.Add((0,0), 1)
129 bottomPanel.SetSizer(sz)
130
131 p.SetSizerAndFit(mainSizer)
132 self.Fit()
133
134 self.PopulateHistory()
135
136
137
138
139 def PopulateHistory(self):
140 """
141 Load and eval a list of lists from a file, load the contents
142 into self.testHistory
143 """
144 fname = os.path.join(os.path.dirname(sys.argv[0]),
145 'widgetLayoutTest.cfg')
146 try:
147 self.history = eval(open(fname).read())
148 except:
149 self.history = []
150
151 self.testHistory.Clear()
152
153 for idx in range(len(self.history)):
154 item = self.history[idx]
155 # check if it is too short
156 while len(item) < 4:
157 item.append('')
158
159 # add it to the listbox
160 self.testHistory.Append(item[0] + '.' + item[1])
161
162 self.needSaved = False
163
164
165 def OnSaveHistory(self, evt):
166 if self.needSaved:
167 fname = os.path.join(os.path.dirname(sys.argv[0]),
168 'widgetLayoutTest.cfg')
169 f = open(fname, 'wb')
170 f.write('[\n')
171 for item in self.history:
172 f.write(str(item) + ',\n')
173 f.write(']\n')
174 f.close()
175 evt.Skip()
176
177
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()
183
184 item = [str(moduleName), str(className), str(parameters), str(postCreate)]
185 self.history.append(item)
186 self.testHistory.Append(item[0] + '.' + item[1])
187
188 self.testHistory.SetSelection(len(self.history)-1)
189 self.needSaved = True
190
191
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
198 self.OnClear(None)
199
200
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()
208
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
213
214
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])
223
224
225 def OnHistoryActivate(self, evt):
226 btn = self.testHistory.GetParent().GetDefaultItem()
227 if btn is not None:
228 e = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, btn.GetId())
229 btn.Command(e)
230
231
232
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()
238
239 expr = "w = %s.%s( testPanel, %s )" % (moduleName, className, parameters)
240 self.expression.SetValue(expr)
241
242 docstring = ""
243 try:
244 docstring = eval("%s.%s.__init__.__doc__" % (moduleName, className))
245 except:
246 pass
247 self.docstring.SetValue(docstring)
248
249
250 def OnEnableDestroy(self, evt):
251 evt.Enable(self.testWidget is not None)
252
253 def OnEnableCreate(self, evt):
254 evt.Enable(self.testWidget is None)
255
256
257 def OnCreateWidget(self, evt):
258 if self.testWidget is not None:
259 return
260
261 testPanel = self.testPanel
262
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()
269
270 # make sure the module is imported already
271 if not sys.modules.has_key(moduleName):
272 try:
273 m = __import__(moduleName)
274 except importError:
275 wx.MessageBox("Unable to import module!", "Error")
276 return
277
278 # create the widget
279 try:
280 w = eval(expr)
281 except Exception, e:
282 wx.MessageBox("Got a '%s' Exception!" % e.__class__.__name__, "Error")
283 import traceback
284 traceback.print_exc()
285 return
286
287 # Is there postCreate code?
288 if postCreate:
289 ns = {}
290 ns.update(globals())
291 ns.update(locals())
292 try:
293 exec postCreate in ns
294 except Exception, e:
295 wx.MessageBox("Got a '%s' Exception!" % e.__class__.__name__, "Error")
296 import traceback
297 traceback.print_exc()
298 return
299
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)
304 self.testWidget = w
305 self.bottomSizer.Layout()
306
307 # make the destroy button be default now
308 self.destroyBtn.SetDefault()
309
310 if False:
311 print 'w size', w.GetSize()
312 print 'w minsize', w.GetMinSize()
313 print 'w bestsize', w.GetBestSize()
314 print 'w abstsize', w.GetAdjustedBestSize()
315
316 tp = self.testPanel
317 #print tp.GetSizer()
318 print 'tp size', tp.GetSize()
319 print 'tp minsize', tp.GetMinSize()
320 print 'tp bestsize', tp.GetBestSize()
321 print 'tp abstsize', tp.GetAdjustedBestSize()
322
323
324
325 def OnDestroyWidget(self, evt):
326 self.testWidget.Destroy()
327 self.testWidget = None
328 self.testPanel.SetSizer(None, True)
329 self.testPanel.Refresh()
330
331 # ensure the panel shrinks again
332 self.testPanel.SetSizeHints((20,20))
333 self.bottomSizer.Layout()
334 self.testPanel.SetSizeHints(wx.DefaultSize)
335
336 # make the create button be default now
337 self.createBtn.SetDefault()
338
339
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("")
347
348
349
350 app = wx.PySimpleApp(redirect=True)
351 frame = LayoutTestFrame()
352 app.SetTopWindow(frame)
353 frame.Show()
354 app.MainLoop()
355