1 from wxPython
.wx
import *
2 from wxPython
.lib
.intctrl
import *
4 #----------------------------------------------------------------------
6 class TestPanel( wxPanel
):
7 def __init__( self
, parent
, log
):
9 wxPanel
.__init
__( self
, parent
, -1 )
11 panel
= wxPanel( self
, -1 )
13 self
.set_min
= wxCheckBox( panel
, -1, "Set minimum value:" )
14 self
.min = wxIntCtrl( panel
, size
=wxSize( 50, -1 ) )
15 self
.min.Enable( False )
17 self
.set_max
= wxCheckBox( panel
, -1, "Set maximum value:" )
18 self
.max = wxIntCtrl( panel
, size
=wxSize( 50, -1 ) )
19 self
.max.Enable( False )
21 self
.limit_target
= wxCheckBox( panel
, -1, "Limit control" )
22 self
.allow_none
= wxCheckBox( panel
, -1, "Allow empty control" )
23 self
.allow_long
= wxCheckBox( panel
, -1, "Allow long integers" )
25 label
= wxStaticText( panel
, -1, "Resulting integer control:" )
26 self
.target_ctl
= wxIntCtrl( panel
)
28 grid
= wxFlexGridSizer( 0, 2, 0, 0 )
29 grid
.AddWindow( self
.set_min
, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxALL
, 5 )
30 grid
.AddWindow( self
.min, 0, wxALIGN_LEFT|wxALL
, 5 )
32 grid
.AddWindow( self
.set_max
, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxALL
, 5 )
33 grid
.AddWindow( self
.max, 0, wxALIGN_LEFT|wxALL
, 5 )
35 grid
.AddWindow( self
.limit_target
, 0, wxALIGN_LEFT|wxALL
, 5 )
36 grid
.AddSpacer( 20, 0, 0, wxALIGN_LEFT|wxALL
, 5 )
37 grid
.AddWindow( self
.allow_none
, 0, wxALIGN_LEFT|wxALL
, 5 )
38 grid
.AddSpacer( 20, 0, 0, wxALIGN_LEFT|wxALL
, 5 )
39 grid
.AddWindow( self
.allow_long
, 0, wxALIGN_LEFT|wxALL
, 5 )
40 grid
.AddSpacer( 20, 0, 0, wxALIGN_LEFT|wxALL
, 5 )
42 grid
.AddSpacer( 20, 0, 0, wxALIGN_LEFT|wxALL
, 5 )
43 grid
.AddSpacer( 20, 0, 0, wxALIGN_LEFT|wxALL
, 5 )
45 grid
.AddWindow( label
, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxALL
, 5 )
46 grid
.AddWindow( self
.target_ctl
, 0, wxALIGN_LEFT|wxALL
, 5 )
48 outer_box
= wxBoxSizer( wxVERTICAL
)
49 outer_box
.AddSizer( grid
, 0, wxALIGN_CENTRE|wxALL
, 20 )
51 panel
.SetAutoLayout( True )
52 panel
.SetSizer( outer_box
)
53 outer_box
.Fit( panel
)
57 EVT_CHECKBOX( self
, self
.set_min
.GetId(), self
.OnSetMin
)
58 EVT_CHECKBOX( self
, self
.set_max
.GetId(), self
.OnSetMax
)
59 EVT_CHECKBOX( self
, self
.limit_target
.GetId(), self
.SetTargetMinMax
)
60 EVT_CHECKBOX( self
, self
.allow_none
.GetId(), self
.OnSetAllowNone
)
61 EVT_CHECKBOX( self
, self
.allow_long
.GetId(), self
.OnSetAllowLong
)
62 EVT_INT( self
, self
.min.GetId(), self
.SetTargetMinMax
)
63 EVT_INT( self
, self
.max.GetId(), self
.SetTargetMinMax
)
64 EVT_INT( self
, self
.target_ctl
.GetId(), self
.OnTargetChange
)
67 def OnSetMin( self
, event
):
68 self
.min.Enable( self
.set_min
.GetValue() )
69 self
.SetTargetMinMax()
71 def OnSetMax( self
, event
):
72 self
.max.Enable( self
.set_max
.GetValue() )
73 self
.SetTargetMinMax()
76 def SetTargetMinMax( self
, event
=None ):
78 self
.target_ctl
.SetLimited( self
.limit_target
.GetValue() )
80 if self
.set_min
.GetValue():
81 min = self
.min.GetValue()
82 if self
.set_max
.GetValue():
83 max = self
.max.GetValue()
85 cur_min
, cur_max
= self
.target_ctl
.GetBounds()
87 if min != cur_min
and not self
.target_ctl
.SetMin( min ):
88 self
.log
.write( "min (%d) > current max (%d) -- bound not set\n" % ( min, self
.target_ctl
.GetMax() ) )
89 self
.min.SetForegroundColour( wxRED
)
91 self
.min.SetForegroundColour( wxBLACK
)
94 if max != cur_max
and not self
.target_ctl
.SetMax( max ):
95 self
.log
.write( "max (%d) < current min (%d) -- bound not set\n" % ( max, self
.target_ctl
.GetMin() ) )
96 self
.max.SetForegroundColour( wxRED
)
98 self
.max.SetForegroundColour( wxBLACK
)
101 if min != cur_min
or max != cur_max
:
102 new_min
, new_max
= self
.target_ctl
.GetBounds()
103 self
.log
.write( "current min, max: (%s, %s)\n" % ( str(new_min
), str(new_max
) ) )
106 def OnSetAllowNone( self
, event
):
107 self
.target_ctl
.SetNoneAllowed( self
.allow_none
.GetValue() )
110 def OnSetAllowLong( self
, event
):
111 self
.target_ctl
.SetLongAllowed( self
.allow_long
.GetValue() )
114 def OnTargetChange( self
, event
):
115 ctl
= event
.GetEventObject()
116 value
= ctl
.GetValue()
117 ib_str
= [ " (out of bounds)", "" ]
118 self
.log
.write( "integer value = %s%s\n" % ( str(value
), ib_str
[ ctl
.IsInBounds(value
) ] ) )
121 #----------------------------------------------------------------------
123 def runTest( frame
, nb
, log
):
124 win
= TestPanel( nb
, log
)
127 #----------------------------------------------------------------------
129 overview
= """<html><body>
131 <B>wxIntCtrl</B> provides a control that takes and returns integers as
132 value, and provides bounds support and optional value limiting.
135 Here's the API for wxIntCtrl:
142 <B>limited</B> = False,
143 <B>allow_none</B> = False,
144 <B>allow_long</B> = False,
145 <B>default_color</B> = wxBLACK,
146 <B>oob_color</B> = wxRED,
147 pos = wxDefaultPosition,
148 size = wxDefaultSize,
154 <DD>If no initial value is set, the default will be zero, or
155 the minimum value, if specified. If an illegal string is specified,
156 a ValueError will result. (You can always later set the initial
157 value with SetValue() after instantiation of the control.)
160 <DD>The minimum value that the control should allow. This can be
161 adjusted with SetMin(). If the control is not limited, any value
162 below this bound will be colored with the current out-of-bounds color.
165 <DD>The maximum value that the control should allow. This can be
166 adjusted with SetMax(). If the control is not limited, any value
167 above this bound will be colored with the current out-of-bounds color.
170 <DD>Boolean indicating whether the control prevents values from
171 exceeding the currently set minimum and maximum values (bounds).
172 If <I>False</I> and bounds are set, out-of-bounds values will
173 be colored with the current out-of-bounds color.
175 <DT><B>allow_none</B>
176 <DD>Boolean indicating whether or not the control is allowed to be
177 empty, representing a value of <I>None</I> for the control.
179 <DT><B>allow_long</B>
180 <DD>Boolean indicating whether or not the control is allowed to hold
181 and return a value of type long as well as int. If False, the
182 control will be implicitly limited to have a value such that
183 -sys.maxint-1 <= n <= sys.maxint.
185 <DT><B>default_color</B>
186 <DD>Color value used for in-bounds values of the control.
189 <DD>Color value used for out-of-bounds values of the control
190 when the bounds are set but the control is not limited.
194 <DT><B>EVT_INT(win, id, func)</B>
195 <DD>Respond to a wxEVT_COMMAND_INT_UPDATED event, generated when the
196 value changes. Notice that this event will always be sent when the
197 control's contents changes - whether this is due to user input or
198 comes from the program itself (for example, if SetValue() is called.)
201 <DT><B>SetValue(int)</B>
202 <DD>Sets the value of the control to the integer value specified.
203 The resulting actual value of the control may be altered to
204 conform with the bounds set on the control if limited,
205 or colored if not limited but the value is out-of-bounds.
206 A ValueError exception will be raised if an invalid value
209 <DT><B>GetValue()</B>
210 <DD>Retrieves the integer value from the control. The value
211 retrieved will be sized as an int if possible or a long,
215 <DT><B>SetMin(min=None)</B>
216 <DD>Sets the expected minimum value, or lower bound, of the control.
217 (The lower bound will only be enforced if the control is
218 configured to limit its values to the set bounds.)
219 If a value of <I>None</I> is provided, then the control will have
220 no explicit lower bound. If the value specified is greater than
221 the current lower bound, then the function returns 0 and the
222 lower bound will not change from its current setting. On success,
223 the function returns 1.
224 <DT><DD>If successful and the current value is
225 lower than the new lower bound, if the control is limited, the
226 value will be automatically adjusted to the new minimum value;
227 if not limited, the value in the control will be colored with
228 the current out-of-bounds color.
231 <DD>Gets the current lower bound value for the control.
232 It will return None if no lower bound is currently specified.
235 <DT><B>SetMax(max=None)</B>
236 <DD>Sets the expected maximum value, or upper bound, of the control.
237 (The upper bound will only be enforced if the control is
238 configured to limit its values to the set bounds.)
239 If a value of <I>None</I> is provided, then the control will
240 have no explicit upper bound. If the value specified is less
241 than the current lower bound, then the function returns 0 and
242 the maximum will not change from its current setting. On success,
243 the function returns 1.
244 <DT><DD>If successful and the current value
245 is greater than the new upper bound, if the control is limited
246 the value will be automatically adjusted to the new maximum value;
247 if not limited, the value in the control will be colored with the
248 current out-of-bounds color.
251 <DD>Gets the current upper bound value for the control.
252 It will return None if no upper bound is currently specified.
255 <DT><B>SetBounds(min=None,max=None)</B>
256 <DD>This function is a convenience function for setting the min and max
257 values at the same time. The function only applies the maximum bound
258 if setting the minimum bound is successful, and returns True
259 only if both operations succeed. <B><I>Note:</I></B> leaving out an argument
260 will remove the corresponding bound.
261 <DT><B>GetBounds()</B>
262 <DD>This function returns a two-tuple (min,max), indicating the
263 current bounds of the control. Each value can be None if
264 that bound is not set.
267 <DT><B>IsInBounds(value=None)</B>
268 <DD>Returns <I>True</I> if no value is specified and the current value
269 of the control falls within the current bounds. This function can also
270 be called with a value to see if that value would fall within the current
271 bounds of the given control.
274 <DT><B>SetLimited(bool)</B>
275 <DD>If called with a value of True, this function will cause the control
276 to limit the value to fall within the bounds currently specified.
277 If the control's value currently exceeds the bounds, it will then
278 be limited accordingly.
279 If called with a value of 0, this function will disable value
280 limiting, but coloring of out-of-bounds values will still take
281 place if bounds have been set for the control.
282 <DT><B>IsLimited()</B>
283 <DD>Returns <I>True</I> if the control is currently limiting the
284 value to fall within the current bounds.
287 <DT><B>SetNoneAllowed(bool)</B>
288 <DD>If called with a value of True, this function will cause the control
289 to allow the value to be empty, representing a value of None.
290 If called with a value of fakse, this function will prevent the value
291 from being None. If the value of the control is currently None,
292 ie. the control is empty, then the value will be changed to that
293 of the lower bound of the control, or 0 if no lower bound is set.
294 <DT><B>IsNoneAllowed()</B>
295 <DD>Returns <I>True</I> if the control currently allows its
299 <DT><B>SetLongAllowed(bool)</B>
300 <DD>If called with a value of True, this function will cause the
301 control to allow the value to be a long. If called with a value
302 of False, and the value of the control is currently a long value,
303 the value of the control will be adjusted to fall within the
304 size of an integer type, at either the sys.maxint or -sys.maxint-1,
305 for positive and negative values, respectively.
306 <DT><B>IsLongAllowed()</B>
307 <DD>Returns <I>True</I> if the control currently allows its
308 value to be of type long.
311 <DT><B>SetColors(default_color=wxBLACK, oob_color=wxRED)</B>
312 <DD>Tells the control what colors to use for normal and out-of-bounds
313 values. If the value currently exceeds the bounds, it will be
314 recolored accordingly.
315 <DT><B>GetColors()</B>
316 <DD>Returns a tuple of <I>(default_color, oob_color)</I> indicating
317 the current color settings for the control.
321 <DD>Will allow cuts to the clipboard of the text portion of the value,
322 leaving the value of zero if the entire contents are "cut."
324 <DD>Will paste the contents of the clipboard to the selected portion
325 of the value; if the resulting string does not represent a legal
326 value, a ValueError will result. If the result is out-of bounds,
327 it will either be adjusted or colored as appropriate.
334 if __name__
== '__main__':
337 run
.main(['', os
.path
.basename(sys
.argv
[0])])