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