* X11Keyboard.cs: Detect and use the num lock mask.
[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 Novell, Inc.
21 //
22 // Autors:
23 //              Jordi Mas i Hernandez   jordi@ximian.com
24 //
25 //
26 // $Revision: 1.9 $
27 // $Modtime: $
28 // $Log: ProgressBar.cs,v $
29 // Revision 1.9  2004/10/05 04:56:11  jackson
30 // Let the base Control handle the buffers, derived classes should not have to CreateBuffers themselves.
31 //
32 // Revision 1.8  2004/09/28 18:44:25  pbartok
33 // - Streamlined Theme interfaces:
34 //   * Each DrawXXX method for a control now is passed the object for the
35 //     control to be drawn in order to allow accessing any state the theme
36 //     might require
37 //
38 //   * ControlPaint methods for the theme now have a CP prefix to avoid
39 //     name clashes with the Draw methods for controls
40 //
41 //   * Every control now retrieves it's DefaultSize from the current theme
42 //
43 // Revision 1.7  2004/08/25 18:29:14  jordi
44 // new methods, properties, and fixes for progressbar
45 //
46 // Revision 1.6  2004/08/10 15:41:50  jackson
47 // Allow control to handle buffering
48 //
49 // Revision 1.5  2004/07/26 17:42:03  jordi
50 // Theme support
51 //
52 // Revision 1.4  2004/07/09 20:13:05  miguel
53 // Spelling
54 //
55 // Revision 1.3  2004/07/09 17:25:23  pbartok
56 // - Removed usage of Rectangle for drawing. Miguel pointed out it's faster
57 //
58 // Revision 1.2  2004/07/09 17:17:46  miguel
59 // 2004-07-09  Miguel de Icaza  <miguel@ximian.com>
60 //
61 //      * ProgressBar.cs: Fixed spelling for `block'
62 //
63 //      drawProgressBar: renamed to `DrawProgressBar' to follow the coding
64 //      style guidelines.
65 //
66 //      Avoid using the += on rect.X, that exposed a bug in the compiler.
67 //
68 // Revision 1.1  2004/07/09 05:21:25  pbartok
69 // - Initial check-in
70 //
71 //
72
73 using System.Drawing;
74 using System.ComponentModel;
75 using System.Drawing.Imaging;
76 using System.Drawing.Drawing2D;
77
78 namespace System.Windows.Forms
79 {
80         public sealed class ProgressBar : Control
81         {
82                 #region Local Variables
83                 private int maximum;
84                 private int minimum;
85                 internal int step;
86                 internal int val;
87                 internal Rectangle paint_area = new Rectangle ();
88                 internal Rectangle client_area = new Rectangle ();
89                 #endregion      // Local Variables
90
91                 #region Events
92                 public new event EventHandler BackColorChanged;
93                 public new event EventHandler BackgroundImageChanged;\r
94                 public new event EventHandler CausesValidationChanged;
95                 public new event EventHandler DoubleClick;
96                 public new event EventHandler Enter;
97                 public new event EventHandler FontChanged;
98                 public new event EventHandler ForeColorChanged;
99                 public new event EventHandler ImeModeChanged;\r
100                 public new event KeyEventHandler KeyDown;\r
101                 public new event KeyPressEventHandler KeyPress;\r
102                 public new event KeyEventHandler KeyUp;
103                 public new event EventHandler Leave;
104                 public new event PaintEventHandler Paint;\r
105                 public new event EventHandler RightToLeftChanged;\r
106                 public new event EventHandler TabStopChanged;
107                 public new event EventHandler TextChanged;
108                 #endregion Events
109
110                 #region Public Constructors
111                 public ProgressBar()
112                 {
113                         maximum = 100;
114                         minimum = 0;
115                         step = 10;
116                         val = 0;
117
118                         base.Paint += new PaintEventHandler (OnPaintPB);
119                         base.Resize += new EventHandler (OnResizeTB);
120
121                         SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
122                         SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
123                 }
124                 #endregion      // Public Constructors
125
126                 #region Public Instance Properties\r
127 \r
128                 public override bool AllowDrop\r
129                 {\r
130                         get { return base.AllowDrop; }
131                         set {
132                                 base.AllowDrop = value;
133                         }\r
134                 }\r
135 \r
136                 // Setting this property in MS .Net 1.1 does not have any visual effect and it\r
137                 // does not fires a BackColorChanged event\r
138                 public override Color BackColor\r
139                 {\r
140                         get { return base.BackColor; }
141                         set { BackColor = value; }
142                 }
143
144                 // Setting this property in MS .Net 1.1 does not have any visual effect and it\r
145                 // does not fires a BackgroundImageChanged event\r
146                 public override Image BackgroundImage\r
147                 {\r
148                         get { return base.BackgroundImage; }
149                         set {BackgroundImage = value; }
150                 }
151 \r
152                 public new bool CausesValidation\r
153                 {\r
154                         get { return base.CausesValidation; }
155                         set {
156                                 if (base.CausesValidation == value)
157                                         return;
158
159                                 CausesValidation = value;
160                                 if (CausesValidationChanged != null)
161                                         CausesValidationChanged (this, new EventArgs ());
162                         }
163                 }
164
165                 protected override CreateParams CreateParams\r
166                 {
167                         get { return base.CreateParams; }
168                 }
169
170                 protected override ImeMode DefaultImeMode\r
171                 {
172                         get { return base.DefaultImeMode; }
173                 }
174
175                 protected override Size DefaultSize\r
176                 {
177                         get { return ThemeEngine.Current.ProgressBarDefaultSize; }
178                 }
179
180                 // Setting this property in MS .Net 1.1 does not have any visual effect and it\r
181                 // does not fires a FontChanged event
182                 public override Font Font\r
183                 {
184                         get { return base.Font; }
185                         set { base.Font = value; }
186                 }
187
188                 // Setting this property in MS .Net 1.1 does not have any visual effect and it\r
189                 // does not fires a FontChanged event
190                 public override Color ForeColor\r
191                 {
192                         get { return base.ForeColor; }
193                         set { base.ForeColor = value; }
194                 }
195
196                 public new ImeMode ImeMode\r
197                 {
198                         get { return base.ImeMode; }
199                         set\r
200                         {
201                                 if (value == base.ImeMode)
202                                         return;
203
204                                 base.ImeMode = value;
205                                 if (ImeModeChanged != null)
206                                         ImeModeChanged (this, EventArgs.Empty);
207                         }
208                 }
209
210                 public int Maximum\r
211                 {
212                         get {
213                                 return maximum;
214                         }
215                         set {
216                                 if (value < 0)
217                                         throw new ArgumentException(
218                                                 string.Format("Value '{0}' must be greater than or equal to 0.", value ));
219
220                                 maximum = value;
221                                 Refresh ();
222                         }
223                 }
224
225                 public int Minimum {
226                         get {
227                                 return minimum;
228                         }
229                         set {
230                                 if (value < 0)
231                                         throw new ArgumentException(
232                                                 string.Format("Value '{0}' must be greater than or equal to 0.", value ));
233
234                                 minimum = value;
235                                 Refresh ();
236                         }
237                 }
238
239                 public override RightToLeft RightToLeft\r
240                 {
241                         get { return base.RightToLeft; }
242                         set {
243                                 if (base.RightToLeft == value)
244                                         return;
245
246                                 base.RightToLeft = value;
247
248                                 if (RightToLeftChanged != null)
249                                         RightToLeftChanged (this, EventArgs.Empty);
250
251                         }
252                 }
253
254                 public int Step\r
255                 {
256                         get { return step; }
257                         set {
258                                 step = value;
259                                 Refresh ();
260                         }
261                 }
262
263                 public new bool TabStop\r
264                 {
265                         get { return base.TabStop; }
266                         set {
267                                 if (base.TabStop == value)
268                                         return;
269
270                                 base.TabStop = value;
271
272                                 if (TabStopChanged != null)
273                                         TabStopChanged (this, EventArgs.Empty);
274
275                         }
276                 }
277
278                 public override string Text\r
279                 {
280                         get { return base.Text; }
281                         set\r
282                         {
283                                 if (value == base.Text)
284                                         return;
285
286                                 if (TextChanged != null)
287                                         TextChanged (this, EventArgs.Empty);
288
289                                 Refresh ();
290                         }
291                 }
292
293
294                 public int Value\r
295                 {
296                         get {
297                                 return val;
298                         }
299                         set {
300                                 if (value < Minimum || value > Maximum)
301                                         throw new ArgumentException(
302                                                 string.Format("'{0}' is not a valid value for 'Value'. 'Value' should be between 'Minimum' and 'Maximum'", value));
303
304                                 val = value;
305                                 Refresh ();
306                         }
307                 }
308
309
310                 #endregion      // Protected Instance Properties
311
312                 #region Public Instance Methods
313
314
315                 public void Increment (int value)
316                 {
317                         int newValue = Value + value;
318
319                         if (newValue < Minimum)
320                                 newValue = Minimum;
321
322                         if (newValue > Maximum)
323                                 newValue = Maximum;
324
325                         Value = newValue;
326                         Refresh ();
327                 }
328
329                 protected override void OnHandleCreated (EventArgs e)
330                 {
331                         base.OnHandleCreated (e);
332
333                         UpdateAreas ();
334
335                         CreateBuffers (Width, Height);
336                         Draw ();
337                 }
338
339                 public void PerformStep ()
340                 {
341                         if (Value >= Maximum)
342                                 return;
343
344                         Value = Value + Step;
345                         Refresh ();
346                 }
347
348                 public override string ToString()
349                 {
350                         return string.Format ("{0}, Minimum: {1}, Maximum: {2}, Value: {3}",
351                                 GetType().FullName.ToString (),
352                                 Maximum.ToString (),
353                                 Minimum.ToString (),
354                                 Value.ToString () );
355                 }
356
357                 #endregion      // Public Instance Methods
358
359                 #region Private Instance Methods
360                 private void UpdateAreas ()
361                 {
362                         paint_area.X = paint_area.Y = 0;
363                         paint_area.Width = Width;
364                         paint_area.Height = Height;
365
366                         client_area.X = client_area.Y = 2;
367                         client_area.Width = Width - 4;
368                         client_area.Height = Height - 4;
369                 }
370
371                 private void OnResizeTB (Object o, EventArgs e)
372                 {
373                         if (Width <= 0 || Height <= 0)
374                                 return;
375
376                         UpdateAreas ();
377                 }
378
379                 /* Disable background painting to avoid flickering, since we do our painting*/
380                 protected override void OnPaintBackground (PaintEventArgs pevent)
381                 {
382                         // None
383                 }
384
385                 private void Draw ()
386                 {
387                         ThemeEngine.Current.DrawProgressBar (DeviceContext, this.ClientRectangle, this);
388                 }
389
390                 private void OnPaintPB (Object o, PaintEventArgs pevent)
391                 {
392                         if (Width <= 0 || Height <=  0 || Visible == false)
393                                 return;
394
395                         /* Copies memory drawing buffer to screen*/
396                         Draw ();
397                         pevent.Graphics.DrawImage (ImageBuffer, 0, 0);
398                 }
399
400                 #endregion
401         }
402 }