ddraw/tests: Remove p8_primary_test.
[wine.git] / dlls / comctl32 / header.c
blobedfe385964f0cca76b67c9cd4abbbcc99ba1cf1b
1 /*
2 * Header control
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
23 * TODO:
24 * - Imagelist support (completed?)
25 * - Hottrack support (completed?)
26 * - Filters support (HDS_FILTER, HDI_FILTER, HDM_*FILTER*, HDN_*FILTER*)
27 * - New Windows Vista features
30 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <string.h>
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wine/unicode.h"
37 #include "wingdi.h"
38 #include "winuser.h"
39 #include "winnls.h"
40 #include "commctrl.h"
41 #include "comctl32.h"
42 #include "vssym32.h"
43 #include "uxtheme.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(header);
48 typedef struct
50 INT cxy;
51 HBITMAP hbm;
52 LPWSTR pszText;
53 INT fmt;
54 LPARAM lParam;
55 INT iImage;
56 INT iOrder; /* see documentation of HD_ITEM */
58 BOOL bDown; /* is item pressed? (used for drawing) */
59 RECT rect; /* bounding rectangle of the item */
60 DWORD callbackMask; /* HDI_* flags for items that are callback */
61 } HEADER_ITEM;
64 typedef struct
66 HWND hwndSelf; /* Control window */
67 HWND hwndNotify; /* Owner window to send notifications to */
68 INT nNotifyFormat; /* format used for WM_NOTIFY messages */
69 UINT uNumItem; /* number of items (columns) */
70 INT nHeight; /* height of the header (pixels) */
71 HFONT hFont; /* handle to the current font */
72 HCURSOR hcurArrow; /* handle to the arrow cursor */
73 HCURSOR hcurDivider; /* handle to a cursor (used over dividers) <-|-> */
74 HCURSOR hcurDivopen; /* handle to a cursor (used over dividers) <-||-> */
75 BOOL bCaptured; /* Is the mouse captured? */
76 BOOL bPressed; /* Is a header item pressed (down)? */
77 BOOL bDragging; /* Are we dragging an item? */
78 BOOL bTracking; /* Is in tracking mode? */
79 POINT ptLButtonDown; /* The point where the left button was pressed */
80 DWORD dwStyle; /* the cached window GWL_STYLE */
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 */
87 INT filter_change_timeout; /* change timeout set with HDM_SETFILTERCHANGETIMEOUT */
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 */
93 } HEADER_INFO;
96 #define VERT_BORDER 4
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;
138 else
139 lpItem->callbackMask &= ~HDI_IMAGE;
142 if (mask & HDI_TEXT)
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);
152 if (fUnicode)
153 Str_SetPtrW(&lpItem->pszText, pszText);
154 else
155 Str_SetPtrAtoW(&lpItem->pszText, (LPCSTR)pszText);
156 lpItem->callbackMask &= ~HDI_TEXT;
158 else
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;
174 static INT
175 HEADER_OrderToIndex(const HEADER_INFO *infoPtr, INT iorder)
177 if ((iorder <0) || iorder >= infoPtr->uNumItem)
178 return iorder;
179 return infoPtr->order[iorder];
182 static void
183 HEADER_ChangeItemOrder(const HEADER_INFO *infoPtr, INT iItem, INT iNewOrder)
185 HEADER_ITEM *lpItem = &infoPtr->items[iItem];
186 INT i, nMin, nMax;
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 */
209 static INT
210 HEADER_NextItem(const HEADER_INFO *infoPtr, INT iItem)
212 return HEADER_OrderToIndex(infoPtr, HEADER_IndexToOrder(infoPtr, iItem)+1);
215 static INT
216 HEADER_PrevItem(const HEADER_INFO *infoPtr, INT iItem)
218 return HEADER_OrderToIndex(infoPtr, HEADER_IndexToOrder(infoPtr, iItem)-1);
221 /* TRUE when item is not resizable with dividers,
222 note that valid index should be supplied */
223 static inline BOOL
224 HEADER_IsItemFixed(const HEADER_INFO *infoPtr, INT iItem)
226 return (infoPtr->dwStyle & HDS_NOSIZING) || (infoPtr->items[iItem].fmt & HDF_FIXEDWIDTH);
229 static void
230 HEADER_SetItemBounds (HEADER_INFO *infoPtr)
232 HEADER_ITEM *phdi;
233 RECT rect;
234 unsigned int i;
235 int x;
237 infoPtr->bRectsValid = TRUE;
239 if (infoPtr->uNumItem == 0)
240 return;
242 GetClientRect (infoPtr->hwndSelf, &rect);
244 x = rect.left;
245 for (i = 0; i < infoPtr->uNumItem; i++) {
246 phdi = &infoPtr->items[HEADER_OrderToIndex(infoPtr,i)];
247 phdi->rect.top = rect.top;
248 phdi->rect.bottom = rect.bottom;
249 phdi->rect.left = x;
250 phdi->rect.right = phdi->rect.left + ((phdi->cxy>0)?phdi->cxy:0);
251 x = phdi->rect.right;
255 static LRESULT
256 HEADER_Size (HEADER_INFO *infoPtr)
258 HEADER_SetItemBounds(infoPtr);
259 return 0;
262 static void HEADER_GetHotDividerRect(const HEADER_INFO *infoPtr, RECT *r)
264 INT iDivider = infoPtr->iHotDivider;
265 if (infoPtr->uNumItem > 0)
267 HEADER_ITEM *lpItem;
269 if (iDivider < infoPtr->uNumItem)
271 lpItem = &infoPtr->items[iDivider];
272 r->left = lpItem->rect.left - HOT_DIVIDER_WIDTH/2;
273 r->right = lpItem->rect.left + HOT_DIVIDER_WIDTH/2;
275 else
277 lpItem = &infoPtr->items[HEADER_OrderToIndex(infoPtr, infoPtr->uNumItem-1)];
278 r->left = lpItem->rect.right - HOT_DIVIDER_WIDTH/2;
279 r->right = lpItem->rect.right + HOT_DIVIDER_WIDTH/2;
281 r->top = lpItem->rect.top;
282 r->bottom = lpItem->rect.bottom;
284 else
286 RECT clientRect;
287 GetClientRect(infoPtr->hwndSelf, &clientRect);
288 *r = clientRect;
289 r->right = r->left + HOT_DIVIDER_WIDTH/2;
293 static void
294 HEADER_FillItemFrame(HEADER_INFO *infoPtr, HDC hdc, RECT *r, const HEADER_ITEM *item, BOOL hottrack)
296 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
298 if (theme) {
299 int state = (item->bDown) ? HIS_PRESSED : (hottrack ? HIS_HOT : HIS_NORMAL);
300 DrawThemeBackground (theme, hdc, HP_HEADERITEM, state, r, NULL);
301 GetThemeBackgroundContentRect (theme, hdc, HP_HEADERITEM, state, r, r);
303 else
305 HBRUSH hbr = CreateSolidBrush(GetBkColor(hdc));
306 FillRect(hdc, r, hbr);
307 DeleteObject(hbr);
311 static void
312 HEADER_DrawItemFrame(HEADER_INFO *infoPtr, HDC hdc, RECT *r, const HEADER_ITEM *item)
314 if (GetWindowTheme(infoPtr->hwndSelf)) return;
316 if (!(infoPtr->dwStyle & HDS_FLAT))
318 if (infoPtr->dwStyle & HDS_BUTTONS) {
319 if (item->bDown)
320 DrawEdge (hdc, r, BDR_RAISEDOUTER, BF_RECT | BF_FLAT | BF_ADJUST);
321 else
322 DrawEdge (hdc, r, EDGE_RAISED, BF_RECT | BF_SOFT | BF_ADJUST);
324 else
325 DrawEdge (hdc, r, EDGE_ETCHED, BF_BOTTOM | BF_RIGHT | BF_ADJUST);
329 static INT
330 HEADER_DrawItem (HEADER_INFO *infoPtr, HDC hdc, INT iItem, BOOL bHotTrack, LRESULT lCDFlags)
332 HEADER_ITEM *phdi = &infoPtr->items[iItem];
333 RECT r;
334 INT oldBkMode;
335 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
336 NMCUSTOMDRAW nmcd;
337 int state = 0;
339 TRACE("DrawItem(iItem %d bHotTrack %d unicode flag %d)\n", iItem, bHotTrack, (infoPtr->nNotifyFormat == NFR_UNICODE));
341 r = phdi->rect;
342 if (r.right - r.left == 0)
343 return phdi->rect.right;
345 if (theme)
346 state = (phdi->bDown) ? HIS_PRESSED : (bHotTrack ? HIS_HOT : HIS_NORMAL);
348 /* Set the colors before sending NM_CUSTOMDRAW so that it can change them */
349 SetTextColor(hdc, (bHotTrack && !theme) ? COLOR_HIGHLIGHT : COLOR_BTNTEXT);
350 SetBkColor(hdc, comctl32_color.clr3dFace);
352 if (lCDFlags & CDRF_NOTIFYITEMDRAW && !(phdi->fmt & HDF_OWNERDRAW))
354 LRESULT lCDItemFlags;
356 nmcd.dwDrawStage = CDDS_PREPAINT | CDDS_ITEM;
357 nmcd.hdc = hdc;
358 nmcd.dwItemSpec = iItem;
359 nmcd.rc = r;
360 nmcd.uItemState = phdi->bDown ? CDIS_SELECTED : 0;
361 nmcd.lItemlParam = phdi->lParam;
363 lCDItemFlags = HEADER_SendNotify(infoPtr, NM_CUSTOMDRAW, (NMHDR *)&nmcd);
364 if (lCDItemFlags & CDRF_SKIPDEFAULT)
365 return phdi->rect.right;
368 /* Fill background, owner could draw over it. */
369 HEADER_FillItemFrame(infoPtr, hdc, &r, phdi, bHotTrack);
371 if (phdi->fmt & HDF_OWNERDRAW)
373 DRAWITEMSTRUCT dis;
374 BOOL ret;
376 dis.CtlType = ODT_HEADER;
377 dis.CtlID = GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_ID);
378 dis.itemID = iItem;
379 dis.itemAction = ODA_DRAWENTIRE;
380 dis.itemState = phdi->bDown ? ODS_SELECTED : 0;
381 dis.hwndItem = infoPtr->hwndSelf;
382 dis.hDC = hdc;
383 dis.rcItem = phdi->rect;
384 dis.itemData = phdi->lParam;
385 oldBkMode = SetBkMode(hdc, TRANSPARENT);
386 ret = SendMessageW (infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
387 if (oldBkMode != TRANSPARENT)
388 SetBkMode(hdc, oldBkMode);
390 if (!ret)
391 HEADER_FillItemFrame(infoPtr, hdc, &r, phdi, bHotTrack);
393 /* Edges are always drawn if we don't have attached theme. */
394 HEADER_DrawItemFrame(infoPtr, hdc, &r, phdi);
395 /* If application processed WM_DRAWITEM we should skip label painting,
396 edges are drawn no matter what. */
397 if (ret) return phdi->rect.right;
399 else
400 HEADER_DrawItemFrame(infoPtr, hdc, &r, phdi);
402 if (phdi->bDown) {
403 r.left += 2;
404 r.top += 2;
407 /* Now text and image */
409 UINT rw, rh, /* width and height of r */
410 *x = NULL, *w = NULL; /* x and width of the pic (bmp or img) which is part of cnt */
411 /* cnt,txt,img,bmp */
412 UINT cx, tx, ix, bx,
413 cw, tw, iw, bw;
414 INT img_cx, img_cy;
415 BITMAP bmp;
417 HEADER_PrepareCallbackItems(infoPtr, iItem, HDI_TEXT|HDI_IMAGE);
418 cw = iw = bw = 0;
419 rw = r.right - r.left;
420 rh = r.bottom - r.top;
422 if (phdi->fmt & HDF_STRING) {
423 RECT textRect;
425 SetRectEmpty(&textRect);
427 if (theme) {
428 GetThemeTextExtent(theme, hdc, HP_HEADERITEM, state, phdi->pszText, -1,
429 DT_LEFT|DT_VCENTER|DT_SINGLELINE, NULL, &textRect);
430 } else {
431 DrawTextW (hdc, phdi->pszText, -1,
432 &textRect, DT_LEFT|DT_VCENTER|DT_SINGLELINE|DT_CALCRECT);
434 cw = textRect.right - textRect.left + 2 * infoPtr->iMargin;
437 if ((phdi->fmt & HDF_IMAGE) && ImageList_GetIconSize( infoPtr->himl, &img_cx, &img_cy )) {
438 iw = img_cx + 2 * infoPtr->iMargin;
439 x = &ix;
440 w = &iw;
443 if ((phdi->fmt & HDF_BITMAP) && (phdi->hbm)) {
444 GetObjectW (phdi->hbm, sizeof(BITMAP), &bmp);
445 bw = bmp.bmWidth + 2 * infoPtr->iMargin;
446 if (!iw) {
447 x = &bx;
448 w = &bw;
452 if (bw || iw)
453 cw += *w;
455 /* align cx using the unclipped cw */
456 if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_LEFT)
457 cx = r.left;
458 else if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_CENTER)
459 cx = r.left + rw / 2 - cw / 2;
460 else /* HDF_RIGHT */
461 cx = r.right - cw;
463 /* clip cx & cw */
464 if (cx < r.left)
465 cx = r.left;
466 if (cx + cw > r.right)
467 cw = r.right - cx;
469 tx = cx + infoPtr->iMargin;
470 /* since cw might have changed we have to recalculate tw */
471 tw = cw - infoPtr->iMargin * 2;
473 if (iw || bw) {
474 tw -= *w;
475 if (phdi->fmt & HDF_BITMAP_ON_RIGHT) {
476 /* put pic behind text */
477 *x = cx + tw + infoPtr->iMargin * 3;
478 } else {
479 *x = cx + infoPtr->iMargin;
480 /* move text behind pic */
481 tx += *w;
485 if (iw && bw) {
486 /* since we're done with the layout we can
487 now calculate the position of bmp which
488 has no influence on alignment and layout
489 because of img */
490 if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_RIGHT)
491 bx = cx - bw + infoPtr->iMargin;
492 else
493 bx = cx + cw + infoPtr->iMargin;
496 if (iw || bw) {
497 HDC hClipDC = GetDC(infoPtr->hwndSelf);
498 HRGN hClipRgn = CreateRectRgn(r.left, r.top, r.right, r.bottom);
499 SelectClipRgn(hClipDC, hClipRgn);
501 if (bw) {
502 HDC hdcBitmap = CreateCompatibleDC (hClipDC);
503 SelectObject (hdcBitmap, phdi->hbm);
504 BitBlt (hClipDC, bx, r.top + ((INT)rh - bmp.bmHeight) / 2,
505 bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
506 DeleteDC (hdcBitmap);
509 if (iw) {
510 ImageList_DrawEx (infoPtr->himl, phdi->iImage, hClipDC,
511 ix, r.top + ((INT)rh - img_cy) / 2,
512 img_cx, img_cy, CLR_DEFAULT, CLR_DEFAULT, 0);
515 DeleteObject(hClipRgn);
516 ReleaseDC(infoPtr->hwndSelf, hClipDC);
519 if (((phdi->fmt & HDF_STRING)
520 || (!(phdi->fmt & (HDF_OWNERDRAW|HDF_STRING|HDF_BITMAP|
521 HDF_BITMAP_ON_RIGHT|HDF_IMAGE)))) /* no explicit format specified? */
522 && (phdi->pszText)) {
523 oldBkMode = SetBkMode(hdc, TRANSPARENT);
524 r.left = tx;
525 r.right = tx + tw;
526 if (theme) {
527 DrawThemeText(theme, hdc, HP_HEADERITEM, state, phdi->pszText,
528 -1, DT_LEFT|DT_END_ELLIPSIS|DT_VCENTER|DT_SINGLELINE,
529 0, &r);
530 } else {
531 DrawTextW (hdc, phdi->pszText, -1,
532 &r, DT_LEFT|DT_END_ELLIPSIS|DT_VCENTER|DT_SINGLELINE);
534 if (oldBkMode != TRANSPARENT)
535 SetBkMode(hdc, oldBkMode);
537 HEADER_FreeCallbackItems(phdi);
540 return phdi->rect.right;
543 static void
544 HEADER_DrawHotDivider(const HEADER_INFO *infoPtr, HDC hdc)
546 HBRUSH brush;
547 RECT r;
549 HEADER_GetHotDividerRect(infoPtr, &r);
550 brush = CreateSolidBrush(comctl32_color.clrHighlight);
551 FillRect(hdc, &r, brush);
552 DeleteObject(brush);
555 static void
556 HEADER_Refresh (HEADER_INFO *infoPtr, HDC hdc)
558 HFONT hFont, hOldFont;
559 RECT rect, rcRest;
560 HBRUSH hbrBk;
561 UINT i;
562 INT x;
563 LRESULT lCDFlags;
564 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
566 if (!infoPtr->bRectsValid)
567 HEADER_SetItemBounds(infoPtr);
569 /* get rect for the bar, adjusted for the border */
570 GetClientRect (infoPtr->hwndSelf, &rect);
571 lCDFlags = HEADER_SendCtrlCustomDraw(infoPtr, CDDS_PREPAINT, hdc, &rect);
573 if (infoPtr->bDragging)
574 ImageList_DragShowNolock(FALSE);
576 hFont = infoPtr->hFont ? infoPtr->hFont : GetStockObject (SYSTEM_FONT);
577 hOldFont = SelectObject (hdc, hFont);
579 /* draw Background */
580 if (infoPtr->uNumItem == 0 && theme == NULL) {
581 hbrBk = GetSysColorBrush(COLOR_3DFACE);
582 FillRect(hdc, &rect, hbrBk);
585 x = rect.left;
586 for (i = 0; x <= rect.right && i < infoPtr->uNumItem; i++) {
587 int idx = HEADER_OrderToIndex(infoPtr,i);
588 if (RectVisible(hdc, &infoPtr->items[idx].rect))
589 HEADER_DrawItem(infoPtr, hdc, idx, infoPtr->iHotItem == idx, lCDFlags);
590 x = infoPtr->items[idx].rect.right;
593 rcRest = rect;
594 rcRest.left = x;
595 if ((x <= rect.right) && RectVisible(hdc, &rcRest) && (infoPtr->uNumItem > 0)) {
596 if (theme != NULL) {
597 DrawThemeBackground(theme, hdc, HP_HEADERITEM, HIS_NORMAL, &rcRest, NULL);
599 else if (infoPtr->dwStyle & HDS_FLAT) {
600 hbrBk = GetSysColorBrush(COLOR_3DFACE);
601 FillRect(hdc, &rcRest, hbrBk);
603 else
605 if (infoPtr->dwStyle & HDS_BUTTONS)
606 DrawEdge (hdc, &rcRest, EDGE_RAISED, BF_TOP|BF_LEFT|BF_BOTTOM|BF_SOFT|BF_MIDDLE);
607 else
608 DrawEdge (hdc, &rcRest, EDGE_ETCHED, BF_BOTTOM|BF_MIDDLE);
612 if (infoPtr->iHotDivider != -1)
613 HEADER_DrawHotDivider(infoPtr, hdc);
615 if (infoPtr->bDragging)
616 ImageList_DragShowNolock(TRUE);
617 SelectObject (hdc, hOldFont);
619 if (lCDFlags & CDRF_NOTIFYPOSTPAINT)
620 HEADER_SendCtrlCustomDraw(infoPtr, CDDS_POSTPAINT, hdc, &rect);
624 static void
625 HEADER_RefreshItem (HEADER_INFO *infoPtr, INT iItem)
627 if (!infoPtr->bRectsValid)
628 HEADER_SetItemBounds(infoPtr);
630 InvalidateRect(infoPtr->hwndSelf, &infoPtr->items[iItem].rect, FALSE);
634 static void
635 HEADER_InternalHitTest (const HEADER_INFO *infoPtr, const POINT *lpPt, UINT *pFlags, INT *pItem)
637 RECT rect, rcTest;
638 UINT iCount;
639 INT width;
640 BOOL bNoWidth;
642 GetClientRect (infoPtr->hwndSelf, &rect);
644 *pFlags = 0;
645 bNoWidth = FALSE;
646 if (PtInRect (&rect, *lpPt))
648 if (infoPtr->uNumItem == 0) {
649 *pFlags |= HHT_NOWHERE;
650 *pItem = 1;
651 TRACE("NOWHERE\n");
652 return;
654 else {
655 /* somewhere inside */
656 for (iCount = 0; iCount < infoPtr->uNumItem; iCount++) {
657 rect = infoPtr->items[iCount].rect;
658 width = rect.right - rect.left;
659 if (width == 0) {
660 bNoWidth = TRUE;
661 continue;
663 if (PtInRect (&rect, *lpPt)) {
664 if (width <= 2 * DIVIDER_WIDTH) {
665 *pFlags |= HHT_ONHEADER;
666 *pItem = iCount;
667 TRACE("ON HEADER %d\n", iCount);
668 return;
670 if (HEADER_IndexToOrder(infoPtr, iCount) > 0) {
671 rcTest = rect;
672 rcTest.right = rcTest.left + DIVIDER_WIDTH;
673 if (PtInRect (&rcTest, *lpPt)) {
674 if (HEADER_IsItemFixed(infoPtr, HEADER_PrevItem(infoPtr, iCount)))
676 *pFlags |= HHT_ONHEADER;
677 *pItem = iCount;
678 TRACE("ON HEADER %d\n", *pItem);
679 return;
681 if (bNoWidth) {
682 *pFlags |= HHT_ONDIVOPEN;
683 *pItem = HEADER_PrevItem(infoPtr, iCount);
684 TRACE("ON DIVOPEN %d\n", *pItem);
685 return;
687 else {
688 *pFlags |= HHT_ONDIVIDER;
689 *pItem = HEADER_PrevItem(infoPtr, iCount);
690 TRACE("ON DIVIDER %d\n", *pItem);
691 return;
695 rcTest = rect;
696 rcTest.left = rcTest.right - DIVIDER_WIDTH;
697 if (!HEADER_IsItemFixed(infoPtr, iCount) && PtInRect (&rcTest, *lpPt))
699 *pFlags |= HHT_ONDIVIDER;
700 *pItem = iCount;
701 TRACE("ON DIVIDER %d\n", *pItem);
702 return;
705 *pFlags |= HHT_ONHEADER;
706 *pItem = iCount;
707 TRACE("ON HEADER %d\n", iCount);
708 return;
712 /* check for last divider part (on nowhere) */
713 if (!HEADER_IsItemFixed(infoPtr, infoPtr->uNumItem - 1))
715 rect = infoPtr->items[infoPtr->uNumItem-1].rect;
716 rect.left = rect.right;
717 rect.right += DIVIDER_WIDTH;
718 if (PtInRect (&rect, *lpPt)) {
719 if (bNoWidth) {
720 *pFlags |= HHT_ONDIVOPEN;
721 *pItem = infoPtr->uNumItem - 1;
722 TRACE("ON DIVOPEN %d\n", *pItem);
723 return;
725 else {
726 *pFlags |= HHT_ONDIVIDER;
727 *pItem = infoPtr->uNumItem - 1;
728 TRACE("ON DIVIDER %d\n", *pItem);
729 return;
734 *pFlags |= HHT_NOWHERE;
735 *pItem = 1;
736 TRACE("NOWHERE\n");
737 return;
740 else {
741 if (lpPt->x < rect.left) {
742 TRACE("TO LEFT\n");
743 *pFlags |= HHT_TOLEFT;
745 else if (lpPt->x > rect.right) {
746 TRACE("TO RIGHT\n");
747 *pFlags |= HHT_TORIGHT;
750 if (lpPt->y < rect.top) {
751 TRACE("ABOVE\n");
752 *pFlags |= HHT_ABOVE;
754 else if (lpPt->y > rect.bottom) {
755 TRACE("BELOW\n");
756 *pFlags |= HHT_BELOW;
760 *pItem = 1;
761 TRACE("flags=0x%X\n", *pFlags);
762 return;
766 static void
767 HEADER_DrawTrackLine (const HEADER_INFO *infoPtr, HDC hdc, INT x)
769 RECT rect;
771 GetClientRect (infoPtr->hwndSelf, &rect);
772 PatBlt( hdc, x, rect.top, 1, rect.bottom - rect.top, DSTINVERT );
775 /***
776 * DESCRIPTION:
777 * Convert a HDITEM into the correct format (ANSI/Unicode) to send it in a notify
779 * PARAMETER(S):
780 * [I] infoPtr : the header that wants to send the notify
781 * [O] dest : The buffer to store the HDITEM for notify. It may be set to a HDITEMA of HDITEMW
782 * [I] src : The source HDITEM. It may be a HDITEMA or HDITEMW
783 * [I] fSourceUnicode : is src a HDITEMW or HDITEMA
784 * [O] ppvScratch : a pointer to a scratch buffer that needs to be freed after
785 * the HDITEM is no longer in use or NULL if none was needed
787 * NOTE: We depend on HDITEMA and HDITEMW having the same structure
789 static void HEADER_CopyHDItemForNotify(const HEADER_INFO *infoPtr, HDITEMW *dest,
790 const HDITEMW *src, BOOL fSourceUnicode, LPVOID *ppvScratch)
792 *ppvScratch = NULL;
793 *dest = *src;
795 if (src->mask & HDI_TEXT && src->pszText != LPSTR_TEXTCALLBACKW) /* covers TEXTCALLBACKA as well */
797 if (fSourceUnicode && infoPtr->nNotifyFormat != NFR_UNICODE)
799 dest->pszText = NULL;
800 Str_SetPtrWtoA((LPSTR *)&dest->pszText, src->pszText);
801 *ppvScratch = dest->pszText;
804 if (!fSourceUnicode && infoPtr->nNotifyFormat == NFR_UNICODE)
806 dest->pszText = NULL;
807 Str_SetPtrAtoW(&dest->pszText, (LPSTR)src->pszText);
808 *ppvScratch = dest->pszText;
813 static UINT HEADER_NotifyCodeWtoA(UINT code)
815 /* we use the fact that all the unicode messages are in HDN_FIRST_UNICODE..HDN_LAST*/
816 if (code >= HDN_LAST && code <= HDN_FIRST_UNICODE)
817 return code + HDN_UNICODE_OFFSET;
818 else
819 return code;
822 static LRESULT
823 HEADER_SendNotify(const HEADER_INFO *infoPtr, UINT code, NMHDR *nmhdr)
825 nmhdr->hwndFrom = infoPtr->hwndSelf;
826 nmhdr->idFrom = GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_ID);
827 nmhdr->code = code;
829 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
830 nmhdr->idFrom, (LPARAM)nmhdr);
833 static BOOL
834 HEADER_SendSimpleNotify (const HEADER_INFO *infoPtr, UINT code)
836 NMHDR nmhdr;
837 return (BOOL)HEADER_SendNotify(infoPtr, code, &nmhdr);
840 static LRESULT
841 HEADER_SendCtrlCustomDraw(const HEADER_INFO *infoPtr, DWORD dwDrawStage, HDC hdc, const RECT *rect)
843 NMCUSTOMDRAW nm;
844 nm.dwDrawStage = dwDrawStage;
845 nm.hdc = hdc;
846 nm.rc = *rect;
847 nm.dwItemSpec = 0;
848 nm.uItemState = 0;
849 nm.lItemlParam = 0;
851 return HEADER_SendNotify(infoPtr, NM_CUSTOMDRAW, (NMHDR *)&nm);
854 static BOOL
855 HEADER_SendNotifyWithHDItemT(const HEADER_INFO *infoPtr, UINT code, INT iItem, HDITEMW *lpItem)
857 NMHEADERW nmhdr;
859 if (infoPtr->nNotifyFormat != NFR_UNICODE)
860 code = HEADER_NotifyCodeWtoA(code);
861 nmhdr.iItem = iItem;
862 nmhdr.iButton = 0;
863 nmhdr.pitem = lpItem;
865 return (BOOL)HEADER_SendNotify(infoPtr, code, (NMHDR *)&nmhdr);
868 static BOOL
869 HEADER_SendNotifyWithIntFieldT(const HEADER_INFO *infoPtr, UINT code, INT iItem, INT mask, INT iValue)
871 HDITEMW nmitem;
873 /* copying only the iValue should be ok but to make the code more robust we copy everything */
874 nmitem.cxy = infoPtr->items[iItem].cxy;
875 nmitem.hbm = infoPtr->items[iItem].hbm;
876 nmitem.pszText = NULL;
877 nmitem.cchTextMax = 0;
878 nmitem.fmt = infoPtr->items[iItem].fmt;
879 nmitem.lParam = infoPtr->items[iItem].lParam;
880 nmitem.iOrder = infoPtr->items[iItem].iOrder;
881 nmitem.iImage = infoPtr->items[iItem].iImage;
883 nmitem.mask = mask;
884 switch (mask)
886 case HDI_WIDTH:
887 nmitem.cxy = iValue;
888 break;
889 case HDI_ORDER:
890 nmitem.iOrder = iValue;
891 break;
892 default:
893 ERR("invalid mask value 0x%x\n", iValue);
896 return HEADER_SendNotifyWithHDItemT(infoPtr, code, iItem, &nmitem);
900 * Prepare callback items
901 * depends on NMHDDISPINFOW having same structure as NMHDDISPINFOA
902 * (so we handle the two cases only doing a specific cast for pszText).
903 * Checks if any of the required fields is a callback. If this is the case sends a
904 * NMHDISPINFO notify to retrieve these items. The items are stored in the
905 * HEADER_ITEM pszText and iImage fields. They should be freed with
906 * HEADER_FreeCallbackItems.
908 * @param hwnd : hwnd header container handler
909 * @param iItem : the header item id
910 * @param reqMask : required fields. If any of them is callback this function will fetch it
912 * @return TRUE on success, else FALSE
914 static BOOL
915 HEADER_PrepareCallbackItems(const HEADER_INFO *infoPtr, INT iItem, INT reqMask)
917 HEADER_ITEM *lpItem = &infoPtr->items[iItem];
918 DWORD mask = reqMask & lpItem->callbackMask;
919 NMHDDISPINFOW dispInfo;
920 void *pvBuffer = NULL;
922 if (mask == 0)
923 return TRUE;
924 if (mask&HDI_TEXT && lpItem->pszText != NULL)
926 ERR("(): function called without a call to FreeCallbackItems\n");
927 Free(lpItem->pszText);
928 lpItem->pszText = NULL;
931 memset(&dispInfo, 0, sizeof(NMHDDISPINFOW));
932 dispInfo.hdr.hwndFrom = infoPtr->hwndSelf;
933 dispInfo.hdr.idFrom = GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_ID);
934 if (infoPtr->nNotifyFormat == NFR_UNICODE)
936 dispInfo.hdr.code = HDN_GETDISPINFOW;
937 if (mask & HDI_TEXT)
938 pvBuffer = Alloc(MAX_HEADER_TEXT_LEN * sizeof(WCHAR));
940 else
942 dispInfo.hdr.code = HDN_GETDISPINFOA;
943 if (mask & HDI_TEXT)
944 pvBuffer = Alloc(MAX_HEADER_TEXT_LEN * sizeof(CHAR));
946 dispInfo.pszText = pvBuffer;
947 dispInfo.cchTextMax = (pvBuffer!=NULL?MAX_HEADER_TEXT_LEN:0);
948 dispInfo.iItem = iItem;
949 dispInfo.mask = mask;
950 dispInfo.lParam = lpItem->lParam;
952 TRACE("Sending HDN_GETDISPINFO%c\n", infoPtr->nNotifyFormat == NFR_UNICODE?'W':'A');
953 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, dispInfo.hdr.idFrom, (LPARAM)&dispInfo);
955 TRACE("SendMessage returns(mask:0x%x,str:%s,lParam:%p)\n",
956 dispInfo.mask,
957 (infoPtr->nNotifyFormat == NFR_UNICODE ? debugstr_w(dispInfo.pszText) : (LPSTR) dispInfo.pszText),
958 (void*) dispInfo.lParam);
960 if (mask & HDI_IMAGE)
961 lpItem->iImage = dispInfo.iImage;
962 if (mask & HDI_TEXT)
964 if (infoPtr->nNotifyFormat == NFR_UNICODE)
966 lpItem->pszText = pvBuffer;
968 /* the user might have used his own buffer */
969 if (dispInfo.pszText != lpItem->pszText)
970 Str_GetPtrW(dispInfo.pszText, lpItem->pszText, MAX_HEADER_TEXT_LEN);
972 else
974 Str_SetPtrAtoW(&lpItem->pszText, (LPSTR)dispInfo.pszText);
975 Free(pvBuffer);
979 if (dispInfo.mask & HDI_DI_SETITEM)
981 /* make the items permanent */
982 lpItem->callbackMask &= ~dispInfo.mask;
985 return TRUE;
988 /***
989 * DESCRIPTION:
990 * Free the items that might be allocated with HEADER_PrepareCallbackItems
992 * PARAMETER(S):
993 * [I] lpItem : the item to free the data
996 static void
997 HEADER_FreeCallbackItems(HEADER_ITEM *lpItem)
999 if (lpItem->callbackMask&HDI_TEXT)
1001 Free(lpItem->pszText);
1002 lpItem->pszText = NULL;
1005 if (lpItem->callbackMask&HDI_IMAGE)
1006 lpItem->iImage = I_IMAGECALLBACK;
1009 static HIMAGELIST
1010 HEADER_CreateDragImage (HEADER_INFO *infoPtr, INT iItem)
1012 HEADER_ITEM *lpItem;
1013 HIMAGELIST himl;
1014 HBITMAP hMemory, hOldBitmap;
1015 LRESULT lCDFlags;
1016 RECT rc;
1017 HDC hMemoryDC;
1018 HDC hDeviceDC;
1019 int height, width;
1020 HFONT hFont;
1022 if (iItem >= infoPtr->uNumItem)
1023 return NULL;
1025 if (!infoPtr->bRectsValid)
1026 HEADER_SetItemBounds(infoPtr);
1028 lpItem = &infoPtr->items[iItem];
1029 width = lpItem->rect.right - lpItem->rect.left;
1030 height = lpItem->rect.bottom - lpItem->rect.top;
1032 hDeviceDC = GetDC(NULL);
1033 hMemoryDC = CreateCompatibleDC(hDeviceDC);
1034 hMemory = CreateCompatibleBitmap(hDeviceDC, width, height);
1035 ReleaseDC(NULL, hDeviceDC);
1036 hOldBitmap = SelectObject(hMemoryDC, hMemory);
1037 SetViewportOrgEx(hMemoryDC, -lpItem->rect.left, -lpItem->rect.top, NULL);
1038 hFont = infoPtr->hFont ? infoPtr->hFont : GetStockObject(SYSTEM_FONT);
1039 SelectObject(hMemoryDC, hFont);
1041 GetClientRect(infoPtr->hwndSelf, &rc);
1042 lCDFlags = HEADER_SendCtrlCustomDraw(infoPtr, CDDS_PREPAINT, hMemoryDC, &rc);
1043 HEADER_DrawItem(infoPtr, hMemoryDC, iItem, FALSE, lCDFlags);
1044 if (lCDFlags & CDRF_NOTIFYPOSTPAINT)
1045 HEADER_SendCtrlCustomDraw(infoPtr, CDDS_POSTPAINT, hMemoryDC, &rc);
1047 hMemory = SelectObject(hMemoryDC, hOldBitmap);
1048 DeleteDC(hMemoryDC);
1050 if (hMemory == NULL) /* if anything failed */
1051 return NULL;
1053 himl = ImageList_Create(width, height, ILC_COLORDDB, 1, 1);
1054 ImageList_Add(himl, hMemory, NULL);
1055 DeleteObject(hMemory);
1056 return himl;
1059 static LRESULT
1060 HEADER_SetHotDivider(HEADER_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1062 INT iDivider;
1063 RECT r;
1065 if (wParam)
1067 POINT pt;
1068 UINT flags;
1069 pt.x = (INT)(SHORT)LOWORD(lParam);
1070 pt.y = 0;
1071 HEADER_InternalHitTest (infoPtr, &pt, &flags, &iDivider);
1073 if (flags & HHT_TOLEFT)
1074 iDivider = 0;
1075 else if (flags & HHT_NOWHERE || flags & HHT_TORIGHT)
1076 iDivider = infoPtr->uNumItem;
1077 else
1079 HEADER_ITEM *lpItem = &infoPtr->items[iDivider];
1080 if (pt.x > (lpItem->rect.left+lpItem->rect.right)/2)
1081 iDivider = HEADER_NextItem(infoPtr, iDivider);
1084 else
1085 iDivider = (INT)lParam;
1087 /* Note; wParam==FALSE, lParam==-1 is valid and is used to clear the hot divider */
1088 if (iDivider<-1 || iDivider>(int)infoPtr->uNumItem)
1089 return iDivider;
1091 if (iDivider != infoPtr->iHotDivider)
1093 if (infoPtr->iHotDivider != -1)
1095 HEADER_GetHotDividerRect(infoPtr, &r);
1096 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
1098 infoPtr->iHotDivider = iDivider;
1099 if (iDivider != -1)
1101 HEADER_GetHotDividerRect(infoPtr, &r);
1102 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
1105 return iDivider;
1108 static LRESULT
1109 HEADER_DeleteItem (HEADER_INFO *infoPtr, INT iItem)
1111 INT iOrder;
1112 UINT i;
1114 TRACE("[iItem=%d]\n", iItem);
1116 if ((iItem < 0) || (iItem >= (INT)infoPtr->uNumItem))
1117 return FALSE;
1119 for (i = 0; i < infoPtr->uNumItem; i++)
1120 TRACE("%d: order=%d, iOrder=%d, ->iOrder=%d\n", i, infoPtr->order[i], infoPtr->items[i].iOrder, infoPtr->items[infoPtr->order[i]].iOrder);
1122 iOrder = infoPtr->items[iItem].iOrder;
1123 Free(infoPtr->items[iItem].pszText);
1125 infoPtr->uNumItem--;
1126 memmove(&infoPtr->items[iItem], &infoPtr->items[iItem + 1],
1127 (infoPtr->uNumItem - iItem) * sizeof(HEADER_ITEM));
1128 memmove(&infoPtr->order[iOrder], &infoPtr->order[iOrder + 1],
1129 (infoPtr->uNumItem - iOrder) * sizeof(INT));
1130 infoPtr->items = ReAlloc(infoPtr->items, sizeof(HEADER_ITEM) * infoPtr->uNumItem);
1131 infoPtr->order = ReAlloc(infoPtr->order, sizeof(INT) * infoPtr->uNumItem);
1133 /* Correct the orders */
1134 for (i = 0; i < infoPtr->uNumItem; i++)
1136 if (infoPtr->order[i] > iItem)
1137 infoPtr->order[i]--;
1138 if (i >= iOrder)
1139 infoPtr->items[infoPtr->order[i]].iOrder = i;
1141 for (i = 0; i < infoPtr->uNumItem; i++)
1142 TRACE("%d: order=%d, iOrder=%d, ->iOrder=%d\n", i, infoPtr->order[i], infoPtr->items[i].iOrder, infoPtr->items[infoPtr->order[i]].iOrder);
1144 HEADER_SetItemBounds (infoPtr);
1145 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1147 return TRUE;
1151 static LRESULT
1152 HEADER_GetImageList (const HEADER_INFO *infoPtr)
1154 return (LRESULT)infoPtr->himl;
1158 static LRESULT
1159 HEADER_GetItemT (const HEADER_INFO *infoPtr, INT nItem, LPHDITEMW phdi, BOOL bUnicode)
1161 HEADER_ITEM *lpItem;
1162 UINT mask;
1164 if (!phdi)
1165 return FALSE;
1167 TRACE("[nItem=%d]\n", nItem);
1169 mask = phdi->mask;
1170 if (mask == 0)
1171 return TRUE;
1173 if ((nItem < 0) || (nItem >= (INT)infoPtr->uNumItem))
1174 return FALSE;
1176 if (mask & HDI_UNKNOWN_FIELDS)
1178 TRACE("mask %x contains unknown fields. Using only comctl32 4.0 fields\n", mask);
1179 mask &= HDI_COMCTL32_4_0_FIELDS;
1182 lpItem = &infoPtr->items[nItem];
1183 HEADER_PrepareCallbackItems(infoPtr, nItem, mask);
1185 if (mask & HDI_BITMAP)
1186 phdi->hbm = lpItem->hbm;
1188 if (mask & HDI_FORMAT)
1189 phdi->fmt = lpItem->fmt;
1191 if (mask & HDI_WIDTH)
1192 phdi->cxy = lpItem->cxy;
1194 if (mask & HDI_LPARAM)
1195 phdi->lParam = lpItem->lParam;
1197 if (mask & HDI_IMAGE)
1198 phdi->iImage = lpItem->iImage;
1200 if (mask & HDI_ORDER)
1201 phdi->iOrder = lpItem->iOrder;
1203 if (mask & HDI_TEXT)
1205 if (bUnicode)
1206 Str_GetPtrW (lpItem->pszText, phdi->pszText, phdi->cchTextMax);
1207 else
1208 Str_GetPtrWtoA (lpItem->pszText, (LPSTR)phdi->pszText, phdi->cchTextMax);
1211 HEADER_FreeCallbackItems(lpItem);
1212 return TRUE;
1216 static inline LRESULT
1217 HEADER_GetItemCount (const HEADER_INFO *infoPtr)
1219 return infoPtr->uNumItem;
1223 static LRESULT
1224 HEADER_GetItemRect (const HEADER_INFO *infoPtr, INT iItem, LPRECT lpRect)
1226 if ((iItem < 0) || (iItem >= (INT)infoPtr->uNumItem))
1227 return FALSE;
1229 lpRect->left = infoPtr->items[iItem].rect.left;
1230 lpRect->right = infoPtr->items[iItem].rect.right;
1231 lpRect->top = infoPtr->items[iItem].rect.top;
1232 lpRect->bottom = infoPtr->items[iItem].rect.bottom;
1234 return TRUE;
1238 static LRESULT
1239 HEADER_GetOrderArray(const HEADER_INFO *infoPtr, INT size, LPINT order)
1241 if ((UINT)size <infoPtr->uNumItem)
1242 return FALSE;
1244 memcpy(order, infoPtr->order, infoPtr->uNumItem * sizeof(INT));
1245 return TRUE;
1248 /* Returns index of first duplicate 'value' from [0,to) range,
1249 or -1 if there isn't any */
1250 static INT has_duplicate(const INT *array, INT to, INT value)
1252 INT i;
1253 for(i = 0; i < to; i++)
1254 if (array[i] == value) return i;
1255 return -1;
1258 /* returns next available value from [0,max] not to duplicate in [0,to) */
1259 static INT get_nextvalue(const INT *array, INT to, INT max)
1261 INT i;
1262 for(i = 0; i < max; i++)
1263 if (has_duplicate(array, to, i) == -1) return i;
1264 return 0;
1267 static LRESULT
1268 HEADER_SetOrderArray(HEADER_INFO *infoPtr, INT size, const INT *order)
1270 HEADER_ITEM *lpItem;
1271 INT i;
1273 if ((UINT)size != infoPtr->uNumItem)
1274 return FALSE;
1276 if (TRACE_ON(header))
1278 TRACE("count=%d, order array={", size);
1279 for (i = 0; i < size; i++)
1280 TRACE("%d%c", order[i], i != size-1 ? ',' : '}');
1281 TRACE("\n");
1284 for (i=0; i<size; i++)
1286 if (order[i] >= size || order[i] < 0)
1287 /* on invalid index get next available */
1288 /* FIXME: if i==0 array item is out of range behaviour is
1289 different, see tests */
1290 infoPtr->order[i] = get_nextvalue(infoPtr->order, i, size);
1291 else
1293 INT j, dup;
1295 infoPtr->order[i] = order[i];
1296 j = i;
1297 /* remove duplicates */
1298 while ((dup = has_duplicate(infoPtr->order, j, order[j])) != -1)
1300 INT next;
1302 next = get_nextvalue(infoPtr->order, j, size);
1303 infoPtr->order[dup] = next;
1304 j--;
1308 /* sync with item data */
1309 for (i=0; i<size; i++)
1311 lpItem = &infoPtr->items[infoPtr->order[i]];
1312 lpItem->iOrder = i;
1314 HEADER_SetItemBounds(infoPtr);
1315 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1316 return TRUE;
1319 static inline LRESULT
1320 HEADER_GetUnicodeFormat (const HEADER_INFO *infoPtr)
1322 return (infoPtr->nNotifyFormat == NFR_UNICODE);
1326 static LRESULT
1327 HEADER_HitTest (const HEADER_INFO *infoPtr, LPHDHITTESTINFO phti)
1329 UINT outside = HHT_NOWHERE | HHT_ABOVE | HHT_BELOW | HHT_TOLEFT | HHT_TORIGHT;
1331 HEADER_InternalHitTest (infoPtr, &phti->pt, &phti->flags, &phti->iItem);
1333 if (phti->flags & outside)
1334 return phti->iItem = -1;
1335 else
1336 return phti->iItem;
1340 static LRESULT
1341 HEADER_InsertItemT (HEADER_INFO *infoPtr, INT nItem, const HDITEMW *phdi, BOOL bUnicode)
1343 HEADER_ITEM *lpItem;
1344 INT iOrder;
1345 UINT i;
1346 UINT copyMask;
1348 if ((phdi == NULL) || (nItem < 0) || (phdi->mask == 0))
1349 return -1;
1351 if (nItem > infoPtr->uNumItem)
1352 nItem = infoPtr->uNumItem;
1354 iOrder = (phdi->mask & HDI_ORDER) ? phdi->iOrder : nItem;
1355 if (iOrder < 0)
1356 iOrder = 0;
1357 else if (infoPtr->uNumItem < iOrder)
1358 iOrder = infoPtr->uNumItem;
1360 infoPtr->uNumItem++;
1361 infoPtr->items = ReAlloc(infoPtr->items, sizeof(HEADER_ITEM) * infoPtr->uNumItem);
1362 infoPtr->order = ReAlloc(infoPtr->order, sizeof(INT) * infoPtr->uNumItem);
1364 /* make space for the new item */
1365 memmove(&infoPtr->items[nItem + 1], &infoPtr->items[nItem],
1366 (infoPtr->uNumItem - nItem - 1) * sizeof(HEADER_ITEM));
1367 memmove(&infoPtr->order[iOrder + 1], &infoPtr->order[iOrder],
1368 (infoPtr->uNumItem - iOrder - 1) * sizeof(INT));
1370 /* update the order array */
1371 infoPtr->order[iOrder] = nItem;
1372 for (i = 0; i < infoPtr->uNumItem; i++)
1374 if (i != iOrder && infoPtr->order[i] >= nItem)
1375 infoPtr->order[i]++;
1376 infoPtr->items[infoPtr->order[i]].iOrder = i;
1379 lpItem = &infoPtr->items[nItem];
1380 ZeroMemory(lpItem, sizeof(HEADER_ITEM));
1381 /* cxy, fmt and lParam are copied even if not in the HDITEM mask */
1382 copyMask = phdi->mask | HDI_WIDTH | HDI_FORMAT | HDI_LPARAM;
1383 HEADER_StoreHDItemInHeader(lpItem, copyMask, phdi, bUnicode);
1384 lpItem->iOrder = iOrder;
1386 /* set automatically some format bits */
1387 if (phdi->mask & HDI_TEXT)
1388 lpItem->fmt |= HDF_STRING;
1389 else
1390 lpItem->fmt &= ~HDF_STRING;
1392 if (lpItem->hbm != NULL)
1393 lpItem->fmt |= HDF_BITMAP;
1394 else
1395 lpItem->fmt &= ~HDF_BITMAP;
1397 if (phdi->mask & HDI_IMAGE)
1398 lpItem->fmt |= HDF_IMAGE;
1400 HEADER_SetItemBounds (infoPtr);
1401 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1403 return nItem;
1407 static LRESULT
1408 HEADER_Layout (HEADER_INFO *infoPtr, LPHDLAYOUT lpLayout)
1410 lpLayout->pwpos->hwnd = infoPtr->hwndSelf;
1411 lpLayout->pwpos->hwndInsertAfter = 0;
1412 lpLayout->pwpos->x = lpLayout->prc->left;
1413 lpLayout->pwpos->y = lpLayout->prc->top;
1414 lpLayout->pwpos->cx = lpLayout->prc->right - lpLayout->prc->left;
1415 if (infoPtr->dwStyle & HDS_HIDDEN)
1416 lpLayout->pwpos->cy = 0;
1417 else {
1418 lpLayout->pwpos->cy = infoPtr->nHeight;
1419 lpLayout->prc->top += infoPtr->nHeight;
1421 lpLayout->pwpos->flags = SWP_NOZORDER;
1423 TRACE("Layout x=%d y=%d cx=%d cy=%d\n",
1424 lpLayout->pwpos->x, lpLayout->pwpos->y,
1425 lpLayout->pwpos->cx, lpLayout->pwpos->cy);
1427 infoPtr->bRectsValid = FALSE;
1429 return TRUE;
1433 static LRESULT
1434 HEADER_SetImageList (HEADER_INFO *infoPtr, HIMAGELIST himl)
1436 HIMAGELIST himlOld;
1438 TRACE("(himl %p)\n", himl);
1439 himlOld = infoPtr->himl;
1440 infoPtr->himl = himl;
1442 /* FIXME: Refresh needed??? */
1444 return (LRESULT)himlOld;
1448 static LRESULT
1449 HEADER_GetBitmapMargin(const HEADER_INFO *infoPtr)
1451 return infoPtr->iMargin;
1454 static LRESULT
1455 HEADER_SetBitmapMargin(HEADER_INFO *infoPtr, INT iMargin)
1457 INT oldMargin = infoPtr->iMargin;
1459 infoPtr->iMargin = iMargin;
1461 return oldMargin;
1464 static LRESULT
1465 HEADER_SetItemT (HEADER_INFO *infoPtr, INT nItem, const HDITEMW *phdi, BOOL bUnicode)
1467 HEADER_ITEM *lpItem;
1468 HDITEMW hdNotify;
1469 void *pvScratch;
1471 if (phdi == NULL)
1472 return FALSE;
1473 if ((nItem < 0) || (nItem >= (INT)infoPtr->uNumItem))
1474 return FALSE;
1476 TRACE("[nItem=%d]\n", nItem);
1478 HEADER_CopyHDItemForNotify(infoPtr, &hdNotify, phdi, bUnicode, &pvScratch);
1479 if (HEADER_SendNotifyWithHDItemT(infoPtr, HDN_ITEMCHANGINGW, nItem, &hdNotify))
1481 Free(pvScratch);
1482 return FALSE;
1485 lpItem = &infoPtr->items[nItem];
1486 HEADER_StoreHDItemInHeader(lpItem, phdi->mask, phdi, bUnicode);
1488 if (phdi->mask & HDI_ORDER)
1489 if (phdi->iOrder >= 0 && phdi->iOrder < infoPtr->uNumItem)
1490 HEADER_ChangeItemOrder(infoPtr, nItem, phdi->iOrder);
1492 HEADER_SendNotifyWithHDItemT(infoPtr, HDN_ITEMCHANGEDW, nItem, &hdNotify);
1494 HEADER_SetItemBounds (infoPtr);
1496 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1498 Free(pvScratch);
1499 return TRUE;
1502 static inline LRESULT
1503 HEADER_SetUnicodeFormat (HEADER_INFO *infoPtr, WPARAM wParam)
1505 BOOL bTemp = (infoPtr->nNotifyFormat == NFR_UNICODE);
1507 infoPtr->nNotifyFormat = ((BOOL)wParam ? NFR_UNICODE : NFR_ANSI);
1509 return bTemp;
1513 static LRESULT
1514 HEADER_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
1516 HEADER_INFO *infoPtr;
1517 TEXTMETRICW tm;
1518 HFONT hOldFont;
1519 HDC hdc;
1521 infoPtr = Alloc (sizeof(HEADER_INFO));
1522 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1524 infoPtr->hwndSelf = hwnd;
1525 infoPtr->hwndNotify = lpcs->hwndParent;
1526 infoPtr->uNumItem = 0;
1527 infoPtr->hFont = 0;
1528 infoPtr->items = 0;
1529 infoPtr->order = 0;
1530 infoPtr->bRectsValid = FALSE;
1531 infoPtr->hcurArrow = LoadCursorW (0, (LPWSTR)IDC_ARROW);
1532 infoPtr->hcurDivider = LoadCursorW (COMCTL32_hModule, MAKEINTRESOURCEW(IDC_DIVIDER));
1533 infoPtr->hcurDivopen = LoadCursorW (COMCTL32_hModule, MAKEINTRESOURCEW(IDC_DIVIDEROPEN));
1534 infoPtr->bPressed = FALSE;
1535 infoPtr->bTracking = FALSE;
1536 infoPtr->dwStyle = lpcs->style;
1537 infoPtr->iMoveItem = 0;
1538 infoPtr->himl = 0;
1539 infoPtr->iHotItem = -1;
1540 infoPtr->iHotDivider = -1;
1541 infoPtr->iMargin = 3*GetSystemMetrics(SM_CXEDGE);
1542 infoPtr->nNotifyFormat =
1543 SendMessageW (infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)hwnd, NF_QUERY);
1544 infoPtr->filter_change_timeout = 1000;
1546 hdc = GetDC (0);
1547 hOldFont = SelectObject (hdc, GetStockObject (SYSTEM_FONT));
1548 GetTextMetricsW (hdc, &tm);
1549 infoPtr->nHeight = tm.tmHeight + VERT_BORDER;
1550 SelectObject (hdc, hOldFont);
1551 ReleaseDC (0, hdc);
1553 OpenThemeData(hwnd, themeClass);
1555 return 0;
1559 static LRESULT
1560 HEADER_Destroy (HEADER_INFO *infoPtr)
1562 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
1563 CloseThemeData(theme);
1564 return 0;
1567 static LRESULT
1568 HEADER_NCDestroy (HEADER_INFO *infoPtr)
1570 HEADER_ITEM *lpItem;
1571 INT nItem;
1573 if (infoPtr->items) {
1574 lpItem = infoPtr->items;
1575 for (nItem = 0; nItem < infoPtr->uNumItem; nItem++, lpItem++) {
1576 Free(lpItem->pszText);
1578 Free (infoPtr->items);
1581 Free(infoPtr->order);
1583 SetWindowLongPtrW (infoPtr->hwndSelf, 0, 0);
1584 Free (infoPtr);
1586 return 0;
1590 static inline LRESULT
1591 HEADER_GetFont (const HEADER_INFO *infoPtr)
1593 return (LRESULT)infoPtr->hFont;
1597 static BOOL
1598 HEADER_IsDragDistance(const HEADER_INFO *infoPtr, const POINT *pt)
1600 /* Windows allows for a mouse movement before starting the drag. We use the
1601 * SM_CXDOUBLECLICK/SM_CYDOUBLECLICK as that distance.
1603 return (abs(infoPtr->ptLButtonDown.x - pt->x)>GetSystemMetrics(SM_CXDOUBLECLK) ||
1604 abs(infoPtr->ptLButtonDown.y - pt->y)>GetSystemMetrics(SM_CYDOUBLECLK));
1607 static LRESULT
1608 HEADER_LButtonDblClk (const HEADER_INFO *infoPtr, INT x, INT y)
1610 POINT pt;
1611 UINT flags;
1612 INT nItem;
1614 pt.x = x;
1615 pt.y = y;
1616 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1618 if ((infoPtr->dwStyle & HDS_BUTTONS) && (flags == HHT_ONHEADER))
1619 HEADER_SendNotifyWithHDItemT(infoPtr, HDN_ITEMDBLCLICKW, nItem, NULL);
1620 else if ((flags == HHT_ONDIVIDER) || (flags == HHT_ONDIVOPEN))
1621 HEADER_SendNotifyWithHDItemT(infoPtr, HDN_DIVIDERDBLCLICKW, nItem, NULL);
1623 return 0;
1627 static LRESULT
1628 HEADER_LButtonDown (HEADER_INFO *infoPtr, INT x, INT y)
1630 POINT pt;
1631 UINT flags;
1632 INT nItem;
1633 HDC hdc;
1635 pt.x = x;
1636 pt.y = y;
1637 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1639 if ((infoPtr->dwStyle & HDS_BUTTONS) && (flags == HHT_ONHEADER)) {
1640 SetCapture (infoPtr->hwndSelf);
1641 infoPtr->bCaptured = TRUE;
1642 infoPtr->bPressed = TRUE;
1643 infoPtr->bDragging = FALSE;
1644 infoPtr->iMoveItem = nItem;
1645 infoPtr->ptLButtonDown = pt;
1647 infoPtr->items[nItem].bDown = TRUE;
1649 /* Send WM_CUSTOMDRAW */
1650 hdc = GetDC (infoPtr->hwndSelf);
1651 HEADER_RefreshItem (infoPtr, nItem);
1652 ReleaseDC (infoPtr->hwndSelf, hdc);
1654 TRACE("Pressed item %d!\n", nItem);
1656 else if ((flags == HHT_ONDIVIDER) || (flags == HHT_ONDIVOPEN)) {
1657 INT iCurrWidth = infoPtr->items[nItem].cxy;
1658 if (!HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_BEGINTRACKW, nItem, HDI_WIDTH, iCurrWidth))
1660 SetCapture (infoPtr->hwndSelf);
1661 infoPtr->bCaptured = TRUE;
1662 infoPtr->bTracking = TRUE;
1663 infoPtr->iMoveItem = nItem;
1664 infoPtr->xTrackOffset = infoPtr->items[nItem].rect.right - pt.x;
1666 if (!(infoPtr->dwStyle & HDS_FULLDRAG)) {
1667 infoPtr->xOldTrack = infoPtr->items[nItem].rect.right;
1668 hdc = GetDC (infoPtr->hwndSelf);
1669 HEADER_DrawTrackLine (infoPtr, hdc, infoPtr->xOldTrack);
1670 ReleaseDC (infoPtr->hwndSelf, hdc);
1673 TRACE("Begin tracking item %d!\n", nItem);
1677 return 0;
1681 static LRESULT
1682 HEADER_LButtonUp (HEADER_INFO *infoPtr, INT x, INT y)
1684 POINT pt;
1685 UINT flags;
1686 INT nItem;
1687 HDC hdc;
1689 pt.x = x;
1690 pt.y = y;
1691 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1693 if (infoPtr->bPressed) {
1695 infoPtr->items[infoPtr->iMoveItem].bDown = FALSE;
1697 if (infoPtr->bDragging)
1699 HEADER_ITEM *lpItem = &infoPtr->items[infoPtr->iMoveItem];
1700 INT iNewOrder;
1702 ImageList_DragShowNolock(FALSE);
1703 ImageList_EndDrag();
1705 if (infoPtr->iHotDivider == -1)
1706 iNewOrder = -1;
1707 else if (infoPtr->iHotDivider == infoPtr->uNumItem)
1708 iNewOrder = infoPtr->uNumItem-1;
1709 else
1711 iNewOrder = HEADER_IndexToOrder(infoPtr, infoPtr->iHotDivider);
1712 if (iNewOrder > lpItem->iOrder)
1713 iNewOrder--;
1716 if (iNewOrder != -1 &&
1717 !HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ENDDRAG, infoPtr->iMoveItem, HDI_ORDER, iNewOrder))
1719 HEADER_ChangeItemOrder(infoPtr, infoPtr->iMoveItem, iNewOrder);
1720 infoPtr->bRectsValid = FALSE;
1721 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1723 else
1724 InvalidateRect(infoPtr->hwndSelf, &infoPtr->items[infoPtr->iMoveItem].rect, FALSE);
1726 infoPtr->bDragging = FALSE;
1727 HEADER_SetHotDivider(infoPtr, FALSE, -1);
1729 else
1731 hdc = GetDC (infoPtr->hwndSelf);
1732 HEADER_RefreshItem (infoPtr, infoPtr->iMoveItem);
1733 ReleaseDC (infoPtr->hwndSelf, hdc);
1735 if (!(infoPtr->dwStyle & HDS_DRAGDROP) || !HEADER_IsDragDistance(infoPtr, &pt))
1736 HEADER_SendNotifyWithHDItemT(infoPtr, HDN_ITEMCLICKW, infoPtr->iMoveItem, NULL);
1739 TRACE("Released item %d!\n", infoPtr->iMoveItem);
1740 infoPtr->bPressed = FALSE;
1742 else if (infoPtr->bTracking) {
1743 INT iNewWidth = pt.x - infoPtr->items[infoPtr->iMoveItem].rect.left + infoPtr->xTrackOffset;
1744 if (iNewWidth < 0)
1745 iNewWidth = 0;
1746 TRACE("End tracking item %d!\n", infoPtr->iMoveItem);
1747 infoPtr->bTracking = FALSE;
1749 HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ENDTRACKW, infoPtr->iMoveItem, HDI_WIDTH, iNewWidth);
1751 if (!(infoPtr->dwStyle & HDS_FULLDRAG)) {
1752 hdc = GetDC (infoPtr->hwndSelf);
1753 HEADER_DrawTrackLine (infoPtr, hdc, infoPtr->xOldTrack);
1754 ReleaseDC (infoPtr->hwndSelf, hdc);
1757 if (!HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ITEMCHANGINGW, infoPtr->iMoveItem, HDI_WIDTH, iNewWidth))
1759 infoPtr->items[infoPtr->iMoveItem].cxy = iNewWidth;
1760 HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ITEMCHANGEDW, infoPtr->iMoveItem, HDI_WIDTH, iNewWidth);
1763 HEADER_SetItemBounds (infoPtr);
1764 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1767 if (infoPtr->bCaptured) {
1768 infoPtr->bCaptured = FALSE;
1769 ReleaseCapture ();
1770 HEADER_SendSimpleNotify (infoPtr, NM_RELEASEDCAPTURE);
1773 return 0;
1777 static LRESULT
1778 HEADER_NotifyFormat (HEADER_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1780 switch (lParam)
1782 case NF_QUERY:
1783 return infoPtr->nNotifyFormat;
1785 case NF_REQUERY:
1786 infoPtr->nNotifyFormat =
1787 SendMessageW ((HWND)wParam, WM_NOTIFYFORMAT,
1788 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
1789 return infoPtr->nNotifyFormat;
1792 return 0;
1795 static LRESULT
1796 HEADER_MouseLeave (HEADER_INFO *infoPtr)
1798 /* Reset hot-tracked item when mouse leaves control. */
1799 INT oldHotItem = infoPtr->iHotItem;
1800 HDC hdc = GetDC (infoPtr->hwndSelf);
1802 infoPtr->iHotItem = -1;
1803 if (oldHotItem != -1) HEADER_RefreshItem (infoPtr, oldHotItem);
1804 ReleaseDC (infoPtr->hwndSelf, hdc);
1806 return 0;
1810 static LRESULT
1811 HEADER_MouseMove (HEADER_INFO *infoPtr, LPARAM lParam)
1813 POINT pt;
1814 UINT flags;
1815 INT nItem, nWidth;
1816 HDC hdc;
1817 /* With theming, hottracking is always enabled */
1818 BOOL hotTrackEnabled =
1819 ((infoPtr->dwStyle & HDS_BUTTONS) && (infoPtr->dwStyle & HDS_HOTTRACK))
1820 || (GetWindowTheme (infoPtr->hwndSelf) != NULL);
1821 INT oldHotItem = infoPtr->iHotItem;
1823 pt.x = (INT)(SHORT)LOWORD(lParam);
1824 pt.y = (INT)(SHORT)HIWORD(lParam);
1825 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1827 if (hotTrackEnabled) {
1828 if (flags & (HHT_ONHEADER | HHT_ONDIVIDER | HHT_ONDIVOPEN))
1829 infoPtr->iHotItem = nItem;
1830 else
1831 infoPtr->iHotItem = -1;
1834 if (infoPtr->bCaptured) {
1835 /* check if we should drag the header */
1836 if (infoPtr->bPressed && !infoPtr->bDragging && (infoPtr->dwStyle & HDS_DRAGDROP)
1837 && HEADER_IsDragDistance(infoPtr, &pt))
1839 if (!HEADER_SendNotifyWithHDItemT(infoPtr, HDN_BEGINDRAG, infoPtr->iMoveItem, NULL))
1841 HIMAGELIST hDragItem = HEADER_CreateDragImage(infoPtr, infoPtr->iMoveItem);
1842 if (hDragItem != NULL)
1844 HEADER_ITEM *lpItem = &infoPtr->items[infoPtr->iMoveItem];
1845 TRACE("Starting item drag\n");
1846 ImageList_BeginDrag(hDragItem, 0, pt.x - lpItem->rect.left, 0);
1847 ImageList_DragShowNolock(TRUE);
1848 ImageList_Destroy(hDragItem);
1849 infoPtr->bDragging = TRUE;
1854 if (infoPtr->bDragging)
1856 POINT drag;
1857 drag.x = pt.x;
1858 drag.y = 0;
1859 ClientToScreen(infoPtr->hwndSelf, &drag);
1860 ImageList_DragMove(drag.x, drag.y);
1861 HEADER_SetHotDivider(infoPtr, TRUE, lParam);
1864 if (infoPtr->bPressed && !infoPtr->bDragging) {
1865 BOOL oldState = infoPtr->items[infoPtr->iMoveItem].bDown;
1866 if ((nItem == infoPtr->iMoveItem) && (flags == HHT_ONHEADER))
1867 infoPtr->items[infoPtr->iMoveItem].bDown = TRUE;
1868 else
1869 infoPtr->items[infoPtr->iMoveItem].bDown = FALSE;
1870 if (oldState != infoPtr->items[infoPtr->iMoveItem].bDown) {
1871 hdc = GetDC (infoPtr->hwndSelf);
1872 HEADER_RefreshItem (infoPtr, infoPtr->iMoveItem);
1873 ReleaseDC (infoPtr->hwndSelf, hdc);
1876 TRACE("Moving pressed item %d!\n", infoPtr->iMoveItem);
1878 else if (infoPtr->bTracking) {
1879 if (infoPtr->dwStyle & HDS_FULLDRAG) {
1880 HEADER_ITEM *lpItem = &infoPtr->items[infoPtr->iMoveItem];
1881 nWidth = pt.x - lpItem->rect.left + infoPtr->xTrackOffset;
1882 if (!HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ITEMCHANGINGW, infoPtr->iMoveItem, HDI_WIDTH, nWidth))
1884 INT nOldWidth = lpItem->rect.right - lpItem->rect.left;
1885 RECT rcClient;
1886 RECT rcScroll;
1888 if (nWidth < 0) nWidth = 0;
1889 infoPtr->items[infoPtr->iMoveItem].cxy = nWidth;
1890 HEADER_SetItemBounds(infoPtr);
1892 GetClientRect(infoPtr->hwndSelf, &rcClient);
1893 rcScroll = rcClient;
1894 rcScroll.left = lpItem->rect.left + nOldWidth;
1895 ScrollWindowEx(infoPtr->hwndSelf, nWidth - nOldWidth, 0, &rcScroll, &rcClient, NULL, NULL, 0);
1896 InvalidateRect(infoPtr->hwndSelf, &lpItem->rect, FALSE);
1897 UpdateWindow(infoPtr->hwndSelf);
1899 HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ITEMCHANGEDW, infoPtr->iMoveItem, HDI_WIDTH, nWidth);
1902 else {
1903 INT iTrackWidth;
1904 hdc = GetDC (infoPtr->hwndSelf);
1905 HEADER_DrawTrackLine (infoPtr, hdc, infoPtr->xOldTrack);
1906 infoPtr->xOldTrack = pt.x + infoPtr->xTrackOffset;
1907 if (infoPtr->xOldTrack < infoPtr->items[infoPtr->iMoveItem].rect.left)
1908 infoPtr->xOldTrack = infoPtr->items[infoPtr->iMoveItem].rect.left;
1909 HEADER_DrawTrackLine (infoPtr, hdc, infoPtr->xOldTrack);
1910 ReleaseDC (infoPtr->hwndSelf, hdc);
1911 iTrackWidth = infoPtr->xOldTrack - infoPtr->items[infoPtr->iMoveItem].rect.left;
1912 /* FIXME: should stop tracking if HDN_TRACK returns TRUE */
1913 HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_TRACKW, infoPtr->iMoveItem, HDI_WIDTH, iTrackWidth);
1916 TRACE("Tracking item %d!\n", infoPtr->iMoveItem);
1920 if (hotTrackEnabled) {
1921 TRACKMOUSEEVENT tme;
1922 if (oldHotItem != infoPtr->iHotItem && !infoPtr->bDragging) {
1923 hdc = GetDC (infoPtr->hwndSelf);
1924 if (oldHotItem != -1) HEADER_RefreshItem (infoPtr, oldHotItem);
1925 if (infoPtr->iHotItem != -1) HEADER_RefreshItem (infoPtr, infoPtr->iHotItem);
1926 ReleaseDC (infoPtr->hwndSelf, hdc);
1928 tme.cbSize = sizeof( tme );
1929 tme.dwFlags = TME_LEAVE;
1930 tme.hwndTrack = infoPtr->hwndSelf;
1931 TrackMouseEvent( &tme );
1934 return 0;
1938 static LRESULT
1939 HEADER_Paint (HEADER_INFO *infoPtr, HDC hdcParam)
1941 HDC hdc;
1942 PAINTSTRUCT ps;
1944 hdc = hdcParam==0 ? BeginPaint (infoPtr->hwndSelf, &ps) : hdcParam;
1945 HEADER_Refresh (infoPtr, hdc);
1946 if(!hdcParam)
1947 EndPaint (infoPtr->hwndSelf, &ps);
1948 return 0;
1952 static LRESULT
1953 HEADER_RButtonUp (HEADER_INFO *infoPtr, INT x, INT y)
1955 BOOL bRet;
1956 POINT pt;
1958 pt.x = x;
1959 pt.y = y;
1961 /* Send a Notify message */
1962 bRet = HEADER_SendSimpleNotify (infoPtr, NM_RCLICK);
1964 /* Change to screen coordinate for WM_CONTEXTMENU */
1965 ClientToScreen(infoPtr->hwndSelf, &pt);
1967 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
1968 SendMessageW( infoPtr->hwndSelf, WM_CONTEXTMENU, (WPARAM) infoPtr->hwndSelf, MAKELPARAM(pt.x, pt.y));
1970 return bRet;
1974 static LRESULT
1975 HEADER_SetCursor (HEADER_INFO *infoPtr, LPARAM lParam)
1977 POINT pt;
1978 UINT flags;
1979 INT nItem;
1981 TRACE("code=0x%X id=0x%X\n", LOWORD(lParam), HIWORD(lParam));
1983 GetCursorPos (&pt);
1984 ScreenToClient (infoPtr->hwndSelf, &pt);
1986 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1988 if (flags == HHT_ONDIVIDER)
1989 SetCursor (infoPtr->hcurDivider);
1990 else if (flags == HHT_ONDIVOPEN)
1991 SetCursor (infoPtr->hcurDivopen);
1992 else
1993 SetCursor (infoPtr->hcurArrow);
1995 return 0;
1999 static LRESULT
2000 HEADER_SetFont (HEADER_INFO *infoPtr, HFONT hFont, WORD Redraw)
2002 TEXTMETRICW tm;
2003 HFONT hOldFont;
2004 HDC hdc;
2006 infoPtr->hFont = hFont;
2008 hdc = GetDC (0);
2009 hOldFont = SelectObject (hdc, infoPtr->hFont ? infoPtr->hFont : GetStockObject (SYSTEM_FONT));
2010 GetTextMetricsW (hdc, &tm);
2011 infoPtr->nHeight = tm.tmHeight + VERT_BORDER;
2012 SelectObject (hdc, hOldFont);
2013 ReleaseDC (0, hdc);
2015 infoPtr->bRectsValid = FALSE;
2017 if (Redraw) {
2018 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2021 return 0;
2024 static LRESULT HEADER_SetRedraw(HEADER_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
2026 /* ignoring the InvalidateRect calls is handled by user32. But some apps expect
2027 * that we invalidate the header and this has to be done manually */
2028 LRESULT ret;
2030 ret = DefWindowProcW(infoPtr->hwndSelf, WM_SETREDRAW, wParam, lParam);
2031 if (wParam)
2032 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2033 return ret;
2036 static INT HEADER_StyleChanged(HEADER_INFO *infoPtr, WPARAM wStyleType,
2037 const STYLESTRUCT *lpss)
2039 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2040 wStyleType, lpss->styleOld, lpss->styleNew);
2042 if (wStyleType != GWL_STYLE) return 0;
2044 infoPtr->dwStyle = lpss->styleNew;
2046 return 0;
2049 /* Update the theme handle after a theme change */
2050 static LRESULT HEADER_ThemeChanged(const HEADER_INFO *infoPtr)
2052 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
2053 CloseThemeData(theme);
2054 OpenThemeData(infoPtr->hwndSelf, themeClass);
2055 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2056 return 0;
2059 static INT HEADER_SetFilterChangeTimeout(HEADER_INFO *infoPtr, INT timeout)
2061 INT old_timeout = infoPtr->filter_change_timeout;
2063 if (timeout != 0)
2064 infoPtr->filter_change_timeout = timeout;
2065 return old_timeout;
2068 static LRESULT WINAPI
2069 HEADER_WindowProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2071 HEADER_INFO *infoPtr = (HEADER_INFO *)GetWindowLongPtrW(hwnd, 0);
2073 TRACE("hwnd=%p msg=%x wparam=%lx lParam=%lx\n", hwnd, msg, wParam, lParam);
2074 if (!infoPtr && (msg != WM_CREATE))
2075 return DefWindowProcW (hwnd, msg, wParam, lParam);
2076 switch (msg) {
2077 /* case HDM_CLEARFILTER: */
2079 case HDM_CREATEDRAGIMAGE:
2080 return (LRESULT)HEADER_CreateDragImage (infoPtr, (INT)wParam);
2082 case HDM_DELETEITEM:
2083 return HEADER_DeleteItem (infoPtr, (INT)wParam);
2085 /* case HDM_EDITFILTER: */
2087 case HDM_GETBITMAPMARGIN:
2088 return HEADER_GetBitmapMargin(infoPtr);
2090 case HDM_GETIMAGELIST:
2091 return HEADER_GetImageList (infoPtr);
2093 case HDM_GETITEMA:
2094 case HDM_GETITEMW:
2095 return HEADER_GetItemT (infoPtr, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_GETITEMW);
2097 case HDM_GETITEMCOUNT:
2098 return HEADER_GetItemCount (infoPtr);
2100 case HDM_GETITEMRECT:
2101 return HEADER_GetItemRect (infoPtr, (INT)wParam, (LPRECT)lParam);
2103 case HDM_GETORDERARRAY:
2104 return HEADER_GetOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
2106 case HDM_GETUNICODEFORMAT:
2107 return HEADER_GetUnicodeFormat (infoPtr);
2109 case HDM_HITTEST:
2110 return HEADER_HitTest (infoPtr, (LPHDHITTESTINFO)lParam);
2112 case HDM_INSERTITEMA:
2113 case HDM_INSERTITEMW:
2114 return HEADER_InsertItemT (infoPtr, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_INSERTITEMW);
2116 case HDM_LAYOUT:
2117 return HEADER_Layout (infoPtr, (LPHDLAYOUT)lParam);
2119 case HDM_ORDERTOINDEX:
2120 return HEADER_OrderToIndex(infoPtr, (INT)wParam);
2122 case HDM_SETBITMAPMARGIN:
2123 return HEADER_SetBitmapMargin(infoPtr, (INT)wParam);
2125 case HDM_SETFILTERCHANGETIMEOUT:
2126 return HEADER_SetFilterChangeTimeout(infoPtr, (INT)lParam);
2128 case HDM_SETHOTDIVIDER:
2129 return HEADER_SetHotDivider(infoPtr, wParam, lParam);
2131 case HDM_SETIMAGELIST:
2132 return HEADER_SetImageList (infoPtr, (HIMAGELIST)lParam);
2134 case HDM_SETITEMA:
2135 case HDM_SETITEMW:
2136 return HEADER_SetItemT (infoPtr, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_SETITEMW);
2138 case HDM_SETORDERARRAY:
2139 return HEADER_SetOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
2141 case HDM_SETUNICODEFORMAT:
2142 return HEADER_SetUnicodeFormat (infoPtr, wParam);
2144 case WM_CREATE:
2145 return HEADER_Create (hwnd, (LPCREATESTRUCTW)lParam);
2147 case WM_DESTROY:
2148 return HEADER_Destroy (infoPtr);
2150 case WM_NCDESTROY:
2151 return HEADER_NCDestroy (infoPtr);
2153 case WM_ERASEBKGND:
2154 return 1;
2156 case WM_GETDLGCODE:
2157 return DLGC_WANTTAB | DLGC_WANTARROWS;
2159 case WM_GETFONT:
2160 return HEADER_GetFont (infoPtr);
2162 case WM_LBUTTONDBLCLK:
2163 return HEADER_LButtonDblClk (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2165 case WM_LBUTTONDOWN:
2166 return HEADER_LButtonDown (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2168 case WM_LBUTTONUP:
2169 return HEADER_LButtonUp (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2171 case WM_MOUSELEAVE:
2172 return HEADER_MouseLeave (infoPtr);
2174 case WM_MOUSEMOVE:
2175 return HEADER_MouseMove (infoPtr, lParam);
2177 case WM_NOTIFYFORMAT:
2178 return HEADER_NotifyFormat (infoPtr, wParam, lParam);
2180 case WM_SIZE:
2181 return HEADER_Size (infoPtr);
2183 case WM_THEMECHANGED:
2184 return HEADER_ThemeChanged (infoPtr);
2186 case WM_PRINTCLIENT:
2187 case WM_PAINT:
2188 return HEADER_Paint (infoPtr, (HDC)wParam);
2190 case WM_RBUTTONUP:
2191 return HEADER_RButtonUp (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2193 case WM_SETCURSOR:
2194 return HEADER_SetCursor (infoPtr, lParam);
2196 case WM_SETFONT:
2197 return HEADER_SetFont (infoPtr, (HFONT)wParam, (WORD)lParam);
2199 case WM_SETREDRAW:
2200 return HEADER_SetRedraw(infoPtr, wParam, lParam);
2202 case WM_STYLECHANGED:
2203 return HEADER_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2205 case WM_SYSCOLORCHANGE:
2206 COMCTL32_RefreshSysColors();
2207 return 0;
2209 default:
2210 if ((msg >= WM_USER) && (msg < WM_APP) && !COMCTL32_IsReflectedMessage(msg))
2211 ERR("unknown msg %04x wp=%04lx lp=%08lx\n",
2212 msg, wParam, lParam );
2213 return DefWindowProcW(hwnd, msg, wParam, lParam);
2218 VOID
2219 HEADER_Register (void)
2221 WNDCLASSW wndClass;
2223 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
2224 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
2225 wndClass.lpfnWndProc = HEADER_WindowProc;
2226 wndClass.cbClsExtra = 0;
2227 wndClass.cbWndExtra = sizeof(HEADER_INFO *);
2228 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
2229 wndClass.lpszClassName = WC_HEADERW;
2231 RegisterClassW (&wndClass);
2235 VOID
2236 HEADER_Unregister (void)
2238 UnregisterClassW (WC_HEADERW, NULL);