ProgressBar.cs 9.1 KB

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