Link with libdl only where needed.
[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 = 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                  + 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 = 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             CREATESTRUCTW *lpCreat = (CREATESTRUCTW *)lParam;
1176             if (lpCreat->style & SBS_SIZEBOX)
1177             {
1178                 FIXME("Unimplemented style SBS_SIZEBOX.\n" );
1179                 return 0;
1180             }
1181             if (lpCreat->style & SBS_VERT)
1182             {
1183                 if (lpCreat->style & SBS_LEFTALIGN)
1184                     MoveWindow( hwnd, lpCreat->x, lpCreat->y,
1185                                   GetSystemMetrics(SM_CXVSCROLL)+1, lpCreat->cy, FALSE );
1186                 else if (lpCreat->style & SBS_RIGHTALIGN)
1187                     MoveWindow( hwnd, 
1188                                   lpCreat->x+lpCreat->cx-GetSystemMetrics(SM_CXVSCROLL)-1,
1189                                   lpCreat->y,
1190                                   GetSystemMetrics(SM_CXVSCROLL)+1, lpCreat->cy, FALSE );
1191             }
1192             else  /* SBS_HORZ */
1193             {
1194                 if (lpCreat->style & SBS_TOPALIGN)
1195                     MoveWindow( hwnd, lpCreat->x, lpCreat->y,
1196                                   lpCreat->cx, GetSystemMetrics(SM_CYHSCROLL)+1, FALSE );
1197                 else if (lpCreat->style & SBS_BOTTOMALIGN)
1198                     MoveWindow( hwnd, 
1199                                   lpCreat->x,
1200                                   lpCreat->y+lpCreat->cy-GetSystemMetrics(SM_CYHSCROLL)-1,
1201                                   lpCreat->cx, GetSystemMetrics(SM_CYHSCROLL)+1, FALSE );
1202             }
1203         }
1204         if (!hUpArrow) SCROLL_LoadBitmaps();
1205         TRACE("ScrollBar creation, hwnd=%04x\n", hwnd );
1206         return 0;
1207         
1208     case WM_LBUTTONDOWN:
1209         {
1210             POINT pt;
1211             pt.x = SLOWORD(lParam);
1212             pt.y = SHIWORD(lParam);
1213             SCROLL_TrackScrollBar( hwnd, SB_CTL, pt );
1214         }
1215         break;
1216     case WM_LBUTTONUP:
1217     case WM_MOUSEMOVE:
1218     case WM_SYSTIMER:
1219         {
1220             POINT pt;
1221             pt.x = SLOWORD(lParam);
1222             pt.y = SHIWORD(lParam);
1223             SCROLL_HandleScrollEvent( hwnd, SB_CTL, message, pt );
1224         }
1225         break;
1226
1227     /* if key event is received, the scrollbar has the focus */
1228     case WM_KEYDOWN:
1229         /* hide caret on first KEYDOWN to prevent flicker */
1230         if ((lParam & 0x40000000)==0)
1231             HideCaret(hwnd);
1232         SCROLL_HandleKbdEvent( hwnd, wParam );
1233         break;
1234
1235     case WM_KEYUP:
1236         ShowCaret(hwnd);
1237         break;
1238
1239     case WM_SETFOCUS:
1240         {
1241             /* Create a caret when a ScrollBar get focus */
1242             RECT rect;
1243             int arrowSize, thumbSize, thumbPos, vertical;
1244             vertical = SCROLL_GetScrollBarRect( hwnd, SB_CTL, &rect,
1245                                                 &arrowSize, &thumbSize, &thumbPos );
1246             if (!vertical)
1247             {
1248                 CreateCaret(hwnd,1, thumbSize-2, rect.bottom-rect.top-2);
1249                 SetCaretPos(thumbPos+1, rect.top+1);
1250             }
1251             else
1252             {
1253                 CreateCaret(hwnd,1, rect.right-rect.left-2,thumbSize-2);
1254                 SetCaretPos(rect.top+1, thumbPos+1);
1255             }
1256             ShowCaret(hwnd);
1257         }
1258         break;
1259
1260     case WM_KILLFOCUS:
1261         {
1262             RECT rect;
1263             int arrowSize, thumbSize, thumbPos, vertical;
1264             vertical = SCROLL_GetScrollBarRect( hwnd, SB_CTL, &rect,&arrowSize, &thumbSize, &thumbPos );
1265             if (!vertical){
1266                 rect.left=thumbPos+1;
1267                 rect.right=rect.left+thumbSize;
1268             }
1269             else
1270             {
1271                 rect.top=thumbPos+1;
1272                 rect.bottom=rect.top+thumbSize;
1273             }
1274             HideCaret(hwnd);
1275             InvalidateRect(hwnd,&rect,0);
1276             DestroyCaret();
1277         }
1278         break;
1279
1280     case WM_ERASEBKGND:
1281          return 1;
1282
1283     case WM_GETDLGCODE:
1284          return DLGC_WANTARROWS; /* Windows returns this value */
1285
1286     case WM_PAINT:
1287         {
1288             PAINTSTRUCT ps;
1289             HDC hdc = BeginPaint( hwnd, &ps );
1290             SCROLL_DrawScrollBar( hwnd, hdc, SB_CTL, TRUE, TRUE );
1291             EndPaint( hwnd, &ps );
1292         }
1293         break;
1294
1295     case SBM_SETPOS16:
1296     case SBM_SETPOS:
1297         return SetScrollPos( hwnd, SB_CTL, wParam, (BOOL)lParam );
1298
1299     case SBM_GETPOS16:
1300     case SBM_GETPOS:
1301         return GetScrollPos( hwnd, SB_CTL );
1302
1303     case SBM_SETRANGE16:
1304         SetScrollRange( hwnd, SB_CTL, LOWORD(lParam), HIWORD(lParam),
1305                           wParam  /* FIXME: Is this correct? */ );
1306         return 0;
1307
1308     case SBM_SETRANGE:
1309         {
1310             INT oldPos = GetScrollPos( hwnd, SB_CTL );
1311             SetScrollRange( hwnd, SB_CTL, wParam, lParam, FALSE );
1312             if (oldPos != GetScrollPos( hwnd, SB_CTL )) return oldPos;
1313         }
1314         return 0;
1315
1316     case SBM_GETRANGE16:
1317         FIXME("don't know how to handle SBM_GETRANGE16 (wp=%04x,lp=%08lx)\n", wParam, lParam );
1318         return 0;
1319
1320     case SBM_GETRANGE:
1321         GetScrollRange( hwnd, SB_CTL, (LPINT)wParam, (LPINT)lParam );
1322         return 0;
1323
1324     case SBM_ENABLE_ARROWS16:
1325     case SBM_ENABLE_ARROWS:
1326         return EnableScrollBar( hwnd, SB_CTL, wParam );
1327
1328     case SBM_SETRANGEREDRAW:
1329         {
1330             INT oldPos = GetScrollPos( hwnd, SB_CTL );
1331             SetScrollRange( hwnd, SB_CTL, wParam, lParam, TRUE );
1332             if (oldPos != GetScrollPos( hwnd, SB_CTL )) return oldPos;
1333         }
1334         return 0;
1335
1336     case SBM_SETSCROLLINFO:
1337         return SetScrollInfo( hwnd, SB_CTL, (SCROLLINFO *)lParam, wParam );
1338
1339     case SBM_GETSCROLLINFO:
1340         return GetScrollInfo( hwnd, SB_CTL, (SCROLLINFO *)lParam );
1341
1342     case 0x00e5:
1343     case 0x00e7:
1344     case 0x00e8:
1345     case 0x00eb:
1346     case 0x00ec:
1347     case 0x00ed:
1348     case 0x00ee:
1349     case 0x00ef:
1350         ERR("unknown Win32 msg %04x wp=%08x lp=%08lx\n",
1351                     message, wParam, lParam );
1352         break;
1353
1354     default:
1355         if (message >= WM_USER)
1356             WARN("unknown msg %04x wp=%04x lp=%08lx\n",
1357                          message, wParam, lParam );
1358         return DefWindowProcW( hwnd, message, wParam, lParam );
1359     }
1360     return 0;
1361 }
1362
1363
1364 /*************************************************************************
1365  *           SetScrollInfo   (USER32.@)
1366  * SetScrollInfo can be used to set the position, upper bound, 
1367  * lower bound, and page size of a scrollbar control.
1368  *
1369  * RETURNS
1370  *    Scrollbar position
1371  *
1372  * NOTE
1373  *    For 100 lines of text to be displayed in a window of 25 lines,
1374  *  one would for instance use info->nMin=0, info->nMax=75
1375  *  (corresponding to the 76 different positions of the window on
1376  *  the text), and info->nPage=25.
1377  */
1378 INT WINAPI SetScrollInfo( 
1379 HWND hwnd /* [in] Handle of window whose scrollbar will be affected */, 
1380 INT nBar /* [in] One of SB_HORZ, SB_VERT, or SB_CTL */, 
1381 const SCROLLINFO *info /* [in] Specifies what to change and new values */,
1382 BOOL bRedraw /* [in] Should scrollbar be redrawn afterwards ? */)
1383 {
1384     INT action;
1385     INT retVal = SCROLL_SetScrollInfo( hwnd, nBar, info, &action );
1386
1387     if( action & SA_SSI_HIDE )
1388         SCROLL_ShowScrollBar( hwnd, nBar, FALSE, FALSE );
1389     else
1390     {
1391         if( action & SA_SSI_SHOW )
1392             if( SCROLL_ShowScrollBar( hwnd, nBar, TRUE, TRUE ) )
1393                 return retVal; /* SetWindowPos() already did the painting */
1394
1395         if( bRedraw && (action & SA_SSI_REFRESH))
1396             SCROLL_RefreshScrollBar( hwnd, nBar, TRUE, TRUE );
1397         else if( action & SA_SSI_REPAINT_ARROWS )
1398             SCROLL_RefreshScrollBar( hwnd, nBar, TRUE, FALSE );
1399     }
1400     return retVal;
1401 }
1402
1403 INT SCROLL_SetScrollInfo( HWND hwnd, INT nBar, 
1404                             const SCROLLINFO *info, INT *action  )
1405 {
1406     /* Update the scrollbar state and set action flags according to 
1407      * what has to be done graphics wise. */
1408
1409     SCROLLBAR_INFO *infoPtr;
1410     UINT new_flags;
1411     BOOL bChangeParams = FALSE; /* don't show/hide scrollbar if params don't change */
1412
1413    *action = 0;
1414
1415     if (!(infoPtr = SCROLL_GetScrollInfo(hwnd, nBar))) return 0;
1416     if (info->fMask & ~(SIF_ALL | SIF_DISABLENOSCROLL)) return 0;
1417     if ((info->cbSize != sizeof(*info)) &&
1418         (info->cbSize != sizeof(*info)-sizeof(info->nTrackPos))) return 0;
1419
1420     if (TRACE_ON(scroll))
1421     {
1422         TRACE("hwnd=%04x bar=%d", hwnd, nBar);
1423         if (info->fMask & SIF_PAGE) DPRINTF( " page=%d", info->nPage );
1424         if (info->fMask & SIF_POS) DPRINTF( " pos=%d", info->nPos );
1425         if (info->fMask & SIF_RANGE) DPRINTF( " min=%d max=%d", info->nMin, info->nMax );
1426         DPRINTF("\n");
1427     }
1428
1429     /* Set the page size */
1430
1431     if (info->fMask & SIF_PAGE)
1432     {
1433         if( infoPtr->Page != info->nPage )
1434         {
1435             infoPtr->Page = info->nPage;
1436            *action |= SA_SSI_REFRESH;
1437            bChangeParams = TRUE; 
1438         }
1439     }
1440
1441     /* Set the scroll pos */
1442
1443     if (info->fMask & SIF_POS)
1444     {
1445         if( infoPtr->CurVal != info->nPos )
1446         {
1447             infoPtr->CurVal = info->nPos;
1448            *action |= SA_SSI_REFRESH;
1449         }
1450     }
1451
1452     /* Set the scroll range */
1453
1454     if (info->fMask & SIF_RANGE)
1455     {
1456         /* Invalid range -> range is set to (0,0) */
1457         if ((info->nMin > info->nMax) ||
1458             ((UINT)(info->nMax - info->nMin) >= 0x80000000))
1459         {
1460             infoPtr->MinVal = 0;
1461             infoPtr->MaxVal = 0;
1462             bChangeParams = TRUE;
1463         }
1464         else
1465         {
1466             if( infoPtr->MinVal != info->nMin ||
1467                 infoPtr->MaxVal != info->nMax )
1468             {
1469                 *action |= SA_SSI_REFRESH; 
1470                 infoPtr->MinVal = info->nMin;
1471                 infoPtr->MaxVal = info->nMax;
1472                 bChangeParams = TRUE;
1473             }
1474         }
1475     }
1476
1477     /* Make sure the page size is valid */
1478
1479     if (infoPtr->Page < 0) infoPtr->Page = 0;
1480     else if (infoPtr->Page > infoPtr->MaxVal - infoPtr->MinVal + 1 )
1481         infoPtr->Page = infoPtr->MaxVal - infoPtr->MinVal + 1;
1482
1483     /* Make sure the pos is inside the range */
1484
1485     if (infoPtr->CurVal < infoPtr->MinVal)
1486         infoPtr->CurVal = infoPtr->MinVal;
1487     else if (infoPtr->CurVal > infoPtr->MaxVal - max( infoPtr->Page-1, 0 ))
1488         infoPtr->CurVal = infoPtr->MaxVal - max( infoPtr->Page-1, 0 );
1489
1490     TRACE("    new values: page=%d pos=%d min=%d max=%d\n",
1491                  infoPtr->Page, infoPtr->CurVal,
1492                  infoPtr->MinVal, infoPtr->MaxVal );
1493
1494     /* don't change the scrollbar state if SetScrollInfo 
1495      * is just called with SIF_DISABLENOSCROLL
1496      */
1497     if(!(info->fMask & SIF_ALL)) goto done;
1498
1499     /* Check if the scrollbar should be hidden or disabled */
1500
1501     if (info->fMask & (SIF_RANGE | SIF_PAGE | SIF_DISABLENOSCROLL))
1502     {
1503         new_flags = infoPtr->flags;
1504         if (infoPtr->MinVal >= infoPtr->MaxVal - max( infoPtr->Page-1, 0 ))
1505         {
1506             /* Hide or disable scroll-bar */
1507             if (info->fMask & SIF_DISABLENOSCROLL)
1508             {
1509                 new_flags = ESB_DISABLE_BOTH;
1510                *action |= SA_SSI_REFRESH;
1511             }
1512             else if ((nBar != SB_CTL) && bChangeParams)
1513             {
1514                 *action = SA_SSI_HIDE;
1515                 goto done;
1516             }
1517         }
1518         else  /* Show and enable scroll-bar */
1519         {
1520             new_flags = 0;
1521             if ((nBar != SB_CTL) && bChangeParams)
1522                 *action |= SA_SSI_SHOW;
1523         }
1524
1525         if (infoPtr->flags != new_flags) /* check arrow flags */
1526         {
1527             infoPtr->flags = new_flags;
1528            *action |= SA_SSI_REPAINT_ARROWS;
1529         }
1530     }
1531
1532 done:
1533     /* Return current position */
1534
1535     return infoPtr->CurVal;
1536 }
1537
1538
1539 /*************************************************************************
1540  *           GetScrollInfo   (USER32.@)
1541  * GetScrollInfo can be used to retrieve the position, upper bound, 
1542  * lower bound, and page size of a scrollbar control.
1543  *
1544  * RETURNS STD
1545  */
1546 BOOL WINAPI GetScrollInfo( 
1547   HWND hwnd /* [in] Handle of window */ , 
1548   INT nBar /* [in] One of SB_HORZ, SB_VERT, or SB_CTL */, 
1549   LPSCROLLINFO info /* [in/out] (info.fMask [in] specifies which values are to retrieve) */)
1550 {
1551     SCROLLBAR_INFO *infoPtr;
1552
1553     if (!(infoPtr = SCROLL_GetScrollInfo( hwnd, nBar ))) return FALSE;
1554     if (info->fMask & ~(SIF_ALL | SIF_DISABLENOSCROLL)) return FALSE;
1555     if ((info->cbSize != sizeof(*info)) &&
1556         (info->cbSize != sizeof(*info)-sizeof(info->nTrackPos))) return FALSE;
1557
1558     if (info->fMask & SIF_PAGE) info->nPage = infoPtr->Page;
1559     if (info->fMask & SIF_POS) info->nPos = infoPtr->CurVal;
1560     if ((info->fMask & SIF_TRACKPOS) && (info->cbSize == sizeof(*info)))
1561         info->nTrackPos = (SCROLL_TrackingWin == WIN_GetFullHandle(hwnd)) ? SCROLL_TrackingVal : infoPtr->CurVal;
1562     if (info->fMask & SIF_RANGE)
1563     {
1564         info->nMin = infoPtr->MinVal;
1565         info->nMax = infoPtr->MaxVal;
1566     }
1567     return (info->fMask & SIF_ALL) != 0;
1568 }
1569
1570
1571 /*************************************************************************
1572  *           SetScrollPos   (USER32.@)
1573  *
1574  * RETURNS
1575  *    Success: Scrollbar position
1576  *    Failure: 0
1577  *
1578  * REMARKS
1579  *    Note the ambiguity when 0 is returned.  Use GetLastError
1580  *    to make sure there was an error (and to know which one).
1581  */
1582 INT WINAPI SetScrollPos( 
1583 HWND hwnd /* [in] Handle of window whose scrollbar will be affected */,
1584 INT nBar /* [in] One of SB_HORZ, SB_VERT, or SB_CTL */,
1585 INT nPos /* [in] New value */,
1586 BOOL bRedraw /* [in] Should scrollbar be redrawn afterwards ? */ )
1587 {
1588     SCROLLINFO info;
1589     SCROLLBAR_INFO *infoPtr;
1590     INT oldPos;
1591
1592     if (!(infoPtr = SCROLL_GetScrollInfo( hwnd, nBar ))) return 0;
1593     oldPos      = infoPtr->CurVal;
1594     info.cbSize = sizeof(info);
1595     info.nPos   = nPos;
1596     info.fMask  = SIF_POS;
1597     SetScrollInfo( hwnd, nBar, &info, bRedraw );
1598     return oldPos;
1599 }
1600
1601
1602 /*************************************************************************
1603  *           GetScrollPos   (USER32.@)
1604  *
1605  * RETURNS
1606  *    Success: Current position
1607  *    Failure: 0   
1608  *
1609  * REMARKS
1610  *    Note the ambiguity when 0 is returned.  Use GetLastError
1611  *    to make sure there was an error (and to know which one).
1612  */
1613 INT WINAPI GetScrollPos( 
1614 HWND hwnd, /* [in] Handle of window */
1615 INT nBar /* [in] One of SB_HORZ, SB_VERT, or SB_CTL */)
1616 {
1617     SCROLLBAR_INFO *infoPtr;
1618
1619     if (!(infoPtr = SCROLL_GetScrollInfo( hwnd, nBar ))) return 0;
1620     return infoPtr->CurVal;
1621 }
1622
1623
1624 /*************************************************************************
1625  *           SetScrollRange   (USER32.@)
1626  *
1627  * RETURNS STD
1628  */
1629 BOOL WINAPI SetScrollRange( 
1630 HWND hwnd, /* [in] Handle of window whose scrollbar will be affected */
1631 INT nBar, /* [in] One of SB_HORZ, SB_VERT, or SB_CTL */
1632 INT MinVal, /* [in] New minimum value */
1633 INT MaxVal, /* [in] New maximum value */
1634 BOOL bRedraw /* [in] Should scrollbar be redrawn afterwards ? */)
1635 {
1636     SCROLLINFO info;
1637
1638     info.cbSize = sizeof(info);
1639     info.nMin   = MinVal;
1640     info.nMax   = MaxVal;
1641     info.fMask  = SIF_RANGE;
1642     SetScrollInfo( hwnd, nBar, &info, bRedraw );
1643     return TRUE;
1644 }
1645
1646
1647 /*************************************************************************
1648  *           SCROLL_SetNCSbState
1649  *
1650  * Updates both scrollbars at the same time. Used by MDI CalcChildScroll().
1651  */
1652 INT SCROLL_SetNCSbState(HWND hwnd, int vMin, int vMax, int vPos,
1653                         int hMin, int hMax, int hPos)
1654 {
1655     INT vA, hA;
1656     SCROLLINFO vInfo, hInfo;
1657
1658     vInfo.cbSize = hInfo.cbSize = sizeof(SCROLLINFO);
1659     vInfo.nMin   = vMin;
1660     vInfo.nMax   = vMax;
1661     vInfo.nPos   = vPos;
1662     hInfo.nMin   = hMin;
1663     hInfo.nMax   = hMax;
1664     hInfo.nPos   = hPos;
1665     vInfo.fMask  = hInfo.fMask = SIF_RANGE | SIF_POS;
1666
1667     SCROLL_SetScrollInfo( hwnd, SB_VERT, &vInfo, &vA );
1668     SCROLL_SetScrollInfo( hwnd, SB_HORZ, &hInfo, &hA );
1669
1670     if( !SCROLL_ShowScrollBar( hwnd, 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( hwnd, SB_VERT, FALSE, TRUE );
1676
1677         if( hA & SA_SSI_REFRESH )
1678             SCROLL_RefreshScrollBar( hwnd, SB_HORZ, FALSE, TRUE );
1679     }
1680     return 0;
1681 }
1682
1683
1684 /*************************************************************************
1685  *           GetScrollRange   (USER32.@)
1686  *
1687  * RETURNS STD
1688  */
1689 BOOL WINAPI GetScrollRange( 
1690 HWND hwnd, /* [in] Handle of window */
1691 INT nBar, /* [in] One of SB_HORZ, SB_VERT, or SB_CTL  */
1692 LPINT lpMin, /* [out] Where to store minimum value */
1693 LPINT lpMax /* [out] Where to store maximum value */)
1694 {
1695     SCROLLBAR_INFO *infoPtr;
1696
1697     if (!(infoPtr = SCROLL_GetScrollInfo( hwnd, nBar )))
1698     {
1699         if (lpMin) lpMin = 0;
1700         if (lpMax) lpMax = 0;
1701         return FALSE;
1702     }
1703     if (lpMin) *lpMin = infoPtr->MinVal;
1704     if (lpMax) *lpMax = infoPtr->MaxVal;
1705     return TRUE;
1706 }
1707
1708
1709 /*************************************************************************
1710  *           SCROLL_ShowScrollBar()
1711  *
1712  * Back-end for ShowScrollBar(). Returns FALSE if no action was taken.
1713  */
1714 BOOL SCROLL_ShowScrollBar( HWND hwnd, INT nBar, 
1715                              BOOL fShowH, BOOL fShowV )
1716 {
1717     LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1718
1719     TRACE("hwnd=%04x bar=%d horz=%d, vert=%d\n",
1720                     hwnd, nBar, fShowH, fShowV );
1721
1722     switch(nBar)
1723     {
1724     case SB_CTL:
1725         ShowWindow( hwnd, fShowH ? SW_SHOW : SW_HIDE );
1726         return TRUE;
1727
1728     case SB_BOTH:
1729     case SB_HORZ:
1730         if (fShowH)
1731         {
1732             fShowH = !(style & WS_HSCROLL);
1733             style |= WS_HSCROLL;
1734         }
1735         else  /* hide it */
1736         {
1737             fShowH = (style & WS_HSCROLL);
1738             style &= ~WS_HSCROLL;
1739         }
1740         if( nBar == SB_HORZ ) {
1741             fShowV = FALSE;
1742             break;
1743         }
1744         /* fall through */
1745
1746     case SB_VERT:
1747         if (fShowV)
1748         {
1749             fShowV = !(style & WS_VSCROLL);
1750             style |= WS_VSCROLL;
1751         }
1752         else  /* hide it */
1753         {
1754             fShowV = (style & WS_VSCROLL);
1755             style &= ~WS_VSCROLL;
1756         }
1757         if ( nBar == SB_VERT )
1758            fShowH = FALSE;
1759         break;
1760
1761     default:
1762         return FALSE;  /* Nothing to do! */
1763     }
1764
1765     if( fShowH || fShowV ) /* frame has been changed, let the window redraw itself */
1766     {
1767         WIN_SetStyle( hwnd, style );
1768         SetWindowPos( hwnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE
1769                     | SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
1770         return TRUE;
1771     }
1772     return FALSE; /* no frame changes */
1773 }
1774
1775
1776 /*************************************************************************
1777  *           ShowScrollBar   (USER32.@)
1778  *
1779  * RETURNS STD
1780  */
1781 BOOL WINAPI ShowScrollBar(
1782 HWND hwnd, /* [in] Handle of window whose scrollbar(s) will be affected   */
1783 INT nBar, /* [in] One of SB_HORZ, SB_VERT, SB_BOTH or SB_CTL */
1784 BOOL fShow /* [in] TRUE = show, FALSE = hide  */)
1785 {
1786     SCROLL_ShowScrollBar( hwnd, nBar, (nBar == SB_VERT) ? 0 : fShow,
1787                                       (nBar == SB_HORZ) ? 0 : fShow );
1788     return TRUE;
1789 }
1790
1791
1792 /*************************************************************************
1793  *           EnableScrollBar   (USER32.@)
1794  */
1795 BOOL WINAPI EnableScrollBar( HWND hwnd, INT nBar, UINT flags )
1796 {
1797     BOOL bFineWithMe;
1798     SCROLLBAR_INFO *infoPtr;
1799
1800     TRACE("%04x %d %d\n", hwnd, nBar, flags );
1801
1802     flags &= ESB_DISABLE_BOTH;
1803
1804     if (nBar == SB_BOTH)
1805     {
1806         if (!(infoPtr = SCROLL_GetScrollInfo( hwnd, SB_VERT ))) return FALSE;
1807         if (!(bFineWithMe = (infoPtr->flags == flags)) )
1808         {
1809             infoPtr->flags = flags;
1810             SCROLL_RefreshScrollBar( hwnd, SB_VERT, TRUE, TRUE );
1811         }
1812         nBar = SB_HORZ;
1813     }
1814     else
1815         bFineWithMe = TRUE;
1816    
1817     if (!(infoPtr = SCROLL_GetScrollInfo( hwnd, nBar ))) return FALSE;
1818     if (bFineWithMe && infoPtr->flags == flags) return FALSE;
1819     infoPtr->flags = flags;
1820
1821     SCROLL_RefreshScrollBar( hwnd, nBar, TRUE, TRUE );
1822     return TRUE;
1823 }