ProgressBar.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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-2005 Novell, Inc.
  21. //
  22. // Authors:
  23. // Jordi Mas i Hernandez [email protected]
  24. //
  25. //
  26. using System.Drawing;
  27. using System.ComponentModel;
  28. using System.Drawing.Imaging;
  29. using System.Drawing.Drawing2D;
  30. namespace System.Windows.Forms
  31. {
  32. [DefaultProperty ("Value")]
  33. public sealed class ProgressBar : Control
  34. {
  35. #region Local Variables
  36. private int maximum;
  37. private int minimum;
  38. internal int step;
  39. internal int val;
  40. internal Rectangle client_area = new Rectangle ();
  41. #endregion // Local Variables
  42. #region events
  43. [Browsable (false)]
  44. [EditorBrowsable (EditorBrowsableState.Never)]
  45. public new event EventHandler BackColorChanged;
  46. [Browsable (false)]
  47. [EditorBrowsable (EditorBrowsableState.Never)]
  48. public new event EventHandler BackgroundImageChanged;
  49. [Browsable (false)]
  50. [EditorBrowsable (EditorBrowsableState.Never)]
  51. public new event EventHandler CausesValidationChanged;
  52. [Browsable (false)]
  53. [EditorBrowsable (EditorBrowsableState.Never)]
  54. public new event EventHandler DoubleClick;
  55. [Browsable (false)]
  56. [EditorBrowsable (EditorBrowsableState.Never)]
  57. public new event EventHandler Enter;
  58. [Browsable (false)]
  59. [EditorBrowsable (EditorBrowsableState.Never)]
  60. public new event EventHandler FontChanged;
  61. [Browsable (false)]
  62. [EditorBrowsable (EditorBrowsableState.Never)]
  63. public new event EventHandler ForeColorChanged;
  64. [Browsable (false)]
  65. [EditorBrowsable (EditorBrowsableState.Never)]
  66. public new event EventHandler ImeModeChanged;
  67. [Browsable (false)]
  68. [EditorBrowsable (EditorBrowsableState.Never)]
  69. public new event KeyEventHandler KeyDown;
  70. [Browsable (false)]
  71. [EditorBrowsable (EditorBrowsableState.Never)]
  72. public new event KeyPressEventHandler KeyPress;
  73. [Browsable (false)]
  74. [EditorBrowsable (EditorBrowsableState.Never)]
  75. public new event KeyEventHandler KeyUp;
  76. [Browsable (false)]
  77. [EditorBrowsable (EditorBrowsableState.Never)]
  78. public new event EventHandler Leave;
  79. [Browsable (false)]
  80. [EditorBrowsable (EditorBrowsableState.Never)]
  81. public new event PaintEventHandler Paint;
  82. [Browsable (false)]
  83. [EditorBrowsable (EditorBrowsableState.Never)]
  84. public new event EventHandler RightToLeftChanged;
  85. [Browsable (false)]
  86. [EditorBrowsable (EditorBrowsableState.Never)]
  87. public new event EventHandler TabStopChanged;
  88. [Browsable (false)]
  89. [EditorBrowsable (EditorBrowsableState.Never)]
  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 |
  102. ControlStyles.Selectable |
  103. ControlStyles.ResizeRedraw |
  104. ControlStyles.Opaque, false);
  105. }
  106. #endregion // Public Constructors
  107. #region Public Instance Properties
  108. [Browsable (false)]
  109. [EditorBrowsable (EditorBrowsableState.Never)]
  110. public override bool AllowDrop
  111. {
  112. get { return base.AllowDrop; }
  113. set {
  114. base.AllowDrop = value;
  115. }
  116. }
  117. // Setting this property in MS .Net 1.1 does not have any visual effect and it
  118. // does not fire a BackColorChanged event
  119. [Browsable (false)]
  120. [EditorBrowsable (EditorBrowsableState.Never)]
  121. public override Color BackColor
  122. {
  123. get { return base.BackColor; }
  124. set { base.BackColor = value; }
  125. }
  126. // Setting this property in MS .Net 1.1 does not have any visual effect and it
  127. // does not fire a BackgroundImageChanged event
  128. [Browsable (false)]
  129. [EditorBrowsable (EditorBrowsableState.Never)]
  130. public override Image BackgroundImage
  131. {
  132. get { return base.BackgroundImage; }
  133. set { base.BackgroundImage = value; }
  134. }
  135. [Browsable (false)]
  136. [EditorBrowsable (EditorBrowsableState.Never)]
  137. public new bool CausesValidation
  138. {
  139. get { return base.CausesValidation; }
  140. set {
  141. if (base.CausesValidation == value)
  142. return;
  143. base.CausesValidation = value;
  144. if (CausesValidationChanged != null)
  145. CausesValidationChanged (this, new EventArgs ());
  146. }
  147. }
  148. protected override CreateParams CreateParams
  149. {
  150. get { return base.CreateParams; }
  151. }
  152. protected override ImeMode DefaultImeMode
  153. {
  154. get { return base.DefaultImeMode; }
  155. }
  156. protected override Size DefaultSize
  157. {
  158. get { return ThemeEngine.Current.ProgressBarDefaultSize; }
  159. }
  160. // Setting this property in MS .Net 1.1 does not have any visual effect and it
  161. // does not fire a FontChanged event
  162. [Browsable (false)]
  163. [EditorBrowsable (EditorBrowsableState.Never)]
  164. public override Font Font
  165. {
  166. get { return base.Font; }
  167. set { base.Font = value; }
  168. }
  169. // Setting this property in MS .Net 1.1 does not have any visual effect and it
  170. // does not fire a FontChanged event
  171. [Browsable (false)]
  172. [EditorBrowsable (EditorBrowsableState.Never)]
  173. public override Color ForeColor
  174. {
  175. get { return base.ForeColor; }
  176. set { base.ForeColor = value; }
  177. }
  178. [Browsable (false)]
  179. [EditorBrowsable (EditorBrowsableState.Never)]
  180. public new ImeMode ImeMode
  181. {
  182. get { return base.ImeMode; }
  183. set
  184. {
  185. if (value == base.ImeMode)
  186. return;
  187. base.ImeMode = value;
  188. if (ImeModeChanged != null)
  189. ImeModeChanged (this, EventArgs.Empty);
  190. }
  191. }
  192. [RefreshProperties(RefreshProperties.Repaint)]
  193. [DefaultValue (100)]
  194. public int Maximum
  195. {
  196. get {
  197. return maximum;
  198. }
  199. set {
  200. if (value < 0)
  201. throw new ArgumentException(
  202. string.Format("Value '{0}' must be greater than or equal to 0.", value ));
  203. maximum = value;
  204. Refresh ();
  205. }
  206. }
  207. [RefreshProperties(RefreshProperties.Repaint)]
  208. [DefaultValue (0)]
  209. public int Minimum {
  210. get {
  211. return minimum;
  212. }
  213. set {
  214. if (value < 0)
  215. throw new ArgumentException(
  216. string.Format("Value '{0}' must be greater than or equal to 0.", value ));
  217. minimum = value;
  218. Refresh ();
  219. }
  220. }
  221. [Browsable (false)]
  222. [EditorBrowsable (EditorBrowsableState.Never)]
  223. public override RightToLeft RightToLeft
  224. {
  225. get { return base.RightToLeft; }
  226. set {
  227. if (base.RightToLeft == value)
  228. return;
  229. base.RightToLeft = value;
  230. if (RightToLeftChanged != null)
  231. RightToLeftChanged (this, EventArgs.Empty);
  232. }
  233. }
  234. [DefaultValue (10)]
  235. public int Step
  236. {
  237. get { return step; }
  238. set {
  239. step = value;
  240. Refresh ();
  241. }
  242. }
  243. [Browsable (false)]
  244. [EditorBrowsable (EditorBrowsableState.Never)]
  245. public new bool TabStop
  246. {
  247. get { return base.TabStop; }
  248. set {
  249. if (base.TabStop == value)
  250. return;
  251. base.TabStop = value;
  252. if (TabStopChanged != null)
  253. TabStopChanged (this, EventArgs.Empty);
  254. }
  255. }
  256. [Browsable (false)]
  257. [EditorBrowsable (EditorBrowsableState.Never)]
  258. [Bindable(false)]
  259. public override string Text
  260. {
  261. get { return base.Text; }
  262. set
  263. {
  264. if (value == base.Text)
  265. return;
  266. if (TextChanged != null)
  267. TextChanged (this, EventArgs.Empty);
  268. Refresh ();
  269. }
  270. }
  271. [Bindable(true)]
  272. [DefaultValue (0)]
  273. public int Value
  274. {
  275. get {
  276. return val;
  277. }
  278. set {
  279. if (value < Minimum || value > Maximum)
  280. throw new ArgumentException(
  281. string.Format("'{0}' is not a valid value for 'Value'. 'Value' should be between 'Minimum' and 'Maximum'", value));
  282. val = value;
  283. Refresh ();
  284. }
  285. }
  286. #endregion // Protected Instance Properties
  287. #region Public Instance Methods
  288. protected override void CreateHandle ()
  289. {
  290. base.CreateHandle ();
  291. }
  292. public void Increment (int value)
  293. {
  294. int newValue = Value + value;
  295. if (newValue < Minimum)
  296. newValue = Minimum;
  297. if (newValue > Maximum)
  298. newValue = Maximum;
  299. Value = newValue;
  300. Refresh ();
  301. }
  302. protected override void OnHandleCreated (EventArgs e)
  303. {
  304. base.OnHandleCreated (e);
  305. UpdateAreas ();
  306. }
  307. public void PerformStep ()
  308. {
  309. if (Value >= Maximum)
  310. return;
  311. Value = Value + Step;
  312. Refresh ();
  313. }
  314. public override string ToString()
  315. {
  316. return string.Format ("{0}, Minimum: {1}, Maximum: {2}, Value: {3}",
  317. GetType().FullName.ToString (),
  318. Maximum.ToString (),
  319. Minimum.ToString (),
  320. Value.ToString () );
  321. }
  322. #endregion // Public Instance Methods
  323. #region Private Instance Methods
  324. private void UpdateAreas ()
  325. {
  326. client_area.X = client_area.Y = 2;
  327. client_area.Width = Width - 4;
  328. client_area.Height = Height - 4;
  329. }
  330. private void OnResizeTB (Object o, EventArgs e)
  331. {
  332. if (Width <= 0 || Height <= 0)
  333. return;
  334. UpdateAreas ();
  335. }
  336. private void OnPaintPB (Object o, PaintEventArgs pevent)
  337. {
  338. ThemeEngine.Current.DrawProgressBar (pevent.Graphics, pevent.ClipRectangle, this);
  339. }
  340. #endregion
  341. }
  342. }