msacm32: Update Korean resource.
[wine] / dlls / user32 / scroll.c
1 /*
2  * Scrollbar control
3  *
4  * Copyright 1993 Martin Ayotte
5  * Copyright 1994, 1996 Alexandre Julliard
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  *
21  * NOTES
22  *
23  * This code was audited for completeness against the documented features
24  * of Comctl32.dll version 6.0 on Oct. 8, 2004, by Dimitrie O. Paun.
25  * 
26  * Unless otherwise noted, we believe this code to be complete, as per
27  * the specification mentioned above.
28  * If you discover missing features, or bugs, please note them below.
29  */
30
31 #include <stdarg.h>
32
33 #include "windef.h"
34 #include "winbase.h"
35 #include "wingdi.h"
36 #include "controls.h"
37 #include "win.h"
38 #include "wine/debug.h"
39 #include "user_private.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(scroll);
42
43 /* data for a single scroll bar */
44 typedef struct
45 {
46     INT   curVal;   /* Current scroll-bar value */
47     INT   minVal;   /* Minimum scroll-bar value */
48     INT   maxVal;   /* Maximum scroll-bar value */
49     INT   page;     /* Page size of scroll bar (Win32) */
50     UINT  flags;    /* EnableScrollBar flags */
51 } SCROLLBAR_INFO, *LPSCROLLBAR_INFO;
52
53 /* data for window that has (one or two) scroll bars */
54 typedef struct
55 {
56     SCROLLBAR_INFO horz;
57     SCROLLBAR_INFO vert;
58 } WINSCROLLBAR_INFO, *LPWINSCROLLBAR_INFO;
59
60   /* Minimum size of the rectangle between the arrows */
61 #define SCROLL_MIN_RECT  4
62
63   /* Minimum size of the thumb in pixels */
64 #define SCROLL_MIN_THUMB 6
65
66   /* Overlap between arrows and thumb */
67 #define SCROLL_ARROW_THUMB_OVERLAP 0
68
69   /* Delay (in ms) before first repetition when holding the button down */
70 #define SCROLL_FIRST_DELAY   200
71
72   /* Delay (in ms) between scroll repetitions */
73 #define SCROLL_REPEAT_DELAY  50
74
75   /* Scroll timer id */
76 #define SCROLL_TIMER   0
77
78   /* Scroll-bar hit testing */
79 enum SCROLL_HITTEST
80 {
81     SCROLL_NOWHERE,      /* Outside the scroll bar */
82     SCROLL_TOP_ARROW,    /* Top or left arrow */
83     SCROLL_TOP_RECT,     /* Rectangle between the top arrow and the thumb */
84     SCROLL_THUMB,        /* Thumb rectangle */
85     SCROLL_BOTTOM_RECT,  /* Rectangle between the thumb and the bottom arrow */
86     SCROLL_BOTTOM_ARROW  /* Bottom or right arrow */
87 };
88
89  /* What to do after SCROLL_SetScrollInfo() */
90 #define SA_SSI_HIDE             0x0001
91 #define SA_SSI_SHOW             0x0002
92 #define SA_SSI_REFRESH          0x0004
93 #define SA_SSI_REPAINT_ARROWS   0x0008
94
95  /* Thumb-tracking info */
96 static HWND SCROLL_TrackingWin = 0;
97 static INT  SCROLL_TrackingBar = 0;
98 static INT  SCROLL_TrackingPos = 0;
99 static INT  SCROLL_TrackingVal = 0;
100  /* Hit test code of the last button-down event */
101 static enum SCROLL_HITTEST SCROLL_trackHitTest;
102 static BOOL SCROLL_trackVertical;
103
104  /* Is the moving thumb being displayed? */
105 static BOOL SCROLL_MovingThumb = FALSE;
106
107  /* Local functions */
108 static BOOL SCROLL_ShowScrollBar( HWND hwnd, INT nBar,
109                                     BOOL fShowH, BOOL fShowV );
110 static INT SCROLL_SetScrollInfo( HWND hwnd, INT nBar,
111                                  const SCROLLINFO *info, BOOL bRedraw );
112 static void SCROLL_DrawInterior_9x( HWND hwnd, HDC hdc, INT nBar,
113                                     RECT *rect, INT arrowSize,
114                                     INT thumbSize, INT thumbPos,
115                                     UINT flags, BOOL vertical,
116                                     BOOL top_selected, BOOL bottom_selected );
117
118
119 /*********************************************************************
120  * scrollbar class descriptor
121  */
122 static const WCHAR scrollbarW[] = {'S','c','r','o','l','l','B','a','r',0};
123 const struct builtin_class_descr SCROLL_builtin_class =
124 {
125     scrollbarW,             /* name */
126     CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC, /* style  */
127     WINPROC_SCROLLBAR,      /* proc */
128     sizeof(SCROLLBAR_INFO), /* extra */
129     IDC_ARROW,              /* cursor */
130     0                       /* brush */
131 };
132
133 /***********************************************************************
134  *           SCROLL_ScrollInfoValid
135  *
136  *  Determine if the supplied SCROLLINFO struct is valid.
137  *  info     [in] The SCROLLINFO struct to be tested
138  */
139 static inline BOOL SCROLL_ScrollInfoValid( LPCSCROLLINFO info )
140 {
141     return !(info->fMask & ~(SIF_ALL | SIF_DISABLENOSCROLL)
142         || (info->cbSize != sizeof(*info)
143             && info->cbSize != sizeof(*info) - sizeof(info->nTrackPos)));
144 }
145
146
147 /***********************************************************************
148  *           SCROLL_GetInternalInfo
149
150  * Returns pointer to internal SCROLLBAR_INFO structure for nBar
151  * or NULL if failed (f.i. scroll bar does not exist yet)
152  * If alloc is TRUE and the struct does not exist yet, create it.
153  */
154 static SCROLLBAR_INFO *SCROLL_GetInternalInfo( HWND hwnd, INT nBar, BOOL alloc )
155 {
156     SCROLLBAR_INFO *infoPtr = NULL;
157     WND *wndPtr = WIN_GetPtr( hwnd );
158
159     if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return NULL;
160     switch(nBar)
161     {
162         case SB_HORZ:
163             if (wndPtr->pScroll) infoPtr = &((LPWINSCROLLBAR_INFO)wndPtr->pScroll)->horz;
164             break;
165         case SB_VERT:
166             if (wndPtr->pScroll) infoPtr = &((LPWINSCROLLBAR_INFO)wndPtr->pScroll)->vert;
167             break;
168         case SB_CTL:
169             infoPtr = (SCROLLBAR_INFO *)wndPtr->wExtra;
170             break;
171         case SB_BOTH:
172             WARN("with SB_BOTH\n");
173             break;
174     }
175
176     if (!infoPtr && alloc)
177     {
178         WINSCROLLBAR_INFO *winInfoPtr;
179
180         if (nBar != SB_HORZ && nBar != SB_VERT)
181             WARN("Cannot initialize nBar=%d\n",nBar);
182         else if ((winInfoPtr = HeapAlloc( GetProcessHeap(), 0, sizeof(WINSCROLLBAR_INFO) )))
183         {
184             /* Set default values */
185             winInfoPtr->horz.minVal = 0;
186             winInfoPtr->horz.curVal = 0;
187             winInfoPtr->horz.page = 0;
188             /* From MSDN and our own tests:
189              * max for a standard scroll bar is 100 by default. */
190             winInfoPtr->horz.maxVal = 100;
191             winInfoPtr->horz.flags  = ESB_ENABLE_BOTH;
192             winInfoPtr->vert = winInfoPtr->horz;
193             wndPtr->pScroll = winInfoPtr;
194             infoPtr = nBar == SB_HORZ ? &winInfoPtr->horz : &winInfoPtr->vert;
195         }
196     }
197     WIN_ReleasePtr( wndPtr );
198     return infoPtr;
199 }
200
201
202 /***********************************************************************
203  *           SCROLL_GetScrollBarRect
204  *
205  * Compute the scroll bar rectangle, in drawing coordinates (i.e. client
206  * coords for SB_CTL, window coords for SB_VERT and SB_HORZ).
207  * 'arrowSize' returns the width or height of an arrow (depending on
208  * the orientation of the scrollbar), 'thumbSize' returns the size of
209  * the thumb, and 'thumbPos' returns the position of the thumb
210  * relative to the left or to the top.
211  * Return TRUE if the scrollbar is vertical, FALSE if horizontal.
212  */
213 static BOOL SCROLL_GetScrollBarRect( HWND hwnd, INT nBar, RECT *lprect,
214                                      INT *arrowSize, INT *thumbSize,
215                                      INT *thumbPos )
216 {
217     INT pixels;
218     BOOL vertical;
219     WND *wndPtr = WIN_GetPtr( hwnd );
220
221     if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
222
223     switch(nBar)
224     {
225       case SB_HORZ:
226         WIN_GetRectangles( hwnd, COORDS_WINDOW, NULL, lprect );
227         lprect->top = lprect->bottom;
228         lprect->bottom += GetSystemMetrics(SM_CYHSCROLL);
229         if(wndPtr->dwStyle & WS_VSCROLL)
230           lprect->right++;
231         vertical = FALSE;
232         break;
233
234       case SB_VERT:
235         WIN_GetRectangles( hwnd, COORDS_WINDOW, NULL, lprect );
236         if((wndPtr->dwExStyle & WS_EX_LEFTSCROLLBAR) != 0)
237         {
238             lprect->right = lprect->left;
239             lprect->left -= GetSystemMetrics(SM_CXVSCROLL);
240         }
241         else
242         {
243             lprect->left = lprect->right;
244             lprect->right += GetSystemMetrics(SM_CXVSCROLL);
245         }
246         if(wndPtr->dwStyle & WS_HSCROLL)
247           lprect->bottom++;
248         vertical = TRUE;
249         break;
250
251       case SB_CTL:
252         GetClientRect( hwnd, lprect );
253         vertical = ((wndPtr->dwStyle & SBS_VERT) != 0);
254         break;
255
256     default:
257         WIN_ReleasePtr( wndPtr );
258         return FALSE;
259     }
260
261     if (vertical) pixels = lprect->bottom - lprect->top;
262     else pixels = lprect->right - lprect->left;
263
264     if (pixels <= 2*GetSystemMetrics(SM_CXVSCROLL) + SCROLL_MIN_RECT)
265     {
266         if (pixels > SCROLL_MIN_RECT)
267             *arrowSize = (pixels - SCROLL_MIN_RECT) / 2;
268         else
269             *arrowSize = 0;
270         *thumbPos = *thumbSize = 0;
271     }
272     else
273     {
274         SCROLLBAR_INFO *info = SCROLL_GetInternalInfo( hwnd, nBar, FALSE );
275         if (!info)
276         {
277             WARN("called for missing scroll bar\n");
278             WIN_ReleasePtr( wndPtr );
279             return FALSE;
280         }
281         *arrowSize = GetSystemMetrics(SM_CXVSCROLL);
282         pixels -= (2 * (GetSystemMetrics(SM_CXVSCROLL) - SCROLL_ARROW_THUMB_OVERLAP));
283
284         if (info->page)
285         {
286             *thumbSize = MulDiv(pixels,info->page,(info->maxVal-info->minVal+1));
287             if (*thumbSize < SCROLL_MIN_THUMB) *thumbSize = SCROLL_MIN_THUMB;
288         }
289         else *thumbSize = GetSystemMetrics(SM_CXVSCROLL);
290
291         if (((pixels -= *thumbSize ) < 0) ||
292             ((info->flags & ESB_DISABLE_BOTH) == ESB_DISABLE_BOTH))
293         {
294             /* Rectangle too small or scrollbar disabled -> no thumb */
295             *thumbPos = *thumbSize = 0;
296         }
297         else
298         {
299             INT max = info->maxVal - max( info->page-1, 0 );
300             if (info->minVal >= max)
301                 *thumbPos = *arrowSize - SCROLL_ARROW_THUMB_OVERLAP;
302             else
303                 *thumbPos = *arrowSize - SCROLL_ARROW_THUMB_OVERLAP
304                   + MulDiv(pixels, (info->curVal-info->minVal),(max - info->minVal));
305         }
306     }
307     WIN_ReleasePtr( wndPtr );
308     return vertical;
309 }
310
311
312 /***********************************************************************
313  *           SCROLL_GetThumbVal
314  *
315  * Compute the current scroll position based on the thumb position in pixels
316  * from the top of the scroll-bar.
317  */
318 static UINT SCROLL_GetThumbVal( SCROLLBAR_INFO *infoPtr, RECT *rect,
319                                   BOOL vertical, INT pos )
320 {
321     INT thumbSize;
322     INT pixels = vertical ? rect->bottom-rect->top : rect->right-rect->left;
323     INT range;
324
325     if ((pixels -= 2*(GetSystemMetrics(SM_CXVSCROLL) - SCROLL_ARROW_THUMB_OVERLAP)) <= 0)
326         return infoPtr->minVal;
327
328     if (infoPtr->page)
329     {
330         thumbSize = MulDiv(pixels,infoPtr->page,(infoPtr->maxVal-infoPtr->minVal+1));
331         if (thumbSize < SCROLL_MIN_THUMB) thumbSize = SCROLL_MIN_THUMB;
332     }
333     else thumbSize = GetSystemMetrics(SM_CXVSCROLL);
334
335     if ((pixels -= thumbSize) <= 0) return infoPtr->minVal;
336
337     pos = max( 0, pos - (GetSystemMetrics(SM_CXVSCROLL) - SCROLL_ARROW_THUMB_OVERLAP) );
338     if (pos > pixels) pos = pixels;
339
340     if (!infoPtr->page)
341         range = infoPtr->maxVal - infoPtr->minVal;
342     else
343         range = infoPtr->maxVal - infoPtr->minVal - infoPtr->page + 1;
344
345     return infoPtr->minVal + MulDiv(pos, range, pixels);
346 }
347
348 /***********************************************************************
349  *           SCROLL_PtInRectEx
350  */
351 static BOOL SCROLL_PtInRectEx( LPRECT lpRect, POINT pt, BOOL vertical )
352 {
353     RECT rect = *lpRect;
354     int scrollbarWidth;
355
356     /* Pad hit rect to allow mouse to be dragged outside of scrollbar and
357      * still be considered in the scrollbar. */
358     if (vertical)
359     {
360         scrollbarWidth = lpRect->right - lpRect->left;
361         rect.left -= scrollbarWidth*8;
362         rect.right += scrollbarWidth*8;
363         rect.top -= scrollbarWidth*2;
364         rect.bottom += scrollbarWidth*2;
365     }
366     else
367     {
368         scrollbarWidth = lpRect->bottom - lpRect->top;
369         rect.left -= scrollbarWidth*2;
370         rect.right += scrollbarWidth*2;
371         rect.top -= scrollbarWidth*8;
372         rect.bottom += scrollbarWidth*8;
373     }
374     return PtInRect( &rect, pt );
375 }
376
377 /***********************************************************************
378  *           SCROLL_ClipPos
379  */
380 static POINT SCROLL_ClipPos( LPRECT lpRect, POINT pt )
381 {
382     if( pt.x < lpRect->left )
383         pt.x = lpRect->left;
384     else
385     if( pt.x > lpRect->right )
386         pt.x = lpRect->right;
387
388     if( pt.y < lpRect->top )
389         pt.y = lpRect->top;
390     else
391     if( pt.y > lpRect->bottom )
392         pt.y = lpRect->bottom;
393
394     return pt;
395 }
396
397
398 /***********************************************************************
399  *           SCROLL_HitTest
400  *
401  * Scroll-bar hit testing (don't confuse this with WM_NCHITTEST!).
402  */
403 static enum SCROLL_HITTEST SCROLL_HitTest( HWND hwnd, INT nBar,
404                                            POINT pt, BOOL bDragging )
405 {
406     INT arrowSize, thumbSize, thumbPos;
407     RECT rect;
408
409     BOOL vertical = SCROLL_GetScrollBarRect( hwnd, nBar, &rect,
410                                            &arrowSize, &thumbSize, &thumbPos );
411
412     if ( (bDragging && !SCROLL_PtInRectEx( &rect, pt, vertical )) ||
413          (!PtInRect( &rect, pt )) ) return SCROLL_NOWHERE;
414
415     if (vertical)
416     {
417         if (pt.y < rect.top + arrowSize) return SCROLL_TOP_ARROW;
418         if (pt.y >= rect.bottom - arrowSize) return SCROLL_BOTTOM_ARROW;
419         if (!thumbPos) return SCROLL_TOP_RECT;
420         pt.y -= rect.top;
421         if (pt.y < thumbPos) return SCROLL_TOP_RECT;
422         if (pt.y >= thumbPos + thumbSize) return SCROLL_BOTTOM_RECT;
423     }
424     else  /* horizontal */
425     {
426         if (pt.x < rect.left + arrowSize) return SCROLL_TOP_ARROW;
427         if (pt.x >= rect.right - arrowSize) return SCROLL_BOTTOM_ARROW;
428         if (!thumbPos) return SCROLL_TOP_RECT;
429         pt.x -= rect.left;
430         if (pt.x < thumbPos) return SCROLL_TOP_RECT;
431         if (pt.x >= thumbPos + thumbSize) return SCROLL_BOTTOM_RECT;
432     }
433     return SCROLL_THUMB;
434 }
435
436
437 /***********************************************************************
438  *           SCROLL_DrawArrows
439  *
440  * Draw the scroll bar arrows.
441  */
442 static void SCROLL_DrawArrows( HDC hdc, SCROLLBAR_INFO *infoPtr,
443                                RECT *rect, INT arrowSize, BOOL vertical,
444                                BOOL top_pressed, BOOL bottom_pressed )
445 {
446   RECT r;
447
448   r = *rect;
449   if( vertical )
450     r.bottom = r.top + arrowSize;
451   else
452     r.right = r.left + arrowSize;
453
454   DrawFrameControl( hdc, &r, DFC_SCROLL,
455                     (vertical ? DFCS_SCROLLUP : DFCS_SCROLLLEFT)
456                     | (top_pressed ? (DFCS_PUSHED | DFCS_FLAT) : 0 )
457                     | (infoPtr->flags&ESB_DISABLE_LTUP ? DFCS_INACTIVE : 0 ) );
458
459   r = *rect;
460   if( vertical )
461     r.top = r.bottom-arrowSize;
462   else
463     r.left = r.right-arrowSize;
464
465   DrawFrameControl( hdc, &r, DFC_SCROLL,
466                     (vertical ? DFCS_SCROLLDOWN : DFCS_SCROLLRIGHT)
467                     | (bottom_pressed ? (DFCS_PUSHED | DFCS_FLAT) : 0 )
468                     | (infoPtr->flags&ESB_DISABLE_RTDN ? DFCS_INACTIVE : 0) );
469 }
470
471 static void SCROLL_DrawMovingThumb( HDC hdc, RECT *rect, BOOL vertical,
472                                     INT arrowSize, INT thumbSize )
473 {
474   INT pos = SCROLL_TrackingPos;
475   INT max_size;
476
477   if( vertical )
478     max_size = rect->bottom - rect->top;
479   else
480     max_size = rect->right - rect->left;
481
482   max_size -= (arrowSize-SCROLL_ARROW_THUMB_OVERLAP) + thumbSize;
483
484   if( pos < (arrowSize-SCROLL_ARROW_THUMB_OVERLAP) )
485     pos = (arrowSize-SCROLL_ARROW_THUMB_OVERLAP);
486   else if( pos > max_size )
487     pos = max_size;
488
489   SCROLL_DrawInterior_9x( SCROLL_TrackingWin, hdc, SCROLL_TrackingBar,
490                           rect, arrowSize, thumbSize, pos,
491                           0, vertical, FALSE, FALSE );
492
493   SCROLL_MovingThumb = !SCROLL_MovingThumb;
494 }
495
496 /***********************************************************************
497  *           SCROLL_DrawInterior
498  *
499  * Draw the scroll bar interior (everything except the arrows).
500  */
501 static void SCROLL_DrawInterior_9x( HWND hwnd, HDC hdc, INT nBar,
502                                     RECT *rect, INT arrowSize,
503                                     INT thumbSize, INT thumbPos,
504                                     UINT flags, BOOL vertical,
505                                     BOOL top_selected, BOOL bottom_selected )
506 {
507     RECT r;
508     HPEN hSavePen;
509     HBRUSH hSaveBrush,hBrush;
510
511     /* Only scrollbar controls send WM_CTLCOLORSCROLLBAR.
512      * The window-owned scrollbars need to call DEFWND_ControlColor
513      * to correctly setup default scrollbar colors
514      */
515     if (nBar == SB_CTL)
516     {
517       hBrush = (HBRUSH)SendMessageW( GetParent(hwnd), WM_CTLCOLORSCROLLBAR,
518                                      (WPARAM)hdc,(LPARAM)hwnd);
519     }
520     else
521     {
522       hBrush = DEFWND_ControlColor( hdc, CTLCOLOR_SCROLLBAR );
523     }
524
525     hSavePen = SelectObject( hdc, SYSCOLOR_GetPen(COLOR_WINDOWFRAME) );
526     hSaveBrush = SelectObject( hdc, hBrush );
527
528     /* Calculate the scroll rectangle */
529     r = *rect;
530     if (vertical)
531     {
532         r.top    += arrowSize - SCROLL_ARROW_THUMB_OVERLAP;
533         r.bottom -= (arrowSize - SCROLL_ARROW_THUMB_OVERLAP);
534     }
535     else
536     {
537         r.left  += arrowSize - SCROLL_ARROW_THUMB_OVERLAP;
538         r.right -= (arrowSize - SCROLL_ARROW_THUMB_OVERLAP);
539     }
540
541     /* Draw the scroll rectangles and thumb */
542     if (!thumbPos)  /* No thumb to draw */
543     {
544         PatBlt( hdc, r.left, r.top,
545                      r.right - r.left, r.bottom - r.top,
546                      PATCOPY );
547
548         /* cleanup and return */
549         SelectObject( hdc, hSavePen );
550         SelectObject( hdc, hSaveBrush );
551         return;
552     }
553
554     if (vertical)
555     {
556         PatBlt( hdc, r.left, r.top,
557                   r.right - r.left,
558                   thumbPos - (arrowSize - SCROLL_ARROW_THUMB_OVERLAP),
559                   top_selected ? 0x0f0000 : PATCOPY );
560         r.top += thumbPos - (arrowSize - SCROLL_ARROW_THUMB_OVERLAP);
561         PatBlt( hdc, r.left, r.top + thumbSize,
562                   r.right - r.left,
563                   r.bottom - r.top - thumbSize,
564                   bottom_selected ? 0x0f0000 : PATCOPY );
565         r.bottom = r.top + thumbSize;
566     }
567     else  /* horizontal */
568     {
569         PatBlt( hdc, r.left, r.top,
570                   thumbPos - (arrowSize - SCROLL_ARROW_THUMB_OVERLAP),
571                   r.bottom - r.top,
572                   top_selected ? 0x0f0000 : PATCOPY );
573         r.left += thumbPos - (arrowSize - SCROLL_ARROW_THUMB_OVERLAP);
574         PatBlt( hdc, r.left + thumbSize, r.top,
575                   r.right - r.left - thumbSize,
576                   r.bottom - r.top,
577                   bottom_selected ? 0x0f0000 : PATCOPY );
578         r.right = r.left + thumbSize;
579     }
580
581     /* Draw the thumb */
582     DrawEdge( hdc, &r, EDGE_RAISED, BF_RECT | BF_MIDDLE  );
583
584     /* cleanup */
585     SelectObject( hdc, hSavePen );
586     SelectObject( hdc, hSaveBrush );
587 }
588
589
590 static void SCROLL_DrawInterior( HWND hwnd, HDC hdc, INT nBar,
591                                  RECT *rect, INT arrowSize,
592                                  INT thumbSize, INT thumbPos,
593                                  UINT flags, BOOL vertical,
594                                  BOOL top_selected, BOOL bottom_selected )
595 {
596     RECT r;
597     HPEN hSavePen;
598     HBRUSH hSaveBrush,hBrush;
599     BOOL Save_SCROLL_MovingThumb = SCROLL_MovingThumb;
600
601     if (Save_SCROLL_MovingThumb &&
602         (SCROLL_TrackingWin == hwnd) &&
603         (SCROLL_TrackingBar == nBar))
604         SCROLL_DrawMovingThumb( hdc, rect, vertical, arrowSize, thumbSize );
605
606       /* Select the correct brush and pen */
607
608     /* Only scrollbar controls send WM_CTLCOLORSCROLLBAR.
609      * The window-owned scrollbars need to call DEFWND_ControlColor
610      * to correctly setup default scrollbar colors
611      */
612     if (nBar == SB_CTL) {
613         hBrush = (HBRUSH)SendMessageW( GetParent(hwnd), WM_CTLCOLORSCROLLBAR,
614                                        (WPARAM)hdc,(LPARAM)hwnd);
615     } else {
616         hBrush = DEFWND_ControlColor( hdc, CTLCOLOR_SCROLLBAR );
617     }
618     hSavePen = SelectObject( hdc, SYSCOLOR_GetPen(COLOR_WINDOWFRAME) );
619     hSaveBrush = SelectObject( hdc, hBrush );
620
621       /* Calculate the scroll rectangle */
622
623     r = *rect;
624     if (vertical)
625     {
626         r.top    += arrowSize - SCROLL_ARROW_THUMB_OVERLAP;
627         r.bottom -= (arrowSize - SCROLL_ARROW_THUMB_OVERLAP);
628     }
629     else
630     {
631         r.left  += arrowSize - SCROLL_ARROW_THUMB_OVERLAP;
632         r.right -= (arrowSize - SCROLL_ARROW_THUMB_OVERLAP);
633     }
634
635       /* Draw the scroll bar frame */
636
637       /* Draw the scroll rectangles and thumb */
638
639     if (!thumbPos)  /* No thumb to draw */
640     {
641         PatBlt( hdc, r.left, r.top, r.right - r.left, r.bottom - r.top, PATCOPY );
642
643         /* cleanup and return */
644         SelectObject( hdc, hSavePen );
645         SelectObject( hdc, hSaveBrush );
646         return;
647     }
648
649     if (vertical)
650     {
651         PatBlt( hdc, r.left, r.top, r.right - r.left,
652                 thumbPos - (arrowSize - SCROLL_ARROW_THUMB_OVERLAP),
653                 top_selected ? 0x0f0000 : PATCOPY );
654         r.top += thumbPos - (arrowSize - SCROLL_ARROW_THUMB_OVERLAP);
655         PatBlt( hdc, r.left, r.top + thumbSize, r.right - r.left,
656                 r.bottom - r.top - thumbSize,
657                 bottom_selected ? 0x0f0000 : PATCOPY );
658         r.bottom = r.top + thumbSize;
659     }
660     else  /* horizontal */
661     {
662         PatBlt( hdc, r.left, r.top,
663                 thumbPos - (arrowSize - SCROLL_ARROW_THUMB_OVERLAP),
664                 r.bottom - r.top, top_selected ? 0x0f0000 : PATCOPY );
665         r.left += thumbPos - (arrowSize - SCROLL_ARROW_THUMB_OVERLAP);
666         PatBlt( hdc, r.left + thumbSize, r.top, r.right - r.left - thumbSize,
667                 r.bottom - r.top, bottom_selected ? 0x0f0000 : PATCOPY );
668         r.right = r.left + thumbSize;
669     }
670
671       /* Draw the thumb */
672
673     SelectObject( hdc, GetSysColorBrush(COLOR_BTNFACE) );
674     Rectangle( hdc, r.left+1, r.top+1, r.right-1, r.bottom-1 );
675     DrawEdge( hdc, &r, EDGE_RAISED, BF_RECT );
676
677     if (Save_SCROLL_MovingThumb &&
678         (SCROLL_TrackingWin == hwnd) &&
679         (SCROLL_TrackingBar == nBar))
680         SCROLL_DrawMovingThumb( hdc, rect, vertical, arrowSize, thumbSize );
681
682     /* cleanup */
683     SelectObject( hdc, hSavePen );
684     SelectObject( hdc, hSaveBrush );
685 }
686
687
688 /***********************************************************************
689  *           SCROLL_DrawScrollBar
690  *
691  * Redraw the whole scrollbar.
692  */
693 void SCROLL_DrawScrollBar( HWND hwnd, HDC hdc, INT nBar,
694                            BOOL arrows, BOOL interior )
695 {
696     INT arrowSize, thumbSize, thumbPos;
697     RECT rect;
698     BOOL vertical;
699     SCROLLBAR_INFO *infoPtr = SCROLL_GetInternalInfo( hwnd, nBar, TRUE );
700     BOOL Save_SCROLL_MovingThumb = SCROLL_MovingThumb;
701     DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
702
703     if (!(hwnd = WIN_GetFullHandle( hwnd ))) return;
704
705     if (!infoPtr ||
706         ((nBar == SB_VERT) && !(style & WS_VSCROLL)) ||
707         ((nBar == SB_HORZ) && !(style & WS_HSCROLL))) return;
708     if (!WIN_IsWindowDrawable( hwnd, FALSE )) return;
709
710     vertical = SCROLL_GetScrollBarRect( hwnd, nBar, &rect,
711                                         &arrowSize, &thumbSize, &thumbPos );
712
713     /* do not draw if the scrollbar rectangle is empty */
714     if(IsRectEmpty(&rect)) return;
715
716     if (Save_SCROLL_MovingThumb &&
717         (SCROLL_TrackingWin == hwnd) &&
718         (SCROLL_TrackingBar == nBar))
719         SCROLL_DrawMovingThumb( hdc, &rect, vertical, arrowSize, thumbSize );
720
721       /* Draw the arrows */
722
723     if (arrows && arrowSize)
724     {
725         if( vertical == SCROLL_trackVertical && GetCapture() == hwnd )
726             SCROLL_DrawArrows( hdc, infoPtr, &rect, arrowSize, vertical,
727                                (SCROLL_trackHitTest == SCROLL_TOP_ARROW),
728                                (SCROLL_trackHitTest == SCROLL_BOTTOM_ARROW) );
729         else
730             SCROLL_DrawArrows( hdc, infoPtr, &rect, arrowSize, vertical,
731                                                                FALSE, FALSE );
732     }
733     if( interior )
734         SCROLL_DrawInterior( hwnd, hdc, nBar, &rect, arrowSize, thumbSize,
735                          thumbPos, infoPtr->flags, vertical, FALSE, FALSE );
736
737     if (Save_SCROLL_MovingThumb &&
738         (SCROLL_TrackingWin == hwnd) &&
739         (SCROLL_TrackingBar == nBar))
740         SCROLL_DrawMovingThumb( hdc, &rect, vertical, arrowSize, thumbSize );
741
742     /* if scroll bar has focus, reposition the caret */
743     if(hwnd==GetFocus() && (nBar==SB_CTL))
744     {
745         if (!vertical)
746         {
747             SetCaretPos(thumbPos+1, rect.top+1);
748         }
749         else
750         {
751             SetCaretPos(rect.top+1, thumbPos+1);
752         }
753     }
754 }
755
756 /***********************************************************************
757  *           SCROLL_DrawSizeGrip
758  *
759  *  Draw the size grip.
760  */
761 static void SCROLL_DrawSizeGrip( HWND hwnd,  HDC hdc)
762 {
763     RECT rc;
764
765     GetClientRect( hwnd, &rc );
766     FillRect( hdc, &rc, GetSysColorBrush(COLOR_SCROLLBAR) );
767     rc.left = max( rc.left, rc.right - GetSystemMetrics(SM_CXVSCROLL) - 1 );
768     rc.top  = max( rc.top, rc.bottom - GetSystemMetrics(SM_CYHSCROLL) - 1 );
769     DrawFrameControl( hdc, &rc, DFC_SCROLL, DFCS_SCROLLSIZEGRIP );
770 }
771
772
773 /***********************************************************************
774  *           SCROLL_RefreshScrollBar
775  *
776  * Repaint the scroll bar interior after a SetScrollRange() or
777  * SetScrollPos() call.
778  */
779 static void SCROLL_RefreshScrollBar( HWND hwnd, INT nBar,
780                                      BOOL arrows, BOOL interior )
781 {
782     HDC hdc = GetDCEx( hwnd, 0,
783                            DCX_CACHE | ((nBar == SB_CTL) ? 0 : DCX_WINDOW) );
784     if (!hdc) return;
785
786     SCROLL_DrawScrollBar( hwnd, hdc, nBar, arrows, interior );
787     ReleaseDC( hwnd, hdc );
788 }
789
790
791 /***********************************************************************
792  *           SCROLL_HandleKbdEvent
793  *
794  * Handle a keyboard event (only for SB_CTL scrollbars with focus).
795  *
796  * PARAMS
797  *    hwnd   [I] Handle of window with scrollbar(s)
798  *    wParam [I] Variable input including enable state
799  *    lParam [I] Variable input including input point
800  */
801 static void SCROLL_HandleKbdEvent(HWND hwnd, WPARAM wParam, LPARAM lParam)
802 {
803     TRACE("hwnd=%p wParam=%ld lParam=%ld\n", hwnd, wParam, lParam);
804
805     /* hide caret on first KEYDOWN to prevent flicker */
806     if ((lParam & PFD_DOUBLEBUFFER_DONTCARE) == 0)
807         HideCaret(hwnd);
808
809     switch(wParam)
810     {
811     case VK_PRIOR: wParam = SB_PAGEUP; break;
812     case VK_NEXT:  wParam = SB_PAGEDOWN; break;
813     case VK_HOME:  wParam = SB_TOP; break;
814     case VK_END:   wParam = SB_BOTTOM; break;
815     case VK_UP:    wParam = SB_LINEUP; break;
816     case VK_DOWN:  wParam = SB_LINEDOWN; break;
817     case VK_LEFT:  wParam = SB_LINEUP; break;
818     case VK_RIGHT: wParam = SB_LINEDOWN; break;
819     default: return;
820     }
821     SendMessageW(GetParent(hwnd),
822         ((GetWindowLongW( hwnd, GWL_STYLE ) & SBS_VERT) ?
823             WM_VSCROLL : WM_HSCROLL), wParam, (LPARAM)hwnd);
824 }
825
826
827 /***********************************************************************
828  *           SCROLL_HandleScrollEvent
829  *
830  * Handle a mouse or timer event for the scrollbar.
831  * 'pt' is the location of the mouse event in client (for SB_CTL) or
832  * windows coordinates.
833  */
834 static void SCROLL_HandleScrollEvent( HWND hwnd, INT nBar, UINT msg, POINT pt)
835 {
836       /* Previous mouse position for timer events */
837     static POINT prevPt;
838       /* Thumb position when tracking started. */
839     static UINT trackThumbPos;
840       /* Position in the scroll-bar of the last button-down event. */
841     static INT lastClickPos;
842       /* Position in the scroll-bar of the last mouse event. */
843     static INT lastMousePos;
844
845     enum SCROLL_HITTEST hittest;
846     HWND hwndOwner, hwndCtl;
847     BOOL vertical;
848     INT arrowSize, thumbSize, thumbPos;
849     RECT rect;
850     HDC hdc;
851
852     SCROLLBAR_INFO *infoPtr = SCROLL_GetInternalInfo( hwnd, nBar, FALSE );
853     if (!infoPtr) return;
854     if ((SCROLL_trackHitTest == SCROLL_NOWHERE) && (msg != WM_LBUTTONDOWN))
855                   return;
856
857     if (nBar == SB_CTL && (GetWindowLongW( hwnd, GWL_STYLE ) & (SBS_SIZEGRIP | SBS_SIZEBOX)))
858     {
859         switch(msg)
860         {
861             case WM_LBUTTONDOWN:  /* Initialise mouse tracking */
862                 HideCaret(hwnd);  /* hide caret while holding down LBUTTON */
863                 SetCapture( hwnd );
864                 prevPt = pt;
865                 SCROLL_trackHitTest  = hittest = SCROLL_THUMB;
866                 break;
867             case WM_MOUSEMOVE:
868                 GetClientRect(GetParent(GetParent(hwnd)),&rect);
869                 prevPt = pt;
870                 break;
871             case WM_LBUTTONUP:
872                 ReleaseCapture();
873                 SCROLL_trackHitTest  = hittest = SCROLL_NOWHERE;
874                 if (hwnd==GetFocus()) ShowCaret(hwnd);
875                 break;
876             case WM_SYSTIMER:
877                 pt = prevPt;
878                 break;
879         }
880         return;
881     }
882
883     hdc = GetDCEx( hwnd, 0, DCX_CACHE | ((nBar == SB_CTL) ? 0 : DCX_WINDOW));
884     vertical = SCROLL_GetScrollBarRect( hwnd, nBar, &rect,
885                                         &arrowSize, &thumbSize, &thumbPos );
886     hwndOwner = (nBar == SB_CTL) ? GetParent(hwnd) : hwnd;
887     hwndCtl   = (nBar == SB_CTL) ? hwnd : 0;
888
889     switch(msg)
890     {
891       case WM_LBUTTONDOWN:  /* Initialise mouse tracking */
892           HideCaret(hwnd);  /* hide caret while holding down LBUTTON */
893           SCROLL_trackVertical = vertical;
894           SCROLL_trackHitTest  = hittest = SCROLL_HitTest( hwnd, nBar, pt, FALSE );
895           lastClickPos  = vertical ? (pt.y - rect.top) : (pt.x - rect.left);
896           lastMousePos  = lastClickPos;
897           trackThumbPos = thumbPos;
898           prevPt = pt;
899           if (nBar == SB_CTL && (GetWindowLongW(hwnd, GWL_STYLE) & WS_TABSTOP)) SetFocus( hwnd );
900           SetCapture( hwnd );
901           break;
902
903       case WM_MOUSEMOVE:
904           hittest = SCROLL_HitTest( hwnd, nBar, pt, TRUE );
905           prevPt = pt;
906           break;
907
908       case WM_LBUTTONUP:
909           hittest = SCROLL_NOWHERE;
910           ReleaseCapture();
911           /* if scrollbar has focus, show back caret */
912           if (hwnd==GetFocus()) ShowCaret(hwnd);
913           break;
914
915       case WM_SYSTIMER:
916           pt = prevPt;
917           hittest = SCROLL_HitTest( hwnd, nBar, pt, FALSE );
918           break;
919
920       default:
921           return;  /* Should never happen */
922     }
923
924     TRACE("Event: hwnd=%p bar=%d msg=%s pt=%d,%d hit=%d\n",
925           hwnd, nBar, SPY_GetMsgName(msg,hwnd), pt.x, pt.y, hittest );
926
927     switch(SCROLL_trackHitTest)
928     {
929     case SCROLL_NOWHERE:  /* No tracking in progress */
930         break;
931
932     case SCROLL_TOP_ARROW:
933         SCROLL_DrawArrows( hdc, infoPtr, &rect, arrowSize, vertical,
934                            (hittest == SCROLL_trackHitTest), FALSE );
935         if (hittest == SCROLL_trackHitTest)
936         {
937             if ((msg == WM_LBUTTONDOWN) || (msg == WM_SYSTIMER))
938             {
939                 SendMessageW( hwndOwner, vertical ? WM_VSCROLL : WM_HSCROLL,
940                                 SB_LINEUP, (LPARAM)hwndCtl );
941             }
942
943             SetSystemTimer( hwnd, SCROLL_TIMER, (msg == WM_LBUTTONDOWN) ?
944                             SCROLL_FIRST_DELAY : SCROLL_REPEAT_DELAY, NULL );
945         }
946         else KillSystemTimer( hwnd, SCROLL_TIMER );
947         break;
948
949     case SCROLL_TOP_RECT:
950         SCROLL_DrawInterior( hwnd, hdc, nBar, &rect, arrowSize, thumbSize,
951                              thumbPos, infoPtr->flags, vertical,
952                              (hittest == SCROLL_trackHitTest), FALSE );
953         if (hittest == SCROLL_trackHitTest)
954         {
955             if ((msg == WM_LBUTTONDOWN) || (msg == WM_SYSTIMER))
956             {
957                 SendMessageW( hwndOwner, vertical ? WM_VSCROLL : WM_HSCROLL,
958                                 SB_PAGEUP, (LPARAM)hwndCtl );
959             }
960             SetSystemTimer( hwnd, SCROLL_TIMER, (msg == WM_LBUTTONDOWN) ?
961                               SCROLL_FIRST_DELAY : SCROLL_REPEAT_DELAY, NULL );
962         }
963         else KillSystemTimer( hwnd, SCROLL_TIMER );
964         break;
965
966     case SCROLL_THUMB:
967         if (msg == WM_LBUTTONDOWN)
968         {
969             SCROLL_TrackingWin = hwnd;
970             SCROLL_TrackingBar = nBar;
971             SCROLL_TrackingPos = trackThumbPos + lastMousePos - lastClickPos;
972             SCROLL_TrackingVal = SCROLL_GetThumbVal( infoPtr, &rect,
973                                                         vertical,
974                                                         SCROLL_TrackingPos );
975             if (!SCROLL_MovingThumb)
976                 SCROLL_DrawMovingThumb(hdc, &rect, vertical, arrowSize, thumbSize);
977         }
978         else if (msg == WM_LBUTTONUP)
979         {
980             if (SCROLL_MovingThumb)
981                 SCROLL_DrawMovingThumb(hdc, &rect, vertical, arrowSize, thumbSize);
982
983             SCROLL_DrawInterior( hwnd, hdc, nBar, &rect, arrowSize, thumbSize,
984                                  thumbPos, infoPtr->flags, vertical,
985                                  FALSE, FALSE );
986         }
987         else  /* WM_MOUSEMOVE */
988         {
989             INT pos;
990
991             if (!SCROLL_PtInRectEx( &rect, pt, vertical )) pos = lastClickPos;
992             else
993             {
994                 pt = SCROLL_ClipPos( &rect, pt );
995                 pos = vertical ? (pt.y - rect.top) : (pt.x - rect.left);
996             }
997             if ( (pos != lastMousePos) || (!SCROLL_MovingThumb) )
998             {
999                 if (SCROLL_MovingThumb)
1000                     SCROLL_DrawMovingThumb( hdc, &rect, vertical,
1001                                         arrowSize, thumbSize );
1002                 lastMousePos = pos;
1003                 SCROLL_TrackingPos = trackThumbPos + pos - lastClickPos;
1004                 SCROLL_TrackingVal = SCROLL_GetThumbVal( infoPtr, &rect,
1005                                                          vertical,
1006                                                          SCROLL_TrackingPos );
1007                 SendMessageW( hwndOwner, vertical ? WM_VSCROLL : WM_HSCROLL,
1008                                 MAKEWPARAM( SB_THUMBTRACK, SCROLL_TrackingVal),
1009                                 (LPARAM)hwndCtl );
1010                 if (!SCROLL_MovingThumb)
1011                     SCROLL_DrawMovingThumb( hdc, &rect, vertical,
1012                                         arrowSize, thumbSize );
1013             }
1014         }
1015         break;
1016
1017     case SCROLL_BOTTOM_RECT:
1018         SCROLL_DrawInterior( hwnd, hdc, nBar, &rect, arrowSize, thumbSize,
1019                              thumbPos, infoPtr->flags, vertical,
1020                              FALSE, (hittest == SCROLL_trackHitTest) );
1021         if (hittest == SCROLL_trackHitTest)
1022         {
1023             if ((msg == WM_LBUTTONDOWN) || (msg == WM_SYSTIMER))
1024             {
1025                 SendMessageW( hwndOwner, vertical ? WM_VSCROLL : WM_HSCROLL,
1026                                 SB_PAGEDOWN, (LPARAM)hwndCtl );
1027             }
1028             SetSystemTimer( hwnd, SCROLL_TIMER, (msg == WM_LBUTTONDOWN) ?
1029                               SCROLL_FIRST_DELAY : SCROLL_REPEAT_DELAY, NULL );
1030         }
1031         else KillSystemTimer( hwnd, SCROLL_TIMER );
1032         break;
1033
1034     case SCROLL_BOTTOM_ARROW:
1035         SCROLL_DrawArrows( hdc, infoPtr, &rect, arrowSize, vertical,
1036                            FALSE, (hittest == SCROLL_trackHitTest) );
1037         if (hittest == SCROLL_trackHitTest)
1038         {
1039             if ((msg == WM_LBUTTONDOWN) || (msg == WM_SYSTIMER))
1040             {
1041                 SendMessageW( hwndOwner, vertical ? WM_VSCROLL : WM_HSCROLL,
1042                                 SB_LINEDOWN, (LPARAM)hwndCtl );
1043             }
1044
1045             SetSystemTimer( hwnd, SCROLL_TIMER, (msg == WM_LBUTTONDOWN) ?
1046                             SCROLL_FIRST_DELAY : SCROLL_REPEAT_DELAY, NULL );
1047         }
1048         else KillSystemTimer( hwnd, SCROLL_TIMER );
1049         break;
1050     }
1051
1052     if (msg == WM_LBUTTONDOWN)
1053     {
1054
1055         if (hittest == SCROLL_THUMB)
1056         {
1057             UINT val = SCROLL_GetThumbVal( infoPtr, &rect, vertical,
1058                                  trackThumbPos + lastMousePos - lastClickPos );
1059             SendMessageW( hwndOwner, vertical ? WM_VSCROLL : WM_HSCROLL,
1060                             MAKEWPARAM( SB_THUMBTRACK, val ), (LPARAM)hwndCtl );
1061         }
1062     }
1063
1064     if (msg == WM_LBUTTONUP)
1065     {
1066         hittest = SCROLL_trackHitTest;
1067         SCROLL_trackHitTest = SCROLL_NOWHERE;  /* Terminate tracking */
1068
1069         if (hittest == SCROLL_THUMB)
1070         {
1071             UINT val = SCROLL_GetThumbVal( infoPtr, &rect, vertical,
1072                                  trackThumbPos + lastMousePos - lastClickPos );
1073             SendMessageW( hwndOwner, vertical ? WM_VSCROLL : WM_HSCROLL,
1074                             MAKEWPARAM( SB_THUMBPOSITION, val ), (LPARAM)hwndCtl );
1075         }
1076         /* SB_ENDSCROLL doesn't report thumb position */
1077         SendMessageW( hwndOwner, vertical ? WM_VSCROLL : WM_HSCROLL,
1078                           SB_ENDSCROLL, (LPARAM)hwndCtl );
1079
1080         /* Terminate tracking */
1081         SCROLL_TrackingWin = 0;
1082     }
1083
1084     ReleaseDC( hwnd, hdc );
1085 }
1086
1087
1088 /***********************************************************************
1089  *           SCROLL_TrackScrollBar
1090  *
1091  * Track a mouse button press on a scroll-bar.
1092  * pt is in screen-coordinates for non-client scroll bars.
1093  */
1094 void SCROLL_TrackScrollBar( HWND hwnd, INT scrollbar, POINT pt )
1095 {
1096     MSG msg;
1097     INT xoffset = 0, yoffset = 0;
1098
1099     if (scrollbar != SB_CTL)
1100     {
1101         RECT rect;
1102         WIN_GetRectangles( hwnd, COORDS_CLIENT, &rect, NULL );
1103         ScreenToClient( hwnd, &pt );
1104         pt.x -= rect.left;
1105         pt.y -= rect.top;
1106     }
1107
1108     SCROLL_HandleScrollEvent( hwnd, scrollbar, WM_LBUTTONDOWN, pt );
1109
1110     do
1111     {
1112         if (!GetMessageW( &msg, 0, 0, 0 )) break;
1113         if (CallMsgFilterW( &msg, MSGF_SCROLLBAR )) continue;
1114         if (msg.message == WM_LBUTTONUP ||
1115             msg.message == WM_MOUSEMOVE ||
1116             (msg.message == WM_SYSTIMER && msg.wParam == SCROLL_TIMER))
1117         {
1118             pt.x = (short)LOWORD(msg.lParam) + xoffset;
1119             pt.y = (short)HIWORD(msg.lParam) + yoffset;
1120             SCROLL_HandleScrollEvent( hwnd, scrollbar, msg.message, pt );
1121         }
1122         else
1123         {
1124             TranslateMessage( &msg );
1125             DispatchMessageW( &msg );
1126         }
1127         if (!IsWindow( hwnd ))
1128         {
1129             ReleaseCapture();
1130             break;
1131         }
1132     } while (msg.message != WM_LBUTTONUP && GetCapture() == hwnd);
1133 }
1134
1135
1136 /***********************************************************************
1137  *           SCROLL_CreateScrollBar
1138  *
1139  * Create a scroll bar
1140  *
1141  * PARAMS
1142  *    hwnd     [I] Handle of window with scrollbar(s)
1143  *    lpCreate [I] The style and place of the scroll bar
1144  */
1145 static void SCROLL_CreateScrollBar(HWND hwnd, LPCREATESTRUCTW lpCreate)
1146 {
1147     LPSCROLLBAR_INFO info = SCROLL_GetInternalInfo(hwnd, SB_CTL, TRUE);
1148     if (!info) return;
1149
1150     TRACE("hwnd=%p lpCreate=%p\n", hwnd, lpCreate);
1151
1152     if (lpCreate->style & WS_DISABLED)
1153     {
1154         info->flags = ESB_DISABLE_BOTH;
1155         TRACE("Created WS_DISABLED scrollbar\n");
1156     }
1157
1158
1159     if (lpCreate->style & (SBS_SIZEGRIP | SBS_SIZEBOX))
1160     {
1161         if (lpCreate->style & SBS_SIZEBOXTOPLEFTALIGN)
1162             MoveWindow( hwnd, lpCreate->x, lpCreate->y, GetSystemMetrics(SM_CXVSCROLL)+1,
1163                         GetSystemMetrics(SM_CYHSCROLL)+1, FALSE );
1164         else if(lpCreate->style & SBS_SIZEBOXBOTTOMRIGHTALIGN)
1165             MoveWindow( hwnd, lpCreate->x+lpCreate->cx-GetSystemMetrics(SM_CXVSCROLL)-1, 
1166                         lpCreate->y+lpCreate->cy-GetSystemMetrics(SM_CYHSCROLL)-1,
1167                         GetSystemMetrics(SM_CXVSCROLL)+1,
1168                         GetSystemMetrics(SM_CYHSCROLL)+1, FALSE );
1169     }
1170     else if (lpCreate->style & SBS_VERT)
1171     {
1172         if (lpCreate->style & SBS_LEFTALIGN)
1173             MoveWindow( hwnd, lpCreate->x, lpCreate->y,
1174                         GetSystemMetrics(SM_CXVSCROLL)+1, lpCreate->cy, FALSE );
1175         else if (lpCreate->style & SBS_RIGHTALIGN)
1176             MoveWindow( hwnd,
1177                         lpCreate->x+lpCreate->cx-GetSystemMetrics(SM_CXVSCROLL)-1,
1178                         lpCreate->y,
1179                         GetSystemMetrics(SM_CXVSCROLL)+1, lpCreate->cy, FALSE );
1180     }
1181     else  /* SBS_HORZ */
1182     {
1183         if (lpCreate->style & SBS_TOPALIGN)
1184             MoveWindow( hwnd, lpCreate->x, lpCreate->y,
1185                         lpCreate->cx, GetSystemMetrics(SM_CYHSCROLL)+1, FALSE );
1186         else if (lpCreate->style & SBS_BOTTOMALIGN)
1187             MoveWindow( hwnd,
1188                         lpCreate->x,
1189                         lpCreate->y+lpCreate->cy-GetSystemMetrics(SM_CYHSCROLL)-1,
1190                         lpCreate->cx, GetSystemMetrics(SM_CYHSCROLL)+1, FALSE );
1191     }
1192 }
1193
1194
1195 /*************************************************************************
1196  *           SCROLL_GetScrollInfo
1197  *
1198  * Internal helper for the API function
1199  *
1200  * PARAMS
1201  *    hwnd [I]  Handle of window with scrollbar(s)
1202  *    nBar [I]  One of SB_HORZ, SB_VERT, or SB_CTL
1203  *    info [IO] fMask specifies which values to retrieve
1204  *
1205  * RETURNS
1206  *    FALSE if requested field not filled (f.i. scroll bar does not exist)
1207  */
1208 static BOOL SCROLL_GetScrollInfo(HWND hwnd, INT nBar, LPSCROLLINFO info)
1209 {
1210     LPSCROLLBAR_INFO infoPtr;
1211
1212     /* handle invalid data structure */
1213     if (!SCROLL_ScrollInfoValid(info)
1214         || !(infoPtr = SCROLL_GetInternalInfo(hwnd, nBar, FALSE)))
1215             return FALSE;
1216
1217     /* fill in the desired scroll info structure */
1218     if (info->fMask & SIF_PAGE) info->nPage = infoPtr->page;
1219     if (info->fMask & SIF_POS) info->nPos = infoPtr->curVal;
1220     if ((info->fMask & SIF_TRACKPOS) && (info->cbSize == sizeof(*info)))
1221         info->nTrackPos = (SCROLL_TrackingWin == WIN_GetFullHandle(hwnd)) ? SCROLL_TrackingVal : infoPtr->curVal;
1222     if (info->fMask & SIF_RANGE)
1223     {
1224         info->nMin = infoPtr->minVal;
1225         info->nMax = infoPtr->maxVal;
1226     }
1227
1228     TRACE("cbSize %02x fMask %04x nMin %d nMax %d nPage %u nPos %d nTrackPos %d\n",
1229            info->cbSize, info->fMask, info->nMin, info->nMax, info->nPage,
1230            info->nPos, info->nTrackPos);
1231
1232     return (info->fMask & SIF_ALL) != 0;
1233 }
1234
1235
1236 /*************************************************************************
1237  *           SCROLL_GetScrollBarInfo
1238  *
1239  * Internal helper for the API function
1240  *
1241  * PARAMS
1242  *    hwnd     [I]  Handle of window with scrollbar(s)
1243  *    idObject [I]  One of OBJID_CLIENT, OBJID_HSCROLL, or OBJID_VSCROLL
1244  *    info     [IO] cbSize specifies the size of the structure
1245  *
1246  * RETURNS
1247  *    FALSE if failed
1248  */
1249 static BOOL SCROLL_GetScrollBarInfo(HWND hwnd, LONG idObject, LPSCROLLBARINFO info)
1250 {
1251     LPSCROLLBAR_INFO infoPtr;
1252     INT nBar;
1253     INT nDummy;
1254     DWORD style = GetWindowLongW(hwnd, GWL_STYLE);
1255     BOOL pressed;
1256     RECT rect;
1257
1258     switch (idObject)
1259     {
1260         case OBJID_CLIENT: nBar = SB_CTL; break;
1261         case OBJID_HSCROLL: nBar = SB_HORZ; break;
1262         case OBJID_VSCROLL: nBar = SB_VERT; break;
1263         default: return FALSE;
1264     }
1265
1266     /* handle invalid data structure */
1267     if (info->cbSize != sizeof(*info))
1268         return FALSE;
1269
1270     SCROLL_GetScrollBarRect(hwnd, nBar, &info->rcScrollBar, &nDummy,
1271                             &info->dxyLineButton, &info->xyThumbTop);
1272     /* rcScrollBar needs to be in screen coordinates */
1273     GetWindowRect(hwnd, &rect);
1274     OffsetRect(&info->rcScrollBar, rect.left, rect.top);
1275
1276     info->xyThumbBottom = info->xyThumbTop + info->dxyLineButton;
1277
1278     infoPtr = SCROLL_GetInternalInfo(hwnd, nBar, TRUE);
1279     if (!infoPtr)
1280         return FALSE;
1281
1282     /* Scroll bar state */
1283     info->rgstate[0] = 0;
1284     if ((nBar == SB_HORZ && !(style & WS_HSCROLL))
1285         || (nBar == SB_VERT && !(style & WS_VSCROLL)))
1286         info->rgstate[0] |= STATE_SYSTEM_INVISIBLE;
1287     if (infoPtr->minVal >= infoPtr->maxVal - max(infoPtr->page - 1, 0))
1288     {
1289         if (!(info->rgstate[0] & STATE_SYSTEM_INVISIBLE))
1290             info->rgstate[0] |= STATE_SYSTEM_UNAVAILABLE;
1291         else
1292             info->rgstate[0] |= STATE_SYSTEM_OFFSCREEN;
1293     }
1294     if (nBar == SB_CTL && !IsWindowEnabled(hwnd))
1295         info->rgstate[0] |= STATE_SYSTEM_UNAVAILABLE;
1296     
1297     pressed = ((nBar == SB_VERT) == SCROLL_trackVertical && GetCapture() == hwnd);
1298     
1299     /* Top/left arrow button state. MSDN says top/right, but I don't believe it */
1300     info->rgstate[1] = 0;
1301     if (pressed && SCROLL_trackHitTest == SCROLL_TOP_ARROW)
1302         info->rgstate[1] |= STATE_SYSTEM_PRESSED;
1303     if (infoPtr->flags & ESB_DISABLE_LTUP)
1304         info->rgstate[1] |= STATE_SYSTEM_UNAVAILABLE;
1305
1306     /* Page up/left region state. MSDN says up/right, but I don't believe it */
1307     info->rgstate[2] = 0;
1308     if (infoPtr->curVal == infoPtr->minVal)
1309         info->rgstate[2] |= STATE_SYSTEM_INVISIBLE;
1310     if (pressed && SCROLL_trackHitTest == SCROLL_TOP_RECT)
1311         info->rgstate[2] |= STATE_SYSTEM_PRESSED;
1312
1313     /* Thumb state */
1314     info->rgstate[3] = 0;
1315     if (pressed && SCROLL_trackHitTest == SCROLL_THUMB)
1316         info->rgstate[3] |= STATE_SYSTEM_PRESSED;
1317
1318     /* Page down/right region state. MSDN says down/left, but I don't believe it */
1319     info->rgstate[4] = 0;
1320     if (infoPtr->curVal >= infoPtr->maxVal - 1)
1321         info->rgstate[4] |= STATE_SYSTEM_INVISIBLE;
1322     if (pressed && SCROLL_trackHitTest == SCROLL_BOTTOM_RECT)
1323         info->rgstate[4] |= STATE_SYSTEM_PRESSED;
1324     
1325     /* Bottom/right arrow button state. MSDN says bottom/left, but I don't believe it */
1326     info->rgstate[5] = 0;
1327     if (pressed && SCROLL_trackHitTest == SCROLL_BOTTOM_ARROW)
1328         info->rgstate[5] |= STATE_SYSTEM_PRESSED;
1329     if (infoPtr->flags & ESB_DISABLE_RTDN)
1330         info->rgstate[5] |= STATE_SYSTEM_UNAVAILABLE;
1331         
1332     return TRUE;
1333 }
1334
1335
1336 /*************************************************************************
1337  *           SCROLL_GetScrollPos
1338  *
1339  *  Internal helper for the API function
1340  *
1341  * PARAMS
1342  *    hwnd [I]  Handle of window with scrollbar(s)
1343  *    nBar [I]  One of SB_HORZ, SB_VERT, or SB_CTL
1344  */
1345 static INT SCROLL_GetScrollPos(HWND hwnd, INT nBar)
1346 {
1347     LPSCROLLBAR_INFO infoPtr = SCROLL_GetInternalInfo(hwnd, nBar, FALSE);
1348     return infoPtr ? infoPtr->curVal: 0;
1349 }
1350
1351
1352 /*************************************************************************
1353  *           SCROLL_GetScrollRange
1354  *
1355  *  Internal helper for the API function
1356  *
1357  * PARAMS
1358  *    hwnd  [I]  Handle of window with scrollbar(s)
1359  *    nBar  [I]  One of SB_HORZ, SB_VERT, or SB_CTL
1360  *    lpMin [O]  Where to store minimum value
1361  *    lpMax [O]  Where to store maximum value
1362  *
1363  * RETURNS
1364  *    Success: TRUE
1365  *    Failure: FALSE
1366  */
1367 static BOOL SCROLL_GetScrollRange(HWND hwnd, INT nBar, LPINT lpMin, LPINT lpMax)
1368 {
1369     LPSCROLLBAR_INFO infoPtr = SCROLL_GetInternalInfo(hwnd, nBar, FALSE);
1370
1371     if (lpMin) *lpMin = infoPtr ? infoPtr->minVal : 0;
1372     if (lpMax) *lpMax = infoPtr ? infoPtr->maxVal : 0;
1373
1374     return TRUE;
1375 }
1376
1377
1378 /*************************************************************************
1379  *           SCROLL_SetScrollRange
1380  *
1381  * PARAMS
1382  *    hwnd  [I]  Handle of window with scrollbar(s)
1383  *    nBar  [I]  One of SB_HORZ, SB_VERT, or SB_CTL
1384  *    lpMin [I]  Minimum value
1385  *    lpMax [I]  Maximum value
1386  *
1387  */
1388 static BOOL SCROLL_SetScrollRange(HWND hwnd, INT nBar, INT minVal, INT maxVal)
1389 {
1390     LPSCROLLBAR_INFO infoPtr = SCROLL_GetInternalInfo(hwnd, nBar, FALSE);
1391
1392     TRACE("hwnd=%p nBar=%d min=%d max=%d\n", hwnd, nBar, minVal, maxVal);
1393
1394     if (infoPtr)
1395     {
1396         infoPtr->minVal = minVal;
1397         infoPtr->maxVal = maxVal;
1398     }
1399     return TRUE;
1400 }
1401
1402
1403 /***********************************************************************
1404  *           ScrollBarWndProc_common
1405  */
1406 LRESULT ScrollBarWndProc_common( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, BOOL unicode )
1407 {
1408     if (!IsWindow( hwnd )) return 0;
1409
1410     switch(message)
1411     {
1412     case WM_CREATE:
1413         SCROLL_CreateScrollBar(hwnd, (LPCREATESTRUCTW)lParam);
1414         break;
1415
1416     case WM_ENABLE:
1417         {
1418             SCROLLBAR_INFO *infoPtr;
1419             if ((infoPtr = SCROLL_GetInternalInfo( hwnd, SB_CTL, FALSE )))
1420             {
1421                 infoPtr->flags = wParam ? ESB_ENABLE_BOTH : ESB_DISABLE_BOTH;
1422                 SCROLL_RefreshScrollBar(hwnd, SB_CTL, TRUE, TRUE);
1423             }
1424         }
1425         return 0;
1426
1427     case WM_LBUTTONDBLCLK:
1428     case WM_LBUTTONDOWN:
1429         if (GetWindowLongW( hwnd, GWL_STYLE ) & SBS_SIZEGRIP)
1430         {
1431             SendMessageW( GetParent(hwnd), WM_SYSCOMMAND,
1432                           SC_SIZE + ((GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL) ?
1433                                      WMSZ_BOTTOMLEFT : WMSZ_BOTTOMRIGHT), lParam );
1434         }
1435         else
1436         {
1437             POINT pt;
1438             pt.x = (short)LOWORD(lParam);
1439             pt.y = (short)HIWORD(lParam);
1440             SCROLL_TrackScrollBar( hwnd, SB_CTL, pt );
1441         }
1442         break;
1443     case WM_LBUTTONUP:
1444     case WM_MOUSEMOVE:
1445     case WM_SYSTIMER:
1446         {
1447             POINT pt;
1448             pt.x = (short)LOWORD(lParam);
1449             pt.y = (short)HIWORD(lParam);
1450             SCROLL_HandleScrollEvent( hwnd, SB_CTL, message, pt );
1451         }
1452         break;
1453
1454     case WM_KEYDOWN:
1455         SCROLL_HandleKbdEvent(hwnd, wParam, lParam);
1456         break;
1457
1458     case WM_KEYUP:
1459         ShowCaret(hwnd);
1460         break;
1461
1462     case WM_SETFOCUS:
1463         {
1464             /* Create a caret when a ScrollBar get focus */
1465             RECT rect;
1466             int arrowSize, thumbSize, thumbPos, vertical;
1467             vertical = SCROLL_GetScrollBarRect( hwnd, SB_CTL, &rect,
1468                                                 &arrowSize, &thumbSize, &thumbPos );
1469             if (!vertical)
1470             {
1471                 CreateCaret(hwnd, (HBITMAP)1, thumbSize-2, rect.bottom-rect.top-2);
1472                 SetCaretPos(thumbPos+1, rect.top+1);
1473             }
1474             else
1475             {
1476                 CreateCaret(hwnd, (HBITMAP)1, rect.right-rect.left-2,thumbSize-2);
1477                 SetCaretPos(rect.top+1, thumbPos+1);
1478             }
1479             ShowCaret(hwnd);
1480         }
1481         break;
1482
1483     case WM_KILLFOCUS:
1484         {
1485             RECT rect;
1486             int arrowSize, thumbSize, thumbPos, vertical;
1487             vertical = SCROLL_GetScrollBarRect( hwnd, SB_CTL, &rect,&arrowSize, &thumbSize, &thumbPos );
1488             if (!vertical){
1489                 rect.left=thumbPos+1;
1490                 rect.right=rect.left+thumbSize;
1491             }
1492             else
1493             {
1494                 rect.top=thumbPos+1;
1495                 rect.bottom=rect.top+thumbSize;
1496             }
1497             HideCaret(hwnd);
1498             InvalidateRect(hwnd,&rect,0);
1499             DestroyCaret();
1500         }
1501         break;
1502
1503     case WM_ERASEBKGND:
1504          return 1;
1505
1506     case WM_GETDLGCODE:
1507          return DLGC_WANTARROWS; /* Windows returns this value */
1508
1509     case WM_PAINT:
1510         {
1511             PAINTSTRUCT ps;
1512             HDC hdc = wParam ? (HDC)wParam : BeginPaint(hwnd, &ps);
1513             if (GetWindowLongW( hwnd, GWL_STYLE ) & SBS_SIZEGRIP)
1514             {
1515                 SCROLL_DrawSizeGrip( hwnd, hdc);
1516             }
1517             else if (GetWindowLongW( hwnd, GWL_STYLE ) & SBS_SIZEBOX)
1518             {
1519                 RECT rc;
1520                 GetClientRect( hwnd, &rc );
1521                 FillRect( hdc, &rc, GetSysColorBrush(COLOR_SCROLLBAR) );
1522             }
1523             else
1524                 SCROLL_DrawScrollBar( hwnd, hdc, SB_CTL, TRUE, TRUE );
1525             if (!wParam) EndPaint(hwnd, &ps);
1526         }
1527         break;
1528
1529     case WM_SETCURSOR:
1530         if (GetWindowLongW( hwnd, GWL_STYLE ) & SBS_SIZEGRIP)
1531         {
1532             ULONG_PTR cursor = (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL) ? IDC_SIZENESW : IDC_SIZENWSE;
1533             return (LRESULT)SetCursor( LoadCursorA( 0, (LPSTR)cursor ));
1534         }
1535         return DefWindowProcW( hwnd, message, wParam, lParam );
1536
1537     case SBM_SETPOS:
1538         return SetScrollPos( hwnd, SB_CTL, wParam, (BOOL)lParam );
1539
1540     case SBM_GETPOS:
1541        return SCROLL_GetScrollPos(hwnd, SB_CTL);
1542
1543     case SBM_SETRANGEREDRAW:
1544     case SBM_SETRANGE:
1545         {
1546             INT oldPos = SCROLL_GetScrollPos( hwnd, SB_CTL );
1547             SCROLL_SetScrollRange( hwnd, SB_CTL, wParam, lParam );
1548             if (message == SBM_SETRANGEREDRAW)
1549                 SCROLL_RefreshScrollBar( hwnd, SB_CTL, TRUE, TRUE );
1550             if (oldPos != SCROLL_GetScrollPos( hwnd, SB_CTL )) return oldPos;
1551         }
1552         return 0;
1553
1554     case SBM_GETRANGE:
1555         return SCROLL_GetScrollRange(hwnd, SB_CTL, (LPINT)wParam, (LPINT)lParam);
1556
1557     case SBM_ENABLE_ARROWS:
1558         return EnableScrollBar( hwnd, SB_CTL, wParam );
1559
1560     case SBM_SETSCROLLINFO:
1561         return SCROLL_SetScrollInfo( hwnd, SB_CTL, (SCROLLINFO *)lParam, wParam );
1562
1563     case SBM_GETSCROLLINFO:
1564         return SCROLL_GetScrollInfo(hwnd, SB_CTL, (SCROLLINFO *)lParam);
1565
1566     case SBM_GETSCROLLBARINFO:
1567         return SCROLL_GetScrollBarInfo(hwnd, OBJID_CLIENT, (SCROLLBARINFO *)lParam);
1568
1569     case 0x00e5:
1570     case 0x00e7:
1571     case 0x00e8:
1572     case 0x00ec:
1573     case 0x00ed:
1574     case 0x00ee:
1575     case 0x00ef:
1576         ERR("unknown Win32 msg %04x wp=%08lx lp=%08lx\n",
1577                     message, wParam, lParam );
1578         break;
1579
1580     default:
1581         if (message >= WM_USER)
1582             WARN("unknown msg %04x wp=%04lx lp=%08lx\n",
1583                          message, wParam, lParam );
1584         if (unicode)
1585             return DefWindowProcW( hwnd, message, wParam, lParam );
1586         else
1587             return DefWindowProcA( hwnd, message, wParam, lParam );
1588     }
1589     return 0;
1590 }
1591
1592
1593 /*************************************************************************
1594  *           SetScrollInfo   (USER32.@)
1595  *
1596  * SetScrollInfo can be used to set the position, upper bound,
1597  * lower bound, and page size of a scrollbar control.
1598  *
1599  * PARAMS
1600  *    hwnd    [I]  Handle of window with scrollbar(s)
1601  *    nBar    [I]  One of SB_HORZ, SB_VERT, or SB_CTL
1602  *    info    [I]  Specifies what to change and new values
1603  *    bRedraw [I]  Should scrollbar be redrawn afterwards?
1604  *
1605  * RETURNS
1606  *    Scrollbar position
1607  *
1608  * NOTE
1609  *    For 100 lines of text to be displayed in a window of 25 lines,
1610  *  one would for instance use info->nMin=0, info->nMax=75
1611  *  (corresponding to the 76 different positions of the window on
1612  *  the text), and info->nPage=25.
1613  */
1614 INT WINAPI SetScrollInfo(HWND hwnd, INT nBar, const SCROLLINFO *info, BOOL bRedraw)
1615 {
1616     TRACE("hwnd=%p nBar=%d info=%p, bRedraw=%d\n", hwnd, nBar, info, bRedraw);
1617
1618     /* Refer SB_CTL requests to the window */
1619     if (nBar == SB_CTL)
1620         return SendMessageW(hwnd, SBM_SETSCROLLINFO, bRedraw, (LPARAM)info);
1621     else
1622         return SCROLL_SetScrollInfo( hwnd, nBar, info, bRedraw );
1623 }
1624
1625 static INT SCROLL_SetScrollInfo( HWND hwnd, INT nBar, LPCSCROLLINFO info, BOOL bRedraw )
1626 {
1627     /* Update the scrollbar state and set action flags according to
1628      * what has to be done graphics wise. */
1629
1630     SCROLLBAR_INFO *infoPtr;
1631     UINT new_flags;
1632     INT action = 0;
1633
1634     /* handle invalid data structure */
1635     if (!SCROLL_ScrollInfoValid(info)
1636         || !(infoPtr = SCROLL_GetInternalInfo(hwnd, nBar, TRUE)))
1637             return 0;
1638
1639     if (TRACE_ON(scroll))
1640     {
1641         TRACE("hwnd=%p bar=%d", hwnd, nBar);
1642         if (info->fMask & SIF_PAGE) TRACE( " page=%d", info->nPage );
1643         if (info->fMask & SIF_POS) TRACE( " pos=%d", info->nPos );
1644         if (info->fMask & SIF_RANGE) TRACE( " min=%d max=%d", info->nMin, info->nMax );
1645         TRACE("\n");
1646     }
1647
1648     /* Set the page size */
1649
1650     if (info->fMask & SIF_PAGE)
1651     {
1652         if( infoPtr->page != info->nPage )
1653         {
1654             infoPtr->page = info->nPage;
1655             action |= SA_SSI_REFRESH;
1656         }
1657     }
1658
1659     /* Set the scroll pos */
1660
1661     if (info->fMask & SIF_POS)
1662     {
1663         if( infoPtr->curVal != info->nPos )
1664         {
1665             infoPtr->curVal = info->nPos;
1666             action |= SA_SSI_REFRESH;
1667         }
1668     }
1669
1670     /* Set the scroll range */
1671
1672     if (info->fMask & SIF_RANGE)
1673     {
1674         /* Invalid range -> range is set to (0,0) */
1675         if ((info->nMin > info->nMax) ||
1676             ((UINT)(info->nMax - info->nMin) >= 0x80000000))
1677         {
1678             action |= SA_SSI_REFRESH;
1679             infoPtr->minVal = 0;
1680             infoPtr->maxVal = 0;
1681         }
1682         else
1683         {
1684             if( infoPtr->minVal != info->nMin ||
1685                 infoPtr->maxVal != info->nMax )
1686             {
1687                 action |= SA_SSI_REFRESH;
1688                 infoPtr->minVal = info->nMin;
1689                 infoPtr->maxVal = info->nMax;
1690             }
1691         }
1692     }
1693
1694     /* Make sure the page size is valid */
1695     if (infoPtr->page < 0) infoPtr->page = 0;
1696     else if (infoPtr->page > infoPtr->maxVal - infoPtr->minVal + 1 )
1697         infoPtr->page = infoPtr->maxVal - infoPtr->minVal + 1;
1698
1699     /* Make sure the pos is inside the range */
1700
1701     if (infoPtr->curVal < infoPtr->minVal)
1702         infoPtr->curVal = infoPtr->minVal;
1703     else if (infoPtr->curVal > infoPtr->maxVal - max( infoPtr->page-1, 0 ))
1704         infoPtr->curVal = infoPtr->maxVal - max( infoPtr->page-1, 0 );
1705
1706     TRACE("    new values: page=%d pos=%d min=%d max=%d\n",
1707                  infoPtr->page, infoPtr->curVal,
1708                  infoPtr->minVal, infoPtr->maxVal );
1709
1710     /* don't change the scrollbar state if SetScrollInfo
1711      * is just called with SIF_DISABLENOSCROLL
1712      */
1713     if(!(info->fMask & SIF_ALL)) goto done;
1714
1715     /* Check if the scrollbar should be hidden or disabled */
1716
1717     if (info->fMask & (SIF_RANGE | SIF_PAGE | SIF_DISABLENOSCROLL))
1718     {
1719         new_flags = infoPtr->flags;
1720         if (infoPtr->minVal >= infoPtr->maxVal - max( infoPtr->page-1, 0 ))
1721         {
1722             /* Hide or disable scroll-bar */
1723             if (info->fMask & SIF_DISABLENOSCROLL)
1724             {
1725                 new_flags = ESB_DISABLE_BOTH;
1726                 action |= SA_SSI_REFRESH;
1727             }
1728             else if ((nBar != SB_CTL) && (action & SA_SSI_REFRESH))
1729             {
1730                 action = SA_SSI_HIDE;
1731             }
1732         }
1733         else  /* Show and enable scroll-bar only if no page only changed. */
1734         if (info->fMask != SIF_PAGE)
1735         {
1736             new_flags = ESB_ENABLE_BOTH;
1737             if ((nBar != SB_CTL) && ( (action & SA_SSI_REFRESH) ))
1738                 action |= SA_SSI_SHOW;
1739         }
1740
1741         if (infoPtr->flags != new_flags) /* check arrow flags */
1742         {
1743             infoPtr->flags = new_flags;
1744             action |= SA_SSI_REPAINT_ARROWS;
1745         }
1746     }
1747
1748 done:
1749     if( action & SA_SSI_HIDE )
1750         SCROLL_ShowScrollBar( hwnd, nBar, FALSE, FALSE );
1751     else
1752     {
1753         if( action & SA_SSI_SHOW )
1754             if( SCROLL_ShowScrollBar( hwnd, nBar, TRUE, TRUE ) )
1755                 return infoPtr->curVal; /* SetWindowPos() already did the painting */
1756
1757         if( bRedraw )
1758             SCROLL_RefreshScrollBar( hwnd, nBar, TRUE, TRUE );
1759         else if( action & SA_SSI_REPAINT_ARROWS )
1760             SCROLL_RefreshScrollBar( hwnd, nBar, TRUE, FALSE );
1761     }
1762
1763     /* Return current position */
1764     return infoPtr->curVal;
1765 }
1766
1767
1768 /*************************************************************************
1769  *           GetScrollInfo   (USER32.@)
1770  *
1771  * GetScrollInfo can be used to retrieve the position, upper bound,
1772  * lower bound, and page size of a scrollbar control.
1773  *
1774  * PARAMS
1775  *  hwnd [I]  Handle of window with scrollbar(s)
1776  *  nBar [I]  One of SB_HORZ, SB_VERT, or SB_CTL
1777  *  info [IO] fMask specifies which values to retrieve
1778  *
1779  * RETURNS
1780  *  TRUE if SCROLLINFO is filled
1781  *  ( if nBar is SB_CTL, GetScrollInfo returns TRUE even if nothing
1782  *  is filled)
1783  */
1784 BOOL WINAPI GetScrollInfo(HWND hwnd, INT nBar, LPSCROLLINFO info)
1785 {
1786     TRACE("hwnd=%p nBar=%d info=%p\n", hwnd, nBar, info);
1787
1788     /* Refer SB_CTL requests to the window */
1789     if (nBar == SB_CTL)
1790     {
1791         SendMessageW(hwnd, SBM_GETSCROLLINFO, 0, (LPARAM)info);
1792         return TRUE;
1793     }
1794     return SCROLL_GetScrollInfo(hwnd, nBar, info);
1795 }
1796
1797
1798 /*************************************************************************
1799  *           GetScrollBarInfo   (USER32.@)
1800  *
1801  * GetScrollBarInfo can be used to retrieve information about a scrollbar
1802  * control.
1803  *
1804  * PARAMS
1805  *  hwnd     [I]  Handle of window with scrollbar(s)
1806  *  idObject [I]  One of OBJID_CLIENT, OBJID_HSCROLL, or OBJID_VSCROLL
1807  *  info     [IO] cbSize specifies the size of SCROLLBARINFO
1808  *
1809  * RETURNS
1810  *  TRUE if success
1811  */
1812 BOOL WINAPI GetScrollBarInfo(HWND hwnd, LONG idObject, LPSCROLLBARINFO info)
1813 {
1814     TRACE("hwnd=%p idObject=%d info=%p\n", hwnd, idObject, info);
1815
1816     /* Refer OBJID_CLIENT requests to the window */
1817     if (idObject == OBJID_CLIENT)
1818         return SendMessageW(hwnd, SBM_GETSCROLLBARINFO, 0, (LPARAM)info);
1819     else
1820         return SCROLL_GetScrollBarInfo(hwnd, idObject, info);
1821 }
1822
1823
1824 /*************************************************************************
1825  *           SetScrollPos   (USER32.@)
1826  *
1827  * Sets the current position of the scroll thumb.
1828  *
1829  * PARAMS
1830  *    hwnd    [I]  Handle of window with scrollbar(s)
1831  *    nBar    [I]  One of SB_HORZ, SB_VERT, or SB_CTL
1832  *    nPos    [I]  New value
1833  *    bRedraw [I]  Should scrollbar be redrawn afterwards?
1834  *
1835  * RETURNS
1836  *    Success: Scrollbar position
1837  *    Failure: 0
1838  *
1839  * REMARKS
1840  *    Note the ambiguity when 0 is returned.  Use GetLastError
1841  *    to make sure there was an error (and to know which one).
1842  */
1843 INT WINAPI SetScrollPos( HWND hwnd, INT nBar, INT nPos, BOOL bRedraw)
1844 {
1845     SCROLLINFO info;
1846     SCROLLBAR_INFO *infoPtr;
1847     INT oldPos;
1848
1849     if (!(infoPtr = SCROLL_GetInternalInfo( hwnd, nBar, FALSE ))) return 0;
1850     oldPos      = infoPtr->curVal;
1851     info.cbSize = sizeof(info);
1852     info.nPos   = nPos;
1853     info.fMask  = SIF_POS;
1854     SetScrollInfo( hwnd, nBar, &info, bRedraw );
1855     return oldPos;
1856 }
1857
1858
1859 /*************************************************************************
1860  *           GetScrollPos   (USER32.@)
1861  *
1862  * Gets the current position of the scroll thumb.
1863  *
1864  * PARAMS
1865  *    hwnd    [I]  Handle of window with scrollbar(s)
1866  *    nBar    [I]  One of SB_HORZ, SB_VERT, or SB_CTL
1867  *
1868  * RETURNS
1869  *    Success: Current position
1870  *    Failure: 0
1871  *
1872  * REMARKS
1873  *    There is ambiguity when 0 is returned.  Use GetLastError
1874  *    to make sure there was an error (and to know which one).
1875  */
1876 INT WINAPI GetScrollPos(HWND hwnd, INT nBar)
1877 {
1878     TRACE("hwnd=%p nBar=%d\n", hwnd, nBar);
1879
1880     /* Refer SB_CTL requests to the window */
1881     if (nBar == SB_CTL)
1882         return SendMessageW(hwnd, SBM_GETPOS, 0, 0);
1883     else
1884         return SCROLL_GetScrollPos(hwnd, nBar);
1885 }
1886
1887
1888 /*************************************************************************
1889  *           SetScrollRange   (USER32.@)
1890  * The SetScrollRange function sets the minimum and maximum scroll box positions 
1891  * If nMinPos and nMaxPos is the same value, the scroll bar will hide
1892  *
1893  * Sets the range of the scroll bar.
1894  *
1895  * PARAMS
1896  *    hwnd    [I]  Handle of window with scrollbar(s)
1897  *    nBar    [I]  One of SB_HORZ, SB_VERT, or SB_CTL
1898  *    minVal  [I]  New minimum value
1899  *    maxVal  [I]  New Maximum value
1900  *    bRedraw [I]  Should scrollbar be redrawn afterwards?
1901  *
1902  * RETURNS
1903  *    Success: TRUE
1904  *    Failure: FALSE
1905  */
1906 BOOL WINAPI SetScrollRange(HWND hwnd, INT nBar, INT minVal, INT maxVal, BOOL bRedraw)
1907 {
1908     SCROLLINFO info;
1909  
1910     TRACE("hwnd=%p nBar=%d min=%d max=%d, bRedraw=%d\n", hwnd, nBar, minVal, maxVal, bRedraw);
1911
1912     info.cbSize = sizeof(info);
1913     info.fMask  = SIF_RANGE;
1914     info.nMin   = minVal;
1915     info.nMax   = maxVal;
1916     SetScrollInfo( hwnd, nBar, &info, bRedraw );
1917     return TRUE;
1918 }
1919
1920
1921 /*************************************************************************
1922  *           SCROLL_SetNCSbState
1923  *
1924  * Updates both scrollbars at the same time. Used by MDI CalcChildScroll().
1925  */
1926 INT SCROLL_SetNCSbState(HWND hwnd, int vMin, int vMax, int vPos,
1927                         int hMin, int hMax, int hPos)
1928 {
1929     SCROLLINFO vInfo, hInfo;
1930
1931     vInfo.cbSize = hInfo.cbSize = sizeof(SCROLLINFO);
1932     vInfo.nMin   = vMin;
1933     vInfo.nMax   = vMax;
1934     vInfo.nPos   = vPos;
1935     hInfo.nMin   = hMin;
1936     hInfo.nMax   = hMax;
1937     hInfo.nPos   = hPos;
1938     vInfo.fMask  = hInfo.fMask = SIF_RANGE | SIF_POS;
1939
1940     SCROLL_SetScrollInfo( hwnd, SB_VERT, &vInfo, TRUE );
1941     SCROLL_SetScrollInfo( hwnd, SB_HORZ, &hInfo, TRUE );
1942
1943     return 0;
1944 }
1945
1946
1947 /*************************************************************************
1948  *           GetScrollRange   (USER32.@)
1949  *
1950  * Gets the range of the scroll bar.
1951  *
1952  * PARAMS
1953  *    hwnd    [I]  Handle of window with scrollbar(s)
1954  *    nBar    [I]  One of SB_HORZ, SB_VERT, or SB_CTL
1955  *    lpMin   [O]  Where to store minimum value
1956  *    lpMax   [O]  Where to store maximum value
1957  *
1958  * RETURNS
1959  *    TRUE if values is filled
1960  */
1961 BOOL WINAPI GetScrollRange(HWND hwnd, INT nBar, LPINT lpMin, LPINT lpMax)
1962 {
1963     TRACE("hwnd=%p nBar=%d lpMin=%p lpMax=%p\n", hwnd, nBar, lpMin, lpMax);
1964
1965     /* Refer SB_CTL requests to the window */
1966     if (nBar == SB_CTL)
1967         SendMessageW(hwnd, SBM_GETRANGE, (WPARAM)lpMin, (LPARAM)lpMax);
1968     else
1969         SCROLL_GetScrollRange(hwnd, nBar, lpMin, lpMax);
1970
1971     return TRUE;
1972 }
1973
1974
1975 /*************************************************************************
1976  *           SCROLL_ShowScrollBar()
1977  *
1978  * Back-end for ShowScrollBar(). Returns FALSE if no action was taken.
1979  */
1980 static BOOL SCROLL_ShowScrollBar( HWND hwnd, INT nBar, BOOL fShowH, BOOL fShowV )
1981 {
1982     ULONG old_style, set_bits = 0, clear_bits = 0;
1983
1984     TRACE("hwnd=%p bar=%d horz=%d, vert=%d\n", hwnd, nBar, fShowH, fShowV );
1985
1986     switch(nBar)
1987     {
1988     case SB_CTL:
1989         ShowWindow( hwnd, fShowH ? SW_SHOW : SW_HIDE );
1990         return TRUE;
1991
1992     case SB_BOTH:
1993     case SB_HORZ:
1994         if (fShowH) set_bits |= WS_HSCROLL;
1995         else clear_bits |= WS_HSCROLL;
1996         if( nBar == SB_HORZ ) break;
1997         /* fall through */
1998     case SB_VERT:
1999         if (fShowV) set_bits |= WS_VSCROLL;
2000         else clear_bits |= WS_VSCROLL;
2001         break;
2002
2003     default:
2004         return FALSE;  /* Nothing to do! */
2005     }
2006
2007     old_style = WIN_SetStyle( hwnd, set_bits, clear_bits );
2008     if ((old_style & clear_bits) != 0 || (old_style & set_bits) != set_bits)
2009     {
2010         /* frame has been changed, let the window redraw itself */
2011         SetWindowPos( hwnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE
2012                     | SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
2013         return TRUE;
2014     }
2015     return FALSE; /* no frame changes */
2016 }
2017
2018
2019 /*************************************************************************
2020  *           ShowScrollBar   (USER32.@)
2021  *
2022  * Shows or hides the scroll bar.
2023  *
2024  * PARAMS
2025  *    hwnd    [I]  Handle of window with scrollbar(s)
2026  *    nBar    [I]  One of SB_HORZ, SB_VERT, or SB_CTL
2027  *    fShow   [I]  TRUE = show, FALSE = hide
2028  *
2029  * RETURNS
2030  *    Success: TRUE
2031  *    Failure: FALSE
2032  */
2033 BOOL WINAPI ShowScrollBar(HWND hwnd, INT nBar, BOOL fShow)
2034 {
2035     if ( !hwnd )
2036         return FALSE;
2037
2038     SCROLL_ShowScrollBar( hwnd, nBar, (nBar == SB_VERT) ? 0 : fShow,
2039                                       (nBar == SB_HORZ) ? 0 : fShow );
2040     return TRUE;
2041 }
2042
2043
2044 /*************************************************************************
2045  *           EnableScrollBar   (USER32.@)
2046  *
2047  * Enables or disables the scroll bars.
2048  */
2049 BOOL WINAPI EnableScrollBar( HWND hwnd, UINT nBar, UINT flags )
2050 {
2051     BOOL bFineWithMe;
2052     SCROLLBAR_INFO *infoPtr;
2053
2054     flags &= ESB_DISABLE_BOTH;
2055
2056     if (nBar == SB_BOTH)
2057     {
2058         if (!(infoPtr = SCROLL_GetInternalInfo( hwnd, SB_VERT, TRUE ))) return FALSE;
2059         if (!(bFineWithMe = (infoPtr->flags == flags)) )
2060         {
2061             infoPtr->flags = flags;
2062             SCROLL_RefreshScrollBar( hwnd, SB_VERT, TRUE, TRUE );
2063         }
2064         nBar = SB_HORZ;
2065     }
2066     else
2067         bFineWithMe = TRUE;
2068
2069     if (!(infoPtr = SCROLL_GetInternalInfo( hwnd, nBar, TRUE ))) return FALSE;
2070     if (bFineWithMe && infoPtr->flags == flags) return FALSE;
2071     infoPtr->flags = flags;
2072
2073     if (nBar == SB_CTL && (flags == ESB_DISABLE_BOTH || flags == ESB_ENABLE_BOTH))
2074         EnableWindow(hwnd, flags == ESB_ENABLE_BOTH);
2075
2076     SCROLL_RefreshScrollBar( hwnd, nBar, TRUE, TRUE );
2077     return TRUE;
2078 }