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