* TreeView.cs: Don't draw the selected node when we lose
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ProgressBar.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-2005 Novell, Inc.
21 //
22 // Authors:
23 //              Jordi Mas i Hernandez   jordi@ximian.com
24 //
25 //
26
27
28 using System.Drawing;
29 using System.ComponentModel;
30 using System.Drawing.Imaging;
31 using System.Drawing.Drawing2D;
32
33 namespace System.Windows.Forms
34 {
35         [DefaultProperty ("Value")]
36         public sealed class ProgressBar : Control
37         {
38                 #region Local Variables
39                 private int maximum;
40                 private int minimum;
41                 internal int step;
42                 internal int val;               
43                 internal Rectangle client_area = new Rectangle ();
44                 #endregion      // Local Variables
45
46                 #region events
47                 [Browsable (false)]
48                 [EditorBrowsable (EditorBrowsableState.Never)]          
49                 public new event EventHandler BackColorChanged;
50                 
51                 [Browsable (false)]
52                 [EditorBrowsable (EditorBrowsableState.Never)]
53                 public new event EventHandler BackgroundImageChanged;
54                 
55                 [Browsable (false)]
56                 [EditorBrowsable (EditorBrowsableState.Never)]
57                 public new event EventHandler CausesValidationChanged;
58                 
59                 [Browsable (false)]
60                 [EditorBrowsable (EditorBrowsableState.Never)]
61                 public new event EventHandler DoubleClick;
62                 
63                 [Browsable (false)]
64                 [EditorBrowsable (EditorBrowsableState.Never)]
65                 public new event EventHandler Enter;
66                 
67                 [Browsable (false)]
68                 [EditorBrowsable (EditorBrowsableState.Never)]
69                 public new event EventHandler FontChanged;
70                 
71                 [Browsable (false)]
72                 [EditorBrowsable (EditorBrowsableState.Never)]
73                 public new event EventHandler ForeColorChanged;
74                 
75                 [Browsable (false)]
76                 [EditorBrowsable (EditorBrowsableState.Never)]
77                 public new event EventHandler ImeModeChanged;
78                 
79                 [Browsable (false)]
80                 [EditorBrowsable (EditorBrowsableState.Never)]
81                 public new event KeyEventHandler KeyDown;
82                 
83                 [Browsable (false)]
84                 [EditorBrowsable (EditorBrowsableState.Never)]
85                 public new event KeyPressEventHandler KeyPress;
86                 
87                 [Browsable (false)]
88                 [EditorBrowsable (EditorBrowsableState.Never)]
89                 public new event KeyEventHandler KeyUp;
90                 
91                 [Browsable (false)]
92                 [EditorBrowsable (EditorBrowsableState.Never)]
93                 public new event EventHandler Leave;
94                 
95                 [Browsable (false)]
96                 [EditorBrowsable (EditorBrowsableState.Never)]
97                 public new event PaintEventHandler Paint;
98                 
99                 [Browsable (false)]
100                 [EditorBrowsable (EditorBrowsableState.Never)]
101                 public new event EventHandler RightToLeftChanged;
102                 
103                 [Browsable (false)]
104                 [EditorBrowsable (EditorBrowsableState.Never)]
105                 public new event EventHandler TabStopChanged;
106                 
107                 [Browsable (false)]
108                 [EditorBrowsable (EditorBrowsableState.Never)]
109                 public new event EventHandler TextChanged;
110                 #endregion Events
111
112                 #region Public Constructors
113                 public ProgressBar()
114                 {
115                         maximum = 100;
116                         minimum = 0;
117                         step = 10;
118                         val = 0;
119
120                         base.Paint += new PaintEventHandler (OnPaintPB);
121                         base.Resize += new EventHandler (OnResizeTB);
122
123                         SetStyle (ControlStyles.UserPaint | 
124                                 ControlStyles.Selectable | 
125                                 ControlStyles.ResizeRedraw | 
126                                 ControlStyles.Opaque, false);
127                 }
128                 #endregion      // Public Constructors
129
130                 #region Public Instance Properties
131
132                 [Browsable (false)]
133                 [EditorBrowsable (EditorBrowsableState.Never)]
134                 public override bool AllowDrop
135                 {
136                         get { return base.AllowDrop; }
137                         set {
138                                 base.AllowDrop = value;
139                         }
140                 }
141
142                 // Setting this property in MS .Net 1.1 does not have any visual effect and it
143                 // does not fire a BackColorChanged event
144                 [Browsable (false)]
145                 [EditorBrowsable (EditorBrowsableState.Never)]
146                 public override Color BackColor
147                 {
148                         get { return base.BackColor; }
149                         set { base.BackColor = value; }
150                 }
151
152                 // Setting this property in MS .Net 1.1 does not have any visual effect and it
153                 // does not fire a BackgroundImageChanged event
154                 [Browsable (false)]
155                 [EditorBrowsable (EditorBrowsableState.Never)]
156                 public override Image BackgroundImage
157                 {
158                         get { return base.BackgroundImage; }
159                         set { base.BackgroundImage = value; }
160                 }
161
162                 [Browsable (false)]
163                 [EditorBrowsable (EditorBrowsableState.Never)]
164                 public new bool CausesValidation
165                 {
166                         get { return base.CausesValidation; }
167                         set {
168                                 if (base.CausesValidation == value)
169                                         return;
170
171                                 base.CausesValidation = value;
172                                 if (CausesValidationChanged != null)
173                                         CausesValidationChanged (this, new EventArgs ());
174                         }
175                 }
176
177                 protected override CreateParams CreateParams
178                 {
179                         get { return base.CreateParams; }
180                 }
181
182                 protected override ImeMode DefaultImeMode
183                 {
184                         get { return base.DefaultImeMode; }
185                 }
186
187                 protected override Size DefaultSize
188                 {
189                         get { return ThemeEngine.Current.ProgressBarDefaultSize; }
190                 }
191
192                 // Setting this property in MS .Net 1.1 does not have any visual effect and it
193                 // does not fire a FontChanged event
194                 [Browsable (false)]
195                 [EditorBrowsable (EditorBrowsableState.Never)]
196                 public override Font Font
197                 {
198                         get { return base.Font; }
199                         set { base.Font = value; }
200                 }
201
202                 // Setting this property in MS .Net 1.1 does not have any visual effect and it
203                 // does not fire a FontChanged event
204                 [Browsable (false)]
205                 [EditorBrowsable (EditorBrowsableState.Never)]
206                 public override Color ForeColor
207                 {
208                         get { return base.ForeColor; }
209                         set { base.ForeColor = value; }
210                 }
211
212                 [Browsable (false)]
213                 [EditorBrowsable (EditorBrowsableState.Never)]
214                 public new ImeMode ImeMode
215                 {
216                         get { return base.ImeMode; }
217                         set
218                         {
219                                 if (value == base.ImeMode)
220                                         return;
221
222                                 base.ImeMode = value;
223                                 if (ImeModeChanged != null)
224                                         ImeModeChanged (this, EventArgs.Empty);
225                         }
226                 }
227
228                 [RefreshProperties(RefreshProperties.Repaint)]
229                 [DefaultValue (100)]
230                 public int Maximum
231                 {
232                         get {
233                                 return maximum;
234                         }
235                         set {
236                                 if (value < 0)
237                                         throw new ArgumentException(
238                                                 string.Format("Value '{0}' must be greater than or equal to 0.", value ));
239
240                                 maximum = value;
241                                 Refresh ();
242                         }
243                 }
244
245                 [RefreshProperties(RefreshProperties.Repaint)]
246                 [DefaultValue (0)]
247                 public int Minimum {
248                         get {
249                                 return minimum;
250                         }
251                         set {
252                                 if (value < 0)
253                                         throw new ArgumentException(
254                                                 string.Format("Value '{0}' must be greater than or equal to 0.", value ));
255
256                                 minimum = value;
257                                 Refresh ();
258                         }
259                 }
260
261                 [Browsable (false)]
262                 [EditorBrowsable (EditorBrowsableState.Never)]
263                 public override RightToLeft RightToLeft
264                 {
265                         get { return base.RightToLeft; }
266                         set {
267                                 if (base.RightToLeft == value)
268                                         return;
269
270                                 base.RightToLeft = value;
271
272                                 if (RightToLeftChanged != null)
273                                         RightToLeftChanged (this, EventArgs.Empty);
274
275                         }
276                 }
277
278                 [DefaultValue (10)]
279                 public int Step
280                 {
281                         get { return step; }
282                         set {
283                                 step = value;
284                                 Refresh ();
285                         }
286                 }
287
288                 [Browsable (false)]
289                 [EditorBrowsable (EditorBrowsableState.Never)]
290                 public new bool TabStop
291                 {
292                         get { return base.TabStop; }
293                         set {
294                                 if (base.TabStop == value)
295                                         return;
296
297                                 base.TabStop = value;
298
299                                 if (TabStopChanged != null)
300                                         TabStopChanged (this, EventArgs.Empty);
301
302                         }
303                 }
304
305                 [Browsable (false)]
306                 [EditorBrowsable (EditorBrowsableState.Never)]
307                 [Bindable(false)]
308                 public override string Text
309                 {
310                         get { return base.Text; }
311                         set
312                         {
313                                 if (value == base.Text)
314                                         return;
315
316                                 if (TextChanged != null)
317                                         TextChanged (this, EventArgs.Empty);
318
319                                 Refresh ();
320                         }
321                 }
322
323                 [Bindable(true)]
324                 [DefaultValue (0)]
325                 public int Value
326                 {
327                         get {
328                                 return val;
329                         }
330                         set {
331                                 if (value < Minimum || value > Maximum)
332                                         throw new ArgumentException(
333                                                 string.Format("'{0}' is not a valid value for 'Value'. 'Value' should be between 'Minimum' and 'Maximum'", value));
334
335                                 val = value;
336                                 Refresh ();
337                         }
338                 }
339
340
341                 #endregion      // Protected Instance Properties
342
343                 #region Public Instance Methods
344                 
345                 protected override void CreateHandle ()
346                 {
347                         base.CreateHandle ();
348                 }
349
350                 public void Increment (int value)
351                 {
352                         int newValue = Value + value;
353
354                         if (newValue < Minimum)
355                                 newValue = Minimum;
356
357                         if (newValue > Maximum)
358                                 newValue = Maximum;
359
360                         Value = newValue;
361                         Refresh ();
362                 }
363
364                 protected override void OnHandleCreated (EventArgs e)
365                 {
366                         base.OnHandleCreated (e);
367
368                         UpdateAreas ();
369                 }
370
371                 public void PerformStep ()
372                 {
373                         if (Value >= Maximum)
374                                 return;
375
376                         Value = Value + Step;
377                         Refresh ();
378                 }
379
380                 public override string ToString()
381                 {
382                         return string.Format ("{0}, Minimum: {1}, Maximum: {2}, Value: {3}",
383                                 GetType().FullName.ToString (),
384                                 Maximum.ToString (),
385                                 Minimum.ToString (),
386                                 Value.ToString () );
387                 }
388
389                 #endregion      // Public Instance Methods
390
391                 #region Private Instance Methods
392                 private void UpdateAreas ()
393                 {
394                         client_area.X = client_area.Y = 2;
395                         client_area.Width = Width - 4;
396                         client_area.Height = Height - 4;
397                 }
398
399                 private void OnResizeTB (Object o, EventArgs e)
400                 {
401                         if (Width <= 0 || Height <= 0)
402                                 return;
403
404                         UpdateAreas ();
405                 }
406
407                 private void OnPaintPB (Object o, PaintEventArgs pevent)
408                 {
409                         ThemeEngine.Current.DrawProgressBar (pevent.Graphics, pevent.ClipRectangle, this);
410                 }               
411                 
412                 #endregion
413         }
414 }