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