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