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