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