Fixed typo
[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-2006 Novell, Inc.
21 //
22 // Authors:
23 //              Jordi Mas i Hernandez   jordi@ximian.com
24 //              Peter Dennis Bartok     pbartok@novell.com
25 //
26 //
27
28 using System.Drawing;
29 using System.ComponentModel;
30 using System.Drawing.Imaging;
31 using System.Drawing.Drawing2D;
32 using System.Runtime.InteropServices;
33
34 namespace System.Windows.Forms
35 {
36         [DefaultProperty ("Value")]
37 #if NET_2_0
38         [DefaultBindingProperty ("Value")]
39         [ClassInterface (ClassInterfaceType.AutoDispatch)]
40         [ComVisible (true)]
41 #endif
42         public
43 #if !NET_2_0
44         sealed
45 #endif
46         class ProgressBar : Control
47         {
48                 #region Local Variables
49                 private int maximum;
50                 private int minimum;
51                 internal int step;
52                 internal int val;
53                 internal DateTime start = DateTime.Now;
54                 internal Rectangle client_area = new Rectangle ();
55 #if NET_2_0
56                 internal ProgressBarStyle style;
57                 Timer marquee_timer;
58 #endif
59
60 #if NET_2_0
61                 private static readonly Color defaultForeColor = SystemColors.Highlight;
62 #endif
63                 #endregion      // Local Variables
64
65                 #region events
66
67 #if ONLY_1_1
68                 [Browsable (false)]
69                 [EditorBrowsable (EditorBrowsableState.Never)]
70                 public new event EventHandler BackColorChanged {
71                         add { base.BackColorChanged += value; }
72                         remove { base.BackColorChanged -= value; }
73                 }
74 #endif
75                 
76                 [Browsable (false)]
77                 [EditorBrowsable (EditorBrowsableState.Never)]
78                 public new event EventHandler BackgroundImageChanged {
79                         add { base.BackgroundImageChanged += value; }
80                         remove { base.BackgroundImageChanged -= value; }
81                 }
82                 
83                 [Browsable (false)]
84                 [EditorBrowsable (EditorBrowsableState.Never)]
85                 public new event EventHandler CausesValidationChanged {
86                         add { base.CausesValidationChanged += value; }
87                         remove { base.CausesValidationChanged -= value; }
88                 }
89                 
90                 [Browsable (false)]
91                 [EditorBrowsable (EditorBrowsableState.Never)]
92                 public new event EventHandler DoubleClick {
93                         add { base.DoubleClick += value; }
94                         remove { base.DoubleClick -= value; }
95                 }
96                 
97                 [Browsable (false)]
98                 [EditorBrowsable (EditorBrowsableState.Never)]
99                 public new event EventHandler Enter {
100                         add { base.Enter += value; }
101                         remove { base.Enter -= value; }
102                 }
103                 
104                 [Browsable (false)]
105                 [EditorBrowsable (EditorBrowsableState.Never)]
106                 public new event EventHandler FontChanged {
107                         add { base.FontChanged += value; }
108                         remove { base.FontChanged -= value; }
109                 }
110                 
111 #if ONLY_1_1
112                 [Browsable (false)]
113                 [EditorBrowsable (EditorBrowsableState.Never)]
114                 public new event EventHandler ForeColorChanged {
115                         add { base.ForeColorChanged += value; }
116                         remove { base.ForeColorChanged -= value; }
117                 }
118 #endif
119                 
120                 [Browsable (false)]
121                 [EditorBrowsable (EditorBrowsableState.Never)]
122                 public new event EventHandler ImeModeChanged {
123                         add { base.ImeModeChanged += value; }
124                         remove { base.ImeModeChanged -= value; }
125                 }
126                 
127                 [Browsable (false)]
128                 [EditorBrowsable (EditorBrowsableState.Never)]
129                 public new event KeyEventHandler KeyDown {
130                         add { base.KeyDown += value; }
131                         remove { base.KeyDown -= value; }
132                 }
133                 
134                 [Browsable (false)]
135                 [EditorBrowsable (EditorBrowsableState.Never)]
136                 public new event KeyPressEventHandler KeyPress {
137                         add { base.KeyPress += value; }
138                         remove { base.KeyPress -= value; }
139                 }
140                 
141                 [Browsable (false)]
142                 [EditorBrowsable (EditorBrowsableState.Never)]
143                 public new event KeyEventHandler KeyUp {
144                         add { base.KeyUp += value; }
145                         remove { base.KeyUp -= value; }
146                 }
147                 
148                 [Browsable (false)]
149                 [EditorBrowsable (EditorBrowsableState.Never)]
150                 public new event EventHandler Leave {
151                         add { base.Leave += value; }
152                         remove { base.Leave -= value; }
153                 }
154                 
155                 [Browsable (false)]
156                 [EditorBrowsable (EditorBrowsableState.Never)]
157                 public new event PaintEventHandler Paint {
158                         add { base.Paint += value; }
159                         remove { base.Paint -= value; }
160                 }
161                 
162 #if ONLY_1_1
163                 [Browsable (false)]
164                 [EditorBrowsable (EditorBrowsableState.Never)]
165                 public new event EventHandler RightToLeftChanged {
166                         add { base.RightToLeftChanged += value; }
167                         remove { base.RightToLeftChanged -= value; }
168                 }
169 #endif
170                 
171                 [Browsable (false)]
172                 [EditorBrowsable (EditorBrowsableState.Never)]
173                 public new event EventHandler TabStopChanged {
174                         add { base.TabStopChanged += value; }
175                         remove { base.TabStopChanged -= value; }
176                 }
177                 
178                 [Browsable (false)]
179                 [EditorBrowsable (EditorBrowsableState.Never)]
180                 public new event EventHandler TextChanged {
181                         add { base.TextChanged += value; }
182                         remove { base.TextChanged -= value; }
183                 }
184                 #endregion Events
185
186                 #region Public Constructors
187                 public ProgressBar()
188                 {
189                         maximum = 100;
190                         minimum = 0;
191                         step = 10;
192                         val = 0;
193
194                         base.Resize += new EventHandler (OnResizeTB);
195
196                         SetStyle (ControlStyles.UserPaint | 
197                                 ControlStyles.Selectable | 
198                                 ControlStyles.ResizeRedraw | 
199                                 ControlStyles.Opaque
200 #if NET_2_0
201                                 | ControlStyles.UseTextForAccessibility
202 #endif
203                                 , false);
204
205 #if NET_2_0
206                         ForeColor = defaultForeColor;
207 #endif
208                 }
209                 #endregion      // Public Constructors
210
211                 #region Public Instance Properties
212
213                 [Browsable (false)]
214                 [EditorBrowsable (EditorBrowsableState.Never)]
215                 public override bool AllowDrop
216                 {
217                         get { return base.AllowDrop; }
218                         set {
219                                 base.AllowDrop = value;
220                         }
221                 }
222
223                 // Setting this property in MS .Net 1.1 does not have any visual effect and it
224                 // does not fire a BackColorChanged event
225                 [Browsable (false)]
226                 [EditorBrowsable (EditorBrowsableState.Never)]
227                 public override Color BackColor
228                 {
229                         get { return base.BackColor; }
230                         set { base.BackColor = value; }
231                 }
232
233                 // Setting this property in MS .Net 1.1 does not have any visual effect and it
234                 // does not fire a BackgroundImageChanged event
235                 [Browsable (false)]
236                 [EditorBrowsable (EditorBrowsableState.Never)]
237                 public override Image BackgroundImage
238                 {
239                         get { return base.BackgroundImage; }
240                         set { base.BackgroundImage = value; }
241                 }
242
243                 [Browsable (false)]
244                 [EditorBrowsable (EditorBrowsableState.Never)]
245                 public new bool CausesValidation
246                 {
247                         get { return base.CausesValidation; }
248                         set { base.CausesValidation = value; }
249                 }
250
251                 protected override CreateParams CreateParams
252                 {
253                         get { return base.CreateParams; }
254                 }
255
256                 protected override ImeMode DefaultImeMode
257                 {
258                         get { return base.DefaultImeMode; }
259                 }
260
261                 protected override Size DefaultSize
262                 {
263                         get { return ThemeEngine.Current.ProgressBarDefaultSize; }
264                 }
265
266                 // Setting this property in MS .Net 1.1 does not have any visual effect and it
267                 // does not fire a FontChanged event
268                 [Browsable (false)]
269                 [EditorBrowsable (EditorBrowsableState.Never)]
270                 public override Font Font
271                 {
272                         get { return base.Font; }
273                         set { base.Font = value; }
274                 }
275
276 #if ONLY_1_1
277                 // Setting this property in MS .Net 1.1 does not have any visual effect and it
278                 // does not fire a FontChanged event
279                 [Browsable (false)]
280                 [EditorBrowsable (EditorBrowsableState.Never)]
281                 public override Color ForeColor
282                 {
283                         get { return base.ForeColor; }
284                         set { base.ForeColor = value; }
285                 }
286 #endif
287
288                 [Browsable (false)]
289                 [EditorBrowsable (EditorBrowsableState.Never)]
290                 public new ImeMode ImeMode
291                 {
292                         get { return base.ImeMode; }
293                         set { base.ImeMode = value; }
294                 }
295
296                 [RefreshProperties(RefreshProperties.Repaint)]
297                 [DefaultValue (100)]
298                 public int Maximum
299                 {
300                         get {
301                                 return maximum;
302                         }
303                         set {
304                                 if (value < 0)
305                                         throw new ArgumentException(
306                                                 string.Format("Value '{0}' must be greater than or equal to 0.", value ));
307
308                                 maximum = value;
309                                 Refresh ();
310                         }
311                 }
312
313                 [RefreshProperties(RefreshProperties.Repaint)]
314                 [DefaultValue (0)]
315                 public int Minimum {
316                         get {
317                                 return minimum;
318                         }
319                         set {
320                                 if (value < 0)
321                                         throw new ArgumentException(
322                                                 string.Format("Value '{0}' must be greater than or equal to 0.", value ));
323
324                                 minimum = value;
325                                 Refresh ();
326                         }
327                 }
328
329 #if ONLY_1_1
330                 [Browsable (false)]
331                 [EditorBrowsable (EditorBrowsableState.Never)]
332                 public override RightToLeft RightToLeft
333                 {
334                         get { return base.RightToLeft; }
335                         set { base.RightToLeft = value; }
336                 }
337 #endif
338
339                 [DefaultValue (10)]
340                 public int Step
341                 {
342                         get { return step; }
343                         set {
344                                 step = value;
345                                 Refresh ();
346                         }
347                 }
348
349 #if NET_2_0
350                 [Browsable (true)]
351                 [DefaultValue (ProgressBarStyle.Blocks)]
352                 [EditorBrowsable (EditorBrowsableState.Always)]
353                 public ProgressBarStyle Style {
354                         get {
355                                 return style;
356                         }
357
358                         set {
359                                 style = value;
360                                 
361                                 if (style == ProgressBarStyle.Marquee) {
362                                         if (marquee_timer == null) {
363                                                 marquee_timer = new Timer ();
364                                                 marquee_timer.Interval = 10;
365                                                 marquee_timer.Tick += new EventHandler (marquee_timer_Tick);
366                                         }
367                                         marquee_timer.Start ();
368                                 } else {
369                                         if (marquee_timer != null) {
370                                                 marquee_timer.Stop ();
371                                         }
372                                 }
373                         }
374                 }
375
376                 void marquee_timer_Tick (object sender, EventArgs e)
377                 {
378                         Invalidate ();
379                 }
380                 
381                 int marquee_animation_speed = 100;
382                 [DefaultValue (100)]
383                 public int MarqueeAnimationSpeed {
384                         get {
385                                 return marquee_animation_speed;
386                         }
387
388                         set {
389                                 marquee_animation_speed = value;
390                         }
391                 }
392 #endif
393
394                 [Browsable (false)]
395                 [EditorBrowsable (EditorBrowsableState.Never)]
396                 public new bool TabStop
397                 {
398                         get { return base.TabStop; }
399                         set { base.TabStop = value; }
400                 }
401
402                 [Browsable (false)]
403                 [EditorBrowsable (EditorBrowsableState.Never)]
404                 [Bindable(false)]
405                 public override string Text
406                 {
407                         get { return base.Text; }
408                         set { base.Text = value; }
409                 }
410
411                 [Bindable(true)]
412                 [DefaultValue (0)]
413                 public int Value
414                 {
415                         get {
416                                 return val;
417                         }
418                         set {
419                                 if (value < Minimum || value > Maximum)
420 #if NET_2_0
421                                         throw new ArgumentOutOfRangeException ("Value", string.Format("'{0}' is not a valid value for 'Value'. 'Value' should be between 'Minimum' and 'Maximum'", value));
422 #else
423                                         throw new ArgumentException(string.Format("'{0}' is not a valid value for 'Value'. 'Value' should be between 'Minimum' and 'Maximum'", value));
424 #endif
425                                 val = value;
426                                 Refresh ();
427                         }
428                 }
429
430
431                 #endregion      // Protected Instance Properties
432
433                 #region Public Instance Methods
434                 
435                 protected override void CreateHandle ()
436                 {
437                         base.CreateHandle ();
438                 }
439
440                 public void Increment (int value)
441                 {
442                         int newValue = Value + value;
443
444                         if (newValue < Minimum)
445                                 newValue = Minimum;
446
447                         if (newValue > Maximum)
448                                 newValue = Maximum;
449
450                         Value = newValue;
451                         Refresh ();
452                 }
453
454                 protected override void OnHandleCreated (EventArgs e)
455                 {
456                         base.OnHandleCreated (e);
457
458                         UpdateAreas ();
459                 }
460
461                 public void PerformStep ()
462                 {
463                         if (Value >= Maximum)
464                                 return;
465
466                         Value = Maximum < Value + Step ? Maximum : Value + Step;
467                         Refresh ();     // FIXME - calculate delta and only expose that
468                 }
469
470 #if NET_2_0
471                 public override void ResetForeColor ()
472                 {
473                         ForeColor = defaultForeColor;
474                 }
475 #endif
476
477                 public override string ToString()
478                 {
479                         return string.Format ("{0}, Minimum: {1}, Maximum: {2}, Value: {3}",
480                                 GetType().FullName.ToString (),
481                                 Minimum.ToString (),
482                                 Maximum.ToString (),
483                                 Value.ToString () );
484                 }
485
486                 #endregion      // Public Instance Methods
487
488                 #region Private Instance Methods
489
490                 private void UpdateAreas ()
491                 {
492                         client_area.X = client_area.Y = 2;
493                         client_area.Width = Width - 4;
494                         client_area.Height = Height - 4;
495                 }
496
497                 private void OnResizeTB (Object o, EventArgs e)
498                 {
499                         if (Width <= 0 || Height <= 0)
500                                 return;
501
502                         UpdateAreas ();
503                         Invalidate();   // Invalidate the full surface, blocks will not match
504                 }
505
506                 internal override void OnPaintInternal (PaintEventArgs pevent)
507                 {
508                         ThemeEngine.Current.DrawProgressBar (pevent.Graphics, pevent.ClipRectangle, this);
509                 }
510
511                 #endregion
512         }
513 }