ProgressBar.cs 9.5 KB

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