TrackBar.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. //
  2. // System.Windows.Forms.TrackBar.cs
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. // Autors:
  24. // Jordi Mas i Hernandez, [email protected]
  25. //
  26. // TODO:
  27. // - The AutoSize functionality seems quite broken for vertical controls in .Net 1.1. Not
  28. // sure if we are implementing it the right way.
  29. //
  30. // Copyright (C) Novell Inc., 2004
  31. //
  32. //
  33. // $Revision: 1.10 $
  34. // $Modtime: $
  35. // $Log: TrackBar.cs,v $
  36. // Revision 1.10 2004/08/13 20:55:20 jordi
  37. // change from wndproc to events
  38. //
  39. // Revision 1.9 2004/08/13 18:46:26 jordi
  40. // adds timer and grap window
  41. //
  42. // Revision 1.8 2004/08/12 20:29:01 jordi
  43. // Trackbar enhancement, fix mouse problems, highli thumb, etc
  44. //
  45. // Revision 1.7 2004/08/10 23:27:12 jordi
  46. // add missing methods, properties, and restructure to hide extra ones
  47. //
  48. // Revision 1.6 2004/08/10 15:47:11 jackson
  49. // Allow control to handle buffering
  50. //
  51. // Revision 1.5 2004/08/07 23:32:26 jordi
  52. // throw exceptions of invalid enums values
  53. //
  54. // Revision 1.4 2004/08/06 23:18:06 pbartok
  55. // - Fixed some rounding issues with float/int
  56. //
  57. // Revision 1.3 2004/07/27 15:53:02 jordi
  58. // fixes trackbar events, def classname, methods signature
  59. //
  60. // Revision 1.2 2004/07/26 17:42:03 jordi
  61. // Theme support
  62. //
  63. // Revision 1.1 2004/07/15 09:38:02 jordi
  64. // Horizontal and Vertical TrackBar control implementation
  65. //
  66. //
  67. // NOT COMPLETE
  68. using System.ComponentModel;
  69. using System.Drawing;
  70. using System.Drawing.Imaging;
  71. using System.Drawing.Drawing2D;
  72. using System.Timers;
  73. namespace System.Windows.Forms
  74. {
  75. public class TrackBar : Control, ISupportInitialize
  76. {
  77. private int minimum;
  78. private int maximum;
  79. private int tickFrequency;
  80. private bool autosize;
  81. private int position;
  82. private int smallChange;
  83. private int largeChange;
  84. private Orientation orientation;
  85. private TickStyle tickStyle;
  86. private Rectangle paint_area = new Rectangle ();
  87. private Rectangle thumb_pos = new Rectangle (); /* Current position and size of the thumb */
  88. private Rectangle thumb_area = new Rectangle (); /* Area where the thumb can scroll */
  89. private bool thumb_pressed = false;
  90. private System.Timers.Timer holdclick_timer = new System.Timers.Timer ();
  91. private int thumb_mouseclick;
  92. private bool mouse_clickmove;
  93. #region Events
  94. public event EventHandler Scroll;
  95. public event EventHandler ValueChanged;
  96. #endregion // Events
  97. public TrackBar ()
  98. {
  99. orientation = Orientation.Horizontal;
  100. minimum = 0;
  101. maximum = 10;
  102. tickFrequency = 1;
  103. autosize = true;
  104. position = 0;
  105. tickStyle = TickStyle.BottomRight;
  106. smallChange = 1;
  107. largeChange = 5;
  108. Scroll = null;
  109. ValueChanged = null;
  110. mouse_clickmove = false;
  111. SizeChanged += new System.EventHandler (OnResizeTB);
  112. MouseDown += new MouseEventHandler (OnMouseDownTB);
  113. MouseUp += new MouseEventHandler (OnMouseUpTB);
  114. SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
  115. SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
  116. }
  117. #region Public Properties
  118. public bool AutoSize {
  119. get { return autosize; }
  120. set { autosize = value;}
  121. }
  122. [EditorBrowsable (EditorBrowsableState.Never)]
  123. public override Image BackgroundImage {
  124. get { return base.BackgroundImage; }
  125. set { base.BackgroundImage = value; }
  126. }
  127. protected override CreateParams CreateParams {
  128. get {
  129. CreateParams createParams = base.CreateParams;
  130. createParams.ClassName = XplatUI.DefaultClassName;
  131. createParams.Style = (int) (
  132. WindowStyles.WS_CHILD |
  133. WindowStyles.WS_VISIBLE);
  134. return createParams;
  135. }
  136. }
  137. protected override ImeMode DefaultImeMode {
  138. get {return ImeMode.Disable; }
  139. }
  140. protected override Size DefaultSize {
  141. get { return new System.Drawing.Size (104, 42); }
  142. }
  143. [EditorBrowsable (EditorBrowsableState.Never)]
  144. public override Font Font {
  145. get { return base.Font; }
  146. set { base.Font = value; }
  147. }
  148. [EditorBrowsable (EditorBrowsableState.Never)]
  149. public override Color ForeColor {
  150. get { return base.ForeColor; }
  151. set { base.ForeColor = value; }
  152. }
  153. public int LargeChange {
  154. get { return largeChange; }
  155. set {
  156. if (value < 0)
  157. throw new Exception( string.Format("Value '{0}' must be greater than or equal to 0.", value));
  158. largeChange = value;
  159. Refresh ();
  160. }
  161. }
  162. public int Maximum {
  163. get { return maximum; }
  164. set {
  165. if (maximum != value) {
  166. maximum = value;
  167. if (maximum < minimum)
  168. minimum = maximum;
  169. Refresh ();
  170. }
  171. }
  172. }
  173. public int Minimum {
  174. get { return minimum; }
  175. set {
  176. if (Minimum != value) {
  177. minimum = value;
  178. if (minimum > maximum)
  179. maximum = minimum;
  180. Refresh ();
  181. }
  182. }
  183. }
  184. public Orientation Orientation {
  185. get { return orientation; }
  186. set {
  187. if (!Enum.IsDefined (typeof (Orientation), value))
  188. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for Orientation", value));
  189. /* Orientation can be changed once the control has been created */
  190. if (orientation != value) {
  191. orientation = value;
  192. int old_witdh = Width;
  193. Width = Height;
  194. Height = old_witdh;
  195. Refresh ();
  196. }
  197. }
  198. }
  199. public int SmallChange {
  200. get { return smallChange;}
  201. set {
  202. if ( value < 0 )
  203. throw new Exception( string.Format("Value '{0}' must be greater than or equal to 0.", value));
  204. if (smallChange != value) {
  205. smallChange = value;
  206. Refresh ();
  207. }
  208. }
  209. }
  210. [EditorBrowsable (EditorBrowsableState.Never)]
  211. public override string Text {
  212. get { return base.Text; }
  213. set { base.Text = value; }
  214. }
  215. public int TickFrequency {
  216. get { return tickFrequency; }
  217. set {
  218. if ( value > 0 ) {
  219. tickFrequency = value;
  220. Refresh ();
  221. }
  222. }
  223. }
  224. public TickStyle TickStyle {
  225. get { return tickStyle; }
  226. set {
  227. if (!Enum.IsDefined (typeof (TickStyle), value))
  228. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for TickStyle", value));
  229. if (tickStyle != value) {
  230. tickStyle = value;
  231. Refresh ();
  232. }
  233. }
  234. }
  235. public int Value {
  236. get { return position; }
  237. set {
  238. if (value < Minimum || value > Maximum)
  239. throw new ArgumentException(
  240. string.Format("'{0}' is not a valid value for 'Value'. 'Value' should be between 'Minimum' and 'Maximum'", value));
  241. if (position != value) {
  242. position = value;
  243. if (ValueChanged != null)
  244. ValueChanged (this, new EventArgs ());
  245. Refresh ();
  246. }
  247. }
  248. }
  249. #endregion //Public Properties
  250. #region Public Methods
  251. public virtual void BeginInit ()
  252. {
  253. }
  254. protected override void CreateHandle ()
  255. {
  256. base.CreateHandle ();
  257. }
  258. public virtual void EndInit ()
  259. {
  260. }
  261. protected override bool IsInputKey (Keys keyData)
  262. {
  263. return false;
  264. }
  265. protected override void OnBackColorChanged (EventArgs e)
  266. {
  267. }
  268. protected override void OnHandleCreated (EventArgs e)
  269. {
  270. if (AutoSize)
  271. if (Orientation == Orientation.Horizontal)
  272. Size = new Size (Width, 40);
  273. else
  274. Size = new Size (50, Height);
  275. UpdateArea ();
  276. CreateBuffers (Width, Height);
  277. UpdatePos (Value, true);
  278. }
  279. protected override void OnMouseWheel (MouseEventArgs e)
  280. {
  281. if (!Enabled) return;
  282. if (e.Delta > 0)
  283. SmallDecrement ();
  284. else
  285. SmallIncrement ();
  286. }
  287. protected virtual void OnScroll (EventArgs e)
  288. {
  289. if (Scroll != null)
  290. Scroll (this, e);
  291. }
  292. protected virtual void OnValueChanged (EventArgs e)
  293. {
  294. if (ValueChanged != null)
  295. ValueChanged (this, e);
  296. }
  297. public void SetRange (int minValue, int maxValue)
  298. {
  299. Minimum = minValue;
  300. Maximum = maxValue;
  301. Refresh ();
  302. }
  303. public override string ToString()
  304. {
  305. return string.Format("System.Windows.Forms.Trackbar, Minimum: {0}, Maximum: {1}, Value: {2}",
  306. Minimum, Maximum, Value);
  307. }
  308. protected override void WndProc (ref Message m)
  309. {
  310. int clicks = 1;
  311. switch ((Msg) m.Msg) {
  312. case Msg.WM_MOUSEMOVE:
  313. OnMouseMoveTB (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
  314. clicks,
  315. LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()),
  316. 0));
  317. break;
  318. case Msg.WM_PAINT: {
  319. PaintEventArgs paint_event;
  320. paint_event = XplatUI.PaintEventStart (Handle);
  321. OnPaintTB (paint_event);
  322. XplatUI.PaintEventEnd (Handle);
  323. return;
  324. }
  325. case Msg.WM_KEYDOWN:
  326. OnKeyDownTB (new KeyEventArgs ((Keys)m.WParam.ToInt32 ()));
  327. return;
  328. case Msg.WM_ERASEBKGND:
  329. m.Result = (IntPtr) 1; /* Disable background painting to avoid flickering */
  330. return;
  331. default:
  332. break;
  333. }
  334. base.WndProc (ref m);
  335. }
  336. #endregion Public Methods
  337. #region Private Methods
  338. private void UpdateArea ()
  339. {
  340. paint_area.X = paint_area.Y = 0;
  341. paint_area.Width = Width;
  342. paint_area.Height = Height;
  343. }
  344. private void UpdatePos (int newPos, bool update_trumbpos)
  345. {
  346. int old = position;
  347. if (newPos < minimum)
  348. Value = minimum;
  349. else
  350. if (newPos > maximum)
  351. Value = maximum;
  352. else
  353. Value = newPos;
  354. }
  355. private void LargeIncrement ()
  356. {
  357. UpdatePos (position + LargeChange, true);
  358. Refresh ();
  359. OnScroll (new EventArgs ());
  360. }
  361. private void LargeDecrement ()
  362. {
  363. UpdatePos (position - LargeChange, true);
  364. Refresh ();
  365. OnScroll (new EventArgs ());
  366. }
  367. private void SmallIncrement ()
  368. {
  369. UpdatePos (position + SmallChange, true);
  370. Refresh ();
  371. OnScroll (new EventArgs ());
  372. }
  373. private void SmallDecrement ()
  374. {
  375. UpdatePos (position - SmallChange, true);
  376. Refresh ();
  377. OnScroll (new EventArgs ());
  378. }
  379. private void Draw ()
  380. {
  381. float ticks = (Maximum - Minimum) / tickFrequency; /* N of ticks draw*/
  382. if (thumb_pressed)
  383. ThemeEngine.Current.DrawTrackBar (DeviceContext, paint_area, this,
  384. ref thumb_pos, ref thumb_area, thumb_pressed, ticks, thumb_mouseclick, true);
  385. else
  386. ThemeEngine.Current.DrawTrackBar (DeviceContext, paint_area, this,
  387. ref thumb_pos, ref thumb_area, thumb_pressed, ticks, Value - Minimum, false);
  388. }
  389. private void OnMouseUpTB (object sender, MouseEventArgs e)
  390. {
  391. if (!Enabled) return;
  392. if (thumb_pressed == true || mouse_clickmove == true) {
  393. thumb_pressed = false;
  394. holdclick_timer.Enabled = false;
  395. XplatUI.ReleaseWindow (Handle);
  396. Refresh ();
  397. }
  398. }
  399. private void OnMouseDownTB (object sender, MouseEventArgs e)
  400. {
  401. if (!Enabled) return;
  402. bool fire_timer = false;
  403. Point point = new Point (e.X, e.Y);
  404. if (orientation == Orientation.Horizontal) {
  405. if (thumb_pos.Contains (point)) {
  406. XplatUI.GrabWindow (Handle);
  407. thumb_pressed = true;
  408. thumb_mouseclick = e.X;
  409. Refresh ();
  410. }
  411. else {
  412. if (paint_area.Contains (point)) {
  413. if (e.X > thumb_pos.X + thumb_pos.Width)
  414. LargeIncrement ();
  415. else
  416. LargeDecrement ();
  417. Refresh ();
  418. fire_timer = true;
  419. mouse_clickmove = true;
  420. }
  421. }
  422. }
  423. else {
  424. if (thumb_pos.Contains (point)) {
  425. XplatUI.GrabWindow (Handle);
  426. thumb_pressed = true;
  427. thumb_mouseclick = e.Y;
  428. Refresh ();
  429. }
  430. else {
  431. if (paint_area.Contains (point)) {
  432. if (e.Y > thumb_pos.Y + thumb_pos.Height)
  433. LargeIncrement ();
  434. else
  435. LargeDecrement ();
  436. Refresh ();
  437. fire_timer = true;
  438. mouse_clickmove = true;
  439. }
  440. }
  441. }
  442. if (fire_timer) {
  443. holdclick_timer.Elapsed += new ElapsedEventHandler (OnFirstClickTimer);
  444. holdclick_timer.Interval = 300;
  445. holdclick_timer.Enabled = true;
  446. }
  447. }
  448. private void OnMouseMoveTB (MouseEventArgs e)
  449. {
  450. if (!Enabled) return;
  451. Point pnt = new Point (e.X, e.Y);
  452. /* Moving the thumb */
  453. if (thumb_pressed) {
  454. if (orientation == Orientation.Horizontal){
  455. if (paint_area.Contains (e.X, thumb_pos.Y))
  456. thumb_mouseclick = e.X;
  457. }
  458. else {
  459. if (paint_area.Contains (thumb_pos.X, e.Y))
  460. thumb_mouseclick = e.Y;
  461. }
  462. Refresh ();
  463. OnScroll (new EventArgs ());
  464. }
  465. }
  466. private void OnResizeTB (object sender, System.EventArgs e)
  467. {
  468. if (Width <= 0 || Height <= 0)
  469. return;
  470. UpdateArea ();
  471. CreateBuffers (Width, Height);
  472. }
  473. private void OnPaintTB (PaintEventArgs pevent)
  474. {
  475. if (Width <= 0 || Height <= 0 || Visible == false)
  476. return;
  477. /* Copies memory drawing buffer to screen*/
  478. UpdateArea ();
  479. Draw ();
  480. pevent.Graphics.DrawImage (ImageBuffer, 0, 0);
  481. }
  482. private void OnKeyDownTB (KeyEventArgs e)
  483. {
  484. switch (e.KeyCode) {
  485. case Keys.Up:
  486. case Keys.Right:
  487. SmallIncrement ();
  488. break;
  489. case Keys.Down:
  490. case Keys.Left:
  491. SmallDecrement ();
  492. break;
  493. default:
  494. break;
  495. }
  496. }
  497. private void OnFirstClickTimer (Object source, ElapsedEventArgs e)
  498. {
  499. Point pnt;
  500. pnt = PointToClient (MousePosition);
  501. if (thumb_area.Contains (pnt)) {
  502. if (orientation == Orientation.Horizontal) {
  503. if (pnt.X > thumb_pos.X + thumb_pos.Width)
  504. LargeIncrement ();
  505. if (pnt.X < thumb_pos.X)
  506. LargeDecrement ();
  507. }
  508. else {
  509. if (pnt.Y > thumb_pos.Y + thumb_pos.Height)
  510. LargeIncrement ();
  511. if (pnt.Y < thumb_pos.Y)
  512. LargeDecrement ();
  513. }
  514. Refresh ();
  515. }
  516. }
  517. #endregion // Private Methods
  518. }
  519. }