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 * - Filters support (HDS_FILTER, HDI_FILTER, HDM_*FILTER*, HDN_*FILTER*)
27 * - New Windows Vista features
36 #include "wine/unicode.h"
42 #include "imagelist.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(header);
57 INT iOrder; /* see documentation of HD_ITEM */
59 BOOL bDown; /* is item pressed? (used for drawing) */
60 RECT rect; /* bounding rectangle of the item */
61 DWORD callbackMask; /* HDI_* flags for items that are callback */
67 HWND hwndSelf; /* Control window */
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 DWORD dwStyle; /* the cached window GWL_STYLE */
82 INT iMoveItem; /* index of tracked item. (Tracking mode) */
83 INT xTrackOffset; /* distance between the right side of the tracked item and the cursor */
84 INT xOldTrack; /* track offset (see above) after the last WM_MOUSEMOVE */
85 INT iHotItem; /* index of hot item (cursor is over this item) */
86 INT iHotDivider; /* index of the hot divider (used while dragging an item or by HDM_SETHOTDIVIDER) */
87 INT iMargin; /* width of the margin that surrounds a bitmap */
89 HIMAGELIST himl; /* handle to an image list (may be 0) */
90 HEADER_ITEM *items; /* pointer to array of HEADER_ITEM's */
91 INT *order; /* array of item IDs indexed by order */
92 BOOL bRectsValid; /* validity flag for bounding rectangles */
97 #define DIVIDER_WIDTH 10
98 #define HOT_DIVIDER_WIDTH 2
99 #define MAX_HEADER_TEXT_LEN 260
100 #define HDN_UNICODE_OFFSET 20
101 #define HDN_FIRST_UNICODE (HDN_FIRST-HDN_UNICODE_OFFSET)
103 #define HDI_SUPPORTED_FIELDS (HDI_WIDTH|HDI_TEXT|HDI_FORMAT|HDI_LPARAM|HDI_BITMAP|HDI_IMAGE|HDI_ORDER)
104 #define HDI_UNSUPPORTED_FIELDS (HDI_FILTER)
105 #define HDI_UNKNOWN_FIELDS (~(HDI_SUPPORTED_FIELDS|HDI_UNSUPPORTED_FIELDS|HDI_DI_SETITEM))
106 #define HDI_COMCTL32_4_0_FIELDS (HDI_WIDTH|HDI_TEXT|HDI_FORMAT|HDI_LPARAM|HDI_BITMAP)
109 static BOOL HEADER_PrepareCallbackItems(const HEADER_INFO *infoPtr, INT iItem, INT reqMask);
110 static void HEADER_FreeCallbackItems(HEADER_ITEM *lpItem);
111 static LRESULT HEADER_SendNotify(const HEADER_INFO *infoPtr, UINT code, NMHDR *hdr);
112 static LRESULT HEADER_SendCtrlCustomDraw(const HEADER_INFO *infoPtr, DWORD dwDrawStage, HDC hdc, const RECT *rect);
114 static const WCHAR themeClass[] = {'H','e','a','d','e','r',0};
116 static void HEADER_StoreHDItemInHeader(HEADER_ITEM *lpItem, UINT mask, const HDITEMW *phdi, BOOL fUnicode)
118 if (mask & HDI_UNSUPPORTED_FIELDS)
119 FIXME("unsupported header fields %x\n", (mask & HDI_UNSUPPORTED_FIELDS));
121 if (mask & HDI_BITMAP)
122 lpItem->hbm = phdi->hbm;
124 if (mask & HDI_FORMAT)
125 lpItem->fmt = phdi->fmt;
127 if (mask & HDI_LPARAM)
128 lpItem->lParam = phdi->lParam;
130 if (mask & HDI_WIDTH)
131 lpItem->cxy = phdi->cxy;
133 if (mask & HDI_IMAGE)
135 lpItem->iImage = phdi->iImage;
136 if (phdi->iImage == I_IMAGECALLBACK)
137 lpItem->callbackMask |= HDI_IMAGE;
139 lpItem->callbackMask &= ~HDI_IMAGE;
144 Free(lpItem->pszText);
145 lpItem->pszText = NULL;
147 if (phdi->pszText != LPSTR_TEXTCALLBACKW) /* covers != TEXTCALLBACKA too */
149 static const WCHAR emptyString[] = {0};
151 LPCWSTR pszText = (phdi->pszText != NULL ? phdi->pszText : emptyString);
153 Str_SetPtrW(&lpItem->pszText, pszText);
155 Str_SetPtrAtoW(&lpItem->pszText, (LPCSTR)pszText);
156 lpItem->callbackMask &= ~HDI_TEXT;
160 lpItem->pszText = NULL;
161 lpItem->callbackMask |= HDI_TEXT;
166 static inline LRESULT
167 HEADER_IndexToOrder (const HEADER_INFO *infoPtr, INT iItem)
169 HEADER_ITEM *lpItem = &infoPtr->items[iItem];
170 return lpItem->iOrder;
175 HEADER_OrderToIndex(const HEADER_INFO *infoPtr, INT iorder)
177 if ((iorder <0) || iorder >= infoPtr->uNumItem)
179 return infoPtr->order[iorder];
183 HEADER_ChangeItemOrder(const HEADER_INFO *infoPtr, INT iItem, INT iNewOrder)
185 HEADER_ITEM *lpItem = &infoPtr->items[iItem];
188 TRACE("%d: %d->%d\n", iItem, lpItem->iOrder, iNewOrder);
189 if (lpItem->iOrder < iNewOrder)
191 memmove(&infoPtr->order[lpItem->iOrder],
192 &infoPtr->order[lpItem->iOrder + 1],
193 (iNewOrder - lpItem->iOrder) * sizeof(INT));
195 if (iNewOrder < lpItem->iOrder)
197 memmove(&infoPtr->order[iNewOrder + 1],
198 &infoPtr->order[iNewOrder],
199 (lpItem->iOrder - iNewOrder) * sizeof(INT));
201 infoPtr->order[iNewOrder] = iItem;
202 nMin = min(lpItem->iOrder, iNewOrder);
203 nMax = max(lpItem->iOrder, iNewOrder);
204 for (i = nMin; i <= nMax; i++)
205 infoPtr->items[infoPtr->order[i]].iOrder = i;
208 /* Note: if iItem is the last item then this function returns infoPtr->uNumItem */
210 HEADER_NextItem(const HEADER_INFO *infoPtr, INT iItem)
212 return HEADER_OrderToIndex(infoPtr, HEADER_IndexToOrder(infoPtr, iItem)+1);
216 HEADER_PrevItem(const HEADER_INFO *infoPtr, INT iItem)
218 return HEADER_OrderToIndex(infoPtr, HEADER_IndexToOrder(infoPtr, iItem)-1);
222 HEADER_SetItemBounds (HEADER_INFO *infoPtr)
229 infoPtr->bRectsValid = TRUE;
231 if (infoPtr->uNumItem == 0)
234 GetClientRect (infoPtr->hwndSelf, &rect);
237 for (i = 0; i < infoPtr->uNumItem; i++) {
238 phdi = &infoPtr->items[HEADER_OrderToIndex(infoPtr,i)];
239 phdi->rect.top = rect.top;
240 phdi->rect.bottom = rect.bottom;
242 phdi->rect.right = phdi->rect.left + ((phdi->cxy>0)?phdi->cxy:0);
243 x = phdi->rect.right;
248 HEADER_Size (HEADER_INFO *infoPtr)
250 infoPtr->bRectsValid = FALSE;
255 static void HEADER_GetHotDividerRect(const HEADER_INFO *infoPtr, RECT *r)
257 INT iDivider = infoPtr->iHotDivider;
258 if (infoPtr->uNumItem > 0)
262 if (iDivider < infoPtr->uNumItem)
264 lpItem = &infoPtr->items[iDivider];
265 r->left = lpItem->rect.left - HOT_DIVIDER_WIDTH/2;
266 r->right = lpItem->rect.left + HOT_DIVIDER_WIDTH/2;
270 lpItem = &infoPtr->items[HEADER_OrderToIndex(infoPtr, infoPtr->uNumItem-1)];
271 r->left = lpItem->rect.right - HOT_DIVIDER_WIDTH/2;
272 r->right = lpItem->rect.right + HOT_DIVIDER_WIDTH/2;
274 r->top = lpItem->rect.top;
275 r->bottom = lpItem->rect.bottom;
280 GetClientRect(infoPtr->hwndSelf, &clientRect);
282 r->right = r->left + HOT_DIVIDER_WIDTH/2;
288 HEADER_DrawItem (HEADER_INFO *infoPtr, HDC hdc, INT iItem, BOOL bHotTrack, LRESULT lCDFlags)
290 HEADER_ITEM *phdi = &infoPtr->items[iItem];
293 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
296 TRACE("DrawItem(iItem %d bHotTrack %d unicode flag %d)\n", iItem, bHotTrack, (infoPtr->nNotifyFormat == NFR_UNICODE));
299 if (r.right - r.left == 0)
300 return phdi->rect.right;
302 /* Set the colors before sending NM_CUSTOMDRAW so that it can change them */
303 SetTextColor(hdc, (bHotTrack && !theme) ? COLOR_HIGHLIGHT : COLOR_BTNTEXT);
304 SetBkColor(hdc, comctl32_color.clr3dFace);
306 if (lCDFlags & CDRF_NOTIFYITEMDRAW && !(phdi->fmt & HDF_OWNERDRAW))
308 LRESULT lCDItemFlags;
310 nmcd.dwDrawStage = CDDS_PREPAINT | CDDS_ITEM;
312 nmcd.dwItemSpec = iItem;
314 nmcd.uItemState = phdi->bDown ? CDIS_SELECTED : 0;
315 nmcd.lItemlParam = phdi->lParam;
317 lCDItemFlags = HEADER_SendNotify(infoPtr, NM_CUSTOMDRAW, (NMHDR *)&nmcd);
318 if (lCDItemFlags & CDRF_SKIPDEFAULT)
319 return phdi->rect.right;
323 int state = (phdi->bDown) ? HIS_PRESSED :
324 (bHotTrack ? HIS_HOT : HIS_NORMAL);
325 DrawThemeBackground (theme, hdc, HP_HEADERITEM, state,
327 GetThemeBackgroundContentRect (theme, hdc, HP_HEADERITEM, state,
333 if (infoPtr->dwStyle & HDS_BUTTONS) {
335 DrawEdge (hdc, &r, BDR_RAISEDOUTER,
336 BF_RECT | BF_FLAT | BF_MIDDLE | BF_ADJUST);
339 DrawEdge (hdc, &r, EDGE_RAISED,
340 BF_RECT | BF_SOFT | BF_MIDDLE | BF_ADJUST);
343 DrawEdge (hdc, &r, EDGE_ETCHED, BF_BOTTOM | BF_RIGHT | BF_ADJUST);
345 hbr = CreateSolidBrush(GetBkColor(hdc));
346 FillRect(hdc, &r, hbr);
354 if (phdi->fmt & HDF_OWNERDRAW) {
357 dis.CtlType = ODT_HEADER;
358 dis.CtlID = GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_ID);
360 dis.itemAction = ODA_DRAWENTIRE;
361 dis.itemState = phdi->bDown ? ODS_SELECTED : 0;
362 dis.hwndItem = infoPtr->hwndSelf;
364 dis.rcItem = phdi->rect;
365 dis.itemData = phdi->lParam;
366 oldBkMode = SetBkMode(hdc, TRANSPARENT);
367 SendMessageW (infoPtr->hwndNotify, WM_DRAWITEM,
368 (WPARAM)dis.CtlID, (LPARAM)&dis);
369 if (oldBkMode != TRANSPARENT)
370 SetBkMode(hdc, oldBkMode);
373 UINT rw, rh, /* width and height of r */
374 *x = NULL, *w = NULL; /* x and width of the pic (bmp or img) which is part of cnt */
375 /* cnt,txt,img,bmp */
380 HEADER_PrepareCallbackItems(infoPtr, iItem, HDI_TEXT|HDI_IMAGE);
381 cw = tw = iw = bw = 0;
382 rw = r.right - r.left;
383 rh = r.bottom - r.top;
385 if (phdi->fmt & HDF_STRING) {
388 SetRectEmpty(&textRect);
389 DrawTextW (hdc, phdi->pszText, -1,
390 &textRect, DT_LEFT|DT_VCENTER|DT_SINGLELINE|DT_CALCRECT);
391 cw = textRect.right - textRect.left + 2 * infoPtr->iMargin;
394 if ((phdi->fmt & HDF_IMAGE) && (infoPtr->himl)) {
395 iw = infoPtr->himl->cx + 2 * infoPtr->iMargin;
400 if ((phdi->fmt & HDF_BITMAP) && (phdi->hbm)) {
401 GetObjectW (phdi->hbm, sizeof(BITMAP), &bmp);
402 bw = bmp.bmWidth + 2 * infoPtr->iMargin;
412 /* align cx using the unclipped cw */
413 if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_LEFT)
415 else if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_CENTER)
416 cx = r.left + rw / 2 - cw / 2;
423 if (cx + cw > r.right)
426 tx = cx + infoPtr->iMargin;
427 /* since cw might have changed we have to recalculate tw */
428 tw = cw - infoPtr->iMargin * 2;
432 if (phdi->fmt & HDF_BITMAP_ON_RIGHT) {
433 /* put pic behind text */
434 *x = cx + tw + infoPtr->iMargin * 3;
436 *x = cx + infoPtr->iMargin;
437 /* move text behind pic */
443 /* since we're done with the layout we can
444 now calculate the position of bmp which
445 has no influence on alignment and layout
447 if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_RIGHT)
448 bx = cx - bw + infoPtr->iMargin;
450 bx = cx + cw + infoPtr->iMargin;
454 HDC hClipDC = GetDC(infoPtr->hwndSelf);
455 HRGN hClipRgn = CreateRectRgn(r.left, r.top, r.right, r.bottom);
456 SelectClipRgn(hClipDC, hClipRgn);
459 HDC hdcBitmap = CreateCompatibleDC (hClipDC);
460 SelectObject (hdcBitmap, phdi->hbm);
461 BitBlt (hClipDC, bx, r.top + ((INT)rh - bmp.bmHeight) / 2,
462 bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
463 DeleteDC (hdcBitmap);
467 ImageList_DrawEx (infoPtr->himl, phdi->iImage, hClipDC,
468 ix, r.top + ((INT)rh - infoPtr->himl->cy) / 2,
469 infoPtr->himl->cx, infoPtr->himl->cy, CLR_DEFAULT, CLR_DEFAULT, 0);
472 DeleteObject(hClipRgn);
473 ReleaseDC(infoPtr->hwndSelf, hClipDC);
476 if (((phdi->fmt & HDF_STRING)
477 || (!(phdi->fmt & (HDF_OWNERDRAW|HDF_STRING|HDF_BITMAP|
478 HDF_BITMAP_ON_RIGHT|HDF_IMAGE)))) /* no explicit format specified? */
479 && (phdi->pszText)) {
480 oldBkMode = SetBkMode(hdc, TRANSPARENT);
483 DrawTextW (hdc, phdi->pszText, -1,
484 &r, DT_LEFT|DT_END_ELLIPSIS|DT_VCENTER|DT_SINGLELINE);
485 if (oldBkMode != TRANSPARENT)
486 SetBkMode(hdc, oldBkMode);
488 HEADER_FreeCallbackItems(phdi);
491 return phdi->rect.right;
495 HEADER_DrawHotDivider(const HEADER_INFO *infoPtr, HDC hdc)
500 HEADER_GetHotDividerRect(infoPtr, &r);
501 brush = CreateSolidBrush(comctl32_color.clrHighlight);
502 FillRect(hdc, &r, brush);
507 HEADER_Refresh (HEADER_INFO *infoPtr, HDC hdc)
509 HFONT hFont, hOldFont;
515 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
517 if (!infoPtr->bRectsValid)
518 HEADER_SetItemBounds(infoPtr);
520 /* get rect for the bar, adjusted for the border */
521 GetClientRect (infoPtr->hwndSelf, &rect);
522 lCDFlags = HEADER_SendCtrlCustomDraw(infoPtr, CDDS_PREPAINT, hdc, &rect);
524 if (infoPtr->bDragging)
525 ImageList_DragShowNolock(FALSE);
527 hFont = infoPtr->hFont ? infoPtr->hFont : GetStockObject (SYSTEM_FONT);
528 hOldFont = SelectObject (hdc, hFont);
530 /* draw Background */
531 if (infoPtr->uNumItem == 0 && theme == NULL) {
532 hbrBk = GetSysColorBrush(COLOR_3DFACE);
533 FillRect(hdc, &rect, hbrBk);
537 for (i = 0; x <= rect.right && i < infoPtr->uNumItem; i++) {
538 int idx = HEADER_OrderToIndex(infoPtr,i);
539 if (RectVisible(hdc, &infoPtr->items[idx].rect))
540 HEADER_DrawItem(infoPtr, hdc, idx, infoPtr->iHotItem == idx, lCDFlags);
541 x = infoPtr->items[idx].rect.right;
546 if ((x <= rect.right) && RectVisible(hdc, &rcRest) && (infoPtr->uNumItem > 0)) {
548 DrawThemeBackground(theme, hdc, HP_HEADERITEM, HIS_NORMAL, &rcRest, NULL);
551 if (infoPtr->dwStyle & HDS_BUTTONS)
552 DrawEdge (hdc, &rcRest, EDGE_RAISED, BF_TOP|BF_LEFT|BF_BOTTOM|BF_SOFT|BF_MIDDLE);
554 DrawEdge (hdc, &rcRest, EDGE_ETCHED, BF_BOTTOM|BF_MIDDLE);
558 if (infoPtr->iHotDivider != -1)
559 HEADER_DrawHotDivider(infoPtr, hdc);
561 if (infoPtr->bDragging)
562 ImageList_DragShowNolock(TRUE);
563 SelectObject (hdc, hOldFont);
565 if (lCDFlags & CDRF_NOTIFYPOSTPAINT)
566 HEADER_SendCtrlCustomDraw(infoPtr, CDDS_POSTPAINT, hdc, &rect);
571 HEADER_RefreshItem (HEADER_INFO *infoPtr, INT iItem)
573 if (!infoPtr->bRectsValid)
574 HEADER_SetItemBounds(infoPtr);
576 InvalidateRect(infoPtr->hwndSelf, &infoPtr->items[iItem].rect, FALSE);
581 HEADER_InternalHitTest (const HEADER_INFO *infoPtr, const POINT *lpPt, UINT *pFlags, INT *pItem)
588 GetClientRect (infoPtr->hwndSelf, &rect);
592 if (PtInRect (&rect, *lpPt))
594 if (infoPtr->uNumItem == 0) {
595 *pFlags |= HHT_NOWHERE;
601 /* somewhere inside */
602 for (iCount = 0; iCount < infoPtr->uNumItem; iCount++) {
603 rect = infoPtr->items[iCount].rect;
604 width = rect.right - rect.left;
609 if (PtInRect (&rect, *lpPt)) {
610 if (width <= 2 * DIVIDER_WIDTH) {
611 *pFlags |= HHT_ONHEADER;
613 TRACE("ON HEADER %d\n", iCount);
616 if (HEADER_IndexToOrder(infoPtr, iCount) > 0) {
618 rcTest.right = rcTest.left + DIVIDER_WIDTH;
619 if (PtInRect (&rcTest, *lpPt)) {
621 *pFlags |= HHT_ONDIVOPEN;
622 *pItem = HEADER_PrevItem(infoPtr, iCount);
623 TRACE("ON DIVOPEN %d\n", *pItem);
627 *pFlags |= HHT_ONDIVIDER;
628 *pItem = HEADER_PrevItem(infoPtr, iCount);
629 TRACE("ON DIVIDER %d\n", *pItem);
635 rcTest.left = rcTest.right - DIVIDER_WIDTH;
636 if (PtInRect (&rcTest, *lpPt)) {
637 *pFlags |= HHT_ONDIVIDER;
639 TRACE("ON DIVIDER %d\n", *pItem);
643 *pFlags |= HHT_ONHEADER;
645 TRACE("ON HEADER %d\n", iCount);
650 /* check for last divider part (on nowhere) */
651 rect = infoPtr->items[infoPtr->uNumItem-1].rect;
652 rect.left = rect.right;
653 rect.right += DIVIDER_WIDTH;
654 if (PtInRect (&rect, *lpPt)) {
656 *pFlags |= HHT_ONDIVOPEN;
657 *pItem = infoPtr->uNumItem - 1;
658 TRACE("ON DIVOPEN %d\n", *pItem);
662 *pFlags |= HHT_ONDIVIDER;
663 *pItem = infoPtr->uNumItem-1;
664 TRACE("ON DIVIDER %d\n", *pItem);
669 *pFlags |= HHT_NOWHERE;
676 if (lpPt->x < rect.left) {
678 *pFlags |= HHT_TOLEFT;
680 else if (lpPt->x > rect.right) {
682 *pFlags |= HHT_TORIGHT;
685 if (lpPt->y < rect.top) {
687 *pFlags |= HHT_ABOVE;
689 else if (lpPt->y > rect.bottom) {
691 *pFlags |= HHT_BELOW;
696 TRACE("flags=0x%X\n", *pFlags);
702 HEADER_DrawTrackLine (const HEADER_INFO *infoPtr, HDC hdc, INT x)
708 GetClientRect (infoPtr->hwndSelf, &rect);
710 hOldPen = SelectObject (hdc, GetStockObject (BLACK_PEN));
711 oldRop = SetROP2 (hdc, R2_XORPEN);
712 MoveToEx (hdc, x, rect.top, NULL);
713 LineTo (hdc, x, rect.bottom);
714 SetROP2 (hdc, oldRop);
715 SelectObject (hdc, hOldPen);
720 * Convert a HDITEM into the correct format (ANSI/Unicode) to send it in a notify
723 * [I] infoPtr : the header that wants to send the notify
724 * [O] dest : The buffer to store the HDITEM for notify. It may be set to a HDITEMA of HDITEMW
725 * [I] src : The source HDITEM. It may be a HDITEMA or HDITEMW
726 * [I] fSourceUnicode : is src a HDITEMW or HDITEMA
727 * [O] ppvScratch : a pointer to a scratch buffer that needs to be freed after
728 * the HDITEM is no longer in use or NULL if none was needed
730 * NOTE: We depend on HDITEMA and HDITEMW having the same structure
732 static void HEADER_CopyHDItemForNotify(const HEADER_INFO *infoPtr, HDITEMW *dest,
733 const HDITEMW *src, BOOL fSourceUnicode, LPVOID *ppvScratch)
738 if (src->mask & HDI_TEXT && src->pszText != LPSTR_TEXTCALLBACKW) /* covers TEXTCALLBACKA as well */
740 if (fSourceUnicode && infoPtr->nNotifyFormat != NFR_UNICODE)
742 dest->pszText = NULL;
743 Str_SetPtrWtoA((LPSTR *)&dest->pszText, src->pszText);
744 *ppvScratch = dest->pszText;
747 if (!fSourceUnicode && infoPtr->nNotifyFormat == NFR_UNICODE)
749 dest->pszText = NULL;
750 Str_SetPtrAtoW(&dest->pszText, (LPSTR)src->pszText);
751 *ppvScratch = dest->pszText;
756 static UINT HEADER_NotifyCodeWtoA(UINT code)
758 /* we use the fact that all the unicode messages are in HDN_FIRST_UNICODE..HDN_LAST*/
759 if (code >= HDN_LAST && code <= HDN_FIRST_UNICODE)
760 return code + HDN_UNICODE_OFFSET;
766 HEADER_SendNotify(const HEADER_INFO *infoPtr, UINT code, NMHDR *nmhdr)
768 nmhdr->hwndFrom = infoPtr->hwndSelf;
769 nmhdr->idFrom = GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_ID);
772 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
773 nmhdr->idFrom, (LPARAM)nmhdr);
777 HEADER_SendSimpleNotify (const HEADER_INFO *infoPtr, UINT code)
780 return (BOOL)HEADER_SendNotify(infoPtr, code, &nmhdr);
784 HEADER_SendCtrlCustomDraw(const HEADER_INFO *infoPtr, DWORD dwDrawStage, HDC hdc, const RECT *rect)
787 nm.dwDrawStage = dwDrawStage;
794 return HEADER_SendNotify(infoPtr, NM_CUSTOMDRAW, (NMHDR *)&nm);
798 HEADER_SendNotifyWithHDItemT(const HEADER_INFO *infoPtr, UINT code, INT iItem, HDITEMW *lpItem)
802 if (infoPtr->nNotifyFormat != NFR_UNICODE)
803 code = HEADER_NotifyCodeWtoA(code);
806 nmhdr.pitem = lpItem;
808 return (BOOL)HEADER_SendNotify(infoPtr, code, (NMHDR *)&nmhdr);
812 HEADER_SendNotifyWithIntFieldT(const HEADER_INFO *infoPtr, UINT code, INT iItem, INT mask, INT iValue)
816 /* copying only the iValue should be ok but to make the code more robust we copy everything */
817 nmitem.cxy = infoPtr->items[iItem].cxy;
818 nmitem.hbm = infoPtr->items[iItem].hbm;
819 nmitem.pszText = NULL;
820 nmitem.cchTextMax = 0;
821 nmitem.fmt = infoPtr->items[iItem].fmt;
822 nmitem.lParam = infoPtr->items[iItem].lParam;
823 nmitem.iOrder = infoPtr->items[iItem].iOrder;
824 nmitem.iImage = infoPtr->items[iItem].iImage;
833 nmitem.iOrder = iValue;
836 ERR("invalid mask value 0x%x\n", iValue);
839 return HEADER_SendNotifyWithHDItemT(infoPtr, code, iItem, &nmitem);
843 * Prepare callback items
844 * depends on NMHDDISPINFOW having same structure as NMHDDISPINFOA
845 * (so we handle the two cases only doing a specific cast for pszText).
846 * Checks if any of the required field are callback. If there are sends a
847 * NMHDISPINFO notify to retrieve these items. The items are stored in the
848 * HEADER_ITEM pszText and iImage fields. They should be freed with
849 * HEADER_FreeCallbackItems.
851 * @param hwnd : hwnd header container handler
852 * @param iItem : the header item id
853 * @param reqMask : required fields. If any of them is callback this function will fetch it
855 * @return TRUE on success, else FALSE
858 HEADER_PrepareCallbackItems(const HEADER_INFO *infoPtr, INT iItem, INT reqMask)
860 HEADER_ITEM *lpItem = &infoPtr->items[iItem];
861 DWORD mask = reqMask & lpItem->callbackMask;
862 NMHDDISPINFOW dispInfo;
863 void *pvBuffer = NULL;
867 if (mask&HDI_TEXT && lpItem->pszText != NULL)
869 ERR("(): function called without a call to FreeCallbackItems\n");
870 Free(lpItem->pszText);
871 lpItem->pszText = NULL;
874 memset(&dispInfo, 0, sizeof(NMHDDISPINFOW));
875 dispInfo.hdr.hwndFrom = infoPtr->hwndSelf;
876 dispInfo.hdr.idFrom = GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_ID);
877 if (infoPtr->nNotifyFormat == NFR_UNICODE)
879 dispInfo.hdr.code = HDN_GETDISPINFOW;
881 pvBuffer = Alloc(MAX_HEADER_TEXT_LEN * sizeof(WCHAR));
885 dispInfo.hdr.code = HDN_GETDISPINFOA;
887 pvBuffer = Alloc(MAX_HEADER_TEXT_LEN * sizeof(CHAR));
889 dispInfo.pszText = pvBuffer;
890 dispInfo.cchTextMax = (pvBuffer!=NULL?MAX_HEADER_TEXT_LEN:0);
891 dispInfo.iItem = iItem;
892 dispInfo.mask = mask;
893 dispInfo.lParam = lpItem->lParam;
895 TRACE("Sending HDN_GETDISPINFO%c\n", infoPtr->nNotifyFormat == NFR_UNICODE?'W':'A');
896 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, dispInfo.hdr.idFrom, (LPARAM)&dispInfo);
898 TRACE("SendMessage returns(mask:0x%x,str:%s,lParam:%p)\n",
900 (infoPtr->nNotifyFormat == NFR_UNICODE ? debugstr_w(dispInfo.pszText) : (LPSTR) dispInfo.pszText),
901 (void*) dispInfo.lParam);
903 if (mask & HDI_IMAGE)
904 lpItem->iImage = dispInfo.iImage;
907 if (infoPtr->nNotifyFormat == NFR_UNICODE)
909 lpItem->pszText = pvBuffer;
911 /* the user might have used his own buffer */
912 if (dispInfo.pszText != lpItem->pszText)
913 Str_GetPtrW(dispInfo.pszText, lpItem->pszText, MAX_HEADER_TEXT_LEN);
917 Str_SetPtrAtoW(&lpItem->pszText, (LPSTR)dispInfo.pszText);
922 if (dispInfo.mask & HDI_DI_SETITEM)
924 /* make the items permanent */
925 lpItem->callbackMask &= ~dispInfo.mask;
933 * Free the items that might be allocated with HEADER_PrepareCallbackItems
936 * [I] lpItem : the item to free the data
940 HEADER_FreeCallbackItems(HEADER_ITEM *lpItem)
942 if (lpItem->callbackMask&HDI_TEXT)
944 Free(lpItem->pszText);
945 lpItem->pszText = NULL;
948 if (lpItem->callbackMask&HDI_IMAGE)
949 lpItem->iImage = I_IMAGECALLBACK;
953 HEADER_CreateDragImage (HEADER_INFO *infoPtr, INT iItem)
957 HBITMAP hMemory, hOldBitmap;
965 if (iItem >= infoPtr->uNumItem)
968 if (!infoPtr->bRectsValid)
969 HEADER_SetItemBounds(infoPtr);
971 lpItem = &infoPtr->items[iItem];
972 width = lpItem->rect.right - lpItem->rect.left;
973 height = lpItem->rect.bottom - lpItem->rect.top;
975 hDeviceDC = GetDC(NULL);
976 hMemoryDC = CreateCompatibleDC(hDeviceDC);
977 hMemory = CreateCompatibleBitmap(hDeviceDC, width, height);
978 ReleaseDC(NULL, hDeviceDC);
979 hOldBitmap = SelectObject(hMemoryDC, hMemory);
980 SetViewportOrgEx(hMemoryDC, -lpItem->rect.left, -lpItem->rect.top, NULL);
981 hFont = infoPtr->hFont ? infoPtr->hFont : GetStockObject(SYSTEM_FONT);
982 SelectObject(hMemoryDC, hFont);
984 GetClientRect(infoPtr->hwndSelf, &rc);
985 lCDFlags = HEADER_SendCtrlCustomDraw(infoPtr, CDDS_PREPAINT, hMemoryDC, &rc);
986 HEADER_DrawItem(infoPtr, hMemoryDC, iItem, FALSE, lCDFlags);
987 if (lCDFlags & CDRF_NOTIFYPOSTPAINT)
988 HEADER_SendCtrlCustomDraw(infoPtr, CDDS_POSTPAINT, hMemoryDC, &rc);
990 hMemory = SelectObject(hMemoryDC, hOldBitmap);
993 if (hMemory == NULL) /* if anything failed */
996 himl = ImageList_Create(width, height, ILC_COLORDDB, 1, 1);
997 ImageList_Add(himl, hMemory, NULL);
998 DeleteObject(hMemory);
999 return (LRESULT)himl;
1003 HEADER_SetHotDivider(HEADER_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1012 pt.x = (INT)(SHORT)LOWORD(lParam);
1014 HEADER_InternalHitTest (infoPtr, &pt, &flags, &iDivider);
1016 if (flags & HHT_TOLEFT)
1018 else if (flags & HHT_NOWHERE || flags & HHT_TORIGHT)
1019 iDivider = infoPtr->uNumItem;
1022 HEADER_ITEM *lpItem = &infoPtr->items[iDivider];
1023 if (pt.x > (lpItem->rect.left+lpItem->rect.right)/2)
1024 iDivider = HEADER_NextItem(infoPtr, iDivider);
1028 iDivider = (INT)lParam;
1030 /* Note; wParam==FALSE, lParam==-1 is valid and is used to clear the hot divider */
1031 if (iDivider<-1 || iDivider>(int)infoPtr->uNumItem)
1034 if (iDivider != infoPtr->iHotDivider)
1036 if (infoPtr->iHotDivider != -1)
1038 HEADER_GetHotDividerRect(infoPtr, &r);
1039 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
1041 infoPtr->iHotDivider = iDivider;
1044 HEADER_GetHotDividerRect(infoPtr, &r);
1045 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
1052 HEADER_DeleteItem (HEADER_INFO *infoPtr, INT iItem)
1057 TRACE("[iItem=%d]\n", iItem);
1059 if ((iItem < 0) || (iItem >= (INT)infoPtr->uNumItem))
1062 for (i = 0; i < infoPtr->uNumItem; i++)
1063 TRACE("%d: order=%d, iOrder=%d, ->iOrder=%d\n", i, infoPtr->order[i], infoPtr->items[i].iOrder, infoPtr->items[infoPtr->order[i]].iOrder);
1065 iOrder = infoPtr->items[iItem].iOrder;
1066 Free(infoPtr->items[iItem].pszText);
1068 infoPtr->uNumItem--;
1069 memmove(&infoPtr->items[iItem], &infoPtr->items[iItem + 1],
1070 (infoPtr->uNumItem - iItem) * sizeof(HEADER_ITEM));
1071 memmove(&infoPtr->order[iOrder], &infoPtr->order[iOrder + 1],
1072 (infoPtr->uNumItem - iOrder) * sizeof(INT));
1073 infoPtr->items = ReAlloc(infoPtr->items, sizeof(HEADER_ITEM) * infoPtr->uNumItem);
1074 infoPtr->order = ReAlloc(infoPtr->order, sizeof(INT) * infoPtr->uNumItem);
1076 /* Correct the orders */
1077 for (i = 0; i < infoPtr->uNumItem; i++)
1079 if (infoPtr->order[i] > iItem)
1080 infoPtr->order[i]--;
1082 infoPtr->items[infoPtr->order[i]].iOrder = i;
1084 for (i = 0; i < infoPtr->uNumItem; i++)
1085 TRACE("%d: order=%d, iOrder=%d, ->iOrder=%d\n", i, infoPtr->order[i], infoPtr->items[i].iOrder, infoPtr->items[infoPtr->order[i]].iOrder);
1087 HEADER_SetItemBounds (infoPtr);
1088 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1095 HEADER_GetImageList (const HEADER_INFO *infoPtr)
1097 return (LRESULT)infoPtr->himl;
1102 HEADER_GetItemT (const HEADER_INFO *infoPtr, INT nItem, LPHDITEMW phdi, BOOL bUnicode)
1104 HEADER_ITEM *lpItem;
1110 TRACE("[nItem=%d]\n", nItem);
1116 if ((nItem < 0) || (nItem >= (INT)infoPtr->uNumItem))
1119 if (mask & HDI_UNKNOWN_FIELDS)
1121 TRACE("mask %x contains unknown fields. Using only comctl32 4.0 fields\n", mask);
1122 mask &= HDI_COMCTL32_4_0_FIELDS;
1125 lpItem = &infoPtr->items[nItem];
1126 HEADER_PrepareCallbackItems(infoPtr, nItem, mask);
1128 if (mask & HDI_BITMAP)
1129 phdi->hbm = lpItem->hbm;
1131 if (mask & HDI_FORMAT)
1132 phdi->fmt = lpItem->fmt;
1134 if (mask & HDI_WIDTH)
1135 phdi->cxy = lpItem->cxy;
1137 if (mask & HDI_LPARAM)
1138 phdi->lParam = lpItem->lParam;
1140 if (mask & HDI_IMAGE)
1141 phdi->iImage = lpItem->iImage;
1143 if (mask & HDI_ORDER)
1144 phdi->iOrder = lpItem->iOrder;
1146 if (mask & HDI_TEXT)
1149 Str_GetPtrW (lpItem->pszText, phdi->pszText, phdi->cchTextMax);
1151 Str_GetPtrWtoA (lpItem->pszText, (LPSTR)phdi->pszText, phdi->cchTextMax);
1154 HEADER_FreeCallbackItems(lpItem);
1159 static inline LRESULT
1160 HEADER_GetItemCount (const HEADER_INFO *infoPtr)
1162 return infoPtr->uNumItem;
1167 HEADER_GetItemRect (const HEADER_INFO *infoPtr, INT iItem, LPRECT lpRect)
1169 if ((iItem < 0) || (iItem >= (INT)infoPtr->uNumItem))
1172 lpRect->left = infoPtr->items[iItem].rect.left;
1173 lpRect->right = infoPtr->items[iItem].rect.right;
1174 lpRect->top = infoPtr->items[iItem].rect.top;
1175 lpRect->bottom = infoPtr->items[iItem].rect.bottom;
1182 HEADER_GetOrderArray(const HEADER_INFO *infoPtr, INT size, LPINT order)
1184 if ((UINT)size <infoPtr->uNumItem)
1187 memcpy(order, infoPtr->order, infoPtr->uNumItem * sizeof(INT));
1192 HEADER_SetOrderArray(HEADER_INFO *infoPtr, INT size, LPINT order)
1195 HEADER_ITEM *lpItem;
1197 if ((UINT)size <infoPtr->uNumItem)
1199 memcpy(infoPtr->order, order, infoPtr->uNumItem * sizeof(INT));
1200 for (i=0; i<size; i++)
1202 lpItem = &infoPtr->items[*order++];
1205 infoPtr->bRectsValid=0;
1206 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1210 static inline LRESULT
1211 HEADER_GetUnicodeFormat (const HEADER_INFO *infoPtr)
1213 return (infoPtr->nNotifyFormat == NFR_UNICODE);
1218 HEADER_HitTest (const HEADER_INFO *infoPtr, LPHDHITTESTINFO phti)
1220 HEADER_InternalHitTest (infoPtr, &phti->pt, &phti->flags, &phti->iItem);
1222 if (phti->flags == HHT_NOWHERE)
1230 HEADER_InsertItemT (HEADER_INFO *infoPtr, INT nItem, const HDITEMW *phdi, BOOL bUnicode)
1232 HEADER_ITEM *lpItem;
1237 if ((phdi == NULL) || (nItem < 0) || (phdi->mask == 0))
1240 if (nItem > infoPtr->uNumItem)
1241 nItem = infoPtr->uNumItem;
1243 iOrder = (phdi->mask & HDI_ORDER) ? phdi->iOrder : nItem;
1246 else if (infoPtr->uNumItem < iOrder)
1247 iOrder = infoPtr->uNumItem;
1249 infoPtr->uNumItem++;
1250 infoPtr->items = ReAlloc(infoPtr->items, sizeof(HEADER_ITEM) * infoPtr->uNumItem);
1251 infoPtr->order = ReAlloc(infoPtr->order, sizeof(INT) * infoPtr->uNumItem);
1253 /* make space for the new item */
1254 memmove(&infoPtr->items[nItem + 1], &infoPtr->items[nItem],
1255 (infoPtr->uNumItem - nItem - 1) * sizeof(HEADER_ITEM));
1256 memmove(&infoPtr->order[iOrder + 1], &infoPtr->order[iOrder],
1257 (infoPtr->uNumItem - iOrder - 1) * sizeof(INT));
1259 /* update the order array */
1260 infoPtr->order[iOrder] = nItem;
1261 for (i = 0; i < infoPtr->uNumItem; i++)
1263 if (i != iOrder && infoPtr->order[i] >= nItem)
1264 infoPtr->order[i]++;
1265 infoPtr->items[infoPtr->order[i]].iOrder = i;
1268 lpItem = &infoPtr->items[nItem];
1269 ZeroMemory(lpItem, sizeof(HEADER_ITEM));
1270 /* cxy, fmt and lParam are copied even if not in the HDITEM mask */
1271 copyMask = phdi->mask | HDI_WIDTH | HDI_FORMAT | HDI_LPARAM;
1272 HEADER_StoreHDItemInHeader(lpItem, copyMask, phdi, bUnicode);
1273 lpItem->iOrder = iOrder;
1275 /* set automatically some format bits */
1276 if (phdi->mask & HDI_TEXT)
1277 lpItem->fmt |= HDF_STRING;
1279 lpItem->fmt &= ~HDF_STRING;
1281 if (lpItem->hbm != NULL)
1282 lpItem->fmt |= HDF_BITMAP;
1284 lpItem->fmt &= ~HDF_BITMAP;
1286 if (phdi->mask & HDI_IMAGE)
1287 lpItem->fmt |= HDF_IMAGE;
1289 HEADER_SetItemBounds (infoPtr);
1290 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1297 HEADER_Layout (HEADER_INFO *infoPtr, LPHDLAYOUT lpLayout)
1299 lpLayout->pwpos->hwnd = infoPtr->hwndSelf;
1300 lpLayout->pwpos->hwndInsertAfter = 0;
1301 lpLayout->pwpos->x = lpLayout->prc->left;
1302 lpLayout->pwpos->y = lpLayout->prc->top;
1303 lpLayout->pwpos->cx = lpLayout->prc->right - lpLayout->prc->left;
1304 if (infoPtr->dwStyle & HDS_HIDDEN)
1305 lpLayout->pwpos->cy = 0;
1307 lpLayout->pwpos->cy = infoPtr->nHeight;
1308 lpLayout->prc->top += infoPtr->nHeight;
1310 lpLayout->pwpos->flags = SWP_NOZORDER;
1312 TRACE("Layout x=%d y=%d cx=%d cy=%d\n",
1313 lpLayout->pwpos->x, lpLayout->pwpos->y,
1314 lpLayout->pwpos->cx, lpLayout->pwpos->cy);
1316 infoPtr->bRectsValid = FALSE;
1323 HEADER_SetImageList (HEADER_INFO *infoPtr, HIMAGELIST himl)
1327 TRACE("(himl %p)\n", himl);
1328 himlOld = infoPtr->himl;
1329 infoPtr->himl = himl;
1331 /* FIXME: Refresh needed??? */
1333 return (LRESULT)himlOld;
1338 HEADER_GetBitmapMargin(const HEADER_INFO *infoPtr)
1340 return infoPtr->iMargin;
1344 HEADER_SetBitmapMargin(HEADER_INFO *infoPtr, INT iMargin)
1346 INT oldMargin = infoPtr->iMargin;
1348 infoPtr->iMargin = iMargin;
1354 HEADER_SetItemT (HEADER_INFO *infoPtr, INT nItem, const HDITEMW *phdi, BOOL bUnicode)
1356 HEADER_ITEM *lpItem;
1362 if ((nItem < 0) || (nItem >= (INT)infoPtr->uNumItem))
1365 TRACE("[nItem=%d]\n", nItem);
1367 HEADER_CopyHDItemForNotify(infoPtr, &hdNotify, phdi, bUnicode, &pvScratch);
1368 if (HEADER_SendNotifyWithHDItemT(infoPtr, HDN_ITEMCHANGINGW, nItem, &hdNotify))
1374 lpItem = &infoPtr->items[nItem];
1375 HEADER_StoreHDItemInHeader(lpItem, phdi->mask, phdi, bUnicode);
1377 if (phdi->mask & HDI_ORDER)
1378 if (phdi->iOrder >= 0 && phdi->iOrder < infoPtr->uNumItem)
1379 HEADER_ChangeItemOrder(infoPtr, nItem, phdi->iOrder);
1381 HEADER_SendNotifyWithHDItemT(infoPtr, HDN_ITEMCHANGEDW, nItem, &hdNotify);
1383 HEADER_SetItemBounds (infoPtr);
1385 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1391 static inline LRESULT
1392 HEADER_SetUnicodeFormat (HEADER_INFO *infoPtr, WPARAM wParam)
1394 BOOL bTemp = (infoPtr->nNotifyFormat == NFR_UNICODE);
1396 infoPtr->nNotifyFormat = ((BOOL)wParam ? NFR_UNICODE : NFR_ANSI);
1403 HEADER_Create (HWND hwnd, LPCREATESTRUCTW lpcs)
1405 HEADER_INFO *infoPtr;
1410 infoPtr = Alloc (sizeof(HEADER_INFO));
1411 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1413 infoPtr->hwndSelf = hwnd;
1414 infoPtr->hwndNotify = lpcs->hwndParent;
1415 infoPtr->uNumItem = 0;
1419 infoPtr->bRectsValid = FALSE;
1420 infoPtr->hcurArrow = LoadCursorW (0, (LPWSTR)IDC_ARROW);
1421 infoPtr->hcurDivider = LoadCursorW (COMCTL32_hModule, MAKEINTRESOURCEW(IDC_DIVIDER));
1422 infoPtr->hcurDivopen = LoadCursorW (COMCTL32_hModule, MAKEINTRESOURCEW(IDC_DIVIDEROPEN));
1423 infoPtr->bPressed = FALSE;
1424 infoPtr->bTracking = FALSE;
1425 infoPtr->dwStyle = lpcs->style;
1426 infoPtr->iMoveItem = 0;
1428 infoPtr->iHotItem = -1;
1429 infoPtr->iHotDivider = -1;
1430 infoPtr->iMargin = 3*GetSystemMetrics(SM_CXEDGE);
1431 infoPtr->nNotifyFormat =
1432 SendMessageW (infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)hwnd, NF_QUERY);
1435 hOldFont = SelectObject (hdc, GetStockObject (SYSTEM_FONT));
1436 GetTextMetricsW (hdc, &tm);
1437 infoPtr->nHeight = tm.tmHeight + VERT_BORDER;
1438 SelectObject (hdc, hOldFont);
1441 OpenThemeData(hwnd, themeClass);
1448 HEADER_Destroy (HEADER_INFO *infoPtr)
1450 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
1451 CloseThemeData(theme);
1456 HEADER_NCDestroy (HEADER_INFO *infoPtr)
1458 HEADER_ITEM *lpItem;
1461 if (infoPtr->items) {
1462 lpItem = infoPtr->items;
1463 for (nItem = 0; nItem < infoPtr->uNumItem; nItem++, lpItem++) {
1464 Free(lpItem->pszText);
1466 Free (infoPtr->items);
1469 Free(infoPtr->order);
1472 ImageList_Destroy (infoPtr->himl);
1474 SetWindowLongPtrW (infoPtr->hwndSelf, 0, 0);
1481 static inline LRESULT
1482 HEADER_GetFont (const HEADER_INFO *infoPtr)
1484 return (LRESULT)infoPtr->hFont;
1489 HEADER_IsDragDistance(const HEADER_INFO *infoPtr, const POINT *pt)
1491 /* Windows allows for a mouse movement before starting the drag. We use the
1492 * SM_CXDOUBLECLICK/SM_CYDOUBLECLICK as that distance.
1494 return (abs(infoPtr->ptLButtonDown.x - pt->x)>GetSystemMetrics(SM_CXDOUBLECLK) ||
1495 abs(infoPtr->ptLButtonDown.y - pt->y)>GetSystemMetrics(SM_CYDOUBLECLK));
1499 HEADER_LButtonDblClk (HEADER_INFO *infoPtr, INT x, INT y)
1507 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1509 if ((infoPtr->dwStyle & HDS_BUTTONS) && (flags == HHT_ONHEADER))
1510 HEADER_SendNotifyWithHDItemT(infoPtr, HDN_ITEMDBLCLICKW, nItem, NULL);
1511 else if ((flags == HHT_ONDIVIDER) || (flags == HHT_ONDIVOPEN))
1512 HEADER_SendNotifyWithHDItemT(infoPtr, HDN_DIVIDERDBLCLICKW, nItem, NULL);
1519 HEADER_LButtonDown (HEADER_INFO *infoPtr, INT x, INT y)
1528 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1530 if ((infoPtr->dwStyle & HDS_BUTTONS) && (flags == HHT_ONHEADER)) {
1531 SetCapture (infoPtr->hwndSelf);
1532 infoPtr->bCaptured = TRUE;
1533 infoPtr->bPressed = TRUE;
1534 infoPtr->bDragging = FALSE;
1535 infoPtr->iMoveItem = nItem;
1536 infoPtr->ptLButtonDown = pt;
1538 infoPtr->items[nItem].bDown = TRUE;
1540 /* Send WM_CUSTOMDRAW */
1541 hdc = GetDC (infoPtr->hwndSelf);
1542 HEADER_RefreshItem (infoPtr, nItem);
1543 ReleaseDC (infoPtr->hwndSelf, hdc);
1545 TRACE("Pressed item %d!\n", nItem);
1547 else if ((flags == HHT_ONDIVIDER) || (flags == HHT_ONDIVOPEN)) {
1548 INT iCurrWidth = infoPtr->items[nItem].cxy;
1549 if (!HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_BEGINTRACKW, nItem, HDI_WIDTH, iCurrWidth))
1551 SetCapture (infoPtr->hwndSelf);
1552 infoPtr->bCaptured = TRUE;
1553 infoPtr->bTracking = TRUE;
1554 infoPtr->iMoveItem = nItem;
1555 infoPtr->xTrackOffset = infoPtr->items[nItem].rect.right - pt.x;
1557 if (!(infoPtr->dwStyle & HDS_FULLDRAG)) {
1558 infoPtr->xOldTrack = infoPtr->items[nItem].rect.right;
1559 hdc = GetDC (infoPtr->hwndSelf);
1560 HEADER_DrawTrackLine (infoPtr, hdc, infoPtr->xOldTrack);
1561 ReleaseDC (infoPtr->hwndSelf, hdc);
1564 TRACE("Begin tracking item %d!\n", nItem);
1573 HEADER_LButtonUp (HEADER_INFO *infoPtr, INT x, INT y)
1582 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1584 if (infoPtr->bPressed) {
1585 if (infoPtr->bDragging)
1587 HEADER_ITEM *lpItem = &infoPtr->items[infoPtr->iMoveItem];
1590 ImageList_DragShowNolock(FALSE);
1591 ImageList_EndDrag();
1592 lpItem->bDown=FALSE;
1594 if (infoPtr->iHotDivider == -1)
1596 else if (infoPtr->iHotDivider == infoPtr->uNumItem)
1597 iNewOrder = infoPtr->uNumItem-1;
1600 iNewOrder = HEADER_IndexToOrder(infoPtr, infoPtr->iHotDivider);
1601 if (iNewOrder > lpItem->iOrder)
1605 if (iNewOrder != -1 &&
1606 !HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ENDDRAG, infoPtr->iMoveItem, HDI_ORDER, iNewOrder))
1608 HEADER_ChangeItemOrder(infoPtr, infoPtr->iMoveItem, iNewOrder);
1609 infoPtr->bRectsValid = FALSE;
1610 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1613 InvalidateRect(infoPtr->hwndSelf, &infoPtr->items[infoPtr->iMoveItem].rect, FALSE);
1615 infoPtr->bDragging = FALSE;
1616 HEADER_SetHotDivider(infoPtr, FALSE, -1);
1618 else if (!(infoPtr->dwStyle & HDS_DRAGDROP) || !HEADER_IsDragDistance(infoPtr, &pt))
1620 infoPtr->items[infoPtr->iMoveItem].bDown = FALSE;
1621 hdc = GetDC (infoPtr->hwndSelf);
1622 HEADER_RefreshItem (infoPtr, infoPtr->iMoveItem);
1623 ReleaseDC (infoPtr->hwndSelf, hdc);
1625 HEADER_SendNotifyWithHDItemT(infoPtr, HDN_ITEMCLICKW, infoPtr->iMoveItem, NULL);
1628 TRACE("Released item %d!\n", infoPtr->iMoveItem);
1629 infoPtr->bPressed = FALSE;
1631 else if (infoPtr->bTracking) {
1632 INT iNewWidth = pt.x - infoPtr->items[infoPtr->iMoveItem].rect.left + infoPtr->xTrackOffset;
1635 TRACE("End tracking item %d!\n", infoPtr->iMoveItem);
1636 infoPtr->bTracking = FALSE;
1638 HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ENDTRACKW, infoPtr->iMoveItem, HDI_WIDTH, iNewWidth);
1640 if (!(infoPtr->dwStyle & HDS_FULLDRAG)) {
1641 hdc = GetDC (infoPtr->hwndSelf);
1642 HEADER_DrawTrackLine (infoPtr, hdc, infoPtr->xOldTrack);
1643 ReleaseDC (infoPtr->hwndSelf, hdc);
1646 if (!HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ITEMCHANGINGW, infoPtr->iMoveItem, HDI_WIDTH, iNewWidth))
1648 infoPtr->items[infoPtr->iMoveItem].cxy = iNewWidth;
1649 HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ITEMCHANGEDW, infoPtr->iMoveItem, HDI_WIDTH, iNewWidth);
1652 HEADER_SetItemBounds (infoPtr);
1653 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1656 if (infoPtr->bCaptured) {
1657 infoPtr->bCaptured = FALSE;
1659 HEADER_SendSimpleNotify (infoPtr, NM_RELEASEDCAPTURE);
1667 HEADER_NotifyFormat (HEADER_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1672 return infoPtr->nNotifyFormat;
1675 infoPtr->nNotifyFormat =
1676 SendMessageW ((HWND)wParam, WM_NOTIFYFORMAT,
1677 (WPARAM)infoPtr->hwndSelf, (LPARAM)NF_QUERY);
1678 return infoPtr->nNotifyFormat;
1685 HEADER_MouseLeave (HEADER_INFO *infoPtr)
1687 /* Reset hot-tracked item when mouse leaves control. */
1688 INT oldHotItem = infoPtr->iHotItem;
1689 HDC hdc = GetDC (infoPtr->hwndSelf);
1691 infoPtr->iHotItem = -1;
1692 if (oldHotItem != -1) HEADER_RefreshItem (infoPtr, oldHotItem);
1693 ReleaseDC (infoPtr->hwndSelf, hdc);
1700 HEADER_MouseMove (HEADER_INFO *infoPtr, LPARAM lParam)
1706 /* With theming, hottracking is always enabled */
1707 BOOL hotTrackEnabled =
1708 ((infoPtr->dwStyle & HDS_BUTTONS) && (infoPtr->dwStyle & HDS_HOTTRACK))
1709 || (GetWindowTheme (infoPtr->hwndSelf) != NULL);
1710 INT oldHotItem = infoPtr->iHotItem;
1712 pt.x = (INT)(SHORT)LOWORD(lParam);
1713 pt.y = (INT)(SHORT)HIWORD(lParam);
1714 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1716 if (hotTrackEnabled) {
1717 if (flags & (HHT_ONHEADER | HHT_ONDIVIDER | HHT_ONDIVOPEN))
1718 infoPtr->iHotItem = nItem;
1720 infoPtr->iHotItem = -1;
1723 if (infoPtr->bCaptured) {
1724 /* check if we should drag the header */
1725 if (infoPtr->bPressed && !infoPtr->bDragging && (infoPtr->dwStyle & HDS_DRAGDROP)
1726 && HEADER_IsDragDistance(infoPtr, &pt))
1728 if (!HEADER_SendNotifyWithHDItemT(infoPtr, HDN_BEGINDRAG, infoPtr->iMoveItem, NULL))
1730 HIMAGELIST hDragItem = (HIMAGELIST)HEADER_CreateDragImage(infoPtr, infoPtr->iMoveItem);
1731 if (hDragItem != NULL)
1733 HEADER_ITEM *lpItem = &infoPtr->items[infoPtr->iMoveItem];
1734 TRACE("Starting item drag\n");
1735 ImageList_BeginDrag(hDragItem, 0, pt.x - lpItem->rect.left, 0);
1736 ImageList_DragShowNolock(TRUE);
1737 ImageList_Destroy(hDragItem);
1738 infoPtr->bDragging = TRUE;
1743 if (infoPtr->bDragging)
1748 ClientToScreen(infoPtr->hwndSelf, &drag);
1749 ImageList_DragMove(drag.x, drag.y);
1750 HEADER_SetHotDivider(infoPtr, TRUE, lParam);
1753 if (infoPtr->bPressed && !infoPtr->bDragging) {
1754 BOOL oldState = infoPtr->items[infoPtr->iMoveItem].bDown;
1755 if ((nItem == infoPtr->iMoveItem) && (flags == HHT_ONHEADER))
1756 infoPtr->items[infoPtr->iMoveItem].bDown = TRUE;
1758 infoPtr->items[infoPtr->iMoveItem].bDown = FALSE;
1759 if (oldState != infoPtr->items[infoPtr->iMoveItem].bDown) {
1760 hdc = GetDC (infoPtr->hwndSelf);
1761 HEADER_RefreshItem (infoPtr, infoPtr->iMoveItem);
1762 ReleaseDC (infoPtr->hwndSelf, hdc);
1765 TRACE("Moving pressed item %d!\n", infoPtr->iMoveItem);
1767 else if (infoPtr->bTracking) {
1768 if (infoPtr->dwStyle & HDS_FULLDRAG) {
1769 HEADER_ITEM *lpItem = &infoPtr->items[infoPtr->iMoveItem];
1770 nWidth = pt.x - lpItem->rect.left + infoPtr->xTrackOffset;
1771 if (!HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ITEMCHANGINGW, infoPtr->iMoveItem, HDI_WIDTH, nWidth))
1773 INT nOldWidth = lpItem->rect.right - lpItem->rect.left;
1777 if (nWidth < 0) nWidth = 0;
1778 infoPtr->items[infoPtr->iMoveItem].cxy = nWidth;
1779 HEADER_SetItemBounds(infoPtr);
1781 GetClientRect(infoPtr->hwndSelf, &rcClient);
1782 rcScroll = rcClient;
1783 rcScroll.left = lpItem->rect.left + nOldWidth;
1784 ScrollWindowEx(infoPtr->hwndSelf, nWidth - nOldWidth, 0, &rcScroll, &rcClient, NULL, NULL, 0);
1785 InvalidateRect(infoPtr->hwndSelf, &lpItem->rect, FALSE);
1786 UpdateWindow(infoPtr->hwndSelf);
1788 HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ITEMCHANGEDW, infoPtr->iMoveItem, HDI_WIDTH, nWidth);
1793 hdc = GetDC (infoPtr->hwndSelf);
1794 HEADER_DrawTrackLine (infoPtr, hdc, infoPtr->xOldTrack);
1795 infoPtr->xOldTrack = pt.x + infoPtr->xTrackOffset;
1796 if (infoPtr->xOldTrack < infoPtr->items[infoPtr->iMoveItem].rect.left)
1797 infoPtr->xOldTrack = infoPtr->items[infoPtr->iMoveItem].rect.left;
1798 HEADER_DrawTrackLine (infoPtr, hdc, infoPtr->xOldTrack);
1799 ReleaseDC (infoPtr->hwndSelf, hdc);
1800 iTrackWidth = infoPtr->xOldTrack - infoPtr->items[infoPtr->iMoveItem].rect.left;
1801 /* FIXME: should stop tracking if HDN_TRACK returns TRUE */
1802 HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_TRACKW, infoPtr->iMoveItem, HDI_WIDTH, iTrackWidth);
1805 TRACE("Tracking item %d!\n", infoPtr->iMoveItem);
1809 if (hotTrackEnabled) {
1810 TRACKMOUSEEVENT tme;
1811 if (oldHotItem != infoPtr->iHotItem && !infoPtr->bDragging) {
1812 hdc = GetDC (infoPtr->hwndSelf);
1813 if (oldHotItem != -1) HEADER_RefreshItem (infoPtr, oldHotItem);
1814 if (infoPtr->iHotItem != -1) HEADER_RefreshItem (infoPtr, infoPtr->iHotItem);
1815 ReleaseDC (infoPtr->hwndSelf, hdc);
1817 tme.cbSize = sizeof( tme );
1818 tme.dwFlags = TME_LEAVE;
1819 tme.hwndTrack = infoPtr->hwndSelf;
1820 TrackMouseEvent( &tme );
1828 HEADER_Paint (HEADER_INFO *infoPtr, HDC hdcParam)
1833 hdc = hdcParam==0 ? BeginPaint (infoPtr->hwndSelf, &ps) : hdcParam;
1834 HEADER_Refresh (infoPtr, hdc);
1836 EndPaint (infoPtr->hwndSelf, &ps);
1842 HEADER_RButtonUp (HEADER_INFO *infoPtr, INT x, INT y)
1850 /* Send a Notify message */
1851 bRet = HEADER_SendSimpleNotify (infoPtr, NM_RCLICK);
1853 /* Change to screen coordinate for WM_CONTEXTMENU */
1854 ClientToScreen(infoPtr->hwndSelf, &pt);
1856 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
1857 SendMessageW( infoPtr->hwndSelf, WM_CONTEXTMENU, (WPARAM) infoPtr->hwndSelf, MAKELPARAM(pt.x, pt.y));
1864 HEADER_SetCursor (HEADER_INFO *infoPtr, LPARAM lParam)
1870 TRACE("code=0x%X id=0x%X\n", LOWORD(lParam), HIWORD(lParam));
1873 ScreenToClient (infoPtr->hwndSelf, &pt);
1875 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1877 if (flags == HHT_ONDIVIDER)
1878 SetCursor (infoPtr->hcurDivider);
1879 else if (flags == HHT_ONDIVOPEN)
1880 SetCursor (infoPtr->hcurDivopen);
1882 SetCursor (infoPtr->hcurArrow);
1889 HEADER_SetFont (HEADER_INFO *infoPtr, HFONT hFont, WORD Redraw)
1895 infoPtr->hFont = hFont;
1898 hOldFont = SelectObject (hdc, infoPtr->hFont ? infoPtr->hFont : GetStockObject (SYSTEM_FONT));
1899 GetTextMetricsW (hdc, &tm);
1900 infoPtr->nHeight = tm.tmHeight + VERT_BORDER;
1901 SelectObject (hdc, hOldFont);
1904 infoPtr->bRectsValid = FALSE;
1907 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1913 static LRESULT HEADER_SetRedraw(HEADER_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1915 /* ignoring the InvalidateRect calls is handled by user32. But some apps expect
1916 * that we invalidate the header and this has to be done manually */
1919 ret = DefWindowProcW(infoPtr->hwndSelf, WM_SETREDRAW, wParam, lParam);
1921 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1925 static INT HEADER_StyleChanged(HEADER_INFO *infoPtr, WPARAM wStyleType,
1926 const LPSTYLESTRUCT lpss)
1928 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
1929 wStyleType, lpss->styleOld, lpss->styleNew);
1931 if (wStyleType != GWL_STYLE) return 0;
1933 infoPtr->dwStyle = lpss->styleNew;
1938 /* Update the theme handle after a theme change */
1939 static LRESULT HEADER_ThemeChanged(const HEADER_INFO *infoPtr)
1941 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
1942 CloseThemeData(theme);
1943 OpenThemeData(infoPtr->hwndSelf, themeClass);
1944 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1949 static LRESULT WINAPI
1950 HEADER_WindowProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
1952 HEADER_INFO *infoPtr = (HEADER_INFO *)GetWindowLongPtrW(hwnd, 0);
1954 TRACE("hwnd=%p msg=%x wparam=%lx lParam=%lx\n", hwnd, msg, wParam, lParam);
1955 if (!infoPtr && (msg != WM_CREATE))
1956 return DefWindowProcW (hwnd, msg, wParam, lParam);
1958 /* case HDM_CLEARFILTER: */
1960 case HDM_CREATEDRAGIMAGE:
1961 return HEADER_CreateDragImage (infoPtr, (INT)wParam);
1963 case HDM_DELETEITEM:
1964 return HEADER_DeleteItem (infoPtr, (INT)wParam);
1966 /* case HDM_EDITFILTER: */
1968 case HDM_GETBITMAPMARGIN:
1969 return HEADER_GetBitmapMargin(infoPtr);
1971 case HDM_GETIMAGELIST:
1972 return HEADER_GetImageList (infoPtr);
1976 return HEADER_GetItemT (infoPtr, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_GETITEMW);
1978 case HDM_GETITEMCOUNT:
1979 return HEADER_GetItemCount (infoPtr);
1981 case HDM_GETITEMRECT:
1982 return HEADER_GetItemRect (infoPtr, (INT)wParam, (LPRECT)lParam);
1984 case HDM_GETORDERARRAY:
1985 return HEADER_GetOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
1987 case HDM_GETUNICODEFORMAT:
1988 return HEADER_GetUnicodeFormat (infoPtr);
1991 return HEADER_HitTest (infoPtr, (LPHDHITTESTINFO)lParam);
1993 case HDM_INSERTITEMA:
1994 case HDM_INSERTITEMW:
1995 return HEADER_InsertItemT (infoPtr, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_INSERTITEMW);
1998 return HEADER_Layout (infoPtr, (LPHDLAYOUT)lParam);
2000 case HDM_ORDERTOINDEX:
2001 return HEADER_OrderToIndex(infoPtr, (INT)wParam);
2003 case HDM_SETBITMAPMARGIN:
2004 return HEADER_SetBitmapMargin(infoPtr, (INT)wParam);
2006 /* case HDM_SETFILTERCHANGETIMEOUT: */
2008 case HDM_SETHOTDIVIDER:
2009 return HEADER_SetHotDivider(infoPtr, wParam, lParam);
2011 case HDM_SETIMAGELIST:
2012 return HEADER_SetImageList (infoPtr, (HIMAGELIST)lParam);
2016 return HEADER_SetItemT (infoPtr, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_SETITEMW);
2018 case HDM_SETORDERARRAY:
2019 return HEADER_SetOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
2021 case HDM_SETUNICODEFORMAT:
2022 return HEADER_SetUnicodeFormat (infoPtr, wParam);
2025 return HEADER_Create (hwnd, (LPCREATESTRUCTW)lParam);
2028 return HEADER_Destroy (infoPtr);
2031 return HEADER_NCDestroy (infoPtr);
2037 return DLGC_WANTTAB | DLGC_WANTARROWS;
2040 return HEADER_GetFont (infoPtr);
2042 case WM_LBUTTONDBLCLK:
2043 return HEADER_LButtonDblClk (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2045 case WM_LBUTTONDOWN:
2046 return HEADER_LButtonDown (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2049 return HEADER_LButtonUp (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2052 return HEADER_MouseLeave (infoPtr);
2055 return HEADER_MouseMove (infoPtr, lParam);
2057 case WM_NOTIFYFORMAT:
2058 return HEADER_NotifyFormat (infoPtr, wParam, lParam);
2061 return HEADER_Size (infoPtr);
2063 case WM_THEMECHANGED:
2064 return HEADER_ThemeChanged (infoPtr);
2066 case WM_PRINTCLIENT:
2068 return HEADER_Paint (infoPtr, (HDC)wParam);
2071 return HEADER_RButtonUp (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2074 return HEADER_SetCursor (infoPtr, lParam);
2077 return HEADER_SetFont (infoPtr, (HFONT)wParam, (WORD)lParam);
2080 return HEADER_SetRedraw(infoPtr, wParam, lParam);
2082 case WM_STYLECHANGED:
2083 return HEADER_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2085 case WM_SYSCOLORCHANGE:
2086 COMCTL32_RefreshSysColors();
2090 if ((msg >= WM_USER) && (msg < WM_APP) && !COMCTL32_IsReflectedMessage(msg))
2091 ERR("unknown msg %04x wp=%04lx lp=%08lx\n",
2092 msg, wParam, lParam );
2093 return DefWindowProcW(hwnd, msg, wParam, lParam);
2099 HEADER_Register (void)
2103 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
2104 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
2105 wndClass.lpfnWndProc = HEADER_WindowProc;
2106 wndClass.cbClsExtra = 0;
2107 wndClass.cbWndExtra = sizeof(HEADER_INFO *);
2108 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
2109 wndClass.lpszClassName = WC_HEADERW;
2111 RegisterClassW (&wndClass);
2116 HEADER_Unregister (void)
2118 UnregisterClassW (WC_HEADERW, NULL);