ScrollBar.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. //
  2. // System.Windows.Forms.ScrollBar.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. // Copyright (C) 2004, Novell, Inc.
  24. //
  25. // Authors:
  26. // Jordi Mas i Hernandez [email protected]
  27. //
  28. //
  29. // $Revision: 1.3 $
  30. // $Modtime: $
  31. // $Log: ScrollBar.cs,v $
  32. // Revision 1.3 2004/07/27 15:29:40 jordi
  33. // fixes scrollbar events
  34. //
  35. // Revision 1.2 2004/07/26 17:42:03 jordi
  36. // Theme support
  37. //
  38. // NOT COMPLETE
  39. using System.Drawing;
  40. using System.Drawing.Imaging;
  41. using System.Drawing.Drawing2D;
  42. using System.ComponentModel;
  43. using System.Runtime.InteropServices;
  44. using System.Timers;
  45. namespace System.Windows.Forms
  46. {
  47. public class ScrollBar : Control
  48. {
  49. #region Local Variables
  50. private int position;
  51. private int minimum;
  52. private int maximum;
  53. private int largeChange;
  54. private int smallChange;
  55. private int scrollbutton_height;
  56. private int scrollbutton_width;
  57. private Rectangle paint_area = new Rectangle ();
  58. private ScrollBars type;
  59. private Bitmap bmp_mem = null;
  60. private Graphics dc_mem = null;
  61. private Rectangle first_arrow_area = new Rectangle (); // up or left
  62. private Rectangle second_arrow_area = new Rectangle (); // down or right
  63. private Rectangle thumb_pos = new Rectangle ();
  64. private Rectangle thumb_area = new Rectangle ();
  65. private ButtonState firstbutton_state = ButtonState.Normal;
  66. private ButtonState secondbutton_state = ButtonState.Normal;
  67. private bool thumb_pressed = false;
  68. private float pixel_per_pos = 0;
  69. private System.Timers.Timer firstclick_timer = new System.Timers.Timer ();
  70. private System.Timers.Timer holdclick_timer = new System.Timers.Timer ();
  71. private int thumb_pixel_click_move;
  72. private int thumb_size = 0;
  73. protected bool vert;
  74. public event ScrollEventHandler Scroll;
  75. public event EventHandler ValueChanged;
  76. #endregion // Local Variables
  77. public ScrollBar() : base()
  78. {
  79. position = 0;
  80. minimum = 0;
  81. maximum = 100;
  82. largeChange = 10;
  83. smallChange = 1;
  84. // base.TabStop = false;
  85. // RightToLeft = RightToLeft.No;
  86. Scroll = null;
  87. ValueChanged = null;
  88. SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
  89. SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
  90. }
  91. [EditorBrowsable (EditorBrowsableState.Never)]
  92. public override Color BackColor
  93. {
  94. get { return base.BackColor; }
  95. set { base.BackColor = value;}
  96. }
  97. [EditorBrowsable (EditorBrowsableState.Never)]
  98. [MonoTODO]
  99. public override Image BackgroundImage
  100. {
  101. get {
  102. throw new NotImplementedException();
  103. }
  104. set {
  105. throw new NotImplementedException();
  106. }
  107. }
  108. [EditorBrowsable (EditorBrowsableState.Never)]
  109. public override Color ForeColor
  110. {
  111. get { return base.ForeColor; }
  112. set { base.ForeColor = value; }
  113. }
  114. [EditorBrowsable (EditorBrowsableState.Never)]
  115. [MonoTODO]
  116. public new ImeMode ImeMode
  117. {
  118. get { return ImeMode.Disable;
  119. throw new NotImplementedException(); }
  120. set { throw new NotImplementedException(); }
  121. }
  122. public int LargeChange {
  123. get { return largeChange; }
  124. set {
  125. if (value < 0)
  126. throw new Exception( string.Format("Value '{0}' must be greater than or equal to 0.", value));
  127. if (largeChange != value) {
  128. largeChange = value;
  129. Invalidate ();
  130. }
  131. }
  132. }
  133. public int Maximum {
  134. get { return maximum; }
  135. set {
  136. maximum = value;
  137. if (maximum < minimum)
  138. minimum = maximum;
  139. }
  140. }
  141. public int Minimum {
  142. get { return minimum; }
  143. set {
  144. minimum = value;
  145. if (minimum > maximum)
  146. maximum = minimum;
  147. }
  148. }
  149. public int SmallChange {
  150. get { return smallChange; }
  151. set {
  152. if ( value < 0 )
  153. throw new Exception( string.Format("Value '{0}' must be greater than or equal to 0.", value));
  154. if (smallChange != value) {
  155. smallChange = value;
  156. Invalidate ();
  157. }
  158. }
  159. }
  160. [EditorBrowsable (EditorBrowsableState.Never)]
  161. public override string Text {
  162. get { return base.Text; }
  163. set { base.Text = value; }
  164. }
  165. public int Value {
  166. get { return position; }
  167. set {
  168. if ( value < Minimum || value > Maximum )
  169. throw new ArgumentException(
  170. string.Format("'{0}' is not a valid value for 'Value'. 'Value' should be between 'Minimum' and 'Maximum'", value));
  171. if (position != value){
  172. position = value;
  173. if (ValueChanged != null)
  174. ValueChanged (this, EventArgs.Empty);
  175. }
  176. }
  177. }
  178. public override string ToString()
  179. {
  180. return string.Format("{0}, Minimum: {1}, Maximum: {2}, Value: {3}",
  181. GetType( ).FullName.ToString( ), Minimum, Maximum, position);
  182. }
  183. protected override CreateParams CreateParams {
  184. get {
  185. CreateParams createParams = base.CreateParams;
  186. createParams.ClassName = XplatUI.DefaultClassName;
  187. createParams.Style = (int) (
  188. WindowStyles.WS_CHILD |
  189. WindowStyles.WS_VISIBLE);
  190. return createParams;
  191. }
  192. }
  193. protected override ImeMode DefaultImeMode {
  194. get { return ImeMode.Disable; }
  195. }
  196. private void fire_Scroll (ScrollEventArgs event_args)
  197. {
  198. if (Scroll == null)
  199. return;
  200. Scroll (this, event_args);
  201. //if (event_args.NewValue != position)
  202. // UpdatePos (event_args.NewValue, true);
  203. }
  204. protected virtual void OnValueChanged (EventArgs e)
  205. {
  206. if (ValueChanged != null) {
  207. Console.Write ("OnValueChanged");
  208. ValueChanged (this, e);
  209. }
  210. }
  211. private void draw()
  212. {
  213. ThemeEngine.Current.DrawScrollBar (dc_mem, paint_area, thumb_pos,
  214. ref first_arrow_area, ref second_arrow_area,
  215. firstbutton_state, secondbutton_state,
  216. ref scrollbutton_width, ref scrollbutton_height,
  217. Enabled, vert);
  218. }
  219. private void CalcThumbArea ()
  220. {
  221. // Thumb area
  222. if (vert) {
  223. if (Height < scrollbutton_height * 2)
  224. thumb_size = 0;
  225. else
  226. if (Height < 70)
  227. thumb_size = 8;
  228. else
  229. thumb_size = Height /10;
  230. thumb_area.X = 0;
  231. thumb_area.Y = scrollbutton_height;
  232. thumb_area.Width = Width;
  233. thumb_area.Height = Height - scrollbutton_height - scrollbutton_height;
  234. pixel_per_pos = ((float)(thumb_area.Height - thumb_size) / (float) ((Maximum - Minimum - LargeChange) + 1));
  235. } else {
  236. if (Width < scrollbutton_width * 2)
  237. thumb_size = 0;
  238. else
  239. if (Width < 70)
  240. thumb_size = 8;
  241. else
  242. thumb_size = Width /10;
  243. thumb_area.Y = 0;
  244. thumb_area.X = scrollbutton_width;
  245. thumb_area.Height = Height;
  246. thumb_area.Width = Width - scrollbutton_width - scrollbutton_width;
  247. pixel_per_pos = ((float)(thumb_area.Width - thumb_size) / (float) ((Maximum - Minimum - LargeChange) + 1));
  248. }
  249. //Console.WriteLine ("thumb_area:" + thumb_area);
  250. //Console.WriteLine ("Maximum {0} Minimum {1} " , Maximum, Minimum);
  251. }
  252. protected override void OnResize (EventArgs e)
  253. {
  254. base.OnResize (e);
  255. if (Width <= 0 || Height <= 0)
  256. return;
  257. paint_area.X = paint_area. Y = 0;
  258. paint_area.Width = Width;
  259. paint_area.Height = Height;
  260. /* Area for double buffering */
  261. bmp_mem = new Bitmap (Width, Height, PixelFormat.Format32bppArgb);
  262. dc_mem = Graphics.FromImage (bmp_mem);
  263. CalcThumbArea ();
  264. UpdatePos (position, true);
  265. }
  266. /*
  267. Called when the control is created
  268. */
  269. protected override void CreateHandle()
  270. {
  271. //Console.WriteLine ("CreateHandle()");
  272. base.CreateHandle(); // Let control.cs create the underlying Window
  273. scrollbutton_height = 17;
  274. scrollbutton_width = 17;
  275. /* Area for double buffering */
  276. bmp_mem = new Bitmap (Width, Height, PixelFormat.Format32bppArgb);
  277. dc_mem = Graphics.FromImage (bmp_mem);
  278. //Console.WriteLine ("OnCreate: Width " + Width + " Height " + Height);
  279. CalcThumbArea ();
  280. UpdatePos (Value, true);
  281. draw();
  282. }
  283. protected override void OnPaint (PaintEventArgs pevent)
  284. {
  285. if (Width <= 0 || Height <= 0 || Visible == false)
  286. return;
  287. /* Copies memory drawing buffer to screen*/
  288. draw();
  289. pevent.Graphics.DrawImage (bmp_mem, 0, 0);
  290. }
  291. /* Disable background painting to avoid flickering, since we do our painting*/
  292. protected override void OnPaintBackground (PaintEventArgs pevent)
  293. {
  294. // None
  295. }
  296. protected override void OnClick (EventArgs e)
  297. {
  298. //Console.WriteLine ("On click");
  299. }
  300. private void UpdatePos (int newPos, bool update_trumbpos)
  301. {
  302. int old = position;
  303. if (newPos < minimum)
  304. position = minimum;
  305. else
  306. if (newPos > maximum)
  307. position = maximum;
  308. else
  309. position = newPos;
  310. //Console.WriteLine ("event : {0} {1} {2}", Scroll != null, position, old);
  311. if (update_trumbpos)
  312. if (vert)
  313. UpdateThumbPos (thumb_area.Y + (int)(((float)(position - Minimum)) * pixel_per_pos), false);
  314. else
  315. UpdateThumbPos (thumb_area.X + (int)(((float)(position - Minimum)) * pixel_per_pos), false);
  316. if (position != old) // Fire event
  317. fire_Scroll (new ScrollEventArgs (ScrollEventType.ThumbTrack, position));
  318. }
  319. private void UpdateThumbPos (int pixel, bool update_value)
  320. {
  321. float new_pos = 0;
  322. if (vert) {
  323. if (pixel < thumb_area.Y)
  324. thumb_pos.Y = thumb_area.Y;
  325. else
  326. if (pixel > thumb_area.Y + thumb_area.Height - thumb_size)
  327. thumb_pos.Y = thumb_area.Y + thumb_area.Height - thumb_size;
  328. else
  329. thumb_pos.Y = pixel;
  330. thumb_pos = new Rectangle (0, thumb_pos.Y, Width, thumb_size);
  331. new_pos = (float) (thumb_pos.Y - thumb_area.Y);
  332. new_pos = new_pos / pixel_per_pos;
  333. } else {
  334. if (pixel < thumb_area.X)
  335. thumb_pos.X = thumb_area.X;
  336. else
  337. if (pixel > thumb_area.X + thumb_area.Width - thumb_size)
  338. thumb_pos.X = thumb_area.X + thumb_area.Width - thumb_size;
  339. else
  340. thumb_pos.X = pixel;
  341. thumb_pos = new Rectangle (thumb_pos.X, 0, thumb_size, Height);
  342. new_pos = (float) (thumb_pos.X - thumb_area.X);
  343. new_pos = new_pos / pixel_per_pos;
  344. }
  345. //Console.WriteLine ("UpdateThumbPos: thumb_pos.Y {0} thumb_area.Y {1} pixel_per_pos {2}, new pos {3}, pixel {4}",
  346. // thumb_pos.Y, thumb_area.Y, pixel_per_pos, new_pos, pixel);
  347. if (update_value)
  348. UpdatePos ((int) new_pos, false);
  349. }
  350. private void OnHoldClickTimer (Object source, ElapsedEventArgs e)
  351. {
  352. if ((firstbutton_state & ButtonState.Pushed) == ButtonState.Pushed)
  353. SmallDecrement();
  354. if ((secondbutton_state & ButtonState.Pushed) == ButtonState.Pushed)
  355. SmallIncrement();
  356. Invalidate ();
  357. }
  358. private void OnFirstClickTimer (Object source, ElapsedEventArgs e)
  359. {
  360. //Console.WriteLine ("OnFirstClickTimer");
  361. firstclick_timer.Enabled = false;
  362. holdclick_timer.Elapsed += new ElapsedEventHandler (OnHoldClickTimer);
  363. holdclick_timer.Interval = 50;
  364. holdclick_timer.Enabled = true;
  365. }
  366. protected override void OnMouseMove (MouseEventArgs e)
  367. {
  368. if (!first_arrow_area.Contains (new Point (e.X, e.Y)) &&
  369. ((firstbutton_state & ButtonState.Pushed) == ButtonState.Pushed)) {
  370. firstbutton_state = ButtonState.Normal;
  371. Invalidate ();
  372. }
  373. if (!second_arrow_area.Contains (new Point (e.X, e.Y)) &&
  374. ((secondbutton_state & ButtonState.Pushed) == ButtonState.Pushed)) {
  375. secondbutton_state = ButtonState.Normal;
  376. Invalidate ();
  377. }
  378. if (thumb_pos.Contains (new Point (e.X, e.Y)) && thumb_pressed) {
  379. int pixel_pos;
  380. if (vert)
  381. pixel_pos = e.Y - (thumb_pixel_click_move - thumb_pos.Y);
  382. else
  383. pixel_pos = e.X - (thumb_pixel_click_move - thumb_pos.X);
  384. UpdateThumbPos (pixel_pos, true);
  385. if (vert)
  386. thumb_pixel_click_move = e.Y;
  387. else
  388. thumb_pixel_click_move = e.X;
  389. //System.Console.WriteLine ("OnMouseMove thumb "+ e.Y
  390. // + " clickpos " + thumb_pixel_click_move + " pos:" + thumb_pos.Y);
  391. Invalidate ();
  392. }
  393. if (!thumb_pos.Contains (new Point (e.X, e.Y)) && thumb_pressed) {
  394. thumb_pressed = false;
  395. Invalidate ();
  396. }
  397. }
  398. protected override void OnMouseDown (MouseEventArgs e)
  399. {
  400. //System.Console.WriteLine ("OnMouseDown");
  401. Point point = new Point (e.X, e.Y);
  402. if (first_arrow_area.Contains (point)) {
  403. firstbutton_state = ButtonState.Pushed;
  404. Invalidate ();
  405. }
  406. if (second_arrow_area.Contains (point)) {
  407. secondbutton_state = ButtonState.Pushed;
  408. Invalidate ();
  409. }
  410. if (thumb_pos.Contains (point)) {
  411. thumb_pressed = true;
  412. Invalidate ();
  413. if (vert)
  414. thumb_pixel_click_move = e.Y;
  415. else
  416. thumb_pixel_click_move = e.X;
  417. }
  418. else
  419. if (thumb_area.Contains (point)) {
  420. if (vert) {
  421. if (e.Y > thumb_pos.Y + thumb_pos.Height)
  422. LargeIncrement();
  423. else
  424. LargeDecrement();
  425. } else {
  426. if (e.X > thumb_pos.X + thumb_pos.Width)
  427. LargeIncrement();
  428. else
  429. LargeDecrement();
  430. }
  431. Invalidate ();
  432. }
  433. /* If arrows are pressed, lunch timer for auto-repeat */
  434. if ((((firstbutton_state & ButtonState.Pushed) == ButtonState.Pushed)
  435. || ((secondbutton_state & ButtonState.Pushed) == ButtonState.Pushed)) &&
  436. firstclick_timer.Enabled == false) {
  437. //Console.WriteLine ("Activate Timer");
  438. firstclick_timer.Elapsed += new ElapsedEventHandler (OnFirstClickTimer);
  439. firstclick_timer.Interval = 400;
  440. firstclick_timer.Enabled = true;
  441. firstclick_timer.AutoReset = false;
  442. }
  443. }
  444. private void SmallIncrement()
  445. {
  446. if (vert)
  447. UpdateThumbPos (thumb_pos.Y + SmallChange, true);
  448. else
  449. UpdateThumbPos (thumb_pos.X + SmallChange, true);
  450. fire_Scroll (new ScrollEventArgs (ScrollEventType.SmallIncrement, position));
  451. fire_Scroll (new ScrollEventArgs (ScrollEventType.EndScroll, position));
  452. }
  453. private void SmallDecrement()
  454. {
  455. if (vert)
  456. UpdateThumbPos (thumb_pos.Y - SmallChange, true);
  457. else
  458. UpdateThumbPos (thumb_pos.X - SmallChange, true);
  459. fire_Scroll (new ScrollEventArgs (ScrollEventType.SmallDecrement, position));
  460. fire_Scroll (new ScrollEventArgs (ScrollEventType.EndScroll, position));
  461. }
  462. private void LargeIncrement()
  463. {
  464. if (vert)
  465. UpdateThumbPos (thumb_pos.Y + LargeChange, true);
  466. else
  467. UpdateThumbPos (thumb_pos.X + LargeChange, true);
  468. fire_Scroll (new ScrollEventArgs (ScrollEventType.LargeIncrement, position));
  469. fire_Scroll (new ScrollEventArgs (ScrollEventType.EndScroll, position));
  470. }
  471. private void LargeDecrement()
  472. {
  473. if (vert)
  474. UpdateThumbPos (thumb_pos.Y - LargeChange, true);
  475. else
  476. UpdateThumbPos (thumb_pos.X - LargeChange, true);
  477. fire_Scroll (new ScrollEventArgs (ScrollEventType.LargeDecrement, position));
  478. fire_Scroll (new ScrollEventArgs (ScrollEventType.EndScroll, position));
  479. }
  480. protected override void OnMouseUp (MouseEventArgs e)
  481. {
  482. //System.Console.WriteLine ("OnMouseUp");
  483. if (first_arrow_area.Contains (new Point (e.X, e.Y))) {
  484. firstbutton_state = ButtonState.Normal;
  485. SmallDecrement ();
  486. Invalidate ();
  487. holdclick_timer.Enabled = false;
  488. }
  489. if (second_arrow_area.Contains (new Point (e.X, e.Y))) {
  490. secondbutton_state = ButtonState.Normal;
  491. SmallIncrement ();
  492. Invalidate ();
  493. holdclick_timer.Enabled = false;
  494. }
  495. if (thumb_pos.Contains (new Point (e.X, e.Y))) {
  496. fire_Scroll (new ScrollEventArgs (ScrollEventType.ThumbPosition, position));
  497. fire_Scroll (new ScrollEventArgs (ScrollEventType.EndScroll, position));
  498. thumb_pressed = false;
  499. Invalidate ();
  500. }
  501. }
  502. //TODO: Untested
  503. protected override void OnKeyDown (KeyEventArgs key)
  504. {
  505. switch (key.KeyCode){
  506. case Keys.Up:
  507. {
  508. SmallDecrement();
  509. break;
  510. }
  511. case Keys.Down:
  512. {
  513. SmallIncrement();
  514. break;
  515. }
  516. case Keys.PageUp:
  517. {
  518. LargeDecrement();
  519. break;
  520. }
  521. case Keys.PageDown:
  522. {
  523. LargeIncrement();
  524. break;
  525. }
  526. default:
  527. break;
  528. }
  529. }
  530. protected void UpdateScrollInfo ()
  531. {
  532. Invalidate ();
  533. }
  534. }
  535. }