]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/tests/test4.py
HKEY -> WXHKEY, wxScrolledWindow::SetScale added
[wxWidgets.git] / utils / wxPython / tests / test4.py
1 #!/bin/env python
2 #----------------------------------------------------------------------------
3 # Name: test4.py
4 # Purpose: Testing lots of stuff, controls, window types, etc.
5 #
6 # Author: Robin Dunn
7 #
8 # Created:
9 # RCS-ID: $Id$
10 # Copyright: (c) 1998 by Total Control Software
11 # Licence: wxWindows license
12 #----------------------------------------------------------------------------
13
14
15 from wxPython import *
16
17
18 #---------------------------------------------------------------------------
19
20 class TestSimpleControlsDlg(wxDialog):
21 def __init__(self, parent, log):
22 self.log = log
23 wxDialog.__init__(self, parent, -1, "Test Simple Controls",
24 wxPyDefaultPosition, wxSize(350, 350))
25
26
27 sampleList = ['zero', 'one', 'two', 'three', 'four', 'five',
28 'six', 'seven', 'eight']
29
30 y_pos = 5
31 delta = 25
32
33 wxStaticText(self, -1, "wxTextCtrl", wxPoint(5, y_pos), wxSize(75, 20))
34 wxTextCtrl(self, 10, "", wxPoint(80, y_pos), wxSize(150, 20))
35 EVT_TEXT(self, 10, self.EvtText)
36 y_pos = y_pos + delta
37
38 wxCheckBox(self, 20, "wxCheckBox", wxPoint(80, y_pos), wxSize(150, 20))
39 EVT_CHECKBOX(self, 20, self.EvtCheckBox)
40 y_pos = y_pos + delta
41
42 rb = wxRadioBox(self, 30, "wxRadioBox", wxPoint(80, y_pos), wxPyDefaultSize,
43 sampleList, 3, wxRA_HORIZONTAL)
44 EVT_RADIOBOX(self, 30, self.EvtRadioBox)
45 width, height = rb.GetSize()
46 y_pos = y_pos + height + 5
47
48 wxStaticText(self, -1, "wxChoice", wxPoint(5, y_pos), wxSize(75, 20))
49 wxChoice(self, 40, wxPoint(80, y_pos), wxSize(95, 20), #wxPyDefaultSize,
50 sampleList)
51 EVT_CHOICE(self, 40, self.EvtChoice)
52 y_pos = y_pos + delta
53
54 wxStaticText(self, -1, "wxComboBox", wxPoint(5, y_pos), wxSize(75, 18))
55 wxComboBox(self, 50, "default value", wxPoint(80, y_pos), wxSize(95, 20),
56 sampleList, wxCB_DROPDOWN)
57 EVT_COMBOBOX(self, 50, self.EvtComboBox)
58 y_pos = y_pos + delta
59
60 wxStaticText(self, -1, "wxListBox", wxPoint(5, y_pos), wxSize(75, 18))
61 lb = wxListBox(self, 60, wxPoint(80, y_pos), wxPyDefaultSize,
62 sampleList, wxLB_SINGLE)
63 EVT_LISTBOX(self, 60, self.EvtListBox)
64 EVT_LISTBOX_DCLICK(self, 60, self.EvtListBoxDClick)
65 lb.SetSelection(0)
66 width, height = lb.GetSize()
67 y_pos = y_pos + height + 5
68
69
70
71 y_pos = y_pos + 15
72 wxButton(self, wxID_OK, ' OK ', wxPoint(80, y_pos), wxPyDefaultSize).SetDefault()
73 wxButton(self, wxID_CANCEL, ' Cancel ', wxPoint(140, y_pos))
74
75
76 def EvtText(self, event):
77 self.log.WriteText('EvtText: %s\n' % event.GetString())
78
79 def EvtCheckBox(self, event):
80 self.log.WriteText('EvtCheckBox: %d\n' % event.GetInt())
81
82 def EvtRadioBox(self, event):
83 self.log.WriteText('EvtRadioBox: %d\n' % event.GetInt())
84
85 def EvtChoice(self, event):
86 self.log.WriteText('EvtChoice: %s\n' % event.GetString())
87
88 def EvtComboBox(self, event):
89 self.log.WriteText('EvtComboBox: %s\n' % event.GetString())
90
91 def EvtListBox(self, event):
92 self.log.WriteText('EvtListBox: %s\n' % event.GetString())
93
94 def EvtListBoxDClick(self, event):
95 self.log.WriteText('EvtListBoxDClick:\n')
96
97
98
99 #---------------------------------------------------------------------------
100
101 class TestTimer(wxTimer):
102 def __init__(self, log):
103 wxTimer.__init__(self)
104 self.log = log
105
106 def Notify(self):
107 wxBell()
108 self.log.WriteText('beep!\n')
109
110
111 #---------------------------------------------------------------------------
112
113 class TestLayoutConstraints(wxFrame):
114 def __init__(self, parent):
115 wxFrame.__init__(self, parent, -1, 'Test Layout Constraints',
116 wxPyDefaultPosition, wxSize(500, 300))
117
118 self.SetAutoLayout(true)
119 EVT_BUTTON(self, 100, self.OnButton)
120
121 self.panelA = wxWindow(self, -1, wxPyDefaultPosition, wxPyDefaultSize,
122 wxSIMPLE_BORDER)
123 self.panelA.SetBackgroundColour(wxBLUE)
124 lc = wxLayoutConstraints()
125 lc.top.SameAs(self, wxTop, 10)
126 lc.left.SameAs(self, wxLeft, 10)
127 lc.bottom.SameAs(self, wxBottom, 10)
128 lc.right.PercentOf(self, wxRight, 50)
129 self.panelA.SetConstraints(lc)
130
131 self.panelB = wxWindow(self, -1, wxPyDefaultPosition, wxPyDefaultSize,
132 wxSIMPLE_BORDER)
133 self.panelB.SetBackgroundColour(wxRED)
134 lc = wxLayoutConstraints()
135 lc.top.SameAs(self, wxTop, 10)
136 lc.right.SameAs(self, wxRight, 10)
137 lc.bottom.PercentOf(self, wxBottom, 30)
138 lc.left.RightOf(self.panelA, 10)
139 self.panelB.SetConstraints(lc)
140
141 self.panelC = wxWindow(self, -1, wxPyDefaultPosition, wxPyDefaultSize,
142 wxSIMPLE_BORDER)
143 self.panelC.SetBackgroundColour(wxWHITE)
144 lc = wxLayoutConstraints()
145 lc.top.Below(self.panelB, 10)
146 lc.right.SameAs(self, wxRight, 10)
147 lc.bottom.SameAs(self, wxBottom, 10)
148 lc.left.RightOf(self.panelA, 10)
149 self.panelC.SetConstraints(lc)
150
151 b = wxButton(self.panelA, 100, ' Panel A ')
152 lc = wxLayoutConstraints()
153 lc.centreX.SameAs (self.panelA, wxCentreX)
154 lc.centreY.SameAs (self.panelA, wxCentreY)
155 lc.height.AsIs ()
156 lc.width.PercentOf (self.panelA, wxWidth, 50)
157 b.SetConstraints(lc);
158
159 b = wxButton(self.panelB, 100, ' Panel B ')
160 lc = wxLayoutConstraints()
161 lc.top.SameAs (self.panelB, wxTop, 2)
162 lc.right.SameAs (self.panelB, wxRight, 4)
163 lc.height.AsIs ()
164 lc.width.AsIs ()
165 b.SetConstraints(lc);
166
167 self.panelD = wxWindow(self.panelC, -1, wxPyDefaultPosition, wxPyDefaultSize,
168 wxSIMPLE_BORDER)
169 self.panelD.SetBackgroundColour(wxGREEN)
170 wxStaticText(self.panelD, -1, "Panel D", wxPoint(4, 4)).SetBackgroundColour(wxGREEN)
171
172 b = wxButton(self.panelC, 100, ' Panel C ')
173 lc = wxLayoutConstraints()
174 lc.top.Below (self.panelD)
175 lc.left.RightOf (self.panelD)
176 lc.height.AsIs ()
177 lc.width.AsIs ()
178 b.SetConstraints(lc);
179
180 lc = wxLayoutConstraints()
181 lc.bottom.PercentOf (self.panelC, wxHeight, 50)
182 lc.right.PercentOf (self.panelC, wxWidth, 50)
183 lc.height.SameAs (b, wxHeight)
184 lc.width.SameAs (b, wxWidth)
185 self.panelD.SetConstraints(lc);
186
187
188 def OnButton(self, event):
189 self.Close(true)
190
191
192 def OnCloseWindow(self, event):
193 self.Destroy()
194
195
196 #---------------------------------------------------------------------------
197
198 class TestGrid(wxFrame):
199 def __init__(self, parent):
200 wxFrame.__init__(self, parent, -1, 'Test Grid',
201 wxPyDefaultPosition, wxSize(500, 300))
202
203 grid = wxGrid(self, -1)
204
205 grid.CreateGrid(16, 16)
206 grid.SetColumnWidth(3, 200)
207 grid.SetRowHeight(4, 45)
208 grid.SetCellValue("First cell", 0, 0)
209 grid.SetCellValue("Another cell", 1, 1)
210 grid.SetCellValue("Yet another cell", 2, 2)
211 grid.SetCellTextFont(wxFont(12, wxROMAN, wxITALIC, wxNORMAL), 0, 0)
212 grid.SetCellTextColour(wxRED, 1, 1)
213 grid.SetCellBackgroundColour(wxCYAN, 2, 2)
214 grid.UpdateDimensions()
215 grid.AdjustScrollbars()
216
217
218 def OnCloseWindow(self, event):
219 self.Destroy()
220
221
222 #---------------------------------------------------------------------------
223
224 class TestNotebookWindow(wxFrame):
225 def __init__(self, parent):
226 wxFrame.__init__(self, parent, -1, 'Test wxNotebook',
227 wxPyDefaultPosition, wxPyDefaultSize)
228
229 nb = wxNotebook(self, -1)
230
231 win = wxWindow(nb, -1, wxPyDefaultPosition, wxPyDefaultSize, wxRAISED_BORDER)
232 win.SetBackgroundColour(wxBLUE)
233 nb.AddPage(win, "Blue")
234
235 win = wxWindow(nb, -1, wxPyDefaultPosition, wxPyDefaultSize, wxRAISED_BORDER)
236 win.SetBackgroundColour(wxRED)
237 nb.AddPage(win, "Red")
238
239 win = wxWindow(nb, -1, wxPyDefaultPosition, wxPyDefaultSize, wxRAISED_BORDER)
240 win.SetBackgroundColour(wxGREEN)
241 nb.AddPage(win, "Green")
242
243 win = wxWindow(nb, -1, wxPyDefaultPosition, wxPyDefaultSize, wxRAISED_BORDER)
244 win.SetBackgroundColour(wxCYAN)
245 nb.AddPage(win, "Cyan")
246
247 win = wxWindow(nb, -1, wxPyDefaultPosition, wxPyDefaultSize, wxRAISED_BORDER)
248 win.SetBackgroundColour(wxWHITE)
249 nb.AddPage(win, "White")
250
251 win = wxWindow(nb, -1, wxPyDefaultPosition, wxPyDefaultSize, wxRAISED_BORDER)
252 win.SetBackgroundColour(wxBLACK)
253 nb.AddPage(win, "Black")
254
255 win = wxWindow(nb, -1, wxPyDefaultPosition, wxPyDefaultSize, wxRAISED_BORDER)
256 win.SetBackgroundColour(wxNamedColour('MIDNIGHT BLUE'))
257 nb.AddPage(win, "MIDNIGHT BLUE")
258
259 win = wxWindow(nb, -1, wxPyDefaultPosition, wxPyDefaultSize, wxRAISED_BORDER)
260 win.SetBackgroundColour(wxNamedColour('INDIAN RED'))
261 nb.AddPage(win, "INDIAN RED")
262
263
264 nb.SetSelection(0)
265 self.SetSize(wxSize(500, 300)) # force a redraw so the notebook will draw
266
267
268 def OnCloseWindow(self, event):
269 self.Destroy()
270
271 #---------------------------------------------------------------------------
272
273 class AppFrame(wxFrame):
274 def __init__(self, parent, id, title):
275 wxFrame.__init__(self, parent, id, title, wxPyDefaultPosition,
276 wxSize(420, 200))
277 if wxPlatform == '__WXMSW__':
278 self.icon = wxIcon('bitmaps/mondrian.ico', wxBITMAP_TYPE_ICO)
279 self.SetIcon(self.icon)
280
281 self.mainmenu = wxMenuBar()
282 menu = wxMenu()
283 menu.Append(200, 'E&xit', 'Get the heck outta here!')
284 EVT_MENU(self, 200, self.OnFileExit)
285 self.mainmenu.Append(menu, '&File')
286
287 menu = self.MakeTestsMenu()
288 self.mainmenu.Append(menu, '&Tests')
289 self.SetMenuBar(self.mainmenu)
290
291 self.log = wxTextCtrl(self, -1, '', wxPyDefaultPosition, wxPyDefaultSize,
292 wxTE_MULTILINE|wxTE_READONLY)
293 self.log.WriteText('Test 4:\n')
294 (w, self.charHeight) = self.log.GetTextExtent('X')
295
296
297 def MakeTestsMenu(self):
298 menu = wxMenu()
299
300 mID = NewId()
301 menu.Append(mID, '&Simple Controls')
302 EVT_MENU(self, mID, self.OnTestSimpleControls)
303
304 mID = NewId()
305 menu.Append(mID, '&Timer', '', true)
306 EVT_MENU(self, mID, self.OnTestTimer)
307 self.timerID = mID
308 self.timer = None
309
310 mID = NewId()
311 menu.Append(mID, '&Layout Constraints')
312 EVT_MENU(self, mID, self.OnTestLayoutConstraints)
313
314 mID = NewId()
315 menu.Append(mID, '&Grid')
316 EVT_MENU(self, mID, self.OnTestGrid)
317
318
319 smenu = wxMenu() # make a sub-menu
320
321 mID = NewId()
322 smenu.Append(mID, '&Colour')
323 EVT_MENU(self, mID, self.OnTestColourDlg)
324
325 mID = NewId()
326 smenu.Append(mID, '&Directory')
327 EVT_MENU(self, mID, self.OnTestDirDlg)
328
329 mID = NewId()
330 smenu.Append(mID, '&File')
331 EVT_MENU(self, mID, self.OnTestFileDlg)
332
333 mID = NewId()
334 smenu.Append(mID, '&Single Choice')
335 EVT_MENU(self, mID, self.OnTestSingleChoiceDlg)
336
337 mID = NewId()
338 smenu.Append(mID, '&TextEntry')
339 EVT_MENU(self, mID, self.OnTestTextEntryDlg)
340
341 mID = NewId()
342 smenu.Append(mID, '&Font')
343 EVT_MENU(self, mID, self.OnTestFontDlg)
344
345 mID = NewId()
346 smenu.Append(mID, '&PageSetup')
347 EVT_MENU(self, mID, self.OnTestPageSetupDlg)
348
349 mID = NewId()
350 smenu.Append(mID, '&Print')
351 EVT_MENU(self, mID, self.OnTestPrintDlg)
352
353 mID = NewId()
354 smenu.Append(mID, '&Message')
355 EVT_MENU(self, mID, self.OnTestMessageDlg)
356
357
358 menu.AppendMenu(NewId(), '&Common Dialogs', smenu)
359
360
361 mID = NewId()
362 menu.Append(mID, '&Notebook')
363 EVT_MENU(self, mID, self.OnTestNotebook)
364
365 return menu
366
367
368
369
370 def WriteText(self, str):
371 self.log.WriteText(str)
372 w, h = self.log.GetClientSize()
373 numLines = h/self.charHeight
374 x, y = self.log.PositionToXY(self.log.GetLastPosition())
375 self.log.ShowPosition(self.log.XYToPosition(x, y-numLines+1))
376
377 def OnFileExit(self, event):
378 self.Close()
379
380 def OnCloseWindow(self, event):
381 self.Destroy()
382
383
384
385
386 def OnTestSimpleControls(self, event):
387 dlg = TestSimpleControlsDlg(self, self)
388 dlg.Centre()
389 dlg.ShowModal()
390 dlg.Destroy()
391
392 def OnTestTimer(self, event):
393 if self.timer:
394 self.mainmenu.Check(self.timerID, false)
395 self.timer.Stop()
396 self.timer = None
397 else:
398 self.mainmenu.Check(self.timerID, true)
399 self.timer = TestTimer(self)
400 self.timer.Start(1000)
401
402 def OnTestLayoutConstraints(self, event):
403 win = TestLayoutConstraints(self)
404 win.Show(true)
405
406 def OnTestGrid(self, event):
407 win = TestGrid(self)
408 win.Show(true)
409 win.SetSize(wxSize(505, 300)) # have to force a resize, or the grid doesn't
410 # show up for some reason....
411
412 def OnTestColourDlg(self, event):
413 data = wxColourData()
414 data.SetChooseFull(true)
415 dlg = wxColourDialog(self, data)
416 if dlg.ShowModal() == wxID_OK:
417 data = dlg.GetColourData()
418 self.log.WriteText('You selected: %s\n' % str(data.GetColour().Get()))
419 dlg.Destroy()
420
421 def OnTestDirDlg(self, event):
422 dlg = wxDirDialog(self)
423 if dlg.ShowModal() == wxID_OK:
424 self.log.WriteText('You selected: %s\n' % dlg.GetPath())
425 dlg.Destroy()
426
427 def OnTestFileDlg(self, event):
428 dlg = wxFileDialog(self, "Choose a file", ".", "", "*.*", wxOPEN)
429 if dlg.ShowModal() == wxID_OK:
430 self.log.WriteText('You selected: %s\n' % dlg.GetPath())
431 dlg.Destroy()
432
433 def OnTestSingleChoiceDlg(self, event):
434 dlg = wxSingleChoiceDialog(self, 'Test Single Choice', 'The Caption',
435 ['zero', 'one', 'two', 'three', 'four', 'five',
436 'six', 'seven', 'eight'])
437 if dlg.ShowModal() == wxID_OK:
438 self.log.WriteText('You selected: %s\n' % dlg.GetStringSelection())
439 dlg.Destroy()
440
441 def OnTestTextEntryDlg(self, event):
442 dlg = wxTextEntryDialog(self, 'What is your favorite programming language?',
443 'Duh??', 'Python')
444 #dlg.SetValue("Python is the best!") #### this doesn't work?
445 if dlg.ShowModal() == wxID_OK:
446 self.log.WriteText('You entered: %s\n' % dlg.GetValue())
447 dlg.Destroy()
448
449
450 def OnTestFontDlg(self, event):
451 dlg = wxFontDialog(self)
452 if dlg.ShowModal() == wxID_OK:
453 data = dlg.GetFontData()
454 font = data.GetChosenFont()
455 self.log.WriteText('You selected: "%s", %d points, color %s\n' %
456 (font.GetFaceName(), font.GetPointSize(),
457 data.GetColour().Get()))
458 dlg.Destroy()
459
460
461 def OnTestPageSetupDlg(self, event):
462 data = wxPageSetupData()
463 data.SetMarginTopLeft(wxPoint(50,50))
464 data.SetMarginBottomRight(wxPoint(50,50))
465 dlg = wxPageSetupDialog(self, data)
466 if dlg.ShowModal() == wxID_OK:
467 data = dlg.GetPageSetupData()
468 tl = data.GetMarginTopLeft()
469 br = data.GetMarginBottomRight()
470 self.log.WriteText('Margins are: %s %s\n' % (str(tl), str(br)))
471 dlg.Destroy()
472
473 def OnTestPrintDlg(self, event):
474 data = wxPrintData()
475 data.EnablePrintToFile(true)
476 data.EnablePageNumbers(true)
477 data.EnableSelection(true)
478 dlg = wxPrintDialog(self, data)
479 if dlg.ShowModal() == wxID_OK:
480 self.log.WriteText('\n')
481 dlg.Destroy()
482
483 def OnTestMessageDlg(self, event):
484 dlg = wxMessageDialog(self, 'Hello from Python and wxWindows!',
485 'A Message Box', wxOK | wxICON_INFORMATION)
486 dlg.ShowModal()
487 dlg.Destroy()
488
489
490 def OnTestNotebook(self, event):
491 win = TestNotebookWindow(self)
492 win.Show(true)
493
494 #---------------------------------------------------------------------------
495
496
497 class MyApp(wxApp):
498 def OnInit(self):
499 frame = AppFrame(NULL, -1, "Test 4: (lots of little tests...)")
500 frame.Show(true)
501 self.SetTopWindow(frame)
502 return true
503
504 #---------------------------------------------------------------------------
505
506
507 def main():
508 app = MyApp(0)
509 app.MainLoop()
510
511
512 def t():
513 import pdb
514 pdb.run('main()')
515
516 if __name__ == '__main__':
517 main()
518
519
520 #----------------------------------------------------------------------------
521 #
522 # $Log$
523 # Revision 1.2 1998/08/22 19:51:18 RD
524 # some tweaks for wxGTK
525 #
526 # Revision 1.1 1998/08/09 08:28:05 RD
527 # Initial version
528 #
529 #