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