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