Revert tree-restructure attempt: r66583, r66582, r66581, r66578, sauf ntdll changes...
[reactos.git] / reactos / win32ss / user / user32 / controls / scrollbar.c
1 /*
2 * ReactOS User32 Library
3 * - ScrollBar control
4 *
5 * Copyright 2001 Casper S. Hornstrup
6 * Copyright 2003 Thomas Weidenmueller
7 * Copyright 2003 Filip Navara
8 *
9 * Based on Wine code.
10 *
11 * Copyright 1993 Martin Ayotte
12 * Copyright 1994, 1996 Alexandre Julliard
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29 /* INCLUDES *******************************************************************/
30
31 #include <user32.h>
32
33 #include <wine/debug.h>
34 WINE_DEFAULT_DEBUG_CHANNEL(scrollbar);
35
36 /* GLOBAL VARIABLES ***********************************************************/
37
38 /* Definitions for scrollbar hit testing [See SCROLLBARINFO in MSDN] */
39 #define SCROLL_NOWHERE 0x00 /* Outside the scroll bar */
40 #define SCROLL_TOP_ARROW 0x01 /* Top or left arrow */
41 #define SCROLL_TOP_RECT 0x02 /* Rectangle between the top arrow and the thumb */
42 #define SCROLL_THUMB 0x03 /* Thumb rectangle */
43 #define SCROLL_BOTTOM_RECT 0x04 /* Rectangle between the thumb and the bottom arrow */
44 #define SCROLL_BOTTOM_ARROW 0x05 /* Bottom or right arrow */
45
46 #define SCROLL_FIRST_DELAY 200 /* Delay (in ms) before first repetition when
47 holding the button down */
48 #define SCROLL_REPEAT_DELAY 50 /* Delay (in ms) between scroll repetitions */
49
50 #define SCROLL_TIMER 0 /* Scroll timer id */
51
52 /* Minimum size of the rectangle between the arrows */
53 #define SCROLL_MIN_RECT 4
54
55 /* Minimum size of the thumb in pixels */
56 #define SCROLL_MIN_THUMB 6
57
58 /* Overlap between arrows and thumb */
59 #define SCROLL_ARROW_THUMB_OVERLAP 0
60
61 /* Thumb-tracking info */
62 static HWND ScrollTrackingWin = 0;
63 static INT ScrollTrackingBar = 0;
64 static INT ScrollTrackingPos = 0;
65 static INT ScrollTrackingVal = 0;
66 /* Hit test code of the last button-down event */
67 static DWORD ScrollTrackHitTest = SCROLL_NOWHERE;
68 static BOOL ScrollTrackVertical;
69
70 /* Is the moving thumb being displayed? */
71 static BOOL ScrollMovingThumb = FALSE;
72
73 HBRUSH DefWndControlColor(HDC hDC, UINT ctlType);
74
75 UINT_PTR WINAPI SetSystemTimer(HWND,UINT_PTR,UINT,TIMERPROC);
76 BOOL WINAPI KillSystemTimer(HWND,UINT_PTR);
77
78 /*********************************************************************
79 * scrollbar class descriptor
80 */
81 const struct builtin_class_descr SCROLL_builtin_class =
82 {
83 L"ScrollBar", /* name */
84 CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC, /* style */
85 ScrollBarWndProcA, /* procA */
86 ScrollBarWndProcW, /* procW */
87 sizeof(SBWND)-sizeof(WND), /* extra */
88 IDC_ARROW, /* cursor */
89 0 /* brush */
90 };
91
92 /* PRIVATE FUNCTIONS **********************************************************/
93
94 static PSBDATA
95 IntGetSBData(PWND pwnd, INT Bar)
96 {
97 PSBWND pSBWnd;
98 PSBINFO pSBInfo;
99
100 pSBInfo = DesktopPtrToUser(pwnd->pSBInfo);
101 switch (Bar)
102 {
103 case SB_HORZ:
104 return &pSBInfo->Horz;
105 case SB_VERT:
106 return &pSBInfo->Vert;
107 case SB_CTL:
108 if ( pwnd->cbwndExtra != (sizeof(SBWND)-sizeof(WND)) )
109 {
110 ERR("IntGetSBData Wrong Extra bytes for CTL Scrollbar!\n");
111 return 0;
112 }
113 pSBWnd = (PSBWND)pwnd;
114 return (PSBDATA)&pSBWnd->SBCalc;
115 default:
116 ERR("IntGetSBData Bad Bar!\n");
117 }
118 return NULL;
119 }
120
121 static void
122 IntDrawScrollInterior(HWND hWnd, HDC hDC, INT nBar, BOOL Vertical,
123 PSCROLLBARINFO ScrollBarInfo)
124 {
125 INT ThumbSize = ScrollBarInfo->xyThumbBottom - ScrollBarInfo->xyThumbTop;
126 INT ThumbTop = ScrollBarInfo->xyThumbTop;
127 RECT Rect;
128 HBRUSH hSaveBrush, hBrush;
129 BOOL TopSelected = FALSE, BottomSelected = FALSE;
130
131 if (ScrollBarInfo->rgstate[SCROLL_TOP_RECT] & STATE_SYSTEM_PRESSED)
132 TopSelected = TRUE;
133 if (ScrollBarInfo->rgstate[SCROLL_BOTTOM_RECT] & STATE_SYSTEM_PRESSED)
134 BottomSelected = TRUE;
135
136 /*
137 * Only scrollbar controls send WM_CTLCOLORSCROLLBAR.
138 * The window-owned scrollbars need to call DefWndControlColor
139 * to correctly setup default scrollbar colors
140 */
141 if (nBar == SB_CTL)
142 {
143 hBrush = GetControlBrush( hWnd, hDC, WM_CTLCOLORSCROLLBAR);
144 if (!hBrush)
145 hBrush = GetSysColorBrush(COLOR_SCROLLBAR);
146 }
147 else
148 {
149 hBrush = DefWndControlColor(hDC, CTLCOLOR_SCROLLBAR);
150 }
151
152 hSaveBrush = SelectObject(hDC, hBrush);
153
154 /* Calculate the scroll rectangle */
155 if (Vertical)
156 {
157 Rect.top = ScrollBarInfo->rcScrollBar.top + ScrollBarInfo->dxyLineButton;
158 Rect.bottom = ScrollBarInfo->rcScrollBar.bottom - ScrollBarInfo->dxyLineButton;
159 Rect.left = ScrollBarInfo->rcScrollBar.left;
160 Rect.right = ScrollBarInfo->rcScrollBar.right;
161 }
162 else
163 {
164 Rect.top = ScrollBarInfo->rcScrollBar.top;
165 Rect.bottom = ScrollBarInfo->rcScrollBar.bottom;
166 Rect.left = ScrollBarInfo->rcScrollBar.left + ScrollBarInfo->dxyLineButton;
167 Rect.right = ScrollBarInfo->rcScrollBar.right - ScrollBarInfo->dxyLineButton;
168 }
169
170 /* Draw the scroll rectangles and thumb */
171 if (!ScrollBarInfo->xyThumbBottom)
172 {
173 PatBlt(hDC, Rect.left, Rect.top, Rect.right - Rect.left,
174 Rect.bottom - Rect.top, PATCOPY);
175
176 /* Cleanup and return */
177 SelectObject(hDC, hSaveBrush);
178 return;
179 }
180
181 ThumbTop -= ScrollBarInfo->dxyLineButton;
182
183 if (ScrollBarInfo->dxyLineButton)
184 {
185 if (Vertical)
186 {
187 if (ThumbSize)
188 {
189 PatBlt(hDC, Rect.left, Rect.top, Rect.right - Rect.left,
190 ThumbTop, TopSelected ? BLACKNESS : PATCOPY);
191 Rect.top += ThumbTop;
192 PatBlt(hDC, Rect.left, Rect.top + ThumbSize, Rect.right - Rect.left,
193 Rect.bottom - Rect.top - ThumbSize, BottomSelected ? BLACKNESS : PATCOPY);
194 Rect.bottom = Rect.top + ThumbSize;
195 }
196 else
197 {
198 if (ThumbTop)
199 {
200 PatBlt(hDC, Rect.left, ScrollBarInfo->dxyLineButton,
201 Rect.right - Rect.left, Rect.bottom - Rect.top, PATCOPY);
202 }
203 }
204 }
205 else
206 {
207 if (ThumbSize)
208 {
209 PatBlt(hDC, Rect.left, Rect.top, ThumbTop,
210 Rect.bottom - Rect.top, TopSelected ? BLACKNESS : PATCOPY);
211 Rect.left += ThumbTop;
212 PatBlt(hDC, Rect.left + ThumbSize, Rect.top,
213 Rect.right - Rect.left - ThumbSize, Rect.bottom - Rect.top,
214 BottomSelected ? BLACKNESS : PATCOPY);
215 Rect.right = Rect.left + ThumbSize;
216 }
217 else
218 {
219 if (ThumbTop)
220 {
221 PatBlt(hDC, ScrollBarInfo->dxyLineButton, Rect.top,
222 Rect.right - Rect.left, Rect.bottom - Rect.top, PATCOPY);
223 }
224 }
225 }
226 }
227
228 /* Draw the thumb */
229 if (ThumbSize)
230 DrawEdge(hDC, &Rect, EDGE_RAISED, BF_RECT | BF_MIDDLE);
231
232 /* Cleanup */
233 SelectObject(hDC, hSaveBrush);
234 }
235
236 static VOID FASTCALL
237 IntDrawScrollArrows(HDC hDC, PSCROLLBARINFO ScrollBarInfo, BOOL Vertical)
238 {
239 RECT RectLT, RectRB;
240 INT ScrollDirFlagLT, ScrollDirFlagRB;
241
242 RectLT = RectRB = ScrollBarInfo->rcScrollBar;
243 if (Vertical)
244 {
245 ScrollDirFlagLT = DFCS_SCROLLUP;
246 ScrollDirFlagRB = DFCS_SCROLLDOWN;
247 RectLT.bottom = RectLT.top + ScrollBarInfo->dxyLineButton;
248 RectRB.top = RectRB.bottom - ScrollBarInfo->dxyLineButton;
249 }
250 else
251 {
252 ScrollDirFlagLT = DFCS_SCROLLLEFT;
253 ScrollDirFlagRB = DFCS_SCROLLRIGHT;
254 RectLT.right = RectLT.left + ScrollBarInfo->dxyLineButton;
255 RectRB.left = RectRB.right - ScrollBarInfo->dxyLineButton;
256 }
257
258 if (ScrollBarInfo->rgstate[SCROLL_TOP_ARROW] & STATE_SYSTEM_PRESSED)
259 {
260 ScrollDirFlagLT |= DFCS_PUSHED | DFCS_FLAT;
261 }
262 if (ScrollBarInfo->rgstate[SCROLL_TOP_ARROW] & STATE_SYSTEM_UNAVAILABLE)
263 {
264 ScrollDirFlagLT |= DFCS_INACTIVE;
265 }
266 if (ScrollBarInfo->rgstate[SCROLL_BOTTOM_ARROW] & STATE_SYSTEM_PRESSED)
267 {
268 ScrollDirFlagRB |= DFCS_PUSHED | DFCS_FLAT;
269 }
270 if (ScrollBarInfo->rgstate[SCROLL_BOTTOM_ARROW] & STATE_SYSTEM_UNAVAILABLE)
271 {
272 ScrollDirFlagRB |= DFCS_INACTIVE;
273 }
274
275 DrawFrameControl(hDC, &RectLT, DFC_SCROLL, ScrollDirFlagLT);
276 DrawFrameControl(hDC, &RectRB, DFC_SCROLL, ScrollDirFlagRB);
277 }
278
279 static VOID FASTCALL
280 IntScrollDrawMovingThumb(HDC Dc, PSCROLLBARINFO ScrollBarInfo, BOOL Vertical)
281 {
282 INT Pos = ScrollTrackingPos;
283 INT MaxSize;
284 INT OldTop;
285
286 if (Vertical)
287 MaxSize = ScrollBarInfo->rcScrollBar.bottom - ScrollBarInfo->rcScrollBar.top;
288 else
289 MaxSize = ScrollBarInfo->rcScrollBar.right - ScrollBarInfo->rcScrollBar.left;
290
291 MaxSize -= ScrollBarInfo->dxyLineButton + ScrollBarInfo->xyThumbBottom - ScrollBarInfo->xyThumbTop;
292
293 if (Pos < ScrollBarInfo->dxyLineButton)
294 Pos = ScrollBarInfo->dxyLineButton;
295 else if (MaxSize < Pos)
296 Pos = MaxSize;
297
298 OldTop = ScrollBarInfo->xyThumbTop;
299 ScrollBarInfo->xyThumbBottom = Pos + ScrollBarInfo->xyThumbBottom - ScrollBarInfo->xyThumbTop;
300 ScrollBarInfo->xyThumbTop = Pos;
301 IntDrawScrollInterior(ScrollTrackingWin, Dc, ScrollTrackingBar, Vertical, ScrollBarInfo);
302 ScrollBarInfo->xyThumbBottom = OldTop + ScrollBarInfo->xyThumbBottom - ScrollBarInfo->xyThumbTop;
303 ScrollBarInfo->xyThumbTop = OldTop;
304
305 ScrollMovingThumb = !ScrollMovingThumb;
306 }
307
308 static LONG FASTCALL
309 IntScrollGetObjectId(INT SBType)
310 {
311 if (SBType == SB_VERT)
312 return OBJID_VSCROLL;
313 if (SBType == SB_HORZ)
314 return OBJID_HSCROLL;
315 return OBJID_CLIENT;
316 }
317
318 static BOOL FASTCALL
319 IntGetScrollBarInfo(HWND Wnd, INT Bar, PSCROLLBARINFO ScrollBarInfo)
320 {
321 ScrollBarInfo->cbSize = sizeof(SCROLLBARINFO);
322
323 return NtUserGetScrollBarInfo(Wnd, IntScrollGetObjectId(Bar), ScrollBarInfo);
324 }
325
326 void
327 IntDrawScrollBar(HWND Wnd, HDC DC, INT Bar)
328 {
329 //PSBWND pSBWnd;
330 //INT ThumbSize;
331 SCROLLBARINFO Info;
332 BOOL Vertical;
333
334 /*
335 * Get scroll bar info.
336 */
337 switch (Bar)
338 {
339 case SB_HORZ:
340 Vertical = FALSE;
341 break;
342
343 case SB_VERT:
344 Vertical = TRUE;
345 break;
346
347 case SB_CTL:
348 Vertical = (GetWindowLongPtrW(Wnd, GWL_STYLE) & SBS_VERT) != 0;
349 break;
350
351 default:
352 return;
353 }
354 if (!IntGetScrollBarInfo(Wnd, Bar, &Info))
355 {
356 return;
357 }
358
359 if (IsRectEmpty(&Info.rcScrollBar))
360 {
361 return;
362 }
363
364 //ThumbSize = pSBWnd->pSBCalc->pxThumbBottom - pSBWnd->pSBCalc->pxThumbTop;
365
366 /*
367 * Draw the arrows.
368 */
369 if (Info.dxyLineButton)
370 {
371 IntDrawScrollArrows(DC, &Info, Vertical);
372 }
373
374 /*
375 * Draw the interior.
376 */
377 IntDrawScrollInterior(Wnd, DC, Bar, Vertical, &Info);
378
379 /*
380 * If scroll bar has focus, reposition the caret.
381 */
382 if (Wnd == GetFocus() && SB_CTL == Bar)
383 {
384 if (Vertical)
385 {
386 SetCaretPos(Info.rcScrollBar.top + 1, Info.dxyLineButton + 1);
387 }
388 else
389 {
390 SetCaretPos(Info.dxyLineButton + 1, Info.rcScrollBar.top + 1);
391 }
392 }
393 }
394
395 static BOOL FASTCALL
396 IntScrollPtInRectEx(LPRECT Rect, POINT Pt, BOOL Vertical)
397 {
398 RECT TempRect = *Rect;
399 int scrollbarWidth;
400
401 /* Pad hit rect to allow mouse to be dragged outside of scrollbar and
402 * still be considered in the scrollbar. */
403 if (Vertical)
404 {
405 scrollbarWidth = Rect->right - Rect->left;
406 TempRect.left -= scrollbarWidth*8;
407 TempRect.right += scrollbarWidth*8;
408 TempRect.top -= scrollbarWidth*2;
409 TempRect.bottom += scrollbarWidth*2;
410 }
411 else
412 {
413 scrollbarWidth = Rect->bottom - Rect->top;
414 TempRect.left -= scrollbarWidth*2;
415 TempRect.right += scrollbarWidth*2;
416 TempRect.top -= scrollbarWidth*8;
417 TempRect.bottom += scrollbarWidth*8;
418 }
419
420 return PtInRect(&TempRect, Pt);
421 }
422
423 static DWORD FASTCALL
424 IntScrollHitTest(PSCROLLBARINFO ScrollBarInfo, BOOL Vertical, POINT Pt, BOOL Dragging)
425 {
426 INT ArrowSize, ThumbSize, ThumbPos;
427
428 if ((Dragging && ! IntScrollPtInRectEx(&ScrollBarInfo->rcScrollBar, Pt, Vertical)) ||
429 ! PtInRect(&ScrollBarInfo->rcScrollBar, Pt)) return SCROLL_NOWHERE;
430
431 ThumbPos = ScrollBarInfo->xyThumbTop;
432 ThumbSize = ScrollBarInfo->xyThumbBottom - ThumbPos;
433 ArrowSize = ScrollBarInfo->dxyLineButton;
434
435 if (Vertical)
436 {
437 if (Pt.y < ScrollBarInfo->rcScrollBar.top + ArrowSize) return SCROLL_TOP_ARROW;
438 if (Pt.y >= ScrollBarInfo->rcScrollBar.bottom - ArrowSize) return SCROLL_BOTTOM_ARROW;
439 if (!ThumbPos) return SCROLL_TOP_RECT;
440 Pt.y -= ScrollBarInfo->rcScrollBar.top;
441 if (Pt.y < ThumbPos) return SCROLL_TOP_RECT;
442 if (Pt.y >= ThumbPos + ThumbSize) return SCROLL_BOTTOM_RECT;
443 }
444 else
445 {
446 if (Pt.x < ScrollBarInfo->rcScrollBar.left + ArrowSize) return SCROLL_TOP_ARROW;
447 if (Pt.x >= ScrollBarInfo->rcScrollBar.right - ArrowSize) return SCROLL_BOTTOM_ARROW;
448 if (!ThumbPos) return SCROLL_TOP_RECT;
449 Pt.x -= ScrollBarInfo->rcScrollBar.left;
450 if (Pt.x < ThumbPos) return SCROLL_TOP_RECT;
451 if (Pt.x >= ThumbPos + ThumbSize) return SCROLL_BOTTOM_RECT;
452 }
453
454 return SCROLL_THUMB;
455 }
456
457
458 /***********************************************************************
459 * IntScrollGetScrollBarRect
460 *
461 * Compute the scroll bar rectangle, in drawing coordinates (i.e. client
462 * coords for SB_CTL, window coords for SB_VERT and SB_HORZ).
463 * 'arrowSize' returns the width or height of an arrow (depending on
464 * the orientation of the scrollbar), 'thumbSize' returns the size of
465 * the thumb, and 'thumbPos' returns the position of the thumb
466 * relative to the left or to the top.
467 * Return TRUE if the scrollbar is vertical, FALSE if horizontal.
468 */
469 static BOOL FASTCALL
470 IntScrollGetScrollBarRect(HWND Wnd, INT Bar, RECT *Rect,
471 INT *ArrowSize, INT *ThumbSize,
472 INT *ThumbPos)
473 {
474 INT Pixels;
475 BOOL Vertical;
476 PWND pWnd;
477 PSBINFO pSBInfo;
478 PSBDATA pSBData;
479 PSBWND pSBWnd;
480
481 pWnd = ValidateHwnd( Wnd );
482 if (!pWnd) return FALSE;
483 pSBInfo = DesktopPtrToUser(pWnd->pSBInfo);
484
485 *Rect = pWnd->rcClient;
486 OffsetRect( Rect, -pWnd->rcWindow.left, -pWnd->rcWindow.top );
487 if (pWnd->ExStyle & WS_EX_LAYOUTRTL)
488 mirror_rect( &pWnd->rcWindow, Rect );
489
490 switch (Bar)
491 {
492 case SB_HORZ:
493 // WIN_GetRectangles( Wnd, COORDS_WINDOW, NULL, Rect );
494 Rect->top = Rect->bottom;
495 Rect->bottom += GetSystemMetrics(SM_CYHSCROLL);
496 if (pWnd->style & WS_BORDER)
497 {
498 Rect->left--;
499 Rect->right++;
500 }
501 else if (pWnd->style & WS_VSCROLL)
502 {
503 Rect->right++;
504 }
505 Vertical = FALSE;
506 pSBData = &pSBInfo->Horz;
507 break;
508
509 case SB_VERT:
510 // WIN_GetRectangles( Wnd, COORDS_WINDOW, NULL, Rect );
511 if (pWnd->ExStyle & WS_EX_LEFTSCROLLBAR)
512 {
513 Rect->right = Rect->left;
514 Rect->left -= GetSystemMetrics(SM_CXVSCROLL);
515 }
516 else
517 {
518 Rect->left = Rect->right;
519 Rect->right += GetSystemMetrics(SM_CXVSCROLL);
520 }
521 if (pWnd->style & WS_BORDER)
522 {
523 Rect->top--;
524 Rect->bottom++;
525 }
526 else if (pWnd->style & WS_HSCROLL)
527 {
528 Rect->bottom++;
529 }
530 Vertical = TRUE;
531 pSBData = &pSBInfo->Vert;
532 break;
533
534 case SB_CTL:
535 GetClientRect( Wnd, Rect );
536 Vertical = (pWnd->style & SBS_VERT);
537 pSBWnd = (PSBWND)pWnd;
538 pSBData = (PSBDATA)&pSBWnd->SBCalc;
539 break;
540
541 default:
542 return FALSE;
543 }
544
545 if (Vertical) Pixels = Rect->bottom - Rect->top;
546 else Pixels = Rect->right - Rect->left;
547
548 if (Pixels <= 2 * GetSystemMetrics(SM_CXVSCROLL) + SCROLL_MIN_RECT)
549 {
550 if (SCROLL_MIN_RECT < Pixels)
551 *ArrowSize = (Pixels - SCROLL_MIN_RECT) / 2;
552 else
553 *ArrowSize = 0;
554 *ThumbPos = *ThumbSize = 0;
555 }
556 else
557 {
558 *ArrowSize = GetSystemMetrics(SM_CXVSCROLL);
559 Pixels -= (2 * (GetSystemMetrics(SM_CXVSCROLL) - SCROLL_ARROW_THUMB_OVERLAP));
560 if (pSBData->page)
561 {
562 *ThumbSize = MulDiv(Pixels, pSBData->page, (pSBData->posMax - pSBData->posMin + 1));
563 if (*ThumbSize < SCROLL_MIN_THUMB) *ThumbSize = SCROLL_MIN_THUMB;
564 }
565 else *ThumbSize = GetSystemMetrics(SM_CXVSCROLL);
566
567 if (((Pixels -= *ThumbSize ) < 0) ||
568 (( pSBInfo->WSBflags & ESB_DISABLE_BOTH) == ESB_DISABLE_BOTH))
569 {
570 /* Rectangle too small or scrollbar disabled -> no thumb */
571 *ThumbPos = *ThumbSize = 0;
572 }
573 else
574 {
575 INT Max = pSBData->posMax - max(pSBData->page - 1, 0);
576 if (pSBData->posMin >= Max)
577 *ThumbPos = *ArrowSize - SCROLL_ARROW_THUMB_OVERLAP;
578 else
579 *ThumbPos = *ArrowSize - SCROLL_ARROW_THUMB_OVERLAP
580 + MulDiv(Pixels, (pSBData->pos - pSBData->posMin),(Max - pSBData->posMin));
581 }
582 }
583 return Vertical;
584 }
585
586 /***********************************************************************
587 * IntScrollGetThumbVal
588 *
589 * Compute the current scroll position based on the thumb position in pixels
590 * from the top of the scroll-bar.
591 */
592 static UINT FASTCALL
593 IntScrollGetThumbVal(HWND Wnd, INT SBType, PSCROLLBARINFO ScrollBarInfo,
594 BOOL Vertical, INT Pos)
595 {
596 PWND pWnd;
597 PSBDATA pSBData;
598 INT Pixels = Vertical ? ScrollBarInfo->rcScrollBar.bottom
599 - ScrollBarInfo->rcScrollBar.top
600 : ScrollBarInfo->rcScrollBar.right
601 - ScrollBarInfo->rcScrollBar.left;
602
603 pWnd = ValidateHwnd( Wnd );
604 if (!pWnd) return FALSE;
605
606 pSBData = IntGetSBData(pWnd, SBType);
607
608 if ((Pixels -= 2 * ScrollBarInfo->dxyLineButton) <= 0)
609 {
610 return pSBData->posMin;
611 }
612
613 if ((Pixels -= (ScrollBarInfo->xyThumbBottom - ScrollBarInfo->xyThumbTop)) <= 0)
614 {
615 return pSBData->posMin;
616 }
617
618 Pos = Pos - ScrollBarInfo->dxyLineButton;
619 if (Pos < 0)
620 {
621 Pos = 0;
622 }
623 if (Pos > Pixels) Pos = Pixels;
624
625 if (!pSBData->page)
626 Pos *= pSBData->posMax - pSBData->posMin;
627 else
628 Pos *= pSBData->posMax - pSBData->posMin - pSBData->page + 1;
629
630 return pSBData->posMin + ((Pos + Pixels / 2) / Pixels);
631 }
632
633 /***********************************************************************
634 * IntScrollClipPos
635 */
636 static POINT IntScrollClipPos(PRECT lpRect, POINT pt)
637 {
638 if( pt.x < lpRect->left )
639 pt.x = lpRect->left;
640 else
641 if( pt.x > lpRect->right )
642 pt.x = lpRect->right;
643
644 if( pt.y < lpRect->top )
645 pt.y = lpRect->top;
646 else
647 if( pt.y > lpRect->bottom )
648 pt.y = lpRect->bottom;
649
650 return pt;
651 }
652
653 /***********************************************************************
654 * IntScrollDrawSizeGrip
655 *
656 * Draw the size grip.
657 */
658 static void FASTCALL
659 IntScrollDrawSizeGrip(HWND Wnd, HDC Dc)
660 {
661 RECT Rect;
662
663 GetClientRect(Wnd, &Rect);
664 FillRect(Dc, &Rect, GetSysColorBrush(COLOR_SCROLLBAR));
665 Rect.left = max(Rect.left, Rect.right - GetSystemMetrics(SM_CXVSCROLL) - 1);
666 Rect.top = max(Rect.top, Rect.bottom - GetSystemMetrics(SM_CYHSCROLL) - 1);
667 DrawFrameControl(Dc, &Rect, DFC_SCROLL, DFCS_SCROLLSIZEGRIP);
668 }
669
670 /***********************************************************************
671 * SCROLL_RefreshScrollBar
672 *
673 * Repaint the scroll bar interior after a SetScrollRange() or
674 * SetScrollPos() call.
675 */
676 static void SCROLL_RefreshScrollBar( HWND hwnd, INT nBar,
677 BOOL arrows, BOOL interior )
678 {
679 HDC hdc = GetDCEx( hwnd, 0,
680 DCX_CACHE | ((nBar == SB_CTL) ? 0 : DCX_WINDOW) );
681 if (!hdc) return;
682
683 IntDrawScrollBar( hwnd, hdc, nBar);//, arrows, interior );
684 ReleaseDC( hwnd, hdc );
685 }
686
687
688 /***********************************************************************
689 * IntScrollHandleKbdEvent
690 *
691 * Handle a keyboard event (only for SB_CTL scrollbars with focus).
692 */
693 static void FASTCALL
694 IntScrollHandleKbdEvent(
695 HWND Wnd /* [in] Handle of window with scrollbar(s) */,
696 WPARAM wParam /* [in] Variable input including enable state */,
697 LPARAM lParam /* [in] Variable input including input point */)
698 {
699 TRACE("Wnd=%p wParam=%ld lParam=%ld\n", Wnd, wParam, lParam);
700
701 /* hide caret on first KEYDOWN to prevent flicker */
702 if (0 == (lParam & PFD_DOUBLEBUFFER_DONTCARE))
703 {
704 HideCaret(Wnd);
705 }
706
707 switch(wParam)
708 {
709 case VK_PRIOR:
710 wParam = SB_PAGEUP;
711 break;
712
713 case VK_NEXT:
714 wParam = SB_PAGEDOWN;
715 break;
716
717 case VK_HOME:
718 wParam = SB_TOP;
719 break;
720
721 case VK_END:
722 wParam = SB_BOTTOM;
723 break;
724
725 case VK_UP:
726 wParam = SB_LINEUP;
727 break;
728
729 case VK_DOWN:
730 wParam = SB_LINEDOWN;
731 break;
732
733 default:
734 return;
735 }
736
737 SendMessageW(GetParent(Wnd),
738 (0 != (GetWindowLongPtrW(Wnd, GWL_STYLE ) & SBS_VERT) ?
739 WM_VSCROLL : WM_HSCROLL), wParam, (LPARAM) Wnd);
740 }
741
742 /***********************************************************************
743 * IntScrollHandleScrollEvent
744 *
745 * Handle a mouse or timer event for the scrollbar.
746 * 'Pt' is the location of the mouse event in drawing coordinates
747 */
748 static VOID FASTCALL
749 IntScrollHandleScrollEvent(HWND Wnd, INT SBType, UINT Msg, POINT Pt)
750 {
751 static POINT PrevPt; /* Previous mouse position for timer events */
752 static UINT TrackThumbPos; /* Thumb position when tracking started. */
753 static INT LastClickPos; /* Position in the scroll-bar of the last
754 button-down event. */
755 static INT LastMousePos; /* Position in the scroll-bar of the last
756 mouse event. */
757
758 DWORD HitTest;
759 HWND WndOwner, WndCtl;
760 BOOL Vertical;
761 HDC Dc;
762 SCROLLBARINFO ScrollBarInfo;
763 SETSCROLLBARINFO NewInfo;
764
765 if (! IntGetScrollBarInfo(Wnd, SBType, &ScrollBarInfo))
766 {
767 return;
768 }
769 if ((ScrollTrackHitTest == SCROLL_NOWHERE) && (Msg != WM_LBUTTONDOWN))
770 {
771 //// ReactOS : Justin Case something goes wrong.
772 if (Wnd == GetCapture())
773 {
774 ReleaseCapture();
775 }
776 ////
777 return;
778 }
779
780 NewInfo.nTrackPos = ScrollTrackingVal;
781 NewInfo.reserved = ScrollBarInfo.reserved;
782 memcpy(NewInfo.rgstate, ScrollBarInfo.rgstate, (CCHILDREN_SCROLLBAR + 1) * sizeof(DWORD));
783
784 if (SBType == SB_CTL && (GetWindowLongPtrW(Wnd, GWL_STYLE) & (SBS_SIZEGRIP | SBS_SIZEBOX)))
785 {
786 switch(Msg)
787 {
788 case WM_LBUTTONDOWN: /* Initialise mouse tracking */
789 HideCaret(Wnd); /* hide caret while holding down LBUTTON */
790 SetCapture(Wnd);
791 PrevPt = Pt;
792 ScrollTrackHitTest = HitTest = SCROLL_THUMB;
793 break;
794 case WM_MOUSEMOVE:
795 GetClientRect(GetParent(GetParent(Wnd)), &ScrollBarInfo.rcScrollBar);
796 PrevPt = Pt;
797 break;
798 case WM_LBUTTONUP:
799 ReleaseCapture();
800 ScrollTrackHitTest = HitTest = SCROLL_NOWHERE;
801 if (Wnd == GetFocus()) ShowCaret(Wnd);
802 break;
803 case WM_SYSTIMER:
804 Pt = PrevPt;
805 break;
806 }
807 return;
808 }
809
810 Dc = GetDCEx(Wnd, 0, DCX_CACHE | ((SB_CTL == SBType) ? 0 : DCX_WINDOW));
811 if (SB_VERT == SBType)
812 {
813 Vertical = TRUE;
814 }
815 else if (SB_HORZ == SBType)
816 {
817 Vertical = FALSE;
818 }
819 else
820 {
821 Vertical = (0 != (GetWindowLongPtrW(Wnd, GWL_STYLE) & SBS_VERT));
822 }
823 WndOwner = (SB_CTL == SBType) ? GetParent(Wnd) : Wnd;
824 WndCtl = (SB_CTL == SBType) ? Wnd : NULL;
825
826 switch (Msg)
827 {
828 case WM_LBUTTONDOWN: /* Initialise mouse tracking */
829 HideCaret(Wnd); /* hide caret while holding down LBUTTON */
830 ScrollTrackVertical = Vertical;
831 ScrollTrackHitTest = HitTest = IntScrollHitTest(&ScrollBarInfo, Vertical, Pt, FALSE );
832 LastClickPos = Vertical ? (Pt.y - ScrollBarInfo.rcScrollBar.top)
833 : (Pt.x - ScrollBarInfo.rcScrollBar.left);
834 LastMousePos = LastClickPos;
835 TrackThumbPos = ScrollBarInfo.xyThumbTop;
836 PrevPt = Pt;
837 if (SBType == SB_CTL && (GetWindowLongPtrW(Wnd, GWL_STYLE) & WS_TABSTOP)) SetFocus(Wnd);
838 SetCapture(Wnd);
839 ScrollBarInfo.rgstate[ScrollTrackHitTest] |= STATE_SYSTEM_PRESSED;
840 NewInfo.rgstate[ScrollTrackHitTest] = ScrollBarInfo.rgstate[ScrollTrackHitTest];
841 NtUserSetScrollBarInfo(Wnd, IntScrollGetObjectId(SBType), &NewInfo);
842 break;
843
844 case WM_MOUSEMOVE:
845 HitTest = IntScrollHitTest(&ScrollBarInfo, Vertical, Pt, TRUE);
846 PrevPt = Pt;
847 break;
848
849 case WM_LBUTTONUP:
850 HitTest = SCROLL_NOWHERE;
851 ReleaseCapture();
852 /* if scrollbar has focus, show back caret */
853 if (Wnd == GetFocus()) ShowCaret(Wnd);
854 ScrollBarInfo.rgstate[ScrollTrackHitTest] &= ~STATE_SYSTEM_PRESSED;
855 NewInfo.rgstate[ScrollTrackHitTest] = ScrollBarInfo.rgstate[ScrollTrackHitTest];
856 NtUserSetScrollBarInfo(Wnd, IntScrollGetObjectId(SBType), &NewInfo);
857
858 IntDrawScrollArrows(Dc, &ScrollBarInfo, Vertical);
859
860 break;
861
862 case WM_SYSTIMER:
863 Pt = PrevPt;
864 HitTest = IntScrollHitTest(&ScrollBarInfo, Vertical, Pt, FALSE);
865 break;
866
867 default:
868 return; /* Should never happen */
869 }
870
871 TRACE("Event: hwnd=%p bar=%d msg=%s pt=%d,%d hit=%d\n",
872 Wnd, SBType, SPY_GetMsgName(Msg,Wnd), Pt.x, Pt.y, HitTest );
873
874 switch (ScrollTrackHitTest)
875 {
876 case SCROLL_NOWHERE: /* No tracking in progress */
877 break;
878
879 case SCROLL_TOP_ARROW:
880 if (HitTest == ScrollTrackHitTest)
881 {
882 if ((WM_LBUTTONDOWN == Msg) || (WM_SYSTIMER == Msg))
883 {
884 SendMessageW(WndOwner, Vertical ? WM_VSCROLL : WM_HSCROLL,
885 SB_LINEUP, (LPARAM) WndCtl);
886 }
887 SetSystemTimer(Wnd, SCROLL_TIMER, (WM_LBUTTONDOWN == Msg) ?
888 SCROLL_FIRST_DELAY : SCROLL_REPEAT_DELAY,
889 (TIMERPROC) NULL);
890 }
891 else
892 {
893 KillSystemTimer(Wnd, SCROLL_TIMER);
894 }
895 break;
896
897 case SCROLL_TOP_RECT:
898 if (HitTest == ScrollTrackHitTest)
899 {
900 if ((WM_LBUTTONDOWN == Msg) || (WM_SYSTIMER == Msg))
901 {
902 SendMessageW(WndOwner, Vertical ? WM_VSCROLL : WM_HSCROLL,
903 SB_PAGEUP, (LPARAM) WndCtl);
904 }
905 SetSystemTimer(Wnd, SCROLL_TIMER, (WM_LBUTTONDOWN == Msg) ?
906 SCROLL_FIRST_DELAY : SCROLL_REPEAT_DELAY,
907 (TIMERPROC) NULL);
908 }
909 else
910 {
911 KillSystemTimer(Wnd, SCROLL_TIMER);
912 }
913 break;
914
915 case SCROLL_THUMB:
916 if (Msg == WM_LBUTTONDOWN)
917 {
918 ScrollTrackingWin = Wnd;
919 ScrollTrackingBar = SBType;
920 ScrollTrackingPos = TrackThumbPos + LastMousePos - LastClickPos;
921 ScrollTrackingVal = IntScrollGetThumbVal(Wnd, SBType, &ScrollBarInfo,
922 Vertical, ScrollTrackingPos);
923 NewInfo.nTrackPos = ScrollTrackingVal;
924 NtUserSetScrollBarInfo(Wnd, IntScrollGetObjectId(SBType), &NewInfo);
925 IntScrollDrawMovingThumb(Dc, &ScrollBarInfo, Vertical);
926 }
927 else if (Msg == WM_LBUTTONUP)
928 {
929 ScrollTrackingWin = 0;
930 ScrollTrackingVal = 0;
931 IntDrawScrollInterior(Wnd, Dc, SBType, Vertical, &ScrollBarInfo);
932 }
933 else /* WM_MOUSEMOVE */
934 {
935 UINT Pos;
936
937 if (! IntScrollPtInRectEx(&ScrollBarInfo.rcScrollBar, Pt, Vertical))
938 {
939 Pos = LastClickPos;
940 }
941 else
942 {
943 Pt = IntScrollClipPos(&ScrollBarInfo.rcScrollBar, Pt);
944 Pos = Vertical ? (Pt.y - ScrollBarInfo.rcScrollBar.top)
945 : (Pt.x - ScrollBarInfo.rcScrollBar.left);
946 }
947 if (Pos != LastMousePos || ! ScrollMovingThumb)
948 {
949 LastMousePos = Pos;
950 ScrollTrackingPos = TrackThumbPos + Pos - LastClickPos;
951 ScrollTrackingVal = IntScrollGetThumbVal(Wnd, SBType, &ScrollBarInfo,
952 Vertical, ScrollTrackingPos);
953 NewInfo.nTrackPos = ScrollTrackingVal;
954 NtUserSetScrollBarInfo(Wnd, IntScrollGetObjectId(SBType), &NewInfo);
955 IntScrollDrawMovingThumb(Dc, &ScrollBarInfo, Vertical);
956 SendMessageW(WndOwner, Vertical ? WM_VSCROLL : WM_HSCROLL,
957 MAKEWPARAM(SB_THUMBTRACK, ScrollTrackingVal),
958 (LPARAM) WndCtl);
959 }
960 }
961 break;
962
963 case SCROLL_BOTTOM_RECT:
964 if (HitTest == ScrollTrackHitTest)
965 {
966 if ((Msg == WM_LBUTTONDOWN) || (Msg == WM_SYSTIMER))
967 {
968 SendMessageW(WndOwner, Vertical ? WM_VSCROLL : WM_HSCROLL,
969 SB_PAGEDOWN, (LPARAM) WndCtl);
970 }
971 SetSystemTimer(Wnd, SCROLL_TIMER, (WM_LBUTTONDOWN == Msg) ?
972 SCROLL_FIRST_DELAY : SCROLL_REPEAT_DELAY,
973 (TIMERPROC) NULL);
974 }
975 else
976 {
977 KillSystemTimer(Wnd, SCROLL_TIMER);
978 }
979 break;
980
981 case SCROLL_BOTTOM_ARROW:
982 if (HitTest == ScrollTrackHitTest)
983 {
984 if ((Msg == WM_LBUTTONDOWN) || (Msg == WM_SYSTIMER))
985 {
986 SendMessageW(WndOwner, Vertical ? WM_VSCROLL : WM_HSCROLL,
987 SB_LINEDOWN, (LPARAM) WndCtl);
988 }
989 SetSystemTimer(Wnd, SCROLL_TIMER, (WM_LBUTTONDOWN == Msg) ?
990 SCROLL_FIRST_DELAY : SCROLL_REPEAT_DELAY,
991 (TIMERPROC) NULL);
992 }
993 else KillSystemTimer(Wnd, SCROLL_TIMER);
994 break;
995 }
996
997 if (Msg == WM_LBUTTONDOWN)
998 {
999 if (SCROLL_THUMB == HitTest)
1000 {
1001 UINT Val = IntScrollGetThumbVal(Wnd, SBType, &ScrollBarInfo, Vertical,
1002 TrackThumbPos + LastMousePos - LastClickPos);
1003 SendMessageW(WndOwner, Vertical ? WM_VSCROLL : WM_HSCROLL,
1004 MAKEWPARAM(SB_THUMBTRACK, Val), (LPARAM) WndCtl);
1005 }
1006 }
1007
1008 if (Msg == WM_LBUTTONUP)
1009 {
1010 HitTest = ScrollTrackHitTest;
1011 ScrollTrackHitTest = SCROLL_NOWHERE; /* Terminate tracking */
1012
1013 if (SCROLL_THUMB == HitTest)
1014 {
1015 UINT Val = IntScrollGetThumbVal(Wnd, SBType, &ScrollBarInfo, Vertical,
1016 TrackThumbPos + LastMousePos - LastClickPos);
1017 SendMessageW(WndOwner, Vertical ? WM_VSCROLL : WM_HSCROLL,
1018 MAKEWPARAM(SB_THUMBPOSITION, Val), (LPARAM) WndCtl);
1019 }
1020 /* SB_ENDSCROLL doesn't report thumb position */
1021 SendMessageW(WndOwner, Vertical ? WM_VSCROLL : WM_HSCROLL,
1022 SB_ENDSCROLL, (LPARAM) WndCtl);
1023
1024 /* Terminate tracking */
1025 ScrollTrackingWin = 0;
1026 }
1027
1028 ReleaseDC(Wnd, Dc);
1029 }
1030
1031
1032 /***********************************************************************
1033 * IntScrollCreateScrollBar
1034 *
1035 * Create a scroll bar
1036 */
1037 static void IntScrollCreateScrollBar(
1038 HWND Wnd /* [in] Handle of window with scrollbar(s) */,
1039 LPCREATESTRUCTW lpCreate /* [in] The style and place of the scroll bar */)
1040 {
1041 SCROLLINFO Info;
1042
1043 Info.cbSize = sizeof(SCROLLINFO);
1044 Info.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
1045 Info.nMin = 0;
1046 Info.nMax = 100;
1047 Info.nPage = 0;
1048 Info.nPos = 0;
1049 Info.nTrackPos = 0;
1050 NtUserSetScrollInfo(Wnd, SB_CTL, &Info, FALSE);
1051
1052 TRACE("hwnd=%p lpCreate=%p\n", Wnd, lpCreate);
1053
1054 #if 0 /* FIXME */
1055 if (lpCreate->style & WS_DISABLED)
1056 {
1057 // info->flags = ESB_DISABLE_BOTH;
1058 //NtUserEnableScrollBar(Wnd,SB_CTL,(wParam ? ESB_ENABLE_BOTH : ESB_DISABLE_BOTH));
1059 NtUserMessageCall( Wnd, WM_ENABLE, FALSE, 0, 0, FNID_SCROLLBAR, FALSE);
1060 ERR("Created WS_DISABLED scrollbar\n");
1061 }
1062 #endif
1063 if (0 != (lpCreate->style & (SBS_SIZEGRIP | SBS_SIZEBOX)))
1064 {
1065 if (0 != (lpCreate->style & SBS_SIZEBOXTOPLEFTALIGN))
1066 {
1067 MoveWindow(Wnd, lpCreate->x, lpCreate->y, GetSystemMetrics(SM_CXVSCROLL) + 1,
1068 GetSystemMetrics(SM_CYHSCROLL) + 1, FALSE);
1069 }
1070 else if (0 != (lpCreate->style & SBS_SIZEBOXBOTTOMRIGHTALIGN))
1071 {
1072 MoveWindow(Wnd, lpCreate->x + lpCreate->cx - GetSystemMetrics(SM_CXVSCROLL) - 1,
1073 lpCreate->y + lpCreate->cy - GetSystemMetrics(SM_CYHSCROLL) - 1,
1074 GetSystemMetrics(SM_CXVSCROLL) + 1,
1075 GetSystemMetrics(SM_CYHSCROLL) + 1, FALSE);
1076 }
1077 }
1078 else if (0 != (lpCreate->style & SBS_VERT))
1079 {
1080 if (0 != (lpCreate->style & SBS_LEFTALIGN))
1081 {
1082 MoveWindow(Wnd, lpCreate->x, lpCreate->y,
1083 GetSystemMetrics(SM_CXVSCROLL) + 1, lpCreate->cy, FALSE);
1084 }
1085 else if (0 != (lpCreate->style & SBS_RIGHTALIGN))
1086 {
1087 MoveWindow(Wnd,
1088 lpCreate->x + lpCreate->cx - GetSystemMetrics(SM_CXVSCROLL) - 1,
1089 lpCreate->y,
1090 GetSystemMetrics(SM_CXVSCROLL) + 1, lpCreate->cy, FALSE);
1091 }
1092 }
1093 else /* SBS_HORZ */
1094 {
1095 if (0 != (lpCreate->style & SBS_TOPALIGN))
1096 {
1097 MoveWindow(Wnd, lpCreate->x, lpCreate->y,
1098 lpCreate->cx, GetSystemMetrics(SM_CYHSCROLL) + 1, FALSE);
1099 }
1100 else if (0 != (lpCreate->style & SBS_BOTTOMALIGN))
1101 {
1102 MoveWindow(Wnd,
1103 lpCreate->x,
1104 lpCreate->y + lpCreate->cy - GetSystemMetrics(SM_CYHSCROLL) - 1,
1105 lpCreate->cx, GetSystemMetrics(SM_CYHSCROLL) + 1, FALSE);
1106 }
1107 }
1108 }
1109
1110 /* USER32 INTERNAL FUNCTIONS **************************************************/
1111
1112 /***********************************************************************
1113 * ScrollTrackScrollBar
1114 *
1115 * Track a mouse button press on a scroll-bar.
1116 * pt is in screen-coordinates for non-client scroll bars.
1117 */
1118 VOID FASTCALL
1119 ScrollTrackScrollBar(HWND Wnd, INT SBType, POINT Pt)
1120 {
1121 MSG Msg;
1122 UINT XOffset = 0, YOffset = 0;
1123
1124 if (SBType != SB_CTL)
1125 { // Used with CMD mouse tracking.
1126 PWND pwnd = ValidateHwnd(Wnd);
1127 if (!pwnd) return;
1128 XOffset = pwnd->rcClient.left - pwnd->rcWindow.left;
1129 YOffset = pwnd->rcClient.top - pwnd->rcWindow.top;
1130 // RECT rect;
1131 // WIN_GetRectangles( Wnd, COORDS_CLIENT, &rect, NULL );
1132 ScreenToClient(Wnd, &Pt);
1133 // Pt.x -= rect.left;
1134 // Pt.y -= rect.top;
1135 Pt.x += XOffset;
1136 Pt.y += YOffset;
1137 }
1138
1139 IntScrollHandleScrollEvent(Wnd, SBType, WM_LBUTTONDOWN, Pt);
1140
1141 do
1142 {
1143 if (!GetMessageW(&Msg, 0, 0, 0)) break;
1144 if (CallMsgFilterW(&Msg, MSGF_SCROLLBAR)) continue;
1145 if ( Msg.message == WM_LBUTTONUP ||
1146 Msg.message == WM_MOUSEMOVE ||
1147 (Msg.message == WM_SYSTIMER && Msg.wParam == SCROLL_TIMER))
1148 {
1149 Pt.x = LOWORD(Msg.lParam) + XOffset;
1150 Pt.y = HIWORD(Msg.lParam) + YOffset;
1151 IntScrollHandleScrollEvent(Wnd, SBType, Msg.message, Pt);
1152 }
1153 else
1154 {
1155 TranslateMessage(&Msg);
1156 DispatchMessageW(&Msg);
1157 }
1158 if (!IsWindow(Wnd))
1159 {
1160 ReleaseCapture();
1161 break;
1162 }
1163 } while (Msg.message != WM_LBUTTONUP && GetCapture() == Wnd);
1164 }
1165
1166
1167 static DWORD FASTCALL
1168 IntSetScrollInfo(HWND Wnd, LPCSCROLLINFO Info, BOOL bRedraw)
1169 {
1170 DWORD Ret = NtUserSetScrollInfo(Wnd, SB_CTL, Info, bRedraw);
1171 if (Ret) IntNotifyWinEvent(EVENT_OBJECT_VALUECHANGE, Wnd, OBJID_CLIENT, CHILDID_SELF, WEF_SETBYWNDPTI);
1172 return Ret;
1173 }
1174
1175
1176 /***********************************************************************
1177 * ScrollBarWndProc
1178 */
1179 LRESULT WINAPI
1180 ScrollBarWndProc_common(WNDPROC DefWindowProc, HWND Wnd, UINT Msg, WPARAM wParam, LPARAM lParam, BOOL unicode )
1181 {
1182 #ifdef __REACTOS__ // Do this now, remove after Server side is fixed.
1183 PWND pWnd;
1184 PSBWND pSBWnd;
1185 SCROLLINFO ScrollInfo;
1186
1187 pWnd = ValidateHwnd(Wnd);
1188 if (pWnd)
1189 {
1190 if (!pWnd->fnid)
1191 {
1192 ERR("ScrollBar CTL size %d\n",(sizeof(SBWND)-sizeof(WND)));
1193 if ( pWnd->cbwndExtra != (sizeof(SBWND)-sizeof(WND)) )
1194 {
1195 ERR("Wrong Extra bytes for Scrollbar!\n");
1196 return 0;
1197 }
1198
1199 if (Msg != WM_CREATE)
1200 {
1201 return DefWindowProc(Wnd, Msg, wParam, lParam);
1202 }
1203 NtUserSetWindowFNID(Wnd, FNID_SCROLLBAR);
1204 }
1205 else
1206 {
1207 if (pWnd->fnid != FNID_SCROLLBAR)
1208 {
1209 ERR("Wrong window class for Scrollbar!\n");
1210 return 0;
1211 }
1212 }
1213 }
1214 #endif
1215
1216 if (! IsWindow(Wnd))
1217 {
1218 return 0;
1219 }
1220
1221 // Must be a scroll bar control!
1222 pSBWnd = (PSBWND)pWnd;
1223
1224 switch (Msg)
1225 {
1226 case WM_CREATE:
1227 IntScrollCreateScrollBar(Wnd, (LPCREATESTRUCTW) lParam);
1228 break;
1229
1230 case WM_ENABLE:
1231 {
1232 return SendMessageW( Wnd, SBM_ENABLE_ARROWS, wParam ? ESB_ENABLE_BOTH : ESB_DISABLE_BOTH, 0);
1233 }
1234
1235 case WM_LBUTTONDBLCLK:
1236 case WM_LBUTTONDOWN:
1237 if (GetWindowLongW( Wnd, GWL_STYLE ) & SBS_SIZEGRIP)
1238 {
1239 SendMessageW( GetParent(Wnd), WM_SYSCOMMAND,
1240 SC_SIZE + ((GetWindowLongW( Wnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL) ?
1241 WMSZ_BOTTOMLEFT : WMSZ_BOTTOMRIGHT), lParam );
1242 }
1243 else
1244 {
1245 POINT Pt;
1246 Pt.x = (short)LOWORD(lParam);
1247 Pt.y = (short)HIWORD(lParam);
1248 ScrollTrackScrollBar(Wnd, SB_CTL, Pt);
1249 }
1250 break;
1251
1252 case WM_LBUTTONUP:
1253 case WM_MOUSEMOVE:
1254 case WM_SYSTIMER:
1255 {
1256 POINT Pt;
1257 Pt.x = (short)LOWORD(lParam);
1258 Pt.y = (short)HIWORD(lParam);
1259 IntScrollHandleScrollEvent(Wnd, SB_CTL, Msg, Pt);
1260 }
1261 break;
1262
1263 case WM_KEYDOWN:
1264 IntScrollHandleKbdEvent(Wnd, wParam, lParam);
1265 break;
1266
1267 case WM_KEYUP:
1268 ShowCaret(Wnd);
1269 break;
1270
1271 case WM_SETFOCUS:
1272 {
1273 /* Create a caret when a ScrollBar get focus */
1274 RECT Rect;
1275 int ArrowSize, ThumbSize, ThumbPos, Vertical;
1276
1277 Vertical = IntScrollGetScrollBarRect(Wnd, SB_CTL, &Rect,
1278 &ArrowSize, &ThumbSize, &ThumbPos);
1279 if (! Vertical)
1280 {
1281 CreateCaret(Wnd, (HBITMAP) 1, ThumbSize - 2, Rect.bottom - Rect.top - 2);
1282 SetCaretPos(ThumbPos + 1, Rect.top + 1);
1283 }
1284 else
1285 {
1286 CreateCaret(Wnd, (HBITMAP) 1, Rect.right - Rect.left - 2, ThumbSize - 2);
1287 SetCaretPos(Rect.top + 1, ThumbPos + 1);
1288 }
1289 ShowCaret(Wnd);
1290 }
1291 break;
1292
1293 case WM_KILLFOCUS:
1294 {
1295 RECT Rect;
1296 int ArrowSize, ThumbSize, ThumbPos, Vertical;
1297
1298 Vertical = IntScrollGetScrollBarRect(Wnd, SB_CTL, &Rect,
1299 &ArrowSize, &ThumbSize, &ThumbPos);
1300 if (! Vertical)
1301 {
1302 Rect.left = ThumbPos + 1;
1303 Rect.right = Rect.left + ThumbSize;
1304 }
1305 else
1306 {
1307 Rect.top = ThumbPos + 1;
1308 Rect.bottom = Rect.top + ThumbSize;
1309 }
1310 HideCaret(Wnd);
1311 InvalidateRect(Wnd, &Rect, FALSE);
1312 DestroyCaret();
1313 }
1314 break;
1315
1316 case WM_ERASEBKGND:
1317 return 1;
1318
1319 case WM_GETDLGCODE:
1320 return DLGC_WANTARROWS; /* Windows returns this value */
1321
1322 case WM_PAINT:
1323 {
1324 PAINTSTRUCT Ps;
1325 HDC Dc;
1326
1327 Dc = (0 != wParam ? (HDC) wParam : BeginPaint(Wnd, &Ps));
1328
1329 if (GetWindowLongPtrW(Wnd, GWL_STYLE) & SBS_SIZEGRIP)
1330 {
1331 IntScrollDrawSizeGrip(Wnd, Dc);
1332 }
1333 else if (0 != (GetWindowLongPtrW(Wnd, GWL_STYLE) & SBS_SIZEBOX))
1334 {
1335 RECT Rect;
1336 GetClientRect(Wnd, &Rect);
1337 FillRect(Dc, &Rect, GetSysColorBrush(COLOR_SCROLLBAR));
1338 }
1339 else
1340 {
1341 IntDrawScrollBar(Wnd, Dc, SB_CTL/*, TRUE, TRUE*/);
1342 }
1343
1344 if (0 == wParam)
1345 {
1346 EndPaint(Wnd, &Ps);
1347 }
1348 }
1349 break;
1350
1351 case SBM_GETPOS:
1352 return pSBWnd->SBCalc.pos;
1353
1354 case SBM_GETRANGE:
1355 *(LPINT)wParam = pSBWnd->SBCalc.posMin;
1356 *(LPINT)lParam = pSBWnd->SBCalc.posMax;
1357 // This message does not return a value.
1358 return 0;
1359
1360 case SBM_ENABLE_ARROWS:
1361 return EnableScrollBar( Wnd, SB_CTL, wParam );
1362
1363 case SBM_SETPOS:
1364 {
1365 ScrollInfo.cbSize = sizeof(SCROLLINFO);
1366 ScrollInfo.fMask = SIF_POS|SIF_PREVIOUSPOS;
1367 ScrollInfo.nPos = wParam;
1368 return IntSetScrollInfo(Wnd, &ScrollInfo, lParam);
1369 }
1370
1371 case SBM_SETRANGEREDRAW:
1372 case SBM_SETRANGE:
1373 {
1374 ScrollInfo.cbSize = sizeof(SCROLLINFO);
1375 ScrollInfo.fMask = SIF_RANGE|SIF_PREVIOUSPOS;
1376 ScrollInfo.nMin = wParam;
1377 ScrollInfo.nMax = lParam;
1378 return IntSetScrollInfo(Wnd, &ScrollInfo, Msg == SBM_SETRANGEREDRAW ? TRUE : FALSE);
1379 }
1380
1381 case SBM_SETSCROLLINFO:
1382 return IntSetScrollInfo(Wnd, (LPCSCROLLINFO)lParam, wParam);
1383
1384 case SBM_GETSCROLLINFO:
1385 {
1386 PSBDATA pSBData = (PSBDATA)&pSBWnd->SBCalc;
1387 DWORD ret = NtUserSBGetParms(Wnd, SB_CTL, pSBData, (SCROLLINFO *) lParam);
1388 if (!ret)
1389 {
1390 ERR("SBM_GETSCROLLINFO No ScrollInfo\n");
1391 }
1392 return ret;
1393 }
1394 case SBM_GETSCROLLBARINFO:
1395 ((PSCROLLBARINFO)lParam)->cbSize = sizeof(SCROLLBARINFO);
1396 return NtUserGetScrollBarInfo(Wnd, OBJID_CLIENT, (PSCROLLBARINFO)lParam);
1397
1398 case 0x00e5:
1399 case 0x00e7:
1400 case 0x00e8:
1401 case 0x00ec:
1402 case 0x00ed:
1403 case 0x00ee:
1404 case 0x00ef:
1405 WARN("unknown Win32 msg %04x wp=%08lx lp=%08lx\n",
1406 Msg, wParam, lParam );
1407 break;
1408
1409 default:
1410 if (WM_USER <= Msg)
1411 {
1412 WARN("unknown msg %04x wp=%04lx lp=%08lx\n", Msg, wParam, lParam);
1413 }
1414 if (unicode)
1415 return DefWindowProcW( Wnd, Msg, wParam, lParam );
1416 else
1417 return DefWindowProcA( Wnd, Msg, wParam, lParam );
1418 }
1419
1420 return 0;
1421 }
1422
1423 LRESULT WINAPI
1424 ScrollBarWndProcW(HWND Wnd, UINT Msg, WPARAM wParam, LPARAM lParam)
1425 {
1426 return ScrollBarWndProc_common(DefWindowProcW, Wnd, Msg, wParam, lParam, TRUE);
1427 }
1428
1429 LRESULT WINAPI
1430 ScrollBarWndProcA(HWND Wnd, UINT Msg, WPARAM wParam, LPARAM lParam)
1431 {
1432 return ScrollBarWndProc_common(DefWindowProcA, Wnd, Msg, wParam, lParam, FALSE);
1433 }
1434
1435
1436 /* PUBLIC FUNCTIONS ***********************************************************/
1437
1438 /*
1439 * @implemented
1440 */
1441 BOOL
1442 WINAPI
1443 DECLSPEC_HOTPATCH
1444 EnableScrollBar( HWND hwnd, UINT nBar, UINT flags )
1445 {
1446 BOOL Hook, Ret = FALSE;
1447
1448 LoadUserApiHook();
1449
1450 Hook = BeginIfHookedUserApiHook();
1451
1452 /* Bypass SEH and go direct. */
1453 if (!Hook)
1454 {
1455 Ret = NtUserEnableScrollBar(hwnd, nBar, flags);
1456 if (!Ret) return Ret;
1457 SCROLL_RefreshScrollBar( hwnd, nBar, TRUE, TRUE );
1458 return Ret;
1459 }
1460 _SEH2_TRY
1461 {
1462 Ret = guah.EnableScrollBar(hwnd, nBar, flags);
1463 }
1464 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
1465 {
1466 }
1467 _SEH2_END;
1468
1469 EndUserApiHook();
1470
1471 return Ret;
1472 }
1473
1474 BOOL WINAPI
1475 RealGetScrollInfo(HWND Wnd, INT SBType, LPSCROLLINFO Info)
1476 {
1477 PWND pWnd;
1478 PSBDATA pSBData;
1479
1480 if (SB_CTL == SBType)
1481 {
1482 return SendMessageW(Wnd, SBM_GETSCROLLINFO, 0, (LPARAM) Info);
1483 }
1484
1485 pWnd = ValidateHwnd(Wnd);
1486 if (!pWnd) return FALSE;
1487
1488 if (SBType < SB_HORZ || SBType > SB_VERT)
1489 {
1490 SetLastError(ERROR_INVALID_PARAMETER);
1491 return FALSE;
1492 }
1493 if (!pWnd->pSBInfo)
1494 {
1495 SetLastError(ERROR_NO_SCROLLBARS);
1496 return FALSE;
1497 }
1498 pSBData = IntGetSBData(pWnd, SBType);
1499 return NtUserSBGetParms(Wnd, SBType, pSBData, Info);
1500 }
1501
1502 /*
1503 * @implemented
1504 */
1505 BOOL
1506 WINAPI
1507 DECLSPEC_HOTPATCH
1508 GetScrollInfo(HWND Wnd, INT SBType, LPSCROLLINFO Info)
1509 {
1510 BOOL Hook, Ret = FALSE;
1511
1512 LoadUserApiHook();
1513
1514 Hook = BeginIfHookedUserApiHook();
1515
1516 /* Bypass SEH and go direct. */
1517 if (!Hook) return RealGetScrollInfo(Wnd, SBType, Info);
1518
1519 _SEH2_TRY
1520 {
1521 Ret = guah.GetScrollInfo(Wnd, SBType, Info);
1522 }
1523 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
1524 {
1525 }
1526 _SEH2_END;
1527
1528 EndUserApiHook();
1529
1530 return Ret;
1531 }
1532
1533 /*
1534 * @implemented
1535 */
1536 INT
1537 WINAPI
1538 DECLSPEC_HOTPATCH
1539 GetScrollPos(HWND Wnd, INT Bar)
1540 {
1541 PWND pwnd;
1542 PSBDATA pSBData;
1543
1544 TRACE("Wnd=%p Bar=%d\n", Wnd, Bar);
1545
1546 /* Refer SB_CTL requests to the window */
1547 if (SB_CTL == Bar)
1548 {
1549 return SendMessageW(Wnd, SBM_GETPOS, (WPARAM) 0, (LPARAM) 0);
1550 }
1551 else if (Bar == SB_HORZ || Bar == SB_VERT )
1552 {
1553 pwnd = ValidateHwnd(Wnd);
1554 if (!pwnd) return 0;
1555
1556 if (pwnd->pSBInfo)
1557 {
1558 pSBData = IntGetSBData(pwnd, Bar);
1559 return pSBData->pos;
1560 }
1561
1562 SetLastError(ERROR_NO_SCROLLBARS);
1563 TRACE("GetScrollPos No Scroll Info\n");
1564 return 0;
1565 }
1566 SetLastError(ERROR_INVALID_PARAMETER);
1567 return 0;
1568 }
1569
1570 /*
1571 * @implemented
1572 */
1573 BOOL
1574 WINAPI
1575 DECLSPEC_HOTPATCH
1576 GetScrollRange(HWND Wnd, int Bar, LPINT MinPos, LPINT MaxPos)
1577 {
1578 PWND pwnd;
1579 PSBDATA pSBData;
1580
1581 TRACE("Wnd=%x Bar=%d Min=%p Max=%p\n", Wnd, Bar, MinPos, MaxPos);
1582
1583 /* Refer SB_CTL requests to the window */
1584 if (SB_CTL == Bar)
1585 {
1586 return SendMessageW(Wnd, SBM_GETRANGE, (WPARAM) MinPos, (LPARAM) MaxPos);
1587 }
1588 else if (Bar == SB_HORZ || Bar == SB_VERT )
1589 {
1590 pwnd = ValidateHwnd(Wnd);
1591 if (!pwnd) return FALSE;
1592
1593 if (pwnd->pSBInfo)
1594 {
1595 pSBData = IntGetSBData(pwnd, Bar);
1596 *MinPos = pSBData->posMin;
1597 *MaxPos = pSBData->posMax;
1598 }
1599 else
1600 {
1601 SetLastError(ERROR_NO_SCROLLBARS);
1602 *MinPos = 0;
1603 *MaxPos = 0;
1604 }
1605 return TRUE;
1606 }
1607 SetLastError(ERROR_INVALID_PARAMETER);
1608 return FALSE;
1609 }
1610
1611 INT WINAPI
1612 RealSetScrollInfo(HWND Wnd, int SBType, LPCSCROLLINFO Info, BOOL bRedraw)
1613 {
1614 if (SB_CTL == SBType)
1615 {
1616 return SendMessageW(Wnd, SBM_SETSCROLLINFO, (WPARAM) bRedraw, (LPARAM) Info);
1617 }
1618 else
1619 {
1620 return NtUserSetScrollInfo(Wnd, SBType, Info, bRedraw);
1621 }
1622 }
1623
1624 /*
1625 * @implemented
1626 */
1627 INT
1628 WINAPI
1629 DECLSPEC_HOTPATCH
1630 SetScrollInfo(HWND Wnd, int SBType, LPCSCROLLINFO Info, BOOL bRedraw)
1631 {
1632 BOOL Hook;
1633 INT Ret = 0;
1634
1635 LoadUserApiHook();
1636
1637 Hook = BeginIfHookedUserApiHook();
1638
1639 /* Bypass SEH and go direct. */
1640 if (!Hook) return RealSetScrollInfo(Wnd, SBType, Info, bRedraw);
1641
1642 _SEH2_TRY
1643 {
1644 Ret = guah.SetScrollInfo(Wnd, SBType, Info, bRedraw);
1645 }
1646 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
1647 {
1648 }
1649 _SEH2_END;
1650
1651 EndUserApiHook();
1652
1653 return Ret;
1654
1655 }
1656
1657 /*
1658 * @implemented
1659 */
1660 INT
1661 WINAPI
1662 DECLSPEC_HOTPATCH
1663 SetScrollPos(HWND hWnd, INT nBar, INT nPos, BOOL bRedraw)
1664 {
1665 SCROLLINFO ScrollInfo;
1666
1667 ScrollInfo.cbSize = sizeof(SCROLLINFO);
1668 ScrollInfo.fMask = SIF_POS|SIF_PREVIOUSPOS;
1669 ScrollInfo.nPos = nPos;
1670
1671 return RealSetScrollInfo(hWnd, nBar, &ScrollInfo, bRedraw);
1672 }
1673
1674 /*
1675 * @implemented
1676 */
1677 BOOL
1678 WINAPI
1679 DECLSPEC_HOTPATCH
1680 SetScrollRange(HWND hWnd, INT nBar, INT nMinPos, INT nMaxPos, BOOL bRedraw)
1681 {
1682 PWND pWnd;
1683 SCROLLINFO ScrollInfo;
1684
1685 pWnd = ValidateHwnd(hWnd);
1686 if ( !pWnd ) return FALSE;
1687
1688 if ((nMaxPos - nMinPos) > MAXLONG)
1689 {
1690 SetLastError(ERROR_INVALID_SCROLLBAR_RANGE);
1691 return FALSE;
1692 }
1693
1694 ScrollInfo.cbSize = sizeof(SCROLLINFO);
1695 ScrollInfo.fMask = SIF_RANGE;
1696 ScrollInfo.nMin = nMinPos;
1697 ScrollInfo.nMax = nMaxPos;
1698 SetScrollInfo(hWnd, nBar, &ScrollInfo, bRedraw); // do not bypass themes.
1699 return TRUE;
1700 }