41eab550dd9179bd66ee5425414cccad3ef13600
[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
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         public sealed class ProgressBar : Control
36         {
37                 #region Local Variables
38                 private int maximum;
39                 private int minimum;
40                 internal int step;
41                 internal int val;
42                 internal Rectangle paint_area = new Rectangle ();
43                 internal Rectangle client_area = new Rectangle ();
44                 #endregion      // Local Variables
45
46                 #region Events
47                 public new event EventHandler BackColorChanged;
48                 public new event EventHandler BackgroundImageChanged;\r
49                 public new event EventHandler CausesValidationChanged;
50                 public new event EventHandler DoubleClick;
51                 public new event EventHandler Enter;
52                 public new event EventHandler FontChanged;
53                 public new event EventHandler ForeColorChanged;
54                 public new event EventHandler ImeModeChanged;\r
55                 public new event KeyEventHandler KeyDown;\r
56                 public new event KeyPressEventHandler KeyPress;\r
57                 public new event KeyEventHandler KeyUp;
58                 public new event EventHandler Leave;
59                 public new event PaintEventHandler Paint;\r
60                 public new event EventHandler RightToLeftChanged;\r
61                 public new event EventHandler TabStopChanged;
62                 public new event EventHandler TextChanged;
63                 #endregion Events
64
65                 #region Public Constructors
66                 public ProgressBar()
67                 {
68                         maximum = 100;
69                         minimum = 0;
70                         step = 10;
71                         val = 0;
72
73                         base.Paint += new PaintEventHandler (OnPaintPB);
74                         base.Resize += new EventHandler (OnResizeTB);
75
76                         SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
77                         SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
78                 }
79                 #endregion      // Public Constructors
80
81                 #region Public Instance Properties\r
82 \r
83                 public override bool AllowDrop\r
84                 {\r
85                         get { return base.AllowDrop; }
86                         set {
87                                 base.AllowDrop = value;
88                         }\r
89                 }\r
90 \r
91                 // Setting this property in MS .Net 1.1 does not have any visual effect and it\r
92                 // does not fires a BackColorChanged event\r
93                 public override Color BackColor\r
94                 {\r
95                         get { return base.BackColor; }
96                         set { BackColor = value; }
97                 }
98
99                 // Setting this property in MS .Net 1.1 does not have any visual effect and it\r
100                 // does not fires a BackgroundImageChanged event\r
101                 public override Image BackgroundImage\r
102                 {\r
103                         get { return base.BackgroundImage; }
104                         set {BackgroundImage = value; }
105                 }
106 \r
107                 public new bool CausesValidation\r
108                 {\r
109                         get { return base.CausesValidation; }
110                         set {
111                                 if (base.CausesValidation == value)
112                                         return;
113
114                                 CausesValidation = value;
115                                 if (CausesValidationChanged != null)
116                                         CausesValidationChanged (this, new EventArgs ());
117                         }
118                 }
119
120                 protected override CreateParams CreateParams\r
121                 {
122                         get { return base.CreateParams; }
123                 }
124
125                 protected override ImeMode DefaultImeMode\r
126                 {
127                         get { return base.DefaultImeMode; }
128                 }
129
130                 protected override Size DefaultSize\r
131                 {
132                         get { return ThemeEngine.Current.ProgressBarDefaultSize; }
133                 }
134
135                 // Setting this property in MS .Net 1.1 does not have any visual effect and it\r
136                 // does not fires a FontChanged event
137                 public override Font Font\r
138                 {
139                         get { return base.Font; }
140                         set { base.Font = value; }
141                 }
142
143                 // Setting this property in MS .Net 1.1 does not have any visual effect and it\r
144                 // does not fires a FontChanged event
145                 public override Color ForeColor\r
146                 {
147                         get { return base.ForeColor; }
148                         set { base.ForeColor = value; }
149                 }
150
151                 public new ImeMode ImeMode\r
152                 {
153                         get { return base.ImeMode; }
154                         set\r
155                         {
156                                 if (value == base.ImeMode)
157                                         return;
158
159                                 base.ImeMode = value;
160                                 if (ImeModeChanged != null)
161                                         ImeModeChanged (this, EventArgs.Empty);
162                         }
163                 }
164
165                 public int Maximum\r
166                 {
167                         get {
168                                 return maximum;
169                         }
170                         set {
171                                 if (value < 0)
172                                         throw new ArgumentException(
173                                                 string.Format("Value '{0}' must be greater than or equal to 0.", value ));
174
175                                 maximum = value;
176                                 Refresh ();
177                         }
178                 }
179
180                 public int Minimum {
181                         get {
182                                 return minimum;
183                         }
184                         set {
185                                 if (value < 0)
186                                         throw new ArgumentException(
187                                                 string.Format("Value '{0}' must be greater than or equal to 0.", value ));
188
189                                 minimum = value;
190                                 Refresh ();
191                         }
192                 }
193
194                 public override RightToLeft RightToLeft\r
195                 {
196                         get { return base.RightToLeft; }
197                         set {
198                                 if (base.RightToLeft == value)
199                                         return;
200
201                                 base.RightToLeft = value;
202
203                                 if (RightToLeftChanged != null)
204                                         RightToLeftChanged (this, EventArgs.Empty);
205
206                         }
207                 }
208
209                 public int Step\r
210                 {
211                         get { return step; }
212                         set {
213                                 step = value;
214                                 Refresh ();
215                         }
216                 }
217
218                 public new bool TabStop\r
219                 {
220                         get { return base.TabStop; }
221                         set {
222                                 if (base.TabStop == value)
223                                         return;
224
225                                 base.TabStop = value;
226
227                                 if (TabStopChanged != null)
228                                         TabStopChanged (this, EventArgs.Empty);
229
230                         }
231                 }
232
233                 public override string Text\r
234                 {
235                         get { return base.Text; }
236                         set\r
237                         {
238                                 if (value == base.Text)
239                                         return;
240
241                                 if (TextChanged != null)
242                                         TextChanged (this, EventArgs.Empty);
243
244                                 Refresh ();
245                         }
246                 }
247
248
249                 public int Value\r
250                 {
251                         get {
252                                 return val;
253                         }
254                         set {
255                                 if (value < Minimum || value > Maximum)
256                                         throw new ArgumentException(
257                                                 string.Format("'{0}' is not a valid value for 'Value'. 'Value' should be between 'Minimum' and 'Maximum'", value));
258
259                                 val = value;
260                                 Refresh ();
261                         }
262                 }
263
264
265                 #endregion      // Protected Instance Properties
266
267                 #region Public Instance Methods
268
269
270                 public void Increment (int value)
271                 {
272                         int newValue = Value + value;
273
274                         if (newValue < Minimum)
275                                 newValue = Minimum;
276
277                         if (newValue > Maximum)
278                                 newValue = Maximum;
279
280                         Value = newValue;
281                         Refresh ();
282                 }
283
284                 protected override void OnHandleCreated (EventArgs e)
285                 {
286                         base.OnHandleCreated (e);
287
288                         UpdateAreas ();
289
290                         CreateBuffers (Width, Height);
291                         Draw ();
292                 }
293
294                 public void PerformStep ()
295                 {
296                         if (Value >= Maximum)
297                                 return;
298
299                         Value = Value + Step;
300                         Refresh ();
301                 }
302
303                 public override string ToString()
304                 {
305                         return string.Format ("{0}, Minimum: {1}, Maximum: {2}, Value: {3}",
306                                 GetType().FullName.ToString (),
307                                 Maximum.ToString (),
308                                 Minimum.ToString (),
309                                 Value.ToString () );
310                 }
311
312                 #endregion      // Public Instance Methods
313
314                 #region Private Instance Methods
315                 private void UpdateAreas ()
316                 {
317                         paint_area.X = paint_area.Y = 0;
318                         paint_area.Width = Width;
319                         paint_area.Height = Height;
320
321                         client_area.X = client_area.Y = 2;
322                         client_area.Width = Width - 4;
323                         client_area.Height = Height - 4;
324                 }
325
326                 private void OnResizeTB (Object o, EventArgs e)
327                 {
328                         if (Width <= 0 || Height <= 0)
329                                 return;
330
331                         UpdateAreas ();
332                 }
333
334                 private void Draw ()
335                 {
336                         ThemeEngine.Current.DrawProgressBar (DeviceContext, this.ClientRectangle, this);
337                 }
338
339                 private void OnPaintPB (Object o, PaintEventArgs pevent)
340                 {
341                         if (Width <= 0 || Height <=  0 || Visible == false)
342                                 return;
343
344                         /* Copies memory drawing buffer to screen*/
345                         Draw ();
346                         pevent.Graphics.DrawImage (ImageBuffer, 0, 0);
347                 }
348
349                 #endregion
350         }
351 }