server: Do not accept sizeof(struct WS_sockaddr_in6_old).
[wine.git] / dlls / user32 / scroll.c
blob088e41eee43dc10ecc955d2b7167fd980251f648
1 /*
2 * Scrollbar control
4 * Copyright 1993 Martin Ayotte
5 * Copyright 1994, 1996 Alexandre Julliard
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.
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.
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
22 #include <stdarg.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "controls.h"
28 #include "win.h"
29 #include "wine/debug.h"
30 #include "user_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(scroll);
34 /* data for a single scroll bar */
35 typedef struct
37 INT curVal; /* Current scroll-bar value */
38 INT minVal; /* Minimum scroll-bar value */
39 INT maxVal; /* Maximum scroll-bar value */
40 INT page; /* Page size of scroll bar (Win32) */
41 UINT flags; /* EnableScrollBar flags */
42 } SCROLLBAR_INFO, *LPSCROLLBAR_INFO;
44 /* data for window that has (one or two) scroll bars */
45 typedef struct
47 SCROLLBAR_INFO horz;
48 SCROLLBAR_INFO vert;
49 } WINSCROLLBAR_INFO, *LPWINSCROLLBAR_INFO;
51 typedef struct
53 DWORD magic;
54 SCROLLBAR_INFO info;
55 } SCROLLBAR_WNDDATA;
57 #define SCROLLBAR_MAGIC 0x5c6011ba
59 /* Minimum size of the rectangle between the arrows */
60 #define SCROLL_MIN_RECT 4
62 /* Minimum size of the thumb in pixels */
63 #define SCROLL_MIN_THUMB 6
65 /* Overlap between arrows and thumb */
66 #define SCROLL_ARROW_THUMB_OVERLAP 0
68 /* Delay (in ms) before first repetition when holding the button down */
69 #define SCROLL_FIRST_DELAY 200
71 /* Delay (in ms) between scroll repetitions */
72 #define SCROLL_REPEAT_DELAY 50
74 /* Scroll timer id */
75 #define SCROLL_TIMER 0
77 /* What to do after SCROLL_SetScrollInfo() */
78 #define SA_SSI_HIDE 0x0001
79 #define SA_SSI_SHOW 0x0002
80 #define SA_SSI_REFRESH 0x0004
81 #define SA_SSI_REPAINT_ARROWS 0x0008
83 /* Scroll Bar tracking information */
84 static struct SCROLL_TRACKING_INFO g_tracking_info;
86 /* Is the moving thumb being displayed? */
87 static BOOL SCROLL_MovingThumb = FALSE;
89 /* Local functions */
90 static BOOL SCROLL_ShowScrollBar( HWND hwnd, INT nBar,
91 BOOL fShowH, BOOL fShowV );
92 static INT SCROLL_SetScrollInfo( HWND hwnd, INT nBar,
93 const SCROLLINFO *info, BOOL bRedraw );
95 /*********************************************************************
96 * scrollbar class descriptor
98 const struct builtin_class_descr SCROLL_builtin_class =
100 L"ScrollBar", /* name */
101 CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC, /* style */
102 WINPROC_SCROLLBAR, /* proc */
103 sizeof(SCROLLBAR_WNDDATA), /* extra */
104 IDC_ARROW, /* cursor */
105 0 /* brush */
108 /***********************************************************************
109 * SCROLL_ScrollInfoValid
111 * Determine if the supplied SCROLLINFO struct is valid.
112 * info [in] The SCROLLINFO struct to be tested
114 static inline BOOL SCROLL_ScrollInfoValid( LPCSCROLLINFO info )
116 return !(info->fMask & ~(SIF_ALL | SIF_DISABLENOSCROLL)
117 || (info->cbSize != sizeof(*info)
118 && info->cbSize != sizeof(*info) - sizeof(info->nTrackPos)));
122 /***********************************************************************
123 * SCROLL_GetInternalInfo
125 * Returns pointer to internal SCROLLBAR_INFO structure for nBar
126 * or NULL if failed (f.i. scroll bar does not exist yet)
127 * If alloc is TRUE and the struct does not exist yet, create it.
129 static SCROLLBAR_INFO *SCROLL_GetInternalInfo( HWND hwnd, INT nBar, BOOL alloc )
131 SCROLLBAR_INFO *infoPtr = NULL;
132 WND *wndPtr = WIN_GetPtr( hwnd );
134 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return NULL;
135 switch(nBar)
137 case SB_HORZ:
138 if (wndPtr->pScroll) infoPtr = &((LPWINSCROLLBAR_INFO)wndPtr->pScroll)->horz;
139 break;
140 case SB_VERT:
141 if (wndPtr->pScroll) infoPtr = &((LPWINSCROLLBAR_INFO)wndPtr->pScroll)->vert;
142 break;
143 case SB_CTL:
144 if (wndPtr->cbWndExtra >= sizeof(SCROLLBAR_WNDDATA))
146 SCROLLBAR_WNDDATA *data = (SCROLLBAR_WNDDATA*)wndPtr->wExtra;
147 if (data->magic == SCROLLBAR_MAGIC)
148 infoPtr = &data->info;
150 if (!infoPtr) WARN("window is not a scrollbar control\n");
151 break;
152 case SB_BOTH:
153 WARN("with SB_BOTH\n");
154 break;
157 if (!infoPtr && alloc)
159 WINSCROLLBAR_INFO *winInfoPtr;
161 if (nBar != SB_HORZ && nBar != SB_VERT)
162 WARN("Cannot initialize nBar=%d\n",nBar);
163 else if ((winInfoPtr = HeapAlloc( GetProcessHeap(), 0, sizeof(WINSCROLLBAR_INFO) )))
165 /* Set default values */
166 winInfoPtr->horz.minVal = 0;
167 winInfoPtr->horz.curVal = 0;
168 winInfoPtr->horz.page = 0;
169 /* From MSDN and our own tests:
170 * max for a standard scroll bar is 100 by default. */
171 winInfoPtr->horz.maxVal = 100;
172 winInfoPtr->horz.flags = ESB_ENABLE_BOTH;
173 winInfoPtr->vert = winInfoPtr->horz;
174 wndPtr->pScroll = winInfoPtr;
175 infoPtr = nBar == SB_HORZ ? &winInfoPtr->horz : &winInfoPtr->vert;
178 WIN_ReleasePtr( wndPtr );
179 return infoPtr;
183 /***********************************************************************
184 * SCROLL_GetScrollBarRect
186 * Compute the scroll bar rectangle, in drawing coordinates (i.e. client
187 * coords for SB_CTL, window coords for SB_VERT and SB_HORZ).
188 * 'arrowSize' returns the width or height of an arrow (depending on
189 * the orientation of the scrollbar), 'thumbSize' returns the size of
190 * the thumb, and 'thumbPos' returns the position of the thumb
191 * relative to the left or to the top.
192 * Return TRUE if the scrollbar is vertical, FALSE if horizontal.
194 static BOOL SCROLL_GetScrollBarRect( HWND hwnd, INT nBar, RECT *lprect,
195 INT *arrowSize, INT *thumbSize,
196 INT *thumbPos )
198 INT pixels;
199 BOOL vertical;
200 WND *wndPtr = WIN_GetPtr( hwnd );
202 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
204 switch(nBar)
206 case SB_HORZ:
207 WIN_GetRectangles( hwnd, COORDS_WINDOW, NULL, lprect );
208 lprect->top = lprect->bottom;
209 lprect->bottom += GetSystemMetrics(SM_CYHSCROLL);
210 if(wndPtr->dwStyle & WS_VSCROLL)
211 lprect->right++;
212 vertical = FALSE;
213 break;
215 case SB_VERT:
216 WIN_GetRectangles( hwnd, COORDS_WINDOW, NULL, lprect );
217 if((wndPtr->dwExStyle & WS_EX_LEFTSCROLLBAR) != 0)
219 lprect->right = lprect->left;
220 lprect->left -= GetSystemMetrics(SM_CXVSCROLL);
222 else
224 lprect->left = lprect->right;
225 lprect->right += GetSystemMetrics(SM_CXVSCROLL);
227 if(wndPtr->dwStyle & WS_HSCROLL)
228 lprect->bottom++;
229 vertical = TRUE;
230 break;
232 case SB_CTL:
233 GetClientRect( hwnd, lprect );
234 vertical = ((wndPtr->dwStyle & SBS_VERT) != 0);
235 break;
237 default:
238 WIN_ReleasePtr( wndPtr );
239 return FALSE;
242 if (vertical) pixels = lprect->bottom - lprect->top;
243 else pixels = lprect->right - lprect->left;
245 if (pixels <= 2*GetSystemMetrics(SM_CXVSCROLL) + SCROLL_MIN_RECT)
247 if (pixels > SCROLL_MIN_RECT)
248 *arrowSize = (pixels - SCROLL_MIN_RECT) / 2;
249 else
250 *arrowSize = 0;
251 *thumbPos = *thumbSize = 0;
253 else
255 SCROLLBAR_INFO *info = SCROLL_GetInternalInfo( hwnd, nBar, TRUE );
256 if (!info)
258 WARN("called for missing scroll bar\n");
259 WIN_ReleasePtr( wndPtr );
260 return FALSE;
262 *arrowSize = GetSystemMetrics(SM_CXVSCROLL);
263 pixels -= (2 * (GetSystemMetrics(SM_CXVSCROLL) - SCROLL_ARROW_THUMB_OVERLAP));
265 if (info->page)
267 *thumbSize = MulDiv(pixels,info->page,(info->maxVal-info->minVal+1));
268 if (*thumbSize < SCROLL_MIN_THUMB) *thumbSize = SCROLL_MIN_THUMB;
270 else *thumbSize = GetSystemMetrics(SM_CXVSCROLL);
272 if (((pixels -= *thumbSize ) < 0) ||
273 ((info->flags & ESB_DISABLE_BOTH) == ESB_DISABLE_BOTH))
275 /* Rectangle too small or scrollbar disabled -> no thumb */
276 *thumbPos = *thumbSize = 0;
278 else
280 INT max = info->maxVal - max( info->page-1, 0 );
281 if (info->minVal >= max)
282 *thumbPos = *arrowSize - SCROLL_ARROW_THUMB_OVERLAP;
283 else
284 *thumbPos = *arrowSize - SCROLL_ARROW_THUMB_OVERLAP
285 + MulDiv(pixels, (info->curVal-info->minVal),(max - info->minVal));
288 WIN_ReleasePtr( wndPtr );
289 return vertical;
292 static void SCROLL_GetScrollBarDrawInfo( HWND hwnd, INT bar,
293 const struct SCROLL_TRACKING_INFO *tracking_info,
294 RECT *rect, INT *arrow_size, INT *thumb_size,
295 INT *thumb_pos, BOOL *vertical )
297 INT pos, max_size;
299 if (bar == SB_CTL && GetWindowLongW( hwnd, GWL_STYLE ) & (SBS_SIZEGRIP | SBS_SIZEBOX))
301 GetClientRect( hwnd, rect );
302 *arrow_size = 0;
303 *thumb_pos = 0;
304 *thumb_size = 0;
305 *vertical = FALSE;
306 return;
309 *vertical = SCROLL_GetScrollBarRect( hwnd, bar, rect, arrow_size, thumb_size, thumb_pos );
311 if (SCROLL_MovingThumb && tracking_info->win == hwnd && tracking_info->bar == bar)
313 max_size = *vertical ? rect->bottom - rect->top : rect->right - rect->left;
314 max_size -= *arrow_size - SCROLL_ARROW_THUMB_OVERLAP + *thumb_size;
316 pos = tracking_info->thumb_pos;
317 if (pos < *arrow_size - SCROLL_ARROW_THUMB_OVERLAP)
318 pos = *arrow_size - SCROLL_ARROW_THUMB_OVERLAP;
319 else if (pos > max_size)
320 pos = max_size;
322 *thumb_pos = pos;
326 /***********************************************************************
327 * SCROLL_GetThumbVal
329 * Compute the current scroll position based on the thumb position in pixels
330 * from the top of the scroll-bar.
332 static UINT SCROLL_GetThumbVal( SCROLLBAR_INFO *infoPtr, RECT *rect,
333 BOOL vertical, INT pos )
335 INT thumbSize;
336 INT pixels = vertical ? rect->bottom-rect->top : rect->right-rect->left;
337 INT range;
339 if ((pixels -= 2*(GetSystemMetrics(SM_CXVSCROLL) - SCROLL_ARROW_THUMB_OVERLAP)) <= 0)
340 return infoPtr->minVal;
342 if (infoPtr->page)
344 thumbSize = MulDiv(pixels,infoPtr->page,(infoPtr->maxVal-infoPtr->minVal+1));
345 if (thumbSize < SCROLL_MIN_THUMB) thumbSize = SCROLL_MIN_THUMB;
347 else thumbSize = GetSystemMetrics(SM_CXVSCROLL);
349 if ((pixels -= thumbSize) <= 0) return infoPtr->minVal;
351 pos = max( 0, pos - (GetSystemMetrics(SM_CXVSCROLL) - SCROLL_ARROW_THUMB_OVERLAP) );
352 if (pos > pixels) pos = pixels;
354 if (!infoPtr->page)
355 range = infoPtr->maxVal - infoPtr->minVal;
356 else
357 range = infoPtr->maxVal - infoPtr->minVal - infoPtr->page + 1;
359 return infoPtr->minVal + MulDiv(pos, range, pixels);
362 /***********************************************************************
363 * SCROLL_PtInRectEx
365 static BOOL SCROLL_PtInRectEx( LPRECT lpRect, POINT pt, BOOL vertical )
367 RECT rect = *lpRect;
368 int scrollbarWidth;
370 /* Pad hit rect to allow mouse to be dragged outside of scrollbar and
371 * still be considered in the scrollbar. */
372 if (vertical)
374 scrollbarWidth = lpRect->right - lpRect->left;
375 InflateRect(&rect, scrollbarWidth * 8, scrollbarWidth * 2);
377 else
379 scrollbarWidth = lpRect->bottom - lpRect->top;
380 InflateRect(&rect, scrollbarWidth * 2, scrollbarWidth * 8);
382 return PtInRect( &rect, pt );
385 /***********************************************************************
386 * SCROLL_ClipPos
388 static POINT SCROLL_ClipPos( LPRECT lpRect, POINT pt )
390 if( pt.x < lpRect->left )
391 pt.x = lpRect->left;
392 else
393 if( pt.x > lpRect->right )
394 pt.x = lpRect->right;
396 if( pt.y < lpRect->top )
397 pt.y = lpRect->top;
398 else
399 if( pt.y > lpRect->bottom )
400 pt.y = lpRect->bottom;
402 return pt;
406 /***********************************************************************
407 * SCROLL_HitTest
409 * Scroll-bar hit testing (don't confuse this with WM_NCHITTEST!).
411 static enum SCROLL_HITTEST SCROLL_HitTest( HWND hwnd, INT nBar,
412 POINT pt, BOOL bDragging )
414 INT arrowSize, thumbSize, thumbPos;
415 RECT rect;
417 BOOL vertical = SCROLL_GetScrollBarRect( hwnd, nBar, &rect,
418 &arrowSize, &thumbSize, &thumbPos );
420 if ( (bDragging && !SCROLL_PtInRectEx( &rect, pt, vertical )) ||
421 (!PtInRect( &rect, pt )) ) return SCROLL_NOWHERE;
423 if (vertical)
425 if (pt.y < rect.top + arrowSize) return SCROLL_TOP_ARROW;
426 if (pt.y >= rect.bottom - arrowSize) return SCROLL_BOTTOM_ARROW;
427 if (!thumbPos) return SCROLL_TOP_RECT;
428 pt.y -= rect.top;
429 if (pt.y < thumbPos) return SCROLL_TOP_RECT;
430 if (pt.y >= thumbPos + thumbSize) return SCROLL_BOTTOM_RECT;
432 else /* horizontal */
434 if (pt.x < rect.left + arrowSize) return SCROLL_TOP_ARROW;
435 if (pt.x >= rect.right - arrowSize) return SCROLL_BOTTOM_ARROW;
436 if (!thumbPos) return SCROLL_TOP_RECT;
437 pt.x -= rect.left;
438 if (pt.x < thumbPos) return SCROLL_TOP_RECT;
439 if (pt.x >= thumbPos + thumbSize) return SCROLL_BOTTOM_RECT;
441 return SCROLL_THUMB;
445 /***********************************************************************
446 * SCROLL_DrawArrows
448 * Draw the scroll bar arrows.
450 static void SCROLL_DrawArrows( HDC hdc, SCROLLBAR_INFO *infoPtr,
451 RECT *rect, INT arrowSize, BOOL vertical,
452 BOOL top_pressed, BOOL bottom_pressed )
454 RECT r;
456 r = *rect;
457 if( vertical )
458 r.bottom = r.top + arrowSize;
459 else
460 r.right = r.left + arrowSize;
462 DrawFrameControl( hdc, &r, DFC_SCROLL,
463 (vertical ? DFCS_SCROLLUP : DFCS_SCROLLLEFT)
464 | (top_pressed ? (DFCS_PUSHED | DFCS_FLAT) : 0 )
465 | (infoPtr->flags&ESB_DISABLE_LTUP ? DFCS_INACTIVE : 0 ) );
467 r = *rect;
468 if( vertical )
469 r.top = r.bottom-arrowSize;
470 else
471 r.left = r.right-arrowSize;
473 DrawFrameControl( hdc, &r, DFC_SCROLL,
474 (vertical ? DFCS_SCROLLDOWN : DFCS_SCROLLRIGHT)
475 | (bottom_pressed ? (DFCS_PUSHED | DFCS_FLAT) : 0 )
476 | (infoPtr->flags&ESB_DISABLE_RTDN ? DFCS_INACTIVE : 0) );
479 /***********************************************************************
480 * SCROLL_DrawInterior
482 * Draw the scroll bar interior (everything except the arrows).
484 static void SCROLL_DrawInterior( HWND hwnd, HDC hdc, INT nBar,
485 RECT *rect, INT arrowSize,
486 INT thumbSize, INT thumbPos,
487 UINT flags, BOOL vertical,
488 BOOL top_selected, BOOL bottom_selected )
490 RECT r;
491 HPEN hSavePen;
492 HBRUSH hSaveBrush,hBrush;
494 /* Select the correct brush and pen */
496 /* Only scrollbar controls send WM_CTLCOLORSCROLLBAR.
497 * The window-owned scrollbars need to call DEFWND_ControlColor
498 * to correctly setup default scrollbar colors
500 if (nBar == SB_CTL) {
501 hBrush = (HBRUSH)SendMessageW( GetParent(hwnd), WM_CTLCOLORSCROLLBAR,
502 (WPARAM)hdc,(LPARAM)hwnd);
503 } else {
504 hBrush = DEFWND_ControlColor( hdc, CTLCOLOR_SCROLLBAR );
506 hSavePen = SelectObject( hdc, SYSCOLOR_GetPen(COLOR_WINDOWFRAME) );
507 hSaveBrush = SelectObject( hdc, hBrush );
509 /* Calculate the scroll rectangle */
511 r = *rect;
512 if (vertical)
514 r.top += arrowSize - SCROLL_ARROW_THUMB_OVERLAP;
515 r.bottom -= (arrowSize - SCROLL_ARROW_THUMB_OVERLAP);
517 else
519 r.left += arrowSize - SCROLL_ARROW_THUMB_OVERLAP;
520 r.right -= (arrowSize - SCROLL_ARROW_THUMB_OVERLAP);
523 /* Draw the scroll bar frame */
525 /* Draw the scroll rectangles and thumb */
527 if (!thumbPos) /* No thumb to draw */
529 PatBlt( hdc, r.left, r.top, r.right - r.left, r.bottom - r.top, PATCOPY );
531 /* cleanup and return */
532 SelectObject( hdc, hSavePen );
533 SelectObject( hdc, hSaveBrush );
534 return;
537 if (vertical)
539 PatBlt( hdc, r.left, r.top, r.right - r.left,
540 thumbPos - (arrowSize - SCROLL_ARROW_THUMB_OVERLAP),
541 top_selected ? 0x0f0000 : PATCOPY );
542 r.top += thumbPos - (arrowSize - SCROLL_ARROW_THUMB_OVERLAP);
543 PatBlt( hdc, r.left, r.top + thumbSize, r.right - r.left,
544 r.bottom - r.top - thumbSize,
545 bottom_selected ? 0x0f0000 : PATCOPY );
546 r.bottom = r.top + thumbSize;
548 else /* horizontal */
550 PatBlt( hdc, r.left, r.top,
551 thumbPos - (arrowSize - SCROLL_ARROW_THUMB_OVERLAP),
552 r.bottom - r.top, top_selected ? 0x0f0000 : PATCOPY );
553 r.left += thumbPos - (arrowSize - SCROLL_ARROW_THUMB_OVERLAP);
554 PatBlt( hdc, r.left + thumbSize, r.top, r.right - r.left - thumbSize,
555 r.bottom - r.top, bottom_selected ? 0x0f0000 : PATCOPY );
556 r.right = r.left + thumbSize;
559 /* Draw the thumb */
561 SelectObject( hdc, GetSysColorBrush(COLOR_BTNFACE) );
562 Rectangle( hdc, r.left+1, r.top+1, r.right-1, r.bottom-1 );
563 DrawEdge( hdc, &r, EDGE_RAISED, BF_RECT );
565 /* cleanup */
566 SelectObject( hdc, hSavePen );
567 SelectObject( hdc, hSaveBrush );
570 void WINAPI USER_ScrollBarDraw( HWND hwnd, HDC hdc, INT nBar, enum SCROLL_HITTEST hit_test,
571 const struct SCROLL_TRACKING_INFO *tracking_info, BOOL arrows,
572 BOOL interior, RECT *rect, INT arrowSize, INT thumbPos,
573 INT thumbSize, BOOL vertical )
575 SCROLLBAR_INFO *infoPtr;
577 if (nBar == SB_CTL)
579 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
581 if (style & SBS_SIZEGRIP)
583 RECT rc = *rect;
585 FillRect( hdc, &rc, GetSysColorBrush( COLOR_SCROLLBAR ) );
586 rc.left = max( rc.left, rc.right - GetSystemMetrics( SM_CXVSCROLL ) - 1 );
587 rc.top = max( rc.top, rc.bottom - GetSystemMetrics( SM_CYHSCROLL ) - 1 );
588 DrawFrameControl( hdc, &rc, DFC_SCROLL, DFCS_SCROLLSIZEGRIP );
589 return;
592 if (style & SBS_SIZEBOX)
594 FillRect( hdc, rect, GetSysColorBrush( COLOR_SCROLLBAR ) );
595 return;
599 if (!(infoPtr = SCROLL_GetInternalInfo( hwnd, nBar, TRUE )))
600 return;
602 /* Draw the arrows */
604 if (arrows && arrowSize)
606 if (vertical == tracking_info->vertical && GetCapture() == hwnd)
607 SCROLL_DrawArrows( hdc, infoPtr, rect, arrowSize, vertical,
608 hit_test == tracking_info->hit_test && hit_test == SCROLL_TOP_ARROW,
609 hit_test == tracking_info->hit_test && hit_test == SCROLL_BOTTOM_ARROW );
610 else
611 SCROLL_DrawArrows( hdc, infoPtr, rect, arrowSize, vertical, FALSE, FALSE );
614 if (interior)
616 if (vertical == tracking_info->vertical && GetCapture() == hwnd)
618 SCROLL_DrawInterior( hwnd, hdc, nBar, rect, arrowSize, thumbSize, thumbPos,
619 infoPtr->flags, vertical,
620 hit_test == tracking_info->hit_test && hit_test == SCROLL_TOP_RECT,
621 hit_test == tracking_info->hit_test && hit_test == SCROLL_BOTTOM_RECT );
623 else
625 SCROLL_DrawInterior( hwnd, hdc, nBar, rect, arrowSize, thumbSize, thumbPos,
626 infoPtr->flags, vertical, FALSE, FALSE );
630 /* if scroll bar has focus, reposition the caret */
631 if(hwnd==GetFocus() && (nBar==SB_CTL))
633 if (!vertical)
635 SetCaretPos(thumbPos + 1, rect->top + 1);
637 else
639 SetCaretPos(rect->top + 1, thumbPos + 1);
644 /***********************************************************************
645 * SCROLL_DrawScrollBar
647 * Redraw the whole scrollbar.
649 void SCROLL_DrawScrollBar( HWND hwnd, HDC hdc, INT bar, enum SCROLL_HITTEST hit_test,
650 const struct SCROLL_TRACKING_INFO *tracking_info, BOOL draw_arrows,
651 BOOL draw_interior )
653 INT arrow_size, thumb_size, thumb_pos;
654 BOOL vertical;
655 DWORD style;
656 RECT rect;
658 if (!(hwnd = WIN_GetFullHandle( hwnd )))
659 return;
661 style = GetWindowLongW( hwnd, GWL_STYLE );
662 if ((bar == SB_VERT && !(style & WS_VSCROLL)) || (bar == SB_HORZ && !(style & WS_HSCROLL)))
663 return;
665 if (!WIN_IsWindowDrawable( hwnd, FALSE ))
666 return;
668 SCROLL_GetScrollBarDrawInfo( hwnd, bar, tracking_info, &rect, &arrow_size, &thumb_size,
669 &thumb_pos, &vertical );
670 /* do not draw if the scrollbar rectangle is empty */
671 if (IsRectEmpty( &rect ))
672 return;
674 TRACE("hwnd %p, hdc %p, bar %d, hit_test %d, tracking_info(win %p, bar %d, thumb_pos %d, "
675 "track_pos %d, vertical %d, hit_test %d), draw_arrows %d, draw_interior %d, rect %s, "
676 "arrow_size %d, thumb_pos %d, thumb_val %d, vertical %d, captured window %p\n", hwnd, hdc,
677 bar, hit_test, tracking_info->win, tracking_info->bar, tracking_info->thumb_pos,
678 tracking_info->thumb_val, tracking_info->vertical, tracking_info->hit_test, draw_arrows,
679 draw_interior, wine_dbgstr_rect(&rect), arrow_size, thumb_pos, thumb_size, vertical,
680 GetCapture());
681 user_api->pScrollBarDraw( hwnd, hdc, bar, hit_test, tracking_info, draw_arrows, draw_interior,
682 &rect, arrow_size, thumb_pos, thumb_size, vertical );
685 void SCROLL_DrawNCScrollBar( HWND hwnd, HDC hdc, BOOL draw_horizontal, BOOL draw_vertical )
687 if (draw_horizontal)
688 SCROLL_DrawScrollBar( hwnd, hdc, SB_HORZ, g_tracking_info.hit_test, &g_tracking_info, TRUE, TRUE );
689 if (draw_vertical)
690 SCROLL_DrawScrollBar( hwnd, hdc, SB_VERT, g_tracking_info.hit_test, &g_tracking_info, TRUE, TRUE );
693 /***********************************************************************
694 * SCROLL_RefreshScrollBar
696 * Repaint the scroll bar interior after a SetScrollRange() or
697 * SetScrollPos() call.
699 static void SCROLL_RefreshScrollBar( HWND hwnd, INT nBar,
700 BOOL arrows, BOOL interior )
702 HDC hdc = GetDCEx( hwnd, 0,
703 DCX_CACHE | ((nBar == SB_CTL) ? 0 : DCX_WINDOW) );
704 if (!hdc) return;
706 SCROLL_DrawScrollBar( hwnd, hdc, nBar, g_tracking_info.hit_test, &g_tracking_info, arrows, interior );
707 ReleaseDC( hwnd, hdc );
711 /***********************************************************************
712 * SCROLL_HandleKbdEvent
714 * Handle a keyboard event (only for SB_CTL scrollbars with focus).
716 * PARAMS
717 * hwnd [I] Handle of window with scrollbar(s)
718 * wParam [I] Variable input including enable state
719 * lParam [I] Variable input including input point
721 static void SCROLL_HandleKbdEvent(HWND hwnd, WPARAM wParam, LPARAM lParam)
723 TRACE("hwnd=%p wParam=%ld lParam=%ld\n", hwnd, wParam, lParam);
725 /* hide caret on first KEYDOWN to prevent flicker */
726 if ((lParam & PFD_DOUBLEBUFFER_DONTCARE) == 0)
727 HideCaret(hwnd);
729 switch(wParam)
731 case VK_PRIOR: wParam = SB_PAGEUP; break;
732 case VK_NEXT: wParam = SB_PAGEDOWN; break;
733 case VK_HOME: wParam = SB_TOP; break;
734 case VK_END: wParam = SB_BOTTOM; break;
735 case VK_UP: wParam = SB_LINEUP; break;
736 case VK_DOWN: wParam = SB_LINEDOWN; break;
737 case VK_LEFT: wParam = SB_LINEUP; break;
738 case VK_RIGHT: wParam = SB_LINEDOWN; break;
739 default: return;
741 SendMessageW(GetParent(hwnd),
742 ((GetWindowLongW( hwnd, GWL_STYLE ) & SBS_VERT) ?
743 WM_VSCROLL : WM_HSCROLL), wParam, (LPARAM)hwnd);
747 /***********************************************************************
748 * SCROLL_HandleScrollEvent
750 * Handle a mouse or timer event for the scrollbar.
751 * 'pt' is the location of the mouse event in client (for SB_CTL) or
752 * windows coordinates.
754 void SCROLL_HandleScrollEvent( HWND hwnd, INT nBar, UINT msg, POINT pt )
756 /* Previous mouse position for timer events */
757 static POINT prevPt;
758 /* Thumb position when tracking started. */
759 static UINT trackThumbPos;
760 /* Position in the scroll-bar of the last button-down event. */
761 static INT lastClickPos;
762 /* Position in the scroll-bar of the last mouse event. */
763 static INT lastMousePos;
765 enum SCROLL_HITTEST hittest;
766 HWND hwndOwner, hwndCtl;
767 TRACKMOUSEEVENT tme;
768 BOOL vertical;
769 INT arrowSize, thumbSize, thumbPos;
770 RECT rect;
771 HDC hdc;
773 SCROLLBAR_INFO *infoPtr = SCROLL_GetInternalInfo( hwnd, nBar, FALSE );
774 if (!infoPtr) return;
775 if ((g_tracking_info.hit_test == SCROLL_NOWHERE)
776 && (msg != WM_LBUTTONDOWN && msg != WM_MOUSEMOVE && msg != WM_MOUSELEAVE
777 && msg != WM_NCMOUSEMOVE && msg != WM_NCMOUSELEAVE))
778 return;
780 if (nBar == SB_CTL && (GetWindowLongW( hwnd, GWL_STYLE ) & (SBS_SIZEGRIP | SBS_SIZEBOX)))
782 switch(msg)
784 case WM_LBUTTONDOWN: /* Initialise mouse tracking */
785 HideCaret(hwnd); /* hide caret while holding down LBUTTON */
786 SetCapture( hwnd );
787 prevPt = pt;
788 g_tracking_info.hit_test = hittest = SCROLL_THUMB;
789 break;
790 case WM_MOUSEMOVE:
791 GetClientRect(GetParent(GetParent(hwnd)),&rect);
792 prevPt = pt;
793 break;
794 case WM_LBUTTONUP:
795 ReleaseCapture();
796 g_tracking_info.hit_test = hittest = SCROLL_NOWHERE;
797 if (hwnd==GetFocus()) ShowCaret(hwnd);
798 break;
799 case WM_SYSTIMER:
800 pt = prevPt;
801 break;
803 return;
806 hdc = GetDCEx( hwnd, 0, DCX_CACHE | ((nBar == SB_CTL) ? 0 : DCX_WINDOW));
807 vertical = SCROLL_GetScrollBarRect( hwnd, nBar, &rect,
808 &arrowSize, &thumbSize, &thumbPos );
809 hwndOwner = (nBar == SB_CTL) ? GetParent(hwnd) : hwnd;
810 hwndCtl = (nBar == SB_CTL) ? hwnd : 0;
812 switch(msg)
814 case WM_LBUTTONDOWN: /* Initialise mouse tracking */
815 HideCaret(hwnd); /* hide caret while holding down LBUTTON */
816 g_tracking_info.vertical = vertical;
817 g_tracking_info.hit_test = hittest = SCROLL_HitTest( hwnd, nBar, pt, FALSE );
818 lastClickPos = vertical ? (pt.y - rect.top) : (pt.x - rect.left);
819 lastMousePos = lastClickPos;
820 trackThumbPos = thumbPos;
821 prevPt = pt;
822 if (nBar == SB_CTL && (GetWindowLongW(hwnd, GWL_STYLE) & WS_TABSTOP)) SetFocus( hwnd );
823 SetCapture( hwnd );
824 break;
826 case WM_MOUSEMOVE:
827 hittest = SCROLL_HitTest( hwnd, nBar, pt, vertical == g_tracking_info.vertical && GetCapture() == hwnd );
828 prevPt = pt;
830 if (nBar != SB_CTL)
831 break;
833 tme.cbSize = sizeof(tme);
834 tme.dwFlags = TME_QUERY;
835 TrackMouseEvent( &tme );
836 if (!(tme.dwFlags & TME_LEAVE) || tme.hwndTrack != hwnd)
838 tme.dwFlags = TME_LEAVE;
839 tme.hwndTrack = hwnd;
840 TrackMouseEvent( &tme );
843 break;
845 case WM_NCMOUSEMOVE:
846 hittest = SCROLL_HitTest( hwnd, nBar, pt, vertical == g_tracking_info.vertical && GetCapture() == hwnd );
847 prevPt = pt;
849 if (nBar == SB_CTL)
850 break;
852 tme.cbSize = sizeof(tme);
853 tme.dwFlags = TME_QUERY;
854 TrackMouseEvent( &tme );
855 if (((tme.dwFlags & (TME_NONCLIENT | TME_LEAVE)) != (TME_NONCLIENT | TME_LEAVE)) || tme.hwndTrack != hwnd)
857 tme.dwFlags = TME_NONCLIENT | TME_LEAVE;
858 tme.hwndTrack = hwnd;
859 TrackMouseEvent( &tme );
862 break;
864 case WM_NCMOUSELEAVE:
865 if (nBar == SB_CTL)
866 return;
868 hittest = SCROLL_NOWHERE;
869 break;
871 case WM_MOUSELEAVE:
872 if (nBar != SB_CTL)
873 return;
875 hittest = SCROLL_NOWHERE;
876 break;
878 case WM_LBUTTONUP:
879 hittest = SCROLL_NOWHERE;
880 ReleaseCapture();
881 /* if scrollbar has focus, show back caret */
882 if (hwnd==GetFocus()) ShowCaret(hwnd);
883 break;
885 case WM_SYSTIMER:
886 pt = prevPt;
887 hittest = SCROLL_HitTest( hwnd, nBar, pt, FALSE );
888 break;
890 default:
891 return; /* Should never happen */
894 TRACE("Event: hwnd=%p bar=%d msg=%s pt=%d,%d hit=%d\n",
895 hwnd, nBar, SPY_GetMsgName(msg,hwnd), pt.x, pt.y, hittest );
897 switch (g_tracking_info.hit_test)
899 case SCROLL_NOWHERE: /* No tracking in progress */
900 if (msg == WM_MOUSEMOVE || msg == WM_MOUSELEAVE || msg == WM_NCMOUSEMOVE || msg == WM_NCMOUSELEAVE)
901 SCROLL_DrawScrollBar( hwnd, hdc, nBar, hittest, &g_tracking_info, TRUE, TRUE );
902 break;
904 case SCROLL_TOP_ARROW:
905 SCROLL_DrawScrollBar( hwnd, hdc, nBar, hittest, &g_tracking_info, TRUE, FALSE );
906 if (hittest == g_tracking_info.hit_test)
908 if ((msg == WM_LBUTTONDOWN) || (msg == WM_SYSTIMER))
910 SendMessageW( hwndOwner, vertical ? WM_VSCROLL : WM_HSCROLL,
911 SB_LINEUP, (LPARAM)hwndCtl );
914 SetSystemTimer( hwnd, SCROLL_TIMER, (msg == WM_LBUTTONDOWN) ?
915 SCROLL_FIRST_DELAY : SCROLL_REPEAT_DELAY, NULL );
917 else KillSystemTimer( hwnd, SCROLL_TIMER );
918 break;
920 case SCROLL_TOP_RECT:
921 SCROLL_DrawScrollBar( hwnd, hdc, nBar, hittest, &g_tracking_info, FALSE, TRUE );
922 if (hittest == g_tracking_info.hit_test)
924 if ((msg == WM_LBUTTONDOWN) || (msg == WM_SYSTIMER))
926 SendMessageW( hwndOwner, vertical ? WM_VSCROLL : WM_HSCROLL,
927 SB_PAGEUP, (LPARAM)hwndCtl );
929 SetSystemTimer( hwnd, SCROLL_TIMER, (msg == WM_LBUTTONDOWN) ?
930 SCROLL_FIRST_DELAY : SCROLL_REPEAT_DELAY, NULL );
932 else KillSystemTimer( hwnd, SCROLL_TIMER );
933 break;
935 case SCROLL_THUMB:
936 if (msg == WM_LBUTTONDOWN)
938 g_tracking_info.win = hwnd;
939 g_tracking_info.bar = nBar;
940 g_tracking_info.thumb_pos = trackThumbPos + lastMousePos - lastClickPos;
941 g_tracking_info.thumb_val = SCROLL_GetThumbVal( infoPtr, &rect, vertical,
942 g_tracking_info.thumb_pos );
943 if (!SCROLL_MovingThumb)
945 SCROLL_MovingThumb = TRUE;
946 SCROLL_DrawScrollBar( hwnd, hdc, nBar, hittest, &g_tracking_info, FALSE, TRUE );
949 else if (msg == WM_LBUTTONUP)
951 SCROLL_DrawScrollBar( hwnd, hdc, nBar, SCROLL_NOWHERE, &g_tracking_info, FALSE, TRUE );
953 else if (msg == WM_MOUSEMOVE)
955 INT pos;
957 if (!SCROLL_PtInRectEx( &rect, pt, vertical )) pos = lastClickPos;
958 else
960 pt = SCROLL_ClipPos( &rect, pt );
961 pos = vertical ? (pt.y - rect.top) : (pt.x - rect.left);
963 if ( (pos != lastMousePos) || (!SCROLL_MovingThumb) )
965 lastMousePos = pos;
966 g_tracking_info.thumb_pos = trackThumbPos + pos - lastClickPos;
967 g_tracking_info.thumb_val = SCROLL_GetThumbVal( infoPtr, &rect, vertical,
968 g_tracking_info.thumb_pos );
969 SendMessageW( hwndOwner, vertical ? WM_VSCROLL : WM_HSCROLL,
970 MAKEWPARAM( SB_THUMBTRACK, g_tracking_info.thumb_val ),
971 (LPARAM)hwndCtl );
972 SCROLL_MovingThumb = TRUE;
973 SCROLL_DrawScrollBar( hwnd, hdc, nBar, hittest, &g_tracking_info, FALSE, TRUE );
976 break;
978 case SCROLL_BOTTOM_RECT:
979 SCROLL_DrawScrollBar( hwnd, hdc, nBar, hittest, &g_tracking_info, FALSE, TRUE );
980 if (hittest == g_tracking_info.hit_test)
982 if ((msg == WM_LBUTTONDOWN) || (msg == WM_SYSTIMER))
984 SendMessageW( hwndOwner, vertical ? WM_VSCROLL : WM_HSCROLL,
985 SB_PAGEDOWN, (LPARAM)hwndCtl );
987 SetSystemTimer( hwnd, SCROLL_TIMER, (msg == WM_LBUTTONDOWN) ?
988 SCROLL_FIRST_DELAY : SCROLL_REPEAT_DELAY, NULL );
990 else KillSystemTimer( hwnd, SCROLL_TIMER );
991 break;
993 case SCROLL_BOTTOM_ARROW:
994 SCROLL_DrawScrollBar( hwnd, hdc, nBar, hittest, &g_tracking_info, TRUE, FALSE );
995 if (hittest == g_tracking_info.hit_test)
997 if ((msg == WM_LBUTTONDOWN) || (msg == WM_SYSTIMER))
999 SendMessageW( hwndOwner, vertical ? WM_VSCROLL : WM_HSCROLL,
1000 SB_LINEDOWN, (LPARAM)hwndCtl );
1003 SetSystemTimer( hwnd, SCROLL_TIMER, (msg == WM_LBUTTONDOWN) ?
1004 SCROLL_FIRST_DELAY : SCROLL_REPEAT_DELAY, NULL );
1006 else KillSystemTimer( hwnd, SCROLL_TIMER );
1007 break;
1010 if (msg == WM_LBUTTONDOWN)
1013 if (hittest == SCROLL_THUMB)
1015 UINT val = SCROLL_GetThumbVal( infoPtr, &rect, vertical,
1016 trackThumbPos + lastMousePos - lastClickPos );
1017 SendMessageW( hwndOwner, vertical ? WM_VSCROLL : WM_HSCROLL,
1018 MAKEWPARAM( SB_THUMBTRACK, val ), (LPARAM)hwndCtl );
1022 if (msg == WM_LBUTTONUP)
1024 hittest = g_tracking_info.hit_test;
1025 g_tracking_info.hit_test = SCROLL_NOWHERE; /* Terminate tracking */
1027 if (hittest == SCROLL_THUMB)
1029 UINT val = SCROLL_GetThumbVal( infoPtr, &rect, vertical,
1030 trackThumbPos + lastMousePos - lastClickPos );
1031 SendMessageW( hwndOwner, vertical ? WM_VSCROLL : WM_HSCROLL,
1032 MAKEWPARAM( SB_THUMBPOSITION, val ), (LPARAM)hwndCtl );
1034 /* SB_ENDSCROLL doesn't report thumb position */
1035 SendMessageW( hwndOwner, vertical ? WM_VSCROLL : WM_HSCROLL,
1036 SB_ENDSCROLL, (LPARAM)hwndCtl );
1038 /* Terminate tracking */
1039 g_tracking_info.win = 0;
1040 SCROLL_MovingThumb = FALSE;
1041 hittest = SCROLL_NOWHERE;
1042 SCROLL_DrawScrollBar( hwnd, hdc, nBar, hittest, &g_tracking_info, TRUE, TRUE );
1045 ReleaseDC( hwnd, hdc );
1049 /***********************************************************************
1050 * SCROLL_TrackScrollBar
1052 * Track a mouse button press on a scroll-bar.
1053 * pt is in screen-coordinates for non-client scroll bars.
1055 void SCROLL_TrackScrollBar( HWND hwnd, INT scrollbar, POINT pt )
1057 MSG msg;
1058 RECT rect;
1060 if (scrollbar != SB_CTL)
1062 WIN_GetRectangles( hwnd, COORDS_CLIENT, &rect, NULL );
1063 ScreenToClient( hwnd, &pt );
1064 pt.x -= rect.left;
1065 pt.y -= rect.top;
1067 else
1068 rect.left = rect.top = 0;
1070 SCROLL_HandleScrollEvent( hwnd, scrollbar, WM_LBUTTONDOWN, pt );
1074 if (!GetMessageW( &msg, 0, 0, 0 )) break;
1075 if (CallMsgFilterW( &msg, MSGF_SCROLLBAR )) continue;
1076 if (msg.message == WM_LBUTTONUP ||
1077 msg.message == WM_MOUSEMOVE ||
1078 msg.message == WM_MOUSELEAVE ||
1079 msg.message == WM_NCMOUSEMOVE ||
1080 msg.message == WM_NCMOUSELEAVE ||
1081 (msg.message == WM_SYSTIMER && msg.wParam == SCROLL_TIMER))
1083 pt.x = (short)LOWORD(msg.lParam) - rect.left;
1084 pt.y = (short)HIWORD(msg.lParam) - rect.top;
1085 SCROLL_HandleScrollEvent( hwnd, scrollbar, msg.message, pt );
1087 else
1089 TranslateMessage( &msg );
1090 DispatchMessageW( &msg );
1092 if (!IsWindow( hwnd ))
1094 ReleaseCapture();
1095 break;
1097 } while (msg.message != WM_LBUTTONUP && GetCapture() == hwnd);
1101 /***********************************************************************
1102 * SCROLL_CreateScrollBar
1104 * Create a scroll bar
1106 * PARAMS
1107 * hwnd [I] Handle of window with scrollbar(s)
1108 * lpCreate [I] The style and place of the scroll bar
1110 static void SCROLL_CreateScrollBar(HWND hwnd, LPCREATESTRUCTW lpCreate)
1112 LPSCROLLBAR_INFO info = NULL;
1113 WND *win;
1115 TRACE("hwnd=%p lpCreate=%p\n", hwnd, lpCreate);
1117 win = WIN_GetPtr(hwnd);
1118 if (win->cbWndExtra >= sizeof(SCROLLBAR_WNDDATA))
1120 SCROLLBAR_WNDDATA *data = (SCROLLBAR_WNDDATA*)win->wExtra;
1121 data->magic = SCROLLBAR_MAGIC;
1122 info = &data->info;
1124 else WARN("Not enough extra data\n");
1125 WIN_ReleasePtr(win);
1126 if (!info) return;
1128 if (lpCreate->style & WS_DISABLED)
1130 info->flags = ESB_DISABLE_BOTH;
1131 TRACE("Created WS_DISABLED scrollbar\n");
1134 if (lpCreate->style & (SBS_SIZEGRIP | SBS_SIZEBOX))
1136 if (lpCreate->style & SBS_SIZEBOXTOPLEFTALIGN)
1137 MoveWindow( hwnd, lpCreate->x, lpCreate->y, GetSystemMetrics(SM_CXVSCROLL)+1,
1138 GetSystemMetrics(SM_CYHSCROLL)+1, FALSE );
1139 else if(lpCreate->style & SBS_SIZEBOXBOTTOMRIGHTALIGN)
1140 MoveWindow( hwnd, lpCreate->x+lpCreate->cx-GetSystemMetrics(SM_CXVSCROLL)-1,
1141 lpCreate->y+lpCreate->cy-GetSystemMetrics(SM_CYHSCROLL)-1,
1142 GetSystemMetrics(SM_CXVSCROLL)+1,
1143 GetSystemMetrics(SM_CYHSCROLL)+1, FALSE );
1145 else if (lpCreate->style & SBS_VERT)
1147 if (lpCreate->style & SBS_LEFTALIGN)
1148 MoveWindow( hwnd, lpCreate->x, lpCreate->y,
1149 GetSystemMetrics(SM_CXVSCROLL)+1, lpCreate->cy, FALSE );
1150 else if (lpCreate->style & SBS_RIGHTALIGN)
1151 MoveWindow( hwnd,
1152 lpCreate->x+lpCreate->cx-GetSystemMetrics(SM_CXVSCROLL)-1,
1153 lpCreate->y,
1154 GetSystemMetrics(SM_CXVSCROLL)+1, lpCreate->cy, FALSE );
1156 else /* SBS_HORZ */
1158 if (lpCreate->style & SBS_TOPALIGN)
1159 MoveWindow( hwnd, lpCreate->x, lpCreate->y,
1160 lpCreate->cx, GetSystemMetrics(SM_CYHSCROLL)+1, FALSE );
1161 else if (lpCreate->style & SBS_BOTTOMALIGN)
1162 MoveWindow( hwnd,
1163 lpCreate->x,
1164 lpCreate->y+lpCreate->cy-GetSystemMetrics(SM_CYHSCROLL)-1,
1165 lpCreate->cx, GetSystemMetrics(SM_CYHSCROLL)+1, FALSE );
1170 /*************************************************************************
1171 * SCROLL_GetScrollInfo
1173 * Internal helper for the API function
1175 * PARAMS
1176 * hwnd [I] Handle of window with scrollbar(s)
1177 * nBar [I] One of SB_HORZ, SB_VERT, or SB_CTL
1178 * info [IO] fMask specifies which values to retrieve
1180 * RETURNS
1181 * FALSE if requested field not filled (f.i. scroll bar does not exist)
1183 static BOOL SCROLL_GetScrollInfo(HWND hwnd, INT nBar, LPSCROLLINFO info)
1185 LPSCROLLBAR_INFO infoPtr;
1187 /* handle invalid data structure */
1188 if (!SCROLL_ScrollInfoValid(info)
1189 || !(infoPtr = SCROLL_GetInternalInfo(hwnd, nBar, FALSE)))
1190 return FALSE;
1192 /* fill in the desired scroll info structure */
1193 if (info->fMask & SIF_PAGE) info->nPage = infoPtr->page;
1194 if (info->fMask & SIF_POS) info->nPos = infoPtr->curVal;
1195 if ((info->fMask & SIF_TRACKPOS) && (info->cbSize == sizeof(*info)))
1196 info->nTrackPos = (g_tracking_info.win == WIN_GetFullHandle(hwnd)) ? g_tracking_info.thumb_val : infoPtr->curVal;
1197 if (info->fMask & SIF_RANGE)
1199 info->nMin = infoPtr->minVal;
1200 info->nMax = infoPtr->maxVal;
1203 TRACE("cbSize %02x fMask %04x nMin %d nMax %d nPage %u nPos %d nTrackPos %d\n",
1204 info->cbSize, info->fMask, info->nMin, info->nMax, info->nPage,
1205 info->nPos, info->nTrackPos);
1207 return (info->fMask & SIF_ALL) != 0;
1211 /*************************************************************************
1212 * SCROLL_GetScrollBarInfo
1214 * Internal helper for the API function
1216 * PARAMS
1217 * hwnd [I] Handle of window with scrollbar(s)
1218 * idObject [I] One of OBJID_CLIENT, OBJID_HSCROLL, or OBJID_VSCROLL
1219 * info [IO] cbSize specifies the size of the structure
1221 * RETURNS
1222 * FALSE if failed
1224 static BOOL SCROLL_GetScrollBarInfo(HWND hwnd, LONG idObject, LPSCROLLBARINFO info)
1226 LPSCROLLBAR_INFO infoPtr;
1227 INT nBar;
1228 INT nDummy;
1229 DWORD style = GetWindowLongW(hwnd, GWL_STYLE);
1230 BOOL pressed;
1231 RECT rect;
1233 switch (idObject)
1235 case OBJID_CLIENT: nBar = SB_CTL; break;
1236 case OBJID_HSCROLL: nBar = SB_HORZ; break;
1237 case OBJID_VSCROLL: nBar = SB_VERT; break;
1238 default: return FALSE;
1241 /* handle invalid data structure */
1242 if (info->cbSize != sizeof(*info))
1243 return FALSE;
1245 SCROLL_GetScrollBarRect(hwnd, nBar, &info->rcScrollBar, &nDummy,
1246 &info->dxyLineButton, &info->xyThumbTop);
1247 /* rcScrollBar needs to be in screen coordinates */
1248 GetWindowRect(hwnd, &rect);
1249 OffsetRect(&info->rcScrollBar, rect.left, rect.top);
1251 info->xyThumbBottom = info->xyThumbTop + info->dxyLineButton;
1253 infoPtr = SCROLL_GetInternalInfo(hwnd, nBar, TRUE);
1254 if (!infoPtr)
1255 return FALSE;
1257 /* Scroll bar state */
1258 info->rgstate[0] = 0;
1259 if ((nBar == SB_HORZ && !(style & WS_HSCROLL))
1260 || (nBar == SB_VERT && !(style & WS_VSCROLL)))
1261 info->rgstate[0] |= STATE_SYSTEM_INVISIBLE;
1262 if (infoPtr->minVal >= infoPtr->maxVal - max(infoPtr->page - 1, 0))
1264 if (!(info->rgstate[0] & STATE_SYSTEM_INVISIBLE))
1265 info->rgstate[0] |= STATE_SYSTEM_UNAVAILABLE;
1266 else
1267 info->rgstate[0] |= STATE_SYSTEM_OFFSCREEN;
1269 if (nBar == SB_CTL && !IsWindowEnabled(hwnd))
1270 info->rgstate[0] |= STATE_SYSTEM_UNAVAILABLE;
1272 pressed = ((nBar == SB_VERT) == g_tracking_info.vertical && GetCapture() == hwnd);
1274 /* Top/left arrow button state. MSDN says top/right, but I don't believe it */
1275 info->rgstate[1] = 0;
1276 if (pressed && g_tracking_info.hit_test == SCROLL_TOP_ARROW)
1277 info->rgstate[1] |= STATE_SYSTEM_PRESSED;
1278 if (infoPtr->flags & ESB_DISABLE_LTUP)
1279 info->rgstate[1] |= STATE_SYSTEM_UNAVAILABLE;
1281 /* Page up/left region state. MSDN says up/right, but I don't believe it */
1282 info->rgstate[2] = 0;
1283 if (infoPtr->curVal == infoPtr->minVal)
1284 info->rgstate[2] |= STATE_SYSTEM_INVISIBLE;
1285 if (pressed && g_tracking_info.hit_test == SCROLL_TOP_RECT)
1286 info->rgstate[2] |= STATE_SYSTEM_PRESSED;
1288 /* Thumb state */
1289 info->rgstate[3] = 0;
1290 if (pressed && g_tracking_info.hit_test == SCROLL_THUMB)
1291 info->rgstate[3] |= STATE_SYSTEM_PRESSED;
1293 /* Page down/right region state. MSDN says down/left, but I don't believe it */
1294 info->rgstate[4] = 0;
1295 if (infoPtr->curVal >= infoPtr->maxVal - 1)
1296 info->rgstate[4] |= STATE_SYSTEM_INVISIBLE;
1297 if (pressed && g_tracking_info.hit_test == SCROLL_BOTTOM_RECT)
1298 info->rgstate[4] |= STATE_SYSTEM_PRESSED;
1300 /* Bottom/right arrow button state. MSDN says bottom/left, but I don't believe it */
1301 info->rgstate[5] = 0;
1302 if (pressed && g_tracking_info.hit_test == SCROLL_BOTTOM_ARROW)
1303 info->rgstate[5] |= STATE_SYSTEM_PRESSED;
1304 if (infoPtr->flags & ESB_DISABLE_RTDN)
1305 info->rgstate[5] |= STATE_SYSTEM_UNAVAILABLE;
1307 return TRUE;
1311 /*************************************************************************
1312 * SCROLL_GetScrollPos
1314 * Internal helper for the API function
1316 * PARAMS
1317 * hwnd [I] Handle of window with scrollbar(s)
1318 * nBar [I] One of SB_HORZ, SB_VERT, or SB_CTL
1320 static INT SCROLL_GetScrollPos(HWND hwnd, INT nBar)
1322 LPSCROLLBAR_INFO infoPtr = SCROLL_GetInternalInfo(hwnd, nBar, FALSE);
1323 return infoPtr ? infoPtr->curVal: 0;
1327 /*************************************************************************
1328 * SCROLL_GetScrollRange
1330 * Internal helper for the API function
1332 * PARAMS
1333 * hwnd [I] Handle of window with scrollbar(s)
1334 * nBar [I] One of SB_HORZ, SB_VERT, or SB_CTL
1335 * lpMin [O] Where to store minimum value
1336 * lpMax [O] Where to store maximum value
1338 * RETURNS
1339 * Success: TRUE
1340 * Failure: FALSE
1342 static BOOL SCROLL_GetScrollRange(HWND hwnd, INT nBar, LPINT lpMin, LPINT lpMax)
1344 LPSCROLLBAR_INFO infoPtr = SCROLL_GetInternalInfo(hwnd, nBar, FALSE);
1346 if (lpMin) *lpMin = infoPtr ? infoPtr->minVal : 0;
1347 if (lpMax) *lpMax = infoPtr ? infoPtr->maxVal : 0;
1349 return TRUE;
1353 /*************************************************************************
1354 * SCROLL_SetScrollRange
1356 * PARAMS
1357 * hwnd [I] Handle of window with scrollbar(s)
1358 * nBar [I] One of SB_HORZ, SB_VERT, or SB_CTL
1359 * lpMin [I] Minimum value
1360 * lpMax [I] Maximum value
1363 static BOOL SCROLL_SetScrollRange(HWND hwnd, INT nBar, INT minVal, INT maxVal)
1365 LPSCROLLBAR_INFO infoPtr = SCROLL_GetInternalInfo(hwnd, nBar, FALSE);
1367 TRACE("hwnd=%p nBar=%d min=%d max=%d\n", hwnd, nBar, minVal, maxVal);
1369 if (infoPtr)
1371 infoPtr->minVal = minVal;
1372 infoPtr->maxVal = maxVal;
1374 return TRUE;
1377 LRESULT WINAPI USER_ScrollBarProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, BOOL unicode )
1379 if (!IsWindow( hwnd )) return 0;
1381 switch(message)
1383 case WM_CREATE:
1384 SCROLL_CreateScrollBar(hwnd, (LPCREATESTRUCTW)lParam);
1385 break;
1387 case WM_ENABLE:
1389 SCROLLBAR_INFO *infoPtr;
1390 if ((infoPtr = SCROLL_GetInternalInfo( hwnd, SB_CTL, FALSE )))
1392 infoPtr->flags = wParam ? ESB_ENABLE_BOTH : ESB_DISABLE_BOTH;
1393 SCROLL_RefreshScrollBar(hwnd, SB_CTL, TRUE, TRUE);
1396 return 0;
1398 case WM_LBUTTONDBLCLK:
1399 case WM_LBUTTONDOWN:
1400 if (GetWindowLongW( hwnd, GWL_STYLE ) & SBS_SIZEGRIP)
1402 SendMessageW( GetParent(hwnd), WM_SYSCOMMAND,
1403 SC_SIZE + ((GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL) ?
1404 WMSZ_BOTTOMLEFT : WMSZ_BOTTOMRIGHT), lParam );
1406 else
1408 POINT pt;
1409 pt.x = (short)LOWORD(lParam);
1410 pt.y = (short)HIWORD(lParam);
1411 SCROLL_TrackScrollBar( hwnd, SB_CTL, pt );
1413 break;
1414 case WM_LBUTTONUP:
1415 case WM_NCMOUSEMOVE:
1416 case WM_NCMOUSELEAVE:
1417 case WM_MOUSEMOVE:
1418 case WM_MOUSELEAVE:
1419 case WM_SYSTIMER:
1421 POINT pt;
1422 pt.x = (short)LOWORD(lParam);
1423 pt.y = (short)HIWORD(lParam);
1424 SCROLL_HandleScrollEvent( hwnd, SB_CTL, message, pt );
1426 break;
1428 case WM_KEYDOWN:
1429 SCROLL_HandleKbdEvent(hwnd, wParam, lParam);
1430 break;
1432 case WM_KEYUP:
1433 ShowCaret(hwnd);
1434 break;
1436 case WM_SETFOCUS:
1438 /* Create a caret when a ScrollBar get focus */
1439 RECT rect;
1440 int arrowSize, thumbSize, thumbPos, vertical;
1441 vertical = SCROLL_GetScrollBarRect( hwnd, SB_CTL, &rect,
1442 &arrowSize, &thumbSize, &thumbPos );
1443 if (!vertical)
1445 CreateCaret(hwnd, (HBITMAP)1, thumbSize-2, rect.bottom-rect.top-2);
1446 SetCaretPos(thumbPos+1, rect.top+1);
1448 else
1450 CreateCaret(hwnd, (HBITMAP)1, rect.right-rect.left-2,thumbSize-2);
1451 SetCaretPos(rect.top+1, thumbPos+1);
1453 ShowCaret(hwnd);
1455 break;
1457 case WM_KILLFOCUS:
1459 RECT rect;
1460 int arrowSize, thumbSize, thumbPos, vertical;
1461 vertical = SCROLL_GetScrollBarRect( hwnd, SB_CTL, &rect,&arrowSize, &thumbSize, &thumbPos );
1462 if (!vertical){
1463 rect.left=thumbPos+1;
1464 rect.right=rect.left+thumbSize;
1466 else
1468 rect.top=thumbPos+1;
1469 rect.bottom=rect.top+thumbSize;
1471 HideCaret(hwnd);
1472 InvalidateRect(hwnd,&rect,0);
1473 DestroyCaret();
1475 break;
1477 case WM_ERASEBKGND:
1478 return 1;
1480 case WM_GETDLGCODE:
1481 return DLGC_WANTARROWS; /* Windows returns this value */
1483 case WM_PAINT:
1485 PAINTSTRUCT ps;
1486 HDC hdc = wParam ? (HDC)wParam : BeginPaint(hwnd, &ps);
1487 SCROLL_DrawScrollBar( hwnd, hdc, SB_CTL, g_tracking_info.hit_test, &g_tracking_info, TRUE, TRUE );
1488 if (!wParam) EndPaint(hwnd, &ps);
1490 break;
1492 case WM_SETCURSOR:
1493 if (GetWindowLongW( hwnd, GWL_STYLE ) & SBS_SIZEGRIP)
1495 ULONG_PTR cursor = (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL) ? IDC_SIZENESW : IDC_SIZENWSE;
1496 return (LRESULT)SetCursor( LoadCursorA( 0, (LPSTR)cursor ));
1498 return DefWindowProcW( hwnd, message, wParam, lParam );
1500 case SBM_SETPOS:
1501 return SetScrollPos( hwnd, SB_CTL, wParam, (BOOL)lParam );
1503 case SBM_GETPOS:
1504 return SCROLL_GetScrollPos(hwnd, SB_CTL);
1506 case SBM_SETRANGEREDRAW:
1507 case SBM_SETRANGE:
1509 INT oldPos = SCROLL_GetScrollPos( hwnd, SB_CTL );
1510 SCROLL_SetScrollRange( hwnd, SB_CTL, wParam, lParam );
1511 if (message == SBM_SETRANGEREDRAW)
1512 SCROLL_RefreshScrollBar( hwnd, SB_CTL, TRUE, TRUE );
1513 if (oldPos != SCROLL_GetScrollPos( hwnd, SB_CTL )) return oldPos;
1515 return 0;
1517 case SBM_GETRANGE:
1518 return SCROLL_GetScrollRange(hwnd, SB_CTL, (LPINT)wParam, (LPINT)lParam);
1520 case SBM_ENABLE_ARROWS:
1521 return EnableScrollBar( hwnd, SB_CTL, wParam );
1523 case SBM_SETSCROLLINFO:
1524 return SCROLL_SetScrollInfo( hwnd, SB_CTL, (SCROLLINFO *)lParam, wParam );
1526 case SBM_GETSCROLLINFO:
1527 return SCROLL_GetScrollInfo(hwnd, SB_CTL, (SCROLLINFO *)lParam);
1529 case SBM_GETSCROLLBARINFO:
1530 return SCROLL_GetScrollBarInfo(hwnd, OBJID_CLIENT, (SCROLLBARINFO *)lParam);
1532 case 0x00e5:
1533 case 0x00e7:
1534 case 0x00e8:
1535 case 0x00ec:
1536 case 0x00ed:
1537 case 0x00ee:
1538 case 0x00ef:
1539 ERR("unknown Win32 msg %04x wp=%08lx lp=%08lx\n",
1540 message, wParam, lParam );
1541 break;
1543 default:
1544 if (message >= WM_USER)
1545 WARN("unknown msg %04x wp=%04lx lp=%08lx\n",
1546 message, wParam, lParam );
1547 if (unicode)
1548 return DefWindowProcW( hwnd, message, wParam, lParam );
1549 else
1550 return DefWindowProcA( hwnd, message, wParam, lParam );
1552 return 0;
1555 /***********************************************************************
1556 * ScrollBarWndProc_common
1558 LRESULT ScrollBarWndProc_common( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode )
1560 return user_api->pScrollBarWndProc( hwnd, msg, wParam, lParam, unicode );
1563 /*************************************************************************
1564 * SetScrollInfo (USER32.@)
1566 * SetScrollInfo can be used to set the position, upper bound,
1567 * lower bound, and page size of a scrollbar control.
1569 * PARAMS
1570 * hwnd [I] Handle of window with scrollbar(s)
1571 * nBar [I] One of SB_HORZ, SB_VERT, or SB_CTL
1572 * info [I] Specifies what to change and new values
1573 * bRedraw [I] Should scrollbar be redrawn afterwards?
1575 * RETURNS
1576 * Scrollbar position
1578 * NOTE
1579 * For 100 lines of text to be displayed in a window of 25 lines,
1580 * one would for instance use info->nMin=0, info->nMax=75
1581 * (corresponding to the 76 different positions of the window on
1582 * the text), and info->nPage=25.
1584 INT WINAPI DECLSPEC_HOTPATCH SetScrollInfo(HWND hwnd, INT nBar, const SCROLLINFO *info, BOOL bRedraw)
1586 TRACE("hwnd=%p nBar=%d info=%p, bRedraw=%d\n", hwnd, nBar, info, bRedraw);
1588 /* Refer SB_CTL requests to the window */
1589 if (nBar == SB_CTL)
1590 return SendMessageW(hwnd, SBM_SETSCROLLINFO, bRedraw, (LPARAM)info);
1591 else
1592 return SCROLL_SetScrollInfo( hwnd, nBar, info, bRedraw );
1595 static INT SCROLL_SetScrollInfo( HWND hwnd, INT nBar, LPCSCROLLINFO info, BOOL bRedraw )
1597 /* Update the scrollbar state and set action flags according to
1598 * what has to be done graphics wise. */
1600 SCROLLBAR_INFO *infoPtr;
1601 UINT new_flags;
1602 INT action = 0;
1604 /* handle invalid data structure */
1605 if (!SCROLL_ScrollInfoValid(info)
1606 || !(infoPtr = SCROLL_GetInternalInfo(hwnd, nBar, TRUE)))
1607 return 0;
1609 if (TRACE_ON(scroll))
1611 TRACE("hwnd=%p bar=%d", hwnd, nBar);
1612 if (info->fMask & SIF_PAGE) TRACE( " page=%d", info->nPage );
1613 if (info->fMask & SIF_POS) TRACE( " pos=%d", info->nPos );
1614 if (info->fMask & SIF_RANGE) TRACE( " min=%d max=%d", info->nMin, info->nMax );
1615 TRACE("\n");
1618 /* Set the page size */
1620 if (info->fMask & SIF_PAGE)
1622 if( infoPtr->page != info->nPage )
1624 infoPtr->page = info->nPage;
1625 action |= SA_SSI_REFRESH;
1629 /* Set the scroll pos */
1631 if (info->fMask & SIF_POS)
1633 if( infoPtr->curVal != info->nPos )
1635 infoPtr->curVal = info->nPos;
1636 action |= SA_SSI_REFRESH;
1640 /* Set the scroll range */
1642 if (info->fMask & SIF_RANGE)
1644 /* Invalid range -> range is set to (0,0) */
1645 if ((info->nMin > info->nMax) ||
1646 ((UINT)(info->nMax - info->nMin) >= 0x80000000))
1648 action |= SA_SSI_REFRESH;
1649 infoPtr->minVal = 0;
1650 infoPtr->maxVal = 0;
1652 else
1654 if( infoPtr->minVal != info->nMin ||
1655 infoPtr->maxVal != info->nMax )
1657 action |= SA_SSI_REFRESH;
1658 infoPtr->minVal = info->nMin;
1659 infoPtr->maxVal = info->nMax;
1664 /* Make sure the page size is valid */
1665 if (infoPtr->page < 0) infoPtr->page = 0;
1666 else if (infoPtr->page > infoPtr->maxVal - infoPtr->minVal + 1 )
1667 infoPtr->page = infoPtr->maxVal - infoPtr->minVal + 1;
1669 /* Make sure the pos is inside the range */
1671 if (infoPtr->curVal < infoPtr->minVal)
1672 infoPtr->curVal = infoPtr->minVal;
1673 else if (infoPtr->curVal > infoPtr->maxVal - max( infoPtr->page-1, 0 ))
1674 infoPtr->curVal = infoPtr->maxVal - max( infoPtr->page-1, 0 );
1676 TRACE(" new values: page=%d pos=%d min=%d max=%d\n",
1677 infoPtr->page, infoPtr->curVal,
1678 infoPtr->minVal, infoPtr->maxVal );
1680 /* don't change the scrollbar state if SetScrollInfo
1681 * is just called with SIF_DISABLENOSCROLL
1683 if(!(info->fMask & SIF_ALL)) goto done;
1685 /* Check if the scrollbar should be hidden or disabled */
1687 if (info->fMask & (SIF_RANGE | SIF_PAGE | SIF_DISABLENOSCROLL))
1689 new_flags = infoPtr->flags;
1690 if (infoPtr->minVal >= infoPtr->maxVal - max( infoPtr->page-1, 0 ))
1692 /* Hide or disable scroll-bar */
1693 if (info->fMask & SIF_DISABLENOSCROLL)
1695 new_flags = ESB_DISABLE_BOTH;
1696 action |= SA_SSI_REFRESH;
1698 else if ((nBar != SB_CTL) && (action & SA_SSI_REFRESH))
1700 action = SA_SSI_HIDE;
1703 else /* Show and enable scroll-bar only if no page only changed. */
1704 if (info->fMask != SIF_PAGE)
1706 new_flags = ESB_ENABLE_BOTH;
1707 if ((nBar != SB_CTL) && ( (action & SA_SSI_REFRESH) ))
1708 action |= SA_SSI_SHOW;
1711 if (nBar == SB_CTL && bRedraw && IsWindowVisible(hwnd) &&
1712 (new_flags == ESB_ENABLE_BOTH || new_flags == ESB_DISABLE_BOTH))
1714 EnableWindow(hwnd, new_flags == ESB_ENABLE_BOTH);
1717 if (infoPtr->flags != new_flags) /* check arrow flags */
1719 infoPtr->flags = new_flags;
1720 action |= SA_SSI_REPAINT_ARROWS;
1724 done:
1725 if( action & SA_SSI_HIDE )
1726 SCROLL_ShowScrollBar( hwnd, nBar, FALSE, FALSE );
1727 else
1729 if( action & SA_SSI_SHOW )
1730 if( SCROLL_ShowScrollBar( hwnd, nBar, TRUE, TRUE ) )
1731 return infoPtr->curVal; /* SetWindowPos() already did the painting */
1733 if( bRedraw )
1734 SCROLL_RefreshScrollBar( hwnd, nBar, TRUE, TRUE );
1735 else if( action & SA_SSI_REPAINT_ARROWS )
1736 SCROLL_RefreshScrollBar( hwnd, nBar, TRUE, FALSE );
1739 /* Return current position */
1740 return infoPtr->curVal;
1744 /*************************************************************************
1745 * GetScrollInfo (USER32.@)
1747 * GetScrollInfo can be used to retrieve the position, upper bound,
1748 * lower bound, and page size of a scrollbar control.
1750 * PARAMS
1751 * hwnd [I] Handle of window with scrollbar(s)
1752 * nBar [I] One of SB_HORZ, SB_VERT, or SB_CTL
1753 * info [IO] fMask specifies which values to retrieve
1755 * RETURNS
1756 * TRUE if SCROLLINFO is filled
1757 * ( if nBar is SB_CTL, GetScrollInfo returns TRUE even if nothing
1758 * is filled)
1760 BOOL WINAPI DECLSPEC_HOTPATCH GetScrollInfo(HWND hwnd, INT nBar, LPSCROLLINFO info)
1762 TRACE("hwnd=%p nBar=%d info=%p\n", hwnd, nBar, info);
1764 /* Refer SB_CTL requests to the window */
1765 if (nBar == SB_CTL)
1767 SendMessageW(hwnd, SBM_GETSCROLLINFO, 0, (LPARAM)info);
1768 return TRUE;
1770 return SCROLL_GetScrollInfo(hwnd, nBar, info);
1774 /*************************************************************************
1775 * GetScrollBarInfo (USER32.@)
1777 * GetScrollBarInfo can be used to retrieve information about a scrollbar
1778 * control.
1780 * PARAMS
1781 * hwnd [I] Handle of window with scrollbar(s)
1782 * idObject [I] One of OBJID_CLIENT, OBJID_HSCROLL, or OBJID_VSCROLL
1783 * info [IO] cbSize specifies the size of SCROLLBARINFO
1785 * RETURNS
1786 * TRUE if success
1788 BOOL WINAPI GetScrollBarInfo(HWND hwnd, LONG idObject, LPSCROLLBARINFO info)
1790 TRACE("hwnd=%p idObject=%d info=%p\n", hwnd, idObject, info);
1792 /* Refer OBJID_CLIENT requests to the window */
1793 if (idObject == OBJID_CLIENT)
1794 return SendMessageW(hwnd, SBM_GETSCROLLBARINFO, 0, (LPARAM)info);
1795 else
1796 return SCROLL_GetScrollBarInfo(hwnd, idObject, info);
1800 /*************************************************************************
1801 * SetScrollPos (USER32.@)
1803 * Sets the current position of the scroll thumb.
1805 * PARAMS
1806 * hwnd [I] Handle of window with scrollbar(s)
1807 * nBar [I] One of SB_HORZ, SB_VERT, or SB_CTL
1808 * nPos [I] New value
1809 * bRedraw [I] Should scrollbar be redrawn afterwards?
1811 * RETURNS
1812 * Success: Scrollbar position
1813 * Failure: 0
1815 * REMARKS
1816 * Note the ambiguity when 0 is returned. Use GetLastError
1817 * to make sure there was an error (and to know which one).
1819 INT WINAPI DECLSPEC_HOTPATCH SetScrollPos( HWND hwnd, INT nBar, INT nPos, BOOL bRedraw)
1821 SCROLLINFO info;
1822 SCROLLBAR_INFO *infoPtr;
1823 INT oldPos = 0;
1825 if ((infoPtr = SCROLL_GetInternalInfo( hwnd, nBar, FALSE ))) oldPos = infoPtr->curVal;
1826 info.cbSize = sizeof(info);
1827 info.nPos = nPos;
1828 info.fMask = SIF_POS;
1829 SetScrollInfo( hwnd, nBar, &info, bRedraw );
1830 return oldPos;
1834 /*************************************************************************
1835 * GetScrollPos (USER32.@)
1837 * Gets the current position of the scroll thumb.
1839 * PARAMS
1840 * hwnd [I] Handle of window with scrollbar(s)
1841 * nBar [I] One of SB_HORZ, SB_VERT, or SB_CTL
1843 * RETURNS
1844 * Success: Current position
1845 * Failure: 0
1847 * REMARKS
1848 * There is ambiguity when 0 is returned. Use GetLastError
1849 * to make sure there was an error (and to know which one).
1851 INT WINAPI DECLSPEC_HOTPATCH GetScrollPos(HWND hwnd, INT nBar)
1853 TRACE("hwnd=%p nBar=%d\n", hwnd, nBar);
1855 /* Refer SB_CTL requests to the window */
1856 if (nBar == SB_CTL)
1857 return SendMessageW(hwnd, SBM_GETPOS, 0, 0);
1858 else
1859 return SCROLL_GetScrollPos(hwnd, nBar);
1863 /*************************************************************************
1864 * SetScrollRange (USER32.@)
1865 * The SetScrollRange function sets the minimum and maximum scroll box positions
1866 * If nMinPos and nMaxPos is the same value, the scroll bar will hide
1868 * Sets the range of the scroll bar.
1870 * PARAMS
1871 * hwnd [I] Handle of window with scrollbar(s)
1872 * nBar [I] One of SB_HORZ, SB_VERT, or SB_CTL
1873 * minVal [I] New minimum value
1874 * maxVal [I] New Maximum value
1875 * bRedraw [I] Should scrollbar be redrawn afterwards?
1877 * RETURNS
1878 * Success: TRUE
1879 * Failure: FALSE
1881 BOOL WINAPI DECLSPEC_HOTPATCH SetScrollRange(HWND hwnd, INT nBar, INT minVal, INT maxVal, BOOL bRedraw)
1883 SCROLLINFO info;
1885 TRACE("hwnd=%p nBar=%d min=%d max=%d, bRedraw=%d\n", hwnd, nBar, minVal, maxVal, bRedraw);
1887 info.cbSize = sizeof(info);
1888 info.fMask = SIF_RANGE;
1889 info.nMin = minVal;
1890 info.nMax = maxVal;
1891 SetScrollInfo( hwnd, nBar, &info, bRedraw );
1892 return TRUE;
1896 /*************************************************************************
1897 * GetScrollRange (USER32.@)
1899 * Gets the range of the scroll bar.
1901 * PARAMS
1902 * hwnd [I] Handle of window with scrollbar(s)
1903 * nBar [I] One of SB_HORZ, SB_VERT, or SB_CTL
1904 * lpMin [O] Where to store minimum value
1905 * lpMax [O] Where to store maximum value
1907 * RETURNS
1908 * TRUE if values is filled
1910 BOOL WINAPI DECLSPEC_HOTPATCH GetScrollRange(HWND hwnd, INT nBar, LPINT lpMin, LPINT lpMax)
1912 TRACE("hwnd=%p nBar=%d lpMin=%p lpMax=%p\n", hwnd, nBar, lpMin, lpMax);
1914 /* Refer SB_CTL requests to the window */
1915 if (nBar == SB_CTL)
1916 SendMessageW(hwnd, SBM_GETRANGE, (WPARAM)lpMin, (LPARAM)lpMax);
1917 else
1918 SCROLL_GetScrollRange(hwnd, nBar, lpMin, lpMax);
1920 return TRUE;
1924 /*************************************************************************
1925 * SCROLL_ShowScrollBar()
1927 * Back-end for ShowScrollBar(). Returns FALSE if no action was taken.
1929 static BOOL SCROLL_ShowScrollBar( HWND hwnd, INT nBar, BOOL fShowH, BOOL fShowV )
1931 ULONG old_style, set_bits = 0, clear_bits = 0;
1933 TRACE("hwnd=%p bar=%d horz=%d, vert=%d\n", hwnd, nBar, fShowH, fShowV );
1935 switch(nBar)
1937 case SB_CTL:
1938 ShowWindow( hwnd, fShowH ? SW_SHOW : SW_HIDE );
1939 return TRUE;
1941 case SB_BOTH:
1942 case SB_HORZ:
1943 if (fShowH) set_bits |= WS_HSCROLL;
1944 else clear_bits |= WS_HSCROLL;
1945 if( nBar == SB_HORZ ) break;
1946 /* fall through */
1947 case SB_VERT:
1948 if (fShowV) set_bits |= WS_VSCROLL;
1949 else clear_bits |= WS_VSCROLL;
1950 break;
1952 default:
1953 return FALSE; /* Nothing to do! */
1956 old_style = WIN_SetStyle( hwnd, set_bits, clear_bits );
1957 if ((old_style & clear_bits) != 0 || (old_style & set_bits) != set_bits)
1959 /* frame has been changed, let the window redraw itself */
1960 SetWindowPos( hwnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE
1961 | SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
1962 return TRUE;
1964 return FALSE; /* no frame changes */
1968 /*************************************************************************
1969 * ShowScrollBar (USER32.@)
1971 * Shows or hides the scroll bar.
1973 * PARAMS
1974 * hwnd [I] Handle of window with scrollbar(s)
1975 * nBar [I] One of SB_HORZ, SB_VERT, or SB_CTL
1976 * fShow [I] TRUE = show, FALSE = hide
1978 * RETURNS
1979 * Success: TRUE
1980 * Failure: FALSE
1982 BOOL WINAPI DECLSPEC_HOTPATCH ShowScrollBar(HWND hwnd, INT nBar, BOOL fShow)
1984 if ( !hwnd )
1985 return FALSE;
1987 SCROLL_ShowScrollBar( hwnd, nBar, (nBar == SB_VERT) ? 0 : fShow,
1988 (nBar == SB_HORZ) ? 0 : fShow );
1989 return TRUE;
1993 /*************************************************************************
1994 * EnableScrollBar (USER32.@)
1996 * Enables or disables the scroll bars.
1998 BOOL WINAPI DECLSPEC_HOTPATCH EnableScrollBar( HWND hwnd, UINT nBar, UINT flags )
2000 BOOL bFineWithMe;
2001 SCROLLBAR_INFO *infoPtr;
2003 flags &= ESB_DISABLE_BOTH;
2005 if (nBar == SB_BOTH)
2007 if (!(infoPtr = SCROLL_GetInternalInfo( hwnd, SB_VERT, TRUE ))) return FALSE;
2008 if (!(bFineWithMe = (infoPtr->flags == flags)) )
2010 infoPtr->flags = flags;
2011 SCROLL_RefreshScrollBar( hwnd, SB_VERT, TRUE, TRUE );
2013 nBar = SB_HORZ;
2015 else
2016 bFineWithMe = nBar != SB_CTL;
2018 if (!(infoPtr = SCROLL_GetInternalInfo( hwnd, nBar, TRUE ))) return FALSE;
2019 if (bFineWithMe && infoPtr->flags == flags) return FALSE;
2020 infoPtr->flags = flags;
2022 if (nBar == SB_CTL && (flags == ESB_DISABLE_BOTH || flags == ESB_ENABLE_BOTH))
2023 EnableWindow(hwnd, flags == ESB_ENABLE_BOTH);
2025 SCROLL_RefreshScrollBar( hwnd, nBar, TRUE, TRUE );
2026 return TRUE;