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