* ScrollBar.cs: Refresh afterbeing resized.
[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                         base.cursor = Cursors.Default;
167                         
168                         SetStyle (ControlStyles.UserPaint | ControlStyles.StandardClick, false);
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                                         
415                                         if (IsHandleCreated) {
416                                                 UpdateThumbPos (thumb_area.Y + (int)(((float)(position - minimum)) * pixel_per_pos), false);
417                                         }
418                                         
419                                         InvalidateDirty ();
420                                 }
421                         }
422                 }
423
424                 #endregion //Public Properties
425
426                 #region Public Methods
427                 
428                 protected override void OnEnabledChanged (EventArgs e)
429                 {
430                         base.OnEnabledChanged (e);
431
432                         if (Enabled)
433                                 firstbutton_state = secondbutton_state = ButtonState.Normal;
434                         else
435                                 firstbutton_state = secondbutton_state = ButtonState.Inactive;
436
437                         Refresh ();
438                 }
439                 
440                 protected override void OnHandleCreated (System.EventArgs e)
441                 {
442                         base.OnHandleCreated (e);               
443
444                         CalcButtonSizes ();
445                         CalcThumbArea ();                       
446                         UpdateThumbPos (thumb_area.Y + (int)(((float)(position - minimum)) * pixel_per_pos), false);                    
447                 }
448
449                 protected virtual void OnScroll (ScrollEventArgs event_args)
450                 {
451                         if (Scroll == null)
452                                 return;
453                                 
454                         if (event_args.NewValue < Minimum) {
455                                 event_args.NewValue = Minimum;
456                         }
457                         
458                         if (event_args.NewValue > Maximum) {
459                                 event_args.NewValue = Maximum;
460                         }
461
462                         Scroll (this, event_args);
463                 }
464
465                 protected virtual void OnValueChanged (EventArgs e)
466                 {
467                         if (ValueChanged != null)
468                                 ValueChanged (this, e);
469                 }
470
471                 public override string ToString()
472                 {
473                         return string.Format("{0}, Minimum: {1}, Maximum: {2}, Value: {3}",
474                                                 GetType( ).FullName.ToString( ), minimum, maximum, position);
475                 }
476
477                 protected void UpdateScrollInfo ()
478                 {
479                         Refresh ();
480                 }
481
482                 protected override void WndProc (ref Message m)
483                 {
484                         switch ((Msg) m.Msg)
485                         {
486                                 case Msg.WM_PAINT:
487                                 {
488                                         PaintEventArgs  paint_event;
489
490                                         paint_event = XplatUI.PaintEventStart (Handle, true);
491                                         OnPaintSB (paint_event);
492                                         XplatUI.PaintEventEnd (Handle, true);
493                                         return;
494                                 }
495
496                                 case Msg.WM_ERASEBKGND:
497                                         m.Result = (IntPtr) 1; /// Disable background painting to avoid flickering
498                                         return;
499
500                                 default:
501                                         break;
502                         }
503
504                         base.WndProc (ref m);
505                 }
506
507                 #endregion //Public Methods
508
509                 #region Private Methods
510                 
511                 private void CalcButtonSizes ()
512                 {               
513                         if (vert) {
514                                 if (Height < ThemeEngine.Current.ScrollBarButtonSize * 2)
515                                         scrollbutton_height = Height /2;
516                                 else
517                                         scrollbutton_height = ThemeEngine.Current.ScrollBarButtonSize;
518                                 
519                         } else {
520                                 if (Width < ThemeEngine.Current.ScrollBarButtonSize * 2)
521                                         scrollbutton_width = Width /2;
522                                 else
523                                         scrollbutton_width = ThemeEngine.Current.ScrollBarButtonSize;
524                         }
525                 }
526                                 
527                 private void CalcThumbArea ()
528                 {
529                         // Thumb area
530                         if (vert) {
531
532                                 thumb_area.Height = Height - scrollbutton_height -  scrollbutton_height;
533                                 thumb_area.X = 0;
534                                 thumb_area.Y = scrollbutton_height;
535                                 thumb_area.Width = Width;
536
537                                 if (Height < thumb_notshown_size)
538                                         thumb_size = 0;
539                                 else {
540                                         double per =  ((double) this.LargeChange / (double)((1 + maximum - minimum)));
541                                         thumb_size = 1 + (int) (thumb_area.Height * per);                                       
542                                         
543                                         if (thumb_size < thumb_min_size)
544                                                 thumb_size = thumb_min_size;
545                                 }                               
546
547                                 pixel_per_pos = ((float)(thumb_area.Height - thumb_size) / (float) ((maximum - minimum - this.LargeChange) + 1));
548
549                         } else  {
550
551                                 thumb_area.Y = 0;
552                                 thumb_area.X = scrollbutton_width;
553                                 thumb_area.Height = Height;
554                                 thumb_area.Width = Width - scrollbutton_width -  scrollbutton_width;    
555                                 
556                                 if (Width < thumb_notshown_size)
557                                         thumb_size = 0;
558                                 else {
559                                         double per =  ((double) this.LargeChange / (double)((1 + maximum - minimum)));
560                                         thumb_size = 1 + (int) (thumb_area.Width * per);
561                                         
562                                         if (thumb_size < thumb_min_size)
563                                                 thumb_size = thumb_min_size;
564                                 }
565                                 
566                                 pixel_per_pos = ((float)(thumb_area.Width - thumb_size) / (float) ((maximum - minimum - this.LargeChange) + 1));
567                         }
568                 }
569
570                 private void LargeIncrement ()
571                 {                       
572                         ScrollEventArgs event_args;
573                         int pos = position + large_change;
574                         
575                         event_args = new ScrollEventArgs (ScrollEventType.LargeIncrement, pos);
576                         OnScroll (event_args);                          
577                         pos = event_args.NewValue;                      
578                         
579                         event_args = new ScrollEventArgs (ScrollEventType.EndScroll, pos);
580                         OnScroll (event_args);          
581                         pos = event_args.NewValue;                      
582
583                         UpdatePos (pos, true);
584                 }
585
586                 private void LargeDecrement ()
587                 {                       
588                         ScrollEventArgs event_args;
589                         int pos = position - large_change;
590                         
591                         event_args = new ScrollEventArgs (ScrollEventType.LargeDecrement, pos);
592                         OnScroll (event_args);
593                         pos = event_args.NewValue;                      
594                         
595                         event_args = new ScrollEventArgs (ScrollEventType.EndScroll, pos);
596                         OnScroll (event_args);
597                         pos = event_args.NewValue;
598                         
599
600                         UpdatePos (pos, true);
601                 }               
602                 
603                 private void OnResizeSB (Object o, EventArgs e)
604                 {                       
605                         if (Width <= 0 || Height <= 0)
606                                 return;
607                         
608                         CalcButtonSizes ();
609                         CalcThumbArea ();
610                         UpdatePos (position, true);
611
612                         Refresh ();
613                 }
614
615                 private void OnPaintSB (PaintEventArgs pevent)
616                 {
617                         if (Paint != null) {
618                                 Paint (this, pevent);
619                         }
620                         ThemeEngine.Current.DrawScrollBar (pevent.Graphics, pevent.ClipRectangle, this);
621                 }
622
623                 private void OnTimer (Object source, EventArgs e)
624                 {
625                         ClearDirty ();
626
627                         switch (timer_type) {
628
629                         case TimerType.HoldButton:
630                                 SetRepeatButtonTimer ();
631                                 break;
632
633                         case TimerType.RepeatButton:
634                         {
635                                 if ((firstbutton_state & ButtonState.Pushed) == ButtonState.Pushed &&
636                                                 position != Minimum)
637                                         SmallDecrement();
638
639                                 if ((secondbutton_state & ButtonState.Pushed) == ButtonState.Pushed &&
640                                         position != Maximum)
641                                         SmallIncrement();
642
643                                 break;
644                         }
645
646                         case TimerType.HoldThumbArea:
647                                 SetRepeatThumbAreaTimer ();
648                                 break;
649
650                         case TimerType.RepeatThumbArea:
651                         {
652                                 Point pnt, pnt_screen;
653                                 Rectangle thumb_area_screen = thumb_area;
654
655                                 pnt_screen = PointToScreen (new Point (thumb_area.X, thumb_area.Y));
656                                 thumb_area_screen.X = pnt_screen.X;
657                                 thumb_area_screen.Y = pnt_screen.Y;
658                                 
659                                 if (thumb_area_screen.Contains (MousePosition) == false) {                                      
660                                         timer.Enabled = false;
661                                         thumb_moving = ThumbMoving.None;
662                                         DirtyThumbArea ();
663                                         InvalidateDirty ();
664                                 }                               
665                                 
666                                 pnt = PointToClient (MousePosition);
667                                 
668                                 if (vert)
669                                         lastclick_pos = pnt.Y;
670                                 else
671                                         lastclick_pos = pnt.X;
672
673                                 if (thumb_moving == ThumbMoving.Forward) {
674                                         if ((vert && (thumb_pos.Y + thumb_size > lastclick_pos)) ||
675                                                 (!vert && (thumb_pos.X + thumb_size > lastclick_pos)) ||
676                                                 (thumb_area.Contains (pnt) == false)){
677                                                 timer.Enabled = false;                                          
678                                                 thumb_moving = ThumbMoving.None;
679                                                 Refresh ();                     
680                                                 return;
681                                         } else {
682                                                 LargeIncrement ();
683                                 }
684                                 }
685                                 else
686                                         if ((vert && (thumb_pos.Y < lastclick_pos)) ||
687                                                 (!vert && (thumb_pos.X  < lastclick_pos))){
688                                                 timer.Enabled = false;
689                                                 thumb_moving = ThumbMoving.None;
690                                                 Refresh ();                                             
691                                         } else {
692                                                 LargeDecrement ();
693                                         }
694
695                                 break;
696                         }
697                         default:
698                                 break;
699                         }
700
701                         InvalidateDirty ();
702                 }               
703
704                 private void OnMouseMoveSB (object sender, MouseEventArgs e)
705                 {
706                         if (MouseMove != null) {
707                                 MouseMove (this, e);
708                         }
709                                 
710                         if (Enabled == false || thumb_size == 0)
711                                 return;
712
713                         if (firstbutton_pressed) {
714                                 if (!first_arrow_area.Contains (e.X, e.Y) && ((firstbutton_state & ButtonState.Pushed) == ButtonState.Pushed)) {
715                                         firstbutton_state = ButtonState.Normal;
716                                         Invalidate (first_arrow_area);
717                                         return;
718                                 } else if (first_arrow_area.Contains (e.X, e.Y) && ((firstbutton_state & ButtonState.Normal) == ButtonState.Normal)) {
719                                         firstbutton_state = ButtonState.Pushed;
720                                         Invalidate (first_arrow_area);
721                                         return;
722                                 }
723                         } else if (secondbutton_pressed) {
724                                 if (!second_arrow_area.Contains (e.X, e.Y) && ((secondbutton_state & ButtonState.Pushed) == ButtonState.Pushed)) {
725                                         secondbutton_state = ButtonState.Normal;
726                                         Invalidate (second_arrow_area);
727                                         return;
728                                 } else if (second_arrow_area.Contains (e.X, e.Y) && ((secondbutton_state & ButtonState.Normal) == ButtonState.Normal)) {
729                                         secondbutton_state = ButtonState.Pushed;
730                                         Invalidate (second_arrow_area);
731                                         return;
732                                 }
733                         } else if (thumb_pressed == true) {
734                                 int pixel_pos;
735
736                                 if (vert) {
737
738                                         int mouse_click = e.Y;
739                                         int outside_curpos = thumb_area.Y + thumb_area.Height - thumb_size + lastclick_pos_thumb;
740                                         
741                                         
742                                         if (mouse_click > thumb_area.Y + thumb_area.Height) {
743                                                 outside_thumbarea_right = true;
744                                                 mouse_click = thumb_area.Y + thumb_area.Height;
745                                         }
746
747                                         if (mouse_click < thumb_area.Y) {
748                                                 outside_thumbarea_left = true;
749                                                 mouse_click = thumb_area.Y;
750                                         }
751
752                                         if (outside_thumbarea_right && mouse_click < outside_curpos) {
753                                                 outside_thumbarea_right = false;
754                                                 thumb_pixel_click_move_prev =
755                                                 thumb_pixel_click_move = outside_curpos;
756                                         }
757
758                                         if (outside_thumbarea_left && mouse_click > thumb_area.Y + lastclick_pos_thumb) {
759                                                 outside_thumbarea_left = false;
760                                                 thumb_pixel_click_move_prev =
761                                                 thumb_pixel_click_move = thumb_area.Y + lastclick_pos_thumb;
762                                         }
763
764                                         if (outside_thumbarea_right == false && outside_thumbarea_left == false) {
765                                                 pixel_pos = thumb_pos.Y + (thumb_pixel_click_move - thumb_pixel_click_move_prev);
766                                                 thumb_pixel_click_move_prev = thumb_pixel_click_move;
767                                                 thumb_pixel_click_move = mouse_click;
768                                                 
769                                                 UpdateThumbPos (pixel_pos, true);
770                                                 OnScroll (new ScrollEventArgs (ScrollEventType.ThumbTrack, position));
771                                         }
772
773                                 }
774                                 else {
775                                         int mouse_click = e.X;
776                                         int outside_curpos = thumb_area.X + thumb_area.Width - thumb_size + lastclick_pos_thumb;
777                                                                                 
778                                         if (mouse_click >  thumb_area.X + thumb_area.Width) {
779                                                 outside_thumbarea_right = true;
780                                                 mouse_click = thumb_area.X + thumb_area.Width;
781                                         }
782
783                                         if (mouse_click <  thumb_area.X) {
784                                                 outside_thumbarea_left = true;
785                                                 mouse_click = thumb_area.X;
786                                         }
787
788                                         if (outside_thumbarea_right && mouse_click < outside_curpos) {
789                                                 outside_thumbarea_right = false;
790                                                 thumb_pixel_click_move_prev =
791                                                 thumb_pixel_click_move = outside_curpos;
792                                         }
793
794                                         if (outside_thumbarea_left && mouse_click > thumb_area.X + lastclick_pos_thumb) {
795                                                 outside_thumbarea_left = false;
796                                                 thumb_pixel_click_move_prev =
797                                                 thumb_pixel_click_move = thumb_area.X + lastclick_pos_thumb;
798                                         }
799
800                                         if (outside_thumbarea_right == false && outside_thumbarea_left == false) {
801                                                 pixel_pos = thumb_pos.X + (thumb_pixel_click_move - thumb_pixel_click_move_prev);
802                                                 thumb_pixel_click_move_prev = thumb_pixel_click_move;
803                                                 thumb_pixel_click_move = mouse_click;
804                                                 UpdateThumbPos (pixel_pos, true);                                               
805                                                 OnScroll (new ScrollEventArgs (ScrollEventType.ThumbTrack, position));
806                                         }
807
808                                 }
809
810                                 Refresh ();
811                         }
812
813                 }
814
815                 private void OnMouseDownSB (object sender, MouseEventArgs e)
816                 {
817                         ClearDirty ();
818                         
819                         if (e.Button == MouseButtons.Right) {
820                                 if (MouseDown != null) {
821                                         MouseDown (this, e);
822                                 }
823                         }
824                         
825                         if (Enabled == false)
826                                 return;
827
828                         if (firstbutton_state != ButtonState.Inactive && first_arrow_area.Contains (e.X, e.Y)) {
829                                 this.Capture = true;                            
830                                 firstbutton_state = ButtonState.Pushed;
831                                 firstbutton_pressed = true;
832                                 Invalidate (first_arrow_area);
833                                 if (!timer.Enabled) {
834                                         SetHoldButtonClickTimer ();
835                                         timer.Enabled = true;
836                                 }
837                         }
838
839                         if (secondbutton_state != ButtonState.Inactive && second_arrow_area.Contains (e.X, e.Y)) {
840                                 this.Capture = true;                            
841                                 secondbutton_state = ButtonState.Pushed;
842                                 secondbutton_pressed = true;
843                                 Invalidate (second_arrow_area);
844                                 if (!timer.Enabled) {
845                                         SetHoldButtonClickTimer ();
846                                         timer.Enabled = true;
847                                 }
848                         }
849
850                         if (thumb_size > 0 && thumb_pos.Contains (e.X, e.Y)) {
851                                 thumb_pressed = true;
852                                 this.Capture = true;
853                                 if (vert) {
854                                         lastclick_pos_thumb = e.Y - thumb_pos.Y;
855                                         lastclick_pos = e.Y;                                    
856                                         thumb_pixel_click_move_prev = thumb_pixel_click_move = e.Y;
857                                 }
858                                 else {
859                                         lastclick_pos_thumb = e.X - thumb_pos.X;
860                                         lastclick_pos = e.X;
861                                         thumb_pixel_click_move_prev = thumb_pixel_click_move = e.X;
862                                 }
863                         } else {
864                                 if (thumb_size > 0 && thumb_area.Contains (e.X, e.Y)) {
865
866                                         if (vert) {
867                                                 lastclick_pos_thumb = e.Y - thumb_pos.Y;
868                                                 lastclick_pos = e.Y;
869
870                                                 if (e.Y > thumb_pos.Y + thumb_pos.Height) {
871                                                         LargeIncrement ();                                                      
872                                                         thumb_moving = ThumbMoving.Forward;                                                     
873                                                         Dirty (new Rectangle (0, thumb_pos.Y + thumb_pos.Height,
874                                                                                       ClientRectangle.Width,
875                                                                                       ClientRectangle.Height -  (thumb_pos.Y + thumb_pos.Height) -
876                                                                                       scrollbutton_height));
877                                                 } else {
878                                                         LargeDecrement ();                                                      
879                                                         thumb_moving = ThumbMoving.Backwards;
880                                                         Dirty (new Rectangle (0,  scrollbutton_height,
881                                                                                       ClientRectangle.Width,
882                                                                                       thumb_pos.Y - scrollbutton_height));
883                                                 }
884                                         } else {
885
886                                                 lastclick_pos_thumb = e.X - thumb_pos.X;
887                                                 lastclick_pos = e.X;
888
889                                                 if (e.X > thumb_pos.X + thumb_pos.Width) {
890                                                         thumb_moving = ThumbMoving.Forward;
891                                                         LargeIncrement ();                                                      
892                                                         Dirty (new Rectangle (thumb_pos.X + thumb_pos.Width, 0,
893                                                                                       ClientRectangle.Width -  (thumb_pos.X + thumb_pos.Width) -
894                                                                                       scrollbutton_width,
895                                                                                       ClientRectangle.Height));
896                                                 } else {
897                                                         thumb_moving = ThumbMoving.Backwards;
898                                                         LargeDecrement ();                                                      
899                                                         Dirty (new Rectangle (scrollbutton_width,  0,
900                                                                                       thumb_pos.X - scrollbutton_width,
901                                                                                       ClientRectangle.Height));
902                                                 }
903                                         }
904
905                                         SetHoldThumbAreaTimer ();
906                                         timer.Enabled = true;
907                                         InvalidateDirty ();
908                                 }
909                         }
910                 }
911                 
912                 private void OnMouseUpSB (object sender, MouseEventArgs e)
913                 {
914                         ClearDirty ();
915
916                         if (e.Button == MouseButtons.Right) {
917                                 if (MouseUp != null) {
918                                         MouseUp (this, e);
919                                 }
920                         }
921                         
922                         if (Enabled == false)
923                                 return;
924
925                         timer.Enabled = false;
926                         if (thumb_moving != ThumbMoving.None) {
927                                 DirtyThumbArea ();
928                                 thumb_moving = ThumbMoving.None;
929                         }                       
930                         this.Capture = false;
931
932                         if (firstbutton_pressed) {
933                                 firstbutton_state = ButtonState.Normal;
934                                 if (first_arrow_area.Contains (e.X, e.Y)) {
935                                         SmallDecrement ();
936                                 }
937                                 firstbutton_pressed = false;
938                                 Dirty (first_arrow_area);
939                         } else if (secondbutton_pressed) {
940                                 secondbutton_state = ButtonState.Normal;
941                                 if (second_arrow_area.Contains (e.X, e.Y)) {
942                                         SmallIncrement ();
943                                 }
944                                 Dirty (second_arrow_area);
945                                 secondbutton_pressed = false;
946                         } else if (thumb_pressed == true) {
947                                 OnScroll (new ScrollEventArgs (ScrollEventType.ThumbPosition, position));
948                                 OnScroll (new ScrollEventArgs (ScrollEventType.EndScroll, position));
949                                 thumb_pressed = false;
950                                 Refresh ();
951                                 return;
952                         }
953
954                         InvalidateDirty ();
955                 }
956
957                 private void OnKeyDownSB (Object o, KeyEventArgs key)
958                 {
959                         if (Enabled == false)
960                                 return;
961
962                         ClearDirty ();
963
964                         switch (key.KeyCode){
965                         case Keys.Up:
966                         {
967                                 SmallDecrement ();
968                                 break;
969                         }
970                         case Keys.Down:
971                         {
972                                 SmallIncrement ();
973                                 break;
974                         }
975                         case Keys.PageUp:
976                         {
977                                 LargeDecrement ();
978                                 break;
979                         }
980                         case Keys.PageDown:
981                         {
982                                 LargeIncrement ();
983                                 break;
984                         }
985                         case Keys.Home:
986                         {               
987                                 SetHomePosition ();                             
988                                 break;
989                         }                       
990                         case Keys.End:
991                         {       
992                                 SetEndPosition ();
993                                 break;
994                         }
995                         default:
996                                 break;
997                         }
998
999                         InvalidateDirty ();
1000                 }               
1001                 
1002                 private void SetEndPosition () 
1003                 {                       
1004                         ScrollEventArgs event_args;
1005                         int pos = Maximum;
1006                         
1007                         event_args = new ScrollEventArgs (ScrollEventType.Last, pos);
1008                         OnScroll (event_args);
1009                         pos = event_args.NewValue;                      
1010                         
1011                         event_args = new ScrollEventArgs (ScrollEventType.EndScroll, pos);
1012                         OnScroll (event_args);                  
1013                         pos = event_args.NewValue;                      
1014
1015                         SetValue (pos);
1016                 }
1017                 
1018                 private void SetHomePosition ()
1019                 {
1020                         ScrollEventArgs event_args;
1021                         int pos = Minimum;
1022                         
1023                         event_args = new ScrollEventArgs (ScrollEventType.First, pos);
1024                         OnScroll (event_args);
1025                         pos = event_args.NewValue;
1026                                                 
1027                         event_args = new ScrollEventArgs (ScrollEventType.EndScroll, pos);
1028                         OnScroll (event_args);                  
1029                         pos = event_args.NewValue;                      
1030                         
1031                         SetValue (pos);
1032                 }               
1033
1034                 private void SmallIncrement ()
1035                 {
1036                         ScrollEventArgs event_args;
1037                         int pos = position + small_change;
1038                         
1039                         event_args = new ScrollEventArgs (ScrollEventType.SmallIncrement, pos);
1040                         OnScroll (event_args);                          
1041                         pos = event_args.NewValue;                      
1042                         
1043                         event_args = new ScrollEventArgs (ScrollEventType.EndScroll, pos);
1044                         OnScroll (event_args);                  
1045                         pos = event_args.NewValue;                      
1046
1047                         UpdatePos (pos, true);
1048                 }
1049
1050                 private void SmallDecrement ()
1051                 {                       
1052                         ScrollEventArgs event_args;
1053                         int pos = position - small_change;
1054                         
1055                         event_args = new ScrollEventArgs (ScrollEventType.SmallDecrement, pos);
1056                         OnScroll (event_args);
1057                         pos = event_args.NewValue;
1058                                                 
1059                         event_args = new ScrollEventArgs (ScrollEventType.EndScroll, pos);
1060                         OnScroll (event_args);                  
1061                         pos = event_args.NewValue;                      
1062
1063                         UpdatePos (pos, true);
1064                 }
1065                 
1066                 private void SetHoldButtonClickTimer ()
1067                 {
1068                         timer.Enabled = false;
1069                         timer.Interval = 200;
1070                         timer_type = TimerType.HoldButton;
1071                         timer.Enabled = true;
1072                 }
1073
1074                 private void SetRepeatButtonTimer ()
1075                 {
1076                         timer.Enabled = false;
1077                         timer.Interval = 50;
1078                         timer_type = TimerType.RepeatButton;
1079                         timer.Enabled = true;
1080                 }
1081
1082                 private void SetHoldThumbAreaTimer ()
1083                 {
1084                         timer.Enabled = false;
1085                         timer.Interval = 200;
1086                         timer_type = TimerType.HoldThumbArea;
1087                         timer.Enabled = true;
1088                 }
1089
1090                 private void SetRepeatThumbAreaTimer ()
1091                 {
1092                         timer.Enabled = false;
1093                         timer.Interval = 50;
1094                         timer_type = TimerType.RepeatThumbArea;
1095                         timer.Enabled = true;
1096                 }                               
1097                 
1098                 private void UpdatePos (int newPos, bool update_thumbpos)
1099                 {                       
1100                         int pos;
1101
1102                         if (newPos < minimum)
1103                                 pos = minimum;
1104                         else
1105                                 if (newPos > maximum + 1 - large_change)
1106                                         pos = maximum + 1 - large_change;
1107                                 else
1108                                         pos = newPos;
1109
1110                         // pos can't be less than minimum
1111                         if (pos < minimum)
1112                                 pos = minimum;
1113
1114                         if (update_thumbpos) {
1115                                 if (vert)
1116                                         UpdateThumbPos (thumb_area.Y + (int)(((float)(pos - minimum)) * pixel_per_pos), false);
1117                                 else
1118                                         UpdateThumbPos (thumb_area.X + (int)(((float)(pos - minimum)) * pixel_per_pos), false);
1119                                 SetValue (pos);
1120                         }
1121                         else {
1122                                 position = pos; // Updates directly the value to avoid thumb pos update
1123                                 
1124                                 if (ValueChanged != null)
1125                                         ValueChanged (this, EventArgs.Empty);
1126                         }                       
1127                 }
1128
1129                 private void UpdateThumbPos (int pixel, bool update_value)
1130                 {
1131                         float new_pos = 0;
1132
1133                         if (vert) {
1134                                 Dirty (thumb_pos);
1135                                 if (pixel < thumb_area.Y)
1136                                         thumb_pos.Y = thumb_area.Y;
1137                                 else
1138                                         if (pixel > thumb_area.Y + thumb_area.Height - thumb_size)
1139                                                 thumb_pos.Y = thumb_area.Y +  thumb_area.Height - thumb_size;
1140                                         else
1141                                                 thumb_pos.Y = pixel;
1142
1143                                 thumb_pos.X = 0;
1144                                 thumb_pos.Width = ThemeEngine.Current.ScrollBarButtonSize;
1145                                 thumb_pos.Height = thumb_size;
1146                                 new_pos = (float) (thumb_pos.Y - thumb_area.Y);
1147                                 new_pos = new_pos / pixel_per_pos;
1148
1149                                 Dirty (thumb_pos);
1150                         } else  {
1151                                 Dirty (thumb_pos);
1152                                 if (pixel < thumb_area.X)
1153                                         thumb_pos.X = thumb_area.X;
1154                                 else
1155                                         if (pixel > thumb_area.X + thumb_area.Width - thumb_size)
1156                                                 thumb_pos.X = thumb_area.X +  thumb_area.Width - thumb_size;
1157                                         else
1158                                                 thumb_pos.X = pixel;
1159
1160                                 thumb_pos.Y = 0;
1161                                 thumb_pos.Width =  thumb_size;
1162                                 thumb_pos.Height = ThemeEngine.Current.ScrollBarButtonSize;
1163                                 new_pos = (float) (thumb_pos.X - thumb_area.X);
1164                                 new_pos = new_pos / pixel_per_pos;
1165
1166                                 Dirty (thumb_pos);
1167                         }
1168
1169                         if (update_value)
1170                                 UpdatePos ((int) new_pos + minimum, false);
1171                 }
1172
1173                 private void SetValue (int value)
1174                 {
1175                         if ( value < minimum || value > maximum )
1176                                 throw new ArgumentException(
1177                                         String.Format("'{0}' is not a valid value for 'Value'. 'Value' should be between 'Minimum' and 'Maximum'", value));
1178
1179                         if (position != value){
1180                                 position = value;
1181
1182                                 OnValueChanged (EventArgs.Empty);
1183                                 UpdatePos (value, true);
1184                         }
1185                 }
1186
1187                 private void ClearDirty ()
1188                 {
1189                         dirty = Rectangle.Empty;
1190                 }
1191
1192                 private void Dirty (Rectangle r)
1193                 {
1194                         if (dirty == Rectangle.Empty) {
1195                                 dirty = r;
1196                                 return;
1197                         }
1198                         dirty = Rectangle.Union (dirty, r);
1199                 }
1200
1201                 private void DirtyThumbArea ()
1202                 {
1203                         if (thumb_moving == ThumbMoving.Forward) {
1204                                 if (vert) {
1205                                         Dirty (new Rectangle (0, thumb_pos.Y + thumb_pos.Height,
1206                                                                       ClientRectangle.Width,
1207                                                                       ClientRectangle.Height -  (thumb_pos.Y + thumb_pos.Height) -
1208                                                                       scrollbutton_height));
1209                                 } else {
1210                                         Dirty (new Rectangle (thumb_pos.X + thumb_pos.Width, 0,
1211                                                                       ClientRectangle.Width -  (thumb_pos.X + thumb_pos.Width) -
1212                                                                       scrollbutton_width,
1213                                                                       ClientRectangle.Height));
1214                                 }
1215                         } else if (thumb_moving == ThumbMoving.Backwards) {
1216                                 if (vert) {
1217                                         Dirty(new Rectangle (0,  scrollbutton_height,
1218                                                                       ClientRectangle.Width,
1219                                                                       thumb_pos.Y - scrollbutton_height));
1220                                 } else {
1221                                         Dirty (new Rectangle (scrollbutton_width,  0,
1222                                                                       thumb_pos.X - scrollbutton_width,
1223                                                                       ClientRectangle.Height));
1224                                 }
1225                         }
1226                 }
1227
1228                 private void InvalidateDirty ()
1229                 {
1230                         Invalidate (dirty);
1231                         dirty = Rectangle.Empty;
1232                 }
1233
1234                 #endregion //Private Methods
1235          }
1236 }
1237
1238