ProgressBar.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 [email protected]
  24. //
  25. //
  26. // $Revision: 1.5 $
  27. // $Modtime: $
  28. // $Log: ProgressBar.cs,v $
  29. // Revision 1.5 2004/07/26 17:42:03 jordi
  30. // Theme support
  31. //
  32. // Revision 1.4 2004/07/09 20:13:05 miguel
  33. // Spelling
  34. //
  35. // Revision 1.3 2004/07/09 17:25:23 pbartok
  36. // - Removed usage of Rectangle for drawing. Miguel pointed out it's faster
  37. //
  38. // Revision 1.2 2004/07/09 17:17:46 miguel
  39. // 2004-07-09 Miguel de Icaza <[email protected]>
  40. //
  41. // * ProgressBar.cs: Fixed spelling for `block'
  42. //
  43. // drawProgressBar: renamed to `DrawProgressBar' to follow the coding
  44. // style guidelines.
  45. //
  46. // Avoid using the += on rect.X, that exposed a bug in the compiler.
  47. //
  48. // Revision 1.1 2004/07/09 05:21:25 pbartok
  49. // - Initial check-in
  50. //
  51. //
  52. using System.Drawing;
  53. using System.ComponentModel;
  54. using System.Drawing.Imaging;
  55. using System.Drawing.Drawing2D;
  56. namespace System.Windows.Forms
  57. {
  58. public sealed class ProgressBar : Control
  59. {
  60. #region Local Variables
  61. private int maximum;
  62. private int minimum;
  63. private int step;
  64. private int val;
  65. private Bitmap bmp_mem = null;
  66. private Graphics dc_mem = null;
  67. private Rectangle paint_area = new Rectangle ();
  68. private Rectangle client_area = new Rectangle ();
  69. #endregion // Local Variables
  70. #region Public Constructors
  71. public ProgressBar()
  72. {
  73. maximum = 100;
  74. minimum = 0;
  75. step = 10;
  76. val = 0;
  77. SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
  78. SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
  79. }
  80. #endregion // Public Constructors
  81. #region Public Instance Properties
  82. public int Maximum {
  83. get {
  84. return maximum;
  85. }
  86. set {
  87. if (value < 0)
  88. throw new ArgumentException(
  89. string.Format("Value '{0}' must be greater than or equal to 0.", value ));
  90. maximum = value;
  91. Invalidate ();
  92. }
  93. }
  94. public int Minimum {
  95. get {
  96. return minimum;
  97. }
  98. set {
  99. if (value < 0)
  100. throw new ArgumentException(
  101. string.Format("Value '{0}' must be greater than or equal to 0.", value ));
  102. minimum = value;
  103. Invalidate ();
  104. }
  105. }
  106. public int Step {
  107. get { return step; }
  108. set {
  109. step = value;
  110. Invalidate ();
  111. }
  112. }
  113. public int Value {
  114. get {
  115. return val;
  116. }
  117. set {
  118. if (value < Minimum || value > Maximum)
  119. throw new ArgumentException(
  120. string.Format("'{0}' is not a valid value for 'Value'. 'Value' should be between 'Minimum' and 'Maximum'", value));
  121. val = value;
  122. Invalidate ();
  123. }
  124. }
  125. #endregion // Public Instance Properties
  126. #region Protected Instance Properties
  127. protected override CreateParams CreateParams {
  128. get {
  129. CreateParams createParams = base.CreateParams;
  130. createParams.ClassName = XplatUI.DefaultClassName;
  131. createParams.Style = (int) (
  132. WindowStyles.WS_CHILD |
  133. WindowStyles.WS_VISIBLE);
  134. return createParams;
  135. }
  136. }
  137. protected override ImeMode DefaultImeMode {
  138. get { return ImeMode.Disable; }
  139. }
  140. protected override Size DefaultSize {
  141. get { return new Size(100, 23); }
  142. }
  143. #endregion // Protected Instance Properties
  144. #region Public Instance Methods
  145. public void Increment (int value)
  146. {
  147. int newValue = Value + value;
  148. if (newValue < Minimum)
  149. newValue = Minimum;
  150. if (newValue > Maximum)
  151. newValue = Maximum;
  152. Value = newValue;
  153. Invalidate ();
  154. }
  155. public void PerformStep () {
  156. if (Value >= Maximum)
  157. return;
  158. Value = Value + Step;
  159. Invalidate ();
  160. }
  161. #endregion // Public Instance Methods
  162. private void UpdateAreas ()
  163. {
  164. paint_area.X = paint_area.Y = 0;
  165. paint_area.Width = Width;
  166. paint_area.Height = Height;
  167. client_area.X = client_area.Y = 2;
  168. client_area.Width = Width - 4;
  169. client_area.Height = Height - 4;
  170. }
  171. protected override void OnResize (EventArgs e)
  172. {
  173. //Console.WriteLine ("Onresize");
  174. base.OnResize (e);
  175. if (Width <= 0 || Height <= 0)
  176. return;
  177. UpdateAreas ();
  178. /* Area for double buffering */
  179. bmp_mem = new Bitmap (Width, Height, PixelFormat.Format32bppArgb);
  180. dc_mem = Graphics.FromImage (bmp_mem);
  181. }
  182. protected override void OnHandleCreated (EventArgs e)
  183. {
  184. base.OnHandleCreated(e);
  185. //Console.WriteLine ("OnHandleCreated");
  186. UpdateAreas ();
  187. bmp_mem = new Bitmap (Width, Height, PixelFormat.Format32bppArgb);
  188. dc_mem = Graphics.FromImage (bmp_mem);
  189. draw ();
  190. }
  191. public override string ToString()
  192. {
  193. return string.Format ("{0}, Minimum: {1}, Maximum: {2}, Value: {3}",
  194. GetType().FullName.ToString (),
  195. Maximum.ToString (),
  196. Minimum.ToString (),
  197. Value.ToString () );
  198. }
  199. /* Disable background painting to avoid flickering, since we do our painting*/
  200. protected override void OnPaintBackground (PaintEventArgs pevent)
  201. {
  202. // None
  203. }
  204. private void draw ()
  205. {
  206. int block_width, barpos_pixels;
  207. int steps = (Maximum - Minimum) / step;
  208. block_width = ((client_area.Height) * 2 ) / 3;
  209. barpos_pixels = ((Value - Minimum) * client_area.Width) / (Maximum - Minimum);
  210. //Console.WriteLine ("draw block witdh:{0} barpos: {1}", block_width, barpos_pixels);
  211. //Console.WriteLine ("draw Max {0} Min {1} Value {2}",
  212. // Maximum, Minimum, Value);
  213. ThemeEngine.Current.DrawProgressBar (dc_mem, paint_area, client_area, barpos_pixels,
  214. block_width);
  215. }
  216. protected override void OnPaint (PaintEventArgs pevent)
  217. {
  218. if (Width <= 0 || Height <= 0 || Visible == false)
  219. return;
  220. /* Copies memory drawing buffer to screen*/
  221. draw();
  222. pevent.Graphics.DrawImage (bmp_mem, 0, 0);
  223. }
  224. }
  225. }