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