* ScrollBar.cs: Give the correct clipping rect to the theme. Dirty
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ScrollBar.cs
1 //
2 // System.Windows.Forms.ScrollBar.cs
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 //
23 // Copyright (C) 2004, Novell, Inc.
24 //
25 // Authors:
26 //      Jordi Mas i Hernandez   jordi@ximian.com
27 //
28 //
29
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                 public new event EventHandler BackColorChanged;
98                 public new event EventHandler BackgroundImageChanged;
99                 public new event EventHandler Click;
100                 public new event EventHandler DoubleClick;
101                 public new event EventHandler FontChanged;
102                 public new event EventHandler ForeColorChanged;
103                 public new event EventHandler ImeModeChanged;
104                 public new event MouseEventHandler MouseDown;
105                 public new event MouseEventHandler MouseMove;
106                 public new event MouseEventHandler MouseUp;
107                 public new event PaintEventHandler Paint;
108                 public event ScrollEventHandler Scroll;
109                 public new event EventHandler TextChanged;
110                 public event EventHandler ValueChanged;
111                 #endregion Events
112
113                 public ScrollBar ()
114                 {
115                         position = 0;
116                         minimum = 0;
117                         maximum = 100;
118                         large_change = 10;
119                         small_change = 1;
120
121                         timer.Tick += new EventHandler (OnTimer);
122                         base.KeyDown += new KeyEventHandler (OnKeyDownSB);
123                         base.MouseDown += new MouseEventHandler (OnMouseDownSB);
124                         base.MouseUp += new MouseEventHandler (OnMouseUpSB);
125                         base.MouseMove += new MouseEventHandler (OnMouseMoveSB);
126                         base.Resize += new EventHandler (OnResizeSB);
127                         base.TabStop = false;
128
129                         if (ThemeEngine.Current.DoubleBufferingSupported == true) {
130                                 double_buffering = true;
131                         } else {
132                                 double_buffering = false;
133                         }
134
135                         SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
136                         SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
137                 }
138
139                 #region Internal & Private Properties
140                 internal Rectangle FirstArrowArea {
141                         get {
142                                 return this.first_arrow_area;
143                         }
144
145                         set {
146                                 this.first_arrow_area = value;
147                         }
148                 }
149
150                 internal Rectangle SecondArrowArea {
151                         get {
152                                 return this.second_arrow_area;
153                         }
154
155                         set {
156                                 this.second_arrow_area = value;
157                         }
158                 }
159
160                 internal Rectangle ThumbPos {
161                         get {
162                                 return thumb_pos;
163                         }
164
165                         set {
166                                 thumb_pos = value;
167                         }
168                 }
169                 #endregion      // Internal & Private Properties
170
171                 #region Public Properties
172
173                 [EditorBrowsable (EditorBrowsableState.Never)]
174                 public override Color BackColor
175                 {
176                         get { return base.BackColor; }
177                         set {
178                                 if (base.BackColor == value)
179                                         return;
180
181                                 if (BackColorChanged != null)
182                                         BackColorChanged (this, EventArgs.Empty);
183
184                                 base.BackColor = value;
185                                 Refresh ();
186                         }
187                 }
188
189                 [EditorBrowsable (EditorBrowsableState.Never)]
190                 public override Image BackgroundImage
191                 {
192                         get { return base.BackgroundImage; }
193                         set {
194                                 if (base.BackgroundImage == value)
195                                         return;
196
197                                 if (BackgroundImageChanged != null)
198                                         BackgroundImageChanged (this, EventArgs.Empty);
199
200                                 base.BackgroundImage = value;
201                         }
202                 }
203
204                 protected override CreateParams CreateParams
205                 {
206                         get {   return base.CreateParams; }
207                 }
208
209                 protected override ImeMode DefaultImeMode
210                 {
211                         get { return ImeMode.Disable; }
212                 }
213
214                 public override Font Font
215                 {
216                         get { return base.Font; }
217                         set {
218                                 if (base.Font == value)
219                                         return;
220
221                                 if (FontChanged != null)
222                                         FontChanged (this, EventArgs.Empty);
223
224                                 base.Font = value;
225                         }
226                 }
227
228                 [EditorBrowsable (EditorBrowsableState.Never)]
229                 public override Color ForeColor
230                 {
231                         get { return base.ForeColor; }
232                         set {
233                                 if (base.ForeColor == value)
234                                         return;
235
236                                 if (ForeColorChanged != null)
237                                         ForeColorChanged (this, EventArgs.Empty);
238
239                                 base.ForeColor = value;
240                                 Refresh ();
241                         }
242                 }
243
244                 [EditorBrowsable (EditorBrowsableState.Never)]
245                 public new ImeMode ImeMode
246                 {
247                         get { return base.ImeMode; }
248                         set {
249                                 if (base.ImeMode == value)
250                                         return;
251
252                                 if (ImeModeChanged != null)
253                                         ImeModeChanged (this, EventArgs.Empty);
254
255                                 base.ImeMode = value;
256                         }
257                 }
258
259                 public int LargeChange {
260                         get {
261                                 if (large_change > maximum)
262                                         return (maximum + 1);
263                                 else
264                                         return large_change;
265                         }
266                         set {
267                                 if (value < 0)
268                                         throw new Exception( string.Format("Value '{0}' must be greater than or equal to 0.", value));
269
270                                 if (large_change != value) {
271                                         large_change = value;
272
273                                         // thumb area depends on large change value,
274                                         // so we need to recalculate it.
275                                         CalcThumbArea ();
276                                         UpdatePos (Value, true);
277                                         Refresh ();
278                                 }
279                         }
280                 }
281
282                 public int Maximum {
283                         get { return maximum; }
284                         set {
285                                 maximum = value;
286
287                                 if (maximum < minimum)
288                                         minimum = maximum;
289
290                                 // thumb area depends on maximum value,
291                                 // so we need to recalculate it.
292                                 CalcThumbArea ();
293                                 UpdatePos (Value, true);
294                                 Refresh ();
295                         }
296                 }
297
298                 public int Minimum {
299                         get { return minimum; }
300                         set {
301                                 minimum = value;
302
303                                 if (minimum > maximum)
304                                         maximum = minimum;
305
306                                 // thumb area depends on minimum value,
307                                 // so we need to recalculate it.
308                                 CalcThumbArea ();
309                                 UpdatePos (Value, true);
310                                 Refresh ();
311                         }
312                 }
313
314                 public int SmallChange {
315                         get { return small_change; }
316                         set {
317                                 if ( value < 0 )
318                                         throw new Exception( string.Format("Value '{0}' must be greater than or equal to 0.", value));
319
320                                 if (small_change != value) {
321                                         small_change = value;
322                                         UpdatePos (Value, true);
323                                         Refresh ();
324                                 }
325                         }
326                 }
327
328                 public new bool TabStop {
329                         get { return base.TabStop; }
330                         set { base.TabStop = value; }
331                 }
332
333                 [EditorBrowsable (EditorBrowsableState.Never)]
334                 public override string Text {
335                          get { return base.Text;  }
336                          set { base.Text = value; }
337                 }
338
339                 public int Value {
340                         get { return position; }
341                         set {
342                                 if ( value < minimum || value > maximum )
343                                         throw new ArgumentException(
344                                                 string.Format("'{0}' is not a valid value for 'Value'. 'Value' should be between 'Minimum' and 'Maximum'", value));
345
346                                 if (position != value){
347                                         position = value;
348
349                                         OnValueChanged (EventArgs.Empty);
350
351                                         ClearDirty ();
352                                         UpdatePos (Value, true);
353                                         InvalidateDirty ();
354                                 }
355                         }
356                 }
357
358                 #endregion //Public Properties
359
360                 #region Public Methods
361                 
362                 protected override void OnEnabledChanged (EventArgs e)
363                 {
364                         base.OnEnabledChanged (e);
365
366                         if (Enabled)
367                                 firstbutton_state = secondbutton_state = ButtonState.Normal;
368                         else
369                                 firstbutton_state = secondbutton_state = ButtonState.Inactive;
370
371                         Refresh ();
372                 }
373                 
374                 protected override void OnHandleCreated (System.EventArgs e)
375                 {
376                         base.OnHandleCreated (e);               
377
378                         CalcButtonSizes ();
379                         CalcThumbArea ();
380                         UpdatePos (Value, true);
381                 }
382
383                 protected virtual void OnScroll (ScrollEventArgs event_args)
384                 {
385                         if (Scroll == null)
386                                 return;
387
388                         Scroll (this, event_args);
389                 }
390
391                 protected virtual void OnValueChanged (EventArgs e)
392                 {
393                         if (ValueChanged != null)
394                                 ValueChanged (this, e);
395                 }
396
397                 public override string ToString()
398                 {
399                         return string.Format("{0}, Minimum: {1}, Maximum: {2}, Value: {3}",
400                                                 GetType( ).FullName.ToString( ), minimum, maximum, position);
401                 }
402
403                 protected void UpdateScrollInfo ()
404                 {
405                         Refresh ();
406                 }
407
408                 protected override void WndProc (ref Message m)
409                 {
410                         switch ((Msg) m.Msg)
411                         {
412                                 case Msg.WM_PAINT:
413                                 {
414                                         PaintEventArgs  paint_event;
415
416                                         paint_event = XplatUI.PaintEventStart (Handle);
417                                         OnPaintSB (paint_event);
418                                         XplatUI.PaintEventEnd (Handle);
419                                         return;
420                                 }
421
422
423                                 case Msg.WM_ERASEBKGND:
424                                         m.Result = (IntPtr) 1; /// Disable background painting to avoid flickering
425                                         return;
426
427                                 default:
428                                         break;
429                         }
430
431                         base.WndProc (ref m);
432                 }
433
434                 #endregion //Public Methods
435
436                 #region Private Methods
437                 
438                 private void CalcButtonSizes ()
439                 {               
440                         if (vert) {
441                                 if (Height < ThemeEngine.Current.ScrollBarButtonSize * 2)
442                                         scrollbutton_height = Height /2;
443                                 else
444                                         scrollbutton_height = ThemeEngine.Current.ScrollBarButtonSize;
445                                 
446                         } else {
447                                 if (Width < ThemeEngine.Current.ScrollBarButtonSize * 2)
448                                         scrollbutton_width = Width /2;
449                                 else
450                                         scrollbutton_width = ThemeEngine.Current.ScrollBarButtonSize;
451                         }
452                 }
453                                 
454                 private void CalcThumbArea ()
455                 {
456                         // Thumb area
457                         if (vert) {
458
459                                 thumb_area.Height = Height - scrollbutton_height -  scrollbutton_height;
460                                 thumb_area.X = 0;
461                                 thumb_area.Y = scrollbutton_height;
462                                 thumb_area.Width = Width;
463
464                                 if (Height < thumb_notshown_size)
465                                         thumb_size = 0;
466                                 else {
467                                         double per =  ((double) this.LargeChange / (double)((1 + maximum - minimum)));
468                                         thumb_size = 1 + (int) (thumb_area.Height * per);                                       
469                                         
470                                         if (thumb_size < thumb_min_size)
471                                                 thumb_size = thumb_min_size;
472                                 }                               
473
474                                 pixel_per_pos = ((float)(thumb_area.Height - thumb_size) / (float) ((maximum - minimum - this.LargeChange) + 1));
475
476                         } else  {
477
478                                 thumb_area.Y = 0;
479                                 thumb_area.X = scrollbutton_width;
480                                 thumb_area.Height = Height;
481                                 thumb_area.Width = Width - scrollbutton_width -  scrollbutton_width;    
482                                 
483                                 if (Width < thumb_notshown_size)
484                                         thumb_size = 0;
485                                 else {
486                                         double per =  ((double) this.LargeChange / (double)((1 + maximum - minimum)));
487                                         thumb_size = 1 + (int) (thumb_area.Width * per);
488                                         
489                                         if (thumb_size < thumb_min_size)
490                                                 thumb_size = thumb_min_size;
491                                 }
492                                 
493                                 pixel_per_pos = ((float)(thumb_area.Width - thumb_size) / (float) ((maximum - minimum - this.LargeChange) + 1));
494                         }
495                 }
496                 
497                 private void Draw (Rectangle clip)
498                 {
499                         ThemeEngine.Current.DrawScrollBar(DeviceContext, clip, this);
500                 }
501
502                 private void LargeIncrement ()
503                 {
504                         UpdatePos (position + large_change, true);
505
506                         OnScroll (new ScrollEventArgs (ScrollEventType.LargeIncrement, position));
507                         OnScroll (new ScrollEventArgs (ScrollEventType.EndScroll, position));
508                 }
509
510                 private void LargeDecrement ()
511                 {
512                         UpdatePos (position - large_change, true);
513
514                         OnScroll (new ScrollEventArgs (ScrollEventType.LargeDecrement, position));
515                         OnScroll (new ScrollEventArgs (ScrollEventType.EndScroll, position));
516                 }               
517                 
518                 private void OnResizeSB (Object o, EventArgs e)
519                 {                       
520                         if (Width <= 0 || Height <= 0)
521                                 return;
522                         
523                         CalcButtonSizes ();
524                         CalcThumbArea ();
525                         UpdatePos (position, true);
526                 }
527
528                 private void OnPaintSB (PaintEventArgs pevent)
529                 {
530                         if (Paint != null) {
531                                 Paint (this, pevent);
532                         }
533                         
534                         if (Width <= 0 || Height <=  0 || Visible == false)
535                                 return;
536
537                         Draw (pevent.ClipRectangle);
538                         pevent.Graphics.DrawImage (ImageBuffer, pevent.ClipRectangle, pevent.ClipRectangle, GraphicsUnit.Pixel);
539                 }
540
541                 private void OnTimer (Object source, EventArgs e)
542                 {
543                         ClearDirty ();
544
545                         switch (timer_type) {
546
547                         case TimerType.HoldButton:
548                                 SetRepeatButtonTimer ();
549                                 break;
550
551                         case TimerType.RepeatButton:
552                         {
553                                 if ((firstbutton_state & ButtonState.Pushed) == ButtonState.Pushed)
554                                         SmallDecrement();
555
556                                 if ((secondbutton_state & ButtonState.Pushed) == ButtonState.Pushed)
557                                         SmallIncrement();
558
559                                 break;
560                         }
561
562                         case TimerType.HoldThumbArea:
563                                 SetRepeatThumbAreaTimer ();
564                                 break;
565
566                         case TimerType.RepeatThumbArea:
567                         {
568                                 Point pnt, pnt_screen;
569                                 Rectangle thumb_area_screen = thumb_area;
570
571                                 pnt_screen = PointToScreen (new Point (thumb_area.X, thumb_area.Y));
572                                 thumb_area_screen.X = pnt_screen.X;
573                                 thumb_area_screen.Y = pnt_screen.Y;
574                                 
575                                 if (thumb_area_screen.Contains (MousePosition) == false) {                                      
576                                         timer.Enabled = false;
577                                         thumb_moving = ThumbMoving.None;
578                                         DirtyThumbArea ();
579                                         InvalidateDirty ();
580                                 }                               
581                                 
582                                 pnt = PointToClient (MousePosition);
583                                 
584                                 if (vert)
585                                         lastclick_pos = pnt.Y;
586                                 else
587                                         lastclick_pos = pnt.X;
588
589                                 if (thumb_moving == ThumbMoving.Forward) {
590                                         if ((vert && (thumb_pos.Y + thumb_size > lastclick_pos)) ||
591                                                 (!vert && (thumb_pos.X + thumb_size > lastclick_pos)) ||
592                                                 (thumb_area.Contains (pnt) == false)){
593                                                 timer.Enabled = false;                                          
594                                                 thumb_moving = ThumbMoving.None;
595                                                 Refresh ();                     
596                                                 return;
597                                         } else {
598                                                 LargeIncrement ();
599                                 }
600                                 }
601                                 else
602                                         if ((vert && (thumb_pos.Y < lastclick_pos)) ||
603                                                 (!vert && (thumb_pos.X  < lastclick_pos))){
604                                                 timer.Enabled = false;
605                                                 thumb_moving = ThumbMoving.None;
606                                                 Refresh ();                                             
607                                         } else {
608                                                 LargeDecrement ();
609                                         }
610
611                                 break;
612                         }
613                         default:
614                                 break;
615                         }
616
617                         InvalidateDirty ();
618                 }               
619
620                 private void OnMouseMoveSB (object sender, MouseEventArgs e)
621                 {
622                         if (MouseMove != null) {
623                                 MouseMove (this, e);
624                         }
625                                 
626                         if (Enabled == false || thumb_size == 0)
627                                 return;
628
629                         if (firstbutton_pressed) {
630                                 if (!first_arrow_area.Contains (e.X, e.Y) && ((firstbutton_state & ButtonState.Pushed) == ButtonState.Pushed)) {
631                                         firstbutton_state = ButtonState.Normal;
632                                         Invalidate (first_arrow_area);
633                                         return;
634                                 } else if (first_arrow_area.Contains (e.X, e.Y) && ((firstbutton_state & ButtonState.Normal) == ButtonState.Normal)) {
635                                         firstbutton_state = ButtonState.Pushed;
636                                         Invalidate (first_arrow_area);
637                                         return;
638                                 }
639                         } else if (secondbutton_pressed) {
640                                 if (!second_arrow_area.Contains (e.X, e.Y) && ((secondbutton_state & ButtonState.Pushed) == ButtonState.Pushed)) {
641                                         secondbutton_state = ButtonState.Normal;
642                                         Invalidate (second_arrow_area);
643                                         return;
644                                 } else if (second_arrow_area.Contains (e.X, e.Y) && ((secondbutton_state & ButtonState.Normal) == ButtonState.Normal)) {
645                                         secondbutton_state = ButtonState.Pushed;
646                                         Invalidate (second_arrow_area);
647                                         return;
648                                 }
649                         } else if (thumb_pressed == true) {
650                                 int pixel_pos;
651
652                                 if (vert) {
653
654                                         int mouse_click = e.Y;
655                                         int outside_curpos = thumb_area.Y + thumb_area.Height - thumb_size + lastclick_pos_thumb;
656                                         
657                                         
658                                         if (mouse_click > thumb_area.Y + thumb_area.Height) {
659                                                 outside_thumbarea_right = true;
660                                                 mouse_click = thumb_area.Y + thumb_area.Height;
661                                         }
662
663                                         if (mouse_click < thumb_area.Y) {
664                                                 outside_thumbarea_left = true;
665                                                 mouse_click = thumb_area.Y;
666                                         }
667
668                                         if (outside_thumbarea_right && mouse_click < outside_curpos) {
669                                                 outside_thumbarea_right = false;
670                                                 thumb_pixel_click_move_prev =
671                                                 thumb_pixel_click_move = outside_curpos;
672                                         }
673
674                                         if (outside_thumbarea_left && mouse_click > thumb_area.Y + lastclick_pos_thumb) {
675                                                 outside_thumbarea_left = false;
676                                                 thumb_pixel_click_move_prev =
677                                                 thumb_pixel_click_move = thumb_area.Y + lastclick_pos_thumb;
678                                         }
679
680                                         if (outside_thumbarea_right == false && outside_thumbarea_left == false) {
681                                                 pixel_pos = thumb_pos.Y + (thumb_pixel_click_move - thumb_pixel_click_move_prev);
682                                                 thumb_pixel_click_move_prev = thumb_pixel_click_move;
683                                                 thumb_pixel_click_move = mouse_click;
684                                                 UpdateThumbPos (pixel_pos, true);
685                                         }
686
687                                 }
688                                 else {
689                                         int mouse_click = e.X;
690                                         int outside_curpos = thumb_area.X + thumb_area.Width - thumb_size + lastclick_pos_thumb;
691                                                                                 
692                                         if (mouse_click >  thumb_area.X + thumb_area.Width) {
693                                                 outside_thumbarea_right = true;
694                                                 mouse_click = thumb_area.X + thumb_area.Width;
695                                         }
696
697                                         if (mouse_click <  thumb_area.X) {
698                                                 outside_thumbarea_left = true;
699                                                 mouse_click = thumb_area.X;
700                                         }
701
702                                         if (outside_thumbarea_right && mouse_click < outside_curpos) {
703                                                 outside_thumbarea_right = false;
704                                                 thumb_pixel_click_move_prev =
705                                                 thumb_pixel_click_move = outside_curpos;
706                                         }
707
708                                         if (outside_thumbarea_left && mouse_click > thumb_area.X + lastclick_pos_thumb) {
709                                                 outside_thumbarea_left = false;
710                                                 thumb_pixel_click_move_prev =
711                                                 thumb_pixel_click_move = thumb_area.X + lastclick_pos_thumb;
712                                         }
713
714                                         if (outside_thumbarea_right == false && outside_thumbarea_left == false) {
715                                                 pixel_pos = thumb_pos.X + (thumb_pixel_click_move - thumb_pixel_click_move_prev);
716                                                 thumb_pixel_click_move_prev = thumb_pixel_click_move;
717                                                 thumb_pixel_click_move = mouse_click;
718                                                 UpdateThumbPos (pixel_pos, true);
719                                         }
720
721                                 }
722
723                                 Refresh ();
724                         }
725
726                 }
727
728                 private void OnMouseDownSB (object sender, MouseEventArgs e)
729                 {
730                         ClearDirty ();
731                         
732                         if (e.Button == MouseButtons.Right) {
733                                 if (MouseDown != null) {
734                                         MouseDown (this, e);
735                                 }
736                         }
737                         
738                         if (Enabled == false)
739                                 return;
740
741                         if (firstbutton_state != ButtonState.Inactive && first_arrow_area.Contains (e.X, e.Y)) {
742                                 this.Capture = true;                            
743                                 firstbutton_state = ButtonState.Pushed;
744                                 firstbutton_pressed = true;
745                                 Invalidate (first_arrow_area);
746                                 if (!timer.Enabled) {
747                                         SetHoldButtonClickTimer ();
748                                         timer.Enabled = true;
749                                 }
750                         }
751
752                         if (secondbutton_state != ButtonState.Inactive && second_arrow_area.Contains (e.X, e.Y)) {
753                                 this.Capture = true;                            
754                                 secondbutton_state = ButtonState.Pushed;
755                                 secondbutton_pressed = true;
756                                 Invalidate (second_arrow_area);
757                                 if (!timer.Enabled) {
758                                         SetHoldButtonClickTimer ();
759                                         timer.Enabled = true;
760                                 }
761                         }
762
763                         if (thumb_size > 0 && thumb_pos.Contains (e.X, e.Y)) {
764                                 thumb_pressed = true;
765                                 this.Capture = true;
766                                 if (vert) {
767                                         lastclick_pos_thumb = e.Y - thumb_pos.Y;
768                                         lastclick_pos = e.Y;                                    
769                                         thumb_pixel_click_move_prev = thumb_pixel_click_move = e.Y;
770                                 }
771                                 else {
772                                         lastclick_pos_thumb = e.X - thumb_pos.X;
773                                         lastclick_pos = e.X;
774                                         thumb_pixel_click_move_prev = thumb_pixel_click_move = e.X;
775                                 }
776                         } else {
777                                 if (thumb_size > 0 && thumb_area.Contains (e.X, e.Y)) {
778
779                                         if (vert) {
780                                                 lastclick_pos_thumb = e.Y - thumb_pos.Y;
781                                                 lastclick_pos = e.Y;
782
783                                                 if (e.Y > thumb_pos.Y + thumb_pos.Height) {
784                                                         LargeIncrement ();                                                      
785                                                         thumb_moving = ThumbMoving.Forward;                                                     
786                                                         Dirty (new Rectangle (0, thumb_pos.Y + thumb_pos.Height,
787                                                                                       ClientRectangle.Width,
788                                                                                       ClientRectangle.Height -  (thumb_pos.Y + thumb_pos.Height) -
789                                                                                       scrollbutton_height));
790                                                 } else {
791                                                         LargeDecrement ();                                                      
792                                                         thumb_moving = ThumbMoving.Backwards;
793                                                         Dirty (new Rectangle (0,  scrollbutton_height,
794                                                                                       ClientRectangle.Width,
795                                                                                       thumb_pos.Y - scrollbutton_height));
796                                                 }
797                                         } else {
798
799                                                 lastclick_pos_thumb = e.X - thumb_pos.X;
800                                                 lastclick_pos = e.X;
801
802                                                 if (e.X > thumb_pos.X + thumb_pos.Width) {
803                                                         thumb_moving = ThumbMoving.Forward;
804                                                         LargeIncrement ();                                                      
805                                                         Dirty (new Rectangle (thumb_pos.X + thumb_pos.Width, 0,
806                                                                                       ClientRectangle.Width -  (thumb_pos.X + thumb_pos.Width) -
807                                                                                       scrollbutton_width,
808                                                                                       ClientRectangle.Height));
809                                                 } else {
810                                                         thumb_moving = ThumbMoving.Backwards;
811                                                         LargeDecrement ();                                                      
812                                                         Dirty (new Rectangle (scrollbutton_width,  0,
813                                                                                       thumb_pos.X - scrollbutton_width,
814                                                                                       ClientRectangle.Height));
815                                                 }
816                                         }
817
818                                         SetHoldThumbAreaTimer ();
819                                         timer.Enabled = true;
820                                         InvalidateDirty ();
821                                 }
822                         }
823                 }
824                 
825                 private void OnMouseUpSB (object sender, MouseEventArgs e)
826                 {
827                         ClearDirty ();
828
829                         if (e.Button == MouseButtons.Right) {
830                                 if (MouseUp != null) {
831                                         MouseUp (this, e);
832                                 }
833                         }
834                         
835                         if (Enabled == false)
836                                 return;
837
838                         timer.Enabled = false;
839                         if (thumb_moving != ThumbMoving.None) {
840                                 DirtyThumbArea ();
841                                 thumb_moving = ThumbMoving.None;
842                         }                       
843                         this.Capture = false;
844
845                         if (firstbutton_pressed) {
846                                 firstbutton_state = ButtonState.Normal;
847                                 if (first_arrow_area.Contains (e.X, e.Y)) {
848                                         SmallDecrement ();
849                                 }
850                                 firstbutton_pressed = false;
851                                 Dirty (first_arrow_area);
852                         } else if (secondbutton_pressed) {
853                                 secondbutton_state = ButtonState.Normal;
854                                 if (second_arrow_area.Contains (e.X, e.Y)) {
855                                         SmallIncrement ();
856                                 }
857                                 Dirty (second_arrow_area);
858                                 secondbutton_pressed = false;
859                         } else if (thumb_pressed == true) {
860                                 OnScroll (new ScrollEventArgs (ScrollEventType.ThumbPosition, position));
861                                 OnScroll (new ScrollEventArgs (ScrollEventType.EndScroll, position));
862                                 thumb_pressed = false;
863                                 Refresh ();
864                                 return;
865                         }
866
867                         InvalidateDirty ();
868                 }
869
870                 private void OnKeyDownSB (Object o, KeyEventArgs key)
871                 {
872                         if (Enabled == false)
873                                 return;
874
875                         ClearDirty ();
876
877                         switch (key.KeyCode){
878                         case Keys.Up:
879                         {
880                                 SmallDecrement ();
881                                 break;
882                         }
883                         case Keys.Down:
884                         {
885                                 SmallIncrement ();
886                                 break;
887                         }
888                         case Keys.PageUp:
889                         {
890                                 LargeDecrement ();
891                                 break;
892                         }
893                         case Keys.PageDown:
894                         {
895                                 LargeIncrement ();
896                                 break;
897                         }
898                         case Keys.Home:
899                         {               
900                                 SetValue (0);
901                                 break;
902                         }                       
903                         case Keys.End:
904                         {       
905                                 SetValue (Maximum);
906                                 break;
907                         }
908                         default:
909                                 break;
910                         }
911
912                         InvalidateDirty ();
913                 }
914
915                 private void SmallIncrement ()
916                 {
917                         UpdatePos (position + small_change, true);
918
919                         OnScroll (new ScrollEventArgs (ScrollEventType.SmallIncrement, position));
920                         OnScroll (new ScrollEventArgs (ScrollEventType.EndScroll, position));
921                 }
922
923                 private void SmallDecrement ()
924                 {
925                         UpdatePos (position - small_change, true);
926
927                         OnScroll (new ScrollEventArgs (ScrollEventType.SmallDecrement, position));
928                         OnScroll (new ScrollEventArgs (ScrollEventType.EndScroll, position));
929                 }
930                 
931                 private void SetHoldButtonClickTimer ()
932                 {
933                         timer.Enabled = false;
934                         timer.Interval = 200;
935                         timer_type = TimerType.HoldButton;
936                         timer.Enabled = true;
937                 }
938
939                 private void SetRepeatButtonTimer ()
940                 {
941                         timer.Enabled = false;
942                         timer.Interval = 50;
943                         timer_type = TimerType.RepeatButton;
944                         timer.Enabled = true;
945                 }
946
947                 private void SetHoldThumbAreaTimer ()
948                 {
949                         timer.Enabled = false;
950                         timer.Interval = 200;
951                         timer_type = TimerType.HoldThumbArea;
952                         timer.Enabled = true;
953                 }
954
955                 private void SetRepeatThumbAreaTimer ()
956                 {
957                         timer.Enabled = false;
958                         timer.Interval = 50;
959                         timer_type = TimerType.RepeatThumbArea;
960                         timer.Enabled = true;
961                 }               
962                 
963                 private void UpdatePos (int newPos, bool update_thumbpos)
964                 {
965                         int old = position;
966                         int pos;
967
968                         if (newPos < minimum)
969                                 pos = minimum;
970                         else
971                                 if (newPos > maximum + 1 - large_change)
972                                         pos = maximum + 1 - large_change;
973                                         else
974                                                 pos = newPos;
975
976                         // pos can't be less than minimum
977                         if (pos < minimum)
978                                 pos = minimum;
979
980                         if (update_thumbpos) {
981                                 if (vert)
982                                         UpdateThumbPos (thumb_area.Y + (int)(((float)(pos - minimum)) * pixel_per_pos), false);
983                                 else
984                                         UpdateThumbPos (thumb_area.X + (int)(((float)(pos - minimum)) * pixel_per_pos), false);
985                                 SetValue (pos);
986                         }
987                         else {
988                                 position = pos; // Updates directly the value to avoid thumb pos update
989                                 
990                                 if (ValueChanged != null)
991                                         ValueChanged (this, EventArgs.Empty);
992                         }
993
994                         if (pos != old) // Fire event
995                                 OnScroll (new ScrollEventArgs (ScrollEventType.ThumbTrack, pos));
996
997                 }
998
999                 private void UpdateThumbPos (int pixel, bool update_value)
1000                 {
1001                         float new_pos = 0;
1002
1003                         if (vert) {
1004                                 Dirty (thumb_pos);
1005                                 if (pixel < thumb_area.Y)
1006                                         thumb_pos.Y = thumb_area.Y;
1007                                 else
1008                                         if (pixel > thumb_area.Y + thumb_area.Height - thumb_size)
1009                                                 thumb_pos.Y = thumb_area.Y +  thumb_area.Height - thumb_size;
1010                                         else
1011                                                 thumb_pos.Y = pixel;
1012
1013                                 thumb_pos.X = 0;
1014                                 thumb_pos.Width = ThemeEngine.Current.ScrollBarButtonSize;
1015                                 thumb_pos.Height = thumb_size;
1016                                 new_pos = (float) (thumb_pos.Y - thumb_area.Y);
1017                                 new_pos = new_pos / pixel_per_pos;
1018
1019                                 Dirty (thumb_pos);
1020                         } else  {
1021                                 Dirty (thumb_pos);
1022                                 if (pixel < thumb_area.X)
1023                                         thumb_pos.X = thumb_area.X;
1024                                 else
1025                                         if (pixel > thumb_area.X + thumb_area.Width - thumb_size)
1026                                                 thumb_pos.X = thumb_area.X +  thumb_area.Width - thumb_size;
1027                                         else
1028                                                 thumb_pos.X = pixel;
1029
1030                                 thumb_pos.Y = 0;
1031                                 thumb_pos.Width =  thumb_size;
1032                                 thumb_pos.Height = ThemeEngine.Current.ScrollBarButtonSize;
1033                                 new_pos = (float) (thumb_pos.X - thumb_area.X);
1034                                 new_pos = new_pos / pixel_per_pos;
1035
1036                                 Dirty (thumb_pos);
1037                         }
1038
1039                         if (update_value)
1040                                 UpdatePos ((int) new_pos + minimum, false);
1041                 }
1042
1043                 private void SetValue (int value)
1044                 {
1045                         if ( value < minimum || value > maximum )
1046                                 throw new ArgumentException(
1047                                         String.Format("'{0}' is not a valid value for 'Value'. 'Value' should be between 'Minimum' and 'Maximum'", value));
1048
1049                         if (position != value){
1050                                 position = value;
1051
1052                                 OnValueChanged (EventArgs.Empty);
1053                                 UpdatePos (value, true);
1054                         }
1055                 }
1056
1057                 private void ClearDirty ()
1058                 {
1059                         dirty = Rectangle.Empty;
1060                 }
1061
1062                 private void Dirty (Rectangle r)
1063                 {
1064                         if (dirty == Rectangle.Empty) {
1065                                 dirty = r;
1066                                 return;
1067                         }
1068                         dirty = Rectangle.Union (dirty, r);
1069                 }
1070
1071                 private void DirtyThumbArea ()
1072                 {
1073                         if (thumb_moving == ThumbMoving.Forward) {
1074                                 if (vert) {
1075                                         Dirty (new Rectangle (0, thumb_pos.Y + thumb_pos.Height,
1076                                                                       ClientRectangle.Width,
1077                                                                       ClientRectangle.Height -  (thumb_pos.Y + thumb_pos.Height) -
1078                                                                       scrollbutton_height));
1079                                 } else {
1080                                         Dirty (new Rectangle (thumb_pos.X + thumb_pos.Width, 0,
1081                                                                       ClientRectangle.Width -  (thumb_pos.X + thumb_pos.Width) -
1082                                                                       scrollbutton_width,
1083                                                                       ClientRectangle.Height));
1084                                 }
1085                         } else if (thumb_moving == ThumbMoving.Backwards) {
1086                                 if (vert) {
1087                                         Dirty(new Rectangle (0,  scrollbutton_height,
1088                                                                       ClientRectangle.Width,
1089                                                                       thumb_pos.Y - scrollbutton_height));
1090                                 } else {
1091                                         Dirty (new Rectangle (scrollbutton_width,  0,
1092                                                                       thumb_pos.X - scrollbutton_width,
1093                                                                       ClientRectangle.Height));
1094                                 }
1095                         }
1096                 }
1097
1098                 private void InvalidateDirty ()
1099                 {
1100                         Invalidate (dirty);
1101                         dirty = Rectangle.Empty;
1102                 }
1103
1104                 #endregion //Private Methods
1105          }
1106 }
1107
1108