TrackBar.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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. //
  24. // Copyright (c) 2004-2006 Novell, Inc.
  25. //
  26. // Authors:
  27. // Jordi Mas i Hernandez, [email protected]
  28. //
  29. // TODO:
  30. // - The AutoSize functionality seems quite broken for vertical controls in .Net 1.1. Not
  31. // sure if we are implementing it the right way.
  32. //
  33. // NOT COMPLETE
  34. using System.ComponentModel;
  35. using System.ComponentModel.Design;
  36. using System.Drawing;
  37. using System.Drawing.Imaging;
  38. using System.Drawing.Drawing2D;
  39. using System.Timers;
  40. namespace System.Windows.Forms
  41. {
  42. [Designer("System.Windows.Forms.Design.TrackBarDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  43. [DefaultEvent ("Scroll")]
  44. [DefaultProperty("Value")]
  45. public class TrackBar : Control, ISupportInitialize
  46. {
  47. private int minimum;
  48. private int maximum;
  49. internal int tickFrequency;
  50. private bool autosize;
  51. private int position;
  52. private int smallChange;
  53. private int largeChange;
  54. private Orientation orientation;
  55. private TickStyle tickStyle;
  56. private Rectangle thumb_pos = new Rectangle (); /* Current position and size of the thumb */
  57. private Rectangle thumb_area = new Rectangle (); /* Area where the thumb can scroll */
  58. internal bool thumb_pressed = false;
  59. private System.Timers.Timer holdclick_timer = new System.Timers.Timer ();
  60. internal int thumb_mouseclick;
  61. private bool mouse_clickmove;
  62. #region events
  63. [Browsable (false)]
  64. [EditorBrowsable (EditorBrowsableState.Never)]
  65. public new event EventHandler BackgroundImageChanged;
  66. [Browsable (false)]
  67. [EditorBrowsable (EditorBrowsableState.Never)]
  68. public new event EventHandler Click;
  69. [Browsable (false)]
  70. [EditorBrowsable (EditorBrowsableState.Never)]
  71. public new event EventHandler DoubleClick;
  72. [Browsable (false)]
  73. [EditorBrowsable (EditorBrowsableState.Never)]
  74. public new event EventHandler FontChanged;
  75. [Browsable (false)]
  76. [EditorBrowsable (EditorBrowsableState.Never)]
  77. public new event EventHandler ForeColorChanged;
  78. [Browsable (false)]
  79. [EditorBrowsable (EditorBrowsableState.Never)]
  80. public new event EventHandler ImeModeChanged;
  81. [Browsable (false)]
  82. [EditorBrowsable (EditorBrowsableState.Never)]
  83. public new event PaintEventHandler Paint;
  84. public event EventHandler Scroll;
  85. [Browsable (false)]
  86. [EditorBrowsable (EditorBrowsableState.Never)]
  87. public new event EventHandler TextChanged;
  88. public event EventHandler ValueChanged;
  89. #endregion // Events
  90. public TrackBar ()
  91. {
  92. orientation = Orientation.Horizontal;
  93. minimum = 0;
  94. maximum = 10;
  95. tickFrequency = 1;
  96. autosize = true;
  97. position = 0;
  98. tickStyle = TickStyle.BottomRight;
  99. smallChange = 1;
  100. largeChange = 5;
  101. mouse_clickmove = false;
  102. MouseDown += new MouseEventHandler (OnMouseDownTB);
  103. MouseUp += new MouseEventHandler (OnMouseUpTB);
  104. MouseMove += new MouseEventHandler (OnMouseMoveTB);
  105. KeyDown += new KeyEventHandler (OnKeyDownTB);
  106. holdclick_timer.Elapsed += new ElapsedEventHandler (OnFirstClickTimer);
  107. SetStyle (ControlStyles.UserPaint | ControlStyles.Opaque, false);
  108. }
  109. #region Private & Internal Properties
  110. internal Rectangle ThumbPos {
  111. get {
  112. return thumb_pos;
  113. }
  114. set {
  115. thumb_pos = value;
  116. }
  117. }
  118. internal Rectangle ThumbArea {
  119. get {
  120. return thumb_area;
  121. }
  122. set {
  123. thumb_area = value;
  124. }
  125. }
  126. #endregion // Private & Internal Properties
  127. #region Public Properties
  128. [DefaultValue (true)]
  129. public bool AutoSize {
  130. get { return autosize; }
  131. set { autosize = value;}
  132. }
  133. [EditorBrowsable (EditorBrowsableState.Never)]
  134. [Browsable (false)]
  135. public override Image BackgroundImage {
  136. get { return base.BackgroundImage; }
  137. set {
  138. if (base.BackgroundImage == value)
  139. return;
  140. if (BackgroundImageChanged != null)
  141. BackgroundImageChanged (this, EventArgs.Empty);
  142. base.BackgroundImage = value;
  143. }
  144. }
  145. protected override CreateParams CreateParams {
  146. get {
  147. return base.CreateParams;
  148. }
  149. }
  150. protected override ImeMode DefaultImeMode {
  151. get {return ImeMode.Disable; }
  152. }
  153. protected override Size DefaultSize {
  154. get { return ThemeEngine.Current.TrackBarDefaultSize; }
  155. }
  156. [Browsable(false)]
  157. [EditorBrowsable (EditorBrowsableState.Never)]
  158. public override Font Font {
  159. get { return base.Font; }
  160. set { base.Font = value; }
  161. }
  162. [EditorBrowsable (EditorBrowsableState.Never)]
  163. [Browsable (false)]
  164. public override Color ForeColor {
  165. get { return base.ForeColor; }
  166. set {
  167. if (value == base.ForeColor)
  168. return;
  169. if (ForeColorChanged != null)
  170. ForeColorChanged (this, EventArgs.Empty);
  171. Refresh ();
  172. }
  173. }
  174. [EditorBrowsable (EditorBrowsableState.Never)]
  175. [Browsable (false)]
  176. public new ImeMode ImeMode {
  177. get { return base.ImeMode; }
  178. set {
  179. if (value == base.ImeMode)
  180. return;
  181. base.ImeMode = value;
  182. if (ImeModeChanged != null)
  183. ImeModeChanged (this, EventArgs.Empty);
  184. }
  185. }
  186. [DefaultValue (5)]
  187. public int LargeChange
  188. {
  189. get { return largeChange; }
  190. set {
  191. if (value < 0)
  192. throw new Exception( string.Format("Value '{0}' must be greater than or equal to 0.", value));
  193. largeChange = value;
  194. }
  195. }
  196. [DefaultValue (10)]
  197. [RefreshProperties (RefreshProperties.All)]
  198. public int Maximum {
  199. get { return maximum; }
  200. set {
  201. if (maximum != value) {
  202. maximum = value;
  203. if (maximum < minimum)
  204. minimum = maximum;
  205. Refresh ();
  206. }
  207. }
  208. }
  209. [DefaultValue (0)]
  210. [RefreshProperties (RefreshProperties.All)]
  211. public int Minimum {
  212. get { return minimum; }
  213. set {
  214. if (Minimum != value) {
  215. minimum = value;
  216. if (minimum > maximum)
  217. maximum = minimum;
  218. Refresh ();
  219. }
  220. }
  221. }
  222. [DefaultValue (Orientation.Horizontal)]
  223. [Localizable (true)]
  224. public Orientation Orientation {
  225. get { return orientation; }
  226. set {
  227. if (!Enum.IsDefined (typeof (Orientation), value))
  228. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for Orientation", value));
  229. /* Orientation can be changed once the control has been created */
  230. if (orientation != value) {
  231. orientation = value;
  232. int old_witdh = Width;
  233. Width = Height;
  234. Height = old_witdh;
  235. Refresh ();
  236. }
  237. }
  238. }
  239. [DefaultValue (1)]
  240. public int SmallChange {
  241. get { return smallChange;}
  242. set {
  243. if ( value < 0 )
  244. throw new Exception( string.Format("Value '{0}' must be greater than or equal to 0.", value));
  245. if (smallChange != value) {
  246. smallChange = value;
  247. }
  248. }
  249. }
  250. [EditorBrowsable (EditorBrowsableState.Never)]
  251. [Bindable (false)]
  252. [Browsable (false)]
  253. public override string Text {
  254. get { return base.Text; }
  255. set {
  256. if (value == base.Text)
  257. return;
  258. if (TextChanged != null)
  259. TextChanged (this, EventArgs.Empty);
  260. Refresh ();
  261. }
  262. }
  263. [DefaultValue (1)]
  264. public int TickFrequency {
  265. get { return tickFrequency; }
  266. set {
  267. if ( value > 0 ) {
  268. tickFrequency = value;
  269. Refresh ();
  270. }
  271. }
  272. }
  273. [DefaultValue (TickStyle.BottomRight)]
  274. public TickStyle TickStyle {
  275. get { return tickStyle; }
  276. set {
  277. if (!Enum.IsDefined (typeof (TickStyle), value))
  278. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for TickStyle", value));
  279. if (tickStyle != value) {
  280. tickStyle = value;
  281. Refresh ();
  282. }
  283. }
  284. }
  285. [DefaultValue (0)]
  286. [Bindable (true)]
  287. public int Value {
  288. get { return position; }
  289. set {
  290. if (value < Minimum || value > Maximum)
  291. throw new ArgumentException(
  292. string.Format("'{0}' is not a valid value for 'Value'. 'Value' should be between 'Minimum' and 'Maximum'", value));
  293. if (position != value) {
  294. position = value;
  295. if (ValueChanged != null)
  296. ValueChanged (this, new EventArgs ());
  297. Invalidate (thumb_area);
  298. }
  299. }
  300. }
  301. #endregion //Public Properties
  302. #region Public Methods
  303. public void BeginInit ()
  304. {
  305. }
  306. protected override void CreateHandle ()
  307. {
  308. base.CreateHandle ();
  309. }
  310. public void EndInit ()
  311. {
  312. }
  313. protected override bool IsInputKey (Keys keyData)
  314. {
  315. if ((keyData & Keys.Alt) == 0) {
  316. switch (keyData & Keys.KeyCode) {
  317. case Keys.Down:
  318. case Keys.Right:
  319. case Keys.Up:
  320. case Keys.Left:
  321. case Keys.PageUp:
  322. case Keys.PageDown:
  323. case Keys.Home:
  324. case Keys.End:
  325. return true;
  326. }
  327. }
  328. return base.IsInputKey (keyData);
  329. }
  330. protected override void OnBackColorChanged (EventArgs e)
  331. {
  332. base.OnBackColorChanged (e);
  333. }
  334. protected override void OnHandleCreated (EventArgs e)
  335. {
  336. base.OnHandleCreated (e);
  337. if (AutoSize)
  338. if (Orientation == Orientation.Horizontal)
  339. Size = new Size (Width, 40);
  340. else
  341. Size = new Size (50, Height);
  342. UpdatePos (Value, true);
  343. }
  344. [EditorBrowsable (EditorBrowsableState.Advanced)]
  345. protected override void OnMouseWheel (MouseEventArgs e)
  346. {
  347. base.OnMouseWheel (e);
  348. if (!Enabled) return;
  349. if (e.Delta > 0)
  350. SmallDecrement ();
  351. else
  352. SmallIncrement ();
  353. }
  354. protected virtual void OnScroll (EventArgs e)
  355. {
  356. if (Scroll != null)
  357. Scroll (this, e);
  358. }
  359. protected virtual void OnValueChanged (EventArgs e)
  360. {
  361. if (ValueChanged != null)
  362. ValueChanged (this, e);
  363. }
  364. public void SetRange (int minValue, int maxValue)
  365. {
  366. Minimum = minValue;
  367. Maximum = maxValue;
  368. }
  369. public override string ToString()
  370. {
  371. return string.Format("System.Windows.Forms.Trackbar, Minimum: {0}, Maximum: {1}, Value: {2}",
  372. Minimum, Maximum, Value);
  373. }
  374. protected override void WndProc (ref Message m)
  375. {
  376. switch ((Msg) m.Msg) {
  377. case Msg.WM_PAINT: {
  378. PaintEventArgs paint_event;
  379. paint_event = XplatUI.PaintEventStart (Handle, true);
  380. OnPaintTB (paint_event);
  381. XplatUI.PaintEventEnd (Handle, true);
  382. return;
  383. }
  384. case Msg.WM_ERASEBKGND:
  385. m.Result = (IntPtr) 1; /* Disable background painting to avoid flickering */
  386. return;
  387. default:
  388. break;
  389. }
  390. base.WndProc (ref m);
  391. }
  392. #endregion Public Methods
  393. #region Private Methods
  394. private void UpdatePos (int newPos, bool update_trumbpos)
  395. {
  396. if (newPos < minimum){
  397. Value = minimum;
  398. }
  399. else {
  400. if (newPos > maximum) {
  401. Value = maximum;
  402. }
  403. else {
  404. Value = newPos;
  405. }
  406. }
  407. }
  408. private void LargeIncrement ()
  409. {
  410. UpdatePos (position + LargeChange, true);
  411. Invalidate (thumb_area);
  412. OnScroll (new EventArgs ());
  413. }
  414. private void LargeDecrement ()
  415. {
  416. UpdatePos (position - LargeChange, true);
  417. Invalidate (thumb_area);
  418. OnScroll (new EventArgs ());
  419. }
  420. private void SmallIncrement ()
  421. {
  422. UpdatePos (position + SmallChange, true);
  423. Invalidate (thumb_area);
  424. OnScroll (new EventArgs ());
  425. }
  426. private void SmallDecrement ()
  427. {
  428. UpdatePos (position - SmallChange, true);
  429. Invalidate (thumb_area);
  430. OnScroll (new EventArgs ());
  431. }
  432. private void OnMouseUpTB (object sender, MouseEventArgs e)
  433. {
  434. if (!Enabled) return;
  435. if (thumb_pressed == true || mouse_clickmove == true) {
  436. thumb_pressed = false;
  437. holdclick_timer.Enabled = false;
  438. this.Capture = false;
  439. Invalidate (thumb_area);
  440. }
  441. }
  442. private void OnMouseDownTB (object sender, MouseEventArgs e)
  443. {
  444. if (!Enabled) return;
  445. bool fire_timer = false;
  446. Point point = new Point (e.X, e.Y);
  447. if (orientation == Orientation.Horizontal) {
  448. if (thumb_pos.Contains (point)) {
  449. this.Capture = true;
  450. thumb_pressed = true;
  451. thumb_mouseclick = e.X;
  452. Invalidate (thumb_area);
  453. }
  454. else {
  455. if (ClientRectangle.Contains (point)) {
  456. if (e.X > thumb_pos.X + thumb_pos.Width)
  457. LargeIncrement ();
  458. else
  459. LargeDecrement ();
  460. Invalidate (thumb_area);
  461. fire_timer = true;
  462. mouse_clickmove = true;
  463. }
  464. }
  465. }
  466. else {
  467. if (thumb_pos.Contains (point)) {
  468. this.Capture = true;
  469. thumb_pressed = true;
  470. thumb_mouseclick = e.Y;
  471. Invalidate (thumb_area);
  472. }
  473. else {
  474. if (ClientRectangle.Contains (point)) {
  475. if (e.Y > thumb_pos.Y + thumb_pos.Height)
  476. LargeDecrement ();
  477. else
  478. LargeIncrement ();
  479. Invalidate (thumb_area);
  480. fire_timer = true;
  481. mouse_clickmove = true;
  482. }
  483. }
  484. }
  485. if (fire_timer) {
  486. holdclick_timer.Interval = 300;
  487. holdclick_timer.Enabled = true;
  488. }
  489. }
  490. private void OnMouseMoveTB (object sender, MouseEventArgs e)
  491. {
  492. if (!Enabled) return;
  493. /* Moving the thumb */
  494. if (thumb_pressed) {
  495. if (orientation == Orientation.Horizontal){
  496. if (ClientRectangle.Contains (e.X, thumb_pos.Y))
  497. thumb_mouseclick = e.X;
  498. }
  499. else {
  500. if (ClientRectangle.Contains (thumb_pos.X, e.Y))
  501. thumb_mouseclick = e.Y;
  502. }
  503. Invalidate (thumb_area);
  504. OnScroll (new EventArgs ());
  505. }
  506. }
  507. private void OnPaintTB (PaintEventArgs pevent)
  508. {
  509. if (Paint != null) {
  510. Paint (this, pevent);
  511. }
  512. ThemeEngine.Current.DrawTrackBar (pevent.Graphics, pevent.ClipRectangle, this);
  513. }
  514. private void OnKeyDownTB (object sender, KeyEventArgs e)
  515. {
  516. switch (e.KeyCode) {
  517. case Keys.Down:
  518. case Keys.Right:
  519. SmallDecrement ();
  520. break;
  521. case Keys.Up:
  522. case Keys.Left:
  523. SmallIncrement ();
  524. break;
  525. case Keys.PageUp:
  526. LargeIncrement ();
  527. break;
  528. case Keys.PageDown:
  529. LargeDecrement ();
  530. break;
  531. case Keys.Home:
  532. Value = Maximum;
  533. break;
  534. case Keys.End:
  535. Value = Minimum;
  536. break;
  537. default:
  538. break;
  539. }
  540. }
  541. private void OnFirstClickTimer (Object source, ElapsedEventArgs e)
  542. {
  543. Point pnt;
  544. pnt = PointToClient (MousePosition);
  545. if (thumb_area.Contains (pnt)) {
  546. if (orientation == Orientation.Horizontal) {
  547. if (pnt.X > thumb_pos.X + thumb_pos.Width)
  548. LargeIncrement ();
  549. if (pnt.X < thumb_pos.X)
  550. LargeDecrement ();
  551. }
  552. else {
  553. if (pnt.Y > thumb_pos.Y + thumb_pos.Height)
  554. LargeIncrement ();
  555. if (pnt.Y < thumb_pos.Y)
  556. LargeDecrement ();
  557. }
  558. Invalidate (thumb_area);
  559. }
  560. }
  561. protected override void SetBoundsCore (int x, int y,int width, int height, BoundsSpecified specified)
  562. {
  563. base.SetBoundsCore (x, y,width, height, specified);
  564. }
  565. #endregion // Private Methods
  566. }
  567. }