TrackBar.cs 17 KB

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