ScrollBar.cs 18 KB

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