4 * Copyright 1998 Eric Kohl
5 * Copyright 2000 Eric Kohl for CodeWeavers
6 * Copyright 2003 Maxime Bellenge
7 * Copyright 2006 Mikolaj Zalewski
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * - Imagelist support (completed?)
25 * - Hottrack support (completed?)
26 * - Custom draw support (completed?)
27 * - Filters support (HDS_FILTER, HDI_FILTER, HDM_*FILTER*, HDN_*FILTER*)
28 * - New Windows Vista features
37 #include "wine/unicode.h"
43 #include "imagelist.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(header);
58 INT iOrder; /* see documentation of HD_ITEM */
60 BOOL bDown; /* is item pressed? (used for drawing) */
61 RECT rect; /* bounding rectangle of the item */
62 DWORD callbackMask; /* HDI_* flags for items that are callback */
68 HWND hwndNotify; /* Owner window to send notifications to */
69 INT nNotifyFormat; /* format used for WM_NOTIFY messages */
70 UINT uNumItem; /* number of items (columns) */
71 INT nHeight; /* height of the header (pixels) */
72 HFONT hFont; /* handle to the current font */
73 HCURSOR hcurArrow; /* handle to the arrow cursor */
74 HCURSOR hcurDivider; /* handle to a cursor (used over dividers) <-|-> */
75 HCURSOR hcurDivopen; /* handle to a cursor (used over dividers) <-||-> */
76 BOOL bCaptured; /* Is the mouse captured? */
77 BOOL bPressed; /* Is a header item pressed (down)? */
78 BOOL bDragging; /* Are we dragging an item? */
79 BOOL bTracking; /* Is in tracking mode? */
80 POINT ptLButtonDown; /* The point where the left button was pressed */
81 INT iMoveItem; /* index of tracked item. (Tracking mode) */
82 INT xTrackOffset; /* distance between the right side of the tracked item and the cursor */
83 INT xOldTrack; /* track offset (see above) after the last WM_MOUSEMOVE */
84 INT iHotItem; /* index of hot item (cursor is over this item) */
85 INT iHotDivider; /* index of the hot divider (used while dragging an item or by HDM_SETHOTDIVIDER) */
86 INT iMargin; /* width of the margin that surrounds a bitmap */
88 HIMAGELIST himl; /* handle to an image list (may be 0) */
89 HEADER_ITEM *items; /* pointer to array of HEADER_ITEM's */
90 INT *order; /* array of item IDs indexed by order */
91 BOOL bRectsValid; /* validity flag for bounding rectangles */
96 #define DIVIDER_WIDTH 10
97 #define HOT_DIVIDER_WIDTH 2
98 #define MAX_HEADER_TEXT_LEN 260
99 #define HDN_UNICODE_OFFSET 20
100 #define HDN_FIRST_UNICODE (HDN_FIRST-HDN_UNICODE_OFFSET)
102 #define HDI_SUPPORTED_FIELDS (HDI_WIDTH|HDI_TEXT|HDI_FORMAT|HDI_LPARAM|HDI_BITMAP|HDI_IMAGE|HDI_ORDER)
103 #define HDI_UNSUPPORTED_FIELDS (HDI_FILTER)
104 #define HDI_UNKNOWN_FIELDS (~(HDI_SUPPORTED_FIELDS|HDI_UNSUPPORTED_FIELDS|HDI_DI_SETITEM))
105 #define HDI_COMCTL32_4_0_FIELDS (HDI_WIDTH|HDI_TEXT|HDI_FORMAT|HDI_LPARAM|HDI_BITMAP)
107 #define HEADER_GetInfoPtr(hwnd) ((HEADER_INFO *)GetWindowLongPtrW(hwnd,0))
109 static BOOL HEADER_PrepareCallbackItems(HWND hwnd, INT iItem, INT reqMask);
110 static void HEADER_FreeCallbackItems(HEADER_ITEM *lpItem);
112 static const WCHAR themeClass[] = {'H','e','a','d','e','r',0};
113 static WCHAR emptyString[] = {0};
115 static void HEADER_DisposeItem(HEADER_ITEM *lpItem)
119 Free(lpItem->pszText);
123 static void HEADER_StoreHDItemInHeader(HEADER_ITEM *lpItem, UINT mask, HDITEMW *phdi, BOOL fUnicode)
125 if (mask & HDI_UNSUPPORTED_FIELDS)
126 FIXME("unsupported header fields %x\n", (mask & HDI_UNSUPPORTED_FIELDS));
128 if (mask & HDI_BITMAP)
129 lpItem->hbm = phdi->hbm;
131 if (mask & HDI_FORMAT)
132 lpItem->fmt = phdi->fmt;
134 if (mask & HDI_LPARAM)
135 lpItem->lParam = phdi->lParam;
137 if (mask & HDI_WIDTH)
138 lpItem->cxy = phdi->cxy;
140 if (mask & HDI_IMAGE)
142 lpItem->iImage = phdi->iImage;
143 if (phdi->iImage == I_IMAGECALLBACK)
144 lpItem->callbackMask |= HDI_IMAGE;
146 lpItem->callbackMask &= ~HDI_IMAGE;
153 Free(lpItem->pszText);
154 lpItem->pszText = NULL;
157 if (phdi->pszText != LPSTR_TEXTCALLBACKW) /* covers != TEXTCALLBACKA too */
159 LPWSTR pszText = (phdi->pszText != NULL ? phdi->pszText : emptyString);
161 Str_SetPtrW(&lpItem->pszText, pszText);
163 Str_SetPtrAtoW(&lpItem->pszText, (LPSTR)pszText);
164 lpItem->callbackMask &= ~HDI_TEXT;
168 lpItem->pszText = NULL;
169 lpItem->callbackMask |= HDI_TEXT;
174 inline static LRESULT
175 HEADER_IndexToOrder (HWND hwnd, INT iItem)
177 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
178 HEADER_ITEM *lpItem = &infoPtr->items[iItem];
179 return lpItem->iOrder;
184 HEADER_OrderToIndex(HWND hwnd, WPARAM wParam)
186 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
187 INT iorder = (INT)wParam;
189 if ((iorder <0) || iorder >= infoPtr->uNumItem)
191 return infoPtr->order[iorder];
195 HEADER_ChangeItemOrder(HEADER_INFO *infoPtr, INT iItem, INT iNewOrder)
197 HEADER_ITEM *lpItem = &infoPtr->items[iItem];
200 TRACE("%d: %d->%d\n", iItem, lpItem->iOrder, iNewOrder);
201 if (lpItem->iOrder < iNewOrder)
203 memmove(&infoPtr->order[lpItem->iOrder],
204 &infoPtr->order[lpItem->iOrder + 1],
205 (iNewOrder - lpItem->iOrder) * sizeof(INT));
207 if (iNewOrder < lpItem->iOrder)
209 memmove(&infoPtr->order[iNewOrder + 1],
210 &infoPtr->order[iNewOrder],
211 (lpItem->iOrder - iNewOrder) * sizeof(INT));
213 infoPtr->order[iNewOrder] = iItem;
214 nMin = min(lpItem->iOrder, iNewOrder);
215 nMax = max(lpItem->iOrder, iNewOrder);
216 for (i = nMin; i <= nMax; i++)
217 infoPtr->items[infoPtr->order[i]].iOrder = i;
220 /* Note: if iItem is the last item then this function returns infoPtr->uNumItem */
222 HEADER_NextItem(HWND hwnd, INT iItem)
224 return HEADER_OrderToIndex(hwnd, HEADER_IndexToOrder(hwnd, iItem)+1);
228 HEADER_PrevItem(HWND hwnd, INT iItem)
230 return HEADER_OrderToIndex(hwnd, HEADER_IndexToOrder(hwnd, iItem)-1);
234 HEADER_SetItemBounds (HWND hwnd)
236 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
242 infoPtr->bRectsValid = TRUE;
244 if (infoPtr->uNumItem == 0)
247 GetClientRect (hwnd, &rect);
250 for (i = 0; i < infoPtr->uNumItem; i++) {
251 phdi = &infoPtr->items[HEADER_OrderToIndex(hwnd,i)];
252 phdi->rect.top = rect.top;
253 phdi->rect.bottom = rect.bottom;
255 phdi->rect.right = phdi->rect.left + ((phdi->cxy>0)?phdi->cxy:0);
256 x = phdi->rect.right;
261 HEADER_Size (HWND hwnd, WPARAM wParam)
263 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
265 infoPtr->bRectsValid = FALSE;
270 static void HEADER_GetHotDividerRect(HWND hwnd, HEADER_INFO *infoPtr, RECT *r)
272 INT iDivider = infoPtr->iHotDivider;
273 if (infoPtr->uNumItem > 0)
277 if (iDivider < infoPtr->uNumItem)
279 lpItem = &infoPtr->items[iDivider];
280 r->left = lpItem->rect.left - HOT_DIVIDER_WIDTH/2;
281 r->right = lpItem->rect.left + HOT_DIVIDER_WIDTH/2;
285 lpItem = &infoPtr->items[HEADER_OrderToIndex(hwnd, infoPtr->uNumItem-1)];
286 r->left = lpItem->rect.right - HOT_DIVIDER_WIDTH/2;
287 r->right = lpItem->rect.right + HOT_DIVIDER_WIDTH/2;
289 r->top = lpItem->rect.top;
290 r->bottom = lpItem->rect.bottom;
295 GetClientRect(hwnd, &clientRect);
297 r->right = r->left + HOT_DIVIDER_WIDTH/2;
303 HEADER_DrawItem (HWND hwnd, HDC hdc, INT iItem, BOOL bHotTrack)
305 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
306 HEADER_ITEM *phdi = &infoPtr->items[iItem];
308 INT oldBkMode, cxEdge = GetSystemMetrics(SM_CXEDGE);
309 HTHEME theme = GetWindowTheme (hwnd);
312 TRACE("DrawItem(iItem %d bHotTrack %d unicode flag %d)\n", iItem, bHotTrack, (infoPtr->nNotifyFormat == NFR_UNICODE));
314 if (!infoPtr->bRectsValid)
315 HEADER_SetItemBounds(hwnd);
318 if (r.right - r.left == 0)
319 return phdi->rect.right;
322 int state = (phdi->bDown) ? HIS_PRESSED :
323 (bHotTrack ? HIS_HOT : HIS_NORMAL);
324 DrawThemeBackground (theme, hdc, HP_HEADERITEM, state,
326 GetThemeBackgroundContentRect (theme, hdc, HP_HEADERITEM, state,
330 if (GetWindowLongW (hwnd, GWL_STYLE) & HDS_BUTTONS) {
332 DrawEdge (hdc, &r, BDR_RAISEDOUTER,
333 BF_RECT | BF_FLAT | BF_MIDDLE | BF_ADJUST);
336 DrawEdge (hdc, &r, EDGE_RAISED,
337 BF_RECT | BF_SOFT | BF_MIDDLE | BF_ADJUST);
340 DrawEdge (hdc, &r, EDGE_ETCHED, BF_BOTTOM | BF_RIGHT | BF_ADJUST);
350 /* Set the colors before sending NM_CUSTOMDRAW so that it can change them */
351 SetTextColor (hdc, (bHotTrack && !theme) ? COLOR_HIGHLIGHT : COLOR_BTNTEXT);
352 SetBkColor(hdc, GetSysColor(COLOR_3DFACE));
354 nmcd.hdr.hwndFrom = hwnd;
355 nmcd.hdr.idFrom = GetWindowLongPtrW (hwnd, GWLP_ID);
356 nmcd.hdr.code = NM_CUSTOMDRAW;
357 nmcd.dwDrawStage = CDDS_PREPAINT | CDDS_ITEM | CDDS_ITEMPOSTERASE;
359 nmcd.dwItemSpec = iItem;
361 nmcd.uItemState = phdi->bDown ? CDIS_SELECTED : 0;
362 nmcd.lItemlParam = phdi->lParam;
364 SendMessageW (infoPtr->hwndNotify, WM_NOTIFY, nmcd.hdr.idFrom, (LPARAM)&nmcd);
366 if (phdi->fmt & HDF_OWNERDRAW) {
369 dis.CtlType = ODT_HEADER;
370 dis.CtlID = GetWindowLongPtrW (hwnd, GWLP_ID);
372 dis.itemAction = ODA_DRAWENTIRE;
373 dis.itemState = phdi->bDown ? ODS_SELECTED : 0;
377 dis.itemData = phdi->lParam;
378 oldBkMode = SetBkMode(hdc, TRANSPARENT);
379 SendMessageW (infoPtr->hwndNotify, WM_DRAWITEM,
380 (WPARAM)dis.CtlID, (LPARAM)&dis);
381 if (oldBkMode != TRANSPARENT)
382 SetBkMode(hdc, oldBkMode);
385 UINT rw, rh, /* width and height of r */
386 *x = NULL, *w = NULL; /* x and width of the pic (bmp or img) which is part of cnt */
387 /* cnt,txt,img,bmp */
392 HEADER_PrepareCallbackItems(hwnd, iItem, HDI_TEXT|HDI_IMAGE);
393 cw = tw = iw = bw = 0;
394 rw = r.right - r.left;
395 rh = r.bottom - r.top;
398 HBRUSH hbr = CreateSolidBrush(GetBkColor(hdc));
399 RECT rcBackground = r;
401 rcBackground.right -= cxEdge;
402 rcBackground.left += cxEdge;
403 FillRect(hdc, &rcBackground, hbr);
406 if (phdi->fmt & HDF_STRING) {
409 DrawTextW (hdc, phdi->pszText, -1,
410 &textRect, DT_LEFT|DT_VCENTER|DT_SINGLELINE|DT_CALCRECT);
411 cw = textRect.right - textRect.left + 2 * infoPtr->iMargin;
414 if ((phdi->fmt & HDF_IMAGE) && (infoPtr->himl)) {
415 iw = infoPtr->himl->cx + 2 * infoPtr->iMargin;
420 if ((phdi->fmt & HDF_BITMAP) && (phdi->hbm)) {
421 GetObjectW (phdi->hbm, sizeof(BITMAP), (LPVOID)&bmp);
422 bw = bmp.bmWidth + 2 * infoPtr->iMargin;
432 /* align cx using the unclipped cw */
433 if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_LEFT)
435 else if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_CENTER)
436 cx = r.left + rw / 2 - cw / 2;
443 if (cx + cw > r.right)
446 tx = cx + infoPtr->iMargin;
447 /* since cw might have changed we have to recalculate tw */
448 tw = cw - infoPtr->iMargin * 2;
452 if (phdi->fmt & HDF_BITMAP_ON_RIGHT) {
453 /* put pic behind text */
454 *x = cx + tw + infoPtr->iMargin * 3;
456 *x = cx + infoPtr->iMargin;
457 /* move text behind pic */
463 /* since we're done with the layout we can
464 now calculate the position of bmp which
465 has no influence on alignment and layout
467 if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_RIGHT)
468 bx = cx - bw + infoPtr->iMargin;
470 bx = cx + cw + infoPtr->iMargin;
474 HDC hClipDC = GetDC(hwnd);
475 HRGN hClipRgn = CreateRectRgn(r.left, r.top, r.right, r.bottom);
476 SelectClipRgn(hClipDC, hClipRgn);
479 HDC hdcBitmap = CreateCompatibleDC (hClipDC);
480 SelectObject (hdcBitmap, phdi->hbm);
481 BitBlt (hClipDC, bx, r.top + ((INT)rh - bmp.bmHeight) / 2,
482 bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
483 DeleteDC (hdcBitmap);
487 ImageList_DrawEx (infoPtr->himl, phdi->iImage, hClipDC,
488 ix, r.top + ((INT)rh - infoPtr->himl->cy) / 2,
489 infoPtr->himl->cx, infoPtr->himl->cy, CLR_DEFAULT, CLR_DEFAULT, 0);
492 DeleteObject(hClipRgn);
493 ReleaseDC(hwnd, hClipDC);
496 if (((phdi->fmt & HDF_STRING)
497 || (!(phdi->fmt & (HDF_OWNERDRAW|HDF_STRING|HDF_BITMAP|
498 HDF_BITMAP_ON_RIGHT|HDF_IMAGE)))) /* no explicit format specified? */
499 && (phdi->pszText)) {
500 oldBkMode = SetBkMode(hdc, TRANSPARENT);
503 DrawTextW (hdc, phdi->pszText, -1,
504 &r, DT_LEFT|DT_END_ELLIPSIS|DT_VCENTER|DT_SINGLELINE);
505 if (oldBkMode != TRANSPARENT)
506 SetBkMode(hdc, oldBkMode);
508 HEADER_FreeCallbackItems(phdi);
511 return phdi->rect.right;
515 HEADER_DrawHotDivider(HWND hwnd, HDC hdc)
517 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
521 HEADER_GetHotDividerRect(hwnd, infoPtr, &r);
522 brush = CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT));
523 FillRect(hdc, &r, brush);
528 HEADER_Refresh (HWND hwnd, HDC hdc)
530 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
531 HFONT hFont, hOldFont;
536 HTHEME theme = GetWindowTheme (hwnd);
538 if (!infoPtr->bRectsValid)
539 HEADER_SetItemBounds(hwnd);
541 /* get rect for the bar, adjusted for the border */
542 GetClientRect (hwnd, &rect);
544 if (infoPtr->bDragging)
545 ImageList_DragShowNolock(FALSE);
547 hFont = infoPtr->hFont ? infoPtr->hFont : GetStockObject (SYSTEM_FONT);
548 hOldFont = SelectObject (hdc, hFont);
550 /* draw Background */
551 if (infoPtr->uNumItem == 0 && theme == NULL) {
552 hbrBk = GetSysColorBrush(COLOR_3DFACE);
553 FillRect(hdc, &rect, hbrBk);
557 for (i = 0; x <= rect.right && i < infoPtr->uNumItem; i++) {
558 int idx = HEADER_OrderToIndex(hwnd,i);
559 if (RectVisible(hdc, &infoPtr->items[idx].rect))
560 HEADER_DrawItem (hwnd, hdc, idx, infoPtr->iHotItem == i);
561 x = infoPtr->items[idx].rect.right;
564 if ((x <= rect.right) && (infoPtr->uNumItem > 0)) {
567 DrawThemeBackground (theme, hdc, HP_HEADERITEM, HIS_NORMAL, &rect,
571 if (GetWindowLongW (hwnd, GWL_STYLE) & HDS_BUTTONS)
572 DrawEdge (hdc, &rect, EDGE_RAISED, BF_TOP|BF_LEFT|BF_BOTTOM|BF_SOFT|BF_MIDDLE);
574 DrawEdge (hdc, &rect, EDGE_ETCHED, BF_BOTTOM|BF_MIDDLE);
578 if (infoPtr->iHotDivider != -1)
579 HEADER_DrawHotDivider(hwnd, hdc);
581 if (infoPtr->bDragging)
582 ImageList_DragShowNolock(TRUE);
583 SelectObject (hdc, hOldFont);
588 HEADER_RefreshItem (HWND hwnd, HDC hdc, INT iItem)
590 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
591 HFONT hFont, hOldFont;
593 hFont = infoPtr->hFont ? infoPtr->hFont : GetStockObject (SYSTEM_FONT);
594 hOldFont = SelectObject (hdc, hFont);
595 HEADER_DrawItem (hwnd, hdc, iItem, infoPtr->iHotItem == iItem);
596 SelectObject (hdc, hOldFont);
601 HEADER_InternalHitTest (HWND hwnd, LPPOINT lpPt, UINT *pFlags, INT *pItem)
603 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
609 GetClientRect (hwnd, &rect);
613 if (PtInRect (&rect, *lpPt))
615 if (infoPtr->uNumItem == 0) {
616 *pFlags |= HHT_NOWHERE;
622 /* somewhere inside */
623 for (iCount = 0; iCount < infoPtr->uNumItem; iCount++) {
624 rect = infoPtr->items[iCount].rect;
625 width = rect.right - rect.left;
630 if (PtInRect (&rect, *lpPt)) {
631 if (width <= 2 * DIVIDER_WIDTH) {
632 *pFlags |= HHT_ONHEADER;
634 TRACE("ON HEADER %d\n", iCount);
637 if (HEADER_IndexToOrder(hwnd, iCount) > 0) {
639 rcTest.right = rcTest.left + DIVIDER_WIDTH;
640 if (PtInRect (&rcTest, *lpPt)) {
642 *pFlags |= HHT_ONDIVOPEN;
643 *pItem = HEADER_PrevItem(hwnd, iCount);
644 TRACE("ON DIVOPEN %d\n", *pItem);
648 *pFlags |= HHT_ONDIVIDER;
649 *pItem = HEADER_PrevItem(hwnd, iCount);
650 TRACE("ON DIVIDER %d\n", *pItem);
656 rcTest.left = rcTest.right - DIVIDER_WIDTH;
657 if (PtInRect (&rcTest, *lpPt)) {
658 *pFlags |= HHT_ONDIVIDER;
660 TRACE("ON DIVIDER %d\n", *pItem);
664 *pFlags |= HHT_ONHEADER;
666 TRACE("ON HEADER %d\n", iCount);
671 /* check for last divider part (on nowhere) */
672 rect = infoPtr->items[infoPtr->uNumItem-1].rect;
673 rect.left = rect.right;
674 rect.right += DIVIDER_WIDTH;
675 if (PtInRect (&rect, *lpPt)) {
677 *pFlags |= HHT_ONDIVOPEN;
678 *pItem = infoPtr->uNumItem - 1;
679 TRACE("ON DIVOPEN %d\n", *pItem);
683 *pFlags |= HHT_ONDIVIDER;
684 *pItem = infoPtr->uNumItem-1;
685 TRACE("ON DIVIDER %d\n", *pItem);
690 *pFlags |= HHT_NOWHERE;
697 if (lpPt->x < rect.left) {
699 *pFlags |= HHT_TOLEFT;
701 else if (lpPt->x > rect.right) {
703 *pFlags |= HHT_TORIGHT;
706 if (lpPt->y < rect.top) {
708 *pFlags |= HHT_ABOVE;
710 else if (lpPt->y > rect.bottom) {
712 *pFlags |= HHT_BELOW;
717 TRACE("flags=0x%X\n", *pFlags);
723 HEADER_DrawTrackLine (HWND hwnd, HDC hdc, INT x)
729 GetClientRect (hwnd, &rect);
731 hOldPen = SelectObject (hdc, GetStockObject (BLACK_PEN));
732 oldRop = SetROP2 (hdc, R2_XORPEN);
733 MoveToEx (hdc, x, rect.top, NULL);
734 LineTo (hdc, x, rect.bottom);
735 SetROP2 (hdc, oldRop);
736 SelectObject (hdc, hOldPen);
741 * Convert a HDITEM into the correct format (ANSI/Unicode) to send it in a notify
744 * [I] infoPtr : the header that wants to send the notify
745 * [O] dest : The buffer to store the HDITEM for notify. It may be set to a HDITEMA of HDITEMW
746 * [I] src : The source HDITEM. It may be a HDITEMA or HDITEMW
747 * [I] fSourceUnicode : is src a HDITEMW or HDITEMA
748 * [O] ppvScratch : a pointer to a scratch buffer that needs to be freed after
749 * the HDITEM is no longer in use or NULL if none was needed
751 * NOTE: We depend on HDITEMA and HDITEMW having the same structure
753 static void HEADER_CopyHDItemForNotify(HEADER_INFO *infoPtr, HDITEMW *dest,
754 HDITEMW *src, BOOL fSourceUnicode, LPVOID *ppvScratch)
759 if (src->mask & HDI_TEXT && src->pszText != LPSTR_TEXTCALLBACKW) /* covers TEXTCALLBACKA as well */
761 if (fSourceUnicode && infoPtr->nNotifyFormat != NFR_UNICODE)
763 dest->pszText = NULL;
764 Str_SetPtrWtoA((LPSTR *)&dest->pszText, src->pszText);
765 *ppvScratch = dest->pszText;
768 if (!fSourceUnicode && infoPtr->nNotifyFormat == NFR_UNICODE)
770 dest->pszText = NULL;
771 Str_SetPtrAtoW(&dest->pszText, (LPSTR)src->pszText);
772 *ppvScratch = dest->pszText;
777 static UINT HEADER_NotifyCodeWtoA(UINT code)
779 /* we use the fact that all the unicode messages are in HDN_FIRST_UNICODE..HDN_LAST*/
780 if (code >= HDN_LAST && code <= HDN_FIRST_UNICODE)
781 return code + HDN_UNICODE_OFFSET;
787 HEADER_SendSimpleNotify (HWND hwnd, UINT code)
789 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
792 nmhdr.hwndFrom = hwnd;
793 nmhdr.idFrom = GetWindowLongPtrW (hwnd, GWLP_ID);
796 return (BOOL)SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
797 (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
801 HEADER_SendNotifyWithHDItemT(HWND hwnd, UINT code, INT iItem, HDITEMW *lpItem)
803 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
806 nmhdr.hdr.hwndFrom = hwnd;
807 nmhdr.hdr.idFrom = GetWindowLongPtrW (hwnd, GWLP_ID);
808 nmhdr.hdr.code = (infoPtr->nNotifyFormat == NFR_UNICODE ? code : HEADER_NotifyCodeWtoA(code));
811 nmhdr.pitem = lpItem;
813 return (BOOL)SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
814 (WPARAM)nmhdr.hdr.idFrom, (LPARAM)&nmhdr);
818 HEADER_SendNotifyWithIntFieldT(HWND hwnd, UINT code, INT iItem, INT mask, INT iValue)
820 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
823 /* copying only the iValue should be ok but to make the code more robust we copy everything */
824 nmitem.cxy = infoPtr->items[iItem].cxy;
825 nmitem.hbm = infoPtr->items[iItem].hbm;
826 nmitem.pszText = NULL;
827 nmitem.cchTextMax = 0;
828 nmitem.fmt = infoPtr->items[iItem].fmt;
829 nmitem.lParam = infoPtr->items[iItem].lParam;
830 nmitem.iOrder = infoPtr->items[iItem].iOrder;
831 nmitem.iImage = infoPtr->items[iItem].iImage;
840 nmitem.iOrder = iValue;
843 ERR("invalid mask value 0x%x\n", iValue);
846 return HEADER_SendNotifyWithHDItemT(hwnd, code, iItem, &nmitem);
850 * Prepare callback items
851 * depends on NMHDDISPINFOW having same structure as NMHDDISPINFOA
852 * (so we handle the two cases only doing a specific cast for pszText).
853 * Checks if any of the required field are callback. If there are sends a
854 * NMHDISPINFO notify to retrieve these items. The items are stored in the
855 * HEADER_ITEM pszText and iImage fields. They should be freed with
856 * HEADER_FreeCallbackItems.
858 * @param hwnd : hwnd header container handler
859 * @param iItem : the header item id
860 * @param reqMask : required fields. If any of them is callback this function will fetch it
862 * @return TRUE on success, else FALSE
865 HEADER_PrepareCallbackItems(HWND hwnd, INT iItem, INT reqMask)
867 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
868 HEADER_ITEM *lpItem = &infoPtr->items[iItem];
869 DWORD mask = reqMask & lpItem->callbackMask;
870 NMHDDISPINFOW dispInfo;
871 void *pvBuffer = NULL;
875 if (mask&HDI_TEXT && lpItem->pszText != NULL)
877 ERR("(): function called without a call to FreeCallbackItems\n");
878 Free(lpItem->pszText);
879 lpItem->pszText = NULL;
882 memset(&dispInfo, 0, sizeof(NMHDDISPINFOW));
883 dispInfo.hdr.hwndFrom = hwnd;
884 dispInfo.hdr.idFrom = GetWindowLongPtrW (hwnd, GWLP_ID);
885 if (infoPtr->nNotifyFormat == NFR_UNICODE)
887 dispInfo.hdr.code = HDN_GETDISPINFOW;
889 pvBuffer = Alloc(MAX_HEADER_TEXT_LEN * sizeof(WCHAR));
893 dispInfo.hdr.code = HDN_GETDISPINFOA;
895 pvBuffer = Alloc(MAX_HEADER_TEXT_LEN * sizeof(CHAR));
897 dispInfo.pszText = (LPWSTR)pvBuffer;
898 dispInfo.cchTextMax = (pvBuffer!=NULL?MAX_HEADER_TEXT_LEN:0);
899 dispInfo.iItem = iItem;
900 dispInfo.mask = mask;
901 dispInfo.lParam = lpItem->lParam;
903 TRACE("Sending HDN_GETDISPINFO%c\n", infoPtr->nNotifyFormat == NFR_UNICODE?'W':'A');
904 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
905 (WPARAM) dispInfo.hdr.idFrom,
908 TRACE("SendMessage returns(mask:0x%x,str:%s,lParam:%p)\n",
910 (infoPtr->nNotifyFormat == NFR_UNICODE ? debugstr_w(dispInfo.pszText) : (LPSTR) dispInfo.pszText),
911 (void*) dispInfo.lParam);
913 if (mask & HDI_IMAGE)
914 lpItem->iImage = dispInfo.iImage;
917 if (infoPtr->nNotifyFormat == NFR_UNICODE)
919 lpItem->pszText = (LPWSTR)pvBuffer;
921 /* the user might have used his own buffer */
922 if (dispInfo.pszText != lpItem->pszText)
923 Str_GetPtrW(dispInfo.pszText, lpItem->pszText, MAX_HEADER_TEXT_LEN);
927 Str_SetPtrAtoW(&lpItem->pszText, (LPSTR)dispInfo.pszText);
932 if (dispInfo.mask & HDI_DI_SETITEM)
934 /* make the items permanent */
935 lpItem->callbackMask &= ~dispInfo.mask;
943 * Free the items that might be allocated with HEADER_PrepareCallbackItems
946 * [I] lpItem : the item to free the data
950 HEADER_FreeCallbackItems(HEADER_ITEM *lpItem)
952 if (lpItem->callbackMask&HDI_TEXT && lpItem->pszText != NULL)
954 Free(lpItem->pszText);
955 lpItem->pszText = NULL;
958 if (lpItem->callbackMask&HDI_IMAGE)
959 lpItem->iImage = I_IMAGECALLBACK;
963 HEADER_CreateDragImage (HWND hwnd, WPARAM wParam)
965 HEADER_INFO *infoPtr = HEADER_GetInfoPtr(hwnd);
968 HBITMAP hMemory, hOldBitmap;
973 if (wParam < 0 || wParam >= infoPtr->uNumItem)
975 lpItem = &infoPtr->items[wParam];
976 width = lpItem->rect.right - lpItem->rect.left;
977 height = lpItem->rect.bottom - lpItem->rect.top;
979 hDeviceDC = GetDC(NULL);
980 hMemoryDC = CreateCompatibleDC(hDeviceDC);
981 hMemory = CreateCompatibleBitmap(hDeviceDC, width, height);
982 ReleaseDC(NULL, hDeviceDC);
983 hOldBitmap = SelectObject(hMemoryDC, hMemory);
984 SetViewportOrgEx(hMemoryDC, -lpItem->rect.left, -lpItem->rect.top, NULL);
985 HEADER_DrawItem(hwnd, hMemoryDC, wParam, FALSE);
986 hMemory = SelectObject(hMemoryDC, hOldBitmap);
989 if (hMemory == NULL) /* if anything failed */
992 himl = ImageList_Create(width, height, ILC_COLORDDB, 1, 1);
993 ImageList_Add(himl, hMemory, NULL);
994 DeleteObject(hMemory);
995 return (LRESULT)himl;
999 HEADER_SetHotDivider(HWND hwnd, WPARAM wParam, LPARAM lParam)
1001 HEADER_INFO *infoPtr = HEADER_GetInfoPtr(hwnd);
1009 pt.x = (INT)(SHORT)LOWORD(lParam);
1011 HEADER_InternalHitTest (hwnd, &pt, &flags, &iDivider);
1013 if (flags & HHT_TOLEFT)
1015 else if (flags & HHT_NOWHERE || flags & HHT_TORIGHT)
1016 iDivider = infoPtr->uNumItem;
1019 HEADER_ITEM *lpItem = &infoPtr->items[iDivider];
1020 if (pt.x > (lpItem->rect.left+lpItem->rect.right)/2)
1021 iDivider = HEADER_NextItem(hwnd, iDivider);
1025 iDivider = (INT)lParam;
1027 /* Note; wParam==FALSE, lParam==-1 is valid and is used to clear the hot divider */
1028 if (iDivider<-1 || iDivider>(int)infoPtr->uNumItem)
1031 if (iDivider != infoPtr->iHotDivider)
1033 if (infoPtr->iHotDivider != -1)
1035 HEADER_GetHotDividerRect(hwnd, infoPtr, &r);
1036 InvalidateRect(hwnd, &r, FALSE);
1038 infoPtr->iHotDivider = iDivider;
1041 HEADER_GetHotDividerRect(hwnd, infoPtr, &r);
1042 InvalidateRect(hwnd, &r, FALSE);
1049 HEADER_DeleteItem (HWND hwnd, WPARAM wParam)
1051 HEADER_INFO *infoPtr = HEADER_GetInfoPtr(hwnd);
1052 INT iItem = (INT)wParam;
1056 TRACE("[iItem=%d]\n", iItem);
1058 if ((iItem < 0) || (iItem >= (INT)infoPtr->uNumItem))
1061 for (i = 0; i < infoPtr->uNumItem; i++)
1062 TRACE("%d: order=%d, iOrder=%d, ->iOrder=%d\n", i, infoPtr->order[i], infoPtr->items[i].iOrder, infoPtr->items[infoPtr->order[i]].iOrder);
1064 iOrder = infoPtr->items[iItem].iOrder;
1065 HEADER_DisposeItem(&infoPtr->items[iItem]);
1067 infoPtr->uNumItem--;
1068 memmove(&infoPtr->items[iItem], &infoPtr->items[iItem + 1],
1069 (infoPtr->uNumItem - iItem) * sizeof(HEADER_ITEM));
1070 memmove(&infoPtr->order[iOrder], &infoPtr->order[iOrder + 1],
1071 (infoPtr->uNumItem - iOrder) * sizeof(INT));
1072 infoPtr->items = ReAlloc(infoPtr->items, sizeof(HEADER_ITEM) * infoPtr->uNumItem);
1073 infoPtr->order = ReAlloc(infoPtr->order, sizeof(INT) * infoPtr->uNumItem);
1075 /* Correct the orders */
1076 for (i = 0; i < infoPtr->uNumItem; i++)
1078 if (infoPtr->order[i] > iItem)
1079 infoPtr->order[i]--;
1081 infoPtr->items[infoPtr->order[i]].iOrder = i;
1083 for (i = 0; i < infoPtr->uNumItem; i++)
1084 TRACE("%d: order=%d, iOrder=%d, ->iOrder=%d\n", i, infoPtr->order[i], infoPtr->items[i].iOrder, infoPtr->items[infoPtr->order[i]].iOrder);
1086 HEADER_SetItemBounds (hwnd);
1087 InvalidateRect(hwnd, NULL, FALSE);
1094 HEADER_GetImageList (HWND hwnd)
1096 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1098 return (LRESULT)infoPtr->himl;
1103 HEADER_GetItemT (HWND hwnd, INT nItem, LPHDITEMW phdi, BOOL bUnicode)
1105 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1106 HEADER_ITEM *lpItem;
1112 TRACE("[nItem=%d]\n", nItem);
1118 if ((nItem < 0) || (nItem >= (INT)infoPtr->uNumItem))
1121 if (mask & HDI_UNKNOWN_FIELDS)
1123 TRACE("mask %x contains unknown fields. Using only comctl32 4.0 fields\n", mask);
1124 mask &= HDI_COMCTL32_4_0_FIELDS;
1127 lpItem = &infoPtr->items[nItem];
1128 HEADER_PrepareCallbackItems(hwnd, nItem, mask);
1130 if (mask & HDI_BITMAP)
1131 phdi->hbm = lpItem->hbm;
1133 if (mask & HDI_FORMAT)
1134 phdi->fmt = lpItem->fmt;
1136 if (mask & HDI_WIDTH)
1137 phdi->cxy = lpItem->cxy;
1139 if (mask & HDI_LPARAM)
1140 phdi->lParam = lpItem->lParam;
1142 if (mask & HDI_IMAGE)
1143 phdi->iImage = lpItem->iImage;
1145 if (mask & HDI_ORDER)
1146 phdi->iOrder = lpItem->iOrder;
1148 if (mask & HDI_TEXT)
1151 Str_GetPtrW (lpItem->pszText, phdi->pszText, phdi->cchTextMax);
1153 Str_GetPtrWtoA (lpItem->pszText, (LPSTR)phdi->pszText, phdi->cchTextMax);
1156 HEADER_FreeCallbackItems(lpItem);
1161 inline static LRESULT
1162 HEADER_GetItemCount (HWND hwnd)
1164 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1165 return infoPtr->uNumItem;
1170 HEADER_GetItemRect (HWND hwnd, WPARAM wParam, LPARAM lParam)
1172 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1173 INT iItem = (INT)wParam;
1174 LPRECT lpRect = (LPRECT)lParam;
1176 if ((iItem < 0) || (iItem >= (INT)infoPtr->uNumItem))
1179 lpRect->left = infoPtr->items[iItem].rect.left;
1180 lpRect->right = infoPtr->items[iItem].rect.right;
1181 lpRect->top = infoPtr->items[iItem].rect.top;
1182 lpRect->bottom = infoPtr->items[iItem].rect.bottom;
1189 HEADER_GetOrderArray(HWND hwnd, WPARAM wParam, LPARAM lParam)
1191 LPINT order = (LPINT) lParam;
1192 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1194 if ((unsigned int)wParam <infoPtr->uNumItem)
1197 memcpy(order, infoPtr->order, infoPtr->uNumItem * sizeof(INT));
1202 HEADER_SetOrderArray(HWND hwnd, WPARAM wParam, LPARAM lParam)
1205 LPINT order = (LPINT) lParam;
1206 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1207 HEADER_ITEM *lpItem;
1209 if ((unsigned int)wParam <infoPtr->uNumItem)
1211 memcpy(infoPtr->order, order, infoPtr->uNumItem * sizeof(INT));
1212 for (i=0; i<(int)wParam; i++)
1214 lpItem = &infoPtr->items[*order++];
1217 infoPtr->bRectsValid=0;
1218 InvalidateRect(hwnd, NULL, FALSE);
1222 inline static LRESULT
1223 HEADER_GetUnicodeFormat (HWND hwnd)
1225 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1226 return (infoPtr->nNotifyFormat == NFR_UNICODE);
1231 HEADER_HitTest (HWND hwnd, WPARAM wParam, LPARAM lParam)
1233 LPHDHITTESTINFO phti = (LPHDHITTESTINFO)lParam;
1235 HEADER_InternalHitTest (hwnd, &phti->pt, &phti->flags, &phti->iItem);
1237 if (phti->flags == HHT_NOWHERE)
1245 HEADER_InsertItemT (HWND hwnd, INT nItem, LPHDITEMW phdi, BOOL bUnicode)
1247 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1248 HEADER_ITEM *lpItem;
1253 if ((phdi == NULL) || (nItem < 0) || (phdi->mask == 0))
1256 if (nItem > infoPtr->uNumItem)
1257 nItem = infoPtr->uNumItem;
1259 iOrder = (phdi->mask & HDI_ORDER) ? phdi->iOrder : nItem;
1262 else if (infoPtr->uNumItem < iOrder)
1263 iOrder = infoPtr->uNumItem;
1265 infoPtr->uNumItem++;
1266 infoPtr->items = ReAlloc(infoPtr->items, sizeof(HEADER_ITEM) * infoPtr->uNumItem);
1267 infoPtr->order = ReAlloc(infoPtr->order, sizeof(INT) * infoPtr->uNumItem);
1269 /* make space for the new item */
1270 memmove(&infoPtr->items[nItem + 1], &infoPtr->items[nItem],
1271 (infoPtr->uNumItem - nItem - 1) * sizeof(HEADER_ITEM));
1272 memmove(&infoPtr->order[iOrder + 1], &infoPtr->order[iOrder],
1273 (infoPtr->uNumItem - iOrder - 1) * sizeof(INT));
1275 /* update the order array */
1276 infoPtr->order[iOrder] = nItem;
1277 for (i = 0; i < infoPtr->uNumItem; i++)
1279 if (i != iOrder && infoPtr->order[i] >= nItem)
1280 infoPtr->order[i]++;
1281 infoPtr->items[infoPtr->order[i]].iOrder = i;
1284 lpItem = &infoPtr->items[nItem];
1285 ZeroMemory(lpItem, sizeof(HEADER_ITEM));
1286 /* cxy, fmt and lParam are copied even if not in the HDITEM mask */
1287 copyMask = phdi->mask | HDI_WIDTH | HDI_FORMAT | HDI_LPARAM;
1288 HEADER_StoreHDItemInHeader(lpItem, copyMask, phdi, bUnicode);
1289 lpItem->iOrder = iOrder;
1291 /* set automatically some format bits */
1292 if (phdi->mask & HDI_TEXT)
1293 lpItem->fmt |= HDF_STRING;
1295 lpItem->fmt &= ~HDF_STRING;
1297 if (lpItem->hbm != NULL)
1298 lpItem->fmt |= HDF_BITMAP;
1300 lpItem->fmt &= ~HDF_BITMAP;
1302 if (phdi->mask & HDI_IMAGE)
1303 lpItem->fmt |= HDF_IMAGE;
1305 HEADER_SetItemBounds (hwnd);
1306 InvalidateRect(hwnd, NULL, FALSE);
1313 HEADER_Layout (HWND hwnd, WPARAM wParam, LPARAM lParam)
1315 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1316 LPHDLAYOUT lpLayout = (LPHDLAYOUT)lParam;
1318 lpLayout->pwpos->hwnd = hwnd;
1319 lpLayout->pwpos->hwndInsertAfter = 0;
1320 lpLayout->pwpos->x = lpLayout->prc->left;
1321 lpLayout->pwpos->y = lpLayout->prc->top;
1322 lpLayout->pwpos->cx = lpLayout->prc->right - lpLayout->prc->left;
1323 if (GetWindowLongW (hwnd, GWL_STYLE) & HDS_HIDDEN)
1324 lpLayout->pwpos->cy = 0;
1326 lpLayout->pwpos->cy = infoPtr->nHeight;
1327 lpLayout->prc->top += infoPtr->nHeight;
1329 lpLayout->pwpos->flags = SWP_NOZORDER;
1331 TRACE("Layout x=%d y=%d cx=%d cy=%d\n",
1332 lpLayout->pwpos->x, lpLayout->pwpos->y,
1333 lpLayout->pwpos->cx, lpLayout->pwpos->cy);
1335 infoPtr->bRectsValid = FALSE;
1342 HEADER_SetImageList (HWND hwnd, HIMAGELIST himl)
1344 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1347 TRACE("(himl %p)\n", himl);
1348 himlOld = infoPtr->himl;
1349 infoPtr->himl = himl;
1351 /* FIXME: Refresh needed??? */
1353 return (LRESULT)himlOld;
1358 HEADER_GetBitmapMargin(HWND hwnd)
1360 HEADER_INFO *infoPtr = HEADER_GetInfoPtr(hwnd);
1362 return infoPtr->iMargin;
1366 HEADER_SetBitmapMargin(HWND hwnd, WPARAM wParam)
1368 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1369 INT oldMargin = infoPtr->iMargin;
1371 infoPtr->iMargin = (INT)wParam;
1377 HEADER_SetItemT (HWND hwnd, INT nItem, LPHDITEMW phdi, BOOL bUnicode)
1379 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1380 HEADER_ITEM *lpItem;
1386 if ((nItem < 0) || (nItem >= (INT)infoPtr->uNumItem))
1389 TRACE("[nItem=%d]\n", nItem);
1391 HEADER_CopyHDItemForNotify(infoPtr, &hdNotify, phdi, bUnicode, &pvScratch);
1392 if (HEADER_SendNotifyWithHDItemT(hwnd, HDN_ITEMCHANGINGW, nItem, &hdNotify))
1394 if (pvScratch) Free(pvScratch);
1398 lpItem = &infoPtr->items[nItem];
1399 HEADER_StoreHDItemInHeader(lpItem, phdi->mask, phdi, bUnicode);
1401 if (phdi->mask & HDI_ORDER)
1402 if (phdi->iOrder >= 0 && phdi->iOrder < infoPtr->uNumItem)
1403 HEADER_ChangeItemOrder(infoPtr, nItem, phdi->iOrder);
1405 HEADER_SendNotifyWithHDItemT(hwnd, HDN_ITEMCHANGEDW, nItem, &hdNotify);
1407 HEADER_SetItemBounds (hwnd);
1409 InvalidateRect(hwnd, NULL, FALSE);
1411 if (pvScratch != NULL)
1416 inline static LRESULT
1417 HEADER_SetUnicodeFormat (HWND hwnd, WPARAM wParam)
1419 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1420 BOOL bTemp = (infoPtr->nNotifyFormat == NFR_UNICODE);
1422 infoPtr->nNotifyFormat = ((BOOL)wParam ? NFR_UNICODE : NFR_ANSI);
1429 HEADER_Create (HWND hwnd, WPARAM wParam, LPARAM lParam)
1431 HEADER_INFO *infoPtr;
1436 infoPtr = (HEADER_INFO *)Alloc (sizeof(HEADER_INFO));
1437 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1439 infoPtr->hwndNotify = ((LPCREATESTRUCTA)lParam)->hwndParent;
1440 infoPtr->uNumItem = 0;
1444 infoPtr->bRectsValid = FALSE;
1445 infoPtr->hcurArrow = LoadCursorW (0, (LPWSTR)IDC_ARROW);
1446 infoPtr->hcurDivider = LoadCursorW (COMCTL32_hModule, MAKEINTRESOURCEW(IDC_DIVIDER));
1447 infoPtr->hcurDivopen = LoadCursorW (COMCTL32_hModule, MAKEINTRESOURCEW(IDC_DIVIDEROPEN));
1448 infoPtr->bPressed = FALSE;
1449 infoPtr->bTracking = FALSE;
1450 infoPtr->iMoveItem = 0;
1452 infoPtr->iHotItem = -1;
1453 infoPtr->iHotDivider = -1;
1454 infoPtr->iMargin = 3*GetSystemMetrics(SM_CXEDGE);
1455 infoPtr->nNotifyFormat =
1456 SendMessageW (infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)hwnd, NF_QUERY);
1459 hOldFont = SelectObject (hdc, GetStockObject (SYSTEM_FONT));
1460 GetTextMetricsW (hdc, &tm);
1461 infoPtr->nHeight = tm.tmHeight + VERT_BORDER;
1462 SelectObject (hdc, hOldFont);
1465 OpenThemeData(hwnd, themeClass);
1472 HEADER_Destroy (HWND hwnd, WPARAM wParam, LPARAM lParam)
1474 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1475 HEADER_ITEM *lpItem;
1479 if (infoPtr->items) {
1480 lpItem = infoPtr->items;
1481 for (nItem = 0; nItem < infoPtr->uNumItem; nItem++, lpItem++) {
1482 HEADER_DisposeItem(lpItem);
1484 Free (infoPtr->items);
1488 Free(infoPtr->order);
1491 ImageList_Destroy (infoPtr->himl);
1493 SetWindowLongPtrW (hwnd, 0, 0);
1496 theme = GetWindowTheme(hwnd);
1497 CloseThemeData(theme);
1502 static inline LRESULT
1503 HEADER_GetFont (HWND hwnd)
1505 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1507 return (LRESULT)infoPtr->hFont;
1512 HEADER_IsDragDistance(HEADER_INFO *infoPtr, POINT *pt)
1514 /* Windows allows for a mouse movement before starting the drag. We use the
1515 * SM_CXDOUBLECLICK/SM_CYDOUBLECLICK as that distance.
1517 return (abs(infoPtr->ptLButtonDown.x - pt->x)>GetSystemMetrics(SM_CXDOUBLECLK) ||
1518 abs(infoPtr->ptLButtonDown.y - pt->y)>GetSystemMetrics(SM_CYDOUBLECLK));
1522 HEADER_LButtonDblClk (HWND hwnd, WPARAM wParam, LPARAM lParam)
1528 pt.x = (INT)LOWORD(lParam);
1529 pt.y = (INT)HIWORD(lParam);
1530 HEADER_InternalHitTest (hwnd, &pt, &flags, &nItem);
1532 if ((GetWindowLongW (hwnd, GWL_STYLE) & HDS_BUTTONS) && (flags == HHT_ONHEADER))
1533 HEADER_SendNotifyWithHDItemT(hwnd, HDN_ITEMDBLCLICKW, nItem, NULL);
1534 else if ((flags == HHT_ONDIVIDER) || (flags == HHT_ONDIVOPEN))
1535 HEADER_SendNotifyWithHDItemT(hwnd, HDN_DIVIDERDBLCLICKW, nItem, NULL);
1542 HEADER_LButtonDown (HWND hwnd, WPARAM wParam, LPARAM lParam)
1544 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1545 DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
1551 pt.x = (INT)LOWORD(lParam);
1552 pt.y = (INT)HIWORD(lParam);
1553 HEADER_InternalHitTest (hwnd, &pt, &flags, &nItem);
1555 if ((dwStyle & HDS_BUTTONS) && (flags == HHT_ONHEADER)) {
1557 infoPtr->bCaptured = TRUE;
1558 infoPtr->bPressed = TRUE;
1559 infoPtr->bDragging = FALSE;
1560 infoPtr->iMoveItem = nItem;
1561 infoPtr->ptLButtonDown = pt;
1563 infoPtr->items[nItem].bDown = TRUE;
1565 /* Send WM_CUSTOMDRAW */
1567 HEADER_RefreshItem (hwnd, hdc, nItem);
1568 ReleaseDC (hwnd, hdc);
1570 TRACE("Pressed item %d!\n", nItem);
1572 else if ((flags == HHT_ONDIVIDER) || (flags == HHT_ONDIVOPEN)) {
1573 INT iCurrWidth = infoPtr->items[nItem].cxy;
1574 if (!HEADER_SendNotifyWithIntFieldT(hwnd, HDN_BEGINTRACKW, nItem, HDI_WIDTH, iCurrWidth))
1577 infoPtr->bCaptured = TRUE;
1578 infoPtr->bTracking = TRUE;
1579 infoPtr->iMoveItem = nItem;
1580 infoPtr->xTrackOffset = infoPtr->items[nItem].rect.right - pt.x;
1582 if (!(dwStyle & HDS_FULLDRAG)) {
1583 infoPtr->xOldTrack = infoPtr->items[nItem].rect.right;
1585 HEADER_DrawTrackLine (hwnd, hdc, infoPtr->xOldTrack);
1586 ReleaseDC (hwnd, hdc);
1589 TRACE("Begin tracking item %d!\n", nItem);
1598 HEADER_LButtonUp (HWND hwnd, WPARAM wParam, LPARAM lParam)
1600 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1601 DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
1607 pt.x = (INT)(SHORT)LOWORD(lParam);
1608 pt.y = (INT)(SHORT)HIWORD(lParam);
1609 HEADER_InternalHitTest (hwnd, &pt, &flags, &nItem);
1611 if (infoPtr->bPressed) {
1612 if (infoPtr->bDragging)
1614 HEADER_ITEM *lpItem = &infoPtr->items[infoPtr->iMoveItem];
1617 ImageList_DragShowNolock(FALSE);
1618 ImageList_EndDrag();
1619 lpItem->bDown=FALSE;
1621 if (infoPtr->iHotDivider == -1)
1623 else if (infoPtr->iHotDivider == infoPtr->uNumItem)
1624 iNewOrder = infoPtr->uNumItem-1;
1627 iNewOrder = HEADER_IndexToOrder(hwnd, infoPtr->iHotDivider);
1628 if (iNewOrder > lpItem->iOrder)
1632 if (iNewOrder != -1 &&
1633 !HEADER_SendNotifyWithIntFieldT(hwnd, HDN_ENDDRAG, infoPtr->iMoveItem, HDI_ORDER, iNewOrder))
1635 HEADER_ChangeItemOrder(infoPtr, infoPtr->iMoveItem, iNewOrder);
1636 infoPtr->bRectsValid = FALSE;
1637 InvalidateRect(hwnd, NULL, FALSE);
1640 InvalidateRect(hwnd, &infoPtr->items[infoPtr->iMoveItem].rect, FALSE);
1642 HEADER_SetHotDivider(hwnd, FALSE, -1);
1644 else if (!(dwStyle&HDS_DRAGDROP) || !HEADER_IsDragDistance(infoPtr, &pt))
1646 infoPtr->items[infoPtr->iMoveItem].bDown = FALSE;
1648 HEADER_RefreshItem (hwnd, hdc, infoPtr->iMoveItem);
1649 ReleaseDC (hwnd, hdc);
1651 HEADER_SendNotifyWithHDItemT(hwnd, HDN_ITEMCLICKW, infoPtr->iMoveItem, NULL);
1654 TRACE("Released item %d!\n", infoPtr->iMoveItem);
1655 infoPtr->bPressed = FALSE;
1657 else if (infoPtr->bTracking) {
1658 INT iNewWidth = pt.x - infoPtr->items[infoPtr->iMoveItem].rect.left + infoPtr->xTrackOffset;
1661 TRACE("End tracking item %d!\n", infoPtr->iMoveItem);
1662 infoPtr->bTracking = FALSE;
1664 HEADER_SendNotifyWithIntFieldT(hwnd, HDN_ENDTRACKW, infoPtr->iMoveItem, HDI_WIDTH, iNewWidth);
1666 if (!(dwStyle & HDS_FULLDRAG)) {
1668 HEADER_DrawTrackLine (hwnd, hdc, infoPtr->xOldTrack);
1669 ReleaseDC (hwnd, hdc);
1672 if (!HEADER_SendNotifyWithIntFieldT(hwnd, HDN_ITEMCHANGINGW, infoPtr->iMoveItem, HDI_WIDTH, iNewWidth))
1674 infoPtr->items[infoPtr->iMoveItem].cxy = iNewWidth;
1675 HEADER_SendNotifyWithIntFieldT(hwnd, HDN_ITEMCHANGEDW, infoPtr->iMoveItem, HDI_WIDTH, iNewWidth);
1678 HEADER_SetItemBounds (hwnd);
1679 InvalidateRect(hwnd, NULL, TRUE);
1682 if (infoPtr->bCaptured) {
1683 infoPtr->bCaptured = FALSE;
1685 HEADER_SendSimpleNotify (hwnd, NM_RELEASEDCAPTURE);
1693 HEADER_NotifyFormat (HWND hwnd, WPARAM wParam, LPARAM lParam)
1695 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1700 return infoPtr->nNotifyFormat;
1703 infoPtr->nNotifyFormat =
1704 SendMessageW ((HWND)wParam, WM_NOTIFYFORMAT,
1705 (WPARAM)hwnd, (LPARAM)NF_QUERY);
1706 return infoPtr->nNotifyFormat;
1713 HEADER_MouseLeave (HWND hwnd, WPARAM wParam, LPARAM lParam)
1715 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1716 /* Reset hot-tracked item when mouse leaves control. */
1717 INT oldHotItem = infoPtr->iHotItem;
1718 HDC hdc = GetDC (hwnd);
1720 infoPtr->iHotItem = -1;
1721 if (oldHotItem != -1) HEADER_RefreshItem (hwnd, hdc, oldHotItem);
1722 ReleaseDC (hwnd, hdc);
1729 HEADER_MouseMove (HWND hwnd, WPARAM wParam, LPARAM lParam)
1731 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1732 DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
1737 /* With theming, hottracking is always enabled */
1738 BOOL hotTrackEnabled =
1739 ((dwStyle & HDS_BUTTONS) && (dwStyle & HDS_HOTTRACK))
1740 || (GetWindowTheme (hwnd) != NULL);
1741 INT oldHotItem = infoPtr->iHotItem;
1743 pt.x = (INT)(SHORT)LOWORD(lParam);
1744 pt.y = (INT)(SHORT)HIWORD(lParam);
1745 HEADER_InternalHitTest (hwnd, &pt, &flags, &nItem);
1747 if (hotTrackEnabled) {
1748 if (flags & (HHT_ONHEADER | HHT_ONDIVIDER | HHT_ONDIVOPEN))
1749 infoPtr->iHotItem = nItem;
1751 infoPtr->iHotItem = -1;
1754 if (infoPtr->bCaptured) {
1755 /* check if we should drag the header */
1756 if (infoPtr->bPressed && !infoPtr->bDragging && dwStyle&HDS_DRAGDROP
1757 && HEADER_IsDragDistance(infoPtr, &pt))
1759 if (!HEADER_SendNotifyWithHDItemT(hwnd, HDN_BEGINDRAG, infoPtr->iMoveItem, NULL))
1761 HIMAGELIST hDragItem = (HIMAGELIST)HEADER_CreateDragImage(hwnd, infoPtr->iMoveItem);
1762 if (hDragItem != NULL)
1764 HEADER_ITEM *lpItem = &infoPtr->items[infoPtr->iMoveItem];
1765 TRACE("Starting item drag\n");
1766 ImageList_BeginDrag(hDragItem, 0, pt.x - lpItem->rect.left, 0);
1767 ImageList_DragShowNolock(TRUE);
1768 ImageList_Destroy(hDragItem);
1769 infoPtr->bDragging = TRUE;
1774 if (infoPtr->bDragging)
1779 ClientToScreen(hwnd, &drag);
1780 ImageList_DragMove(drag.x, drag.y);
1781 HEADER_SetHotDivider(hwnd, TRUE, lParam);
1784 if (infoPtr->bPressed && !infoPtr->bDragging) {
1785 BOOL oldState = infoPtr->items[infoPtr->iMoveItem].bDown;
1786 if ((nItem == infoPtr->iMoveItem) && (flags == HHT_ONHEADER))
1787 infoPtr->items[infoPtr->iMoveItem].bDown = TRUE;
1789 infoPtr->items[infoPtr->iMoveItem].bDown = FALSE;
1790 if (oldState != infoPtr->items[infoPtr->iMoveItem].bDown) {
1792 HEADER_RefreshItem (hwnd, hdc, infoPtr->iMoveItem);
1793 ReleaseDC (hwnd, hdc);
1796 TRACE("Moving pressed item %d!\n", infoPtr->iMoveItem);
1798 else if (infoPtr->bTracking) {
1799 if (dwStyle & HDS_FULLDRAG) {
1800 HEADER_ITEM *lpItem = &infoPtr->items[infoPtr->iMoveItem];
1801 nWidth = pt.x - lpItem->rect.left + infoPtr->xTrackOffset;
1802 if (!HEADER_SendNotifyWithIntFieldT(hwnd, HDN_ITEMCHANGINGW, infoPtr->iMoveItem, HDI_WIDTH, nWidth))
1804 INT nOldWidth = lpItem->rect.right - lpItem->rect.left;
1808 if (nWidth < 0) nWidth = 0;
1809 infoPtr->items[infoPtr->iMoveItem].cxy = nWidth;
1810 HEADER_SetItemBounds(hwnd);
1812 GetClientRect(hwnd, &rcClient);
1813 rcScroll = rcClient;
1814 rcScroll.left = lpItem->rect.left + nOldWidth;
1815 ScrollWindowEx(hwnd, nWidth - nOldWidth, 0, &rcScroll, &rcClient, NULL, NULL, 0);
1816 InvalidateRect(hwnd, &lpItem->rect, FALSE);
1819 HEADER_SendNotifyWithIntFieldT(hwnd, HDN_ITEMCHANGEDW, infoPtr->iMoveItem, HDI_WIDTH, nWidth);
1825 HEADER_DrawTrackLine (hwnd, hdc, infoPtr->xOldTrack);
1826 infoPtr->xOldTrack = pt.x + infoPtr->xTrackOffset;
1827 if (infoPtr->xOldTrack < infoPtr->items[infoPtr->iMoveItem].rect.left)
1828 infoPtr->xOldTrack = infoPtr->items[infoPtr->iMoveItem].rect.left;
1829 HEADER_DrawTrackLine (hwnd, hdc, infoPtr->xOldTrack);
1830 ReleaseDC (hwnd, hdc);
1831 iTrackWidth = infoPtr->xOldTrack - infoPtr->items[infoPtr->iMoveItem].rect.left;
1832 /* FIXME: should stop tracking if HDN_TRACK returns TRUE */
1833 HEADER_SendNotifyWithIntFieldT(hwnd, HDN_TRACKW, infoPtr->iMoveItem, HDI_WIDTH, iTrackWidth);
1836 TRACE("Tracking item %d!\n", infoPtr->iMoveItem);
1840 if (hotTrackEnabled) {
1841 TRACKMOUSEEVENT tme;
1842 if (oldHotItem != infoPtr->iHotItem && !infoPtr->bDragging) {
1844 if (oldHotItem != -1) HEADER_RefreshItem (hwnd, hdc, oldHotItem);
1845 if (infoPtr->iHotItem != -1) HEADER_RefreshItem (hwnd, hdc, infoPtr->iHotItem);
1846 ReleaseDC (hwnd, hdc);
1848 tme.cbSize = sizeof( tme );
1849 tme.dwFlags = TME_LEAVE;
1850 tme.hwndTrack = hwnd;
1851 TrackMouseEvent( &tme );
1859 HEADER_Paint (HWND hwnd, WPARAM wParam)
1864 hdc = wParam==0 ? BeginPaint (hwnd, &ps) : (HDC)wParam;
1865 HEADER_Refresh (hwnd, hdc);
1867 EndPaint (hwnd, &ps);
1873 HEADER_RButtonUp (HWND hwnd, WPARAM wParam, LPARAM lParam)
1878 pt.x = LOWORD(lParam);
1879 pt.y = HIWORD(lParam);
1881 /* Send a Notify message */
1882 bRet = HEADER_SendSimpleNotify (hwnd, NM_RCLICK);
1884 /* Change to screen coordinate for WM_CONTEXTMENU */
1885 ClientToScreen(hwnd, &pt);
1887 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
1888 SendMessageW( hwnd, WM_CONTEXTMENU, (WPARAM) hwnd, MAKELPARAM(pt.x, pt.y));
1895 HEADER_SetCursor (HWND hwnd, WPARAM wParam, LPARAM lParam)
1897 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1902 TRACE("code=0x%X id=0x%X\n", LOWORD(lParam), HIWORD(lParam));
1905 ScreenToClient (hwnd, &pt);
1907 HEADER_InternalHitTest (hwnd, &pt, &flags, &nItem);
1909 if (flags == HHT_ONDIVIDER)
1910 SetCursor (infoPtr->hcurDivider);
1911 else if (flags == HHT_ONDIVOPEN)
1912 SetCursor (infoPtr->hcurDivopen);
1914 SetCursor (infoPtr->hcurArrow);
1921 HEADER_SetFont (HWND hwnd, WPARAM wParam, LPARAM lParam)
1923 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1925 HFONT hFont, hOldFont;
1928 infoPtr->hFont = (HFONT)wParam;
1930 hFont = infoPtr->hFont ? infoPtr->hFont : GetStockObject (SYSTEM_FONT);
1933 hOldFont = SelectObject (hdc, hFont);
1934 GetTextMetricsW (hdc, &tm);
1935 infoPtr->nHeight = tm.tmHeight + VERT_BORDER;
1936 SelectObject (hdc, hOldFont);
1939 infoPtr->bRectsValid = FALSE;
1942 InvalidateRect(hwnd, NULL, FALSE);
1948 /* Update the theme handle after a theme change */
1949 static LRESULT HEADER_ThemeChanged(HWND hwnd)
1951 HTHEME theme = GetWindowTheme(hwnd);
1952 CloseThemeData(theme);
1953 OpenThemeData(hwnd, themeClass);
1954 InvalidateRect(hwnd, NULL, FALSE);
1959 static LRESULT WINAPI
1960 HEADER_WindowProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
1962 TRACE("hwnd=%p msg=%x wparam=%x lParam=%lx\n", hwnd, msg, wParam, lParam);
1963 if (!HEADER_GetInfoPtr (hwnd) && (msg != WM_CREATE))
1964 return DefWindowProcW (hwnd, msg, wParam, lParam);
1966 /* case HDM_CLEARFILTER: */
1968 case HDM_CREATEDRAGIMAGE:
1969 return HEADER_CreateDragImage (hwnd, wParam);
1971 case HDM_DELETEITEM:
1972 return HEADER_DeleteItem (hwnd, wParam);
1974 /* case HDM_EDITFILTER: */
1976 case HDM_GETBITMAPMARGIN:
1977 return HEADER_GetBitmapMargin(hwnd);
1979 case HDM_GETIMAGELIST:
1980 return HEADER_GetImageList (hwnd);
1984 return HEADER_GetItemT (hwnd, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_GETITEMW);
1986 case HDM_GETITEMCOUNT:
1987 return HEADER_GetItemCount (hwnd);
1989 case HDM_GETITEMRECT:
1990 return HEADER_GetItemRect (hwnd, wParam, lParam);
1992 case HDM_GETORDERARRAY:
1993 return HEADER_GetOrderArray(hwnd, wParam, lParam);
1995 case HDM_GETUNICODEFORMAT:
1996 return HEADER_GetUnicodeFormat (hwnd);
1999 return HEADER_HitTest (hwnd, wParam, lParam);
2001 case HDM_INSERTITEMA:
2002 case HDM_INSERTITEMW:
2003 return HEADER_InsertItemT (hwnd, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_INSERTITEMW);
2006 return HEADER_Layout (hwnd, wParam, lParam);
2008 case HDM_ORDERTOINDEX:
2009 return HEADER_OrderToIndex(hwnd, wParam);
2011 case HDM_SETBITMAPMARGIN:
2012 return HEADER_SetBitmapMargin(hwnd, wParam);
2014 /* case HDM_SETFILTERCHANGETIMEOUT: */
2016 case HDM_SETHOTDIVIDER:
2017 return HEADER_SetHotDivider(hwnd, wParam, lParam);
2019 case HDM_SETIMAGELIST:
2020 return HEADER_SetImageList (hwnd, (HIMAGELIST)lParam);
2024 return HEADER_SetItemT (hwnd, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_SETITEMW);
2026 case HDM_SETORDERARRAY:
2027 return HEADER_SetOrderArray(hwnd, wParam, lParam);
2029 case HDM_SETUNICODEFORMAT:
2030 return HEADER_SetUnicodeFormat (hwnd, wParam);
2033 return HEADER_Create (hwnd, wParam, lParam);
2036 return HEADER_Destroy (hwnd, wParam, lParam);
2042 return DLGC_WANTTAB | DLGC_WANTARROWS;
2045 return HEADER_GetFont (hwnd);
2047 case WM_LBUTTONDBLCLK:
2048 return HEADER_LButtonDblClk (hwnd, wParam, lParam);
2050 case WM_LBUTTONDOWN:
2051 return HEADER_LButtonDown (hwnd, wParam, lParam);
2054 return HEADER_LButtonUp (hwnd, wParam, lParam);
2057 return HEADER_MouseLeave (hwnd, wParam, lParam);
2060 return HEADER_MouseMove (hwnd, wParam, lParam);
2062 case WM_NOTIFYFORMAT:
2063 return HEADER_NotifyFormat (hwnd, wParam, lParam);
2066 return HEADER_Size (hwnd, wParam);
2068 case WM_THEMECHANGED:
2069 return HEADER_ThemeChanged (hwnd);
2071 case WM_PRINTCLIENT:
2073 return HEADER_Paint (hwnd, wParam);
2076 return HEADER_RButtonUp (hwnd, wParam, lParam);
2079 return HEADER_SetCursor (hwnd, wParam, lParam);
2082 return HEADER_SetFont (hwnd, wParam, lParam);
2085 if ((msg >= WM_USER) && (msg < WM_APP))
2086 ERR("unknown msg %04x wp=%04x lp=%08lx\n",
2087 msg, wParam, lParam );
2088 return DefWindowProcA (hwnd, msg, wParam, lParam);
2094 HEADER_Register (void)
2098 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
2099 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
2100 wndClass.lpfnWndProc = HEADER_WindowProc;
2101 wndClass.cbClsExtra = 0;
2102 wndClass.cbWndExtra = sizeof(HEADER_INFO *);
2103 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
2104 wndClass.lpszClassName = WC_HEADERW;
2106 RegisterClassW (&wndClass);
2111 HEADER_Unregister (void)
2113 UnregisterClassW (WC_HEADERW, NULL);