2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / NumericUpDown.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2004 Novell, Inc.
21 //
22 // Authors:
23 //      Miguel de Icaza (miguel@novell.com).
24 //
25 //
26
27 using System;
28 using System.ComponentModel;
29
30 namespace System.Windows.Forms {
31         public class NumericUpDown : UpDownBase, ISupportInitialize {
32                 Decimal updown_value;
33                 Decimal min = 0m;
34                 Decimal max = 100m;
35                 bool thousand = false;
36                 bool on_init = false;
37                 
38                 public NumericUpDown () : base ()
39                 {
40                         UpdateEditText ();
41                 }
42                 
43 #region ISupportInitialize methods
44
45                 //
46                 // These are used for batch updates of properties:
47                 // checking of values should be disabled during this
48                 // time, assuming that the caller will set us up
49                 // correctly.
50                 //
51                 // See: http://www.vkarlsen.no/articles/02_isupportinitialize/ISupportInitialize.pdf
52                 // for the strategy.
53                 //
54                 public void BeginInit ()
55                 {
56                         on_init = true;
57                 }
58
59                 public void EndInit ()
60                 {
61                         on_init = false;
62                         if (updown_value < min)
63                                 updown_value = min;
64                         if (updown_value > max)
65                                 updown_value = max;
66                         
67                         UpdateEditText ();
68                 }
69 #endregion
70
71                 public override void DownButton ()
72                 {
73                         if (UserEdit)
74                                 ParseEditText ();
75
76                         if (updown_value > min){
77                                 updown_value--;
78                                 UpdateEditText ();
79                         }
80                 }
81
82                 public override void UpButton ()
83                 {
84                         if (UserEdit)
85                                 ParseEditText ();
86
87                         if (updown_value < max){
88                                 updown_value++;
89                                 UpdateEditText ();
90                         }
91                 }
92
93                 public override void UpdateEditText ()
94                 {
95                         if (on_init)
96                                 return;
97                         
98                         ChangingText = true;
99                         Text = updown_value.ToString ();
100                 }
101
102                 public void ParseEditText ()
103                 {
104                         decimal res;
105                         
106                         try {
107                                 res = decimal.Parse (Text);
108                         } catch {
109                                 res = min;
110                         }
111                         
112                         if (res < min)
113                                 res = min;
114                         else if (res > max)
115                                 res = max;
116                         updown_value = res;
117                 }
118
119                 protected override void ValidateEditText ()
120                 {
121                         ParseEditText ();
122                         UpdateEditText ();
123                 }
124
125                 public decimal Value {
126                         get {
127                                 return updown_value;
128                         }
129
130                         set {
131                                 if (on_init){
132                                         updown_value = value;
133                                         return;
134                                 }
135                                 
136                                 if (value < min || value > max)
137                                         throw new ArgumentOutOfRangeException (
138                                                 String.Format ("Value {0} not within boundaries [{1}, {2}]", value, min, max));
139                                 
140                                 updown_value = value;
141                                 Text = updown_value.ToString ();
142                         }
143                 }
144
145                 public decimal Maximum {
146                         get {
147                                 return max;
148                         }
149
150                         set {
151                                 max = value;
152                                 if (value > max)
153                                         value = max;
154                                 if (min > max)
155                                         min = max;
156                                 UpdateEditText ();
157                         }
158                 }
159
160                 public decimal Minimum {
161                         get {
162                                 return min;
163                         }
164
165                         set {
166                                 min = value;
167                                 if (value < min)
168                                         value = min;
169                                 if (min > max)
170                                         max = min;
171                                 UpdateEditText ();
172                         }
173                 }
174
175                 public override string Text {
176                         get {
177                                 return base.Text;
178                         }
179
180                         set {
181                                 base.Text = value;
182                         }
183                 }
184
185                 public bool ThousandsSepator {
186                         get {
187                                 return thousand;
188                         }
189
190                         set {
191                                 thousand = value;
192                                 UpdateEditText ();
193                         }
194                 }
195
196 #region Overrides for Control hooks
197
198                 protected override void OnLostFocus (EventArgs e)
199                 {
200                         base.OnLostFocus (e);
201                         if (UserEdit){
202                                 ParseEditText ();
203                                 UpdateEditText ();
204                         }
205                 }
206
207                 protected override void OnMouseDown (MouseEventArgs e)
208                 {
209                         // TODO: What to do?
210                 }
211
212                 protected override void OnMouseUp (MouseEventArgs e)
213                 {
214                         // TODO: What to do?
215                 }
216
217                 protected override void OnTextBoxKeyPress (object source, KeyPressEventArgs e)
218                 {
219                         Console.WriteLine ("OnTextBoxKeyPress: " + e);
220
221                         // TODO: Apparently we must validate digit input here
222                 }
223 #endregion
224                 
225                 
226         }
227 }