gdiplus: Add TRACE(..) for font calls.
[wine/multimedia.git] / dlls / comctl32 / header.c
blob496df62b0f45f891a4c2ae8cff41113d06493a88
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 "imagelist.h"
43 #include "tmschema.h"
44 #include "uxtheme.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(header);
49 typedef struct
51 INT cxy;
52 HBITMAP hbm;
53 LPWSTR pszText;
54 INT fmt;
55 LPARAM lParam;
56 INT iImage;
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 */
62 } HEADER_ITEM;
65 typedef struct
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 INT iMoveItem; /* index of tracked item. (Tracking mode) */
81 INT xTrackOffset; /* distance between the right side of the tracked item and the cursor */
82 INT xOldTrack; /* track offset (see above) after the last WM_MOUSEMOVE */
83 INT iHotItem; /* index of hot item (cursor is over this item) */
84 INT iHotDivider; /* index of the hot divider (used while dragging an item or by HDM_SETHOTDIVIDER) */
85 INT iMargin; /* width of the margin that surrounds a bitmap */
87 HIMAGELIST himl; /* handle to an image list (may be 0) */
88 HEADER_ITEM *items; /* pointer to array of HEADER_ITEM's */
89 INT *order; /* array of item IDs indexed by order */
90 BOOL bRectsValid; /* validity flag for bounding rectangles */
91 } HEADER_INFO;
94 #define VERT_BORDER 4
95 #define DIVIDER_WIDTH 10
96 #define HOT_DIVIDER_WIDTH 2
97 #define MAX_HEADER_TEXT_LEN 260
98 #define HDN_UNICODE_OFFSET 20
99 #define HDN_FIRST_UNICODE (HDN_FIRST-HDN_UNICODE_OFFSET)
101 #define HDI_SUPPORTED_FIELDS (HDI_WIDTH|HDI_TEXT|HDI_FORMAT|HDI_LPARAM|HDI_BITMAP|HDI_IMAGE|HDI_ORDER)
102 #define HDI_UNSUPPORTED_FIELDS (HDI_FILTER)
103 #define HDI_UNKNOWN_FIELDS (~(HDI_SUPPORTED_FIELDS|HDI_UNSUPPORTED_FIELDS|HDI_DI_SETITEM))
104 #define HDI_COMCTL32_4_0_FIELDS (HDI_WIDTH|HDI_TEXT|HDI_FORMAT|HDI_LPARAM|HDI_BITMAP)
106 #define HEADER_GetInfoPtr(hwnd) ((HEADER_INFO *)GetWindowLongPtrW(hwnd,0))
108 static BOOL HEADER_PrepareCallbackItems(HWND hwnd, INT iItem, INT reqMask);
109 static void HEADER_FreeCallbackItems(HEADER_ITEM *lpItem);
110 static LRESULT HEADER_SendNotify(HWND hwnd, UINT code, NMHDR *hdr);
111 static LRESULT HEADER_SendCtrlCustomDraw(HWND hwnd, DWORD dwDrawStage, HDC hdc, const RECT *rect);
113 static const WCHAR themeClass[] = {'H','e','a','d','e','r',0};
115 static void HEADER_StoreHDItemInHeader(HEADER_ITEM *lpItem, UINT mask, const HDITEMW *phdi, BOOL fUnicode)
117 if (mask & HDI_UNSUPPORTED_FIELDS)
118 FIXME("unsupported header fields %x\n", (mask & HDI_UNSUPPORTED_FIELDS));
120 if (mask & HDI_BITMAP)
121 lpItem->hbm = phdi->hbm;
123 if (mask & HDI_FORMAT)
124 lpItem->fmt = phdi->fmt;
126 if (mask & HDI_LPARAM)
127 lpItem->lParam = phdi->lParam;
129 if (mask & HDI_WIDTH)
130 lpItem->cxy = phdi->cxy;
132 if (mask & HDI_IMAGE)
134 lpItem->iImage = phdi->iImage;
135 if (phdi->iImage == I_IMAGECALLBACK)
136 lpItem->callbackMask |= HDI_IMAGE;
137 else
138 lpItem->callbackMask &= ~HDI_IMAGE;
141 if (mask & HDI_TEXT)
143 Free(lpItem->pszText);
144 lpItem->pszText = NULL;
146 if (phdi->pszText != LPSTR_TEXTCALLBACKW) /* covers != TEXTCALLBACKA too */
148 static const WCHAR emptyString[] = {0};
150 LPCWSTR pszText = (phdi->pszText != NULL ? phdi->pszText : emptyString);
151 if (fUnicode)
152 Str_SetPtrW(&lpItem->pszText, pszText);
153 else
154 Str_SetPtrAtoW(&lpItem->pszText, (LPCSTR)pszText);
155 lpItem->callbackMask &= ~HDI_TEXT;
157 else
159 lpItem->pszText = NULL;
160 lpItem->callbackMask |= HDI_TEXT;
165 static inline LRESULT
166 HEADER_IndexToOrder (HWND hwnd, INT iItem)
168 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
169 HEADER_ITEM *lpItem = &infoPtr->items[iItem];
170 return lpItem->iOrder;
174 static INT
175 HEADER_OrderToIndex(HWND hwnd, WPARAM wParam)
177 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
178 INT iorder = (INT)wParam;
180 if ((iorder <0) || iorder >= infoPtr->uNumItem)
181 return iorder;
182 return infoPtr->order[iorder];
185 static void
186 HEADER_ChangeItemOrder(const HEADER_INFO *infoPtr, INT iItem, INT iNewOrder)
188 HEADER_ITEM *lpItem = &infoPtr->items[iItem];
189 INT i, nMin, nMax;
191 TRACE("%d: %d->%d\n", iItem, lpItem->iOrder, iNewOrder);
192 if (lpItem->iOrder < iNewOrder)
194 memmove(&infoPtr->order[lpItem->iOrder],
195 &infoPtr->order[lpItem->iOrder + 1],
196 (iNewOrder - lpItem->iOrder) * sizeof(INT));
198 if (iNewOrder < lpItem->iOrder)
200 memmove(&infoPtr->order[iNewOrder + 1],
201 &infoPtr->order[iNewOrder],
202 (lpItem->iOrder - iNewOrder) * sizeof(INT));
204 infoPtr->order[iNewOrder] = iItem;
205 nMin = min(lpItem->iOrder, iNewOrder);
206 nMax = max(lpItem->iOrder, iNewOrder);
207 for (i = nMin; i <= nMax; i++)
208 infoPtr->items[infoPtr->order[i]].iOrder = i;
211 /* Note: if iItem is the last item then this function returns infoPtr->uNumItem */
212 static INT
213 HEADER_NextItem(HWND hwnd, INT iItem)
215 return HEADER_OrderToIndex(hwnd, HEADER_IndexToOrder(hwnd, iItem)+1);
218 static INT
219 HEADER_PrevItem(HWND hwnd, INT iItem)
221 return HEADER_OrderToIndex(hwnd, HEADER_IndexToOrder(hwnd, iItem)-1);
224 static void
225 HEADER_SetItemBounds (HWND hwnd)
227 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
228 HEADER_ITEM *phdi;
229 RECT rect;
230 unsigned int i;
231 int x;
233 infoPtr->bRectsValid = TRUE;
235 if (infoPtr->uNumItem == 0)
236 return;
238 GetClientRect (hwnd, &rect);
240 x = rect.left;
241 for (i = 0; i < infoPtr->uNumItem; i++) {
242 phdi = &infoPtr->items[HEADER_OrderToIndex(hwnd,i)];
243 phdi->rect.top = rect.top;
244 phdi->rect.bottom = rect.bottom;
245 phdi->rect.left = x;
246 phdi->rect.right = phdi->rect.left + ((phdi->cxy>0)?phdi->cxy:0);
247 x = phdi->rect.right;
251 static LRESULT
252 HEADER_Size (HWND hwnd)
254 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
256 infoPtr->bRectsValid = FALSE;
258 return 0;
261 static void HEADER_GetHotDividerRect(HWND hwnd, const HEADER_INFO *infoPtr, RECT *r)
263 INT iDivider = infoPtr->iHotDivider;
264 if (infoPtr->uNumItem > 0)
266 HEADER_ITEM *lpItem;
268 if (iDivider < infoPtr->uNumItem)
270 lpItem = &infoPtr->items[iDivider];
271 r->left = lpItem->rect.left - HOT_DIVIDER_WIDTH/2;
272 r->right = lpItem->rect.left + HOT_DIVIDER_WIDTH/2;
274 else
276 lpItem = &infoPtr->items[HEADER_OrderToIndex(hwnd, infoPtr->uNumItem-1)];
277 r->left = lpItem->rect.right - HOT_DIVIDER_WIDTH/2;
278 r->right = lpItem->rect.right + HOT_DIVIDER_WIDTH/2;
280 r->top = lpItem->rect.top;
281 r->bottom = lpItem->rect.bottom;
283 else
285 RECT clientRect;
286 GetClientRect(hwnd, &clientRect);
287 *r = clientRect;
288 r->right = r->left + HOT_DIVIDER_WIDTH/2;
293 static INT
294 HEADER_DrawItem (HWND hwnd, HDC hdc, INT iItem, BOOL bHotTrack, LRESULT lCDFlags)
296 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
297 HEADER_ITEM *phdi = &infoPtr->items[iItem];
298 RECT r;
299 INT oldBkMode;
300 HTHEME theme = GetWindowTheme (hwnd);
301 NMCUSTOMDRAW nmcd;
303 TRACE("DrawItem(iItem %d bHotTrack %d unicode flag %d)\n", iItem, bHotTrack, (infoPtr->nNotifyFormat == NFR_UNICODE));
305 r = phdi->rect;
306 if (r.right - r.left == 0)
307 return phdi->rect.right;
309 /* Set the colors before sending NM_CUSTOMDRAW so that it can change them */
310 SetTextColor(hdc, (bHotTrack && !theme) ? COLOR_HIGHLIGHT : COLOR_BTNTEXT);
311 SetBkColor(hdc, GetSysColor(COLOR_3DFACE));
313 if (lCDFlags & CDRF_NOTIFYITEMDRAW && !(phdi->fmt & HDF_OWNERDRAW))
315 LRESULT lCDItemFlags;
317 nmcd.dwDrawStage = CDDS_PREPAINT | CDDS_ITEM;
318 nmcd.hdc = hdc;
319 nmcd.dwItemSpec = iItem;
320 nmcd.rc = r;
321 nmcd.uItemState = phdi->bDown ? CDIS_SELECTED : 0;
322 nmcd.lItemlParam = phdi->lParam;
324 lCDItemFlags = HEADER_SendNotify(hwnd, NM_CUSTOMDRAW, (NMHDR *)&nmcd);
325 if (lCDItemFlags & CDRF_SKIPDEFAULT)
326 return phdi->rect.right;
329 if (theme != NULL) {
330 int state = (phdi->bDown) ? HIS_PRESSED :
331 (bHotTrack ? HIS_HOT : HIS_NORMAL);
332 DrawThemeBackground (theme, hdc, HP_HEADERITEM, state,
333 &r, NULL);
334 GetThemeBackgroundContentRect (theme, hdc, HP_HEADERITEM, state,
335 &r, &r);
337 else {
338 HBRUSH hbr;
340 if (GetWindowLongW (hwnd, GWL_STYLE) & HDS_BUTTONS) {
341 if (phdi->bDown) {
342 DrawEdge (hdc, &r, BDR_RAISEDOUTER,
343 BF_RECT | BF_FLAT | BF_MIDDLE | BF_ADJUST);
345 else
346 DrawEdge (hdc, &r, EDGE_RAISED,
347 BF_RECT | BF_SOFT | BF_MIDDLE | BF_ADJUST);
349 else
350 DrawEdge (hdc, &r, EDGE_ETCHED, BF_BOTTOM | BF_RIGHT | BF_ADJUST);
352 hbr = CreateSolidBrush(GetBkColor(hdc));
353 FillRect(hdc, &r, hbr);
354 DeleteObject(hbr);
356 if (phdi->bDown) {
357 r.left += 2;
358 r.top += 2;
361 if (phdi->fmt & HDF_OWNERDRAW) {
362 DRAWITEMSTRUCT dis;
364 dis.CtlType = ODT_HEADER;
365 dis.CtlID = GetWindowLongPtrW (hwnd, GWLP_ID);
366 dis.itemID = iItem;
367 dis.itemAction = ODA_DRAWENTIRE;
368 dis.itemState = phdi->bDown ? ODS_SELECTED : 0;
369 dis.hwndItem = hwnd;
370 dis.hDC = hdc;
371 dis.rcItem = phdi->rect;
372 dis.itemData = phdi->lParam;
373 oldBkMode = SetBkMode(hdc, TRANSPARENT);
374 SendMessageW (infoPtr->hwndNotify, WM_DRAWITEM,
375 (WPARAM)dis.CtlID, (LPARAM)&dis);
376 if (oldBkMode != TRANSPARENT)
377 SetBkMode(hdc, oldBkMode);
379 else {
380 UINT rw, rh, /* width and height of r */
381 *x = NULL, *w = NULL; /* x and width of the pic (bmp or img) which is part of cnt */
382 /* cnt,txt,img,bmp */
383 UINT cx, tx, ix, bx,
384 cw, tw, iw, bw;
385 BITMAP bmp;
387 HEADER_PrepareCallbackItems(hwnd, iItem, HDI_TEXT|HDI_IMAGE);
388 cw = tw = iw = bw = 0;
389 rw = r.right - r.left;
390 rh = r.bottom - r.top;
392 if (phdi->fmt & HDF_STRING) {
393 RECT textRect;
395 SetRectEmpty(&textRect);
396 DrawTextW (hdc, phdi->pszText, -1,
397 &textRect, DT_LEFT|DT_VCENTER|DT_SINGLELINE|DT_CALCRECT);
398 cw = textRect.right - textRect.left + 2 * infoPtr->iMargin;
401 if ((phdi->fmt & HDF_IMAGE) && (infoPtr->himl)) {
402 iw = infoPtr->himl->cx + 2 * infoPtr->iMargin;
403 x = &ix;
404 w = &iw;
407 if ((phdi->fmt & HDF_BITMAP) && (phdi->hbm)) {
408 GetObjectW (phdi->hbm, sizeof(BITMAP), (LPVOID)&bmp);
409 bw = bmp.bmWidth + 2 * infoPtr->iMargin;
410 if (!iw) {
411 x = &bx;
412 w = &bw;
416 if (bw || iw)
417 cw += *w;
419 /* align cx using the unclipped cw */
420 if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_LEFT)
421 cx = r.left;
422 else if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_CENTER)
423 cx = r.left + rw / 2 - cw / 2;
424 else /* HDF_RIGHT */
425 cx = r.right - cw;
427 /* clip cx & cw */
428 if (cx < r.left)
429 cx = r.left;
430 if (cx + cw > r.right)
431 cw = r.right - cx;
433 tx = cx + infoPtr->iMargin;
434 /* since cw might have changed we have to recalculate tw */
435 tw = cw - infoPtr->iMargin * 2;
437 if (iw || bw) {
438 tw -= *w;
439 if (phdi->fmt & HDF_BITMAP_ON_RIGHT) {
440 /* put pic behind text */
441 *x = cx + tw + infoPtr->iMargin * 3;
442 } else {
443 *x = cx + infoPtr->iMargin;
444 /* move text behind pic */
445 tx += *w;
449 if (iw && bw) {
450 /* since we're done with the layout we can
451 now calculate the position of bmp which
452 has no influence on alignment and layout
453 because of img */
454 if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_RIGHT)
455 bx = cx - bw + infoPtr->iMargin;
456 else
457 bx = cx + cw + infoPtr->iMargin;
460 if (iw || bw) {
461 HDC hClipDC = GetDC(hwnd);
462 HRGN hClipRgn = CreateRectRgn(r.left, r.top, r.right, r.bottom);
463 SelectClipRgn(hClipDC, hClipRgn);
465 if (bw) {
466 HDC hdcBitmap = CreateCompatibleDC (hClipDC);
467 SelectObject (hdcBitmap, phdi->hbm);
468 BitBlt (hClipDC, bx, r.top + ((INT)rh - bmp.bmHeight) / 2,
469 bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
470 DeleteDC (hdcBitmap);
473 if (iw) {
474 ImageList_DrawEx (infoPtr->himl, phdi->iImage, hClipDC,
475 ix, r.top + ((INT)rh - infoPtr->himl->cy) / 2,
476 infoPtr->himl->cx, infoPtr->himl->cy, CLR_DEFAULT, CLR_DEFAULT, 0);
479 DeleteObject(hClipRgn);
480 ReleaseDC(hwnd, hClipDC);
483 if (((phdi->fmt & HDF_STRING)
484 || (!(phdi->fmt & (HDF_OWNERDRAW|HDF_STRING|HDF_BITMAP|
485 HDF_BITMAP_ON_RIGHT|HDF_IMAGE)))) /* no explicit format specified? */
486 && (phdi->pszText)) {
487 oldBkMode = SetBkMode(hdc, TRANSPARENT);
488 r.left = tx;
489 r.right = tx + tw;
490 DrawTextW (hdc, phdi->pszText, -1,
491 &r, DT_LEFT|DT_END_ELLIPSIS|DT_VCENTER|DT_SINGLELINE);
492 if (oldBkMode != TRANSPARENT)
493 SetBkMode(hdc, oldBkMode);
495 HEADER_FreeCallbackItems(phdi);
496 }/*Ownerdrawn*/
498 return phdi->rect.right;
501 static void
502 HEADER_DrawHotDivider(HWND hwnd, HDC hdc)
504 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
505 HBRUSH brush;
506 RECT r;
508 HEADER_GetHotDividerRect(hwnd, infoPtr, &r);
509 brush = CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT));
510 FillRect(hdc, &r, brush);
511 DeleteObject(brush);
514 static void
515 HEADER_Refresh (HWND hwnd, HDC hdc)
517 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
518 HFONT hFont, hOldFont;
519 RECT rect, rcRest;
520 HBRUSH hbrBk;
521 UINT i;
522 INT x;
523 LRESULT lCDFlags;
524 HTHEME theme = GetWindowTheme (hwnd);
526 if (!infoPtr->bRectsValid)
527 HEADER_SetItemBounds(hwnd);
529 /* get rect for the bar, adjusted for the border */
530 GetClientRect (hwnd, &rect);
531 lCDFlags = HEADER_SendCtrlCustomDraw(hwnd, CDDS_PREPAINT, hdc, &rect);
533 if (infoPtr->bDragging)
534 ImageList_DragShowNolock(FALSE);
536 hFont = infoPtr->hFont ? infoPtr->hFont : GetStockObject (SYSTEM_FONT);
537 hOldFont = SelectObject (hdc, hFont);
539 /* draw Background */
540 if (infoPtr->uNumItem == 0 && theme == NULL) {
541 hbrBk = GetSysColorBrush(COLOR_3DFACE);
542 FillRect(hdc, &rect, hbrBk);
545 x = rect.left;
546 for (i = 0; x <= rect.right && i < infoPtr->uNumItem; i++) {
547 int idx = HEADER_OrderToIndex(hwnd,i);
548 if (RectVisible(hdc, &infoPtr->items[idx].rect))
549 HEADER_DrawItem(hwnd, hdc, idx, infoPtr->iHotItem == idx, lCDFlags);
550 x = infoPtr->items[idx].rect.right;
553 rcRest = rect;
554 rcRest.left = x;
555 if ((x <= rect.right) && RectVisible(hdc, &rcRest) && (infoPtr->uNumItem > 0)) {
556 if (theme != NULL) {
557 DrawThemeBackground(theme, hdc, HP_HEADERITEM, HIS_NORMAL, &rcRest, NULL);
559 else {
560 if (GetWindowLongW (hwnd, GWL_STYLE) & HDS_BUTTONS)
561 DrawEdge (hdc, &rcRest, EDGE_RAISED, BF_TOP|BF_LEFT|BF_BOTTOM|BF_SOFT|BF_MIDDLE);
562 else
563 DrawEdge (hdc, &rcRest, EDGE_ETCHED, BF_BOTTOM|BF_MIDDLE);
567 if (infoPtr->iHotDivider != -1)
568 HEADER_DrawHotDivider(hwnd, hdc);
570 if (infoPtr->bDragging)
571 ImageList_DragShowNolock(TRUE);
572 SelectObject (hdc, hOldFont);
574 if (lCDFlags & CDRF_NOTIFYPOSTPAINT)
575 HEADER_SendCtrlCustomDraw(hwnd, CDDS_POSTPAINT, hdc, &rect);
579 static void
580 HEADER_RefreshItem (HWND hwnd, INT iItem)
582 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
584 if (!infoPtr->bRectsValid)
585 HEADER_SetItemBounds(hwnd);
587 InvalidateRect(hwnd, &infoPtr->items[iItem].rect, FALSE);
591 static void
592 HEADER_InternalHitTest (HWND hwnd, const POINT *lpPt, UINT *pFlags, INT *pItem)
594 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
595 RECT rect, rcTest;
596 UINT iCount;
597 INT width;
598 BOOL bNoWidth;
600 GetClientRect (hwnd, &rect);
602 *pFlags = 0;
603 bNoWidth = FALSE;
604 if (PtInRect (&rect, *lpPt))
606 if (infoPtr->uNumItem == 0) {
607 *pFlags |= HHT_NOWHERE;
608 *pItem = 1;
609 TRACE("NOWHERE\n");
610 return;
612 else {
613 /* somewhere inside */
614 for (iCount = 0; iCount < infoPtr->uNumItem; iCount++) {
615 rect = infoPtr->items[iCount].rect;
616 width = rect.right - rect.left;
617 if (width == 0) {
618 bNoWidth = TRUE;
619 continue;
621 if (PtInRect (&rect, *lpPt)) {
622 if (width <= 2 * DIVIDER_WIDTH) {
623 *pFlags |= HHT_ONHEADER;
624 *pItem = iCount;
625 TRACE("ON HEADER %d\n", iCount);
626 return;
628 if (HEADER_IndexToOrder(hwnd, iCount) > 0) {
629 rcTest = rect;
630 rcTest.right = rcTest.left + DIVIDER_WIDTH;
631 if (PtInRect (&rcTest, *lpPt)) {
632 if (bNoWidth) {
633 *pFlags |= HHT_ONDIVOPEN;
634 *pItem = HEADER_PrevItem(hwnd, iCount);
635 TRACE("ON DIVOPEN %d\n", *pItem);
636 return;
638 else {
639 *pFlags |= HHT_ONDIVIDER;
640 *pItem = HEADER_PrevItem(hwnd, iCount);
641 TRACE("ON DIVIDER %d\n", *pItem);
642 return;
646 rcTest = rect;
647 rcTest.left = rcTest.right - DIVIDER_WIDTH;
648 if (PtInRect (&rcTest, *lpPt)) {
649 *pFlags |= HHT_ONDIVIDER;
650 *pItem = iCount;
651 TRACE("ON DIVIDER %d\n", *pItem);
652 return;
655 *pFlags |= HHT_ONHEADER;
656 *pItem = iCount;
657 TRACE("ON HEADER %d\n", iCount);
658 return;
662 /* check for last divider part (on nowhere) */
663 rect = infoPtr->items[infoPtr->uNumItem-1].rect;
664 rect.left = rect.right;
665 rect.right += DIVIDER_WIDTH;
666 if (PtInRect (&rect, *lpPt)) {
667 if (bNoWidth) {
668 *pFlags |= HHT_ONDIVOPEN;
669 *pItem = infoPtr->uNumItem - 1;
670 TRACE("ON DIVOPEN %d\n", *pItem);
671 return;
673 else {
674 *pFlags |= HHT_ONDIVIDER;
675 *pItem = infoPtr->uNumItem-1;
676 TRACE("ON DIVIDER %d\n", *pItem);
677 return;
681 *pFlags |= HHT_NOWHERE;
682 *pItem = 1;
683 TRACE("NOWHERE\n");
684 return;
687 else {
688 if (lpPt->x < rect.left) {
689 TRACE("TO LEFT\n");
690 *pFlags |= HHT_TOLEFT;
692 else if (lpPt->x > rect.right) {
693 TRACE("TO RIGHT\n");
694 *pFlags |= HHT_TORIGHT;
697 if (lpPt->y < rect.top) {
698 TRACE("ABOVE\n");
699 *pFlags |= HHT_ABOVE;
701 else if (lpPt->y > rect.bottom) {
702 TRACE("BELOW\n");
703 *pFlags |= HHT_BELOW;
707 *pItem = 1;
708 TRACE("flags=0x%X\n", *pFlags);
709 return;
713 static void
714 HEADER_DrawTrackLine (HWND hwnd, HDC hdc, INT x)
716 RECT rect;
717 HPEN hOldPen;
718 INT oldRop;
720 GetClientRect (hwnd, &rect);
722 hOldPen = SelectObject (hdc, GetStockObject (BLACK_PEN));
723 oldRop = SetROP2 (hdc, R2_XORPEN);
724 MoveToEx (hdc, x, rect.top, NULL);
725 LineTo (hdc, x, rect.bottom);
726 SetROP2 (hdc, oldRop);
727 SelectObject (hdc, hOldPen);
730 /***
731 * DESCRIPTION:
732 * Convert a HDITEM into the correct format (ANSI/Unicode) to send it in a notify
734 * PARAMETER(S):
735 * [I] infoPtr : the header that wants to send the notify
736 * [O] dest : The buffer to store the HDITEM for notify. It may be set to a HDITEMA of HDITEMW
737 * [I] src : The source HDITEM. It may be a HDITEMA or HDITEMW
738 * [I] fSourceUnicode : is src a HDITEMW or HDITEMA
739 * [O] ppvScratch : a pointer to a scratch buffer that needs to be freed after
740 * the HDITEM is no longer in use or NULL if none was needed
742 * NOTE: We depend on HDITEMA and HDITEMW having the same structure
744 static void HEADER_CopyHDItemForNotify(const HEADER_INFO *infoPtr, HDITEMW *dest,
745 const HDITEMW *src, BOOL fSourceUnicode, LPVOID *ppvScratch)
747 *ppvScratch = NULL;
748 *dest = *src;
750 if (src->mask & HDI_TEXT && src->pszText != LPSTR_TEXTCALLBACKW) /* covers TEXTCALLBACKA as well */
752 if (fSourceUnicode && infoPtr->nNotifyFormat != NFR_UNICODE)
754 dest->pszText = NULL;
755 Str_SetPtrWtoA((LPSTR *)&dest->pszText, src->pszText);
756 *ppvScratch = dest->pszText;
759 if (!fSourceUnicode && infoPtr->nNotifyFormat == NFR_UNICODE)
761 dest->pszText = NULL;
762 Str_SetPtrAtoW(&dest->pszText, (LPSTR)src->pszText);
763 *ppvScratch = dest->pszText;
768 static UINT HEADER_NotifyCodeWtoA(UINT code)
770 /* we use the fact that all the unicode messages are in HDN_FIRST_UNICODE..HDN_LAST*/
771 if (code >= HDN_LAST && code <= HDN_FIRST_UNICODE)
772 return code + HDN_UNICODE_OFFSET;
773 else
774 return code;
777 static LRESULT
778 HEADER_SendNotify(HWND hwnd, UINT code, NMHDR *nmhdr)
780 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
782 nmhdr->hwndFrom = hwnd;
783 nmhdr->idFrom = GetWindowLongPtrW (hwnd, GWLP_ID);
784 nmhdr->code = code;
786 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
787 nmhdr->idFrom, (LPARAM)nmhdr);
790 static BOOL
791 HEADER_SendSimpleNotify (HWND hwnd, UINT code)
793 NMHDR nmhdr;
794 return (BOOL)HEADER_SendNotify(hwnd, code, &nmhdr);
797 static LRESULT
798 HEADER_SendCtrlCustomDraw(HWND hwnd, DWORD dwDrawStage, HDC hdc, const RECT *rect)
800 NMCUSTOMDRAW nm;
801 nm.dwDrawStage = dwDrawStage;
802 nm.hdc = hdc;
803 nm.rc = *rect;
804 nm.dwItemSpec = 0;
805 nm.uItemState = 0;
806 nm.lItemlParam = 0;
808 return HEADER_SendNotify(hwnd, NM_CUSTOMDRAW, (NMHDR *)&nm);
811 static BOOL
812 HEADER_SendNotifyWithHDItemT(HWND hwnd, UINT code, INT iItem, HDITEMW *lpItem)
814 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
815 NMHEADERW nmhdr;
817 if (infoPtr->nNotifyFormat != NFR_UNICODE)
818 code = HEADER_NotifyCodeWtoA(code);
819 nmhdr.iItem = iItem;
820 nmhdr.iButton = 0;
821 nmhdr.pitem = lpItem;
823 return (BOOL)HEADER_SendNotify(hwnd, code, (NMHDR *)&nmhdr);
826 static BOOL
827 HEADER_SendNotifyWithIntFieldT(HWND hwnd, UINT code, INT iItem, INT mask, INT iValue)
829 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
830 HDITEMW nmitem;
832 /* copying only the iValue should be ok but to make the code more robust we copy everything */
833 nmitem.cxy = infoPtr->items[iItem].cxy;
834 nmitem.hbm = infoPtr->items[iItem].hbm;
835 nmitem.pszText = NULL;
836 nmitem.cchTextMax = 0;
837 nmitem.fmt = infoPtr->items[iItem].fmt;
838 nmitem.lParam = infoPtr->items[iItem].lParam;
839 nmitem.iOrder = infoPtr->items[iItem].iOrder;
840 nmitem.iImage = infoPtr->items[iItem].iImage;
842 nmitem.mask = mask;
843 switch (mask)
845 case HDI_WIDTH:
846 nmitem.cxy = iValue;
847 break;
848 case HDI_ORDER:
849 nmitem.iOrder = iValue;
850 break;
851 default:
852 ERR("invalid mask value 0x%x\n", iValue);
855 return HEADER_SendNotifyWithHDItemT(hwnd, code, iItem, &nmitem);
859 * Prepare callback items
860 * depends on NMHDDISPINFOW having same structure as NMHDDISPINFOA
861 * (so we handle the two cases only doing a specific cast for pszText).
862 * Checks if any of the required field are callback. If there are sends a
863 * NMHDISPINFO notify to retrieve these items. The items are stored in the
864 * HEADER_ITEM pszText and iImage fields. They should be freed with
865 * HEADER_FreeCallbackItems.
867 * @param hwnd : hwnd header container handler
868 * @param iItem : the header item id
869 * @param reqMask : required fields. If any of them is callback this function will fetch it
871 * @return TRUE on success, else FALSE
873 static BOOL
874 HEADER_PrepareCallbackItems(HWND hwnd, INT iItem, INT reqMask)
876 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
877 HEADER_ITEM *lpItem = &infoPtr->items[iItem];
878 DWORD mask = reqMask & lpItem->callbackMask;
879 NMHDDISPINFOW dispInfo;
880 void *pvBuffer = NULL;
882 if (mask == 0)
883 return TRUE;
884 if (mask&HDI_TEXT && lpItem->pszText != NULL)
886 ERR("(): function called without a call to FreeCallbackItems\n");
887 Free(lpItem->pszText);
888 lpItem->pszText = NULL;
891 memset(&dispInfo, 0, sizeof(NMHDDISPINFOW));
892 dispInfo.hdr.hwndFrom = hwnd;
893 dispInfo.hdr.idFrom = GetWindowLongPtrW (hwnd, GWLP_ID);
894 if (infoPtr->nNotifyFormat == NFR_UNICODE)
896 dispInfo.hdr.code = HDN_GETDISPINFOW;
897 if (mask & HDI_TEXT)
898 pvBuffer = Alloc(MAX_HEADER_TEXT_LEN * sizeof(WCHAR));
900 else
902 dispInfo.hdr.code = HDN_GETDISPINFOA;
903 if (mask & HDI_TEXT)
904 pvBuffer = Alloc(MAX_HEADER_TEXT_LEN * sizeof(CHAR));
906 dispInfo.pszText = (LPWSTR)pvBuffer;
907 dispInfo.cchTextMax = (pvBuffer!=NULL?MAX_HEADER_TEXT_LEN:0);
908 dispInfo.iItem = iItem;
909 dispInfo.mask = mask;
910 dispInfo.lParam = lpItem->lParam;
912 TRACE("Sending HDN_GETDISPINFO%c\n", infoPtr->nNotifyFormat == NFR_UNICODE?'W':'A');
913 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, dispInfo.hdr.idFrom, (LPARAM)&dispInfo);
915 TRACE("SendMessage returns(mask:0x%x,str:%s,lParam:%p)\n",
916 dispInfo.mask,
917 (infoPtr->nNotifyFormat == NFR_UNICODE ? debugstr_w(dispInfo.pszText) : (LPSTR) dispInfo.pszText),
918 (void*) dispInfo.lParam);
920 if (mask & HDI_IMAGE)
921 lpItem->iImage = dispInfo.iImage;
922 if (mask & HDI_TEXT)
924 if (infoPtr->nNotifyFormat == NFR_UNICODE)
926 lpItem->pszText = (LPWSTR)pvBuffer;
928 /* the user might have used his own buffer */
929 if (dispInfo.pszText != lpItem->pszText)
930 Str_GetPtrW(dispInfo.pszText, lpItem->pszText, MAX_HEADER_TEXT_LEN);
932 else
934 Str_SetPtrAtoW(&lpItem->pszText, (LPSTR)dispInfo.pszText);
935 Free(pvBuffer);
939 if (dispInfo.mask & HDI_DI_SETITEM)
941 /* make the items permanent */
942 lpItem->callbackMask &= ~dispInfo.mask;
945 return TRUE;
948 /***
949 * DESCRIPTION:
950 * Free the items that might be allocated with HEADER_PrepareCallbackItems
952 * PARAMETER(S):
953 * [I] lpItem : the item to free the data
956 static void
957 HEADER_FreeCallbackItems(HEADER_ITEM *lpItem)
959 if (lpItem->callbackMask&HDI_TEXT)
961 Free(lpItem->pszText);
962 lpItem->pszText = NULL;
965 if (lpItem->callbackMask&HDI_IMAGE)
966 lpItem->iImage = I_IMAGECALLBACK;
969 static LRESULT
970 HEADER_CreateDragImage (HWND hwnd, WPARAM wParam)
972 HEADER_INFO *infoPtr = HEADER_GetInfoPtr(hwnd);
973 HEADER_ITEM *lpItem;
974 HIMAGELIST himl;
975 HBITMAP hMemory, hOldBitmap;
976 LRESULT lCDFlags;
977 RECT rc;
978 HDC hMemoryDC;
979 HDC hDeviceDC;
980 int height, width;
981 HFONT hFont;
983 if (wParam >= infoPtr->uNumItem)
984 return FALSE;
986 if (!infoPtr->bRectsValid)
987 HEADER_SetItemBounds(hwnd);
989 lpItem = &infoPtr->items[wParam];
990 width = lpItem->rect.right - lpItem->rect.left;
991 height = lpItem->rect.bottom - lpItem->rect.top;
993 hDeviceDC = GetDC(NULL);
994 hMemoryDC = CreateCompatibleDC(hDeviceDC);
995 hMemory = CreateCompatibleBitmap(hDeviceDC, width, height);
996 ReleaseDC(NULL, hDeviceDC);
997 hOldBitmap = SelectObject(hMemoryDC, hMemory);
998 SetViewportOrgEx(hMemoryDC, -lpItem->rect.left, -lpItem->rect.top, NULL);
999 hFont = infoPtr->hFont ? infoPtr->hFont : GetStockObject(SYSTEM_FONT);
1000 SelectObject(hMemoryDC, hFont);
1002 GetClientRect(hwnd, &rc);
1003 lCDFlags = HEADER_SendCtrlCustomDraw(hwnd, CDDS_PREPAINT, hMemoryDC, &rc);
1004 HEADER_DrawItem(hwnd, hMemoryDC, wParam, FALSE, lCDFlags);
1005 if (lCDFlags & CDRF_NOTIFYPOSTPAINT)
1006 HEADER_SendCtrlCustomDraw(hwnd, CDDS_POSTPAINT, hMemoryDC, &rc);
1008 hMemory = SelectObject(hMemoryDC, hOldBitmap);
1009 DeleteDC(hMemoryDC);
1011 if (hMemory == NULL) /* if anything failed */
1012 return FALSE;
1014 himl = ImageList_Create(width, height, ILC_COLORDDB, 1, 1);
1015 ImageList_Add(himl, hMemory, NULL);
1016 DeleteObject(hMemory);
1017 return (LRESULT)himl;
1020 static LRESULT
1021 HEADER_SetHotDivider(HWND hwnd, WPARAM wParam, LPARAM lParam)
1023 HEADER_INFO *infoPtr = HEADER_GetInfoPtr(hwnd);
1024 INT iDivider;
1025 RECT r;
1027 if (wParam)
1029 POINT pt;
1030 UINT flags;
1031 pt.x = (INT)(SHORT)LOWORD(lParam);
1032 pt.y = 0;
1033 HEADER_InternalHitTest (hwnd, &pt, &flags, &iDivider);
1035 if (flags & HHT_TOLEFT)
1036 iDivider = 0;
1037 else if (flags & HHT_NOWHERE || flags & HHT_TORIGHT)
1038 iDivider = infoPtr->uNumItem;
1039 else
1041 HEADER_ITEM *lpItem = &infoPtr->items[iDivider];
1042 if (pt.x > (lpItem->rect.left+lpItem->rect.right)/2)
1043 iDivider = HEADER_NextItem(hwnd, iDivider);
1046 else
1047 iDivider = (INT)lParam;
1049 /* Note; wParam==FALSE, lParam==-1 is valid and is used to clear the hot divider */
1050 if (iDivider<-1 || iDivider>(int)infoPtr->uNumItem)
1051 return iDivider;
1053 if (iDivider != infoPtr->iHotDivider)
1055 if (infoPtr->iHotDivider != -1)
1057 HEADER_GetHotDividerRect(hwnd, infoPtr, &r);
1058 InvalidateRect(hwnd, &r, FALSE);
1060 infoPtr->iHotDivider = iDivider;
1061 if (iDivider != -1)
1063 HEADER_GetHotDividerRect(hwnd, infoPtr, &r);
1064 InvalidateRect(hwnd, &r, FALSE);
1067 return iDivider;
1070 static LRESULT
1071 HEADER_DeleteItem (HWND hwnd, WPARAM wParam)
1073 HEADER_INFO *infoPtr = HEADER_GetInfoPtr(hwnd);
1074 INT iItem = (INT)wParam;
1075 INT iOrder;
1076 UINT i;
1078 TRACE("[iItem=%d]\n", iItem);
1080 if ((iItem < 0) || (iItem >= (INT)infoPtr->uNumItem))
1081 return FALSE;
1083 for (i = 0; i < infoPtr->uNumItem; i++)
1084 TRACE("%d: order=%d, iOrder=%d, ->iOrder=%d\n", i, infoPtr->order[i], infoPtr->items[i].iOrder, infoPtr->items[infoPtr->order[i]].iOrder);
1086 iOrder = infoPtr->items[iItem].iOrder;
1087 Free(infoPtr->items[iItem].pszText);
1089 infoPtr->uNumItem--;
1090 memmove(&infoPtr->items[iItem], &infoPtr->items[iItem + 1],
1091 (infoPtr->uNumItem - iItem) * sizeof(HEADER_ITEM));
1092 memmove(&infoPtr->order[iOrder], &infoPtr->order[iOrder + 1],
1093 (infoPtr->uNumItem - iOrder) * sizeof(INT));
1094 infoPtr->items = ReAlloc(infoPtr->items, sizeof(HEADER_ITEM) * infoPtr->uNumItem);
1095 infoPtr->order = ReAlloc(infoPtr->order, sizeof(INT) * infoPtr->uNumItem);
1097 /* Correct the orders */
1098 for (i = 0; i < infoPtr->uNumItem; i++)
1100 if (infoPtr->order[i] > iItem)
1101 infoPtr->order[i]--;
1102 if (i >= iOrder)
1103 infoPtr->items[infoPtr->order[i]].iOrder = i;
1105 for (i = 0; i < infoPtr->uNumItem; i++)
1106 TRACE("%d: order=%d, iOrder=%d, ->iOrder=%d\n", i, infoPtr->order[i], infoPtr->items[i].iOrder, infoPtr->items[infoPtr->order[i]].iOrder);
1108 HEADER_SetItemBounds (hwnd);
1109 InvalidateRect(hwnd, NULL, FALSE);
1111 return TRUE;
1115 static LRESULT
1116 HEADER_GetImageList (HWND hwnd)
1118 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1120 return (LRESULT)infoPtr->himl;
1124 static LRESULT
1125 HEADER_GetItemT (HWND hwnd, INT nItem, LPHDITEMW phdi, BOOL bUnicode)
1127 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1128 HEADER_ITEM *lpItem;
1129 UINT mask;
1131 if (!phdi)
1132 return FALSE;
1134 TRACE("[nItem=%d]\n", nItem);
1136 mask = phdi->mask;
1137 if (mask == 0)
1138 return TRUE;
1140 if ((nItem < 0) || (nItem >= (INT)infoPtr->uNumItem))
1141 return FALSE;
1143 if (mask & HDI_UNKNOWN_FIELDS)
1145 TRACE("mask %x contains unknown fields. Using only comctl32 4.0 fields\n", mask);
1146 mask &= HDI_COMCTL32_4_0_FIELDS;
1149 lpItem = &infoPtr->items[nItem];
1150 HEADER_PrepareCallbackItems(hwnd, nItem, mask);
1152 if (mask & HDI_BITMAP)
1153 phdi->hbm = lpItem->hbm;
1155 if (mask & HDI_FORMAT)
1156 phdi->fmt = lpItem->fmt;
1158 if (mask & HDI_WIDTH)
1159 phdi->cxy = lpItem->cxy;
1161 if (mask & HDI_LPARAM)
1162 phdi->lParam = lpItem->lParam;
1164 if (mask & HDI_IMAGE)
1165 phdi->iImage = lpItem->iImage;
1167 if (mask & HDI_ORDER)
1168 phdi->iOrder = lpItem->iOrder;
1170 if (mask & HDI_TEXT)
1172 if (bUnicode)
1173 Str_GetPtrW (lpItem->pszText, phdi->pszText, phdi->cchTextMax);
1174 else
1175 Str_GetPtrWtoA (lpItem->pszText, (LPSTR)phdi->pszText, phdi->cchTextMax);
1178 HEADER_FreeCallbackItems(lpItem);
1179 return TRUE;
1183 static inline LRESULT
1184 HEADER_GetItemCount (HWND hwnd)
1186 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1187 return infoPtr->uNumItem;
1191 static LRESULT
1192 HEADER_GetItemRect (HWND hwnd, WPARAM wParam, LPARAM lParam)
1194 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1195 INT iItem = (INT)wParam;
1196 LPRECT lpRect = (LPRECT)lParam;
1198 if ((iItem < 0) || (iItem >= (INT)infoPtr->uNumItem))
1199 return FALSE;
1201 lpRect->left = infoPtr->items[iItem].rect.left;
1202 lpRect->right = infoPtr->items[iItem].rect.right;
1203 lpRect->top = infoPtr->items[iItem].rect.top;
1204 lpRect->bottom = infoPtr->items[iItem].rect.bottom;
1206 return TRUE;
1210 static LRESULT
1211 HEADER_GetOrderArray(HWND hwnd, WPARAM wParam, LPARAM lParam)
1213 LPINT order = (LPINT) lParam;
1214 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1216 if ((unsigned int)wParam <infoPtr->uNumItem)
1217 return FALSE;
1219 memcpy(order, infoPtr->order, infoPtr->uNumItem * sizeof(INT));
1220 return TRUE;
1223 static LRESULT
1224 HEADER_SetOrderArray(HWND hwnd, WPARAM wParam, LPARAM lParam)
1226 int i;
1227 LPINT order = (LPINT) lParam;
1228 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1229 HEADER_ITEM *lpItem;
1231 if ((unsigned int)wParam <infoPtr->uNumItem)
1232 return FALSE;
1233 memcpy(infoPtr->order, order, infoPtr->uNumItem * sizeof(INT));
1234 for (i=0; i<(int)wParam; i++)
1236 lpItem = &infoPtr->items[*order++];
1237 lpItem->iOrder=i;
1239 infoPtr->bRectsValid=0;
1240 InvalidateRect(hwnd, NULL, FALSE);
1241 return TRUE;
1244 static inline LRESULT
1245 HEADER_GetUnicodeFormat (HWND hwnd)
1247 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1248 return (infoPtr->nNotifyFormat == NFR_UNICODE);
1252 static LRESULT
1253 HEADER_HitTest (HWND hwnd, LPARAM lParam)
1255 LPHDHITTESTINFO phti = (LPHDHITTESTINFO)lParam;
1257 HEADER_InternalHitTest (hwnd, &phti->pt, &phti->flags, &phti->iItem);
1259 if (phti->flags == HHT_NOWHERE)
1260 return -1;
1261 else
1262 return phti->iItem;
1266 static LRESULT
1267 HEADER_InsertItemT (HWND hwnd, INT nItem, const HDITEMW *phdi, BOOL bUnicode)
1269 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1270 HEADER_ITEM *lpItem;
1271 INT iOrder;
1272 UINT i;
1273 UINT copyMask;
1275 if ((phdi == NULL) || (nItem < 0) || (phdi->mask == 0))
1276 return -1;
1278 if (nItem > infoPtr->uNumItem)
1279 nItem = infoPtr->uNumItem;
1281 iOrder = (phdi->mask & HDI_ORDER) ? phdi->iOrder : nItem;
1282 if (iOrder < 0)
1283 iOrder = 0;
1284 else if (infoPtr->uNumItem < iOrder)
1285 iOrder = infoPtr->uNumItem;
1287 infoPtr->uNumItem++;
1288 infoPtr->items = ReAlloc(infoPtr->items, sizeof(HEADER_ITEM) * infoPtr->uNumItem);
1289 infoPtr->order = ReAlloc(infoPtr->order, sizeof(INT) * infoPtr->uNumItem);
1291 /* make space for the new item */
1292 memmove(&infoPtr->items[nItem + 1], &infoPtr->items[nItem],
1293 (infoPtr->uNumItem - nItem - 1) * sizeof(HEADER_ITEM));
1294 memmove(&infoPtr->order[iOrder + 1], &infoPtr->order[iOrder],
1295 (infoPtr->uNumItem - iOrder - 1) * sizeof(INT));
1297 /* update the order array */
1298 infoPtr->order[iOrder] = nItem;
1299 for (i = 0; i < infoPtr->uNumItem; i++)
1301 if (i != iOrder && infoPtr->order[i] >= nItem)
1302 infoPtr->order[i]++;
1303 infoPtr->items[infoPtr->order[i]].iOrder = i;
1306 lpItem = &infoPtr->items[nItem];
1307 ZeroMemory(lpItem, sizeof(HEADER_ITEM));
1308 /* cxy, fmt and lParam are copied even if not in the HDITEM mask */
1309 copyMask = phdi->mask | HDI_WIDTH | HDI_FORMAT | HDI_LPARAM;
1310 HEADER_StoreHDItemInHeader(lpItem, copyMask, phdi, bUnicode);
1311 lpItem->iOrder = iOrder;
1313 /* set automatically some format bits */
1314 if (phdi->mask & HDI_TEXT)
1315 lpItem->fmt |= HDF_STRING;
1316 else
1317 lpItem->fmt &= ~HDF_STRING;
1319 if (lpItem->hbm != NULL)
1320 lpItem->fmt |= HDF_BITMAP;
1321 else
1322 lpItem->fmt &= ~HDF_BITMAP;
1324 if (phdi->mask & HDI_IMAGE)
1325 lpItem->fmt |= HDF_IMAGE;
1327 HEADER_SetItemBounds (hwnd);
1328 InvalidateRect(hwnd, NULL, FALSE);
1330 return nItem;
1334 static LRESULT
1335 HEADER_Layout (HWND hwnd, LPARAM lParam)
1337 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1338 LPHDLAYOUT lpLayout = (LPHDLAYOUT)lParam;
1340 lpLayout->pwpos->hwnd = hwnd;
1341 lpLayout->pwpos->hwndInsertAfter = 0;
1342 lpLayout->pwpos->x = lpLayout->prc->left;
1343 lpLayout->pwpos->y = lpLayout->prc->top;
1344 lpLayout->pwpos->cx = lpLayout->prc->right - lpLayout->prc->left;
1345 if (GetWindowLongW (hwnd, GWL_STYLE) & HDS_HIDDEN)
1346 lpLayout->pwpos->cy = 0;
1347 else {
1348 lpLayout->pwpos->cy = infoPtr->nHeight;
1349 lpLayout->prc->top += infoPtr->nHeight;
1351 lpLayout->pwpos->flags = SWP_NOZORDER;
1353 TRACE("Layout x=%d y=%d cx=%d cy=%d\n",
1354 lpLayout->pwpos->x, lpLayout->pwpos->y,
1355 lpLayout->pwpos->cx, lpLayout->pwpos->cy);
1357 infoPtr->bRectsValid = FALSE;
1359 return TRUE;
1363 static LRESULT
1364 HEADER_SetImageList (HWND hwnd, HIMAGELIST himl)
1366 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1367 HIMAGELIST himlOld;
1369 TRACE("(himl %p)\n", himl);
1370 himlOld = infoPtr->himl;
1371 infoPtr->himl = himl;
1373 /* FIXME: Refresh needed??? */
1375 return (LRESULT)himlOld;
1379 static LRESULT
1380 HEADER_GetBitmapMargin(HWND hwnd)
1382 HEADER_INFO *infoPtr = HEADER_GetInfoPtr(hwnd);
1384 return infoPtr->iMargin;
1387 static LRESULT
1388 HEADER_SetBitmapMargin(HWND hwnd, WPARAM wParam)
1390 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1391 INT oldMargin = infoPtr->iMargin;
1393 infoPtr->iMargin = (INT)wParam;
1395 return oldMargin;
1398 static LRESULT
1399 HEADER_SetItemT (HWND hwnd, INT nItem, const HDITEMW *phdi, BOOL bUnicode)
1401 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1402 HEADER_ITEM *lpItem;
1403 HDITEMW hdNotify;
1404 void *pvScratch;
1406 if (phdi == NULL)
1407 return FALSE;
1408 if ((nItem < 0) || (nItem >= (INT)infoPtr->uNumItem))
1409 return FALSE;
1411 TRACE("[nItem=%d]\n", nItem);
1413 HEADER_CopyHDItemForNotify(infoPtr, &hdNotify, phdi, bUnicode, &pvScratch);
1414 if (HEADER_SendNotifyWithHDItemT(hwnd, HDN_ITEMCHANGINGW, nItem, &hdNotify))
1416 Free(pvScratch);
1417 return FALSE;
1420 lpItem = &infoPtr->items[nItem];
1421 HEADER_StoreHDItemInHeader(lpItem, phdi->mask, phdi, bUnicode);
1423 if (phdi->mask & HDI_ORDER)
1424 if (phdi->iOrder >= 0 && phdi->iOrder < infoPtr->uNumItem)
1425 HEADER_ChangeItemOrder(infoPtr, nItem, phdi->iOrder);
1427 HEADER_SendNotifyWithHDItemT(hwnd, HDN_ITEMCHANGEDW, nItem, &hdNotify);
1429 HEADER_SetItemBounds (hwnd);
1431 InvalidateRect(hwnd, NULL, FALSE);
1433 Free(pvScratch);
1434 return TRUE;
1437 static inline LRESULT
1438 HEADER_SetUnicodeFormat (HWND hwnd, WPARAM wParam)
1440 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1441 BOOL bTemp = (infoPtr->nNotifyFormat == NFR_UNICODE);
1443 infoPtr->nNotifyFormat = ((BOOL)wParam ? NFR_UNICODE : NFR_ANSI);
1445 return bTemp;
1449 static LRESULT
1450 HEADER_Create (HWND hwnd, LPARAM lParam)
1452 HEADER_INFO *infoPtr;
1453 TEXTMETRICW tm;
1454 HFONT hOldFont;
1455 HDC hdc;
1457 infoPtr = (HEADER_INFO *)Alloc (sizeof(HEADER_INFO));
1458 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1460 infoPtr->hwndNotify = ((LPCREATESTRUCTA)lParam)->hwndParent;
1461 infoPtr->uNumItem = 0;
1462 infoPtr->hFont = 0;
1463 infoPtr->items = 0;
1464 infoPtr->order = 0;
1465 infoPtr->bRectsValid = FALSE;
1466 infoPtr->hcurArrow = LoadCursorW (0, (LPWSTR)IDC_ARROW);
1467 infoPtr->hcurDivider = LoadCursorW (COMCTL32_hModule, MAKEINTRESOURCEW(IDC_DIVIDER));
1468 infoPtr->hcurDivopen = LoadCursorW (COMCTL32_hModule, MAKEINTRESOURCEW(IDC_DIVIDEROPEN));
1469 infoPtr->bPressed = FALSE;
1470 infoPtr->bTracking = FALSE;
1471 infoPtr->iMoveItem = 0;
1472 infoPtr->himl = 0;
1473 infoPtr->iHotItem = -1;
1474 infoPtr->iHotDivider = -1;
1475 infoPtr->iMargin = 3*GetSystemMetrics(SM_CXEDGE);
1476 infoPtr->nNotifyFormat =
1477 SendMessageW (infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)hwnd, NF_QUERY);
1479 hdc = GetDC (0);
1480 hOldFont = SelectObject (hdc, GetStockObject (SYSTEM_FONT));
1481 GetTextMetricsW (hdc, &tm);
1482 infoPtr->nHeight = tm.tmHeight + VERT_BORDER;
1483 SelectObject (hdc, hOldFont);
1484 ReleaseDC (0, hdc);
1486 OpenThemeData(hwnd, themeClass);
1488 return 0;
1492 static LRESULT
1493 HEADER_Destroy (HWND hwnd)
1495 HTHEME theme = GetWindowTheme(hwnd);
1496 CloseThemeData(theme);
1497 return 0;
1500 static LRESULT
1501 HEADER_NCDestroy (HWND hwnd)
1503 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1504 HEADER_ITEM *lpItem;
1505 INT nItem;
1507 if (infoPtr->items) {
1508 lpItem = infoPtr->items;
1509 for (nItem = 0; nItem < infoPtr->uNumItem; nItem++, lpItem++) {
1510 Free(lpItem->pszText);
1512 Free (infoPtr->items);
1515 Free(infoPtr->order);
1517 if (infoPtr->himl)
1518 ImageList_Destroy (infoPtr->himl);
1520 SetWindowLongPtrW (hwnd, 0, 0);
1521 Free (infoPtr);
1523 return 0;
1527 static inline LRESULT
1528 HEADER_GetFont (HWND hwnd)
1530 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1532 return (LRESULT)infoPtr->hFont;
1536 static BOOL
1537 HEADER_IsDragDistance(const HEADER_INFO *infoPtr, const POINT *pt)
1539 /* Windows allows for a mouse movement before starting the drag. We use the
1540 * SM_CXDOUBLECLICK/SM_CYDOUBLECLICK as that distance.
1542 return (abs(infoPtr->ptLButtonDown.x - pt->x)>GetSystemMetrics(SM_CXDOUBLECLK) ||
1543 abs(infoPtr->ptLButtonDown.y - pt->y)>GetSystemMetrics(SM_CYDOUBLECLK));
1546 static LRESULT
1547 HEADER_LButtonDblClk (HWND hwnd, LPARAM lParam)
1549 POINT pt;
1550 UINT flags;
1551 INT nItem;
1553 pt.x = (short)LOWORD(lParam);
1554 pt.y = (short)HIWORD(lParam);
1555 HEADER_InternalHitTest (hwnd, &pt, &flags, &nItem);
1557 if ((GetWindowLongW (hwnd, GWL_STYLE) & HDS_BUTTONS) && (flags == HHT_ONHEADER))
1558 HEADER_SendNotifyWithHDItemT(hwnd, HDN_ITEMDBLCLICKW, nItem, NULL);
1559 else if ((flags == HHT_ONDIVIDER) || (flags == HHT_ONDIVOPEN))
1560 HEADER_SendNotifyWithHDItemT(hwnd, HDN_DIVIDERDBLCLICKW, nItem, NULL);
1562 return 0;
1566 static LRESULT
1567 HEADER_LButtonDown (HWND hwnd, LPARAM lParam)
1569 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1570 DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
1571 POINT pt;
1572 UINT flags;
1573 INT nItem;
1574 HDC hdc;
1576 pt.x = (short)LOWORD(lParam);
1577 pt.y = (short)HIWORD(lParam);
1578 HEADER_InternalHitTest (hwnd, &pt, &flags, &nItem);
1580 if ((dwStyle & HDS_BUTTONS) && (flags == HHT_ONHEADER)) {
1581 SetCapture (hwnd);
1582 infoPtr->bCaptured = TRUE;
1583 infoPtr->bPressed = TRUE;
1584 infoPtr->bDragging = FALSE;
1585 infoPtr->iMoveItem = nItem;
1586 infoPtr->ptLButtonDown = pt;
1588 infoPtr->items[nItem].bDown = TRUE;
1590 /* Send WM_CUSTOMDRAW */
1591 hdc = GetDC (hwnd);
1592 HEADER_RefreshItem (hwnd, nItem);
1593 ReleaseDC (hwnd, hdc);
1595 TRACE("Pressed item %d!\n", nItem);
1597 else if ((flags == HHT_ONDIVIDER) || (flags == HHT_ONDIVOPEN)) {
1598 INT iCurrWidth = infoPtr->items[nItem].cxy;
1599 if (!HEADER_SendNotifyWithIntFieldT(hwnd, HDN_BEGINTRACKW, nItem, HDI_WIDTH, iCurrWidth))
1601 SetCapture (hwnd);
1602 infoPtr->bCaptured = TRUE;
1603 infoPtr->bTracking = TRUE;
1604 infoPtr->iMoveItem = nItem;
1605 infoPtr->xTrackOffset = infoPtr->items[nItem].rect.right - pt.x;
1607 if (!(dwStyle & HDS_FULLDRAG)) {
1608 infoPtr->xOldTrack = infoPtr->items[nItem].rect.right;
1609 hdc = GetDC (hwnd);
1610 HEADER_DrawTrackLine (hwnd, hdc, infoPtr->xOldTrack);
1611 ReleaseDC (hwnd, hdc);
1614 TRACE("Begin tracking item %d!\n", nItem);
1618 return 0;
1622 static LRESULT
1623 HEADER_LButtonUp (HWND hwnd, LPARAM lParam)
1625 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1626 DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
1627 POINT pt;
1628 UINT flags;
1629 INT nItem;
1630 HDC hdc;
1632 pt.x = (INT)(SHORT)LOWORD(lParam);
1633 pt.y = (INT)(SHORT)HIWORD(lParam);
1634 HEADER_InternalHitTest (hwnd, &pt, &flags, &nItem);
1636 if (infoPtr->bPressed) {
1637 if (infoPtr->bDragging)
1639 HEADER_ITEM *lpItem = &infoPtr->items[infoPtr->iMoveItem];
1640 INT iNewOrder;
1642 ImageList_DragShowNolock(FALSE);
1643 ImageList_EndDrag();
1644 lpItem->bDown=FALSE;
1646 if (infoPtr->iHotDivider == -1)
1647 iNewOrder = -1;
1648 else if (infoPtr->iHotDivider == infoPtr->uNumItem)
1649 iNewOrder = infoPtr->uNumItem-1;
1650 else
1652 iNewOrder = HEADER_IndexToOrder(hwnd, infoPtr->iHotDivider);
1653 if (iNewOrder > lpItem->iOrder)
1654 iNewOrder--;
1657 if (iNewOrder != -1 &&
1658 !HEADER_SendNotifyWithIntFieldT(hwnd, HDN_ENDDRAG, infoPtr->iMoveItem, HDI_ORDER, iNewOrder))
1660 HEADER_ChangeItemOrder(infoPtr, infoPtr->iMoveItem, iNewOrder);
1661 infoPtr->bRectsValid = FALSE;
1662 InvalidateRect(hwnd, NULL, FALSE);
1664 else
1665 InvalidateRect(hwnd, &infoPtr->items[infoPtr->iMoveItem].rect, FALSE);
1667 HEADER_SetHotDivider(hwnd, FALSE, -1);
1669 else if (!(dwStyle&HDS_DRAGDROP) || !HEADER_IsDragDistance(infoPtr, &pt))
1671 infoPtr->items[infoPtr->iMoveItem].bDown = FALSE;
1672 hdc = GetDC (hwnd);
1673 HEADER_RefreshItem (hwnd, infoPtr->iMoveItem);
1674 ReleaseDC (hwnd, hdc);
1676 HEADER_SendNotifyWithHDItemT(hwnd, HDN_ITEMCLICKW, infoPtr->iMoveItem, NULL);
1679 TRACE("Released item %d!\n", infoPtr->iMoveItem);
1680 infoPtr->bPressed = FALSE;
1682 else if (infoPtr->bTracking) {
1683 INT iNewWidth = pt.x - infoPtr->items[infoPtr->iMoveItem].rect.left + infoPtr->xTrackOffset;
1684 if (iNewWidth < 0)
1685 iNewWidth = 0;
1686 TRACE("End tracking item %d!\n", infoPtr->iMoveItem);
1687 infoPtr->bTracking = FALSE;
1689 HEADER_SendNotifyWithIntFieldT(hwnd, HDN_ENDTRACKW, infoPtr->iMoveItem, HDI_WIDTH, iNewWidth);
1691 if (!(dwStyle & HDS_FULLDRAG)) {
1692 hdc = GetDC (hwnd);
1693 HEADER_DrawTrackLine (hwnd, hdc, infoPtr->xOldTrack);
1694 ReleaseDC (hwnd, hdc);
1697 if (!HEADER_SendNotifyWithIntFieldT(hwnd, HDN_ITEMCHANGINGW, infoPtr->iMoveItem, HDI_WIDTH, iNewWidth))
1699 infoPtr->items[infoPtr->iMoveItem].cxy = iNewWidth;
1700 HEADER_SendNotifyWithIntFieldT(hwnd, HDN_ITEMCHANGEDW, infoPtr->iMoveItem, HDI_WIDTH, iNewWidth);
1703 HEADER_SetItemBounds (hwnd);
1704 InvalidateRect(hwnd, NULL, TRUE);
1707 if (infoPtr->bCaptured) {
1708 infoPtr->bCaptured = FALSE;
1709 ReleaseCapture ();
1710 HEADER_SendSimpleNotify (hwnd, NM_RELEASEDCAPTURE);
1713 return 0;
1717 static LRESULT
1718 HEADER_NotifyFormat (HWND hwnd, WPARAM wParam, LPARAM lParam)
1720 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1722 switch (lParam)
1724 case NF_QUERY:
1725 return infoPtr->nNotifyFormat;
1727 case NF_REQUERY:
1728 infoPtr->nNotifyFormat =
1729 SendMessageW ((HWND)wParam, WM_NOTIFYFORMAT,
1730 (WPARAM)hwnd, (LPARAM)NF_QUERY);
1731 return infoPtr->nNotifyFormat;
1734 return 0;
1737 static LRESULT
1738 HEADER_MouseLeave (HWND hwnd)
1740 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1741 /* Reset hot-tracked item when mouse leaves control. */
1742 INT oldHotItem = infoPtr->iHotItem;
1743 HDC hdc = GetDC (hwnd);
1745 infoPtr->iHotItem = -1;
1746 if (oldHotItem != -1) HEADER_RefreshItem (hwnd, oldHotItem);
1747 ReleaseDC (hwnd, hdc);
1749 return 0;
1753 static LRESULT
1754 HEADER_MouseMove (HWND hwnd, LPARAM lParam)
1756 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1757 DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
1758 POINT pt;
1759 UINT flags;
1760 INT nItem, nWidth;
1761 HDC hdc;
1762 /* With theming, hottracking is always enabled */
1763 BOOL hotTrackEnabled =
1764 ((dwStyle & HDS_BUTTONS) && (dwStyle & HDS_HOTTRACK))
1765 || (GetWindowTheme (hwnd) != NULL);
1766 INT oldHotItem = infoPtr->iHotItem;
1768 pt.x = (INT)(SHORT)LOWORD(lParam);
1769 pt.y = (INT)(SHORT)HIWORD(lParam);
1770 HEADER_InternalHitTest (hwnd, &pt, &flags, &nItem);
1772 if (hotTrackEnabled) {
1773 if (flags & (HHT_ONHEADER | HHT_ONDIVIDER | HHT_ONDIVOPEN))
1774 infoPtr->iHotItem = nItem;
1775 else
1776 infoPtr->iHotItem = -1;
1779 if (infoPtr->bCaptured) {
1780 /* check if we should drag the header */
1781 if (infoPtr->bPressed && !infoPtr->bDragging && dwStyle&HDS_DRAGDROP
1782 && HEADER_IsDragDistance(infoPtr, &pt))
1784 if (!HEADER_SendNotifyWithHDItemT(hwnd, HDN_BEGINDRAG, infoPtr->iMoveItem, NULL))
1786 HIMAGELIST hDragItem = (HIMAGELIST)HEADER_CreateDragImage(hwnd, infoPtr->iMoveItem);
1787 if (hDragItem != NULL)
1789 HEADER_ITEM *lpItem = &infoPtr->items[infoPtr->iMoveItem];
1790 TRACE("Starting item drag\n");
1791 ImageList_BeginDrag(hDragItem, 0, pt.x - lpItem->rect.left, 0);
1792 ImageList_DragShowNolock(TRUE);
1793 ImageList_Destroy(hDragItem);
1794 infoPtr->bDragging = TRUE;
1799 if (infoPtr->bDragging)
1801 POINT drag;
1802 drag.x = pt.x;
1803 drag.y = 0;
1804 ClientToScreen(hwnd, &drag);
1805 ImageList_DragMove(drag.x, drag.y);
1806 HEADER_SetHotDivider(hwnd, TRUE, lParam);
1809 if (infoPtr->bPressed && !infoPtr->bDragging) {
1810 BOOL oldState = infoPtr->items[infoPtr->iMoveItem].bDown;
1811 if ((nItem == infoPtr->iMoveItem) && (flags == HHT_ONHEADER))
1812 infoPtr->items[infoPtr->iMoveItem].bDown = TRUE;
1813 else
1814 infoPtr->items[infoPtr->iMoveItem].bDown = FALSE;
1815 if (oldState != infoPtr->items[infoPtr->iMoveItem].bDown) {
1816 hdc = GetDC (hwnd);
1817 HEADER_RefreshItem (hwnd, infoPtr->iMoveItem);
1818 ReleaseDC (hwnd, hdc);
1821 TRACE("Moving pressed item %d!\n", infoPtr->iMoveItem);
1823 else if (infoPtr->bTracking) {
1824 if (dwStyle & HDS_FULLDRAG) {
1825 HEADER_ITEM *lpItem = &infoPtr->items[infoPtr->iMoveItem];
1826 nWidth = pt.x - lpItem->rect.left + infoPtr->xTrackOffset;
1827 if (!HEADER_SendNotifyWithIntFieldT(hwnd, HDN_ITEMCHANGINGW, infoPtr->iMoveItem, HDI_WIDTH, nWidth))
1829 INT nOldWidth = lpItem->rect.right - lpItem->rect.left;
1830 RECT rcClient;
1831 RECT rcScroll;
1833 if (nWidth < 0) nWidth = 0;
1834 infoPtr->items[infoPtr->iMoveItem].cxy = nWidth;
1835 HEADER_SetItemBounds(hwnd);
1837 GetClientRect(hwnd, &rcClient);
1838 rcScroll = rcClient;
1839 rcScroll.left = lpItem->rect.left + nOldWidth;
1840 ScrollWindowEx(hwnd, nWidth - nOldWidth, 0, &rcScroll, &rcClient, NULL, NULL, 0);
1841 InvalidateRect(hwnd, &lpItem->rect, FALSE);
1842 UpdateWindow(hwnd);
1844 HEADER_SendNotifyWithIntFieldT(hwnd, HDN_ITEMCHANGEDW, infoPtr->iMoveItem, HDI_WIDTH, nWidth);
1847 else {
1848 INT iTrackWidth;
1849 hdc = GetDC (hwnd);
1850 HEADER_DrawTrackLine (hwnd, hdc, infoPtr->xOldTrack);
1851 infoPtr->xOldTrack = pt.x + infoPtr->xTrackOffset;
1852 if (infoPtr->xOldTrack < infoPtr->items[infoPtr->iMoveItem].rect.left)
1853 infoPtr->xOldTrack = infoPtr->items[infoPtr->iMoveItem].rect.left;
1854 HEADER_DrawTrackLine (hwnd, hdc, infoPtr->xOldTrack);
1855 ReleaseDC (hwnd, hdc);
1856 iTrackWidth = infoPtr->xOldTrack - infoPtr->items[infoPtr->iMoveItem].rect.left;
1857 /* FIXME: should stop tracking if HDN_TRACK returns TRUE */
1858 HEADER_SendNotifyWithIntFieldT(hwnd, HDN_TRACKW, infoPtr->iMoveItem, HDI_WIDTH, iTrackWidth);
1861 TRACE("Tracking item %d!\n", infoPtr->iMoveItem);
1865 if (hotTrackEnabled) {
1866 TRACKMOUSEEVENT tme;
1867 if (oldHotItem != infoPtr->iHotItem && !infoPtr->bDragging) {
1868 hdc = GetDC (hwnd);
1869 if (oldHotItem != -1) HEADER_RefreshItem (hwnd, oldHotItem);
1870 if (infoPtr->iHotItem != -1) HEADER_RefreshItem (hwnd, infoPtr->iHotItem);
1871 ReleaseDC (hwnd, hdc);
1873 tme.cbSize = sizeof( tme );
1874 tme.dwFlags = TME_LEAVE;
1875 tme.hwndTrack = hwnd;
1876 TrackMouseEvent( &tme );
1879 return 0;
1883 static LRESULT
1884 HEADER_Paint (HWND hwnd, WPARAM wParam)
1886 HDC hdc;
1887 PAINTSTRUCT ps;
1889 hdc = wParam==0 ? BeginPaint (hwnd, &ps) : (HDC)wParam;
1890 HEADER_Refresh (hwnd, hdc);
1891 if(!wParam)
1892 EndPaint (hwnd, &ps);
1893 return 0;
1897 static LRESULT
1898 HEADER_RButtonUp (HWND hwnd, LPARAM lParam)
1900 BOOL bRet;
1901 POINT pt;
1903 pt.x = (short)LOWORD(lParam);
1904 pt.y = (short)HIWORD(lParam);
1906 /* Send a Notify message */
1907 bRet = HEADER_SendSimpleNotify (hwnd, NM_RCLICK);
1909 /* Change to screen coordinate for WM_CONTEXTMENU */
1910 ClientToScreen(hwnd, &pt);
1912 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
1913 SendMessageW( hwnd, WM_CONTEXTMENU, (WPARAM) hwnd, MAKELPARAM(pt.x, pt.y));
1915 return bRet;
1919 static LRESULT
1920 HEADER_SetCursor (HWND hwnd, LPARAM lParam)
1922 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1923 POINT pt;
1924 UINT flags;
1925 INT nItem;
1927 TRACE("code=0x%X id=0x%X\n", LOWORD(lParam), HIWORD(lParam));
1929 GetCursorPos (&pt);
1930 ScreenToClient (hwnd, &pt);
1932 HEADER_InternalHitTest (hwnd, &pt, &flags, &nItem);
1934 if (flags == HHT_ONDIVIDER)
1935 SetCursor (infoPtr->hcurDivider);
1936 else if (flags == HHT_ONDIVOPEN)
1937 SetCursor (infoPtr->hcurDivopen);
1938 else
1939 SetCursor (infoPtr->hcurArrow);
1941 return 0;
1945 static LRESULT
1946 HEADER_SetFont (HWND hwnd, WPARAM wParam, LPARAM lParam)
1948 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1949 TEXTMETRICW tm;
1950 HFONT hFont, hOldFont;
1951 HDC hdc;
1953 infoPtr->hFont = (HFONT)wParam;
1955 hFont = infoPtr->hFont ? infoPtr->hFont : GetStockObject (SYSTEM_FONT);
1957 hdc = GetDC (0);
1958 hOldFont = SelectObject (hdc, hFont);
1959 GetTextMetricsW (hdc, &tm);
1960 infoPtr->nHeight = tm.tmHeight + VERT_BORDER;
1961 SelectObject (hdc, hOldFont);
1962 ReleaseDC (0, hdc);
1964 infoPtr->bRectsValid = FALSE;
1966 if (lParam) {
1967 InvalidateRect(hwnd, NULL, FALSE);
1970 return 0;
1973 static LRESULT HEADER_SetRedraw(HWND hwnd, WPARAM wParam, LPARAM lParam)
1975 /* ignoring the InvalidateRect calls is handled by user32. But some apps expect
1976 * that we invalidate the header and this has to be done manually */
1977 LRESULT ret;
1979 ret = DefWindowProcW(hwnd, WM_SETREDRAW, wParam, lParam);
1980 if (wParam)
1981 InvalidateRect(hwnd, NULL, TRUE);
1982 return ret;
1985 /* Update the theme handle after a theme change */
1986 static LRESULT HEADER_ThemeChanged(HWND hwnd)
1988 HTHEME theme = GetWindowTheme(hwnd);
1989 CloseThemeData(theme);
1990 OpenThemeData(hwnd, themeClass);
1991 InvalidateRect(hwnd, NULL, FALSE);
1992 return 0;
1996 static LRESULT WINAPI
1997 HEADER_WindowProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
1999 TRACE("hwnd=%p msg=%x wparam=%lx lParam=%lx\n", hwnd, msg, wParam, lParam);
2000 if (!HEADER_GetInfoPtr (hwnd) && (msg != WM_CREATE))
2001 return DefWindowProcW (hwnd, msg, wParam, lParam);
2002 switch (msg) {
2003 /* case HDM_CLEARFILTER: */
2005 case HDM_CREATEDRAGIMAGE:
2006 return HEADER_CreateDragImage (hwnd, wParam);
2008 case HDM_DELETEITEM:
2009 return HEADER_DeleteItem (hwnd, wParam);
2011 /* case HDM_EDITFILTER: */
2013 case HDM_GETBITMAPMARGIN:
2014 return HEADER_GetBitmapMargin(hwnd);
2016 case HDM_GETIMAGELIST:
2017 return HEADER_GetImageList (hwnd);
2019 case HDM_GETITEMA:
2020 case HDM_GETITEMW:
2021 return HEADER_GetItemT (hwnd, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_GETITEMW);
2023 case HDM_GETITEMCOUNT:
2024 return HEADER_GetItemCount (hwnd);
2026 case HDM_GETITEMRECT:
2027 return HEADER_GetItemRect (hwnd, wParam, lParam);
2029 case HDM_GETORDERARRAY:
2030 return HEADER_GetOrderArray(hwnd, wParam, lParam);
2032 case HDM_GETUNICODEFORMAT:
2033 return HEADER_GetUnicodeFormat (hwnd);
2035 case HDM_HITTEST:
2036 return HEADER_HitTest (hwnd, lParam);
2038 case HDM_INSERTITEMA:
2039 case HDM_INSERTITEMW:
2040 return HEADER_InsertItemT (hwnd, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_INSERTITEMW);
2042 case HDM_LAYOUT:
2043 return HEADER_Layout (hwnd, lParam);
2045 case HDM_ORDERTOINDEX:
2046 return HEADER_OrderToIndex(hwnd, wParam);
2048 case HDM_SETBITMAPMARGIN:
2049 return HEADER_SetBitmapMargin(hwnd, wParam);
2051 /* case HDM_SETFILTERCHANGETIMEOUT: */
2053 case HDM_SETHOTDIVIDER:
2054 return HEADER_SetHotDivider(hwnd, wParam, lParam);
2056 case HDM_SETIMAGELIST:
2057 return HEADER_SetImageList (hwnd, (HIMAGELIST)lParam);
2059 case HDM_SETITEMA:
2060 case HDM_SETITEMW:
2061 return HEADER_SetItemT (hwnd, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_SETITEMW);
2063 case HDM_SETORDERARRAY:
2064 return HEADER_SetOrderArray(hwnd, wParam, lParam);
2066 case HDM_SETUNICODEFORMAT:
2067 return HEADER_SetUnicodeFormat (hwnd, wParam);
2069 case WM_CREATE:
2070 return HEADER_Create (hwnd, lParam);
2072 case WM_DESTROY:
2073 return HEADER_Destroy (hwnd);
2075 case WM_NCDESTROY:
2076 return HEADER_NCDestroy (hwnd);
2078 case WM_ERASEBKGND:
2079 return 1;
2081 case WM_GETDLGCODE:
2082 return DLGC_WANTTAB | DLGC_WANTARROWS;
2084 case WM_GETFONT:
2085 return HEADER_GetFont (hwnd);
2087 case WM_LBUTTONDBLCLK:
2088 return HEADER_LButtonDblClk (hwnd, lParam);
2090 case WM_LBUTTONDOWN:
2091 return HEADER_LButtonDown (hwnd, lParam);
2093 case WM_LBUTTONUP:
2094 return HEADER_LButtonUp (hwnd, lParam);
2096 case WM_MOUSELEAVE:
2097 return HEADER_MouseLeave (hwnd);
2099 case WM_MOUSEMOVE:
2100 return HEADER_MouseMove (hwnd, lParam);
2102 case WM_NOTIFYFORMAT:
2103 return HEADER_NotifyFormat (hwnd, wParam, lParam);
2105 case WM_SIZE:
2106 return HEADER_Size (hwnd);
2108 case WM_THEMECHANGED:
2109 return HEADER_ThemeChanged (hwnd);
2111 case WM_PRINTCLIENT:
2112 case WM_PAINT:
2113 return HEADER_Paint (hwnd, wParam);
2115 case WM_RBUTTONUP:
2116 return HEADER_RButtonUp (hwnd, lParam);
2118 case WM_SETCURSOR:
2119 return HEADER_SetCursor (hwnd, lParam);
2121 case WM_SETFONT:
2122 return HEADER_SetFont (hwnd, wParam, lParam);
2124 case WM_SETREDRAW:
2125 return HEADER_SetRedraw(hwnd, wParam, lParam);
2127 default:
2128 if ((msg >= WM_USER) && (msg < WM_APP) && !COMCTL32_IsReflectedMessage(msg))
2129 ERR("unknown msg %04x wp=%04lx lp=%08lx\n",
2130 msg, wParam, lParam );
2131 return DefWindowProcW(hwnd, msg, wParam, lParam);
2136 VOID
2137 HEADER_Register (void)
2139 WNDCLASSW wndClass;
2141 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
2142 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
2143 wndClass.lpfnWndProc = HEADER_WindowProc;
2144 wndClass.cbClsExtra = 0;
2145 wndClass.cbWndExtra = sizeof(HEADER_INFO *);
2146 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
2147 wndClass.lpszClassName = WC_HEADERW;
2149 RegisterClassW (&wndClass);
2153 VOID
2154 HEADER_Unregister (void)
2156 UnregisterClassW (WC_HEADERW, NULL);