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