]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/tests/test4.py
Some chicken-and-egg problems solved for wxPython on wxGTK
[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 if wxPlatform == '__WXMSW__':
373 w, h = self.log.GetClientSize()
374 numLines = h/self.charHeight
375 x, y = self.log.PositionToXY(self.log.GetLastPosition())
376 self.log.ShowPosition(self.log.XYToPosition(x, y-numLines+1))
377
378 def OnFileExit(self, event):
379 self.Close()
380
381 def OnCloseWindow(self, event):
382 self.Destroy()
383
384
385
386
387 def OnTestSimpleControls(self, event):
388 dlg = TestSimpleControlsDlg(self, self)
389 dlg.Centre()
390 dlg.ShowModal()
391 dlg.Destroy()
392
393 def OnTestTimer(self, event):
394 if self.timer:
395 self.mainmenu.Check(self.timerID, false)
396 self.timer.Stop()
397 self.timer = None
398 else:
399 self.mainmenu.Check(self.timerID, true)
400 self.timer = TestTimer(self)
401 self.timer.Start(1000)
402
403 def OnTestLayoutConstraints(self, event):
404 win = TestLayoutConstraints(self)
405 win.Show(true)
406
407 def OnTestGrid(self, event):
408 win = TestGrid(self)
409 win.Show(true)
410 win.SetSize(wxSize(505, 300)) # have to force a resize, or the grid doesn't
411 # show up for some reason....
412
413 def OnTestColourDlg(self, event):
414 data = wxColourData()
415 data.SetChooseFull(true)
416 dlg = wxColourDialog(self, data)
417 if dlg.ShowModal() == wxID_OK:
418 data = dlg.GetColourData()
419 self.log.WriteText('You selected: %s\n' % str(data.GetColour().Get()))
420 dlg.Destroy()
421
422 def OnTestDirDlg(self, event):
423 dlg = wxDirDialog(self)
424 if dlg.ShowModal() == wxID_OK:
425 self.log.WriteText('You selected: %s\n' % dlg.GetPath())
426 dlg.Destroy()
427
428 def OnTestFileDlg(self, event):
429 dlg = wxFileDialog(self, "Choose a file", ".", "", "*.*", wxOPEN)
430 if dlg.ShowModal() == wxID_OK:
431 self.log.WriteText('You selected: %s\n' % dlg.GetPath())
432 dlg.Destroy()
433
434 def OnTestSingleChoiceDlg(self, event):
435 dlg = wxSingleChoiceDialog(self, 'Test Single Choice', 'The Caption',
436 ['zero', 'one', 'two', 'three', 'four', 'five',
437 'six', 'seven', 'eight'])
438 if dlg.ShowModal() == wxID_OK:
439 self.log.WriteText('You selected: %s\n' % dlg.GetStringSelection())
440 dlg.Destroy()
441
442 def OnTestTextEntryDlg(self, event):
443 dlg = wxTextEntryDialog(self, 'What is your favorite programming language?',
444 'Duh??', 'Python')
445 #dlg.SetValue("Python is the best!") #### this doesn't work?
446 if dlg.ShowModal() == wxID_OK:
447 self.log.WriteText('You entered: %s\n' % dlg.GetValue())
448 dlg.Destroy()
449
450
451 def OnTestFontDlg(self, event):
452 dlg = wxFontDialog(self)
453 if dlg.ShowModal() == wxID_OK:
454 data = dlg.GetFontData()
455 font = data.GetChosenFont()
456 self.log.WriteText('You selected: "%s", %d points, color %s\n' %
457 (font.GetFaceName(), font.GetPointSize(),
458 data.GetColour().Get()))
459 dlg.Destroy()
460
461
462 def OnTestPageSetupDlg(self, event):
463 data = wxPageSetupData()
464 data.SetMarginTopLeft(wxPoint(50,50))
465 data.SetMarginBottomRight(wxPoint(50,50))
466 dlg = wxPageSetupDialog(self, data)
467 if dlg.ShowModal() == wxID_OK:
468 data = dlg.GetPageSetupData()
469 tl = data.GetMarginTopLeft()
470 br = data.GetMarginBottomRight()
471 self.log.WriteText('Margins are: %s %s\n' % (str(tl), str(br)))
472 dlg.Destroy()
473
474 def OnTestPrintDlg(self, event):
475 data = wxPrintData()
476 data.EnablePrintToFile(true)
477 data.EnablePageNumbers(true)
478 data.EnableSelection(true)
479 dlg = wxPrintDialog(self, data)
480 if dlg.ShowModal() == wxID_OK:
481 self.log.WriteText('\n')
482 dlg.Destroy()
483
484 def OnTestMessageDlg(self, event):
485 dlg = wxMessageDialog(self, 'Hello from Python and wxWindows!',
486 'A Message Box', wxOK | wxICON_INFORMATION)
487 dlg.ShowModal()
488 dlg.Destroy()
489
490
491 def OnTestNotebook(self, event):
492 win = TestNotebookWindow(self)
493 win.Show(true)
494
495 #---------------------------------------------------------------------------
496
497
498 class MyApp(wxApp):
499 def OnInit(self):
500 frame = AppFrame(NULL, -1, "Test 4: (lots of little tests...)")
501 frame.Show(true)
502 self.SetTopWindow(frame)
503 return true
504
505 #---------------------------------------------------------------------------
506
507
508 def main():
509 app = MyApp(0)
510 app.MainLoop()
511
512
513 def t():
514 import pdb
515 pdb.run('main()')
516
517
518 # for focused testing...
519 def main2():
520 class T2App(wxApp):
521 def OnInit(self):
522 frame = TestLayoutConstraints(NULL)
523 frame.Show(true)
524 self.SetTopWindow(frame)
525 return true
526
527 app = T2App(0)
528 app.MainLoop()
529
530 def t2():
531 import pdb
532 pdb.run('main2()')
533
534
535
536 if __name__ == '__main__':
537 main()
538
539
540 #----------------------------------------------------------------------------
541 #
542 # $Log$
543 # Revision 1.4 1998/08/27 21:59:51 RD
544 # Some chicken-and-egg problems solved for wxPython on wxGTK
545 #
546 # Revision 1.3 1998/08/27 00:01:17 RD
547 # - more tweaks
548 # - have discovered some problems but not yet discovered solutions...
549 #
550 # Revision 1.2 1998/08/22 19:51:18 RD
551 # some tweaks for wxGTK
552 #
553 # Revision 1.1 1998/08/09 08:28:05 RD
554 # Initial version
555 #
556 #