]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxTimeCtrl.py
fixed too small pagesize
[wxWidgets.git] / wxPython / demo / wxTimeCtrl.py
1 from wxPython.wx import *
2 from wxPython.lib.timectrl import *
3 from wxPython.lib.timectrl import __doc__ as overviewdoc
4 from wxPython.lib.scrolledpanel import wxScrolledPanel
5
6 #----------------------------------------------------------------------
7
8 class TestPanel( wxScrolledPanel ):
9 def __init__( self, parent, log ):
10
11 wxScrolledPanel.__init__( self, parent, -1 )
12 self.log = log
13
14
15 text1 = wxStaticText( self, -1, "12-hour format:")
16 self.time12 = wxTimeCtrl( self, -1, name="12 hour control" )
17 spin1 = wxSpinButton( self, -1, wxDefaultPosition, wxSize(-1,20), 0 )
18 self.time12.BindSpinButton( spin1 )
19
20 text2 = wxStaticText( self, -1, "24-hour format:")
21 spin2 = wxSpinButton( self, -1, wxDefaultPosition, wxSize(-1,20), 0 )
22 self.time24 = wxTimeCtrl( self, -1, name="24 hour control", fmt24hr=True, spinButton = spin2 )
23
24 text3 = wxStaticText( self, -1, "No seconds\nor spin button:")
25 self.spinless_ctrl = wxTimeCtrl( self, -1, name="spinless control", display_seconds = False )
26
27 grid = wxFlexGridSizer( 0, 2, 10, 5 )
28 grid.Add( text1, 0, wxALIGN_RIGHT )
29 hbox1 = wxBoxSizer( wxHORIZONTAL )
30 hbox1.Add( self.time12, 0, wxALIGN_CENTRE )
31 hbox1.Add( spin1, 0, wxALIGN_CENTRE )
32 grid.Add( hbox1, 0, wxLEFT )
33
34 grid.Add( text2, 0, wxALIGN_RIGHT|wxTOP|wxBOTTOM )
35 hbox2 = wxBoxSizer( wxHORIZONTAL )
36 hbox2.Add( self.time24, 0, wxALIGN_CENTRE )
37 hbox2.Add( spin2, 0, wxALIGN_CENTRE )
38 grid.Add( hbox2, 0, wxLEFT )
39
40 grid.Add( text3, 0, wxALIGN_RIGHT|wxTOP|wxBOTTOM )
41 grid.Add( self.spinless_ctrl, 0, wxLEFT )
42
43
44 buttonChange = wxButton( self, -1, "Change Controls")
45 self.radio12to24 = wxRadioButton( self, -1, "Copy 12-hour time to 24-hour control", wxDefaultPosition, wxDefaultSize, wxRB_GROUP )
46 self.radio24to12 = wxRadioButton( self, -1, "Copy 24-hour time to 12-hour control")
47 self.radioWx = wxRadioButton( self, -1, "Set controls to 'now' using wxDateTime")
48 self.radioMx = wxRadioButton( self, -1, "Set controls to 'now' using mxDateTime")
49
50 radio_vbox = wxBoxSizer( wxVERTICAL )
51 radio_vbox.Add( self.radio12to24, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 )
52 radio_vbox.Add( self.radio24to12, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 )
53 radio_vbox.Add( self.radioWx, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 )
54 radio_vbox.Add( self.radioMx, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 )
55
56 box_label = wxStaticBox( self, -1, "Change Controls through API" )
57 buttonbox = wxStaticBoxSizer( box_label, wxHORIZONTAL )
58 buttonbox.Add( buttonChange, 0, wxALIGN_CENTRE|wxALL, 5 )
59 buttonbox.Add( radio_vbox, 0, wxALIGN_CENTRE|wxALL, 5 )
60
61 hbox = wxBoxSizer( wxHORIZONTAL )
62 hbox.Add( grid, 0, wxALIGN_LEFT|wxALL, 15 )
63 hbox.Add( buttonbox, 0, wxALIGN_RIGHT|wxBOTTOM, 20 )
64
65
66 box_label = wxStaticBox( self, -1, "Bounds Control" )
67 boundsbox = wxStaticBoxSizer( box_label, wxHORIZONTAL )
68 self.set_bounds = wxCheckBox( self, -1, "Set time bounds:" )
69
70 minlabel = wxStaticText( self, -1, "minimum time:" )
71 self.min = wxTimeCtrl( self, -1, name="min", display_seconds = False )
72 self.min.Enable( False )
73
74 maxlabel = wxStaticText( self, -1, "maximum time:" )
75 self.max = wxTimeCtrl( self, -1, name="max", display_seconds = False )
76 self.max.Enable( False )
77
78 self.limit_check = wxCheckBox( self, -1, "Limit control" )
79
80 label = wxStaticText( self, -1, "Resulting time control:" )
81 self.target_ctrl = wxTimeCtrl( self, -1, name="new" )
82
83 grid2 = wxFlexGridSizer( 0, 2, 0, 0 )
84 grid2.Add( 20, 0, 0, wxALIGN_LEFT|wxALL, 5 )
85 grid2.Add( 20, 0, 0, wxALIGN_LEFT|wxALL, 5 )
86
87 grid2.Add( self.set_bounds, 0, wxALIGN_LEFT|wxALL, 5 )
88 grid3 = wxFlexGridSizer( 0, 2, 5, 5 )
89 grid3.Add(minlabel, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL )
90 grid3.Add( self.min, 0, wxALIGN_LEFT )
91 grid3.Add(maxlabel, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL )
92 grid3.Add( self.max, 0, wxALIGN_LEFT )
93 grid2.Add(grid3, 0, wxALIGN_LEFT )
94
95 grid2.Add( self.limit_check, 0, wxALIGN_LEFT|wxALL, 5 )
96 grid2.Add( 20, 0, 0, wxALIGN_LEFT|wxALL, 5 )
97
98 grid2.Add( 20, 0, 0, wxALIGN_LEFT|wxALL, 5 )
99 grid2.Add( 20, 0, 0, wxALIGN_LEFT|wxALL, 5 )
100 grid2.Add( label, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxALL, 5 )
101 grid2.Add( self.target_ctrl, 0, wxALIGN_LEFT|wxALL, 5 )
102 boundsbox.Add(grid2, 0, wxALIGN_CENTER|wxEXPAND|wxALL, 5)
103
104 vbox = wxBoxSizer( wxVERTICAL )
105 vbox.AddSpacer(20, 20)
106 vbox.Add( hbox, 0, wxALIGN_LEFT|wxALL, 5)
107 vbox.Add( boundsbox, 0, wxALIGN_LEFT|wxALL, 5 )
108
109
110 outer_box = wxBoxSizer( wxVERTICAL )
111 outer_box.Add( vbox, 0, wxALIGN_LEFT|wxALL, 5)
112
113
114 # Turn on mxDateTime option only if we can import the module:
115 try:
116 from mx import DateTime
117 except ImportError:
118 self.radioMx.Enable( False )
119
120
121 self.SetAutoLayout( True )
122 self.SetSizer( outer_box )
123 outer_box.Fit( self )
124 self.SetupScrolling()
125
126 EVT_BUTTON( self, buttonChange.GetId(), self.OnButtonClick )
127 EVT_TIMEUPDATE( self, self.time12.GetId(), self.OnTimeChange )
128 EVT_TIMEUPDATE( self, self.time24.GetId(), self.OnTimeChange )
129 EVT_TIMEUPDATE( self, self.spinless_ctrl.GetId(), self.OnTimeChange )
130
131
132 EVT_CHECKBOX( self, self.set_bounds.GetId(), self.OnBoundsCheck )
133 EVT_CHECKBOX( self, self.limit_check.GetId(), self.SetTargetMinMax )
134 EVT_TIMEUPDATE( self, self.min.GetId(), self.SetTargetMinMax )
135 EVT_TIMEUPDATE( self, self.max.GetId(), self.SetTargetMinMax )
136 EVT_TIMEUPDATE( self, self.target_ctrl.GetId(), self.OnTimeChange )
137
138
139
140 def OnTimeChange( self, event ):
141 timectrl = self.FindWindowById( event.GetId() )
142 ib_str = [ " (out of bounds)", "" ]
143
144 self.log.write('%s time = %s%s\n' % ( timectrl.GetName(), timectrl.GetValue(), ib_str[ timectrl.IsInBounds() ] ) )
145
146
147 def OnButtonClick( self, event ):
148 if self.radio12to24.GetValue():
149 self.time24.SetValue( self.time12.GetValue() )
150
151 elif self.radio24to12.GetValue():
152 self.time12.SetValue( self.time24.GetValue() )
153
154 elif self.radioWx.GetValue():
155 now = wxDateTime_Now()
156 self.time12.SetValue( now )
157 # (demonstrates that G/SetValue returns/takes a wxDateTime)
158 self.time24.SetValue( self.time12.GetValue(as_wxDateTime=True) )
159
160 # (demonstrates that G/SetValue returns/takes a wxTimeSpan)
161 self.spinless_ctrl.SetValue( self.time12.GetValue(as_wxTimeSpan=True) )
162
163 elif self.radioMx.GetValue():
164 from mx import DateTime
165 now = DateTime.now()
166 self.time12.SetValue( now )
167
168 # (demonstrates that G/SetValue returns/takes a DateTime)
169 self.time24.SetValue( self.time12.GetValue(as_mxDateTime=True) )
170
171 # (demonstrates that G/SetValue returns/takes a DateTimeDelta)
172 self.spinless_ctrl.SetValue( self.time12.GetValue(as_mxDateTimeDelta=True) )
173
174
175 def OnBoundsCheck( self, event ):
176 self.min.Enable( self.set_bounds.GetValue() )
177 self.max.Enable( self.set_bounds.GetValue() )
178 self.SetTargetMinMax()
179
180
181 def SetTargetMinMax( self, event=None ):
182 min = max = None
183
184 if self.set_bounds.GetValue():
185 min = self.min.GetWxDateTime()
186 max = self.max.GetWxDateTime()
187 else:
188 min, max = None, None
189
190 cur_min, cur_max = self.target_ctrl.GetBounds()
191
192 if min != cur_min: self.target_ctrl.SetMin( min )
193 if max != cur_max: self.target_ctrl.SetMax( max )
194
195 self.target_ctrl.SetLimited( self.limit_check.GetValue() )
196
197 if min != cur_min or max != cur_max:
198 new_min, new_max = self.target_ctrl.GetBounds()
199 if new_min and new_max:
200 self.log.write( "current min, max: (%s, %s)\n" % ( new_min.FormatTime(), new_max.FormatTime() ) )
201 else:
202 self.log.write( "current min, max: (None, None)\n" )
203
204 #----------------------------------------------------------------------
205
206 def runTest( frame, nb, log ):
207 win = TestPanel( nb, log )
208 return win
209
210 #----------------------------------------------------------------------
211
212 overview = overviewdoc
213
214 if __name__ == '__main__':
215 import sys,os
216 import run
217 run.main(['', os.path.basename(sys.argv[0])])
218