Release 960506
[wine/multimedia.git] / controls / scroll.c
blobe0e7617efa449f93af7d487d5637cead6f7fece4
1 /*
2 * Interface code to SCROLLBAR widget
4 * Copyright Martin Ayotte, 1993
5 * Copyright Alexandre Julliard, 1994
7 * Small fixes and implemented SB_THUMBPOSITION
8 * by Peter Broadhurst, 940611
9 */
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include "windows.h"
16 #include "syscolor.h"
17 #include "sysmetrics.h"
18 #include "scroll.h"
19 #include "user.h"
20 #include "graphics.h"
21 #include "win.h"
22 #include "stddebug.h"
23 /* #define DEBUG_SCROLL */
24 #include "debug.h"
27 static HBITMAP hUpArrow = 0;
28 static HBITMAP hDnArrow = 0;
29 static HBITMAP hLfArrow = 0;
30 static HBITMAP hRgArrow = 0;
31 static HBITMAP hUpArrowD = 0;
32 static HBITMAP hDnArrowD = 0;
33 static HBITMAP hLfArrowD = 0;
34 static HBITMAP hRgArrowD = 0;
35 static HBITMAP hUpArrowI = 0;
36 static HBITMAP hDnArrowI = 0;
37 static HBITMAP hLfArrowI = 0;
38 static HBITMAP hRgArrowI = 0;
40 #define TOP_ARROW(flags,pressed) \
41 (((flags)&ESB_DISABLE_UP) ? hUpArrowI : ((pressed) ? hUpArrowD:hUpArrow))
42 #define BOTTOM_ARROW(flags,pressed) \
43 (((flags)&ESB_DISABLE_DOWN) ? hDnArrowI : ((pressed) ? hDnArrowD:hDnArrow))
44 #define LEFT_ARROW(flags,pressed) \
45 (((flags)&ESB_DISABLE_LEFT) ? hLfArrowI : ((pressed) ? hLfArrowD:hLfArrow))
46 #define RIGHT_ARROW(flags,pressed) \
47 (((flags)&ESB_DISABLE_RIGHT) ? hRgArrowI : ((pressed) ? hRgArrowD:hRgArrow))
50 /* Minimum size of the rectangle between the arrows */
51 #define SCROLL_MIN_RECT 4
53 /* Delay (in ms) before first repetition when holding the button down */
54 #define SCROLL_FIRST_DELAY 200
56 /* Delay (in ms) between scroll repetitions */
57 #define SCROLL_REPEAT_DELAY 100
59 /* Scroll timer id */
60 #define SCROLL_TIMER 0
62 /* Scroll-bar hit testing */
63 enum SCROLL_HITTEST
65 SCROLL_NOWHERE, /* Outside the scroll bar */
66 SCROLL_TOP_ARROW, /* Top or left arrow */
67 SCROLL_TOP_RECT, /* Rectangle between the top arrow and the thumb */
68 SCROLL_THUMB, /* Thumb rectangle */
69 SCROLL_BOTTOM_RECT, /* Rectangle between the thumb and the bottom arrow */
70 SCROLL_BOTTOM_ARROW /* Bottom or right arrow */
73 /* Thumb-tracking info */
74 static HWND hwndTracking = 0;
75 static int nBarTracking = 0;
76 static UINT uTrackingPos = 0;
78 /***********************************************************************
79 * SCROLL_LoadBitmaps
81 static void SCROLL_LoadBitmaps(void)
83 hUpArrow = LoadBitmap((HINSTANCE)NULL, MAKEINTRESOURCE(OBM_UPARROW));
84 hDnArrow = LoadBitmap((HINSTANCE)NULL, MAKEINTRESOURCE(OBM_DNARROW));
85 hLfArrow = LoadBitmap((HINSTANCE)NULL, MAKEINTRESOURCE(OBM_LFARROW));
86 hRgArrow = LoadBitmap((HINSTANCE)NULL, MAKEINTRESOURCE(OBM_RGARROW));
87 hUpArrowD = LoadBitmap((HINSTANCE)NULL, MAKEINTRESOURCE(OBM_UPARROWD));
88 hDnArrowD = LoadBitmap((HINSTANCE)NULL, MAKEINTRESOURCE(OBM_DNARROWD));
89 hLfArrowD = LoadBitmap((HINSTANCE)NULL, MAKEINTRESOURCE(OBM_LFARROWD));
90 hRgArrowD = LoadBitmap((HINSTANCE)NULL, MAKEINTRESOURCE(OBM_RGARROWD));
91 hUpArrowI = LoadBitmap((HINSTANCE)NULL, MAKEINTRESOURCE(OBM_UPARROWI));
92 hDnArrowI = LoadBitmap((HINSTANCE)NULL, MAKEINTRESOURCE(OBM_DNARROWI));
93 hLfArrowI = LoadBitmap((HINSTANCE)NULL, MAKEINTRESOURCE(OBM_LFARROWI));
94 hRgArrowI = LoadBitmap((HINSTANCE)NULL, MAKEINTRESOURCE(OBM_RGARROWI));
97 /***********************************************************************
98 * SCROLL_GetPtrScrollInfo
100 static SCROLLINFO *SCROLL_GetPtrScrollInfo( WND* wndPtr, int nBar )
102 HANDLE handle;
104 if (!wndPtr) return NULL;
105 switch(nBar)
107 case SB_HORZ: handle = wndPtr->hHScroll; break;
108 case SB_VERT: handle = wndPtr->hVScroll; break;
109 case SB_CTL: return (SCROLLINFO *)wndPtr->wExtra;
110 default: return NULL;
113 if (!handle) /* Create the info structure if needed */
115 if ((handle = USER_HEAP_ALLOC( sizeof(SCROLLINFO) )))
117 SCROLLINFO *infoPtr = (SCROLLINFO *) USER_HEAP_LIN_ADDR( handle );
118 infoPtr->MinVal = infoPtr->CurVal = 0;
119 infoPtr->MaxVal = 100;
120 infoPtr->flags = ESB_ENABLE_BOTH;
121 if (nBar == SB_HORZ) wndPtr->hHScroll = handle;
122 else wndPtr->hVScroll = handle;
124 if (!hUpArrow) SCROLL_LoadBitmaps();
126 return (SCROLLINFO *) USER_HEAP_LIN_ADDR( handle );
129 /***********************************************************************
130 * SCROLL_GetScrollInfo
132 static SCROLLINFO *SCROLL_GetScrollInfo( HWND hwnd, int nBar )
134 WND *wndPtr = WIN_FindWndPtr( hwnd );
135 return SCROLL_GetPtrScrollInfo( wndPtr, nBar );
139 /***********************************************************************
140 * SCROLL_GetScrollBarRect
142 * Compute the scroll bar rectangle, in drawing coordinates (i.e. client
143 * coords for SB_CTL, window coords for SB_VERT and SB_HORZ).
144 * 'arrowSize' returns the width or height of an arrow (depending on the
145 * orientation of the scrollbar), and 'thumbPos' returns the position of
146 * the thumb relative to the left or to the top.
147 * Return TRUE if the scrollbar is vertical, FALSE if horizontal.
149 static BOOL SCROLL_GetScrollBarRect( HWND hwnd, int nBar, RECT *lprect,
150 WORD *arrowSize, WORD *thumbPos )
152 int pixels;
153 BOOL vertical;
154 WND *wndPtr = WIN_FindWndPtr( hwnd );
156 switch(nBar)
158 case SB_HORZ:
159 lprect->left = wndPtr->rectClient.left - wndPtr->rectWindow.left - 1;
160 lprect->top = wndPtr->rectClient.bottom - wndPtr->rectWindow.top;
161 lprect->right = wndPtr->rectClient.right - wndPtr->rectWindow.left +1;
162 lprect->bottom = lprect->top + SYSMETRICS_CYHSCROLL + 1;
163 vertical = FALSE;
164 break;
166 case SB_VERT:
167 lprect->left = wndPtr->rectClient.right - wndPtr->rectWindow.left;
168 lprect->top = wndPtr->rectClient.top - wndPtr->rectWindow.top - 1;
169 lprect->right = lprect->left + SYSMETRICS_CXVSCROLL + 1;
170 lprect->bottom = wndPtr->rectClient.bottom - wndPtr->rectWindow.top +1;
171 vertical = TRUE;
172 break;
174 case SB_CTL:
175 GetClientRect( hwnd, lprect );
176 vertical = ((wndPtr->dwStyle & SBS_VERT) != 0);
177 break;
179 default:
180 return FALSE;
183 if (vertical) pixels = lprect->bottom - lprect->top;
184 else pixels = lprect->right - lprect->left;
186 if (pixels > 2*SYSMETRICS_CXVSCROLL + SCROLL_MIN_RECT)
188 *arrowSize = SYSMETRICS_CXVSCROLL;
190 else if (pixels > SCROLL_MIN_RECT)
192 *arrowSize = (pixels - SCROLL_MIN_RECT) / 2;
194 else *arrowSize = 0;
196 if ((pixels -= 3*SYSMETRICS_CXVSCROLL+1) > 0)
198 SCROLLINFO *info = SCROLL_GetPtrScrollInfo( wndPtr, nBar );
199 if ((info->flags & ESB_DISABLE_BOTH) == ESB_DISABLE_BOTH)
200 *thumbPos = 0;
201 else if (info->MinVal == info->MaxVal)
202 *thumbPos = *arrowSize;
203 else
204 *thumbPos = *arrowSize + pixels * (info->CurVal - info->MinVal) /
205 (info->MaxVal - info->MinVal);
207 else *thumbPos = 0;
208 return vertical;
212 /***********************************************************************
213 * SCROLL_GetThumbVal
215 * Compute the current scroll position based on the thumb position in pixels
216 * from the top of the scroll-bar.
218 static UINT SCROLL_GetThumbVal( SCROLLINFO *infoPtr, RECT *rect,
219 BOOL vertical, WORD pos )
221 int pixels = vertical ? rect->bottom-rect->top : rect->right-rect->left;
222 if ((pixels -= 3*SYSMETRICS_CXVSCROLL+1) <= 0) return infoPtr->MinVal;
223 pos = MAX( 0, pos - SYSMETRICS_CXVSCROLL );
224 if (pos > pixels) pos = pixels;
225 dprintf_scroll(stddeb,"GetThumbVal: pos=%d ret=%d\n", pos,
226 (infoPtr->MinVal
227 + (UINT)(((int)pos * (infoPtr->MaxVal-infoPtr->MinVal) + pixels/2)
228 / pixels)) );
229 return (infoPtr->MinVal
230 + (UINT)(((int)pos * (infoPtr->MaxVal-infoPtr->MinVal) + pixels/2)
231 / pixels));
235 /***********************************************************************
236 * SCROLL_HitTest
238 * Scroll-bar hit testing (don't confuse this with WM_NCHITTEST!).
240 static enum SCROLL_HITTEST SCROLL_HitTest( HWND hwnd, int nBar, POINT pt )
242 WORD arrowSize, thumbPos;
243 RECT rect;
245 BOOL vertical = SCROLL_GetScrollBarRect( hwnd, nBar, &rect,
246 &arrowSize, &thumbPos );
247 if (!PtInRect( &rect, pt )) return SCROLL_NOWHERE;
249 if (vertical)
251 if (pt.y <= rect.top + arrowSize + 1) return SCROLL_TOP_ARROW;
252 if (pt.y >= rect.bottom - arrowSize) return SCROLL_BOTTOM_ARROW;
253 if (!thumbPos) return SCROLL_TOP_RECT;
254 pt.y -= rect.top;
255 if (pt.y < (INT)thumbPos) return SCROLL_TOP_RECT;
256 if (pt.y > thumbPos+SYSMETRICS_CYHSCROLL) return SCROLL_BOTTOM_RECT;
257 return SCROLL_THUMB;
259 else /* horizontal */
261 if (pt.x <= rect.left + arrowSize) return SCROLL_TOP_ARROW;
262 if (pt.x >= rect.right - arrowSize) return SCROLL_BOTTOM_ARROW;
263 if (!thumbPos) return SCROLL_TOP_RECT;
264 pt.x -= rect.left;
265 if (pt.x < (INT)thumbPos) return SCROLL_TOP_RECT;
266 if (pt.x > thumbPos+SYSMETRICS_CXVSCROLL) return SCROLL_BOTTOM_RECT;
267 return SCROLL_THUMB;
272 /***********************************************************************
273 * SCROLL_DrawArrows
275 * Draw the scroll bar arrows.
277 static void SCROLL_DrawArrows( HDC hdc, SCROLLINFO *infoPtr, RECT *rect,
278 WORD arrowSize, BOOL vertical,
279 BOOL top_pressed, BOOL bottom_pressed )
281 HDC hdcMem = CreateCompatibleDC( hdc );
282 HBITMAP hbmpPrev = SelectObject( hdcMem, vertical ?
283 TOP_ARROW(infoPtr->flags, top_pressed)
284 : LEFT_ARROW(infoPtr->flags, top_pressed));
285 SetStretchBltMode( hdc, STRETCH_DELETESCANS );
286 StretchBlt( hdc, rect->left, rect->top,
287 vertical ? rect->right-rect->left : arrowSize+1,
288 vertical ? arrowSize+1 : rect->bottom-rect->top,
289 hdcMem, 0, 0,
290 SYSMETRICS_CXVSCROLL + 1, SYSMETRICS_CYHSCROLL + 1,
291 SRCCOPY );
293 SelectObject( hdcMem, vertical ?
294 BOTTOM_ARROW( infoPtr->flags, bottom_pressed )
295 : RIGHT_ARROW( infoPtr->flags, bottom_pressed ) );
296 if (vertical)
297 StretchBlt( hdc, rect->left, rect->bottom - arrowSize - 1,
298 rect->right - rect->left, arrowSize + 1,
299 hdcMem, 0, 0,
300 SYSMETRICS_CXVSCROLL + 1, SYSMETRICS_CYHSCROLL + 1,
301 SRCCOPY );
302 else
303 StretchBlt( hdc, rect->right - arrowSize - 1, rect->top,
304 arrowSize + 1, rect->bottom - rect->top,
305 hdcMem, 0, 0,
306 SYSMETRICS_CXVSCROLL + 1, SYSMETRICS_CYHSCROLL + 1,
307 SRCCOPY );
308 SelectObject( hdcMem, hbmpPrev );
309 DeleteDC( hdcMem );
313 /***********************************************************************
314 * SCROLL_DrawMovingThumb
316 * Draw the moving thumb rectangle.
318 static void SCROLL_DrawMovingThumb( HDC hdc, RECT *rect, BOOL vertical,
319 WORD arrowSize, WORD thumbPos )
321 RECT r = *rect;
322 if (vertical)
324 r.top += thumbPos;
325 if (r.top < rect->top + arrowSize) r.top = rect->top + arrowSize;
326 if (r.top + SYSMETRICS_CYHSCROLL >= rect->bottom - arrowSize)
327 r.top = rect->bottom - arrowSize - SYSMETRICS_CYHSCROLL - 1;
328 r.bottom = r.top + SYSMETRICS_CYHSCROLL + 1;
330 else
332 r.left += thumbPos;
333 if (r.left < rect->left + arrowSize) r.left = rect->left + arrowSize;
334 if (r.left + SYSMETRICS_CXVSCROLL >= rect->right - arrowSize)
335 r.left = rect->right - arrowSize - SYSMETRICS_CXVSCROLL - 1;
336 r.right = r.left + SYSMETRICS_CXVSCROLL + 1;
338 InflateRect( &r, -1, -1 );
339 DrawFocusRect( hdc, &r );
343 /***********************************************************************
344 * SCROLL_DrawInterior
346 * Draw the scroll bar interior (everything except the arrows).
348 static void SCROLL_DrawInterior( HWND hwnd, HDC hdc, int nBar, RECT *rect,
349 WORD arrowSize, WORD thumbPos, WORD flags,
350 BOOL vertical, BOOL top_selected,
351 BOOL bottom_selected )
353 RECT r;
355 /* Select the correct brush and pen */
357 SelectObject( hdc, sysColorObjects.hpenWindowFrame );
358 if ((flags & ESB_DISABLE_BOTH) == ESB_DISABLE_BOTH)
360 /* This ought to be the color of the parent window */
361 SelectObject( hdc, sysColorObjects.hbrushWindow );
363 else
365 if (nBar == SB_CTL) /* Only scrollbar controls send WM_CTLCOLOR */
367 #ifdef WINELIB32
368 HBRUSH hbrush = SendMessage( GetParent(hwnd), WM_CTLCOLORSCROLLBAR,
369 hdc, hwnd );
370 #else
371 HBRUSH hbrush = SendMessage( GetParent(hwnd), WM_CTLCOLOR, hdc,
372 MAKELONG(hwnd, CTLCOLOR_SCROLLBAR) );
373 #endif
374 SelectObject( hdc, hbrush );
376 else SelectObject( hdc, sysColorObjects.hbrushScrollbar );
379 /* Calculate the scroll rectangle */
381 r = *rect;
382 if (vertical)
384 r.top += arrowSize;
385 r.bottom -= arrowSize;
387 else
389 r.left += arrowSize;
390 r.right -= arrowSize;
393 /* Draw the scroll bar frame */
395 MoveTo( hdc, r.left, r.top );
396 LineTo( hdc, r.right-1, r.top );
397 LineTo( hdc, r.right-1, r.bottom-1 );
398 LineTo( hdc, r.left, r.bottom-1 );
399 LineTo( hdc, r.left, r.top );
401 /* Draw the scroll rectangles and thumb */
403 if (!thumbPos) /* No thumb to draw */
405 PatBlt( hdc, r.left+1, r.top+1, r.right - r.left - 2,
406 r.bottom - r.top - 2, PATCOPY );
407 return;
410 if (vertical)
412 PatBlt( hdc, r.left + 1, r.top + 1,
413 r.right - r.left - 2,
414 thumbPos - arrowSize,
415 top_selected ? 0x0f0000 : PATCOPY );
416 r.top += thumbPos - arrowSize;
417 PatBlt( hdc, r.left + 1, r.top + SYSMETRICS_CYHSCROLL + 1,
418 r.right - r.left - 2,
419 r.bottom - r.top - SYSMETRICS_CYHSCROLL - 2,
420 bottom_selected ? 0x0f0000 : PATCOPY );
421 r.bottom = r.top + SYSMETRICS_CYHSCROLL + 1;
423 else /* horizontal */
425 PatBlt( hdc, r.left + 1, r.top + 1,
426 thumbPos - arrowSize,
427 r.bottom - r.top - 2,
428 top_selected ? 0x0f0000 : PATCOPY );
429 r.left += thumbPos - arrowSize;
430 PatBlt( hdc, r.left + SYSMETRICS_CYHSCROLL + 1, r.top + 1,
431 r.right - r.left - SYSMETRICS_CYHSCROLL - 2,
432 r.bottom - r.top - 2,
433 bottom_selected ? 0x0f0000 : PATCOPY );
434 r.right = r.left + SYSMETRICS_CXVSCROLL + 1;
437 /* Draw the thumb */
439 SelectObject( hdc, sysColorObjects.hbrushBtnFace );
440 Rectangle( hdc, r.left, r.top, r.right, r.bottom );
441 InflateRect( &r, -1, -1 );
442 GRAPH_DrawReliefRect( hdc, &r, 1, 2, FALSE );
443 if ((hwndTracking == hwnd) && (nBarTracking == nBar))
444 SCROLL_DrawMovingThumb( hdc, rect, vertical, arrowSize, uTrackingPos);
448 /***********************************************************************
449 * SCROLL_DrawScrollBar
451 * Redraw the whole scrollbar.
453 void SCROLL_DrawScrollBar( HWND hwnd, HDC hdc, int nBar )
455 WORD arrowSize, thumbPos;
456 RECT rect;
457 BOOL vertical;
458 WND *wndPtr = WIN_FindWndPtr( hwnd );
459 SCROLLINFO *infoPtr = SCROLL_GetPtrScrollInfo( wndPtr, nBar );
461 if (!wndPtr || !infoPtr ||
462 ((nBar == SB_VERT) && !(wndPtr->dwStyle & WS_VSCROLL)) ||
463 ((nBar == SB_HORZ) && !(wndPtr->dwStyle & WS_HSCROLL))) return;
465 vertical = SCROLL_GetScrollBarRect( hwnd, nBar, &rect,
466 &arrowSize, &thumbPos );
467 /* Draw the arrows */
469 if (arrowSize) SCROLL_DrawArrows( hdc, infoPtr, &rect, arrowSize,
470 vertical, FALSE, FALSE );
472 SCROLL_DrawInterior( hwnd, hdc, nBar, &rect, arrowSize, thumbPos,
473 infoPtr->flags, vertical, FALSE, FALSE );
477 /***********************************************************************
478 * SCROLL_RefreshScrollBar
480 * Repaint the scroll bar interior after a SetScrollRange() or
481 * SetScrollPos() call.
483 static void SCROLL_RefreshScrollBar( HWND hwnd, int nBar )
485 WORD arrowSize, thumbPos;
486 RECT rect;
487 BOOL vertical;
488 HDC hdc;
489 WND *wndPtr = WIN_FindWndPtr( hwnd );
490 SCROLLINFO *infoPtr = SCROLL_GetPtrScrollInfo( wndPtr, nBar );
492 if (!wndPtr || !infoPtr ||
493 ((nBar == SB_VERT) && !(wndPtr->dwStyle & WS_VSCROLL)) ||
494 ((nBar == SB_HORZ) && !(wndPtr->dwStyle & WS_HSCROLL))) return;
496 vertical = SCROLL_GetScrollBarRect( hwnd, nBar, &rect,
497 &arrowSize, &thumbPos );
498 hdc = (nBar == SB_CTL) ? GetDC(hwnd) : GetWindowDC(hwnd);
499 if (!hdc) return;
500 SCROLL_DrawInterior( hwnd, hdc, nBar, &rect, arrowSize, thumbPos,
501 infoPtr->flags, vertical, FALSE, FALSE );
502 ReleaseDC( hwnd, hdc );
506 /***********************************************************************
507 * SCROLL_HandleKbdEvent
509 * Handle a keyboard event (only for SB_CTL scrollbars).
511 static void SCROLL_HandleKbdEvent( HWND hwnd, WORD wParam )
513 WND *wndPtr = WIN_FindWndPtr( hwnd );
514 WPARAM msg;
516 switch(wParam)
518 case VK_PRIOR: msg = SB_PAGEUP; break;
519 case VK_NEXT: msg = SB_PAGEDOWN; break;
520 case VK_HOME: msg = SB_TOP; break;
521 case VK_END: msg = SB_BOTTOM; break;
522 case VK_UP: msg = SB_LINEUP; break;
523 case VK_DOWN: msg = SB_LINEDOWN; break;
524 default:
525 return;
527 #ifdef WINELIB32
528 SendMessage( GetParent(hwnd),
529 (wndPtr->dwStyle & SBS_VERT) ? WM_VSCROLL : WM_HSCROLL,
530 msg, hwnd );
531 #else
532 SendMessage( GetParent(hwnd),
533 (wndPtr->dwStyle & SBS_VERT) ? WM_VSCROLL : WM_HSCROLL,
534 msg, MAKELONG( 0, hwnd ));
535 #endif
539 /***********************************************************************
540 * SCROLL_HandleScrollEvent
542 * Handle a mouse or timer event for the scrollbar.
543 * 'pt' is the location of the mouse event in client (for SB_CTL) or
544 * windows coordinates.
546 void SCROLL_HandleScrollEvent( HWND hwnd, int nBar, WORD msg, POINT pt )
548 /* Previous mouse position for timer events */
549 static POINT prevPt;
550 /* Hit test code of the last button-down event */
551 static enum SCROLL_HITTEST trackHitTest;
552 /* Thumb position when tracking started. */
553 static UINT trackThumbPos;
554 /* Position in the scroll-bar of the last button-down event. */
555 static int lastClickPos;
556 /* Position in the scroll-bar of the last mouse event. */
557 static int lastMousePos;
559 enum SCROLL_HITTEST hittest;
560 HWND hwndOwner, hwndCtl;
561 BOOL vertical;
562 WORD arrowSize, thumbPos;
563 RECT rect;
564 HDC hdc;
566 SCROLLINFO *infoPtr = SCROLL_GetScrollInfo( hwnd, nBar );
567 if (!infoPtr) return;
568 if ((trackHitTest == SCROLL_NOWHERE) && (msg != WM_LBUTTONDOWN)) return;
570 hdc = (nBar == SB_CTL) ? GetDC(hwnd) : GetWindowDC(hwnd);
571 vertical = SCROLL_GetScrollBarRect( hwnd, nBar, &rect,
572 &arrowSize, &thumbPos );
573 hwndOwner = (nBar == SB_CTL) ? GetParent(hwnd) : hwnd;
574 hwndCtl = (nBar == SB_CTL) ? hwnd : 0;
576 switch(msg)
578 case WM_LBUTTONDOWN: /* Initialise mouse tracking */
579 trackHitTest = hittest = SCROLL_HitTest( hwnd, nBar, pt );
580 lastClickPos = vertical ? (pt.y - rect.top) : (pt.x - rect.left);
581 lastMousePos = lastClickPos;
582 trackThumbPos = thumbPos;
583 prevPt = pt;
584 SetCapture( hwnd );
585 if (nBar == SB_CTL) SetFocus( hwnd );
586 break;
588 case WM_MOUSEMOVE:
589 hittest = SCROLL_HitTest( hwnd, nBar, pt );
590 prevPt = pt;
591 break;
593 case WM_LBUTTONUP:
594 hittest = SCROLL_NOWHERE;
595 ReleaseCapture();
596 break;
598 case WM_SYSTIMER:
599 pt = prevPt;
600 hittest = SCROLL_HitTest( hwnd, nBar, pt );
601 break;
603 default:
604 return; /* Should never happen */
607 dprintf_scroll( stddeb, "ScrollBar Event: hwnd=%04x bar=%d msg=%x pt=%d,%d hit=%d\n",
608 hwnd, nBar, msg, pt.x, pt.y, hittest );
610 switch(trackHitTest)
612 case SCROLL_NOWHERE: /* No tracking in progress */
613 break;
615 case SCROLL_TOP_ARROW:
616 SCROLL_DrawArrows( hdc, infoPtr, &rect, arrowSize, vertical,
617 (hittest == trackHitTest), FALSE );
618 if (hittest == trackHitTest)
620 if ((msg == WM_LBUTTONDOWN) || (msg == WM_SYSTIMER))
622 #ifdef WINELIB32
623 SendMessage( hwndOwner, vertical ? WM_VSCROLL : WM_HSCROLL,
624 SB_LINEUP, hwndCtl );
625 #else
626 SendMessage( hwndOwner, vertical ? WM_VSCROLL : WM_HSCROLL,
627 SB_LINEUP, MAKELONG( 0, hwndCtl ));
628 #endif
629 SetSystemTimer( hwnd, SCROLL_TIMER, (msg == WM_LBUTTONDOWN) ?
630 SCROLL_FIRST_DELAY : SCROLL_REPEAT_DELAY,
631 (FARPROC)0 );
634 else KillSystemTimer( hwnd, SCROLL_TIMER );
635 break;
637 case SCROLL_TOP_RECT:
638 SCROLL_DrawInterior( hwnd, hdc, nBar, &rect, arrowSize, thumbPos,
639 infoPtr->flags, vertical,
640 (hittest == trackHitTest), FALSE );
641 if (hittest == trackHitTest)
643 if ((msg == WM_LBUTTONDOWN) || (msg == WM_SYSTIMER))
645 #ifdef WINELIB32
646 SendMessage( hwndOwner, vertical ? WM_VSCROLL : WM_HSCROLL,
647 SB_PAGEUP, hwndCtl );
648 #else
649 SendMessage( hwndOwner, vertical ? WM_VSCROLL : WM_HSCROLL,
650 SB_PAGEUP, MAKELONG( 0, hwndCtl ));
651 #endif
652 SetSystemTimer( hwnd, SCROLL_TIMER, (msg == WM_LBUTTONDOWN) ?
653 SCROLL_FIRST_DELAY : SCROLL_REPEAT_DELAY,
654 (FARPROC)0 );
657 else KillSystemTimer( hwnd, SCROLL_TIMER );
658 break;
660 case SCROLL_THUMB:
661 if (msg == WM_LBUTTONDOWN)
663 SCROLL_DrawMovingThumb( hdc, &rect, vertical, arrowSize,
664 trackThumbPos + lastMousePos - lastClickPos );
665 hwndTracking = hwnd;
666 nBarTracking = nBar;
668 else if (msg == WM_LBUTTONUP)
670 hwndTracking = 0;
671 SCROLL_DrawInterior( hwnd, hdc, nBar, &rect, arrowSize, thumbPos,
672 infoPtr->flags, vertical, FALSE, FALSE );
674 else /* WM_MOUSEMOVE */
676 UINT pos, val;
678 if (!PtInRect( &rect, pt )) pos = lastClickPos;
679 else pos = vertical ? (pt.y - rect.top) : (pt.x - rect.left);
680 if (pos != lastMousePos)
682 SCROLL_DrawMovingThumb( hdc, &rect, vertical, arrowSize,
683 trackThumbPos + lastMousePos - lastClickPos );
684 lastMousePos = pos;
685 val = SCROLL_GetThumbVal( infoPtr, &rect, vertical,
686 trackThumbPos + lastMousePos - lastClickPos );
687 /* Save tracking info */
688 uTrackingPos = trackThumbPos + pos - lastClickPos;
689 #ifdef WINELIB32
690 SendMessage( hwndOwner, vertical ? WM_VSCROLL : WM_HSCROLL,
691 MAKEWPARAM(SB_THUMBTRACK,val), hwndCtl );
692 #else
693 SendMessage( hwndOwner, vertical ? WM_VSCROLL : WM_HSCROLL,
694 SB_THUMBTRACK, MAKELONG( val, hwndCtl ));
695 #endif
696 SCROLL_DrawMovingThumb( hdc, &rect, vertical,
697 arrowSize, uTrackingPos );
700 break;
702 case SCROLL_BOTTOM_RECT:
703 SCROLL_DrawInterior( hwnd, hdc, nBar, &rect, arrowSize, thumbPos,
704 infoPtr->flags, vertical,
705 FALSE, (hittest == trackHitTest) );
706 if (hittest == trackHitTest)
708 if ((msg == WM_LBUTTONDOWN) || (msg == WM_SYSTIMER))
710 #ifdef WINELIB32
711 SendMessage( hwndOwner, vertical ? WM_VSCROLL : WM_HSCROLL,
712 SB_PAGEDOWN, hwndCtl );
713 #else
714 SendMessage( hwndOwner, vertical ? WM_VSCROLL : WM_HSCROLL,
715 SB_PAGEDOWN, MAKELONG( 0, hwndCtl ));
716 #endif
717 SetSystemTimer( hwnd, SCROLL_TIMER, (msg == WM_LBUTTONDOWN) ?
718 SCROLL_FIRST_DELAY : SCROLL_REPEAT_DELAY,
719 (FARPROC)0 );
722 else KillSystemTimer( hwnd, SCROLL_TIMER );
723 break;
725 case SCROLL_BOTTOM_ARROW:
726 SCROLL_DrawArrows( hdc, infoPtr, &rect, arrowSize, vertical,
727 FALSE, (hittest == trackHitTest) );
728 if (hittest == trackHitTest)
730 if ((msg == WM_LBUTTONDOWN) || (msg == WM_SYSTIMER))
732 #ifdef WINELIB32
733 SendMessage( hwndOwner, vertical ? WM_VSCROLL : WM_HSCROLL,
734 SB_LINEDOWN, hwndCtl );
735 #else
736 SendMessage( hwndOwner, vertical ? WM_VSCROLL : WM_HSCROLL,
737 SB_LINEDOWN, MAKELONG( 0, hwndCtl ));
738 #endif
739 SetSystemTimer( hwnd, SCROLL_TIMER, (msg == WM_LBUTTONDOWN) ?
740 SCROLL_FIRST_DELAY : SCROLL_REPEAT_DELAY,
741 (FARPROC)0 );
744 else KillSystemTimer( hwnd, SCROLL_TIMER );
745 break;
748 if (msg == WM_LBUTTONUP)
750 if (trackHitTest == SCROLL_THUMB)
752 UINT val = SCROLL_GetThumbVal( infoPtr, &rect, vertical,
753 trackThumbPos + lastMousePos - lastClickPos );
754 #ifdef WINELIB32
755 SendMessage( hwndOwner, vertical ? WM_VSCROLL : WM_HSCROLL,
756 MAKEWPARAM(SB_THUMBPOSITION,val), hwndCtl );
757 #else
758 SendMessage( hwndOwner, vertical ? WM_VSCROLL : WM_HSCROLL,
759 SB_THUMBPOSITION, MAKELONG( val, hwndCtl ) );
760 #endif
762 else
763 #ifdef WINELIB32
764 SendMessage( hwndOwner, vertical ? WM_VSCROLL : WM_HSCROLL,
765 SB_ENDSCROLL, hwndCtl );
766 #else
767 SendMessage( hwndOwner, vertical ? WM_VSCROLL : WM_HSCROLL,
768 SB_ENDSCROLL, MAKELONG( 0, hwndCtl ) );
769 #endif
770 trackHitTest = SCROLL_NOWHERE; /* Terminate tracking */
773 ReleaseDC( hwnd, hdc );
777 /***********************************************************************
778 * ScrollBarWndProc
780 LONG ScrollBarWndProc( HWND hwnd, WORD message, WORD wParam, LONG lParam )
782 POINT Pt;
783 Pt.x = LOWORD(lParam); Pt.y = HIWORD(lParam);
784 /* ^ Can't use MAKEPOINT macro in WINELIB32 */
786 switch(message)
788 case WM_CREATE:
790 CREATESTRUCT *lpCreat = (CREATESTRUCT *)PTR_SEG_TO_LIN(lParam);
791 if (lpCreat->style & SBS_SIZEBOX)
793 fprintf( stdnimp, "Unimplemented style SBS_SIZEBOX.\n" );
794 return 0; /* FIXME */
797 if (lpCreat->style & SBS_VERT)
799 if (lpCreat->style & SBS_LEFTALIGN)
800 MoveWindow( hwnd, lpCreat->x, lpCreat->y,
801 SYSMETRICS_CXVSCROLL+1, lpCreat->cy, FALSE );
802 else if (lpCreat->style & SBS_RIGHTALIGN)
803 MoveWindow( hwnd,
804 lpCreat->x+lpCreat->cx-SYSMETRICS_CXVSCROLL-1,
805 lpCreat->y,
806 SYSMETRICS_CXVSCROLL + 1, lpCreat->cy, FALSE );
808 else /* SBS_HORZ */
810 if (lpCreat->style & SBS_TOPALIGN)
811 MoveWindow( hwnd, lpCreat->x, lpCreat->y,
812 lpCreat->cx, SYSMETRICS_CYHSCROLL+1, FALSE );
813 else if (lpCreat->style & SBS_BOTTOMALIGN)
814 MoveWindow( hwnd,
815 lpCreat->x,
816 lpCreat->y+lpCreat->cy-SYSMETRICS_CYHSCROLL-1,
817 lpCreat->cx, SYSMETRICS_CYHSCROLL+1, FALSE );
820 if (!hUpArrow) SCROLL_LoadBitmaps();
821 dprintf_scroll( stddeb, "ScrollBar creation, hwnd=%04x\n", hwnd );
822 return 0;
824 case WM_LBUTTONDOWN:
825 case WM_LBUTTONUP:
826 case WM_MOUSEMOVE:
827 case WM_SYSTIMER:
828 SCROLL_HandleScrollEvent( hwnd, SB_CTL, message, Pt );
829 break;
831 case WM_KEYDOWN:
832 SCROLL_HandleKbdEvent( hwnd, wParam );
833 break;
835 case WM_ERASEBKGND:
836 return 1;
838 case WM_GETDLGCODE:
839 return DLGC_WANTARROWS; /* Windows returns this value */
841 case WM_PAINT:
843 PAINTSTRUCT ps;
844 HDC hdc = BeginPaint( hwnd, &ps );
845 SCROLL_DrawScrollBar( hwnd, hdc, SB_CTL );
846 EndPaint( hwnd, &ps );
848 break;
850 case 0x400: /* SB_SETSCROLLPOS */
851 case 0x401: /* SB_GETSCROLLPOS */
852 case 0x402: /* SB_GETSCROLLRANGE */
853 case 0x403: /* SB_ENABLE */
854 case 0x404: /* SB_REDRAW */
855 fprintf(stdnimp,"ScrollBarWndProc: undocumented message %04x, please report\n", message );
857 default:
858 return DefWindowProc( hwnd, message, wParam, lParam );
860 return 0;
864 /*************************************************************************
865 * SetScrollPos (USER.62)
867 int SetScrollPos( HWND hwnd, int nBar, int nPos, BOOL bRedraw )
869 SCROLLINFO *infoPtr;
870 INT oldPos;
872 if (!(infoPtr = SCROLL_GetScrollInfo( hwnd, nBar ))) return 0;
874 dprintf_scroll( stddeb,"SetScrollPos min=%d max=%d pos=%d\n",
875 infoPtr->MinVal, infoPtr->MaxVal, nPos );
877 if (nPos < infoPtr->MinVal) nPos = infoPtr->MinVal;
878 else if (nPos > infoPtr->MaxVal) nPos = infoPtr->MaxVal;
879 oldPos = infoPtr->CurVal;
880 infoPtr->CurVal = nPos;
881 if (bRedraw) SCROLL_RefreshScrollBar( hwnd, nBar );
882 return oldPos;
886 /*************************************************************************
887 * GetScrollPos (USER.63)
889 int GetScrollPos( HWND hwnd, int nBar )
891 SCROLLINFO *infoPtr;
893 if (!(infoPtr = SCROLL_GetScrollInfo( hwnd, nBar ))) return 0;
894 return infoPtr->CurVal;
898 /*************************************************************************
899 * SetScrollRange (USER.64)
901 void SetScrollRange(HWND hwnd, int nBar, int MinVal, int MaxVal, BOOL bRedraw)
903 SCROLLINFO *infoPtr;
905 if (!(infoPtr = SCROLL_GetScrollInfo( hwnd, nBar ))) return;
907 dprintf_scroll( stddeb,"SetScrollRange hwnd=%04x bar=%d min=%d max=%d\n",
908 hwnd, nBar, MinVal, MaxVal );
910 /* Invalid range -> range is set to (0,0) */
911 if ((MinVal > MaxVal) || ((long)MaxVal - MinVal > 32767L))
912 MinVal = MaxVal = 0;
913 if (infoPtr->CurVal < MinVal) infoPtr->CurVal = MinVal;
914 else if (infoPtr->CurVal > MaxVal) infoPtr->CurVal = MaxVal;
915 infoPtr->MinVal = MinVal;
916 infoPtr->MaxVal = MaxVal;
918 /* Non-client scroll-bar is hidden if min==max */
919 if (nBar != SB_CTL) ShowScrollBar( hwnd, nBar, (MinVal != MaxVal) );
920 if (bRedraw) SCROLL_RefreshScrollBar( hwnd, nBar );
923 /*************************************************************************
924 * SCROLL_SetNCSbState
926 * This is for CalcChildScroll in windows/mdi.c
928 DWORD SCROLL_SetNCSbState(WND* wndPtr, int vMin, int vMax, int vPos,
929 int hMin, int hMax, int hPos)
931 SCROLLINFO *infoPtr = SCROLL_GetPtrScrollInfo(wndPtr, SB_VERT);
933 wndPtr->dwStyle |= (WS_VSCROLL | WS_HSCROLL);
935 if( vMin >= vMax )
936 { vMin = vMax;
937 wndPtr->dwStyle &= ~WS_VSCROLL; }
938 if( vPos > vMax ) vPos = vMax; else if( vPos < vMin ) vPos = vMin;
939 infoPtr->MinVal = vMin;
940 infoPtr->MaxVal = vMax;
941 infoPtr->CurVal = vPos;
943 infoPtr = SCROLL_GetPtrScrollInfo(wndPtr, SB_HORZ);
945 if( hMin >= hMax )
946 { hMin = hMax;
947 wndPtr->dwStyle &= ~WS_HSCROLL; }
948 if( hPos > hMax ) hPos = hMax; else if( hPos < hMin ) hPos = hMin;
949 infoPtr->MinVal = hMin;
950 infoPtr->MaxVal = hMax;
951 infoPtr->CurVal = hPos;
953 return wndPtr->dwStyle & (WS_VSCROLL | WS_HSCROLL);
956 /*************************************************************************
957 * GetScrollRange (USER.65)
959 void GetScrollRange(HWND hwnd, int nBar, LPINT16 lpMin, LPINT16 lpMax)
961 SCROLLINFO *infoPtr;
963 if (!(infoPtr = SCROLL_GetScrollInfo( hwnd, nBar ))) return;
964 if (lpMin) *lpMin = infoPtr->MinVal;
965 if (lpMax) *lpMax = infoPtr->MaxVal;
969 /*************************************************************************
970 * ShowScrollBar (USER.267)
972 void ShowScrollBar( HWND hwnd, WORD wBar, BOOL fShow )
974 WND *wndPtr = WIN_FindWndPtr( hwnd );
976 if (!wndPtr) return;
977 dprintf_scroll( stddeb, "ShowScrollBar: hwnd=%04x bar=%d on=%d\n", hwnd, wBar, fShow );
979 switch(wBar)
981 case SB_CTL:
982 ShowWindow( hwnd, fShow ? SW_SHOW : SW_HIDE );
983 return;
985 case SB_HORZ:
986 if (fShow)
988 if (wndPtr->dwStyle & WS_HSCROLL) return;
989 wndPtr->dwStyle |= WS_HSCROLL;
991 else /* hide it */
993 if (!(wndPtr->dwStyle & WS_HSCROLL)) return;
994 wndPtr->dwStyle &= ~WS_HSCROLL;
996 break;
998 case SB_VERT:
999 if (fShow)
1001 if (wndPtr->dwStyle & WS_VSCROLL) return;
1002 wndPtr->dwStyle |= WS_VSCROLL;
1004 else /* hide it */
1006 if (!(wndPtr->dwStyle & WS_VSCROLL)) return;
1007 wndPtr->dwStyle &= ~WS_VSCROLL;
1009 break;
1011 case SB_BOTH:
1012 if (fShow)
1014 if ((wndPtr->dwStyle & WS_HSCROLL)
1015 && (wndPtr->dwStyle & WS_VSCROLL)) return;
1016 wndPtr->dwStyle |= WS_HSCROLL | WS_VSCROLL;
1018 else /* hide it */
1020 if (!(wndPtr->dwStyle & WS_HSCROLL)
1021 && !(wndPtr->dwStyle & WS_HSCROLL)) return;
1022 wndPtr->dwStyle &= ~(WS_HSCROLL | WS_VSCROLL);
1024 break;
1026 default:
1027 return; /* Nothing to do! */
1029 SetWindowPos( hwnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE
1030 | SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
1034 /*************************************************************************
1035 * EnableScrollBar (USER.482)
1037 BOOL EnableScrollBar( HWND hwnd, UINT nBar, UINT flags )
1039 SCROLLINFO *infoPtr;
1040 HDC hdc;
1042 if (!(infoPtr = SCROLL_GetScrollInfo( hwnd, nBar ))) return FALSE;
1043 dprintf_scroll( stddeb, "EnableScrollBar: %04x %d %d\n", hwnd, nBar, flags );
1044 flags &= ESB_DISABLE_BOTH;
1045 if (infoPtr->flags == flags) return FALSE;
1046 infoPtr->flags = flags;
1048 /* Redraw the whole scroll bar */
1049 hdc = (nBar == SB_CTL) ? GetDC(hwnd) : GetWindowDC(hwnd);
1050 SCROLL_DrawScrollBar( hwnd, hdc, nBar );
1051 ReleaseDC( hwnd, hdc );
1052 return TRUE;