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