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