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