ScrollBar.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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.19 $
  30. // $Modtime: $
  31. // $Log: ScrollBar.cs,v $
  32. // Revision 1.19 2004/09/05 08:03:51 jordi
  33. // fixes bugs, adds flashing on certain situations
  34. //
  35. // Revision 1.18 2004/08/31 10:35:04 jordi
  36. // adds autorepeat timer, uses a single timer, fixes scrolling bugs, adds new methods
  37. //
  38. // Revision 1.17 2004/08/25 21:35:18 jordi
  39. // more fixes to scrollbar
  40. //
  41. // Revision 1.16 2004/08/25 19:20:13 jordi
  42. // small bug fix regarding bar position
  43. //
  44. // Revision 1.15 2004/08/24 18:37:02 jordi
  45. // fixes formmating, methods signature, and adds missing events
  46. //
  47. // Revision 1.14 2004/08/23 22:53:15 jordi
  48. // small fix
  49. //
  50. // Revision 1.13 2004/08/23 22:43:46 jordi
  51. // *** empty log message ***
  52. //
  53. // Revision 1.11 2004/08/22 19:34:22 jackson
  54. // Update the position through the Value property so the OnValueChanged event is raised.
  55. //
  56. // Revision 1.10 2004/08/21 20:22:21 pbartok
  57. // - Replaced direct XplatUI calls with their Control counterpart
  58. //
  59. // Revision 1.9 2004/08/20 19:35:33 jackson
  60. // Use the SWF timer so callbacks are run in the correct thread
  61. //
  62. // Revision 1.8 2004/08/20 19:34:26 jackson
  63. // Use the SWF timer so callbacks are run in the correct thread
  64. //
  65. // Revision 1.7 2004/08/19 22:25:31 jordi
  66. // theme enhancaments
  67. //
  68. // Revision 1.6 2004/08/18 15:56:12 jordi
  69. // fixes to scrollbar: steps and multiple timers
  70. //
  71. // Revision 1.5 2004/08/10 19:21:27 jordi
  72. // scrollbar enhancements and standarize on win colors defaults
  73. //
  74. // Revision 1.4 2004/08/10 15:41:50 jackson
  75. // Allow control to handle buffering
  76. //
  77. // Revision 1.3 2004/07/27 15:29:40 jordi
  78. // fixes scrollbar events
  79. //
  80. // Revision 1.2 2004/07/26 17:42:03 jordi
  81. // Theme support
  82. //
  83. // COMPLETE
  84. using System.Drawing;
  85. using System.Drawing.Imaging;
  86. using System.Drawing.Drawing2D;
  87. using System.ComponentModel;
  88. using System.Runtime.InteropServices;
  89. namespace System.Windows.Forms
  90. {
  91. [DefaultEvent ("Scroll")]
  92. [DefaultProperty ("Value")]
  93. public class ScrollBar : Control
  94. {
  95. #region Local Variables
  96. private int position;
  97. private int minimum;
  98. private int maximum;
  99. private int large_change;
  100. private int small_change;
  101. private int scrollbutton_height;
  102. private int scrollbutton_width;
  103. private Rectangle paint_area = new Rectangle ();
  104. private ScrollBars type;
  105. private Rectangle first_arrow_area = new Rectangle (); // up or left
  106. private Rectangle second_arrow_area = new Rectangle (); // down or right
  107. private Rectangle thumb_pos = new Rectangle ();
  108. private Rectangle thumb_area = new Rectangle ();
  109. private ButtonState firstbutton_state = ButtonState.Normal;
  110. private ButtonState secondbutton_state = ButtonState.Normal;
  111. private bool thumb_pressed = false;
  112. private float pixel_per_pos = 0;
  113. private Timer timer = new Timer ();
  114. private TimerType timer_type;
  115. private int thumb_pixel_click_move;
  116. private int thumb_pixel_click_move_prev;
  117. private int thumb_size = 0;
  118. private const int thumb_min_size = 8;
  119. internal bool vert;
  120. private int lastclick_pos; // Position of the last button-down event
  121. private int lastclick_pos_thumb; // Position of the last button-down event relative to the thumb
  122. private bool outside_thumbarea_right = false;
  123. private bool outside_thumbarea_left = false;
  124. internal ThumbMoving thumb_moving = ThumbMoving.None;
  125. #endregion // Local Variables
  126. private enum TimerType
  127. {
  128. HoldButton,
  129. RepeatButton,
  130. HoldThumbArea,
  131. RepeatThumbArea
  132. }
  133. internal enum ThumbMoving
  134. {
  135. None,
  136. Forward,
  137. Backwards,
  138. }
  139. #region Events
  140. public new event EventHandler BackColorChanged;
  141. public new event EventHandler BackgroundImageChanged;
  142. public new event EventHandler Click;
  143. public new event EventHandler DoubleClick;
  144. public new event EventHandler FontChanged;
  145. public new event EventHandler ForeColorChanged;
  146. public new event EventHandler ImeModeChanged;
  147. public new event MouseEventHandler MouseDown;
  148. public new event MouseEventHandler MouseMove;
  149. public new event MouseEventHandler MouseUp;
  150. public new event PaintEventHandler Paint;
  151. public event ScrollEventHandler Scroll;
  152. public new event EventHandler TextChanged;
  153. public event EventHandler ValueChanged;
  154. #endregion Events
  155. public ScrollBar ()
  156. {
  157. position = 0;
  158. minimum = 0;
  159. maximum = 100;
  160. large_change = 10;
  161. small_change = 1;
  162. timer.Tick += new EventHandler (OnTimer);
  163. base.KeyDown += new KeyEventHandler (OnKeyDownSB);
  164. base.MouseDown += new MouseEventHandler (OnMouseDownSB);
  165. base.MouseUp += new MouseEventHandler (OnMouseUpSB);
  166. base.MouseMove += new MouseEventHandler (OnMouseMoveSB);
  167. base.Resize += new EventHandler (OnResizeSB);
  168. base.TabStop = false;
  169. if (ThemeEngine.Current.WriteToWindow == true)
  170. double_buffering = false;
  171. else
  172. double_buffering = true;
  173. SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
  174. SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
  175. }
  176. #region Public Properties
  177. [EditorBrowsable (EditorBrowsableState.Never)]
  178. public override Color BackColor
  179. {
  180. get { return base.BackColor; }
  181. set {
  182. if (base.BackColor == value)
  183. return;
  184. if (BackColorChanged != null)
  185. BackColorChanged (this, EventArgs.Empty);
  186. base.BackColor = value;
  187. Refresh ();
  188. }
  189. }
  190. [EditorBrowsable (EditorBrowsableState.Never)]
  191. public override Image BackgroundImage
  192. {
  193. get { return base.BackgroundImage; }
  194. set {
  195. if (base.BackgroundImage == value)
  196. return;
  197. if (BackgroundImageChanged != null)
  198. BackgroundImageChanged (this, EventArgs.Empty);
  199. base.BackgroundImage = value;
  200. }
  201. }
  202. protected override CreateParams CreateParams
  203. {
  204. get { return base.CreateParams; }
  205. }
  206. protected override ImeMode DefaultImeMode
  207. {
  208. get { return ImeMode.Disable; }
  209. }
  210. public override Font Font
  211. {
  212. get { return base.Font; }
  213. set {
  214. if (base.Font == value)
  215. return;
  216. if (FontChanged != null)
  217. FontChanged (this, EventArgs.Empty);
  218. base.Font = value;
  219. }
  220. }
  221. [EditorBrowsable (EditorBrowsableState.Never)]
  222. public override Color ForeColor
  223. {
  224. get { return base.ForeColor; }
  225. set {
  226. if (base.ForeColor == value)
  227. return;
  228. if (ForeColorChanged != null)
  229. ForeColorChanged (this, EventArgs.Empty);
  230. base.ForeColor = value;
  231. Refresh ();
  232. }
  233. }
  234. [EditorBrowsable (EditorBrowsableState.Never)]
  235. public new ImeMode ImeMode
  236. {
  237. get { return base.ImeMode; }
  238. set {
  239. if (base.ImeMode == value)
  240. return;
  241. if (ImeModeChanged != null)
  242. ImeModeChanged (this, EventArgs.Empty);
  243. base.ImeMode = value;
  244. }
  245. }
  246. public int LargeChange {
  247. get { return large_change; }
  248. set {
  249. if (value < 0)
  250. throw new Exception( string.Format("Value '{0}' must be greater than or equal to 0.", value));
  251. if (large_change != value) {
  252. large_change = value;
  253. Refresh ();
  254. }
  255. }
  256. }
  257. public int Maximum {
  258. get { return maximum; }
  259. set {
  260. maximum = value;
  261. if (maximum < minimum)
  262. minimum = maximum;
  263. Refresh ();
  264. }
  265. }
  266. public int Minimum {
  267. get { return minimum; }
  268. set {
  269. minimum = value;
  270. if (minimum > maximum)
  271. maximum = minimum;
  272. Refresh ();
  273. }
  274. }
  275. public int SmallChange {
  276. get { return small_change; }
  277. set {
  278. if ( value < 0 )
  279. throw new Exception( string.Format("Value '{0}' must be greater than or equal to 0.", value));
  280. if (small_change != value) {
  281. small_change = value;
  282. Refresh ();
  283. }
  284. }
  285. }
  286. public new bool TabStop {
  287. get { return base.TabStop; }
  288. set { base.TabStop = value; }
  289. }
  290. [EditorBrowsable (EditorBrowsableState.Never)]
  291. public override string Text {
  292. get { return base.Text; }
  293. set { base.Text = value; }
  294. }
  295. public int Value {
  296. get { return position; }
  297. set {
  298. if ( value < minimum || value > maximum )
  299. throw new ArgumentException(
  300. string.Format("'{0}' is not a valid value for 'Value'. 'Value' should be between 'Minimum' and 'Maximum'", value));
  301. if (position != value){
  302. position = value;
  303. if (ValueChanged != null)
  304. ValueChanged (this, EventArgs.Empty);
  305. UpdatePos (Value, true);
  306. Refresh ();
  307. }
  308. }
  309. }
  310. #endregion //Public Properties
  311. #region Public Methods
  312. protected override void OnEnabledChanged (EventArgs e)
  313. {
  314. base.OnEnabledChanged (e);
  315. if (Enabled)
  316. firstbutton_state = secondbutton_state = ButtonState.Normal;
  317. else
  318. firstbutton_state = secondbutton_state = ButtonState.Inactive;
  319. Refresh ();
  320. }
  321. protected override void OnHandleCreated (System.EventArgs e)
  322. {
  323. base.OnHandleCreated (e);
  324. scrollbutton_height = ThemeEngine.Current.ScrollBarButtonSize;
  325. scrollbutton_width = ThemeEngine.Current.ScrollBarButtonSize;
  326. CreateBuffers (Width, Height);
  327. CalcThumbArea ();
  328. UpdatePos (Value, true);
  329. }
  330. protected virtual void OnScroll (ScrollEventArgs event_args)
  331. {
  332. if (Scroll == null)
  333. return;
  334. Scroll (this, event_args);
  335. }
  336. protected virtual void OnValueChanged (EventArgs e)
  337. {
  338. if (ValueChanged != null)
  339. ValueChanged (this, e);
  340. }
  341. public override string ToString()
  342. {
  343. return string.Format("{0}, Minimum: {1}, Maximum: {2}, Value: {3}",
  344. GetType( ).FullName.ToString( ), minimum, maximum, position);
  345. }
  346. protected void UpdateScrollInfo ()
  347. {
  348. Refresh ();
  349. }
  350. protected override void WndProc (ref Message m)
  351. {
  352. switch ((Msg) m.Msg)
  353. {
  354. case Msg.WM_PAINT:
  355. {
  356. PaintEventArgs paint_event;
  357. paint_event = XplatUI.PaintEventStart (Handle);
  358. OnPaintSB (paint_event);
  359. XplatUI.PaintEventEnd (Handle);
  360. return;
  361. }
  362. case Msg.WM_ERASEBKGND:
  363. m.Result = (IntPtr) 1; /// Disable background painting to avoid flickering
  364. return;
  365. default:
  366. break;
  367. }
  368. base.WndProc (ref m);
  369. }
  370. #endregion //Public Methods
  371. #region Private Methods
  372. private void CalcThumbArea ()
  373. {
  374. // Thumb area
  375. if (vert) {
  376. thumb_area.Height = Height - scrollbutton_height - scrollbutton_height;
  377. thumb_area.X = 0;
  378. thumb_area.Y = scrollbutton_height;
  379. thumb_area.Width = Width;
  380. if (Height < scrollbutton_height * 2)
  381. thumb_size = 0;
  382. else {
  383. double per = ((double) large_change / (double)((1 + maximum - minimum)));
  384. thumb_size = 1 + (int) (thumb_area.Height * per);
  385. if (thumb_size < thumb_min_size)
  386. thumb_size = thumb_min_size;
  387. }
  388. pixel_per_pos = ((float)(thumb_area.Height - thumb_size) / (float) ((maximum - minimum - large_change) + 1));
  389. } else {
  390. thumb_area.Y = 0;
  391. thumb_area.X = scrollbutton_width;
  392. thumb_area.Height = Height;
  393. thumb_area.Width = Width - scrollbutton_width - scrollbutton_width;
  394. if (Width < scrollbutton_width * 2)
  395. thumb_size = 0;
  396. else {
  397. double per = ((double) large_change / (double)((1 + maximum - minimum)));
  398. thumb_size = 1 + (int) (thumb_area.Width * per);
  399. if (thumb_size < thumb_min_size)
  400. thumb_size = thumb_min_size;
  401. }
  402. pixel_per_pos = ((float)(thumb_area.Width - thumb_size) / (float) ((maximum - minimum - large_change) + 1));
  403. }
  404. }
  405. private void Draw ()
  406. {
  407. ThemeEngine.Current.DrawScrollBar (DeviceContext, paint_area, this, ref thumb_pos,
  408. ref first_arrow_area, ref second_arrow_area,
  409. firstbutton_state, secondbutton_state,
  410. ref scrollbutton_width, ref scrollbutton_height, vert);
  411. }
  412. private void LargeIncrement ()
  413. {
  414. UpdatePos (position + large_change, true);
  415. Refresh ();
  416. OnScroll (new ScrollEventArgs (ScrollEventType.LargeIncrement, position));
  417. OnScroll (new ScrollEventArgs (ScrollEventType.EndScroll, position));
  418. }
  419. private void LargeDecrement ()
  420. {
  421. UpdatePos (position - large_change, true);
  422. Refresh ();
  423. OnScroll (new ScrollEventArgs (ScrollEventType.LargeDecrement, position));
  424. OnScroll (new ScrollEventArgs (ScrollEventType.EndScroll, position));
  425. }
  426. private void OnResizeSB (Object o, EventArgs e)
  427. {
  428. if (Width <= 0 || Height <= 0)
  429. return;
  430. paint_area.X = paint_area. Y = 0;
  431. paint_area.Width = Width;
  432. paint_area.Height = Height;
  433. CreateBuffers (Width, Height);
  434. CalcThumbArea ();
  435. UpdatePos (position, true);
  436. }
  437. private void OnPaintSB (PaintEventArgs pevent)
  438. {
  439. if (Width <= 0 || Height <= 0 || Visible == false)
  440. return;
  441. /* Copies memory drawing buffer to screen*/
  442. Draw ();
  443. if (double_buffering)
  444. pevent.Graphics.DrawImage (ImageBuffer, 0, 0);
  445. }
  446. private void OnTimer (Object source, EventArgs e)
  447. {
  448. switch (timer_type) {
  449. case TimerType.HoldButton:
  450. SetRepeatButtonTimer ();
  451. break;
  452. case TimerType.RepeatButton:
  453. {
  454. if ((firstbutton_state & ButtonState.Pushed) == ButtonState.Pushed)
  455. SmallDecrement();
  456. if ((secondbutton_state & ButtonState.Pushed) == ButtonState.Pushed)
  457. SmallIncrement();
  458. break;
  459. }
  460. case TimerType.HoldThumbArea:
  461. SetRepeatThumbAreaTimer ();
  462. break;
  463. case TimerType.RepeatThumbArea:
  464. {
  465. Point pnt;
  466. pnt = PointToClient (MousePosition);
  467. if (vert)
  468. lastclick_pos = pnt.Y;
  469. else
  470. lastclick_pos = pnt.X;
  471. if (thumb_moving == ThumbMoving.Forward) {
  472. if ((vert && (thumb_pos.Y + thumb_size > lastclick_pos)) ||
  473. (!vert && (thumb_pos.X + thumb_size > lastclick_pos)) ||
  474. (thumb_area.Contains (pnt) == false)){
  475. timer.Enabled = false;
  476. thumb_moving = ThumbMoving.None;
  477. Refresh ();
  478. } else
  479. LargeIncrement ();
  480. }
  481. else
  482. if ((vert && (thumb_pos.Y < lastclick_pos)) ||
  483. (!vert && (thumb_pos.X < lastclick_pos))){
  484. timer.Enabled = false;
  485. thumb_moving = ThumbMoving.None;
  486. Refresh ();
  487. } else
  488. LargeDecrement ();
  489. break;
  490. }
  491. default:
  492. break;
  493. }
  494. }
  495. private void OnMouseMoveSB (object sender, MouseEventArgs e)
  496. {
  497. if (!first_arrow_area.Contains (e.X, e.Y) &&
  498. ((firstbutton_state & ButtonState.Pushed) == ButtonState.Pushed)) {
  499. firstbutton_state = ButtonState.Normal;
  500. this.Capture = false;
  501. Refresh ();
  502. }
  503. if (!second_arrow_area.Contains (e.X, e.Y) &&
  504. ((secondbutton_state & ButtonState.Pushed) == ButtonState.Pushed)) {
  505. secondbutton_state = ButtonState.Normal;
  506. this.Capture = false;
  507. Refresh ();
  508. }
  509. if (thumb_pressed == true) {
  510. int pixel_pos;
  511. if (vert) {
  512. int mouse_click = e.Y;
  513. int outside_curpos = thumb_area.Y + thumb_area.Height - thumb_size + lastclick_pos_thumb;
  514. if (mouse_click > thumb_area.Y + thumb_area.Height) {
  515. outside_thumbarea_right = true;
  516. mouse_click = thumb_area.Y + thumb_area.Height;
  517. }
  518. if (mouse_click < thumb_area.Y) {
  519. outside_thumbarea_left = true;
  520. mouse_click = thumb_area.Y;
  521. }
  522. if (outside_thumbarea_right && mouse_click < outside_curpos) {
  523. outside_thumbarea_right = false;
  524. thumb_pixel_click_move_prev =
  525. thumb_pixel_click_move = outside_curpos;
  526. }
  527. if (outside_thumbarea_left && mouse_click > thumb_area.Y + lastclick_pos_thumb) {
  528. outside_thumbarea_left = false;
  529. thumb_pixel_click_move_prev =
  530. thumb_pixel_click_move = thumb_area.Y + lastclick_pos_thumb;
  531. }
  532. if (outside_thumbarea_right == false && outside_thumbarea_left == false) {
  533. pixel_pos = thumb_pos.Y + (thumb_pixel_click_move - thumb_pixel_click_move_prev);
  534. thumb_pixel_click_move_prev = thumb_pixel_click_move;
  535. thumb_pixel_click_move = mouse_click;
  536. UpdateThumbPos (pixel_pos, true);
  537. }
  538. }
  539. else {
  540. int mouse_click = e.X;
  541. int outside_curpos = thumb_area.X + thumb_area.Width - thumb_size + lastclick_pos_thumb;
  542. if (mouse_click > thumb_area.X + thumb_area.Width) {
  543. outside_thumbarea_right = true;
  544. mouse_click = thumb_area.X + thumb_area.Width;
  545. }
  546. if (mouse_click < thumb_area.X) {
  547. outside_thumbarea_left = true;
  548. mouse_click = thumb_area.X;
  549. }
  550. if (outside_thumbarea_right && mouse_click < outside_curpos) {
  551. outside_thumbarea_right = false;
  552. thumb_pixel_click_move_prev =
  553. thumb_pixel_click_move = outside_curpos;
  554. }
  555. if (outside_thumbarea_left && mouse_click > thumb_area.X + lastclick_pos_thumb) {
  556. outside_thumbarea_left = false;
  557. thumb_pixel_click_move_prev =
  558. thumb_pixel_click_move = thumb_area.X + lastclick_pos_thumb;
  559. }
  560. if (outside_thumbarea_right == false && outside_thumbarea_left == false) {
  561. pixel_pos = thumb_pos.X + (thumb_pixel_click_move - thumb_pixel_click_move_prev);
  562. thumb_pixel_click_move_prev = thumb_pixel_click_move;
  563. thumb_pixel_click_move = mouse_click;
  564. UpdateThumbPos (pixel_pos, true);
  565. }
  566. }
  567. Refresh ();
  568. }
  569. }
  570. private void OnMouseDownSB (object sender, MouseEventArgs e)
  571. {
  572. if (firstbutton_state != ButtonState.Inactive && first_arrow_area.Contains (e.X, e.Y)) {
  573. this.Capture = true;
  574. firstbutton_state = ButtonState.Pushed;
  575. Refresh ();
  576. }
  577. if (secondbutton_state != ButtonState.Inactive && second_arrow_area.Contains (e.X, e.Y)) {
  578. this.Capture = true;
  579. secondbutton_state = ButtonState.Pushed;
  580. Refresh ();
  581. }
  582. if (thumb_pos.Contains (e.X, e.Y)) {
  583. thumb_pressed = true;
  584. this.Capture = true;
  585. Refresh ();
  586. if (vert) {
  587. lastclick_pos_thumb = e.Y - thumb_pos.Y;
  588. lastclick_pos = e.Y;
  589. thumb_pixel_click_move_prev = thumb_pixel_click_move = e.Y;
  590. }
  591. else {
  592. lastclick_pos_thumb = e.X - thumb_pos.X;
  593. lastclick_pos = e.X;
  594. thumb_pixel_click_move_prev = thumb_pixel_click_move = e.X;
  595. }
  596. }
  597. else {
  598. if (thumb_area.Contains (e.X, e.Y)) {
  599. if (vert) {
  600. lastclick_pos_thumb = e.Y - thumb_pos.Y;
  601. lastclick_pos = e.Y;
  602. if (e.Y > thumb_pos.Y + thumb_pos.Height) {
  603. LargeIncrement ();
  604. thumb_moving = ThumbMoving.Forward;
  605. }
  606. else {
  607. LargeDecrement ();
  608. thumb_moving = ThumbMoving.Backwards;
  609. }
  610. }
  611. else {
  612. lastclick_pos_thumb = e.X - thumb_pos.X;
  613. lastclick_pos = e.X;
  614. if (e.X > thumb_pos.X + thumb_pos.Width) {
  615. thumb_moving = ThumbMoving.Forward;
  616. LargeIncrement ();
  617. }
  618. else {
  619. thumb_moving = ThumbMoving.Backwards;
  620. LargeDecrement ();
  621. }
  622. }
  623. SetHoldThumbAreaTimer ();
  624. timer.Enabled = true;
  625. Refresh ();
  626. }
  627. }
  628. /* If arrows are pressed, fire timer for auto-repeat */
  629. if ((((firstbutton_state & ButtonState.Pushed) == ButtonState.Pushed)
  630. || ((secondbutton_state & ButtonState.Pushed) == ButtonState.Pushed)) &&
  631. timer.Enabled == false) {
  632. SetHoldButtonClickTimer ();
  633. timer.Enabled = true;
  634. }
  635. }
  636. private void OnMouseUpSB (object sender, MouseEventArgs e)
  637. {
  638. timer.Enabled = false;
  639. if (thumb_moving != ThumbMoving.None) {
  640. thumb_moving = ThumbMoving.None;
  641. Refresh ();
  642. }
  643. if (firstbutton_state != ButtonState.Inactive && first_arrow_area.Contains (e.X, e.Y)) {
  644. firstbutton_state = ButtonState.Normal;
  645. SmallDecrement ();
  646. }
  647. if (secondbutton_state != ButtonState.Inactive && second_arrow_area.Contains (e.X, e.Y)) {
  648. secondbutton_state = ButtonState.Normal;
  649. SmallIncrement ();
  650. }
  651. if (thumb_pressed == true) {
  652. OnScroll (new ScrollEventArgs (ScrollEventType.ThumbPosition, position));
  653. OnScroll (new ScrollEventArgs (ScrollEventType.EndScroll, position));
  654. this.Capture = false;
  655. thumb_pressed = false;
  656. Refresh ();
  657. }
  658. }
  659. private void OnKeyDownSB (Object o, KeyEventArgs key)
  660. {
  661. switch (key.KeyCode){
  662. case Keys.Up:
  663. {
  664. SmallDecrement ();
  665. break;
  666. }
  667. case Keys.Down:
  668. {
  669. SmallIncrement ();
  670. break;
  671. }
  672. case Keys.PageUp:
  673. {
  674. LargeDecrement ();
  675. break;
  676. }
  677. case Keys.PageDown:
  678. {
  679. LargeIncrement ();
  680. break;
  681. }
  682. default:
  683. break;
  684. }
  685. }
  686. private void SmallIncrement ()
  687. {
  688. UpdatePos (position + small_change, true);
  689. Refresh ();
  690. OnScroll (new ScrollEventArgs (ScrollEventType.SmallIncrement, position));
  691. OnScroll (new ScrollEventArgs (ScrollEventType.EndScroll, position));
  692. }
  693. private void SmallDecrement ()
  694. {
  695. UpdatePos (position - small_change, true);
  696. Refresh ();
  697. OnScroll (new ScrollEventArgs (ScrollEventType.SmallDecrement, position));
  698. OnScroll (new ScrollEventArgs (ScrollEventType.EndScroll, position));
  699. }
  700. private void SetHoldButtonClickTimer ()
  701. {
  702. timer.Enabled = false;
  703. timer.Interval = 200;
  704. timer_type = TimerType.HoldButton;
  705. timer.Enabled = true;
  706. }
  707. private void SetRepeatButtonTimer ()
  708. {
  709. timer.Enabled = false;
  710. timer.Interval = 50;
  711. timer_type = TimerType.RepeatButton;
  712. timer.Enabled = true;
  713. }
  714. private void SetHoldThumbAreaTimer ()
  715. {
  716. timer.Enabled = false;
  717. timer.Interval = 200;
  718. timer_type = TimerType.HoldThumbArea;
  719. timer.Enabled = true;
  720. }
  721. private void SetRepeatThumbAreaTimer ()
  722. {
  723. timer.Enabled = false;
  724. timer.Interval = 50;
  725. timer_type = TimerType.RepeatThumbArea;
  726. timer.Enabled = true;
  727. }
  728. private void UpdatePos (int newPos, bool update_thumbpos)
  729. {
  730. int old = position;
  731. int pos;
  732. if (newPos < minimum)
  733. pos = minimum;
  734. else
  735. if (newPos > maximum + 1 - large_change)
  736. pos = maximum + 1 - large_change;
  737. else
  738. pos = newPos;
  739. if (update_thumbpos) {
  740. if (vert)
  741. UpdateThumbPos (thumb_area.Y + (int)(((float)(pos - minimum)) * pixel_per_pos), false);
  742. else
  743. UpdateThumbPos (thumb_area.X + (int)(((float)(pos - minimum)) * pixel_per_pos), false);
  744. Value = pos;
  745. }
  746. else {
  747. position = pos; // Updates directly the value to avoid thumb pos update
  748. if (ValueChanged != null)
  749. ValueChanged (this, EventArgs.Empty);
  750. }
  751. if (pos != old) // Fire event
  752. OnScroll (new ScrollEventArgs (ScrollEventType.ThumbTrack, pos));
  753. }
  754. private void UpdateThumbPos (int pixel, bool update_value)
  755. {
  756. float new_pos = 0;
  757. if (vert) {
  758. if (pixel < thumb_area.Y)
  759. thumb_pos.Y = thumb_area.Y;
  760. else
  761. if (pixel > thumb_area.Y + thumb_area.Height - thumb_size)
  762. thumb_pos.Y = thumb_area.Y + thumb_area.Height - thumb_size;
  763. else
  764. thumb_pos.Y = pixel;
  765. thumb_pos.X = 0;
  766. thumb_pos.Width = ThemeEngine.Current.ScrollBarButtonSize;
  767. thumb_pos.Height = thumb_size;
  768. new_pos = (float) (thumb_pos.Y - thumb_area.Y);
  769. new_pos = new_pos / pixel_per_pos;
  770. } else {
  771. if (pixel < thumb_area.X)
  772. thumb_pos.X = thumb_area.X;
  773. else
  774. if (pixel > thumb_area.X + thumb_area.Width - thumb_size)
  775. thumb_pos.X = thumb_area.X + thumb_area.Width - thumb_size;
  776. else
  777. thumb_pos.X = pixel;
  778. thumb_pos.Y = 0;
  779. thumb_pos.Width = thumb_size;
  780. thumb_pos.Height = ThemeEngine.Current.ScrollBarButtonSize;
  781. new_pos = (float) (thumb_pos.X - thumb_area.X);
  782. new_pos = new_pos / pixel_per_pos;
  783. }
  784. if (update_value)
  785. UpdatePos ((int) new_pos + minimum, false);
  786. }
  787. #endregion //Private Methods
  788. }
  789. }