ProgressBar.cs 9.4 KB

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