]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxIntCtrl.py
Swtch to using Bind, and use better styles for the code viewer
[wxWidgets.git] / wxPython / demo / wxIntCtrl.py
1 # 11/19/2003 - Jeff Grimmett (grimmtooth@softhome.net)
2 #
3 # o Updated for wx namespace
4 #
5 # 11/29/2003 - Jeff Grimmett (grimmtooth@softhome.net)
6 #
7 # o intctrl needs the renamer applied.
8 # o intctrl needs new event binders.
9 #
10 # 12/08/2003 - Jeff Grimmett (grimmtooth@softhome.net)
11 #
12 # o All issues corrected
13 #
14
15 import wx
16 import wx.lib.intctrl as intctrl
17
18 #----------------------------------------------------------------------
19
20 class TestPanel( wx.Panel ):
21 def __init__( self, parent, log ):
22
23 wx.Panel.__init__( self, parent, -1 )
24 self.log = log
25 panel = wx.Panel( self, -1 )
26
27 self.set_min = wx.CheckBox( panel, -1, "Set minimum value:" )
28 self.min = intctrl.wxIntCtrl( panel, size=( 50, -1 ) )
29 self.min.Enable( False )
30
31 self.set_max = wx.CheckBox( panel, -1, "Set maximum value:" )
32 self.max = intctrl.wxIntCtrl( panel, size=( 50, -1 ) )
33 self.max.Enable( False )
34
35 self.limit_target = wx.CheckBox( panel, -1, "Limit control" )
36 self.allow_none = wx.CheckBox( panel, -1, "Allow empty control" )
37 self.allow_long = wx.CheckBox( panel, -1, "Allow long integers" )
38
39 label = wx.StaticText( panel, -1, "Resulting integer control:" )
40 self.target_ctl = intctrl.wxIntCtrl( panel )
41
42 grid = wx.FlexGridSizer( 0, 2, 0, 0 )
43 grid.Add( self.set_min, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
44 grid.Add( self.min, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
45
46 grid.Add(self.set_max, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
47 grid.Add( self.max, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
48
49 grid.Add( self.limit_target, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
50 grid.Add( (20, 0), 0, wx.ALIGN_LEFT|wx.ALL, 5 )
51 grid.Add( self.allow_none, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
52 grid.Add( (20, 0), 0, wx.ALIGN_LEFT|wx.ALL, 5 )
53 grid.Add( self.allow_long, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
54 grid.Add( (20, 0), 0, wx.ALIGN_LEFT|wx.ALL, 5 )
55
56 grid.Add( (20, 0), 0, wx.ALIGN_LEFT|wx.ALL, 5 )
57 grid.Add( (20, 0), 0, wx.ALIGN_LEFT|wx.ALL, 5 )
58
59 grid.Add( label, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
60 grid.Add( self.target_ctl, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
61
62 outer_box = wx.BoxSizer( wx.VERTICAL )
63 outer_box.AddSizer( grid, 0, wx.ALIGN_CENTRE|wx.ALL, 20 )
64
65 panel.SetAutoLayout( True )
66 panel.SetSizer( outer_box )
67 outer_box.Fit( panel )
68 panel.Move( (50,50) )
69 self.panel = panel
70
71 self.Bind(wx.EVT_CHECKBOX, self.OnSetMin, self.set_min)
72 self.Bind(wx.EVT_CHECKBOX, self.OnSetMax, self.set_max)
73 self.Bind(wx.EVT_CHECKBOX, self.SetTargetMinMax, self.limit_target)
74 self.Bind(wx.EVT_CHECKBOX, self.OnSetAllowNone, self.allow_none)
75 self.Bind(wx.EVT_CHECKBOX, self.OnSetAllowLong, self.allow_long)
76
77 self.Bind(intctrl.EVT_INT, self.SetTargetMinMax, self.min)
78 self.Bind(intctrl.EVT_INT, self.SetTargetMinMax, self.max)
79 self.Bind(intctrl.EVT_INT, self.OnTargetChange, self.target_ctl)
80
81
82 def OnSetMin( self, event ):
83 self.min.Enable( self.set_min.GetValue() )
84 self.SetTargetMinMax()
85
86 def OnSetMax( self, event ):
87 self.max.Enable( self.set_max.GetValue() )
88 self.SetTargetMinMax()
89
90
91 def SetTargetMinMax( self, event=None ):
92 min = max = None
93 self.target_ctl.SetLimited( self.limit_target.GetValue() )
94
95 if self.set_min.GetValue():
96 min = self.min.GetValue()
97
98 if self.set_max.GetValue():
99 max = self.max.GetValue()
100
101 cur_min, cur_max = self.target_ctl.GetBounds()
102
103 if min != cur_min and not self.target_ctl.SetMin( min ):
104 self.log.write( "min (%d) > current max (%d) -- bound not set\n" % ( min, self.target_ctl.GetMax() ) )
105 self.min.SetForegroundColour( wx.RED )
106 else:
107 self.min.SetForegroundColour( wx.BLACK )
108
109 self.min.Refresh()
110
111 if max != cur_max and not self.target_ctl.SetMax( max ):
112 self.log.write( "max (%d) < current min (%d) -- bound not set\n" % ( max, self.target_ctl.GetMin() ) )
113 self.max.SetForegroundColour( wx.RED )
114 else:
115 self.max.SetForegroundColour( wx.BLACK )
116
117 self.max.Refresh()
118
119 if min != cur_min or max != cur_max:
120 new_min, new_max = self.target_ctl.GetBounds()
121 self.log.write( "current min, max: (%s, %s)\n" % ( str(new_min), str(new_max) ) )
122
123
124 def OnSetAllowNone( self, event ):
125 self.target_ctl.SetNoneAllowed( self.allow_none.GetValue() )
126
127
128 def OnSetAllowLong( self, event ):
129 self.target_ctl.SetLongAllowed( self.allow_long.GetValue() )
130
131
132 def OnTargetChange( self, event ):
133 ctl = event.GetEventObject()
134 value = ctl.GetValue()
135 ib_str = [ " (out of bounds)", "" ]
136 self.log.write( "integer value = %s%s\n" % ( str(value), ib_str[ ctl.IsInBounds(value) ] ) )
137
138
139 #----------------------------------------------------------------------
140
141 def runTest( frame, nb, log ):
142 win = TestPanel( nb, log )
143 return win
144
145 #----------------------------------------------------------------------
146
147 overview = """<html><body>
148 <P>
149 <B>wxIntCtrl</B> provides a control that takes and returns integers as
150 value, and provides bounds support and optional value limiting.
151 <P>
152 <P>
153 Here's the API for wxIntCtrl:
154 <DL><PRE>
155 <B>wxIntCtrl</B>(
156 parent, id = -1,
157 <B>value</B> = 0,
158 <B>min</B> = None,
159 <B>max</B> = None,
160 <B>limited</B> = False,
161 <B>allow_none</B> = False,
162 <B>allow_long</B> = False,
163 <B>default_color</B> = wxBLACK,
164 <B>oob_color</B> = wxRED,
165 pos = wxDefaultPosition,
166 size = wxDefaultSize,
167 style = 0,
168 name = "integer")
169 </PRE>
170 <UL>
171 <DT><B>value</B>
172 <DD>If no initial value is set, the default will be zero, or
173 the minimum value, if specified. If an illegal string is specified,
174 a ValueError will result. (You can always later set the initial
175 value with SetValue() after instantiation of the control.)
176 <BR>
177 <DL><B>min</B>
178 <DD>The minimum value that the control should allow. This can be
179 adjusted with SetMin(). If the control is not limited, any value
180 below this bound will be colored with the current out-of-bounds color.
181 <BR>
182 <DT><B>max</B>
183 <DD>The maximum value that the control should allow. This can be
184 adjusted with SetMax(). If the control is not limited, any value
185 above this bound will be colored with the current out-of-bounds color.
186 <BR>
187 <DT><B>limited</B>
188 <DD>Boolean indicating whether the control prevents values from
189 exceeding the currently set minimum and maximum values (bounds).
190 If <I>False</I> and bounds are set, out-of-bounds values will
191 be colored with the current out-of-bounds color.
192 <BR>
193 <DT><B>allow_none</B>
194 <DD>Boolean indicating whether or not the control is allowed to be
195 empty, representing a value of <I>None</I> for the control.
196 <BR>
197 <DT><B>allow_long</B>
198 <DD>Boolean indicating whether or not the control is allowed to hold
199 and return a value of type long as well as int. If False, the
200 control will be implicitly limited to have a value such that
201 -sys.maxint-1 &lt;= n &lt;= sys.maxint.
202 <BR>
203 <DT><B>default_color</B>
204 <DD>Color value used for in-bounds values of the control.
205 <BR>
206 <DT><B>oob_color</B>
207 <DD>Color value used for out-of-bounds values of the control
208 when the bounds are set but the control is not limited.
209 </UL>
210 <BR>
211 <BR>
212 <DT><B>EVT_INT(win, id, func)</B>
213 <DD>Respond to a wxEVT_COMMAND_INT_UPDATED event, generated when the
214 value changes. Notice that this event will always be sent when the
215 control's contents changes - whether this is due to user input or
216 comes from the program itself (for example, if SetValue() is called.)
217 <BR>
218 <BR>
219 <DT><B>SetValue(int)</B>
220 <DD>Sets the value of the control to the integer value specified.
221 The resulting actual value of the control may be altered to
222 conform with the bounds set on the control if limited,
223 or colored if not limited but the value is out-of-bounds.
224 A ValueError exception will be raised if an invalid value
225 is specified.
226 <BR>
227 <DT><B>GetValue()</B>
228 <DD>Retrieves the integer value from the control. The value
229 retrieved will be sized as an int if possible or a long,
230 if necessary.
231 <BR>
232 <BR>
233 <DT><B>SetMin(min=None)</B>
234 <DD>Sets the expected minimum value, or lower bound, of the control.
235 (The lower bound will only be enforced if the control is
236 configured to limit its values to the set bounds.)
237 If a value of <I>None</I> is provided, then the control will have
238 no explicit lower bound. If the value specified is greater than
239 the current lower bound, then the function returns 0 and the
240 lower bound will not change from its current setting. On success,
241 the function returns 1.
242 <DT><DD>If successful and the current value is
243 lower than the new lower bound, if the control is limited, the
244 value will be automatically adjusted to the new minimum value;
245 if not limited, the value in the control will be colored with
246 the current out-of-bounds color.
247 <BR>
248 <DT><B>GetMin()</B>
249 <DD>Gets the current lower bound value for the control.
250 It will return None if no lower bound is currently specified.
251 <BR>
252 <BR>
253 <DT><B>SetMax(max=None)</B>
254 <DD>Sets the expected maximum value, or upper bound, of the control.
255 (The upper bound will only be enforced if the control is
256 configured to limit its values to the set bounds.)
257 If a value of <I>None</I> is provided, then the control will
258 have no explicit upper bound. If the value specified is less
259 than the current lower bound, then the function returns 0 and
260 the maximum will not change from its current setting. On success,
261 the function returns 1.
262 <DT><DD>If successful and the current value
263 is greater than the new upper bound, if the control is limited
264 the value will be automatically adjusted to the new maximum value;
265 if not limited, the value in the control will be colored with the
266 current out-of-bounds color.
267 <BR>
268 <DT><B>GetMax()</B>
269 <DD>Gets the current upper bound value for the control.
270 It will return None if no upper bound is currently specified.
271 <BR>
272 <BR>
273 <DT><B>SetBounds(min=None,max=None)</B>
274 <DD>This function is a convenience function for setting the min and max
275 values at the same time. The function only applies the maximum bound
276 if setting the minimum bound is successful, and returns True
277 only if both operations succeed. <B><I>Note:</I></B> leaving out an argument
278 will remove the corresponding bound.
279 <DT><B>GetBounds()</B>
280 <DD>This function returns a two-tuple (min,max), indicating the
281 current bounds of the control. Each value can be None if
282 that bound is not set.
283 <BR>
284 <BR>
285 <DT><B>IsInBounds(value=None)</B>
286 <DD>Returns <I>True</I> if no value is specified and the current value
287 of the control falls within the current bounds. This function can also
288 be called with a value to see if that value would fall within the current
289 bounds of the given control.
290 <BR>
291 <BR>
292 <DT><B>SetLimited(bool)</B>
293 <DD>If called with a value of True, this function will cause the control
294 to limit the value to fall within the bounds currently specified.
295 If the control's value currently exceeds the bounds, it will then
296 be limited accordingly.
297 If called with a value of 0, this function will disable value
298 limiting, but coloring of out-of-bounds values will still take
299 place if bounds have been set for the control.
300 <DT><B>IsLimited()</B>
301 <DD>Returns <I>True</I> if the control is currently limiting the
302 value to fall within the current bounds.
303 <BR>
304 <BR>
305 <DT><B>SetNoneAllowed(bool)</B>
306 <DD>If called with a value of True, this function will cause the control
307 to allow the value to be empty, representing a value of None.
308 If called with a value of false, this function will prevent the value
309 from being None. If the value of the control is currently None,
310 ie. the control is empty, then the value will be changed to that
311 of the lower bound of the control, or 0 if no lower bound is set.
312 <DT><B>IsNoneAllowed()</B>
313 <DD>Returns <I>True</I> if the control currently allows its
314 value to be None.
315 <BR>
316 <BR>
317 <DT><B>SetLongAllowed(bool)</B>
318 <DD>If called with a value of True, this function will cause the
319 control to allow the value to be a long. If called with a value
320 of False, and the value of the control is currently a long value,
321 the value of the control will be adjusted to fall within the
322 size of an integer type, at either the sys.maxint or -sys.maxint-1,
323 for positive and negative values, respectively.
324 <DT><B>IsLongAllowed()</B>
325 <DD>Returns <I>True</I> if the control currently allows its
326 value to be of type long.
327 <BR>
328 <BR>
329 <DT><B>SetColors(default_color=wxBLACK, oob_color=wxRED)</B>
330 <DD>Tells the control what colors to use for normal and out-of-bounds
331 values. If the value currently exceeds the bounds, it will be
332 recolored accordingly.
333 <DT><B>GetColors()</B>
334 <DD>Returns a tuple of <I>(default_color, oob_color)</I> indicating
335 the current color settings for the control.
336 <BR>
337 <BR>
338 <DT><B>Cut()</B>
339 <DD>Will allow cuts to the clipboard of the text portion of the value,
340 leaving the value of zero if the entire contents are "cut."
341 <DT><B>Paste()</B>
342 <DD>Will paste the contents of the clipboard to the selected portion
343 of the value; if the resulting string does not represent a legal
344 value, a ValueError will result. If the result is out-of bounds,
345 it will either be adjusted or colored as appropriate.
346 </DL>
347 </body></html>
348 """
349
350
351
352 if __name__ == '__main__':
353 import sys,os
354 import run
355 run.main(['', os.path.basename(sys.argv[0])])