xref: /reactos/dll/win32/comctl32/pager.c (revision 8786e12d)
1 /*
2  * Pager control
3  *
4  * Copyright 1998, 1999 Eric Kohl
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * NOTES
21  *
22  * This code was audited for completeness against the documented features
23  * of Comctl32.dll version 6.0 on Sep. 18, 2004, by Robert Shearman.
24  *
25  * Unless otherwise noted, we believe this code to be complete, as per
26  * the specification mentioned above.
27  * If you discover missing features or bugs please note them below.
28  *
29  * TODO:
30  *    Implement repetitive button press.
31  *    Adjust arrow size relative to size of button.
32  *    Allow border size changes.
33  *    Styles:
34  *      PGS_DRAGNDROP
35  *    Notifications:
36  *      PGN_HOTITEMCHANGE
37  *    Messages:
38  *      WM_PRINT and/or WM_PRINTCLIENT
39  *
40  * TESTING:
41  *    Tested primarily with the controlspy Pager application.
42  *       Susan Farley (susan@codeweavers.com)
43  *
44  * IMPLEMENTATION NOTES:
45  *    This control uses WM_NCPAINT instead of WM_PAINT to paint itself
46  *    as we need to scroll a child window. In order to do this we move
47  *    the child window in the control's client area, using the clipping
48  *    region that is automatically set around the client area. As the
49  *    entire client area now consists of the child window, we must
50  *    allocate space (WM_NCCALCSIZE) for the buttons and draw them as
51  *    a non-client area (WM_NCPAINT).
52  *       Robert Shearman <rob@codeweavers.com>
53  */
54 
55 #include <stdarg.h>
56 #include <string.h>
57 #include "windef.h"
58 #include "winbase.h"
59 #include "wingdi.h"
60 #include "winuser.h"
61 #include "winnls.h"
62 #include "commctrl.h"
63 #include "comctl32.h"
64 #include "wine/debug.h"
65 #include "wine/heap.h"
66 
67 WINE_DEFAULT_DEBUG_CHANNEL(pager);
68 
69 typedef struct
70 {
71     HWND   hwndSelf;   /* handle of the control wnd */
72     HWND   hwndChild;  /* handle of the contained wnd */
73     HWND   hwndNotify; /* handle of the parent wnd */
74     DWORD  dwStyle;    /* styles for this control */
75     COLORREF clrBk;    /* background color */
76     INT    nBorder;    /* border size for the control */
77     INT    nButtonSize;/* size of the pager btns */
78     INT    nPos;       /* scroll position */
79     INT    nWidth;     /* from child wnd's response to PGN_CALCSIZE */
80     INT    nHeight;    /* from child wnd's response to PGN_CALCSIZE */
81     BOOL   bForward;   /* forward WM_MOUSEMOVE msgs to the contained wnd */
82     BOOL   bCapture;   /* we have captured the mouse  */
83     INT    TLbtnState; /* state of top or left btn */
84     INT    BRbtnState; /* state of bottom or right btn */
85     INT    direction;  /* direction of the scroll, (e.g. PGF_SCROLLUP) */
86 } PAGER_INFO;
87 
88 #define TIMERID1         1
89 #define TIMERID2         2
90 #define INITIAL_DELAY    500
91 #define REPEAT_DELAY     50
92 
93 static void
94 PAGER_GetButtonRects(const PAGER_INFO* infoPtr, RECT* prcTopLeft, RECT* prcBottomRight, BOOL bClientCoords)
95 {
96     RECT rcWindow;
97     GetWindowRect (infoPtr->hwndSelf, &rcWindow);
98 
99     if (bClientCoords)
100         MapWindowPoints( 0, infoPtr->hwndSelf, (POINT *)&rcWindow, 2 );
101     else
102         OffsetRect(&rcWindow, -rcWindow.left, -rcWindow.top);
103 
104     *prcTopLeft = *prcBottomRight = rcWindow;
105     if (infoPtr->dwStyle & PGS_HORZ)
106     {
107         prcTopLeft->right = prcTopLeft->left + infoPtr->nButtonSize;
108         prcBottomRight->left = prcBottomRight->right - infoPtr->nButtonSize;
109     }
110     else
111     {
112         prcTopLeft->bottom = prcTopLeft->top + infoPtr->nButtonSize;
113         prcBottomRight->top = prcBottomRight->bottom - infoPtr->nButtonSize;
114     }
115 }
116 
117 static void
118 PAGER_DrawButton(HDC hdc, COLORREF clrBk, RECT rc,
119                  BOOL horz, BOOL topLeft, INT btnState)
120 {
121     UINT flags;
122 
123     TRACE("rc = %s, btnState = %d\n", wine_dbgstr_rect(&rc), btnState);
124 
125     if (btnState == PGF_INVISIBLE)
126         return;
127 
128     if ((rc.right - rc.left <= 0) || (rc.bottom - rc.top <= 0))
129         return;
130 
131     if (horz)
132         flags = topLeft ? DFCS_SCROLLLEFT : DFCS_SCROLLRIGHT;
133     else
134         flags = topLeft ? DFCS_SCROLLUP : DFCS_SCROLLDOWN;
135 
136     switch (btnState)
137     {
138     case PGF_HOT:
139         break;
140     case PGF_NORMAL:
141         flags |= DFCS_FLAT;
142         break;
143     case PGF_DEPRESSED:
144         flags |= DFCS_PUSHED;
145         break;
146     case PGF_GRAYED:
147         flags |= DFCS_INACTIVE | DFCS_FLAT;
148         break;
149     }
150     DrawFrameControl( hdc, &rc, DFC_SCROLL, flags );
151 }
152 
153 /* << PAGER_GetDropTarget >> */
154 
155 static inline LRESULT
156 PAGER_ForwardMouse (PAGER_INFO* infoPtr, BOOL bFwd)
157 {
158     TRACE("[%p]\n", infoPtr->hwndSelf);
159 
160     infoPtr->bForward = bFwd;
161 
162     return 0;
163 }
164 
165 static inline LRESULT
166 PAGER_GetButtonState (const PAGER_INFO* infoPtr, INT btn)
167 {
168     LRESULT btnState = PGF_INVISIBLE;
169     TRACE("[%p]\n", infoPtr->hwndSelf);
170 
171     if (btn == PGB_TOPORLEFT)
172         btnState = infoPtr->TLbtnState;
173     else if (btn == PGB_BOTTOMORRIGHT)
174         btnState = infoPtr->BRbtnState;
175 
176     return btnState;
177 }
178 
179 
180 static inline INT
181 PAGER_GetPos(const PAGER_INFO *infoPtr)
182 {
183     TRACE("[%p] returns %d\n", infoPtr->hwndSelf, infoPtr->nPos);
184     return infoPtr->nPos;
185 }
186 
187 static inline INT
188 PAGER_GetButtonSize(const PAGER_INFO *infoPtr)
189 {
190     TRACE("[%p] returns %d\n", infoPtr->hwndSelf, infoPtr->nButtonSize);
191     return infoPtr->nButtonSize;
192 }
193 
194 static inline INT
195 PAGER_GetBorder(const PAGER_INFO *infoPtr)
196 {
197     TRACE("[%p] returns %d\n", infoPtr->hwndSelf, infoPtr->nBorder);
198     return infoPtr->nBorder;
199 }
200 
201 static inline COLORREF
202 PAGER_GetBkColor(const PAGER_INFO *infoPtr)
203 {
204     TRACE("[%p] returns %06x\n", infoPtr->hwndSelf, infoPtr->clrBk);
205     return infoPtr->clrBk;
206 }
207 
208 static void
209 PAGER_CalcSize( PAGER_INFO *infoPtr )
210 {
211     NMPGCALCSIZE nmpgcs;
212     ZeroMemory (&nmpgcs, sizeof (NMPGCALCSIZE));
213     nmpgcs.hdr.hwndFrom = infoPtr->hwndSelf;
214     nmpgcs.hdr.idFrom   = GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_ID);
215     nmpgcs.hdr.code = PGN_CALCSIZE;
216     nmpgcs.dwFlag = (infoPtr->dwStyle & PGS_HORZ) ? PGF_CALCWIDTH : PGF_CALCHEIGHT;
217     nmpgcs.iWidth = infoPtr->nWidth;
218     nmpgcs.iHeight = infoPtr->nHeight;
219     SendMessageW (infoPtr->hwndNotify, WM_NOTIFY, nmpgcs.hdr.idFrom, (LPARAM)&nmpgcs);
220 
221     if (infoPtr->dwStyle & PGS_HORZ)
222         infoPtr->nWidth = nmpgcs.iWidth;
223     else
224         infoPtr->nHeight = nmpgcs.iHeight;
225 
226     TRACE("[%p] PGN_CALCSIZE returns %dx%d\n", infoPtr->hwndSelf, nmpgcs.iWidth, nmpgcs.iHeight );
227 }
228 
229 static void
230 PAGER_PositionChildWnd(PAGER_INFO* infoPtr)
231 {
232     if (infoPtr->hwndChild)
233     {
234         RECT rcClient;
235         int nPos = infoPtr->nPos;
236 
237         /* compensate for a grayed btn, which will soon become invisible */
238         if (infoPtr->TLbtnState == PGF_GRAYED)
239             nPos += infoPtr->nButtonSize;
240 
241         GetClientRect(infoPtr->hwndSelf, &rcClient);
242 
243         if (infoPtr->dwStyle & PGS_HORZ)
244         {
245             int wndSize = max(0, rcClient.right - rcClient.left);
246             if (infoPtr->nWidth < wndSize)
247                 infoPtr->nWidth = wndSize;
248 
249             TRACE("[%p] SWP %dx%d at (%d,%d)\n", infoPtr->hwndSelf,
250                          infoPtr->nWidth, infoPtr->nHeight,
251                          -nPos, 0);
252             SetWindowPos(infoPtr->hwndChild, HWND_TOP,
253                          -nPos, 0,
254                          infoPtr->nWidth, infoPtr->nHeight, 0);
255         }
256         else
257         {
258             int wndSize = max(0, rcClient.bottom - rcClient.top);
259             if (infoPtr->nHeight < wndSize)
260                 infoPtr->nHeight = wndSize;
261 
262             TRACE("[%p] SWP %dx%d at (%d,%d)\n", infoPtr->hwndSelf,
263                          infoPtr->nWidth, infoPtr->nHeight,
264                          0, -nPos);
265             SetWindowPos(infoPtr->hwndChild, HWND_TOP,
266                          0, -nPos,
267                          infoPtr->nWidth, infoPtr->nHeight, 0);
268         }
269 
270         InvalidateRect(infoPtr->hwndChild, NULL, TRUE);
271     }
272 }
273 
274 static INT
275 PAGER_GetScrollRange(PAGER_INFO* infoPtr, BOOL calc_size)
276 {
277     INT scrollRange = 0;
278 
279     if (infoPtr->hwndChild)
280     {
281         INT wndSize, childSize;
282         RECT wndRect;
283         GetWindowRect(infoPtr->hwndSelf, &wndRect);
284 
285         if (calc_size)
286             PAGER_CalcSize(infoPtr);
287         if (infoPtr->dwStyle & PGS_HORZ)
288         {
289             wndSize = wndRect.right - wndRect.left;
290             childSize = infoPtr->nWidth;
291         }
292         else
293         {
294             wndSize = wndRect.bottom - wndRect.top;
295             childSize = infoPtr->nHeight;
296         }
297 
298         TRACE("childSize = %d,  wndSize = %d\n", childSize, wndSize);
299         if (childSize > wndSize)
300             scrollRange = childSize - wndSize + infoPtr->nButtonSize;
301     }
302 
303     TRACE("[%p] returns %d\n", infoPtr->hwndSelf, scrollRange);
304     return scrollRange;
305 }
306 
307 static void
308 PAGER_UpdateBtns(PAGER_INFO *infoPtr, INT scrollRange, BOOL hideGrayBtns)
309 {
310     BOOL resizeClient;
311     BOOL repaintBtns;
312     INT oldTLbtnState = infoPtr->TLbtnState;
313     INT oldBRbtnState = infoPtr->BRbtnState;
314     POINT pt;
315     RECT rcTopLeft, rcBottomRight;
316 
317     /* get button rects */
318     PAGER_GetButtonRects(infoPtr, &rcTopLeft, &rcBottomRight, TRUE);
319 
320     GetCursorPos(&pt);
321     ScreenToClient( infoPtr->hwndSelf, &pt );
322 
323     /* update states based on scroll position */
324     if (infoPtr->nPos > 0)
325     {
326         if (infoPtr->TLbtnState == PGF_INVISIBLE || infoPtr->TLbtnState == PGF_GRAYED)
327             infoPtr->TLbtnState = PGF_NORMAL;
328     }
329     else if (!hideGrayBtns && PtInRect(&rcTopLeft, pt))
330         infoPtr->TLbtnState = PGF_GRAYED;
331     else
332         infoPtr->TLbtnState = PGF_INVISIBLE;
333 
334     if (scrollRange <= 0)
335     {
336         infoPtr->TLbtnState = PGF_INVISIBLE;
337         infoPtr->BRbtnState = PGF_INVISIBLE;
338     }
339     else if (infoPtr->nPos < scrollRange)
340     {
341         if (infoPtr->BRbtnState == PGF_INVISIBLE || infoPtr->BRbtnState == PGF_GRAYED)
342             infoPtr->BRbtnState = PGF_NORMAL;
343     }
344     else if (!hideGrayBtns && PtInRect(&rcBottomRight, pt))
345         infoPtr->BRbtnState = PGF_GRAYED;
346     else
347         infoPtr->BRbtnState = PGF_INVISIBLE;
348 
349     /* only need to resize when entering or leaving PGF_INVISIBLE state */
350     resizeClient =
351         ((oldTLbtnState == PGF_INVISIBLE) != (infoPtr->TLbtnState == PGF_INVISIBLE)) ||
352         ((oldBRbtnState == PGF_INVISIBLE) != (infoPtr->BRbtnState == PGF_INVISIBLE));
353     /* initiate NCCalcSize to resize client wnd if necessary */
354     if (resizeClient)
355         SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0,
356                      SWP_FRAMECHANGED | SWP_NOSIZE | SWP_NOMOVE |
357                      SWP_NOZORDER | SWP_NOACTIVATE);
358 
359     /* repaint when changing any state */
360     repaintBtns = (oldTLbtnState != infoPtr->TLbtnState) ||
361                   (oldBRbtnState != infoPtr->BRbtnState);
362     if (repaintBtns)
363         SendMessageW(infoPtr->hwndSelf, WM_NCPAINT, 0, 0);
364 }
365 
366 static LRESULT
367 PAGER_SetPos(PAGER_INFO* infoPtr, INT newPos, BOOL fromBtnPress, BOOL calc_size)
368 {
369     INT scrollRange = PAGER_GetScrollRange(infoPtr, calc_size);
370     INT oldPos = infoPtr->nPos;
371 
372     if ((scrollRange <= 0) || (newPos < 0))
373         infoPtr->nPos = 0;
374     else if (newPos > scrollRange)
375         infoPtr->nPos = scrollRange;
376     else
377         infoPtr->nPos = newPos;
378 
379     TRACE("[%p] pos=%d, oldpos=%d\n", infoPtr->hwndSelf, infoPtr->nPos, oldPos);
380 
381     if (infoPtr->nPos != oldPos)
382     {
383         /* gray and restore btns, and if from WM_SETPOS, hide the gray btns */
384         PAGER_UpdateBtns(infoPtr, scrollRange, !fromBtnPress);
385         PAGER_PositionChildWnd(infoPtr);
386     }
387 
388     return 0;
389 }
390 
391 /******************************************************************
392  * For the PGM_RECALCSIZE message (but not the other uses in      *
393  * this module), the native control does only the following:      *
394  *                                                                *
395  *    if (some condition)                                         *
396  *          PostMessageW(hwnd, EM_FMTLINES, 0, 0);                *
397  *    return DefWindowProcW(hwnd, PGM_RECALCSIZE, 0, 0);          *
398  *                                                                *
399  * When we figure out what the "some condition" is we will        *
400  * implement that for the message processing.                     *
401  ******************************************************************/
402 
403 static LRESULT
404 PAGER_RecalcSize(PAGER_INFO *infoPtr)
405 {
406     TRACE("[%p]\n", infoPtr->hwndSelf);
407 
408     if (infoPtr->hwndChild)
409     {
410         INT scrollRange = PAGER_GetScrollRange(infoPtr, TRUE);
411 
412         if (scrollRange <= 0)
413         {
414             infoPtr->nPos = -1;
415             PAGER_SetPos(infoPtr, 0, FALSE, TRUE);
416         }
417         else
418             PAGER_PositionChildWnd(infoPtr);
419     }
420 
421     return 1;
422 }
423 
424 
425 static COLORREF
426 PAGER_SetBkColor (PAGER_INFO* infoPtr, COLORREF clrBk)
427 {
428     COLORREF clrTemp = infoPtr->clrBk;
429 
430     infoPtr->clrBk = clrBk;
431     TRACE("[%p] %06x\n", infoPtr->hwndSelf, infoPtr->clrBk);
432 
433     /* the native control seems to do things this way */
434     SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0,
435 		 SWP_FRAMECHANGED | SWP_NOSIZE | SWP_NOMOVE |
436 		 SWP_NOZORDER | SWP_NOACTIVATE);
437 
438     RedrawWindow(infoPtr->hwndSelf, 0, 0, RDW_ERASE | RDW_INVALIDATE);
439 
440     return clrTemp;
441 }
442 
443 
444 static INT
445 PAGER_SetBorder (PAGER_INFO* infoPtr, INT iBorder)
446 {
447     INT nTemp = infoPtr->nBorder;
448 
449     infoPtr->nBorder = iBorder;
450     TRACE("[%p] %d\n", infoPtr->hwndSelf, infoPtr->nBorder);
451 
452     PAGER_RecalcSize(infoPtr);
453 
454     return nTemp;
455 }
456 
457 
458 static INT
459 PAGER_SetButtonSize (PAGER_INFO* infoPtr, INT iButtonSize)
460 {
461     INT nTemp = infoPtr->nButtonSize;
462 
463     infoPtr->nButtonSize = iButtonSize;
464     TRACE("[%p] %d\n", infoPtr->hwndSelf, infoPtr->nButtonSize);
465 
466     PAGER_RecalcSize(infoPtr);
467 
468     return nTemp;
469 }
470 
471 
472 static LRESULT
473 PAGER_SetChild (PAGER_INFO* infoPtr, HWND hwndChild)
474 {
475     infoPtr->hwndChild = IsWindow (hwndChild) ? hwndChild : 0;
476 
477     if (infoPtr->hwndChild)
478     {
479         TRACE("[%p] hwndChild=%p\n", infoPtr->hwndSelf, infoPtr->hwndChild);
480 
481         SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0,
482                      SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
483 
484         infoPtr->nPos = -1;
485         PAGER_SetPos(infoPtr, 0, FALSE, FALSE);
486     }
487 
488     return 0;
489 }
490 
491 static void
492 PAGER_Scroll(PAGER_INFO* infoPtr, INT dir)
493 {
494     NMPGSCROLL nmpgScroll;
495     RECT rcWnd;
496 
497     if (infoPtr->hwndChild)
498     {
499         ZeroMemory (&nmpgScroll, sizeof (NMPGSCROLL));
500         nmpgScroll.hdr.hwndFrom = infoPtr->hwndSelf;
501         nmpgScroll.hdr.idFrom   = GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_ID);
502         nmpgScroll.hdr.code = PGN_SCROLL;
503 
504         GetWindowRect(infoPtr->hwndSelf, &rcWnd);
505         GetClientRect(infoPtr->hwndSelf, &nmpgScroll.rcParent);
506         nmpgScroll.iXpos = nmpgScroll.iYpos = 0;
507         nmpgScroll.iDir = dir;
508 
509         if (infoPtr->dwStyle & PGS_HORZ)
510         {
511             nmpgScroll.iScroll = rcWnd.right - rcWnd.left;
512             nmpgScroll.iXpos = infoPtr->nPos;
513         }
514         else
515         {
516             nmpgScroll.iScroll = rcWnd.bottom - rcWnd.top;
517             nmpgScroll.iYpos = infoPtr->nPos;
518         }
519         nmpgScroll.iScroll -= 2*infoPtr->nButtonSize;
520 
521         SendMessageW (infoPtr->hwndNotify, WM_NOTIFY, nmpgScroll.hdr.idFrom, (LPARAM)&nmpgScroll);
522 
523         TRACE("[%p] PGN_SCROLL returns iScroll=%d\n", infoPtr->hwndSelf, nmpgScroll.iScroll);
524 
525         if (nmpgScroll.iScroll > 0)
526         {
527             infoPtr->direction = dir;
528 
529             if (dir == PGF_SCROLLLEFT || dir == PGF_SCROLLUP)
530                 PAGER_SetPos(infoPtr, infoPtr->nPos - nmpgScroll.iScroll, TRUE, TRUE);
531             else
532                 PAGER_SetPos(infoPtr, infoPtr->nPos + nmpgScroll.iScroll, TRUE, TRUE);
533         }
534         else
535             infoPtr->direction = -1;
536     }
537 }
538 
539 static LRESULT
540 PAGER_FmtLines(const PAGER_INFO *infoPtr)
541 {
542     /* initiate NCCalcSize to resize client wnd and get size */
543     SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0,
544 		 SWP_FRAMECHANGED | SWP_NOSIZE | SWP_NOMOVE |
545 		 SWP_NOZORDER | SWP_NOACTIVATE);
546 
547     SetWindowPos(infoPtr->hwndChild, 0,
548 		 0,0,infoPtr->nWidth,infoPtr->nHeight,
549 		 0);
550 
551     return DefWindowProcW (infoPtr->hwndSelf, EM_FMTLINES, 0, 0);
552 }
553 
554 static LRESULT
555 PAGER_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
556 {
557     PAGER_INFO *infoPtr;
558 
559     /* allocate memory for info structure */
560     infoPtr = heap_alloc_zero (sizeof(*infoPtr));
561     if (!infoPtr) return -1;
562     SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
563 
564     /* set default settings */
565     infoPtr->hwndSelf = hwnd;
566     infoPtr->hwndChild = NULL;
567     infoPtr->hwndNotify = lpcs->hwndParent;
568     infoPtr->dwStyle = lpcs->style;
569     infoPtr->clrBk = GetSysColor(COLOR_BTNFACE);
570     infoPtr->nBorder = 0;
571     infoPtr->nButtonSize = 12;
572     infoPtr->nPos = 0;
573     infoPtr->nWidth = 0;
574     infoPtr->nHeight = 0;
575     infoPtr->bForward = FALSE;
576     infoPtr->bCapture = FALSE;
577     infoPtr->TLbtnState = PGF_INVISIBLE;
578     infoPtr->BRbtnState = PGF_INVISIBLE;
579     infoPtr->direction = -1;
580 
581     if (infoPtr->dwStyle & PGS_DRAGNDROP)
582         FIXME("[%p] Drag and Drop style is not implemented yet.\n", infoPtr->hwndSelf);
583 
584     return 0;
585 }
586 
587 
588 static LRESULT
589 PAGER_Destroy (PAGER_INFO *infoPtr)
590 {
591     SetWindowLongPtrW (infoPtr->hwndSelf, 0, 0);
592     heap_free (infoPtr);
593     return 0;
594 }
595 
596 static LRESULT
597 PAGER_NCCalcSize(PAGER_INFO* infoPtr, WPARAM wParam, LPRECT lpRect)
598 {
599     RECT rcChild, rcWindow;
600 
601     /*
602      * lpRect points to a RECT struct.  On entry, the struct
603      * contains the proposed wnd rectangle for the window.
604      * On exit, the struct should contain the screen
605      * coordinates of the corresponding window's client area.
606      */
607 
608     DefWindowProcW (infoPtr->hwndSelf, WM_NCCALCSIZE, wParam, (LPARAM)lpRect);
609 
610     TRACE("orig rect=%s\n", wine_dbgstr_rect(lpRect));
611 
612     GetWindowRect (infoPtr->hwndChild, &rcChild);
613     MapWindowPoints (0, infoPtr->hwndSelf, (LPPOINT)&rcChild, 2); /* FIXME: RECT != 2 POINTS */
614     GetWindowRect (infoPtr->hwndSelf, &rcWindow);
615 
616     infoPtr->nWidth = lpRect->right - lpRect->left;
617     infoPtr->nHeight = lpRect->bottom - lpRect->top;
618     PAGER_CalcSize( infoPtr );
619 
620     if (infoPtr->dwStyle & PGS_HORZ)
621     {
622 	if (infoPtr->TLbtnState && (lpRect->left + infoPtr->nButtonSize < lpRect->right))
623 	    lpRect->left += infoPtr->nButtonSize;
624 	if (infoPtr->BRbtnState && (lpRect->right - infoPtr->nButtonSize > lpRect->left))
625 	    lpRect->right -= infoPtr->nButtonSize;
626     }
627     else
628     {
629 	if (infoPtr->TLbtnState && (lpRect->top + infoPtr->nButtonSize < lpRect->bottom))
630 	    lpRect->top += infoPtr->nButtonSize;
631 	if (infoPtr->BRbtnState && (lpRect->bottom - infoPtr->nButtonSize > lpRect->top))
632 	    lpRect->bottom -= infoPtr->nButtonSize;
633     }
634 
635     TRACE("nPos=%d, nHeight=%d, window=%s\n", infoPtr->nPos, infoPtr->nHeight, wine_dbgstr_rect(&rcWindow));
636     TRACE("[%p] client rect set to %s BtnState[%d,%d]\n", infoPtr->hwndSelf, wine_dbgstr_rect(lpRect),
637 	  infoPtr->TLbtnState, infoPtr->BRbtnState);
638 
639     return 0;
640 }
641 
642 static LRESULT
643 PAGER_NCPaint (const PAGER_INFO* infoPtr, HRGN hRgn)
644 {
645     RECT rcBottomRight, rcTopLeft;
646     HDC hdc;
647 
648     if (infoPtr->dwStyle & WS_MINIMIZE)
649         return 0;
650 
651     DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)hRgn, 0);
652 
653     if (!(hdc = GetDCEx (infoPtr->hwndSelf, 0, DCX_USESTYLE | DCX_WINDOW)))
654         return 0;
655 
656     PAGER_GetButtonRects(infoPtr, &rcTopLeft, &rcBottomRight, FALSE);
657 
658     PAGER_DrawButton(hdc, infoPtr->clrBk, rcTopLeft,
659                      infoPtr->dwStyle & PGS_HORZ, TRUE, infoPtr->TLbtnState);
660     PAGER_DrawButton(hdc, infoPtr->clrBk, rcBottomRight,
661                      infoPtr->dwStyle & PGS_HORZ, FALSE, infoPtr->BRbtnState);
662 
663     ReleaseDC( infoPtr->hwndSelf, hdc );
664     return 0;
665 }
666 
667 static INT
668 PAGER_HitTest (const PAGER_INFO* infoPtr, const POINT * pt)
669 {
670     RECT clientRect, rcTopLeft, rcBottomRight;
671     POINT ptWindow;
672 
673     GetClientRect (infoPtr->hwndSelf, &clientRect);
674 
675     if (PtInRect(&clientRect, *pt))
676     {
677         TRACE("child\n");
678         return -1;
679     }
680 
681     ptWindow = *pt;
682     PAGER_GetButtonRects(infoPtr, &rcTopLeft, &rcBottomRight, TRUE);
683 
684     if ((infoPtr->TLbtnState != PGF_INVISIBLE) && PtInRect(&rcTopLeft, ptWindow))
685     {
686         TRACE("PGB_TOPORLEFT\n");
687         return PGB_TOPORLEFT;
688     }
689     else if ((infoPtr->BRbtnState != PGF_INVISIBLE) && PtInRect(&rcBottomRight, ptWindow))
690     {
691         TRACE("PGB_BOTTOMORRIGHT\n");
692         return PGB_BOTTOMORRIGHT;
693     }
694 
695     TRACE("nowhere\n");
696     return -1;
697 }
698 
699 static LRESULT
700 PAGER_NCHitTest (const PAGER_INFO* infoPtr, INT x, INT y)
701 {
702     POINT pt;
703     INT nHit;
704 
705     pt.x = x;
706     pt.y = y;
707 
708     ScreenToClient (infoPtr->hwndSelf, &pt);
709     nHit = PAGER_HitTest(infoPtr, &pt);
710 
711     return (nHit < 0) ? HTTRANSPARENT : HTCLIENT;
712 }
713 
714 static LRESULT
715 PAGER_MouseMove (PAGER_INFO* infoPtr, INT keys, INT x, INT y)
716 {
717     POINT clpt, pt;
718     RECT wnrect;
719     BOOL topLeft = FALSE;
720     INT btnstate = 0;
721     INT hit;
722     HDC hdc;
723 
724     pt.x = x;
725     pt.y = y;
726 
727     TRACE("[%p] to (%d,%d)\n", infoPtr->hwndSelf, x, y);
728     ClientToScreen(infoPtr->hwndSelf, &pt);
729     GetWindowRect(infoPtr->hwndSelf, &wnrect);
730     if (PtInRect(&wnrect, pt)) {
731 	RECT topleft, bottomright, *rect = NULL;
732 
733 	PAGER_GetButtonRects(infoPtr, &topleft, &bottomright, FALSE);
734 
735 	clpt = pt;
736 	MapWindowPoints(0, infoPtr->hwndSelf, &clpt, 1);
737 	hit = PAGER_HitTest(infoPtr, &clpt);
738 	if ((hit == PGB_TOPORLEFT) && (infoPtr->TLbtnState == PGF_NORMAL))
739 	{
740 	    topLeft = TRUE;
741 	    rect = &topleft;
742 	    infoPtr->TLbtnState = PGF_HOT;
743 	    btnstate = infoPtr->TLbtnState;
744 	}
745 	else if ((hit == PGB_BOTTOMORRIGHT) && (infoPtr->BRbtnState == PGF_NORMAL))
746 	{
747 	    topLeft = FALSE;
748 	    rect = &bottomright;
749 	    infoPtr->BRbtnState = PGF_HOT;
750 	    btnstate = infoPtr->BRbtnState;
751 	}
752 
753 	/* If in one of the buttons the capture and draw buttons */
754 	if (rect)
755 	{
756             TRACE("[%p] draw btn (%s), Capture %s, style %08x\n",
757                   infoPtr->hwndSelf, wine_dbgstr_rect(rect),
758 		  (infoPtr->bCapture) ? "TRUE" : "FALSE",
759 		  infoPtr->dwStyle);
760 	    if (!infoPtr->bCapture)
761 	    {
762 	        TRACE("[%p] SetCapture\n", infoPtr->hwndSelf);
763 	        SetCapture(infoPtr->hwndSelf);
764 	        infoPtr->bCapture = TRUE;
765 	    }
766 	    if (infoPtr->dwStyle & PGS_AUTOSCROLL)
767 		SetTimer(infoPtr->hwndSelf, TIMERID1, 0x3e, 0);
768 	    hdc = GetWindowDC(infoPtr->hwndSelf);
769 	    /* OffsetRect(wnrect, 0 | 1, 0 | 1) */
770 	    PAGER_DrawButton(hdc, infoPtr->clrBk, *rect,
771 			     infoPtr->dwStyle & PGS_HORZ, topLeft, btnstate);
772 	    ReleaseDC(infoPtr->hwndSelf, hdc);
773 	    return 0;
774 	}
775     }
776 
777     /* If we think we are captured, then do release */
778     if (infoPtr->bCapture && (WindowFromPoint(pt) != infoPtr->hwndSelf))
779     {
780     	NMHDR nmhdr;
781 
782         infoPtr->bCapture = FALSE;
783 
784         if (GetCapture() == infoPtr->hwndSelf)
785         {
786             ReleaseCapture();
787 
788             if (infoPtr->TLbtnState == PGF_GRAYED)
789             {
790                 infoPtr->TLbtnState = PGF_INVISIBLE;
791                 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0,
792                              SWP_FRAMECHANGED | SWP_NOSIZE | SWP_NOMOVE |
793                              SWP_NOZORDER | SWP_NOACTIVATE);
794             }
795             else if (infoPtr->TLbtnState == PGF_HOT)
796             {
797         	infoPtr->TLbtnState = PGF_NORMAL;
798         	/* FIXME: just invalidate button rect */
799                 RedrawWindow(infoPtr->hwndSelf, NULL, NULL, RDW_FRAME | RDW_INVALIDATE);
800             }
801 
802             if (infoPtr->BRbtnState == PGF_GRAYED)
803             {
804                 infoPtr->BRbtnState = PGF_INVISIBLE;
805                 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0,
806                              SWP_FRAMECHANGED | SWP_NOSIZE | SWP_NOMOVE |
807                              SWP_NOZORDER | SWP_NOACTIVATE);
808             }
809             else if (infoPtr->BRbtnState == PGF_HOT)
810             {
811         	infoPtr->BRbtnState = PGF_NORMAL;
812         	/* FIXME: just invalidate button rect */
813                 RedrawWindow(infoPtr->hwndSelf, NULL, NULL, RDW_FRAME | RDW_INVALIDATE);
814             }
815 
816             /* Notify parent of released mouse capture */
817         	memset(&nmhdr, 0, sizeof(NMHDR));
818         	nmhdr.hwndFrom = infoPtr->hwndSelf;
819         	nmhdr.idFrom   = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
820         	nmhdr.code = NM_RELEASEDCAPTURE;
821 		SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr.idFrom, (LPARAM)&nmhdr);
822         }
823         if (IsWindow(infoPtr->hwndSelf))
824             KillTimer(infoPtr->hwndSelf, TIMERID1);
825     }
826     return 0;
827 }
828 
829 static LRESULT
830 PAGER_LButtonDown (PAGER_INFO* infoPtr, INT keys, INT x, INT y)
831 {
832     BOOL repaintBtns = FALSE;
833     POINT pt;
834     INT hit;
835 
836     pt.x = x;
837     pt.y = y;
838 
839     TRACE("[%p] at (%d,%d)\n", infoPtr->hwndSelf, x, y);
840 
841     hit = PAGER_HitTest(infoPtr, &pt);
842 
843     /* put btn in DEPRESSED state */
844     if (hit == PGB_TOPORLEFT)
845     {
846         repaintBtns = infoPtr->TLbtnState != PGF_DEPRESSED;
847         infoPtr->TLbtnState = PGF_DEPRESSED;
848         SetTimer(infoPtr->hwndSelf, TIMERID1, INITIAL_DELAY, 0);
849     }
850     else if (hit == PGB_BOTTOMORRIGHT)
851     {
852         repaintBtns = infoPtr->BRbtnState != PGF_DEPRESSED;
853         infoPtr->BRbtnState = PGF_DEPRESSED;
854         SetTimer(infoPtr->hwndSelf, TIMERID1, INITIAL_DELAY, 0);
855     }
856 
857     if (repaintBtns)
858         SendMessageW(infoPtr->hwndSelf, WM_NCPAINT, 0, 0);
859 
860     switch(hit)
861     {
862     case PGB_TOPORLEFT:
863         if (infoPtr->dwStyle & PGS_HORZ)
864         {
865             TRACE("[%p] PGF_SCROLLLEFT\n", infoPtr->hwndSelf);
866             PAGER_Scroll(infoPtr, PGF_SCROLLLEFT);
867         }
868         else
869         {
870             TRACE("[%p] PGF_SCROLLUP\n", infoPtr->hwndSelf);
871             PAGER_Scroll(infoPtr, PGF_SCROLLUP);
872         }
873         break;
874     case PGB_BOTTOMORRIGHT:
875         if (infoPtr->dwStyle & PGS_HORZ)
876         {
877             TRACE("[%p] PGF_SCROLLRIGHT\n", infoPtr->hwndSelf);
878             PAGER_Scroll(infoPtr, PGF_SCROLLRIGHT);
879         }
880         else
881         {
882             TRACE("[%p] PGF_SCROLLDOWN\n", infoPtr->hwndSelf);
883             PAGER_Scroll(infoPtr, PGF_SCROLLDOWN);
884         }
885         break;
886     default:
887         break;
888     }
889 
890     return 0;
891 }
892 
893 static LRESULT
894 PAGER_LButtonUp (PAGER_INFO* infoPtr, INT keys, INT x, INT y)
895 {
896     TRACE("[%p]\n", infoPtr->hwndSelf);
897 
898     KillTimer (infoPtr->hwndSelf, TIMERID1);
899     KillTimer (infoPtr->hwndSelf, TIMERID2);
900 
901     /* make PRESSED btns NORMAL but don't hide gray btns */
902     if (infoPtr->TLbtnState & (PGF_HOT | PGF_DEPRESSED))
903         infoPtr->TLbtnState = PGF_NORMAL;
904     if (infoPtr->BRbtnState & (PGF_HOT | PGF_DEPRESSED))
905         infoPtr->BRbtnState = PGF_NORMAL;
906 
907     return 0;
908 }
909 
910 static LRESULT
911 PAGER_Timer (PAGER_INFO* infoPtr, INT nTimerId)
912 {
913     INT dir;
914 
915     /* if initial timer, kill it and start the repeat timer */
916     if (nTimerId == TIMERID1) {
917 	if (infoPtr->TLbtnState == PGF_HOT)
918 	    dir = (infoPtr->dwStyle & PGS_HORZ) ?
919 		PGF_SCROLLLEFT : PGF_SCROLLUP;
920 	else
921 	    dir = (infoPtr->dwStyle & PGS_HORZ) ?
922 		PGF_SCROLLRIGHT : PGF_SCROLLDOWN;
923 	TRACE("[%p] TIMERID1: style=%08x, dir=%d\n",
924               infoPtr->hwndSelf, infoPtr->dwStyle, dir);
925 	KillTimer(infoPtr->hwndSelf, TIMERID1);
926 	SetTimer(infoPtr->hwndSelf, TIMERID1, REPEAT_DELAY, 0);
927 	if (infoPtr->dwStyle & PGS_AUTOSCROLL) {
928 	    PAGER_Scroll(infoPtr, dir);
929 	    SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0,
930 			 SWP_FRAMECHANGED | SWP_NOSIZE | SWP_NOMOVE |
931 			 SWP_NOZORDER | SWP_NOACTIVATE);
932 	}
933 	return 0;
934 
935     }
936 
937     TRACE("[%p] TIMERID2: dir=%d\n", infoPtr->hwndSelf, infoPtr->direction);
938     KillTimer(infoPtr->hwndSelf, TIMERID2);
939     if (infoPtr->direction > 0) {
940 	PAGER_Scroll(infoPtr, infoPtr->direction);
941 	SetTimer(infoPtr->hwndSelf, TIMERID2, REPEAT_DELAY, 0);
942     }
943     return 0;
944 }
945 
946 static LRESULT
947 PAGER_EraseBackground (const PAGER_INFO* infoPtr, HDC hdc)
948 {
949     POINT pt, ptorig;
950     HWND parent;
951     LRESULT ret;
952 
953     pt.x = 0;
954     pt.y = 0;
955     parent = GetParent(infoPtr->hwndSelf);
956     MapWindowPoints(infoPtr->hwndSelf, parent, &pt, 1);
957     OffsetWindowOrgEx (hdc, pt.x, pt.y, &ptorig);
958     ret = SendMessageW (parent, WM_ERASEBKGND, (WPARAM)hdc, 0);
959     SetWindowOrgEx (hdc, ptorig.x, ptorig.y, 0);
960 
961     return ret;
962 }
963 
964 
965 static LRESULT
966 PAGER_Size (PAGER_INFO* infoPtr, INT type, INT x, INT y)
967 {
968     /* note that WM_SIZE is sent whenever NCCalcSize resizes the client wnd */
969 
970     TRACE("[%p] %d,%d\n", infoPtr->hwndSelf, x, y);
971 
972     if (infoPtr->dwStyle & PGS_HORZ)
973         infoPtr->nHeight = y;
974     else
975         infoPtr->nWidth = x;
976 
977     return PAGER_RecalcSize(infoPtr);
978 }
979 
980 
981 static LRESULT
982 PAGER_StyleChanged(PAGER_INFO *infoPtr, WPARAM wStyleType, const STYLESTRUCT *lpss)
983 {
984     DWORD oldStyle = infoPtr->dwStyle;
985 
986     TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
987           wStyleType, lpss->styleOld, lpss->styleNew);
988 
989     if (wStyleType != GWL_STYLE) return 0;
990 
991     infoPtr->dwStyle = lpss->styleNew;
992 
993     if ((oldStyle ^ lpss->styleNew) & (PGS_HORZ | PGS_VERT))
994     {
995         PAGER_RecalcSize(infoPtr);
996     }
997 
998     return 0;
999 }
1000 
1001 static LRESULT WINAPI
1002 PAGER_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1003 {
1004     PAGER_INFO *infoPtr = (PAGER_INFO *)GetWindowLongPtrW(hwnd, 0);
1005 
1006     TRACE("(%p, %#x, %#lx, %#lx)\n", hwnd, uMsg, wParam, lParam);
1007 
1008     if (!infoPtr && (uMsg != WM_CREATE))
1009 	return DefWindowProcW (hwnd, uMsg, wParam, lParam);
1010 
1011     switch (uMsg)
1012     {
1013         case EM_FMTLINES:
1014 	    return PAGER_FmtLines(infoPtr);
1015 
1016         case PGM_FORWARDMOUSE:
1017             return PAGER_ForwardMouse (infoPtr, (BOOL)wParam);
1018 
1019         case PGM_GETBKCOLOR:
1020             return PAGER_GetBkColor(infoPtr);
1021 
1022         case PGM_GETBORDER:
1023             return PAGER_GetBorder(infoPtr);
1024 
1025         case PGM_GETBUTTONSIZE:
1026             return PAGER_GetButtonSize(infoPtr);
1027 
1028         case PGM_GETPOS:
1029             return PAGER_GetPos(infoPtr);
1030 
1031         case PGM_GETBUTTONSTATE:
1032             return PAGER_GetButtonState (infoPtr, (INT)lParam);
1033 
1034 /*      case PGM_GETDROPTARGET: */
1035 
1036         case PGM_RECALCSIZE:
1037             return PAGER_RecalcSize(infoPtr);
1038 
1039         case PGM_SETBKCOLOR:
1040             return PAGER_SetBkColor (infoPtr, (COLORREF)lParam);
1041 
1042         case PGM_SETBORDER:
1043             return PAGER_SetBorder (infoPtr, (INT)lParam);
1044 
1045         case PGM_SETBUTTONSIZE:
1046             return PAGER_SetButtonSize (infoPtr, (INT)lParam);
1047 
1048         case PGM_SETCHILD:
1049             return PAGER_SetChild (infoPtr, (HWND)lParam);
1050 
1051         case PGM_SETPOS:
1052             return PAGER_SetPos(infoPtr, (INT)lParam, FALSE, TRUE);
1053 
1054         case WM_CREATE:
1055             return PAGER_Create (hwnd, (LPCREATESTRUCTW)lParam);
1056 
1057         case WM_DESTROY:
1058             return PAGER_Destroy (infoPtr);
1059 
1060         case WM_SIZE:
1061             return PAGER_Size (infoPtr, (INT)wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
1062 
1063         case WM_NCPAINT:
1064             return PAGER_NCPaint (infoPtr, (HRGN)wParam);
1065 
1066         case WM_STYLECHANGED:
1067             return PAGER_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1068 
1069         case WM_NCCALCSIZE:
1070             return PAGER_NCCalcSize (infoPtr, wParam, (LPRECT)lParam);
1071 
1072         case WM_NCHITTEST:
1073             return PAGER_NCHitTest (infoPtr, (short)LOWORD(lParam), (short)HIWORD(lParam));
1074 
1075         case WM_MOUSEMOVE:
1076             if (infoPtr->bForward && infoPtr->hwndChild)
1077                 PostMessageW(infoPtr->hwndChild, WM_MOUSEMOVE, wParam, lParam);
1078             return PAGER_MouseMove (infoPtr, (INT)wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
1079 
1080         case WM_LBUTTONDOWN:
1081             return PAGER_LButtonDown (infoPtr, (INT)wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
1082 
1083         case WM_LBUTTONUP:
1084             return PAGER_LButtonUp (infoPtr, (INT)wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
1085 
1086         case WM_ERASEBKGND:
1087             return PAGER_EraseBackground (infoPtr, (HDC)wParam);
1088 
1089         case WM_TIMER:
1090             return PAGER_Timer (infoPtr, (INT)wParam);
1091 
1092         case WM_NOTIFY:
1093         case WM_COMMAND:
1094             return SendMessageW (infoPtr->hwndNotify, uMsg, wParam, lParam);
1095 
1096         default:
1097             return DefWindowProcW (hwnd, uMsg, wParam, lParam);
1098     }
1099 }
1100 
1101 
1102 VOID
1103 PAGER_Register (void)
1104 {
1105     WNDCLASSW wndClass;
1106 
1107     ZeroMemory (&wndClass, sizeof(WNDCLASSW));
1108     wndClass.style         = CS_GLOBALCLASS;
1109     wndClass.lpfnWndProc   = PAGER_WindowProc;
1110     wndClass.cbClsExtra    = 0;
1111     wndClass.cbWndExtra    = sizeof(PAGER_INFO *);
1112     wndClass.hCursor       = LoadCursorW (0, (LPWSTR)IDC_ARROW);
1113     wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
1114     wndClass.lpszClassName = WC_PAGESCROLLERW;
1115 
1116     RegisterClassW (&wndClass);
1117 }
1118 
1119 
1120 VOID
1121 PAGER_Unregister (void)
1122 {
1123     UnregisterClassW (WC_PAGESCROLLERW, NULL);
1124 }
1125