imported everything from my branch (which is slightly harmless).
[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-2005, Novell, Inc.
24 //
25 // Authors:
26 //      Jordi Mas i Hernandez   jordi@ximian.com
27 //
28 //
29
30 // COMPLETE
31
32 using System.Drawing;
33 using System.Drawing.Imaging;
34 using System.Drawing.Drawing2D;
35 using System.ComponentModel;
36 using System.Runtime.InteropServices;
37
38 namespace System.Windows.Forms
39 {
40         [DefaultEvent ("Scroll")]
41         [DefaultProperty ("Value")]
42         public abstract class ScrollBar : Control
43         {
44                 #region Local Variables
45                 private int position;
46                 private int minimum;
47                 private int maximum;
48                 private int large_change;
49                 private int small_change;
50                 internal int scrollbutton_height;
51                 internal int scrollbutton_width;                
52                 private ScrollBars type;
53                 private Rectangle first_arrow_area = new Rectangle ();          // up or left
54                 private Rectangle second_arrow_area = new Rectangle ();         // down or right
55                 private Rectangle thumb_pos = new Rectangle ();
56                 private Rectangle thumb_area = new Rectangle ();
57                 internal ButtonState firstbutton_state = ButtonState.Normal;
58                 internal ButtonState secondbutton_state = ButtonState.Normal;
59                 private bool firstbutton_pressed = false;
60                 private bool secondbutton_pressed = false;
61                 private bool thumb_pressed = false;
62                 private float pixel_per_pos = 0;
63                 private Timer timer = new Timer ();
64                 private TimerType timer_type;
65                 private int thumb_pixel_click_move;
66                 private int thumb_pixel_click_move_prev;
67                 private int thumb_size = 40;
68                 private const int thumb_min_size = 8;
69                 private const int thumb_notshown_size = 40;
70                 internal bool vert;
71                 private int lastclick_pos;      // Position of the last button-down event
72                 private int lastclick_pos_thumb;      // Position of the last button-down event relative to the thumb           
73                 private bool outside_thumbarea_right = false;
74                 private bool outside_thumbarea_left = false;
75
76                 private Rectangle dirty;
77
78                 internal ThumbMoving thumb_moving = ThumbMoving.None;
79                 #endregion      // Local Variables
80
81                 private enum TimerType
82                 {
83                         HoldButton,
84                         RepeatButton,
85                         HoldThumbArea,
86                         RepeatThumbArea
87                 }
88                 
89                 internal enum ThumbMoving
90                 {
91                         None,
92                         Forward,
93                         Backwards,
94                 }
95
96                 #region events
97                 
98                 [Browsable (false)]
99                 [EditorBrowsable (EditorBrowsableState.Never)]
100                 public new event EventHandler BackColorChanged;
101                 
102                 [Browsable (false)]
103                 [EditorBrowsable (EditorBrowsableState.Never)]
104                 public new event EventHandler BackgroundImageChanged;
105                 
106                 [Browsable (false)]
107                 [EditorBrowsable (EditorBrowsableState.Never)]
108                 public new event EventHandler Click;
109                 
110                 [Browsable (false)]
111                 [EditorBrowsable (EditorBrowsableState.Never)]
112                 public new event EventHandler DoubleClick;
113                 
114                 [Browsable (false)]
115                 [EditorBrowsable (EditorBrowsableState.Never)]
116                 public new event EventHandler FontChanged;
117                 
118                 [Browsable (false)]
119                 [EditorBrowsable (EditorBrowsableState.Never)]
120                 public new event EventHandler ForeColorChanged;
121                 
122                 [Browsable (false)]
123                 [EditorBrowsable (EditorBrowsableState.Never)]
124                 public new event EventHandler ImeModeChanged;
125                 
126                 [Browsable (false)]
127                 [EditorBrowsable (EditorBrowsableState.Never)]
128                 public new event MouseEventHandler MouseDown;
129                 
130                 [Browsable (false)]
131                 [EditorBrowsable (EditorBrowsableState.Never)]
132                 public new event MouseEventHandler MouseMove;
133                 
134                 [Browsable (false)]
135                 [EditorBrowsable (EditorBrowsableState.Never)]
136                 public new event MouseEventHandler MouseUp;
137                 
138                 [Browsable (false)]
139                 [EditorBrowsable (EditorBrowsableState.Never)]
140                 public new event PaintEventHandler Paint;
141                 
142                 public event ScrollEventHandler Scroll;
143                 
144                 [Browsable (false)]
145                 [EditorBrowsable (EditorBrowsableState.Never)]
146                 public new event EventHandler TextChanged;
147                 
148                 public event EventHandler ValueChanged;
149                 #endregion Events
150
151                 public ScrollBar ()
152                 {
153                         position = 0;
154                         minimum = 0;
155                         maximum = 100;
156                         large_change = 10;
157                         small_change = 1;
158
159                         timer.Tick += new EventHandler (OnTimer);
160                         base.KeyDown += new KeyEventHandler (OnKeyDownSB);
161                         base.MouseDown += new MouseEventHandler (OnMouseDownSB);
162                         base.MouseUp += new MouseEventHandler (OnMouseUpSB);
163                         base.MouseMove += new MouseEventHandler (OnMouseMoveSB);
164                         base.Resize += new EventHandler (OnResizeSB);
165                         base.TabStop = false;
166                         
167                         SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
168                         SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
169                 }
170
171                 #region Internal & Private Properties
172                 internal Rectangle FirstArrowArea {
173                         get {
174                                 return this.first_arrow_area;
175                         }
176
177                         set {
178                                 this.first_arrow_area = value;
179                         }
180                 }
181
182                 internal Rectangle SecondArrowArea {
183                         get {
184                                 return this.second_arrow_area;
185                         }
186
187                         set {
188                                 this.second_arrow_area = value;
189                         }
190                 }
191
192                 internal Rectangle ThumbPos {
193                         get {
194                                 return thumb_pos;
195                         }
196
197                         set {
198                                 thumb_pos = value;
199                         }
200                 }
201                 #endregion      // Internal & Private Properties
202
203                 #region Public Properties
204
205                 [EditorBrowsable (EditorBrowsableState.Never)]
206                 [Browsable (false)]
207                 public override Color BackColor
208                 {
209                         get { return base.BackColor; }
210                         set {
211                                 if (base.BackColor == value)
212                                         return;
213
214                                 if (BackColorChanged != null)
215                                         BackColorChanged (this, EventArgs.Empty);
216
217                                 base.BackColor = value;
218                                 Refresh ();
219                         }
220                 }
221
222                 [EditorBrowsable (EditorBrowsableState.Never)]
223                 [Browsable (false)]
224                 public override Image BackgroundImage
225                 {
226                         get { return base.BackgroundImage; }
227                         set {
228                                 if (base.BackgroundImage == value)
229                                         return;
230
231                                 if (BackgroundImageChanged != null)
232                                         BackgroundImageChanged (this, EventArgs.Empty);
233
234                                 base.BackgroundImage = value;
235                         }
236                 }
237
238                 protected override CreateParams CreateParams
239                 {
240                         get {   return base.CreateParams; }
241                 }
242
243                 protected override ImeMode DefaultImeMode
244                 {
245                         get { return ImeMode.Disable; }
246                 }
247
248                 [EditorBrowsable (EditorBrowsableState.Never)]
249                 [Browsable (false)]
250                 public override Font Font
251                 {
252                         get { return base.Font; }
253                         set {
254                                 if (base.Font.Equals (value))
255                                         return;
256
257                                 if (FontChanged != null)
258                                         FontChanged (this, EventArgs.Empty);
259
260                                 base.Font = value;
261                         }
262                 }
263
264                 [EditorBrowsable (EditorBrowsableState.Never)]
265                 [Browsable (false)]
266                 public override Color ForeColor
267                 {
268                         get { return base.ForeColor; }
269                         set {
270                                 if (base.ForeColor == value)
271                                         return;
272
273                                 if (ForeColorChanged != null)
274                                         ForeColorChanged (this, EventArgs.Empty);
275
276                                 base.ForeColor = value;
277                                 Refresh ();
278                         }
279                 }
280
281                 [EditorBrowsable (EditorBrowsableState.Never)]
282                 [Browsable (false)]
283                 public new ImeMode ImeMode
284                 {
285                         get { return base.ImeMode; }
286                         set {
287                                 if (base.ImeMode == value)
288                                         return;
289
290                                 if (ImeModeChanged != null)
291                                         ImeModeChanged (this, EventArgs.Empty);
292
293                                 base.ImeMode = value;
294                         }
295                 }
296
297                 [DefaultValue (10)]
298                 [RefreshProperties(RefreshProperties.Repaint)]
299                 [MWFDescription("Scroll amount when clicking in the scroll area"), MWFCategory("Behaviour")]
300                 public int LargeChange {
301                         get {
302                                 if (large_change > maximum)
303                                         return (maximum + 1);
304                                 else
305                                         return large_change;
306                         }
307                         set {
308                                 if (value < 0)
309                                         throw new Exception( string.Format("Value '{0}' must be greater than or equal to 0.", value));
310
311                                 if (large_change != value) {
312                                         large_change = value;
313
314                                         // thumb area depends on large change value,
315                                         // so we need to recalculate it.
316                                         CalcThumbArea ();
317                                         UpdatePos (Value, true);
318                                         Refresh ();
319                                 }
320                         }
321                 }
322
323                 [DefaultValue (100)]
324                 [RefreshProperties(RefreshProperties.Repaint)]
325                 [MWFDescription("Highest value for scrollbar"), MWFCategory("Behaviour")]
326                 public int Maximum {
327                         get { return maximum; }
328                         set {
329                                 if (maximum == value)
330                                         return;
331                                         
332                                 maximum = value;
333
334                                 if (maximum < minimum)
335                                         minimum = maximum;
336
337                                 // thumb area depends on maximum value,
338                                 // so we need to recalculate it.
339                                 CalcThumbArea ();
340                                 UpdatePos (Value, true);
341                                 Refresh ();
342                         }
343                 }
344
345                 [DefaultValue (0)]
346                 [RefreshProperties(RefreshProperties.Repaint)]
347                 [MWFDescription("Smallest value for scrollbar"), MWFCategory("Behaviour")]
348                 public int Minimum {
349                         get { return minimum; }
350                         set {
351                                 if (minimum == value)
352                                         return;
353                                         
354                                 minimum = value;
355
356                                 if (minimum > maximum)
357                                         maximum = minimum;
358
359                                 // thumb area depends on minimum value,
360                                 // so we need to recalculate it.
361                                 CalcThumbArea ();
362                                 UpdatePos (Value, true);
363                                 Refresh ();
364                         }
365                 }
366
367                 [DefaultValue (1)]
368                 [MWFDescription("Scroll amount when clicking scroll arrows"), MWFCategory("Behaviour")]
369                 public int SmallChange {
370                         get { return small_change; }
371                         set {
372                                 if ( value < 0 )
373                                         throw new Exception( string.Format("Value '{0}' must be greater than or equal to 0.", value));
374
375                                 if (small_change != value) {
376                                         small_change = value;
377                                         UpdatePos (Value, true);
378                                         Refresh ();
379                                 }
380                         }
381                 }
382
383                 [DefaultValue (false)]
384                 public new bool TabStop {
385                         get { return base.TabStop; }
386                         set { base.TabStop = value; }
387                 }
388
389                 [EditorBrowsable (EditorBrowsableState.Never)]
390                 [Bindable (false)]
391                 [Browsable (false)]
392                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
393                 public override string Text {
394                          get { return base.Text;  }
395                          set { base.Text = value; }
396                 }
397
398                 [Bindable(true)]
399                 [DefaultValue (0)]
400                 [MWFDescription("Current value for scrollbar"), MWFCategory("Behaviour")]
401                 public int Value {
402                         get { return position; }
403                         set {
404                                 if ( value < minimum || value > maximum )
405                                         throw new ArgumentException(
406                                                 string.Format("'{0}' is not a valid value for 'Value'. 'Value' should be between 'Minimum' and 'Maximum'", value));
407
408                                 if (position != value){
409                                         position = value;
410
411                                         OnValueChanged (EventArgs.Empty);
412
413                                         ClearDirty ();
414                                         UpdatePos (Value, true);
415                                         InvalidateDirty ();
416                                 }
417                         }
418                 }
419
420                 #endregion //Public Properties
421
422                 #region Public Methods
423                 
424                 protected override void OnEnabledChanged (EventArgs e)
425                 {
426                         base.OnEnabledChanged (e);
427
428                         if (Enabled)
429                                 firstbutton_state = secondbutton_state = ButtonState.Normal;
430                         else
431                                 firstbutton_state = secondbutton_state = ButtonState.Inactive;
432
433                         Refresh ();
434                 }
435                 
436                 protected override void OnHandleCreated (System.EventArgs e)
437                 {
438                         base.OnHandleCreated (e);               
439
440                         CalcButtonSizes ();
441                         CalcThumbArea ();
442                         UpdatePos (Value, true);
443                 }
444
445                 protected virtual void OnScroll (ScrollEventArgs event_args)
446                 {
447                         if (Scroll == null)
448                                 return;
449                                 
450                         if (event_args.NewValue < Minimum) {
451                                 event_args.NewValue = Minimum;
452                         }
453                         
454                         if (event_args.NewValue > Maximum) {
455                                 event_args.NewValue = Maximum;
456                         }
457
458                         Scroll (this, event_args);
459                 }
460
461                 protected virtual void OnValueChanged (EventArgs e)
462                 {
463                         if (ValueChanged != null)
464                                 ValueChanged (this, e);
465                 }
466
467                 public override string ToString()
468                 {
469                         return string.Format("{0}, Minimum: {1}, Maximum: {2}, Value: {3}",
470                                                 GetType( ).FullName.ToString( ), minimum, maximum, position);
471                 }
472
473                 protected void UpdateScrollInfo ()
474                 {
475                         Refresh ();
476                 }
477
478                 protected override void WndProc (ref Message m)
479                 {
480                         switch ((Msg) m.Msg)
481                         {
482                                 case Msg.WM_PAINT:
483                                 {
484                                         PaintEventArgs  paint_event;
485
486                                         paint_event = XplatUI.PaintEventStart (Handle);
487                                         OnPaintSB (paint_event);
488                                         XplatUI.PaintEventEnd (Handle);
489                                         return;
490                                 }
491
492                                 case Msg.WM_ERASEBKGND:
493                                         m.Result = (IntPtr) 1; /// Disable background painting to avoid flickering
494                                         return;
495
496                                 default:
497                                         break;
498                         }
499
500                         base.WndProc (ref m);
501                 }
502
503                 #endregion //Public Methods
504
505                 #region Private Methods
506                 
507                 private void CalcButtonSizes ()
508                 {               
509                         if (vert) {
510                                 if (Height < ThemeEngine.Current.ScrollBarButtonSize * 2)
511                                         scrollbutton_height = Height /2;
512                                 else
513                                         scrollbutton_height = ThemeEngine.Current.ScrollBarButtonSize;
514                                 
515                         } else {
516                                 if (Width < ThemeEngine.Current.ScrollBarButtonSize * 2)
517                                         scrollbutton_width = Width /2;
518                                 else
519                                         scrollbutton_width = ThemeEngine.Current.ScrollBarButtonSize;
520                         }
521                 }
522                                 
523                 private void CalcThumbArea ()
524                 {
525                         // Thumb area
526                         if (vert) {
527
528                                 thumb_area.Height = Height - scrollbutton_height -  scrollbutton_height;
529                                 thumb_area.X = 0;
530                                 thumb_area.Y = scrollbutton_height;
531                                 thumb_area.Width = Width;
532
533                                 if (Height < thumb_notshown_size)
534                                         thumb_size = 0;
535                                 else {
536                                         double per =  ((double) this.LargeChange / (double)((1 + maximum - minimum)));
537                                         thumb_size = 1 + (int) (thumb_area.Height * per);                                       
538                                         
539                                         if (thumb_size < thumb_min_size)
540                                                 thumb_size = thumb_min_size;
541                                 }                               
542
543                                 pixel_per_pos = ((float)(thumb_area.Height - thumb_size) / (float) ((maximum - minimum - this.LargeChange) + 1));
544
545                         } else  {
546
547                                 thumb_area.Y = 0;
548                                 thumb_area.X = scrollbutton_width;
549                                 thumb_area.Height = Height;
550                                 thumb_area.Width = Width - scrollbutton_width -  scrollbutton_width;    
551                                 
552                                 if (Width < thumb_notshown_size)
553                                         thumb_size = 0;
554                                 else {
555                                         double per =  ((double) this.LargeChange / (double)((1 + maximum - minimum)));
556                                         thumb_size = 1 + (int) (thumb_area.Width * per);
557                                         
558                                         if (thumb_size < thumb_min_size)
559                                                 thumb_size = thumb_min_size;
560                                 }
561                                 
562                                 pixel_per_pos = ((float)(thumb_area.Width - thumb_size) / (float) ((maximum - minimum - this.LargeChange) + 1));
563                         }
564                 }
565
566                 private void LargeIncrement ()
567                 {                       
568                         ScrollEventArgs event_args;
569                         int pos = position + large_change;
570                         
571                         event_args = new ScrollEventArgs (ScrollEventType.LargeIncrement, pos);
572                         OnScroll (event_args);                          
573                         pos = event_args.NewValue;                      
574                         
575                         event_args = new ScrollEventArgs (ScrollEventType.EndScroll, pos);
576                         OnScroll (event_args);          
577                         pos = event_args.NewValue;                      
578
579                         UpdatePos (pos, true);
580                 }
581
582                 private void LargeDecrement ()
583                 {                       
584                         ScrollEventArgs event_args;
585                         int pos = position - large_change;
586                         
587                         event_args = new ScrollEventArgs (ScrollEventType.LargeDecrement, pos);
588                         OnScroll (event_args);
589                         pos = event_args.NewValue;                      
590                         
591                         event_args = new ScrollEventArgs (ScrollEventType.EndScroll, pos);
592                         OnScroll (event_args);
593                         pos = event_args.NewValue;
594                         
595
596                         UpdatePos (pos, true);
597                 }               
598                 
599                 private void OnResizeSB (Object o, EventArgs e)
600                 {                       
601                         if (Width <= 0 || Height <= 0)
602                                 return;
603                         
604                         CalcButtonSizes ();
605                         CalcThumbArea ();
606                         UpdatePos (position, true);
607                 }
608
609                 private void OnPaintSB (PaintEventArgs pevent)
610                 {
611                         if (Paint != null) {
612                                 Paint (this, pevent);
613                         }
614                         ThemeEngine.Current.DrawScrollBar (pevent.Graphics, pevent.ClipRectangle, this);
615                 }
616
617                 private void OnTimer (Object source, EventArgs e)
618                 {
619                         ClearDirty ();
620
621                         switch (timer_type) {
622
623                         case TimerType.HoldButton:
624                                 SetRepeatButtonTimer ();
625                                 break;
626
627                         case TimerType.RepeatButton:
628                         {
629                                 if ((firstbutton_state & ButtonState.Pushed) == ButtonState.Pushed)
630                                         SmallDecrement();
631
632                                 if ((secondbutton_state & ButtonState.Pushed) == ButtonState.Pushed)
633                                         SmallIncrement();
634
635                                 break;
636                         }
637
638                         case TimerType.HoldThumbArea:
639                                 SetRepeatThumbAreaTimer ();
640                                 break;
641
642                         case TimerType.RepeatThumbArea:
643                         {
644                                 Point pnt, pnt_screen;
645                                 Rectangle thumb_area_screen = thumb_area;
646
647                                 pnt_screen = PointToScreen (new Point (thumb_area.X, thumb_area.Y));
648                                 thumb_area_screen.X = pnt_screen.X;
649                                 thumb_area_screen.Y = pnt_screen.Y;
650                                 
651                                 if (thumb_area_screen.Contains (MousePosition) == false) {                                      
652                                         timer.Enabled = false;
653                                         thumb_moving = ThumbMoving.None;
654                                         DirtyThumbArea ();
655                                         InvalidateDirty ();
656                                 }                               
657                                 
658                                 pnt = PointToClient (MousePosition);
659                                 
660                                 if (vert)
661                                         lastclick_pos = pnt.Y;
662                                 else
663                                         lastclick_pos = pnt.X;
664
665                                 if (thumb_moving == ThumbMoving.Forward) {
666                                         if ((vert && (thumb_pos.Y + thumb_size > lastclick_pos)) ||
667                                                 (!vert && (thumb_pos.X + thumb_size > lastclick_pos)) ||
668                                                 (thumb_area.Contains (pnt) == false)){
669                                                 timer.Enabled = false;                                          
670                                                 thumb_moving = ThumbMoving.None;
671                                                 Refresh ();                     
672                                                 return;
673                                         } else {
674                                                 LargeIncrement ();
675                                 }
676                                 }
677                                 else
678                                         if ((vert && (thumb_pos.Y < lastclick_pos)) ||
679                                                 (!vert && (thumb_pos.X  < lastclick_pos))){
680                                                 timer.Enabled = false;
681                                                 thumb_moving = ThumbMoving.None;
682                                                 Refresh ();                                             
683                                         } else {
684                                                 LargeDecrement ();
685                                         }
686
687                                 break;
688                         }
689                         default:
690                                 break;
691                         }
692
693                         InvalidateDirty ();
694                 }               
695
696                 private void OnMouseMoveSB (object sender, MouseEventArgs e)
697                 {
698                         if (MouseMove != null) {
699                                 MouseMove (this, e);
700                         }
701                                 
702                         if (Enabled == false || thumb_size == 0)
703                                 return;
704
705                         if (firstbutton_pressed) {
706                                 if (!first_arrow_area.Contains (e.X, e.Y) && ((firstbutton_state & ButtonState.Pushed) == ButtonState.Pushed)) {
707                                         firstbutton_state = ButtonState.Normal;
708                                         Invalidate (first_arrow_area);
709                                         return;
710                                 } else if (first_arrow_area.Contains (e.X, e.Y) && ((firstbutton_state & ButtonState.Normal) == ButtonState.Normal)) {
711                                         firstbutton_state = ButtonState.Pushed;
712                                         Invalidate (first_arrow_area);
713                                         return;
714                                 }
715                         } else if (secondbutton_pressed) {
716                                 if (!second_arrow_area.Contains (e.X, e.Y) && ((secondbutton_state & ButtonState.Pushed) == ButtonState.Pushed)) {
717                                         secondbutton_state = ButtonState.Normal;
718                                         Invalidate (second_arrow_area);
719                                         return;
720                                 } else if (second_arrow_area.Contains (e.X, e.Y) && ((secondbutton_state & ButtonState.Normal) == ButtonState.Normal)) {
721                                         secondbutton_state = ButtonState.Pushed;
722                                         Invalidate (second_arrow_area);
723                                         return;
724                                 }
725                         } else if (thumb_pressed == true) {
726                                 int pixel_pos;
727
728                                 if (vert) {
729
730                                         int mouse_click = e.Y;
731                                         int outside_curpos = thumb_area.Y + thumb_area.Height - thumb_size + lastclick_pos_thumb;
732                                         
733                                         
734                                         if (mouse_click > thumb_area.Y + thumb_area.Height) {
735                                                 outside_thumbarea_right = true;
736                                                 mouse_click = thumb_area.Y + thumb_area.Height;
737                                         }
738
739                                         if (mouse_click < thumb_area.Y) {
740                                                 outside_thumbarea_left = true;
741                                                 mouse_click = thumb_area.Y;
742                                         }
743
744                                         if (outside_thumbarea_right && mouse_click < outside_curpos) {
745                                                 outside_thumbarea_right = false;
746                                                 thumb_pixel_click_move_prev =
747                                                 thumb_pixel_click_move = outside_curpos;
748                                         }
749
750                                         if (outside_thumbarea_left && mouse_click > thumb_area.Y + lastclick_pos_thumb) {
751                                                 outside_thumbarea_left = false;
752                                                 thumb_pixel_click_move_prev =
753                                                 thumb_pixel_click_move = thumb_area.Y + lastclick_pos_thumb;
754                                         }
755
756                                         if (outside_thumbarea_right == false && outside_thumbarea_left == false) {
757                                                 pixel_pos = thumb_pos.Y + (thumb_pixel_click_move - thumb_pixel_click_move_prev);
758                                                 thumb_pixel_click_move_prev = thumb_pixel_click_move;
759                                                 thumb_pixel_click_move = mouse_click;
760                                                 
761                                                 UpdateThumbPos (pixel_pos, true);
762                                                 OnScroll (new ScrollEventArgs (ScrollEventType.ThumbTrack, position));
763                                         }
764
765                                 }
766                                 else {
767                                         int mouse_click = e.X;
768                                         int outside_curpos = thumb_area.X + thumb_area.Width - thumb_size + lastclick_pos_thumb;
769                                                                                 
770                                         if (mouse_click >  thumb_area.X + thumb_area.Width) {
771                                                 outside_thumbarea_right = true;
772                                                 mouse_click = thumb_area.X + thumb_area.Width;
773                                         }
774
775                                         if (mouse_click <  thumb_area.X) {
776                                                 outside_thumbarea_left = true;
777                                                 mouse_click = thumb_area.X;
778                                         }
779
780                                         if (outside_thumbarea_right && mouse_click < outside_curpos) {
781                                                 outside_thumbarea_right = false;
782                                                 thumb_pixel_click_move_prev =
783                                                 thumb_pixel_click_move = outside_curpos;
784                                         }
785
786                                         if (outside_thumbarea_left && mouse_click > thumb_area.X + lastclick_pos_thumb) {
787                                                 outside_thumbarea_left = false;
788                                                 thumb_pixel_click_move_prev =
789                                                 thumb_pixel_click_move = thumb_area.X + lastclick_pos_thumb;
790                                         }
791
792                                         if (outside_thumbarea_right == false && outside_thumbarea_left == false) {
793                                                 pixel_pos = thumb_pos.X + (thumb_pixel_click_move - thumb_pixel_click_move_prev);
794                                                 thumb_pixel_click_move_prev = thumb_pixel_click_move;
795                                                 thumb_pixel_click_move = mouse_click;
796                                                 UpdateThumbPos (pixel_pos, true);                                               
797                                                 OnScroll (new ScrollEventArgs (ScrollEventType.ThumbTrack, position));
798                                         }
799
800                                 }
801
802                                 Refresh ();
803                         }
804
805                 }
806
807                 private void OnMouseDownSB (object sender, MouseEventArgs e)
808                 {
809                         ClearDirty ();
810                         
811                         if (e.Button == MouseButtons.Right) {
812                                 if (MouseDown != null) {
813                                         MouseDown (this, e);
814                                 }
815                         }
816                         
817                         if (Enabled == false)
818                                 return;
819
820                         if (firstbutton_state != ButtonState.Inactive && first_arrow_area.Contains (e.X, e.Y)) {
821                                 this.Capture = true;                            
822                                 firstbutton_state = ButtonState.Pushed;
823                                 firstbutton_pressed = true;
824                                 Invalidate (first_arrow_area);
825                                 if (!timer.Enabled) {
826                                         SetHoldButtonClickTimer ();
827                                         timer.Enabled = true;
828                                 }
829                         }
830
831                         if (secondbutton_state != ButtonState.Inactive && second_arrow_area.Contains (e.X, e.Y)) {
832                                 this.Capture = true;                            
833                                 secondbutton_state = ButtonState.Pushed;
834                                 secondbutton_pressed = true;
835                                 Invalidate (second_arrow_area);
836                                 if (!timer.Enabled) {
837                                         SetHoldButtonClickTimer ();
838                                         timer.Enabled = true;
839                                 }
840                         }
841
842                         if (thumb_size > 0 && thumb_pos.Contains (e.X, e.Y)) {
843                                 thumb_pressed = true;
844                                 this.Capture = true;
845                                 if (vert) {
846                                         lastclick_pos_thumb = e.Y - thumb_pos.Y;
847                                         lastclick_pos = e.Y;                                    
848                                         thumb_pixel_click_move_prev = thumb_pixel_click_move = e.Y;
849                                 }
850                                 else {
851                                         lastclick_pos_thumb = e.X - thumb_pos.X;
852                                         lastclick_pos = e.X;
853                                         thumb_pixel_click_move_prev = thumb_pixel_click_move = e.X;
854                                 }
855                         } else {
856                                 if (thumb_size > 0 && thumb_area.Contains (e.X, e.Y)) {
857
858                                         if (vert) {
859                                                 lastclick_pos_thumb = e.Y - thumb_pos.Y;
860                                                 lastclick_pos = e.Y;
861
862                                                 if (e.Y > thumb_pos.Y + thumb_pos.Height) {
863                                                         LargeIncrement ();                                                      
864                                                         thumb_moving = ThumbMoving.Forward;                                                     
865                                                         Dirty (new Rectangle (0, thumb_pos.Y + thumb_pos.Height,
866                                                                                       ClientRectangle.Width,
867                                                                                       ClientRectangle.Height -  (thumb_pos.Y + thumb_pos.Height) -
868                                                                                       scrollbutton_height));
869                                                 } else {
870                                                         LargeDecrement ();                                                      
871                                                         thumb_moving = ThumbMoving.Backwards;
872                                                         Dirty (new Rectangle (0,  scrollbutton_height,
873                                                                                       ClientRectangle.Width,
874                                                                                       thumb_pos.Y - scrollbutton_height));
875                                                 }
876                                         } else {
877
878                                                 lastclick_pos_thumb = e.X - thumb_pos.X;
879                                                 lastclick_pos = e.X;
880
881                                                 if (e.X > thumb_pos.X + thumb_pos.Width) {
882                                                         thumb_moving = ThumbMoving.Forward;
883                                                         LargeIncrement ();                                                      
884                                                         Dirty (new Rectangle (thumb_pos.X + thumb_pos.Width, 0,
885                                                                                       ClientRectangle.Width -  (thumb_pos.X + thumb_pos.Width) -
886                                                                                       scrollbutton_width,
887                                                                                       ClientRectangle.Height));
888                                                 } else {
889                                                         thumb_moving = ThumbMoving.Backwards;
890                                                         LargeDecrement ();                                                      
891                                                         Dirty (new Rectangle (scrollbutton_width,  0,
892                                                                                       thumb_pos.X - scrollbutton_width,
893                                                                                       ClientRectangle.Height));
894                                                 }
895                                         }
896
897                                         SetHoldThumbAreaTimer ();
898                                         timer.Enabled = true;
899                                         InvalidateDirty ();
900                                 }
901                         }
902                 }
903                 
904                 private void OnMouseUpSB (object sender, MouseEventArgs e)
905                 {
906                         ClearDirty ();
907
908                         if (e.Button == MouseButtons.Right) {
909                                 if (MouseUp != null) {
910                                         MouseUp (this, e);
911                                 }
912                         }
913                         
914                         if (Enabled == false)
915                                 return;
916
917                         timer.Enabled = false;
918                         if (thumb_moving != ThumbMoving.None) {
919                                 DirtyThumbArea ();
920                                 thumb_moving = ThumbMoving.None;
921                         }                       
922                         this.Capture = false;
923
924                         if (firstbutton_pressed) {
925                                 firstbutton_state = ButtonState.Normal;
926                                 if (first_arrow_area.Contains (e.X, e.Y)) {
927                                         SmallDecrement ();
928                                 }
929                                 firstbutton_pressed = false;
930                                 Dirty (first_arrow_area);
931                         } else if (secondbutton_pressed) {
932                                 secondbutton_state = ButtonState.Normal;
933                                 if (second_arrow_area.Contains (e.X, e.Y)) {
934                                         SmallIncrement ();
935                                 }
936                                 Dirty (second_arrow_area);
937                                 secondbutton_pressed = false;
938                         } else if (thumb_pressed == true) {
939                                 OnScroll (new ScrollEventArgs (ScrollEventType.ThumbPosition, position));
940                                 OnScroll (new ScrollEventArgs (ScrollEventType.EndScroll, position));
941                                 thumb_pressed = false;
942                                 Refresh ();
943                                 return;
944                         }
945
946                         InvalidateDirty ();
947                 }
948
949                 private void OnKeyDownSB (Object o, KeyEventArgs key)
950                 {
951                         if (Enabled == false)
952                                 return;
953
954                         ClearDirty ();
955
956                         switch (key.KeyCode){
957                         case Keys.Up:
958                         {
959                                 SmallDecrement ();
960                                 break;
961                         }
962                         case Keys.Down:
963                         {
964                                 SmallIncrement ();
965                                 break;
966                         }
967                         case Keys.PageUp:
968                         {
969                                 LargeDecrement ();
970                                 break;
971                         }
972                         case Keys.PageDown:
973                         {
974                                 LargeIncrement ();
975                                 break;
976                         }
977                         case Keys.Home:
978                         {               
979                                 SetHomePosition ();                             
980                                 break;
981                         }                       
982                         case Keys.End:
983                         {       
984                                 SetEndPosition ();
985                                 break;
986                         }
987                         default:
988                                 break;
989                         }
990
991                         InvalidateDirty ();
992                 }               
993                 
994                 private void SetEndPosition () 
995                 {                       
996                         ScrollEventArgs event_args;
997                         int pos = Maximum;
998                         
999                         event_args = new ScrollEventArgs (ScrollEventType.Last, pos);
1000                         OnScroll (event_args);
1001                         pos = event_args.NewValue;                      
1002                         
1003                         event_args = new ScrollEventArgs (ScrollEventType.EndScroll, pos);
1004                         OnScroll (event_args);                  
1005                         pos = event_args.NewValue;                      
1006
1007                         SetValue (pos);
1008                 }
1009                 
1010                 private void SetHomePosition ()
1011                 {
1012                         ScrollEventArgs event_args;
1013                         int pos = Minimum;
1014                         
1015                         event_args = new ScrollEventArgs (ScrollEventType.First, pos);
1016                         OnScroll (event_args);
1017                         pos = event_args.NewValue;
1018                                                 
1019                         event_args = new ScrollEventArgs (ScrollEventType.EndScroll, pos);
1020                         OnScroll (event_args);                  
1021                         pos = event_args.NewValue;                      
1022                         
1023                         SetValue (pos);
1024                 }               
1025
1026                 private void SmallIncrement ()
1027                 {
1028                         ScrollEventArgs event_args;
1029                         int pos = position + small_change;
1030                         
1031                         event_args = new ScrollEventArgs (ScrollEventType.SmallIncrement, pos);
1032                         OnScroll (event_args);                          
1033                         pos = event_args.NewValue;                      
1034                         
1035                         event_args = new ScrollEventArgs (ScrollEventType.EndScroll, pos);
1036                         OnScroll (event_args);                  
1037                         pos = event_args.NewValue;                      
1038
1039                         UpdatePos (pos, true);
1040                 }
1041
1042                 private void SmallDecrement ()
1043                 {                       
1044                         ScrollEventArgs event_args;
1045                         int pos = position - small_change;
1046                         
1047                         event_args = new ScrollEventArgs (ScrollEventType.SmallDecrement, pos);
1048                         OnScroll (event_args);
1049                         pos = event_args.NewValue;
1050                                                 
1051                         event_args = new ScrollEventArgs (ScrollEventType.EndScroll, pos);
1052                         OnScroll (event_args);                  
1053                         pos = event_args.NewValue;                      
1054
1055                         UpdatePos (pos, true);
1056                 }
1057                 
1058                 private void SetHoldButtonClickTimer ()
1059                 {
1060                         timer.Enabled = false;
1061                         timer.Interval = 200;
1062                         timer_type = TimerType.HoldButton;
1063                         timer.Enabled = true;
1064                 }
1065
1066                 private void SetRepeatButtonTimer ()
1067                 {
1068                         timer.Enabled = false;
1069                         timer.Interval = 50;
1070                         timer_type = TimerType.RepeatButton;
1071                         timer.Enabled = true;
1072                 }
1073
1074                 private void SetHoldThumbAreaTimer ()
1075                 {
1076                         timer.Enabled = false;
1077                         timer.Interval = 200;
1078                         timer_type = TimerType.HoldThumbArea;
1079                         timer.Enabled = true;
1080                 }
1081
1082                 private void SetRepeatThumbAreaTimer ()
1083                 {
1084                         timer.Enabled = false;
1085                         timer.Interval = 50;
1086                         timer_type = TimerType.RepeatThumbArea;
1087                         timer.Enabled = true;
1088                 }                               
1089                 
1090                 private void UpdatePos (int newPos, bool update_thumbpos)
1091                 {                       
1092                         int pos;
1093
1094                         if (newPos < minimum)
1095                                 pos = minimum;
1096                         else
1097                                 if (newPos > maximum + 1 - large_change)
1098                                         pos = maximum + 1 - large_change;
1099                                         else
1100                                                 pos = newPos;
1101
1102                         // pos can't be less than minimum
1103                         if (pos < minimum)
1104                                 pos = minimum;
1105
1106                         if (update_thumbpos) {
1107                                 if (vert)
1108                                         UpdateThumbPos (thumb_area.Y + (int)(((float)(pos - minimum)) * pixel_per_pos), false);
1109                                 else
1110                                         UpdateThumbPos (thumb_area.X + (int)(((float)(pos - minimum)) * pixel_per_pos), false);
1111                                 SetValue (pos);
1112                         }
1113                         else {
1114                                 position = pos; // Updates directly the value to avoid thumb pos update
1115                                 
1116                                 if (ValueChanged != null)
1117                                         ValueChanged (this, EventArgs.Empty);
1118                         }                       
1119                 }
1120
1121                 private void UpdateThumbPos (int pixel, bool update_value)
1122                 {
1123                         float new_pos = 0;
1124
1125                         if (vert) {
1126                                 Dirty (thumb_pos);
1127                                 if (pixel < thumb_area.Y)
1128                                         thumb_pos.Y = thumb_area.Y;
1129                                 else
1130                                         if (pixel > thumb_area.Y + thumb_area.Height - thumb_size)
1131                                                 thumb_pos.Y = thumb_area.Y +  thumb_area.Height - thumb_size;
1132                                         else
1133                                                 thumb_pos.Y = pixel;
1134
1135                                 thumb_pos.X = 0;
1136                                 thumb_pos.Width = ThemeEngine.Current.ScrollBarButtonSize;
1137                                 thumb_pos.Height = thumb_size;
1138                                 new_pos = (float) (thumb_pos.Y - thumb_area.Y);
1139                                 new_pos = new_pos / pixel_per_pos;
1140
1141                                 Dirty (thumb_pos);
1142                         } else  {
1143                                 Dirty (thumb_pos);
1144                                 if (pixel < thumb_area.X)
1145                                         thumb_pos.X = thumb_area.X;
1146                                 else
1147                                         if (pixel > thumb_area.X + thumb_area.Width - thumb_size)
1148                                                 thumb_pos.X = thumb_area.X +  thumb_area.Width - thumb_size;
1149                                         else
1150                                                 thumb_pos.X = pixel;
1151
1152                                 thumb_pos.Y = 0;
1153                                 thumb_pos.Width =  thumb_size;
1154                                 thumb_pos.Height = ThemeEngine.Current.ScrollBarButtonSize;
1155                                 new_pos = (float) (thumb_pos.X - thumb_area.X);
1156                                 new_pos = new_pos / pixel_per_pos;
1157
1158                                 Dirty (thumb_pos);
1159                         }
1160
1161                         if (update_value)
1162                                 UpdatePos ((int) new_pos + minimum, false);
1163                 }
1164
1165                 private void SetValue (int value)
1166                 {
1167                         if ( value < minimum || value > maximum )
1168                                 throw new ArgumentException(
1169                                         String.Format("'{0}' is not a valid value for 'Value'. 'Value' should be between 'Minimum' and 'Maximum'", value));
1170
1171                         if (position != value){
1172                                 position = value;
1173
1174                                 OnValueChanged (EventArgs.Empty);
1175                                 UpdatePos (value, true);
1176                         }
1177                 }
1178
1179                 private void ClearDirty ()
1180                 {
1181                         dirty = Rectangle.Empty;
1182                 }
1183
1184                 private void Dirty (Rectangle r)
1185                 {
1186                         if (dirty == Rectangle.Empty) {
1187                                 dirty = r;
1188                                 return;
1189                         }
1190                         dirty = Rectangle.Union (dirty, r);
1191                 }
1192
1193                 private void DirtyThumbArea ()
1194                 {
1195                         if (thumb_moving == ThumbMoving.Forward) {
1196                                 if (vert) {
1197                                         Dirty (new Rectangle (0, thumb_pos.Y + thumb_pos.Height,
1198                                                                       ClientRectangle.Width,
1199                                                                       ClientRectangle.Height -  (thumb_pos.Y + thumb_pos.Height) -
1200                                                                       scrollbutton_height));
1201                                 } else {
1202                                         Dirty (new Rectangle (thumb_pos.X + thumb_pos.Width, 0,
1203                                                                       ClientRectangle.Width -  (thumb_pos.X + thumb_pos.Width) -
1204                                                                       scrollbutton_width,
1205                                                                       ClientRectangle.Height));
1206                                 }
1207                         } else if (thumb_moving == ThumbMoving.Backwards) {
1208                                 if (vert) {
1209                                         Dirty(new Rectangle (0,  scrollbutton_height,
1210                                                                       ClientRectangle.Width,
1211                                                                       thumb_pos.Y - scrollbutton_height));
1212                                 } else {
1213                                         Dirty (new Rectangle (scrollbutton_width,  0,
1214                                                                       thumb_pos.X - scrollbutton_width,
1215                                                                       ClientRectangle.Height));
1216                                 }
1217                         }
1218                 }
1219
1220                 private void InvalidateDirty ()
1221                 {
1222                         Invalidate (dirty);
1223                         dirty = Rectangle.Empty;
1224                 }
1225
1226                 #endregion //Private Methods
1227          }
1228 }
1229
1230