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