* ImageList.cs: When the image stream is set pull all the images
[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         [DefaultEvent ("ValueChanged")]
32         [DefaultProperty ("Value")]
33         public class NumericUpDown : UpDownBase, ISupportInitialize {
34                 Decimal updown_value;
35                 Decimal min = 0m;
36                 Decimal max = 100m;
37                 Decimal increment = 1m;
38                 bool thousand = false;
39                 bool on_init = false;
40                 string format;
41                 int decimal_places;
42                 
43                 public NumericUpDown () : base ()
44                 {
45                         UpdateFormat ();
46                         UpdateEditText ();
47                 }
48                 
49 #region ISupportInitialize methods
50
51                 //
52                 // These are used for batch updates of properties:
53                 // checking of values should be disabled during this
54                 // time, assuming that the caller will set us up
55                 // correctly.
56                 //
57                 // See: http://www.vkarlsen.no/articles/02_isupportinitialize/ISupportInitialize.pdf
58                 // for the strategy.
59                 //
60                 public void BeginInit ()
61                 {
62                         on_init = true;
63                 }
64
65                 public void EndInit ()
66                 {
67                         on_init = false;
68                         if (updown_value < min)
69                                 updown_value = min;
70                         if (updown_value > max)
71                                 updown_value = max;
72                         
73                         UpdateEditText ();
74                 }
75 #endregion
76
77                 public override void DownButton ()
78                 {
79                         if (UserEdit)
80                                 ParseEditText ();
81
82                         if (updown_value-increment >= min){
83                                 updown_value -= increment;
84                                 UpdateEditText ();
85                         }
86                 }
87
88                 public override void UpButton ()
89                 {
90                         if (UserEdit)
91                                 ParseEditText ();
92
93                         if (updown_value + increment <= max){
94                                 updown_value += increment;
95                                 UpdateEditText ();
96                         }
97                 }
98
99                 public override void UpdateEditText ()
100                 {
101                         if (on_init)
102                                 return;
103                         
104                         ChangingText = true;
105                         Text = updown_value.ToString (format);
106                 }
107
108                 public void ParseEditText ()
109                 {
110                         decimal res;
111                         
112                         try {
113                                 res = decimal.Parse (Text);
114                         } catch {
115                                 res = min;
116                         }
117                         
118                         if (res < min)
119                                 res = min;
120                         else if (res > max)
121                                 res = max;
122                         updown_value = res;
123                 }
124
125                 protected override void ValidateEditText ()
126                 {
127                         ParseEditText ();
128                         UpdateEditText ();
129                 }
130
131                 [Bindable(true)]
132                 public decimal Value {
133                         get {
134                                 return updown_value;
135                         }
136
137                         set {
138                                 if (on_init){
139                                         updown_value = value;
140                                         return;
141                                 }
142                                 
143                                 if (value < min || value > max)
144                                         throw new ArgumentOutOfRangeException (
145                                                 String.Format ("Value {0} not within boundaries [{1}, {2}]", value, min, max));
146                                 
147                                 updown_value = value;
148                                 Text = updown_value.ToString (format);
149                         }
150                 }
151
152                 public decimal Increment {
153                         get {
154                                 return increment;
155                         }
156
157                         set {
158                                 increment = value;
159                         }
160                 }
161                 
162                 [RefreshProperties(RefreshProperties.All)]
163                 public decimal Maximum {
164                         get {
165                                 return max;
166                         }
167
168                         set {
169                                 max = value;
170                                 if (value > max)
171                                         value = max;
172                                 if (min > max)
173                                         min = max;
174                                 UpdateEditText ();
175                         }
176                 }
177
178                 [RefreshProperties(RefreshProperties.All)]
179                 public decimal Minimum {
180                         get {
181                                 return min;
182                         }
183
184                         set {
185                                 min = value;
186                                 if (value < min)
187                                         value = min;
188                                 if (min > max)
189                                         max = min;
190                                 UpdateEditText ();
191                         }
192                 }
193
194                 [Bindable(false)]
195                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
196                 [EditorBrowsable(EditorBrowsableState.Never)]
197                 [Browsable (false)]
198                 public override string Text {
199                         get {
200                                 return base.Text;
201                         }
202
203                         set {
204                                 base.Text = value;
205                         }
206                 }
207
208                 void UpdateFormat ()
209                 {
210                         if (thousand)
211                                 format = "N" + decimal_places.ToString ();
212                         else
213                                 format = "F" + decimal_places.ToString ();
214                 }
215                 
216                 public bool ThousandsSeparator {
217                         get {
218                                 return thousand;
219                         }
220
221                         set {
222                                 thousand = value;
223                                 UpdateFormat ();
224                                 UpdateEditText ();
225                         }
226                 }
227
228                 public int DecimalPlaces {
229                         get {
230                                 return decimal_places;
231                         }
232
233                         set {
234                                 decimal_places = value;
235                                 UpdateFormat ();
236                                 UpdateEditText ();
237                         }
238                 }
239                 
240 #region Overrides for Control hooks
241
242                 protected override void OnLostFocus (EventArgs e)
243                 {
244                         base.OnLostFocus (e);
245                         if (UserEdit){
246                                 ParseEditText ();
247                                 UpdateEditText ();
248                         }
249                 }
250
251                 protected override void OnMouseDown (MouseEventArgs e)
252                 {
253                         // TODO: What to do?
254                 }
255
256                 protected override void OnMouseUp (MouseEventArgs e)
257                 {
258                         // TODO: What to do?
259                 }
260
261                 protected override void OnTextBoxKeyPress (object source, KeyPressEventArgs e)
262                 {
263                         Console.WriteLine ("OnTextBoxKeyPress: " + e);
264
265                         // TODO: Apparently we must validate digit input here
266                 }
267 #endregion
268                 
269
270                 public override string ToString ()
271                 {
272                         return String.Format ("{0} Min={0} Max={1}", base.ToString (), min, max);
273                 }
274         }
275 }