TrackBar.cs 17 KB

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