TrackBar.cs 13 KB

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