]> git.saurik.com Git - wxWidgets.git/blob - wxPython/misc/widgetLayoutTest.py
e9f61c6bbb8980fae720ec66311f013540044cbd
[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.SetSizeHints((20,20))
46 #self.testPanel.SetDefaultBackgroundColour("dark red")
47 self.testWidget = None
48
49
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)
59
60 self.Bind(wx.EVT_CLOSE, self.OnSaveHistory)
61
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)
67
68
69 # setup the layout
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)
75
76 topSizer.Add(self.testHistory, 0, wx.RIGHT, 30)
77
78 ctlsSizer.Add((1,25))
79 ctlsSizer.Add((1,25))
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)
95
96 btnSizer.Add((5,5))
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)
105
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)
111
112 self.bottomSizer = sz = wx.BoxSizer(wx.VERTICAL)
113 sz.Add((0,0), 1)
114 sz.Add(self.testPanel, 0, wx.ALIGN_CENTER)
115 sz.Add((0,0), 1)
116 bottomPanel.SetSizer(sz)
117
118 p.SetSizerAndFit(mainSizer)
119 self.Fit()
120
121 self.PopulateHistory()
122
123
124
125
126 def PopulateHistory(self):
127 """
128 Load and eval a list of lists from a file, load the contents
129 into self.testHistory
130 """
131 fname = os.path.join(os.path.dirname(sys.argv[0]),
132 'widgetLayoutTest.cfg')
133 try:
134 self.history = eval(open(fname).read())
135 except:
136 self.history = []
137
138 self.testHistory.Clear()
139
140 for idx in range(len(self.history)):
141 item = self.history[idx]
142 # check if it is too short
143 while len(item) < 3:
144 item.append('')
145
146 # add it to the listbox
147 self.testHistory.Append(item[0] + '.' + item[1])
148
149 self.needSaved = False
150
151
152 def OnSaveHistory(self, evt):
153 if self.needSaved:
154 fname = os.path.join(os.path.dirname(sys.argv[0]),
155 'widgetLayoutTest.cfg')
156 f = open(fname, 'wb')
157 f.write('[\n')
158 for item in self.history:
159 f.write(str(item) + ',\n')
160 f.write(']\n')
161 f.close()
162 evt.Skip()
163
164
165 def OnAddHistory(self, evt):
166 moduleName = self.moduleName.GetValue()
167 className = self.className.GetValue()
168 parameters = self.parameters.GetValue()
169
170 item = [str(moduleName), str(className), str(parameters)]
171 self.history.append(item)
172 self.testHistory.Append(item[0] + '.' + item[1])
173
174 self.testHistory.SetSelection(len(self.history)-1)
175 self.needSaved = True
176
177
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
184
185
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()
192
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
197
198
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])
206
207
208 def OnHistoryActivate(self, evt):
209 btn = self.testHistory.GetParent().GetDefaultItem()
210 if btn is not None:
211 e = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, btn.GetId())
212 btn.Command(e)
213
214
215
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()
221
222 expr = "%s.%s(self.testPanel, %s)" % (moduleName, className, parameters)
223 self.expression.SetValue(expr)
224
225 docstring = ""
226 try:
227 docstring = eval("%s.%s.__init__.__doc__" % (moduleName, className))
228 except:
229 pass
230 self.docstring.SetValue(docstring)
231
232
233 def OnEnableDestroy(self, evt):
234 evt.Enable(self.testWidget is not None)
235
236 def OnEnableCreate(self, evt):
237 evt.Enable(self.testWidget is None)
238
239
240 def OnCreateWidget(self, evt):
241 if self.testWidget is not None:
242 return
243
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()
249
250 # make sure the module is imported already
251 if not sys.modules.has_key(moduleName):
252 try:
253 m = __import__(moduleName)
254 except importError:
255 wx.MessageBox("Unable to import module!", "Error")
256 return
257
258 # create the widget
259 try:
260 w = eval(expr)
261 except Exception, e:
262 wx.MessageBox("Got a '%s' Exception!" % e.__class__.__name__, "Error")
263 import traceback
264 traceback.print_exc()
265 return
266
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)
271 self.testWidget = w
272 self.bottomSizer.Layout()
273
274 # make the destroy button be default now
275 self.destroyBtn.SetDefault()
276
277
278
279 def OnDestroyWidget(self, evt):
280 self.testWidget.Destroy()
281 self.testWidget = None
282 self.testPanel.SetSizer(None, True)
283 self.bottomSizer.Layout()
284
285 # make the create button be default now
286 self.createBtn.SetDefault()
287
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()
294
295
296
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("")
303
304
305
306 app = wx.PySimpleApp(redirect=False)
307 frame = LayoutTestFrame()
308 app.SetTopWindow(frame)
309 frame.Show()
310 app.MainLoop()
311