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