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