ProgressBar.cs 6.6 KB

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