TrackBar.cs 15 KB

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