TrackBar.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. // - Draging the thumb does not behave exactly like in Win32
  28. // - The AutoSize functionality seems quite broken for vertical controls in .Net 1.1. Not
  29. // sure if we are implementing it the right way.
  30. // - Vertical orientation still needs some work
  31. // - OnMouseWheel event
  32. //
  33. // Copyright (C) Novell Inc., 2004
  34. //
  35. //
  36. // $Revision: 1.6 $
  37. // $Modtime: $
  38. // $Log: TrackBar.cs,v $
  39. // Revision 1.6 2004/08/10 15:47:11 jackson
  40. // Allow control to handle buffering
  41. //
  42. // Revision 1.5 2004/08/07 23:32:26 jordi
  43. // throw exceptions of invalid enums values
  44. //
  45. // Revision 1.4 2004/08/06 23:18:06 pbartok
  46. // - Fixed some rounding issues with float/int
  47. //
  48. // Revision 1.3 2004/07/27 15:53:02 jordi
  49. // fixes trackbar events, def classname, methods signature
  50. //
  51. // Revision 1.2 2004/07/26 17:42:03 jordi
  52. // Theme support
  53. //
  54. // Revision 1.1 2004/07/15 09:38:02 jordi
  55. // Horizontal and Vertical TrackBar control implementation
  56. //
  57. //
  58. // NOT COMPLETE
  59. using System.ComponentModel;
  60. using System.Drawing;
  61. using System.Drawing.Imaging;
  62. using System.Drawing.Drawing2D;
  63. namespace System.Windows.Forms
  64. {
  65. public class TrackBar : Control, ISupportInitialize
  66. {
  67. private int minimum;
  68. private int maximum;
  69. private int tickFrequency;
  70. private bool autosize;
  71. private int position;
  72. private int smallChange;
  73. private int largeChange;
  74. private Orientation orientation;
  75. private TickStyle tickStyle;
  76. private Rectangle paint_area = new Rectangle ();
  77. private Rectangle thumb_pos = new Rectangle (); /* Current position and size of the thumb */
  78. private Rectangle thumb_area = new Rectangle (); /* Area where the thumb can scroll */
  79. private bool thumb_pressed = false;
  80. private int thumb_pixel_click_move;
  81. private float pixel_per_pos = 0;
  82. #region Events
  83. public event EventHandler Scroll;
  84. public event EventHandler ValueChanged;
  85. #endregion // Events
  86. public TrackBar ()
  87. {
  88. orientation = Orientation.Horizontal;
  89. minimum = 0;
  90. maximum = 10;
  91. tickFrequency = 1;
  92. autosize = true;
  93. position = 0;
  94. tickStyle = TickStyle.BottomRight;
  95. smallChange = 1;
  96. largeChange = 5;
  97. Scroll = null;
  98. ValueChanged = null;
  99. SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
  100. SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
  101. }
  102. #region Public Properties
  103. public bool AutoSize {
  104. get { return autosize; }
  105. set { autosize = value;}
  106. }
  107. public override Image BackgroundImage {
  108. get { return base.BackgroundImage; }
  109. set { base.BackgroundImage = value; }
  110. }
  111. public override Font Font {
  112. get { return base.Font; }
  113. set { base.Font = value; }
  114. }
  115. public override Color ForeColor {
  116. get { return base.ForeColor; }
  117. set { base.ForeColor = value; }
  118. }
  119. public int LargeChange {
  120. get { return largeChange; }
  121. set {
  122. if (value < 0)
  123. throw new Exception( string.Format("Value '{0}' must be greater than or equal to 0.", value));
  124. largeChange = value;
  125. Invalidate ();
  126. }
  127. }
  128. public int Maximum {
  129. get { return maximum; }
  130. set {
  131. maximum = value;
  132. if (maximum < minimum)
  133. minimum = maximum;
  134. Invalidate ();
  135. }
  136. }
  137. public int Minimum {
  138. get { return minimum; }
  139. set {
  140. minimum = value;
  141. if (minimum > maximum)
  142. maximum = minimum;
  143. Invalidate ();
  144. }
  145. }
  146. public Orientation Orientation {
  147. get { return orientation; }
  148. set {
  149. if (!Enum.IsDefined (typeof (Orientation), value))
  150. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for Orientation", value));
  151. /* Orientation can be changed once the control has been created*/
  152. orientation = value;
  153. int old_witdh = Width;
  154. Width = Height;
  155. Height = old_witdh;
  156. Invalidate ();
  157. }
  158. }
  159. public int SmallChange {
  160. get { return smallChange;}
  161. set {
  162. if ( value < 0 )
  163. throw new Exception( string.Format("Value '{0}' must be greater than or equal to 0.", value));
  164. smallChange = value;
  165. Invalidate ();
  166. }
  167. }
  168. public override string Text {
  169. get { return base.Text; }
  170. set { base.Text = value; }
  171. }
  172. public int TickFrequency {
  173. get { return tickFrequency; }
  174. set {
  175. if ( value > 0 ) {
  176. tickFrequency = value;
  177. Invalidate ();
  178. }
  179. }
  180. }
  181. public TickStyle TickStyle {
  182. get { return tickStyle; }
  183. set {
  184. if (!Enum.IsDefined (typeof (TickStyle), value))
  185. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for TickStyle", value));
  186. tickStyle = value;
  187. }
  188. }
  189. public int Value {
  190. get { return position; }
  191. set {
  192. if (value < Minimum || value > Maximum)
  193. throw new ArgumentException(
  194. string.Format("'{0}' is not a valid value for 'Value'. 'Value' should be between 'Minimum' and 'Maximum'", value));
  195. if (position != value) {
  196. position = value;
  197. if (ValueChanged != null)
  198. ValueChanged (this, new EventArgs ());
  199. Invalidate ();
  200. }
  201. }
  202. }
  203. #endregion //Public Properties
  204. #region Public Methods
  205. public void SetRange (int minValue, int maxValue)
  206. {
  207. Minimum = minValue;
  208. Maximum = maxValue;
  209. Invalidate ();
  210. }
  211. public override string ToString()
  212. {
  213. return string.Format("System.Windows.Forms.Trackbar, Minimum: {0}, Maximum: {1}, Value: {2}",
  214. Minimum, Maximum, Value);
  215. }
  216. protected override CreateParams CreateParams {
  217. get {
  218. CreateParams createParams = base.CreateParams;
  219. createParams.ClassName = XplatUI.DefaultClassName;
  220. createParams.Style = (int) (
  221. WindowStyles.WS_CHILD |
  222. WindowStyles.WS_VISIBLE);
  223. return createParams;
  224. }
  225. }
  226. protected override ImeMode DefaultImeMode {
  227. get {return ImeMode.Disable; }
  228. }
  229. protected override Size DefaultSize {
  230. get { return new System.Drawing.Size (104, 42); }
  231. }
  232. public virtual void BeginInit()
  233. {
  234. }
  235. public virtual void EndInit()
  236. {
  237. }
  238. #endregion Public Methods
  239. #region Private Methods
  240. private void fire_scroll_event ()
  241. {
  242. if (Scroll == null)
  243. return;
  244. Scroll (this, new EventArgs ());
  245. }
  246. private void UpdateArea ()
  247. {
  248. paint_area.X = paint_area.Y = 0;
  249. paint_area.Width = Width;
  250. paint_area.Height = Height;
  251. UpdatePixelPerPos ();
  252. //Console.WriteLine ("UpdateArea: {0} {1} {2}", thumb_area.Width, thumb_pos.Width, pixel_per_pos);
  253. }
  254. private void UpdateThumbPos (int pixel, bool update_value)
  255. {
  256. float new_pos = 0;
  257. //Console.WriteLine ("UpdateThumbPos: " + pixel + " per " + pixel_per_pos);
  258. if (orientation == Orientation.Horizontal) {
  259. if (pixel < thumb_area.X)
  260. thumb_pos.X = thumb_area.X;
  261. else
  262. if (pixel > thumb_area.X + thumb_area.Width - thumb_pos.Width)
  263. thumb_pos.X = thumb_area.X + thumb_area.Width - thumb_pos.Width;
  264. else
  265. thumb_pos.X = pixel;
  266. new_pos = (float) (thumb_pos.X - thumb_area.X);
  267. new_pos = new_pos / pixel_per_pos;
  268. } else {
  269. if (pixel < thumb_area.Y)
  270. thumb_pos.Y = thumb_area.Y;
  271. else
  272. if (pixel > thumb_area.Y + thumb_area.Height - thumb_pos.Height)
  273. thumb_pos.Y = thumb_area.Y + thumb_area.Height - thumb_pos.Height;
  274. else
  275. thumb_pos.Y = pixel;
  276. new_pos = (float) (thumb_pos.Y - thumb_area.Y);
  277. new_pos = new_pos / pixel_per_pos;
  278. }
  279. //Console.WriteLine ("UpdateThumbPos: thumb_pos.Y {0} thumb_area.Y {1} pixel_per_pos {2}, new pos {3}, pixel {4}",
  280. // thumb_pos.Y, thumb_area.Y, pixel_per_pos, new_pos, pixel);
  281. if (update_value)
  282. UpdatePos ((int) new_pos, false);
  283. }
  284. private void LargeIncrement()
  285. {
  286. //Console.WriteLine ("large inc: {0} {1}", position, LargeChange);
  287. UpdatePos (position + LargeChange, true);
  288. fire_scroll_event ();
  289. }
  290. private void LargeDecrement()
  291. {
  292. //Console.WriteLine ("large dec: {0} {1}", position, LargeChange);
  293. UpdatePos (position - LargeChange, true);
  294. fire_scroll_event ();
  295. }
  296. private void UpdatePixelPerPos ()
  297. {
  298. pixel_per_pos = ((float)(thumb_area.Width)
  299. / (float) (1 + Maximum - Minimum));
  300. }
  301. private void UpdatePos (int newPos, bool update_trumbpos)
  302. {
  303. int old = position;
  304. if (newPos < minimum)
  305. Value = minimum;
  306. else
  307. if (newPos > maximum)
  308. Value = maximum;
  309. else
  310. Value = newPos;
  311. if (orientation == Orientation.Horizontal) {
  312. if (update_trumbpos)
  313. UpdateThumbPos (thumb_area.X + (int)(((float)(Value - Minimum)) * pixel_per_pos), false);
  314. }
  315. else {
  316. if (update_trumbpos)
  317. UpdateThumbPos (thumb_area.Y + (int)(((float)(Value - Minimum)) * pixel_per_pos), false);
  318. }
  319. }
  320. #endregion // Private Methods
  321. #region Override Event Methods
  322. protected override void OnResize (EventArgs e)
  323. {
  324. //Console.WriteLine ("OnResize");
  325. base.OnResize (e);
  326. if (Width <= 0 || Height <= 0)
  327. return;
  328. UpdateArea ();
  329. CreateBuffers (Width, Height);
  330. }
  331. protected override void OnHandleCreated (EventArgs e)
  332. {
  333. base.OnHandleCreated(e);
  334. //Console.WriteLine ("OnHandleCreated");
  335. UpdateArea ();
  336. CreateBuffers (Width, Height);
  337. UpdatePos (Value, true);
  338. UpdatePixelPerPos ();
  339. Draw();
  340. UpdatePos (Value, true);
  341. if (AutoSize)
  342. if (Orientation == Orientation.Horizontal)
  343. Size = new Size (Width, 45);
  344. else
  345. Size = new Size (50, Height);
  346. }
  347. /* Disable background painting to avoid flickering, since we do our painting*/
  348. protected override void OnPaintBackground (PaintEventArgs pevent)
  349. {
  350. // None
  351. }
  352. private void Draw ()
  353. {
  354. int ticks = (Maximum - Minimum) / tickFrequency;
  355. ThemeEngine.Current.DrawTrackBar (DeviceContext, paint_area, ref thumb_pos, ref thumb_area,
  356. tickStyle, ticks, Orientation, Focused);
  357. }
  358. protected override void OnPaint (PaintEventArgs pevent)
  359. {
  360. Console.WriteLine ("OnDraw");
  361. if (Width <= 0 || Height <= 0 || Visible == false)
  362. return;
  363. /* Copies memory drawing buffer to screen*/
  364. UpdateArea ();
  365. Draw();
  366. pevent.Graphics.DrawImage (ImageBuffer, 0, 0);
  367. }
  368. protected override void OnMouseDown (MouseEventArgs e)
  369. {
  370. if (!Enabled) return;
  371. //System.Console.WriteLine ("OnMouseDown" + thumb_pos);
  372. Point point = new Point (e.X, e.Y);
  373. if (orientation == Orientation.Horizontal) {
  374. if (thumb_pos.Contains (point)) {
  375. thumb_pressed = true;
  376. Invalidate ();
  377. thumb_pixel_click_move = e.X;
  378. }
  379. else {
  380. if (paint_area.Contains (point)) {
  381. if (e.X > thumb_pos.X + thumb_pos.Width)
  382. LargeIncrement ();
  383. else
  384. LargeDecrement ();
  385. Invalidate ();
  386. }
  387. }
  388. }
  389. else {
  390. if (thumb_pos.Contains (point)) {
  391. thumb_pressed = true;
  392. Invalidate ();
  393. thumb_pixel_click_move = e.Y;
  394. }
  395. else {
  396. if (paint_area.Contains (point)) {
  397. if (e.Y > thumb_pos.Y + thumb_pos.Height)
  398. LargeIncrement ();
  399. else
  400. LargeDecrement ();
  401. Invalidate ();
  402. }
  403. }
  404. }
  405. }
  406. protected override void OnMouseMove (MouseEventArgs e)
  407. {
  408. if (!Enabled) return;
  409. //System.Console.WriteLine ("OnMouseMove " + thumb_pressed);
  410. Point pnt = new Point (e.X, e.Y);
  411. /* Moving the thumb */
  412. if (thumb_pos.Contains (pnt) && thumb_pressed) {
  413. System.Console.WriteLine ("OnMouseMove " + thumb_pressed);
  414. int pixel_pos;
  415. if (orientation == Orientation.Horizontal)
  416. pixel_pos = e.X - (thumb_pixel_click_move - thumb_pos.X);
  417. else
  418. pixel_pos = e.Y - (thumb_pixel_click_move - thumb_pos.Y);
  419. UpdateThumbPos (pixel_pos, true);
  420. if (orientation == Orientation.Horizontal)
  421. thumb_pixel_click_move = e.X;
  422. else
  423. thumb_pixel_click_move = e.Y;
  424. fire_scroll_event ();
  425. //System.Console.WriteLine ("OnMouseMove thumb "+ e.Y
  426. // + " clickpos " + thumb_pixel_click_move + " pos:" + thumb_pos.Y);
  427. Invalidate ();
  428. }
  429. if (!thumb_pos.Contains (pnt) && thumb_pressed) {
  430. thumb_pressed = false;
  431. Invalidate ();
  432. }
  433. }
  434. protected override void OnMouseWheel (MouseEventArgs e)
  435. {
  436. if (!Enabled) return;
  437. System.Console.WriteLine ("OnMouseWheel delta: " + e.Delta + " clicks:" + e.Clicks);
  438. }
  439. #endregion //Override Event Methods
  440. }
  441. }