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