ScrollBar.cs 26 KB

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