* ImageList.cs: When the image stream is set pull all the images
[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)]\r
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)]\r
81                 public new event KeyEventHandler KeyDown;
82                 
83                 [Browsable (false)]
84                 [EditorBrowsable (EditorBrowsableState.Never)]\r
85                 public new event KeyPressEventHandler KeyPress;
86                 
87                 [Browsable (false)]
88                 [EditorBrowsable (EditorBrowsableState.Never)]\r
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)]\r
101                 public new event EventHandler RightToLeftChanged;
102                 
103                 [Browsable (false)]
104                 [EditorBrowsable (EditorBrowsableState.Never)]\r
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 | ControlStyles.AllPaintingInWmPaint, true);
124                         SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
125                 }
126                 #endregion      // Public Constructors
127
128                 #region Public Instance Properties\r
129
130                 [Browsable (false)]
131                 [EditorBrowsable (EditorBrowsableState.Never)]\r
132                 public override bool AllowDrop\r
133                 {\r
134                         get { return base.AllowDrop; }
135                         set {
136                                 base.AllowDrop = value;
137                         }\r
138                 }\r
139 \r
140                 // Setting this property in MS .Net 1.1 does not have any visual effect and it\r
141                 // does not fire a BackColorChanged event
142                 [Browsable (false)]
143                 [EditorBrowsable (EditorBrowsableState.Never)]\r
144                 public override Color BackColor\r
145                 {\r
146                         get { return base.BackColor; }
147                         set { BackColor = value; }
148                 }
149
150                 // Setting this property in MS .Net 1.1 does not have any visual effect and it\r
151                 // does not fire a BackgroundImageChanged event
152                 [Browsable (false)]
153                 [EditorBrowsable (EditorBrowsableState.Never)]\r
154                 public override Image BackgroundImage\r
155                 {\r
156                         get { return base.BackgroundImage; }
157                         set {BackgroundImage = value; }
158                 }
159
160                 [Browsable (false)]
161                 [EditorBrowsable (EditorBrowsableState.Never)]\r
162                 public new bool CausesValidation\r
163                 {\r
164                         get { return base.CausesValidation; }
165                         set {
166                                 if (base.CausesValidation == value)
167                                         return;
168
169                                 CausesValidation = value;
170                                 if (CausesValidationChanged != null)
171                                         CausesValidationChanged (this, new EventArgs ());
172                         }
173                 }
174
175                 protected override CreateParams CreateParams\r
176                 {
177                         get { return base.CreateParams; }
178                 }
179
180                 protected override ImeMode DefaultImeMode\r
181                 {
182                         get { return base.DefaultImeMode; }
183                 }
184
185                 protected override Size DefaultSize\r
186                 {
187                         get { return ThemeEngine.Current.ProgressBarDefaultSize; }
188                 }
189
190                 // Setting this property in MS .Net 1.1 does not have any visual effect and it\r
191                 // does not fire a FontChanged event
192                 [Browsable (false)]
193                 [EditorBrowsable (EditorBrowsableState.Never)]
194                 public override Font Font\r
195                 {
196                         get { return base.Font; }
197                         set { base.Font = value; }
198                 }
199
200                 // Setting this property in MS .Net 1.1 does not have any visual effect and it\r
201                 // does not fire a FontChanged event
202                 [Browsable (false)]
203                 [EditorBrowsable (EditorBrowsableState.Never)]
204                 public override Color ForeColor\r
205                 {
206                         get { return base.ForeColor; }
207                         set { base.ForeColor = value; }
208                 }
209
210                 [Browsable (false)]
211                 [EditorBrowsable (EditorBrowsableState.Never)]
212                 public new ImeMode ImeMode\r
213                 {
214                         get { return base.ImeMode; }
215                         set\r
216                         {
217                                 if (value == base.ImeMode)
218                                         return;
219
220                                 base.ImeMode = value;
221                                 if (ImeModeChanged != null)
222                                         ImeModeChanged (this, EventArgs.Empty);
223                         }
224                 }
225
226                 [RefreshProperties(RefreshProperties.Repaint)]
227                 [DefaultValue (100)]
228                 public int Maximum\r
229                 {
230                         get {
231                                 return maximum;
232                         }
233                         set {
234                                 if (value < 0)
235                                         throw new ArgumentException(
236                                                 string.Format("Value '{0}' must be greater than or equal to 0.", value ));
237
238                                 maximum = value;
239                                 Refresh ();
240                         }
241                 }
242
243                 [RefreshProperties(RefreshProperties.Repaint)]
244                 [DefaultValue (0)]
245                 public int Minimum {
246                         get {
247                                 return minimum;
248                         }
249                         set {
250                                 if (value < 0)
251                                         throw new ArgumentException(
252                                                 string.Format("Value '{0}' must be greater than or equal to 0.", value ));
253
254                                 minimum = value;
255                                 Refresh ();
256                         }
257                 }
258
259                 [Browsable (false)]
260                 [EditorBrowsable (EditorBrowsableState.Never)]
261                 public override RightToLeft RightToLeft\r
262                 {
263                         get { return base.RightToLeft; }
264                         set {
265                                 if (base.RightToLeft == value)
266                                         return;
267
268                                 base.RightToLeft = value;
269
270                                 if (RightToLeftChanged != null)
271                                         RightToLeftChanged (this, EventArgs.Empty);
272
273                         }
274                 }
275
276                 [DefaultValue (10)]
277                 public int Step\r
278                 {
279                         get { return step; }
280                         set {
281                                 step = value;
282                                 Refresh ();
283                         }
284                 }
285
286                 [Browsable (false)]
287                 [EditorBrowsable (EditorBrowsableState.Never)]
288                 public new bool TabStop\r
289                 {
290                         get { return base.TabStop; }
291                         set {
292                                 if (base.TabStop == value)
293                                         return;
294
295                                 base.TabStop = value;
296
297                                 if (TabStopChanged != null)
298                                         TabStopChanged (this, EventArgs.Empty);
299
300                         }
301                 }
302
303                 [Browsable (false)]
304                 [EditorBrowsable (EditorBrowsableState.Never)]
305                 [Bindable(false)]
306                 public override string Text\r
307                 {
308                         get { return base.Text; }
309                         set\r
310                         {
311                                 if (value == base.Text)
312                                         return;
313
314                                 if (TextChanged != null)
315                                         TextChanged (this, EventArgs.Empty);
316
317                                 Refresh ();
318                         }
319                 }
320
321                 [Bindable(true)]
322                 [DefaultValue (0)]
323                 public int Value\r
324                 {
325                         get {
326                                 return val;
327                         }
328                         set {
329                                 if (value < Minimum || value > Maximum)
330                                         throw new ArgumentException(
331                                                 string.Format("'{0}' is not a valid value for 'Value'. 'Value' should be between 'Minimum' and 'Maximum'", value));
332
333                                 val = value;
334                                 Refresh ();
335                         }
336                 }
337
338
339                 #endregion      // Protected Instance Properties
340
341                 #region Public Instance Methods
342                 
343                 protected override void CreateHandle ()
344                 {
345                         base.CreateHandle ();
346                 }
347
348                 public void Increment (int value)
349                 {
350                         int newValue = Value + value;
351
352                         if (newValue < Minimum)
353                                 newValue = Minimum;
354
355                         if (newValue > Maximum)
356                                 newValue = Maximum;
357
358                         Value = newValue;
359                         Refresh ();
360                 }
361
362                 protected override void OnHandleCreated (EventArgs e)
363                 {
364                         base.OnHandleCreated (e);
365
366                         UpdateAreas ();
367
368                         CreateBuffers (Width, Height);
369                         
370                 }
371
372                 public void PerformStep ()
373                 {
374                         if (Value >= Maximum)
375                                 return;
376
377                         Value = Value + Step;
378                         Refresh ();
379                 }
380
381                 public override string ToString()
382                 {
383                         return string.Format ("{0}, Minimum: {1}, Maximum: {2}, Value: {3}",
384                                 GetType().FullName.ToString (),
385                                 Maximum.ToString (),
386                                 Minimum.ToString (),
387                                 Value.ToString () );
388                 }
389
390                 #endregion      // Public Instance Methods
391
392                 #region Private Instance Methods
393                 private void UpdateAreas ()
394                 {
395                         client_area.X = client_area.Y = 2;
396                         client_area.Width = Width - 4;
397                         client_area.Height = Height - 4;
398                 }
399
400                 private void OnResizeTB (Object o, EventArgs e)
401                 {
402                         if (Width <= 0 || Height <= 0)
403                                 return;
404
405                         UpdateAreas ();
406                 }
407
408                 private void Draw (Rectangle clip)
409                 {
410                         ThemeEngine.Current.DrawProgressBar (DeviceContext, clip, this);
411                 }
412
413                 private void OnPaintPB (Object o, PaintEventArgs pevent)
414                 {
415                         if (Width <= 0 || Height <=  0 || Visible == false)
416                                 return;
417
418                         /* Copies memory drawing buffer to screen*/
419                         Draw (pevent.ClipRectangle);
420                         pevent.Graphics.DrawImage (ImageBuffer, pevent.ClipRectangle, pevent.ClipRectangle, GraphicsUnit.Pixel);
421                 }               
422                 
423                 #endregion
424         }
425 }