2 #----------------------------------------------------------------------------
4 # Purpose: Testing lots of stuff, controls, window types, etc.
10 # Copyright: (c) 1998 by Total Control Software
11 # Licence: wxWindows license
12 #----------------------------------------------------------------------------
15 from wxPython
import *
18 #---------------------------------------------------------------------------
20 class TestSimpleControlsDlg(wxDialog
):
21 def __init__(self
, parent
, log
):
23 wxDialog
.__init
__(self
, parent
, -1, "Test Simple Controls",
24 wxPyDefaultPosition
, wxSize(350, 350))
27 sampleList
= ['zero', 'one', 'two', 'three', 'four', 'five',
28 'six', 'seven', 'eight']
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
)
38 wxCheckBox(self
, 20, "wxCheckBox", wxPoint(80, y_pos
), wxSize(150, 20))
39 EVT_CHECKBOX(self
, 20, self
.EvtCheckBox
)
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
48 wxStaticText(self
, -1, "wxChoice", wxPoint(5, y_pos
), wxSize(75, 20))
49 wxChoice(self
, 40, wxPoint(80, y_pos
), wxSize(95, 20), #wxPyDefaultSize,
51 EVT_CHOICE(self
, 40, self
.EvtChoice
)
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
)
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
)
66 width
, height
= lb
.GetSize()
67 y_pos
= y_pos
+ height
+ 5
72 wxButton(self
, wxID_OK
, ' OK ', wxPoint(80, y_pos
), wxPyDefaultSize
).SetDefault()
73 wxButton(self
, wxID_CANCEL
, ' Cancel ', wxPoint(140, y_pos
))
76 def EvtText(self
, event
):
77 self
.log
.WriteText('EvtText: %s\n' % event
.GetString())
79 def EvtCheckBox(self
, event
):
80 self
.log
.WriteText('EvtCheckBox: %d\n' % event
.GetInt())
82 def EvtRadioBox(self
, event
):
83 self
.log
.WriteText('EvtRadioBox: %d\n' % event
.GetInt())
85 def EvtChoice(self
, event
):
86 self
.log
.WriteText('EvtChoice: %s\n' % event
.GetString())
88 def EvtComboBox(self
, event
):
89 self
.log
.WriteText('EvtComboBox: %s\n' % event
.GetString())
91 def EvtListBox(self
, event
):
92 self
.log
.WriteText('EvtListBox: %s\n' % event
.GetString())
94 def EvtListBoxDClick(self
, event
):
95 self
.log
.WriteText('EvtListBoxDClick:\n')
99 #---------------------------------------------------------------------------
101 class TestTimer(wxTimer
):
102 def __init__(self
, log
):
103 wxTimer
.__init
__(self
)
108 self
.log
.WriteText('beep!\n')
111 #---------------------------------------------------------------------------
113 class TestLayoutConstraints(wxFrame
):
114 def __init__(self
, parent
):
115 wxFrame
.__init
__(self
, parent
, -1, 'Test Layout Constraints',
116 wxPyDefaultPosition
, wxSize(500, 300))
118 self
.SetAutoLayout(true
)
119 EVT_BUTTON(self
, 100, self
.OnButton
)
121 self
.panelA
= wxWindow(self
, -1, wxPyDefaultPosition
, wxPyDefaultSize
,
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
)
131 self
.panelB
= wxWindow(self
, -1, wxPyDefaultPosition
, wxPyDefaultSize
,
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
)
141 self
.panelC
= wxWindow(self
, -1, wxPyDefaultPosition
, wxPyDefaultSize
,
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
)
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
)
156 lc
.width
.PercentOf (self
.panelA
, wxWidth
, 50)
157 b
.SetConstraints(lc
);
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)
165 b
.SetConstraints(lc
);
167 self
.panelD
= wxWindow(self
.panelC
, -1, wxPyDefaultPosition
, wxPyDefaultSize
,
169 self
.panelD
.SetBackgroundColour(wxGREEN
)
170 wxStaticText(self
.panelD
, -1, "Panel D", wxPoint(4, 4)).SetBackgroundColour(wxGREEN
)
172 b
= wxButton(self
.panelC
, 100, ' Panel C ')
173 lc
= wxLayoutConstraints()
174 lc
.top
.Below (self
.panelD
)
175 lc
.left
.RightOf (self
.panelD
)
178 b
.SetConstraints(lc
);
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
);
188 def OnButton(self
, event
):
192 def OnCloseWindow(self
, event
):
196 #---------------------------------------------------------------------------
198 class TestGrid(wxFrame
):
199 def __init__(self
, parent
):
200 wxFrame
.__init
__(self
, parent
, -1, 'Test Grid',
201 wxPyDefaultPosition
, wxSize(500, 300))
203 grid
= wxGrid(self
, -1)
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()
218 def OnCloseWindow(self
, event
):
222 #---------------------------------------------------------------------------
224 class TestNotebookWindow(wxFrame
):
225 def __init__(self
, parent
):
226 wxFrame
.__init
__(self
, parent
, -1, 'Test wxNotebook',
227 wxPyDefaultPosition
, wxPyDefaultSize
)
229 nb
= wxNotebook(self
, -1)
231 win
= wxWindow(nb
, -1, wxPyDefaultPosition
, wxPyDefaultSize
, wxRAISED_BORDER
)
232 win
.SetBackgroundColour(wxBLUE
)
233 nb
.AddPage(win
, "Blue")
235 win
= wxWindow(nb
, -1, wxPyDefaultPosition
, wxPyDefaultSize
, wxRAISED_BORDER
)
236 win
.SetBackgroundColour(wxRED
)
237 nb
.AddPage(win
, "Red")
239 win
= wxWindow(nb
, -1, wxPyDefaultPosition
, wxPyDefaultSize
, wxRAISED_BORDER
)
240 win
.SetBackgroundColour(wxGREEN
)
241 nb
.AddPage(win
, "Green")
243 win
= wxWindow(nb
, -1, wxPyDefaultPosition
, wxPyDefaultSize
, wxRAISED_BORDER
)
244 win
.SetBackgroundColour(wxCYAN
)
245 nb
.AddPage(win
, "Cyan")
247 win
= wxWindow(nb
, -1, wxPyDefaultPosition
, wxPyDefaultSize
, wxRAISED_BORDER
)
248 win
.SetBackgroundColour(wxWHITE
)
249 nb
.AddPage(win
, "White")
251 win
= wxWindow(nb
, -1, wxPyDefaultPosition
, wxPyDefaultSize
, wxRAISED_BORDER
)
252 win
.SetBackgroundColour(wxBLACK
)
253 nb
.AddPage(win
, "Black")
255 win
= wxWindow(nb
, -1, wxPyDefaultPosition
, wxPyDefaultSize
, wxRAISED_BORDER
)
256 win
.SetBackgroundColour(wxNamedColour('MIDNIGHT BLUE'))
257 nb
.AddPage(win
, "MIDNIGHT BLUE")
259 win
= wxWindow(nb
, -1, wxPyDefaultPosition
, wxPyDefaultSize
, wxRAISED_BORDER
)
260 win
.SetBackgroundColour(wxNamedColour('INDIAN RED'))
261 nb
.AddPage(win
, "INDIAN RED")
265 self
.SetSize(wxSize(500, 300)) # force a redraw so the notebook will draw
268 def OnCloseWindow(self
, event
):
271 #---------------------------------------------------------------------------
273 class AppFrame(wxFrame
):
274 def __init__(self
, parent
, id, title
):
275 wxFrame
.__init
__(self
, parent
, id, title
, wxPyDefaultPosition
,
277 if wxPlatform
== '__WXMSW__':
278 self
.icon
= wxIcon('bitmaps/mondrian.ico', wxBITMAP_TYPE_ICO
)
279 self
.SetIcon(self
.icon
)
281 self
.mainmenu
= wxMenuBar()
283 menu
.Append(200, 'E&xit', 'Get the heck outta here!')
284 EVT_MENU(self
, 200, self
.OnFileExit
)
285 self
.mainmenu
.Append(menu
, '&File')
287 menu
= self
.MakeTestsMenu()
288 self
.mainmenu
.Append(menu
, '&Tests')
289 self
.SetMenuBar(self
.mainmenu
)
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')
297 def MakeTestsMenu(self
):
301 menu
.Append(mID
, '&Simple Controls')
302 EVT_MENU(self
, mID
, self
.OnTestSimpleControls
)
305 menu
.Append(mID
, '&Timer', '', true
)
306 EVT_MENU(self
, mID
, self
.OnTestTimer
)
311 menu
.Append(mID
, '&Layout Constraints')
312 EVT_MENU(self
, mID
, self
.OnTestLayoutConstraints
)
315 menu
.Append(mID
, '&Grid')
316 EVT_MENU(self
, mID
, self
.OnTestGrid
)
319 smenu
= wxMenu() # make a sub-menu
322 smenu
.Append(mID
, '&Colour')
323 EVT_MENU(self
, mID
, self
.OnTestColourDlg
)
326 smenu
.Append(mID
, '&Directory')
327 EVT_MENU(self
, mID
, self
.OnTestDirDlg
)
330 smenu
.Append(mID
, '&File')
331 EVT_MENU(self
, mID
, self
.OnTestFileDlg
)
334 smenu
.Append(mID
, '&Single Choice')
335 EVT_MENU(self
, mID
, self
.OnTestSingleChoiceDlg
)
338 smenu
.Append(mID
, '&TextEntry')
339 EVT_MENU(self
, mID
, self
.OnTestTextEntryDlg
)
342 smenu
.Append(mID
, '&Font')
343 EVT_MENU(self
, mID
, self
.OnTestFontDlg
)
346 smenu
.Append(mID
, '&PageSetup')
347 EVT_MENU(self
, mID
, self
.OnTestPageSetupDlg
)
350 smenu
.Append(mID
, '&Print')
351 EVT_MENU(self
, mID
, self
.OnTestPrintDlg
)
354 smenu
.Append(mID
, '&Message')
355 EVT_MENU(self
, mID
, self
.OnTestMessageDlg
)
358 menu
.AppendMenu(NewId(), '&Common Dialogs', smenu
)
362 menu
.Append(mID
, '&Notebook')
363 EVT_MENU(self
, mID
, self
.OnTestNotebook
)
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))
377 def OnFileExit(self
, event
):
380 def OnCloseWindow(self
, event
):
386 def OnTestSimpleControls(self
, event
):
387 dlg
= TestSimpleControlsDlg(self
, self
)
392 def OnTestTimer(self
, event
):
394 self
.mainmenu
.Check(self
.timerID
, false
)
398 self
.mainmenu
.Check(self
.timerID
, true
)
399 self
.timer
= TestTimer(self
)
400 self
.timer
.Start(1000)
402 def OnTestLayoutConstraints(self
, event
):
403 win
= TestLayoutConstraints(self
)
406 def OnTestGrid(self
, event
):
409 win
.SetSize(wxSize(505, 300)) # have to force a resize, or the grid doesn't
410 # show up for some reason....
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()))
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())
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())
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())
441 def OnTestTextEntryDlg(self
, event
):
442 dlg
= wxTextEntryDialog(self
, 'What is your favorite programming language?',
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())
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()))
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
)))
473 def OnTestPrintDlg(self
, event
):
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')
483 def OnTestMessageDlg(self
, event
):
484 dlg
= wxMessageDialog(self
, 'Hello from Python and wxWindows!',
485 'A Message Box', wxOK | wxICON_INFORMATION
)
490 def OnTestNotebook(self
, event
):
491 win
= TestNotebookWindow(self
)
494 #---------------------------------------------------------------------------
499 frame
= AppFrame(NULL
, -1, "Test 4: (lots of little tests...)")
501 self
.SetTopWindow(frame
)
504 #---------------------------------------------------------------------------
516 if __name__
== '__main__':
520 #----------------------------------------------------------------------------
523 # Revision 1.2 1998/08/22 19:51:18 RD
524 # some tweaks for wxGTK
526 # Revision 1.1 1998/08/09 08:28:05 RD