]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxTimeCtrl.py
Fixed a typo
[wxWidgets.git] / wxPython / demo / wxTimeCtrl.py
1 # 11/21/2003 - Jeff Grimmett (grimmtooth@softhome.net)
2 #
3 # o Updated for wx namespace
4 #
5 # 11/21/2003 - Jeff Grimmett (grimmtooth@softhome.net)
6 #
7 # o wx renamer needed for timectrl lib
8 # o presense of spin control causing probs (see spin ctrl demo for details)
9 #
10 # 12/13/2003 - Jeff Grimmett (grimmtooth@softhome.net)
11 #
12 # o New binders applied. Issues still exist.
13 #
14 # 12/20/2003 - Jeff Grimmett (grimmtooth@softhome.net)
15 #
16 # o wxTimeCtrl -> TimeCtrl
17 # o wxScrolledPanel -> ScrolledPanel
18 #
19
20 import wx
21 import wx.lib.timectrl as timectl
22 import wx.lib.scrolledpanel as scrolled
23
24 #----------------------------------------------------------------------
25
26 class TestPanel( scrolled.ScrolledPanel ):
27 def __init__( self, parent, log ):
28
29 scrolled.ScrolledPanel.__init__( self, parent, -1 )
30 self.log = log
31
32
33 text1 = wx.StaticText( self, -1, "12-hour format:")
34 self.time12 = timectl.TimeCtrl( self, -1, name="12 hour control" )
35 spin1 = wx.SpinButton( self, -1, wx.DefaultPosition, (-1,20), 0 )
36 self.time12.BindSpinButton( spin1 )
37
38 text2 = wx.StaticText( self, -1, "24-hour format:")
39 spin2 = wx.SpinButton( self, -1, wx.DefaultPosition, (-1,20), 0 )
40 self.time24 = timectl.TimeCtrl(
41 self, -1, name="24 hour control", fmt24hr=True,
42 spinButton = spin2
43 )
44
45 text3 = wx.StaticText( self, -1, "No seconds\nor spin button:")
46 self.spinless_ctrl = timectl.TimeCtrl(
47 self, -1, name="spinless control",
48 display_seconds = False
49 )
50
51 grid = wx.FlexGridSizer( 0, 2, 10, 5 )
52 grid.Add( text1, 0, wx.ALIGN_RIGHT )
53 hbox1 = wx.BoxSizer( wx.HORIZONTAL )
54 hbox1.Add( self.time12, 0, wx.ALIGN_CENTRE )
55 hbox1.Add( spin1, 0, wx.ALIGN_CENTRE )
56 grid.Add( hbox1, 0, wx.LEFT )
57
58 grid.Add( text2, 0, wx.ALIGN_RIGHT|wx.TOP|wx.BOTTOM )
59 hbox2 = wx.BoxSizer( wx.HORIZONTAL )
60 hbox2.Add( self.time24, 0, wx.ALIGN_CENTRE )
61 hbox2.Add( spin2, 0, wx.ALIGN_CENTRE )
62 grid.Add( hbox2, 0, wx.LEFT )
63
64 grid.Add( text3, 0, wx.ALIGN_RIGHT|wx.TOP|wx.BOTTOM )
65 grid.Add( self.spinless_ctrl, 0, wx.LEFT )
66
67
68 buttonChange = wx.Button( self, -1, "Change Controls")
69 self.radio12to24 = wx.RadioButton(
70 self, -1, "Copy 12-hour time to 24-hour control",
71 wx.DefaultPosition, wx.DefaultSize, wx.RB_GROUP
72 )
73
74 self.radio24to12 = wx.RadioButton(
75 self, -1, "Copy 24-hour time to 12-hour control"
76 )
77
78 self.radioWx = wx.RadioButton( self, -1, "Set controls to 'now' using wxDateTime")
79 self.radioMx = wx.RadioButton( self, -1, "Set controls to 'now' using mxDateTime")
80
81 radio_vbox = wx.BoxSizer( wx.VERTICAL )
82 radio_vbox.Add( self.radio12to24, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
83 radio_vbox.Add( self.radio24to12, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
84 radio_vbox.Add( self.radioWx, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
85 radio_vbox.Add( self.radioMx, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
86
87 box_label = wx.StaticBox( self, -1, "Change Controls through API" )
88 buttonbox = wx.StaticBoxSizer( box_label, wx.HORIZONTAL )
89 buttonbox.Add( buttonChange, 0, wx.ALIGN_CENTRE|wx.ALL, 5 )
90 buttonbox.Add( radio_vbox, 0, wx.ALIGN_CENTRE|wx.ALL, 5 )
91
92 hbox = wx.BoxSizer( wx.HORIZONTAL )
93 hbox.Add( grid, 0, wx.ALIGN_LEFT|wx.ALL, 15 )
94 hbox.Add( buttonbox, 0, wx.ALIGN_RIGHT|wx.BOTTOM, 20 )
95
96
97 box_label = wx.StaticBox( self, -1, "Bounds Control" )
98 boundsbox = wx.StaticBoxSizer( box_label, wx.HORIZONTAL )
99 self.set_bounds = wx.CheckBox( self, -1, "Set time bounds:" )
100
101 minlabel = wx.StaticText( self, -1, "minimum time:" )
102 self.min = timectl.TimeCtrl( self, -1, name="min", display_seconds = False )
103 self.min.Enable( False )
104
105 maxlabel = wx.StaticText( self, -1, "maximum time:" )
106 self.max = timectl.TimeCtrl( self, -1, name="max", display_seconds = False )
107 self.max.Enable( False )
108
109 self.limit_check = wx.CheckBox( self, -1, "Limit control" )
110
111 label = wx.StaticText( self, -1, "Resulting time control:" )
112 self.target_ctrl = timectl.TimeCtrl( self, -1, name="new" )
113
114 grid2 = wx.FlexGridSizer( 0, 2, 0, 0 )
115 grid2.Add( (20, 0), 0, wx.ALIGN_LEFT|wx.ALL, 5 )
116 grid2.Add( (20, 0), 0, wx.ALIGN_LEFT|wx.ALL, 5 )
117
118 grid2.Add( self.set_bounds, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
119 grid3 = wx.FlexGridSizer( 0, 2, 5, 5 )
120 grid3.Add(minlabel, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL )
121 grid3.Add( self.min, 0, wx.ALIGN_LEFT )
122 grid3.Add(maxlabel, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL )
123 grid3.Add( self.max, 0, wx.ALIGN_LEFT )
124 grid2.Add(grid3, 0, wx.ALIGN_LEFT )
125
126 grid2.Add( self.limit_check, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
127 grid2.Add( (20, 0), 0, wx.ALIGN_LEFT|wx.ALL, 5 )
128
129 grid2.Add( (20, 0), 0, wx.ALIGN_LEFT|wx.ALL, 5 )
130 grid2.Add( (20, 0), 0, wx.ALIGN_LEFT|wx.ALL, 5 )
131 grid2.Add( label, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
132 grid2.Add( self.target_ctrl, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
133 boundsbox.Add(grid2, 0, wx.ALIGN_CENTER|wx.EXPAND|wx.ALL, 5)
134
135 vbox = wx.BoxSizer( wx.VERTICAL )
136 vbox.Add( (20, 20) )
137 vbox.Add( hbox, 0, wx.ALIGN_LEFT|wx.ALL, 5)
138 vbox.Add( boundsbox, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
139
140
141 outer_box = wx.BoxSizer( wx.VERTICAL )
142 outer_box.Add( vbox, 0, wx.ALIGN_LEFT|wx.ALL, 5)
143
144
145 # Turn on mxDateTime option only if we can import the module:
146 try:
147 from mx import DateTime
148 except ImportError:
149 self.radioMx.Enable( False )
150
151
152 self.SetAutoLayout( True )
153 self.SetSizer( outer_box )
154 outer_box.Fit( self )
155 self.SetupScrolling()
156
157 self.Bind(wx.EVT_BUTTON, self.OnButtonClick, buttonChange )
158 self.Bind(timectl.EVT_TIMEUPDATE, self.OnTimeChange, self.time12 )
159 self.Bind(timectl.EVT_TIMEUPDATE, self.OnTimeChange, self.time24 )
160 self.Bind(timectl.EVT_TIMEUPDATE, self.OnTimeChange, self.spinless_ctrl )
161 self.Bind(wx.EVT_CHECKBOX, self.OnBoundsCheck, self.set_bounds )
162 self.Bind(wx.EVT_CHECKBOX, self.SetTargetMinMax, self.limit_check )
163 self.Bind(timectl.EVT_TIMEUPDATE, self.SetTargetMinMax, self.min )
164 self.Bind(timectl.EVT_TIMEUPDATE, self.SetTargetMinMax, self.max )
165 self.Bind(timectl.EVT_TIMEUPDATE, self.OnTimeChange, self.target_ctrl )
166
167
168 def OnTimeChange( self, event ):
169 timectrl = self.FindWindowById( event.GetId() )
170 ib_str = [ " (out of bounds)", "" ]
171
172 self.log.write('%s time = %s%s\n' % ( timectrl.GetName(), timectrl.GetValue(), ib_str[ timectrl.IsInBounds() ] ) )
173
174
175 def OnButtonClick( self, event ):
176 if self.radio12to24.GetValue():
177 self.time24.SetValue( self.time12.GetValue() )
178
179 elif self.radio24to12.GetValue():
180 self.time12.SetValue( self.time24.GetValue() )
181
182 elif self.radioWx.GetValue():
183 now = wx.DateTime_Now()
184 self.time12.SetValue( now )
185 # (demonstrates that G/SetValue returns/takes a wxDateTime)
186 self.time24.SetValue( self.time12.GetValue(as_wxDateTime=True) )
187
188 # (demonstrates that G/SetValue returns/takes a wxTimeSpan)
189 self.spinless_ctrl.SetValue( self.time12.GetValue(as_wxTimeSpan=True) )
190
191 elif self.radioMx.GetValue():
192 from mx import DateTime
193 now = DateTime.now()
194 self.time12.SetValue( now )
195
196 # (demonstrates that G/SetValue returns/takes a DateTime)
197 self.time24.SetValue( self.time12.GetValue(as_mxDateTime=True) )
198
199 # (demonstrates that G/SetValue returns/takes a DateTimeDelta)
200 self.spinless_ctrl.SetValue( self.time12.GetValue(as_mxDateTimeDelta=True) )
201
202
203 def OnBoundsCheck( self, event ):
204 self.min.Enable( self.set_bounds.GetValue() )
205 self.max.Enable( self.set_bounds.GetValue() )
206 self.SetTargetMinMax()
207
208
209 def SetTargetMinMax( self, event=None ):
210 min = None
211 max = None
212
213 if self.set_bounds.GetValue():
214 min = self.min.GetWxDateTime()
215 max = self.max.GetWxDateTime()
216 else:
217 min, max = None, None
218
219 cur_min, cur_max = self.target_ctrl.GetBounds()
220
221 # jmg - A little expirimental change to ensure that min
222 # or max contain valid values before we use them
223 if min and (min != cur_min): self.target_ctrl.SetMin( min )
224 if max and (max != cur_max): self.target_ctrl.SetMax( max )
225
226 self.target_ctrl.SetLimited( self.limit_check.GetValue() )
227
228 if min != cur_min or max != cur_max:
229 new_min, new_max = self.target_ctrl.GetBounds()
230
231 if new_min and new_max:
232 self.log.write( "current min, max: (%s, %s)\n" % ( new_min.FormatTime(), new_max.FormatTime() ) )
233 else:
234 self.log.write( "current min, max: (None, None)\n" )
235
236 #----------------------------------------------------------------------
237
238 def runTest( frame, nb, log ):
239 win = TestPanel( nb, log )
240 return win
241
242 #----------------------------------------------------------------------
243
244 overview = timectl.__doc__
245
246 if __name__ == '__main__':
247 import sys,os
248 import run
249 run.main(['', os.path.basename(sys.argv[0])])
250