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