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