]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/demo/Sizers.py
Cleaned up paint DC cache in ~wxPaintDC to avoid spurious memory warning
[wxWidgets.git] / utils / wxPython / demo / Sizers.py
1 #----------------------------------------------------------------------
2 # sizer test code
3 #----------------------------------------------------------------------
4
5 from wxPython.wx import *
6 from wxPython.lib.grids import wxGridSizer, wxFlexGridSizer
7
8 #----------------------------------------------------------------------
9
10 def makeSimpleBox1(win):
11 box = wxBoxSizer(wxHORIZONTAL)
12 box.Add(wxButton(win, 1010, "one"), 0, wxEXPAND)
13 box.Add(wxButton(win, 1010, "two"), 0, wxEXPAND)
14 box.Add(wxButton(win, 1010, "three"), 0, wxEXPAND)
15 box.Add(wxButton(win, 1010, "four"), 0, wxEXPAND)
16
17 return box
18
19 #----------------------------------------------------------------------
20
21 def makeSimpleBox2(win):
22 box = wxBoxSizer(wxVERTICAL)
23 box.Add(wxButton(win, 1010, "one"), 0, wxEXPAND)
24 box.Add(wxButton(win, 1010, "two"), 0, wxEXPAND)
25 box.Add(wxButton(win, 1010, "three"), 0, wxEXPAND)
26 box.Add(wxButton(win, 1010, "four"), 0, wxEXPAND)
27
28 return box
29
30 #----------------------------------------------------------------------
31
32 def makeSimpleBox3(win):
33 box = wxBoxSizer(wxHORIZONTAL)
34 box.Add(wxButton(win, 1010, "one"), 0, wxEXPAND)
35 box.Add(wxButton(win, 1010, "two"), 0, wxEXPAND)
36 box.Add(wxButton(win, 1010, "three"), 0, wxEXPAND)
37 box.Add(wxButton(win, 1010, "four"), 0, wxEXPAND)
38 box.Add(wxButton(win, 1010, "five"), 1, wxEXPAND)
39
40 return box
41
42 #----------------------------------------------------------------------
43
44 def makeSimpleBox4(win):
45 box = wxBoxSizer(wxHORIZONTAL)
46 box.Add(wxButton(win, 1010, "one"), 0, wxEXPAND)
47 box.Add(wxButton(win, 1010, "two"), 0, wxEXPAND)
48 box.Add(wxButton(win, 1010, "three"), 1, wxEXPAND)
49 box.Add(wxButton(win, 1010, "four"), 1, wxEXPAND)
50 box.Add(wxButton(win, 1010, "five"), 1, wxEXPAND)
51
52 return box
53
54 #----------------------------------------------------------------------
55
56 def makeSimpleBox5(win):
57 box = wxBoxSizer(wxHORIZONTAL)
58 box.Add(wxButton(win, 1010, "one"), 0, wxEXPAND)
59 box.Add(wxButton(win, 1010, "two"), 0, wxEXPAND)
60 box.Add(wxButton(win, 1010, "three"), 3, wxEXPAND)
61 box.Add(wxButton(win, 1010, "four"), 1, wxEXPAND)
62 box.Add(wxButton(win, 1010, "five"), 1, wxEXPAND)
63
64 return box
65
66 #----------------------------------------------------------------------
67
68 def makeSimpleBox6(win):
69 box = wxBoxSizer(wxHORIZONTAL)
70 box.Add(wxButton(win, 1010, "one"), 1, wxALIGN_TOP)
71 box.Add(wxButton(win, 1010, "two"), 1, wxEXPAND)
72 box.Add(wxButton(win, 1010, "three"), 1, wxCENTER)
73 box.Add(wxButton(win, 1010, "four"), 1, wxEXPAND)
74 box.Add(wxButton(win, 1010, "five"), 1, wxALIGN_BOTTOM)
75
76 return box
77
78 #----------------------------------------------------------------------
79
80 def makeSimpleBox7(win):
81 box = wxBoxSizer(wxHORIZONTAL)
82 box.Add(wxButton(win, 1010, "one"), 0, wxEXPAND)
83 box.Add(wxButton(win, 1010, "two"), 0, wxEXPAND)
84 box.Add(wxButton(win, 1010, "three"), 0, wxEXPAND)
85 box.Add(60, 20, 0, wxEXPAND)
86 box.Add(wxButton(win, 1010, "five"), 1, wxEXPAND)
87
88 return box
89
90 #----------------------------------------------------------------------
91
92 def makeSimpleBox8(win):
93 box = wxBoxSizer(wxVERTICAL)
94 box.Add(wxButton(win, 1010, "one"), 0, wxEXPAND)
95 box.Add(0,0, 1)
96 box.Add(wxButton(win, 1010, "two"), 0, wxCENTER)
97 box.Add(0,0, 1)
98 box.Add(wxButton(win, 1010, "three"), 0, wxEXPAND)
99 box.Add(wxButton(win, 1010, "four"), 0, wxEXPAND)
100 # box.Add(wxButton(win, 1010, "five"), 1, wxEXPAND)
101
102 return box
103
104 #----------------------------------------------------------------------
105 #----------------------------------------------------------------------
106
107 def makeSimpleBorder1(win):
108 bdr = wxBoxSizer(wxHORIZONTAL)
109 btn = wxButton(win, 1010, "border")
110 btn.SetSize(wxSize(80, 80))
111 bdr.Add(btn, 1, wxEXPAND|wxALL, 15)
112
113 return bdr
114
115 #----------------------------------------------------------------------
116
117 def makeSimpleBorder2(win):
118 bdr = wxBoxSizer(wxHORIZONTAL)
119 btn = wxButton(win, 1010, "border")
120 btn.SetSize(wxSize(80, 80))
121 bdr.Add(btn, 1, wxEXPAND | wxEAST | wxWEST, 15)
122
123 return bdr
124
125 #----------------------------------------------------------------------
126
127 def makeSimpleBorder3(win):
128 bdr = wxBoxSizer(wxHORIZONTAL)
129 btn = wxButton(win, 1010, "border")
130 btn.SetSize(wxSize(80, 80))
131 bdr.Add(btn, 1, wxEXPAND | wxNORTH | wxWEST, 15)
132
133 return bdr
134
135 #----------------------------------------------------------------------
136 #----------------------------------------------------------------------
137
138 def makeBoxInBox(win):
139 box = wxBoxSizer(wxVERTICAL)
140
141 box.Add(wxButton(win, 1010, "one"), 0, wxEXPAND)
142
143 box2 = wxBoxSizer(wxHORIZONTAL)
144 box2.AddMany([ (wxButton(win, 1010, "two"), 0, wxEXPAND),
145 (wxButton(win, 1010, "three"), 0, wxEXPAND),
146 (wxButton(win, 1010, "four"), 0, wxEXPAND),
147 (wxButton(win, 1010, "five"), 0, wxEXPAND),
148 ])
149
150 box3 = wxBoxSizer(wxVERTICAL)
151 box3.AddMany([ (wxButton(win, 1010, "six"), 0, wxEXPAND),
152 (wxButton(win, 1010, "seven"), 2, wxEXPAND),
153 (wxButton(win, 1010, "eight"), 1, wxEXPAND),
154 (wxButton(win, 1010, "nine"), 1, wxEXPAND),
155 ])
156
157 box2.Add(box3, 1, wxEXPAND)
158 box.Add(box2, 1, wxEXPAND)
159
160 box.Add(wxButton(win, 1010, "ten"), 0, wxEXPAND)
161
162 return box
163
164 #----------------------------------------------------------------------
165
166 def makeBoxInBorder(win):
167 bdr = wxBoxSizer(wxHORIZONTAL)
168 box = makeSimpleBox3(win)
169 bdr.Add(box, 1, wxEXPAND | wxALL, 15)
170
171 return bdr
172
173 #----------------------------------------------------------------------
174
175 def makeBorderInBox(win):
176 insideBox = wxBoxSizer(wxHORIZONTAL)
177
178 box2 = wxBoxSizer(wxHORIZONTAL)
179 box2.AddMany([ (wxButton(win, 1010, "one"), 0, wxEXPAND),
180 (wxButton(win, 1010, "two"), 0, wxEXPAND),
181 (wxButton(win, 1010, "three"), 0, wxEXPAND),
182 (wxButton(win, 1010, "four"), 0, wxEXPAND),
183 (wxButton(win, 1010, "five"), 0, wxEXPAND),
184 ])
185
186 insideBox.Add(box2, 0, wxEXPAND)
187
188 bdr = wxBoxSizer(wxHORIZONTAL)
189 bdr.Add(wxButton(win, 1010, "border"), 1, wxEXPAND | wxALL)
190 insideBox.Add(bdr, 1, wxEXPAND | wxALL, 20)
191
192 box3 = wxBoxSizer(wxVERTICAL)
193 box3.AddMany([ (wxButton(win, 1010, "six"), 0, wxEXPAND),
194 (wxButton(win, 1010, "seven"), 2, wxEXPAND),
195 (wxButton(win, 1010, "eight"), 1, wxEXPAND),
196 (wxButton(win, 1010, "nine"), 1, wxEXPAND),
197 ])
198 insideBox.Add(box3, 1, wxEXPAND)
199
200 outsideBox = wxBoxSizer(wxVERTICAL)
201 outsideBox.Add(wxButton(win, 1010, "top"), 0, wxEXPAND)
202 outsideBox.Add(insideBox, 1, wxEXPAND)
203 outsideBox.Add(wxButton(win, 1010, "bottom"), 0, wxEXPAND)
204
205 return outsideBox
206
207
208 #----------------------------------------------------------------------
209
210 def makeGrid1(win):
211 gs = wxGridSizer(3, 3, 2, 2) # rows, cols, hgap, vgap
212
213 gs.AddMany([ (wxButton(win, 1010, 'one'), 0, wxEXPAND),
214 (wxButton(win, 1010, 'two'), 0, wxEXPAND),
215 (wxButton(win, 1010, 'three'), 0, wxEXPAND),
216 (wxButton(win, 1010, 'four'), 0, wxEXPAND),
217 (wxButton(win, 1010, 'five'), 0, wxEXPAND),
218 #(75, 50),
219 (wxButton(win, 1010, 'six'), 0, wxEXPAND),
220 (wxButton(win, 1010, 'seven'), 0, wxEXPAND),
221 (wxButton(win, 1010, 'eight'), 0, wxEXPAND),
222 (wxButton(win, 1010, 'nine'), 0, wxEXPAND),
223 ])
224
225 return gs
226
227 #----------------------------------------------------------------------
228
229 def makeGrid2(win):
230 gs = wxGridSizer(3, 3) # rows, cols, hgap, vgap
231
232 box = wxBoxSizer(wxVERTICAL)
233 box.Add(wxButton(win, 1010, 'A'), 0, wxEXPAND)
234 box.Add(wxButton(win, 1010, 'B'), 1, wxEXPAND)
235
236 gs2 = wxGridSizer(2,2, 4, 4)
237 gs2.AddMany([ (wxButton(win, 1010, 'C'), 0, wxEXPAND),
238 (wxButton(win, 1010, 'E'), 0, wxEXPAND),
239 (wxButton(win, 1010, 'F'), 0, wxEXPAND),
240 (wxButton(win, 1010, 'G'), 0, wxEXPAND)])
241
242 gs.AddMany([ (wxButton(win, 1010, 'one'), 0, wxALIGN_RIGHT | wxALIGN_BOTTOM),
243 (wxButton(win, 1010, 'two'), 0, wxEXPAND),
244 (wxButton(win, 1010, 'three'), 0, wxALIGN_LEFT | wxALIGN_BOTTOM),
245 (wxButton(win, 1010, 'four'), 0, wxEXPAND),
246 (wxButton(win, 1010, 'five'), 0, wxCENTER),
247 (wxButton(win, 1010, 'six'), 0, wxEXPAND),
248 (box, 0, wxEXPAND | wxALL, 10),
249 (wxButton(win, 1010, 'eight'), 0, wxEXPAND),
250 (gs2, 0, wxEXPAND | wxALL, 4),
251 ])
252
253 return gs
254
255 #----------------------------------------------------------------------
256
257 def makeGrid3(win):
258 gs = wxFlexGridSizer(3, 3, 2, 2) # rows, cols, hgap, vgap
259
260 gs.AddMany([ (wxButton(win, 1010, 'one'), 0, wxEXPAND),
261 (wxButton(win, 1010, 'two'), 0, wxEXPAND),
262 (wxButton(win, 1010, 'three'), 0, wxEXPAND),
263 (wxButton(win, 1010, 'four'), 0, wxEXPAND),
264 #(wxButton(win, 1010, 'five'), 0, wxEXPAND),
265 (175, 50),
266 (wxButton(win, 1010, 'six'), 0, wxEXPAND),
267 (wxButton(win, 1010, 'seven'), 0, wxEXPAND),
268 (wxButton(win, 1010, 'eight'), 0, wxEXPAND),
269 (wxButton(win, 1010, 'nine'), 0, wxEXPAND),
270 ])
271
272 gs.AddGrowableRow(0)
273 gs.AddGrowableRow(2)
274 gs.AddGrowableCol(1)
275 return gs
276
277 #----------------------------------------------------------------------
278
279 theTests = [
280 ("Simple horizontal boxes", makeSimpleBox1,
281 "This is a HORIZONTAL box sizer with four non-stretchable buttons held "
282 "within it. Notice that the buttons are added and aligned in the horizontal "
283 "dimension. Also notice that they are fixed size in the horizontal dimension, "
284 "but will stretch vertically."
285 ),
286
287 ("Simple vertical boxes", makeSimpleBox2,
288 "Exactly the same as the previous sample but using a VERTICAL box sizer "
289 "instead of a HORIZONTAL one."
290 ),
291
292 ("Add a stretchable", makeSimpleBox3,
293 "We've added one more button with the strechable flag turned on. Notice "
294 "how it grows to fill the extra space in the otherwise fixed dimension."
295 ),
296
297 ("More than one stretchable", makeSimpleBox4,
298 "Here there are several items that are stretchable, they all divide up the "
299 "extra space evenly."
300 ),
301
302 ("Weighting factor", makeSimpleBox5,
303 "This one shows more than one strechable, but one of them has a weighting "
304 "factor so it gets more of the free space."
305 ),
306
307 ("Edge Affinity", makeSimpleBox6,
308 "For items that don't completly fill their allotted space, and don't "
309 "stretch, you can specify which side (or the center) they should stay "
310 "attached to."
311 ),
312
313 ("Spacer", makeSimpleBox7,
314 "You can add empty space to be managed by a Sizer just as if it were a "
315 "window or another Sizer."
316 ),
317
318 ("Centering in available space", makeSimpleBox8,
319 "This one shows an item that does not expand to fill it's space, but rather"
320 "stays centered within it."
321 ),
322
323 # ("Percent Sizer", makeSimpleBox6,
324 # "You can use the wxBoxSizer like a Percent Sizer. Just make sure that all "
325 # "the weighting factors add up to 100!"
326 # ),
327
328 ("", None, ""),
329
330 ("Simple border sizer", makeSimpleBorder1,
331 "The wxBorderSizer leaves empty space around its contents. This one "
332 "gives a border all the way around."
333 ),
334
335 ("East and West border", makeSimpleBorder2,
336 "You can pick and choose which sides have borders."
337 ),
338
339 ("North and West border", makeSimpleBorder3,
340 "You can pick and choose which sides have borders."
341 ),
342
343 ("", None, ""),
344
345 ("Boxes inside of boxes", makeBoxInBox,
346 "This one shows nesting of boxes within boxes within boxes, using both "
347 "orientations. Notice also that button seven has a greater weighting "
348 "factor than its siblings."
349 ),
350
351 ("Boxes inside a Border", makeBoxInBorder,
352 "Sizers of different types can be nested withing each other as well. "
353 "Here is a box sizer with several buttons embedded within a border sizer."
354 ),
355
356 ("Border in a Box", makeBorderInBox,
357 "Another nesting example. This one has Boxes and a Border inside another Box."
358 ),
359
360 ("", None, ""),
361
362 ("Simple Grid", makeGrid1,
363 "This is an example of the wxGridSizer. In this case all row heights "
364 "and column widths are kept the same as all the others and all items "
365 "fill their available space. The horzontal and vertical gaps are set to "
366 "2 pixels each."
367 ),
368
369 ("More Grid Features", makeGrid2,
370 "This is another example of the wxGridSizer. This one has no gaps in the grid, "
371 "but various cells are given different alignment options and some of them "
372 "hold nested sizers."
373 ),
374
375 ("Flexible Grid", makeGrid3,
376 "This grid allows the rows to have different heights and the columns to have "
377 "different widths. You can also specify rows and columns that are growable, "
378 "which we have done for the first and last row and the middle column for "
379 "this example.\n"
380 "\nThere is also a spacer in the middle cell instead of an actual window."
381 ),
382
383 ]
384 #----------------------------------------------------------------------
385
386 class TestFrame(wxFrame):
387 def __init__(self, parent, title, sizerFunc):
388 wxFrame.__init__(self, parent, -1, title)
389 EVT_BUTTON(self, 1010, self.OnButton)
390
391 self.sizer = sizerFunc(self)
392 self.CreateStatusBar()
393 self.SetStatusText("Resize this frame to see how the sizers respond...")
394 self.sizer.Fit(self)
395
396 self.SetAutoLayout(true)
397 self.SetSizer(self.sizer)
398
399 def OnCloseWindow(self, event):
400 self.MakeModal(false)
401 self.Destroy()
402
403 def OnButton(self, event):
404 self.Close(true)
405
406 #----------------------------------------------------------------------
407
408
409
410 class TestSelectionPanel(wxPanel):
411 def __init__(self, parent, frame):
412 wxPanel.__init__(self, parent, -1)
413 self.frame = frame
414
415 self.list = wxListBox(self, 401,
416 wxDLG_PNT(self, 10, 10), wxDLG_SZE(self, 100, 100),
417 [])
418 EVT_LISTBOX(self, 401, self.OnSelect)
419 EVT_LISTBOX_DCLICK(self, 401, self.OnDClick)
420
421 self.btn = wxButton(self, 402, "Try it!", wxDLG_PNT(self, 120, 10)).SetDefault()
422 EVT_BUTTON(self, 402, self.OnDClick)
423
424 self.text = wxTextCtrl(self, -1, "",
425 wxDLG_PNT(self, 10, 115),
426 wxDLG_SZE(self, 200, 50),
427 wxTE_MULTILINE | wxTE_READONLY)
428
429 for item in theTests:
430 self.list.Append(item[0])
431
432
433
434 def OnSelect(self, event):
435 pos = self.list.GetSelection()
436 self.text.SetValue(theTests[pos][2])
437
438
439 def OnDClick(self, event):
440 pos = self.list.GetSelection()
441 title = theTests[pos][0]
442 func = theTests[pos][1]
443
444 if func:
445 win = TestFrame(self, title, func)
446 win.CentreOnParent(wxBOTH)
447 win.Show(true)
448 win.MakeModal(true)
449
450 #----------------------------------------------------------------------
451
452 def runTest(frame, nb, log):
453 win = TestSelectionPanel(nb, frame)
454 return win
455
456 overview = ""
457 #wxSizer.__doc__ + '\n' + '-' * 80 + '\n' + \
458 #wxBoxSizer.__doc__ + '\n' + '-' * 80 + '\n' + \
459 #wxBorderSizer.__doc__
460
461 #----------------------------------------------------------------------
462
463
464
465 if __name__ == '__main__':
466
467 class MainFrame(wxFrame):
468 def __init__(self):
469 wxFrame.__init__(self, NULL, -1, "Testing...")
470
471 self.CreateStatusBar()
472 mainmenu = wxMenuBar()
473 menu = wxMenu()
474 menu.Append(200, 'E&xit', 'Get the heck outta here!')
475 mainmenu.Append(menu, "&File")
476 self.SetMenuBar(mainmenu)
477 EVT_MENU(self, 200, self.OnExit)
478 self.panel = TestSelectionPanel(self, self)
479 self.SetSize(wxSize(400, 380))
480
481 def OnCloseWindow(self, event):
482 self.Destroy()
483
484 def OnExit(self, event):
485 self.Close(true)
486
487
488 class TestApp(wxApp):
489 def OnInit(self):
490 frame = MainFrame()
491 frame.Show(true)
492 self.SetTopWindow(frame)
493 return true
494
495 app = TestApp(0)
496 app.MainLoop()
497
498
499 #----------------------------------------------------------------------