TrackBar.cs 15 KB

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