comctl32/edit: Force update on focus change.
[wine.git] / dlls / comctl32 / pager.c
blobfe68515e67354fd3b1a73fca31671c9e9d9bfc03
1 /*
2 * Pager control
4 * Copyright 1998, 1999 Eric Kohl
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.
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.
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
20 * NOTES
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.
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.
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
40 * TESTING:
41 * Tested primarily with the controlspy Pager application.
42 * Susan Farley (susan@codeweavers.com)
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>
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"
67 WINE_DEFAULT_DEBUG_CHANNEL(pager);
69 typedef struct
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;
88 #define TIMERID1 1
89 #define TIMERID2 2
90 #define INITIAL_DELAY 500
91 #define REPEAT_DELAY 50
93 static void
94 PAGER_GetButtonRects(const PAGER_INFO* infoPtr, RECT* prcTopLeft, RECT* prcBottomRight, BOOL bClientCoords)
96 RECT rcWindow;
97 GetWindowRect (infoPtr->hwndSelf, &rcWindow);
99 if (bClientCoords)
100 MapWindowPoints( 0, infoPtr->hwndSelf, (POINT *)&rcWindow, 2 );
101 else
102 OffsetRect(&rcWindow, -rcWindow.left, -rcWindow.top);
104 *prcTopLeft = *prcBottomRight = rcWindow;
105 if (infoPtr->dwStyle & PGS_HORZ)
107 prcTopLeft->right = prcTopLeft->left + infoPtr->nButtonSize;
108 prcBottomRight->left = prcBottomRight->right - infoPtr->nButtonSize;
110 else
112 prcTopLeft->bottom = prcTopLeft->top + infoPtr->nButtonSize;
113 prcBottomRight->top = prcBottomRight->bottom - infoPtr->nButtonSize;
117 static void
118 PAGER_DrawButton(HDC hdc, COLORREF clrBk, RECT rc,
119 BOOL horz, BOOL topLeft, INT btnState)
121 UINT flags;
123 TRACE("rc = %s, btnState = %d\n", wine_dbgstr_rect(&rc), btnState);
125 if (btnState == PGF_INVISIBLE)
126 return;
128 if ((rc.right - rc.left <= 0) || (rc.bottom - rc.top <= 0))
129 return;
131 if (horz)
132 flags = topLeft ? DFCS_SCROLLLEFT : DFCS_SCROLLRIGHT;
133 else
134 flags = topLeft ? DFCS_SCROLLUP : DFCS_SCROLLDOWN;
136 switch (btnState)
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;
150 DrawFrameControl( hdc, &rc, DFC_SCROLL, flags );
153 /* << PAGER_GetDropTarget >> */
155 static inline LRESULT
156 PAGER_ForwardMouse (PAGER_INFO* infoPtr, BOOL bFwd)
158 TRACE("[%p]\n", infoPtr->hwndSelf);
160 infoPtr->bForward = bFwd;
162 return 0;
165 static inline LRESULT
166 PAGER_GetButtonState (const PAGER_INFO* infoPtr, INT btn)
168 LRESULT btnState = PGF_INVISIBLE;
169 TRACE("[%p]\n", infoPtr->hwndSelf);
171 if (btn == PGB_TOPORLEFT)
172 btnState = infoPtr->TLbtnState;
173 else if (btn == PGB_BOTTOMORRIGHT)
174 btnState = infoPtr->BRbtnState;
176 return btnState;
180 static inline INT
181 PAGER_GetPos(const PAGER_INFO *infoPtr)
183 TRACE("[%p] returns %d\n", infoPtr->hwndSelf, infoPtr->nPos);
184 return infoPtr->nPos;
187 static inline INT
188 PAGER_GetButtonSize(const PAGER_INFO *infoPtr)
190 TRACE("[%p] returns %d\n", infoPtr->hwndSelf, infoPtr->nButtonSize);
191 return infoPtr->nButtonSize;
194 static inline INT
195 PAGER_GetBorder(const PAGER_INFO *infoPtr)
197 TRACE("[%p] returns %d\n", infoPtr->hwndSelf, infoPtr->nBorder);
198 return infoPtr->nBorder;
201 static inline COLORREF
202 PAGER_GetBkColor(const PAGER_INFO *infoPtr)
204 TRACE("[%p] returns %06x\n", infoPtr->hwndSelf, infoPtr->clrBk);
205 return infoPtr->clrBk;
208 static void
209 PAGER_CalcSize( PAGER_INFO *infoPtr )
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);
221 if (infoPtr->dwStyle & PGS_HORZ)
222 infoPtr->nWidth = nmpgcs.iWidth;
223 else
224 infoPtr->nHeight = nmpgcs.iHeight;
226 TRACE("[%p] PGN_CALCSIZE returns %dx%d\n", infoPtr->hwndSelf, nmpgcs.iWidth, nmpgcs.iHeight );
229 static void
230 PAGER_PositionChildWnd(PAGER_INFO* infoPtr)
232 if (infoPtr->hwndChild)
234 RECT rcClient;
235 int nPos = infoPtr->nPos;
237 /* compensate for a grayed btn, which will soon become invisible */
238 if (infoPtr->TLbtnState == PGF_GRAYED)
239 nPos += infoPtr->nButtonSize;
241 GetClientRect(infoPtr->hwndSelf, &rcClient);
243 if (infoPtr->dwStyle & PGS_HORZ)
245 int wndSize = max(0, rcClient.right - rcClient.left);
246 if (infoPtr->nWidth < wndSize)
247 infoPtr->nWidth = wndSize;
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);
256 else
258 int wndSize = max(0, rcClient.bottom - rcClient.top);
259 if (infoPtr->nHeight < wndSize)
260 infoPtr->nHeight = wndSize;
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);
270 InvalidateRect(infoPtr->hwndChild, NULL, TRUE);
274 static INT
275 PAGER_GetScrollRange(PAGER_INFO* infoPtr, BOOL calc_size)
277 INT scrollRange = 0;
279 if (infoPtr->hwndChild)
281 INT wndSize, childSize;
282 RECT wndRect;
283 GetWindowRect(infoPtr->hwndSelf, &wndRect);
285 if (calc_size)
286 PAGER_CalcSize(infoPtr);
287 if (infoPtr->dwStyle & PGS_HORZ)
289 wndSize = wndRect.right - wndRect.left;
290 childSize = infoPtr->nWidth;
292 else
294 wndSize = wndRect.bottom - wndRect.top;
295 childSize = infoPtr->nHeight;
298 TRACE("childSize = %d, wndSize = %d\n", childSize, wndSize);
299 if (childSize > wndSize)
300 scrollRange = childSize - wndSize + infoPtr->nButtonSize;
303 TRACE("[%p] returns %d\n", infoPtr->hwndSelf, scrollRange);
304 return scrollRange;
307 static void
308 PAGER_UpdateBtns(PAGER_INFO *infoPtr, INT scrollRange, BOOL hideGrayBtns)
310 BOOL resizeClient;
311 BOOL repaintBtns;
312 INT oldTLbtnState = infoPtr->TLbtnState;
313 INT oldBRbtnState = infoPtr->BRbtnState;
314 POINT pt;
315 RECT rcTopLeft, rcBottomRight;
317 /* get button rects */
318 PAGER_GetButtonRects(infoPtr, &rcTopLeft, &rcBottomRight, TRUE);
320 GetCursorPos(&pt);
321 ScreenToClient( infoPtr->hwndSelf, &pt );
323 /* update states based on scroll position */
324 if (infoPtr->nPos > 0)
326 if (infoPtr->TLbtnState == PGF_INVISIBLE || infoPtr->TLbtnState == PGF_GRAYED)
327 infoPtr->TLbtnState = PGF_NORMAL;
329 else if (!hideGrayBtns && PtInRect(&rcTopLeft, pt))
330 infoPtr->TLbtnState = PGF_GRAYED;
331 else
332 infoPtr->TLbtnState = PGF_INVISIBLE;
334 if (scrollRange <= 0)
336 infoPtr->TLbtnState = PGF_INVISIBLE;
337 infoPtr->BRbtnState = PGF_INVISIBLE;
339 else if (infoPtr->nPos < scrollRange)
341 if (infoPtr->BRbtnState == PGF_INVISIBLE || infoPtr->BRbtnState == PGF_GRAYED)
342 infoPtr->BRbtnState = PGF_NORMAL;
344 else if (!hideGrayBtns && PtInRect(&rcBottomRight, pt))
345 infoPtr->BRbtnState = PGF_GRAYED;
346 else
347 infoPtr->BRbtnState = PGF_INVISIBLE;
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);
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);
366 static LRESULT
367 PAGER_SetPos(PAGER_INFO* infoPtr, INT newPos, BOOL fromBtnPress, BOOL calc_size)
369 INT scrollRange = PAGER_GetScrollRange(infoPtr, calc_size);
370 INT oldPos = infoPtr->nPos;
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;
379 TRACE("[%p] pos=%d, oldpos=%d\n", infoPtr->hwndSelf, infoPtr->nPos, oldPos);
381 if (infoPtr->nPos != oldPos)
383 /* gray and restore btns, and if from WM_SETPOS, hide the gray btns */
384 PAGER_UpdateBtns(infoPtr, scrollRange, !fromBtnPress);
385 PAGER_PositionChildWnd(infoPtr);
388 return 0;
391 /******************************************************************
392 * For the PGM_RECALCSIZE message (but not the other uses in *
393 * this module), the native control does only the following: *
395 * if (some condition) *
396 * PostMessageW(hwnd, EM_FMTLINES, 0, 0); *
397 * return DefWindowProcW(hwnd, PGM_RECALCSIZE, 0, 0); *
399 * When we figure out what the "some condition" is we will *
400 * implement that for the message processing. *
401 ******************************************************************/
403 static LRESULT
404 PAGER_RecalcSize(PAGER_INFO *infoPtr)
406 TRACE("[%p]\n", infoPtr->hwndSelf);
408 if (infoPtr->hwndChild)
410 INT scrollRange = PAGER_GetScrollRange(infoPtr, TRUE);
412 if (scrollRange <= 0)
414 infoPtr->nPos = -1;
415 PAGER_SetPos(infoPtr, 0, FALSE, TRUE);
417 else
418 PAGER_PositionChildWnd(infoPtr);
421 return 1;
425 static COLORREF
426 PAGER_SetBkColor (PAGER_INFO* infoPtr, COLORREF clrBk)
428 COLORREF clrTemp = infoPtr->clrBk;
430 infoPtr->clrBk = clrBk;
431 TRACE("[%p] %06x\n", infoPtr->hwndSelf, infoPtr->clrBk);
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);
438 RedrawWindow(infoPtr->hwndSelf, 0, 0, RDW_ERASE | RDW_INVALIDATE);
440 return clrTemp;
444 static INT
445 PAGER_SetBorder (PAGER_INFO* infoPtr, INT iBorder)
447 INT nTemp = infoPtr->nBorder;
449 infoPtr->nBorder = iBorder;
450 TRACE("[%p] %d\n", infoPtr->hwndSelf, infoPtr->nBorder);
452 PAGER_RecalcSize(infoPtr);
454 return nTemp;
458 static INT
459 PAGER_SetButtonSize (PAGER_INFO* infoPtr, INT iButtonSize)
461 INT nTemp = infoPtr->nButtonSize;
463 infoPtr->nButtonSize = iButtonSize;
464 TRACE("[%p] %d\n", infoPtr->hwndSelf, infoPtr->nButtonSize);
466 PAGER_RecalcSize(infoPtr);
468 return nTemp;
472 static LRESULT
473 PAGER_SetChild (PAGER_INFO* infoPtr, HWND hwndChild)
475 infoPtr->hwndChild = IsWindow (hwndChild) ? hwndChild : 0;
477 if (infoPtr->hwndChild)
479 TRACE("[%p] hwndChild=%p\n", infoPtr->hwndSelf, infoPtr->hwndChild);
481 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0,
482 SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
484 infoPtr->nPos = -1;
485 PAGER_SetPos(infoPtr, 0, FALSE, FALSE);
488 return 0;
491 static void
492 PAGER_Scroll(PAGER_INFO* infoPtr, INT dir)
494 NMPGSCROLL nmpgScroll;
495 RECT rcWnd;
497 if (infoPtr->hwndChild)
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;
504 GetWindowRect(infoPtr->hwndSelf, &rcWnd);
505 GetClientRect(infoPtr->hwndSelf, &nmpgScroll.rcParent);
506 nmpgScroll.iXpos = nmpgScroll.iYpos = 0;
507 nmpgScroll.iDir = dir;
509 if (infoPtr->dwStyle & PGS_HORZ)
511 nmpgScroll.iScroll = rcWnd.right - rcWnd.left;
512 nmpgScroll.iXpos = infoPtr->nPos;
514 else
516 nmpgScroll.iScroll = rcWnd.bottom - rcWnd.top;
517 nmpgScroll.iYpos = infoPtr->nPos;
519 nmpgScroll.iScroll -= 2*infoPtr->nButtonSize;
521 SendMessageW (infoPtr->hwndNotify, WM_NOTIFY, nmpgScroll.hdr.idFrom, (LPARAM)&nmpgScroll);
523 TRACE("[%p] PGN_SCROLL returns iScroll=%d\n", infoPtr->hwndSelf, nmpgScroll.iScroll);
525 if (nmpgScroll.iScroll > 0)
527 infoPtr->direction = dir;
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);
534 else
535 infoPtr->direction = -1;
539 static LRESULT
540 PAGER_FmtLines(const PAGER_INFO *infoPtr)
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);
547 SetWindowPos(infoPtr->hwndChild, 0,
548 0,0,infoPtr->nWidth,infoPtr->nHeight,
551 return DefWindowProcW (infoPtr->hwndSelf, EM_FMTLINES, 0, 0);
554 static LRESULT
555 PAGER_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
557 PAGER_INFO *infoPtr;
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);
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;
581 if (infoPtr->dwStyle & PGS_DRAGNDROP)
582 FIXME("[%p] Drag and Drop style is not implemented yet.\n", infoPtr->hwndSelf);
584 return 0;
588 static LRESULT
589 PAGER_Destroy (PAGER_INFO *infoPtr)
591 SetWindowLongPtrW (infoPtr->hwndSelf, 0, 0);
592 heap_free (infoPtr);
593 return 0;
596 static LRESULT
597 PAGER_NCCalcSize(PAGER_INFO* infoPtr, WPARAM wParam, LPRECT lpRect)
599 RECT rcChild, rcWindow;
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.
608 DefWindowProcW (infoPtr->hwndSelf, WM_NCCALCSIZE, wParam, (LPARAM)lpRect);
610 TRACE("orig rect=%s\n", wine_dbgstr_rect(lpRect));
612 GetWindowRect (infoPtr->hwndChild, &rcChild);
613 MapWindowPoints (0, infoPtr->hwndSelf, (LPPOINT)&rcChild, 2); /* FIXME: RECT != 2 POINTS */
614 GetWindowRect (infoPtr->hwndSelf, &rcWindow);
616 infoPtr->nWidth = lpRect->right - lpRect->left;
617 infoPtr->nHeight = lpRect->bottom - lpRect->top;
618 PAGER_CalcSize( infoPtr );
620 if (infoPtr->dwStyle & PGS_HORZ)
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;
627 else
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;
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);
639 return 0;
642 static LRESULT
643 PAGER_NCPaint (const PAGER_INFO* infoPtr, HRGN hRgn)
645 RECT rcBottomRight, rcTopLeft;
646 HDC hdc;
648 if (infoPtr->dwStyle & WS_MINIMIZE)
649 return 0;
651 DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)hRgn, 0);
653 if (!(hdc = GetDCEx (infoPtr->hwndSelf, 0, DCX_USESTYLE | DCX_WINDOW)))
654 return 0;
656 PAGER_GetButtonRects(infoPtr, &rcTopLeft, &rcBottomRight, FALSE);
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);
663 ReleaseDC( infoPtr->hwndSelf, hdc );
664 return 0;
667 static INT
668 PAGER_HitTest (const PAGER_INFO* infoPtr, const POINT * pt)
670 RECT clientRect, rcTopLeft, rcBottomRight;
671 POINT ptWindow;
673 GetClientRect (infoPtr->hwndSelf, &clientRect);
675 if (PtInRect(&clientRect, *pt))
677 TRACE("child\n");
678 return -1;
681 ptWindow = *pt;
682 PAGER_GetButtonRects(infoPtr, &rcTopLeft, &rcBottomRight, TRUE);
684 if ((infoPtr->TLbtnState != PGF_INVISIBLE) && PtInRect(&rcTopLeft, ptWindow))
686 TRACE("PGB_TOPORLEFT\n");
687 return PGB_TOPORLEFT;
689 else if ((infoPtr->BRbtnState != PGF_INVISIBLE) && PtInRect(&rcBottomRight, ptWindow))
691 TRACE("PGB_BOTTOMORRIGHT\n");
692 return PGB_BOTTOMORRIGHT;
695 TRACE("nowhere\n");
696 return -1;
699 static LRESULT
700 PAGER_NCHitTest (const PAGER_INFO* infoPtr, INT x, INT y)
702 POINT pt;
703 INT nHit;
705 pt.x = x;
706 pt.y = y;
708 ScreenToClient (infoPtr->hwndSelf, &pt);
709 nHit = PAGER_HitTest(infoPtr, &pt);
711 return (nHit < 0) ? HTTRANSPARENT : HTCLIENT;
714 static LRESULT
715 PAGER_MouseMove (PAGER_INFO* infoPtr, INT keys, INT x, INT y)
717 POINT clpt, pt;
718 RECT wnrect;
719 BOOL topLeft = FALSE;
720 INT btnstate = 0;
721 INT hit;
722 HDC hdc;
724 pt.x = x;
725 pt.y = y;
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;
733 PAGER_GetButtonRects(infoPtr, &topleft, &bottomright, FALSE);
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))
740 topLeft = TRUE;
741 rect = &topleft;
742 infoPtr->TLbtnState = PGF_HOT;
743 btnstate = infoPtr->TLbtnState;
745 else if ((hit == PGB_BOTTOMORRIGHT) && (infoPtr->BRbtnState == PGF_NORMAL))
747 topLeft = FALSE;
748 rect = &bottomright;
749 infoPtr->BRbtnState = PGF_HOT;
750 btnstate = infoPtr->BRbtnState;
753 /* If in one of the buttons the capture and draw buttons */
754 if (rect)
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)
762 TRACE("[%p] SetCapture\n", infoPtr->hwndSelf);
763 SetCapture(infoPtr->hwndSelf);
764 infoPtr->bCapture = TRUE;
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;
777 /* If we think we are captured, then do release */
778 if (infoPtr->bCapture && (WindowFromPoint(pt) != infoPtr->hwndSelf))
780 NMHDR nmhdr;
782 infoPtr->bCapture = FALSE;
784 if (GetCapture() == infoPtr->hwndSelf)
786 ReleaseCapture();
788 if (infoPtr->TLbtnState == PGF_GRAYED)
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);
795 else if (infoPtr->TLbtnState == PGF_HOT)
797 infoPtr->TLbtnState = PGF_NORMAL;
798 /* FIXME: just invalidate button rect */
799 RedrawWindow(infoPtr->hwndSelf, NULL, NULL, RDW_FRAME | RDW_INVALIDATE);
802 if (infoPtr->BRbtnState == PGF_GRAYED)
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);
809 else if (infoPtr->BRbtnState == PGF_HOT)
811 infoPtr->BRbtnState = PGF_NORMAL;
812 /* FIXME: just invalidate button rect */
813 RedrawWindow(infoPtr->hwndSelf, NULL, NULL, RDW_FRAME | RDW_INVALIDATE);
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);
823 if (IsWindow(infoPtr->hwndSelf))
824 KillTimer(infoPtr->hwndSelf, TIMERID1);
826 return 0;
829 static LRESULT
830 PAGER_LButtonDown (PAGER_INFO* infoPtr, INT keys, INT x, INT y)
832 BOOL repaintBtns = FALSE;
833 POINT pt;
834 INT hit;
836 pt.x = x;
837 pt.y = y;
839 TRACE("[%p] at (%d,%d)\n", infoPtr->hwndSelf, x, y);
841 hit = PAGER_HitTest(infoPtr, &pt);
843 /* put btn in DEPRESSED state */
844 if (hit == PGB_TOPORLEFT)
846 repaintBtns = infoPtr->TLbtnState != PGF_DEPRESSED;
847 infoPtr->TLbtnState = PGF_DEPRESSED;
848 SetTimer(infoPtr->hwndSelf, TIMERID1, INITIAL_DELAY, 0);
850 else if (hit == PGB_BOTTOMORRIGHT)
852 repaintBtns = infoPtr->BRbtnState != PGF_DEPRESSED;
853 infoPtr->BRbtnState = PGF_DEPRESSED;
854 SetTimer(infoPtr->hwndSelf, TIMERID1, INITIAL_DELAY, 0);
857 if (repaintBtns)
858 SendMessageW(infoPtr->hwndSelf, WM_NCPAINT, 0, 0);
860 switch(hit)
862 case PGB_TOPORLEFT:
863 if (infoPtr->dwStyle & PGS_HORZ)
865 TRACE("[%p] PGF_SCROLLLEFT\n", infoPtr->hwndSelf);
866 PAGER_Scroll(infoPtr, PGF_SCROLLLEFT);
868 else
870 TRACE("[%p] PGF_SCROLLUP\n", infoPtr->hwndSelf);
871 PAGER_Scroll(infoPtr, PGF_SCROLLUP);
873 break;
874 case PGB_BOTTOMORRIGHT:
875 if (infoPtr->dwStyle & PGS_HORZ)
877 TRACE("[%p] PGF_SCROLLRIGHT\n", infoPtr->hwndSelf);
878 PAGER_Scroll(infoPtr, PGF_SCROLLRIGHT);
880 else
882 TRACE("[%p] PGF_SCROLLDOWN\n", infoPtr->hwndSelf);
883 PAGER_Scroll(infoPtr, PGF_SCROLLDOWN);
885 break;
886 default:
887 break;
890 return 0;
893 static LRESULT
894 PAGER_LButtonUp (PAGER_INFO* infoPtr, INT keys, INT x, INT y)
896 TRACE("[%p]\n", infoPtr->hwndSelf);
898 KillTimer (infoPtr->hwndSelf, TIMERID1);
899 KillTimer (infoPtr->hwndSelf, TIMERID2);
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;
907 return 0;
910 static LRESULT
911 PAGER_Timer (PAGER_INFO* infoPtr, INT nTimerId)
913 INT dir;
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);
933 return 0;
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);
943 return 0;
946 static LRESULT
947 PAGER_EraseBackground (const PAGER_INFO* infoPtr, HDC hdc)
949 POINT pt, ptorig;
950 HWND parent;
951 LRESULT ret;
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);
961 return ret;
965 static LRESULT
966 PAGER_Size (PAGER_INFO* infoPtr, INT type, INT x, INT y)
968 /* note that WM_SIZE is sent whenever NCCalcSize resizes the client wnd */
970 TRACE("[%p] %d,%d\n", infoPtr->hwndSelf, x, y);
972 if (infoPtr->dwStyle & PGS_HORZ)
973 infoPtr->nHeight = y;
974 else
975 infoPtr->nWidth = x;
977 return PAGER_RecalcSize(infoPtr);
981 static LRESULT
982 PAGER_StyleChanged(PAGER_INFO *infoPtr, WPARAM wStyleType, const STYLESTRUCT *lpss)
984 DWORD oldStyle = infoPtr->dwStyle;
986 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
987 wStyleType, lpss->styleOld, lpss->styleNew);
989 if (wStyleType != GWL_STYLE) return 0;
991 infoPtr->dwStyle = lpss->styleNew;
993 if ((oldStyle ^ lpss->styleNew) & (PGS_HORZ | PGS_VERT))
995 PAGER_RecalcSize(infoPtr);
998 return 0;
1001 static LRESULT WINAPI
1002 PAGER_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1004 PAGER_INFO *infoPtr = (PAGER_INFO *)GetWindowLongPtrW(hwnd, 0);
1006 TRACE("(%p, %#x, %#lx, %#lx)\n", hwnd, uMsg, wParam, lParam);
1008 if (!infoPtr && (uMsg != WM_CREATE))
1009 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
1011 switch (uMsg)
1013 case EM_FMTLINES:
1014 return PAGER_FmtLines(infoPtr);
1016 case PGM_FORWARDMOUSE:
1017 return PAGER_ForwardMouse (infoPtr, (BOOL)wParam);
1019 case PGM_GETBKCOLOR:
1020 return PAGER_GetBkColor(infoPtr);
1022 case PGM_GETBORDER:
1023 return PAGER_GetBorder(infoPtr);
1025 case PGM_GETBUTTONSIZE:
1026 return PAGER_GetButtonSize(infoPtr);
1028 case PGM_GETPOS:
1029 return PAGER_GetPos(infoPtr);
1031 case PGM_GETBUTTONSTATE:
1032 return PAGER_GetButtonState (infoPtr, (INT)lParam);
1034 /* case PGM_GETDROPTARGET: */
1036 case PGM_RECALCSIZE:
1037 return PAGER_RecalcSize(infoPtr);
1039 case PGM_SETBKCOLOR:
1040 return PAGER_SetBkColor (infoPtr, (COLORREF)lParam);
1042 case PGM_SETBORDER:
1043 return PAGER_SetBorder (infoPtr, (INT)lParam);
1045 case PGM_SETBUTTONSIZE:
1046 return PAGER_SetButtonSize (infoPtr, (INT)lParam);
1048 case PGM_SETCHILD:
1049 return PAGER_SetChild (infoPtr, (HWND)lParam);
1051 case PGM_SETPOS:
1052 return PAGER_SetPos(infoPtr, (INT)lParam, FALSE, TRUE);
1054 case WM_CREATE:
1055 return PAGER_Create (hwnd, (LPCREATESTRUCTW)lParam);
1057 case WM_DESTROY:
1058 return PAGER_Destroy (infoPtr);
1060 case WM_SIZE:
1061 return PAGER_Size (infoPtr, (INT)wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
1063 case WM_NCPAINT:
1064 return PAGER_NCPaint (infoPtr, (HRGN)wParam);
1066 case WM_STYLECHANGED:
1067 return PAGER_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1069 case WM_NCCALCSIZE:
1070 return PAGER_NCCalcSize (infoPtr, wParam, (LPRECT)lParam);
1072 case WM_NCHITTEST:
1073 return PAGER_NCHitTest (infoPtr, (short)LOWORD(lParam), (short)HIWORD(lParam));
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));
1080 case WM_LBUTTONDOWN:
1081 return PAGER_LButtonDown (infoPtr, (INT)wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
1083 case WM_LBUTTONUP:
1084 return PAGER_LButtonUp (infoPtr, (INT)wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
1086 case WM_ERASEBKGND:
1087 return PAGER_EraseBackground (infoPtr, (HDC)wParam);
1089 case WM_TIMER:
1090 return PAGER_Timer (infoPtr, (INT)wParam);
1092 case WM_NOTIFY:
1093 case WM_COMMAND:
1094 return SendMessageW (infoPtr->hwndNotify, uMsg, wParam, lParam);
1096 default:
1097 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
1102 VOID
1103 PAGER_Register (void)
1105 WNDCLASSW wndClass;
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;
1116 RegisterClassW (&wndClass);
1120 VOID
1121 PAGER_Unregister (void)
1123 UnregisterClassW (WC_PAGESCROLLERW, NULL);