ProgressBar.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. using System.Drawing;
  27. using System.ComponentModel;
  28. using System.Drawing.Imaging;
  29. using System.Drawing.Drawing2D;
  30. namespace System.Windows.Forms
  31. {
  32. public sealed class ProgressBar : Control
  33. {
  34. #region Local Variables
  35. private int maximum;
  36. private int minimum;
  37. internal int step;
  38. internal int val;
  39. internal Rectangle paint_area = new Rectangle ();
  40. internal Rectangle client_area = new Rectangle ();
  41. #endregion // Local Variables
  42. #region Events
  43. public new event EventHandler BackColorChanged;
  44. public new event EventHandler BackgroundImageChanged;
  45. public new event EventHandler CausesValidationChanged;
  46. public new event EventHandler DoubleClick;
  47. public new event EventHandler Enter;
  48. public new event EventHandler FontChanged;
  49. public new event EventHandler ForeColorChanged;
  50. public new event EventHandler ImeModeChanged;
  51. public new event KeyEventHandler KeyDown;
  52. public new event KeyPressEventHandler KeyPress;
  53. public new event KeyEventHandler KeyUp;
  54. public new event EventHandler Leave;
  55. public new event PaintEventHandler Paint;
  56. public new event EventHandler RightToLeftChanged;
  57. public new event EventHandler TabStopChanged;
  58. public new event EventHandler TextChanged;
  59. #endregion Events
  60. #region Public Constructors
  61. public ProgressBar()
  62. {
  63. maximum = 100;
  64. minimum = 0;
  65. step = 10;
  66. val = 0;
  67. base.Paint += new PaintEventHandler (OnPaintPB);
  68. base.Resize += new EventHandler (OnResizeTB);
  69. SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
  70. SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
  71. }
  72. #endregion // Public Constructors
  73. #region Public Instance Properties
  74. public override bool AllowDrop
  75. {
  76. get { return base.AllowDrop; }
  77. set {
  78. base.AllowDrop = value;
  79. }
  80. }
  81. // Setting this property in MS .Net 1.1 does not have any visual effect and it
  82. // does not fires a BackColorChanged event
  83. public override Color BackColor
  84. {
  85. get { return base.BackColor; }
  86. set { BackColor = value; }
  87. }
  88. // Setting this property in MS .Net 1.1 does not have any visual effect and it
  89. // does not fires a BackgroundImageChanged event
  90. public override Image BackgroundImage
  91. {
  92. get { return base.BackgroundImage; }
  93. set {BackgroundImage = value; }
  94. }
  95. public new bool CausesValidation
  96. {
  97. get { return base.CausesValidation; }
  98. set {
  99. if (base.CausesValidation == value)
  100. return;
  101. CausesValidation = value;
  102. if (CausesValidationChanged != null)
  103. CausesValidationChanged (this, new EventArgs ());
  104. }
  105. }
  106. protected override CreateParams CreateParams
  107. {
  108. get { return base.CreateParams; }
  109. }
  110. protected override ImeMode DefaultImeMode
  111. {
  112. get { return base.DefaultImeMode; }
  113. }
  114. protected override Size DefaultSize
  115. {
  116. get { return ThemeEngine.Current.ProgressBarDefaultSize; }
  117. }
  118. // Setting this property in MS .Net 1.1 does not have any visual effect and it
  119. // does not fires a FontChanged event
  120. public override Font Font
  121. {
  122. get { return base.Font; }
  123. set { base.Font = value; }
  124. }
  125. // Setting this property in MS .Net 1.1 does not have any visual effect and it
  126. // does not fires a FontChanged event
  127. public override Color ForeColor
  128. {
  129. get { return base.ForeColor; }
  130. set { base.ForeColor = value; }
  131. }
  132. public new ImeMode ImeMode
  133. {
  134. get { return base.ImeMode; }
  135. set
  136. {
  137. if (value == base.ImeMode)
  138. return;
  139. base.ImeMode = value;
  140. if (ImeModeChanged != null)
  141. ImeModeChanged (this, EventArgs.Empty);
  142. }
  143. }
  144. public int Maximum
  145. {
  146. get {
  147. return maximum;
  148. }
  149. set {
  150. if (value < 0)
  151. throw new ArgumentException(
  152. string.Format("Value '{0}' must be greater than or equal to 0.", value ));
  153. maximum = value;
  154. Refresh ();
  155. }
  156. }
  157. public int Minimum {
  158. get {
  159. return minimum;
  160. }
  161. set {
  162. if (value < 0)
  163. throw new ArgumentException(
  164. string.Format("Value '{0}' must be greater than or equal to 0.", value ));
  165. minimum = value;
  166. Refresh ();
  167. }
  168. }
  169. public override RightToLeft RightToLeft
  170. {
  171. get { return base.RightToLeft; }
  172. set {
  173. if (base.RightToLeft == value)
  174. return;
  175. base.RightToLeft = value;
  176. if (RightToLeftChanged != null)
  177. RightToLeftChanged (this, EventArgs.Empty);
  178. }
  179. }
  180. public int Step
  181. {
  182. get { return step; }
  183. set {
  184. step = value;
  185. Refresh ();
  186. }
  187. }
  188. public new bool TabStop
  189. {
  190. get { return base.TabStop; }
  191. set {
  192. if (base.TabStop == value)
  193. return;
  194. base.TabStop = value;
  195. if (TabStopChanged != null)
  196. TabStopChanged (this, EventArgs.Empty);
  197. }
  198. }
  199. public override string Text
  200. {
  201. get { return base.Text; }
  202. set
  203. {
  204. if (value == base.Text)
  205. return;
  206. if (TextChanged != null)
  207. TextChanged (this, EventArgs.Empty);
  208. Refresh ();
  209. }
  210. }
  211. public int Value
  212. {
  213. get {
  214. return val;
  215. }
  216. set {
  217. if (value < Minimum || value > Maximum)
  218. throw new ArgumentException(
  219. string.Format("'{0}' is not a valid value for 'Value'. 'Value' should be between 'Minimum' and 'Maximum'", value));
  220. val = value;
  221. Refresh ();
  222. }
  223. }
  224. #endregion // Protected Instance Properties
  225. #region Public Instance Methods
  226. public void Increment (int value)
  227. {
  228. int newValue = Value + value;
  229. if (newValue < Minimum)
  230. newValue = Minimum;
  231. if (newValue > Maximum)
  232. newValue = Maximum;
  233. Value = newValue;
  234. Refresh ();
  235. }
  236. protected override void OnHandleCreated (EventArgs e)
  237. {
  238. base.OnHandleCreated (e);
  239. UpdateAreas ();
  240. CreateBuffers (Width, Height);
  241. Draw ();
  242. }
  243. public void PerformStep ()
  244. {
  245. if (Value >= Maximum)
  246. return;
  247. Value = Value + Step;
  248. Refresh ();
  249. }
  250. public override string ToString()
  251. {
  252. return string.Format ("{0}, Minimum: {1}, Maximum: {2}, Value: {3}",
  253. GetType().FullName.ToString (),
  254. Maximum.ToString (),
  255. Minimum.ToString (),
  256. Value.ToString () );
  257. }
  258. #endregion // Public Instance Methods
  259. #region Private Instance Methods
  260. private void UpdateAreas ()
  261. {
  262. paint_area.X = paint_area.Y = 0;
  263. paint_area.Width = Width;
  264. paint_area.Height = Height;
  265. client_area.X = client_area.Y = 2;
  266. client_area.Width = Width - 4;
  267. client_area.Height = Height - 4;
  268. }
  269. private void OnResizeTB (Object o, EventArgs e)
  270. {
  271. if (Width <= 0 || Height <= 0)
  272. return;
  273. UpdateAreas ();
  274. }
  275. private void Draw ()
  276. {
  277. ThemeEngine.Current.DrawProgressBar (DeviceContext, this.ClientRectangle, this);
  278. }
  279. private void OnPaintPB (Object o, PaintEventArgs pevent)
  280. {
  281. if (Width <= 0 || Height <= 0 || Visible == false)
  282. return;
  283. /* Copies memory drawing buffer to screen*/
  284. Draw ();
  285. pevent.Graphics.DrawImage (ImageBuffer, 0, 0);
  286. }
  287. #endregion
  288. }
  289. }