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